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