mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 11:13:27 +00:00
compiler: align and zero-initialize stack allocated values
This commit is contained in:
@@ -139,9 +139,12 @@ func (c *Compiler) OptimizeAllocs() {
|
|||||||
// promote it to a SSA value.
|
// promote it to a SSA value.
|
||||||
fn := bitcast.InstructionParent().Parent()
|
fn := bitcast.InstructionParent().Parent()
|
||||||
c.builder.SetInsertPointBefore(fn.EntryBasicBlock().FirstInstruction())
|
c.builder.SetInsertPointBefore(fn.EntryBasicBlock().FirstInstruction())
|
||||||
allocaType := llvm.ArrayType(llvm.Int8Type(), int(size))
|
alignment := c.targetData.ABITypeAlignment(c.i8ptrType)
|
||||||
// TODO: alignment?
|
sizeInWords := (size + uint64(alignment) - 1) / uint64(alignment)
|
||||||
|
allocaType := llvm.ArrayType(c.ctx.IntType(alignment*8), int(sizeInWords))
|
||||||
alloca := c.builder.CreateAlloca(allocaType, "stackalloc.alloca")
|
alloca := c.builder.CreateAlloca(allocaType, "stackalloc.alloca")
|
||||||
|
zero, _ := c.getZeroValue(alloca.Type().ElementType())
|
||||||
|
c.builder.CreateStore(zero, alloca)
|
||||||
stackalloc := c.builder.CreateBitCast(alloca, bitcast.Type(), "stackalloc")
|
stackalloc := c.builder.CreateBitCast(alloca, bitcast.Type(), "stackalloc")
|
||||||
bitcast.ReplaceAllUsesWith(stackalloc)
|
bitcast.ReplaceAllUsesWith(stackalloc)
|
||||||
if heapalloc != bitcast {
|
if heapalloc != bitcast {
|
||||||
|
|||||||
Reference in New Issue
Block a user