Chris Lattner | 1e7b7b5 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 1 | //===- InstCombineSelect.cpp ----------------------------------------------===// |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 1e7b7b5 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 10 | // This file implements the visitSelect function. |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/Optional.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/Analysis/AssumptionCache.h" |
Craig Topper | 882f296 | 2017-08-16 21:52:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CmpInstAnalysis.h" |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionSimplify.h" |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 23 | #include "llvm/IR/BasicBlock.h" |
| 24 | #include "llvm/IR/Constant.h" |
| 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/IRBuilder.h" |
| 28 | #include "llvm/IR/InstrTypes.h" |
| 29 | #include "llvm/IR/Instruction.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 34 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Type.h" |
| 36 | #include "llvm/IR/User.h" |
| 37 | #include "llvm/IR/Value.h" |
| 38 | #include "llvm/Support/Casting.h" |
| 39 | #include "llvm/Support/ErrorHandling.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 40 | #include "llvm/Support/KnownBits.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" |
| 42 | #include <cassert> |
| 43 | #include <utility> |
| 44 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | using namespace PatternMatch; |
| 47 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 48 | #define DEBUG_TYPE "instcombine" |
| 49 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 50 | static Value *createMinMax(InstCombiner::BuilderTy &Builder, |
| 51 | SelectPatternFlavor SPF, Value *A, Value *B) { |
| 52 | CmpInst::Predicate Pred = getMinMaxPred(SPF); |
| 53 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer predicate"); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 54 | return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 57 | /// This folds: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 58 | /// select (icmp eq (and X, C1)), TC, FC |
| 59 | /// iff C1 is a power 2 and the difference between TC and FC is a power-of-2. |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 60 | /// To something like: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 61 | /// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 62 | /// Or: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 63 | /// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC |
| 64 | /// With some variations depending if FC is larger than TC, or the shift |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 65 | /// isn't needed, or the bit widths don't match. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 66 | static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp, |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 67 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 68 | const APInt *SelTC, *SelFC; |
| 69 | if (!match(Sel.getTrueValue(), m_APInt(SelTC)) || |
| 70 | !match(Sel.getFalseValue(), m_APInt(SelFC))) |
| 71 | return nullptr; |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 72 | |
| 73 | // If this is a vector select, we need a vector compare. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 74 | Type *SelType = Sel.getType(); |
| 75 | if (SelType->isVectorTy() != Cmp->getType()->isVectorTy()) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 76 | return nullptr; |
| 77 | |
| 78 | Value *V; |
| 79 | APInt AndMask; |
| 80 | bool CreateAnd = false; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 81 | ICmpInst::Predicate Pred = Cmp->getPredicate(); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 82 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 83 | if (!match(Cmp->getOperand(1), m_Zero())) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 84 | return nullptr; |
| 85 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 86 | V = Cmp->getOperand(0); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 87 | const APInt *AndRHS; |
| 88 | if (!match(V, m_And(m_Value(), m_Power2(AndRHS)))) |
| 89 | return nullptr; |
| 90 | |
| 91 | AndMask = *AndRHS; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 92 | } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1), |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 93 | Pred, V, AndMask)) { |
| 94 | assert(ICmpInst::isEquality(Pred) && "Not equality test?"); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 95 | if (!AndMask.isPowerOf2()) |
| 96 | return nullptr; |
| 97 | |
| 98 | CreateAnd = true; |
| 99 | } else { |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 103 | // In general, when both constants are non-zero, we would need an offset to |
| 104 | // replace the select. This would require more instructions than we started |
| 105 | // with. But there's one special-case that we handle here because it can |
| 106 | // simplify/reduce the instructions. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 107 | APInt TC = *SelTC; |
| 108 | APInt FC = *SelFC; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 109 | if (!TC.isNullValue() && !FC.isNullValue()) { |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 110 | // If the select constants differ by exactly one bit and that's the same |
| 111 | // bit that is masked and checked by the select condition, the select can |
| 112 | // be replaced by bitwise logic to set/clear one bit of the constant result. |
| 113 | if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 114 | return nullptr; |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 115 | if (CreateAnd) { |
| 116 | // If we have to create an 'and', then we must kill the cmp to not |
| 117 | // increase the instruction count. |
| 118 | if (!Cmp->hasOneUse()) |
| 119 | return nullptr; |
| 120 | V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask)); |
| 121 | } |
| 122 | bool ExtraBitInTC = TC.ugt(FC); |
| 123 | if (Pred == ICmpInst::ICMP_EQ) { |
| 124 | // If the masked bit in V is clear, clear or set the bit in the result: |
| 125 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC |
| 126 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC |
| 127 | Constant *C = ConstantInt::get(SelType, TC); |
| 128 | return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C); |
| 129 | } |
| 130 | if (Pred == ICmpInst::ICMP_NE) { |
| 131 | // If the masked bit in V is set, set or clear the bit in the result: |
| 132 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC |
| 133 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC |
| 134 | Constant *C = ConstantInt::get(SelType, FC); |
| 135 | return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C); |
| 136 | } |
| 137 | llvm_unreachable("Only expecting equality predicates"); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 140 | // Make sure one of the select arms is a power-of-2. |
| 141 | if (!TC.isPowerOf2() && !FC.isPowerOf2()) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 142 | return nullptr; |
| 143 | |
| 144 | // Determine which shift is needed to transform result of the 'and' into the |
| 145 | // desired result. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 146 | const APInt &ValC = !TC.isNullValue() ? TC : FC; |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 147 | unsigned ValZeros = ValC.logBase2(); |
| 148 | unsigned AndZeros = AndMask.logBase2(); |
| 149 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 150 | // Insert the 'and' instruction on the input to the truncate. |
| 151 | if (CreateAnd) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 152 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask)); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 153 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 154 | // If types don't match, we can still convert the select by introducing a zext |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 155 | // or a trunc of the 'and'. |
| 156 | if (ValZeros > AndZeros) { |
| 157 | V = Builder.CreateZExtOrTrunc(V, SelType); |
| 158 | V = Builder.CreateShl(V, ValZeros - AndZeros); |
| 159 | } else if (ValZeros < AndZeros) { |
| 160 | V = Builder.CreateLShr(V, AndZeros - ValZeros); |
| 161 | V = Builder.CreateZExtOrTrunc(V, SelType); |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 162 | } else { |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 163 | V = Builder.CreateZExtOrTrunc(V, SelType); |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 164 | } |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 165 | |
| 166 | // Okay, now we know that everything is set up, we just don't know whether we |
| 167 | // have a icmp_ne or icmp_eq and whether the true or false val is the zero. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 168 | bool ShouldNotVal = !TC.isNullValue(); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 169 | ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; |
| 170 | if (ShouldNotVal) |
| 171 | V = Builder.CreateXor(V, ValC); |
| 172 | |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 173 | return V; |
| 174 | } |
| 175 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 176 | /// We want to turn code that looks like this: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 177 | /// %C = or %A, %B |
| 178 | /// %D = select %cond, %C, %A |
| 179 | /// into: |
| 180 | /// %C = select %cond, %B, 0 |
| 181 | /// %D = or %A, %C |
| 182 | /// |
| 183 | /// Assuming that the specified instruction is an operand to the select, return |
| 184 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 185 | /// equal the other incoming value of the select. |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 186 | static unsigned getSelectFoldableOperands(BinaryOperator *I) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 187 | switch (I->getOpcode()) { |
| 188 | case Instruction::Add: |
| 189 | case Instruction::Mul: |
| 190 | case Instruction::And: |
| 191 | case Instruction::Or: |
| 192 | case Instruction::Xor: |
| 193 | return 3; // Can fold through either operand. |
| 194 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 195 | case Instruction::Shl: // Can only fold on the shift amount. |
| 196 | case Instruction::LShr: |
| 197 | case Instruction::AShr: |
| 198 | return 1; |
| 199 | default: |
| 200 | return 0; // Cannot fold |
| 201 | } |
| 202 | } |
| 203 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 204 | /// For the same transformation as the previous function, return the identity |
| 205 | /// constant that goes into the select. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 206 | static APInt getSelectFoldableConstant(BinaryOperator *I) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 207 | switch (I->getOpcode()) { |
| 208 | default: llvm_unreachable("This cannot happen!"); |
| 209 | case Instruction::Add: |
| 210 | case Instruction::Sub: |
| 211 | case Instruction::Or: |
| 212 | case Instruction::Xor: |
| 213 | case Instruction::Shl: |
| 214 | case Instruction::LShr: |
| 215 | case Instruction::AShr: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 216 | return APInt::getNullValue(I->getType()->getScalarSizeInBits()); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 217 | case Instruction::And: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 218 | return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits()); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 219 | case Instruction::Mul: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 220 | return APInt(I->getType()->getScalarSizeInBits(), 1); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 224 | /// We have (select c, TI, FI), and we know that TI and FI have the same opcode. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 225 | Instruction *InstCombiner::foldSelectOpOp(SelectInst &SI, Instruction *TI, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 226 | Instruction *FI) { |
Sanjay Patel | 6105bb5 | 2017-03-16 20:42:45 +0000 | [diff] [blame] | 227 | // Don't break up min/max patterns. The hasOneUse checks below prevent that |
| 228 | // for most cases, but vector min/max with bitcasts can be transformed. If the |
| 229 | // one-use restrictions are eased for other patterns, we still don't want to |
| 230 | // obfuscate min/max. |
| 231 | if ((match(&SI, m_SMin(m_Value(), m_Value())) || |
| 232 | match(&SI, m_SMax(m_Value(), m_Value())) || |
| 233 | match(&SI, m_UMin(m_Value(), m_Value())) || |
| 234 | match(&SI, m_UMax(m_Value(), m_Value())))) |
| 235 | return nullptr; |
| 236 | |
Sanjay Patel | 384d0f2 | 2016-06-08 20:31:52 +0000 | [diff] [blame] | 237 | // If this is a cast from the same type, merge. |
| 238 | if (TI->getNumOperands() == 1 && TI->isCast()) { |
| 239 | Type *FIOpndTy = FI->getOperand(0)->getType(); |
| 240 | if (TI->getOperand(0)->getType() != FIOpndTy) |
| 241 | return nullptr; |
| 242 | |
| 243 | // The select condition may be a vector. We may only change the operand |
| 244 | // type if the vector width remains the same (and matches the condition). |
| 245 | Type *CondTy = SI.getCondition()->getType(); |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 246 | if (CondTy->isVectorTy()) { |
| 247 | if (!FIOpndTy->isVectorTy()) |
| 248 | return nullptr; |
| 249 | if (CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()) |
| 250 | return nullptr; |
| 251 | |
| 252 | // TODO: If the backend knew how to deal with casts better, we could |
| 253 | // remove this limitation. For now, there's too much potential to create |
| 254 | // worse codegen by promoting the select ahead of size-altering casts |
| 255 | // (PR28160). |
| 256 | // |
| 257 | // Note that ValueTracking's matchSelectPattern() looks through casts |
| 258 | // without checking 'hasOneUse' when it matches min/max patterns, so this |
| 259 | // transform may end up happening anyway. |
| 260 | if (TI->getOpcode() != Instruction::BitCast && |
| 261 | (!TI->hasOneUse() || !FI->hasOneUse())) |
| 262 | return nullptr; |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 263 | } else if (!TI->hasOneUse() || !FI->hasOneUse()) { |
| 264 | // TODO: The one-use restrictions for a scalar select could be eased if |
| 265 | // the fold of a select in visitLoadInst() was enhanced to match a pattern |
| 266 | // that includes a cast. |
Sanjay Patel | 384d0f2 | 2016-06-08 20:31:52 +0000 | [diff] [blame] | 267 | return nullptr; |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 269 | |
| 270 | // Fold this by inserting a select from the input values. |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 271 | Value *NewSI = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 272 | Builder.CreateSelect(SI.getCondition(), TI->getOperand(0), |
| 273 | FI->getOperand(0), SI.getName() + ".v", &SI); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 274 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 275 | TI->getType()); |
| 276 | } |
| 277 | |
John Brawn | 2867bd7 | 2018-01-19 10:05:15 +0000 | [diff] [blame] | 278 | // Only handle binary operators (including two-operand getelementptr) with |
| 279 | // one-use here. As with the cast case above, it may be possible to relax the |
| 280 | // one-use constraint, but that needs be examined carefully since it may not |
| 281 | // reduce the total number of instructions. |
| 282 | if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 || |
| 283 | (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) || |
| 284 | !TI->hasOneUse() || !FI->hasOneUse()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 285 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 286 | |
| 287 | // Figure out if the operations have any operands in common. |
| 288 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 289 | bool MatchIsOpZero; |
| 290 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 291 | MatchOp = TI->getOperand(0); |
| 292 | OtherOpT = TI->getOperand(1); |
| 293 | OtherOpF = FI->getOperand(1); |
| 294 | MatchIsOpZero = true; |
| 295 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 296 | MatchOp = TI->getOperand(1); |
| 297 | OtherOpT = TI->getOperand(0); |
| 298 | OtherOpF = FI->getOperand(0); |
| 299 | MatchIsOpZero = false; |
| 300 | } else if (!TI->isCommutative()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 301 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 302 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 303 | MatchOp = TI->getOperand(0); |
| 304 | OtherOpT = TI->getOperand(1); |
| 305 | OtherOpF = FI->getOperand(0); |
| 306 | MatchIsOpZero = true; |
| 307 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 308 | MatchOp = TI->getOperand(1); |
| 309 | OtherOpT = TI->getOperand(0); |
| 310 | OtherOpF = FI->getOperand(1); |
| 311 | MatchIsOpZero = true; |
| 312 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 313 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // If we reach here, they do have operations in common. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 317 | Value *NewSI = Builder.CreateSelect(SI.getCondition(), OtherOpT, OtherOpF, |
| 318 | SI.getName() + ".v", &SI); |
Sanjay Patel | cb2199b | 2016-11-11 23:01:20 +0000 | [diff] [blame] | 319 | Value *Op0 = MatchIsOpZero ? MatchOp : NewSI; |
| 320 | Value *Op1 = MatchIsOpZero ? NewSI : MatchOp; |
John Brawn | 2867bd7 | 2018-01-19 10:05:15 +0000 | [diff] [blame] | 321 | if (auto *BO = dyn_cast<BinaryOperator>(TI)) { |
| 322 | return BinaryOperator::Create(BO->getOpcode(), Op0, Op1); |
| 323 | } |
| 324 | if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) { |
| 325 | auto *FGEP = cast<GetElementPtrInst>(FI); |
| 326 | Type *ElementType = TGEP->getResultElementType(); |
| 327 | return TGEP->isInBounds() && FGEP->isInBounds() |
| 328 | ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1}) |
| 329 | : GetElementPtrInst::Create(ElementType, Op0, {Op1}); |
| 330 | } |
| 331 | llvm_unreachable("Expected BinaryOperator or GEP"); |
| 332 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 335 | static bool isSelect01(const APInt &C1I, const APInt &C2I) { |
| 336 | if (!C1I.isNullValue() && !C2I.isNullValue()) // One side must be zero. |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 337 | return false; |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 338 | return C1I.isOneValue() || C1I.isAllOnesValue() || |
| 339 | C2I.isOneValue() || C2I.isAllOnesValue(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 342 | /// Try to fold the select into one of the operands to allow further |
| 343 | /// optimization. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 344 | Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 345 | Value *FalseVal) { |
| 346 | // See the comment above GetSelectFoldableOperands for a description of the |
| 347 | // transformation we are doing here. |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 348 | if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) { |
| 349 | if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 350 | if (unsigned SFO = getSelectFoldableOperands(TVI)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 351 | unsigned OpToFold = 0; |
| 352 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 353 | OpToFold = 1; |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 354 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 355 | OpToFold = 2; |
| 356 | } |
| 357 | |
| 358 | if (OpToFold) { |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 359 | APInt CI = getSelectFoldableConstant(TVI); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 360 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 361 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 8ef5001 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 362 | // between 0, 1 and -1. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 363 | const APInt *OOpC; |
| 364 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); |
| 365 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { |
| 366 | Value *C = ConstantInt::get(OOp->getType(), CI); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 367 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 368 | NewSel->takeName(TVI); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 369 | BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(), |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 370 | FalseVal, NewSel); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 371 | BO->copyIRFlags(TVI); |
Nick Lewycky | ebc2f3a | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 372 | return BO; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 379 | if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) { |
| 380 | if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 381 | if (unsigned SFO = getSelectFoldableOperands(FVI)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 382 | unsigned OpToFold = 0; |
| 383 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 384 | OpToFold = 1; |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 385 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 386 | OpToFold = 2; |
| 387 | } |
| 388 | |
| 389 | if (OpToFold) { |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 390 | APInt CI = getSelectFoldableConstant(FVI); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 391 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 392 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 8ef5001 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 393 | // between 0, 1 and -1. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 394 | const APInt *OOpC; |
| 395 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); |
| 396 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { |
| 397 | Value *C = ConstantInt::get(OOp->getType(), CI); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 398 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 399 | NewSel->takeName(FVI); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 400 | BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(), |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 401 | TrueVal, NewSel); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 402 | BO->copyIRFlags(FVI); |
Nick Lewycky | ebc2f3a | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 403 | return BO; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 410 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 413 | /// We want to turn: |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 414 | /// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1) |
| 415 | /// into: |
| 416 | /// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0) |
| 417 | /// Note: |
| 418 | /// Z may be 0 if lshr is missing. |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 419 | /// Worst-case scenario is that we will replace 5 instructions with 5 different |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 420 | /// instructions, but we got rid of select. |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 421 | static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp, |
| 422 | Value *TVal, Value *FVal, |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 423 | InstCombiner::BuilderTy &Builder) { |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 424 | if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() && |
| 425 | Cmp->getPredicate() == ICmpInst::ICMP_EQ && |
| 426 | match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One()))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 427 | return nullptr; |
| 428 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 429 | // The TrueVal has general form of: and %B, 1 |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 430 | Value *B; |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 431 | if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One())))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 432 | return nullptr; |
| 433 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 434 | // Where %B may be optionally shifted: lshr %X, %Z. |
| 435 | Value *X, *Z; |
| 436 | const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z)))); |
| 437 | if (!HasShift) |
| 438 | X = B; |
| 439 | |
| 440 | Value *Y; |
| 441 | if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y)))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 442 | return nullptr; |
| 443 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 444 | // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0 |
| 445 | // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0 |
| 446 | Constant *One = ConstantInt::get(SelType, 1); |
| 447 | Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One; |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 448 | Value *FullMask = Builder.CreateOr(Y, MaskB); |
| 449 | Value *MaskedX = Builder.CreateAnd(X, FullMask); |
| 450 | Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX); |
| 451 | return new ZExtInst(ICmpNeZero, SelType); |
| 452 | } |
| 453 | |
| 454 | /// We want to turn: |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 455 | /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) |
| 456 | /// into: |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 457 | /// (or (shl (and X, C1), C3), Y) |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 458 | /// iff: |
| 459 | /// C1 and C2 are both powers of 2 |
| 460 | /// where: |
| 461 | /// C3 = Log(C2) - Log(C1) |
| 462 | /// |
| 463 | /// This transform handles cases where: |
| 464 | /// 1. The icmp predicate is inverted |
| 465 | /// 2. The select operands are reversed |
| 466 | /// 3. The magnitude of C2 and C1 are flipped |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 467 | static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal, |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 468 | Value *FalseVal, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 469 | InstCombiner::BuilderTy &Builder) { |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 470 | // Only handle integer compares. Also, if this is a vector select, we need a |
| 471 | // vector compare. |
| 472 | if (!TrueVal->getType()->isIntOrIntVectorTy() || |
| 473 | TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 474 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 475 | |
| 476 | Value *CmpLHS = IC->getOperand(0); |
| 477 | Value *CmpRHS = IC->getOperand(1); |
| 478 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 479 | Value *V; |
| 480 | unsigned C1Log; |
| 481 | bool IsEqualZero; |
| 482 | bool NeedAnd = false; |
| 483 | if (IC->isEquality()) { |
| 484 | if (!match(CmpRHS, m_Zero())) |
| 485 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 486 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 487 | const APInt *C1; |
| 488 | if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1)))) |
| 489 | return nullptr; |
| 490 | |
| 491 | V = CmpLHS; |
| 492 | C1Log = C1->logBase2(); |
| 493 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ; |
| 494 | } else if (IC->getPredicate() == ICmpInst::ICMP_SLT || |
| 495 | IC->getPredicate() == ICmpInst::ICMP_SGT) { |
| 496 | // We also need to recognize (icmp slt (trunc (X)), 0) and |
| 497 | // (icmp sgt (trunc (X)), -1). |
| 498 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT; |
| 499 | if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) || |
| 500 | (!IsEqualZero && !match(CmpRHS, m_Zero()))) |
| 501 | return nullptr; |
| 502 | |
| 503 | if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V))))) |
| 504 | return nullptr; |
| 505 | |
| 506 | C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1; |
| 507 | NeedAnd = true; |
| 508 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 509 | return nullptr; |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 510 | } |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 511 | |
| 512 | const APInt *C2; |
Dinesh Dwivedi | 83c11da | 2014-05-15 08:22:55 +0000 | [diff] [blame] | 513 | bool OrOnTrueVal = false; |
| 514 | bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2))); |
| 515 | if (!OrOnFalseVal) |
| 516 | OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2))); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 517 | |
| 518 | if (!OrOnFalseVal && !OrOnTrueVal) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 519 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 520 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 521 | Value *Y = OrOnFalseVal ? TrueVal : FalseVal; |
| 522 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 523 | unsigned C2Log = C2->logBase2(); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 524 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 525 | bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 526 | bool NeedShift = C1Log != C2Log; |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 527 | bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() != |
| 528 | V->getType()->getScalarSizeInBits(); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 529 | |
| 530 | // Make sure we don't create more instructions than we save. |
| 531 | Value *Or = OrOnFalseVal ? FalseVal : TrueVal; |
| 532 | if ((NeedShift + NeedXor + NeedZExtTrunc) > |
| 533 | (IC->hasOneUse() + Or->hasOneUse())) |
| 534 | return nullptr; |
| 535 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 536 | if (NeedAnd) { |
| 537 | // Insert the AND instruction on the input to the truncate. |
| 538 | APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 539 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1)); |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 540 | } |
| 541 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 542 | if (C2Log > C1Log) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 543 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
| 544 | V = Builder.CreateShl(V, C2Log - C1Log); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 545 | } else if (C1Log > C2Log) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 546 | V = Builder.CreateLShr(V, C1Log - C2Log); |
| 547 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
David Majnemer | d73f37b | 2013-04-30 10:36:33 +0000 | [diff] [blame] | 548 | } else |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 549 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 550 | |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 551 | if (NeedXor) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 552 | V = Builder.CreateXor(V, *C2); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 553 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 554 | return Builder.CreateOr(V, Y); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Sanjay Patel | e9a153f | 2018-02-05 17:53:29 +0000 | [diff] [blame] | 557 | /// Transform patterns such as: (a > b) ? a - b : 0 |
| 558 | /// into: ((a > b) ? a : b) - b) |
| 559 | /// This produces a canonical max pattern that is more easily recognized by the |
| 560 | /// backend and converted into saturated subtraction instructions if those |
| 561 | /// exist. |
| 562 | /// There are 8 commuted/swapped variants of this pattern. |
| 563 | /// TODO: Also support a - UMIN(a,b) patterns. |
| 564 | static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI, |
| 565 | const Value *TrueVal, |
| 566 | const Value *FalseVal, |
| 567 | InstCombiner::BuilderTy &Builder) { |
| 568 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 569 | if (!ICmpInst::isUnsigned(Pred)) |
| 570 | return nullptr; |
| 571 | |
| 572 | // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0 |
| 573 | if (match(TrueVal, m_Zero())) { |
| 574 | Pred = ICmpInst::getInversePredicate(Pred); |
| 575 | std::swap(TrueVal, FalseVal); |
| 576 | } |
| 577 | if (!match(FalseVal, m_Zero())) |
| 578 | return nullptr; |
| 579 | |
| 580 | Value *A = ICI->getOperand(0); |
| 581 | Value *B = ICI->getOperand(1); |
| 582 | if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) { |
| 583 | // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0 |
| 584 | std::swap(A, B); |
| 585 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 586 | } |
| 587 | |
| 588 | assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && |
| 589 | "Unexpected isUnsigned predicate!"); |
| 590 | |
| 591 | // Account for swapped form of subtraction: ((a > b) ? b - a : 0). |
| 592 | bool IsNegative = false; |
| 593 | if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A)))) |
| 594 | IsNegative = true; |
| 595 | else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B)))) |
| 596 | return nullptr; |
| 597 | |
| 598 | // If sub is used anywhere else, we wouldn't be able to eliminate it |
| 599 | // afterwards. |
| 600 | if (!TrueVal->hasOneUse()) |
| 601 | return nullptr; |
| 602 | |
| 603 | // All checks passed, convert to canonical unsigned saturated subtraction |
| 604 | // form: sub(max()). |
| 605 | // (a > b) ? a - b : 0 -> ((a > b) ? a : b) - b) |
| 606 | Value *Max = Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); |
| 607 | return IsNegative ? Builder.CreateSub(B, Max) : Builder.CreateSub(Max, B); |
| 608 | } |
| 609 | |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 610 | /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single |
| 611 | /// call to cttz/ctlz with flag 'is_zero_undef' cleared. |
| 612 | /// |
| 613 | /// For example, we can fold the following code sequence: |
| 614 | /// \code |
| 615 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true) |
| 616 | /// %1 = icmp ne i32 %x, 0 |
| 617 | /// %2 = select i1 %1, i32 %0, i32 32 |
| 618 | /// \code |
Junmo Park | 820964e | 2016-03-23 01:38:35 +0000 | [diff] [blame] | 619 | /// |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 620 | /// into: |
| 621 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) |
| 622 | static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 623 | InstCombiner::BuilderTy &Builder) { |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 624 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 625 | Value *CmpLHS = ICI->getOperand(0); |
| 626 | Value *CmpRHS = ICI->getOperand(1); |
| 627 | |
| 628 | // Check if the condition value compares a value for equality against zero. |
| 629 | if (!ICI->isEquality() || !match(CmpRHS, m_Zero())) |
| 630 | return nullptr; |
| 631 | |
| 632 | Value *Count = FalseVal; |
| 633 | Value *ValueOnZero = TrueVal; |
| 634 | if (Pred == ICmpInst::ICMP_NE) |
| 635 | std::swap(Count, ValueOnZero); |
| 636 | |
| 637 | // Skip zero extend/truncate. |
| 638 | Value *V = nullptr; |
| 639 | if (match(Count, m_ZExt(m_Value(V))) || |
| 640 | match(Count, m_Trunc(m_Value(V)))) |
| 641 | Count = V; |
| 642 | |
| 643 | // Check if the value propagated on zero is a constant number equal to the |
| 644 | // sizeof in bits of 'Count'. |
| 645 | unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits(); |
| 646 | if (!match(ValueOnZero, m_SpecificInt(SizeOfInBits))) |
| 647 | return nullptr; |
| 648 | |
| 649 | // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the |
| 650 | // input to the cttz/ctlz is used as LHS for the compare instruction. |
| 651 | if (match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) || |
| 652 | match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) { |
| 653 | IntrinsicInst *II = cast<IntrinsicInst>(Count); |
Andrea Di Biagio | 30d471f | 2015-02-13 16:33:34 +0000 | [diff] [blame] | 654 | // Explicitly clear the 'undef_on_zero' flag. |
| 655 | IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone()); |
Craig Topper | 3b74a68 | 2017-08-04 16:07:18 +0000 | [diff] [blame] | 656 | NewI->setArgOperand(1, ConstantInt::getFalse(NewI->getContext())); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 657 | Builder.Insert(NewI); |
| 658 | return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType()); |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | return nullptr; |
| 662 | } |
| 663 | |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 664 | /// Return true if we find and adjust an icmp+select pattern where the compare |
| 665 | /// is with a constant that can be incremented or decremented to match the |
| 666 | /// minimum or maximum idiom. |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 667 | static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { |
| 668 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 669 | Value *CmpLHS = Cmp.getOperand(0); |
| 670 | Value *CmpRHS = Cmp.getOperand(1); |
| 671 | Value *TrueVal = Sel.getTrueValue(); |
| 672 | Value *FalseVal = Sel.getFalseValue(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 673 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 674 | // We may move or edit the compare, so make sure the select is the only user. |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 675 | const APInt *CmpC; |
| 676 | if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 677 | return false; |
Frits van Bommel | 6a1fb8f | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 678 | |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 679 | // These transforms only work for selects of integers or vector selects of |
| 680 | // integer vectors. |
| 681 | Type *SelTy = Sel.getType(); |
| 682 | auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType()); |
| 683 | if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy()) |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 684 | return false; |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 685 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 686 | Constant *AdjustedRHS; |
| 687 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 688 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 689 | else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 690 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 691 | else |
| 692 | return false; |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 693 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 694 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
| 695 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
| 696 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 697 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 698 | ; // Nothing to do here. Values match without any sign/zero extension. |
| 699 | } |
| 700 | // Types do not match. Instead of calculating this with mixed types, promote |
| 701 | // all to the larger type. This enables scalar evolution to analyze this |
| 702 | // expression. |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 703 | else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { |
| 704 | Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy); |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 705 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 706 | // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X |
| 707 | // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X |
| 708 | // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X |
| 709 | // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X |
| 710 | if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) { |
| 711 | CmpLHS = TrueVal; |
| 712 | AdjustedRHS = SextRHS; |
| 713 | } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) && |
| 714 | SextRHS == TrueVal) { |
| 715 | CmpLHS = FalseVal; |
| 716 | AdjustedRHS = SextRHS; |
| 717 | } else if (Cmp.isUnsigned()) { |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 718 | Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 719 | // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X |
| 720 | // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X |
| 721 | // zext + signed compare cannot be changed: |
| 722 | // 0xff <s 0x00, but 0x00ff >s 0x0000 |
| 723 | if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) { |
| 724 | CmpLHS = TrueVal; |
| 725 | AdjustedRHS = ZextRHS; |
| 726 | } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) && |
| 727 | ZextRHS == TrueVal) { |
| 728 | CmpLHS = FalseVal; |
| 729 | AdjustedRHS = ZextRHS; |
| 730 | } else { |
| 731 | return false; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 732 | } |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 733 | } else { |
| 734 | return false; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 735 | } |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 736 | } else { |
| 737 | return false; |
| 738 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 739 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 740 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 741 | CmpRHS = AdjustedRHS; |
| 742 | std::swap(FalseVal, TrueVal); |
| 743 | Cmp.setPredicate(Pred); |
| 744 | Cmp.setOperand(0, CmpLHS); |
| 745 | Cmp.setOperand(1, CmpRHS); |
| 746 | Sel.setOperand(1, TrueVal); |
| 747 | Sel.setOperand(2, FalseVal); |
| 748 | Sel.swapProfMetadata(); |
| 749 | |
| 750 | // Move the compare instruction right before the select instruction. Otherwise |
| 751 | // the sext/zext value may be defined after the compare instruction uses it. |
| 752 | Cmp.moveBefore(&Sel); |
| 753 | |
| 754 | return true; |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 757 | /// If this is an integer min/max (icmp + select) with a constant operand, |
| 758 | /// create the canonical icmp for the min/max operation and canonicalize the |
| 759 | /// constant to the 'false' operand of the select: |
| 760 | /// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2 |
| 761 | /// Note: if C1 != C2, this will change the icmp constant to the existing |
| 762 | /// constant operand of the select. |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 763 | static Instruction * |
| 764 | canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp, |
| 765 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 766 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 767 | return nullptr; |
| 768 | |
| 769 | // Canonicalize the compare predicate based on whether we have min or max. |
| 770 | Value *LHS, *RHS; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 771 | SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS); |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 772 | if (!SelectPatternResult::isMinOrMax(SPR.Flavor)) |
| 773 | return nullptr; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 774 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 775 | // Is this already canonical? |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 776 | ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor); |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 777 | if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS && |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 778 | Cmp.getPredicate() == CanonicalPred) |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 779 | return nullptr; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 780 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 781 | // Create the canonical compare and plug it into the select. |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 782 | Sel.setCondition(Builder.CreateICmp(CanonicalPred, LHS, RHS)); |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 783 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 784 | // If the select operands did not change, we're done. |
| 785 | if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS) |
| 786 | return &Sel; |
| 787 | |
| 788 | // If we are swapping the select operands, swap the metadata too. |
| 789 | assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && |
| 790 | "Unexpected results from matchSelectPattern"); |
| 791 | Sel.setTrueValue(LHS); |
| 792 | Sel.setFalseValue(RHS); |
| 793 | Sel.swapProfMetadata(); |
| 794 | return &Sel; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 797 | /// There are 4 select variants for each of ABS/NABS (different compare |
| 798 | /// constants, compare predicates, select operands). Canonicalize to 1 pattern. |
| 799 | /// This makes CSE more likely. |
| 800 | static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp, |
| 801 | InstCombiner::BuilderTy &Builder) { |
| 802 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) |
| 803 | return nullptr; |
| 804 | |
| 805 | // Choose a sign-bit check for the compare (likely simpler for codegen). |
| 806 | // ABS: (X <s 0) ? -X : X |
| 807 | // NABS: (X <s 0) ? X : -X |
| 808 | Value *LHS, *RHS; |
| 809 | SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor; |
| 810 | if (SPF != SelectPatternFlavor::SPF_ABS && |
| 811 | SPF != SelectPatternFlavor::SPF_NABS) |
| 812 | return nullptr; |
| 813 | |
| 814 | // Is this already canonical? |
| 815 | if (match(Cmp.getOperand(1), m_ZeroInt()) && |
| 816 | Cmp.getPredicate() == ICmpInst::ICMP_SLT) |
| 817 | return nullptr; |
| 818 | |
| 819 | // Create the canonical compare. |
| 820 | Cmp.setPredicate(ICmpInst::ICMP_SLT); |
| 821 | Cmp.setOperand(1, ConstantInt::getNullValue(LHS->getType())); |
| 822 | |
| 823 | // If the select operands do not change, we're done. |
| 824 | Value *TVal = Sel.getTrueValue(); |
| 825 | Value *FVal = Sel.getFalseValue(); |
| 826 | if (SPF == SelectPatternFlavor::SPF_NABS) { |
| 827 | if (TVal == LHS && match(FVal, m_Neg(m_Specific(TVal)))) |
| 828 | return &Sel; |
| 829 | assert(FVal == LHS && match(TVal, m_Neg(m_Specific(FVal))) && |
| 830 | "Unexpected results from matchSelectPattern"); |
| 831 | } else { |
| 832 | if (FVal == LHS && match(TVal, m_Neg(m_Specific(FVal)))) |
| 833 | return &Sel; |
| 834 | assert(TVal == LHS && match(FVal, m_Neg(m_Specific(TVal))) && |
| 835 | "Unexpected results from matchSelectPattern"); |
| 836 | } |
| 837 | |
| 838 | // We are swapping the select operands, so swap the metadata too. |
| 839 | Sel.setTrueValue(FVal); |
| 840 | Sel.setFalseValue(TVal); |
| 841 | Sel.swapProfMetadata(); |
| 842 | return &Sel; |
| 843 | } |
| 844 | |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 845 | /// Visit a SelectInst that has an ICmpInst as its first operand. |
| 846 | Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, |
| 847 | ICmpInst *ICI) { |
Craig Topper | c2d3c63 | 2017-08-04 05:12:37 +0000 | [diff] [blame] | 848 | Value *TrueVal = SI.getTrueValue(); |
| 849 | Value *FalseVal = SI.getFalseValue(); |
| 850 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 851 | if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder)) |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 852 | return NewSel; |
| 853 | |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 854 | if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder)) |
| 855 | return NewAbs; |
| 856 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 857 | bool Changed = adjustMinMax(SI, *ICI); |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 858 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 859 | if (Value *V = foldSelectICmpAnd(SI, ICI, Builder)) |
| 860 | return replaceInstUsesWith(SI, V); |
Craig Topper | 74177e1 | 2017-08-21 19:02:06 +0000 | [diff] [blame] | 861 | |
Benjamin Kramer | 749ef5f | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 862 | // NOTE: if we wanted to, this is where to detect integer MIN/MAX |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 863 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 864 | Value *CmpLHS = ICI->getOperand(0); |
| 865 | Value *CmpRHS = ICI->getOperand(1); |
Benjamin Kramer | b8743a9 | 2012-05-28 19:18:16 +0000 | [diff] [blame] | 866 | if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) { |
Nick Lewycky | 83167df | 2011-03-27 07:30:57 +0000 | [diff] [blame] | 867 | if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) { |
| 868 | // Transform (X == C) ? X : Y -> (X == C) ? C : Y |
| 869 | SI.setOperand(1, CmpRHS); |
| 870 | Changed = true; |
| 871 | } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) { |
| 872 | // Transform (X != C) ? Y : X -> (X != C) ? Y : C |
| 873 | SI.setOperand(2, CmpRHS); |
| 874 | Changed = true; |
| 875 | } |
| 876 | } |
| 877 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 878 | // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring |
| 879 | // decomposeBitTestICmp() might help. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 880 | { |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 881 | unsigned BitWidth = |
| 882 | DL.getTypeSizeInBits(TrueVal->getType()->getScalarType()); |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 883 | APInt MinSignedValue = APInt::getSignedMinValue(BitWidth); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 884 | Value *X; |
| 885 | const APInt *Y, *C; |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 886 | bool TrueWhenUnset; |
| 887 | bool IsBitTest = false; |
| 888 | if (ICmpInst::isEquality(Pred) && |
| 889 | match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 890 | match(CmpRHS, m_Zero())) { |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 891 | IsBitTest = true; |
| 892 | TrueWhenUnset = Pred == ICmpInst::ICMP_EQ; |
| 893 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
| 894 | X = CmpLHS; |
| 895 | Y = &MinSignedValue; |
| 896 | IsBitTest = true; |
| 897 | TrueWhenUnset = false; |
| 898 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
| 899 | X = CmpLHS; |
| 900 | Y = &MinSignedValue; |
| 901 | IsBitTest = true; |
| 902 | TrueWhenUnset = true; |
| 903 | } |
| 904 | if (IsBitTest) { |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 905 | Value *V = nullptr; |
| 906 | // (X & Y) == 0 ? X : X ^ Y --> X & ~Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 907 | if (TrueWhenUnset && TrueVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 908 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 909 | V = Builder.CreateAnd(X, ~(*Y)); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 910 | // (X & Y) != 0 ? X ^ Y : X --> X & ~Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 911 | else if (!TrueWhenUnset && FalseVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 912 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 913 | V = Builder.CreateAnd(X, ~(*Y)); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 914 | // (X & Y) == 0 ? X ^ Y : X --> X | Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 915 | else if (TrueWhenUnset && FalseVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 916 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 917 | V = Builder.CreateOr(X, *Y); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 918 | // (X & Y) != 0 ? X : X ^ Y --> X | Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 919 | else if (!TrueWhenUnset && TrueVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 920 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 921 | V = Builder.CreateOr(X, *Y); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 922 | |
| 923 | if (V) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 924 | return replaceInstUsesWith(SI, V); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 928 | if (Instruction *V = |
| 929 | foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder)) |
| 930 | return V; |
| 931 | |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 932 | if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 933 | return replaceInstUsesWith(SI, V); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 934 | |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 935 | if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 936 | return replaceInstUsesWith(SI, V); |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 937 | |
Sanjay Patel | e9a153f | 2018-02-05 17:53:29 +0000 | [diff] [blame] | 938 | if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder)) |
| 939 | return replaceInstUsesWith(SI, V); |
| 940 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 941 | return Changed ? &SI : nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 944 | /// SI is a select whose condition is a PHI node (but the two may be in |
| 945 | /// different blocks). See if the true/false values (V) are live in all of the |
| 946 | /// predecessor blocks of the PHI. For example, cases like this can't be mapped: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 947 | /// |
| 948 | /// X = phi [ C1, BB1], [C2, BB2] |
| 949 | /// Y = add |
| 950 | /// Z = select X, Y, 0 |
| 951 | /// |
| 952 | /// because Y is not live in BB1/BB2. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 953 | static bool canSelectOperandBeMappingIntoPredBlock(const Value *V, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 954 | const SelectInst &SI) { |
| 955 | // If the value is a non-instruction value like a constant or argument, it |
| 956 | // can always be mapped. |
| 957 | const Instruction *I = dyn_cast<Instruction>(V); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 958 | if (!I) return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 959 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 960 | // If V is a PHI node defined in the same block as the condition PHI, we can |
| 961 | // map the arguments. |
| 962 | const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 963 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 964 | if (const PHINode *VP = dyn_cast<PHINode>(I)) |
| 965 | if (VP->getParent() == CondPHI->getParent()) |
| 966 | return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 968 | // Otherwise, if the PHI and select are defined in the same block and if V is |
| 969 | // defined in a different block, then we can transform it. |
| 970 | if (SI.getParent() == CondPHI->getParent() && |
| 971 | I->getParent() != CondPHI->getParent()) |
| 972 | return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 974 | // Otherwise we have a 'hard' case and we can't tell without doing more |
| 975 | // detailed dominator based analysis, punt. |
| 976 | return false; |
| 977 | } |
| 978 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 979 | /// We have an SPF (e.g. a min or max) of an SPF of the form: |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 980 | /// SPF2(SPF1(A, B), C) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 981 | Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 982 | SelectPatternFlavor SPF1, |
| 983 | Value *A, Value *B, |
| 984 | Instruction &Outer, |
| 985 | SelectPatternFlavor SPF2, Value *C) { |
David Majnemer | 5673772 | 2016-04-08 16:51:49 +0000 | [diff] [blame] | 986 | if (Outer.getType() != Inner->getType()) |
| 987 | return nullptr; |
| 988 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 989 | if (C == A || C == B) { |
| 990 | // MAX(MAX(A, B), B) -> MAX(A, B) |
| 991 | // MIN(MIN(a, b), a) -> MIN(a, b) |
Craig Topper | 0198b73 | 2018-05-18 21:21:56 +0000 | [diff] [blame] | 992 | if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 993 | return replaceInstUsesWith(Outer, Inner); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 995 | // MAX(MIN(a, b), a) -> a |
| 996 | // MIN(MAX(a, b), a) -> a |
| 997 | if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || |
| 998 | (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || |
| 999 | (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || |
| 1000 | (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1001 | return replaceInstUsesWith(Outer, C); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1002 | } |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1003 | |
Dinesh Dwivedi | f675f42 | 2014-05-15 06:13:40 +0000 | [diff] [blame] | 1004 | if (SPF1 == SPF2) { |
Sanjay Patel | c0de9c9 | 2016-10-27 21:19:40 +0000 | [diff] [blame] | 1005 | const APInt *CB, *CC; |
| 1006 | if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) { |
| 1007 | // MIN(MIN(A, 23), 97) -> MIN(A, 23) |
| 1008 | // MAX(MAX(A, 97), 23) -> MAX(A, 97) |
| 1009 | if ((SPF1 == SPF_UMIN && CB->ule(*CC)) || |
| 1010 | (SPF1 == SPF_SMIN && CB->sle(*CC)) || |
| 1011 | (SPF1 == SPF_UMAX && CB->uge(*CC)) || |
| 1012 | (SPF1 == SPF_SMAX && CB->sge(*CC))) |
| 1013 | return replaceInstUsesWith(Outer, Inner); |
Dinesh Dwivedi | f82f16e | 2014-05-19 07:08:32 +0000 | [diff] [blame] | 1014 | |
Sanjay Patel | c0de9c9 | 2016-10-27 21:19:40 +0000 | [diff] [blame] | 1015 | // MIN(MIN(A, 97), 23) -> MIN(A, 23) |
| 1016 | // MAX(MAX(A, 23), 97) -> MAX(A, 97) |
| 1017 | if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) || |
| 1018 | (SPF1 == SPF_SMIN && CB->sgt(*CC)) || |
| 1019 | (SPF1 == SPF_UMAX && CB->ult(*CC)) || |
| 1020 | (SPF1 == SPF_SMAX && CB->slt(*CC))) { |
| 1021 | Outer.replaceUsesOfWith(Inner, A); |
| 1022 | return &Outer; |
Dinesh Dwivedi | f675f42 | 2014-05-15 06:13:40 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | } |
Dinesh Dwivedi | 3217b6c | 2014-06-06 06:54:45 +0000 | [diff] [blame] | 1026 | |
| 1027 | // ABS(ABS(X)) -> ABS(X) |
| 1028 | // NABS(NABS(X)) -> NABS(X) |
| 1029 | if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1030 | return replaceInstUsesWith(Outer, Inner); |
Dinesh Dwivedi | 3217b6c | 2014-06-06 06:54:45 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Dinesh Dwivedi | 95f0d51 | 2014-06-12 14:06:00 +0000 | [diff] [blame] | 1033 | // ABS(NABS(X)) -> ABS(X) |
| 1034 | // NABS(ABS(X)) -> NABS(X) |
| 1035 | if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) || |
| 1036 | (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) { |
| 1037 | SelectInst *SI = cast<SelectInst>(Inner); |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 1038 | Value *NewSI = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1039 | Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(), |
| 1040 | SI->getTrueValue(), SI->getName(), SI); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1041 | return replaceInstUsesWith(Outer, NewSI); |
Dinesh Dwivedi | 95f0d51 | 2014-06-12 14:06:00 +0000 | [diff] [blame] | 1042 | } |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1043 | |
| 1044 | auto IsFreeOrProfitableToInvert = |
| 1045 | [&](Value *V, Value *&NotV, bool &ElidesXor) { |
| 1046 | if (match(V, m_Not(m_Value(NotV)))) { |
| 1047 | // If V has at most 2 uses then we can get rid of the xor operation |
| 1048 | // entirely. |
| 1049 | ElidesXor |= !V->hasNUsesOrMore(3); |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | if (IsFreeToInvert(V, !V->hasNUsesOrMore(3))) { |
| 1054 | NotV = nullptr; |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | return false; |
| 1059 | }; |
| 1060 | |
| 1061 | Value *NotA, *NotB, *NotC; |
| 1062 | bool ElidesXor = false; |
| 1063 | |
| 1064 | // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C) |
| 1065 | // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C) |
| 1066 | // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C) |
| 1067 | // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C) |
| 1068 | // |
| 1069 | // This transform is performance neutral if we can elide at least one xor from |
| 1070 | // the set of three operands, since we'll be tacking on an xor at the very |
| 1071 | // end. |
Anna Thomas | ec36f3b | 2017-02-21 14:40:28 +0000 | [diff] [blame] | 1072 | if (SelectPatternResult::isMinOrMax(SPF1) && |
| 1073 | SelectPatternResult::isMinOrMax(SPF2) && |
| 1074 | IsFreeOrProfitableToInvert(A, NotA, ElidesXor) && |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1075 | IsFreeOrProfitableToInvert(B, NotB, ElidesXor) && |
| 1076 | IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) { |
| 1077 | if (!NotA) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1078 | NotA = Builder.CreateNot(A); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1079 | if (!NotB) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1080 | NotB = Builder.CreateNot(B); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1081 | if (!NotC) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1082 | NotC = Builder.CreateNot(C); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1083 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1084 | Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA, |
| 1085 | NotB); |
| 1086 | Value *NewOuter = Builder.CreateNot( |
| 1087 | createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC)); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1088 | return replaceInstUsesWith(Outer, NewOuter); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1091 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1094 | /// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))). |
| 1095 | /// This is even legal for FP. |
| 1096 | static Instruction *foldAddSubSelect(SelectInst &SI, |
| 1097 | InstCombiner::BuilderTy &Builder) { |
| 1098 | Value *CondVal = SI.getCondition(); |
| 1099 | Value *TrueVal = SI.getTrueValue(); |
| 1100 | Value *FalseVal = SI.getFalseValue(); |
| 1101 | auto *TI = dyn_cast<Instruction>(TrueVal); |
| 1102 | auto *FI = dyn_cast<Instruction>(FalseVal); |
| 1103 | if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse()) |
| 1104 | return nullptr; |
| 1105 | |
| 1106 | Instruction *AddOp = nullptr, *SubOp = nullptr; |
| 1107 | if ((TI->getOpcode() == Instruction::Sub && |
| 1108 | FI->getOpcode() == Instruction::Add) || |
| 1109 | (TI->getOpcode() == Instruction::FSub && |
| 1110 | FI->getOpcode() == Instruction::FAdd)) { |
| 1111 | AddOp = FI; |
| 1112 | SubOp = TI; |
| 1113 | } else if ((FI->getOpcode() == Instruction::Sub && |
| 1114 | TI->getOpcode() == Instruction::Add) || |
| 1115 | (FI->getOpcode() == Instruction::FSub && |
| 1116 | TI->getOpcode() == Instruction::FAdd)) { |
| 1117 | AddOp = TI; |
| 1118 | SubOp = FI; |
| 1119 | } |
| 1120 | |
| 1121 | if (AddOp) { |
| 1122 | Value *OtherAddOp = nullptr; |
| 1123 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 1124 | OtherAddOp = AddOp->getOperand(1); |
| 1125 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 1126 | OtherAddOp = AddOp->getOperand(0); |
| 1127 | } |
| 1128 | |
| 1129 | if (OtherAddOp) { |
| 1130 | // So at this point we know we have (Y -> OtherAddOp): |
| 1131 | // select C, (add X, Y), (sub X, Z) |
| 1132 | Value *NegVal; // Compute -Z |
| 1133 | if (SI.getType()->isFPOrFPVectorTy()) { |
| 1134 | NegVal = Builder.CreateFNeg(SubOp->getOperand(1)); |
| 1135 | if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) { |
| 1136 | FastMathFlags Flags = AddOp->getFastMathFlags(); |
| 1137 | Flags &= SubOp->getFastMathFlags(); |
| 1138 | NegInst->setFastMathFlags(Flags); |
| 1139 | } |
| 1140 | } else { |
| 1141 | NegVal = Builder.CreateNeg(SubOp->getOperand(1)); |
| 1142 | } |
| 1143 | |
| 1144 | Value *NewTrueOp = OtherAddOp; |
| 1145 | Value *NewFalseOp = NegVal; |
| 1146 | if (AddOp != TI) |
| 1147 | std::swap(NewTrueOp, NewFalseOp); |
| 1148 | Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp, |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 1149 | SI.getName() + ".p", &SI); |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1150 | |
| 1151 | if (SI.getType()->isFPOrFPVectorTy()) { |
| 1152 | Instruction *RI = |
| 1153 | BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel); |
| 1154 | |
| 1155 | FastMathFlags Flags = AddOp->getFastMathFlags(); |
| 1156 | Flags &= SubOp->getFastMathFlags(); |
| 1157 | RI->setFastMathFlags(Flags); |
| 1158 | return RI; |
| 1159 | } else |
| 1160 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
| 1161 | } |
| 1162 | } |
| 1163 | return nullptr; |
| 1164 | } |
| 1165 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1166 | Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame^] | 1167 | Constant *C; |
| 1168 | if (!match(Sel.getTrueValue(), m_Constant(C)) && |
| 1169 | !match(Sel.getFalseValue(), m_Constant(C))) |
| 1170 | return nullptr; |
| 1171 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1172 | Instruction *ExtInst; |
| 1173 | if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) && |
| 1174 | !match(Sel.getFalseValue(), m_Instruction(ExtInst))) |
| 1175 | return nullptr; |
| 1176 | |
| 1177 | auto ExtOpcode = ExtInst->getOpcode(); |
| 1178 | if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt) |
| 1179 | return nullptr; |
| 1180 | |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame^] | 1181 | // If we are extending from a boolean type or if we can create a select that |
| 1182 | // has the same size operands as its condition, try to narrow the select. |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1183 | Value *X = ExtInst->getOperand(0); |
| 1184 | Type *SmallType = X->getType(); |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame^] | 1185 | Value *Cond = Sel.getCondition(); |
| 1186 | auto *Cmp = dyn_cast<CmpInst>(Cond); |
| 1187 | if (!SmallType->isIntOrIntVectorTy(1) && |
| 1188 | (!Cmp || Cmp->getOperand(0)->getType() != SmallType)) |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1189 | return nullptr; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1190 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1191 | // If the constant is the same after truncation to the smaller type and |
| 1192 | // extension to the original type, we can narrow the select. |
| 1193 | Type *SelType = Sel.getType(); |
| 1194 | Constant *TruncC = ConstantExpr::getTrunc(C, SmallType); |
| 1195 | Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType); |
| 1196 | if (ExtC == C) { |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1197 | Value *TruncCVal = cast<Value>(TruncC); |
| 1198 | if (ExtInst == Sel.getFalseValue()) |
| 1199 | std::swap(X, TruncCVal); |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1200 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1201 | // select Cond, (ext X), C --> ext(select Cond, X, C') |
| 1202 | // select Cond, C, (ext X) --> ext(select Cond, C', X) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1203 | Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel); |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1204 | return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType); |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1205 | } |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1206 | |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1207 | // If one arm of the select is the extend of the condition, replace that arm |
| 1208 | // with the extension of the appropriate known bool value. |
| 1209 | if (Cond == X) { |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1210 | if (ExtInst == Sel.getTrueValue()) { |
| 1211 | // select X, (sext X), C --> select X, -1, C |
| 1212 | // select X, (zext X), C --> select X, 1, C |
| 1213 | Constant *One = ConstantInt::getTrue(SmallType); |
| 1214 | Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType); |
Sanjay Patel | 91e73a7 | 2016-11-26 15:01:59 +0000 | [diff] [blame] | 1215 | return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel); |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1216 | } else { |
| 1217 | // select X, C, (sext X) --> select X, C, 0 |
| 1218 | // select X, C, (zext X) --> select X, C, 0 |
| 1219 | Constant *Zero = ConstantInt::getNullValue(SelType); |
Sanjay Patel | 91e73a7 | 2016-11-26 15:01:59 +0000 | [diff] [blame] | 1220 | return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel); |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1221 | } |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1224 | return nullptr; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1227 | /// Try to transform a vector select with a constant condition vector into a |
| 1228 | /// shuffle for easier combining with other shuffles and insert/extract. |
| 1229 | static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) { |
| 1230 | Value *CondVal = SI.getCondition(); |
| 1231 | Constant *CondC; |
| 1232 | if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC))) |
| 1233 | return nullptr; |
| 1234 | |
| 1235 | unsigned NumElts = CondVal->getType()->getVectorNumElements(); |
| 1236 | SmallVector<Constant *, 16> Mask; |
| 1237 | Mask.reserve(NumElts); |
| 1238 | Type *Int32Ty = Type::getInt32Ty(CondVal->getContext()); |
| 1239 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1240 | Constant *Elt = CondC->getAggregateElement(i); |
| 1241 | if (!Elt) |
| 1242 | return nullptr; |
| 1243 | |
| 1244 | if (Elt->isOneValue()) { |
| 1245 | // If the select condition element is true, choose from the 1st vector. |
| 1246 | Mask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1247 | } else if (Elt->isNullValue()) { |
| 1248 | // If the select condition element is false, choose from the 2nd vector. |
| 1249 | Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts)); |
| 1250 | } else if (isa<UndefValue>(Elt)) { |
Sanjay Patel | 6e41018 | 2017-04-12 18:39:53 +0000 | [diff] [blame] | 1251 | // Undef in a select condition (choose one of the operands) does not mean |
| 1252 | // the same thing as undef in a shuffle mask (any value is acceptable), so |
| 1253 | // give up. |
| 1254 | return nullptr; |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1255 | } else { |
| 1256 | // Bail out on a constant expression. |
| 1257 | return nullptr; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), |
| 1262 | ConstantVector::get(Mask)); |
| 1263 | } |
| 1264 | |
Sanjay Patel | 978f827 | 2016-10-29 15:22:04 +0000 | [diff] [blame] | 1265 | /// Reuse bitcasted operands between a compare and select: |
| 1266 | /// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> |
| 1267 | /// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D)) |
| 1268 | static Instruction *foldSelectCmpBitcasts(SelectInst &Sel, |
| 1269 | InstCombiner::BuilderTy &Builder) { |
| 1270 | Value *Cond = Sel.getCondition(); |
| 1271 | Value *TVal = Sel.getTrueValue(); |
| 1272 | Value *FVal = Sel.getFalseValue(); |
| 1273 | |
| 1274 | CmpInst::Predicate Pred; |
| 1275 | Value *A, *B; |
| 1276 | if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) |
| 1277 | return nullptr; |
| 1278 | |
| 1279 | // The select condition is a compare instruction. If the select's true/false |
| 1280 | // values are already the same as the compare operands, there's nothing to do. |
| 1281 | if (TVal == A || TVal == B || FVal == A || FVal == B) |
| 1282 | return nullptr; |
| 1283 | |
| 1284 | Value *C, *D; |
| 1285 | if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D)))) |
| 1286 | return nullptr; |
| 1287 | |
| 1288 | // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc) |
| 1289 | Value *TSrc, *FSrc; |
| 1290 | if (!match(TVal, m_BitCast(m_Value(TSrc))) || |
| 1291 | !match(FVal, m_BitCast(m_Value(FSrc)))) |
| 1292 | return nullptr; |
| 1293 | |
| 1294 | // If the select true/false values are *different bitcasts* of the same source |
| 1295 | // operands, make the select operands the same as the compare operands and |
| 1296 | // cast the result. This is the canonical select form for min/max. |
| 1297 | Value *NewSel; |
| 1298 | if (TSrc == C && FSrc == D) { |
| 1299 | // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> |
| 1300 | // bitcast (select (cmp A, B), A, B) |
| 1301 | NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel); |
| 1302 | } else if (TSrc == D && FSrc == C) { |
| 1303 | // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) --> |
| 1304 | // bitcast (select (cmp A, B), B, A) |
| 1305 | NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel); |
| 1306 | } else { |
| 1307 | return nullptr; |
| 1308 | } |
| 1309 | return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType()); |
| 1310 | } |
| 1311 | |
Matthew Simpson | b6915fb | 2017-10-31 12:34:02 +0000 | [diff] [blame] | 1312 | /// Try to eliminate select instructions that test the returned flag of cmpxchg |
| 1313 | /// instructions. |
| 1314 | /// |
| 1315 | /// If a select instruction tests the returned flag of a cmpxchg instruction and |
| 1316 | /// selects between the returned value of the cmpxchg instruction its compare |
| 1317 | /// operand, the result of the select will always be equal to its false value. |
| 1318 | /// For example: |
| 1319 | /// |
| 1320 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst |
| 1321 | /// %1 = extractvalue { i64, i1 } %0, 1 |
| 1322 | /// %2 = extractvalue { i64, i1 } %0, 0 |
| 1323 | /// %3 = select i1 %1, i64 %compare, i64 %2 |
| 1324 | /// ret i64 %3 |
| 1325 | /// |
| 1326 | /// The returned value of the cmpxchg instruction (%2) is the original value |
| 1327 | /// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2 |
| 1328 | /// must have been equal to %compare. Thus, the result of the select is always |
| 1329 | /// equal to %2, and the code can be simplified to: |
| 1330 | /// |
| 1331 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst |
| 1332 | /// %1 = extractvalue { i64, i1 } %0, 0 |
| 1333 | /// ret i64 %1 |
| 1334 | /// |
| 1335 | static Instruction *foldSelectCmpXchg(SelectInst &SI) { |
| 1336 | // A helper that determines if V is an extractvalue instruction whose |
| 1337 | // aggregate operand is a cmpxchg instruction and whose single index is equal |
| 1338 | // to I. If such conditions are true, the helper returns the cmpxchg |
| 1339 | // instruction; otherwise, a nullptr is returned. |
| 1340 | auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * { |
| 1341 | auto *Extract = dyn_cast<ExtractValueInst>(V); |
| 1342 | if (!Extract) |
| 1343 | return nullptr; |
| 1344 | if (Extract->getIndices()[0] != I) |
| 1345 | return nullptr; |
| 1346 | return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand()); |
| 1347 | }; |
| 1348 | |
| 1349 | // If the select has a single user, and this user is a select instruction that |
| 1350 | // we can simplify, skip the cmpxchg simplification for now. |
| 1351 | if (SI.hasOneUse()) |
| 1352 | if (auto *Select = dyn_cast<SelectInst>(SI.user_back())) |
| 1353 | if (Select->getCondition() == SI.getCondition()) |
| 1354 | if (Select->getFalseValue() == SI.getTrueValue() || |
| 1355 | Select->getTrueValue() == SI.getFalseValue()) |
| 1356 | return nullptr; |
| 1357 | |
| 1358 | // Ensure the select condition is the returned flag of a cmpxchg instruction. |
| 1359 | auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1); |
| 1360 | if (!CmpXchg) |
| 1361 | return nullptr; |
| 1362 | |
| 1363 | // Check the true value case: The true value of the select is the returned |
| 1364 | // value of the same cmpxchg used by the condition, and the false value is the |
| 1365 | // cmpxchg instruction's compare operand. |
| 1366 | if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0)) |
| 1367 | if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) { |
| 1368 | SI.setTrueValue(SI.getFalseValue()); |
| 1369 | return &SI; |
| 1370 | } |
| 1371 | |
| 1372 | // Check the false value case: The false value of the select is the returned |
| 1373 | // value of the same cmpxchg used by the condition, and the true value is the |
| 1374 | // cmpxchg instruction's compare operand. |
| 1375 | if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0)) |
| 1376 | if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) { |
| 1377 | SI.setTrueValue(SI.getFalseValue()); |
| 1378 | return &SI; |
| 1379 | } |
| 1380 | |
| 1381 | return nullptr; |
| 1382 | } |
| 1383 | |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1384 | /// Reduce a sequence of min/max with a common operand. |
| 1385 | static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS, |
| 1386 | Value *RHS, |
| 1387 | InstCombiner::BuilderTy &Builder) { |
| 1388 | assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max"); |
| 1389 | // TODO: Allow FP min/max with nnan/nsz. |
| 1390 | if (!LHS->getType()->isIntOrIntVectorTy()) |
| 1391 | return nullptr; |
| 1392 | |
| 1393 | // Match 3 of the same min/max ops. Example: umin(umin(), umin()). |
| 1394 | Value *A, *B, *C, *D; |
| 1395 | SelectPatternResult L = matchSelectPattern(LHS, A, B); |
| 1396 | SelectPatternResult R = matchSelectPattern(RHS, C, D); |
| 1397 | if (SPF != L.Flavor || L.Flavor != R.Flavor) |
| 1398 | return nullptr; |
| 1399 | |
| 1400 | // Look for a common operand. The use checks are different than usual because |
| 1401 | // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by |
| 1402 | // the select. |
| 1403 | Value *MinMaxOp = nullptr; |
| 1404 | Value *ThirdOp = nullptr; |
Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 1405 | if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) { |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1406 | // If the LHS is only used in this chain and the RHS is used outside of it, |
| 1407 | // reuse the RHS min/max because that will eliminate the LHS. |
| 1408 | if (D == A || C == A) { |
| 1409 | // min(min(a, b), min(c, a)) --> min(min(c, a), b) |
| 1410 | // min(min(a, b), min(a, d)) --> min(min(a, d), b) |
| 1411 | MinMaxOp = RHS; |
| 1412 | ThirdOp = B; |
| 1413 | } else if (D == B || C == B) { |
| 1414 | // min(min(a, b), min(c, b)) --> min(min(c, b), a) |
| 1415 | // min(min(a, b), min(b, d)) --> min(min(b, d), a) |
| 1416 | MinMaxOp = RHS; |
| 1417 | ThirdOp = A; |
| 1418 | } |
Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 1419 | } else if (!RHS->hasNUsesOrMore(3)) { |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1420 | // Reuse the LHS. This will eliminate the RHS. |
| 1421 | if (D == A || D == B) { |
| 1422 | // min(min(a, b), min(c, a)) --> min(min(a, b), c) |
| 1423 | // min(min(a, b), min(c, b)) --> min(min(a, b), c) |
| 1424 | MinMaxOp = LHS; |
| 1425 | ThirdOp = C; |
| 1426 | } else if (C == A || C == B) { |
| 1427 | // min(min(a, b), min(b, d)) --> min(min(a, b), d) |
| 1428 | // min(min(a, b), min(c, b)) --> min(min(a, b), d) |
| 1429 | MinMaxOp = LHS; |
| 1430 | ThirdOp = D; |
| 1431 | } |
| 1432 | } |
| 1433 | if (!MinMaxOp || !ThirdOp) |
| 1434 | return nullptr; |
| 1435 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1436 | CmpInst::Predicate P = getMinMaxPred(SPF); |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1437 | Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp); |
| 1438 | return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp); |
| 1439 | } |
| 1440 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1441 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
| 1442 | Value *CondVal = SI.getCondition(); |
| 1443 | Value *TrueVal = SI.getTrueValue(); |
| 1444 | Value *FalseVal = SI.getFalseValue(); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1445 | Type *SelType = SI.getType(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1446 | |
Wei Mi | fc0e245 | 2017-07-25 23:37:17 +0000 | [diff] [blame] | 1447 | // FIXME: Remove this workaround when freeze related patches are done. |
| 1448 | // For select with undef operand which feeds into an equality comparison, |
| 1449 | // don't simplify it so loop unswitch can know the equality comparison |
| 1450 | // may have an undef operand. This is a workaround for PR31652 caused by |
| 1451 | // descrepancy about branch on undef between LoopUnswitch and GVN. |
| 1452 | if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) { |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1453 | if (llvm::any_of(SI.users(), [&](User *U) { |
Wei Mi | fc0e245 | 2017-07-25 23:37:17 +0000 | [diff] [blame] | 1454 | ICmpInst *CI = dyn_cast<ICmpInst>(U); |
| 1455 | if (CI && CI->isEquality()) |
| 1456 | return true; |
| 1457 | return false; |
| 1458 | })) { |
| 1459 | return nullptr; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1463 | if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, |
| 1464 | SQ.getWithInstruction(&SI))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1465 | return replaceInstUsesWith(SI, V); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1466 | |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1467 | if (Instruction *I = canonicalizeSelectToShuffle(SI)) |
| 1468 | return I; |
| 1469 | |
Sanjay Patel | 7227276 | 2017-06-27 17:53:22 +0000 | [diff] [blame] | 1470 | // Canonicalize a one-use integer compare with a non-canonical predicate by |
| 1471 | // inverting the predicate and swapping the select operands. This matches a |
| 1472 | // compare canonicalization for conditional branches. |
| 1473 | // TODO: Should we do the same for FP compares? |
| 1474 | CmpInst::Predicate Pred; |
| 1475 | if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) && |
| 1476 | !isCanonicalPredicate(Pred)) { |
| 1477 | // Swap true/false values and condition. |
| 1478 | CmpInst *Cond = cast<CmpInst>(CondVal); |
| 1479 | Cond->setPredicate(CmpInst::getInversePredicate(Pred)); |
| 1480 | SI.setOperand(1, FalseVal); |
| 1481 | SI.setOperand(2, TrueVal); |
| 1482 | SI.swapProfMetadata(); |
| 1483 | Worklist.Add(Cond); |
| 1484 | return &SI; |
| 1485 | } |
| 1486 | |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 1487 | if (SelType->isIntOrIntVectorTy(1) && |
Sanjay Patel | cbaac41 | 2016-07-03 14:34:39 +0000 | [diff] [blame] | 1488 | TrueVal->getType() == CondVal->getType()) { |
Sanjay Patel | ea23436 | 2016-07-06 21:01:26 +0000 | [diff] [blame] | 1489 | if (match(TrueVal, m_One())) { |
| 1490 | // Change: A = select B, true, C --> A = or B, C |
| 1491 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
| 1492 | } |
| 1493 | if (match(TrueVal, m_Zero())) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1494 | // Change: A = select B, false, C --> A = and !B, C |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1495 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1496 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1497 | } |
Sanjay Patel | ea23436 | 2016-07-06 21:01:26 +0000 | [diff] [blame] | 1498 | if (match(FalseVal, m_Zero())) { |
| 1499 | // Change: A = select B, C, false --> A = and B, C |
| 1500 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
| 1501 | } |
| 1502 | if (match(FalseVal, m_One())) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1503 | // Change: A = select B, C, true --> A = or !B, C |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1504 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1505 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1506 | } |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1507 | |
Sanjay Patel | a1a4e10 | 2016-07-03 14:08:19 +0000 | [diff] [blame] | 1508 | // select a, a, b -> a | b |
| 1509 | // select a, b, a -> a & b |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1510 | if (CondVal == TrueVal) |
| 1511 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1512 | if (CondVal == FalseVal) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1513 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1514 | |
Sanjay Patel | a1a4e10 | 2016-07-03 14:08:19 +0000 | [diff] [blame] | 1515 | // select a, ~a, b -> (~a) & b |
| 1516 | // select a, b, ~a -> (~a) | b |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1517 | if (match(TrueVal, m_Not(m_Specific(CondVal)))) |
| 1518 | return BinaryOperator::CreateAnd(TrueVal, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1519 | if (match(FalseVal, m_Not(m_Specific(CondVal)))) |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1520 | return BinaryOperator::CreateOr(TrueVal, FalseVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1523 | // Selecting between two integer or vector splat integer constants? |
| 1524 | // |
| 1525 | // Note that we don't handle a scalar select of vectors: |
| 1526 | // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0> |
| 1527 | // because that may need 3 instructions to splat the condition value: |
| 1528 | // extend, insertelement, shufflevector. |
Craig Topper | e79b3e7 | 2017-07-09 03:25:17 +0000 | [diff] [blame] | 1529 | if (SelType->isIntOrIntVectorTy() && |
| 1530 | CondVal->getType()->isVectorTy() == SelType->isVectorTy()) { |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1531 | // select C, 1, 0 -> zext C to int |
| 1532 | if (match(TrueVal, m_One()) && match(FalseVal, m_Zero())) |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1533 | return new ZExtInst(CondVal, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1534 | |
| 1535 | // select C, -1, 0 -> sext C to int |
| 1536 | if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero())) |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1537 | return new SExtInst(CondVal, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1538 | |
| 1539 | // select C, 0, 1 -> zext !C to int |
| 1540 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1541 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1542 | return new ZExtInst(NotCond, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | // select C, 0, -1 -> sext !C to int |
| 1546 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1547 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1548 | return new SExtInst(NotCond, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1552 | // See if we are selecting two values based on a comparison of the two values. |
| 1553 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 1554 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
| 1555 | // Transform (X == Y) ? X : Y -> Y |
| 1556 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1557 | // This is not safe in general for floating point: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1558 | // consider X== -0, Y== +0. |
| 1559 | // It becomes safe if either operand is a nonzero constant. |
| 1560 | ConstantFP *CFPt, *CFPf; |
| 1561 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 1562 | !CFPt->getValueAPF().isZero()) || |
| 1563 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 1564 | !CFPf->getValueAPF().isZero())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1565 | return replaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1566 | } |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1567 | // Transform (X une Y) ? X : Y -> X |
| 1568 | if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1569 | // This is not safe in general for floating point: |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1570 | // consider X== -0, Y== +0. |
| 1571 | // It becomes safe if either operand is a nonzero constant. |
| 1572 | ConstantFP *CFPt, *CFPf; |
| 1573 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 1574 | !CFPt->getValueAPF().isZero()) || |
| 1575 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 1576 | !CFPf->getValueAPF().isZero())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1577 | return replaceInstUsesWith(SI, TrueVal); |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1578 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1579 | |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1580 | // Canonicalize to use ordered comparisons by swapping the select |
| 1581 | // operands. |
| 1582 | // |
| 1583 | // e.g. |
| 1584 | // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X |
| 1585 | if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { |
| 1586 | FCmpInst::Predicate InvPred = FCI->getInversePredicate(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1587 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
| 1588 | Builder.setFastMathFlags(FCI->getFastMathFlags()); |
| 1589 | Value *NewCond = Builder.CreateFCmp(InvPred, TrueVal, FalseVal, |
| 1590 | FCI->getName() + ".inv"); |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1591 | |
| 1592 | return SelectInst::Create(NewCond, FalseVal, TrueVal, |
| 1593 | SI.getName() + ".p"); |
| 1594 | } |
| 1595 | |
| 1596 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1597 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
| 1598 | // Transform (X == Y) ? Y : X -> X |
| 1599 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1600 | // This is not safe in general for floating point: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1601 | // consider X== -0, Y== +0. |
| 1602 | // It becomes safe if either operand is a nonzero constant. |
| 1603 | ConstantFP *CFPt, *CFPf; |
| 1604 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 1605 | !CFPt->getValueAPF().isZero()) || |
| 1606 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 1607 | !CFPf->getValueAPF().isZero())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1608 | return replaceInstUsesWith(SI, FalseVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1609 | } |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1610 | // Transform (X une Y) ? Y : X -> Y |
| 1611 | if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1612 | // This is not safe in general for floating point: |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1613 | // consider X== -0, Y== +0. |
| 1614 | // It becomes safe if either operand is a nonzero constant. |
| 1615 | ConstantFP *CFPt, *CFPf; |
| 1616 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 1617 | !CFPt->getValueAPF().isZero()) || |
| 1618 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 1619 | !CFPf->getValueAPF().isZero())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1620 | return replaceInstUsesWith(SI, TrueVal); |
Dan Gohman | cd4c03e | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 1621 | } |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1622 | |
| 1623 | // Canonicalize to use ordered comparisons by swapping the select |
| 1624 | // operands. |
| 1625 | // |
| 1626 | // e.g. |
| 1627 | // (X ugt Y) ? X : Y -> (X ole Y) ? X : Y |
| 1628 | if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { |
| 1629 | FCmpInst::Predicate InvPred = FCI->getInversePredicate(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1630 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
| 1631 | Builder.setFastMathFlags(FCI->getFastMathFlags()); |
| 1632 | Value *NewCond = Builder.CreateFCmp(InvPred, FalseVal, TrueVal, |
| 1633 | FCI->getName() + ".inv"); |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1634 | |
| 1635 | return SelectInst::Create(NewCond, FalseVal, TrueVal, |
| 1636 | SI.getName() + ".p"); |
| 1637 | } |
| 1638 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1639 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| 1640 | } |
Sanjay Patel | 0ce3086 | 2018-03-19 15:14:30 +0000 | [diff] [blame] | 1641 | |
| 1642 | // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need |
| 1643 | // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We |
| 1644 | // also require nnan because we do not want to unintentionally change the |
| 1645 | // sign of a NaN value. |
| 1646 | Value *X = FCI->getOperand(0); |
| 1647 | FCmpInst::Predicate Pred = FCI->getPredicate(); |
| 1648 | if (match(FCI->getOperand(1), m_AnyZeroFP()) && FCI->hasNoNaNs()) { |
| 1649 | // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X) |
| 1650 | // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 1651 | if ((X == FalseVal && Pred == FCmpInst::FCMP_OLE && |
| 1652 | match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) || |
| 1653 | (X == TrueVal && Pred == FCmpInst::FCMP_OGT && |
| 1654 | match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(X))))) { |
Sanjay Patel | 0ce3086 | 2018-03-19 15:14:30 +0000 | [diff] [blame] | 1655 | Value *Fabs = Builder.CreateIntrinsic(Intrinsic::fabs, { X }, FCI); |
| 1656 | return replaceInstUsesWith(SI, Fabs); |
| 1657 | } |
| 1658 | // With nsz: |
| 1659 | // (X < +/-0.0) ? -X : X --> fabs(X) |
| 1660 | // (X <= +/-0.0) ? -X : X --> fabs(X) |
| 1661 | // (X > +/-0.0) ? X : -X --> fabs(X) |
| 1662 | // (X >= +/-0.0) ? X : -X --> fabs(X) |
| 1663 | if (FCI->hasNoSignedZeros() && |
| 1664 | ((X == FalseVal && match(TrueVal, m_FNeg(m_Specific(X))) && |
| 1665 | (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE)) || |
| 1666 | (X == TrueVal && match(FalseVal, m_FNeg(m_Specific(X))) && |
| 1667 | (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE)))) { |
| 1668 | Value *Fabs = Builder.CreateIntrinsic(Intrinsic::fabs, { X }, FCI); |
| 1669 | return replaceInstUsesWith(SI, Fabs); |
| 1670 | } |
| 1671 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | // See if we are selecting two values based on a comparison of the two values. |
| 1675 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1676 | if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1677 | return Result; |
| 1678 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1679 | if (Instruction *Add = foldAddSubSelect(SI, Builder)) |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1680 | return Add; |
| 1681 | |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1682 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
Sanjay Patel | 10a2c38 | 2016-06-08 20:09:04 +0000 | [diff] [blame] | 1683 | auto *TI = dyn_cast<Instruction>(TrueVal); |
| 1684 | auto *FI = dyn_cast<Instruction>(FalseVal); |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1685 | if (TI && FI && TI->getOpcode() == FI->getOpcode()) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1686 | if (Instruction *IV = foldSelectOpOp(SI, TI, FI)) |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1687 | return IV; |
Sanjay Patel | 10a2c38 | 2016-06-08 20:09:04 +0000 | [diff] [blame] | 1688 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1689 | if (Instruction *I = foldSelectExtConst(SI)) |
| 1690 | return I; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1692 | // See if we can fold the select into one of our operands. |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1693 | if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1694 | if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1695 | return FoldI; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1696 | |
Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 1697 | Value *LHS, *RHS, *LHS2, *RHS2; |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1698 | Instruction::CastOps CastOp; |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1699 | SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp); |
| 1700 | auto SPF = SPR.Flavor; |
Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 1701 | |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1702 | if (SelectPatternResult::isMinOrMax(SPF)) { |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 1703 | // Canonicalize so that |
| 1704 | // - type casts are outside select patterns. |
| 1705 | // - float clamp is transformed to min/max pattern |
| 1706 | |
| 1707 | bool IsCastNeeded = LHS->getType() != SelType; |
| 1708 | Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0); |
| 1709 | Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1); |
| 1710 | if (IsCastNeeded || |
| 1711 | (LHS->getType()->isFPOrFPVectorTy() && |
| 1712 | ((CmpLHS != LHS && CmpLHS != RHS) || |
| 1713 | (CmpRHS != LHS && CmpRHS != RHS)))) { |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1714 | CmpInst::Predicate Pred = getMinMaxPred(SPF, SPR.Ordered); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1715 | |
| 1716 | Value *Cmp; |
| 1717 | if (CmpInst::isIntPredicate(Pred)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1718 | Cmp = Builder.CreateICmp(Pred, LHS, RHS); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1719 | } else { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1720 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1721 | auto FMF = cast<FPMathOperator>(SI.getCondition())->getFastMathFlags(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1722 | Builder.setFastMathFlags(FMF); |
| 1723 | Cmp = Builder.CreateFCmp(Pred, LHS, RHS); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 1726 | Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI); |
| 1727 | if (!IsCastNeeded) |
| 1728 | return replaceInstUsesWith(SI, NewSI); |
| 1729 | |
| 1730 | Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType); |
| 1731 | return replaceInstUsesWith(SI, NewCast); |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1732 | } |
Sanjay Patel | 5b6aacf | 2018-01-05 19:01:17 +0000 | [diff] [blame] | 1733 | |
| 1734 | // MAX(~a, ~b) -> ~MIN(a, b) |
| 1735 | // MIN(~a, ~b) -> ~MAX(a, b) |
| 1736 | Value *A, *B; |
Sanjay Patel | 26a6fcd | 2018-01-06 17:34:22 +0000 | [diff] [blame] | 1737 | if (match(LHS, m_Not(m_Value(A))) && match(RHS, m_Not(m_Value(B))) && |
| 1738 | (LHS->getNumUses() <= 2 || RHS->getNumUses() <= 2)) { |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1739 | CmpInst::Predicate InvertedPred = getInverseMinMaxPred(SPF); |
Sanjay Patel | 5b6aacf | 2018-01-05 19:01:17 +0000 | [diff] [blame] | 1740 | Value *InvertedCmp = Builder.CreateICmp(InvertedPred, A, B); |
| 1741 | Value *NewSel = Builder.CreateSelect(InvertedCmp, A, B); |
| 1742 | return BinaryOperator::CreateNot(NewSel); |
| 1743 | } |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1744 | |
| 1745 | if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder)) |
| 1746 | return I; |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1747 | } |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1748 | |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1749 | if (SPF) { |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1750 | // MAX(MAX(a, b), a) -> MAX(a, b) |
| 1751 | // MIN(MIN(a, b), a) -> MIN(a, b) |
| 1752 | // MAX(MIN(a, b), a) -> a |
| 1753 | // MIN(MAX(a, b), a) -> a |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1754 | // ABS(ABS(a)) -> ABS(a) |
| 1755 | // NABS(NABS(a)) -> NABS(a) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1756 | if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1757 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1758 | SI, SPF, RHS)) |
| 1759 | return R; |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1760 | if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1761 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1762 | SI, SPF, LHS)) |
| 1763 | return R; |
| 1764 | } |
| 1765 | |
| 1766 | // TODO. |
| 1767 | // ABS(-X) -> ABS(X) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | // See if we can fold the select into a phi node if the condition is a select. |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1771 | if (auto *PN = dyn_cast<PHINode>(SI.getCondition())) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1772 | // The true/false values have to be live in the PHI predecessor's blocks. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1773 | if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && |
| 1774 | canSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1775 | if (Instruction *NV = foldOpIntoPhi(SI, PN)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1776 | return NV; |
| 1777 | |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1778 | if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) { |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1779 | if (TrueSI->getCondition()->getType() == CondVal->getType()) { |
| 1780 | // select(C, select(C, a, b), c) -> select(C, a, c) |
| 1781 | if (TrueSI->getCondition() == CondVal) { |
| 1782 | if (SI.getTrueValue() == TrueSI->getTrueValue()) |
| 1783 | return nullptr; |
| 1784 | SI.setOperand(1, TrueSI->getTrueValue()); |
| 1785 | return &SI; |
| 1786 | } |
| 1787 | // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b) |
| 1788 | // We choose this as normal form to enable folding on the And and shortening |
| 1789 | // paths for the values (this helps GetUnderlyingObjects() for example). |
| 1790 | if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1791 | Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition()); |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1792 | SI.setOperand(0, And); |
| 1793 | SI.setOperand(1, TrueSI->getTrueValue()); |
| 1794 | return &SI; |
| 1795 | } |
Matthias Braun | 2e40459 | 2015-02-06 17:49:36 +0000 | [diff] [blame] | 1796 | } |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1797 | } |
| 1798 | if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) { |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1799 | if (FalseSI->getCondition()->getType() == CondVal->getType()) { |
| 1800 | // select(C, a, select(C, b, c)) -> select(C, a, c) |
| 1801 | if (FalseSI->getCondition() == CondVal) { |
| 1802 | if (SI.getFalseValue() == FalseSI->getFalseValue()) |
| 1803 | return nullptr; |
| 1804 | SI.setOperand(2, FalseSI->getFalseValue()); |
| 1805 | return &SI; |
| 1806 | } |
| 1807 | // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b) |
| 1808 | if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1809 | Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition()); |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1810 | SI.setOperand(0, Or); |
| 1811 | SI.setOperand(2, FalseSI->getFalseValue()); |
| 1812 | return &SI; |
| 1813 | } |
Matthias Braun | 2e40459 | 2015-02-06 17:49:36 +0000 | [diff] [blame] | 1814 | } |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1817 | auto canMergeSelectThroughBinop = [](BinaryOperator *BO) { |
| 1818 | // The select might be preventing a division by 0. |
| 1819 | switch (BO->getOpcode()) { |
| 1820 | default: |
| 1821 | return true; |
| 1822 | case Instruction::SRem: |
| 1823 | case Instruction::URem: |
| 1824 | case Instruction::SDiv: |
| 1825 | case Instruction::UDiv: |
| 1826 | return false; |
| 1827 | } |
| 1828 | }; |
| 1829 | |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1830 | // Try to simplify a binop sandwiched between 2 selects with the same |
| 1831 | // condition. |
| 1832 | // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z) |
| 1833 | BinaryOperator *TrueBO; |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1834 | if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && |
| 1835 | canMergeSelectThroughBinop(TrueBO)) { |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1836 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) { |
| 1837 | if (TrueBOSI->getCondition() == CondVal) { |
| 1838 | TrueBO->setOperand(0, TrueBOSI->getTrueValue()); |
| 1839 | Worklist.Add(TrueBO); |
| 1840 | return &SI; |
| 1841 | } |
| 1842 | } |
| 1843 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) { |
| 1844 | if (TrueBOSI->getCondition() == CondVal) { |
| 1845 | TrueBO->setOperand(1, TrueBOSI->getTrueValue()); |
| 1846 | Worklist.Add(TrueBO); |
| 1847 | return &SI; |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W)) |
| 1853 | BinaryOperator *FalseBO; |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1854 | if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && |
| 1855 | canMergeSelectThroughBinop(FalseBO)) { |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1856 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) { |
| 1857 | if (FalseBOSI->getCondition() == CondVal) { |
| 1858 | FalseBO->setOperand(0, FalseBOSI->getFalseValue()); |
| 1859 | Worklist.Add(FalseBO); |
| 1860 | return &SI; |
| 1861 | } |
| 1862 | } |
| 1863 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) { |
| 1864 | if (FalseBOSI->getCondition() == CondVal) { |
| 1865 | FalseBO->setOperand(1, FalseBOSI->getFalseValue()); |
| 1866 | Worklist.Add(FalseBO); |
| 1867 | return &SI; |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1872 | if (BinaryOperator::isNot(CondVal)) { |
| 1873 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 1874 | SI.setOperand(1, FalseVal); |
| 1875 | SI.setOperand(2, TrueVal); |
| 1876 | return &SI; |
| 1877 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1878 | |
Sanjay Patel | 4e463b4 | 2016-09-06 18:16:31 +0000 | [diff] [blame] | 1879 | if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) { |
Pete Cooper | abc13af | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 1880 | unsigned VWidth = VecTy->getNumElements(); |
| 1881 | APInt UndefElts(VWidth, 0); |
| 1882 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 1883 | if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) { |
| 1884 | if (V != &SI) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1885 | return replaceInstUsesWith(SI, V); |
Pete Cooper | abc13af | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 1886 | return &SI; |
| 1887 | } |
| 1888 | } |
| 1889 | |
Chad Rosier | cd62bf5 | 2016-04-29 21:12:31 +0000 | [diff] [blame] | 1890 | // See if we can determine the result of this select based on a dominating |
| 1891 | // condition. |
| 1892 | BasicBlock *Parent = SI.getParent(); |
| 1893 | if (BasicBlock *Dom = Parent->getSinglePredecessor()) { |
| 1894 | auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator()); |
| 1895 | if (PBI && PBI->isConditional() && |
| 1896 | PBI->getSuccessor(0) != PBI->getSuccessor(1) && |
| 1897 | (PBI->getSuccessor(0) == Parent || PBI->getSuccessor(1) == Parent)) { |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 1898 | bool CondIsTrue = PBI->getSuccessor(0) == Parent; |
Chad Rosier | cd62bf5 | 2016-04-29 21:12:31 +0000 | [diff] [blame] | 1899 | Optional<bool> Implication = isImpliedCondition( |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 1900 | PBI->getCondition(), SI.getCondition(), DL, CondIsTrue); |
Chad Rosier | cd62bf5 | 2016-04-29 21:12:31 +0000 | [diff] [blame] | 1901 | if (Implication) { |
| 1902 | Value *V = *Implication ? TrueVal : FalseVal; |
| 1903 | return replaceInstUsesWith(SI, V); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 1908 | // If we can compute the condition, there's no need for a select. |
| 1909 | // Like the above fold, we are attempting to reduce compile-time cost by |
| 1910 | // putting this fold here with limitations rather than in InstSimplify. |
| 1911 | // The motivation for this call into value tracking is to take advantage of |
| 1912 | // the assumption cache, so make sure that is populated. |
| 1913 | if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1914 | KnownBits Known(1); |
| 1915 | computeKnownBits(CondVal, Known, 0, &SI); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1916 | if (Known.One.isOneValue()) |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 1917 | return replaceInstUsesWith(SI, TrueVal); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1918 | if (Known.Zero.isOneValue()) |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 1919 | return replaceInstUsesWith(SI, FalseVal); |
| 1920 | } |
| 1921 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1922 | if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder)) |
Sanjay Patel | 978f827 | 2016-10-29 15:22:04 +0000 | [diff] [blame] | 1923 | return BitCastSel; |
| 1924 | |
Matthew Simpson | b6915fb | 2017-10-31 12:34:02 +0000 | [diff] [blame] | 1925 | // Simplify selects that test the returned flag of cmpxchg instructions. |
| 1926 | if (Instruction *Select = foldSelectCmpXchg(SI)) |
| 1927 | return Select; |
| 1928 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1929 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1930 | } |