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