Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 1 | //===- InstCombineShifts.cpp ----------------------------------------------===// |
| 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 | // |
| 10 | // This file implements the visitShl, visitLShr, and visitAShr functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eli Friedman | 911e12f | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace PatternMatch; |
| 21 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "instcombine" |
| 23 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 24 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 25 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 26 | assert(Op0->getType() == Op1->getType()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 27 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 28 | // See if we can fold away this shift. |
| 29 | if (SimplifyDemandedInstructionBits(I)) |
| 30 | return &I; |
| 31 | |
| 32 | // Try to fold constant and into select arguments. |
| 33 | if (isa<Constant>(Op0)) |
| 34 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 35 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 36 | return R; |
| 37 | |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 38 | if (Constant *CUI = dyn_cast<Constant>(Op1)) |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 39 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 40 | return Res; |
Benjamin Kramer | b5afa65 | 2010-11-23 18:52:42 +0000 | [diff] [blame] | 41 | |
Simon Pilgrim | 6dd8fab | 2016-11-01 15:40:30 +0000 | [diff] [blame] | 42 | // (C1 shift (A add C2)) -> (C1 shift C2) shift A) |
| 43 | // iff A and C2 are both positive. |
| 44 | Value *A; |
| 45 | Constant *C; |
| 46 | if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C)))) |
| 47 | if (isKnownNonNegative(A, DL) && isKnownNonNegative(C, DL)) |
| 48 | return BinaryOperator::Create( |
| 49 | I.getOpcode(), Builder->CreateBinOp(I.getOpcode(), Op0, C), A); |
| 50 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 51 | // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2. |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 52 | // Because shifts by negative values (which could occur if A were negative) |
| 53 | // are undefined. |
Simon Pilgrim | 6dd8fab | 2016-11-01 15:40:30 +0000 | [diff] [blame] | 54 | const APInt *B; |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 55 | if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) { |
| 56 | // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't |
| 57 | // demand the sign bit (and many others) here?? |
| 58 | Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1), |
| 59 | Op1->getName()); |
| 60 | I.setOperand(1, Rem); |
| 61 | return &I; |
| 62 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 63 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 64 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 67 | /// Return true if we can simplify two logical (either left or right) shifts |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 68 | /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2. |
| 69 | static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, |
| 70 | Instruction *InnerShift, InstCombiner &IC, |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 71 | Instruction *CxtI) { |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 72 | assert(InnerShift->isLogicalShift() && "Unexpected instruction type"); |
Sanjay Patel | b91bcd7 | 2016-04-11 17:35:57 +0000 | [diff] [blame] | 73 | |
Sanjay Patel | ab8b32d | 2017-01-16 19:35:45 +0000 | [diff] [blame] | 74 | // We need constant scalar or constant splat shifts. |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 75 | const APInt *InnerShiftConst; |
| 76 | if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst))) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 77 | return false; |
| 78 | |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 79 | // Two logical shifts in the same direction: |
| 80 | // shl (shl X, C1), C2 --> shl X, C1 + C2 |
| 81 | // lshr (lshr X, C1), C2 --> lshr X, C1 + C2 |
| 82 | bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl; |
| 83 | if (IsInnerShl == IsOuterShl) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 84 | return true; |
| 85 | |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 86 | // Equal shift amounts in opposite directions become bitwise 'and': |
| 87 | // lshr (shl X, C), C --> and X, C' |
| 88 | // shl (lshr X, C), C --> and X, C' |
| 89 | unsigned InnerShAmt = InnerShiftConst->getZExtValue(); |
| 90 | if (InnerShAmt == OuterShAmt) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 91 | return true; |
| 92 | |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 93 | // If the 2nd shift is bigger than the 1st, we can fold: |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 94 | // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3 |
| 95 | // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3 |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 96 | // but it isn't profitable unless we know the and'd out bits are already zero. |
Sanjay Patel | 65cce20 | 2017-01-16 20:05:26 +0000 | [diff] [blame] | 97 | // Also, check that the inner shift is valid (less than the type width) or |
| 98 | // we'll crash trying to produce the bit mask for the 'and'. |
| 99 | unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits(); |
| 100 | if (InnerShAmt > OuterShAmt && InnerShAmt < TypeWidth) { |
| 101 | unsigned MaskShift = |
| 102 | IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt; |
| 103 | APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift; |
| 104 | if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI)) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 111 | /// See if we can compute the specified value, but shifted logically to the left |
| 112 | /// or right by some number of bits. This should return true if the expression |
| 113 | /// can be computed for the same cost as the current expression tree. This is |
| 114 | /// used to eliminate extraneous shifting from things like: |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 115 | /// %C = shl i128 %A, 64 |
| 116 | /// %D = shl i128 %B, 96 |
| 117 | /// %E = or i128 %C, %D |
| 118 | /// %F = lshr i128 %E, 64 |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 119 | /// where the client will ask if E can be computed shifted right by 64-bits. If |
| 120 | /// this succeeds, getShiftedValue() will be called to produce the value. |
| 121 | static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 122 | InstCombiner &IC, Instruction *CxtI) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 123 | // We can always evaluate constants shifted. |
| 124 | if (isa<Constant>(V)) |
| 125 | return true; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 127 | Instruction *I = dyn_cast<Instruction>(V); |
| 128 | if (!I) return false; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 130 | // If this is the opposite shift, we can directly reuse the input of the shift |
| 131 | // if the needed bits are already zero in the input. This allows us to reuse |
| 132 | // the value which means that we don't care if the shift has multiple uses. |
| 133 | // TODO: Handle opposite shift by exact value. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 134 | ConstantInt *CI = nullptr; |
Sanjay Patel | 4b9c682 | 2016-04-11 17:25:23 +0000 | [diff] [blame] | 135 | if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) || |
| 136 | (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 137 | if (CI->getZExtValue() == NumBits) { |
| 138 | // TODO: Check that the input bits are already zero with MaskedValueIsZero |
| 139 | #if 0 |
| 140 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 141 | // lshr iff we know that the bits we would otherwise be shifting in are |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 142 | // already zeros. |
| 143 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 144 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 145 | if (MaskedValueIsZero(I->getOperand(0), |
| 146 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 147 | CI->getLimitedValue(BitWidth) < BitWidth) { |
| 148 | return CanEvaluateTruncated(I->getOperand(0), Ty); |
| 149 | } |
| 150 | #endif |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 155 | // We can't mutate something that has multiple uses: doing so would |
| 156 | // require duplicating the instruction in general, which isn't profitable. |
| 157 | if (!I->hasOneUse()) return false; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 159 | switch (I->getOpcode()) { |
| 160 | default: return false; |
| 161 | case Instruction::And: |
| 162 | case Instruction::Or: |
| 163 | case Instruction::Xor: |
| 164 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 165 | return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) && |
| 166 | canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 167 | |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 168 | case Instruction::Shl: |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 169 | case Instruction::LShr: |
Sanjay Patel | 4b9c682 | 2016-04-11 17:25:23 +0000 | [diff] [blame] | 170 | return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 172 | case Instruction::Select: { |
| 173 | SelectInst *SI = cast<SelectInst>(I); |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 174 | Value *TrueVal = SI->getTrueValue(); |
| 175 | Value *FalseVal = SI->getFalseValue(); |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 176 | return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) && |
| 177 | canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 178 | } |
| 179 | case Instruction::PHI: { |
| 180 | // We can change a phi if we can change all operands. Note that we never |
| 181 | // get into trouble with cyclic PHIs here because we only consider |
| 182 | // instructions with a single use. |
| 183 | PHINode *PN = cast<PHINode>(I); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 184 | for (Value *IncValue : PN->incoming_values()) |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 185 | if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN)) |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 186 | return false; |
| 187 | return true; |
| 188 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Sanjay Patel | 646734a | 2017-01-16 17:27:50 +0000 | [diff] [blame] | 192 | /// Fold OuterShift (InnerShift X, C1), C2. |
| 193 | /// See canEvaluateShiftedShift() for the constraints on these instructions. |
| 194 | static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, |
| 195 | bool IsOuterShl, |
| 196 | InstCombiner::BuilderTy &Builder) { |
| 197 | bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl; |
| 198 | Type *ShType = InnerShift->getType(); |
| 199 | unsigned TypeWidth = ShType->getScalarSizeInBits(); |
| 200 | |
| 201 | // We only accept shifts-by-a-constant in canEvaluateShifted(). |
Sanjay Patel | ab8b32d | 2017-01-16 19:35:45 +0000 | [diff] [blame] | 202 | const APInt *C1; |
| 203 | match(InnerShift->getOperand(1), m_APInt(C1)); |
Sanjay Patel | 646734a | 2017-01-16 17:27:50 +0000 | [diff] [blame] | 204 | unsigned InnerShAmt = C1->getZExtValue(); |
| 205 | |
| 206 | // Change the shift amount and clear the appropriate IR flags. |
| 207 | auto NewInnerShift = [&](unsigned ShAmt) { |
| 208 | InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt)); |
| 209 | if (IsInnerShl) { |
| 210 | InnerShift->setHasNoUnsignedWrap(false); |
| 211 | InnerShift->setHasNoSignedWrap(false); |
| 212 | } else { |
| 213 | InnerShift->setIsExact(false); |
| 214 | } |
| 215 | return InnerShift; |
| 216 | }; |
| 217 | |
| 218 | // Two logical shifts in the same direction: |
| 219 | // shl (shl X, C1), C2 --> shl X, C1 + C2 |
| 220 | // lshr (lshr X, C1), C2 --> lshr X, C1 + C2 |
| 221 | if (IsInnerShl == IsOuterShl) { |
| 222 | // If this is an oversized composite shift, then unsigned shifts get 0. |
| 223 | if (InnerShAmt + OuterShAmt >= TypeWidth) |
| 224 | return Constant::getNullValue(ShType); |
| 225 | |
| 226 | return NewInnerShift(InnerShAmt + OuterShAmt); |
| 227 | } |
| 228 | |
| 229 | // Equal shift amounts in opposite directions become bitwise 'and': |
| 230 | // lshr (shl X, C), C --> and X, C' |
| 231 | // shl (lshr X, C), C --> and X, C' |
| 232 | if (InnerShAmt == OuterShAmt) { |
| 233 | APInt Mask = IsInnerShl |
| 234 | ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt) |
| 235 | : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt); |
| 236 | Value *And = Builder.CreateAnd(InnerShift->getOperand(0), |
| 237 | ConstantInt::get(ShType, Mask)); |
| 238 | if (auto *AndI = dyn_cast<Instruction>(And)) { |
| 239 | AndI->moveBefore(InnerShift); |
| 240 | AndI->takeName(InnerShift); |
| 241 | } |
| 242 | return And; |
| 243 | } |
| 244 | |
| 245 | assert(InnerShAmt > OuterShAmt && |
| 246 | "Unexpected opposite direction logical shift pair"); |
| 247 | |
| 248 | // In general, we would need an 'and' for this transform, but |
| 249 | // canEvaluateShiftedShift() guarantees that the masked-off bits are not used. |
| 250 | // lshr (shl X, C1), C2 --> shl X, C1 - C2 |
| 251 | // shl (lshr X, C1), C2 --> lshr X, C1 - C2 |
| 252 | return NewInnerShift(InnerShAmt - OuterShAmt); |
| 253 | } |
| 254 | |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 255 | /// When canEvaluateShifted() returns true for an expression, this function |
| 256 | /// inserts the new computation that produces the shifted value. |
| 257 | static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 258 | InstCombiner &IC, const DataLayout &DL) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 259 | // We can always evaluate constants shifted. |
| 260 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 261 | if (isLeftShift) |
| 262 | V = IC.Builder->CreateShl(C, NumBits); |
| 263 | else |
| 264 | V = IC.Builder->CreateLShr(C, NumBits); |
| 265 | // If we got a constantexpr back, try to simplify it with TD info. |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 266 | if (auto *C = dyn_cast<Constant>(V)) |
| 267 | if (auto *FoldedC = |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 268 | ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo())) |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 269 | V = FoldedC; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 270 | return V; |
| 271 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 273 | Instruction *I = cast<Instruction>(V); |
| 274 | IC.Worklist.Add(I); |
| 275 | |
| 276 | switch (I->getOpcode()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 277 | default: llvm_unreachable("Inconsistency with CanEvaluateShifted"); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 278 | case Instruction::And: |
| 279 | case Instruction::Or: |
| 280 | case Instruction::Xor: |
| 281 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 282 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 283 | 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 284 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 285 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 286 | return I; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 287 | |
Sanjay Patel | 646734a | 2017-01-16 17:27:50 +0000 | [diff] [blame] | 288 | case Instruction::Shl: |
| 289 | case Instruction::LShr: |
| 290 | return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift, |
| 291 | *(IC.Builder)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 293 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 294 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 295 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 296 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 297 | 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 298 | return I; |
| 299 | case Instruction::PHI: { |
| 300 | // We can change a phi if we can change all operands. Note that we never |
| 301 | // get into trouble with cyclic PHIs here because we only consider |
| 302 | // instructions with a single use. |
| 303 | PHINode *PN = cast<PHINode>(I); |
| 304 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame] | 305 | PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 306 | isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 307 | return PN; |
| 308 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 312 | /// Try to fold (X << C1) << C2, where the shifts are some combination of |
| 313 | /// shl/ashr/lshr. |
| 314 | static Instruction * |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 315 | foldShiftByConstOfShiftByConst(BinaryOperator &I, const APInt *COp1, |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 316 | InstCombiner::BuilderTy *Builder) { |
| 317 | Value *Op0 = I.getOperand(0); |
Sanjay Patel | 478a83c | 2017-01-21 17:59:59 +0000 | [diff] [blame] | 318 | unsigned TypeBits = Op0->getType()->getScalarSizeInBits(); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 319 | |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 320 | // Find out if this is a shift of a shift by a constant. |
| 321 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
Sanjay Patel | 478a83c | 2017-01-21 17:59:59 +0000 | [diff] [blame] | 322 | if (!ShiftOp || !ShiftOp->isShift()) |
Sanjay Patel | 5424bd2 | 2017-01-17 16:59:09 +0000 | [diff] [blame] | 323 | return nullptr; |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 324 | |
Sanjay Patel | 478a83c | 2017-01-21 17:59:59 +0000 | [diff] [blame] | 325 | const APInt *ShAmt1; |
| 326 | if (!match(ShiftOp->getOperand(1), m_APInt(ShAmt1))) |
| 327 | return nullptr; |
| 328 | |
| 329 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
| 330 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
| 331 | unsigned AmtSum = (*ShAmt1 + *COp1).getZExtValue(); |
| 332 | // If this is an oversized composite shift, then unsigned shifts become |
| 333 | // zero (handled in InstSimplify) and ashr saturates. |
| 334 | if (AmtSum >= TypeBits) { |
| 335 | if (I.getOpcode() != Instruction::AShr) |
| 336 | return nullptr; |
| 337 | AmtSum = TypeBits - 1; // Saturate to 31 for i32 ashr. |
| 338 | } |
| 339 | |
| 340 | return BinaryOperator::Create(I.getOpcode(), ShiftOp->getOperand(0), |
| 341 | ConstantInt::get(I.getType(), AmtSum)); |
| 342 | } |
| 343 | |
Sanjay Patel | 5424bd2 | 2017-01-17 16:59:09 +0000 | [diff] [blame] | 344 | // This is a constant shift of a constant shift. Be careful about hiding |
| 345 | // shl instructions behind bit masks. They are used to represent multiplies |
| 346 | // by a constant, and it is important that simple arithmetic expressions |
| 347 | // are still recognizable by scalar evolution. |
| 348 | // |
| 349 | // The transforms applied to shl are very similar to the transforms applied |
| 350 | // to mul by constant. We can be more aggressive about optimizing right |
| 351 | // shifts. |
| 352 | // |
| 353 | // Combinations of right and left shifts will still be optimized in |
| 354 | // DAGCombine where scalar evolution no longer applies. |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 355 | |
Sanjay Patel | b0d96d3 | 2017-01-26 20:52:27 +0000 | [diff] [blame] | 356 | Value *X = ShiftOp->getOperand(0); |
| 357 | unsigned ShiftAmt1 = ShAmt1->getLimitedValue(); |
| 358 | unsigned ShiftAmt2 = COp1->getLimitedValue(); |
| 359 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 360 | if (ShiftAmt1 == 0) |
| 361 | return nullptr; // Will be simplified in the future. |
| 362 | |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 363 | if (ShiftAmt1 == ShiftAmt2) |
| 364 | return nullptr; |
Sanjay Patel | b0d96d3 | 2017-01-26 20:52:27 +0000 | [diff] [blame] | 365 | |
Sanjay Patel | 478a83c | 2017-01-21 17:59:59 +0000 | [diff] [blame] | 366 | // FIXME: Everything under here should be extended to work with vector types. |
| 367 | |
| 368 | auto *ShiftAmt1C = dyn_cast<ConstantInt>(ShiftOp->getOperand(1)); |
| 369 | if (!ShiftAmt1C) |
| 370 | return nullptr; |
| 371 | |
Sanjay Patel | 5424bd2 | 2017-01-17 16:59:09 +0000 | [diff] [blame] | 372 | IntegerType *Ty = cast<IntegerType>(I.getType()); |
Sanjay Patel | 77732d5 | 2017-01-30 17:19:32 +0000 | [diff] [blame^] | 373 | if (ShiftAmt2 < ShiftAmt1) { |
Sanjay Patel | 5424bd2 | 2017-01-17 16:59:09 +0000 | [diff] [blame] | 374 | uint32_t ShiftDiff = ShiftAmt1 - ShiftAmt2; |
| 375 | |
| 376 | // (X >>?exact C1) << C2 --> X >>?exact (C1-C2) |
| 377 | // The inexact version is deferred to DAGCombine so we don't hide shl |
| 378 | // behind a bit mask. |
| 379 | if (I.getOpcode() == Instruction::Shl && |
| 380 | ShiftOp->getOpcode() != Instruction::Shl && ShiftOp->isExact()) { |
| 381 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 382 | BinaryOperator *NewShr = |
| 383 | BinaryOperator::Create(ShiftOp->getOpcode(), X, ShiftDiffCst); |
| 384 | NewShr->setIsExact(true); |
| 385 | return NewShr; |
| 386 | } |
| 387 | |
| 388 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
| 389 | if (I.getOpcode() == Instruction::LShr && |
| 390 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 391 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 392 | if (ShiftOp->hasNoUnsignedWrap()) { |
| 393 | // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2) |
| 394 | BinaryOperator *NewShl = |
| 395 | BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
| 396 | NewShl->setHasNoUnsignedWrap(true); |
| 397 | return NewShl; |
| 398 | } |
| 399 | Value *Shift = Builder->CreateShl(X, ShiftDiffCst); |
| 400 | |
| 401 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 402 | return BinaryOperator::CreateAnd(Shift, |
| 403 | ConstantInt::get(I.getContext(), Mask)); |
| 404 | } |
| 405 | |
| 406 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However, |
| 407 | // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits. |
| 408 | if (I.getOpcode() == Instruction::AShr && |
| 409 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 410 | if (ShiftOp->hasNoSignedWrap()) { |
| 411 | // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2) |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 412 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 413 | BinaryOperator *NewShl = |
| 414 | BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
Sanjay Patel | 5424bd2 | 2017-01-17 16:59:09 +0000 | [diff] [blame] | 415 | NewShl->setHasNoSignedWrap(true); |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 416 | return NewShl; |
| 417 | } |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | return nullptr; |
| 422 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 423 | |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 424 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1, |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 425 | BinaryOperator &I) { |
| 426 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 427 | |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 428 | const APInt *Op1C; |
| 429 | if (!match(Op1, m_APInt(Op1C))) |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 430 | return nullptr; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 432 | // See if we can propagate this shift into the input, this covers the trivial |
| 433 | // cast of lshr(shl(x,c1),c2) as well as other more complex cases. |
| 434 | if (I.getOpcode() != Instruction::AShr && |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 435 | canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) { |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 436 | DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression" |
| 437 | " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 438 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 439 | return replaceInstUsesWith( |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 440 | I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 441 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 442 | |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 443 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 444 | // purpose is to compute bits we don't care about. |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 445 | unsigned TypeBits = Op0->getType()->getScalarSizeInBits(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 446 | |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 447 | assert(!Op1C->uge(TypeBits) && |
Matt Arsenault | 6b4bed4 | 2014-04-23 16:48:40 +0000 | [diff] [blame] | 448 | "Shift over the type width should have been removed already"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 449 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 450 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 451 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 452 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 453 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 454 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 455 | ConstantExpr::getShl(BOOp, Op1)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 456 | |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 457 | if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I)) |
| 458 | return FoldedShift; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 459 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 460 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 461 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 462 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 463 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 464 | // place. Don't try to do this transformation in this case. Also, we |
| 465 | // require that the input operand is a shift-by-constant so that we have |
| 466 | // confidence that the shifts will get folded together. We could do this |
| 467 | // xform in more cases, but it is unlikely to be profitable. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 468 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 469 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 470 | // Okay, we'll do this xform. Make the shift of shift. |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 471 | Constant *ShAmt = |
| 472 | ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 473 | // (shift2 (shift1 & 0x00FF), c2) |
| 474 | Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName()); |
| 475 | |
| 476 | // For logical shifts, the truncation has the effect of making the high |
| 477 | // part of the register be zeros. Emulate this by inserting an AND to |
| 478 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 479 | // other xforms later if dead. |
| 480 | unsigned SrcSize = TrOp->getType()->getScalarSizeInBits(); |
| 481 | unsigned DstSize = TI->getType()->getScalarSizeInBits(); |
| 482 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 483 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 484 | // The mask we constructed says what the trunc would do if occurring |
| 485 | // between the shifts. We want to know the effect *after* the second |
| 486 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 487 | // mask as appropriate. |
| 488 | if (I.getOpcode() == Instruction::Shl) |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 489 | MaskV <<= Op1C->getZExtValue(); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 490 | else { |
| 491 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 492 | MaskV = MaskV.lshr(Op1C->getZExtValue()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // shift1 & 0x00FF |
| 496 | Value *And = Builder->CreateAnd(NSh, |
| 497 | ConstantInt::get(I.getContext(), MaskV), |
| 498 | TI->getName()); |
| 499 | |
| 500 | // Return the value truncated to the interesting size. |
| 501 | return new TruncInst(And, I.getType()); |
| 502 | } |
| 503 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 504 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 505 | if (Op0->hasOneUse()) { |
| 506 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 507 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 508 | Value *V1, *V2; |
| 509 | ConstantInt *CC; |
| 510 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 511 | default: break; |
| 512 | case Instruction::Add: |
| 513 | case Instruction::And: |
| 514 | case Instruction::Or: |
| 515 | case Instruction::Xor: { |
| 516 | // These operators commute. |
| 517 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
| 518 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 519 | match(Op0BO->getOperand(1), m_Shr(m_Value(V1), |
| 520 | m_Specific(Op1)))) { |
| 521 | Value *YS = // (Y << C) |
| 522 | Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName()); |
| 523 | // (X + (Y << C)) |
| 524 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1, |
| 525 | Op0BO->getOperand(1)->getName()); |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 526 | unsigned Op1Val = Op1C->getLimitedValue(TypeBits); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 527 | |
| 528 | APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val); |
| 529 | Constant *Mask = ConstantInt::get(I.getContext(), Bits); |
| 530 | if (VectorType *VT = dyn_cast<VectorType>(X->getType())) |
| 531 | Mask = ConstantVector::getSplat(VT->getNumElements(), Mask); |
| 532 | return BinaryOperator::CreateAnd(X, Mask); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 533 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 535 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 536 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 537 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 538 | match(Op0BOOp1, |
Jakub Staszak | 8432185 | 2012-12-09 16:06:44 +0000 | [diff] [blame] | 539 | m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))), |
| 540 | m_ConstantInt(CC)))) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 541 | Value *YS = // (Y << C) |
| 542 | Builder->CreateShl(Op0BO->getOperand(0), Op1, |
| 543 | Op0BO->getName()); |
| 544 | // X & (CC << C) |
| 545 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 546 | V1->getName()+".mask"); |
| 547 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 548 | } |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 549 | LLVM_FALLTHROUGH; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 550 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 552 | case Instruction::Sub: { |
| 553 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 554 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 555 | match(Op0BO->getOperand(0), m_Shr(m_Value(V1), |
| 556 | m_Specific(Op1)))) { |
| 557 | Value *YS = // (Y << C) |
| 558 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 559 | // (X + (Y << C)) |
| 560 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS, |
| 561 | Op0BO->getOperand(0)->getName()); |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 562 | unsigned Op1Val = Op1C->getLimitedValue(TypeBits); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 563 | |
| 564 | APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val); |
| 565 | Constant *Mask = ConstantInt::get(I.getContext(), Bits); |
| 566 | if (VectorType *VT = dyn_cast<VectorType>(X->getType())) |
| 567 | Mask = ConstantVector::getSplat(VT->getNumElements(), Mask); |
| 568 | return BinaryOperator::CreateAnd(X, Mask); |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 569 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 571 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
| 572 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 573 | match(Op0BO->getOperand(0), |
Jakub Staszak | 8432185 | 2012-12-09 16:06:44 +0000 | [diff] [blame] | 574 | m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))), |
| 575 | m_ConstantInt(CC))) && V2 == Op1) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 576 | Value *YS = // (Y << C) |
| 577 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 578 | // X & (CC << C) |
| 579 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 580 | V1->getName()+".mask"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 582 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
| 583 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 588 | |
| 589 | |
Sanjay Patel | fc3d8f0 | 2014-07-22 04:57:06 +0000 | [diff] [blame] | 590 | // If the operand is a bitwise operator with a constant RHS, and the |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 591 | // shift is the only use, we can pull it out of the shift. |
| 592 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 593 | bool isValid = true; // Valid only for And, Or, Xor |
| 594 | bool highBitSet = false; // Transform if high bit of constant set? |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 595 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 596 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 597 | default: isValid = false; break; // Do not perform transform! |
| 598 | case Instruction::Add: |
| 599 | isValid = isLeftShift; |
| 600 | break; |
| 601 | case Instruction::Or: |
| 602 | case Instruction::Xor: |
| 603 | highBitSet = false; |
| 604 | break; |
| 605 | case Instruction::And: |
| 606 | highBitSet = true; |
| 607 | break; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 608 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 609 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 610 | // If this is a signed shift right, and the high bit is modified |
| 611 | // by the logical operation, do not perform the transformation. |
| 612 | // The highBitSet boolean indicates the value of the high bit of |
| 613 | // the constant which would cause it to be modified for this |
| 614 | // operation. |
| 615 | // |
| 616 | if (isValid && I.getOpcode() == Instruction::AShr) |
| 617 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 618 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 619 | if (isValid) { |
| 620 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 621 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 622 | Value *NewShift = |
| 623 | Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1); |
| 624 | NewShift->takeName(Op0BO); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 625 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 626 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
| 627 | NewRHS); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 632 | |
Sanjay Patel | da5682a | 2017-01-16 21:24:41 +0000 | [diff] [blame] | 633 | if (Instruction *Folded = foldShiftByConstOfShiftByConst(I, Op1C, Builder)) |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 634 | return Folded; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 635 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 636 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 640 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 641 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 642 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 643 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 644 | if (Value *V = SimplifyShlInst(Op0, Op1, I.hasNoSignedWrap(), |
| 645 | I.hasNoUnsignedWrap(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 646 | return replaceInstUsesWith(I, V); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 648 | if (Instruction *V = commonShiftTransforms(I)) |
| 649 | return V; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 650 | |
Sanjay Patel | b22f6c5 | 2017-01-13 18:39:09 +0000 | [diff] [blame] | 651 | const APInt *ShAmtAPInt; |
| 652 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 653 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 654 | unsigned BitWidth = I.getType()->getScalarSizeInBits(); |
Sanjay Patel | 062adaa | 2017-01-29 17:11:18 +0000 | [diff] [blame] | 655 | Type *Ty = I.getType(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 656 | |
Sanjay Patel | acd24c7 | 2017-01-13 18:52:10 +0000 | [diff] [blame] | 657 | // shl (zext X), ShAmt --> zext (shl X, ShAmt) |
| 658 | // This is only valid if X would have zeros shifted out. |
| 659 | Value *X; |
| 660 | if (match(Op0, m_ZExt(m_Value(X)))) { |
| 661 | unsigned SrcWidth = X->getType()->getScalarSizeInBits(); |
| 662 | if (ShAmt < SrcWidth && |
| 663 | MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I)) |
Sanjay Patel | 062adaa | 2017-01-29 17:11:18 +0000 | [diff] [blame] | 664 | return new ZExtInst(Builder->CreateShl(X, ShAmt), Ty); |
David Majnemer | cb892e9 | 2017-01-04 02:21:34 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 667 | // (X >>u C) << C --> X & (-1 << C) |
| 668 | if (match(Op0, m_LShr(m_Value(X), m_Specific(Op1)))) { |
| 669 | APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt)); |
Sanjay Patel | 062adaa | 2017-01-29 17:11:18 +0000 | [diff] [blame] | 670 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask)); |
| 671 | } |
| 672 | |
| 673 | const APInt *ShrAmt; |
| 674 | if (match(Op0, m_CombineOr(m_Exact(m_LShr(m_Value(X), m_APInt(ShrAmt))), |
| 675 | m_Exact(m_AShr(m_Value(X), m_APInt(ShrAmt))))) && |
| 676 | ShrAmt->ult(*ShAmtAPInt)) { |
| 677 | // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1) |
| 678 | // The inexact version is deferred to DAGCombine, so we don't hide shl |
| 679 | // behind a bit mask. |
| 680 | Constant *ShiftDiffCst = ConstantInt::get(Ty, *ShAmtAPInt - *ShrAmt); |
| 681 | auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
| 682 | NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 683 | NewShl->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 684 | return NewShl; |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 687 | // If the shifted-out value is known-zero, then this is a NUW shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 688 | if (!I.hasNoUnsignedWrap() && |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 689 | MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) { |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 690 | I.setHasNoUnsignedWrap(); |
| 691 | return &I; |
| 692 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 693 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 694 | // If the shifted-out value is all signbits, then this is a NSW shift. |
| 695 | if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 696 | I.setHasNoSignedWrap(); |
| 697 | return &I; |
| 698 | } |
| 699 | } |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 700 | |
Benjamin Kramer | f0e3f04 | 2011-04-29 08:41:23 +0000 | [diff] [blame] | 701 | // (C1 << A) << C2 -> (C1 << C2) << A |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 702 | Constant *C1, *C2; |
| 703 | Value *A; |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 704 | if (match(Op0, m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) && |
| 705 | match(Op1, m_Constant(C2))) |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 706 | return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A); |
| 707 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 708 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 712 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 713 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 714 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 715 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 716 | if (Value *V = SimplifyLShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 717 | return replaceInstUsesWith(I, V); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 719 | if (Instruction *R = commonShiftTransforms(I)) |
| 720 | return R; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 721 | |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 722 | Type *Ty = I.getType(); |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 723 | const APInt *ShAmtAPInt; |
| 724 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 725 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 726 | unsigned BitWidth = Ty->getScalarSizeInBits(); |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 727 | auto *II = dyn_cast<IntrinsicInst>(Op0); |
| 728 | if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt && |
| 729 | (II->getIntrinsicID() == Intrinsic::ctlz || |
| 730 | II->getIntrinsicID() == Intrinsic::cttz || |
| 731 | II->getIntrinsicID() == Intrinsic::ctpop)) { |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 732 | // ctlz.i32(x)>>5 --> zext(x == 0) |
| 733 | // cttz.i32(x)>>5 --> zext(x == 0) |
| 734 | // ctpop.i32(x)>>5 --> zext(x == -1) |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 735 | bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop; |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 736 | Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0); |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 737 | Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS); |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 738 | return new ZExtInst(Cmp, Ty); |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 739 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 740 | |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 741 | Value *X; |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 742 | const APInt *ShlAmtAPInt; |
| 743 | if (match(Op0, m_Shl(m_Value(X), m_APInt(ShlAmtAPInt)))) { |
| 744 | unsigned ShlAmt = ShlAmtAPInt->getZExtValue(); |
| 745 | if (ShlAmt == ShAmt) { |
| 746 | // (X << C) >>u C --> X & (-1 >>u C) |
| 747 | APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt)); |
| 748 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask)); |
| 749 | } |
| 750 | if (ShlAmt < ShAmt) { |
| 751 | Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt); |
| 752 | if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) { |
| 753 | // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1) |
| 754 | BinaryOperator *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff); |
| 755 | NewLShr->setIsExact(I.isExact()); |
| 756 | return NewLShr; |
| 757 | } |
| 758 | // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2) |
Sanjay Patel | 8e644c0 | 2017-01-30 16:53:03 +0000 | [diff] [blame] | 759 | Value *NewLShr = Builder->CreateLShr(X, ShiftDiff, "", I.isExact()); |
Sanjay Patel | 1196d7c | 2017-01-30 16:11:40 +0000 | [diff] [blame] | 760 | APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt)); |
| 761 | return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask)); |
| 762 | } |
Sanjay Patel | 50753f0 | 2017-01-26 22:08:10 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 765 | // If the shifted-out value is known-zero, then this is an exact shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 766 | if (!I.isExact() && |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 767 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 768 | I.setIsExact(); |
| 769 | return &I; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 770 | } |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 771 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 772 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 776 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 777 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 778 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 779 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 780 | if (Value *V = SimplifyAShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 781 | return replaceInstUsesWith(I, V); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 782 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 783 | if (Instruction *R = commonShiftTransforms(I)) |
| 784 | return R; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 785 | |
Sanjay Patel | 77732d5 | 2017-01-30 17:19:32 +0000 | [diff] [blame^] | 786 | Type *Ty = I.getType(); |
| 787 | unsigned BitWidth = Ty->getScalarSizeInBits(); |
Sanjay Patel | 5f8451a | 2017-01-15 16:38:19 +0000 | [diff] [blame] | 788 | const APInt *ShAmtAPInt; |
| 789 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 790 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 791 | |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 792 | // If the shift amount equals the difference in width of the destination |
Sanjay Patel | 5f8451a | 2017-01-15 16:38:19 +0000 | [diff] [blame] | 793 | // and source scalar types: |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 794 | // ashr (shl (zext X), C), C --> sext X |
Chris Lattner | a1e223e | 2010-01-08 19:04:21 +0000 | [diff] [blame] | 795 | Value *X; |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 796 | if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) && |
| 797 | ShAmt == BitWidth - X->getType()->getScalarSizeInBits()) |
Sanjay Patel | 77732d5 | 2017-01-30 17:19:32 +0000 | [diff] [blame^] | 798 | return new SExtInst(X, Ty); |
| 799 | |
| 800 | // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However, |
| 801 | // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits. |
| 802 | const APInt *ShlAmtAPInt; |
| 803 | if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShlAmtAPInt))) && |
| 804 | ShlAmtAPInt->ult(*ShAmtAPInt)) { |
| 805 | // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1) |
| 806 | Constant *ShiftDiff = ConstantInt::get(Ty, *ShAmtAPInt - *ShlAmtAPInt); |
| 807 | BinaryOperator *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff); |
| 808 | NewAShr->setIsExact(I.isExact()); |
| 809 | return NewAShr; |
| 810 | } |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 811 | |
| 812 | // If the shifted-out value is known-zero, then this is an exact shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 813 | if (!I.isExact() && |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 814 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 815 | I.setIsExact(); |
| 816 | return &I; |
| 817 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 820 | // See if we can turn a signed shr into an unsigned shr. |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 821 | if (MaskedValueIsZero(Op0, APInt::getSignBit(BitWidth), 0, &I)) |
Chris Lattner | a1e223e | 2010-01-08 19:04:21 +0000 | [diff] [blame] | 822 | return BinaryOperator::CreateLShr(Op0, Op1); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 823 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 824 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 825 | } |