Skip to content

Commit a3de164

Browse files
committed
LLVMCodeBuilder: Optimize number and string const comparison
1 parent bce615b commit a3de164

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/dev/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,16 @@ llvm::Value *LLVMCodeBuilder::createComparison(LLVMRegister *arg1, LLVMRegister
23472347
optNumberBool = 1; // operand 1 was bool
23482348
}
23492349

2350+
// Optimize number and string constant comparison
2351+
// TODO: GT and LT comparison can be optimized here (e. g. by checking the string constant characters and comparing with numbers and .+-e)
2352+
if (type == Comparison::EQ) {
2353+
if (type1 == Compiler::StaticType::Number && type2 == Compiler::StaticType::String && arg2->isConst() && !arg2->constValue().isValidNumber())
2354+
return m_builder.getInt1(false);
2355+
2356+
if (type1 == Compiler::StaticType::String && type2 == Compiler::StaticType::Number && arg1->isConst() && !arg1->constValue().isValidNumber())
2357+
return m_builder.getInt1(false);
2358+
}
2359+
23502360
if (type1 != type2 || type1 == Compiler::StaticType::Unknown || type2 == Compiler::StaticType::Unknown) {
23512361
// If the types are different or at least one of them
23522362
// is unknown, we must use value functions

test/dev/llvm/llvmcodebuilder_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,8 @@ TEST_F(LLVMCodeBuilderTest, GreaterAndLowerThanComparison)
983983
runOpTest(type, "1", 0);
984984
runOpTest(type, 0, "test");
985985
runOpTest(type, "test", 0);
986+
runOpTest(type, 55, "abc");
987+
runOpTest(type, "abc", 55);
986988

987989
static const double inf = std::numeric_limits<double>::infinity();
988990
static const double nan = std::numeric_limits<double>::quiet_NaN();

0 commit comments

Comments
 (0)