From 023bbb28f393032a8df6c743319cd835feddf5ba Mon Sep 17 00:00:00 2001 From: Max Graey Date: Fri, 16 Jul 2021 22:33:46 +0300 Subject: [Optimize-Instructions] Simplify sign patterns like x < 0 ? -1 : 1 (#3756) i32(x) < 0 ? i32(-1) : i32(1) -> x >> 31 | 1 i64(x) < 0 ? i64(-1) : i64(1) -> x >> 63 | 1 This shrinks 2 bytes. --- src/passes/OptimizeInstructions.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') 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); { @@ -1481,6 +1482,29 @@ private: return curr->type == Type::i64 ? builder.makeUnary(ExtendUInt32, c) : c; } } + { + // 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(); + 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()->value = Literal::makeOne(type); + return builder.makeBinary( + Abstract::getBinary(type, Or), bin, curr->ifTrue); + } + } + } { // Sides are identical, fold Expression *ifTrue, *ifFalse, *c; -- cgit v1.2.3