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