diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index d3c3f26f6..106bfea19 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1430,6 +1430,7 @@ private: Expression* optimizeSelect(Select* curr) { using namespace Match; + using namespace Abstract; Builder builder(*getModule()); curr->condition = optimizeBoolean(curr->condition); { @@ -1482,6 +1483,29 @@ private: } } { + // Simplify x < 0 ? -1 : 1 or x >= 0 ? 1 : -1 to + // i32(x) >> 31 | 1 + // i64(x) >> 63 | 1 + Binary* bin; + if (matches( + curr, + select(ival(-1), ival(1), binary(&bin, LtS, any(), ival(0)))) || + matches( + curr, + select(ival(1), ival(-1), binary(&bin, GeS, any(), ival(0))))) { + auto c = bin->right->cast<Const>(); + auto type = curr->ifTrue->type; + if (type == c->type) { + bin->type = type; + bin->op = Abstract::getBinary(type, ShrS); + c->value = Literal::makeFromInt32(type.getByteSize() * 8 - 1, type); + curr->ifTrue->cast<Const>()->value = Literal::makeOne(type); + return builder.makeBinary( + Abstract::getBinary(type, Or), bin, curr->ifTrue); + } + } + } + { // Sides are identical, fold Expression *ifTrue, *ifFalse, *c; if (matches(curr, select(any(&ifTrue), any(&ifFalse), any(&c))) && |