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