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