diff options
author | Ayke van Laethem <[email protected]> | 2021-11-29 14:51:49 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-06-16 07:59:21 +0200 |
commit | 79ba6a50c3e125db4918a4b3b51f57380045973d (patch) | |
tree | 2700d2740b38d14395a788f0c28b5dd5343b2752 /compiler/defer.go | |
parent | 2fb5174910fd9cd38d592226e4660c99ca276ebb (diff) | |
download | tinygo-79ba6a50c3e125db4918a4b3b51f57380045973d.tar.gz tinygo-79ba6a50c3e125db4918a4b3b51f57380045973d.zip |
compiler: insert basic blocks at an appropriate location
For example, this commit moves the 'throw' branch of an assertion (nil
check, slice index check, etc) to the end of the function while
inserting the "continue" branch right after the insert location. This
makes the resulting IR easier to follow.
For some reason, this also reduces code size a bit on average. The
TinyGo smoke tests saw a reduction of 0.22%, mainly from WebAssembly.
The drivers repo saw little average change in code size (-0.01%).
This commit also adds a few compiler tests for the defer keyword.
Diffstat (limited to 'compiler/defer.go')
-rw-r--r-- | compiler/defer.go | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/compiler/defer.go b/compiler/defer.go index 1207ce649..235ffaab1 100644 --- a/compiler/defer.go +++ b/compiler/defer.go @@ -15,6 +15,7 @@ package compiler import ( "go/types" + "strconv" "github.com/tinygo-org/tinygo/compiler/llvmutil" "golang.org/x/tools/go/ssa" @@ -248,11 +249,11 @@ func (b *builder) createRunDefers() { // } // } - // Create loop. - loophead := b.ctx.AddBasicBlock(b.llvmFn, "rundefers.loophead") - loop := b.ctx.AddBasicBlock(b.llvmFn, "rundefers.loop") - unreachable := b.ctx.AddBasicBlock(b.llvmFn, "rundefers.default") - end := b.ctx.AddBasicBlock(b.llvmFn, "rundefers.end") + // Create loop, in the order: loophead, loop, callback0, callback1, ..., unreachable, end. + end := b.insertBasicBlock("rundefers.end") + unreachable := b.ctx.InsertBasicBlock(end, "rundefers.default") + loop := b.ctx.InsertBasicBlock(unreachable, "rundefers.loop") + loophead := b.ctx.InsertBasicBlock(loop, "rundefers.loophead") b.CreateBr(loophead) // Create loop head: @@ -284,7 +285,7 @@ func (b *builder) createRunDefers() { // Create switch case, for example: // case 0: // // run first deferred call - block := b.ctx.AddBasicBlock(b.llvmFn, "rundefers.callback") + block := b.insertBasicBlock("rundefers.callback" + strconv.Itoa(i)) sw.AddCase(llvm.ConstInt(b.uintptrType, uint64(i), false), block) b.SetInsertPointAtEnd(block) switch callback := callback.(type) { |