[llvm] r369411 - [InstCombine] make fold for icmp with sext more efficient; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 10:03:23 PDT 2019


Author: spatel
Date: Tue Aug 20 10:03:22 2019
New Revision: 369411

URL: http://llvm.org/viewvc/llvm-project?rev=369411&view=rev
Log:
[InstCombine] make fold for icmp with sext more efficient; NFC

We were creating 2 instructions and relying on a subsequent fold
to invert a not(icmp). Create the final icmp directly instead.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=369411&r1=369410&r2=369411&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Tue Aug 20 10:03:22 2019
@@ -4128,27 +4128,21 @@ Instruction *InstCombiner::foldICmpWithC
   // The re-extended constant changed, partly changed (in the case of a vector),
   // or could not be determined to be equal (in the case of a constant
   // expression), so the constant cannot be represented in the shorter type.
-  // Consequently, we cannot emit a simple comparison.
   // All the cases that fold to true or false will have already been handled
   // by SimplifyICmpInst, so only deal with the tricky case.
-
   if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C))
     return nullptr;
 
-  // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
-  // should have been folded away previously and not enter in here.
-
-  // We're performing an unsigned comp with a sign extended value.
-  // This is true if the input is >= 0. [aka >s -1]
-  Constant *NegOne = Constant::getAllOnesValue(SrcTy);
-  Value *Result = Builder.CreateICmpSGT(Op0Src, NegOne, ICmp.getName());
-
-  // Finally, return the value computed.
+  // Is source op positive?
+  // icmp ult (sext X), C --> icmp sgt X, -1
   if (ICmp.getPredicate() == ICmpInst::ICMP_ULT)
-    return replaceInstUsesWith(ICmp, Result);
+    return new ICmpInst(CmpInst::ICMP_SGT, Op0Src,
+                        Constant::getAllOnesValue(SrcTy));
 
+  // Is source op negative?
+  // icmp ugt (sext X), C --> icmp slt X, 0
   assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
-  return BinaryOperator::CreateNot(Result);
+  return new ICmpInst(CmpInst::ICMP_SLT, Op0Src, Constant::getNullValue(SrcTy));
 }
 
 static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS) {




More information about the llvm-commits mailing list