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