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