Chris Lattner | 1e7b7b5 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 1 | //===- InstCombineSelect.cpp ----------------------------------------------===// |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 1e7b7b5 | 2010-01-05 06:05:07 +0000 | [diff] [blame] | 10 | // This file implements the visitSelect function. |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/Optional.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/Analysis/AssumptionCache.h" |
Craig Topper | 882f296 | 2017-08-16 21:52:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CmpInstAnalysis.h" |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionSimplify.h" |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 23 | #include "llvm/IR/BasicBlock.h" |
| 24 | #include "llvm/IR/Constant.h" |
| 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/IRBuilder.h" |
| 28 | #include "llvm/IR/InstrTypes.h" |
| 29 | #include "llvm/IR/Instruction.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 34 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Type.h" |
| 36 | #include "llvm/IR/User.h" |
| 37 | #include "llvm/IR/Value.h" |
| 38 | #include "llvm/Support/Casting.h" |
| 39 | #include "llvm/Support/ErrorHandling.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 40 | #include "llvm/Support/KnownBits.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" |
| 42 | #include <cassert> |
| 43 | #include <utility> |
| 44 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | using namespace PatternMatch; |
| 47 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 48 | #define DEBUG_TYPE "instcombine" |
| 49 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 50 | static Value *createMinMax(InstCombiner::BuilderTy &Builder, |
| 51 | SelectPatternFlavor SPF, Value *A, Value *B) { |
| 52 | CmpInst::Predicate Pred = getMinMaxPred(SPF); |
| 53 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer predicate"); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 54 | return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 57 | /// Replace a select operand based on an equality comparison with the identity |
| 58 | /// constant of a binop. |
Sanjay Patel | c615910 | 2018-08-27 23:01:10 +0000 | [diff] [blame] | 59 | static Instruction *foldSelectBinOpIdentity(SelectInst &Sel, |
| 60 | const TargetLibraryInfo &TLI) { |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 61 | // The select condition must be an equality compare with a constant operand. |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 62 | Value *X; |
David Bolvansky | 6737b3a | 2018-07-30 20:38:53 +0000 | [diff] [blame] | 63 | Constant *C; |
| 64 | CmpInst::Predicate Pred; |
David Bolvansky | 43b0e25 | 2018-08-23 15:22:15 +0000 | [diff] [blame] | 65 | if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C)))) |
| 66 | return nullptr; |
| 67 | |
| 68 | bool IsEq; |
| 69 | if (ICmpInst::isEquality(Pred)) |
| 70 | IsEq = Pred == ICmpInst::ICMP_EQ; |
| 71 | else if (Pred == FCmpInst::FCMP_OEQ) |
| 72 | IsEq = true; |
| 73 | else if (Pred == FCmpInst::FCMP_UNE) |
| 74 | IsEq = false; |
| 75 | else |
David Bolvansky | 6737b3a | 2018-07-30 20:38:53 +0000 | [diff] [blame] | 76 | return nullptr; |
| 77 | |
Sanjay Patel | 87aa100 | 2018-11-05 16:50:44 +0000 | [diff] [blame] | 78 | // A select operand must be a binop. |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 79 | BinaryOperator *BO; |
Sanjay Patel | 87aa100 | 2018-11-05 16:50:44 +0000 | [diff] [blame] | 80 | if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO))) |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 81 | return nullptr; |
| 82 | |
Sanjay Patel | 87aa100 | 2018-11-05 16:50:44 +0000 | [diff] [blame] | 83 | // The compare constant must be the identity constant for that binop. |
| 84 | // If this a floating-point compare with 0.0, any zero constant will do. |
| 85 | Type *Ty = BO->getType(); |
| 86 | Constant *IdC = ConstantExpr::getBinOpIdentity(BO->getOpcode(), Ty, true); |
| 87 | if (IdC != C) { |
| 88 | if (!IdC || !CmpInst::isFPPredicate(Pred)) |
| 89 | return nullptr; |
| 90 | if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP())) |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 94 | // Last, match the compare variable operand with a binop operand. |
| 95 | Value *Y; |
David Bolvansky | 01d98cc | 2018-08-12 17:30:07 +0000 | [diff] [blame] | 96 | if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X)))) |
| 97 | return nullptr; |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 98 | if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X)))) |
| 99 | return nullptr; |
| 100 | |
David Bolvansky | 43b0e25 | 2018-08-23 15:22:15 +0000 | [diff] [blame] | 101 | // +0.0 compares equal to -0.0, and so it does not behave as required for this |
| 102 | // transform. Bail out if we can not exclude that possibility. |
| 103 | if (isa<FPMathOperator>(BO)) |
| 104 | if (!BO->hasNoSignedZeros() && !CannotBeNegativeZero(Y, &TLI)) |
| 105 | return nullptr; |
| 106 | |
Sanjay Patel | 85e17bb | 2018-08-10 20:30:35 +0000 | [diff] [blame] | 107 | // BO = binop Y, X |
| 108 | // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO } |
| 109 | // => |
| 110 | // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y } |
| 111 | Sel.setOperand(IsEq ? 1 : 2, Y); |
| 112 | return &Sel; |
David Bolvansky | 6737b3a | 2018-07-30 20:38:53 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 115 | /// This folds: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 116 | /// select (icmp eq (and X, C1)), TC, FC |
| 117 | /// iff C1 is a power 2 and the difference between TC and FC is a power-of-2. |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 118 | /// To something like: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 119 | /// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 120 | /// Or: |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 121 | /// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC |
| 122 | /// With some variations depending if FC is larger than TC, or the shift |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 123 | /// isn't needed, or the bit widths don't match. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 124 | static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp, |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 125 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 126 | const APInt *SelTC, *SelFC; |
| 127 | if (!match(Sel.getTrueValue(), m_APInt(SelTC)) || |
| 128 | !match(Sel.getFalseValue(), m_APInt(SelFC))) |
| 129 | return nullptr; |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 130 | |
| 131 | // If this is a vector select, we need a vector compare. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 132 | Type *SelType = Sel.getType(); |
| 133 | if (SelType->isVectorTy() != Cmp->getType()->isVectorTy()) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 134 | return nullptr; |
| 135 | |
| 136 | Value *V; |
| 137 | APInt AndMask; |
| 138 | bool CreateAnd = false; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 139 | ICmpInst::Predicate Pred = Cmp->getPredicate(); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 140 | if (ICmpInst::isEquality(Pred)) { |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 141 | if (!match(Cmp->getOperand(1), m_Zero())) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 142 | return nullptr; |
| 143 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 144 | V = Cmp->getOperand(0); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 145 | const APInt *AndRHS; |
| 146 | if (!match(V, m_And(m_Value(), m_Power2(AndRHS)))) |
| 147 | return nullptr; |
| 148 | |
| 149 | AndMask = *AndRHS; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 150 | } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1), |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 151 | Pred, V, AndMask)) { |
| 152 | assert(ICmpInst::isEquality(Pred) && "Not equality test?"); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 153 | if (!AndMask.isPowerOf2()) |
| 154 | return nullptr; |
| 155 | |
| 156 | CreateAnd = true; |
| 157 | } else { |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 161 | // In general, when both constants are non-zero, we would need an offset to |
| 162 | // replace the select. This would require more instructions than we started |
| 163 | // with. But there's one special-case that we handle here because it can |
| 164 | // simplify/reduce the instructions. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 165 | APInt TC = *SelTC; |
| 166 | APInt FC = *SelFC; |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 167 | if (!TC.isNullValue() && !FC.isNullValue()) { |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 168 | // If the select constants differ by exactly one bit and that's the same |
| 169 | // bit that is masked and checked by the select condition, the select can |
| 170 | // be replaced by bitwise logic to set/clear one bit of the constant result. |
| 171 | if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 172 | return nullptr; |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 173 | if (CreateAnd) { |
| 174 | // If we have to create an 'and', then we must kill the cmp to not |
| 175 | // increase the instruction count. |
| 176 | if (!Cmp->hasOneUse()) |
| 177 | return nullptr; |
| 178 | V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask)); |
| 179 | } |
| 180 | bool ExtraBitInTC = TC.ugt(FC); |
| 181 | if (Pred == ICmpInst::ICMP_EQ) { |
| 182 | // If the masked bit in V is clear, clear or set the bit in the result: |
| 183 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC |
| 184 | // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC |
| 185 | Constant *C = ConstantInt::get(SelType, TC); |
| 186 | return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C); |
| 187 | } |
| 188 | if (Pred == ICmpInst::ICMP_NE) { |
| 189 | // If the masked bit in V is set, set or clear the bit in the result: |
| 190 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC |
| 191 | // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC |
| 192 | Constant *C = ConstantInt::get(SelType, FC); |
| 193 | return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C); |
| 194 | } |
| 195 | llvm_unreachable("Only expecting equality predicates"); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 198 | // Make sure one of the select arms is a power-of-2. |
| 199 | if (!TC.isPowerOf2() && !FC.isPowerOf2()) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 200 | return nullptr; |
| 201 | |
| 202 | // Determine which shift is needed to transform result of the 'and' into the |
| 203 | // desired result. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 204 | const APInt &ValC = !TC.isNullValue() ? TC : FC; |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 205 | unsigned ValZeros = ValC.logBase2(); |
| 206 | unsigned AndZeros = AndMask.logBase2(); |
| 207 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 208 | // Insert the 'and' instruction on the input to the truncate. |
| 209 | if (CreateAnd) |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 210 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask)); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 211 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 212 | // If types don't match, we can still convert the select by introducing a zext |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 213 | // or a trunc of the 'and'. |
| 214 | if (ValZeros > AndZeros) { |
| 215 | V = Builder.CreateZExtOrTrunc(V, SelType); |
| 216 | V = Builder.CreateShl(V, ValZeros - AndZeros); |
| 217 | } else if (ValZeros < AndZeros) { |
| 218 | V = Builder.CreateLShr(V, AndZeros - ValZeros); |
| 219 | V = Builder.CreateZExtOrTrunc(V, SelType); |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 220 | } else { |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 221 | V = Builder.CreateZExtOrTrunc(V, SelType); |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 222 | } |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 223 | |
| 224 | // Okay, now we know that everything is set up, we just don't know whether we |
| 225 | // have a icmp_ne or icmp_eq and whether the true or false val is the zero. |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 226 | bool ShouldNotVal = !TC.isNullValue(); |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 227 | ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; |
| 228 | if (ShouldNotVal) |
| 229 | V = Builder.CreateXor(V, ValC); |
| 230 | |
Craig Topper | 28d6d96 | 2017-09-05 05:26:37 +0000 | [diff] [blame] | 231 | return V; |
| 232 | } |
| 233 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 234 | /// We want to turn code that looks like this: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 235 | /// %C = or %A, %B |
| 236 | /// %D = select %cond, %C, %A |
| 237 | /// into: |
| 238 | /// %C = select %cond, %B, 0 |
| 239 | /// %D = or %A, %C |
| 240 | /// |
| 241 | /// Assuming that the specified instruction is an operand to the select, return |
| 242 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 243 | /// equal the other incoming value of the select. |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 244 | static unsigned getSelectFoldableOperands(BinaryOperator *I) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 245 | switch (I->getOpcode()) { |
| 246 | case Instruction::Add: |
| 247 | case Instruction::Mul: |
| 248 | case Instruction::And: |
| 249 | case Instruction::Or: |
| 250 | case Instruction::Xor: |
| 251 | return 3; // Can fold through either operand. |
| 252 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 253 | case Instruction::Shl: // Can only fold on the shift amount. |
| 254 | case Instruction::LShr: |
| 255 | case Instruction::AShr: |
| 256 | return 1; |
| 257 | default: |
| 258 | return 0; // Cannot fold |
| 259 | } |
| 260 | } |
| 261 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 262 | /// For the same transformation as the previous function, return the identity |
| 263 | /// constant that goes into the select. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 264 | static APInt getSelectFoldableConstant(BinaryOperator *I) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 265 | switch (I->getOpcode()) { |
| 266 | default: llvm_unreachable("This cannot happen!"); |
| 267 | case Instruction::Add: |
| 268 | case Instruction::Sub: |
| 269 | case Instruction::Or: |
| 270 | case Instruction::Xor: |
| 271 | case Instruction::Shl: |
| 272 | case Instruction::LShr: |
| 273 | case Instruction::AShr: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 274 | return APInt::getNullValue(I->getType()->getScalarSizeInBits()); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 275 | case Instruction::And: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 276 | return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits()); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 277 | case Instruction::Mul: |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 278 | return APInt(I->getType()->getScalarSizeInBits(), 1); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 282 | /// We have (select c, TI, FI), and we know that TI and FI have the same opcode. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 283 | Instruction *InstCombiner::foldSelectOpOp(SelectInst &SI, Instruction *TI, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 284 | Instruction *FI) { |
Sanjay Patel | 6105bb5 | 2017-03-16 20:42:45 +0000 | [diff] [blame] | 285 | // Don't break up min/max patterns. The hasOneUse checks below prevent that |
| 286 | // for most cases, but vector min/max with bitcasts can be transformed. If the |
| 287 | // one-use restrictions are eased for other patterns, we still don't want to |
| 288 | // obfuscate min/max. |
| 289 | if ((match(&SI, m_SMin(m_Value(), m_Value())) || |
| 290 | match(&SI, m_SMax(m_Value(), m_Value())) || |
| 291 | match(&SI, m_UMin(m_Value(), m_Value())) || |
| 292 | match(&SI, m_UMax(m_Value(), m_Value())))) |
| 293 | return nullptr; |
| 294 | |
Sanjay Patel | 384d0f2 | 2016-06-08 20:31:52 +0000 | [diff] [blame] | 295 | // If this is a cast from the same type, merge. |
| 296 | if (TI->getNumOperands() == 1 && TI->isCast()) { |
| 297 | Type *FIOpndTy = FI->getOperand(0)->getType(); |
| 298 | if (TI->getOperand(0)->getType() != FIOpndTy) |
| 299 | return nullptr; |
| 300 | |
| 301 | // The select condition may be a vector. We may only change the operand |
| 302 | // type if the vector width remains the same (and matches the condition). |
| 303 | Type *CondTy = SI.getCondition()->getType(); |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 304 | if (CondTy->isVectorTy()) { |
| 305 | if (!FIOpndTy->isVectorTy()) |
| 306 | return nullptr; |
| 307 | if (CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()) |
| 308 | return nullptr; |
| 309 | |
| 310 | // TODO: If the backend knew how to deal with casts better, we could |
| 311 | // remove this limitation. For now, there's too much potential to create |
| 312 | // worse codegen by promoting the select ahead of size-altering casts |
| 313 | // (PR28160). |
| 314 | // |
| 315 | // Note that ValueTracking's matchSelectPattern() looks through casts |
| 316 | // without checking 'hasOneUse' when it matches min/max patterns, so this |
| 317 | // transform may end up happening anyway. |
| 318 | if (TI->getOpcode() != Instruction::BitCast && |
| 319 | (!TI->hasOneUse() || !FI->hasOneUse())) |
| 320 | return nullptr; |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 321 | } else if (!TI->hasOneUse() || !FI->hasOneUse()) { |
| 322 | // TODO: The one-use restrictions for a scalar select could be eased if |
| 323 | // the fold of a select in visitLoadInst() was enhanced to match a pattern |
| 324 | // that includes a cast. |
Sanjay Patel | 384d0f2 | 2016-06-08 20:31:52 +0000 | [diff] [blame] | 325 | return nullptr; |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 327 | |
| 328 | // Fold this by inserting a select from the input values. |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 329 | Value *NewSI = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 330 | Builder.CreateSelect(SI.getCondition(), TI->getOperand(0), |
| 331 | FI->getOperand(0), SI.getName() + ".v", &SI); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 332 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 333 | TI->getType()); |
| 334 | } |
| 335 | |
John Brawn | 2867bd7 | 2018-01-19 10:05:15 +0000 | [diff] [blame] | 336 | // Only handle binary operators (including two-operand getelementptr) with |
| 337 | // one-use here. As with the cast case above, it may be possible to relax the |
| 338 | // one-use constraint, but that needs be examined carefully since it may not |
| 339 | // reduce the total number of instructions. |
| 340 | if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 || |
| 341 | (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) || |
| 342 | !TI->hasOneUse() || !FI->hasOneUse()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 343 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 344 | |
| 345 | // Figure out if the operations have any operands in common. |
| 346 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 347 | bool MatchIsOpZero; |
| 348 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 349 | MatchOp = TI->getOperand(0); |
| 350 | OtherOpT = TI->getOperand(1); |
| 351 | OtherOpF = FI->getOperand(1); |
| 352 | MatchIsOpZero = true; |
| 353 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 354 | MatchOp = TI->getOperand(1); |
| 355 | OtherOpT = TI->getOperand(0); |
| 356 | OtherOpF = FI->getOperand(0); |
| 357 | MatchIsOpZero = false; |
| 358 | } else if (!TI->isCommutative()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 359 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 360 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 361 | MatchOp = TI->getOperand(0); |
| 362 | OtherOpT = TI->getOperand(1); |
| 363 | OtherOpF = FI->getOperand(0); |
| 364 | MatchIsOpZero = true; |
| 365 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 366 | MatchOp = TI->getOperand(1); |
| 367 | OtherOpT = TI->getOperand(0); |
| 368 | OtherOpF = FI->getOperand(1); |
| 369 | MatchIsOpZero = true; |
| 370 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 371 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Florian Hahn | e32ff4b | 2018-09-07 14:40:06 +0000 | [diff] [blame] | 374 | // If the select condition is a vector, the operands of the original select's |
| 375 | // operands also must be vectors. This may not be the case for getelementptr |
| 376 | // for example. |
| 377 | if (SI.getCondition()->getType()->isVectorTy() && |
| 378 | (!OtherOpT->getType()->isVectorTy() || |
| 379 | !OtherOpF->getType()->isVectorTy())) |
| 380 | return nullptr; |
| 381 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 382 | // If we reach here, they do have operations in common. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 383 | Value *NewSI = Builder.CreateSelect(SI.getCondition(), OtherOpT, OtherOpF, |
| 384 | SI.getName() + ".v", &SI); |
Sanjay Patel | cb2199b | 2016-11-11 23:01:20 +0000 | [diff] [blame] | 385 | Value *Op0 = MatchIsOpZero ? MatchOp : NewSI; |
| 386 | Value *Op1 = MatchIsOpZero ? NewSI : MatchOp; |
John Brawn | 2867bd7 | 2018-01-19 10:05:15 +0000 | [diff] [blame] | 387 | if (auto *BO = dyn_cast<BinaryOperator>(TI)) { |
Michael Berg | ed89d06 | 2018-08-16 20:59:45 +0000 | [diff] [blame] | 388 | BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1); |
Michael Berg | 0b838de | 2018-08-20 22:26:58 +0000 | [diff] [blame] | 389 | NewBO->copyIRFlags(TI); |
| 390 | NewBO->andIRFlags(FI); |
Michael Berg | ed89d06 | 2018-08-16 20:59:45 +0000 | [diff] [blame] | 391 | return NewBO; |
John Brawn | 2867bd7 | 2018-01-19 10:05:15 +0000 | [diff] [blame] | 392 | } |
| 393 | if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) { |
| 394 | auto *FGEP = cast<GetElementPtrInst>(FI); |
| 395 | Type *ElementType = TGEP->getResultElementType(); |
| 396 | return TGEP->isInBounds() && FGEP->isInBounds() |
| 397 | ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1}) |
| 398 | : GetElementPtrInst::Create(ElementType, Op0, {Op1}); |
| 399 | } |
| 400 | llvm_unreachable("Expected BinaryOperator or GEP"); |
| 401 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 404 | static bool isSelect01(const APInt &C1I, const APInt &C2I) { |
| 405 | if (!C1I.isNullValue() && !C2I.isNullValue()) // One side must be zero. |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 406 | return false; |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 407 | return C1I.isOneValue() || C1I.isAllOnesValue() || |
| 408 | C2I.isOneValue() || C2I.isAllOnesValue(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 411 | /// Try to fold the select into one of the operands to allow further |
| 412 | /// optimization. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 413 | Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 414 | Value *FalseVal) { |
| 415 | // See the comment above GetSelectFoldableOperands for a description of the |
| 416 | // transformation we are doing here. |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 417 | if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) { |
| 418 | if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 419 | if (unsigned SFO = getSelectFoldableOperands(TVI)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 420 | unsigned OpToFold = 0; |
| 421 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 422 | OpToFold = 1; |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 423 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 424 | OpToFold = 2; |
| 425 | } |
| 426 | |
| 427 | if (OpToFold) { |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 428 | APInt CI = getSelectFoldableConstant(TVI); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 429 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 430 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 8ef5001 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 431 | // between 0, 1 and -1. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 432 | const APInt *OOpC; |
| 433 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); |
| 434 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { |
| 435 | Value *C = ConstantInt::get(OOp->getType(), CI); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 436 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 437 | NewSel->takeName(TVI); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 438 | BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(), |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 439 | FalseVal, NewSel); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 440 | BO->copyIRFlags(TVI); |
Nick Lewycky | ebc2f3a | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 441 | return BO; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 448 | if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) { |
| 449 | if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 450 | if (unsigned SFO = getSelectFoldableOperands(FVI)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 451 | unsigned OpToFold = 0; |
| 452 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 453 | OpToFold = 1; |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 454 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 455 | OpToFold = 2; |
| 456 | } |
| 457 | |
| 458 | if (OpToFold) { |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 459 | APInt CI = getSelectFoldableConstant(FVI); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 460 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 461 | // Avoid creating select between 2 constants unless it's selecting |
Benjamin Kramer | 8ef5001 | 2010-12-22 23:12:15 +0000 | [diff] [blame] | 462 | // between 0, 1 and -1. |
Craig Topper | 4c766a0 | 2017-09-05 05:26:36 +0000 | [diff] [blame] | 463 | const APInt *OOpC; |
| 464 | bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); |
| 465 | if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { |
| 466 | Value *C = ConstantInt::get(OOp->getType(), CI); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 467 | Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 468 | NewSel->takeName(FVI); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 469 | BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(), |
Nick Lewycky | 8544228 | 2011-03-27 19:51:23 +0000 | [diff] [blame] | 470 | TrueVal, NewSel); |
Craig Topper | 8e351e9 | 2017-08-08 06:19:24 +0000 | [diff] [blame] | 471 | BO->copyIRFlags(FVI); |
Nick Lewycky | ebc2f3a | 2011-03-28 17:48:26 +0000 | [diff] [blame] | 472 | return BO; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 479 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 482 | /// We want to turn: |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 483 | /// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1) |
| 484 | /// into: |
| 485 | /// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0) |
| 486 | /// Note: |
| 487 | /// Z may be 0 if lshr is missing. |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 488 | /// Worst-case scenario is that we will replace 5 instructions with 5 different |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 489 | /// instructions, but we got rid of select. |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 490 | static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp, |
| 491 | Value *TVal, Value *FVal, |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 492 | InstCombiner::BuilderTy &Builder) { |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 493 | if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() && |
| 494 | Cmp->getPredicate() == ICmpInst::ICMP_EQ && |
| 495 | match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One()))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 496 | return nullptr; |
| 497 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 498 | // The TrueVal has general form of: and %B, 1 |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 499 | Value *B; |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 500 | if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One())))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 501 | return nullptr; |
| 502 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 503 | // Where %B may be optionally shifted: lshr %X, %Z. |
| 504 | Value *X, *Z; |
| 505 | const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z)))); |
| 506 | if (!HasShift) |
| 507 | X = B; |
| 508 | |
| 509 | Value *Y; |
| 510 | if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y)))) |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 511 | return nullptr; |
| 512 | |
Roman Lebedev | c006593 | 2018-04-13 09:57:57 +0000 | [diff] [blame] | 513 | // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0 |
| 514 | // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0 |
| 515 | Constant *One = ConstantInt::get(SelType, 1); |
| 516 | Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One; |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 517 | Value *FullMask = Builder.CreateOr(Y, MaskB); |
| 518 | Value *MaskedX = Builder.CreateAnd(X, FullMask); |
| 519 | Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX); |
| 520 | return new ZExtInst(ICmpNeZero, SelType); |
| 521 | } |
| 522 | |
| 523 | /// We want to turn: |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 524 | /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) |
| 525 | /// into: |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 526 | /// (or (shl (and X, C1), C3), Y) |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 527 | /// iff: |
| 528 | /// C1 and C2 are both powers of 2 |
| 529 | /// where: |
| 530 | /// C3 = Log(C2) - Log(C1) |
| 531 | /// |
| 532 | /// This transform handles cases where: |
| 533 | /// 1. The icmp predicate is inverted |
| 534 | /// 2. The select operands are reversed |
| 535 | /// 3. The magnitude of C2 and C1 are flipped |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 536 | static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal, |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 537 | Value *FalseVal, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 538 | InstCombiner::BuilderTy &Builder) { |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 539 | // Only handle integer compares. Also, if this is a vector select, we need a |
| 540 | // vector compare. |
| 541 | if (!TrueVal->getType()->isIntOrIntVectorTy() || |
| 542 | TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 543 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 544 | |
| 545 | Value *CmpLHS = IC->getOperand(0); |
| 546 | Value *CmpRHS = IC->getOperand(1); |
| 547 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 548 | Value *V; |
| 549 | unsigned C1Log; |
| 550 | bool IsEqualZero; |
| 551 | bool NeedAnd = false; |
| 552 | if (IC->isEquality()) { |
| 553 | if (!match(CmpRHS, m_Zero())) |
| 554 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 555 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 556 | const APInt *C1; |
| 557 | if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1)))) |
| 558 | return nullptr; |
| 559 | |
| 560 | V = CmpLHS; |
| 561 | C1Log = C1->logBase2(); |
| 562 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ; |
| 563 | } else if (IC->getPredicate() == ICmpInst::ICMP_SLT || |
| 564 | IC->getPredicate() == ICmpInst::ICMP_SGT) { |
| 565 | // We also need to recognize (icmp slt (trunc (X)), 0) and |
| 566 | // (icmp sgt (trunc (X)), -1). |
| 567 | IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT; |
| 568 | if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) || |
| 569 | (!IsEqualZero && !match(CmpRHS, m_Zero()))) |
| 570 | return nullptr; |
| 571 | |
| 572 | if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V))))) |
| 573 | return nullptr; |
| 574 | |
| 575 | C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1; |
| 576 | NeedAnd = true; |
| 577 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 578 | return nullptr; |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 579 | } |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 580 | |
| 581 | const APInt *C2; |
Dinesh Dwivedi | 83c11da | 2014-05-15 08:22:55 +0000 | [diff] [blame] | 582 | bool OrOnTrueVal = false; |
| 583 | bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2))); |
| 584 | if (!OrOnFalseVal) |
| 585 | OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2))); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 586 | |
| 587 | if (!OrOnFalseVal && !OrOnTrueVal) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 588 | return nullptr; |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 589 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 590 | Value *Y = OrOnFalseVal ? TrueVal : FalseVal; |
| 591 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 592 | unsigned C2Log = C2->logBase2(); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 593 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 594 | bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 595 | bool NeedShift = C1Log != C2Log; |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 596 | bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() != |
| 597 | V->getType()->getScalarSizeInBits(); |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 598 | |
| 599 | // Make sure we don't create more instructions than we save. |
| 600 | Value *Or = OrOnFalseVal ? FalseVal : TrueVal; |
| 601 | if ((NeedShift + NeedXor + NeedZExtTrunc) > |
| 602 | (IC->hasOneUse() + Or->hasOneUse())) |
| 603 | return nullptr; |
| 604 | |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 605 | if (NeedAnd) { |
| 606 | // Insert the AND instruction on the input to the truncate. |
| 607 | APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 608 | V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1)); |
Craig Topper | dffbbcb | 2017-06-22 16:23:30 +0000 | [diff] [blame] | 609 | } |
| 610 | |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 611 | if (C2Log > C1Log) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 612 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
| 613 | V = Builder.CreateShl(V, C2Log - C1Log); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 614 | } else if (C1Log > C2Log) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 615 | V = Builder.CreateLShr(V, C1Log - C2Log); |
| 616 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
David Majnemer | d73f37b | 2013-04-30 10:36:33 +0000 | [diff] [blame] | 617 | } else |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 618 | V = Builder.CreateZExtOrTrunc(V, Y->getType()); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 619 | |
Craig Topper | ae86cc7 | 2017-06-21 16:07:13 +0000 | [diff] [blame] | 620 | if (NeedXor) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 621 | V = Builder.CreateXor(V, *C2); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 622 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 623 | return Builder.CreateOr(V, Y); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Sanjay Patel | e9a153f | 2018-02-05 17:53:29 +0000 | [diff] [blame] | 626 | /// Transform patterns such as: (a > b) ? a - b : 0 |
| 627 | /// into: ((a > b) ? a : b) - b) |
| 628 | /// This produces a canonical max pattern that is more easily recognized by the |
| 629 | /// backend and converted into saturated subtraction instructions if those |
| 630 | /// exist. |
| 631 | /// There are 8 commuted/swapped variants of this pattern. |
| 632 | /// TODO: Also support a - UMIN(a,b) patterns. |
| 633 | static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI, |
| 634 | const Value *TrueVal, |
| 635 | const Value *FalseVal, |
| 636 | InstCombiner::BuilderTy &Builder) { |
| 637 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 638 | if (!ICmpInst::isUnsigned(Pred)) |
| 639 | return nullptr; |
| 640 | |
| 641 | // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0 |
| 642 | if (match(TrueVal, m_Zero())) { |
| 643 | Pred = ICmpInst::getInversePredicate(Pred); |
| 644 | std::swap(TrueVal, FalseVal); |
| 645 | } |
| 646 | if (!match(FalseVal, m_Zero())) |
| 647 | return nullptr; |
| 648 | |
| 649 | Value *A = ICI->getOperand(0); |
| 650 | Value *B = ICI->getOperand(1); |
| 651 | if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) { |
| 652 | // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0 |
| 653 | std::swap(A, B); |
| 654 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 655 | } |
| 656 | |
| 657 | assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && |
| 658 | "Unexpected isUnsigned predicate!"); |
| 659 | |
| 660 | // Account for swapped form of subtraction: ((a > b) ? b - a : 0). |
| 661 | bool IsNegative = false; |
| 662 | if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A)))) |
| 663 | IsNegative = true; |
| 664 | else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B)))) |
| 665 | return nullptr; |
| 666 | |
| 667 | // If sub is used anywhere else, we wouldn't be able to eliminate it |
| 668 | // afterwards. |
| 669 | if (!TrueVal->hasOneUse()) |
| 670 | return nullptr; |
| 671 | |
| 672 | // All checks passed, convert to canonical unsigned saturated subtraction |
| 673 | // form: sub(max()). |
| 674 | // (a > b) ? a - b : 0 -> ((a > b) ? a : b) - b) |
| 675 | Value *Max = Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); |
| 676 | return IsNegative ? Builder.CreateSub(B, Max) : Builder.CreateSub(Max, B); |
| 677 | } |
| 678 | |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 679 | /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single |
| 680 | /// call to cttz/ctlz with flag 'is_zero_undef' cleared. |
| 681 | /// |
| 682 | /// For example, we can fold the following code sequence: |
| 683 | /// \code |
| 684 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true) |
| 685 | /// %1 = icmp ne i32 %x, 0 |
| 686 | /// %2 = select i1 %1, i32 %0, i32 32 |
| 687 | /// \code |
Junmo Park | 820964e | 2016-03-23 01:38:35 +0000 | [diff] [blame] | 688 | /// |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 689 | /// into: |
| 690 | /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) |
| 691 | static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 692 | InstCombiner::BuilderTy &Builder) { |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 693 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 694 | Value *CmpLHS = ICI->getOperand(0); |
| 695 | Value *CmpRHS = ICI->getOperand(1); |
| 696 | |
| 697 | // Check if the condition value compares a value for equality against zero. |
| 698 | if (!ICI->isEquality() || !match(CmpRHS, m_Zero())) |
| 699 | return nullptr; |
| 700 | |
| 701 | Value *Count = FalseVal; |
| 702 | Value *ValueOnZero = TrueVal; |
| 703 | if (Pred == ICmpInst::ICMP_NE) |
| 704 | std::swap(Count, ValueOnZero); |
| 705 | |
| 706 | // Skip zero extend/truncate. |
| 707 | Value *V = nullptr; |
| 708 | if (match(Count, m_ZExt(m_Value(V))) || |
| 709 | match(Count, m_Trunc(m_Value(V)))) |
| 710 | Count = V; |
| 711 | |
| 712 | // Check if the value propagated on zero is a constant number equal to the |
| 713 | // sizeof in bits of 'Count'. |
| 714 | unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits(); |
| 715 | if (!match(ValueOnZero, m_SpecificInt(SizeOfInBits))) |
| 716 | return nullptr; |
| 717 | |
| 718 | // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the |
| 719 | // input to the cttz/ctlz is used as LHS for the compare instruction. |
| 720 | if (match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) || |
| 721 | match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) { |
| 722 | IntrinsicInst *II = cast<IntrinsicInst>(Count); |
Andrea Di Biagio | 30d471f | 2015-02-13 16:33:34 +0000 | [diff] [blame] | 723 | // Explicitly clear the 'undef_on_zero' flag. |
| 724 | IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone()); |
Craig Topper | 3b74a68 | 2017-08-04 16:07:18 +0000 | [diff] [blame] | 725 | NewI->setArgOperand(1, ConstantInt::getFalse(NewI->getContext())); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 726 | Builder.Insert(NewI); |
| 727 | return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType()); |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return nullptr; |
| 731 | } |
| 732 | |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 733 | /// Return true if we find and adjust an icmp+select pattern where the compare |
| 734 | /// is with a constant that can be incremented or decremented to match the |
| 735 | /// minimum or maximum idiom. |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 736 | static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { |
| 737 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 738 | Value *CmpLHS = Cmp.getOperand(0); |
| 739 | Value *CmpRHS = Cmp.getOperand(1); |
| 740 | Value *TrueVal = Sel.getTrueValue(); |
| 741 | Value *FalseVal = Sel.getFalseValue(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 742 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 743 | // We may move or edit the compare, so make sure the select is the only user. |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 744 | const APInt *CmpC; |
| 745 | if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 746 | return false; |
Frits van Bommel | 6a1fb8f | 2011-01-08 10:51:36 +0000 | [diff] [blame] | 747 | |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 748 | // These transforms only work for selects of integers or vector selects of |
| 749 | // integer vectors. |
| 750 | Type *SelTy = Sel.getType(); |
| 751 | auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType()); |
| 752 | if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy()) |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 753 | return false; |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 754 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 755 | Constant *AdjustedRHS; |
| 756 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 757 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 758 | else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 759 | AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 760 | else |
| 761 | return false; |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 762 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 763 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
| 764 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
| 765 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 766 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 767 | ; // Nothing to do here. Values match without any sign/zero extension. |
| 768 | } |
| 769 | // Types do not match. Instead of calculating this with mixed types, promote |
| 770 | // all to the larger type. This enables scalar evolution to analyze this |
| 771 | // expression. |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 772 | else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { |
| 773 | Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy); |
Tobias Grosser | fc3d7f6 | 2011-01-07 21:33:14 +0000 | [diff] [blame] | 774 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 775 | // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X |
| 776 | // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X |
| 777 | // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X |
| 778 | // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X |
| 779 | if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) { |
| 780 | CmpLHS = TrueVal; |
| 781 | AdjustedRHS = SextRHS; |
| 782 | } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) && |
| 783 | SextRHS == TrueVal) { |
| 784 | CmpLHS = FalseVal; |
| 785 | AdjustedRHS = SextRHS; |
| 786 | } else if (Cmp.isUnsigned()) { |
Sanjay Patel | 86408a8 | 2016-11-07 15:52:45 +0000 | [diff] [blame] | 787 | Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy); |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 788 | // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X |
| 789 | // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X |
| 790 | // zext + signed compare cannot be changed: |
| 791 | // 0xff <s 0x00, but 0x00ff >s 0x0000 |
| 792 | if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) { |
| 793 | CmpLHS = TrueVal; |
| 794 | AdjustedRHS = ZextRHS; |
| 795 | } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) && |
| 796 | ZextRHS == TrueVal) { |
| 797 | CmpLHS = FalseVal; |
| 798 | AdjustedRHS = ZextRHS; |
| 799 | } else { |
| 800 | return false; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 801 | } |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 802 | } else { |
| 803 | return false; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 804 | } |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 805 | } else { |
| 806 | return false; |
| 807 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 808 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 809 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 810 | CmpRHS = AdjustedRHS; |
| 811 | std::swap(FalseVal, TrueVal); |
| 812 | Cmp.setPredicate(Pred); |
| 813 | Cmp.setOperand(0, CmpLHS); |
| 814 | Cmp.setOperand(1, CmpRHS); |
| 815 | Sel.setOperand(1, TrueVal); |
| 816 | Sel.setOperand(2, FalseVal); |
| 817 | Sel.swapProfMetadata(); |
| 818 | |
| 819 | // Move the compare instruction right before the select instruction. Otherwise |
| 820 | // the sext/zext value may be defined after the compare instruction uses it. |
| 821 | Cmp.moveBefore(&Sel); |
| 822 | |
| 823 | return true; |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 826 | /// If this is an integer min/max (icmp + select) with a constant operand, |
| 827 | /// create the canonical icmp for the min/max operation and canonicalize the |
| 828 | /// constant to the 'false' operand of the select: |
| 829 | /// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2 |
| 830 | /// Note: if C1 != C2, this will change the icmp constant to the existing |
| 831 | /// constant operand of the select. |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 832 | static Instruction * |
| 833 | canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp, |
| 834 | InstCombiner::BuilderTy &Builder) { |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 835 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 836 | return nullptr; |
| 837 | |
| 838 | // Canonicalize the compare predicate based on whether we have min or max. |
| 839 | Value *LHS, *RHS; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 840 | SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS); |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 841 | if (!SelectPatternResult::isMinOrMax(SPR.Flavor)) |
| 842 | return nullptr; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 843 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 844 | // Is this already canonical? |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 845 | ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor); |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 846 | if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS && |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 847 | Cmp.getPredicate() == CanonicalPred) |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 848 | return nullptr; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 849 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 850 | // Create the canonical compare and plug it into the select. |
Sanjay Patel | 1f2f5d1 | 2018-03-06 19:01:18 +0000 | [diff] [blame] | 851 | Sel.setCondition(Builder.CreateICmp(CanonicalPred, LHS, RHS)); |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 852 | |
Sanjay Patel | cb731f1 | 2017-02-21 19:33:53 +0000 | [diff] [blame] | 853 | // If the select operands did not change, we're done. |
| 854 | if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS) |
| 855 | return &Sel; |
| 856 | |
| 857 | // If we are swapping the select operands, swap the metadata too. |
| 858 | assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && |
| 859 | "Unexpected results from matchSelectPattern"); |
| 860 | Sel.setTrueValue(LHS); |
| 861 | Sel.setFalseValue(RHS); |
| 862 | Sel.swapProfMetadata(); |
| 863 | return &Sel; |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 866 | /// There are many select variants for each of ABS/NABS. |
| 867 | /// In matchSelectPattern(), there are different compare constants, compare |
| 868 | /// predicates/operands and select operands. |
| 869 | /// In isKnownNegation(), there are different formats of negated operands. |
| 870 | /// Canonicalize all these variants to 1 pattern. |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 871 | /// This makes CSE more likely. |
| 872 | static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp, |
| 873 | InstCombiner::BuilderTy &Builder) { |
| 874 | if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) |
| 875 | return nullptr; |
| 876 | |
| 877 | // Choose a sign-bit check for the compare (likely simpler for codegen). |
| 878 | // ABS: (X <s 0) ? -X : X |
| 879 | // NABS: (X <s 0) ? X : -X |
| 880 | Value *LHS, *RHS; |
| 881 | SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor; |
| 882 | if (SPF != SelectPatternFlavor::SPF_ABS && |
| 883 | SPF != SelectPatternFlavor::SPF_NABS) |
| 884 | return nullptr; |
| 885 | |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 886 | Value *TVal = Sel.getTrueValue(); |
| 887 | Value *FVal = Sel.getFalseValue(); |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 888 | assert(isKnownNegation(TVal, FVal) && |
| 889 | "Unexpected result from matchSelectPattern"); |
| 890 | |
| 891 | // The compare may use the negated abs()/nabs() operand, or it may use |
| 892 | // negation in non-canonical form such as: sub A, B. |
| 893 | bool CmpUsesNegatedOp = match(Cmp.getOperand(0), m_Neg(m_Specific(TVal))) || |
| 894 | match(Cmp.getOperand(0), m_Neg(m_Specific(FVal))); |
| 895 | |
| 896 | bool CmpCanonicalized = !CmpUsesNegatedOp && |
| 897 | match(Cmp.getOperand(1), m_ZeroInt()) && |
| 898 | Cmp.getPredicate() == ICmpInst::ICMP_SLT; |
| 899 | bool RHSCanonicalized = match(RHS, m_Neg(m_Specific(LHS))); |
| 900 | |
| 901 | // Is this already canonical? |
| 902 | if (CmpCanonicalized && RHSCanonicalized) |
| 903 | return nullptr; |
| 904 | |
| 905 | // If RHS is used by other instructions except compare and select, don't |
| 906 | // canonicalize it to not increase the instruction count. |
| 907 | if (!(RHS->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp))) |
| 908 | return nullptr; |
| 909 | |
| 910 | // Create the canonical compare: icmp slt LHS 0. |
| 911 | if (!CmpCanonicalized) { |
| 912 | Cmp.setPredicate(ICmpInst::ICMP_SLT); |
| 913 | Cmp.setOperand(1, ConstantInt::getNullValue(Cmp.getOperand(0)->getType())); |
| 914 | if (CmpUsesNegatedOp) |
| 915 | Cmp.setOperand(0, LHS); |
| 916 | } |
| 917 | |
| 918 | // Create the canonical RHS: RHS = sub (0, LHS). |
| 919 | if (!RHSCanonicalized) { |
| 920 | assert(RHS->hasOneUse() && "RHS use number is not right"); |
| 921 | RHS = Builder.CreateNeg(LHS); |
| 922 | if (TVal == LHS) { |
| 923 | Sel.setFalseValue(RHS); |
| 924 | FVal = RHS; |
| 925 | } else { |
| 926 | Sel.setTrueValue(RHS); |
| 927 | TVal = RHS; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | // If the select operands do not change, we're done. |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 932 | if (SPF == SelectPatternFlavor::SPF_NABS) { |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 933 | if (TVal == LHS) |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 934 | return &Sel; |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 935 | assert(FVal == LHS && "Unexpected results from matchSelectPattern"); |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 936 | } else { |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 937 | if (FVal == LHS) |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 938 | return &Sel; |
Chen Zheng | 567485a | 2018-07-27 01:49:51 +0000 | [diff] [blame] | 939 | assert(TVal == LHS && "Unexpected results from matchSelectPattern"); |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // We are swapping the select operands, so swap the metadata too. |
| 943 | Sel.setTrueValue(FVal); |
| 944 | Sel.setFalseValue(TVal); |
| 945 | Sel.swapProfMetadata(); |
| 946 | return &Sel; |
| 947 | } |
| 948 | |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 949 | /// Visit a SelectInst that has an ICmpInst as its first operand. |
| 950 | Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, |
| 951 | ICmpInst *ICI) { |
Craig Topper | c2d3c63 | 2017-08-04 05:12:37 +0000 | [diff] [blame] | 952 | Value *TrueVal = SI.getTrueValue(); |
| 953 | Value *FalseVal = SI.getFalseValue(); |
| 954 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 955 | if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder)) |
Sanjay Patel | 3b0bafe | 2016-11-21 22:04:14 +0000 | [diff] [blame] | 956 | return NewSel; |
| 957 | |
Sanjay Patel | a003c72 | 2018-05-20 14:23:23 +0000 | [diff] [blame] | 958 | if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder)) |
| 959 | return NewAbs; |
| 960 | |
Sanjay Patel | 644d7c3 | 2016-11-01 18:15:03 +0000 | [diff] [blame] | 961 | bool Changed = adjustMinMax(SI, *ICI); |
Sanjay Patel | 7ce6583 | 2016-11-01 17:46:08 +0000 | [diff] [blame] | 962 | |
Sanjay Patel | 807ddee | 2018-04-25 16:34:01 +0000 | [diff] [blame] | 963 | if (Value *V = foldSelectICmpAnd(SI, ICI, Builder)) |
| 964 | return replaceInstUsesWith(SI, V); |
Craig Topper | 74177e1 | 2017-08-21 19:02:06 +0000 | [diff] [blame] | 965 | |
Benjamin Kramer | 749ef5f | 2011-05-27 13:00:16 +0000 | [diff] [blame] | 966 | // NOTE: if we wanted to, this is where to detect integer MIN/MAX |
Sanjay Patel | e7b6654 | 2018-05-03 21:58:44 +0000 | [diff] [blame] | 967 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 968 | Value *CmpLHS = ICI->getOperand(0); |
| 969 | Value *CmpRHS = ICI->getOperand(1); |
Benjamin Kramer | b8743a9 | 2012-05-28 19:18:16 +0000 | [diff] [blame] | 970 | if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) { |
Nick Lewycky | 83167df | 2011-03-27 07:30:57 +0000 | [diff] [blame] | 971 | if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) { |
| 972 | // Transform (X == C) ? X : Y -> (X == C) ? C : Y |
| 973 | SI.setOperand(1, CmpRHS); |
| 974 | Changed = true; |
| 975 | } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) { |
| 976 | // Transform (X != C) ? Y : X -> (X != C) ? Y : C |
| 977 | SI.setOperand(2, CmpRHS); |
| 978 | Changed = true; |
| 979 | } |
| 980 | } |
| 981 | |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 982 | // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring |
| 983 | // decomposeBitTestICmp() might help. |
David Majnemer | 3f0fb98 | 2015-06-06 22:40:21 +0000 | [diff] [blame] | 984 | { |
Sanjay Patel | 5f3c703 | 2016-07-20 23:40:01 +0000 | [diff] [blame] | 985 | unsigned BitWidth = |
| 986 | DL.getTypeSizeInBits(TrueVal->getType()->getScalarType()); |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 987 | APInt MinSignedValue = APInt::getSignedMinValue(BitWidth); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 988 | Value *X; |
| 989 | const APInt *Y, *C; |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 990 | bool TrueWhenUnset; |
| 991 | bool IsBitTest = false; |
| 992 | if (ICmpInst::isEquality(Pred) && |
| 993 | match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 994 | match(CmpRHS, m_Zero())) { |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 995 | IsBitTest = true; |
| 996 | TrueWhenUnset = Pred == ICmpInst::ICMP_EQ; |
| 997 | } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { |
| 998 | X = CmpLHS; |
| 999 | Y = &MinSignedValue; |
| 1000 | IsBitTest = true; |
| 1001 | TrueWhenUnset = false; |
| 1002 | } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { |
| 1003 | X = CmpLHS; |
| 1004 | Y = &MinSignedValue; |
| 1005 | IsBitTest = true; |
| 1006 | TrueWhenUnset = true; |
| 1007 | } |
| 1008 | if (IsBitTest) { |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1009 | Value *V = nullptr; |
| 1010 | // (X & Y) == 0 ? X : X ^ Y --> X & ~Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 1011 | if (TrueWhenUnset && TrueVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1012 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1013 | V = Builder.CreateAnd(X, ~(*Y)); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1014 | // (X & Y) != 0 ? X ^ Y : X --> X & ~Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 1015 | else if (!TrueWhenUnset && FalseVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1016 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1017 | V = Builder.CreateAnd(X, ~(*Y)); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1018 | // (X & Y) == 0 ? X ^ Y : X --> X | Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 1019 | else if (TrueWhenUnset && FalseVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1020 | match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1021 | V = Builder.CreateOr(X, *Y); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1022 | // (X & Y) != 0 ? X : X ^ Y --> X | Y |
David Majnemer | b0362e4 | 2014-12-20 04:45:35 +0000 | [diff] [blame] | 1023 | else if (!TrueWhenUnset && TrueVal == X && |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1024 | match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1025 | V = Builder.CreateOr(X, *Y); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (V) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1028 | return replaceInstUsesWith(SI, V); |
David Majnemer | 40157d5 | 2014-11-27 07:25:21 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Roman Lebedev | 41922f1 | 2018-04-07 10:37:24 +0000 | [diff] [blame] | 1032 | if (Instruction *V = |
| 1033 | foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder)) |
| 1034 | return V; |
| 1035 | |
Craig Topper | 5d6ddda | 2017-08-29 00:13:49 +0000 | [diff] [blame] | 1036 | if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1037 | return replaceInstUsesWith(SI, V); |
David Majnemer | 8d048d0 | 2013-04-30 08:57:58 +0000 | [diff] [blame] | 1038 | |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 1039 | if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1040 | return replaceInstUsesWith(SI, V); |
Andrea Di Biagio | 086cbc3 | 2015-01-27 15:58:14 +0000 | [diff] [blame] | 1041 | |
Sanjay Patel | e9a153f | 2018-02-05 17:53:29 +0000 | [diff] [blame] | 1042 | if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder)) |
| 1043 | return replaceInstUsesWith(SI, V); |
| 1044 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1045 | return Changed ? &SI : nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 1048 | /// SI is a select whose condition is a PHI node (but the two may be in |
| 1049 | /// different blocks). See if the true/false values (V) are live in all of the |
| 1050 | /// predecessor blocks of the PHI. For example, cases like this can't be mapped: |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1051 | /// |
| 1052 | /// X = phi [ C1, BB1], [C2, BB2] |
| 1053 | /// Y = add |
| 1054 | /// Z = select X, Y, 0 |
| 1055 | /// |
| 1056 | /// because Y is not live in BB1/BB2. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1057 | static bool canSelectOperandBeMappingIntoPredBlock(const Value *V, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1058 | const SelectInst &SI) { |
| 1059 | // If the value is a non-instruction value like a constant or argument, it |
| 1060 | // can always be mapped. |
| 1061 | const Instruction *I = dyn_cast<Instruction>(V); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1062 | if (!I) return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1064 | // If V is a PHI node defined in the same block as the condition PHI, we can |
| 1065 | // map the arguments. |
| 1066 | const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1068 | if (const PHINode *VP = dyn_cast<PHINode>(I)) |
| 1069 | if (VP->getParent() == CondPHI->getParent()) |
| 1070 | return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1072 | // Otherwise, if the PHI and select are defined in the same block and if V is |
| 1073 | // defined in a different block, then we can transform it. |
| 1074 | if (SI.getParent() == CondPHI->getParent() && |
| 1075 | I->getParent() != CondPHI->getParent()) |
| 1076 | return true; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1078 | // Otherwise we have a 'hard' case and we can't tell without doing more |
| 1079 | // detailed dominator based analysis, punt. |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 1083 | /// We have an SPF (e.g. a min or max) of an SPF of the form: |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1084 | /// SPF2(SPF1(A, B), C) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1085 | Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner, |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1086 | SelectPatternFlavor SPF1, |
| 1087 | Value *A, Value *B, |
| 1088 | Instruction &Outer, |
| 1089 | SelectPatternFlavor SPF2, Value *C) { |
David Majnemer | 5673772 | 2016-04-08 16:51:49 +0000 | [diff] [blame] | 1090 | if (Outer.getType() != Inner->getType()) |
| 1091 | return nullptr; |
| 1092 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1093 | if (C == A || C == B) { |
| 1094 | // MAX(MAX(A, B), B) -> MAX(A, B) |
| 1095 | // MIN(MIN(a, b), a) -> MIN(a, b) |
Sanjay Patel | 6f00fc3 | 2018-09-13 16:04:06 +0000 | [diff] [blame] | 1096 | // TODO: This could be done in instsimplify. |
Craig Topper | 0198b73 | 2018-05-18 21:21:56 +0000 | [diff] [blame] | 1097 | if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1098 | return replaceInstUsesWith(Outer, Inner); |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1100 | // MAX(MIN(a, b), a) -> a |
| 1101 | // MIN(MAX(a, b), a) -> a |
Sanjay Patel | 6f00fc3 | 2018-09-13 16:04:06 +0000 | [diff] [blame] | 1102 | // TODO: This could be done in instsimplify. |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1103 | if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || |
| 1104 | (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || |
| 1105 | (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || |
| 1106 | (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1107 | return replaceInstUsesWith(Outer, C); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1108 | } |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1109 | |
Dinesh Dwivedi | f675f42 | 2014-05-15 06:13:40 +0000 | [diff] [blame] | 1110 | if (SPF1 == SPF2) { |
Sanjay Patel | c0de9c9 | 2016-10-27 21:19:40 +0000 | [diff] [blame] | 1111 | const APInt *CB, *CC; |
| 1112 | if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) { |
| 1113 | // MIN(MIN(A, 23), 97) -> MIN(A, 23) |
| 1114 | // MAX(MAX(A, 97), 23) -> MAX(A, 97) |
Sanjay Patel | 6f00fc3 | 2018-09-13 16:04:06 +0000 | [diff] [blame] | 1115 | // TODO: This could be done in instsimplify. |
Sanjay Patel | c0de9c9 | 2016-10-27 21:19:40 +0000 | [diff] [blame] | 1116 | if ((SPF1 == SPF_UMIN && CB->ule(*CC)) || |
| 1117 | (SPF1 == SPF_SMIN && CB->sle(*CC)) || |
| 1118 | (SPF1 == SPF_UMAX && CB->uge(*CC)) || |
| 1119 | (SPF1 == SPF_SMAX && CB->sge(*CC))) |
| 1120 | return replaceInstUsesWith(Outer, Inner); |
Dinesh Dwivedi | f82f16e | 2014-05-19 07:08:32 +0000 | [diff] [blame] | 1121 | |
Sanjay Patel | c0de9c9 | 2016-10-27 21:19:40 +0000 | [diff] [blame] | 1122 | // MIN(MIN(A, 97), 23) -> MIN(A, 23) |
| 1123 | // MAX(MAX(A, 23), 97) -> MAX(A, 97) |
| 1124 | if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) || |
| 1125 | (SPF1 == SPF_SMIN && CB->sgt(*CC)) || |
| 1126 | (SPF1 == SPF_UMAX && CB->ult(*CC)) || |
| 1127 | (SPF1 == SPF_SMAX && CB->slt(*CC))) { |
| 1128 | Outer.replaceUsesOfWith(Inner, A); |
| 1129 | return &Outer; |
Dinesh Dwivedi | f675f42 | 2014-05-15 06:13:40 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | } |
Dinesh Dwivedi | 3217b6c | 2014-06-06 06:54:45 +0000 | [diff] [blame] | 1133 | |
| 1134 | // ABS(ABS(X)) -> ABS(X) |
| 1135 | // NABS(NABS(X)) -> NABS(X) |
Sanjay Patel | 6f00fc3 | 2018-09-13 16:04:06 +0000 | [diff] [blame] | 1136 | // TODO: This could be done in instsimplify. |
Dinesh Dwivedi | 3217b6c | 2014-06-06 06:54:45 +0000 | [diff] [blame] | 1137 | if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1138 | return replaceInstUsesWith(Outer, Inner); |
Dinesh Dwivedi | 3217b6c | 2014-06-06 06:54:45 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Dinesh Dwivedi | 95f0d51 | 2014-06-12 14:06:00 +0000 | [diff] [blame] | 1141 | // ABS(NABS(X)) -> ABS(X) |
| 1142 | // NABS(ABS(X)) -> NABS(X) |
| 1143 | if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) || |
| 1144 | (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) { |
| 1145 | SelectInst *SI = cast<SelectInst>(Inner); |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 1146 | Value *NewSI = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1147 | Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(), |
| 1148 | SI->getTrueValue(), SI->getName(), SI); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1149 | return replaceInstUsesWith(Outer, NewSI); |
Dinesh Dwivedi | 95f0d51 | 2014-06-12 14:06:00 +0000 | [diff] [blame] | 1150 | } |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1151 | |
| 1152 | auto IsFreeOrProfitableToInvert = |
| 1153 | [&](Value *V, Value *&NotV, bool &ElidesXor) { |
| 1154 | if (match(V, m_Not(m_Value(NotV)))) { |
| 1155 | // If V has at most 2 uses then we can get rid of the xor operation |
| 1156 | // entirely. |
| 1157 | ElidesXor |= !V->hasNUsesOrMore(3); |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | if (IsFreeToInvert(V, !V->hasNUsesOrMore(3))) { |
| 1162 | NotV = nullptr; |
| 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | return false; |
| 1167 | }; |
| 1168 | |
| 1169 | Value *NotA, *NotB, *NotC; |
| 1170 | bool ElidesXor = false; |
| 1171 | |
| 1172 | // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C) |
| 1173 | // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C) |
| 1174 | // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C) |
| 1175 | // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C) |
| 1176 | // |
| 1177 | // This transform is performance neutral if we can elide at least one xor from |
| 1178 | // the set of three operands, since we'll be tacking on an xor at the very |
| 1179 | // end. |
Anna Thomas | ec36f3b | 2017-02-21 14:40:28 +0000 | [diff] [blame] | 1180 | if (SelectPatternResult::isMinOrMax(SPF1) && |
| 1181 | SelectPatternResult::isMinOrMax(SPF2) && |
| 1182 | IsFreeOrProfitableToInvert(A, NotA, ElidesXor) && |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1183 | IsFreeOrProfitableToInvert(B, NotB, ElidesXor) && |
| 1184 | IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) { |
| 1185 | if (!NotA) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1186 | NotA = Builder.CreateNot(A); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1187 | if (!NotB) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1188 | NotB = Builder.CreateNot(B); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1189 | if (!NotC) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1190 | NotC = Builder.CreateNot(C); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1191 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1192 | Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA, |
| 1193 | NotB); |
| 1194 | Value *NewOuter = Builder.CreateNot( |
| 1195 | createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC)); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1196 | return replaceInstUsesWith(Outer, NewOuter); |
Sanjoy Das | 08e95b4 | 2015-04-30 04:56:04 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1199 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1202 | /// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))). |
| 1203 | /// This is even legal for FP. |
| 1204 | static Instruction *foldAddSubSelect(SelectInst &SI, |
| 1205 | InstCombiner::BuilderTy &Builder) { |
| 1206 | Value *CondVal = SI.getCondition(); |
| 1207 | Value *TrueVal = SI.getTrueValue(); |
| 1208 | Value *FalseVal = SI.getFalseValue(); |
| 1209 | auto *TI = dyn_cast<Instruction>(TrueVal); |
| 1210 | auto *FI = dyn_cast<Instruction>(FalseVal); |
| 1211 | if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse()) |
| 1212 | return nullptr; |
| 1213 | |
| 1214 | Instruction *AddOp = nullptr, *SubOp = nullptr; |
| 1215 | if ((TI->getOpcode() == Instruction::Sub && |
| 1216 | FI->getOpcode() == Instruction::Add) || |
| 1217 | (TI->getOpcode() == Instruction::FSub && |
| 1218 | FI->getOpcode() == Instruction::FAdd)) { |
| 1219 | AddOp = FI; |
| 1220 | SubOp = TI; |
| 1221 | } else if ((FI->getOpcode() == Instruction::Sub && |
| 1222 | TI->getOpcode() == Instruction::Add) || |
| 1223 | (FI->getOpcode() == Instruction::FSub && |
| 1224 | TI->getOpcode() == Instruction::FAdd)) { |
| 1225 | AddOp = TI; |
| 1226 | SubOp = FI; |
| 1227 | } |
| 1228 | |
| 1229 | if (AddOp) { |
| 1230 | Value *OtherAddOp = nullptr; |
| 1231 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 1232 | OtherAddOp = AddOp->getOperand(1); |
| 1233 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 1234 | OtherAddOp = AddOp->getOperand(0); |
| 1235 | } |
| 1236 | |
| 1237 | if (OtherAddOp) { |
| 1238 | // So at this point we know we have (Y -> OtherAddOp): |
| 1239 | // select C, (add X, Y), (sub X, Z) |
| 1240 | Value *NegVal; // Compute -Z |
| 1241 | if (SI.getType()->isFPOrFPVectorTy()) { |
| 1242 | NegVal = Builder.CreateFNeg(SubOp->getOperand(1)); |
| 1243 | if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) { |
| 1244 | FastMathFlags Flags = AddOp->getFastMathFlags(); |
| 1245 | Flags &= SubOp->getFastMathFlags(); |
| 1246 | NegInst->setFastMathFlags(Flags); |
| 1247 | } |
| 1248 | } else { |
| 1249 | NegVal = Builder.CreateNeg(SubOp->getOperand(1)); |
| 1250 | } |
| 1251 | |
| 1252 | Value *NewTrueOp = OtherAddOp; |
| 1253 | Value *NewFalseOp = NegVal; |
| 1254 | if (AddOp != TI) |
| 1255 | std::swap(NewTrueOp, NewFalseOp); |
| 1256 | Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp, |
Xinliang David Li | cad3a99 | 2016-08-25 00:26:32 +0000 | [diff] [blame] | 1257 | SI.getName() + ".p", &SI); |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (SI.getType()->isFPOrFPVectorTy()) { |
| 1260 | Instruction *RI = |
| 1261 | BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel); |
| 1262 | |
| 1263 | FastMathFlags Flags = AddOp->getFastMathFlags(); |
| 1264 | Flags &= SubOp->getFastMathFlags(); |
| 1265 | RI->setFastMathFlags(Flags); |
| 1266 | return RI; |
| 1267 | } else |
| 1268 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
| 1269 | } |
| 1270 | } |
| 1271 | return nullptr; |
| 1272 | } |
| 1273 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1274 | Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame] | 1275 | Constant *C; |
| 1276 | if (!match(Sel.getTrueValue(), m_Constant(C)) && |
| 1277 | !match(Sel.getFalseValue(), m_Constant(C))) |
| 1278 | return nullptr; |
| 1279 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1280 | Instruction *ExtInst; |
| 1281 | if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) && |
| 1282 | !match(Sel.getFalseValue(), m_Instruction(ExtInst))) |
| 1283 | return nullptr; |
| 1284 | |
| 1285 | auto ExtOpcode = ExtInst->getOpcode(); |
| 1286 | if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt) |
| 1287 | return nullptr; |
| 1288 | |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame] | 1289 | // If we are extending from a boolean type or if we can create a select that |
| 1290 | // has the same size operands as its condition, try to narrow the select. |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1291 | Value *X = ExtInst->getOperand(0); |
| 1292 | Type *SmallType = X->getType(); |
Sanjay Patel | 26368cd | 2018-05-31 19:55:27 +0000 | [diff] [blame] | 1293 | Value *Cond = Sel.getCondition(); |
| 1294 | auto *Cmp = dyn_cast<CmpInst>(Cond); |
| 1295 | if (!SmallType->isIntOrIntVectorTy(1) && |
| 1296 | (!Cmp || Cmp->getOperand(0)->getType() != SmallType)) |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1297 | return nullptr; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1298 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1299 | // If the constant is the same after truncation to the smaller type and |
| 1300 | // extension to the original type, we can narrow the select. |
| 1301 | Type *SelType = Sel.getType(); |
| 1302 | Constant *TruncC = ConstantExpr::getTrunc(C, SmallType); |
| 1303 | Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType); |
| 1304 | if (ExtC == C) { |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1305 | Value *TruncCVal = cast<Value>(TruncC); |
| 1306 | if (ExtInst == Sel.getFalseValue()) |
| 1307 | std::swap(X, TruncCVal); |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1308 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1309 | // select Cond, (ext X), C --> ext(select Cond, X, C') |
| 1310 | // select Cond, C, (ext X) --> ext(select Cond, C', X) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1311 | Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel); |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1312 | return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType); |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1313 | } |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1314 | |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1315 | // If one arm of the select is the extend of the condition, replace that arm |
| 1316 | // with the extension of the appropriate known bool value. |
| 1317 | if (Cond == X) { |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1318 | if (ExtInst == Sel.getTrueValue()) { |
| 1319 | // select X, (sext X), C --> select X, -1, C |
| 1320 | // select X, (zext X), C --> select X, 1, C |
| 1321 | Constant *One = ConstantInt::getTrue(SmallType); |
| 1322 | Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType); |
Sanjay Patel | 91e73a7 | 2016-11-26 15:01:59 +0000 | [diff] [blame] | 1323 | return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel); |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1324 | } else { |
| 1325 | // select X, C, (sext X) --> select X, C, 0 |
| 1326 | // select X, C, (zext X) --> select X, C, 0 |
| 1327 | Constant *Zero = ConstantInt::getNullValue(SelType); |
Sanjay Patel | 91e73a7 | 2016-11-26 15:01:59 +0000 | [diff] [blame] | 1328 | return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel); |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1329 | } |
Sanjay Patel | 4326c4a | 2016-10-07 17:53:07 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1332 | return nullptr; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1335 | /// Try to transform a vector select with a constant condition vector into a |
| 1336 | /// shuffle for easier combining with other shuffles and insert/extract. |
| 1337 | static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) { |
| 1338 | Value *CondVal = SI.getCondition(); |
| 1339 | Constant *CondC; |
| 1340 | if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC))) |
| 1341 | return nullptr; |
| 1342 | |
| 1343 | unsigned NumElts = CondVal->getType()->getVectorNumElements(); |
| 1344 | SmallVector<Constant *, 16> Mask; |
| 1345 | Mask.reserve(NumElts); |
| 1346 | Type *Int32Ty = Type::getInt32Ty(CondVal->getContext()); |
| 1347 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1348 | Constant *Elt = CondC->getAggregateElement(i); |
| 1349 | if (!Elt) |
| 1350 | return nullptr; |
| 1351 | |
| 1352 | if (Elt->isOneValue()) { |
| 1353 | // If the select condition element is true, choose from the 1st vector. |
| 1354 | Mask.push_back(ConstantInt::get(Int32Ty, i)); |
| 1355 | } else if (Elt->isNullValue()) { |
| 1356 | // If the select condition element is false, choose from the 2nd vector. |
| 1357 | Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts)); |
| 1358 | } else if (isa<UndefValue>(Elt)) { |
Sanjay Patel | 6e41018 | 2017-04-12 18:39:53 +0000 | [diff] [blame] | 1359 | // Undef in a select condition (choose one of the operands) does not mean |
| 1360 | // the same thing as undef in a shuffle mask (any value is acceptable), so |
| 1361 | // give up. |
| 1362 | return nullptr; |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1363 | } else { |
| 1364 | // Bail out on a constant expression. |
| 1365 | return nullptr; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), |
| 1370 | ConstantVector::get(Mask)); |
| 1371 | } |
| 1372 | |
Sanjay Patel | 978f827 | 2016-10-29 15:22:04 +0000 | [diff] [blame] | 1373 | /// Reuse bitcasted operands between a compare and select: |
| 1374 | /// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> |
| 1375 | /// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D)) |
| 1376 | static Instruction *foldSelectCmpBitcasts(SelectInst &Sel, |
| 1377 | InstCombiner::BuilderTy &Builder) { |
| 1378 | Value *Cond = Sel.getCondition(); |
| 1379 | Value *TVal = Sel.getTrueValue(); |
| 1380 | Value *FVal = Sel.getFalseValue(); |
| 1381 | |
| 1382 | CmpInst::Predicate Pred; |
| 1383 | Value *A, *B; |
| 1384 | if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) |
| 1385 | return nullptr; |
| 1386 | |
| 1387 | // The select condition is a compare instruction. If the select's true/false |
| 1388 | // values are already the same as the compare operands, there's nothing to do. |
| 1389 | if (TVal == A || TVal == B || FVal == A || FVal == B) |
| 1390 | return nullptr; |
| 1391 | |
| 1392 | Value *C, *D; |
| 1393 | if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D)))) |
| 1394 | return nullptr; |
| 1395 | |
| 1396 | // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc) |
| 1397 | Value *TSrc, *FSrc; |
| 1398 | if (!match(TVal, m_BitCast(m_Value(TSrc))) || |
| 1399 | !match(FVal, m_BitCast(m_Value(FSrc)))) |
| 1400 | return nullptr; |
| 1401 | |
| 1402 | // If the select true/false values are *different bitcasts* of the same source |
| 1403 | // operands, make the select operands the same as the compare operands and |
| 1404 | // cast the result. This is the canonical select form for min/max. |
| 1405 | Value *NewSel; |
| 1406 | if (TSrc == C && FSrc == D) { |
| 1407 | // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> |
| 1408 | // bitcast (select (cmp A, B), A, B) |
| 1409 | NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel); |
| 1410 | } else if (TSrc == D && FSrc == C) { |
| 1411 | // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) --> |
| 1412 | // bitcast (select (cmp A, B), B, A) |
| 1413 | NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel); |
| 1414 | } else { |
| 1415 | return nullptr; |
| 1416 | } |
| 1417 | return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType()); |
| 1418 | } |
| 1419 | |
Matthew Simpson | b6915fb | 2017-10-31 12:34:02 +0000 | [diff] [blame] | 1420 | /// Try to eliminate select instructions that test the returned flag of cmpxchg |
| 1421 | /// instructions. |
| 1422 | /// |
| 1423 | /// If a select instruction tests the returned flag of a cmpxchg instruction and |
| 1424 | /// selects between the returned value of the cmpxchg instruction its compare |
| 1425 | /// operand, the result of the select will always be equal to its false value. |
| 1426 | /// For example: |
| 1427 | /// |
| 1428 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst |
| 1429 | /// %1 = extractvalue { i64, i1 } %0, 1 |
| 1430 | /// %2 = extractvalue { i64, i1 } %0, 0 |
| 1431 | /// %3 = select i1 %1, i64 %compare, i64 %2 |
| 1432 | /// ret i64 %3 |
| 1433 | /// |
| 1434 | /// The returned value of the cmpxchg instruction (%2) is the original value |
| 1435 | /// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2 |
| 1436 | /// must have been equal to %compare. Thus, the result of the select is always |
| 1437 | /// equal to %2, and the code can be simplified to: |
| 1438 | /// |
| 1439 | /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst |
| 1440 | /// %1 = extractvalue { i64, i1 } %0, 0 |
| 1441 | /// ret i64 %1 |
| 1442 | /// |
| 1443 | static Instruction *foldSelectCmpXchg(SelectInst &SI) { |
| 1444 | // A helper that determines if V is an extractvalue instruction whose |
| 1445 | // aggregate operand is a cmpxchg instruction and whose single index is equal |
| 1446 | // to I. If such conditions are true, the helper returns the cmpxchg |
| 1447 | // instruction; otherwise, a nullptr is returned. |
| 1448 | auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * { |
| 1449 | auto *Extract = dyn_cast<ExtractValueInst>(V); |
| 1450 | if (!Extract) |
| 1451 | return nullptr; |
| 1452 | if (Extract->getIndices()[0] != I) |
| 1453 | return nullptr; |
| 1454 | return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand()); |
| 1455 | }; |
| 1456 | |
| 1457 | // If the select has a single user, and this user is a select instruction that |
| 1458 | // we can simplify, skip the cmpxchg simplification for now. |
| 1459 | if (SI.hasOneUse()) |
| 1460 | if (auto *Select = dyn_cast<SelectInst>(SI.user_back())) |
| 1461 | if (Select->getCondition() == SI.getCondition()) |
| 1462 | if (Select->getFalseValue() == SI.getTrueValue() || |
| 1463 | Select->getTrueValue() == SI.getFalseValue()) |
| 1464 | return nullptr; |
| 1465 | |
| 1466 | // Ensure the select condition is the returned flag of a cmpxchg instruction. |
| 1467 | auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1); |
| 1468 | if (!CmpXchg) |
| 1469 | return nullptr; |
| 1470 | |
| 1471 | // Check the true value case: The true value of the select is the returned |
| 1472 | // value of the same cmpxchg used by the condition, and the false value is the |
| 1473 | // cmpxchg instruction's compare operand. |
| 1474 | if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0)) |
| 1475 | if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) { |
| 1476 | SI.setTrueValue(SI.getFalseValue()); |
| 1477 | return &SI; |
| 1478 | } |
| 1479 | |
| 1480 | // Check the false value case: The false value of the select is the returned |
| 1481 | // value of the same cmpxchg used by the condition, and the true value is the |
| 1482 | // cmpxchg instruction's compare operand. |
| 1483 | if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0)) |
| 1484 | if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) { |
| 1485 | SI.setTrueValue(SI.getFalseValue()); |
| 1486 | return &SI; |
| 1487 | } |
| 1488 | |
| 1489 | return nullptr; |
| 1490 | } |
| 1491 | |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1492 | /// Reduce a sequence of min/max with a common operand. |
| 1493 | static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS, |
| 1494 | Value *RHS, |
| 1495 | InstCombiner::BuilderTy &Builder) { |
| 1496 | assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max"); |
| 1497 | // TODO: Allow FP min/max with nnan/nsz. |
| 1498 | if (!LHS->getType()->isIntOrIntVectorTy()) |
| 1499 | return nullptr; |
| 1500 | |
| 1501 | // Match 3 of the same min/max ops. Example: umin(umin(), umin()). |
| 1502 | Value *A, *B, *C, *D; |
| 1503 | SelectPatternResult L = matchSelectPattern(LHS, A, B); |
| 1504 | SelectPatternResult R = matchSelectPattern(RHS, C, D); |
| 1505 | if (SPF != L.Flavor || L.Flavor != R.Flavor) |
| 1506 | return nullptr; |
| 1507 | |
| 1508 | // Look for a common operand. The use checks are different than usual because |
| 1509 | // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by |
| 1510 | // the select. |
| 1511 | Value *MinMaxOp = nullptr; |
| 1512 | Value *ThirdOp = nullptr; |
Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 1513 | if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) { |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1514 | // If the LHS is only used in this chain and the RHS is used outside of it, |
| 1515 | // reuse the RHS min/max because that will eliminate the LHS. |
| 1516 | if (D == A || C == A) { |
| 1517 | // min(min(a, b), min(c, a)) --> min(min(c, a), b) |
| 1518 | // min(min(a, b), min(a, d)) --> min(min(a, d), b) |
| 1519 | MinMaxOp = RHS; |
| 1520 | ThirdOp = B; |
| 1521 | } else if (D == B || C == B) { |
| 1522 | // min(min(a, b), min(c, b)) --> min(min(c, b), a) |
| 1523 | // min(min(a, b), min(b, d)) --> min(min(b, d), a) |
| 1524 | MinMaxOp = RHS; |
| 1525 | ThirdOp = A; |
| 1526 | } |
Craig Topper | ee99aa4 | 2018-03-12 18:46:05 +0000 | [diff] [blame] | 1527 | } else if (!RHS->hasNUsesOrMore(3)) { |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1528 | // Reuse the LHS. This will eliminate the RHS. |
| 1529 | if (D == A || D == B) { |
| 1530 | // min(min(a, b), min(c, a)) --> min(min(a, b), c) |
| 1531 | // min(min(a, b), min(c, b)) --> min(min(a, b), c) |
| 1532 | MinMaxOp = LHS; |
| 1533 | ThirdOp = C; |
| 1534 | } else if (C == A || C == B) { |
| 1535 | // min(min(a, b), min(b, d)) --> min(min(a, b), d) |
| 1536 | // min(min(a, b), min(c, b)) --> min(min(a, b), d) |
| 1537 | MinMaxOp = LHS; |
| 1538 | ThirdOp = D; |
| 1539 | } |
| 1540 | } |
| 1541 | if (!MinMaxOp || !ThirdOp) |
| 1542 | return nullptr; |
| 1543 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1544 | CmpInst::Predicate P = getMinMaxPred(SPF); |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1545 | Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp); |
| 1546 | return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp); |
| 1547 | } |
| 1548 | |
Sanjay Patel | f8f1227 | 2018-11-13 22:47:24 +0000 | [diff] [blame] | 1549 | /// Try to reduce a rotate pattern that includes a compare and select into a |
| 1550 | /// sequence of ALU ops only. Example: |
| 1551 | /// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b))) |
| 1552 | /// --> (a >> (-b & 31)) | (a << (b & 31)) |
| 1553 | static Instruction *foldSelectRotate(SelectInst &Sel, |
| 1554 | InstCombiner::BuilderTy &Builder) { |
| 1555 | // The false value of the select must be a rotate of the true value. |
| 1556 | Value *Or0, *Or1; |
| 1557 | if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_Value(Or0), m_Value(Or1))))) |
| 1558 | return nullptr; |
| 1559 | |
| 1560 | Value *TVal = Sel.getTrueValue(); |
| 1561 | Value *SA0, *SA1; |
| 1562 | if (!match(Or0, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA0)))) || |
| 1563 | !match(Or1, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA1))))) |
| 1564 | return nullptr; |
| 1565 | |
| 1566 | auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode(); |
| 1567 | auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode(); |
| 1568 | if (ShiftOpcode0 == ShiftOpcode1) |
| 1569 | return nullptr; |
| 1570 | |
| 1571 | // We have one of these patterns so far: |
| 1572 | // select ?, TVal, (or (lshr TVal, SA0), (shl TVal, SA1)) |
| 1573 | // select ?, TVal, (or (shl TVal, SA0), (lshr TVal, SA1)) |
| 1574 | // This must be a power-of-2 rotate for a bitmasking transform to be valid. |
| 1575 | unsigned Width = Sel.getType()->getScalarSizeInBits(); |
| 1576 | if (!isPowerOf2_32(Width)) |
| 1577 | return nullptr; |
| 1578 | |
| 1579 | // Check the shift amounts to see if they are an opposite pair. |
| 1580 | Value *ShAmt; |
| 1581 | if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0))))) |
| 1582 | ShAmt = SA0; |
| 1583 | else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1))))) |
| 1584 | ShAmt = SA1; |
| 1585 | else |
| 1586 | return nullptr; |
| 1587 | |
| 1588 | // Finally, see if the select is filtering out a shift-by-zero. |
| 1589 | Value *Cond = Sel.getCondition(); |
| 1590 | ICmpInst::Predicate Pred; |
| 1591 | if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) || |
| 1592 | Pred != ICmpInst::ICMP_EQ) |
| 1593 | return nullptr; |
| 1594 | |
| 1595 | // This is a rotate that avoids shift-by-bitwidth UB in a suboptimal way. |
| 1596 | // Convert to safely bitmasked shifts. |
| 1597 | // TODO: When we can canonicalize to funnel shift intrinsics without risk of |
| 1598 | // performance regressions, replace this sequence with that call. |
| 1599 | Value *NegShAmt = Builder.CreateNeg(ShAmt); |
| 1600 | Value *MaskedShAmt = Builder.CreateAnd(ShAmt, Width - 1); |
| 1601 | Value *MaskedNegShAmt = Builder.CreateAnd(NegShAmt, Width - 1); |
| 1602 | Value *NewSA0 = ShAmt == SA0 ? MaskedShAmt : MaskedNegShAmt; |
| 1603 | Value *NewSA1 = ShAmt == SA1 ? MaskedShAmt : MaskedNegShAmt; |
| 1604 | Value *NewSh0 = Builder.CreateBinOp(ShiftOpcode0, TVal, NewSA0); |
| 1605 | Value *NewSh1 = Builder.CreateBinOp(ShiftOpcode1, TVal, NewSA1); |
| 1606 | return BinaryOperator::CreateOr(NewSh0, NewSh1); |
| 1607 | } |
| 1608 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1609 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
| 1610 | Value *CondVal = SI.getCondition(); |
| 1611 | Value *TrueVal = SI.getTrueValue(); |
| 1612 | Value *FalseVal = SI.getFalseValue(); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1613 | Type *SelType = SI.getType(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1614 | |
Wei Mi | fc0e245 | 2017-07-25 23:37:17 +0000 | [diff] [blame] | 1615 | // FIXME: Remove this workaround when freeze related patches are done. |
| 1616 | // For select with undef operand which feeds into an equality comparison, |
| 1617 | // don't simplify it so loop unswitch can know the equality comparison |
| 1618 | // may have an undef operand. This is a workaround for PR31652 caused by |
| 1619 | // descrepancy about branch on undef between LoopUnswitch and GVN. |
| 1620 | if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) { |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1621 | if (llvm::any_of(SI.users(), [&](User *U) { |
Wei Mi | fc0e245 | 2017-07-25 23:37:17 +0000 | [diff] [blame] | 1622 | ICmpInst *CI = dyn_cast<ICmpInst>(U); |
| 1623 | if (CI && CI->isEquality()) |
| 1624 | return true; |
| 1625 | return false; |
| 1626 | })) { |
| 1627 | return nullptr; |
| 1628 | } |
| 1629 | } |
| 1630 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1631 | if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, |
| 1632 | SQ.getWithInstruction(&SI))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1633 | return replaceInstUsesWith(SI, V); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1634 | |
Sanjay Patel | f26710d | 2016-09-16 22:16:18 +0000 | [diff] [blame] | 1635 | if (Instruction *I = canonicalizeSelectToShuffle(SI)) |
| 1636 | return I; |
| 1637 | |
Sanjay Patel | 7227276 | 2017-06-27 17:53:22 +0000 | [diff] [blame] | 1638 | // Canonicalize a one-use integer compare with a non-canonical predicate by |
| 1639 | // inverting the predicate and swapping the select operands. This matches a |
| 1640 | // compare canonicalization for conditional branches. |
| 1641 | // TODO: Should we do the same for FP compares? |
| 1642 | CmpInst::Predicate Pred; |
| 1643 | if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) && |
| 1644 | !isCanonicalPredicate(Pred)) { |
| 1645 | // Swap true/false values and condition. |
| 1646 | CmpInst *Cond = cast<CmpInst>(CondVal); |
| 1647 | Cond->setPredicate(CmpInst::getInversePredicate(Pred)); |
| 1648 | SI.setOperand(1, FalseVal); |
| 1649 | SI.setOperand(2, TrueVal); |
| 1650 | SI.swapProfMetadata(); |
| 1651 | Worklist.Add(Cond); |
| 1652 | return &SI; |
| 1653 | } |
| 1654 | |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 1655 | if (SelType->isIntOrIntVectorTy(1) && |
Sanjay Patel | cbaac41 | 2016-07-03 14:34:39 +0000 | [diff] [blame] | 1656 | TrueVal->getType() == CondVal->getType()) { |
Sanjay Patel | ea23436 | 2016-07-06 21:01:26 +0000 | [diff] [blame] | 1657 | if (match(TrueVal, m_One())) { |
| 1658 | // Change: A = select B, true, C --> A = or B, C |
| 1659 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
| 1660 | } |
| 1661 | if (match(TrueVal, m_Zero())) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1662 | // Change: A = select B, false, C --> A = and !B, C |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1663 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1664 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1665 | } |
Sanjay Patel | ea23436 | 2016-07-06 21:01:26 +0000 | [diff] [blame] | 1666 | if (match(FalseVal, m_Zero())) { |
| 1667 | // Change: A = select B, C, false --> A = and B, C |
| 1668 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
| 1669 | } |
| 1670 | if (match(FalseVal, m_One())) { |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1671 | // Change: A = select B, C, true --> A = or !B, C |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1672 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Chris Lattner | c707fa9 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1673 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1674 | } |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1675 | |
Sanjay Patel | a1a4e10 | 2016-07-03 14:08:19 +0000 | [diff] [blame] | 1676 | // select a, a, b -> a | b |
| 1677 | // select a, b, a -> a & b |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1678 | if (CondVal == TrueVal) |
| 1679 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1680 | if (CondVal == FalseVal) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1681 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1682 | |
Sanjay Patel | a1a4e10 | 2016-07-03 14:08:19 +0000 | [diff] [blame] | 1683 | // select a, ~a, b -> (~a) & b |
| 1684 | // select a, b, ~a -> (~a) | b |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1685 | if (match(TrueVal, m_Not(m_Specific(CondVal)))) |
| 1686 | return BinaryOperator::CreateAnd(TrueVal, FalseVal); |
Jakub Staszak | 9931726 | 2013-04-19 01:18:04 +0000 | [diff] [blame] | 1687 | if (match(FalseVal, m_Not(m_Specific(CondVal)))) |
Pete Cooper | b33c297 | 2011-12-15 00:56:45 +0000 | [diff] [blame] | 1688 | return BinaryOperator::CreateOr(TrueVal, FalseVal); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1691 | // Selecting between two integer or vector splat integer constants? |
| 1692 | // |
| 1693 | // Note that we don't handle a scalar select of vectors: |
| 1694 | // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0> |
| 1695 | // because that may need 3 instructions to splat the condition value: |
| 1696 | // extend, insertelement, shufflevector. |
Craig Topper | e79b3e7 | 2017-07-09 03:25:17 +0000 | [diff] [blame] | 1697 | if (SelType->isIntOrIntVectorTy() && |
| 1698 | CondVal->getType()->isVectorTy() == SelType->isVectorTy()) { |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1699 | // select C, 1, 0 -> zext C to int |
| 1700 | if (match(TrueVal, m_One()) && match(FalseVal, m_Zero())) |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1701 | return new ZExtInst(CondVal, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1702 | |
| 1703 | // select C, -1, 0 -> sext C to int |
| 1704 | if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero())) |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1705 | return new SExtInst(CondVal, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1706 | |
| 1707 | // select C, 0, 1 -> zext !C to int |
| 1708 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1709 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1710 | return new ZExtInst(NotCond, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | // select C, 0, -1 -> sext !C to int |
| 1714 | if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1715 | Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1716 | return new SExtInst(NotCond, SelType); |
Sanjay Patel | 65a51c2 | 2016-07-06 22:23:01 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1720 | // See if we are selecting two values based on a comparison of the two values. |
| 1721 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 1722 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1723 | // Canonicalize to use ordered comparisons by swapping the select |
| 1724 | // operands. |
| 1725 | // |
| 1726 | // e.g. |
| 1727 | // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X |
| 1728 | if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { |
| 1729 | FCmpInst::Predicate InvPred = FCI->getInversePredicate(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1730 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
| 1731 | Builder.setFastMathFlags(FCI->getFastMathFlags()); |
| 1732 | Value *NewCond = Builder.CreateFCmp(InvPred, TrueVal, FalseVal, |
| 1733 | FCI->getName() + ".inv"); |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1734 | |
| 1735 | return SelectInst::Create(NewCond, FalseVal, TrueVal, |
| 1736 | SI.getName() + ".p"); |
| 1737 | } |
| 1738 | |
| 1739 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1740 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1741 | // Canonicalize to use ordered comparisons by swapping the select |
| 1742 | // operands. |
| 1743 | // |
| 1744 | // e.g. |
| 1745 | // (X ugt Y) ? X : Y -> (X ole Y) ? X : Y |
| 1746 | if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { |
| 1747 | FCmpInst::Predicate InvPred = FCI->getInversePredicate(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1748 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
| 1749 | Builder.setFastMathFlags(FCI->getFastMathFlags()); |
| 1750 | Value *NewCond = Builder.CreateFCmp(InvPred, FalseVal, TrueVal, |
| 1751 | FCI->getName() + ".inv"); |
Matt Arsenault | 238ff1a | 2014-11-24 23:15:18 +0000 | [diff] [blame] | 1752 | |
| 1753 | return SelectInst::Create(NewCond, FalseVal, TrueVal, |
| 1754 | SI.getName() + ".p"); |
| 1755 | } |
| 1756 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1757 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| 1758 | } |
Sanjay Patel | 0ce3086 | 2018-03-19 15:14:30 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need |
| 1761 | // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We |
| 1762 | // also require nnan because we do not want to unintentionally change the |
| 1763 | // sign of a NaN value. |
| 1764 | Value *X = FCI->getOperand(0); |
| 1765 | FCmpInst::Predicate Pred = FCI->getPredicate(); |
| 1766 | if (match(FCI->getOperand(1), m_AnyZeroFP()) && FCI->hasNoNaNs()) { |
| 1767 | // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X) |
| 1768 | // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 1769 | if ((X == FalseVal && Pred == FCmpInst::FCMP_OLE && |
| 1770 | match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(X)))) || |
| 1771 | (X == TrueVal && Pred == FCmpInst::FCMP_OGT && |
| 1772 | match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(X))))) { |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 1773 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, FCI); |
Sanjay Patel | 0ce3086 | 2018-03-19 15:14:30 +0000 | [diff] [blame] | 1774 | return replaceInstUsesWith(SI, Fabs); |
| 1775 | } |
| 1776 | // With nsz: |
| 1777 | // (X < +/-0.0) ? -X : X --> fabs(X) |
| 1778 | // (X <= +/-0.0) ? -X : X --> fabs(X) |
| 1779 | // (X > +/-0.0) ? X : -X --> fabs(X) |
| 1780 | // (X >= +/-0.0) ? X : -X --> fabs(X) |
| 1781 | if (FCI->hasNoSignedZeros() && |
| 1782 | ((X == FalseVal && match(TrueVal, m_FNeg(m_Specific(X))) && |
| 1783 | (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE)) || |
| 1784 | (X == TrueVal && match(FalseVal, m_FNeg(m_Specific(X))) && |
| 1785 | (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE)))) { |
Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 1786 | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, FCI); |
Sanjay Patel | 0ce3086 | 2018-03-19 15:14:30 +0000 | [diff] [blame] | 1787 | return replaceInstUsesWith(SI, Fabs); |
| 1788 | } |
| 1789 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | // See if we are selecting two values based on a comparison of the two values. |
| 1793 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1794 | if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1795 | return Result; |
| 1796 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1797 | if (Instruction *Add = foldAddSubSelect(SI, Builder)) |
Sanjay Patel | 3929313 | 2016-06-08 21:10:01 +0000 | [diff] [blame] | 1798 | return Add; |
| 1799 | |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1800 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
Sanjay Patel | 10a2c38 | 2016-06-08 20:09:04 +0000 | [diff] [blame] | 1801 | auto *TI = dyn_cast<Instruction>(TrueVal); |
| 1802 | auto *FI = dyn_cast<Instruction>(FalseVal); |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1803 | if (TI && FI && TI->getOpcode() == FI->getOpcode()) |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1804 | if (Instruction *IV = foldSelectOpOp(SI, TI, FI)) |
Sanjay Patel | 216d8cf | 2016-06-17 16:46:50 +0000 | [diff] [blame] | 1805 | return IV; |
Sanjay Patel | 10a2c38 | 2016-06-08 20:09:04 +0000 | [diff] [blame] | 1806 | |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 1807 | if (Instruction *I = foldSelectExtConst(SI)) |
| 1808 | return I; |
Nicolai Haehnle | 870bf17 | 2016-08-05 08:22:29 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1810 | // See if we can fold the select into one of our operands. |
Sanjay Patel | 25600f3 | 2016-07-07 15:28:17 +0000 | [diff] [blame] | 1811 | if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) { |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1812 | if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1813 | return FoldI; |
Tobias Grosser | 411e6ee | 2011-01-07 21:33:13 +0000 | [diff] [blame] | 1814 | |
Craig Topper | 24674ca | 2018-08-20 05:35:12 +0000 | [diff] [blame] | 1815 | Value *LHS, *RHS; |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1816 | Instruction::CastOps CastOp; |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1817 | SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp); |
| 1818 | auto SPF = SPR.Flavor; |
Sanjay Patel | 6f00fc3 | 2018-09-13 16:04:06 +0000 | [diff] [blame] | 1819 | if (SPF) { |
| 1820 | Value *LHS2, *RHS2; |
| 1821 | if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor) |
| 1822 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2, |
| 1823 | RHS2, SI, SPF, RHS)) |
| 1824 | return R; |
| 1825 | if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor) |
| 1826 | if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2, |
| 1827 | RHS2, SI, SPF, LHS)) |
| 1828 | return R; |
| 1829 | // TODO. |
| 1830 | // ABS(-X) -> ABS(X) |
| 1831 | } |
Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 1832 | |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1833 | if (SelectPatternResult::isMinOrMax(SPF)) { |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 1834 | // Canonicalize so that |
| 1835 | // - type casts are outside select patterns. |
| 1836 | // - float clamp is transformed to min/max pattern |
| 1837 | |
| 1838 | bool IsCastNeeded = LHS->getType() != SelType; |
| 1839 | Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0); |
| 1840 | Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1); |
| 1841 | if (IsCastNeeded || |
| 1842 | (LHS->getType()->isFPOrFPVectorTy() && |
| 1843 | ((CmpLHS != LHS && CmpLHS != RHS) || |
| 1844 | (CmpRHS != LHS && CmpRHS != RHS)))) { |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 1845 | CmpInst::Predicate Pred = getMinMaxPred(SPF, SPR.Ordered); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1846 | |
| 1847 | Value *Cmp; |
| 1848 | if (CmpInst::isIntPredicate(Pred)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1849 | Cmp = Builder.CreateICmp(Pred, LHS, RHS); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1850 | } else { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1851 | IRBuilder<>::FastMathFlagGuard FMFG(Builder); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1852 | auto FMF = cast<FPMathOperator>(SI.getCondition())->getFastMathFlags(); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1853 | Builder.setFastMathFlags(FMF); |
| 1854 | Cmp = Builder.CreateFCmp(Pred, LHS, RHS); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 1857 | Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI); |
| 1858 | if (!IsCastNeeded) |
| 1859 | return replaceInstUsesWith(SI, NewSI); |
| 1860 | |
| 1861 | Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType); |
| 1862 | return replaceInstUsesWith(SI, NewCast); |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 1863 | } |
Sanjay Patel | 5b6aacf | 2018-01-05 19:01:17 +0000 | [diff] [blame] | 1864 | |
| 1865 | // MAX(~a, ~b) -> ~MIN(a, b) |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1866 | // MAX(~a, C) -> ~MIN(a, ~C) |
Sanjay Patel | 5b6aacf | 2018-01-05 19:01:17 +0000 | [diff] [blame] | 1867 | // MIN(~a, ~b) -> ~MAX(a, b) |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1868 | // MIN(~a, C) -> ~MAX(a, ~C) |
Mandeep Singh Grang | 0905fc7 | 2018-11-14 17:55:07 +0000 | [diff] [blame] | 1869 | auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * { |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1870 | Value *A; |
| 1871 | if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) && |
| 1872 | !IsFreeToInvert(A, A->hasOneUse()) && |
| 1873 | // Passing false to only consider m_Not and constants. |
| 1874 | IsFreeToInvert(Y, false)) { |
| 1875 | Value *B = Builder.CreateNot(Y); |
| 1876 | Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF), |
| 1877 | A, B); |
| 1878 | // Copy the profile metadata. |
| 1879 | if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) { |
| 1880 | cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD); |
| 1881 | // Swap the metadata if the operands are swapped. |
Mandeep Singh Grang | 0905fc7 | 2018-11-14 17:55:07 +0000 | [diff] [blame] | 1882 | if (X == SI.getFalseValue() && Y == SI.getTrueValue()) |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1883 | cast<SelectInst>(NewMinMax)->swapProfMetadata(); |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | return BinaryOperator::CreateNot(NewMinMax); |
| 1887 | } |
| 1888 | |
| 1889 | return nullptr; |
| 1890 | }; |
| 1891 | |
Mandeep Singh Grang | 0905fc7 | 2018-11-14 17:55:07 +0000 | [diff] [blame] | 1892 | if (Instruction *I = moveNotAfterMinMax(LHS, RHS)) |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1893 | return I; |
Mandeep Singh Grang | 0905fc7 | 2018-11-14 17:55:07 +0000 | [diff] [blame] | 1894 | if (Instruction *I = moveNotAfterMinMax(RHS, LHS)) |
Craig Topper | 2b3f5df | 2018-09-22 05:53:27 +0000 | [diff] [blame] | 1895 | return I; |
Sanjay Patel | 31b4b76 | 2018-01-08 15:05:34 +0000 | [diff] [blame] | 1896 | |
| 1897 | if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder)) |
| 1898 | return I; |
Sanjoy Das | 9fe86d9 | 2015-12-05 23:44:22 +0000 | [diff] [blame] | 1899 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | // See if we can fold the select into a phi node if the condition is a select. |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1903 | if (auto *PN = dyn_cast<PHINode>(SI.getCondition())) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1904 | // The true/false values have to be live in the PHI predecessor's blocks. |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 1905 | if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && |
| 1906 | canSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1907 | if (Instruction *NV = foldOpIntoPhi(SI, PN)) |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 1908 | return NV; |
| 1909 | |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1910 | if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) { |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1911 | if (TrueSI->getCondition()->getType() == CondVal->getType()) { |
| 1912 | // select(C, select(C, a, b), c) -> select(C, a, c) |
| 1913 | if (TrueSI->getCondition() == CondVal) { |
| 1914 | if (SI.getTrueValue() == TrueSI->getTrueValue()) |
| 1915 | return nullptr; |
| 1916 | SI.setOperand(1, TrueSI->getTrueValue()); |
| 1917 | return &SI; |
| 1918 | } |
| 1919 | // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b) |
| 1920 | // We choose this as normal form to enable folding on the And and shortening |
| 1921 | // paths for the values (this helps GetUnderlyingObjects() for example). |
| 1922 | if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1923 | Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition()); |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1924 | SI.setOperand(0, And); |
| 1925 | SI.setOperand(1, TrueSI->getTrueValue()); |
| 1926 | return &SI; |
| 1927 | } |
Matthias Braun | 2e40459 | 2015-02-06 17:49:36 +0000 | [diff] [blame] | 1928 | } |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1929 | } |
| 1930 | if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) { |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1931 | if (FalseSI->getCondition()->getType() == CondVal->getType()) { |
| 1932 | // select(C, a, select(C, b, c)) -> select(C, a, c) |
| 1933 | if (FalseSI->getCondition() == CondVal) { |
| 1934 | if (SI.getFalseValue() == FalseSI->getFalseValue()) |
| 1935 | return nullptr; |
| 1936 | SI.setOperand(2, FalseSI->getFalseValue()); |
| 1937 | return &SI; |
| 1938 | } |
| 1939 | // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b) |
| 1940 | if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1941 | Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition()); |
David Majnemer | 1bacc0a | 2015-03-03 22:40:36 +0000 | [diff] [blame] | 1942 | SI.setOperand(0, Or); |
| 1943 | SI.setOperand(2, FalseSI->getFalseValue()); |
| 1944 | return &SI; |
| 1945 | } |
Matthias Braun | 2e40459 | 2015-02-06 17:49:36 +0000 | [diff] [blame] | 1946 | } |
Nick Lewycky | b074e32 | 2011-01-28 03:28:10 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1949 | auto canMergeSelectThroughBinop = [](BinaryOperator *BO) { |
| 1950 | // The select might be preventing a division by 0. |
| 1951 | switch (BO->getOpcode()) { |
| 1952 | default: |
| 1953 | return true; |
| 1954 | case Instruction::SRem: |
| 1955 | case Instruction::URem: |
| 1956 | case Instruction::SDiv: |
| 1957 | case Instruction::UDiv: |
| 1958 | return false; |
| 1959 | } |
| 1960 | }; |
| 1961 | |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1962 | // Try to simplify a binop sandwiched between 2 selects with the same |
| 1963 | // condition. |
| 1964 | // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z) |
| 1965 | BinaryOperator *TrueBO; |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1966 | if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && |
| 1967 | canMergeSelectThroughBinop(TrueBO)) { |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1968 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) { |
| 1969 | if (TrueBOSI->getCondition() == CondVal) { |
| 1970 | TrueBO->setOperand(0, TrueBOSI->getTrueValue()); |
| 1971 | Worklist.Add(TrueBO); |
| 1972 | return &SI; |
| 1973 | } |
| 1974 | } |
| 1975 | if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) { |
| 1976 | if (TrueBOSI->getCondition() == CondVal) { |
| 1977 | TrueBO->setOperand(1, TrueBOSI->getTrueValue()); |
| 1978 | Worklist.Add(TrueBO); |
| 1979 | return &SI; |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W)) |
| 1985 | BinaryOperator *FalseBO; |
Craig Topper | 1c19cc1 | 2018-02-14 18:08:33 +0000 | [diff] [blame] | 1986 | if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && |
| 1987 | canMergeSelectThroughBinop(FalseBO)) { |
Craig Topper | f7b8672 | 2017-11-15 05:23:02 +0000 | [diff] [blame] | 1988 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) { |
| 1989 | if (FalseBOSI->getCondition() == CondVal) { |
| 1990 | FalseBO->setOperand(0, FalseBOSI->getFalseValue()); |
| 1991 | Worklist.Add(FalseBO); |
| 1992 | return &SI; |
| 1993 | } |
| 1994 | } |
| 1995 | if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) { |
| 1996 | if (FalseBOSI->getCondition() == CondVal) { |
| 1997 | FalseBO->setOperand(1, FalseBOSI->getFalseValue()); |
| 1998 | Worklist.Add(FalseBO); |
| 1999 | return &SI; |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
Sanjay Patel | 747feb2 | 2018-10-23 15:05:12 +0000 | [diff] [blame] | 2004 | Value *NotCond; |
| 2005 | if (match(CondVal, m_Not(m_Value(NotCond)))) { |
| 2006 | SI.setOperand(0, NotCond); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2007 | SI.setOperand(1, FalseVal); |
| 2008 | SI.setOperand(2, TrueVal); |
Sanjay Patel | ad76c68 | 2018-10-23 14:43:31 +0000 | [diff] [blame] | 2009 | SI.swapProfMetadata(); |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2010 | return &SI; |
| 2011 | } |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2012 | |
Sanjay Patel | 4e463b4 | 2016-09-06 18:16:31 +0000 | [diff] [blame] | 2013 | if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) { |
Pete Cooper | abc13af | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 2014 | unsigned VWidth = VecTy->getNumElements(); |
| 2015 | APInt UndefElts(VWidth, 0); |
| 2016 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 2017 | if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) { |
| 2018 | if (V != &SI) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2019 | return replaceInstUsesWith(SI, V); |
Pete Cooper | abc13af | 2012-07-26 23:10:24 +0000 | [diff] [blame] | 2020 | return &SI; |
| 2021 | } |
| 2022 | } |
| 2023 | |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 2024 | // If we can compute the condition, there's no need for a select. |
| 2025 | // Like the above fold, we are attempting to reduce compile-time cost by |
| 2026 | // putting this fold here with limitations rather than in InstSimplify. |
| 2027 | // The motivation for this call into value tracking is to take advantage of |
| 2028 | // the assumption cache, so make sure that is populated. |
| 2029 | if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2030 | KnownBits Known(1); |
| 2031 | computeKnownBits(CondVal, Known, 0, &SI); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2032 | if (Known.One.isOneValue()) |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 2033 | return replaceInstUsesWith(SI, TrueVal); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2034 | if (Known.Zero.isOneValue()) |
Sanjay Patel | 5178363 | 2017-01-13 17:02:42 +0000 | [diff] [blame] | 2035 | return replaceInstUsesWith(SI, FalseVal); |
| 2036 | } |
| 2037 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2038 | if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder)) |
Sanjay Patel | 978f827 | 2016-10-29 15:22:04 +0000 | [diff] [blame] | 2039 | return BitCastSel; |
| 2040 | |
Matthew Simpson | b6915fb | 2017-10-31 12:34:02 +0000 | [diff] [blame] | 2041 | // Simplify selects that test the returned flag of cmpxchg instructions. |
| 2042 | if (Instruction *Select = foldSelectCmpXchg(SI)) |
| 2043 | return Select; |
| 2044 | |
David Bolvansky | 43b0e25 | 2018-08-23 15:22:15 +0000 | [diff] [blame] | 2045 | if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI)) |
David Bolvansky | 6737b3a | 2018-07-30 20:38:53 +0000 | [diff] [blame] | 2046 | return Select; |
| 2047 | |
Sanjay Patel | f8f1227 | 2018-11-13 22:47:24 +0000 | [diff] [blame] | 2048 | if (Instruction *Rot = foldSelectRotate(SI, Builder)) |
| 2049 | return Rot; |
| 2050 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2051 | return nullptr; |
Chris Lattner | 8f771cb | 2010-01-05 06:03:12 +0000 | [diff] [blame] | 2052 | } |