diff --git a/compiler/symbol.go b/compiler/symbol.go index b4f47d26..469066af 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -327,13 +327,20 @@ func getParams(sig *types.Signature) []*types.Var { // addStandardDeclaredAttributes adds attributes that are set for any function, // whether declared or defined. func (c *compilerContext) addStandardDeclaredAttributes(llvmFn llvm.Value) { - if c.SizeLevel >= 2 { + if c.SizeLevel >= 1 { // Set the "optsize" attribute to make slightly smaller binaries at the - // cost of some performance. + // cost of minimal performance loss (-Os in Clang). kind := llvm.AttributeKindID("optsize") attr := c.ctx.CreateEnumAttribute(kind, 0) llvmFn.AddFunctionAttr(attr) } + if c.SizeLevel >= 2 { + // Set the "minsize" attribute to reduce code size even further, + // regardless of performance loss (-Oz in Clang). + kind := llvm.AttributeKindID("minsize") + attr := c.ctx.CreateEnumAttribute(kind, 0) + llvmFn.AddFunctionAttr(attr) + } } // addStandardDefinedAttributes adds the set of attributes that are added to diff --git a/stacksize/stacksize.go b/stacksize/stacksize.go index 2b111ddc..ee5ca472 100644 --- a/stacksize/stacksize.go +++ b/stacksize/stacksize.go @@ -180,8 +180,8 @@ func CallGraph(f *elf.File, callsIndirectFunction []string) (map[string][]*CallN // used for getting a function pointer isCall = false case elf.R_ARM_ABS32: - // used in the reset vector for pointers - isCall = false + // when compiling with -Oz (minsize), used for calling + isCall = true default: return nil, fmt.Errorf("unknown relocation: %s", relocType) } diff --git a/transform/transform.go b/transform/transform.go index 441f9efa..3027f913 100644 --- a/transform/transform.go +++ b/transform/transform.go @@ -22,7 +22,10 @@ import ( // the -opt= compiler flag. func AddStandardAttributes(fn llvm.Value, config *compileopts.Config) { _, sizeLevel, _ := config.OptLevels() - if sizeLevel >= 2 { + if sizeLevel >= 1 { fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0)) } + if sizeLevel >= 2 { + fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0)) + } }