From 18abc2c9405aff5a0e4f5548e59d557c956d0adf Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 18 Jan 2017 21:16:12 +0000 Subject: [PATCH] [InstCombine] add an assert to make a shl+icmp transform assumption explicit; NFCI llvm-svn: 292440 --- lib/Transforms/InstCombine/InstCombineCompares.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 268b05dc63c..c6e15c73420 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1921,11 +1921,19 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, // A 'shl nuw' is just shifting out zeros, so adjust the compare constant // and eliminate the shift. if (Shl->hasNoUnsignedWrap()) { - if (Cmp.isEquality() || Pred == ICmpInst::ICMP_UGT) { + if (Pred == ICmpInst::ICMP_UGT) { // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt) APInt ShiftedC = C->lshr(*ShiftAmt); return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); } + if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) { + // This is the same code as the UGT case, but assert the pre-condition + // that is needed for this to work with equality predicates. + assert(C->lshr(*ShiftAmt).shl(*ShiftAmt) == *C && + "Compare known true or false was not folded"); + APInt ShiftedC = C->lshr(*ShiftAmt); + return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); + } if (Pred == ICmpInst::ICMP_ULT) { // ULE is the same as above, but ULE is canonicalized to ULT, so convert: // (X << S) <=u C is equiv to X <=u (C >> S) for all C