fix incorrect starting value for optimized allocations in a loop

This commit is contained in:
Jaden Weiss
2019-11-13 10:05:01 -05:00
committed by Ayke
parent acdaaa17d8
commit 93961f9d41
4 changed files with 36 additions and 2 deletions
+5
View File
@@ -69,8 +69,13 @@ func OptimizeAllocs(mod llvm.Module) {
sizeInWords := (size + uint64(alignment) - 1) / uint64(alignment)
allocaType := llvm.ArrayType(mod.Context().IntType(alignment*8), int(sizeInWords))
alloca := builder.CreateAlloca(allocaType, "stackalloc.alloca")
// Zero the allocation inside the block where the value was originally allocated.
zero := llvm.ConstNull(alloca.Type().ElementType())
builder.SetInsertPointBefore(bitcast)
builder.CreateStore(zero, alloca)
// Replace heap alloc bitcast with stack alloc bitcast.
stackalloc := builder.CreateBitCast(alloca, bitcast.Type(), "stackalloc")
bitcast.ReplaceAllUsesWith(stackalloc)
if heapalloc != bitcast {