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