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