Skip to content

Commit

Permalink
LLVMCodeBuilder: Optimize number and string const comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Jan 11, 2025
1 parent bce615b commit a3de164
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dev/engine/internal/llvm/llvmcodebuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,16 @@ llvm::Value *LLVMCodeBuilder::createComparison(LLVMRegister *arg1, LLVMRegister
optNumberBool = 1; // operand 1 was bool
}

// Optimize number and string constant comparison
// TODO: GT and LT comparison can be optimized here (e. g. by checking the string constant characters and comparing with numbers and .+-e)
if (type == Comparison::EQ) {
if (type1 == Compiler::StaticType::Number && type2 == Compiler::StaticType::String && arg2->isConst() && !arg2->constValue().isValidNumber())
return m_builder.getInt1(false);

if (type1 == Compiler::StaticType::String && type2 == Compiler::StaticType::Number && arg1->isConst() && !arg1->constValue().isValidNumber())
return m_builder.getInt1(false);
}

if (type1 != type2 || type1 == Compiler::StaticType::Unknown || type2 == Compiler::StaticType::Unknown) {
// If the types are different or at least one of them
// is unknown, we must use value functions
Expand Down
2 changes: 2 additions & 0 deletions test/dev/llvm/llvmcodebuilder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ TEST_F(LLVMCodeBuilderTest, GreaterAndLowerThanComparison)
runOpTest(type, "1", 0);
runOpTest(type, 0, "test");
runOpTest(type, "test", 0);
runOpTest(type, 55, "abc");
runOpTest(type, "abc", 55);

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

0 comments on commit a3de164

Please sign in to comment.