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