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