Chris Lattner | 1c22c80 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 1 | //===- InstCombineSelect.cpp ----------------------------------------------===// |
Chris Lattner | c6334b9 | 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 | 1c22c80 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 10 | // This file implements the visitSelect function. |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 15 | #include "llvm/Support/PatternMatch.h" |
Eli Friedman | 7470325 | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace PatternMatch; |
| 20 | |
| 21 | /// MatchSelectPattern - Pattern match integer [SU]MIN, [SU]MAX, and ABS idioms, |
| 22 | /// returning the kind and providing the out parameter results if we |
| 23 | /// successfully match. |
| 24 | static SelectPatternFlavor |
| 25 | MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS) { |
| 26 | SelectInst *SI = dyn_cast<SelectInst>(V); |
| 27 | if (SI == 0) return SPF_UNKNOWN; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 28 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 29 | ICmpInst *ICI = dyn_cast<ICmpInst>(SI->getCondition()); |
| 30 | if (ICI == 0) return SPF_UNKNOWN; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 31 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 32 | LHS = ICI->getOperand(0); |
| 33 | RHS = ICI->getOperand(1); |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 34 | |
| 35 | // (icmp X, Y) ? X : Y |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 36 | if (SI->getTrueValue() == ICI->getOperand(0) && |
| 37 | SI->getFalseValue() == ICI->getOperand(1)) { |
| 38 | switch (ICI->getPredicate()) { |
| 39 | default: return SPF_UNKNOWN; // Equality. |
| 40 | case ICmpInst::ICMP_UGT: |
| 41 | case ICmpInst::ICMP_UGE: return SPF_UMAX; |
| 42 | case ICmpInst::ICMP_SGT: |
| 43 | case ICmpInst::ICMP_SGE: return SPF_SMAX; |
| 44 | case ICmpInst::ICMP_ULT: |
| 45 | case ICmpInst::ICMP_ULE: return SPF_UMIN; |
| 46 | case ICmpInst::ICMP_SLT: |
| 47 | case ICmpInst::ICMP_SLE: return SPF_SMIN; |
| 48 | } |
| 49 | } |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 50 | |
| 51 | // (icmp X, Y) ? Y : X |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 52 | if (SI->getTrueValue() == ICI->getOperand(1) && |
| 53 | SI->getFalseValue() == ICI->getOperand(0)) { |
| 54 | switch (ICI->getPredicate()) { |
| 55 | default: return SPF_UNKNOWN; // Equality. |
| 56 | case ICmpInst::ICMP_UGT: |
| 57 | case ICmpInst::ICMP_UGE: return SPF_UMIN; |
| 58 | case ICmpInst::ICMP_SGT: |
| 59 | case ICmpInst::ICMP_SGE: return SPF_SMIN; |
| 60 | case ICmpInst::ICMP_ULT: |
| 61 | case ICmpInst::ICMP_ULE: return SPF_UMAX; |
| 62 | case ICmpInst::ICMP_SLT: |
| 63 | case ICmpInst::ICMP_SLE: return SPF_SMAX; |
| 64 | } |
| 65 | } |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 66 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 67 | // TODO: (X > 4) ? X : 5 --> (X >= 5) ? X : 5 --> MAX(X, 5) |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 68 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 69 | return SPF_UNKNOWN; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 74 | /// %C = or %A, %B |
| 75 | /// %D = select %cond, %C, %A |
| 76 | /// into: |
| 77 | /// %C = select %cond, %B, 0 |
| 78 | /// %D = or %A, %C |
| 79 | /// |
| 80 | /// Assuming that the specified instruction is an operand to the select, return |
| 81 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 82 | /// equal the other incoming value of the select. |
| 83 | /// |
| 84 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 85 | switch (I->getOpcode()) { |
| 86 | case Instruction::Add: |
| 87 | case Instruction::Mul: |
| 88 | case Instruction::And: |
| 89 | case Instruction::Or: |
| 90 | case Instruction::Xor: |
| 91 | return 3; // Can fold through either operand. |
| 92 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 93 | case Instruction::Shl: // Can only fold on the shift amount. |
| 94 | case Instruction::LShr: |
| 95 | case Instruction::AShr: |
| 96 | return 1; |
| 97 | default: |
| 98 | return 0; // Cannot fold |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 103 | /// function, return the identity constant that goes into the select. |
| 104 | static Constant *GetSelectFoldableConstant(Instruction *I) { |
| 105 | switch (I->getOpcode()) { |
| 106 | default: llvm_unreachable("This cannot happen!"); |
| 107 | case Instruction::Add: |
| 108 | case Instruction::Sub: |
| 109 | case Instruction::Or: |
| 110 | case Instruction::Xor: |
| 111 | case Instruction::Shl: |
| 112 | case Instruction::LShr: |
| 113 | case Instruction::AShr: |
| 114 | return Constant::getNullValue(I->getType()); |
| 115 | case Instruction::And: |
| 116 | return Constant::getAllOnesValue(I->getType()); |
| 117 | case Instruction::Mul: |
| 118 | return ConstantInt::get(I->getType(), 1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 123 | /// have the same opcode and only one use each. Try to simplify this. |
| 124 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 125 | Instruction *FI) { |
| 126 | if (TI->getNumOperands() == 1) { |
| 127 | // If this is a non-volatile load or a cast from the same type, |
| 128 | // merge. |
| 129 | if (TI->isCast()) { |
| 130 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 131 | return 0; |
Nadav Rotem | 2f6622c | 2012-06-07 20:28:57 +0000 | [diff] [blame] | 132 | // The select condition may be a vector. We may only change the operand |
| 133 | // type if the vector width remains the same (and matches the condition). |
| 134 | Type *CondTy = SI.getCondition()->getType(); |
| 135 | if (CondTy->isVectorTy() && CondTy->getVectorNumElements() != |
| 136 | FI->getOperand(0)->getType()->getVectorNumElements()) |
| 137 | return 0; |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 138 | } else { |
| 139 | return 0; // unknown unary op. |
| 140 | } |
| 141 | |
| 142 | // Fold this by inserting a select from the input values. |
Eli Friedman | 976e7e1 | 2011-05-18 18:10:28 +0000 | [diff] [blame] | 143 | Value *NewSI = Builder->CreateSelect(SI.getCondition(), TI->getOperand(0), |
| 144 | FI->getOperand(0), SI.getName()+".v"); |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 145 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 146 | TI->getType()); |
| 147 | } |
| 148 | |
| 149 | // Only handle binary operators here. |
| 150 | if (!isa<BinaryOperator>(TI)) |
| 151 | return 0; |
| 152 | |
| 153 | // Figure out if the operations have any operands in common. |
| 154 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 155 | bool MatchIsOpZero; |
| 156 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 157 | MatchOp = TI->getOperand(0); |
| 158 | OtherOpT = TI->getOperand(1); |
| 159 | OtherOpF = FI->getOperand(1); |
| 160 | MatchIsOpZero = true; |
| 161 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 162 | MatchOp = TI->getOperand(1); |
| 163 | OtherOpT = TI->getOperand(0); |
| 164 | OtherOpF = FI->getOperand(0); |
| 165 | MatchIsOpZero = false; |
| 166 | } else if (!TI->isCommutative()) { |
| 167 | return 0; |
| 168 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 169 | MatchOp = TI->getOperand(0); |
| 170 | OtherOpT = TI->getOperand(1); |
| 171 | OtherOpF = FI->getOperand(0); |
| 172 | MatchIsOpZero = true; |
| 173 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 174 | MatchOp = TI->getOperand(1); |
| 175 | OtherOpT = TI->getOperand(0); |
| 176 | OtherOpF = FI->getOperand(1); |
| 177 | MatchIsOpZero = true; |
| 178 | } else { |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | // If we reach here, they do have operations in common. |
Eli Friedman | 976e7e1 | 2011-05-18 18:10:28 +0000 | [diff] [blame] | 183 | Value *NewSI = Builder->CreateSelect(SI.getCondition(), OtherOpT, |
| 184 | OtherOpF, SI.getName()+".v"); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 185 | |
| 186 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 187 | if (MatchIsOpZero) |
| 188 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
| 189 | else |
| 190 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
| 191 | } |
| 192 | llvm_unreachable("Shouldn't get here"); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static bool isSelect01(Constant *C1, Constant *C2) { |
| 196 | ConstantInt *C1I = dyn_cast<ConstantInt>(C1); |
| 197 | if (!C1I) |
| 198 | return false; |
| 199 | ConstantInt *C2I = dyn_cast<ConstantInt>(C2); |
| 200 | if (!C2I) |
| 201 | return false; |
Benjamin Kramer | 4ac1947 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 202 | if (!C1I->isZero() && !C2I->isZero()) // One side must be zero. |
| 203 | return false; |
| 204 | return C1I->isOne() || C1I->isAllOnesValue() || |
| 205 | C2I->isOne() || C2I->isAllOnesValue(); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /// FoldSelectIntoOp - Try fold the select into one of the operands to |
| 209 | /// facilitate further optimization. |
| 210 | Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
| 211 | Value *FalseVal) { |
| 212 | // See the comment above GetSelectFoldableOperands for a description of the |
| 213 | // transformation we are doing here. |
| 214 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) { |
| 215 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 216 | !isa<Constant>(FalseVal)) { |
| 217 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 218 | unsigned OpToFold = 0; |
| 219 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 220 | OpToFold = 1; |
Nick Lewycky | 675619c | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 221 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 222 | OpToFold = 2; |
| 223 | } |
| 224 | |
| 225 | if (OpToFold) { |
| 226 | Constant *C = GetSelectFoldableConstant(TVI); |
| 227 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 228 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 4ac1947 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 229 | // between 0, 1 and -1. |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 230 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
Eli Friedman | 976e7e1 | 2011-05-18 18:10:28 +0000 | [diff] [blame] | 231 | Value *NewSel = Builder->CreateSelect(SI.getCondition(), OOp, C); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 232 | NewSel->takeName(TVI); |
Nick Lewycky | 2bf026e | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 233 | BinaryOperator *TVI_BO = cast<BinaryOperator>(TVI); |
Nick Lewycky | 675619c | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 234 | BinaryOperator *BO = BinaryOperator::Create(TVI_BO->getOpcode(), |
| 235 | FalseVal, NewSel); |
Nick Lewycky | 2bf026e | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 236 | if (isa<PossiblyExactOperator>(BO)) |
| 237 | BO->setIsExact(TVI_BO->isExact()); |
| 238 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 239 | BO->setHasNoUnsignedWrap(TVI_BO->hasNoUnsignedWrap()); |
| 240 | BO->setHasNoSignedWrap(TVI_BO->hasNoSignedWrap()); |
| 241 | } |
| 242 | return BO; |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) { |
| 250 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 251 | !isa<Constant>(TrueVal)) { |
| 252 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 253 | unsigned OpToFold = 0; |
| 254 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 255 | OpToFold = 1; |
Nick Lewycky | 675619c | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 256 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 257 | OpToFold = 2; |
| 258 | } |
| 259 | |
| 260 | if (OpToFold) { |
| 261 | Constant *C = GetSelectFoldableConstant(FVI); |
| 262 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 263 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 4ac1947 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 264 | // between 0, 1 and -1. |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 265 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
Eli Friedman | 976e7e1 | 2011-05-18 18:10:28 +0000 | [diff] [blame] | 266 | Value *NewSel = Builder->CreateSelect(SI.getCondition(), C, OOp); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 267 | NewSel->takeName(FVI); |
Nick Lewycky | 675619c | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 268 | BinaryOperator *FVI_BO = cast<BinaryOperator>(FVI); |
| 269 | BinaryOperator *BO = BinaryOperator::Create(FVI_BO->getOpcode(), |
| 270 | TrueVal, NewSel); |
Nick Lewycky | 2bf026e | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 271 | if (isa<PossiblyExactOperator>(BO)) |
| 272 | BO->setIsExact(FVI_BO->isExact()); |
| 273 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 274 | BO->setHasNoUnsignedWrap(FVI_BO->hasNoUnsignedWrap()); |
| 275 | BO->setHasNoSignedWrap(FVI_BO->hasNoSignedWrap()); |
| 276 | } |
| 277 | return BO; |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 287 | /// SimplifyWithOpReplaced - See if V simplifies when its operand Op is |
| 288 | /// replaced with RepOp. |
| 289 | static Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 290 | const DataLayout *TD, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 291 | const TargetLibraryInfo *TLI) { |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 292 | // Trivial replacement. |
| 293 | if (V == Op) |
| 294 | return RepOp; |
| 295 | |
| 296 | Instruction *I = dyn_cast<Instruction>(V); |
| 297 | if (!I) |
| 298 | return 0; |
| 299 | |
| 300 | // If this is a binary operator, try to simplify it with the replaced op. |
| 301 | if (BinaryOperator *B = dyn_cast<BinaryOperator>(I)) { |
| 302 | if (B->getOperand(0) == Op) |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 303 | return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1), TD, TLI); |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 304 | if (B->getOperand(1) == Op) |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 305 | return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp, TD, TLI); |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Benjamin Kramer | 2c5cc68 | 2011-05-28 10:16:58 +0000 | [diff] [blame] | 308 | // Same for CmpInsts. |
| 309 | if (CmpInst *C = dyn_cast<CmpInst>(I)) { |
| 310 | if (C->getOperand(0) == Op) |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 311 | return SimplifyCmpInst(C->getPredicate(), RepOp, C->getOperand(1), TD, |
| 312 | TLI); |
Benjamin Kramer | 2c5cc68 | 2011-05-28 10:16:58 +0000 | [diff] [blame] | 313 | if (C->getOperand(1) == Op) |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 314 | return SimplifyCmpInst(C->getPredicate(), C->getOperand(0), RepOp, TD, |
| 315 | TLI); |
Benjamin Kramer | 2c5cc68 | 2011-05-28 10:16:58 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // TODO: We could hand off more cases to instsimplify here. |
| 319 | |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 320 | // If all operands are constant after substituting Op for RepOp then we can |
| 321 | // constant fold the instruction. |
| 322 | if (Constant *CRepOp = dyn_cast<Constant>(RepOp)) { |
| 323 | // Build a list of all constant operands. |
| 324 | SmallVector<Constant*, 8> ConstOps; |
| 325 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 326 | if (I->getOperand(i) == Op) |
| 327 | ConstOps.push_back(CRepOp); |
| 328 | else if (Constant *COp = dyn_cast<Constant>(I->getOperand(i))) |
| 329 | ConstOps.push_back(COp); |
| 330 | else |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | // All operands were constants, fold it. |
Nick Lewycky | 267236a | 2011-10-02 09:12:55 +0000 | [diff] [blame] | 335 | if (ConstOps.size() == I->getNumOperands()) { |
Benjamin Kramer | 82a1833 | 2012-10-20 08:43:52 +0000 | [diff] [blame] | 336 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 337 | return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], |
| 338 | ConstOps[1], TD, TLI); |
| 339 | |
Nick Lewycky | 267236a | 2011-10-02 09:12:55 +0000 | [diff] [blame] | 340 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 341 | if (!LI->isVolatile()) |
| 342 | return ConstantFoldLoadFromConstPtr(ConstOps[0], TD); |
| 343 | |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 344 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 345 | ConstOps, TD, TLI); |
Nick Lewycky | 267236a | 2011-10-02 09:12:55 +0000 | [diff] [blame] | 346 | } |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 352 | /// visitSelectInstWithICmp - Visit a SelectInst that has an |
| 353 | /// ICmpInst as its first operand. |
| 354 | /// |
| 355 | Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, |
| 356 | ICmpInst *ICI) { |
| 357 | bool Changed = false; |
| 358 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 359 | Value *CmpLHS = ICI->getOperand(0); |
| 360 | Value *CmpRHS = ICI->getOperand(1); |
| 361 | Value *TrueVal = SI.getTrueValue(); |
| 362 | Value *FalseVal = SI.getFalseValue(); |
| 363 | |
| 364 | // Check cases where the comparison is with a constant that |
Tobias Grosser | aa2be84 | 2011-01-09 16:00:11 +0000 | [diff] [blame] | 365 | // can be adjusted to fit the min/max idiom. We may move or edit ICI |
| 366 | // here, so make sure the select is the only user. |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 367 | if (ICI->hasOneUse()) |
| 368 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) { |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 369 | // X < MIN ? T : F --> F |
| 370 | if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT) |
| 371 | && CI->isMinValue(Pred == ICmpInst::ICMP_SLT)) |
| 372 | return ReplaceInstUsesWith(SI, FalseVal); |
| 373 | // X > MAX ? T : F --> F |
| 374 | else if ((Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_UGT) |
| 375 | && CI->isMaxValue(Pred == ICmpInst::ICMP_SGT)) |
| 376 | return ReplaceInstUsesWith(SI, FalseVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 377 | switch (Pred) { |
| 378 | default: break; |
| 379 | case ICmpInst::ICMP_ULT: |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 380 | case ICmpInst::ICMP_SLT: |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 381 | case ICmpInst::ICMP_UGT: |
| 382 | case ICmpInst::ICMP_SGT: { |
Frits van Bommel | b686eb9 | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 383 | // These transformations only work for selects over integers. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 384 | IntegerType *SelectTy = dyn_cast<IntegerType>(SI.getType()); |
Frits van Bommel | b686eb9 | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 385 | if (!SelectTy) |
| 386 | break; |
| 387 | |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 388 | Constant *AdjustedRHS; |
| 389 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) |
| 390 | AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1); |
| 391 | else // (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) |
| 392 | AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1); |
| 393 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 394 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 395 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 396 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 397 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) |
| 398 | ; // Nothing to do here. Values match without any sign/zero extension. |
| 399 | |
| 400 | // Types do not match. Instead of calculating this with mixed types |
| 401 | // promote all to the larger type. This enables scalar evolution to |
| 402 | // analyze this expression. |
| 403 | else if (CmpRHS->getType()->getScalarSizeInBits() |
Frits van Bommel | b686eb9 | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 404 | < SelectTy->getBitWidth()) { |
| 405 | Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy); |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 406 | |
| 407 | // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X |
| 408 | // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X |
| 409 | // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X |
| 410 | // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X |
| 411 | if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && |
| 412 | sextRHS == FalseVal) { |
| 413 | CmpLHS = TrueVal; |
| 414 | AdjustedRHS = sextRHS; |
| 415 | } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) && |
| 416 | sextRHS == TrueVal) { |
| 417 | CmpLHS = FalseVal; |
| 418 | AdjustedRHS = sextRHS; |
| 419 | } else if (ICI->isUnsigned()) { |
Frits van Bommel | b686eb9 | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 420 | Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy); |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 421 | // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X |
| 422 | // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X |
| 423 | // zext + signed compare cannot be changed: |
| 424 | // 0xff <s 0x00, but 0x00ff >s 0x0000 |
| 425 | if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && |
| 426 | zextRHS == FalseVal) { |
| 427 | CmpLHS = TrueVal; |
| 428 | AdjustedRHS = zextRHS; |
| 429 | } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) && |
| 430 | zextRHS == TrueVal) { |
| 431 | CmpLHS = FalseVal; |
| 432 | AdjustedRHS = zextRHS; |
| 433 | } else |
| 434 | break; |
| 435 | } else |
| 436 | break; |
| 437 | } else |
| 438 | break; |
| 439 | |
| 440 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 441 | CmpRHS = AdjustedRHS; |
| 442 | std::swap(FalseVal, TrueVal); |
| 443 | ICI->setPredicate(Pred); |
| 444 | ICI->setOperand(0, CmpLHS); |
| 445 | ICI->setOperand(1, CmpRHS); |
| 446 | SI.setOperand(1, TrueVal); |
| 447 | SI.setOperand(2, FalseVal); |
Tobias Grosser | aa2be84 | 2011-01-09 16:00:11 +0000 | [diff] [blame] | 448 | |
| 449 | // Move ICI instruction right before the select instruction. Otherwise |
| 450 | // the sext/zext value may be defined after the ICI instruction uses it. |
| 451 | ICI->moveBefore(&SI); |
| 452 | |
Tobias Grosser | 46431d7 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 453 | Changed = true; |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | } |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Benjamin Kramer | 1db071f | 2010-07-08 11:39:10 +0000 | [diff] [blame] | 459 | // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1 |
| 460 | // and (X <s 0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1 |
| 461 | // FIXME: Type and constness constraints could be lifted, but we have to |
| 462 | // watch code size carefully. We should consider xor instead of |
| 463 | // sub/add when we decide to do that. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 464 | if (IntegerType *Ty = dyn_cast<IntegerType>(CmpLHS->getType())) { |
Benjamin Kramer | 1db071f | 2010-07-08 11:39:10 +0000 | [diff] [blame] | 465 | if (TrueVal->getType() == Ty) { |
| 466 | if (ConstantInt *Cmp = dyn_cast<ConstantInt>(CmpRHS)) { |
| 467 | ConstantInt *C1 = NULL, *C2 = NULL; |
| 468 | if (Pred == ICmpInst::ICMP_SGT && Cmp->isAllOnesValue()) { |
| 469 | C1 = dyn_cast<ConstantInt>(TrueVal); |
| 470 | C2 = dyn_cast<ConstantInt>(FalseVal); |
| 471 | } else if (Pred == ICmpInst::ICMP_SLT && Cmp->isNullValue()) { |
| 472 | C1 = dyn_cast<ConstantInt>(FalseVal); |
| 473 | C2 = dyn_cast<ConstantInt>(TrueVal); |
| 474 | } |
| 475 | if (C1 && C2) { |
| 476 | // This shift results in either -1 or 0. |
| 477 | Value *AShr = Builder->CreateAShr(CmpLHS, Ty->getBitWidth()-1); |
| 478 | |
| 479 | // Check if we can express the operation with a single or. |
| 480 | if (C2->isAllOnesValue()) |
| 481 | return ReplaceInstUsesWith(SI, Builder->CreateOr(AShr, C1)); |
| 482 | |
| 483 | Value *And = Builder->CreateAnd(AShr, C2->getValue()-C1->getValue()); |
| 484 | return ReplaceInstUsesWith(SI, Builder->CreateAdd(And, C1)); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 490 | // If we have an equality comparison then we know the value in one of the |
| 491 | // arms of the select. See if substituting this value into the arm and |
| 492 | // simplifying the result yields the same value as the other arm. |
| 493 | if (Pred == ICmpInst::ICMP_EQ) { |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 494 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, TD, TLI) == TrueVal || |
| 495 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, TD, TLI) == TrueVal) |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 496 | return ReplaceInstUsesWith(SI, FalseVal); |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 497 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, TD, TLI) == FalseVal || |
| 498 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, TD, TLI) == FalseVal) |
Nick Lewycky | 11357d4 | 2011-10-02 10:37:37 +0000 | [diff] [blame] | 499 | return ReplaceInstUsesWith(SI, FalseVal); |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 500 | } else if (Pred == ICmpInst::ICMP_NE) { |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 501 | if (SimplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, TD, TLI) == FalseVal || |
| 502 | SimplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, TD, TLI) == FalseVal) |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 503 | return ReplaceInstUsesWith(SI, TrueVal); |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 504 | if (SimplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, TD, TLI) == TrueVal || |
| 505 | SimplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, TD, TLI) == TrueVal) |
Nick Lewycky | 11357d4 | 2011-10-02 10:37:37 +0000 | [diff] [blame] | 506 | return ReplaceInstUsesWith(SI, TrueVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 507 | } |
Nick Lewycky | 98cd750 | 2011-03-27 07:30:57 +0000 | [diff] [blame] | 508 | |
Benjamin Kramer | 17c1bb5 | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 509 | // NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 510 | |
Benjamin Kramer | 37fa1c8 | 2012-05-28 19:18:16 +0000 | [diff] [blame] | 511 | if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) { |
Nick Lewycky | 98cd750 | 2011-03-27 07:30:57 +0000 | [diff] [blame] | 512 | if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) { |
| 513 | // Transform (X == C) ? X : Y -> (X == C) ? C : Y |
| 514 | SI.setOperand(1, CmpRHS); |
| 515 | Changed = true; |
| 516 | } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) { |
| 517 | // Transform (X != C) ? Y : X -> (X != C) ? Y : C |
| 518 | SI.setOperand(2, CmpRHS); |
| 519 | Changed = true; |
| 520 | } |
| 521 | } |
| 522 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 523 | return Changed ? &SI : 0; |
| 524 | } |
| 525 | |
| 526 | |
| 527 | /// CanSelectOperandBeMappingIntoPredBlock - SI is a select whose condition is a |
| 528 | /// PHI node (but the two may be in different blocks). See if the true/false |
| 529 | /// values (V) are live in all of the predecessor blocks of the PHI. For |
| 530 | /// example, cases like this cannot be mapped: |
| 531 | /// |
| 532 | /// X = phi [ C1, BB1], [C2, BB2] |
| 533 | /// Y = add |
| 534 | /// Z = select X, Y, 0 |
| 535 | /// |
| 536 | /// because Y is not live in BB1/BB2. |
| 537 | /// |
| 538 | static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V, |
| 539 | const SelectInst &SI) { |
| 540 | // If the value is a non-instruction value like a constant or argument, it |
| 541 | // can always be mapped. |
| 542 | const Instruction *I = dyn_cast<Instruction>(V); |
| 543 | if (I == 0) return true; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 544 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 545 | // If V is a PHI node defined in the same block as the condition PHI, we can |
| 546 | // map the arguments. |
| 547 | const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 548 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 549 | if (const PHINode *VP = dyn_cast<PHINode>(I)) |
| 550 | if (VP->getParent() == CondPHI->getParent()) |
| 551 | return true; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 552 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 553 | // Otherwise, if the PHI and select are defined in the same block and if V is |
| 554 | // defined in a different block, then we can transform it. |
| 555 | if (SI.getParent() == CondPHI->getParent() && |
| 556 | I->getParent() != CondPHI->getParent()) |
| 557 | return true; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 558 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 559 | // Otherwise we have a 'hard' case and we can't tell without doing more |
| 560 | // detailed dominator based analysis, punt. |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | /// FoldSPFofSPF - We have an SPF (e.g. a min or max) of an SPF of the form: |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 565 | /// SPF2(SPF1(A, B), C) |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 566 | Instruction *InstCombiner::FoldSPFofSPF(Instruction *Inner, |
| 567 | SelectPatternFlavor SPF1, |
| 568 | Value *A, Value *B, |
| 569 | Instruction &Outer, |
| 570 | SelectPatternFlavor SPF2, Value *C) { |
| 571 | if (C == A || C == B) { |
| 572 | // MAX(MAX(A, B), B) -> MAX(A, B) |
| 573 | // MIN(MIN(a, b), a) -> MIN(a, b) |
| 574 | if (SPF1 == SPF2) |
| 575 | return ReplaceInstUsesWith(Outer, Inner); |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 576 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 577 | // MAX(MIN(a, b), a) -> a |
| 578 | // MIN(MAX(a, b), a) -> a |
| 579 | if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || |
| 580 | (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || |
| 581 | (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || |
| 582 | (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) |
| 583 | return ReplaceInstUsesWith(Outer, C); |
| 584 | } |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 585 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 586 | // TODO: MIN(MIN(A, 23), 97) |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | |
Benjamin Kramer | 20e3b4b | 2010-12-11 09:42:59 +0000 | [diff] [blame] | 591 | /// foldSelectICmpAnd - If one of the constants is zero (we know they can't |
| 592 | /// both be) and we have an icmp instruction with zero, and we have an 'and' |
| 593 | /// with the non-constant value and a power of two we can turn the select |
| 594 | /// into a shift on the result of the 'and'. |
| 595 | static Value *foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal, |
| 596 | ConstantInt *FalseVal, |
| 597 | InstCombiner::BuilderTy *Builder) { |
| 598 | const ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition()); |
| 599 | if (!IC || !IC->isEquality()) |
| 600 | return 0; |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 601 | |
Benjamin Kramer | 6b49725 | 2011-03-11 11:37:40 +0000 | [diff] [blame] | 602 | if (!match(IC->getOperand(1), m_Zero())) |
| 603 | return 0; |
Benjamin Kramer | 20e3b4b | 2010-12-11 09:42:59 +0000 | [diff] [blame] | 604 | |
| 605 | ConstantInt *AndRHS; |
| 606 | Value *LHS = IC->getOperand(0); |
| 607 | if (LHS->getType() != SI.getType() || |
| 608 | !match(LHS, m_And(m_Value(), m_ConstantInt(AndRHS)))) |
| 609 | return 0; |
| 610 | |
Benjamin Kramer | 2f7228b | 2010-12-11 10:49:22 +0000 | [diff] [blame] | 611 | // If both select arms are non-zero see if we have a select of the form |
| 612 | // 'x ? 2^n + C : C'. Then we can offset both arms by C, use the logic |
| 613 | // for 'x ? 2^n : 0' and fix the thing up at the end. |
| 614 | ConstantInt *Offset = 0; |
| 615 | if (!TrueVal->isZero() && !FalseVal->isZero()) { |
| 616 | if ((TrueVal->getValue() - FalseVal->getValue()).isPowerOf2()) |
| 617 | Offset = FalseVal; |
| 618 | else if ((FalseVal->getValue() - TrueVal->getValue()).isPowerOf2()) |
| 619 | Offset = TrueVal; |
| 620 | else |
| 621 | return 0; |
| 622 | |
| 623 | // Adjust TrueVal and FalseVal to the offset. |
| 624 | TrueVal = ConstantInt::get(Builder->getContext(), |
| 625 | TrueVal->getValue() - Offset->getValue()); |
| 626 | FalseVal = ConstantInt::get(Builder->getContext(), |
| 627 | FalseVal->getValue() - Offset->getValue()); |
| 628 | } |
Benjamin Kramer | 20e3b4b | 2010-12-11 09:42:59 +0000 | [diff] [blame] | 629 | |
| 630 | // Make sure the mask in the 'and' and one of the select arms is a power of 2. |
| 631 | if (!AndRHS->getValue().isPowerOf2() || |
| 632 | (!TrueVal->getValue().isPowerOf2() && |
| 633 | !FalseVal->getValue().isPowerOf2())) |
| 634 | return 0; |
| 635 | |
| 636 | // Determine which shift is needed to transform result of the 'and' into the |
| 637 | // desired result. |
| 638 | ConstantInt *ValC = !TrueVal->isZero() ? TrueVal : FalseVal; |
| 639 | unsigned ValZeros = ValC->getValue().logBase2(); |
| 640 | unsigned AndZeros = AndRHS->getValue().logBase2(); |
| 641 | |
| 642 | Value *V = LHS; |
| 643 | if (ValZeros > AndZeros) |
| 644 | V = Builder->CreateShl(V, ValZeros - AndZeros); |
| 645 | else if (ValZeros < AndZeros) |
| 646 | V = Builder->CreateLShr(V, AndZeros - ValZeros); |
| 647 | |
| 648 | // Okay, now we know that everything is set up, we just don't know whether we |
| 649 | // have a icmp_ne or icmp_eq and whether the true or false val is the zero. |
| 650 | bool ShouldNotVal = !TrueVal->isZero(); |
| 651 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
| 652 | if (ShouldNotVal) |
| 653 | V = Builder->CreateXor(V, ValC); |
Benjamin Kramer | 2f7228b | 2010-12-11 10:49:22 +0000 | [diff] [blame] | 654 | |
| 655 | // Apply an offset if needed. |
| 656 | if (Offset) |
| 657 | V = Builder->CreateAdd(V, Offset); |
Benjamin Kramer | 20e3b4b | 2010-12-11 09:42:59 +0000 | [diff] [blame] | 658 | return V; |
| 659 | } |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 660 | |
| 661 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
| 662 | Value *CondVal = SI.getCondition(); |
| 663 | Value *TrueVal = SI.getTrueValue(); |
| 664 | Value *FalseVal = SI.getFalseValue(); |
| 665 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 666 | if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, TD)) |
| 667 | return ReplaceInstUsesWith(SI, V); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 668 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 669 | if (SI.getType()->isIntegerTy(1)) { |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 670 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
| 671 | if (C->getZExtValue()) { |
| 672 | // Change: A = select B, true, C --> A = or B, C |
| 673 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 674 | } |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 675 | // Change: A = select B, false, C --> A = and !B, C |
Eli Friedman | e87ca45 | 2011-05-18 17:31:55 +0000 | [diff] [blame] | 676 | Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName()); |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 677 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 678 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
| 679 | if (C->getZExtValue() == false) { |
| 680 | // Change: A = select B, C, false --> A = and B, C |
| 681 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 682 | } |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 683 | // Change: A = select B, C, true --> A = or !B, C |
Eli Friedman | e87ca45 | 2011-05-18 17:31:55 +0000 | [diff] [blame] | 684 | Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName()); |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 685 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 686 | } |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 687 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 688 | // select a, b, a -> a&b |
| 689 | // select a, a, b -> a|b |
| 690 | if (CondVal == TrueVal) |
| 691 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
| 692 | else if (CondVal == FalseVal) |
| 693 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Pete Cooper | 4e5a1ab | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 694 | |
| 695 | // select a, ~a, b -> (~a)&b |
| 696 | // select a, b, ~a -> (~a)|b |
| 697 | if (match(TrueVal, m_Not(m_Specific(CondVal)))) |
| 698 | return BinaryOperator::CreateAnd(TrueVal, FalseVal); |
| 699 | else if (match(FalseVal, m_Not(m_Specific(CondVal)))) |
| 700 | return BinaryOperator::CreateOr(TrueVal, FalseVal); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | // Selecting between two integer constants? |
| 704 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 705 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 706 | // select C, 1, 0 -> zext C to int |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 707 | if (FalseValC->isZero() && TrueValC->getValue() == 1) |
| 708 | return new ZExtInst(CondVal, SI.getType()); |
| 709 | |
| 710 | // select C, -1, 0 -> sext C to int |
| 711 | if (FalseValC->isZero() && TrueValC->isAllOnesValue()) |
| 712 | return new SExtInst(CondVal, SI.getType()); |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 713 | |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 714 | // select C, 0, 1 -> zext !C to int |
| 715 | if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
| 716 | Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName()); |
| 717 | return new ZExtInst(NotCond, SI.getType()); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 720 | // select C, 0, -1 -> sext !C to int |
| 721 | if (TrueValC->isZero() && FalseValC->isAllOnesValue()) { |
| 722 | Value *NotCond = Builder->CreateNot(CondVal, "not."+CondVal->getName()); |
| 723 | return new SExtInst(NotCond, SI.getType()); |
| 724 | } |
Benjamin Kramer | 20e3b4b | 2010-12-11 09:42:59 +0000 | [diff] [blame] | 725 | |
| 726 | if (Value *V = foldSelectICmpAnd(SI, TrueValC, FalseValC, Builder)) |
| 727 | return ReplaceInstUsesWith(SI, V); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // See if we are selecting two values based on a comparison of the two values. |
| 731 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 732 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
| 733 | // Transform (X == Y) ? X : Y -> Y |
| 734 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 735 | // This is not safe in general for floating point: |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 736 | // consider X== -0, Y== +0. |
| 737 | // It becomes safe if either operand is a nonzero constant. |
| 738 | ConstantFP *CFPt, *CFPf; |
| 739 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 740 | !CFPt->getValueAPF().isZero()) || |
| 741 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 742 | !CFPf->getValueAPF().isZero())) |
| 743 | return ReplaceInstUsesWith(SI, FalseVal); |
| 744 | } |
Dan Gohman | 21dc20c | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 745 | // Transform (X une Y) ? X : Y -> X |
| 746 | if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 747 | // This is not safe in general for floating point: |
Dan Gohman | 21dc20c | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 748 | // consider X== -0, Y== +0. |
| 749 | // It becomes safe if either operand is a nonzero constant. |
| 750 | ConstantFP *CFPt, *CFPf; |
| 751 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 752 | !CFPt->getValueAPF().isZero()) || |
| 753 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 754 | !CFPf->getValueAPF().isZero())) |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 755 | return ReplaceInstUsesWith(SI, TrueVal); |
Dan Gohman | 21dc20c | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 756 | } |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 757 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| 758 | |
| 759 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
| 760 | // Transform (X == Y) ? Y : X -> X |
| 761 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 762 | // This is not safe in general for floating point: |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 763 | // consider X== -0, Y== +0. |
| 764 | // It becomes safe if either operand is a nonzero constant. |
| 765 | ConstantFP *CFPt, *CFPf; |
| 766 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 767 | !CFPt->getValueAPF().isZero()) || |
| 768 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 769 | !CFPf->getValueAPF().isZero())) |
| 770 | return ReplaceInstUsesWith(SI, FalseVal); |
| 771 | } |
Dan Gohman | 21dc20c | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 772 | // Transform (X une Y) ? Y : X -> Y |
| 773 | if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 774 | // This is not safe in general for floating point: |
Dan Gohman | 21dc20c | 2010-02-23 17:17:57 +0000 | [diff] [blame] | 775 | // consider X== -0, Y== +0. |
| 776 | // It becomes safe if either operand is a nonzero constant. |
| 777 | ConstantFP *CFPt, *CFPf; |
| 778 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 779 | !CFPt->getValueAPF().isZero()) || |
| 780 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 781 | !CFPf->getValueAPF().isZero())) |
| 782 | return ReplaceInstUsesWith(SI, TrueVal); |
| 783 | } |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 784 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| 785 | } |
| 786 | // NOTE: if we wanted to, this is where to detect ABS |
| 787 | } |
| 788 | |
| 789 | // See if we are selecting two values based on a comparison of the two values. |
| 790 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
| 791 | if (Instruction *Result = visitSelectInstWithICmp(SI, ICI)) |
| 792 | return Result; |
| 793 | |
| 794 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 795 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 796 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 797 | Instruction *AddOp = 0, *SubOp = 0; |
| 798 | |
| 799 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 800 | if (TI->getOpcode() == FI->getOpcode()) |
| 801 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 802 | return IV; |
| 803 | |
| 804 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 805 | // even legal for FP. |
| 806 | if ((TI->getOpcode() == Instruction::Sub && |
| 807 | FI->getOpcode() == Instruction::Add) || |
| 808 | (TI->getOpcode() == Instruction::FSub && |
| 809 | FI->getOpcode() == Instruction::FAdd)) { |
| 810 | AddOp = FI; SubOp = TI; |
| 811 | } else if ((FI->getOpcode() == Instruction::Sub && |
| 812 | TI->getOpcode() == Instruction::Add) || |
| 813 | (FI->getOpcode() == Instruction::FSub && |
| 814 | TI->getOpcode() == Instruction::FAdd)) { |
| 815 | AddOp = TI; SubOp = FI; |
| 816 | } |
| 817 | |
| 818 | if (AddOp) { |
| 819 | Value *OtherAddOp = 0; |
| 820 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 821 | OtherAddOp = AddOp->getOperand(1); |
| 822 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 823 | OtherAddOp = AddOp->getOperand(0); |
| 824 | } |
| 825 | |
| 826 | if (OtherAddOp) { |
| 827 | // So at this point we know we have (Y -> OtherAddOp): |
| 828 | // select C, (add X, Y), (sub X, Z) |
| 829 | Value *NegVal; // Compute -Z |
Eli Friedman | 00805fa | 2011-06-23 20:40:23 +0000 | [diff] [blame] | 830 | if (SI.getType()->isFPOrFPVectorTy()) { |
Eli Friedman | 1eca76a | 2011-05-18 17:58:37 +0000 | [diff] [blame] | 831 | NegVal = Builder->CreateFNeg(SubOp->getOperand(1)); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 832 | } else { |
Eli Friedman | 1eca76a | 2011-05-18 17:58:37 +0000 | [diff] [blame] | 833 | NegVal = Builder->CreateNeg(SubOp->getOperand(1)); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | Value *NewTrueOp = OtherAddOp; |
| 837 | Value *NewFalseOp = NegVal; |
| 838 | if (AddOp != TI) |
| 839 | std::swap(NewTrueOp, NewFalseOp); |
Eli Friedman | 1eca76a | 2011-05-18 17:58:37 +0000 | [diff] [blame] | 840 | Value *NewSel = |
| 841 | Builder->CreateSelect(CondVal, NewTrueOp, |
| 842 | NewFalseOp, SI.getName() + ".p"); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 843 | |
Eli Friedman | 00805fa | 2011-06-23 20:40:23 +0000 | [diff] [blame] | 844 | if (SI.getType()->isFPOrFPVectorTy()) |
Dale Johannesen | f514f52 | 2010-10-27 23:45:18 +0000 | [diff] [blame] | 845 | return BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel); |
| 846 | else |
| 847 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | // See if we can fold the select into one of our operands. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 853 | if (SI.getType()->isIntegerTy()) { |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 854 | if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal)) |
| 855 | return FoldI; |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 856 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 857 | // MAX(MAX(a, b), a) -> MAX(a, b) |
| 858 | // MIN(MIN(a, b), a) -> MIN(a, b) |
| 859 | // MAX(MIN(a, b), a) -> a |
| 860 | // MIN(MAX(a, b), a) -> a |
| 861 | Value *LHS, *RHS, *LHS2, *RHS2; |
| 862 | if (SelectPatternFlavor SPF = MatchSelectPattern(&SI, LHS, RHS)) { |
| 863 | if (SelectPatternFlavor SPF2 = MatchSelectPattern(LHS, LHS2, RHS2)) |
| 864 | if (Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2, |
| 865 | SI, SPF, RHS)) |
| 866 | return R; |
| 867 | if (SelectPatternFlavor SPF2 = MatchSelectPattern(RHS, LHS2, RHS2)) |
| 868 | if (Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2, |
| 869 | SI, SPF, LHS)) |
| 870 | return R; |
| 871 | } |
| 872 | |
| 873 | // TODO. |
| 874 | // ABS(-X) -> ABS(X) |
| 875 | // ABS(ABS(X)) -> ABS(X) |
| 876 | } |
| 877 | |
| 878 | // See if we can fold the select into a phi node if the condition is a select. |
Tobias Grosser | 8d088bd | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 879 | if (isa<PHINode>(SI.getCondition())) |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 880 | // The true/false values have to be live in the PHI predecessor's blocks. |
| 881 | if (CanSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && |
| 882 | CanSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) |
| 883 | if (Instruction *NV = FoldOpIntoPhi(SI)) |
| 884 | return NV; |
| 885 | |
Nick Lewycky | df3bfae | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 886 | if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) { |
| 887 | if (TrueSI->getCondition() == CondVal) { |
Nuno Lopes | 75564e3 | 2012-07-27 18:03:57 +0000 | [diff] [blame] | 888 | if (SI.getTrueValue() == TrueSI->getTrueValue()) |
| 889 | return 0; |
Nick Lewycky | df3bfae | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 890 | SI.setOperand(1, TrueSI->getTrueValue()); |
| 891 | return &SI; |
| 892 | } |
| 893 | } |
| 894 | if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) { |
| 895 | if (FalseSI->getCondition() == CondVal) { |
Nuno Lopes | 75564e3 | 2012-07-27 18:03:57 +0000 | [diff] [blame] | 896 | if (SI.getFalseValue() == FalseSI->getFalseValue()) |
| 897 | return 0; |
Nick Lewycky | df3bfae | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 898 | SI.setOperand(2, FalseSI->getFalseValue()); |
| 899 | return &SI; |
| 900 | } |
| 901 | } |
| 902 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 903 | if (BinaryOperator::isNot(CondVal)) { |
| 904 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 905 | SI.setOperand(1, FalseVal); |
| 906 | SI.setOperand(2, TrueVal); |
| 907 | return &SI; |
| 908 | } |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 909 | |
Nick Lewycky | 466e0f3 | 2012-09-27 08:33:56 +0000 | [diff] [blame] | 910 | if (VectorType *VecTy = dyn_cast<VectorType>(SI.getType())) { |
Pete Cooper | 7971de4 | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 911 | unsigned VWidth = VecTy->getNumElements(); |
| 912 | APInt UndefElts(VWidth, 0); |
| 913 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 914 | if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) { |
| 915 | if (V != &SI) |
| 916 | return ReplaceInstUsesWith(SI, V); |
| 917 | return &SI; |
| 918 | } |
Nick Lewycky | 466e0f3 | 2012-09-27 08:33:56 +0000 | [diff] [blame] | 919 | |
| 920 | if (ConstantVector *CV = dyn_cast<ConstantVector>(CondVal)) { |
| 921 | // Form a shufflevector instruction. |
| 922 | SmallVector<Constant *, 8> Mask(VWidth); |
| 923 | Type *Int32Ty = Type::getInt32Ty(CV->getContext()); |
| 924 | for (unsigned i = 0; i != VWidth; ++i) { |
| 925 | Constant *Elem = cast<Constant>(CV->getOperand(i)); |
| 926 | if (ConstantInt *E = dyn_cast<ConstantInt>(Elem)) |
| 927 | Mask[i] = ConstantInt::get(Int32Ty, i + (E->isZero() ? VWidth : 0)); |
| 928 | else if (isa<UndefValue>(Elem)) |
| 929 | Mask[i] = UndefValue::get(Int32Ty); |
| 930 | else |
| 931 | return 0; |
| 932 | } |
| 933 | Constant *MaskVal = ConstantVector::get(Mask); |
| 934 | Value *V = Builder->CreateShuffleVector(TrueVal, FalseVal, MaskVal); |
| 935 | return ReplaceInstUsesWith(SI, V); |
| 936 | } |
Nick Lewycky | 7e0e166 | 2012-09-28 09:33:53 +0000 | [diff] [blame] | 937 | |
| 938 | if (isa<ConstantAggregateZero>(CondVal)) { |
| 939 | return ReplaceInstUsesWith(SI, FalseVal); |
| 940 | } |
Pete Cooper | 7971de4 | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Chris Lattner | c6334b9 | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 943 | return 0; |
| 944 | } |