transform.go raw

   1  // Package transform contains transformation passes for the Moxie compiler.
   2  // These transformation passes may be optimization passes or lowering passes.
   3  //
   4  // Optimization passes transform the IR in such a way that they increase the
   5  // performance of the generated code and/or help the LLVM optimizer better do
   6  // its job by simplifying the IR. This usually means that certain
   7  // Moxie-specific runtime calls are removed or replaced with something simpler
   8  // if that is a valid operation.
   9  //
  10  // Lowering passes are usually required to run. One example is the interface
  11  // lowering pass, which replaces stub runtime calls to get an interface method
  12  // with the method implementation (either a direct call or a thunk).
  13  package transform
  14  
  15  import (
  16  	"moxie/compileopts"
  17  	"tinygo.org/x/go-llvm"
  18  )
  19  
  20  // AddStandardAttributes is a helper function to add standard function
  21  // attributes to a function. For example, it adds optsize when requested from
  22  // the -opt= compiler flag.
  23  func AddStandardAttributes(fn llvm.Value, config *compileopts.Config) {
  24  	ctx := fn.Type().Context()
  25  	_, _, sizeLevel := config.OptLevel()
  26  	if sizeLevel >= 1 {
  27  		fn.AddFunctionAttr(ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
  28  	}
  29  	if sizeLevel >= 2 {
  30  		fn.AddFunctionAttr(ctx.CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0))
  31  	}
  32  	if config.CPU() != "" {
  33  		fn.AddFunctionAttr(ctx.CreateStringAttribute("target-cpu", config.CPU()))
  34  	}
  35  	if config.Features() != "" {
  36  		fn.AddFunctionAttr(ctx.CreateStringAttribute("target-features", config.Features()))
  37  	}
  38  }
  39