From 2de7b22f2b245ab2cf4efffab500a88511972bd8 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 28 Nov 2018 16:44:26 -0800 Subject: Start to implement #1764 (#1776) This adds a first instance of the rules discussed in #1764 , specifically, x == y || x > y => x >= y --- src/passes/OptimizeInstructions.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index e0c7217a5..1dd05dd0a 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -516,6 +516,12 @@ struct OptimizeInstructions : public WalkerPassop == OrInt32) { + if (auto* ret = combineOr(binary)) { + return ret; + } + } // relation/comparisons allow for math optimizations if (binary->isRelational()) { if (auto* ret = optimizeRelational(binary)) { @@ -979,6 +985,34 @@ private: } } + // We can combine `or` operations, e.g. + // (x > y) | (x == y) ==> x >= y + Expression* combineOr(Binary* binary) { + assert(binary->op == OrInt32); + if (auto* left = binary->left->dynCast()) { + if (auto* right = binary->right->dynCast()) { + if (left->op != right->op && + ExpressionAnalyzer::equal(left->left, right->left) && + ExpressionAnalyzer::equal(left->right, right->right) && + !EffectAnalyzer(getPassOptions(), left->left).hasSideEffects() && + !EffectAnalyzer(getPassOptions(), left->right).hasSideEffects()) { + switch (left->op) { + // (x > y) | (x == y) ==> x >= y + case EqInt32: { + if (right->op == GtSInt32) { + left->op = GeSInt32; + return left; + } + break; + } + default: {} + } + } + } + } + return nullptr; + } + // fold constant factors into the offset void optimizeMemoryAccess(Expression*& ptr, Address& offset) { // ptr may be a const, but it isn't worth folding that in (we still have a const); in fact, -- cgit v1.2.3