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