diff options
author | Ayke van Laethem <[email protected]> | 2022-05-30 14:19:42 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2022-05-30 20:39:42 +0200 |
commit | 9246899b309110a1fea87abbe5b4d12bb8a71fa4 (patch) | |
tree | 00791c4e982c7a5894109f23b13261a05b67762e /transform | |
parent | 04ace4de5f63b4c78f17183a2b816e6949e690d8 (diff) | |
download | tinygo-9246899b309110a1fea87abbe5b4d12bb8a71fa4.tar.gz tinygo-9246899b309110a1fea87abbe5b4d12bb8a71fa4.zip |
builder: move some code to transform package
The transform package is the more appropriate location for package-level
optimizations, to match `transform.Optimize` for whole-program
optimizations.
This is just a refactor, to make later changes easier to read.
Diffstat (limited to 'transform')
-rw-r--r-- | transform/optimizer.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/transform/optimizer.go b/transform/optimizer.go index 8e2ef7a83..66ad43f4b 100644 --- a/transform/optimizer.go +++ b/transform/optimizer.go @@ -11,6 +11,34 @@ import ( "tinygo.org/x/go-llvm" ) +// OptimizePackage runs optimization passes over the LLVM module for the given +// Go package. +func OptimizePackage(mod llvm.Module, config *compileopts.Config) { + optLevel, sizeLevel, _ := config.OptLevels() + + // Run function passes for each function in the module. + // These passes are intended to be run on each function right + // after they're created to reduce IR size (and maybe also for + // cache locality to improve performance), but for now they're + // run here for each function in turn. Maybe this can be + // improved in the future. + builder := llvm.NewPassManagerBuilder() + defer builder.Dispose() + builder.SetOptLevel(optLevel) + builder.SetSizeLevel(sizeLevel) + funcPasses := llvm.NewFunctionPassManagerForModule(mod) + defer funcPasses.Dispose() + builder.PopulateFunc(funcPasses) + funcPasses.InitializeFunc() + for fn := mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) { + if fn.IsDeclaration() { + continue + } + funcPasses.RunFunc(fn) + } + funcPasses.FinalizeFunc() +} + // Optimize runs a number of optimization and transformation passes over the // given module. Some passes are specific to TinyGo, others are generic LLVM // passes. You can set a preferred performance (0-3) and size (0-2) level and |