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