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)
| 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. |
| 77 | func 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 | } |