Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1 | //===- InstCombineAndOrXor.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visitAnd, visitOr, and visitXor functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 16 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/CmpInstAnalysis.h" |
James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | using namespace PatternMatch; |
| 23 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "instcombine" |
| 25 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 26 | /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into |
Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 27 | /// a four bit mask. |
| 28 | static unsigned getFCmpCode(FCmpInst::Predicate CC) { |
| 29 | assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE && |
| 30 | "Unexpected FCmp predicate!"); |
| 31 | // Take advantage of the bit pattern of FCmpInst::Predicate here. |
| 32 | // U L G E |
| 33 | static_assert(FCmpInst::FCMP_FALSE == 0, ""); // 0 0 0 0 |
| 34 | static_assert(FCmpInst::FCMP_OEQ == 1, ""); // 0 0 0 1 |
| 35 | static_assert(FCmpInst::FCMP_OGT == 2, ""); // 0 0 1 0 |
| 36 | static_assert(FCmpInst::FCMP_OGE == 3, ""); // 0 0 1 1 |
| 37 | static_assert(FCmpInst::FCMP_OLT == 4, ""); // 0 1 0 0 |
| 38 | static_assert(FCmpInst::FCMP_OLE == 5, ""); // 0 1 0 1 |
| 39 | static_assert(FCmpInst::FCMP_ONE == 6, ""); // 0 1 1 0 |
| 40 | static_assert(FCmpInst::FCMP_ORD == 7, ""); // 0 1 1 1 |
| 41 | static_assert(FCmpInst::FCMP_UNO == 8, ""); // 1 0 0 0 |
| 42 | static_assert(FCmpInst::FCMP_UEQ == 9, ""); // 1 0 0 1 |
| 43 | static_assert(FCmpInst::FCMP_UGT == 10, ""); // 1 0 1 0 |
| 44 | static_assert(FCmpInst::FCMP_UGE == 11, ""); // 1 0 1 1 |
| 45 | static_assert(FCmpInst::FCMP_ULT == 12, ""); // 1 1 0 0 |
| 46 | static_assert(FCmpInst::FCMP_ULE == 13, ""); // 1 1 0 1 |
| 47 | static_assert(FCmpInst::FCMP_UNE == 14, ""); // 1 1 1 0 |
| 48 | static_assert(FCmpInst::FCMP_TRUE == 15, ""); // 1 1 1 1 |
| 49 | return CC; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 52 | /// This is the complement of getICmpCode, which turns an opcode and two |
| 53 | /// operands into either a constant true or false, or a brand new ICmp |
| 54 | /// instruction. The sign is passed in to determine which kind of predicate to |
| 55 | /// use in the new icmp instruction. |
Benjamin Kramer | baba1aa | 2012-02-06 11:28:19 +0000 | [diff] [blame] | 56 | static Value *getNewICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS, |
| 57 | InstCombiner::BuilderTy *Builder) { |
Pete Cooper | ebf98c1 | 2011-12-17 01:20:32 +0000 | [diff] [blame] | 58 | ICmpInst::Predicate NewPred; |
| 59 | if (Value *NewConstant = getICmpValue(Sign, Code, LHS, RHS, NewPred)) |
| 60 | return NewConstant; |
| 61 | return Builder->CreateICmp(NewPred, LHS, RHS); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 64 | /// This is the complement of getFCmpCode, which turns an opcode and two |
Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 65 | /// operands into either a FCmp instruction, or a true/false constant. |
| 66 | static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS, |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 67 | InstCombiner::BuilderTy *Builder) { |
Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 68 | const auto Pred = static_cast<FCmpInst::Predicate>(Code); |
| 69 | assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE && |
| 70 | "Unexpected FCmp predicate!"); |
| 71 | if (Pred == FCmpInst::FCMP_FALSE) |
| 72 | return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); |
| 73 | if (Pred == FCmpInst::FCMP_TRUE) |
| 74 | return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 75 | return Builder->CreateFCmp(Pred, LHS, RHS); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 78 | /// \brief Transform BITWISE_OP(BSWAP(A),BSWAP(B)) to BSWAP(BITWISE_OP(A, B)) |
| 79 | /// \param I Binary operator to transform. |
| 80 | /// \return Pointer to node that must replace the original binary operator, or |
| 81 | /// null pointer if no transformation was made. |
| 82 | Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) { |
Craig Topper | c6948c2 | 2017-07-03 05:54:11 +0000 | [diff] [blame] | 83 | assert(I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying"); |
| 84 | |
Craig Topper | 8036970 | 2017-07-03 05:54:16 +0000 | [diff] [blame^] | 85 | // TODO We should probably check for single use of the bswap. |
| 86 | |
Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 87 | Value *NewLHS; |
| 88 | if (!match(I.getOperand(0), m_BSwap(m_Value(NewLHS)))) |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 89 | return nullptr; |
| 90 | |
Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 91 | Value *NewRHS; |
| 92 | const APInt *C; |
| 93 | |
| 94 | if (match(I.getOperand(1), m_BSwap(m_Value(NewRHS)))) { |
| 95 | // OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) ) |
| 96 | // NewRHS initialized by the matcher. |
| 97 | } else if (match(I.getOperand(1), m_APInt(C))) { |
| 98 | // OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) ) |
| 99 | NewRHS = ConstantInt::get(I.getType(), C->byteSwap()); |
| 100 | } else |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 101 | return nullptr; |
| 102 | |
Sanjay Patel | 1e6ca44 | 2016-11-22 22:54:36 +0000 | [diff] [blame] | 103 | Value *BinOp = Builder->CreateBinOp(I.getOpcode(), NewLHS, NewRHS); |
Craig Topper | 1e4643a | 2017-07-03 05:54:13 +0000 | [diff] [blame] | 104 | Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, |
| 105 | I.getType()); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 106 | return Builder->CreateCall(F, BinOp); |
| 107 | } |
| 108 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 109 | /// This handles expressions of the form ((val OP C1) & C2). Where |
Craig Topper | 70e4f43 | 2017-04-02 17:57:30 +0000 | [diff] [blame] | 110 | /// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. |
| 111 | Instruction *InstCombiner::OptAndOp(BinaryOperator *Op, |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 112 | ConstantInt *OpRHS, |
| 113 | ConstantInt *AndRHS, |
| 114 | BinaryOperator &TheAnd) { |
| 115 | Value *X = Op->getOperand(0); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 116 | Constant *Together = nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 117 | if (!Op->isShift()) |
| 118 | Together = ConstantExpr::getAnd(AndRHS, OpRHS); |
| 119 | |
| 120 | switch (Op->getOpcode()) { |
Craig Topper | 70e4f43 | 2017-04-02 17:57:30 +0000 | [diff] [blame] | 121 | default: break; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 122 | case Instruction::Xor: |
| 123 | if (Op->hasOneUse()) { |
| 124 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 125 | Value *And = Builder->CreateAnd(X, AndRHS); |
| 126 | And->takeName(Op); |
| 127 | return BinaryOperator::CreateXor(And, Together); |
| 128 | } |
| 129 | break; |
| 130 | case Instruction::Or: |
Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 131 | if (Op->hasOneUse()){ |
Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 132 | ConstantInt *TogetherCI = dyn_cast<ConstantInt>(Together); |
| 133 | if (TogetherCI && !TogetherCI->isZero()){ |
| 134 | // (X | C1) & C2 --> (X & (C2^(C1&C2))) | C1 |
| 135 | // NOTE: This reduces the number of bits set in the & mask, which |
| 136 | // can expose opportunities for store narrowing. |
| 137 | Together = ConstantExpr::getXor(AndRHS, Together); |
| 138 | Value *And = Builder->CreateAnd(X, Together); |
| 139 | And->takeName(Op); |
| 140 | return BinaryOperator::CreateOr(And, OpRHS); |
| 141 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 142 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 144 | break; |
| 145 | case Instruction::Add: |
| 146 | if (Op->hasOneUse()) { |
| 147 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 148 | // of the bit. First thing to check is to see if this AND is with a |
| 149 | // single bit constant. |
Jakub Staszak | 9de494e | 2013-06-06 00:49:57 +0000 | [diff] [blame] | 150 | const APInt &AndRHSV = AndRHS->getValue(); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 151 | |
| 152 | // If there is only one bit set. |
| 153 | if (AndRHSV.isPowerOf2()) { |
| 154 | // Ok, at this point, we know that we are masking the result of the |
| 155 | // ADD down to exactly one bit. If the constant we are adding has |
| 156 | // no bits set below this bit, then we can eliminate the ADD. |
Jakub Staszak | 9de494e | 2013-06-06 00:49:57 +0000 | [diff] [blame] | 157 | const APInt& AddRHS = OpRHS->getValue(); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 158 | |
| 159 | // Check to see if any bits below the one bit set in AndRHSV are set. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 160 | if ((AddRHS & (AndRHSV - 1)).isNullValue()) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 161 | // If not, the only thing that can effect the output of the AND is |
| 162 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 163 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 164 | // no effect. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 165 | if ((AddRHS & AndRHSV).isNullValue()) { // Bit is not set, noop |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 166 | TheAnd.setOperand(0, X); |
| 167 | return &TheAnd; |
| 168 | } else { |
| 169 | // Pull the XOR out of the AND. |
| 170 | Value *NewAnd = Builder->CreateAnd(X, AndRHS); |
| 171 | NewAnd->takeName(Op); |
| 172 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | break; |
| 178 | |
| 179 | case Instruction::Shl: { |
| 180 | // We know that the AND will not produce any of the bits shifted in, so if |
| 181 | // the anded constant includes them, clear them now! |
| 182 | // |
| 183 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 184 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 185 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 186 | ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShlMask); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 9f0ac0d | 2011-02-15 01:56:08 +0000 | [diff] [blame] | 188 | if (CI->getValue() == ShlMask) |
| 189 | // Masking out bits that the shift already masks. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 190 | return replaceInstUsesWith(TheAnd, Op); // No need for the and. |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 9f0ac0d | 2011-02-15 01:56:08 +0000 | [diff] [blame] | 192 | if (CI != AndRHS) { // Reducing bits set in and. |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 193 | TheAnd.setOperand(1, CI); |
| 194 | return &TheAnd; |
| 195 | } |
| 196 | break; |
| 197 | } |
| 198 | case Instruction::LShr: { |
| 199 | // We know that the AND will not produce any of the bits shifted in, so if |
| 200 | // the anded constant includes them, clear them now! This only applies to |
| 201 | // unsigned shifts, because a signed shr may bring in set bits! |
| 202 | // |
| 203 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 204 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 205 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 206 | ConstantInt *CI = Builder->getInt(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 9f0ac0d | 2011-02-15 01:56:08 +0000 | [diff] [blame] | 208 | if (CI->getValue() == ShrMask) |
| 209 | // Masking out bits that the shift already masks. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 210 | return replaceInstUsesWith(TheAnd, Op); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 9f0ac0d | 2011-02-15 01:56:08 +0000 | [diff] [blame] | 212 | if (CI != AndRHS) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 213 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 214 | return &TheAnd; |
| 215 | } |
| 216 | break; |
| 217 | } |
| 218 | case Instruction::AShr: |
| 219 | // Signed shr. |
| 220 | // See if this is shifting in some sign extension, then masking it out |
| 221 | // with an and. |
| 222 | if (Op->hasOneUse()) { |
| 223 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 224 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 225 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 226 | Constant *C = Builder->getInt(AndRHS->getValue() & ShrMask); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 227 | if (C == AndRHS) { // Masking out bits shifted in. |
| 228 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
| 229 | // Make the argument unsigned. |
| 230 | Value *ShVal = Op->getOperand(0); |
| 231 | ShVal = Builder->CreateLShr(ShVal, OpRHS, Op->getName()); |
| 232 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
| 233 | } |
| 234 | } |
| 235 | break; |
| 236 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 237 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 240 | /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 241 | /// (V < Lo || V >= Hi). This method expects that Lo <= Hi. IsSigned indicates |
| 242 | /// whether to treat V, Lo, and Hi as signed or not. |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 243 | Value *InstCombiner::insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 244 | bool isSigned, bool Inside) { |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 245 | assert((isSigned ? Lo.sle(Hi) : Lo.ule(Hi)) && |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 246 | "Lo is not <= Hi in range emission code!"); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 247 | |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 248 | Type *Ty = V->getType(); |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 249 | if (Lo == Hi) |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 250 | return Inside ? ConstantInt::getFalse(Ty) : ConstantInt::getTrue(Ty); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 251 | |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 252 | // V >= Min && V < Hi --> V < Hi |
| 253 | // V < Min || V >= Hi --> V >= Hi |
| 254 | ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE; |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 255 | if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) { |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 256 | Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred; |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 257 | return Builder->CreateICmp(Pred, V, ConstantInt::get(Ty, Hi)); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 260 | // V >= Lo && V < Hi --> V - Lo u< Hi - Lo |
| 261 | // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 262 | Value *VMinusLo = |
| 263 | Builder->CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off"); |
| 264 | Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo); |
Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 265 | return Builder->CreateICmp(Pred, VMinusLo, HiMinusLo); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 268 | /// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns |
| 269 | /// that can be simplified. |
| 270 | /// One of A and B is considered the mask. The other is the value. This is |
| 271 | /// described as the "AMask" or "BMask" part of the enum. If the enum contains |
| 272 | /// only "Mask", then both A and B can be considered masks. If A is the mask, |
| 273 | /// then it was proven that (A & C) == C. This is trivial if C == A or C == 0. |
| 274 | /// If both A and C are constants, this proof is also easy. |
| 275 | /// For the following explanations, we assume that A is the mask. |
| 276 | /// |
| 277 | /// "AllOnes" declares that the comparison is true only if (A & B) == A or all |
| 278 | /// bits of A are set in B. |
| 279 | /// Example: (icmp eq (A & 3), 3) -> AMask_AllOnes |
| 280 | /// |
| 281 | /// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all |
| 282 | /// bits of A are cleared in B. |
| 283 | /// Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes |
| 284 | /// |
| 285 | /// "Mixed" declares that (A & B) == C and C might or might not contain any |
| 286 | /// number of one bits and zero bits. |
| 287 | /// Example: (icmp eq (A & 3), 1) -> AMask_Mixed |
| 288 | /// |
| 289 | /// "Not" means that in above descriptions "==" should be replaced by "!=". |
| 290 | /// Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes |
| 291 | /// |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 292 | /// If the mask A contains a single bit, then the following is equivalent: |
| 293 | /// (icmp eq (A & B), A) equals (icmp ne (A & B), 0) |
| 294 | /// (icmp ne (A & B), A) equals (icmp eq (A & B), 0) |
| 295 | enum MaskedICmpType { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 296 | AMask_AllOnes = 1, |
| 297 | AMask_NotAllOnes = 2, |
| 298 | BMask_AllOnes = 4, |
| 299 | BMask_NotAllOnes = 8, |
| 300 | Mask_AllZeros = 16, |
| 301 | Mask_NotAllZeros = 32, |
| 302 | AMask_Mixed = 64, |
| 303 | AMask_NotMixed = 128, |
| 304 | BMask_Mixed = 256, |
| 305 | BMask_NotMixed = 512 |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 308 | /// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C) |
| 309 | /// satisfies. |
| 310 | static unsigned getMaskedICmpType(Value *A, Value *B, Value *C, |
| 311 | ICmpInst::Predicate Pred) { |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 312 | ConstantInt *ACst = dyn_cast<ConstantInt>(A); |
| 313 | ConstantInt *BCst = dyn_cast<ConstantInt>(B); |
| 314 | ConstantInt *CCst = dyn_cast<ConstantInt>(C); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 315 | bool IsEq = (Pred == ICmpInst::ICMP_EQ); |
| 316 | bool IsAPow2 = (ACst && !ACst->isZero() && ACst->getValue().isPowerOf2()); |
| 317 | bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2()); |
| 318 | unsigned MaskVal = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 319 | if (CCst && CCst->isZero()) { |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 320 | // if C is zero, then both A and B qualify as mask |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 321 | MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed) |
| 322 | : (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed)); |
| 323 | if (IsAPow2) |
| 324 | MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed) |
| 325 | : (AMask_AllOnes | AMask_Mixed)); |
| 326 | if (IsBPow2) |
| 327 | MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed) |
| 328 | : (BMask_AllOnes | BMask_Mixed)); |
| 329 | return MaskVal; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 330 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 331 | |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 332 | if (A == C) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 333 | MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed) |
| 334 | : (AMask_NotAllOnes | AMask_NotMixed)); |
| 335 | if (IsAPow2) |
| 336 | MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed) |
| 337 | : (Mask_AllZeros | AMask_Mixed)); |
| 338 | } else if (ACst && CCst && ConstantExpr::getAnd(ACst, CCst) == CCst) { |
| 339 | MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 340 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 341 | |
Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 342 | if (B == C) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 343 | MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed) |
| 344 | : (BMask_NotAllOnes | BMask_NotMixed)); |
| 345 | if (IsBPow2) |
| 346 | MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed) |
| 347 | : (Mask_AllZeros | BMask_Mixed)); |
| 348 | } else if (BCst && CCst && ConstantExpr::getAnd(BCst, CCst) == CCst) { |
| 349 | MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 350 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 351 | |
| 352 | return MaskVal; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 355 | /// Convert an analysis of a masked ICmp into its equivalent if all boolean |
| 356 | /// operations had the opposite sense. Since each "NotXXX" flag (recording !=) |
| 357 | /// is adjacent to the corresponding normal flag (recording ==), this just |
| 358 | /// involves swapping those bits over. |
| 359 | static unsigned conjugateICmpMask(unsigned Mask) { |
| 360 | unsigned NewMask; |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 361 | NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros | |
| 362 | AMask_Mixed | BMask_Mixed)) |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 363 | << 1; |
| 364 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 365 | NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros | |
| 366 | AMask_NotMixed | BMask_NotMixed)) |
| 367 | >> 1; |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 368 | |
| 369 | return NewMask; |
| 370 | } |
| 371 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 372 | /// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E). |
| 373 | /// Return the set of pattern classes (from MaskedICmpType) that both LHS and |
| 374 | /// RHS satisfy. |
| 375 | static unsigned getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C, |
| 376 | Value *&D, Value *&E, ICmpInst *LHS, |
| 377 | ICmpInst *RHS, |
| 378 | ICmpInst::Predicate &PredL, |
| 379 | ICmpInst::Predicate &PredR) { |
| 380 | if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) |
| 381 | return 0; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 382 | // vectors are not (yet?) supported |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 383 | if (LHS->getOperand(0)->getType()->isVectorTy()) |
| 384 | return 0; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 385 | |
| 386 | // Here comes the tricky part: |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 387 | // LHS might be of the form L11 & L12 == X, X == L21 & L22, |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 388 | // and L11 & L12 == L21 & L22. The same goes for RHS. |
| 389 | // Now we must find those components L** and R**, that are equal, so |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 390 | // that we can extract the parameters A, B, C, D, and E for the canonical |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 391 | // above. |
| 392 | Value *L1 = LHS->getOperand(0); |
| 393 | Value *L2 = LHS->getOperand(1); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 394 | Value *L11, *L12, *L21, *L22; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 395 | // Check whether the icmp can be decomposed into a bit test. |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 396 | if (decomposeBitTestICmp(LHS, PredL, L11, L12, L2)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 397 | L21 = L22 = L1 = nullptr; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 398 | } else { |
| 399 | // Look for ANDs in the LHS icmp. |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 400 | if (!L1->getType()->isIntegerTy()) { |
| 401 | // You can icmp pointers, for example. They really aren't masks. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 402 | L11 = L12 = nullptr; |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 403 | } else if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) { |
| 404 | // Any icmp can be viewed as being trivially masked; if it allows us to |
| 405 | // remove one, it's worth it. |
| 406 | L11 = L1; |
| 407 | L12 = Constant::getAllOnesValue(L1->getType()); |
| 408 | } |
| 409 | |
| 410 | if (!L2->getType()->isIntegerTy()) { |
| 411 | // You can icmp pointers, for example. They really aren't masks. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 412 | L21 = L22 = nullptr; |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 413 | } else if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) { |
| 414 | L21 = L2; |
| 415 | L22 = Constant::getAllOnesValue(L2->getType()); |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 416 | } |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 417 | } |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 418 | |
| 419 | // Bail if LHS was a icmp that can't be decomposed into an equality. |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 420 | if (!ICmpInst::isEquality(PredL)) |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 421 | return 0; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 422 | |
| 423 | Value *R1 = RHS->getOperand(0); |
| 424 | Value *R2 = RHS->getOperand(1); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 425 | Value *R11, *R12; |
| 426 | bool Ok = false; |
| 427 | if (decomposeBitTestICmp(RHS, PredR, R11, R12, R2)) { |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 428 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 429 | A = R11; |
| 430 | D = R12; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 431 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 432 | A = R12; |
| 433 | D = R11; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 434 | } else { |
| 435 | return 0; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 436 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 437 | E = R2; |
| 438 | R1 = nullptr; |
| 439 | Ok = true; |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 440 | } else if (R1->getType()->isIntegerTy()) { |
| 441 | if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) { |
| 442 | // As before, model no mask as a trivial mask if it'll let us do an |
Mayur Pandey | 75b76c6 | 2014-08-19 06:41:55 +0000 | [diff] [blame] | 443 | // optimization. |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 444 | R11 = R1; |
| 445 | R12 = Constant::getAllOnesValue(R1->getType()); |
| 446 | } |
| 447 | |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 448 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 449 | A = R11; |
| 450 | D = R12; |
| 451 | E = R2; |
| 452 | Ok = true; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 453 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 454 | A = R12; |
| 455 | D = R11; |
| 456 | E = R2; |
| 457 | Ok = true; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 460 | |
| 461 | // Bail if RHS was a icmp that can't be decomposed into an equality. |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 462 | if (!ICmpInst::isEquality(PredR)) |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 463 | return 0; |
| 464 | |
Chad Rosier | 58919cc | 2016-05-09 21:37:43 +0000 | [diff] [blame] | 465 | // Look for ANDs on the right side of the RHS icmp. |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 466 | if (!Ok && R2->getType()->isIntegerTy()) { |
Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 467 | if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) { |
| 468 | R11 = R2; |
| 469 | R12 = Constant::getAllOnesValue(R2->getType()); |
| 470 | } |
| 471 | |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 472 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 473 | A = R11; |
| 474 | D = R12; |
| 475 | E = R1; |
| 476 | Ok = true; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 477 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 478 | A = R12; |
| 479 | D = R11; |
| 480 | E = R1; |
| 481 | Ok = true; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 482 | } else { |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 483 | return 0; |
Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 484 | } |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 485 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 486 | if (!Ok) |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | |
| 489 | if (L11 == A) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 490 | B = L12; |
| 491 | C = L2; |
Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 492 | } else if (L12 == A) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 493 | B = L11; |
| 494 | C = L2; |
Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 495 | } else if (L21 == A) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 496 | B = L22; |
| 497 | C = L1; |
Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 498 | } else if (L22 == A) { |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 499 | B = L21; |
| 500 | C = L1; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 503 | unsigned LeftType = getMaskedICmpType(A, B, C, PredL); |
| 504 | unsigned RightType = getMaskedICmpType(A, D, E, PredR); |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 505 | return LeftType & RightType; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 506 | } |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 507 | |
| 508 | /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) |
| 509 | /// into a single (icmp(A & X) ==/!= Y). |
David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 510 | static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, |
| 511 | llvm::InstCombiner::BuilderTy *Builder) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 512 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr; |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 513 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| 514 | unsigned Mask = |
| 515 | getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR); |
| 516 | if (Mask == 0) |
| 517 | return nullptr; |
| 518 | |
| 519 | assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && |
| 520 | "Expected equality predicates for masked type of icmps."); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 521 | |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 522 | // In full generality: |
| 523 | // (icmp (A & B) Op C) | (icmp (A & D) Op E) |
| 524 | // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ] |
| 525 | // |
| 526 | // If the latter can be converted into (icmp (A & X) Op Y) then the former is |
| 527 | // equivalent to (icmp (A & X) !Op Y). |
| 528 | // |
| 529 | // Therefore, we can pretend for the rest of this function that we're dealing |
| 530 | // with the conjunction, provided we flip the sense of any comparisons (both |
| 531 | // input and output). |
| 532 | |
| 533 | // In most cases we're going to produce an EQ for the "&&" case. |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 534 | ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 535 | if (!IsAnd) { |
| 536 | // Convert the masking analysis into its equivalent with negated |
| 537 | // comparisons. |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 538 | Mask = conjugateICmpMask(Mask); |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 539 | } |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 540 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 541 | if (Mask & Mask_AllZeros) { |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 542 | // (icmp eq (A & B), 0) & (icmp eq (A & D), 0) |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 543 | // -> (icmp eq (A & (B|D)), 0) |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 544 | Value *NewOr = Builder->CreateOr(B, D); |
| 545 | Value *NewAnd = Builder->CreateAnd(A, NewOr); |
| 546 | // We can't use C as zero because we might actually handle |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 547 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 548 | // with B and D, having a single bit set. |
| 549 | Value *Zero = Constant::getNullValue(A->getType()); |
| 550 | return Builder->CreateICmp(NewCC, NewAnd, Zero); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 551 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 552 | if (Mask & BMask_AllOnes) { |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 553 | // (icmp eq (A & B), B) & (icmp eq (A & D), D) |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 554 | // -> (icmp eq (A & (B|D)), (B|D)) |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 555 | Value *NewOr = Builder->CreateOr(B, D); |
| 556 | Value *NewAnd = Builder->CreateAnd(A, NewOr); |
| 557 | return Builder->CreateICmp(NewCC, NewAnd, NewOr); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 558 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 559 | if (Mask & AMask_AllOnes) { |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 560 | // (icmp eq (A & B), A) & (icmp eq (A & D), A) |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 561 | // -> (icmp eq (A & (B&D)), A) |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 562 | Value *NewAnd1 = Builder->CreateAnd(B, D); |
| 563 | Value *NewAnd2 = Builder->CreateAnd(A, NewAnd1); |
| 564 | return Builder->CreateICmp(NewCC, NewAnd2, A); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 565 | } |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 566 | |
| 567 | // Remaining cases assume at least that B and D are constant, and depend on |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 568 | // their actual values. This isn't strictly necessary, just a "handle the |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 569 | // easy cases for now" decision. |
| 570 | ConstantInt *BCst = dyn_cast<ConstantInt>(B); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 571 | if (!BCst) |
| 572 | return nullptr; |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 573 | ConstantInt *DCst = dyn_cast<ConstantInt>(D); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 574 | if (!DCst) |
| 575 | return nullptr; |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 576 | |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 577 | if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) { |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 578 | // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and |
| 579 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
| 580 | // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0) |
| 581 | // Only valid if one of the masks is a superset of the other (check "B&D" is |
| 582 | // the same as either B or D). |
| 583 | APInt NewMask = BCst->getValue() & DCst->getValue(); |
| 584 | |
| 585 | if (NewMask == BCst->getValue()) |
| 586 | return LHS; |
| 587 | else if (NewMask == DCst->getValue()) |
| 588 | return RHS; |
| 589 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 590 | |
| 591 | if (Mask & AMask_NotAllOnes) { |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 592 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
| 593 | // -> (icmp ne (A & B), A) or (icmp ne (A & D), A) |
| 594 | // Only valid if one of the masks is a superset of the other (check "B|D" is |
| 595 | // the same as either B or D). |
| 596 | APInt NewMask = BCst->getValue() | DCst->getValue(); |
| 597 | |
| 598 | if (NewMask == BCst->getValue()) |
| 599 | return LHS; |
| 600 | else if (NewMask == DCst->getValue()) |
| 601 | return RHS; |
| 602 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 603 | |
| 604 | if (Mask & BMask_Mixed) { |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 605 | // (icmp eq (A & B), C) & (icmp eq (A & D), E) |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 606 | // We already know that B & C == C && D & E == E. |
| 607 | // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of |
| 608 | // C and E, which are shared by both the mask B and the mask D, don't |
| 609 | // contradict, then we can transform to |
| 610 | // -> (icmp eq (A & (B|D)), (C|E)) |
| 611 | // Currently, we only handle the case of B, C, D, and E being constant. |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 612 | // We can't simply use C and E because we might actually handle |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 613 | // (icmp ne (A & B), B) & (icmp eq (A & D), D) |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 614 | // with B and D, having a single bit set. |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 615 | ConstantInt *CCst = dyn_cast<ConstantInt>(C); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 616 | if (!CCst) |
| 617 | return nullptr; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 618 | ConstantInt *ECst = dyn_cast<ConstantInt>(E); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 619 | if (!ECst) |
| 620 | return nullptr; |
| 621 | if (PredL != NewCC) |
David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 622 | CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst)); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 623 | if (PredR != NewCC) |
David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 624 | ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst)); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 625 | |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 626 | // If there is a conflict, we should actually return a false for the |
| 627 | // whole construct. |
David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 628 | if (((BCst->getValue() & DCst->getValue()) & |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 629 | (CCst->getValue() ^ ECst->getValue())).getBoolValue()) |
David Majnemer | 6fdb6b8 | 2014-11-18 09:31:41 +0000 | [diff] [blame] | 630 | return ConstantInt::get(LHS->getType(), !IsAnd); |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 631 | |
Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 632 | Value *NewOr1 = Builder->CreateOr(B, D); |
| 633 | Value *NewOr2 = ConstantExpr::getOr(CCst, ECst); |
| 634 | Value *NewAnd = Builder->CreateAnd(A, NewOr1); |
| 635 | return Builder->CreateICmp(NewCC, NewAnd, NewOr2); |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 636 | } |
Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 637 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 638 | return nullptr; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 641 | /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp. |
| 642 | /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n |
| 643 | /// If \p Inverted is true then the check is for the inverted range, e.g. |
| 644 | /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n |
| 645 | Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, |
| 646 | bool Inverted) { |
| 647 | // Check the lower range comparison, e.g. x >= 0 |
| 648 | // InstCombine already ensured that if there is a constant it's on the RHS. |
| 649 | ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1)); |
| 650 | if (!RangeStart) |
| 651 | return nullptr; |
| 652 | |
| 653 | ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() : |
| 654 | Cmp0->getPredicate()); |
| 655 | |
| 656 | // Accept x > -1 or x >= 0 (after potentially inverting the predicate). |
| 657 | if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) || |
| 658 | (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero()))) |
| 659 | return nullptr; |
| 660 | |
| 661 | ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() : |
| 662 | Cmp1->getPredicate()); |
| 663 | |
| 664 | Value *Input = Cmp0->getOperand(0); |
| 665 | Value *RangeEnd; |
| 666 | if (Cmp1->getOperand(0) == Input) { |
| 667 | // For the upper range compare we have: icmp x, n |
| 668 | RangeEnd = Cmp1->getOperand(1); |
| 669 | } else if (Cmp1->getOperand(1) == Input) { |
| 670 | // For the upper range compare we have: icmp n, x |
| 671 | RangeEnd = Cmp1->getOperand(0); |
| 672 | Pred1 = ICmpInst::getSwappedPredicate(Pred1); |
| 673 | } else { |
| 674 | return nullptr; |
| 675 | } |
| 676 | |
| 677 | // Check the upper range comparison, e.g. x < n |
| 678 | ICmpInst::Predicate NewPred; |
| 679 | switch (Pred1) { |
| 680 | case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break; |
| 681 | case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break; |
| 682 | default: return nullptr; |
| 683 | } |
| 684 | |
| 685 | // This simplification is only valid if the upper range is not negative. |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 686 | KnownBits Known = computeKnownBits(RangeEnd, /*Depth=*/0, Cmp1); |
| 687 | if (!Known.isNonNegative()) |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 688 | return nullptr; |
| 689 | |
| 690 | if (Inverted) |
| 691 | NewPred = ICmpInst::getInversePredicate(NewPred); |
| 692 | |
| 693 | return Builder->CreateICmp(NewPred, Input, RangeEnd); |
| 694 | } |
| 695 | |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 696 | static Value * |
| 697 | foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS, |
| 698 | bool JoinedByAnd, |
| 699 | InstCombiner::BuilderTy *Builder) { |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 700 | Value *X = LHS->getOperand(0); |
| 701 | if (X != RHS->getOperand(0)) |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 702 | return nullptr; |
| 703 | |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 704 | const APInt *C1, *C2; |
| 705 | if (!match(LHS->getOperand(1), m_APInt(C1)) || |
| 706 | !match(RHS->getOperand(1), m_APInt(C2))) |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 707 | return nullptr; |
| 708 | |
| 709 | // We only handle (X != C1 && X != C2) and (X == C1 || X == C2). |
| 710 | ICmpInst::Predicate Pred = LHS->getPredicate(); |
| 711 | if (Pred != RHS->getPredicate()) |
| 712 | return nullptr; |
| 713 | if (JoinedByAnd && Pred != ICmpInst::ICMP_NE) |
| 714 | return nullptr; |
| 715 | if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ) |
| 716 | return nullptr; |
| 717 | |
| 718 | // The larger unsigned constant goes on the right. |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 719 | if (C1->ugt(*C2)) |
| 720 | std::swap(C1, C2); |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 721 | |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 722 | APInt Xor = *C1 ^ *C2; |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 723 | if (Xor.isPowerOf2()) { |
| 724 | // If LHSC and RHSC differ by only one bit, then set that bit in X and |
| 725 | // compare against the larger constant: |
| 726 | // (X == C1 || X == C2) --> (X | (C1 ^ C2)) == C2 |
| 727 | // (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2 |
| 728 | // We choose an 'or' with a Pow2 constant rather than the inverse mask with |
| 729 | // 'and' because that may lead to smaller codegen from a smaller constant. |
| 730 | Value *Or = Builder->CreateOr(X, ConstantInt::get(X->getType(), Xor)); |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 731 | return Builder->CreateICmp(Pred, Or, ConstantInt::get(X->getType(), *C2)); |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | // Special case: get the ordering right when the values wrap around zero. |
| 735 | // Ie, we assumed the constants were unsigned when swapping earlier. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 736 | if (C1->isNullValue() && C2->isAllOnesValue()) |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 737 | std::swap(C1, C2); |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 738 | |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 739 | if (*C1 == *C2 - 1) { |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 740 | // (X == 13 || X == 14) --> X - 13 <=u 1 |
| 741 | // (X != 13 && X != 14) --> X - 13 >u 1 |
| 742 | // An 'add' is the canonical IR form, so favor that over a 'sub'. |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 743 | Value *Add = Builder->CreateAdd(X, ConstantInt::get(X->getType(), -(*C1))); |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 744 | auto NewPred = JoinedByAnd ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE; |
| 745 | return Builder->CreateICmp(NewPred, Add, ConstantInt::get(X->getType(), 1)); |
| 746 | } |
| 747 | |
| 748 | return nullptr; |
| 749 | } |
| 750 | |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 751 | // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) |
| 752 | // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2) |
| 753 | Value *InstCombiner::foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS, |
| 754 | bool JoinedByAnd, |
| 755 | Instruction &CxtI) { |
| 756 | ICmpInst::Predicate Pred = LHS->getPredicate(); |
| 757 | if (Pred != RHS->getPredicate()) |
| 758 | return nullptr; |
| 759 | if (JoinedByAnd && Pred != ICmpInst::ICMP_NE) |
| 760 | return nullptr; |
| 761 | if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ) |
| 762 | return nullptr; |
| 763 | |
| 764 | // TODO support vector splats |
| 765 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 766 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
| 767 | if (!LHSC || !RHSC || !LHSC->isZero() || !RHSC->isZero()) |
| 768 | return nullptr; |
| 769 | |
| 770 | Value *A, *B, *C, *D; |
| 771 | if (match(LHS->getOperand(0), m_And(m_Value(A), m_Value(B))) && |
| 772 | match(RHS->getOperand(0), m_And(m_Value(C), m_Value(D)))) { |
| 773 | if (A == D || B == D) |
| 774 | std::swap(C, D); |
| 775 | if (B == C) |
| 776 | std::swap(A, B); |
| 777 | |
| 778 | if (A == C && |
| 779 | isKnownToBeAPowerOfTwo(B, false, 0, &CxtI) && |
| 780 | isKnownToBeAPowerOfTwo(D, false, 0, &CxtI)) { |
| 781 | Value *Mask = Builder->CreateOr(B, D); |
| 782 | Value *Masked = Builder->CreateAnd(A, Mask); |
| 783 | auto NewPred = JoinedByAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
| 784 | return Builder->CreateICmp(NewPred, Masked, Mask); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return nullptr; |
| 789 | } |
| 790 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 791 | /// Fold (icmp)&(icmp) if possible. |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 792 | Value *InstCombiner::foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, |
| 793 | Instruction &CxtI) { |
| 794 | // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2) |
| 795 | // if K1 and K2 are a one-bit mask. |
| 796 | if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, true, CxtI)) |
| 797 | return V; |
| 798 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 799 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 800 | |
| 801 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 802 | if (PredicatesFoldable(PredL, PredR)) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 803 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 804 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 805 | LHS->swapOperands(); |
| 806 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 807 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 808 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 809 | unsigned Code = getICmpCode(LHS) & getICmpCode(RHS); |
| 810 | bool isSigned = LHS->isSigned() || RHS->isSigned(); |
Pete Cooper | ebf98c1 | 2011-12-17 01:20:32 +0000 | [diff] [blame] | 811 | return getNewICmpValue(isSigned, Code, Op0, Op1, Builder); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 814 | |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 815 | // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E) |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 816 | if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder)) |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 817 | return V; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 818 | |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 819 | // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n |
| 820 | if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false)) |
| 821 | return V; |
| 822 | |
| 823 | // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n |
| 824 | if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false)) |
| 825 | return V; |
| 826 | |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 827 | if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder)) |
| 828 | return V; |
| 829 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 830 | // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 831 | Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 832 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 833 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
| 834 | if (!LHSC || !RHSC) |
| 835 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 836 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 837 | if (LHSC == RHSC && PredL == PredR) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 838 | // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) |
Sanjay Patel | c2ceb8b | 2016-01-18 19:17:58 +0000 | [diff] [blame] | 839 | // where C is a power of 2 or |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 840 | // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 841 | if ((PredL == ICmpInst::ICMP_ULT && LHSC->getValue().isPowerOf2()) || |
| 842 | (PredL == ICmpInst::ICMP_EQ && LHSC->isZero())) { |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 843 | Value *NewOr = Builder->CreateOr(LHS0, RHS0); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 844 | return Builder->CreateICmp(PredL, NewOr, LHSC); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 847 | |
Benjamin Kramer | 101720f | 2011-04-28 20:09:57 +0000 | [diff] [blame] | 848 | // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2 |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 849 | // where CMAX is the all ones value for the truncated type, |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 850 | // iff the lower bits of C2 and CA are zero. |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 851 | if (PredL == ICmpInst::ICMP_EQ && PredL == PredR && LHS->hasOneUse() && |
| 852 | RHS->hasOneUse()) { |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 853 | Value *V; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 854 | ConstantInt *AndC, *SmallC = nullptr, *BigC = nullptr; |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 855 | |
| 856 | // (trunc x) == C1 & (and x, CA) == C2 |
Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 857 | // (and x, CA) == C2 & (trunc x) == C1 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 858 | if (match(RHS0, m_Trunc(m_Value(V))) && |
| 859 | match(LHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) { |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 860 | SmallC = RHSC; |
| 861 | BigC = LHSC; |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 862 | } else if (match(LHS0, m_Trunc(m_Value(V))) && |
| 863 | match(RHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) { |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 864 | SmallC = LHSC; |
| 865 | BigC = RHSC; |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 868 | if (SmallC && BigC) { |
| 869 | unsigned BigBitSize = BigC->getType()->getBitWidth(); |
| 870 | unsigned SmallBitSize = SmallC->getType()->getBitWidth(); |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 871 | |
| 872 | // Check that the low bits are zero. |
| 873 | APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 874 | if ((Low & AndC->getValue()).isNullValue() && |
| 875 | (Low & BigC->getValue()).isNullValue()) { |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 876 | Value *NewAnd = Builder->CreateAnd(V, Low | AndC->getValue()); |
| 877 | APInt N = SmallC->getValue().zext(BigBitSize) | BigC->getValue(); |
| 878 | Value *NewVal = ConstantInt::get(AndC->getType()->getContext(), N); |
| 879 | return Builder->CreateICmp(PredL, NewAnd, NewVal); |
Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | } |
Benjamin Kramer | da37e15 | 2012-01-08 18:32:24 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 884 | // From here on, we only handle: |
| 885 | // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 886 | if (LHS0 != RHS0) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 887 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 888 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 889 | // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere. |
| 890 | if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE || |
| 891 | PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE || |
| 892 | PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE || |
| 893 | PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 894 | return nullptr; |
Anders Carlsson | da80afe | 2011-03-01 15:05:01 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 896 | // We can't fold (ugt x, C) & (sgt x, C2). |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 897 | if (!PredicatesFoldable(PredL, PredR)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 898 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 900 | // Ensure that the larger constant is on the RHS. |
| 901 | bool ShouldSwap; |
Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 902 | if (CmpInst::isSigned(PredL) || |
| 903 | (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR))) |
Sanjay Patel | 570e35c | 2017-04-10 16:55:57 +0000 | [diff] [blame] | 904 | ShouldSwap = LHSC->getValue().sgt(RHSC->getValue()); |
Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 905 | else |
| 906 | ShouldSwap = LHSC->getValue().ugt(RHSC->getValue()); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 908 | if (ShouldSwap) { |
| 909 | std::swap(LHS, RHS); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 910 | std::swap(LHSC, RHSC); |
| 911 | std::swap(PredL, PredR); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 914 | // At this point, we know we have two icmp instructions |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 915 | // comparing a value against two constants and and'ing the result |
| 916 | // together. Because of the above check, we know that we only have |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 917 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 918 | // (from the icmp folding check above), that the two constants |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 919 | // are not equal and that the larger constant is on the RHS |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 920 | assert(LHSC != RHSC && "Compares not folded above?"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 921 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 922 | switch (PredL) { |
| 923 | default: |
| 924 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 925 | case ICmpInst::ICMP_NE: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 926 | switch (PredR) { |
| 927 | default: |
| 928 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 929 | case ICmpInst::ICMP_ULT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 930 | if (LHSC == SubOne(RHSC)) // (X != 13 & X u< 14) -> X < 13 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 931 | return Builder->CreateICmpULT(LHS0, LHSC); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 932 | if (LHSC->isNullValue()) // (X != 0 & X u< 14) -> X-1 u< 13 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 933 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 934 | false, true); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 935 | break; // (X != 13 & X u< 15) -> no change |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 936 | case ICmpInst::ICMP_SLT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 937 | if (LHSC == SubOne(RHSC)) // (X != 13 & X s< 14) -> X < 13 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 938 | return Builder->CreateICmpSLT(LHS0, LHSC); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 939 | break; // (X != 13 & X s< 15) -> no change |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 940 | case ICmpInst::ICMP_NE: |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 941 | // Potential folds for this case should already be handled. |
| 942 | break; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 943 | } |
| 944 | break; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 945 | case ICmpInst::ICMP_UGT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 946 | switch (PredR) { |
| 947 | default: |
| 948 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 949 | case ICmpInst::ICMP_NE: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 950 | if (RHSC == AddOne(LHSC)) // (X u> 13 & X != 14) -> X u> 14 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 951 | return Builder->CreateICmp(PredL, LHS0, RHSC); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 952 | break; // (X u> 13 & X != 15) -> no change |
| 953 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 954 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| 955 | false, true); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 956 | } |
| 957 | break; |
| 958 | case ICmpInst::ICMP_SGT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 959 | switch (PredR) { |
| 960 | default: |
| 961 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 962 | case ICmpInst::ICMP_NE: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 963 | if (RHSC == AddOne(LHSC)) // (X s> 13 & X != 14) -> X s> 14 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 964 | return Builder->CreateICmp(PredL, LHS0, RHSC); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 965 | break; // (X s> 13 & X != 15) -> no change |
| 966 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 967 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), true, |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 968 | true); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 969 | } |
| 970 | break; |
| 971 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 972 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 973 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 976 | /// Optimize (fcmp)&(fcmp). NOTE: Unlike the rest of instcombine, this returns |
| 977 | /// a Value which should already be inserted into the function. |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 978 | Value *InstCombiner::foldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS) { |
Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 979 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 980 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 981 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 982 | |
| 983 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 984 | // Swap RHS operands to match LHS. |
| 985 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 986 | std::swap(Op1LHS, Op1RHS); |
| 987 | } |
| 988 | |
| 989 | // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). |
| 990 | // Suppose the relation between x and y is R, where R is one of |
| 991 | // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for |
| 992 | // testing the desired relations. |
| 993 | // |
| 994 | // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this: |
| 995 | // bool(R & CC0) && bool(R & CC1) |
| 996 | // = bool((R & CC0) & (R & CC1)) |
| 997 | // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency |
| 998 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) |
| 999 | return getFCmpValue(getFCmpCode(Op0CC) & getFCmpCode(Op1CC), Op0LHS, Op0RHS, |
| 1000 | Builder); |
| 1001 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1002 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 1003 | RHS->getPredicate() == FCmpInst::FCMP_ORD) { |
Benjamin Kramer | e89c705 | 2013-04-12 21:56:23 +0000 | [diff] [blame] | 1004 | if (LHS->getOperand(0)->getType() != RHS->getOperand(0)->getType()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1005 | return nullptr; |
Benjamin Kramer | e89c705 | 2013-04-12 21:56:23 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1007 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 1008 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 1009 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 1010 | // If either of the constants are nans, then the whole thing returns |
| 1011 | // false. |
| 1012 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 1013 | return Builder->getFalse(); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1014 | return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0)); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1015 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1017 | // Handle vector zeros. This occurs because the canonical form of |
| 1018 | // "fcmp ord x,x" is "fcmp ord x, 0". |
| 1019 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 1020 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1021 | return Builder->CreateFCmpORD(LHS->getOperand(0), RHS->getOperand(0)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1022 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1023 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1024 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1025 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1028 | /// Match De Morgan's Laws: |
| 1029 | /// (~A & ~B) == (~(A | B)) |
| 1030 | /// (~A | ~B) == (~(A & B)) |
| 1031 | static Instruction *matchDeMorgansLaws(BinaryOperator &I, |
Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 1032 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1033 | auto Opcode = I.getOpcode(); |
| 1034 | assert((Opcode == Instruction::And || Opcode == Instruction::Or) && |
| 1035 | "Trying to match De Morgan's Laws with something other than and/or"); |
| 1036 | |
Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 1037 | // Flip the logic operation. |
| 1038 | Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And; |
| 1039 | |
| 1040 | Value *A, *B; |
| 1041 | if (match(I.getOperand(0), m_OneUse(m_Not(m_Value(A)))) && |
| 1042 | match(I.getOperand(1), m_OneUse(m_Not(m_Value(B)))) && |
| 1043 | !IsFreeToInvert(A, A->hasOneUse()) && |
| 1044 | !IsFreeToInvert(B, B->hasOneUse())) { |
| 1045 | Value *AndOr = Builder.CreateBinOp(Opcode, A, B, I.getName() + ".demorgan"); |
| 1046 | return BinaryOperator::CreateNot(AndOr); |
| 1047 | } |
Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1048 | |
| 1049 | return nullptr; |
| 1050 | } |
| 1051 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 1052 | bool InstCombiner::shouldOptimizeCast(CastInst *CI) { |
| 1053 | Value *CastSrc = CI->getOperand(0); |
| 1054 | |
| 1055 | // Noop casts and casts of constants should be eliminated trivially. |
| 1056 | if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc)) |
| 1057 | return false; |
| 1058 | |
| 1059 | // If this cast is paired with another cast that can be eliminated, we prefer |
| 1060 | // to have it eliminated. |
| 1061 | if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc)) |
| 1062 | if (isEliminableCastPair(PrecedingCI, CI)) |
| 1063 | return false; |
| 1064 | |
| 1065 | // If this is a vector sext from a compare, then we don't want to break the |
| 1066 | // idiom where each element of the extended vector is either zero or all ones. |
| 1067 | if (CI->getOpcode() == Instruction::SExt && |
| 1068 | isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy()) |
| 1069 | return false; |
| 1070 | |
| 1071 | return true; |
| 1072 | } |
| 1073 | |
Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1074 | /// Fold {and,or,xor} (cast X), C. |
| 1075 | static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast, |
| 1076 | InstCombiner::BuilderTy *Builder) { |
| 1077 | Constant *C; |
| 1078 | if (!match(Logic.getOperand(1), m_Constant(C))) |
| 1079 | return nullptr; |
| 1080 | |
| 1081 | auto LogicOpc = Logic.getOpcode(); |
| 1082 | Type *DestTy = Logic.getType(); |
| 1083 | Type *SrcTy = Cast->getSrcTy(); |
| 1084 | |
Sanjay Patel | d1e8119 | 2017-06-22 15:46:54 +0000 | [diff] [blame] | 1085 | // Move the logic operation ahead of a zext if the constant is unchanged in |
| 1086 | // the smaller source type. Performing the logic in a smaller type may provide |
| 1087 | // more information to later folds, and the smaller logic instruction may be |
| 1088 | // cheaper (particularly in the case of vectors). |
Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1089 | Value *X; |
Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1090 | if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) { |
| 1091 | Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy); |
| 1092 | Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy); |
| 1093 | if (ZextTruncC == C) { |
| 1094 | // LogicOpc (zext X), C --> zext (LogicOpc X, C) |
| 1095 | Value *NewOp = Builder->CreateBinOp(LogicOpc, X, TruncC); |
| 1096 | return new ZExtInst(NewOp, DestTy); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | return nullptr; |
| 1101 | } |
| 1102 | |
| 1103 | /// Fold {and,or,xor} (cast X), Y. |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1104 | Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) { |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1105 | auto LogicOpc = I.getOpcode(); |
Sanjay Patel | 1e6ca44 | 2016-11-22 22:54:36 +0000 | [diff] [blame] | 1106 | assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding"); |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1107 | |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1108 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1109 | CastInst *Cast0 = dyn_cast<CastInst>(Op0); |
Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1110 | if (!Cast0) |
Sanjay Patel | 7d0d810 | 2016-02-23 16:59:21 +0000 | [diff] [blame] | 1111 | return nullptr; |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1112 | |
Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1113 | // This must be a cast from an integer or integer vector source type to allow |
| 1114 | // transformation of the logic operation to the source type. |
| 1115 | Type *DestTy = I.getType(); |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1116 | Type *SrcTy = Cast0->getSrcTy(); |
Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1117 | if (!SrcTy->isIntOrIntVectorTy()) |
| 1118 | return nullptr; |
| 1119 | |
Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1120 | if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder)) |
| 1121 | return Ret; |
Sanjay Patel | 0753c06 | 2016-07-21 00:24:18 +0000 | [diff] [blame] | 1122 | |
Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1123 | CastInst *Cast1 = dyn_cast<CastInst>(Op1); |
| 1124 | if (!Cast1) |
| 1125 | return nullptr; |
| 1126 | |
| 1127 | // Both operands of the logic operation are casts. The casts must be of the |
| 1128 | // same type for reduction. |
| 1129 | auto CastOpcode = Cast0->getOpcode(); |
| 1130 | if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy()) |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1131 | return nullptr; |
| 1132 | |
| 1133 | Value *Cast0Src = Cast0->getOperand(0); |
| 1134 | Value *Cast1Src = Cast1->getOperand(0); |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1135 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 1136 | // fold logic(cast(A), cast(B)) -> cast(logic(A, B)) |
Tobias Grosser | 8757e38 | 2016-08-03 19:30:35 +0000 | [diff] [blame] | 1137 | if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) { |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1138 | Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src, |
| 1139 | I.getName()); |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1140 | return CastInst::Create(CastOpcode, NewOp, DestTy); |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1141 | } |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1142 | |
Sanjay Patel | dbbaca0 | 2016-02-24 17:00:34 +0000 | [diff] [blame] | 1143 | // For now, only 'and'/'or' have optimizations after this. |
| 1144 | if (LogicOpc == Instruction::Xor) |
| 1145 | return nullptr; |
| 1146 | |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1147 | // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1148 | // cast is otherwise not optimizable. This happens for vector sexts. |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1149 | ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src); |
| 1150 | ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src); |
| 1151 | if (ICmp0 && ICmp1) { |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1152 | Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 1153 | : foldOrOfICmps(ICmp0, ICmp1, I); |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1154 | if (Res) |
| 1155 | return CastInst::Create(CastOpcode, Res, DestTy); |
| 1156 | return nullptr; |
| 1157 | } |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1158 | |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1159 | // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1160 | // cast is otherwise not optimizable. This happens for vector sexts. |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1161 | FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src); |
| 1162 | FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src); |
| 1163 | if (FCmp0 && FCmp1) { |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 1164 | Value *Res = LogicOpc == Instruction::And ? foldAndOfFCmps(FCmp0, FCmp1) |
| 1165 | : foldOrOfFCmps(FCmp0, FCmp1); |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1166 | if (Res) |
| 1167 | return CastInst::Create(CastOpcode, Res, DestTy); |
| 1168 | return nullptr; |
| 1169 | } |
Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1170 | |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1171 | return nullptr; |
| 1172 | } |
| 1173 | |
Sanjay Patel | 74d23ad | 2016-05-27 21:41:29 +0000 | [diff] [blame] | 1174 | static Instruction *foldBoolSextMaskToSelect(BinaryOperator &I) { |
| 1175 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1176 | |
| 1177 | // Canonicalize SExt or Not to the LHS |
| 1178 | if (match(Op1, m_SExt(m_Value())) || match(Op1, m_Not(m_Value()))) { |
| 1179 | std::swap(Op0, Op1); |
| 1180 | } |
| 1181 | |
| 1182 | // Fold (and (sext bool to A), B) --> (select bool, B, 0) |
| 1183 | Value *X = nullptr; |
| 1184 | if (match(Op0, m_SExt(m_Value(X))) && |
| 1185 | X->getType()->getScalarType()->isIntegerTy(1)) { |
| 1186 | Value *Zero = Constant::getNullValue(Op1->getType()); |
| 1187 | return SelectInst::Create(X, Op1, Zero); |
| 1188 | } |
| 1189 | |
| 1190 | // Fold (and ~(sext bool to A), B) --> (select bool, 0, B) |
| 1191 | if (match(Op0, m_Not(m_SExt(m_Value(X)))) && |
| 1192 | X->getType()->getScalarType()->isIntegerTy(1)) { |
| 1193 | Value *Zero = Constant::getNullValue(Op0->getType()); |
| 1194 | return SelectInst::Create(X, Zero, Op1); |
| 1195 | } |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 1196 | |
Sanjay Patel | 74d23ad | 2016-05-27 21:41:29 +0000 | [diff] [blame] | 1197 | return nullptr; |
| 1198 | } |
| 1199 | |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1200 | static Instruction *foldAndToXor(BinaryOperator &I, |
| 1201 | InstCombiner::BuilderTy &Builder) { |
| 1202 | assert(I.getOpcode() == Instruction::And); |
| 1203 | Value *Op0 = I.getOperand(0); |
| 1204 | Value *Op1 = I.getOperand(1); |
| 1205 | Value *A, *B; |
| 1206 | |
| 1207 | // Operand complexity canonicalization guarantees that the 'or' is Op0. |
| 1208 | // (A | B) & ~(A & B) --> A ^ B |
| 1209 | // (A | B) & ~(B & A) --> A ^ B |
| 1210 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 1211 | match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) |
| 1212 | return BinaryOperator::CreateXor(A, B); |
| 1213 | |
| 1214 | // (A | ~B) & (~A | B) --> ~(A ^ B) |
| 1215 | // (A | ~B) & (B | ~A) --> ~(A ^ B) |
| 1216 | // (~B | A) & (~A | B) --> ~(A ^ B) |
| 1217 | // (~B | A) & (B | ~A) --> ~(A ^ B) |
Craig Topper | 0de5e6a | 2017-06-22 16:12:02 +0000 | [diff] [blame] | 1218 | if (Op0->hasOneUse() || Op1->hasOneUse()) |
| 1219 | if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) && |
| 1220 | match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B)))) |
| 1221 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1222 | |
| 1223 | return nullptr; |
| 1224 | } |
| 1225 | |
| 1226 | static Instruction *foldOrToXor(BinaryOperator &I, |
| 1227 | InstCombiner::BuilderTy &Builder) { |
| 1228 | assert(I.getOpcode() == Instruction::Or); |
| 1229 | Value *Op0 = I.getOperand(0); |
| 1230 | Value *Op1 = I.getOperand(1); |
| 1231 | Value *A, *B; |
| 1232 | |
| 1233 | // Operand complexity canonicalization guarantees that the 'and' is Op0. |
| 1234 | // (A & B) | ~(A | B) --> ~(A ^ B) |
| 1235 | // (A & B) | ~(B | A) --> ~(A ^ B) |
Craig Topper | 0de5e6a | 2017-06-22 16:12:02 +0000 | [diff] [blame] | 1236 | if (Op0->hasOneUse() || Op1->hasOneUse()) |
| 1237 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 1238 | match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
| 1239 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1240 | |
| 1241 | // (A & ~B) | (~A & B) --> A ^ B |
| 1242 | // (A & ~B) | (B & ~A) --> A ^ B |
| 1243 | // (~B & A) | (~A & B) --> A ^ B |
| 1244 | // (~B & A) | (B & ~A) --> A ^ B |
| 1245 | if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) && |
| 1246 | match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B)))) |
| 1247 | return BinaryOperator::CreateXor(A, B); |
| 1248 | |
| 1249 | return nullptr; |
| 1250 | } |
| 1251 | |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1252 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 1253 | // here. We should standardize that construct where it is needed or choose some |
| 1254 | // other way to ensure that commutated variants of patterns are not missed. |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1255 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 1256 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1257 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1258 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1259 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1260 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1261 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1262 | if (Value *V = SimplifyAndInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1263 | return replaceInstUsesWith(I, V); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1264 | |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1265 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1266 | // purpose is to compute bits we don't care about. |
| 1267 | if (SimplifyDemandedInstructionBits(I)) |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1268 | return &I; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1269 | |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1270 | // Do this before using distributive laws to catch simple and/or/not patterns. |
| 1271 | if (Instruction *Xor = foldAndToXor(I, *Builder)) |
| 1272 | return Xor; |
| 1273 | |
| 1274 | // (A|B)&(A|C) -> A|(B&C) etc |
| 1275 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 1276 | return replaceInstUsesWith(I, V); |
| 1277 | |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 1278 | if (Value *V = SimplifyBSwap(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1279 | return replaceInstUsesWith(I, V); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1281 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
| 1282 | const APInt &AndRHSMask = AndRHS->getValue(); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1285 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 1286 | Value *Op0LHS = Op0I->getOperand(0); |
| 1287 | Value *Op0RHS = Op0I->getOperand(1); |
| 1288 | switch (Op0I->getOpcode()) { |
| 1289 | default: break; |
| 1290 | case Instruction::Xor: |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1291 | case Instruction::Or: { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1292 | // If the mask is only needed on one incoming arm, push it up. |
| 1293 | if (!Op0I->hasOneUse()) break; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1294 | |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1295 | APInt NotAndRHS(~AndRHSMask); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1296 | if (MaskedValueIsZero(Op0LHS, NotAndRHS, 0, &I)) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1297 | // Not masking anything out for the LHS, move to RHS. |
| 1298 | Value *NewRHS = Builder->CreateAnd(Op0RHS, AndRHS, |
| 1299 | Op0RHS->getName()+".masked"); |
| 1300 | return BinaryOperator::Create(Op0I->getOpcode(), Op0LHS, NewRHS); |
| 1301 | } |
| 1302 | if (!isa<Constant>(Op0RHS) && |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1303 | MaskedValueIsZero(Op0RHS, NotAndRHS, 0, &I)) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1304 | // Not masking anything out for the RHS, move to LHS. |
| 1305 | Value *NewLHS = Builder->CreateAnd(Op0LHS, AndRHS, |
| 1306 | Op0LHS->getName()+".masked"); |
| 1307 | return BinaryOperator::Create(Op0I->getOpcode(), NewLHS, Op0RHS); |
| 1308 | } |
| 1309 | |
| 1310 | break; |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1311 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1312 | case Instruction::Sub: |
Balaram Makam | ccf5973 | 2015-08-20 15:35:00 +0000 | [diff] [blame] | 1313 | // -x & 1 -> x & 1 |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1314 | if (AndRHSMask.isOneValue() && match(Op0LHS, m_Zero())) |
Balaram Makam | ccf5973 | 2015-08-20 15:35:00 +0000 | [diff] [blame] | 1315 | return BinaryOperator::CreateAnd(Op0RHS, AndRHS); |
| 1316 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1317 | break; |
| 1318 | |
| 1319 | case Instruction::Shl: |
| 1320 | case Instruction::LShr: |
| 1321 | // (1 << x) & 1 --> zext(x == 0) |
| 1322 | // (1 >> x) & 1 --> zext(x == 0) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1323 | if (AndRHSMask.isOneValue() && Op0LHS == AndRHS) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1324 | Value *NewICmp = |
| 1325 | Builder->CreateICmpEQ(Op0RHS, Constant::getNullValue(I.getType())); |
| 1326 | return new ZExtInst(NewICmp, I.getType()); |
| 1327 | } |
| 1328 | break; |
| 1329 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1330 | |
David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1331 | // ((C1 OP zext(X)) & C2) -> zext((C1-X) & C2) if C2 fits in the bitwidth |
| 1332 | // of X and OP behaves well when given trunc(C1) and X. |
| 1333 | switch (Op0I->getOpcode()) { |
| 1334 | default: |
| 1335 | break; |
| 1336 | case Instruction::Xor: |
| 1337 | case Instruction::Or: |
| 1338 | case Instruction::Mul: |
| 1339 | case Instruction::Add: |
| 1340 | case Instruction::Sub: |
| 1341 | Value *X; |
| 1342 | ConstantInt *C1; |
Craig Topper | b5194ee | 2017-04-12 05:49:28 +0000 | [diff] [blame] | 1343 | if (match(Op0I, m_c_BinOp(m_ZExt(m_Value(X)), m_ConstantInt(C1)))) { |
David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1344 | if (AndRHSMask.isIntN(X->getType()->getScalarSizeInBits())) { |
| 1345 | auto *TruncC1 = ConstantExpr::getTrunc(C1, X->getType()); |
| 1346 | Value *BinOp; |
| 1347 | if (isa<ZExtInst>(Op0LHS)) |
| 1348 | BinOp = Builder->CreateBinOp(Op0I->getOpcode(), X, TruncC1); |
| 1349 | else |
| 1350 | BinOp = Builder->CreateBinOp(Op0I->getOpcode(), TruncC1, X); |
| 1351 | auto *TruncC2 = ConstantExpr::getTrunc(AndRHS, X->getType()); |
| 1352 | auto *And = Builder->CreateAnd(BinOp, TruncC2); |
| 1353 | return new ZExtInst(And, I.getType()); |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1358 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
| 1359 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
| 1360 | return Res; |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1361 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1363 | // If this is an integer truncation, and if the source is an 'and' with |
| 1364 | // immediate, transform it. This frequently occurs for bitfield accesses. |
| 1365 | { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1366 | Value *X = nullptr; ConstantInt *YC = nullptr; |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1367 | if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) { |
| 1368 | // Change: and (trunc (and X, YC) to T), C2 |
| 1369 | // into : and (trunc X to T), trunc(YC) & C2 |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1370 | // This will fold the two constants together, which may allow |
Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1371 | // other simplifications. |
| 1372 | Value *NewCast = Builder->CreateTrunc(X, I.getType(), "and.shrunk"); |
| 1373 | Constant *C3 = ConstantExpr::getTrunc(YC, I.getType()); |
| 1374 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
| 1375 | return BinaryOperator::CreateAnd(NewCast, C3); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 1378 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1379 | |
Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 1380 | if (isa<Constant>(Op1)) |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 1381 | if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) |
| 1382 | return FoldedLogic; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1383 | |
Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 1384 | if (Instruction *DeMorgan = matchDeMorgansLaws(I, *Builder)) |
Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1385 | return DeMorgan; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1387 | { |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1388 | Value *A = nullptr, *B = nullptr, *C = nullptr; |
Eli Friedman | 61d7c8a | 2011-09-19 21:58:15 +0000 | [diff] [blame] | 1389 | // A&(A^B) => A & ~B |
| 1390 | { |
| 1391 | Value *tmpOp0 = Op0; |
| 1392 | Value *tmpOp1 = Op1; |
Sanjay Patel | 7b7eec1 | 2016-01-18 18:36:38 +0000 | [diff] [blame] | 1393 | if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) { |
Eli Friedman | 61d7c8a | 2011-09-19 21:58:15 +0000 | [diff] [blame] | 1394 | if (A == Op1 || B == Op1 ) { |
| 1395 | tmpOp1 = Op0; |
| 1396 | tmpOp0 = Op1; |
| 1397 | // Simplify below |
| 1398 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1399 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1400 | |
Sanjay Patel | 7b7eec1 | 2016-01-18 18:36:38 +0000 | [diff] [blame] | 1401 | if (match(tmpOp1, m_OneUse(m_Xor(m_Value(A), m_Value(B))))) { |
Eli Friedman | 61d7c8a | 2011-09-19 21:58:15 +0000 | [diff] [blame] | 1402 | if (B == tmpOp0) { |
| 1403 | std::swap(A, B); |
| 1404 | } |
Sanjay Patel | d09b44a | 2016-01-18 17:50:23 +0000 | [diff] [blame] | 1405 | // Notice that the pattern (A&(~B)) is actually (A&(-1^B)), so if |
Eli Friedman | 61d7c8a | 2011-09-19 21:58:15 +0000 | [diff] [blame] | 1406 | // A is originally -1 (or a vector of -1 and undefs), then we enter |
| 1407 | // an endless loop. By checking that A is non-constant we ensure that |
| 1408 | // we will never get to the loop. |
| 1409 | if (A == tmpOp0 && !isa<Constant>(A)) // A&(A^B) -> A & ~B |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1410 | return BinaryOperator::CreateAnd(A, Builder->CreateNot(B)); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1411 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | // (A&((~A)|B)) -> A&B |
Craig Topper | c4b48a3 | 2017-04-25 06:02:11 +0000 | [diff] [blame] | 1415 | if (match(Op0, m_c_Or(m_Not(m_Specific(Op1)), m_Value(A)))) |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1416 | return BinaryOperator::CreateAnd(A, Op1); |
Craig Topper | c4b48a3 | 2017-04-25 06:02:11 +0000 | [diff] [blame] | 1417 | if (match(Op1, m_c_Or(m_Not(m_Specific(Op0)), m_Value(A)))) |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1418 | return BinaryOperator::CreateAnd(A, Op0); |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 1419 | |
| 1420 | // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C |
| 1421 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) |
| 1422 | if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) |
Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 1423 | if (Op1->hasOneUse() || IsFreeToInvert(C, C->hasOneUse())) |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 1424 | return BinaryOperator::CreateAnd(Op0, Builder->CreateNot(C)); |
| 1425 | |
| 1426 | // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C |
| 1427 | if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) |
| 1428 | if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) |
Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 1429 | if (Op0->hasOneUse() || IsFreeToInvert(C, C->hasOneUse())) |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 1430 | return BinaryOperator::CreateAnd(Op1, Builder->CreateNot(C)); |
Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1431 | |
| 1432 | // (A | B) & ((~A) ^ B) -> (A & B) |
Craig Topper | ba01143 | 2017-04-25 15:19:04 +0000 | [diff] [blame] | 1433 | // (A | B) & (B ^ (~A)) -> (A & B) |
| 1434 | // (B | A) & ((~A) ^ B) -> (A & B) |
| 1435 | // (B | A) & (B ^ (~A)) -> (A & B) |
| 1436 | if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) && |
| 1437 | match(Op0, m_c_Or(m_Specific(A), m_Specific(B)))) |
Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1438 | return BinaryOperator::CreateAnd(A, B); |
| 1439 | |
| 1440 | // ((~A) ^ B) & (A | B) -> (A & B) |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1441 | // ((~A) ^ B) & (B | A) -> (A & B) |
Craig Topper | ba01143 | 2017-04-25 15:19:04 +0000 | [diff] [blame] | 1442 | // (B ^ (~A)) & (A | B) -> (A & B) |
| 1443 | // (B ^ (~A)) & (B | A) -> (A & B) |
| 1444 | if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) && |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1445 | match(Op1, m_c_Or(m_Specific(A), m_Specific(B)))) |
Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1446 | return BinaryOperator::CreateAnd(A, B); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1447 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1448 | |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1449 | { |
| 1450 | ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); |
| 1451 | ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); |
| 1452 | if (LHS && RHS) |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1453 | if (Value *Res = foldAndOfICmps(LHS, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1454 | return replaceInstUsesWith(I, Res); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1455 | |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1456 | // TODO: Make this recursive; it's a little tricky because an arbitrary |
| 1457 | // number of 'and' instructions might have to be created. |
| 1458 | Value *X, *Y; |
| 1459 | if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { |
| 1460 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1461 | if (Value *Res = foldAndOfICmps(LHS, Cmp, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1462 | return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y)); |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1463 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1464 | if (Value *Res = foldAndOfICmps(LHS, Cmp, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1465 | return replaceInstUsesWith(I, Builder->CreateAnd(Res, X)); |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1466 | } |
| 1467 | if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { |
| 1468 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1469 | if (Value *Res = foldAndOfICmps(Cmp, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1470 | return replaceInstUsesWith(I, Builder->CreateAnd(Res, Y)); |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1471 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1472 | if (Value *Res = foldAndOfICmps(Cmp, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1473 | return replaceInstUsesWith(I, Builder->CreateAnd(Res, X)); |
David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 1477 | // If and'ing two fcmp, try combine them into one. |
| 1478 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) |
| 1479 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 1480 | if (Value *Res = foldAndOfFCmps(LHS, RHS)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1481 | return replaceInstUsesWith(I, Res); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1482 | |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1483 | if (Instruction *CastedAnd = foldCastedBitwiseLogic(I)) |
| 1484 | return CastedAnd; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1485 | |
Sanjay Patel | 74d23ad | 2016-05-27 21:41:29 +0000 | [diff] [blame] | 1486 | if (Instruction *Select = foldBoolSextMaskToSelect(I)) |
| 1487 | return Select; |
Nadav Rotem | 513bd8a | 2013-01-30 06:35:22 +0000 | [diff] [blame] | 1488 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1489 | return Changed ? &I : nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Chad Rosier | a00df49 | 2016-05-25 16:22:14 +0000 | [diff] [blame] | 1492 | /// Given an OR instruction, check to see if this is a bswap idiom. If so, |
| 1493 | /// insert the new intrinsic and return it. |
| 1494 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
Chad Rosier | e5819e2 | 2016-05-26 14:58:51 +0000 | [diff] [blame] | 1495 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1496 | |
| 1497 | // Look through zero extends. |
| 1498 | if (Instruction *Ext = dyn_cast<ZExtInst>(Op0)) |
| 1499 | Op0 = Ext->getOperand(0); |
| 1500 | |
| 1501 | if (Instruction *Ext = dyn_cast<ZExtInst>(Op1)) |
| 1502 | Op1 = Ext->getOperand(0); |
| 1503 | |
| 1504 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 1505 | bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) || |
| 1506 | match(Op1, m_Or(m_Value(), m_Value())); |
| 1507 | |
| 1508 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
| 1509 | bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) && |
| 1510 | match(Op1, m_LogicalShift(m_Value(), m_Value())); |
| 1511 | |
| 1512 | // (A & B) | (C & D) -> bswap if possible. |
| 1513 | bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) && |
| 1514 | match(Op1, m_And(m_Value(), m_Value())); |
| 1515 | |
| 1516 | if (!OrOfOrs && !OrOfShifts && !OrOfAnds) |
| 1517 | return nullptr; |
| 1518 | |
James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1519 | SmallVector<Instruction*, 4> Insts; |
Chad Rosier | a00df49 | 2016-05-25 16:22:14 +0000 | [diff] [blame] | 1520 | if (!recognizeBSwapOrBitReverseIdiom(&I, true, false, Insts)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1521 | return nullptr; |
James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1522 | Instruction *LastInst = Insts.pop_back_val(); |
| 1523 | LastInst->removeFromParent(); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1524 | |
James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1525 | for (auto *Inst : Insts) |
| 1526 | Worklist.Add(Inst); |
| 1527 | return LastInst; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1530 | /// If all elements of two constant vectors are 0/-1 and inverses, return true. |
| 1531 | static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) { |
| 1532 | unsigned NumElts = C1->getType()->getVectorNumElements(); |
| 1533 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1534 | Constant *EltC1 = C1->getAggregateElement(i); |
| 1535 | Constant *EltC2 = C2->getAggregateElement(i); |
| 1536 | if (!EltC1 || !EltC2) |
| 1537 | return false; |
| 1538 | |
| 1539 | // One element must be all ones, and the other must be all zeros. |
| 1540 | // FIXME: Allow undef elements. |
| 1541 | if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) || |
| 1542 | (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes())))) |
| 1543 | return false; |
| 1544 | } |
| 1545 | return true; |
| 1546 | } |
| 1547 | |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1548 | /// We have an expression of the form (A & C) | (B & D). If A is a scalar or |
| 1549 | /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of |
| 1550 | /// B, it can be used as the condition operand of a select instruction. |
Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1551 | static Value *getSelectCondition(Value *A, Value *B, |
| 1552 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1553 | // If these are scalars or vectors of i1, A can be used directly. |
| 1554 | Type *Ty = A->getType(); |
| 1555 | if (match(A, m_Not(m_Specific(B))) && Ty->getScalarType()->isIntegerTy(1)) |
| 1556 | return A; |
| 1557 | |
| 1558 | // If A and B are sign-extended, look through the sexts to find the booleans. |
| 1559 | Value *Cond; |
Sanjay Patel | d1e8119 | 2017-06-22 15:46:54 +0000 | [diff] [blame] | 1560 | Value *NotB; |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1561 | if (match(A, m_SExt(m_Value(Cond))) && |
| 1562 | Cond->getType()->getScalarType()->isIntegerTy(1) && |
Sanjay Patel | d1e8119 | 2017-06-22 15:46:54 +0000 | [diff] [blame] | 1563 | match(B, m_OneUse(m_Not(m_Value(NotB))))) { |
| 1564 | NotB = peekThroughBitcast(NotB, true); |
| 1565 | if (match(NotB, m_SExt(m_Specific(Cond)))) |
| 1566 | return Cond; |
| 1567 | } |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1568 | |
Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1569 | // All scalar (and most vector) possibilities should be handled now. |
| 1570 | // Try more matches that only apply to non-splat constant vectors. |
| 1571 | if (!Ty->isVectorTy()) |
| 1572 | return nullptr; |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1573 | |
Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1574 | // If both operands are constants, see if the constants are inverse bitmasks. |
| 1575 | Constant *AC, *BC; |
| 1576 | if (match(A, m_Constant(AC)) && match(B, m_Constant(BC)) && |
| 1577 | areInverseVectorBitmasks(AC, BC)) |
| 1578 | return ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty)); |
| 1579 | |
| 1580 | // If both operands are xor'd with constants using the same sexted boolean |
| 1581 | // operand, see if the constants are inverse bitmasks. |
| 1582 | if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AC)))) && |
| 1583 | match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BC)))) && |
| 1584 | Cond->getType()->getScalarType()->isIntegerTy(1) && |
| 1585 | areInverseVectorBitmasks(AC, BC)) { |
| 1586 | AC = ConstantExpr::getTrunc(AC, CmpInst::makeCmpResultType(Ty)); |
| 1587 | return Builder.CreateXor(Cond, AC); |
| 1588 | } |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1589 | return nullptr; |
| 1590 | } |
| 1591 | |
| 1592 | /// We have an expression of the form (A & C) | (B & D). Try to simplify this |
| 1593 | /// to "A' ? C : D", where A' is a boolean or vector of booleans. |
Sanjay Patel | 4e8ebce | 2016-06-24 18:55:27 +0000 | [diff] [blame] | 1594 | static Value *matchSelectFromAndOr(Value *A, Value *C, Value *B, Value *D, |
Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1595 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 4e8ebce | 2016-06-24 18:55:27 +0000 | [diff] [blame] | 1596 | // The potential condition of the select may be bitcasted. In that case, look |
| 1597 | // through its bitcast and the corresponding bitcast of the 'not' condition. |
| 1598 | Type *OrigType = A->getType(); |
Sanjay Patel | e800df8e | 2017-06-22 15:28:01 +0000 | [diff] [blame] | 1599 | A = peekThroughBitcast(A, true); |
| 1600 | B = peekThroughBitcast(B, true); |
Sanjay Patel | 6cf18af | 2016-06-03 14:42:07 +0000 | [diff] [blame] | 1601 | |
Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1602 | if (Value *Cond = getSelectCondition(A, B, Builder)) { |
Sanjay Patel | 6cf18af | 2016-06-03 14:42:07 +0000 | [diff] [blame] | 1603 | // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D)) |
Sanjay Patel | 4e8ebce | 2016-06-24 18:55:27 +0000 | [diff] [blame] | 1604 | // The bitcasts will either all exist or all not exist. The builder will |
| 1605 | // not create unnecessary casts if the types already match. |
| 1606 | Value *BitcastC = Builder.CreateBitCast(C, A->getType()); |
| 1607 | Value *BitcastD = Builder.CreateBitCast(D, A->getType()); |
| 1608 | Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD); |
| 1609 | return Builder.CreateBitCast(Select, OrigType); |
Sanjay Patel | 6cf18af | 2016-06-03 14:42:07 +0000 | [diff] [blame] | 1610 | } |
Sanjay Patel | 5c0bc02 | 2016-06-02 18:03:05 +0000 | [diff] [blame] | 1611 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1612 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 1615 | /// Fold (icmp)|(icmp) if possible. |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 1616 | Value *InstCombiner::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 1617 | Instruction &CxtI) { |
Nadav Rotem | 0ed2fdb | 2013-11-12 22:38:59 +0000 | [diff] [blame] | 1618 | // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) |
| 1619 | // if K1 and K2 are a one-bit mask. |
Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1620 | if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, false, CxtI)) |
| 1621 | return V; |
| 1622 | |
| 1623 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| 1624 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1625 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 1626 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
Nadav Rotem | 0ed2fdb | 2013-11-12 22:38:59 +0000 | [diff] [blame] | 1627 | |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1628 | // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3) |
| 1629 | // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3) |
| 1630 | // The original condition actually refers to the following two ranges: |
| 1631 | // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3] |
| 1632 | // We can fold these two ranges if: |
| 1633 | // 1) C1 and C2 is unsigned greater than C3. |
| 1634 | // 2) The two ranges are separated. |
| 1635 | // 3) C1 ^ C2 is one-bit mask. |
| 1636 | // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask. |
| 1637 | // This implies all values in the two ranges differ by exactly one bit. |
| 1638 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1639 | if ((PredL == ICmpInst::ICMP_ULT || PredL == ICmpInst::ICMP_ULE) && |
| 1640 | PredL == PredR && LHSC && RHSC && LHS->hasOneUse() && RHS->hasOneUse() && |
| 1641 | LHSC->getType() == RHSC->getType() && |
| 1642 | LHSC->getValue() == (RHSC->getValue())) { |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1643 | |
| 1644 | Value *LAdd = LHS->getOperand(0); |
| 1645 | Value *RAdd = RHS->getOperand(0); |
| 1646 | |
| 1647 | Value *LAddOpnd, *RAddOpnd; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1648 | ConstantInt *LAddC, *RAddC; |
| 1649 | if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddC))) && |
| 1650 | match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddC))) && |
| 1651 | LAddC->getValue().ugt(LHSC->getValue()) && |
| 1652 | RAddC->getValue().ugt(LHSC->getValue())) { |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1653 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1654 | APInt DiffC = LAddC->getValue() ^ RAddC->getValue(); |
| 1655 | if (LAddOpnd == RAddOpnd && DiffC.isPowerOf2()) { |
| 1656 | ConstantInt *MaxAddC = nullptr; |
| 1657 | if (LAddC->getValue().ult(RAddC->getValue())) |
| 1658 | MaxAddC = RAddC; |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1659 | else |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1660 | MaxAddC = LAddC; |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1661 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1662 | APInt RRangeLow = -RAddC->getValue(); |
| 1663 | APInt RRangeHigh = RRangeLow + LHSC->getValue(); |
| 1664 | APInt LRangeLow = -LAddC->getValue(); |
| 1665 | APInt LRangeHigh = LRangeLow + LHSC->getValue(); |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1666 | APInt LowRangeDiff = RRangeLow ^ LRangeLow; |
| 1667 | APInt HighRangeDiff = RRangeHigh ^ LRangeHigh; |
| 1668 | APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow |
| 1669 | : RRangeLow - LRangeLow; |
| 1670 | |
| 1671 | if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff && |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1672 | RangeDiff.ugt(LHSC->getValue())) { |
| 1673 | Value *MaskC = ConstantInt::get(LAddC->getType(), ~DiffC); |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1674 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1675 | Value *NewAnd = Builder->CreateAnd(LAddOpnd, MaskC); |
| 1676 | Value *NewAdd = Builder->CreateAdd(NewAnd, MaxAddC); |
| 1677 | return (Builder->CreateICmp(LHS->getPredicate(), NewAdd, LHSC)); |
Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1683 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1684 | if (PredicatesFoldable(PredL, PredR)) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1685 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 1686 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 1687 | LHS->swapOperands(); |
| 1688 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 1689 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 1690 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 1691 | unsigned Code = getICmpCode(LHS) | getICmpCode(RHS); |
| 1692 | bool isSigned = LHS->isSigned() || RHS->isSigned(); |
Pete Cooper | ebf98c1 | 2011-12-17 01:20:32 +0000 | [diff] [blame] | 1693 | return getNewICmpValue(isSigned, Code, Op0, Op1, Builder); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1694 | } |
| 1695 | } |
Benjamin Kramer | 2bca3a6 | 2010-12-20 16:21:59 +0000 | [diff] [blame] | 1696 | |
| 1697 | // handle (roughly): |
| 1698 | // (icmp ne (A & B), C) | (icmp ne (A & D), E) |
Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 1699 | if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder)) |
Benjamin Kramer | 2bca3a6 | 2010-12-20 16:21:59 +0000 | [diff] [blame] | 1700 | return V; |
Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 1701 | |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1702 | Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); |
David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 1703 | if (LHS->hasOneUse() || RHS->hasOneUse()) { |
| 1704 | // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1) |
| 1705 | // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1706 | Value *A = nullptr, *B = nullptr; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1707 | if (PredL == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero()) { |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1708 | B = LHS0; |
| 1709 | if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS->getOperand(1)) |
| 1710 | A = RHS0; |
| 1711 | else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0) |
David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 1712 | A = RHS->getOperand(1); |
| 1713 | } |
| 1714 | // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1) |
| 1715 | // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1716 | else if (PredR == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) { |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1717 | B = RHS0; |
| 1718 | if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS->getOperand(1)) |
| 1719 | A = LHS0; |
| 1720 | else if (PredL == ICmpInst::ICMP_UGT && LHS0 == RHS0) |
David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 1721 | A = LHS->getOperand(1); |
| 1722 | } |
| 1723 | if (A && B) |
| 1724 | return Builder->CreateICmp( |
| 1725 | ICmpInst::ICMP_UGE, |
| 1726 | Builder->CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A); |
| 1727 | } |
| 1728 | |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 1729 | // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n |
| 1730 | if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true)) |
| 1731 | return V; |
| 1732 | |
| 1733 | // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n |
| 1734 | if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true)) |
| 1735 | return V; |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 1736 | |
Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 1737 | if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder)) |
| 1738 | return V; |
| 1739 | |
David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 1740 | // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1741 | if (!LHSC || !RHSC) |
| 1742 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1743 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1744 | if (LHSC == RHSC && PredL == PredR) { |
Owen Anderson | 8f306a7 | 2010-08-02 09:32:13 +0000 | [diff] [blame] | 1745 | // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1746 | if (PredL == ICmpInst::ICMP_NE && LHSC->isZero()) { |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1747 | Value *NewOr = Builder->CreateOr(LHS0, RHS0); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1748 | return Builder->CreateICmp(PredL, NewOr, LHSC); |
Owen Anderson | 8f306a7 | 2010-08-02 09:32:13 +0000 | [diff] [blame] | 1749 | } |
Benjamin Kramer | da37e15 | 2012-01-08 18:32:24 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Benjamin Kramer | f7957d0 | 2010-12-20 20:00:31 +0000 | [diff] [blame] | 1752 | // (icmp ult (X + CA), C1) | (icmp eq X, C2) -> (icmp ule (X + CA), C1) |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1753 | // iff C2 + CA == C1. |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1754 | if (PredL == ICmpInst::ICMP_ULT && PredR == ICmpInst::ICMP_EQ) { |
| 1755 | ConstantInt *AddC; |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1756 | if (match(LHS0, m_Add(m_Specific(RHS0), m_ConstantInt(AddC)))) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1757 | if (RHSC->getValue() + AddC->getValue() == LHSC->getValue()) |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1758 | return Builder->CreateICmpULE(LHS0, LHSC); |
Benjamin Kramer | 68531ba | 2010-12-20 16:18:51 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1761 | // From here on, we only handle: |
| 1762 | // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1763 | if (LHS0 != RHS0) |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1764 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1765 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1766 | // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere. |
| 1767 | if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE || |
| 1768 | PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE || |
| 1769 | PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE || |
| 1770 | PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1771 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1772 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1773 | // We can't fold (ugt x, C) | (sgt x, C2). |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1774 | if (!PredicatesFoldable(PredL, PredR)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1775 | return nullptr; |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1777 | // Ensure that the larger constant is on the RHS. |
| 1778 | bool ShouldSwap; |
Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 1779 | if (CmpInst::isSigned(PredL) || |
| 1780 | (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR))) |
Sanjay Patel | 570e35c | 2017-04-10 16:55:57 +0000 | [diff] [blame] | 1781 | ShouldSwap = LHSC->getValue().sgt(RHSC->getValue()); |
Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 1782 | else |
| 1783 | ShouldSwap = LHSC->getValue().ugt(RHSC->getValue()); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1785 | if (ShouldSwap) { |
| 1786 | std::swap(LHS, RHS); |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1787 | std::swap(LHSC, RHSC); |
| 1788 | std::swap(PredL, PredR); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1789 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1790 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1791 | // At this point, we know we have two icmp instructions |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1792 | // comparing a value against two constants and or'ing the result |
| 1793 | // together. Because of the above check, we know that we only have |
| 1794 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 1795 | // icmp folding check above), that the two constants are not |
| 1796 | // equal. |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1797 | assert(LHSC != RHSC && "Compares not folded above?"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1798 | |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1799 | switch (PredL) { |
| 1800 | default: |
| 1801 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1802 | case ICmpInst::ICMP_EQ: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1803 | switch (PredR) { |
| 1804 | default: |
| 1805 | llvm_unreachable("Unknown integer condition code!"); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1806 | case ICmpInst::ICMP_EQ: |
Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 1807 | // Potential folds for this case should already be handled. |
| 1808 | break; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1809 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 1810 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1811 | break; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1812 | } |
| 1813 | break; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1814 | case ICmpInst::ICMP_ULT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1815 | switch (PredR) { |
| 1816 | default: |
| 1817 | llvm_unreachable("Unknown integer condition code!"); |
| 1818 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1819 | break; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1820 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1821 | assert(!RHSC->isMaxValue(false) && "Missed icmp simplification"); |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1822 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1, |
| 1823 | false, false); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1824 | } |
| 1825 | break; |
| 1826 | case ICmpInst::ICMP_SLT: |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1827 | switch (PredR) { |
| 1828 | default: |
| 1829 | llvm_unreachable("Unknown integer condition code!"); |
| 1830 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1831 | break; |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1832 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 |
Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 1833 | assert(!RHSC->isMaxValue(true) && "Missed icmp simplification"); |
Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1834 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1, true, |
Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1835 | false); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1836 | } |
| 1837 | break; |
| 1838 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1839 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 1842 | /// Optimize (fcmp)|(fcmp). NOTE: Unlike the rest of instcombine, this returns |
| 1843 | /// a Value which should already be inserted into the function. |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 1844 | Value *InstCombiner::foldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS) { |
Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 1845 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 1846 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 1847 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 1848 | |
| 1849 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 1850 | // Swap RHS operands to match LHS. |
| 1851 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 1852 | std::swap(Op1LHS, Op1RHS); |
| 1853 | } |
| 1854 | |
| 1855 | // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y). |
| 1856 | // This is a similar transformation to the one in FoldAndOfFCmps. |
| 1857 | // |
| 1858 | // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this: |
| 1859 | // bool(R & CC0) || bool(R & CC1) |
| 1860 | // = bool((R & CC0) | (R & CC1)) |
| 1861 | // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;) |
| 1862 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) |
| 1863 | return getFCmpValue(getFCmpCode(Op0CC) | getFCmpCode(Op1CC), Op0LHS, Op0RHS, |
| 1864 | Builder); |
| 1865 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1866 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1867 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1868 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { |
| 1869 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 1870 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 1871 | // If either of the constants are nans, then the whole thing returns |
| 1872 | // true. |
| 1873 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 1874 | return Builder->getTrue(); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1875 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1876 | // Otherwise, no need to compare the two constants, compare the |
| 1877 | // rest. |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1878 | return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0)); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1879 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1881 | // Handle vector zeros. This occurs because the canonical form of |
| 1882 | // "fcmp uno x,x" is "fcmp uno x, 0". |
| 1883 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 1884 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1885 | return Builder->CreateFCmpUNO(LHS->getOperand(0), RHS->getOperand(0)); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1886 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1887 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1888 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1889 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1890 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 1893 | /// This helper function folds: |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1894 | /// |
| 1895 | /// ((A | B) & C1) | (B & C2) |
| 1896 | /// |
| 1897 | /// into: |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1898 | /// |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1899 | /// (A & C1) | B |
| 1900 | /// |
| 1901 | /// when the XOR of the two constants is "all ones" (-1). |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 1902 | static Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, |
| 1903 | Value *A, Value *B, Value *C, |
| 1904 | InstCombiner::BuilderTy *Builder) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1905 | ConstantInt *CI1 = dyn_cast<ConstantInt>(C); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1906 | if (!CI1) return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1907 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1908 | Value *V1 = nullptr; |
| 1909 | ConstantInt *CI2 = nullptr; |
| 1910 | if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1911 | |
| 1912 | APInt Xor = CI1->getValue() ^ CI2->getValue(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1913 | if (!Xor.isAllOnesValue()) return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1914 | |
| 1915 | if (V1 == A || V1 == B) { |
| 1916 | Value *NewOp = Builder->CreateAnd((V1 == A) ? B : A, CI1); |
| 1917 | return BinaryOperator::CreateOr(NewOp, V1); |
| 1918 | } |
| 1919 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1920 | return nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 1923 | /// \brief This helper function folds: |
| 1924 | /// |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 1925 | /// ((A ^ B) & C1) | (B & C2) |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 1926 | /// |
| 1927 | /// into: |
| 1928 | /// |
| 1929 | /// (A & C1) ^ B |
| 1930 | /// |
| 1931 | /// when the XOR of the two constants is "all ones" (-1). |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 1932 | static Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, |
| 1933 | Value *A, Value *B, Value *C, |
| 1934 | InstCombiner::BuilderTy *Builder) { |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 1935 | ConstantInt *CI1 = dyn_cast<ConstantInt>(C); |
| 1936 | if (!CI1) |
| 1937 | return nullptr; |
| 1938 | |
| 1939 | Value *V1 = nullptr; |
| 1940 | ConstantInt *CI2 = nullptr; |
| 1941 | if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) |
| 1942 | return nullptr; |
| 1943 | |
| 1944 | APInt Xor = CI1->getValue() ^ CI2->getValue(); |
| 1945 | if (!Xor.isAllOnesValue()) |
| 1946 | return nullptr; |
| 1947 | |
| 1948 | if (V1 == A || V1 == B) { |
| 1949 | Value *NewOp = Builder->CreateAnd(V1 == A ? B : A, CI1); |
| 1950 | return BinaryOperator::CreateXor(NewOp, V1); |
| 1951 | } |
| 1952 | |
| 1953 | return nullptr; |
| 1954 | } |
| 1955 | |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1956 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 1957 | // here. We should standardize that construct where it is needed or choose some |
| 1958 | // other way to ensure that commutated variants of patterns are not missed. |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1959 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 1960 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1961 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1962 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1963 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1964 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1965 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1966 | if (Value *V = SimplifyOrInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1967 | return replaceInstUsesWith(I, V); |
Bill Wendling | af13d82 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 1968 | |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1969 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1970 | // purpose is to compute bits we don't care about. |
| 1971 | if (SimplifyDemandedInstructionBits(I)) |
| 1972 | return &I; |
| 1973 | |
Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1974 | // Do this before using distributive laws to catch simple and/or/not patterns. |
| 1975 | if (Instruction *Xor = foldOrToXor(I, *Builder)) |
| 1976 | return Xor; |
| 1977 | |
| 1978 | // (A&B)|(A&C) -> A&(B|C) etc |
| 1979 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 1980 | return replaceInstUsesWith(I, V); |
| 1981 | |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 1982 | if (Value *V = SimplifyBSwap(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1983 | return replaceInstUsesWith(I, V); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 1984 | |
Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 1985 | if (isa<Constant>(Op1)) |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 1986 | if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) |
| 1987 | return FoldedLogic; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1988 | |
Chad Rosier | e5819e2 | 2016-05-26 14:58:51 +0000 | [diff] [blame] | 1989 | // Given an OR instruction, check to see if this is a bswap. |
| 1990 | if (Instruction *BSwap = MatchBSwap(I)) |
| 1991 | return BSwap; |
| 1992 | |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 1993 | { |
| 1994 | Value *A; |
| 1995 | const APInt *C; |
| 1996 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| 1997 | if (match(Op0, m_OneUse(m_Xor(m_Value(A), m_APInt(C)))) && |
| 1998 | MaskedValueIsZero(Op1, *C, 0, &I)) { |
| 1999 | Value *NOr = Builder->CreateOr(A, Op1); |
| 2000 | NOr->takeName(Op0); |
| 2001 | return BinaryOperator::CreateXor(NOr, |
Davide Italiano | cdc937d | 2017-04-17 20:49:50 +0000 | [diff] [blame] | 2002 | ConstantInt::get(NOr->getType(), *C)); |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2003 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2004 | |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2005 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| 2006 | if (match(Op1, m_OneUse(m_Xor(m_Value(A), m_APInt(C)))) && |
| 2007 | MaskedValueIsZero(Op0, *C, 0, &I)) { |
| 2008 | Value *NOr = Builder->CreateOr(A, Op0); |
| 2009 | NOr->takeName(Op0); |
| 2010 | return BinaryOperator::CreateXor(NOr, |
Davide Italiano | cdc937d | 2017-04-17 20:49:50 +0000 | [diff] [blame] | 2011 | ConstantInt::get(NOr->getType(), *C)); |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2012 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2015 | Value *A, *B; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2016 | |
Suyog Sarda | d64faf6 | 2014-07-22 18:09:41 +0000 | [diff] [blame] | 2017 | // ((~A & B) | A) -> (A | B) |
Craig Topper | 72a622c | 2017-04-07 00:29:47 +0000 | [diff] [blame] | 2018 | if (match(Op0, m_c_And(m_Not(m_Specific(Op1)), m_Value(A)))) |
| 2019 | return BinaryOperator::CreateOr(A, Op1); |
| 2020 | if (match(Op1, m_c_And(m_Not(m_Specific(Op0)), m_Value(A)))) |
| 2021 | return BinaryOperator::CreateOr(Op0, A); |
Suyog Sarda | d64faf6 | 2014-07-22 18:09:41 +0000 | [diff] [blame] | 2022 | |
| 2023 | // ((A & B) | ~A) -> (~A | B) |
Craig Topper | 33e0dbc | 2017-04-07 07:32:00 +0000 | [diff] [blame] | 2024 | // The NOT is guaranteed to be in the RHS by complexity ordering. |
| 2025 | if (match(Op1, m_Not(m_Value(A))) && |
| 2026 | match(Op0, m_c_And(m_Specific(A), m_Value(B)))) |
| 2027 | return BinaryOperator::CreateOr(Op1, B); |
Suyog Sarda | d64faf6 | 2014-07-22 18:09:41 +0000 | [diff] [blame] | 2028 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2029 | // (A & C)|(B & D) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2030 | Value *C = nullptr, *D = nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2031 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 2032 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2033 | Value *V1 = nullptr, *V2 = nullptr; |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2034 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 2035 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2036 | if (C1 && C2) { // (A & C1)|(B & C2) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2037 | if ((C1->getValue() & C2->getValue()).isNullValue()) { |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2038 | // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2) |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2039 | // iff (C1&C2) == 0 and (N&~C1) == 0 |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2040 | if (match(A, m_Or(m_Value(V1), m_Value(V2))) && |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2041 | ((V1 == B && |
| 2042 | MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N) |
| 2043 | (V2 == B && |
| 2044 | MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V) |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2045 | return BinaryOperator::CreateAnd(A, |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 2046 | Builder->getInt(C1->getValue()|C2->getValue())); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2047 | // Or commutes, try both ways. |
| 2048 | if (match(B, m_Or(m_Value(V1), m_Value(V2))) && |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2049 | ((V1 == A && |
| 2050 | MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N) |
| 2051 | (V2 == A && |
| 2052 | MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V) |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2053 | return BinaryOperator::CreateAnd(B, |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 2054 | Builder->getInt(C1->getValue()|C2->getValue())); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2056 | // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2) |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2057 | // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2058 | ConstantInt *C3 = nullptr, *C4 = nullptr; |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2059 | if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) && |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2060 | (C3->getValue() & ~C1->getValue()).isNullValue() && |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2061 | match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) && |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2062 | (C4->getValue() & ~C2->getValue()).isNullValue()) { |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2063 | V2 = Builder->CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield"); |
| 2064 | return BinaryOperator::CreateAnd(V2, |
Jakub Staszak | 461d1fe | 2013-06-06 00:37:23 +0000 | [diff] [blame] | 2065 | Builder->getInt(C1->getValue()|C2->getValue())); |
Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2066 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2069 | |
Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2070 | // Don't try to form a select if it's unlikely that we'll get rid of at |
| 2071 | // least one of the operands. A select is generally more expensive than the |
| 2072 | // 'or' that it is replacing. |
| 2073 | if (Op0->hasOneUse() || Op1->hasOneUse()) { |
| 2074 | // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants. |
| 2075 | if (Value *V = matchSelectFromAndOr(A, C, B, D, *Builder)) |
| 2076 | return replaceInstUsesWith(I, V); |
| 2077 | if (Value *V = matchSelectFromAndOr(A, C, D, B, *Builder)) |
| 2078 | return replaceInstUsesWith(I, V); |
| 2079 | if (Value *V = matchSelectFromAndOr(C, A, B, D, *Builder)) |
| 2080 | return replaceInstUsesWith(I, V); |
| 2081 | if (Value *V = matchSelectFromAndOr(C, A, D, B, *Builder)) |
| 2082 | return replaceInstUsesWith(I, V); |
| 2083 | if (Value *V = matchSelectFromAndOr(B, D, A, C, *Builder)) |
| 2084 | return replaceInstUsesWith(I, V); |
| 2085 | if (Value *V = matchSelectFromAndOr(B, D, C, A, *Builder)) |
| 2086 | return replaceInstUsesWith(I, V); |
| 2087 | if (Value *V = matchSelectFromAndOr(D, B, A, C, *Builder)) |
| 2088 | return replaceInstUsesWith(I, V); |
| 2089 | if (Value *V = matchSelectFromAndOr(D, B, C, A, *Builder)) |
| 2090 | return replaceInstUsesWith(I, V); |
| 2091 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2092 | |
Benjamin Kramer | 1174324 | 2010-07-12 13:34:22 +0000 | [diff] [blame] | 2093 | // ((A|B)&1)|(B&-2) -> (A&1) | B |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 2094 | if (match(A, m_c_Or(m_Value(V1), m_Specific(B)))) { |
| 2095 | if (Instruction *Ret = FoldOrWithConstants(I, Op1, V1, B, C, Builder)) |
| 2096 | return Ret; |
Benjamin Kramer | 1174324 | 2010-07-12 13:34:22 +0000 | [diff] [blame] | 2097 | } |
| 2098 | // (B&-2)|((A|B)&1) -> (A&1) | B |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 2099 | if (match(B, m_c_Or(m_Specific(A), m_Value(V1)))) { |
| 2100 | if (Instruction *Ret = FoldOrWithConstants(I, Op0, A, V1, D, Builder)) |
| 2101 | return Ret; |
Benjamin Kramer | 1174324 | 2010-07-12 13:34:22 +0000 | [diff] [blame] | 2102 | } |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 2103 | // ((A^B)&1)|(B&-2) -> (A&1) ^ B |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 2104 | if (match(A, m_c_Xor(m_Value(V1), m_Specific(B)))) { |
| 2105 | if (Instruction *Ret = FoldXorWithConstants(I, Op1, V1, B, C, Builder)) |
| 2106 | return Ret; |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 2107 | } |
| 2108 | // (B&-2)|((A^B)&1) -> (A&1) ^ B |
Craig Topper | a074c10 | 2017-06-21 18:57:00 +0000 | [diff] [blame] | 2109 | if (match(B, m_c_Xor(m_Specific(A), m_Value(V1)))) { |
| 2110 | if (Instruction *Ret = FoldXorWithConstants(I, Op0, A, V1, D, Builder)) |
| 2111 | return Ret; |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 2112 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2113 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2114 | |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2115 | // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C |
| 2116 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) |
| 2117 | if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A)))) |
Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 2118 | return BinaryOperator::CreateOr(Op0, C); |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2119 | |
| 2120 | // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C |
| 2121 | if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) |
| 2122 | if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) |
Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 2123 | return BinaryOperator::CreateOr(Op1, C); |
David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2124 | |
David Majnemer | f1eda23 | 2014-08-14 06:41:38 +0000 | [diff] [blame] | 2125 | // ((B | C) & A) | B -> B | (A & C) |
| 2126 | if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A)))) |
| 2127 | return BinaryOperator::CreateOr(Op1, Builder->CreateAnd(A, C)); |
| 2128 | |
Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 2129 | if (Instruction *DeMorgan = matchDeMorgansLaws(I, *Builder)) |
Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 2130 | return DeMorgan; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2131 | |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2132 | // Canonicalize xor to the RHS. |
Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2133 | bool SwappedForXor = false; |
| 2134 | if (match(Op0, m_Xor(m_Value(), m_Value()))) { |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2135 | std::swap(Op0, Op1); |
Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2136 | SwappedForXor = true; |
| 2137 | } |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2138 | |
| 2139 | // A | ( A ^ B) -> A | B |
| 2140 | // A | (~A ^ B) -> A | ~B |
Chad Rosier | 7813dce | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 2141 | // (A & B) | (A ^ B) |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2142 | if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 2143 | if (Op0 == A || Op0 == B) |
| 2144 | return BinaryOperator::CreateOr(A, B); |
| 2145 | |
Chad Rosier | 7813dce | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 2146 | if (match(Op0, m_And(m_Specific(A), m_Specific(B))) || |
| 2147 | match(Op0, m_And(m_Specific(B), m_Specific(A)))) |
| 2148 | return BinaryOperator::CreateOr(A, B); |
| 2149 | |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2150 | if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) { |
| 2151 | Value *Not = Builder->CreateNot(B, B->getName()+".not"); |
| 2152 | return BinaryOperator::CreateOr(Not, Op0); |
| 2153 | } |
| 2154 | if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) { |
| 2155 | Value *Not = Builder->CreateNot(A, A->getName()+".not"); |
| 2156 | return BinaryOperator::CreateOr(Not, Op0); |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | // A | ~(A | B) -> A | ~B |
| 2161 | // A | ~(A ^ B) -> A | ~B |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2162 | if (match(Op1, m_Not(m_Value(A)))) |
| 2163 | if (BinaryOperator *B = dyn_cast<BinaryOperator>(A)) |
Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 2164 | if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) && |
| 2165 | Op1->hasOneUse() && (B->getOpcode() == Instruction::Or || |
| 2166 | B->getOpcode() == Instruction::Xor)) { |
| 2167 | Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) : |
| 2168 | B->getOperand(0); |
| 2169 | Value *Not = Builder->CreateNot(NotOp, NotOp->getName()+".not"); |
| 2170 | return BinaryOperator::CreateOr(Not, Op0); |
| 2171 | } |
Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2172 | |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 2173 | // (A & B) | (~A ^ B) -> (~A ^ B) |
| 2174 | // (A & B) | (B ^ ~A) -> (~A ^ B) |
| 2175 | // (B & A) | (~A ^ B) -> (~A ^ B) |
| 2176 | // (B & A) | (B ^ ~A) -> (~A ^ B) |
| 2177 | // The match order is important: match the xor first because the 'not' |
| 2178 | // operation defines 'A'. We do not need to match the xor as Op0 because the |
| 2179 | // xor was canonicalized to Op1 above. |
| 2180 | if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) && |
| 2181 | match(Op0, m_c_And(m_Specific(A), m_Specific(B)))) |
Suyog Sarda | 16d6465 | 2014-08-01 04:41:43 +0000 | [diff] [blame] | 2182 | return BinaryOperator::CreateXor(Builder->CreateNot(A), B); |
| 2183 | |
Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2184 | if (SwappedForXor) |
| 2185 | std::swap(Op0, Op1); |
| 2186 | |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2187 | { |
| 2188 | ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); |
| 2189 | ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); |
| 2190 | if (LHS && RHS) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2191 | if (Value *Res = foldOrOfICmps(LHS, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2192 | return replaceInstUsesWith(I, Res); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2193 | |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2194 | // TODO: Make this recursive; it's a little tricky because an arbitrary |
| 2195 | // number of 'or' instructions might have to be created. |
| 2196 | Value *X, *Y; |
| 2197 | if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { |
| 2198 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2199 | if (Value *Res = foldOrOfICmps(LHS, Cmp, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2200 | return replaceInstUsesWith(I, Builder->CreateOr(Res, Y)); |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2201 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2202 | if (Value *Res = foldOrOfICmps(LHS, Cmp, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2203 | return replaceInstUsesWith(I, Builder->CreateOr(Res, X)); |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2204 | } |
| 2205 | if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { |
| 2206 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2207 | if (Value *Res = foldOrOfICmps(Cmp, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2208 | return replaceInstUsesWith(I, Builder->CreateOr(Res, Y)); |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2209 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2210 | if (Value *Res = foldOrOfICmps(Cmp, RHS, I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2211 | return replaceInstUsesWith(I, Builder->CreateOr(Res, X)); |
David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 2215 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 2216 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) |
| 2217 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2218 | if (Value *Res = foldOrOfFCmps(LHS, RHS)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2219 | return replaceInstUsesWith(I, Res); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2220 | |
Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 2221 | if (Instruction *CastedOr = foldCastedBitwiseLogic(I)) |
| 2222 | return CastedOr; |
Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2223 | |
Sanjay Patel | cbfca9e | 2016-07-08 17:01:15 +0000 | [diff] [blame] | 2224 | // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>. |
Sanjay Patel | 1b6b824 | 2016-07-08 17:26:47 +0000 | [diff] [blame] | 2225 | if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) && |
Sanjay Patel | cbfca9e | 2016-07-08 17:01:15 +0000 | [diff] [blame] | 2226 | A->getType()->getScalarType()->isIntegerTy(1)) |
Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2227 | return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1); |
Sanjay Patel | 1b6b824 | 2016-07-08 17:26:47 +0000 | [diff] [blame] | 2228 | if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) && |
Sanjay Patel | cbfca9e | 2016-07-08 17:01:15 +0000 | [diff] [blame] | 2229 | A->getType()->getScalarType()->isIntegerTy(1)) |
Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2230 | return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0); |
| 2231 | |
Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2232 | // Note: If we've gotten to the point of visiting the outer OR, then the |
| 2233 | // inner one couldn't be simplified. If it was a constant, then it won't |
| 2234 | // be simplified by a later pass either, so we try swapping the inner/outer |
| 2235 | // ORs in the hopes that we'll be able to simplify it this way. |
| 2236 | // (X|C) | V --> (X|V) | C |
Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2237 | ConstantInt *C1; |
Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2238 | if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) && |
| 2239 | match(Op0, m_Or(m_Value(A), m_ConstantInt(C1)))) { |
| 2240 | Value *Inner = Builder->CreateOr(A, Op1); |
| 2241 | Inner->takeName(Op0); |
| 2242 | return BinaryOperator::CreateOr(Inner, C1); |
| 2243 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2244 | |
Bill Wendling | 2324209 | 2013-02-16 23:41:36 +0000 | [diff] [blame] | 2245 | // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D)) |
| 2246 | // Since this OR statement hasn't been optimized further yet, we hope |
| 2247 | // that this transformation will allow the new ORs to be optimized. |
| 2248 | { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2249 | Value *X = nullptr, *Y = nullptr; |
Bill Wendling | 2324209 | 2013-02-16 23:41:36 +0000 | [diff] [blame] | 2250 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 2251 | match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) && |
| 2252 | match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) { |
| 2253 | Value *orTrue = Builder->CreateOr(A, C); |
| 2254 | Value *orFalse = Builder->CreateOr(B, D); |
| 2255 | return SelectInst::Create(X, orTrue, orFalse); |
| 2256 | } |
| 2257 | } |
| 2258 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2259 | return Changed ? &I : nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2262 | /// A ^ B can be specified using other logic ops in a variety of patterns. We |
| 2263 | /// can fold these early and efficiently by morphing an existing instruction. |
Craig Topper | f60ab47 | 2017-07-02 01:15:51 +0000 | [diff] [blame] | 2264 | static Instruction *foldXorToXor(BinaryOperator &I, |
| 2265 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2266 | assert(I.getOpcode() == Instruction::Xor); |
| 2267 | Value *Op0 = I.getOperand(0); |
| 2268 | Value *Op1 = I.getOperand(1); |
| 2269 | Value *A, *B; |
| 2270 | |
| 2271 | // There are 4 commuted variants for each of the basic patterns. |
| 2272 | |
| 2273 | // (A & B) ^ (A | B) -> A ^ B |
| 2274 | // (A & B) ^ (B | A) -> A ^ B |
| 2275 | // (A | B) ^ (A & B) -> A ^ B |
| 2276 | // (A | B) ^ (B & A) -> A ^ B |
| 2277 | if ((match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 2278 | match(Op1, m_c_Or(m_Specific(A), m_Specific(B)))) || |
| 2279 | (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 2280 | match(Op1, m_c_And(m_Specific(A), m_Specific(B))))) { |
| 2281 | I.setOperand(0, A); |
| 2282 | I.setOperand(1, B); |
| 2283 | return &I; |
| 2284 | } |
| 2285 | |
| 2286 | // (A | ~B) ^ (~A | B) -> A ^ B |
| 2287 | // (~B | A) ^ (~A | B) -> A ^ B |
| 2288 | // (~A | B) ^ (A | ~B) -> A ^ B |
| 2289 | // (B | ~A) ^ (A | ~B) -> A ^ B |
Craig Topper | 880bf82 | 2017-06-30 07:37:41 +0000 | [diff] [blame] | 2290 | if ((match(Op0, m_Or(m_Value(A), m_Not(m_Value(B)))) && |
| 2291 | match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B)))) || |
| 2292 | (match(Op0, m_Or(m_Not(m_Value(A)), m_Value(B))) && |
| 2293 | match(Op1, m_c_Or(m_Specific(A), m_Not(m_Specific(B)))))) { |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2294 | I.setOperand(0, A); |
| 2295 | I.setOperand(1, B); |
| 2296 | return &I; |
| 2297 | } |
| 2298 | |
| 2299 | // (A & ~B) ^ (~A & B) -> A ^ B |
| 2300 | // (~B & A) ^ (~A & B) -> A ^ B |
| 2301 | // (~A & B) ^ (A & ~B) -> A ^ B |
| 2302 | // (B & ~A) ^ (A & ~B) -> A ^ B |
Craig Topper | 880bf82 | 2017-06-30 07:37:41 +0000 | [diff] [blame] | 2303 | if ((match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) && |
| 2304 | match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B)))) || |
| 2305 | (match(Op0, m_And(m_Not(m_Value(A)), m_Value(B))) && |
| 2306 | match(Op1, m_c_And(m_Specific(A), m_Not(m_Specific(B)))))) { |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2307 | I.setOperand(0, A); |
| 2308 | I.setOperand(1, B); |
| 2309 | return &I; |
| 2310 | } |
| 2311 | |
Craig Topper | f60ab47 | 2017-07-02 01:15:51 +0000 | [diff] [blame] | 2312 | // For the remaining cases we need to get rid of one of the operands. |
| 2313 | if (!Op0->hasOneUse() && !Op1->hasOneUse()) |
| 2314 | return nullptr; |
| 2315 | |
| 2316 | // (A | B) ^ ~(A & B) -> ~(A ^ B) |
| 2317 | // (A | B) ^ ~(B & A) -> ~(A ^ B) |
| 2318 | // (A & B) ^ ~(A | B) -> ~(A ^ B) |
| 2319 | // (A & B) ^ ~(B | A) -> ~(A ^ B) |
| 2320 | // Complexity sorting ensures the not will be on the right side. |
| 2321 | if ((match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 2322 | match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) || |
| 2323 | (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 2324 | match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))) |
| 2325 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
| 2326 | |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2327 | return nullptr; |
| 2328 | } |
| 2329 | |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2330 | Value *InstCombiner::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS) { |
| 2331 | if (PredicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) { |
| 2332 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 2333 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 2334 | LHS->swapOperands(); |
| 2335 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 2336 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 2337 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 2338 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 2339 | unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS); |
| 2340 | bool isSigned = LHS->isSigned() || RHS->isSigned(); |
| 2341 | return getNewICmpValue(isSigned, Code, Op0, Op1, Builder); |
| 2342 | } |
| 2343 | } |
| 2344 | |
Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2345 | // Instead of trying to imitate the folds for and/or, decompose this 'xor' |
| 2346 | // into those logic ops. That is, try to turn this into an and-of-icmps |
| 2347 | // because we have many folds for that pattern. |
| 2348 | // |
| 2349 | // This is based on a truth table definition of xor: |
| 2350 | // X ^ Y --> (X | Y) & !(X & Y) |
| 2351 | if (Value *OrICmp = SimplifyBinOp(Instruction::Or, LHS, RHS, SQ)) { |
| 2352 | // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y). |
| 2353 | // TODO: If OrICmp is false, the whole thing is false (InstSimplify?). |
| 2354 | if (Value *AndICmp = SimplifyBinOp(Instruction::And, LHS, RHS, SQ)) { |
| 2355 | // TODO: Independently handle cases where the 'and' side is a constant. |
| 2356 | if (OrICmp == LHS && AndICmp == RHS && RHS->hasOneUse()) { |
| 2357 | // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS |
| 2358 | RHS->setPredicate(RHS->getInversePredicate()); |
| 2359 | return Builder->CreateAnd(LHS, RHS); |
| 2360 | } |
| 2361 | if (OrICmp == RHS && AndICmp == LHS && LHS->hasOneUse()) { |
Sanjay Patel | 4ccbd58 | 2017-06-20 12:45:46 +0000 | [diff] [blame] | 2362 | // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS |
Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2363 | LHS->setPredicate(LHS->getInversePredicate()); |
| 2364 | return Builder->CreateAnd(LHS, RHS); |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2369 | return nullptr; |
| 2370 | } |
| 2371 | |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 2372 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 2373 | // here. We should standardize that construct where it is needed or choose some |
| 2374 | // other way to ensure that commutated variants of patterns are not missed. |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2375 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 2376 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2377 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2378 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 2379 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2380 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 2381 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 2382 | if (Value *V = SimplifyXorInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2383 | return replaceInstUsesWith(I, V); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2384 | |
Craig Topper | f60ab47 | 2017-07-02 01:15:51 +0000 | [diff] [blame] | 2385 | if (Instruction *NewXor = foldXorToXor(I, *Builder)) |
Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2386 | return NewXor; |
| 2387 | |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 2388 | // (A&B)^(A&C) -> A&(B^C) etc |
| 2389 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2390 | return replaceInstUsesWith(I, V); |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 2391 | |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2392 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2393 | // purpose is to compute bits we don't care about. |
| 2394 | if (SimplifyDemandedInstructionBits(I)) |
| 2395 | return &I; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2396 | |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 2397 | if (Value *V = SimplifyBSwap(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2398 | return replaceInstUsesWith(I, V); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 2399 | |
Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2400 | // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand. |
| 2401 | Value *X, *Y; |
| 2402 | |
| 2403 | // We must eliminate the and/or (one-use) for these transforms to not increase |
| 2404 | // the instruction count. |
| 2405 | // ~(~X & Y) --> (X | ~Y) |
| 2406 | // ~(Y & ~X) --> (X | ~Y) |
| 2407 | if (match(&I, m_Not(m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y)))))) { |
| 2408 | Value *NotY = Builder->CreateNot(Y, Y->getName() + ".not"); |
| 2409 | return BinaryOperator::CreateOr(X, NotY); |
| 2410 | } |
| 2411 | // ~(~X | Y) --> (X & ~Y) |
| 2412 | // ~(Y | ~X) --> (X & ~Y) |
| 2413 | if (match(&I, m_Not(m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y)))))) { |
| 2414 | Value *NotY = Builder->CreateNot(Y, Y->getName() + ".not"); |
| 2415 | return BinaryOperator::CreateAnd(X, NotY); |
| 2416 | } |
| 2417 | |
Sanjay Patel | 3b863f8 | 2017-04-22 18:05:35 +0000 | [diff] [blame] | 2418 | // Is this a 'not' (~) fed by a binary operator? |
Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2419 | BinaryOperator *NotVal; |
| 2420 | if (match(&I, m_Not(m_BinOp(NotVal)))) { |
| 2421 | if (NotVal->getOpcode() == Instruction::And || |
| 2422 | NotVal->getOpcode() == Instruction::Or) { |
Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2423 | // Apply DeMorgan's Law when inverts are free: |
| 2424 | // ~(X & Y) --> (~X | ~Y) |
| 2425 | // ~(X | Y) --> (~X & ~Y) |
Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2426 | if (IsFreeToInvert(NotVal->getOperand(0), |
| 2427 | NotVal->getOperand(0)->hasOneUse()) && |
| 2428 | IsFreeToInvert(NotVal->getOperand(1), |
| 2429 | NotVal->getOperand(1)->hasOneUse())) { |
| 2430 | Value *NotX = Builder->CreateNot(NotVal->getOperand(0), "notlhs"); |
| 2431 | Value *NotY = Builder->CreateNot(NotVal->getOperand(1), "notrhs"); |
| 2432 | if (NotVal->getOpcode() == Instruction::And) |
Sanjay Patel | 3b863f8 | 2017-04-22 18:05:35 +0000 | [diff] [blame] | 2433 | return BinaryOperator::CreateOr(NotX, NotY); |
| 2434 | return BinaryOperator::CreateAnd(NotX, NotY); |
| 2435 | } |
Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | // ~(~X >>s Y) --> (X >>s Y) |
| 2439 | if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y)))) |
| 2440 | return BinaryOperator::CreateAShr(X, Y); |
| 2441 | |
| 2442 | // If we are inverting a right-shifted constant, we may be able to eliminate |
| 2443 | // the 'not' by inverting the constant and using the opposite shift type. |
| 2444 | // Canonicalization rules ensure that only a negative constant uses 'ashr', |
| 2445 | // but we must check that in case that transform has not fired yet. |
| 2446 | const APInt *C; |
| 2447 | if (match(NotVal, m_AShr(m_APInt(C), m_Value(Y))) && C->isNegative()) { |
| 2448 | // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits) |
| 2449 | Constant *NotC = ConstantInt::get(I.getType(), ~(*C)); |
| 2450 | return BinaryOperator::CreateLShr(NotC, Y); |
| 2451 | } |
| 2452 | |
| 2453 | if (match(NotVal, m_LShr(m_APInt(C), m_Value(Y))) && C->isNonNegative()) { |
| 2454 | // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits) |
| 2455 | Constant *NotC = ConstantInt::get(I.getType(), ~(*C)); |
| 2456 | return BinaryOperator::CreateAShr(NotC, Y); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2457 | } |
| 2458 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2459 | |
Craig Topper | 798a19a | 2017-06-29 00:07:08 +0000 | [diff] [blame] | 2460 | // not (cmp A, B) = !cmp A, B |
Sanjay Patel | 33439f9 | 2017-04-12 15:11:33 +0000 | [diff] [blame] | 2461 | ICmpInst::Predicate Pred; |
Craig Topper | 798a19a | 2017-06-29 00:07:08 +0000 | [diff] [blame] | 2462 | if (match(&I, m_Not(m_OneUse(m_Cmp(Pred, m_Value(), m_Value()))))) { |
Sanjay Patel | 33439f9 | 2017-04-12 15:11:33 +0000 | [diff] [blame] | 2463 | cast<CmpInst>(Op0)->setPredicate(CmpInst::getInversePredicate(Pred)); |
| 2464 | return replaceInstUsesWith(I, Op0); |
Benjamin Kramer | 443c796 | 2015-02-12 20:26:46 +0000 | [diff] [blame] | 2465 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2466 | |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2467 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2468 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
| 2469 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| 2470 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { |
| 2471 | if (CI->hasOneUse() && Op0C->hasOneUse()) { |
| 2472 | Instruction::CastOps Opcode = Op0C->getOpcode(); |
| 2473 | if ((Opcode == Instruction::ZExt || Opcode == Instruction::SExt) && |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2474 | (RHSC == ConstantExpr::getCast(Opcode, Builder->getTrue(), |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2475 | Op0C->getDestTy()))) { |
| 2476 | CI->setPredicate(CI->getInversePredicate()); |
| 2477 | return CastInst::Create(Opcode, CI, Op0C->getType()); |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 2484 | // ~(c-X) == X-c-1 == X+(-c-1) |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2485 | if (Op0I->getOpcode() == Instruction::Sub && RHSC->isAllOnesValue()) |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2486 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
| 2487 | Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C); |
Craig Topper | 437c976 | 2017-04-09 06:12:34 +0000 | [diff] [blame] | 2488 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), |
| 2489 | SubOne(NegOp0I0C)); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2490 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2492 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 2493 | if (Op0I->getOpcode() == Instruction::Add) { |
| 2494 | // ~(X-c) --> (-c-1)-X |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2495 | if (RHSC->isAllOnesValue()) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2496 | Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI); |
Craig Topper | 437c976 | 2017-04-09 06:12:34 +0000 | [diff] [blame] | 2497 | return BinaryOperator::CreateSub(SubOne(NegOp0CI), |
| 2498 | Op0I->getOperand(0)); |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 2499 | } else if (RHSC->getValue().isSignMask()) { |
| 2500 | // (X + C) ^ signmask -> (X + C + signmask) |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2501 | Constant *C = Builder->getInt(RHSC->getValue() + Op0CI->getValue()); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2502 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
| 2503 | |
| 2504 | } |
| 2505 | } else if (Op0I->getOpcode() == Instruction::Or) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2506 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2507 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue(), |
| 2508 | 0, &I)) { |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2509 | Constant *NewRHS = ConstantExpr::getOr(Op0CI, RHSC); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2510 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 2511 | // NewRHS. |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2512 | Constant *CommonBits = ConstantExpr::getAnd(Op0CI, RHSC); |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2513 | NewRHS = ConstantExpr::getAnd(NewRHS, |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2514 | ConstantExpr::getNot(CommonBits)); |
| 2515 | Worklist.Add(Op0I); |
| 2516 | I.setOperand(0, Op0I->getOperand(0)); |
| 2517 | I.setOperand(1, NewRHS); |
| 2518 | return &I; |
| 2519 | } |
Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2520 | } else if (Op0I->getOpcode() == Instruction::LShr) { |
| 2521 | // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3) |
| 2522 | // E1 = "X ^ C1" |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2523 | BinaryOperator *E1; |
Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2524 | ConstantInt *C1; |
| 2525 | if (Op0I->hasOneUse() && |
| 2526 | (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) && |
| 2527 | E1->getOpcode() == Instruction::Xor && |
| 2528 | (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) { |
| 2529 | // fold (C1 >> C2) ^ C3 |
Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2530 | ConstantInt *C2 = Op0CI, *C3 = RHSC; |
Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2531 | APInt FoldConst = C1->getValue().lshr(C2->getValue()); |
| 2532 | FoldConst ^= C3->getValue(); |
| 2533 | // Prepare the two operands. |
| 2534 | Value *Opnd0 = Builder->CreateLShr(E1->getOperand(0), C2); |
| 2535 | Opnd0->takeName(Op0I); |
| 2536 | cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc()); |
| 2537 | Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst); |
| 2538 | |
| 2539 | return BinaryOperator::CreateXor(Opnd0, FoldVal); |
| 2540 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | } |
Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 2544 | } |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2545 | |
Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 2546 | if (isa<Constant>(Op1)) |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 2547 | if (Instruction *FoldedLogic = foldOpWithConstantIntoOperand(I)) |
| 2548 | return FoldedLogic; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2549 | |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2550 | { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2551 | Value *A, *B; |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2552 | if (match(Op1, m_OneUse(m_Or(m_Value(A), m_Value(B))))) { |
Craig Topper | 4738321 | 2017-04-10 06:53:21 +0000 | [diff] [blame] | 2553 | if (A == Op0) { // A^(A|B) == A^(B|A) |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2554 | cast<BinaryOperator>(Op1)->swapOperands(); |
Craig Topper | 4738321 | 2017-04-10 06:53:21 +0000 | [diff] [blame] | 2555 | std::swap(A, B); |
| 2556 | } |
| 2557 | if (B == Op0) { // A^(B|A) == (B|A)^A |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2558 | I.swapOperands(); // Simplified below. |
| 2559 | std::swap(Op0, Op1); |
| 2560 | } |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2561 | } else if (match(Op1, m_OneUse(m_And(m_Value(A), m_Value(B))))) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2562 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2563 | cast<BinaryOperator>(Op1)->swapOperands(); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2564 | std::swap(A, B); |
| 2565 | } |
| 2566 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
| 2567 | I.swapOperands(); // Simplified below. |
| 2568 | std::swap(Op0, Op1); |
| 2569 | } |
| 2570 | } |
| 2571 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2572 | |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2573 | { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2574 | Value *A, *B; |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2575 | if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B))))) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2576 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 2577 | std::swap(A, B); |
| 2578 | if (B == Op1) // (A|B)^B == A & ~B |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2579 | return BinaryOperator::CreateAnd(A, Builder->CreateNot(Op1)); |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2580 | } else if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B))))) { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2581 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 2582 | std::swap(A, B); |
Craig Topper | e63c21b | 2017-04-09 06:12:39 +0000 | [diff] [blame] | 2583 | const APInt *C; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2584 | if (B == Op1 && // (B&A)^A == ~B & A |
Craig Topper | e63c21b | 2017-04-09 06:12:39 +0000 | [diff] [blame] | 2585 | !match(Op1, m_APInt(C))) { // Canonical form is (B&C)^C |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2586 | return BinaryOperator::CreateAnd(Builder->CreateNot(A), Op1); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2587 | } |
| 2588 | } |
| 2589 | } |
Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2590 | |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2591 | { |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2592 | Value *A, *B, *C, *D; |
David Majnemer | 6fe6ea7 | 2014-09-05 06:09:24 +0000 | [diff] [blame] | 2593 | // (A ^ C)^(A | B) -> ((~A) & B) ^ C |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2594 | if (match(Op0, m_Xor(m_Value(D), m_Value(C))) && |
| 2595 | match(Op1, m_Or(m_Value(A), m_Value(B)))) { |
David Majnemer | 6fe6ea7 | 2014-09-05 06:09:24 +0000 | [diff] [blame] | 2596 | if (D == A) |
| 2597 | return BinaryOperator::CreateXor( |
| 2598 | Builder->CreateAnd(Builder->CreateNot(A), B), C); |
| 2599 | if (D == B) |
| 2600 | return BinaryOperator::CreateXor( |
| 2601 | Builder->CreateAnd(Builder->CreateNot(B), A), C); |
Karthik Bhat | a4a4db9 | 2014-08-13 05:13:14 +0000 | [diff] [blame] | 2602 | } |
David Majnemer | 6fe6ea7 | 2014-09-05 06:09:24 +0000 | [diff] [blame] | 2603 | // (A | B)^(A ^ C) -> ((~A) & B) ^ C |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2604 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 2605 | match(Op1, m_Xor(m_Value(D), m_Value(C)))) { |
David Majnemer | 6fe6ea7 | 2014-09-05 06:09:24 +0000 | [diff] [blame] | 2606 | if (D == A) |
| 2607 | return BinaryOperator::CreateXor( |
| 2608 | Builder->CreateAnd(Builder->CreateNot(A), B), C); |
| 2609 | if (D == B) |
| 2610 | return BinaryOperator::CreateXor( |
| 2611 | Builder->CreateAnd(Builder->CreateNot(B), A), C); |
Karthik Bhat | a4a4db9 | 2014-08-13 05:13:14 +0000 | [diff] [blame] | 2612 | } |
Suyog Sarda | b60ec90 | 2014-07-22 18:30:54 +0000 | [diff] [blame] | 2613 | // (A & B) ^ (A ^ B) -> (A | B) |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2614 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Craig Topper | d8840d7 | 2017-04-10 06:53:28 +0000 | [diff] [blame] | 2615 | match(Op1, m_c_Xor(m_Specific(A), m_Specific(B)))) |
Suyog Sarda | b60ec90 | 2014-07-22 18:30:54 +0000 | [diff] [blame] | 2616 | return BinaryOperator::CreateOr(A, B); |
| 2617 | // (A ^ B) ^ (A & B) -> (A | B) |
Craig Topper | 7639460 | 2017-04-10 06:53:23 +0000 | [diff] [blame] | 2618 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
Craig Topper | d8840d7 | 2017-04-10 06:53:28 +0000 | [diff] [blame] | 2619 | match(Op1, m_c_And(m_Specific(A), m_Specific(B)))) |
Suyog Sarda | b60ec90 | 2014-07-22 18:30:54 +0000 | [diff] [blame] | 2620 | return BinaryOperator::CreateOr(A, B); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2621 | } |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 2622 | |
Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 2623 | // (A & ~B) ^ ~A -> ~(A & B) |
| 2624 | // (~B & A) ^ ~A -> ~(A & B) |
| 2625 | Value *A, *B; |
| 2626 | if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) && |
Suyog Sarda | 56c9a87 | 2014-08-01 05:07:20 +0000 | [diff] [blame] | 2627 | match(Op1, m_Not(m_Specific(A)))) |
| 2628 | return BinaryOperator::CreateNot(Builder->CreateAnd(A, B)); |
| 2629 | |
Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2630 | if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) |
| 2631 | if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 2632 | if (Value *V = foldXorOfICmps(LHS, RHS)) |
| 2633 | return replaceInstUsesWith(I, V); |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2634 | |
Sanjay Patel | dbbaca0 | 2016-02-24 17:00:34 +0000 | [diff] [blame] | 2635 | if (Instruction *CastedXor = foldCastedBitwiseLogic(I)) |
| 2636 | return CastedXor; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2637 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2638 | return Changed ? &I : nullptr; |
Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2639 | } |