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