MCPcopy Index your code
hub / github.com/tinygo-org/tinygo / OptimizeStringEqual

Function OptimizeStringEqual

transform/rtcalls.go:77–100  ·  view source on GitHub ↗

OptimizeStringEqual transforms runtime.stringEqual(...) calls into simple integer comparisons if at least one of the sides of the comparison is zero. Ths converts str == "" into len(str) == 0 and "" == "" into false.

(mod llvm.Module)

Source from the content-addressed store, hash-verified

75// integer comparisons if at least one of the sides of the comparison is zero.
76// Ths converts str == "" into len(str) == 0 and "" == "" into false.
77func OptimizeStringEqual(mod llvm.Module) {
78 stringEqual := mod.NamedFunction("runtime.stringEqual")
79 if stringEqual.IsNil() {
80 // nothing to optimize
81 return
82 }
83
84 builder := mod.Context().NewBuilder()
85 defer builder.Dispose()
86
87 for _, call := range getUses(stringEqual) {
88 str1len := call.Operand(1)
89 str2len := call.Operand(3)
90
91 zero := llvm.ConstInt(str1len.Type(), 0, false)
92 if str1len == zero || str2len == zero {
93 builder.SetInsertPointBefore(call)
94 icmp := builder.CreateICmp(llvm.IntEQ, str1len, str2len, "")
95 call.ReplaceAllUsesWith(icmp)
96 call.EraseFromParentAsInstruction()
97 continue
98 }
99 }
100}

Callers 2

TestOptimizeStringEqualFunction · 0.92
OptimizeFunction · 0.85

Calls 4

getUsesFunction · 0.85
IsNilMethod · 0.80
ContextMethod · 0.65
TypeMethod · 0.45

Tested by 1

TestOptimizeStringEqualFunction · 0.74