From 055312055be0454f6689aa15a0d7c75e5957ba4d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 8 Aug 2016 11:49:14 +0300 Subject: [PATCH] Minor optimization of expression negation --- .../wasm/generate/WasmGenerationVisitor.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java index f967c4162..2f5704f54 100644 --- a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java @@ -635,11 +635,69 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { return binary.getFirst(); } } + + WasmIntBinaryOperation negatedOp = negate(binary.getOperation()); + if (negatedOp != null) { + return new WasmIntBinary(binary.getType(), negatedOp, binary.getFirst(), binary.getSecond()); + } + } else if (expr instanceof WasmFloatBinary) { + WasmFloatBinary binary = (WasmFloatBinary) expr; + WasmFloatBinaryOperation negatedOp = negate(binary.getOperation()); + if (negatedOp != null) { + return new WasmFloatBinary(binary.getType(), negatedOp, binary.getFirst(), binary.getSecond()); + } } + return new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.XOR, expr, new WasmInt32Constant(1)); } private boolean isOne(WasmExpression expression) { return expression instanceof WasmInt32Constant && ((WasmInt32Constant) expression).getValue() == 1; } + + private WasmIntBinaryOperation negate(WasmIntBinaryOperation op) { + switch (op) { + case EQ: + return WasmIntBinaryOperation.NE; + case NE: + return WasmIntBinaryOperation.EQ; + case LT_SIGNED: + return WasmIntBinaryOperation.GE_SIGNED; + case LT_UNSIGNED: + return WasmIntBinaryOperation.GE_UNSIGNED; + case LE_SIGNED: + return WasmIntBinaryOperation.GT_SIGNED; + case LE_UNSIGNED: + return WasmIntBinaryOperation.GT_UNSIGNED; + case GT_SIGNED: + return WasmIntBinaryOperation.LE_SIGNED; + case GT_UNSIGNED: + return WasmIntBinaryOperation.LE_UNSIGNED; + case GE_SIGNED: + return WasmIntBinaryOperation.LT_SIGNED; + case GE_UNSIGNED: + return WasmIntBinaryOperation.LT_UNSIGNED; + default: + return null; + } + } + + private WasmFloatBinaryOperation negate(WasmFloatBinaryOperation op) { + switch (op) { + case EQ: + return WasmFloatBinaryOperation.NE; + case NE: + return WasmFloatBinaryOperation.EQ; + case LT: + return WasmFloatBinaryOperation.GE; + case LE: + return WasmFloatBinaryOperation.GT; + case GT: + return WasmFloatBinaryOperation.LE; + case GE: + return WasmFloatBinaryOperation.LT; + default: + return null; + } + } }