Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transform: optimize string comparisons against ""
This optimizes a common pattern like: if s != "" { ... } to: if len(s) != 0 { ... } This avoids a runtime call and thus produces slightly better code.
- Loading branch information
1 parent
13db2c1
commit f9865a0
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" | ||
target triple = "armv7m-none-eabi" | ||
|
||
@zeroString = constant [0 x i8] zeroinitializer | ||
|
||
declare i1 @runtime.stringEqual(i8*, i32, i8*, i32, i8*, i8*) | ||
|
||
define i1 @main.stringCompareEqualConstantZero(i8* %s1.data, i32 %s1.len, i8* %context, i8* %parentHandle) { | ||
entry: | ||
%0 = call i1 @runtime.stringEqual(i8* %s1.data, i32 %s1.len, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @zeroString, i32 0, i32 0), i32 0, i8* undef, i8* null) | ||
ret i1 %0 | ||
} | ||
|
||
define i1 @main.stringCompareUnequalConstantZero(i8* %s1.data, i32 %s1.len, i8* %context, i8* %parentHandle) { | ||
entry: | ||
%0 = call i1 @runtime.stringEqual(i8* %s1.data, i32 %s1.len, i8* getelementptr inbounds ([0 x i8], [0 x i8]* @zeroString, i32 0, i32 0), i32 0, i8* undef, i8* null) | ||
%1 = xor i1 %0, true | ||
ret i1 %1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" | ||
target triple = "armv7m-none-eabi" | ||
|
||
@zeroString = constant [0 x i8] zeroinitializer | ||
|
||
declare i1 @runtime.stringEqual(i8*, i32, i8*, i32, i8*, i8*) | ||
|
||
define i1 @main.stringCompareEqualConstantZero(i8* %s1.data, i32 %s1.len, i8* %context, i8* %parentHandle) { | ||
entry: | ||
%0 = icmp eq i32 %s1.len, 0 | ||
ret i1 %0 | ||
} | ||
|
||
define i1 @main.stringCompareUnequalConstantZero(i8* %s1.data, i32 %s1.len, i8* %context, i8* %parentHandle) { | ||
entry: | ||
%0 = icmp eq i32 %s1.len, 0 | ||
%1 = xor i1 %0, true | ||
ret i1 %1 | ||
} |