| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1 | //===- InstCombineAndOrXor.cpp --------------------------------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the visitAnd, visitOr, and visitXor functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 13 | #include "InstCombineInternal.h" |
| Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/CmpInstAnalysis.h" |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/InstructionSimplify.h" |
| David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Local.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" |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | using namespace PatternMatch; |
| 22 | |
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "instcombine" |
| 24 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 25 | /// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 26 | /// a four bit mask. |
| 27 | static unsigned getFCmpCode(FCmpInst::Predicate CC) { |
| 28 | assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE && |
| 29 | "Unexpected FCmp predicate!"); |
| 30 | // Take advantage of the bit pattern of FCmpInst::Predicate here. |
| 31 | // U L G E |
| 32 | static_assert(FCmpInst::FCMP_FALSE == 0, ""); // 0 0 0 0 |
| 33 | static_assert(FCmpInst::FCMP_OEQ == 1, ""); // 0 0 0 1 |
| 34 | static_assert(FCmpInst::FCMP_OGT == 2, ""); // 0 0 1 0 |
| 35 | static_assert(FCmpInst::FCMP_OGE == 3, ""); // 0 0 1 1 |
| 36 | static_assert(FCmpInst::FCMP_OLT == 4, ""); // 0 1 0 0 |
| 37 | static_assert(FCmpInst::FCMP_OLE == 5, ""); // 0 1 0 1 |
| 38 | static_assert(FCmpInst::FCMP_ONE == 6, ""); // 0 1 1 0 |
| 39 | static_assert(FCmpInst::FCMP_ORD == 7, ""); // 0 1 1 1 |
| 40 | static_assert(FCmpInst::FCMP_UNO == 8, ""); // 1 0 0 0 |
| 41 | static_assert(FCmpInst::FCMP_UEQ == 9, ""); // 1 0 0 1 |
| 42 | static_assert(FCmpInst::FCMP_UGT == 10, ""); // 1 0 1 0 |
| 43 | static_assert(FCmpInst::FCMP_UGE == 11, ""); // 1 0 1 1 |
| 44 | static_assert(FCmpInst::FCMP_ULT == 12, ""); // 1 1 0 0 |
| 45 | static_assert(FCmpInst::FCMP_ULE == 13, ""); // 1 1 0 1 |
| 46 | static_assert(FCmpInst::FCMP_UNE == 14, ""); // 1 1 1 0 |
| 47 | static_assert(FCmpInst::FCMP_TRUE == 15, ""); // 1 1 1 1 |
| 48 | return CC; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 51 | /// This is the complement of getICmpCode, which turns an opcode and two |
| 52 | /// operands into either a constant true or false, or a brand new ICmp |
| 53 | /// instruction. The sign is passed in to determine which kind of predicate to |
| 54 | /// use in the new icmp instruction. |
| Sanjay Patel | 3d5bb15 | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 55 | static Value *getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 56 | InstCombiner::BuilderTy &Builder) { |
| Pete Cooper | ebf98c1 | 2011-12-17 01:20:32 +0000 | [diff] [blame] | 57 | ICmpInst::Predicate NewPred; |
| Sanjay Patel | 3d5bb15 | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 58 | if (Constant *TorF = getPredForICmpCode(Code, Sign, LHS->getType(), NewPred)) |
| 59 | return TorF; |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 60 | return Builder.CreateICmp(NewPred, LHS, RHS); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 63 | /// This is the complement of getFCmpCode, which turns an opcode and two |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 64 | /// operands into either a FCmp instruction, or a true/false constant. |
| 65 | static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 66 | InstCombiner::BuilderTy &Builder) { |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 67 | const auto Pred = static_cast<FCmpInst::Predicate>(Code); |
| 68 | assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE && |
| 69 | "Unexpected FCmp predicate!"); |
| 70 | if (Pred == FCmpInst::FCMP_FALSE) |
| 71 | return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0); |
| 72 | if (Pred == FCmpInst::FCMP_TRUE) |
| 73 | return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 74 | return Builder.CreateFCmp(Pred, LHS, RHS); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 77 | /// Transform BITWISE_OP(BSWAP(A),BSWAP(B)) or |
| Craig Topper | fc42ace | 2017-07-06 16:24:22 +0000 | [diff] [blame] | 78 | /// BITWISE_OP(BSWAP(A), Constant) to BSWAP(BITWISE_OP(A, B)) |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 79 | /// \param I Binary operator to transform. |
| 80 | /// \return Pointer to node that must replace the original binary operator, or |
| 81 | /// null pointer if no transformation was made. |
| Craig Topper | 95e4142 | 2017-07-06 16:24:23 +0000 | [diff] [blame] | 82 | static Value *SimplifyBSwap(BinaryOperator &I, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 83 | InstCombiner::BuilderTy &Builder) { |
| Craig Topper | c6948c2 | 2017-07-03 05:54:11 +0000 | [diff] [blame] | 84 | assert(I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying"); |
| 85 | |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 86 | Value *OldLHS = I.getOperand(0); |
| 87 | Value *OldRHS = I.getOperand(1); |
| Craig Topper | 8036970 | 2017-07-03 05:54:16 +0000 | [diff] [blame] | 88 | |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 89 | Value *NewLHS; |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 90 | if (!match(OldLHS, m_BSwap(m_Value(NewLHS)))) |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 91 | return nullptr; |
| 92 | |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 93 | Value *NewRHS; |
| 94 | const APInt *C; |
| 95 | |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 96 | if (match(OldRHS, m_BSwap(m_Value(NewRHS)))) { |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 97 | // OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) ) |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 98 | if (!OldLHS->hasOneUse() && !OldRHS->hasOneUse()) |
| 99 | return nullptr; |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 100 | // NewRHS initialized by the matcher. |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 101 | } else if (match(OldRHS, m_APInt(C))) { |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 102 | // OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) ) |
| Craig Topper | 22795de | 2017-07-06 16:24:21 +0000 | [diff] [blame] | 103 | if (!OldLHS->hasOneUse()) |
| 104 | return nullptr; |
| Craig Topper | 766ce6e | 2017-07-03 05:54:15 +0000 | [diff] [blame] | 105 | NewRHS = ConstantInt::get(I.getType(), C->byteSwap()); |
| 106 | } else |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 107 | return nullptr; |
| 108 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 109 | Value *BinOp = Builder.CreateBinOp(I.getOpcode(), NewLHS, NewRHS); |
| Craig Topper | 1e4643a | 2017-07-03 05:54:13 +0000 | [diff] [blame] | 110 | Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, |
| 111 | I.getType()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 112 | return Builder.CreateCall(F, BinOp); |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 115 | /// This handles expressions of the form ((val OP C1) & C2). Where |
| Craig Topper | 70e4f43 | 2017-04-02 17:57:30 +0000 | [diff] [blame] | 116 | /// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. |
| 117 | Instruction *InstCombiner::OptAndOp(BinaryOperator *Op, |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 118 | ConstantInt *OpRHS, |
| 119 | ConstantInt *AndRHS, |
| 120 | BinaryOperator &TheAnd) { |
| 121 | Value *X = Op->getOperand(0); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 122 | |
| 123 | switch (Op->getOpcode()) { |
| Craig Topper | 70e4f43 | 2017-04-02 17:57:30 +0000 | [diff] [blame] | 124 | default: break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 125 | case Instruction::Add: |
| 126 | if (Op->hasOneUse()) { |
| 127 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 128 | // of the bit. First thing to check is to see if this AND is with a |
| 129 | // single bit constant. |
| Jakub Staszak | 9de494e | 2013-06-06 00:49:57 +0000 | [diff] [blame] | 130 | const APInt &AndRHSV = AndRHS->getValue(); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 131 | |
| 132 | // If there is only one bit set. |
| 133 | if (AndRHSV.isPowerOf2()) { |
| 134 | // Ok, at this point, we know that we are masking the result of the |
| 135 | // ADD down to exactly one bit. If the constant we are adding has |
| 136 | // no bits set below this bit, then we can eliminate the ADD. |
| Jakub Staszak | 9de494e | 2013-06-06 00:49:57 +0000 | [diff] [blame] | 137 | const APInt& AddRHS = OpRHS->getValue(); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 138 | |
| 139 | // 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] | 140 | if ((AddRHS & (AndRHSV - 1)).isNullValue()) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 141 | // If not, the only thing that can effect the output of the AND is |
| 142 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 143 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 144 | // no effect. |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 145 | if ((AddRHS & AndRHSV).isNullValue()) { // Bit is not set, noop |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 146 | TheAnd.setOperand(0, X); |
| 147 | return &TheAnd; |
| 148 | } else { |
| 149 | // Pull the XOR out of the AND. |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 150 | Value *NewAnd = Builder.CreateAnd(X, AndRHS); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 151 | NewAnd->takeName(Op); |
| 152 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 158 | } |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 159 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 162 | /// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise |
| Craig Topper | e6cd20b | 2019-07-21 16:15:03 +0000 | [diff] [blame] | 163 | /// (V < Lo || V >= Hi). This method expects that Lo < Hi. IsSigned indicates |
| Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 164 | /// whether to treat V, Lo, and Hi as signed or not. |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 165 | Value *InstCombiner::insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, |
| Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 166 | bool isSigned, bool Inside) { |
| Craig Topper | 1d149d0 | 2019-07-21 06:43:38 +0000 | [diff] [blame] | 167 | assert((isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) && |
| 168 | "Lo is not < Hi in range emission code!"); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 169 | |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 170 | Type *Ty = V->getType(); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 171 | |
| Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 172 | // V >= Min && V < Hi --> V < Hi |
| 173 | // V < Min || V >= Hi --> V >= Hi |
| 174 | ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE; |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 175 | if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) { |
| Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 176 | Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred; |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 177 | return Builder.CreateICmp(Pred, V, ConstantInt::get(Ty, Hi)); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| Sanjay Patel | 7d9ebaf | 2016-08-31 00:19:35 +0000 | [diff] [blame] | 180 | // V >= Lo && V < Hi --> V - Lo u< Hi - Lo |
| 181 | // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 182 | Value *VMinusLo = |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 183 | Builder.CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off"); |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 184 | Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 185 | return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 188 | /// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns |
| 189 | /// that can be simplified. |
| 190 | /// One of A and B is considered the mask. The other is the value. This is |
| 191 | /// described as the "AMask" or "BMask" part of the enum. If the enum contains |
| 192 | /// only "Mask", then both A and B can be considered masks. If A is the mask, |
| 193 | /// then it was proven that (A & C) == C. This is trivial if C == A or C == 0. |
| 194 | /// If both A and C are constants, this proof is also easy. |
| 195 | /// For the following explanations, we assume that A is the mask. |
| 196 | /// |
| 197 | /// "AllOnes" declares that the comparison is true only if (A & B) == A or all |
| 198 | /// bits of A are set in B. |
| 199 | /// Example: (icmp eq (A & 3), 3) -> AMask_AllOnes |
| 200 | /// |
| 201 | /// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all |
| 202 | /// bits of A are cleared in B. |
| 203 | /// Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes |
| 204 | /// |
| 205 | /// "Mixed" declares that (A & B) == C and C might or might not contain any |
| 206 | /// number of one bits and zero bits. |
| 207 | /// Example: (icmp eq (A & 3), 1) -> AMask_Mixed |
| 208 | /// |
| 209 | /// "Not" means that in above descriptions "==" should be replaced by "!=". |
| 210 | /// Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes |
| 211 | /// |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 212 | /// If the mask A contains a single bit, then the following is equivalent: |
| 213 | /// (icmp eq (A & B), A) equals (icmp ne (A & B), 0) |
| 214 | /// (icmp ne (A & B), A) equals (icmp eq (A & B), 0) |
| 215 | enum MaskedICmpType { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 216 | AMask_AllOnes = 1, |
| 217 | AMask_NotAllOnes = 2, |
| 218 | BMask_AllOnes = 4, |
| 219 | BMask_NotAllOnes = 8, |
| 220 | Mask_AllZeros = 16, |
| 221 | Mask_NotAllZeros = 32, |
| 222 | AMask_Mixed = 64, |
| 223 | AMask_NotMixed = 128, |
| 224 | BMask_Mixed = 256, |
| 225 | BMask_NotMixed = 512 |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 228 | /// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C) |
| 229 | /// satisfies. |
| 230 | static unsigned getMaskedICmpType(Value *A, Value *B, Value *C, |
| 231 | ICmpInst::Predicate Pred) { |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 232 | ConstantInt *ACst = dyn_cast<ConstantInt>(A); |
| 233 | ConstantInt *BCst = dyn_cast<ConstantInt>(B); |
| 234 | ConstantInt *CCst = dyn_cast<ConstantInt>(C); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 235 | bool IsEq = (Pred == ICmpInst::ICMP_EQ); |
| 236 | bool IsAPow2 = (ACst && !ACst->isZero() && ACst->getValue().isPowerOf2()); |
| 237 | bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2()); |
| 238 | unsigned MaskVal = 0; |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 239 | if (CCst && CCst->isZero()) { |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 240 | // if C is zero, then both A and B qualify as mask |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 241 | MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed) |
| 242 | : (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed)); |
| 243 | if (IsAPow2) |
| 244 | MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed) |
| 245 | : (AMask_AllOnes | AMask_Mixed)); |
| 246 | if (IsBPow2) |
| 247 | MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed) |
| 248 | : (BMask_AllOnes | BMask_Mixed)); |
| 249 | return MaskVal; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 250 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 251 | |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 252 | if (A == C) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 253 | MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed) |
| 254 | : (AMask_NotAllOnes | AMask_NotMixed)); |
| 255 | if (IsAPow2) |
| 256 | MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed) |
| 257 | : (Mask_AllZeros | AMask_Mixed)); |
| 258 | } else if (ACst && CCst && ConstantExpr::getAnd(ACst, CCst) == CCst) { |
| 259 | MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed); |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 260 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 261 | |
| Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 262 | if (B == C) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 263 | MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed) |
| 264 | : (BMask_NotAllOnes | BMask_NotMixed)); |
| 265 | if (IsBPow2) |
| 266 | MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed) |
| 267 | : (Mask_AllZeros | BMask_Mixed)); |
| 268 | } else if (BCst && CCst && ConstantExpr::getAnd(BCst, CCst) == CCst) { |
| 269 | MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed); |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 270 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 271 | |
| 272 | return MaskVal; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 275 | /// Convert an analysis of a masked ICmp into its equivalent if all boolean |
| 276 | /// operations had the opposite sense. Since each "NotXXX" flag (recording !=) |
| 277 | /// is adjacent to the corresponding normal flag (recording ==), this just |
| 278 | /// involves swapping those bits over. |
| 279 | static unsigned conjugateICmpMask(unsigned Mask) { |
| 280 | unsigned NewMask; |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 281 | NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros | |
| 282 | AMask_Mixed | BMask_Mixed)) |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 283 | << 1; |
| 284 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 285 | NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros | |
| 286 | AMask_NotMixed | BMask_NotMixed)) |
| 287 | >> 1; |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 288 | |
| 289 | return NewMask; |
| 290 | } |
| 291 | |
| Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 292 | // Adapts the external decomposeBitTestICmp for local use. |
| 293 | static bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, |
| 294 | Value *&X, Value *&Y, Value *&Z) { |
| 295 | APInt Mask; |
| 296 | if (!llvm::decomposeBitTestICmp(LHS, RHS, Pred, X, Mask)) |
| 297 | return false; |
| 298 | |
| Craig Topper | 085c1f4 | 2017-09-01 21:27:29 +0000 | [diff] [blame] | 299 | Y = ConstantInt::get(X->getType(), Mask); |
| 300 | Z = ConstantInt::get(X->getType(), 0); |
| Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 304 | /// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E). |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 305 | /// Return the pattern classes (from MaskedICmpType) for the left hand side and |
| 306 | /// the right hand side as a pair. |
| 307 | /// LHS and RHS are the left hand side and the right hand side ICmps and PredL |
| 308 | /// and PredR are their predicates, respectively. |
| 309 | static |
| 310 | Optional<std::pair<unsigned, unsigned>> |
| 311 | getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C, |
| 312 | Value *&D, Value *&E, ICmpInst *LHS, |
| 313 | ICmpInst *RHS, |
| 314 | ICmpInst::Predicate &PredL, |
| 315 | ICmpInst::Predicate &PredR) { |
| Craig Topper | 775ffcc | 2017-08-21 21:00:45 +0000 | [diff] [blame] | 316 | // vectors are not (yet?) supported. Don't support pointers either. |
| Craig Topper | d3b4656 | 2017-09-01 21:27:31 +0000 | [diff] [blame] | 317 | if (!LHS->getOperand(0)->getType()->isIntegerTy() || |
| 318 | !RHS->getOperand(0)->getType()->isIntegerTy()) |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 319 | return None; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 320 | |
| 321 | // Here comes the tricky part: |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 322 | // LHS might be of the form L11 & L12 == X, X == L21 & L22, |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 323 | // and L11 & L12 == L21 & L22. The same goes for RHS. |
| 324 | // 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] | 325 | // 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] | 326 | // above. |
| 327 | Value *L1 = LHS->getOperand(0); |
| 328 | Value *L2 = LHS->getOperand(1); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 329 | Value *L11, *L12, *L21, *L22; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 330 | // Check whether the icmp can be decomposed into a bit test. |
| Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 331 | if (decomposeBitTestICmp(L1, L2, PredL, L11, L12, L2)) { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 332 | L21 = L22 = L1 = nullptr; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 333 | } else { |
| 334 | // Look for ANDs in the LHS icmp. |
| Craig Topper | 775ffcc | 2017-08-21 21:00:45 +0000 | [diff] [blame] | 335 | if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) { |
| Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 336 | // Any icmp can be viewed as being trivially masked; if it allows us to |
| 337 | // remove one, it's worth it. |
| 338 | L11 = L1; |
| 339 | L12 = Constant::getAllOnesValue(L1->getType()); |
| 340 | } |
| 341 | |
| Craig Topper | 775ffcc | 2017-08-21 21:00:45 +0000 | [diff] [blame] | 342 | if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) { |
| Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 343 | L21 = L2; |
| 344 | L22 = Constant::getAllOnesValue(L2->getType()); |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 345 | } |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 346 | } |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 347 | |
| 348 | // 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] | 349 | if (!ICmpInst::isEquality(PredL)) |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 350 | return None; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 351 | |
| 352 | Value *R1 = RHS->getOperand(0); |
| 353 | Value *R2 = RHS->getOperand(1); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 354 | Value *R11, *R12; |
| 355 | bool Ok = false; |
| Craig Topper | 0aa3a19 | 2017-08-14 21:39:51 +0000 | [diff] [blame] | 356 | if (decomposeBitTestICmp(R1, R2, PredR, R11, R12, R2)) { |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 357 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 358 | A = R11; |
| 359 | D = R12; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 360 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 361 | A = R12; |
| 362 | D = R11; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 363 | } else { |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 364 | return None; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 365 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 366 | E = R2; |
| 367 | R1 = nullptr; |
| 368 | Ok = true; |
| Craig Topper | 775ffcc | 2017-08-21 21:00:45 +0000 | [diff] [blame] | 369 | } else { |
| Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 370 | if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) { |
| 371 | // 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] | 372 | // optimization. |
| Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 373 | R11 = R1; |
| 374 | R12 = Constant::getAllOnesValue(R1->getType()); |
| 375 | } |
| 376 | |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 377 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 378 | A = R11; |
| 379 | D = R12; |
| 380 | E = R2; |
| 381 | Ok = true; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 382 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 383 | A = R12; |
| 384 | D = R11; |
| 385 | E = R2; |
| 386 | Ok = true; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 389 | |
| 390 | // 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] | 391 | if (!ICmpInst::isEquality(PredR)) |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 392 | return None; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 393 | |
| Chad Rosier | 58919cc | 2016-05-09 21:37:43 +0000 | [diff] [blame] | 394 | // Look for ANDs on the right side of the RHS icmp. |
| Craig Topper | 775ffcc | 2017-08-21 21:00:45 +0000 | [diff] [blame] | 395 | if (!Ok) { |
| Tim Northover | dc647a2 | 2013-09-04 11:57:17 +0000 | [diff] [blame] | 396 | if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) { |
| 397 | R11 = R2; |
| 398 | R12 = Constant::getAllOnesValue(R2->getType()); |
| 399 | } |
| 400 | |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 401 | if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 402 | A = R11; |
| 403 | D = R12; |
| 404 | E = R1; |
| 405 | Ok = true; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 406 | } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 407 | A = R12; |
| 408 | D = R11; |
| 409 | E = R1; |
| 410 | Ok = true; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 411 | } else { |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 412 | return None; |
| Benjamin Kramer | f9d0cc0 | 2012-01-09 17:23:27 +0000 | [diff] [blame] | 413 | } |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 414 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 415 | if (!Ok) |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 416 | return None; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 417 | |
| 418 | if (L11 == A) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 419 | B = L12; |
| 420 | C = L2; |
| Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 421 | } else if (L12 == A) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 422 | B = L11; |
| 423 | C = L2; |
| Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 424 | } else if (L21 == A) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 425 | B = L22; |
| 426 | C = L1; |
| Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 427 | } else if (L22 == A) { |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 428 | B = L21; |
| 429 | C = L1; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 432 | unsigned LeftType = getMaskedICmpType(A, B, C, PredL); |
| 433 | unsigned RightType = getMaskedICmpType(A, D, E, PredR); |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 434 | return Optional<std::pair<unsigned, unsigned>>(std::make_pair(LeftType, RightType)); |
| 435 | } |
| 436 | |
| 437 | /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single |
| 438 | /// (icmp(A & X) ==/!= Y), where the left-hand side is of type Mask_NotAllZeros |
| 439 | /// and the right hand side is of type BMask_Mixed. For example, |
| 440 | /// (icmp (A & 12) != 0) & (icmp (A & 15) == 8) -> (icmp (A & 15) == 8). |
| 441 | static Value * foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed( |
| 442 | ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, |
| 443 | Value *A, Value *B, Value *C, Value *D, Value *E, |
| 444 | ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, |
| 445 | llvm::InstCombiner::BuilderTy &Builder) { |
| 446 | // We are given the canonical form: |
| 447 | // (icmp ne (A & B), 0) & (icmp eq (A & D), E). |
| 448 | // where D & E == E. |
| 449 | // |
| 450 | // If IsAnd is false, we get it in negated form: |
| 451 | // (icmp eq (A & B), 0) | (icmp ne (A & D), E) -> |
| 452 | // !((icmp ne (A & B), 0) & (icmp eq (A & D), E)). |
| 453 | // |
| 454 | // We currently handle the case of B, C, D, E are constant. |
| 455 | // |
| 456 | ConstantInt *BCst = dyn_cast<ConstantInt>(B); |
| 457 | if (!BCst) |
| 458 | return nullptr; |
| 459 | ConstantInt *CCst = dyn_cast<ConstantInt>(C); |
| 460 | if (!CCst) |
| 461 | return nullptr; |
| 462 | ConstantInt *DCst = dyn_cast<ConstantInt>(D); |
| 463 | if (!DCst) |
| 464 | return nullptr; |
| 465 | ConstantInt *ECst = dyn_cast<ConstantInt>(E); |
| 466 | if (!ECst) |
| 467 | return nullptr; |
| 468 | |
| 469 | ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
| 470 | |
| 471 | // Update E to the canonical form when D is a power of two and RHS is |
| 472 | // canonicalized as, |
| 473 | // (icmp ne (A & D), 0) -> (icmp eq (A & D), D) or |
| 474 | // (icmp ne (A & D), D) -> (icmp eq (A & D), 0). |
| 475 | if (PredR != NewCC) |
| 476 | ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst)); |
| 477 | |
| 478 | // If B or D is zero, skip because if LHS or RHS can be trivially folded by |
| 479 | // other folding rules and this pattern won't apply any more. |
| 480 | if (BCst->getValue() == 0 || DCst->getValue() == 0) |
| 481 | return nullptr; |
| 482 | |
| 483 | // If B and D don't intersect, ie. (B & D) == 0, no folding because we can't |
| 484 | // deduce anything from it. |
| 485 | // For example, |
| 486 | // (icmp ne (A & 12), 0) & (icmp eq (A & 3), 1) -> no folding. |
| 487 | if ((BCst->getValue() & DCst->getValue()) == 0) |
| 488 | return nullptr; |
| 489 | |
| 490 | // If the following two conditions are met: |
| 491 | // |
| 492 | // 1. mask B covers only a single bit that's not covered by mask D, that is, |
| 493 | // (B & (B ^ D)) is a power of 2 (in other words, B minus the intersection of |
| 494 | // B and D has only one bit set) and, |
| 495 | // |
| 496 | // 2. RHS (and E) indicates that the rest of B's bits are zero (in other |
| 497 | // words, the intersection of B and D is zero), that is, ((B & D) & E) == 0 |
| 498 | // |
| 499 | // then that single bit in B must be one and thus the whole expression can be |
| 500 | // folded to |
| 501 | // (A & (B | D)) == (B & (B ^ D)) | E. |
| 502 | // |
| 503 | // For example, |
| 504 | // (icmp ne (A & 12), 0) & (icmp eq (A & 7), 1) -> (icmp eq (A & 15), 9) |
| 505 | // (icmp ne (A & 15), 0) & (icmp eq (A & 7), 0) -> (icmp eq (A & 15), 8) |
| 506 | if ((((BCst->getValue() & DCst->getValue()) & ECst->getValue()) == 0) && |
| 507 | (BCst->getValue() & (BCst->getValue() ^ DCst->getValue())).isPowerOf2()) { |
| 508 | APInt BorD = BCst->getValue() | DCst->getValue(); |
| 509 | APInt BandBxorDorE = (BCst->getValue() & (BCst->getValue() ^ DCst->getValue())) | |
| 510 | ECst->getValue(); |
| 511 | Value *NewMask = ConstantInt::get(BCst->getType(), BorD); |
| 512 | Value *NewMaskedValue = ConstantInt::get(BCst->getType(), BandBxorDorE); |
| 513 | Value *NewAnd = Builder.CreateAnd(A, NewMask); |
| 514 | return Builder.CreateICmp(NewCC, NewAnd, NewMaskedValue); |
| 515 | } |
| 516 | |
| 517 | auto IsSubSetOrEqual = [](ConstantInt *C1, ConstantInt *C2) { |
| 518 | return (C1->getValue() & C2->getValue()) == C1->getValue(); |
| 519 | }; |
| 520 | auto IsSuperSetOrEqual = [](ConstantInt *C1, ConstantInt *C2) { |
| 521 | return (C1->getValue() & C2->getValue()) == C2->getValue(); |
| 522 | }; |
| 523 | |
| 524 | // In the following, we consider only the cases where B is a superset of D, B |
| 525 | // is a subset of D, or B == D because otherwise there's at least one bit |
| 526 | // covered by B but not D, in which case we can't deduce much from it, so |
| 527 | // no folding (aside from the single must-be-one bit case right above.) |
| 528 | // For example, |
| 529 | // (icmp ne (A & 14), 0) & (icmp eq (A & 3), 1) -> no folding. |
| 530 | if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst)) |
| 531 | return nullptr; |
| 532 | |
| 533 | // At this point, either B is a superset of D, B is a subset of D or B == D. |
| 534 | |
| 535 | // If E is zero, if B is a subset of (or equal to) D, LHS and RHS contradict |
| 536 | // and the whole expression becomes false (or true if negated), otherwise, no |
| 537 | // folding. |
| 538 | // For example, |
| 539 | // (icmp ne (A & 3), 0) & (icmp eq (A & 7), 0) -> false. |
| 540 | // (icmp ne (A & 15), 0) & (icmp eq (A & 3), 0) -> no folding. |
| 541 | if (ECst->isZero()) { |
| 542 | if (IsSubSetOrEqual(BCst, DCst)) |
| 543 | return ConstantInt::get(LHS->getType(), !IsAnd); |
| 544 | return nullptr; |
| 545 | } |
| 546 | |
| 547 | // At this point, B, D, E aren't zero and (B & D) == B, (B & D) == D or B == |
| 548 | // D. If B is a superset of (or equal to) D, since E is not zero, LHS is |
| 549 | // subsumed by RHS (RHS implies LHS.) So the whole expression becomes |
| 550 | // RHS. For example, |
| 551 | // (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8). |
| 552 | // (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8). |
| 553 | if (IsSuperSetOrEqual(BCst, DCst)) |
| 554 | return RHS; |
| 555 | // Otherwise, B is a subset of D. If B and E have a common bit set, |
| 556 | // ie. (B & E) != 0, then LHS is subsumed by RHS. For example. |
| 557 | // (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8). |
| 558 | assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code"); |
| 559 | if ((BCst->getValue() & ECst->getValue()) != 0) |
| 560 | return RHS; |
| 561 | // Otherwise, LHS and RHS contradict and the whole expression becomes false |
| 562 | // (or true if negated.) For example, |
| 563 | // (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false. |
| 564 | // (icmp ne (A & 6), 0) & (icmp eq (A & 15), 8) -> false. |
| 565 | return ConstantInt::get(LHS->getType(), !IsAnd); |
| 566 | } |
| 567 | |
| 568 | /// Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single |
| 569 | /// (icmp(A & X) ==/!= Y), where the left-hand side and the right hand side |
| 570 | /// aren't of the common mask pattern type. |
| 571 | static Value *foldLogOpOfMaskedICmpsAsymmetric( |
| 572 | ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, |
| 573 | Value *A, Value *B, Value *C, Value *D, Value *E, |
| 574 | ICmpInst::Predicate PredL, ICmpInst::Predicate PredR, |
| 575 | unsigned LHSMask, unsigned RHSMask, |
| 576 | llvm::InstCombiner::BuilderTy &Builder) { |
| 577 | assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && |
| 578 | "Expected equality predicates for masked type of icmps."); |
| 579 | // Handle Mask_NotAllZeros-BMask_Mixed cases. |
| 580 | // (icmp ne/eq (A & B), C) &/| (icmp eq/ne (A & D), E), or |
| 581 | // (icmp eq/ne (A & B), C) &/| (icmp ne/eq (A & D), E) |
| 582 | // which gets swapped to |
| 583 | // (icmp ne/eq (A & D), E) &/| (icmp eq/ne (A & B), C). |
| 584 | if (!IsAnd) { |
| 585 | LHSMask = conjugateICmpMask(LHSMask); |
| 586 | RHSMask = conjugateICmpMask(RHSMask); |
| 587 | } |
| 588 | if ((LHSMask & Mask_NotAllZeros) && (RHSMask & BMask_Mixed)) { |
| 589 | if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed( |
| 590 | LHS, RHS, IsAnd, A, B, C, D, E, |
| 591 | PredL, PredR, Builder)) { |
| 592 | return V; |
| 593 | } |
| 594 | } else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) { |
| 595 | if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed( |
| 596 | RHS, LHS, IsAnd, A, D, E, B, C, |
| 597 | PredR, PredL, Builder)) { |
| 598 | return V; |
| 599 | } |
| 600 | } |
| 601 | return nullptr; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 602 | } |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 603 | |
| 604 | /// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) |
| 605 | /// into a single (icmp(A & X) ==/!= Y). |
| David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 606 | static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 607 | llvm::InstCombiner::BuilderTy &Builder) { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 608 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr; |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 609 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 610 | Optional<std::pair<unsigned, unsigned>> MaskPair = |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 611 | getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR); |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 612 | if (!MaskPair) |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 613 | return nullptr; |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 614 | assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && |
| 615 | "Expected equality predicates for masked type of icmps."); |
| Hiroshi Yamauchi | e6a3dc7 | 2018-03-13 21:13:18 +0000 | [diff] [blame] | 616 | unsigned LHSMask = MaskPair->first; |
| 617 | unsigned RHSMask = MaskPair->second; |
| 618 | unsigned Mask = LHSMask & RHSMask; |
| 619 | if (Mask == 0) { |
| 620 | // Even if the two sides don't share a common pattern, check if folding can |
| 621 | // still happen. |
| 622 | if (Value *V = foldLogOpOfMaskedICmpsAsymmetric( |
| 623 | LHS, RHS, IsAnd, A, B, C, D, E, PredL, PredR, LHSMask, RHSMask, |
| 624 | Builder)) |
| 625 | return V; |
| 626 | return nullptr; |
| 627 | } |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 628 | |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 629 | // In full generality: |
| 630 | // (icmp (A & B) Op C) | (icmp (A & D) Op E) |
| 631 | // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ] |
| 632 | // |
| 633 | // If the latter can be converted into (icmp (A & X) Op Y) then the former is |
| 634 | // equivalent to (icmp (A & X) !Op Y). |
| 635 | // |
| 636 | // Therefore, we can pretend for the rest of this function that we're dealing |
| 637 | // with the conjunction, provided we flip the sense of any comparisons (both |
| 638 | // input and output). |
| 639 | |
| 640 | // 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] | 641 | ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 642 | if (!IsAnd) { |
| 643 | // Convert the masking analysis into its equivalent with negated |
| 644 | // comparisons. |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 645 | Mask = conjugateICmpMask(Mask); |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 646 | } |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 647 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 648 | if (Mask & Mask_AllZeros) { |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 649 | // (icmp eq (A & B), 0) & (icmp eq (A & D), 0) |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 650 | // -> (icmp eq (A & (B|D)), 0) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 651 | Value *NewOr = Builder.CreateOr(B, D); |
| 652 | Value *NewAnd = Builder.CreateAnd(A, NewOr); |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 653 | // We can't use C as zero because we might actually handle |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 654 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 655 | // with B and D, having a single bit set. |
| 656 | Value *Zero = Constant::getNullValue(A->getType()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 657 | return Builder.CreateICmp(NewCC, NewAnd, Zero); |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 658 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 659 | if (Mask & BMask_AllOnes) { |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 660 | // (icmp eq (A & B), B) & (icmp eq (A & D), D) |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 661 | // -> (icmp eq (A & (B|D)), (B|D)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 662 | Value *NewOr = Builder.CreateOr(B, D); |
| 663 | Value *NewAnd = Builder.CreateAnd(A, NewOr); |
| 664 | return Builder.CreateICmp(NewCC, NewAnd, NewOr); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 665 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 666 | if (Mask & AMask_AllOnes) { |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 667 | // (icmp eq (A & B), A) & (icmp eq (A & D), A) |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 668 | // -> (icmp eq (A & (B&D)), A) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 669 | Value *NewAnd1 = Builder.CreateAnd(B, D); |
| 670 | Value *NewAnd2 = Builder.CreateAnd(A, NewAnd1); |
| 671 | return Builder.CreateICmp(NewCC, NewAnd2, A); |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 672 | } |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 673 | |
| 674 | // 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] | 675 | // their actual values. This isn't strictly necessary, just a "handle the |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 676 | // easy cases for now" decision. |
| 677 | ConstantInt *BCst = dyn_cast<ConstantInt>(B); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 678 | if (!BCst) |
| 679 | return nullptr; |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 680 | ConstantInt *DCst = dyn_cast<ConstantInt>(D); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 681 | if (!DCst) |
| 682 | return nullptr; |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 683 | |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 684 | if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) { |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 685 | // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and |
| 686 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
| 687 | // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0) |
| 688 | // Only valid if one of the masks is a superset of the other (check "B&D" is |
| 689 | // the same as either B or D). |
| 690 | APInt NewMask = BCst->getValue() & DCst->getValue(); |
| 691 | |
| 692 | if (NewMask == BCst->getValue()) |
| 693 | return LHS; |
| 694 | else if (NewMask == DCst->getValue()) |
| 695 | return RHS; |
| 696 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 697 | |
| 698 | if (Mask & AMask_NotAllOnes) { |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 699 | // (icmp ne (A & B), B) & (icmp ne (A & D), D) |
| 700 | // -> (icmp ne (A & B), A) or (icmp ne (A & D), A) |
| 701 | // Only valid if one of the masks is a superset of the other (check "B|D" is |
| 702 | // the same as either B or D). |
| 703 | APInt NewMask = BCst->getValue() | DCst->getValue(); |
| 704 | |
| 705 | if (NewMask == BCst->getValue()) |
| 706 | return LHS; |
| 707 | else if (NewMask == DCst->getValue()) |
| 708 | return RHS; |
| 709 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 710 | |
| 711 | if (Mask & BMask_Mixed) { |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 712 | // (icmp eq (A & B), C) & (icmp eq (A & D), E) |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 713 | // We already know that B & C == C && D & E == E. |
| 714 | // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of |
| 715 | // C and E, which are shared by both the mask B and the mask D, don't |
| 716 | // contradict, then we can transform to |
| 717 | // -> (icmp eq (A & (B|D)), (C|E)) |
| 718 | // 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] | 719 | // 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] | 720 | // (icmp ne (A & B), B) & (icmp eq (A & D), D) |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 721 | // with B and D, having a single bit set. |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 722 | ConstantInt *CCst = dyn_cast<ConstantInt>(C); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 723 | if (!CCst) |
| 724 | return nullptr; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 725 | ConstantInt *ECst = dyn_cast<ConstantInt>(E); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 726 | if (!ECst) |
| 727 | return nullptr; |
| 728 | if (PredL != NewCC) |
| David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 729 | CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst)); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 730 | if (PredR != NewCC) |
| David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 731 | ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst)); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 732 | |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 733 | // If there is a conflict, we should actually return a false for the |
| 734 | // whole construct. |
| David Majnemer | 1a3327b | 2014-11-18 09:31:36 +0000 | [diff] [blame] | 735 | if (((BCst->getValue() & DCst->getValue()) & |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 736 | (CCst->getValue() ^ ECst->getValue())).getBoolValue()) |
| David Majnemer | 6fdb6b8 | 2014-11-18 09:31:41 +0000 | [diff] [blame] | 737 | return ConstantInt::get(LHS->getType(), !IsAnd); |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 738 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 739 | Value *NewOr1 = Builder.CreateOr(B, D); |
| Sanjay Patel | 3b8dcc7 | 2016-01-18 18:28:09 +0000 | [diff] [blame] | 740 | Value *NewOr2 = ConstantExpr::getOr(CCst, ECst); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 741 | Value *NewAnd = Builder.CreateAnd(A, NewOr1); |
| 742 | return Builder.CreateICmp(NewCC, NewAnd, NewOr2); |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 743 | } |
| Sanjay Patel | 77bf622 | 2017-04-03 16:53:12 +0000 | [diff] [blame] | 744 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 745 | return nullptr; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 748 | /// Try to fold a signed range checked with lower bound 0 to an unsigned icmp. |
| 749 | /// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n |
| 750 | /// If \p Inverted is true then the check is for the inverted range, e.g. |
| 751 | /// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n |
| 752 | Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, |
| 753 | bool Inverted) { |
| 754 | // Check the lower range comparison, e.g. x >= 0 |
| 755 | // InstCombine already ensured that if there is a constant it's on the RHS. |
| 756 | ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1)); |
| 757 | if (!RangeStart) |
| 758 | return nullptr; |
| 759 | |
| 760 | ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() : |
| 761 | Cmp0->getPredicate()); |
| 762 | |
| 763 | // Accept x > -1 or x >= 0 (after potentially inverting the predicate). |
| 764 | if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) || |
| 765 | (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero()))) |
| 766 | return nullptr; |
| 767 | |
| 768 | ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() : |
| 769 | Cmp1->getPredicate()); |
| 770 | |
| 771 | Value *Input = Cmp0->getOperand(0); |
| 772 | Value *RangeEnd; |
| 773 | if (Cmp1->getOperand(0) == Input) { |
| 774 | // For the upper range compare we have: icmp x, n |
| 775 | RangeEnd = Cmp1->getOperand(1); |
| 776 | } else if (Cmp1->getOperand(1) == Input) { |
| 777 | // For the upper range compare we have: icmp n, x |
| 778 | RangeEnd = Cmp1->getOperand(0); |
| 779 | Pred1 = ICmpInst::getSwappedPredicate(Pred1); |
| 780 | } else { |
| 781 | return nullptr; |
| 782 | } |
| 783 | |
| 784 | // Check the upper range comparison, e.g. x < n |
| 785 | ICmpInst::Predicate NewPred; |
| 786 | switch (Pred1) { |
| 787 | case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break; |
| 788 | case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break; |
| 789 | default: return nullptr; |
| 790 | } |
| 791 | |
| 792 | // This simplification is only valid if the upper range is not negative. |
| Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 793 | KnownBits Known = computeKnownBits(RangeEnd, /*Depth=*/0, Cmp1); |
| 794 | if (!Known.isNonNegative()) |
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 795 | return nullptr; |
| 796 | |
| 797 | if (Inverted) |
| 798 | NewPred = ICmpInst::getInversePredicate(NewPred); |
| 799 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 800 | return Builder.CreateICmp(NewPred, Input, RangeEnd); |
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 803 | static Value * |
| 804 | foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS, |
| 805 | bool JoinedByAnd, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 806 | InstCombiner::BuilderTy &Builder) { |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 807 | Value *X = LHS->getOperand(0); |
| 808 | if (X != RHS->getOperand(0)) |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 809 | return nullptr; |
| 810 | |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 811 | const APInt *C1, *C2; |
| 812 | if (!match(LHS->getOperand(1), m_APInt(C1)) || |
| 813 | !match(RHS->getOperand(1), m_APInt(C2))) |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 814 | return nullptr; |
| 815 | |
| 816 | // We only handle (X != C1 && X != C2) and (X == C1 || X == C2). |
| 817 | ICmpInst::Predicate Pred = LHS->getPredicate(); |
| 818 | if (Pred != RHS->getPredicate()) |
| 819 | return nullptr; |
| 820 | if (JoinedByAnd && Pred != ICmpInst::ICMP_NE) |
| 821 | return nullptr; |
| 822 | if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ) |
| 823 | return nullptr; |
| 824 | |
| 825 | // The larger unsigned constant goes on the right. |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 826 | if (C1->ugt(*C2)) |
| 827 | std::swap(C1, C2); |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 828 | |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 829 | APInt Xor = *C1 ^ *C2; |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 830 | if (Xor.isPowerOf2()) { |
| 831 | // If LHSC and RHSC differ by only one bit, then set that bit in X and |
| 832 | // compare against the larger constant: |
| 833 | // (X == C1 || X == C2) --> (X | (C1 ^ C2)) == C2 |
| 834 | // (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2 |
| 835 | // We choose an 'or' with a Pow2 constant rather than the inverse mask with |
| 836 | // 'and' because that may lead to smaller codegen from a smaller constant. |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 837 | Value *Or = Builder.CreateOr(X, ConstantInt::get(X->getType(), Xor)); |
| 838 | return Builder.CreateICmp(Pred, Or, ConstantInt::get(X->getType(), *C2)); |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Special case: get the ordering right when the values wrap around zero. |
| 842 | // Ie, we assumed the constants were unsigned when swapping earlier. |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 843 | if (C1->isNullValue() && C2->isAllOnesValue()) |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 844 | std::swap(C1, C2); |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 845 | |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 846 | if (*C1 == *C2 - 1) { |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 847 | // (X == 13 || X == 14) --> X - 13 <=u 1 |
| 848 | // (X != 13 && X != 14) --> X - 13 >u 1 |
| 849 | // 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] | 850 | Value *Add = Builder.CreateAdd(X, ConstantInt::get(X->getType(), -(*C1))); |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 851 | auto NewPred = JoinedByAnd ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE; |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 852 | return Builder.CreateICmp(NewPred, Add, ConstantInt::get(X->getType(), 1)); |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | return nullptr; |
| 856 | } |
| 857 | |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 858 | // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) |
| 859 | // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2) |
| 860 | Value *InstCombiner::foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS, |
| 861 | bool JoinedByAnd, |
| 862 | Instruction &CxtI) { |
| 863 | ICmpInst::Predicate Pred = LHS->getPredicate(); |
| 864 | if (Pred != RHS->getPredicate()) |
| 865 | return nullptr; |
| 866 | if (JoinedByAnd && Pred != ICmpInst::ICMP_NE) |
| 867 | return nullptr; |
| 868 | if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ) |
| 869 | return nullptr; |
| 870 | |
| 871 | // TODO support vector splats |
| 872 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 873 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
| 874 | if (!LHSC || !RHSC || !LHSC->isZero() || !RHSC->isZero()) |
| 875 | return nullptr; |
| 876 | |
| 877 | Value *A, *B, *C, *D; |
| 878 | if (match(LHS->getOperand(0), m_And(m_Value(A), m_Value(B))) && |
| 879 | match(RHS->getOperand(0), m_And(m_Value(C), m_Value(D)))) { |
| 880 | if (A == D || B == D) |
| 881 | std::swap(C, D); |
| 882 | if (B == C) |
| 883 | std::swap(A, B); |
| 884 | |
| 885 | if (A == C && |
| 886 | isKnownToBeAPowerOfTwo(B, false, 0, &CxtI) && |
| 887 | isKnownToBeAPowerOfTwo(D, false, 0, &CxtI)) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 888 | Value *Mask = Builder.CreateOr(B, D); |
| 889 | Value *Masked = Builder.CreateAnd(A, Mask); |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 890 | auto NewPred = JoinedByAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 891 | return Builder.CreateICmp(NewPred, Masked, Mask); |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | return nullptr; |
| 896 | } |
| 897 | |
| Roman Lebedev | 3534874 | 2018-08-13 21:54:37 +0000 | [diff] [blame] | 898 | /// General pattern: |
| 899 | /// X & Y |
| 900 | /// |
| 901 | /// Where Y is checking that all the high bits (covered by a mask 4294967168) |
| 902 | /// are uniform, i.e. %arg & 4294967168 can be either 4294967168 or 0 |
| 903 | /// Pattern can be one of: |
| 904 | /// %t = add i32 %arg, 128 |
| 905 | /// %r = icmp ult i32 %t, 256 |
| 906 | /// Or |
| 907 | /// %t0 = shl i32 %arg, 24 |
| 908 | /// %t1 = ashr i32 %t0, 24 |
| 909 | /// %r = icmp eq i32 %t1, %arg |
| 910 | /// Or |
| 911 | /// %t0 = trunc i32 %arg to i8 |
| 912 | /// %t1 = sext i8 %t0 to i32 |
| 913 | /// %r = icmp eq i32 %t1, %arg |
| 914 | /// This pattern is a signed truncation check. |
| 915 | /// |
| 916 | /// And X is checking that some bit in that same mask is zero. |
| 917 | /// I.e. can be one of: |
| 918 | /// %r = icmp sgt i32 %arg, -1 |
| 919 | /// Or |
| 920 | /// %t = and i32 %arg, 2147483648 |
| 921 | /// %r = icmp eq i32 %t, 0 |
| 922 | /// |
| 923 | /// Since we are checking that all the bits in that mask are the same, |
| 924 | /// and a particular bit is zero, what we are really checking is that all the |
| 925 | /// masked bits are zero. |
| 926 | /// So this should be transformed to: |
| 927 | /// %r = icmp ult i32 %arg, 128 |
| 928 | static Value *foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1, |
| 929 | Instruction &CxtI, |
| 930 | InstCombiner::BuilderTy &Builder) { |
| 931 | assert(CxtI.getOpcode() == Instruction::And); |
| 932 | |
| 933 | // Match icmp ult (add %arg, C01), C1 (C1 == C01 << 1; powers of two) |
| 934 | auto tryToMatchSignedTruncationCheck = [](ICmpInst *ICmp, Value *&X, |
| 935 | APInt &SignBitMask) -> bool { |
| 936 | CmpInst::Predicate Pred; |
| 937 | const APInt *I01, *I1; // powers of two; I1 == I01 << 1 |
| 938 | if (!(match(ICmp, |
| 939 | m_ICmp(Pred, m_Add(m_Value(X), m_Power2(I01)), m_Power2(I1))) && |
| 940 | Pred == ICmpInst::ICMP_ULT && I1->ugt(*I01) && I01->shl(1) == *I1)) |
| 941 | return false; |
| 942 | // Which bit is the new sign bit as per the 'signed truncation' pattern? |
| 943 | SignBitMask = *I01; |
| 944 | return true; |
| 945 | }; |
| 946 | |
| 947 | // One icmp needs to be 'signed truncation check'. |
| 948 | // We need to match this first, else we will mismatch commutative cases. |
| 949 | Value *X1; |
| 950 | APInt HighestBit; |
| 951 | ICmpInst *OtherICmp; |
| 952 | if (tryToMatchSignedTruncationCheck(ICmp1, X1, HighestBit)) |
| 953 | OtherICmp = ICmp0; |
| 954 | else if (tryToMatchSignedTruncationCheck(ICmp0, X1, HighestBit)) |
| 955 | OtherICmp = ICmp1; |
| 956 | else |
| 957 | return nullptr; |
| 958 | |
| 959 | assert(HighestBit.isPowerOf2() && "expected to be power of two (non-zero)"); |
| 960 | |
| 961 | // Try to match/decompose into: icmp eq (X & Mask), 0 |
| 962 | auto tryToDecompose = [](ICmpInst *ICmp, Value *&X, |
| 963 | APInt &UnsetBitsMask) -> bool { |
| 964 | CmpInst::Predicate Pred = ICmp->getPredicate(); |
| 965 | // Can it be decomposed into icmp eq (X & Mask), 0 ? |
| 966 | if (llvm::decomposeBitTestICmp(ICmp->getOperand(0), ICmp->getOperand(1), |
| 967 | Pred, X, UnsetBitsMask, |
| Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 968 | /*LookThroughTrunc=*/false) && |
| Roman Lebedev | 3534874 | 2018-08-13 21:54:37 +0000 | [diff] [blame] | 969 | Pred == ICmpInst::ICMP_EQ) |
| 970 | return true; |
| 971 | // Is it icmp eq (X & Mask), 0 already? |
| 972 | const APInt *Mask; |
| 973 | if (match(ICmp, m_ICmp(Pred, m_And(m_Value(X), m_APInt(Mask)), m_Zero())) && |
| 974 | Pred == ICmpInst::ICMP_EQ) { |
| 975 | UnsetBitsMask = *Mask; |
| 976 | return true; |
| 977 | } |
| 978 | return false; |
| 979 | }; |
| 980 | |
| 981 | // And the other icmp needs to be decomposable into a bit test. |
| 982 | Value *X0; |
| 983 | APInt UnsetBitsMask; |
| 984 | if (!tryToDecompose(OtherICmp, X0, UnsetBitsMask)) |
| 985 | return nullptr; |
| 986 | |
| 987 | assert(!UnsetBitsMask.isNullValue() && "empty mask makes no sense."); |
| 988 | |
| 989 | // Are they working on the same value? |
| 990 | Value *X; |
| 991 | if (X1 == X0) { |
| 992 | // Ok as is. |
| 993 | X = X1; |
| 994 | } else if (match(X0, m_Trunc(m_Specific(X1)))) { |
| 995 | UnsetBitsMask = UnsetBitsMask.zext(X1->getType()->getScalarSizeInBits()); |
| 996 | X = X1; |
| 997 | } else |
| 998 | return nullptr; |
| 999 | |
| 1000 | // So which bits should be uniform as per the 'signed truncation check'? |
| 1001 | // (all the bits starting with (i.e. including) HighestBit) |
| 1002 | APInt SignBitsMask = ~(HighestBit - 1U); |
| 1003 | |
| 1004 | // UnsetBitsMask must have some common bits with SignBitsMask, |
| 1005 | if (!UnsetBitsMask.intersects(SignBitsMask)) |
| 1006 | return nullptr; |
| 1007 | |
| 1008 | // Does UnsetBitsMask contain any bits outside of SignBitsMask? |
| 1009 | if (!UnsetBitsMask.isSubsetOf(SignBitsMask)) { |
| 1010 | APInt OtherHighestBit = (~UnsetBitsMask) + 1U; |
| 1011 | if (!OtherHighestBit.isPowerOf2()) |
| 1012 | return nullptr; |
| 1013 | HighestBit = APIntOps::umin(HighestBit, OtherHighestBit); |
| 1014 | } |
| 1015 | // Else, if it does not, then all is ok as-is. |
| 1016 | |
| 1017 | // %r = icmp ult %X, SignBit |
| 1018 | return Builder.CreateICmpULT(X, ConstantInt::get(X->getType(), HighestBit), |
| 1019 | CxtI.getName() + ".simplified"); |
| 1020 | } |
| 1021 | |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1022 | /// Reduce a pair of compares that check if a value has exactly 1 bit set. |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 1023 | static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd, |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1024 | InstCombiner::BuilderTy &Builder) { |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 1025 | // Handle 'and' / 'or' commutation: make the equality check the first operand. |
| 1026 | if (JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_NE) |
| 1027 | std::swap(Cmp0, Cmp1); |
| 1028 | else if (!JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_EQ) |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1029 | std::swap(Cmp0, Cmp1); |
| 1030 | |
| 1031 | // (X != 0) && (ctpop(X) u< 2) --> ctpop(X) == 1 |
| 1032 | CmpInst::Predicate Pred0, Pred1; |
| 1033 | Value *X; |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 1034 | if (JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) && |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1035 | match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)), |
| 1036 | m_SpecificInt(2))) && |
| 1037 | Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT) { |
| 1038 | Value *CtPop = Cmp1->getOperand(0); |
| 1039 | return Builder.CreateICmpEQ(CtPop, ConstantInt::get(CtPop->getType(), 1)); |
| 1040 | } |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 1041 | // (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1 |
| 1042 | if (!JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) && |
| 1043 | match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)), |
| 1044 | m_SpecificInt(1))) && |
| 1045 | Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_UGT) { |
| 1046 | Value *CtPop = Cmp1->getOperand(0); |
| 1047 | return Builder.CreateICmpNE(CtPop, ConstantInt::get(CtPop->getType(), 1)); |
| 1048 | } |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1049 | return nullptr; |
| 1050 | } |
| 1051 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 1052 | /// Fold (icmp)&(icmp) if possible. |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1053 | Value *InstCombiner::foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, |
| 1054 | Instruction &CxtI) { |
| 1055 | // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2) |
| 1056 | // if K1 and K2 are a one-bit mask. |
| 1057 | if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, true, CxtI)) |
| 1058 | return V; |
| 1059 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1060 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1061 | |
| 1062 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| Sanjay Patel | 472652e | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 1063 | if (predicatesFoldable(PredL, PredR)) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1064 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 1065 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 1066 | LHS->swapOperands(); |
| 1067 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 1068 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 1069 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 1070 | unsigned Code = getICmpCode(LHS) & getICmpCode(RHS); |
| Sanjay Patel | 3d5bb15 | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 1071 | bool IsSigned = LHS->isSigned() || RHS->isSigned(); |
| 1072 | return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 1075 | |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1076 | // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E) |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 1077 | if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder)) |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1078 | return V; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1079 | |
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 1080 | // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n |
| 1081 | if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false)) |
| 1082 | return V; |
| 1083 | |
| 1084 | // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n |
| 1085 | if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false)) |
| 1086 | return V; |
| 1087 | |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 1088 | if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder)) |
| 1089 | return V; |
| 1090 | |
| Roman Lebedev | 3534874 | 2018-08-13 21:54:37 +0000 | [diff] [blame] | 1091 | if (Value *V = foldSignedTruncationCheck(LHS, RHS, CxtI, Builder)) |
| 1092 | return V; |
| 1093 | |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 1094 | if (Value *V = foldIsPowerOf2(LHS, RHS, true /* JoinedByAnd */, Builder)) |
| Sanjay Patel | 13a5ae5 | 2019-06-23 14:22:37 +0000 | [diff] [blame] | 1095 | return V; |
| 1096 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1097 | // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1098 | Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1099 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 1100 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
| 1101 | if (!LHSC || !RHSC) |
| 1102 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1103 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1104 | if (LHSC == RHSC && PredL == PredR) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1105 | // (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] | 1106 | // where C is a power of 2 or |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1107 | // (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] | 1108 | if ((PredL == ICmpInst::ICMP_ULT && LHSC->getValue().isPowerOf2()) || |
| 1109 | (PredL == ICmpInst::ICMP_EQ && LHSC->isZero())) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1110 | Value *NewOr = Builder.CreateOr(LHS0, RHS0); |
| 1111 | return Builder.CreateICmp(PredL, NewOr, LHSC); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1114 | |
| Benjamin Kramer | 101720f | 2011-04-28 20:09:57 +0000 | [diff] [blame] | 1115 | // (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] | 1116 | // where CMAX is the all ones value for the truncated type, |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1117 | // iff the lower bits of C2 and CA are zero. |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1118 | if (PredL == ICmpInst::ICMP_EQ && PredL == PredR && LHS->hasOneUse() && |
| 1119 | RHS->hasOneUse()) { |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1120 | Value *V; |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1121 | ConstantInt *AndC, *SmallC = nullptr, *BigC = nullptr; |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1122 | |
| 1123 | // (trunc x) == C1 & (and x, CA) == C2 |
| Craig Topper | ae48cb2 | 2012-12-20 07:15:54 +0000 | [diff] [blame] | 1124 | // (and x, CA) == C2 & (trunc x) == C1 |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1125 | if (match(RHS0, m_Trunc(m_Value(V))) && |
| 1126 | match(LHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) { |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1127 | SmallC = RHSC; |
| 1128 | BigC = LHSC; |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1129 | } else if (match(LHS0, m_Trunc(m_Value(V))) && |
| 1130 | match(RHS0, m_And(m_Specific(V), m_ConstantInt(AndC)))) { |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1131 | SmallC = LHSC; |
| 1132 | BigC = RHSC; |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1135 | if (SmallC && BigC) { |
| 1136 | unsigned BigBitSize = BigC->getType()->getBitWidth(); |
| 1137 | unsigned SmallBitSize = SmallC->getType()->getBitWidth(); |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Check that the low bits are zero. |
| 1140 | APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize); |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1141 | if ((Low & AndC->getValue()).isNullValue() && |
| 1142 | (Low & BigC->getValue()).isNullValue()) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1143 | Value *NewAnd = Builder.CreateAnd(V, Low | AndC->getValue()); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1144 | APInt N = SmallC->getValue().zext(BigBitSize) | BigC->getValue(); |
| 1145 | Value *NewVal = ConstantInt::get(AndC->getType()->getContext(), N); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1146 | return Builder.CreateICmp(PredL, NewAnd, NewVal); |
| Benjamin Kramer | 4145c0d | 2011-04-28 16:58:40 +0000 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | } |
| Benjamin Kramer | da37e15 | 2012-01-08 18:32:24 +0000 | [diff] [blame] | 1150 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1151 | // From here on, we only handle: |
| 1152 | // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1153 | if (LHS0 != RHS0) |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1154 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1155 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1156 | // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere. |
| 1157 | if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE || |
| 1158 | PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE || |
| 1159 | PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE || |
| 1160 | PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1161 | return nullptr; |
| Anders Carlsson | da80afe | 2011-03-01 15:05:01 +0000 | [diff] [blame] | 1162 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1163 | // We can't fold (ugt x, C) & (sgt x, C2). |
| Sanjay Patel | 472652e | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 1164 | if (!predicatesFoldable(PredL, PredR)) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1165 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1166 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1167 | // Ensure that the larger constant is on the RHS. |
| 1168 | bool ShouldSwap; |
| Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 1169 | if (CmpInst::isSigned(PredL) || |
| 1170 | (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR))) |
| Sanjay Patel | 570e35c | 2017-04-10 16:55:57 +0000 | [diff] [blame] | 1171 | ShouldSwap = LHSC->getValue().sgt(RHSC->getValue()); |
| Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 1172 | else |
| 1173 | ShouldSwap = LHSC->getValue().ugt(RHSC->getValue()); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1174 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1175 | if (ShouldSwap) { |
| 1176 | std::swap(LHS, RHS); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1177 | std::swap(LHSC, RHSC); |
| 1178 | std::swap(PredL, PredR); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1181 | // At this point, we know we have two icmp instructions |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1182 | // comparing a value against two constants and and'ing the result |
| 1183 | // together. Because of the above check, we know that we only have |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1184 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 1185 | // (from the icmp folding check above), that the two constants |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1186 | // are not equal and that the larger constant is on the RHS |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1187 | assert(LHSC != RHSC && "Compares not folded above?"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1188 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1189 | switch (PredL) { |
| 1190 | default: |
| 1191 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1192 | case ICmpInst::ICMP_NE: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1193 | switch (PredR) { |
| 1194 | default: |
| 1195 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1196 | case ICmpInst::ICMP_ULT: |
| Craig Topper | 8fabdfe | 2019-07-21 05:26:05 +0000 | [diff] [blame] | 1197 | // (X != 13 & X u< 14) -> X < 13 |
| 1198 | if (LHSC->getValue() == (RHSC->getValue() - 1)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1199 | return Builder.CreateICmpULT(LHS0, LHSC); |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1200 | if (LHSC->isZero()) // (X != 0 & X u< C) -> X-1 u< C-1 |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1201 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 1202 | false, true); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1203 | break; // (X != 13 & X u< 15) -> no change |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1204 | case ICmpInst::ICMP_SLT: |
| Craig Topper | 8fabdfe | 2019-07-21 05:26:05 +0000 | [diff] [blame] | 1205 | // (X != 13 & X s< 14) -> X < 13 |
| 1206 | if (LHSC->getValue() == (RHSC->getValue() - 1)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1207 | return Builder.CreateICmpSLT(LHS0, LHSC); |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1208 | // (X != INT_MIN & X s< C) -> X-(INT_MIN+1) u< (C-(INT_MIN+1)) |
| 1209 | if (LHSC->isMinValue(true)) |
| 1210 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| 1211 | true, true); |
| 1212 | break; // (X != 13 & X s< 15) -> no change |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1213 | case ICmpInst::ICMP_NE: |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 1214 | // Potential folds for this case should already be handled. |
| 1215 | break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1216 | } |
| 1217 | break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1218 | case ICmpInst::ICMP_UGT: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1219 | switch (PredR) { |
| 1220 | default: |
| 1221 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1222 | case ICmpInst::ICMP_NE: |
| Craig Topper | 8fabdfe | 2019-07-21 05:26:05 +0000 | [diff] [blame] | 1223 | // (X u> 13 & X != 14) -> X u> 14 |
| 1224 | if (RHSC->getValue() == (LHSC->getValue() + 1)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1225 | return Builder.CreateICmp(PredL, LHS0, RHSC); |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1226 | // X u> C & X != UINT_MAX -> (X-(C+1)) u< UINT_MAX-(C+1) |
| 1227 | if (RHSC->isMaxValue(false)) |
| 1228 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| 1229 | false, true); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1230 | break; // (X u> 13 & X != 15) -> no change |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1231 | 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] | 1232 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| 1233 | false, true); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1234 | } |
| 1235 | break; |
| 1236 | case ICmpInst::ICMP_SGT: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1237 | switch (PredR) { |
| 1238 | default: |
| 1239 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1240 | case ICmpInst::ICMP_NE: |
| Craig Topper | 8fabdfe | 2019-07-21 05:26:05 +0000 | [diff] [blame] | 1241 | // (X s> 13 & X != 14) -> X s> 14 |
| 1242 | if (RHSC->getValue() == (LHSC->getValue() + 1)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1243 | return Builder.CreateICmp(PredL, LHS0, RHSC); |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1244 | // X s> C & X != INT_MAX -> (X-(C+1)) u< INT_MAX-(C+1) |
| 1245 | if (RHSC->isMaxValue(true)) |
| 1246 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), |
| 1247 | true, true); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1248 | break; // (X s> 13 & X != 15) -> no change |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 1249 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) u< 1 |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 1250 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue(), true, |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 1251 | true); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1252 | } |
| 1253 | break; |
| 1254 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1255 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1256 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 1259 | Value *InstCombiner::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd) { |
| Sanjay Patel | 275bb5a | 2017-09-02 17:17:17 +0000 | [diff] [blame] | 1260 | Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1); |
| 1261 | Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1); |
| 1262 | FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 1263 | |
| Sanjay Patel | 275bb5a | 2017-09-02 17:17:17 +0000 | [diff] [blame] | 1264 | if (LHS0 == RHS1 && RHS0 == LHS1) { |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 1265 | // Swap RHS operands to match LHS. |
| Sanjay Patel | 275bb5a | 2017-09-02 17:17:17 +0000 | [diff] [blame] | 1266 | PredR = FCmpInst::getSwappedPredicate(PredR); |
| 1267 | std::swap(RHS0, RHS1); |
| Tim Shen | aec68b2 | 2016-06-29 20:10:17 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). |
| 1271 | // Suppose the relation between x and y is R, where R is one of |
| 1272 | // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for |
| 1273 | // testing the desired relations. |
| 1274 | // |
| 1275 | // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this: |
| 1276 | // bool(R & CC0) && bool(R & CC1) |
| 1277 | // = bool((R & CC0) & (R & CC1)) |
| 1278 | // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency |
| Sanjay Patel | 4c52f76 | 2017-09-02 16:30:27 +0000 | [diff] [blame] | 1279 | // |
| 1280 | // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this: |
| 1281 | // bool(R & CC0) || bool(R & CC1) |
| 1282 | // = bool((R & CC0) | (R & CC1)) |
| 1283 | // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;) |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 1284 | if (LHS0 == RHS0 && LHS1 == RHS1) { |
| 1285 | unsigned FCmpCodeL = getFCmpCode(PredL); |
| 1286 | unsigned FCmpCodeR = getFCmpCode(PredR); |
| 1287 | unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR; |
| 1288 | return getFCmpValue(NewPred, LHS0, LHS1, Builder); |
| 1289 | } |
| Sanjay Patel | 4c52f76 | 2017-09-02 16:30:27 +0000 | [diff] [blame] | 1290 | |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 1291 | if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) || |
| 1292 | (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) { |
| Sanjay Patel | 275bb5a | 2017-09-02 17:17:17 +0000 | [diff] [blame] | 1293 | if (LHS0->getType() != RHS0->getType()) |
| 1294 | return nullptr; |
| 1295 | |
| Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 1296 | // FCmp canonicalization ensures that (fcmp ord/uno X, X) and |
| Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 1297 | // (fcmp ord/uno X, C) will be transformed to (fcmp X, +0.0). |
| 1298 | if (match(LHS1, m_PosZeroFP()) && match(RHS1, m_PosZeroFP())) |
| Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 1299 | // Ignore the constants because they are obviously not NANs: |
| 1300 | // (fcmp ord x, 0.0) & (fcmp ord y, 0.0) -> (fcmp ord x, y) |
| 1301 | // (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] | 1302 | return Builder.CreateFCmp(PredL, LHS0, RHS0); |
| Sanjay Patel | 4c52f76 | 2017-09-02 16:30:27 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | return nullptr; |
| 1306 | } |
| 1307 | |
| Sanjay Patel | 5b82032 | 2019-03-19 16:39:17 +0000 | [diff] [blame] | 1308 | /// This a limited reassociation for a special case (see above) where we are |
| 1309 | /// checking if two values are either both NAN (unordered) or not-NAN (ordered). |
| 1310 | /// This could be handled more generally in '-reassociation', but it seems like |
| 1311 | /// an unlikely pattern for a large number of logic ops and fcmps. |
| 1312 | static Instruction *reassociateFCmps(BinaryOperator &BO, |
| 1313 | InstCombiner::BuilderTy &Builder) { |
| 1314 | Instruction::BinaryOps Opcode = BO.getOpcode(); |
| 1315 | assert((Opcode == Instruction::And || Opcode == Instruction::Or) && |
| 1316 | "Expecting and/or op for fcmp transform"); |
| 1317 | |
| 1318 | // There are 4 commuted variants of the pattern. Canonicalize operands of this |
| 1319 | // logic op so an fcmp is operand 0 and a matching logic op is operand 1. |
| 1320 | Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1), *X; |
| 1321 | FCmpInst::Predicate Pred; |
| 1322 | if (match(Op1, m_FCmp(Pred, m_Value(), m_AnyZeroFP()))) |
| 1323 | std::swap(Op0, Op1); |
| 1324 | |
| 1325 | // Match inner binop and the predicate for combining 2 NAN checks into 1. |
| 1326 | BinaryOperator *BO1; |
| 1327 | FCmpInst::Predicate NanPred = Opcode == Instruction::And ? FCmpInst::FCMP_ORD |
| 1328 | : FCmpInst::FCMP_UNO; |
| 1329 | if (!match(Op0, m_FCmp(Pred, m_Value(X), m_AnyZeroFP())) || Pred != NanPred || |
| 1330 | !match(Op1, m_BinOp(BO1)) || BO1->getOpcode() != Opcode) |
| 1331 | return nullptr; |
| 1332 | |
| 1333 | // The inner logic op must have a matching fcmp operand. |
| 1334 | Value *BO10 = BO1->getOperand(0), *BO11 = BO1->getOperand(1), *Y; |
| 1335 | if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) || |
| 1336 | Pred != NanPred || X->getType() != Y->getType()) |
| 1337 | std::swap(BO10, BO11); |
| 1338 | |
| 1339 | if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) || |
| 1340 | Pred != NanPred || X->getType() != Y->getType()) |
| 1341 | return nullptr; |
| 1342 | |
| 1343 | // and (fcmp ord X, 0), (and (fcmp ord Y, 0), Z) --> and (fcmp ord X, Y), Z |
| 1344 | // or (fcmp uno X, 0), (or (fcmp uno Y, 0), Z) --> or (fcmp uno X, Y), Z |
| 1345 | Value *NewFCmp = Builder.CreateFCmp(Pred, X, Y); |
| 1346 | if (auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) { |
| 1347 | // Intersect FMF from the 2 source fcmps. |
| 1348 | NewFCmpInst->copyIRFlags(Op0); |
| 1349 | NewFCmpInst->andIRFlags(BO10); |
| 1350 | } |
| 1351 | return BinaryOperator::Create(Opcode, NewFCmp, BO11); |
| 1352 | } |
| 1353 | |
| Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1354 | /// Match De Morgan's Laws: |
| 1355 | /// (~A & ~B) == (~(A | B)) |
| 1356 | /// (~A | ~B) == (~(A & B)) |
| 1357 | static Instruction *matchDeMorgansLaws(BinaryOperator &I, |
| Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 1358 | InstCombiner::BuilderTy &Builder) { |
| Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1359 | auto Opcode = I.getOpcode(); |
| 1360 | assert((Opcode == Instruction::And || Opcode == Instruction::Or) && |
| 1361 | "Trying to match De Morgan's Laws with something other than and/or"); |
| 1362 | |
| Sanjay Patel | 7caaa79 | 2017-05-09 20:05:05 +0000 | [diff] [blame] | 1363 | // Flip the logic operation. |
| 1364 | Opcode = (Opcode == Instruction::And) ? Instruction::Or : Instruction::And; |
| 1365 | |
| 1366 | Value *A, *B; |
| 1367 | if (match(I.getOperand(0), m_OneUse(m_Not(m_Value(A)))) && |
| 1368 | match(I.getOperand(1), m_OneUse(m_Not(m_Value(B)))) && |
| 1369 | !IsFreeToInvert(A, A->hasOneUse()) && |
| 1370 | !IsFreeToInvert(B, B->hasOneUse())) { |
| 1371 | Value *AndOr = Builder.CreateBinOp(Opcode, A, B, I.getName() + ".demorgan"); |
| 1372 | return BinaryOperator::CreateNot(AndOr); |
| 1373 | } |
| Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1374 | |
| 1375 | return nullptr; |
| 1376 | } |
| 1377 | |
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 1378 | bool InstCombiner::shouldOptimizeCast(CastInst *CI) { |
| 1379 | Value *CastSrc = CI->getOperand(0); |
| 1380 | |
| 1381 | // Noop casts and casts of constants should be eliminated trivially. |
| 1382 | if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc)) |
| 1383 | return false; |
| 1384 | |
| 1385 | // If this cast is paired with another cast that can be eliminated, we prefer |
| 1386 | // to have it eliminated. |
| 1387 | if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc)) |
| 1388 | if (isEliminableCastPair(PrecedingCI, CI)) |
| 1389 | return false; |
| 1390 | |
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 1391 | return true; |
| 1392 | } |
| 1393 | |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1394 | /// Fold {and,or,xor} (cast X), C. |
| 1395 | static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1396 | InstCombiner::BuilderTy &Builder) { |
| Craig Topper | 5706c01 | 2017-08-09 06:17:48 +0000 | [diff] [blame] | 1397 | Constant *C = dyn_cast<Constant>(Logic.getOperand(1)); |
| 1398 | if (!C) |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1399 | return nullptr; |
| 1400 | |
| 1401 | auto LogicOpc = Logic.getOpcode(); |
| 1402 | Type *DestTy = Logic.getType(); |
| 1403 | Type *SrcTy = Cast->getSrcTy(); |
| 1404 | |
| Craig Topper | ae9b87d | 2017-08-02 20:25:56 +0000 | [diff] [blame] | 1405 | // Move the logic operation ahead of a zext or sext if the constant is |
| 1406 | // unchanged in the smaller source type. Performing the logic in a smaller |
| 1407 | // type may provide more information to later folds, and the smaller logic |
| 1408 | // instruction may be cheaper (particularly in the case of vectors). |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1409 | Value *X; |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1410 | if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) { |
| 1411 | Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy); |
| 1412 | Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy); |
| 1413 | if (ZextTruncC == C) { |
| 1414 | // LogicOpc (zext X), C --> zext (LogicOpc X, C) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1415 | Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC); |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1416 | return new ZExtInst(NewOp, DestTy); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| Craig Topper | ae9b87d | 2017-08-02 20:25:56 +0000 | [diff] [blame] | 1420 | if (match(Cast, m_OneUse(m_SExt(m_Value(X))))) { |
| 1421 | Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy); |
| 1422 | Constant *SextTruncC = ConstantExpr::getSExt(TruncC, DestTy); |
| 1423 | if (SextTruncC == C) { |
| 1424 | // LogicOpc (sext X), C --> sext (LogicOpc X, C) |
| 1425 | Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC); |
| 1426 | return new SExtInst(NewOp, DestTy); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1430 | return nullptr; |
| 1431 | } |
| 1432 | |
| 1433 | /// Fold {and,or,xor} (cast X), Y. |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1434 | Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) { |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1435 | auto LogicOpc = I.getOpcode(); |
| Sanjay Patel | 1e6ca44 | 2016-11-22 22:54:36 +0000 | [diff] [blame] | 1436 | assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding"); |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1437 | |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1438 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1439 | CastInst *Cast0 = dyn_cast<CastInst>(Op0); |
| Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1440 | if (!Cast0) |
| Sanjay Patel | 7d0d810 | 2016-02-23 16:59:21 +0000 | [diff] [blame] | 1441 | return nullptr; |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1442 | |
| Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1443 | // This must be a cast from an integer or integer vector source type to allow |
| 1444 | // transformation of the logic operation to the source type. |
| 1445 | Type *DestTy = I.getType(); |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1446 | Type *SrcTy = Cast0->getSrcTy(); |
| Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1447 | if (!SrcTy->isIntOrIntVectorTy()) |
| 1448 | return nullptr; |
| 1449 | |
| Sanjay Patel | 60312bc4 | 2016-09-12 00:16:23 +0000 | [diff] [blame] | 1450 | if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder)) |
| 1451 | return Ret; |
| Sanjay Patel | 0753c06 | 2016-07-21 00:24:18 +0000 | [diff] [blame] | 1452 | |
| Sanjay Patel | 9bba750 | 2016-03-03 19:19:04 +0000 | [diff] [blame] | 1453 | CastInst *Cast1 = dyn_cast<CastInst>(Op1); |
| 1454 | if (!Cast1) |
| 1455 | return nullptr; |
| 1456 | |
| 1457 | // Both operands of the logic operation are casts. The casts must be of the |
| 1458 | // same type for reduction. |
| 1459 | auto CastOpcode = Cast0->getOpcode(); |
| 1460 | if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy()) |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1461 | return nullptr; |
| 1462 | |
| 1463 | Value *Cast0Src = Cast0->getOperand(0); |
| 1464 | Value *Cast1Src = Cast1->getOperand(0); |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1465 | |
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 1466 | // fold logic(cast(A), cast(B)) -> cast(logic(A, B)) |
| Tobias Grosser | 8757e38 | 2016-08-03 19:30:35 +0000 | [diff] [blame] | 1467 | if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1468 | Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src, |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1469 | I.getName()); |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1470 | return CastInst::Create(CastOpcode, NewOp, DestTy); |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1471 | } |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1472 | |
| Sanjay Patel | dbbaca0 | 2016-02-24 17:00:34 +0000 | [diff] [blame] | 1473 | // For now, only 'and'/'or' have optimizations after this. |
| 1474 | if (LogicOpc == Instruction::Xor) |
| 1475 | return nullptr; |
| 1476 | |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1477 | // 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] | 1478 | // cast is otherwise not optimizable. This happens for vector sexts. |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1479 | ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src); |
| 1480 | ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src); |
| 1481 | if (ICmp0 && ICmp1) { |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1482 | Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 1483 | : foldOrOfICmps(ICmp0, ICmp1, I); |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1484 | if (Res) |
| 1485 | return CastInst::Create(CastOpcode, Res, DestTy); |
| 1486 | return nullptr; |
| 1487 | } |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1488 | |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1489 | // 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] | 1490 | // cast is otherwise not optimizable. This happens for vector sexts. |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 1491 | FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src); |
| 1492 | FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src); |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 1493 | if (FCmp0 && FCmp1) |
| 1494 | if (Value *R = foldLogicOfFCmps(FCmp0, FCmp1, LogicOpc == Instruction::And)) |
| 1495 | return CastInst::Create(CastOpcode, R, DestTy); |
| Sanjay Patel | 713f25e | 2016-02-23 17:41:34 +0000 | [diff] [blame] | 1496 | |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1497 | return nullptr; |
| 1498 | } |
| 1499 | |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1500 | static Instruction *foldAndToXor(BinaryOperator &I, |
| 1501 | InstCombiner::BuilderTy &Builder) { |
| 1502 | assert(I.getOpcode() == Instruction::And); |
| 1503 | Value *Op0 = I.getOperand(0); |
| 1504 | Value *Op1 = I.getOperand(1); |
| 1505 | Value *A, *B; |
| 1506 | |
| 1507 | // Operand complexity canonicalization guarantees that the 'or' is Op0. |
| 1508 | // (A | B) & ~(A & B) --> A ^ B |
| 1509 | // (A | B) & ~(B & A) --> A ^ B |
| Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 1510 | if (match(&I, m_BinOp(m_Or(m_Value(A), m_Value(B)), |
| 1511 | m_Not(m_c_And(m_Deferred(A), m_Deferred(B)))))) |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1512 | return BinaryOperator::CreateXor(A, B); |
| 1513 | |
| 1514 | // (A | ~B) & (~A | B) --> ~(A ^ B) |
| 1515 | // (A | ~B) & (B | ~A) --> ~(A ^ B) |
| 1516 | // (~B | A) & (~A | B) --> ~(A ^ B) |
| 1517 | // (~B | A) & (B | ~A) --> ~(A ^ B) |
| Craig Topper | 0de5e6a | 2017-06-22 16:12:02 +0000 | [diff] [blame] | 1518 | if (Op0->hasOneUse() || Op1->hasOneUse()) |
| Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 1519 | if (match(&I, m_BinOp(m_c_Or(m_Value(A), m_Not(m_Value(B))), |
| 1520 | m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B))))) |
| Craig Topper | 0de5e6a | 2017-06-22 16:12:02 +0000 | [diff] [blame] | 1521 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1522 | |
| 1523 | return nullptr; |
| 1524 | } |
| 1525 | |
| 1526 | static Instruction *foldOrToXor(BinaryOperator &I, |
| 1527 | InstCombiner::BuilderTy &Builder) { |
| 1528 | assert(I.getOpcode() == Instruction::Or); |
| 1529 | Value *Op0 = I.getOperand(0); |
| 1530 | Value *Op1 = I.getOperand(1); |
| 1531 | Value *A, *B; |
| 1532 | |
| 1533 | // Operand complexity canonicalization guarantees that the 'and' is Op0. |
| 1534 | // (A & B) | ~(A | B) --> ~(A ^ B) |
| 1535 | // (A & B) | ~(B | A) --> ~(A ^ B) |
| Craig Topper | 0de5e6a | 2017-06-22 16:12:02 +0000 | [diff] [blame] | 1536 | if (Op0->hasOneUse() || Op1->hasOneUse()) |
| 1537 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 1538 | match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))) |
| 1539 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1540 | |
| 1541 | // (A & ~B) | (~A & B) --> A ^ B |
| 1542 | // (A & ~B) | (B & ~A) --> A ^ B |
| 1543 | // (~B & A) | (~A & B) --> A ^ B |
| 1544 | // (~B & A) | (B & ~A) --> A ^ B |
| 1545 | if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) && |
| 1546 | match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B)))) |
| 1547 | return BinaryOperator::CreateXor(A, B); |
| 1548 | |
| 1549 | return nullptr; |
| 1550 | } |
| 1551 | |
| Sanjay Patel | 1d68112 | 2018-01-25 16:34:36 +0000 | [diff] [blame] | 1552 | /// Return true if a constant shift amount is always less than the specified |
| 1553 | /// bit-width. If not, the shift could create poison in the narrower type. |
| 1554 | static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth) { |
| 1555 | if (auto *ScalarC = dyn_cast<ConstantInt>(C)) |
| 1556 | return ScalarC->getZExtValue() < BitWidth; |
| 1557 | |
| 1558 | if (C->getType()->isVectorTy()) { |
| 1559 | // Check each element of a constant vector. |
| 1560 | unsigned NumElts = C->getType()->getVectorNumElements(); |
| 1561 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1562 | Constant *Elt = C->getAggregateElement(i); |
| 1563 | if (!Elt) |
| 1564 | return false; |
| 1565 | if (isa<UndefValue>(Elt)) |
| 1566 | continue; |
| 1567 | auto *CI = dyn_cast<ConstantInt>(Elt); |
| 1568 | if (!CI || CI->getZExtValue() >= BitWidth) |
| 1569 | return false; |
| 1570 | } |
| 1571 | return true; |
| 1572 | } |
| 1573 | |
| 1574 | // The constant is a constant expression or unknown. |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
| 1578 | /// Try to use narrower ops (sink zext ops) for an 'and' with binop operand and |
| 1579 | /// a common zext operand: and (binop (zext X), C), (zext X). |
| 1580 | Instruction *InstCombiner::narrowMaskedBinOp(BinaryOperator &And) { |
| 1581 | // This transform could also apply to {or, and, xor}, but there are better |
| 1582 | // folds for those cases, so we don't expect those patterns here. AShr is not |
| 1583 | // handled because it should always be transformed to LShr in this sequence. |
| 1584 | // The subtract transform is different because it has a constant on the left. |
| 1585 | // Add/mul commute the constant to RHS; sub with constant RHS becomes add. |
| 1586 | Value *Op0 = And.getOperand(0), *Op1 = And.getOperand(1); |
| 1587 | Constant *C; |
| 1588 | if (!match(Op0, m_OneUse(m_Add(m_Specific(Op1), m_Constant(C)))) && |
| 1589 | !match(Op0, m_OneUse(m_Mul(m_Specific(Op1), m_Constant(C)))) && |
| 1590 | !match(Op0, m_OneUse(m_LShr(m_Specific(Op1), m_Constant(C)))) && |
| 1591 | !match(Op0, m_OneUse(m_Shl(m_Specific(Op1), m_Constant(C)))) && |
| 1592 | !match(Op0, m_OneUse(m_Sub(m_Constant(C), m_Specific(Op1))))) |
| 1593 | return nullptr; |
| 1594 | |
| 1595 | Value *X; |
| Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 1596 | if (!match(Op1, m_ZExt(m_Value(X))) || Op1->hasNUsesOrMore(3)) |
| Sanjay Patel | 1d68112 | 2018-01-25 16:34:36 +0000 | [diff] [blame] | 1597 | return nullptr; |
| 1598 | |
| 1599 | Type *Ty = And.getType(); |
| 1600 | if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->getType())) |
| 1601 | return nullptr; |
| 1602 | |
| 1603 | // If we're narrowing a shift, the shift amount must be safe (less than the |
| 1604 | // width) in the narrower type. If the shift amount is greater, instsimplify |
| 1605 | // usually handles that case, but we can't guarantee/assert it. |
| 1606 | Instruction::BinaryOps Opc = cast<BinaryOperator>(Op0)->getOpcode(); |
| 1607 | if (Opc == Instruction::LShr || Opc == Instruction::Shl) |
| 1608 | if (!canNarrowShiftAmt(C, X->getType()->getScalarSizeInBits())) |
| 1609 | return nullptr; |
| 1610 | |
| 1611 | // and (sub C, (zext X)), (zext X) --> zext (and (sub C', X), X) |
| 1612 | // and (binop (zext X), C), (zext X) --> zext (and (binop X, C'), X) |
| 1613 | Value *NewC = ConstantExpr::getTrunc(C, X->getType()); |
| 1614 | Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X) |
| 1615 | : Builder.CreateBinOp(Opc, X, NewC); |
| 1616 | return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty); |
| 1617 | } |
| 1618 | |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1619 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 1620 | // here. We should standardize that construct where it is needed or choose some |
| 1621 | // other way to ensure that commutated variants of patterns are not missed. |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1622 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1623 | if (Value *V = SimplifyAndInst(I.getOperand(0), I.getOperand(1), |
| 1624 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1625 | return replaceInstUsesWith(I, V); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1626 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1627 | if (SimplifyAssociativeOrCommutative(I)) |
| 1628 | return &I; |
| 1629 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1630 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1631 | return X; |
| 1632 | |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1633 | // 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] | 1634 | // purpose is to compute bits we don't care about. |
| 1635 | if (SimplifyDemandedInstructionBits(I)) |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1636 | return &I; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1637 | |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1638 | // 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] | 1639 | if (Instruction *Xor = foldAndToXor(I, Builder)) |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 1640 | return Xor; |
| 1641 | |
| 1642 | // (A|B)&(A|C) -> A|(B&C) etc |
| 1643 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 1644 | return replaceInstUsesWith(I, V); |
| 1645 | |
| Craig Topper | 95e4142 | 2017-07-06 16:24:23 +0000 | [diff] [blame] | 1646 | if (Value *V = SimplifyBSwap(I, Builder)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1647 | return replaceInstUsesWith(I, V); |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 1648 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1649 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | dac0ab2 | 2017-07-31 21:01:53 +0000 | [diff] [blame] | 1650 | const APInt *C; |
| 1651 | if (match(Op1, m_APInt(C))) { |
| 1652 | Value *X, *Y; |
| 1653 | if (match(Op0, m_OneUse(m_LogicalShift(m_One(), m_Value(X)))) && |
| 1654 | C->isOneValue()) { |
| 1655 | // (1 << X) & 1 --> zext(X == 0) |
| 1656 | // (1 >> X) & 1 --> zext(X == 0) |
| Sanjay Patel | 3437ee2 | 2017-07-15 17:26:01 +0000 | [diff] [blame] | 1657 | Value *IsZero = Builder.CreateICmpEQ(X, ConstantInt::get(I.getType(), 0)); |
| 1658 | return new ZExtInst(IsZero, I.getType()); |
| 1659 | } |
| Sanjay Patel | dac0ab2 | 2017-07-31 21:01:53 +0000 | [diff] [blame] | 1660 | |
| Craig Topper | a1693a2 | 2017-08-06 23:11:49 +0000 | [diff] [blame] | 1661 | const APInt *XorC; |
| 1662 | if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_APInt(XorC))))) { |
| 1663 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| 1664 | Constant *NewC = ConstantInt::get(I.getType(), *C & *XorC); |
| 1665 | Value *And = Builder.CreateAnd(X, Op1); |
| 1666 | And->takeName(Op0); |
| 1667 | return BinaryOperator::CreateXor(And, NewC); |
| 1668 | } |
| 1669 | |
| Craig Topper | 7091a74 | 2017-08-07 18:10:39 +0000 | [diff] [blame] | 1670 | const APInt *OrC; |
| 1671 | if (match(Op0, m_OneUse(m_Or(m_Value(X), m_APInt(OrC))))) { |
| 1672 | // (X | C1) & C2 --> (X & C2^(C1&C2)) | (C1&C2) |
| 1673 | // NOTE: This reduces the number of bits set in the & mask, which |
| 1674 | // can expose opportunities for store narrowing for scalars. |
| 1675 | // NOTE: SimplifyDemandedBits should have already removed bits from C1 |
| 1676 | // that aren't set in C2. Meaning we can replace (C1&C2) with C1 in |
| 1677 | // above, but this feels safer. |
| 1678 | APInt Together = *C & *OrC; |
| 1679 | Value *And = Builder.CreateAnd(X, ConstantInt::get(I.getType(), |
| 1680 | Together ^ *C)); |
| 1681 | And->takeName(Op0); |
| 1682 | return BinaryOperator::CreateOr(And, ConstantInt::get(I.getType(), |
| 1683 | Together)); |
| 1684 | } |
| 1685 | |
| Sanjay Patel | dac0ab2 | 2017-07-31 21:01:53 +0000 | [diff] [blame] | 1686 | // If the mask is only needed on one incoming arm, push the 'and' op up. |
| 1687 | if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_Value(Y)))) || |
| 1688 | match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { |
| 1689 | APInt NotAndMask(~(*C)); |
| 1690 | BinaryOperator::BinaryOps BinOp = cast<BinaryOperator>(Op0)->getOpcode(); |
| 1691 | if (MaskedValueIsZero(X, NotAndMask, 0, &I)) { |
| 1692 | // Not masking anything out for the LHS, move mask to RHS. |
| 1693 | // and ({x}or X, Y), C --> {x}or X, (and Y, C) |
| 1694 | Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->getName() + ".masked"); |
| 1695 | return BinaryOperator::Create(BinOp, X, NewRHS); |
| 1696 | } |
| 1697 | if (!isa<Constant>(Y) && MaskedValueIsZero(Y, NotAndMask, 0, &I)) { |
| 1698 | // Not masking anything out for the RHS, move mask to LHS. |
| 1699 | // and ({x}or X, Y), C --> {x}or (and X, C), Y |
| 1700 | Value *NewLHS = Builder.CreateAnd(X, Op1, X->getName() + ".masked"); |
| 1701 | return BinaryOperator::Create(BinOp, NewLHS, Y); |
| 1702 | } |
| 1703 | } |
| Craig Topper | a1693a2 | 2017-08-06 23:11:49 +0000 | [diff] [blame] | 1704 | |
| Sanjay Patel | 3437ee2 | 2017-07-15 17:26:01 +0000 | [diff] [blame] | 1705 | } |
| Sanjay Patel | 55b9f88 | 2017-07-15 15:29:47 +0000 | [diff] [blame] | 1706 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1707 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
| 1708 | const APInt &AndRHSMask = AndRHS->getValue(); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1709 | |
| 1710 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 1711 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1712 | // ((C1 OP zext(X)) & C2) -> zext((C1-X) & C2) if C2 fits in the bitwidth |
| 1713 | // of X and OP behaves well when given trunc(C1) and X. |
| Craig Topper | 16dc165 | 2019-03-21 17:50:49 +0000 | [diff] [blame] | 1714 | // TODO: Do this for vectors by using m_APInt isntead of m_ConstantInt. |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1715 | switch (Op0I->getOpcode()) { |
| 1716 | default: |
| 1717 | break; |
| 1718 | case Instruction::Xor: |
| 1719 | case Instruction::Or: |
| 1720 | case Instruction::Mul: |
| 1721 | case Instruction::Add: |
| 1722 | case Instruction::Sub: |
| 1723 | Value *X; |
| 1724 | ConstantInt *C1; |
| Craig Topper | 16dc165 | 2019-03-21 17:50:49 +0000 | [diff] [blame] | 1725 | // TODO: The one use restrictions could be relaxed a little if the AND |
| 1726 | // is going to be removed. |
| 1727 | if (match(Op0I, m_OneUse(m_c_BinOp(m_OneUse(m_ZExt(m_Value(X))), |
| 1728 | m_ConstantInt(C1))))) { |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1729 | if (AndRHSMask.isIntN(X->getType()->getScalarSizeInBits())) { |
| 1730 | auto *TruncC1 = ConstantExpr::getTrunc(C1, X->getType()); |
| 1731 | Value *BinOp; |
| Sanjay Patel | dac0ab2 | 2017-07-31 21:01:53 +0000 | [diff] [blame] | 1732 | Value *Op0LHS = Op0I->getOperand(0); |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1733 | if (isa<ZExtInst>(Op0LHS)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1734 | BinOp = Builder.CreateBinOp(Op0I->getOpcode(), X, TruncC1); |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1735 | else |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1736 | BinOp = Builder.CreateBinOp(Op0I->getOpcode(), TruncC1, X); |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1737 | auto *TruncC2 = ConstantExpr::getTrunc(AndRHS, X->getType()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1738 | auto *And = Builder.CreateAnd(BinOp, TruncC2); |
| David Majnemer | de55c60 | 2017-01-17 18:08:06 +0000 | [diff] [blame] | 1739 | return new ZExtInst(And, I.getType()); |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1744 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
| 1745 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
| 1746 | return Res; |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1747 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1748 | |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1749 | // If this is an integer truncation, and if the source is an 'and' with |
| 1750 | // immediate, transform it. This frequently occurs for bitfield accesses. |
| 1751 | { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1752 | Value *X = nullptr; ConstantInt *YC = nullptr; |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1753 | if (match(Op0, m_Trunc(m_And(m_Value(X), m_ConstantInt(YC))))) { |
| 1754 | // Change: and (trunc (and X, YC) to T), C2 |
| 1755 | // into : and (trunc X to T), trunc(YC) & C2 |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1756 | // This will fold the two constants together, which may allow |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1757 | // other simplifications. |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1758 | Value *NewCast = Builder.CreateTrunc(X, I.getType(), "and.shrunk"); |
| Chris Lattner | dcef03f | 2011-02-10 05:17:27 +0000 | [diff] [blame] | 1759 | Constant *C3 = ConstantExpr::getTrunc(YC, I.getType()); |
| 1760 | C3 = ConstantExpr::getAnd(C3, AndRHS); |
| 1761 | return BinaryOperator::CreateAnd(NewCast, C3); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1762 | } |
| 1763 | } |
| Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 1764 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1765 | |
| Sanjay Patel | 1d68112 | 2018-01-25 16:34:36 +0000 | [diff] [blame] | 1766 | if (Instruction *Z = narrowMaskedBinOp(I)) |
| 1767 | return Z; |
| 1768 | |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 1769 | if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) |
| 1770 | return FoldedLogic; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1771 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1772 | if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) |
| Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 1773 | return DeMorgan; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1774 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1775 | { |
| Sanjay Patel | 9a801cb | 2018-07-31 13:00:03 +0000 | [diff] [blame] | 1776 | Value *A, *B, *C; |
| 1777 | // A & (A ^ B) --> A & ~B |
| 1778 | if (match(Op1, m_OneUse(m_c_Xor(m_Specific(Op0), m_Value(B))))) |
| 1779 | return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B)); |
| 1780 | // (A ^ B) & A --> A & ~B |
| 1781 | if (match(Op0, m_OneUse(m_c_Xor(m_Specific(Op1), m_Value(B))))) |
| 1782 | return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B)); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1783 | |
| David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 1784 | // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C |
| 1785 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) |
| 1786 | 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] | 1787 | if (Op1->hasOneUse() || IsFreeToInvert(C, C->hasOneUse())) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1788 | return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C)); |
| David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 1789 | |
| 1790 | // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C |
| 1791 | if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) |
| 1792 | if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) |
| Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 1793 | if (Op0->hasOneUse() || IsFreeToInvert(C, C->hasOneUse())) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1794 | return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C)); |
| Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1795 | |
| 1796 | // (A | B) & ((~A) ^ B) -> (A & B) |
| Craig Topper | ba01143 | 2017-04-25 15:19:04 +0000 | [diff] [blame] | 1797 | // (A | B) & (B ^ (~A)) -> (A & B) |
| 1798 | // (B | A) & ((~A) ^ B) -> (A & B) |
| 1799 | // (B | A) & (B ^ (~A)) -> (A & B) |
| 1800 | if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) && |
| 1801 | match(Op0, m_c_Or(m_Specific(A), m_Specific(B)))) |
| Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1802 | return BinaryOperator::CreateAnd(A, B); |
| 1803 | |
| 1804 | // ((~A) ^ B) & (A | B) -> (A & B) |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 1805 | // ((~A) ^ B) & (B | A) -> (A & B) |
| Craig Topper | ba01143 | 2017-04-25 15:19:04 +0000 | [diff] [blame] | 1806 | // (B ^ (~A)) & (A | B) -> (A & B) |
| 1807 | // (B ^ (~A)) & (B | A) -> (A & B) |
| 1808 | 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] | 1809 | match(Op1, m_c_Or(m_Specific(A), m_Specific(B)))) |
| Suyog Sarda | 1c6c2f6 | 2014-08-01 04:59:26 +0000 | [diff] [blame] | 1810 | return BinaryOperator::CreateAnd(A, B); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1811 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1812 | |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1813 | { |
| 1814 | ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); |
| 1815 | ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); |
| 1816 | if (LHS && RHS) |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1817 | if (Value *Res = foldAndOfICmps(LHS, RHS, I)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1818 | return replaceInstUsesWith(I, Res); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1819 | |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1820 | // TODO: Make this recursive; it's a little tricky because an arbitrary |
| 1821 | // number of 'and' instructions might have to be created. |
| 1822 | Value *X, *Y; |
| 1823 | if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { |
| 1824 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1825 | if (Value *Res = foldAndOfICmps(LHS, Cmp, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1826 | return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y)); |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1827 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1828 | if (Value *Res = foldAndOfICmps(LHS, Cmp, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1829 | return replaceInstUsesWith(I, Builder.CreateAnd(Res, X)); |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1830 | } |
| 1831 | if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) { |
| 1832 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1833 | if (Value *Res = foldAndOfICmps(Cmp, RHS, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1834 | return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y)); |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1835 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 1836 | if (Value *Res = foldAndOfICmps(Cmp, RHS, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1837 | return replaceInstUsesWith(I, Builder.CreateAnd(Res, X)); |
| David Majnemer | 5e96f1b | 2014-08-30 06:18:20 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 1841 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) |
| 1842 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 1843 | if (Value *Res = foldLogicOfFCmps(LHS, RHS, true)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1844 | return replaceInstUsesWith(I, Res); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1845 | |
| Sanjay Patel | 5b82032 | 2019-03-19 16:39:17 +0000 | [diff] [blame] | 1846 | if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder)) |
| 1847 | return FoldedFCmps; |
| 1848 | |
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 1849 | if (Instruction *CastedAnd = foldCastedBitwiseLogic(I)) |
| 1850 | return CastedAnd; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1851 | |
| Craig Topper | 760ff6e | 2017-08-04 16:07:20 +0000 | [diff] [blame] | 1852 | // and(sext(A), B) / and(B, sext(A)) --> A ? B : 0, where A is i1 or <N x i1>. |
| 1853 | Value *A; |
| 1854 | if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) && |
| 1855 | A->getType()->isIntOrIntVectorTy(1)) |
| 1856 | return SelectInst::Create(A, Op1, Constant::getNullValue(I.getType())); |
| 1857 | if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) && |
| 1858 | A->getType()->isIntOrIntVectorTy(1)) |
| 1859 | return SelectInst::Create(A, Op0, Constant::getNullValue(I.getType())); |
| Nadav Rotem | 513bd8a | 2013-01-30 06:35:22 +0000 | [diff] [blame] | 1860 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1861 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| Sanjay Patel | 6072842 | 2018-11-14 16:03:36 +0000 | [diff] [blame] | 1864 | Instruction *InstCombiner::matchBSwap(BinaryOperator &Or) { |
| 1865 | assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'"); |
| 1866 | Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1); |
| Chad Rosier | e5819e2 | 2016-05-26 14:58:51 +0000 | [diff] [blame] | 1867 | |
| 1868 | // Look through zero extends. |
| 1869 | if (Instruction *Ext = dyn_cast<ZExtInst>(Op0)) |
| 1870 | Op0 = Ext->getOperand(0); |
| 1871 | |
| 1872 | if (Instruction *Ext = dyn_cast<ZExtInst>(Op1)) |
| 1873 | Op1 = Ext->getOperand(0); |
| 1874 | |
| 1875 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 1876 | bool OrOfOrs = match(Op0, m_Or(m_Value(), m_Value())) || |
| 1877 | match(Op1, m_Or(m_Value(), m_Value())); |
| 1878 | |
| 1879 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
| 1880 | bool OrOfShifts = match(Op0, m_LogicalShift(m_Value(), m_Value())) && |
| 1881 | match(Op1, m_LogicalShift(m_Value(), m_Value())); |
| 1882 | |
| 1883 | // (A & B) | (C & D) -> bswap if possible. |
| 1884 | bool OrOfAnds = match(Op0, m_And(m_Value(), m_Value())) && |
| 1885 | match(Op1, m_And(m_Value(), m_Value())); |
| 1886 | |
| Omer Paparo Bivas | 82ef8e1 | 2018-05-01 12:25:46 +0000 | [diff] [blame] | 1887 | // (A << B) | (C & D) -> bswap if possible. |
| 1888 | // The bigger pattern here is ((A & C1) << C2) | ((B >> C2) & C1), which is a |
| 1889 | // part of the bswap idiom for specific values of C1, C2 (e.g. C1 = 16711935, |
| 1890 | // C2 = 8 for i32). |
| 1891 | // This pattern can occur when the operands of the 'or' are not canonicalized |
| 1892 | // for some reason (not having only one use, for example). |
| 1893 | bool OrOfAndAndSh = (match(Op0, m_LogicalShift(m_Value(), m_Value())) && |
| 1894 | match(Op1, m_And(m_Value(), m_Value()))) || |
| 1895 | (match(Op0, m_And(m_Value(), m_Value())) && |
| 1896 | match(Op1, m_LogicalShift(m_Value(), m_Value()))); |
| 1897 | |
| 1898 | if (!OrOfOrs && !OrOfShifts && !OrOfAnds && !OrOfAndAndSh) |
| Chad Rosier | e5819e2 | 2016-05-26 14:58:51 +0000 | [diff] [blame] | 1899 | return nullptr; |
| 1900 | |
| James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1901 | SmallVector<Instruction*, 4> Insts; |
| Sanjay Patel | 6072842 | 2018-11-14 16:03:36 +0000 | [diff] [blame] | 1902 | if (!recognizeBSwapOrBitReverseIdiom(&Or, true, false, Insts)) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1903 | return nullptr; |
| James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1904 | Instruction *LastInst = Insts.pop_back_val(); |
| 1905 | LastInst->removeFromParent(); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 1906 | |
| James Molloy | f01488e | 2016-01-15 09:20:19 +0000 | [diff] [blame] | 1907 | for (auto *Inst : Insts) |
| 1908 | Worklist.Add(Inst); |
| 1909 | return LastInst; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| Sanjay Patel | d023dd6 | 2019-01-08 22:39:55 +0000 | [diff] [blame] | 1912 | /// Transform UB-safe variants of bitwise rotate to the funnel shift intrinsic. |
| 1913 | static Instruction *matchRotate(Instruction &Or) { |
| 1914 | // TODO: Can we reduce the code duplication between this and the related |
| 1915 | // rotate matching code under visitSelect and visitTrunc? |
| 1916 | unsigned Width = Or.getType()->getScalarSizeInBits(); |
| 1917 | if (!isPowerOf2_32(Width)) |
| 1918 | return nullptr; |
| 1919 | |
| 1920 | // First, find an or'd pair of opposite shifts with the same shifted operand: |
| 1921 | // or (lshr ShVal, ShAmt0), (shl ShVal, ShAmt1) |
| Sanjay Patel | 587fd84 | 2019-02-11 19:26:27 +0000 | [diff] [blame] | 1922 | BinaryOperator *Or0, *Or1; |
| 1923 | if (!match(Or.getOperand(0), m_BinOp(Or0)) || |
| 1924 | !match(Or.getOperand(1), m_BinOp(Or1))) |
| 1925 | return nullptr; |
| 1926 | |
| Sanjay Patel | d023dd6 | 2019-01-08 22:39:55 +0000 | [diff] [blame] | 1927 | Value *ShVal, *ShAmt0, *ShAmt1; |
| 1928 | if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal), m_Value(ShAmt0)))) || |
| 1929 | !match(Or1, m_OneUse(m_LogicalShift(m_Specific(ShVal), m_Value(ShAmt1))))) |
| 1930 | return nullptr; |
| 1931 | |
| Sanjay Patel | 587fd84 | 2019-02-11 19:26:27 +0000 | [diff] [blame] | 1932 | BinaryOperator::BinaryOps ShiftOpcode0 = Or0->getOpcode(); |
| 1933 | BinaryOperator::BinaryOps ShiftOpcode1 = Or1->getOpcode(); |
| Sanjay Patel | d023dd6 | 2019-01-08 22:39:55 +0000 | [diff] [blame] | 1934 | if (ShiftOpcode0 == ShiftOpcode1) |
| 1935 | return nullptr; |
| 1936 | |
| 1937 | // Match the shift amount operands for a rotate pattern. This always matches |
| 1938 | // a subtraction on the R operand. |
| 1939 | auto matchShiftAmount = [](Value *L, Value *R, unsigned Width) -> Value * { |
| 1940 | // The shift amount may be masked with negation: |
| 1941 | // (shl ShVal, (X & (Width - 1))) | (lshr ShVal, ((-X) & (Width - 1))) |
| 1942 | Value *X; |
| 1943 | unsigned Mask = Width - 1; |
| 1944 | if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) && |
| 1945 | match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))) |
| 1946 | return X; |
| 1947 | |
| Sanjay Patel | 760f61a | 2019-05-13 17:28:19 +0000 | [diff] [blame] | 1948 | // Similar to above, but the shift amount may be extended after masking, |
| 1949 | // so return the extended value as the parameter for the intrinsic. |
| 1950 | if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) && |
| 1951 | match(R, m_And(m_Neg(m_ZExt(m_And(m_Specific(X), m_SpecificInt(Mask)))), |
| 1952 | m_SpecificInt(Mask)))) |
| 1953 | return L; |
| 1954 | |
| Sanjay Patel | d023dd6 | 2019-01-08 22:39:55 +0000 | [diff] [blame] | 1955 | return nullptr; |
| 1956 | }; |
| 1957 | |
| 1958 | Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width); |
| 1959 | bool SubIsOnLHS = false; |
| 1960 | if (!ShAmt) { |
| 1961 | ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width); |
| 1962 | SubIsOnLHS = true; |
| 1963 | } |
| 1964 | if (!ShAmt) |
| 1965 | return nullptr; |
| 1966 | |
| 1967 | bool IsFshl = (!SubIsOnLHS && ShiftOpcode0 == BinaryOperator::Shl) || |
| 1968 | (SubIsOnLHS && ShiftOpcode1 == BinaryOperator::Shl); |
| 1969 | Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr; |
| 1970 | Function *F = Intrinsic::getDeclaration(Or.getModule(), IID, Or.getType()); |
| 1971 | return IntrinsicInst::Create(F, { ShVal, ShVal, ShAmt }); |
| 1972 | } |
| 1973 | |
| Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1974 | /// If all elements of two constant vectors are 0/-1 and inverses, return true. |
| 1975 | static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) { |
| 1976 | unsigned NumElts = C1->getType()->getVectorNumElements(); |
| 1977 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1978 | Constant *EltC1 = C1->getAggregateElement(i); |
| 1979 | Constant *EltC2 = C2->getAggregateElement(i); |
| 1980 | if (!EltC1 || !EltC2) |
| 1981 | return false; |
| 1982 | |
| 1983 | // One element must be all ones, and the other must be all zeros. |
| Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 1984 | if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) || |
| 1985 | (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes())))) |
| 1986 | return false; |
| 1987 | } |
| 1988 | return true; |
| 1989 | } |
| 1990 | |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1991 | /// We have an expression of the form (A & C) | (B & D). If A is a scalar or |
| 1992 | /// vector composed of all-zeros or all-ones values and is the bitwise 'not' of |
| 1993 | /// B, it can be used as the condition operand of a select instruction. |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 1994 | Value *InstCombiner::getSelectCondition(Value *A, Value *B) { |
| 1995 | // Step 1: We may have peeked through bitcasts in the caller. |
| 1996 | // Exit immediately if we don't have (vector) integer types. |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 1997 | Type *Ty = A->getType(); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 1998 | if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy()) |
| 1999 | return nullptr; |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2000 | |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2001 | // Step 2: We need 0 or all-1's bitmasks. |
| 2002 | if (ComputeNumSignBits(A) != Ty->getScalarSizeInBits()) |
| 2003 | return nullptr; |
| 2004 | |
| 2005 | // Step 3: If B is the 'not' value of A, we have our answer. |
| 2006 | if (match(A, m_Not(m_Specific(B)))) { |
| 2007 | // If these are scalars or vectors of i1, A can be used directly. |
| 2008 | if (Ty->isIntOrIntVectorTy(1)) |
| 2009 | return A; |
| 2010 | return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(Ty)); |
| 2011 | } |
| 2012 | |
| 2013 | // If both operands are constants, see if the constants are inverse bitmasks. |
| 2014 | Constant *AConst, *BConst; |
| 2015 | if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst))) |
| 2016 | if (AConst == ConstantExpr::getNot(BConst)) |
| 2017 | return Builder.CreateZExtOrTrunc(A, CmpInst::makeCmpResultType(Ty)); |
| 2018 | |
| 2019 | // Look for more complex patterns. The 'not' op may be hidden behind various |
| 2020 | // casts. Look through sexts and bitcasts to find the booleans. |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2021 | Value *Cond; |
| Sanjay Patel | d1e8119 | 2017-06-22 15:46:54 +0000 | [diff] [blame] | 2022 | Value *NotB; |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2023 | if (match(A, m_SExt(m_Value(Cond))) && |
| Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 2024 | Cond->getType()->isIntOrIntVectorTy(1) && |
| Sanjay Patel | d1e8119 | 2017-06-22 15:46:54 +0000 | [diff] [blame] | 2025 | match(B, m_OneUse(m_Not(m_Value(NotB))))) { |
| 2026 | NotB = peekThroughBitcast(NotB, true); |
| 2027 | if (match(NotB, m_SExt(m_Specific(Cond)))) |
| 2028 | return Cond; |
| 2029 | } |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2030 | |
| Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 2031 | // All scalar (and most vector) possibilities should be handled now. |
| 2032 | // Try more matches that only apply to non-splat constant vectors. |
| 2033 | if (!Ty->isVectorTy()) |
| 2034 | return nullptr; |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2035 | |
| Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 2036 | // If both operands are xor'd with constants using the same sexted boolean |
| 2037 | // operand, see if the constants are inverse bitmasks. |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2038 | // TODO: Use ConstantExpr::getNot()? |
| 2039 | if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) && |
| 2040 | match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) && |
| Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 2041 | Cond->getType()->isIntOrIntVectorTy(1) && |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2042 | areInverseVectorBitmasks(AConst, BConst)) { |
| 2043 | AConst = ConstantExpr::getTrunc(AConst, CmpInst::makeCmpResultType(Ty)); |
| 2044 | return Builder.CreateXor(Cond, AConst); |
| Sanjay Patel | c00e48a | 2016-07-13 18:07:02 +0000 | [diff] [blame] | 2045 | } |
| Sanjay Patel | 7ad98ba | 2016-06-30 14:18:18 +0000 | [diff] [blame] | 2046 | return nullptr; |
| 2047 | } |
| 2048 | |
| 2049 | /// We have an expression of the form (A & C) | (B & D). Try to simplify this |
| 2050 | /// to "A' ? C : D", where A' is a boolean or vector of booleans. |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2051 | Value *InstCombiner::matchSelectFromAndOr(Value *A, Value *C, Value *B, |
| 2052 | Value *D) { |
| Sanjay Patel | 4e8ebce | 2016-06-24 18:55:27 +0000 | [diff] [blame] | 2053 | // The potential condition of the select may be bitcasted. In that case, look |
| 2054 | // through its bitcast and the corresponding bitcast of the 'not' condition. |
| 2055 | Type *OrigType = A->getType(); |
| Sanjay Patel | e800df8e | 2017-06-22 15:28:01 +0000 | [diff] [blame] | 2056 | A = peekThroughBitcast(A, true); |
| 2057 | B = peekThroughBitcast(B, true); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2058 | if (Value *Cond = getSelectCondition(A, B)) { |
| Sanjay Patel | 6cf18af | 2016-06-03 14:42:07 +0000 | [diff] [blame] | 2059 | // ((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] | 2060 | // The bitcasts will either all exist or all not exist. The builder will |
| 2061 | // not create unnecessary casts if the types already match. |
| 2062 | Value *BitcastC = Builder.CreateBitCast(C, A->getType()); |
| 2063 | Value *BitcastD = Builder.CreateBitCast(D, A->getType()); |
| 2064 | Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD); |
| 2065 | return Builder.CreateBitCast(Select, OrigType); |
| Sanjay Patel | 6cf18af | 2016-06-03 14:42:07 +0000 | [diff] [blame] | 2066 | } |
| Sanjay Patel | 5c0bc02 | 2016-06-02 18:03:05 +0000 | [diff] [blame] | 2067 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2068 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| Sanjay Patel | 1854927 | 2015-09-08 18:24:36 +0000 | [diff] [blame] | 2071 | /// Fold (icmp)|(icmp) if possible. |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2072 | Value *InstCombiner::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2073 | Instruction &CxtI) { |
| Nadav Rotem | 0ed2fdb | 2013-11-12 22:38:59 +0000 | [diff] [blame] | 2074 | // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2) |
| 2075 | // if K1 and K2 are a one-bit mask. |
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 2076 | if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, false, CxtI)) |
| 2077 | return V; |
| 2078 | |
| 2079 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| 2080 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2081 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1)); |
| 2082 | ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1)); |
| Nadav Rotem | 0ed2fdb | 2013-11-12 22:38:59 +0000 | [diff] [blame] | 2083 | |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2084 | // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3) |
| 2085 | // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3) |
| 2086 | // The original condition actually refers to the following two ranges: |
| 2087 | // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3] |
| 2088 | // We can fold these two ranges if: |
| 2089 | // 1) C1 and C2 is unsigned greater than C3. |
| 2090 | // 2) The two ranges are separated. |
| 2091 | // 3) C1 ^ C2 is one-bit mask. |
| 2092 | // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask. |
| 2093 | // This implies all values in the two ranges differ by exactly one bit. |
| 2094 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2095 | if ((PredL == ICmpInst::ICMP_ULT || PredL == ICmpInst::ICMP_ULE) && |
| 2096 | PredL == PredR && LHSC && RHSC && LHS->hasOneUse() && RHS->hasOneUse() && |
| 2097 | LHSC->getType() == RHSC->getType() && |
| 2098 | LHSC->getValue() == (RHSC->getValue())) { |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2099 | |
| 2100 | Value *LAdd = LHS->getOperand(0); |
| 2101 | Value *RAdd = RHS->getOperand(0); |
| 2102 | |
| 2103 | Value *LAddOpnd, *RAddOpnd; |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2104 | ConstantInt *LAddC, *RAddC; |
| 2105 | if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddC))) && |
| 2106 | match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddC))) && |
| 2107 | LAddC->getValue().ugt(LHSC->getValue()) && |
| 2108 | RAddC->getValue().ugt(LHSC->getValue())) { |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2109 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2110 | APInt DiffC = LAddC->getValue() ^ RAddC->getValue(); |
| 2111 | if (LAddOpnd == RAddOpnd && DiffC.isPowerOf2()) { |
| 2112 | ConstantInt *MaxAddC = nullptr; |
| 2113 | if (LAddC->getValue().ult(RAddC->getValue())) |
| 2114 | MaxAddC = RAddC; |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2115 | else |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2116 | MaxAddC = LAddC; |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2117 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2118 | APInt RRangeLow = -RAddC->getValue(); |
| 2119 | APInt RRangeHigh = RRangeLow + LHSC->getValue(); |
| 2120 | APInt LRangeLow = -LAddC->getValue(); |
| 2121 | APInt LRangeHigh = LRangeLow + LHSC->getValue(); |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2122 | APInt LowRangeDiff = RRangeLow ^ LRangeLow; |
| 2123 | APInt HighRangeDiff = RRangeHigh ^ LRangeHigh; |
| 2124 | APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow |
| 2125 | : RRangeLow - LRangeLow; |
| 2126 | |
| 2127 | if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff && |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2128 | RangeDiff.ugt(LHSC->getValue())) { |
| 2129 | Value *MaskC = ConstantInt::get(LAddC->getType(), ~DiffC); |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2130 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2131 | Value *NewAnd = Builder.CreateAnd(LAddOpnd, MaskC); |
| 2132 | Value *NewAdd = Builder.CreateAdd(NewAnd, MaxAddC); |
| 2133 | return Builder.CreateICmp(LHS->getPredicate(), NewAdd, LHSC); |
| Yi Jiang | 1a4e73d | 2014-08-20 22:55:40 +0000 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2139 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| Sanjay Patel | 472652e | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 2140 | if (predicatesFoldable(PredL, PredR)) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2141 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 2142 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 2143 | LHS->swapOperands(); |
| 2144 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 2145 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 2146 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 2147 | unsigned Code = getICmpCode(LHS) | getICmpCode(RHS); |
| Sanjay Patel | 3d5bb15 | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 2148 | bool IsSigned = LHS->isSigned() || RHS->isSigned(); |
| 2149 | return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| Benjamin Kramer | 2bca3a6 | 2010-12-20 16:21:59 +0000 | [diff] [blame] | 2152 | |
| 2153 | // handle (roughly): |
| 2154 | // (icmp ne (A & B), C) | (icmp ne (A & D), E) |
| Tim Northover | c0756c4 | 2013-09-04 11:57:13 +0000 | [diff] [blame] | 2155 | if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder)) |
| Benjamin Kramer | 2bca3a6 | 2010-12-20 16:21:59 +0000 | [diff] [blame] | 2156 | return V; |
| Owen Anderson | 3fe002d | 2010-09-08 22:16:17 +0000 | [diff] [blame] | 2157 | |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2158 | Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2159 | if (LHS->hasOneUse() || RHS->hasOneUse()) { |
| 2160 | // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1) |
| 2161 | // (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] | 2162 | Value *A = nullptr, *B = nullptr; |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2163 | if (PredL == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero()) { |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2164 | B = LHS0; |
| 2165 | if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS->getOperand(1)) |
| 2166 | A = RHS0; |
| 2167 | else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0) |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2168 | A = RHS->getOperand(1); |
| 2169 | } |
| 2170 | // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1) |
| 2171 | // (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] | 2172 | else if (PredR == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) { |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2173 | B = RHS0; |
| 2174 | if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS->getOperand(1)) |
| 2175 | A = LHS0; |
| 2176 | else if (PredL == ICmpInst::ICMP_UGT && LHS0 == RHS0) |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2177 | A = LHS->getOperand(1); |
| 2178 | } |
| 2179 | if (A && B) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2180 | return Builder.CreateICmp( |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2181 | ICmpInst::ICMP_UGE, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2182 | Builder.CreateAdd(B, ConstantInt::getSigned(B->getType(), -1)), A); |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 2185 | // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n |
| 2186 | if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true)) |
| 2187 | return V; |
| 2188 | |
| 2189 | // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n |
| 2190 | if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true)) |
| 2191 | return V; |
| Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 2192 | |
| Sanjay Patel | ef9f586 | 2017-04-15 17:55:06 +0000 | [diff] [blame] | 2193 | if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder)) |
| 2194 | return V; |
| 2195 | |
| Sanjay Patel | 2675b0c | 2019-06-24 22:35:26 +0000 | [diff] [blame] | 2196 | if (Value *V = foldIsPowerOf2(LHS, RHS, false /* JoinedByAnd */, Builder)) |
| 2197 | return V; |
| 2198 | |
| David Majnemer | c2a990b | 2013-07-05 00:31:17 +0000 | [diff] [blame] | 2199 | // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2200 | if (!LHSC || !RHSC) |
| 2201 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2202 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2203 | if (LHSC == RHSC && PredL == PredR) { |
| Owen Anderson | 8f306a7 | 2010-08-02 09:32:13 +0000 | [diff] [blame] | 2204 | // (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] | 2205 | if (PredL == ICmpInst::ICMP_NE && LHSC->isZero()) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2206 | Value *NewOr = Builder.CreateOr(LHS0, RHS0); |
| 2207 | return Builder.CreateICmp(PredL, NewOr, LHSC); |
| Owen Anderson | 8f306a7 | 2010-08-02 09:32:13 +0000 | [diff] [blame] | 2208 | } |
| Benjamin Kramer | da37e15 | 2012-01-08 18:32:24 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| Benjamin Kramer | f7957d0 | 2010-12-20 20:00:31 +0000 | [diff] [blame] | 2211 | // (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] | 2212 | // iff C2 + CA == C1. |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2213 | if (PredL == ICmpInst::ICMP_ULT && PredR == ICmpInst::ICMP_EQ) { |
| 2214 | ConstantInt *AddC; |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2215 | if (match(LHS0, m_Add(m_Specific(RHS0), m_ConstantInt(AddC)))) |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2216 | if (RHSC->getValue() + AddC->getValue() == LHSC->getValue()) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2217 | return Builder.CreateICmpULE(LHS0, LHSC); |
| Benjamin Kramer | 68531ba | 2010-12-20 16:18:51 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2220 | // From here on, we only handle: |
| 2221 | // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2222 | if (LHS0 != RHS0) |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2223 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2224 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2225 | // ICMP_[US][GL]E X, C is folded to ICMP_[US][GL]T elsewhere. |
| 2226 | if (PredL == ICmpInst::ICMP_UGE || PredL == ICmpInst::ICMP_ULE || |
| 2227 | PredR == ICmpInst::ICMP_UGE || PredR == ICmpInst::ICMP_ULE || |
| 2228 | PredL == ICmpInst::ICMP_SGE || PredL == ICmpInst::ICMP_SLE || |
| 2229 | PredR == ICmpInst::ICMP_SGE || PredR == ICmpInst::ICMP_SLE) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2230 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2231 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2232 | // We can't fold (ugt x, C) | (sgt x, C2). |
| Sanjay Patel | 472652e | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 2233 | if (!predicatesFoldable(PredL, PredR)) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2234 | return nullptr; |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2235 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2236 | // Ensure that the larger constant is on the RHS. |
| 2237 | bool ShouldSwap; |
| Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 2238 | if (CmpInst::isSigned(PredL) || |
| 2239 | (ICmpInst::isEquality(PredL) && CmpInst::isSigned(PredR))) |
| Sanjay Patel | 570e35c | 2017-04-10 16:55:57 +0000 | [diff] [blame] | 2240 | ShouldSwap = LHSC->getValue().sgt(RHSC->getValue()); |
| Sanjay Patel | 28611ac | 2017-04-11 15:57:32 +0000 | [diff] [blame] | 2241 | else |
| 2242 | ShouldSwap = LHSC->getValue().ugt(RHSC->getValue()); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2243 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2244 | if (ShouldSwap) { |
| 2245 | std::swap(LHS, RHS); |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2246 | std::swap(LHSC, RHSC); |
| 2247 | std::swap(PredL, PredR); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2248 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2249 | |
| Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 2250 | // At this point, we know we have two icmp instructions |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2251 | // comparing a value against two constants and or'ing the result |
| 2252 | // together. Because of the above check, we know that we only have |
| 2253 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 2254 | // icmp folding check above), that the two constants are not |
| 2255 | // equal. |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2256 | assert(LHSC != RHSC && "Compares not folded above?"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2257 | |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2258 | switch (PredL) { |
| 2259 | default: |
| 2260 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2261 | case ICmpInst::ICMP_EQ: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2262 | switch (PredR) { |
| 2263 | default: |
| 2264 | llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2265 | case ICmpInst::ICMP_EQ: |
| Sanjay Patel | 7cfe416 | 2017-04-14 19:23:50 +0000 | [diff] [blame] | 2266 | // Potential folds for this case should already be handled. |
| 2267 | break; |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 2268 | case ICmpInst::ICMP_UGT: |
| 2269 | // (X == 0 || X u> C) -> (X-1) u>= C |
| 2270 | if (LHSC->isMinValue(false)) |
| 2271 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue() + 1, |
| 2272 | false, false); |
| 2273 | // (X == 13 | X u> 14) -> no change |
| 2274 | break; |
| 2275 | case ICmpInst::ICMP_SGT: |
| 2276 | // (X == INT_MIN || X s> C) -> (X-(INT_MIN+1)) u>= C-INT_MIN |
| 2277 | if (LHSC->isMinValue(true)) |
| 2278 | return insertRangeTest(LHS0, LHSC->getValue() + 1, RHSC->getValue() + 1, |
| 2279 | true, false); |
| 2280 | // (X == 13 | X s> 14) -> no change |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2281 | break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2282 | } |
| 2283 | break; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2284 | case ICmpInst::ICMP_ULT: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2285 | switch (PredR) { |
| 2286 | default: |
| 2287 | llvm_unreachable("Unknown integer condition code!"); |
| 2288 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 2289 | // (X u< C || X == UINT_MAX) => (X-C) u>= UINT_MAX-C |
| 2290 | if (RHSC->isMaxValue(false)) |
| 2291 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue(), |
| 2292 | false, false); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2293 | break; |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2294 | 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] | 2295 | assert(!RHSC->isMaxValue(false) && "Missed icmp simplification"); |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2296 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1, |
| 2297 | false, false); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2298 | } |
| 2299 | break; |
| 2300 | case ICmpInst::ICMP_SLT: |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2301 | switch (PredR) { |
| 2302 | default: |
| 2303 | llvm_unreachable("Unknown integer condition code!"); |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 2304 | case ICmpInst::ICMP_EQ: |
| 2305 | // (X s< C || X == INT_MAX) => (X-C) u>= INT_MAX-C |
| 2306 | if (RHSC->isMaxValue(true)) |
| 2307 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue(), |
| 2308 | true, false); |
| 2309 | // (X s< 13 | X == 14) -> no change |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2310 | break; |
| Craig Topper | e9abc81 | 2019-07-24 20:57:29 +0000 | [diff] [blame] | 2311 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) u> 2 |
| Sanjay Patel | 599e65b | 2017-05-07 15:11:40 +0000 | [diff] [blame] | 2312 | assert(!RHSC->isMaxValue(true) && "Missed icmp simplification"); |
| Sanjay Patel | e4159d2 | 2017-04-10 19:38:36 +0000 | [diff] [blame] | 2313 | return insertRangeTest(LHS0, LHSC->getValue(), RHSC->getValue() + 1, true, |
| Sanjay Patel | 519a87a | 2017-04-05 17:38:34 +0000 | [diff] [blame] | 2314 | false); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2315 | } |
| 2316 | break; |
| 2317 | } |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2318 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 2321 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 2322 | // here. We should standardize that construct where it is needed or choose some |
| 2323 | // other way to ensure that commutated variants of patterns are not missed. |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2324 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 2325 | if (Value *V = SimplifyOrInst(I.getOperand(0), I.getOperand(1), |
| 2326 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2327 | return replaceInstUsesWith(I, V); |
| Bill Wendling | af13d82 | 2010-03-03 00:35:56 +0000 | [diff] [blame] | 2328 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 2329 | if (SimplifyAssociativeOrCommutative(I)) |
| 2330 | return &I; |
| 2331 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 2332 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 2333 | return X; |
| 2334 | |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2335 | // 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] | 2336 | // purpose is to compute bits we don't care about. |
| 2337 | if (SimplifyDemandedInstructionBits(I)) |
| 2338 | return &I; |
| 2339 | |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 2340 | // 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] | 2341 | if (Instruction *Xor = foldOrToXor(I, Builder)) |
| Sanjay Patel | e0c26e0 | 2017-04-23 22:00:02 +0000 | [diff] [blame] | 2342 | return Xor; |
| 2343 | |
| 2344 | // (A&B)|(A&C) -> A&(B|C) etc |
| 2345 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 2346 | return replaceInstUsesWith(I, V); |
| 2347 | |
| Craig Topper | 95e4142 | 2017-07-06 16:24:23 +0000 | [diff] [blame] | 2348 | if (Value *V = SimplifyBSwap(I, Builder)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2349 | return replaceInstUsesWith(I, V); |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 2350 | |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 2351 | if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) |
| 2352 | return FoldedLogic; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2353 | |
| Sanjay Patel | 6072842 | 2018-11-14 16:03:36 +0000 | [diff] [blame] | 2354 | if (Instruction *BSwap = matchBSwap(I)) |
| Chad Rosier | e5819e2 | 2016-05-26 14:58:51 +0000 | [diff] [blame] | 2355 | return BSwap; |
| 2356 | |
| Sanjay Patel | d023dd6 | 2019-01-08 22:39:55 +0000 | [diff] [blame] | 2357 | if (Instruction *Rotate = matchRotate(I)) |
| 2358 | return Rotate; |
| 2359 | |
| Sanjay Patel | 099b1a4 | 2018-09-01 15:08:59 +0000 | [diff] [blame] | 2360 | Value *X, *Y; |
| 2361 | const APInt *CV; |
| 2362 | if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) && |
| 2363 | !CV->isAllOnesValue() && MaskedValueIsZero(Y, *CV, 0, &I)) { |
| 2364 | // (X ^ C) | Y -> (X | Y) ^ C iff Y & C == 0 |
| 2365 | // The check for a 'not' op is for efficiency (if Y is known zero --> ~X). |
| 2366 | Value *Or = Builder.CreateOr(X, Y); |
| 2367 | return BinaryOperator::CreateXor(Or, ConstantInt::get(I.getType(), *CV)); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2370 | // (A & C)|(B & D) |
| Sanjay Patel | 099b1a4 | 2018-09-01 15:08:59 +0000 | [diff] [blame] | 2371 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2372 | Value *A, *B, *C, *D; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2373 | if (match(Op0, m_And(m_Value(A), m_Value(C))) && |
| 2374 | match(Op1, m_And(m_Value(B), m_Value(D)))) { |
| Craig Topper | afa07c5 | 2017-04-09 06:12:41 +0000 | [diff] [blame] | 2375 | ConstantInt *C1 = dyn_cast<ConstantInt>(C); |
| 2376 | ConstantInt *C2 = dyn_cast<ConstantInt>(D); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2377 | if (C1 && C2) { // (A & C1)|(B & C2) |
| Craig Topper | f720099 | 2017-08-14 00:04:21 +0000 | [diff] [blame] | 2378 | Value *V1 = nullptr, *V2 = nullptr; |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2379 | if ((C1->getValue() & C2->getValue()).isNullValue()) { |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2380 | // ((V | N) & C1) | (V & C2) --> (V|N) & (C1|C2) |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2381 | // iff (C1&C2) == 0 and (N&~C1) == 0 |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2382 | if (match(A, m_Or(m_Value(V1), m_Value(V2))) && |
| Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2383 | ((V1 == B && |
| 2384 | MaskedValueIsZero(V2, ~C1->getValue(), 0, &I)) || // (V|N) |
| 2385 | (V2 == B && |
| 2386 | MaskedValueIsZero(V1, ~C1->getValue(), 0, &I)))) // (N|V) |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2387 | return BinaryOperator::CreateAnd(A, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2388 | Builder.getInt(C1->getValue()|C2->getValue())); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2389 | // Or commutes, try both ways. |
| 2390 | if (match(B, m_Or(m_Value(V1), m_Value(V2))) && |
| Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2391 | ((V1 == A && |
| 2392 | MaskedValueIsZero(V2, ~C2->getValue(), 0, &I)) || // (V|N) |
| 2393 | (V2 == A && |
| 2394 | MaskedValueIsZero(V1, ~C2->getValue(), 0, &I)))) // (N|V) |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2395 | return BinaryOperator::CreateAnd(B, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2396 | Builder.getInt(C1->getValue()|C2->getValue())); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2397 | |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2398 | // ((V|C3)&C1) | ((V|C4)&C2) --> (V|C3|C4)&(C1|C2) |
| Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2399 | // iff (C1&C2) == 0 and (C3&~C1) == 0 and (C4&~C2) == 0. |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2400 | ConstantInt *C3 = nullptr, *C4 = nullptr; |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2401 | if (match(A, m_Or(m_Value(V1), m_ConstantInt(C3))) && |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2402 | (C3->getValue() & ~C1->getValue()).isNullValue() && |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2403 | match(B, m_Or(m_Specific(V1), m_ConstantInt(C4))) && |
| Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2404 | (C4->getValue() & ~C2->getValue()).isNullValue()) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2405 | V2 = Builder.CreateOr(V1, ConstantExpr::getOr(C3, C4), "bitfield"); |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2406 | return BinaryOperator::CreateAnd(V2, |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2407 | Builder.getInt(C1->getValue()|C2->getValue())); |
| Chris Lattner | 9518869 | 2010-01-11 06:55:24 +0000 | [diff] [blame] | 2408 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2409 | } |
| Craig Topper | f720099 | 2017-08-14 00:04:21 +0000 | [diff] [blame] | 2410 | |
| 2411 | if (C1->getValue() == ~C2->getValue()) { |
| 2412 | Value *X; |
| 2413 | |
| 2414 | // ((X|B)&C1)|(B&C2) -> (X&C1) | B iff C1 == ~C2 |
| 2415 | if (match(A, m_c_Or(m_Value(X), m_Specific(B)))) |
| 2416 | return BinaryOperator::CreateOr(Builder.CreateAnd(X, C1), B); |
| 2417 | // (A&C2)|((X|A)&C1) -> (X&C2) | A iff C1 == ~C2 |
| 2418 | if (match(B, m_c_Or(m_Specific(A), m_Value(X)))) |
| 2419 | return BinaryOperator::CreateOr(Builder.CreateAnd(X, C2), A); |
| 2420 | |
| 2421 | // ((X^B)&C1)|(B&C2) -> (X&C1) ^ B iff C1 == ~C2 |
| 2422 | if (match(A, m_c_Xor(m_Value(X), m_Specific(B)))) |
| 2423 | return BinaryOperator::CreateXor(Builder.CreateAnd(X, C1), B); |
| 2424 | // (A&C2)|((X^A)&C1) -> (X&C2) ^ A iff C1 == ~C2 |
| 2425 | if (match(B, m_c_Xor(m_Specific(A), m_Value(X)))) |
| 2426 | return BinaryOperator::CreateXor(Builder.CreateAnd(X, C2), A); |
| 2427 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2428 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2429 | |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2430 | // Don't try to form a select if it's unlikely that we'll get rid of at |
| 2431 | // least one of the operands. A select is generally more expensive than the |
| 2432 | // 'or' that it is replacing. |
| 2433 | if (Op0->hasOneUse() || Op1->hasOneUse()) { |
| 2434 | // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants. |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2435 | if (Value *V = matchSelectFromAndOr(A, C, B, D)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2436 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2437 | if (Value *V = matchSelectFromAndOr(A, C, D, B)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2438 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2439 | if (Value *V = matchSelectFromAndOr(C, A, B, D)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2440 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2441 | if (Value *V = matchSelectFromAndOr(C, A, D, B)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2442 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2443 | if (Value *V = matchSelectFromAndOr(B, D, A, C)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2444 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2445 | if (Value *V = matchSelectFromAndOr(B, D, C, A)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2446 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2447 | if (Value *V = matchSelectFromAndOr(D, B, A, C)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2448 | return replaceInstUsesWith(I, V); |
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 2449 | if (Value *V = matchSelectFromAndOr(D, B, C, A)) |
| Sanjay Patel | f4a08ed | 2016-07-08 20:53:29 +0000 | [diff] [blame] | 2450 | return replaceInstUsesWith(I, V); |
| 2451 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2452 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2453 | |
| David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2454 | // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C |
| 2455 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) |
| 2456 | 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] | 2457 | return BinaryOperator::CreateOr(Op0, C); |
| David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2458 | |
| 2459 | // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C |
| 2460 | if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B)))) |
| 2461 | if (match(Op1, m_Xor(m_Specific(B), m_Specific(A)))) |
| Craig Topper | a7529b6 | 2017-06-19 16:23:49 +0000 | [diff] [blame] | 2462 | return BinaryOperator::CreateOr(Op1, C); |
| David Majnemer | 42af360 | 2014-07-30 21:26:37 +0000 | [diff] [blame] | 2463 | |
| David Majnemer | f1eda23 | 2014-08-14 06:41:38 +0000 | [diff] [blame] | 2464 | // ((B | C) & A) | B -> B | (A & C) |
| 2465 | 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] | 2466 | return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C)); |
| David Majnemer | f1eda23 | 2014-08-14 06:41:38 +0000 | [diff] [blame] | 2467 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2468 | if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder)) |
| Sanjay Patel | b54e62f | 2015-09-08 20:14:13 +0000 | [diff] [blame] | 2469 | return DeMorgan; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2470 | |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2471 | // Canonicalize xor to the RHS. |
| Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2472 | bool SwappedForXor = false; |
| 2473 | if (match(Op0, m_Xor(m_Value(), m_Value()))) { |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2474 | std::swap(Op0, Op1); |
| Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2475 | SwappedForXor = true; |
| 2476 | } |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2477 | |
| 2478 | // A | ( A ^ B) -> A | B |
| 2479 | // A | (~A ^ B) -> A | ~B |
| Chad Rosier | 7813dce | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 2480 | // (A & B) | (A ^ B) |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2481 | if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) { |
| 2482 | if (Op0 == A || Op0 == B) |
| 2483 | return BinaryOperator::CreateOr(A, B); |
| 2484 | |
| Chad Rosier | 7813dce | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 2485 | if (match(Op0, m_And(m_Specific(A), m_Specific(B))) || |
| 2486 | match(Op0, m_And(m_Specific(B), m_Specific(A)))) |
| 2487 | return BinaryOperator::CreateOr(A, B); |
| 2488 | |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2489 | if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2490 | Value *Not = Builder.CreateNot(B, B->getName() + ".not"); |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2491 | return BinaryOperator::CreateOr(Not, Op0); |
| 2492 | } |
| 2493 | if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2494 | Value *Not = Builder.CreateNot(A, A->getName() + ".not"); |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2495 | return BinaryOperator::CreateOr(Not, Op0); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | // A | ~(A | B) -> A | ~B |
| 2500 | // A | ~(A ^ B) -> A | ~B |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2501 | if (match(Op1, m_Not(m_Value(A)))) |
| 2502 | if (BinaryOperator *B = dyn_cast<BinaryOperator>(A)) |
| Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 2503 | if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) && |
| 2504 | Op1->hasOneUse() && (B->getOpcode() == Instruction::Or || |
| 2505 | B->getOpcode() == Instruction::Xor)) { |
| 2506 | Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) : |
| 2507 | B->getOperand(0); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2508 | Value *Not = Builder.CreateNot(NotOp, NotOp->getName() + ".not"); |
| Benjamin Kramer | 5b7a4e0 | 2011-02-20 15:20:01 +0000 | [diff] [blame] | 2509 | return BinaryOperator::CreateOr(Not, Op0); |
| 2510 | } |
| Benjamin Kramer | d5d7f37 | 2011-02-20 13:23:43 +0000 | [diff] [blame] | 2511 | |
| Eli Friedman | e06535b | 2012-03-16 00:52:42 +0000 | [diff] [blame] | 2512 | if (SwappedForXor) |
| 2513 | std::swap(Op0, Op1); |
| 2514 | |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2515 | { |
| 2516 | ICmpInst *LHS = dyn_cast<ICmpInst>(Op0); |
| 2517 | ICmpInst *RHS = dyn_cast<ICmpInst>(Op1); |
| 2518 | if (LHS && RHS) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2519 | if (Value *Res = foldOrOfICmps(LHS, RHS, I)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2520 | return replaceInstUsesWith(I, Res); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2521 | |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2522 | // TODO: Make this recursive; it's a little tricky because an arbitrary |
| 2523 | // number of 'or' instructions might have to be created. |
| 2524 | Value *X, *Y; |
| 2525 | if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { |
| 2526 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2527 | if (Value *Res = foldOrOfICmps(LHS, Cmp, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2528 | return replaceInstUsesWith(I, Builder.CreateOr(Res, Y)); |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2529 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2530 | if (Value *Res = foldOrOfICmps(LHS, Cmp, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2531 | return replaceInstUsesWith(I, Builder.CreateOr(Res, X)); |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2532 | } |
| 2533 | if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) { |
| 2534 | if (auto *Cmp = dyn_cast<ICmpInst>(X)) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2535 | if (Value *Res = foldOrOfICmps(Cmp, RHS, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2536 | return replaceInstUsesWith(I, Builder.CreateOr(Res, Y)); |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2537 | if (auto *Cmp = dyn_cast<ICmpInst>(Y)) |
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 2538 | if (Value *Res = foldOrOfICmps(Cmp, RHS, I)) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2539 | return replaceInstUsesWith(I, Builder.CreateOr(Res, X)); |
| David Majnemer | 3d6f80b | 2014-11-28 19:58:29 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
| Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 2543 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) |
| 2544 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 2545 | if (Value *Res = foldLogicOfFCmps(LHS, RHS, false)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2546 | return replaceInstUsesWith(I, Res); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2547 | |
| Sanjay Patel | 5b82032 | 2019-03-19 16:39:17 +0000 | [diff] [blame] | 2548 | if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder)) |
| 2549 | return FoldedFCmps; |
| 2550 | |
| Sanjay Patel | 75b4ae2 | 2016-02-23 23:56:23 +0000 | [diff] [blame] | 2551 | if (Instruction *CastedOr = foldCastedBitwiseLogic(I)) |
| 2552 | return CastedOr; |
| Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2553 | |
| Sanjay Patel | cbfca9e | 2016-07-08 17:01:15 +0000 | [diff] [blame] | 2554 | // 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] | 2555 | if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) && |
| Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 2556 | A->getType()->isIntOrIntVectorTy(1)) |
| Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2557 | return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op1); |
| Sanjay Patel | 1b6b824 | 2016-07-08 17:26:47 +0000 | [diff] [blame] | 2558 | if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) && |
| Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 2559 | A->getType()->isIntOrIntVectorTy(1)) |
| Eli Friedman | 2395626 | 2011-04-14 22:41:27 +0000 | [diff] [blame] | 2560 | return SelectInst::Create(A, ConstantInt::getSigned(I.getType(), -1), Op0); |
| 2561 | |
| Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2562 | // Note: If we've gotten to the point of visiting the outer OR, then the |
| 2563 | // inner one couldn't be simplified. If it was a constant, then it won't |
| 2564 | // be simplified by a later pass either, so we try swapping the inner/outer |
| 2565 | // ORs in the hopes that we'll be able to simplify it this way. |
| 2566 | // (X|C) | V --> (X|V) | C |
| Sanjay Patel | 099b1a4 | 2018-09-01 15:08:59 +0000 | [diff] [blame] | 2567 | ConstantInt *CI; |
| Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2568 | if (Op0->hasOneUse() && !isa<ConstantInt>(Op1) && |
| Sanjay Patel | 099b1a4 | 2018-09-01 15:08:59 +0000 | [diff] [blame] | 2569 | match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2570 | Value *Inner = Builder.CreateOr(A, Op1); |
| Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2571 | Inner->takeName(Op0); |
| Sanjay Patel | 099b1a4 | 2018-09-01 15:08:59 +0000 | [diff] [blame] | 2572 | return BinaryOperator::CreateOr(Inner, CI); |
| Owen Anderson | c237a84 | 2010-09-13 17:59:27 +0000 | [diff] [blame] | 2573 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2574 | |
| Bill Wendling | 2324209 | 2013-02-16 23:41:36 +0000 | [diff] [blame] | 2575 | // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D)) |
| 2576 | // Since this OR statement hasn't been optimized further yet, we hope |
| 2577 | // that this transformation will allow the new ORs to be optimized. |
| 2578 | { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2579 | Value *X = nullptr, *Y = nullptr; |
| Bill Wendling | 2324209 | 2013-02-16 23:41:36 +0000 | [diff] [blame] | 2580 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| 2581 | match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) && |
| 2582 | 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] | 2583 | Value *orTrue = Builder.CreateOr(A, C); |
| 2584 | Value *orFalse = Builder.CreateOr(B, D); |
| Bill Wendling | 2324209 | 2013-02-16 23:41:36 +0000 | [diff] [blame] | 2585 | return SelectInst::Create(X, orTrue, orFalse); |
| 2586 | } |
| 2587 | } |
| 2588 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 2589 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2592 | /// A ^ B can be specified using other logic ops in a variety of patterns. We |
| 2593 | /// can fold these early and efficiently by morphing an existing instruction. |
| Craig Topper | f60ab47 | 2017-07-02 01:15:51 +0000 | [diff] [blame] | 2594 | static Instruction *foldXorToXor(BinaryOperator &I, |
| 2595 | InstCombiner::BuilderTy &Builder) { |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2596 | assert(I.getOpcode() == Instruction::Xor); |
| 2597 | Value *Op0 = I.getOperand(0); |
| 2598 | Value *Op1 = I.getOperand(1); |
| 2599 | Value *A, *B; |
| 2600 | |
| 2601 | // There are 4 commuted variants for each of the basic patterns. |
| 2602 | |
| 2603 | // (A & B) ^ (A | B) -> A ^ B |
| 2604 | // (A & B) ^ (B | A) -> A ^ B |
| 2605 | // (A | B) ^ (A & B) -> A ^ B |
| 2606 | // (A | B) ^ (B & A) -> A ^ B |
| Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 2607 | if (match(&I, m_c_Xor(m_And(m_Value(A), m_Value(B)), |
| 2608 | m_c_Or(m_Deferred(A), m_Deferred(B))))) { |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2609 | I.setOperand(0, A); |
| 2610 | I.setOperand(1, B); |
| 2611 | return &I; |
| 2612 | } |
| 2613 | |
| 2614 | // (A | ~B) ^ (~A | B) -> A ^ B |
| 2615 | // (~B | A) ^ (~A | B) -> A ^ B |
| 2616 | // (~A | B) ^ (A | ~B) -> A ^ B |
| 2617 | // (B | ~A) ^ (A | ~B) -> A ^ B |
| Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 2618 | if (match(&I, m_Xor(m_c_Or(m_Value(A), m_Not(m_Value(B))), |
| 2619 | m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B))))) { |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2620 | I.setOperand(0, A); |
| 2621 | I.setOperand(1, B); |
| 2622 | return &I; |
| 2623 | } |
| 2624 | |
| 2625 | // (A & ~B) ^ (~A & B) -> A ^ B |
| 2626 | // (~B & A) ^ (~A & B) -> A ^ B |
| 2627 | // (~A & B) ^ (A & ~B) -> A ^ B |
| 2628 | // (B & ~A) ^ (A & ~B) -> A ^ B |
| Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 2629 | if (match(&I, m_Xor(m_c_And(m_Value(A), m_Not(m_Value(B))), |
| 2630 | m_c_And(m_Not(m_Deferred(A)), m_Deferred(B))))) { |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2631 | I.setOperand(0, A); |
| 2632 | I.setOperand(1, B); |
| 2633 | return &I; |
| 2634 | } |
| 2635 | |
| Craig Topper | f60ab47 | 2017-07-02 01:15:51 +0000 | [diff] [blame] | 2636 | // For the remaining cases we need to get rid of one of the operands. |
| 2637 | if (!Op0->hasOneUse() && !Op1->hasOneUse()) |
| 2638 | return nullptr; |
| 2639 | |
| 2640 | // (A | B) ^ ~(A & B) -> ~(A ^ B) |
| 2641 | // (A | B) ^ ~(B & A) -> ~(A ^ B) |
| 2642 | // (A & B) ^ ~(A | B) -> ~(A ^ B) |
| 2643 | // (A & B) ^ ~(B | A) -> ~(A ^ B) |
| 2644 | // Complexity sorting ensures the not will be on the right side. |
| 2645 | if ((match(Op0, m_Or(m_Value(A), m_Value(B))) && |
| 2646 | match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) || |
| 2647 | (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 2648 | match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))) |
| 2649 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
| 2650 | |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2651 | return nullptr; |
| 2652 | } |
| 2653 | |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2654 | Value *InstCombiner::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS) { |
| Sanjay Patel | 472652e | 2018-12-03 15:48:30 +0000 | [diff] [blame] | 2655 | if (predicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) { |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2656 | if (LHS->getOperand(0) == RHS->getOperand(1) && |
| 2657 | LHS->getOperand(1) == RHS->getOperand(0)) |
| 2658 | LHS->swapOperands(); |
| 2659 | if (LHS->getOperand(0) == RHS->getOperand(0) && |
| 2660 | LHS->getOperand(1) == RHS->getOperand(1)) { |
| 2661 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 2662 | Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); |
| 2663 | unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS); |
| Sanjay Patel | 3d5bb15 | 2018-12-04 18:53:27 +0000 | [diff] [blame] | 2664 | bool IsSigned = LHS->isSigned() || RHS->isSigned(); |
| 2665 | return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder); |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
| Sanjay Patel | 94c91b7 | 2018-03-22 14:08:16 +0000 | [diff] [blame] | 2669 | // TODO: This can be generalized to compares of non-signbits using |
| 2670 | // decomposeBitTestICmp(). It could be enhanced more by using (something like) |
| 2671 | // foldLogOpOfMaskedICmps(). |
| 2672 | ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); |
| 2673 | Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1); |
| 2674 | Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1); |
| 2675 | if ((LHS->hasOneUse() || RHS->hasOneUse()) && |
| Amara Emerson | 070ac76 | 2018-08-15 17:46:22 +0000 | [diff] [blame] | 2676 | LHS0->getType() == RHS0->getType() && |
| 2677 | LHS0->getType()->isIntOrIntVectorTy()) { |
| Sanjay Patel | 94c91b7 | 2018-03-22 14:08:16 +0000 | [diff] [blame] | 2678 | // (X > -1) ^ (Y > -1) --> (X ^ Y) < 0 |
| 2679 | // (X < 0) ^ (Y < 0) --> (X ^ Y) < 0 |
| 2680 | if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) && |
| 2681 | PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())) || |
| 2682 | (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) && |
| 2683 | PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero()))) { |
| 2684 | Value *Zero = ConstantInt::getNullValue(LHS0->getType()); |
| 2685 | return Builder.CreateICmpSLT(Builder.CreateXor(LHS0, RHS0), Zero); |
| 2686 | } |
| 2687 | // (X > -1) ^ (Y < 0) --> (X ^ Y) > -1 |
| 2688 | // (X < 0) ^ (Y > -1) --> (X ^ Y) > -1 |
| 2689 | if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) && |
| 2690 | PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())) || |
| 2691 | (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) && |
| 2692 | PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes()))) { |
| 2693 | Value *MinusOne = ConstantInt::getAllOnesValue(LHS0->getType()); |
| 2694 | return Builder.CreateICmpSGT(Builder.CreateXor(LHS0, RHS0), MinusOne); |
| 2695 | } |
| 2696 | } |
| 2697 | |
| Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2698 | // Instead of trying to imitate the folds for and/or, decompose this 'xor' |
| 2699 | // into those logic ops. That is, try to turn this into an and-of-icmps |
| 2700 | // because we have many folds for that pattern. |
| 2701 | // |
| 2702 | // This is based on a truth table definition of xor: |
| 2703 | // X ^ Y --> (X | Y) & !(X & Y) |
| 2704 | if (Value *OrICmp = SimplifyBinOp(Instruction::Or, LHS, RHS, SQ)) { |
| 2705 | // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y). |
| 2706 | // TODO: If OrICmp is false, the whole thing is false (InstSimplify?). |
| 2707 | if (Value *AndICmp = SimplifyBinOp(Instruction::And, LHS, RHS, SQ)) { |
| 2708 | // TODO: Independently handle cases where the 'and' side is a constant. |
| 2709 | if (OrICmp == LHS && AndICmp == RHS && RHS->hasOneUse()) { |
| 2710 | // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS |
| 2711 | RHS->setPredicate(RHS->getInversePredicate()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2712 | return Builder.CreateAnd(LHS, RHS); |
| Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2713 | } |
| 2714 | if (OrICmp == RHS && AndICmp == LHS && LHS->hasOneUse()) { |
| Sanjay Patel | 4ccbd58 | 2017-06-20 12:45:46 +0000 | [diff] [blame] | 2715 | // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS |
| Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2716 | LHS->setPredicate(LHS->getInversePredicate()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2717 | return Builder.CreateAnd(LHS, RHS); |
| Sanjay Patel | adca825 | 2017-06-20 12:40:55 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | } |
| 2721 | |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 2722 | return nullptr; |
| 2723 | } |
| 2724 | |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2725 | /// If we have a masked merge, in the canonical form of: |
| Roman Lebedev | aa4faec | 2018-04-30 17:59:33 +0000 | [diff] [blame] | 2726 | /// (assuming that A only has one use.) |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2727 | /// | A | |B| |
| 2728 | /// ((x ^ y) & M) ^ y |
| 2729 | /// | D | |
| 2730 | /// * If M is inverted: |
| 2731 | /// | D | |
| 2732 | /// ((x ^ y) & ~M) ^ y |
| Roman Lebedev | aa4faec | 2018-04-30 17:59:33 +0000 | [diff] [blame] | 2733 | /// We can canonicalize by swapping the final xor operand |
| 2734 | /// to eliminate the 'not' of the mask. |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2735 | /// ((x ^ y) & M) ^ x |
| Roman Lebedev | aa4faec | 2018-04-30 17:59:33 +0000 | [diff] [blame] | 2736 | /// * If M is a constant, and D has one use, we transform to 'and' / 'or' ops |
| 2737 | /// because that shortens the dependency chain and improves analysis: |
| 2738 | /// (x & M) | (y & ~M) |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2739 | static Instruction *visitMaskedMerge(BinaryOperator &I, |
| 2740 | InstCombiner::BuilderTy &Builder) { |
| 2741 | Value *B, *X, *D; |
| 2742 | Value *M; |
| 2743 | if (!match(&I, m_c_Xor(m_Value(B), |
| 2744 | m_OneUse(m_c_And( |
| 2745 | m_CombineAnd(m_c_Xor(m_Deferred(B), m_Value(X)), |
| 2746 | m_Value(D)), |
| 2747 | m_Value(M)))))) |
| 2748 | return nullptr; |
| 2749 | |
| 2750 | Value *NotM; |
| 2751 | if (match(M, m_Not(m_Value(NotM)))) { |
| 2752 | // De-invert the mask and swap the value in B part. |
| 2753 | Value *NewA = Builder.CreateAnd(D, NotM); |
| 2754 | return BinaryOperator::CreateXor(NewA, X); |
| 2755 | } |
| 2756 | |
| Roman Lebedev | aa4faec | 2018-04-30 17:59:33 +0000 | [diff] [blame] | 2757 | Constant *C; |
| 2758 | if (D->hasOneUse() && match(M, m_Constant(C))) { |
| 2759 | // Unfold. |
| 2760 | Value *LHS = Builder.CreateAnd(X, C); |
| 2761 | Value *NotC = Builder.CreateNot(C); |
| 2762 | Value *RHS = Builder.CreateAnd(B, NotC); |
| 2763 | return BinaryOperator::CreateOr(LHS, RHS); |
| 2764 | } |
| 2765 | |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2766 | return nullptr; |
| 2767 | } |
| 2768 | |
| Roman Lebedev | a677651 | 2018-08-08 13:31:19 +0000 | [diff] [blame] | 2769 | // Transform |
| 2770 | // ~(x ^ y) |
| 2771 | // into: |
| 2772 | // (~x) ^ y |
| 2773 | // or into |
| 2774 | // x ^ (~y) |
| 2775 | static Instruction *sinkNotIntoXor(BinaryOperator &I, |
| 2776 | InstCombiner::BuilderTy &Builder) { |
| 2777 | Value *X, *Y; |
| 2778 | // FIXME: one-use check is not needed in general, but currently we are unable |
| 2779 | // to fold 'not' into 'icmp', if that 'icmp' has multiple uses. (D35182) |
| 2780 | if (!match(&I, m_Not(m_OneUse(m_Xor(m_Value(X), m_Value(Y)))))) |
| 2781 | return nullptr; |
| 2782 | |
| 2783 | // We only want to do the transform if it is free to do. |
| 2784 | if (IsFreeToInvert(X, X->hasOneUse())) { |
| 2785 | // Ok, good. |
| 2786 | } else if (IsFreeToInvert(Y, Y->hasOneUse())) { |
| 2787 | std::swap(X, Y); |
| 2788 | } else |
| 2789 | return nullptr; |
| 2790 | |
| 2791 | Value *NotX = Builder.CreateNot(X, X->getName() + ".not"); |
| 2792 | return BinaryOperator::CreateXor(NotX, Y, I.getName() + ".demorgan"); |
| 2793 | } |
| 2794 | |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 2795 | // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches |
| 2796 | // here. We should standardize that construct where it is needed or choose some |
| 2797 | // other way to ensure that commutated variants of patterns are not missed. |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2798 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 2799 | if (Value *V = SimplifyXorInst(I.getOperand(0), I.getOperand(1), |
| 2800 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2801 | return replaceInstUsesWith(I, V); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2802 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 2803 | if (SimplifyAssociativeOrCommutative(I)) |
| 2804 | return &I; |
| 2805 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 2806 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 2807 | return X; |
| 2808 | |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2809 | if (Instruction *NewXor = foldXorToXor(I, Builder)) |
| Sanjay Patel | d13b0bf | 2017-04-23 16:03:00 +0000 | [diff] [blame] | 2810 | return NewXor; |
| 2811 | |
| Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 2812 | // (A&B)^(A&C) -> A&(B^C) etc |
| 2813 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2814 | return replaceInstUsesWith(I, V); |
| Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 2815 | |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2816 | // 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] | 2817 | // purpose is to compute bits we don't care about. |
| 2818 | if (SimplifyDemandedInstructionBits(I)) |
| 2819 | return &I; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2820 | |
| Craig Topper | 95e4142 | 2017-07-06 16:24:23 +0000 | [diff] [blame] | 2821 | if (Value *V = SimplifyBSwap(I, Builder)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2822 | return replaceInstUsesWith(I, V); |
| Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 2823 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 2824 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Craig Topper | 8bb4921 | 2018-08-13 00:38:27 +0000 | [diff] [blame] | 2825 | |
| 2826 | // Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M) |
| Craig Topper | 8caccc3 | 2018-08-13 00:54:23 +0000 | [diff] [blame] | 2827 | // This it a special case in haveNoCommonBitsSet, but the computeKnownBits |
| Craig Topper | 8bb4921 | 2018-08-13 00:38:27 +0000 | [diff] [blame] | 2828 | // calls in there are unnecessary as SimplifyDemandedInstructionBits should |
| 2829 | // have already taken care of those cases. |
| 2830 | Value *M; |
| 2831 | if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()), |
| 2832 | m_c_And(m_Deferred(M), m_Value())))) |
| Roman Lebedev | f84bfb2 | 2018-04-15 18:59:44 +0000 | [diff] [blame] | 2833 | return BinaryOperator::CreateOr(Op0, Op1); |
| 2834 | |
| Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2835 | // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand. |
| 2836 | Value *X, *Y; |
| 2837 | |
| 2838 | // We must eliminate the and/or (one-use) for these transforms to not increase |
| 2839 | // the instruction count. |
| 2840 | // ~(~X & Y) --> (X | ~Y) |
| 2841 | // ~(Y & ~X) --> (X | ~Y) |
| 2842 | 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] | 2843 | Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not"); |
| Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2844 | return BinaryOperator::CreateOr(X, NotY); |
| 2845 | } |
| 2846 | // ~(~X | Y) --> (X & ~Y) |
| 2847 | // ~(Y | ~X) --> (X & ~Y) |
| 2848 | 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] | 2849 | Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not"); |
| Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2850 | return BinaryOperator::CreateAnd(X, NotY); |
| 2851 | } |
| 2852 | |
| Roman Lebedev | 1368679 | 2018-04-28 15:45:07 +0000 | [diff] [blame] | 2853 | if (Instruction *Xor = visitMaskedMerge(I, Builder)) |
| 2854 | return Xor; |
| 2855 | |
| Sanjay Patel | 3b863f8 | 2017-04-22 18:05:35 +0000 | [diff] [blame] | 2856 | // Is this a 'not' (~) fed by a binary operator? |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2857 | BinaryOperator *NotVal; |
| 2858 | if (match(&I, m_Not(m_BinOp(NotVal)))) { |
| 2859 | if (NotVal->getOpcode() == Instruction::And || |
| 2860 | NotVal->getOpcode() == Instruction::Or) { |
| Sanjay Patel | 6381db1 | 2017-05-02 15:31:40 +0000 | [diff] [blame] | 2861 | // Apply DeMorgan's Law when inverts are free: |
| 2862 | // ~(X & Y) --> (~X | ~Y) |
| 2863 | // ~(X | Y) --> (~X & ~Y) |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2864 | if (IsFreeToInvert(NotVal->getOperand(0), |
| 2865 | NotVal->getOperand(0)->hasOneUse()) && |
| 2866 | IsFreeToInvert(NotVal->getOperand(1), |
| 2867 | NotVal->getOperand(1)->hasOneUse())) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2868 | Value *NotX = Builder.CreateNot(NotVal->getOperand(0), "notlhs"); |
| 2869 | Value *NotY = Builder.CreateNot(NotVal->getOperand(1), "notrhs"); |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2870 | if (NotVal->getOpcode() == Instruction::And) |
| Sanjay Patel | 3b863f8 | 2017-04-22 18:05:35 +0000 | [diff] [blame] | 2871 | return BinaryOperator::CreateOr(NotX, NotY); |
| 2872 | return BinaryOperator::CreateAnd(NotX, NotY); |
| 2873 | } |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| Sanjay Patel | 78e4b4d | 2018-07-27 10:54:48 +0000 | [diff] [blame] | 2876 | // ~(X - Y) --> ~X + Y |
| Sanjay Patel | 17e709b | 2018-09-02 19:31:45 +0000 | [diff] [blame] | 2877 | if (match(NotVal, m_Sub(m_Value(X), m_Value(Y)))) |
| 2878 | if (isa<Constant>(X) || NotVal->hasOneUse()) |
| 2879 | return BinaryOperator::CreateAdd(Builder.CreateNot(X), Y); |
| Sanjay Patel | 78e4b4d | 2018-07-27 10:54:48 +0000 | [diff] [blame] | 2880 | |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2881 | // ~(~X >>s Y) --> (X >>s Y) |
| 2882 | if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y)))) |
| 2883 | return BinaryOperator::CreateAShr(X, Y); |
| 2884 | |
| 2885 | // If we are inverting a right-shifted constant, we may be able to eliminate |
| 2886 | // the 'not' by inverting the constant and using the opposite shift type. |
| 2887 | // Canonicalization rules ensure that only a negative constant uses 'ashr', |
| 2888 | // but we must check that in case that transform has not fired yet. |
| Sanjay Patel | 2fe1f62 | 2018-09-03 18:40:56 +0000 | [diff] [blame] | 2889 | |
| 2890 | // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits) |
| Simon Pilgrim | 1949519 | 2018-02-10 21:46:09 +0000 | [diff] [blame] | 2891 | Constant *C; |
| 2892 | if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) && |
| Sanjay Patel | 2fe1f62 | 2018-09-03 18:40:56 +0000 | [diff] [blame] | 2893 | match(C, m_Negative())) |
| 2894 | return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y); |
| Sanjay Patel | a1c8814 | 2017-05-08 20:49:59 +0000 | [diff] [blame] | 2895 | |
| Sanjay Patel | 2fe1f62 | 2018-09-03 18:40:56 +0000 | [diff] [blame] | 2896 | // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits) |
| Simon Pilgrim | 1949519 | 2018-02-10 21:46:09 +0000 | [diff] [blame] | 2897 | if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) && |
| Sanjay Patel | 2fe1f62 | 2018-09-03 18:40:56 +0000 | [diff] [blame] | 2898 | match(C, m_NonNegative())) |
| 2899 | return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y); |
| Sanjay Patel | d75064e | 2018-09-03 18:21:59 +0000 | [diff] [blame] | 2900 | |
| 2901 | // ~(X + C) --> -(C + 1) - X |
| 2902 | if (match(Op0, m_Add(m_Value(X), m_Constant(C)))) |
| 2903 | return BinaryOperator::CreateSub(ConstantExpr::getNeg(AddOne(C)), X); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2904 | } |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2905 | |
| Sanjay Patel | 93bd15a | 2018-09-06 16:23:40 +0000 | [diff] [blame] | 2906 | // Use DeMorgan and reassociation to eliminate a 'not' op. |
| 2907 | Constant *C1; |
| 2908 | if (match(Op1, m_Constant(C1))) { |
| 2909 | Constant *C2; |
| 2910 | if (match(Op0, m_OneUse(m_Or(m_Not(m_Value(X)), m_Constant(C2))))) { |
| 2911 | // (~X | C2) ^ C1 --> ((X & ~C2) ^ -1) ^ C1 --> (X & ~C2) ^ ~C1 |
| 2912 | Value *And = Builder.CreateAnd(X, ConstantExpr::getNot(C2)); |
| 2913 | return BinaryOperator::CreateXor(And, ConstantExpr::getNot(C1)); |
| 2914 | } |
| 2915 | if (match(Op0, m_OneUse(m_And(m_Not(m_Value(X)), m_Constant(C2))))) { |
| 2916 | // (~X & C2) ^ C1 --> ((X | ~C2) ^ -1) ^ C1 --> (X | ~C2) ^ ~C1 |
| 2917 | Value *Or = Builder.CreateOr(X, ConstantExpr::getNot(C2)); |
| 2918 | return BinaryOperator::CreateXor(Or, ConstantExpr::getNot(C1)); |
| 2919 | } |
| 2920 | } |
| 2921 | |
| Craig Topper | 798a19a | 2017-06-29 00:07:08 +0000 | [diff] [blame] | 2922 | // not (cmp A, B) = !cmp A, B |
| Craig Topper | cc418b6 | 2017-07-05 20:31:00 +0000 | [diff] [blame] | 2923 | CmpInst::Predicate Pred; |
| Craig Topper | 798a19a | 2017-06-29 00:07:08 +0000 | [diff] [blame] | 2924 | 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] | 2925 | cast<CmpInst>(Op0)->setPredicate(CmpInst::getInversePredicate(Pred)); |
| 2926 | return replaceInstUsesWith(I, Op0); |
| Benjamin Kramer | 443c796 | 2015-02-12 20:26:46 +0000 | [diff] [blame] | 2927 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2928 | |
| Craig Topper | b5bf016 | 2017-08-06 06:28:41 +0000 | [diff] [blame] | 2929 | { |
| 2930 | const APInt *RHSC; |
| 2931 | if (match(Op1, m_APInt(RHSC))) { |
| Craig Topper | 9a6110b | 2017-08-10 20:35:34 +0000 | [diff] [blame] | 2932 | Value *X; |
| Craig Topper | b5bf016 | 2017-08-06 06:28:41 +0000 | [diff] [blame] | 2933 | const APInt *C; |
| Sanjay Patel | 2fe1f62 | 2018-09-03 18:40:56 +0000 | [diff] [blame] | 2934 | if (RHSC->isSignMask() && match(Op0, m_Sub(m_APInt(C), m_Value(X)))) { |
| 2935 | // (C - X) ^ signmask -> (C + signmask - X) |
| 2936 | Constant *NewC = ConstantInt::get(I.getType(), *C + *RHSC); |
| 2937 | return BinaryOperator::CreateSub(NewC, X); |
| 2938 | } |
| 2939 | if (RHSC->isSignMask() && match(Op0, m_Add(m_Value(X), m_APInt(C)))) { |
| 2940 | // (X + C) ^ signmask -> (X + C + signmask) |
| 2941 | Constant *NewC = ConstantInt::get(I.getType(), *C + *RHSC); |
| 2942 | return BinaryOperator::CreateAdd(X, NewC); |
| Craig Topper | b5bf016 | 2017-08-06 06:28:41 +0000 | [diff] [blame] | 2943 | } |
| Craig Topper | 9a6110b | 2017-08-10 20:35:34 +0000 | [diff] [blame] | 2944 | |
| 2945 | // (X|C1)^C2 -> X^(C1^C2) iff X&~C1 == 0 |
| 2946 | if (match(Op0, m_Or(m_Value(X), m_APInt(C))) && |
| 2947 | MaskedValueIsZero(X, *C, 0, &I)) { |
| 2948 | Constant *NewC = ConstantInt::get(I.getType(), *C ^ *RHSC); |
| 2949 | Worklist.Add(cast<Instruction>(Op0)); |
| 2950 | I.setOperand(0, X); |
| 2951 | I.setOperand(1, NewC); |
| 2952 | return &I; |
| 2953 | } |
| Craig Topper | b5bf016 | 2017-08-06 06:28:41 +0000 | [diff] [blame] | 2954 | } |
| 2955 | } |
| 2956 | |
| Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2957 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2958 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2959 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| Craig Topper | 9a6110b | 2017-08-10 20:35:34 +0000 | [diff] [blame] | 2960 | if (Op0I->getOpcode() == Instruction::LShr) { |
| Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2961 | // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3) |
| 2962 | // E1 = "X ^ C1" |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2963 | BinaryOperator *E1; |
| Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2964 | ConstantInt *C1; |
| 2965 | if (Op0I->hasOneUse() && |
| 2966 | (E1 = dyn_cast<BinaryOperator>(Op0I->getOperand(0))) && |
| 2967 | E1->getOpcode() == Instruction::Xor && |
| 2968 | (C1 = dyn_cast<ConstantInt>(E1->getOperand(1)))) { |
| 2969 | // fold (C1 >> C2) ^ C3 |
| Craig Topper | 9d1821b | 2017-04-09 06:12:31 +0000 | [diff] [blame] | 2970 | ConstantInt *C2 = Op0CI, *C3 = RHSC; |
| Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2971 | APInt FoldConst = C1->getValue().lshr(C2->getValue()); |
| 2972 | FoldConst ^= C3->getValue(); |
| 2973 | // Prepare the two operands. |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2974 | Value *Opnd0 = Builder.CreateLShr(E1->getOperand(0), C2); |
| Shuxin Yang | 6ea79e8 | 2012-11-26 21:44:25 +0000 | [diff] [blame] | 2975 | Opnd0->takeName(Op0I); |
| 2976 | cast<Instruction>(Opnd0)->setDebugLoc(I.getDebugLoc()); |
| 2977 | Value *FoldVal = ConstantInt::get(Opnd0->getType(), FoldConst); |
| 2978 | |
| 2979 | return BinaryOperator::CreateXor(Opnd0, FoldVal); |
| 2980 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | } |
| Craig Topper | 8617360 | 2017-04-04 20:26:25 +0000 | [diff] [blame] | 2984 | } |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2985 | |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 2986 | if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I)) |
| 2987 | return FoldedLogic; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 2988 | |
| Sanjay Patel | a89f183 | 2018-09-04 21:00:13 +0000 | [diff] [blame] | 2989 | // Y ^ (X | Y) --> X & ~Y |
| 2990 | // Y ^ (Y | X) --> X & ~Y |
| 2991 | if (match(Op1, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op0))))) |
| 2992 | return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0)); |
| 2993 | // (X | Y) ^ Y --> X & ~Y |
| 2994 | // (Y | X) ^ Y --> X & ~Y |
| 2995 | if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1))))) |
| 2996 | return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1)); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 2997 | |
| Sanjay Patel | a89f183 | 2018-09-04 21:00:13 +0000 | [diff] [blame] | 2998 | // Y ^ (X & Y) --> ~X & Y |
| 2999 | // Y ^ (Y & X) --> ~X & Y |
| 3000 | if (match(Op1, m_OneUse(m_c_And(m_Value(X), m_Specific(Op0))))) |
| 3001 | return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X)); |
| 3002 | // (X & Y) ^ Y --> ~X & Y |
| 3003 | // (Y & X) ^ Y --> ~X & Y |
| Sanjay Patel | 0f70f86 | 2018-09-04 21:17:14 +0000 | [diff] [blame] | 3004 | // Canonical form is (X & C) ^ C; don't touch that. |
| Sanjay Patel | a89f183 | 2018-09-04 21:00:13 +0000 | [diff] [blame] | 3005 | // TODO: A 'not' op is better for analysis and codegen, but demanded bits must |
| 3006 | // be fixed to prefer that (otherwise we get infinite looping). |
| Sanjay Patel | 0f70f86 | 2018-09-04 21:17:14 +0000 | [diff] [blame] | 3007 | if (!match(Op1, m_Constant()) && |
| Sanjay Patel | a89f183 | 2018-09-04 21:00:13 +0000 | [diff] [blame] | 3008 | match(Op0, m_OneUse(m_c_And(m_Value(X), m_Specific(Op1))))) |
| 3009 | return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X)); |
| Craig Topper | 9d4171a | 2012-12-20 07:09:41 +0000 | [diff] [blame] | 3010 | |
| Sanjay Patel | 63cf26c | 2018-09-04 23:22:13 +0000 | [diff] [blame] | 3011 | Value *A, *B, *C; |
| 3012 | // (A ^ B) ^ (A | C) --> (~A & C) ^ B -- There are 4 commuted variants. |
| 3013 | if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))), |
| 3014 | m_OneUse(m_c_Or(m_Deferred(A), m_Value(C)))))) |
| 3015 | return BinaryOperator::CreateXor( |
| 3016 | Builder.CreateAnd(Builder.CreateNot(A), C), B); |
| 3017 | |
| 3018 | // (A ^ B) ^ (B | C) --> (~B & C) ^ A -- There are 4 commuted variants. |
| 3019 | if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))), |
| 3020 | m_OneUse(m_c_Or(m_Deferred(B), m_Value(C)))))) |
| 3021 | return BinaryOperator::CreateXor( |
| 3022 | Builder.CreateAnd(Builder.CreateNot(B), C), A); |
| 3023 | |
| 3024 | // (A & B) ^ (A ^ B) -> (A | B) |
| 3025 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 3026 | match(Op1, m_c_Xor(m_Specific(A), m_Specific(B)))) |
| 3027 | return BinaryOperator::CreateOr(A, B); |
| 3028 | // (A ^ B) ^ (A & B) -> (A | B) |
| 3029 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 3030 | match(Op1, m_c_And(m_Specific(A), m_Specific(B)))) |
| 3031 | return BinaryOperator::CreateOr(A, B); |
| Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 3032 | |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 3033 | // (A & ~B) ^ ~A -> ~(A & B) |
| 3034 | // (~B & A) ^ ~A -> ~(A & B) |
| Sanjay Patel | 2b9d4b4 | 2016-12-18 18:49:48 +0000 | [diff] [blame] | 3035 | 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] | 3036 | match(Op1, m_Not(m_Specific(A)))) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3037 | return BinaryOperator::CreateNot(Builder.CreateAnd(A, B)); |
| Suyog Sarda | 56c9a87 | 2014-08-01 05:07:20 +0000 | [diff] [blame] | 3038 | |
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 3039 | if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) |
| 3040 | if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| 3041 | if (Value *V = foldXorOfICmps(LHS, RHS)) |
| 3042 | return replaceInstUsesWith(I, V); |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 3043 | |
| Sanjay Patel | dbbaca0 | 2016-02-24 17:00:34 +0000 | [diff] [blame] | 3044 | if (Instruction *CastedXor = foldCastedBitwiseLogic(I)) |
| 3045 | return CastedXor; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 3046 | |
| Sanjay Patel | 3cd1aa8 | 2018-06-06 21:58:12 +0000 | [diff] [blame] | 3047 | // Canonicalize a shifty way to code absolute value to the common pattern. |
| Sanjay Patel | 5a0cdac | 2017-12-16 16:41:17 +0000 | [diff] [blame] | 3048 | // There are 4 potential commuted variants. Move the 'ashr' candidate to Op1. |
| 3049 | // We're relying on the fact that we only do this transform when the shift has |
| 3050 | // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase |
| 3051 | // instructions). |
| Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 3052 | if (Op0->hasNUses(2)) |
| Sanjay Patel | 5a0cdac | 2017-12-16 16:41:17 +0000 | [diff] [blame] | 3053 | std::swap(Op0, Op1); |
| 3054 | |
| 3055 | const APInt *ShAmt; |
| 3056 | Type *Ty = I.getType(); |
| 3057 | if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) && |
| Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 3058 | Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 && |
| Sanjay Patel | 5a0cdac | 2017-12-16 16:41:17 +0000 | [diff] [blame] | 3059 | match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) { |
| 3060 | // B = ashr i32 A, 31 ; smear the sign bit |
| 3061 | // xor (add A, B), B ; add -1 and flip bits if negative |
| 3062 | // --> (A < 0) ? -A : A |
| 3063 | Value *Cmp = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(Ty)); |
| Craig Topper | bd33258 | 2018-05-17 16:29:52 +0000 | [diff] [blame] | 3064 | // Copy the nuw/nsw flags from the add to the negate. |
| 3065 | auto *Add = cast<BinaryOperator>(Op0); |
| 3066 | Value *Neg = Builder.CreateNeg(A, "", Add->hasNoUnsignedWrap(), |
| 3067 | Add->hasNoSignedWrap()); |
| 3068 | return SelectInst::Create(Cmp, Neg, A); |
| Sanjay Patel | 5a0cdac | 2017-12-16 16:41:17 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
| Artur Gainullin | d928201 | 2018-04-11 10:29:37 +0000 | [diff] [blame] | 3071 | // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max: |
| 3072 | // |
| 3073 | // %notx = xor i32 %x, -1 |
| 3074 | // %cmp1 = icmp sgt i32 %notx, %y |
| 3075 | // %smax = select i1 %cmp1, i32 %notx, i32 %y |
| 3076 | // %res = xor i32 %smax, -1 |
| 3077 | // => |
| 3078 | // %noty = xor i32 %y, -1 |
| 3079 | // %cmp2 = icmp slt %x, %noty |
| 3080 | // %res = select i1 %cmp2, i32 %x, i32 %noty |
| 3081 | // |
| 3082 | // Same is applicable for smin/umax/umin. |
| Craig Topper | 3d8fe39 | 2018-08-21 19:17:00 +0000 | [diff] [blame] | 3083 | if (match(Op1, m_AllOnes()) && Op0->hasOneUse()) { |
| Artur Gainullin | d928201 | 2018-04-11 10:29:37 +0000 | [diff] [blame] | 3084 | Value *LHS, *RHS; |
| 3085 | SelectPatternFlavor SPF = matchSelectPattern(Op0, LHS, RHS).Flavor; |
| Craig Topper | 3d8fe39 | 2018-08-21 19:17:00 +0000 | [diff] [blame] | 3086 | if (SelectPatternResult::isMinOrMax(SPF)) { |
| Craig Topper | 040c2b0 | 2018-09-07 16:19:50 +0000 | [diff] [blame] | 3087 | // It's possible we get here before the not has been simplified, so make |
| 3088 | // sure the input to the not isn't freely invertible. |
| 3089 | if (match(LHS, m_Not(m_Value(X))) && !IsFreeToInvert(X, X->hasOneUse())) { |
| Artur Gainullin | d928201 | 2018-04-11 10:29:37 +0000 | [diff] [blame] | 3090 | Value *NotY = Builder.CreateNot(RHS); |
| 3091 | return SelectInst::Create( |
| 3092 | Builder.CreateICmp(getInverseMinMaxPred(SPF), X, NotY), X, NotY); |
| 3093 | } |
| Craig Topper | 040c2b0 | 2018-09-07 16:19:50 +0000 | [diff] [blame] | 3094 | |
| 3095 | // It's possible we get here before the not has been simplified, so make |
| 3096 | // sure the input to the not isn't freely invertible. |
| 3097 | if (match(RHS, m_Not(m_Value(Y))) && !IsFreeToInvert(Y, Y->hasOneUse())) { |
| 3098 | Value *NotX = Builder.CreateNot(LHS); |
| 3099 | return SelectInst::Create( |
| 3100 | Builder.CreateICmp(getInverseMinMaxPred(SPF), NotX, Y), NotX, Y); |
| 3101 | } |
| Craig Topper | 8fc05ce | 2018-09-13 18:52:58 +0000 | [diff] [blame] | 3102 | |
| 3103 | // If both sides are freely invertible, then we can get rid of the xor |
| 3104 | // completely. |
| 3105 | if (IsFreeToInvert(LHS, !LHS->hasNUsesOrMore(3)) && |
| 3106 | IsFreeToInvert(RHS, !RHS->hasNUsesOrMore(3))) { |
| 3107 | Value *NotLHS = Builder.CreateNot(LHS); |
| 3108 | Value *NotRHS = Builder.CreateNot(RHS); |
| 3109 | return SelectInst::Create( |
| 3110 | Builder.CreateICmp(getInverseMinMaxPred(SPF), NotLHS, NotRHS), |
| 3111 | NotLHS, NotRHS); |
| 3112 | } |
| Artur Gainullin | d928201 | 2018-04-11 10:29:37 +0000 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
| Roman Lebedev | a677651 | 2018-08-08 13:31:19 +0000 | [diff] [blame] | 3116 | if (Instruction *NewXor = sinkNotIntoXor(I, Builder)) |
| 3117 | return NewXor; |
| 3118 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 3119 | return nullptr; |
| Chris Lattner | 0a8191e | 2010-01-05 07:50:36 +0000 | [diff] [blame] | 3120 | } |