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