From 8b894eaa42ea00344d66f6d4d4c4b0a449bedc98 Mon Sep 17 00:00:00 2001 From: Sychev Vadim Date: Tue, 7 Jun 2022 20:28:41 +0300 Subject: [PATCH] Optimization for "~x + 1" to "-x" (#69003) (#69600) --- src/coreclr/jit/morph.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index f5d37ca..c3f3d17 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -12895,6 +12895,16 @@ GenTree* Compiler::fgOptimizeAddition(GenTreeOp* add) return add; } + + // Fold (~x + 1) to -x. + if (op1->OperIs(GT_NOT) && op2->IsIntegralConst(1)) + { + op1->SetOper(GT_NEG); + op1->SetVNsFromNode(add); + DEBUG_DESTROY_NODE(op2); + DEBUG_DESTROY_NODE(add); + return op1; + } } return nullptr; -- 2.7.4