blob: 8a28d8eaa2381f39fc4bf18dcb6257bbb76ea661 [file] [log] [blame]
Chris Lattnerdc67e132010-01-05 07:44:46 +00001//===- 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 Friedman911e12f2011-07-20 21:57:23 +000015#include "llvm/Analysis/ConstantFolding.h"
Duncan Sands7f60dc12011-01-14 00:37:45 +000016#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/IntrinsicInst.h"
Chris Lattnerdc67e132010-01-05 07:44:46 +000018#include "llvm/Support/PatternMatch.h"
19using namespace llvm;
20using namespace PatternMatch;
21
22Instruction *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 Lattnerdc67e132010-01-05 07:44:46 +000026 // 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 Kramerb5afa652010-11-23 18:52:42 +000039
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000040 // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
Chris Lattner6b657ae2011-02-10 05:36:31 +000041 // 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 Staszak538e3862012-12-09 15:37:46 +000052
Chris Lattnerdc67e132010-01-05 07:44:46 +000053 return 0;
54}
55
Chris Lattner18d7fc82010-08-27 22:24:38 +000056/// 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.
68static 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 Staszak538e3862012-12-09 15:37:46 +000073
Chris Lattner18d7fc82010-08-27 22:24:38 +000074 Instruction *I = dyn_cast<Instruction>(V);
75 if (!I) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +000076
Chris Lattner18d7fc82010-08-27 22:24:38 +000077 // 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 Kremenek3c4408c2011-01-23 17:05:06 +000081 ConstantInt *CI = 0;
Chris Lattner18d7fc82010-08-27 22:24:38 +000082 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 Ledru91ce36c2012-09-27 10:14:43 +000088 // lshr iff we know that the bits we would otherwise be shifting in are
Chris Lattner18d7fc82010-08-27 22:24:38 +000089 // 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 Staszak538e3862012-12-09 15:37:46 +000098
Chris Lattner18d7fc82010-08-27 22:24:38 +000099 }
100 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000101
Chris Lattner18d7fc82010-08-27 22:24:38 +0000102 // 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 Staszak538e3862012-12-09 15:37:46 +0000105
Chris Lattner18d7fc82010-08-27 22:24:38 +0000106 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 Staszak538e3862012-12-09 15:37:46 +0000114
Chris Lattner6c1395f2010-08-27 22:53:44 +0000115 case Instruction::Shl: {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000116 // 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 Staszak538e3862012-12-09 15:37:46 +0000122
Chris Lattner18d7fc82010-08-27 22:24:38 +0000123 // We can always turn shl(c)+shr(c) -> and(c2).
124 if (CI->getValue() == NumBits) return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000125
Chris Lattner6c1395f2010-08-27 22:53:44 +0000126 unsigned TypeWidth = I->getType()->getScalarSizeInBits();
127
128 // We can turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but it isn't
Chris Lattner18d7fc82010-08-27 22:24:38 +0000129 // profitable unless we know the and'd out bits are already zero.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000130 if (CI->getZExtValue() > NumBits) {
Dale Johannesen0171dc32010-11-10 01:30:56 +0000131 unsigned LowBits = TypeWidth - CI->getZExtValue();
Chris Lattner6c1395f2010-08-27 22:53:44 +0000132 if (MaskedValueIsZero(I->getOperand(0),
Dale Johannesen0171dc32010-11-10 01:30:56 +0000133 APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits))
Chris Lattner6c1395f2010-08-27 22:53:44 +0000134 return true;
135 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000136
Chris Lattner18d7fc82010-08-27 22:24:38 +0000137 return false;
Chris Lattner6c1395f2010-08-27 22:53:44 +0000138 }
139 case Instruction::LShr: {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000140 // 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 Staszak538e3862012-12-09 15:37:46 +0000143
Chris Lattner18d7fc82010-08-27 22:24:38 +0000144 // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
145 if (!isLeftShift) return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000146
Chris Lattner18d7fc82010-08-27 22:24:38 +0000147 // We can always turn lshr(c)+shl(c) -> and(c2).
148 if (CI->getValue() == NumBits) return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000149
Chris Lattner6c1395f2010-08-27 22:53:44 +0000150 unsigned TypeWidth = I->getType()->getScalarSizeInBits();
151
Chris Lattner18d7fc82010-08-27 22:24:38 +0000152 // 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 Kramer152f1062012-05-27 22:03:32 +0000154 if (CI->getValue().ult(TypeWidth) && CI->getZExtValue() > NumBits) {
Owen Anderson226ac142010-12-23 23:56:24 +0000155 unsigned LowBits = CI->getZExtValue() - NumBits;
Chris Lattner6c1395f2010-08-27 22:53:44 +0000156 if (MaskedValueIsZero(I->getOperand(0),
Owen Anderson226ac142010-12-23 23:56:24 +0000157 APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits))
Chris Lattner6c1395f2010-08-27 22:53:44 +0000158 return true;
159 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000160
Chris Lattner6c1395f2010-08-27 22:53:44 +0000161 return false;
162 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000163 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 Staszak538e3862012-12-09 15:37:46 +0000178 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000179}
180
181/// GetShiftedValue - When CanEvaluateShifted returned true for an expression,
182/// this value inserts the new computation that produces the shifted value.
183static 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 Villmowcdfe20b2012-10-08 16:38:25 +0000193 V = ConstantFoldConstantExpression(CE, IC.getDataLayout(),
Chad Rosier43a33062011-12-02 01:26:24 +0000194 IC.getTargetLibraryInfo());
Chris Lattner18d7fc82010-08-27 22:24:38 +0000195 return V;
196 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000197
Chris Lattner18d7fc82010-08-27 22:24:38 +0000198 Instruction *I = cast<Instruction>(V);
199 IC.Worklist.Add(I);
200
201 switch (I->getOpcode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000202 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
Chris Lattner18d7fc82010-08-27 22:24:38 +0000203 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 Staszak538e3862012-12-09 15:37:46 +0000210
Chris Lattner18d7fc82010-08-27 22:24:38 +0000211 case Instruction::Shl: {
Eli Friedman530341d2011-07-29 00:18:19 +0000212 BinaryOperator *BO = cast<BinaryOperator>(I);
213 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000214
215 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000216 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
217
Chris Lattner18d7fc82010-08-27 22:24:38 +0000218 // 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 Friedman530341d2011-07-29 00:18:19 +0000225 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
226 BO->setHasNoUnsignedWrap(false);
227 BO->setHasNoSignedWrap(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000228 return I;
229 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000230
Chris Lattner18d7fc82010-08-27 22:24:38 +0000231 // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
232 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000233 if (CI->getValue() == NumBits) {
234 APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
Eli Friedman530341d2011-07-29 00:18:19 +0000235 V = IC.Builder->CreateAnd(BO->getOperand(0),
236 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000237 if (Instruction *VI = dyn_cast<Instruction>(V)) {
Eli Friedman530341d2011-07-29 00:18:19 +0000238 VI->moveBefore(BO);
239 VI->takeName(BO);
Chris Lattner6c1395f2010-08-27 22:53:44 +0000240 }
241 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000242 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000243
Chris Lattner6c1395f2010-08-27 22:53:44 +0000244 // 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 Friedman530341d2011-07-29 00:18:19 +0000247 BO->setOperand(1, ConstantInt::get(BO->getType(),
248 CI->getZExtValue() - NumBits));
249 BO->setHasNoUnsignedWrap(false);
250 BO->setHasNoSignedWrap(false);
251 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000252 }
253 case Instruction::LShr: {
Eli Friedman530341d2011-07-29 00:18:19 +0000254 BinaryOperator *BO = cast<BinaryOperator>(I);
255 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000256 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000257 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
Jakub Staszak538e3862012-12-09 15:37:46 +0000258
Chris Lattner18d7fc82010-08-27 22:24:38 +0000259 // 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 Friedman530341d2011-07-29 00:18:19 +0000264 return Constant::getNullValue(BO->getType());
Jakub Staszak538e3862012-12-09 15:37:46 +0000265
Eli Friedman530341d2011-07-29 00:18:19 +0000266 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
267 BO->setIsExact(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000268 return I;
269 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000270
Chris Lattner18d7fc82010-08-27 22:24:38 +0000271 // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
272 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000273 if (CI->getValue() == NumBits) {
274 APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
275 V = IC.Builder->CreateAnd(I->getOperand(0),
Eli Friedman530341d2011-07-29 00:18:19 +0000276 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000277 if (Instruction *VI = dyn_cast<Instruction>(V)) {
278 VI->moveBefore(I);
279 VI->takeName(I);
280 }
281 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000282 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000283
Chris Lattner6c1395f2010-08-27 22:53:44 +0000284 // 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 Friedman530341d2011-07-29 00:18:19 +0000287 BO->setOperand(1, ConstantInt::get(BO->getType(),
288 CI->getZExtValue() - NumBits));
289 BO->setIsExact(false);
290 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000291 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000292
Chris Lattner18d7fc82010-08-27 22:24:38 +0000293 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 Staszak538e3862012-12-09 15:37:46 +0000307 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000308}
309
310
311
Chris Lattnerdc67e132010-01-05 07:44:46 +0000312Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
313 BinaryOperator &I) {
314 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Jakub Staszak538e3862012-12-09 15:37:46 +0000315
316
Chris Lattner18d7fc82010-08-27 22:24:38 +0000317 // 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 Lattnerdd660102010-08-28 01:20:38 +0000321 DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
322 " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n");
Jakub Staszak538e3862012-12-09 15:37:46 +0000323
324 return ReplaceInstUsesWith(I,
Chris Lattner18d7fc82010-08-27 22:24:38 +0000325 GetShiftedValue(Op0, Op1->getZExtValue(), isLeftShift, *this));
326 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000327
328
329 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattnerdc67e132010-01-05 07:44:46 +0000330 // purpose is to compute bits we don't care about.
331 uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
Jakub Staszak538e3862012-12-09 15:37:46 +0000332
Chris Lattnerdc67e132010-01-05 07:44:46 +0000333 // 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 Lattner249da5c2010-01-23 18:49:30 +0000339 // ashr i32 X, 32 --> ashr i32 X, 31
340 I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
341 return &I;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000342 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000343
Chris Lattnerdc67e132010-01-05 07:44:46 +0000344 // ((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 Staszak538e3862012-12-09 15:37:46 +0000350
Chris Lattnerdc67e132010-01-05 07:44:46 +0000351 // 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 Staszak538e3862012-12-09 15:37:46 +0000358
Chris Lattnerdc67e132010-01-05 07:44:46 +0000359 // 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 Staszak538e3862012-12-09 15:37:46 +0000367 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
Chris Lattnerdc67e132010-01-05 07:44:46 +0000368 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 Staszak538e3862012-12-09 15:37:46 +0000381
Chris Lattnerdc67e132010-01-05 07:44:46 +0000382 // 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 Staszak538e3862012-12-09 15:37:46 +0000402
Chris Lattnerdc67e132010-01-05 07:44:46 +0000403 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 Lattner2b459fe2010-01-10 06:59:55 +0000409 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 Lattnerdc67e132010-01-05 07:44:46 +0000427 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000428
Chris Lattner2b459fe2010-01-10 06:59:55 +0000429 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
430 Value *Op0BOOp1 = Op0BO->getOperand(1);
431 if (isLeftShift && Op0BOOp1->hasOneUse() &&
Jakub Staszak538e3862012-12-09 15:37:46 +0000432 match(Op0BOOp1,
Jakub Staszak84321852012-12-09 16:06:44 +0000433 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
434 m_ConstantInt(CC)))) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000435 Value *YS = // (Y << C)
436 Builder->CreateShl(Op0BO->getOperand(0), Op1,
437 Op0BO->getName());
438 // X & (CC << C)
439 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
440 V1->getName()+".mask");
441 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000442 }
443 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000444
Chris Lattner2b459fe2010-01-10 06:59:55 +0000445 // FALL THROUGH.
446 case Instruction::Sub: {
447 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
448 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
449 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
450 m_Specific(Op1)))) {
451 Value *YS = // (Y << C)
452 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
453 // (X + (Y << C))
454 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
455 Op0BO->getOperand(0)->getName());
456 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
457 return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(),
458 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
459 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000460
Chris Lattner2b459fe2010-01-10 06:59:55 +0000461 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
462 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
463 match(Op0BO->getOperand(0),
Jakub Staszak84321852012-12-09 16:06:44 +0000464 m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
465 m_ConstantInt(CC))) && V2 == Op1) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000466 Value *YS = // (Y << C)
467 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
468 // X & (CC << C)
469 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
470 V1->getName()+".mask");
Jakub Staszak538e3862012-12-09 15:37:46 +0000471
Chris Lattner2b459fe2010-01-10 06:59:55 +0000472 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
473 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000474
Chris Lattner2b459fe2010-01-10 06:59:55 +0000475 break;
476 }
477 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000478
479
Chris Lattnerdc67e132010-01-05 07:44:46 +0000480 // If the operand is an bitwise operator with a constant RHS, and the
481 // shift is the only use, we can pull it out of the shift.
482 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
483 bool isValid = true; // Valid only for And, Or, Xor
484 bool highBitSet = false; // Transform if high bit of constant set?
Jakub Staszak538e3862012-12-09 15:37:46 +0000485
Chris Lattnerdc67e132010-01-05 07:44:46 +0000486 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000487 default: isValid = false; break; // Do not perform transform!
488 case Instruction::Add:
489 isValid = isLeftShift;
490 break;
491 case Instruction::Or:
492 case Instruction::Xor:
493 highBitSet = false;
494 break;
495 case Instruction::And:
496 highBitSet = true;
497 break;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000498 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000499
Chris Lattnerdc67e132010-01-05 07:44:46 +0000500 // If this is a signed shift right, and the high bit is modified
501 // by the logical operation, do not perform the transformation.
502 // The highBitSet boolean indicates the value of the high bit of
503 // the constant which would cause it to be modified for this
504 // operation.
505 //
506 if (isValid && I.getOpcode() == Instruction::AShr)
507 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
Jakub Staszak538e3862012-12-09 15:37:46 +0000508
Chris Lattnerdc67e132010-01-05 07:44:46 +0000509 if (isValid) {
510 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000511
Chris Lattnerdc67e132010-01-05 07:44:46 +0000512 Value *NewShift =
513 Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
514 NewShift->takeName(Op0BO);
Jakub Staszak538e3862012-12-09 15:37:46 +0000515
Chris Lattnerdc67e132010-01-05 07:44:46 +0000516 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
517 NewRHS);
518 }
519 }
520 }
521 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000522
Chris Lattnerdc67e132010-01-05 07:44:46 +0000523 // Find out if this is a shift of a shift by a constant.
524 BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
525 if (ShiftOp && !ShiftOp->isShift())
526 ShiftOp = 0;
Jakub Staszak538e3862012-12-09 15:37:46 +0000527
Chris Lattnerdc67e132010-01-05 07:44:46 +0000528 if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000529
530 // This is a constant shift of a constant shift. Be careful about hiding
531 // shl instructions behind bit masks. They are used to represent multiplies
532 // by a constant, and it is important that simple arithmetic expressions
533 // are still recognizable by scalar evolution.
534 //
535 // The transforms applied to shl are very similar to the transforms applied
536 // to mul by constant. We can be more aggressive about optimizing right
537 // shifts.
538 //
539 // Combinations of right and left shifts will still be optimized in
540 // DAGCombine where scalar evolution no longer applies.
541
Chris Lattnerdc67e132010-01-05 07:44:46 +0000542 ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
543 uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
544 uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
545 assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
546 if (ShiftAmt1 == 0) return 0; // Will be simplified in the future.
547 Value *X = ShiftOp->getOperand(0);
Jakub Staszak538e3862012-12-09 15:37:46 +0000548
Chris Lattner229907c2011-07-18 04:54:35 +0000549 IntegerType *Ty = cast<IntegerType>(I.getType());
Jakub Staszak538e3862012-12-09 15:37:46 +0000550
Chris Lattnerdc67e132010-01-05 07:44:46 +0000551 // Check for (X << c1) << c2 and (X >> c1) >> c2
552 if (I.getOpcode() == ShiftOp->getOpcode()) {
Nick Lewyckyb59008c2011-12-31 21:30:22 +0000553 uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
Chris Lattnerdc67e132010-01-05 07:44:46 +0000554 // If this is oversized composite shift, then unsigned shifts get 0, ashr
555 // saturates.
556 if (AmtSum >= TypeBits) {
557 if (I.getOpcode() != Instruction::AShr)
558 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
559 AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
560 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000561
Chris Lattnerdc67e132010-01-05 07:44:46 +0000562 return BinaryOperator::Create(I.getOpcode(), X,
563 ConstantInt::get(Ty, AmtSum));
564 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000565
Chris Lattnerdc67e132010-01-05 07:44:46 +0000566 if (ShiftAmt1 == ShiftAmt2) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000567 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
Chris Lattner25a198e2010-08-27 21:04:34 +0000568 if (I.getOpcode() == Instruction::LShr &&
569 ShiftOp->getOpcode() == Instruction::Shl) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000570 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
571 return BinaryOperator::CreateAnd(X,
572 ConstantInt::get(I.getContext(), Mask));
573 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000574 } else if (ShiftAmt1 < ShiftAmt2) {
575 uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000576
577 // (X >>?,exact C1) << C2 --> X << (C2-C1)
578 // The inexact version is deferred to DAGCombine so we don't hide shl
579 // behind a bit mask.
Chris Lattner25a198e2010-08-27 21:04:34 +0000580 if (I.getOpcode() == Instruction::Shl &&
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000581 ShiftOp->getOpcode() != Instruction::Shl &&
582 ShiftOp->isExact()) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000583 assert(ShiftOp->getOpcode() == Instruction::LShr ||
584 ShiftOp->getOpcode() == Instruction::AShr);
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000585 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000586 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
587 X, ShiftDiffCst);
588 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
589 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
590 return NewShl;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000591 }
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000592
Chris Lattnerdc67e132010-01-05 07:44:46 +0000593 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
Chris Lattner25a198e2010-08-27 21:04:34 +0000594 if (I.getOpcode() == Instruction::LShr &&
595 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000596 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
597 // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
598 if (ShiftOp->hasNoUnsignedWrap()) {
599 BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
600 X, ShiftDiffCst);
601 NewLShr->setIsExact(I.isExact());
602 return NewLShr;
603 }
604 Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
Jakub Staszak538e3862012-12-09 15:37:46 +0000605
Chris Lattnerdc67e132010-01-05 07:44:46 +0000606 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
607 return BinaryOperator::CreateAnd(Shift,
608 ConstantInt::get(I.getContext(),Mask));
609 }
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000610
611 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
612 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
613 if (I.getOpcode() == Instruction::AShr &&
614 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000615 if (ShiftOp->hasNoSignedWrap()) {
616 // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
617 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
618 BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
619 X, ShiftDiffCst);
620 NewAShr->setIsExact(I.isExact());
621 return NewAShr;
622 }
623 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000624 } else {
625 assert(ShiftAmt2 < ShiftAmt1);
626 uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
627
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000628 // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
629 // The inexact version is deferred to DAGCombine so we don't hide shl
630 // behind a bit mask.
Chris Lattner25a198e2010-08-27 21:04:34 +0000631 if (I.getOpcode() == Instruction::Shl &&
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000632 ShiftOp->getOpcode() != Instruction::Shl &&
633 ShiftOp->isExact()) {
Nick Lewyckyb59008c2011-12-31 21:30:22 +0000634 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000635 BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
636 X, ShiftDiffCst);
637 NewShr->setIsExact(true);
638 return NewShr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000639 }
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000640
Chris Lattnerdc67e132010-01-05 07:44:46 +0000641 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
Chris Lattner25a198e2010-08-27 21:04:34 +0000642 if (I.getOpcode() == Instruction::LShr &&
643 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000644 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
645 if (ShiftOp->hasNoUnsignedWrap()) {
646 // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
647 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
648 X, ShiftDiffCst);
649 NewShl->setHasNoUnsignedWrap(true);
650 return NewShl;
651 }
652 Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
Jakub Staszak538e3862012-12-09 15:37:46 +0000653
Chris Lattnerdc67e132010-01-05 07:44:46 +0000654 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
655 return BinaryOperator::CreateAnd(Shift,
656 ConstantInt::get(I.getContext(),Mask));
657 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000658
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000659 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
660 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
661 if (I.getOpcode() == Instruction::AShr &&
662 ShiftOp->getOpcode() == Instruction::Shl) {
663 if (ShiftOp->hasNoSignedWrap()) {
664 // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
665 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
666 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
667 X, ShiftDiffCst);
668 NewShl->setHasNoSignedWrap(true);
669 return NewShl;
670 }
671 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000672 }
673 }
674 return 0;
675}
676
677Instruction *InstCombiner::visitShl(BinaryOperator &I) {
Chris Lattner9e4aa022011-02-09 17:15:04 +0000678 if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
679 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
680 TD))
Duncan Sands7f60dc12011-01-14 00:37:45 +0000681 return ReplaceInstUsesWith(I, V);
Jakub Staszak538e3862012-12-09 15:37:46 +0000682
Chris Lattner6b657ae2011-02-10 05:36:31 +0000683 if (Instruction *V = commonShiftTransforms(I))
684 return V;
Jakub Staszak538e3862012-12-09 15:37:46 +0000685
Chris Lattner6b657ae2011-02-10 05:36:31 +0000686 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
687 unsigned ShAmt = Op1C->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000688
Chris Lattner6b657ae2011-02-10 05:36:31 +0000689 // If the shifted-out value is known-zero, then this is a NUW shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000690 if (!I.hasNoUnsignedWrap() &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000691 MaskedValueIsZero(I.getOperand(0),
692 APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt))) {
693 I.setHasNoUnsignedWrap();
694 return &I;
695 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000696
Chris Lattner6b657ae2011-02-10 05:36:31 +0000697 // If the shifted out value is all signbits, this is a NSW shift.
698 if (!I.hasNoSignedWrap() &&
699 ComputeNumSignBits(I.getOperand(0)) > ShAmt) {
700 I.setHasNoSignedWrap();
701 return &I;
702 }
703 }
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000704
Benjamin Kramerf0e3f042011-04-29 08:41:23 +0000705 // (C1 << A) << C2 -> (C1 << C2) << A
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000706 Constant *C1, *C2;
707 Value *A;
708 if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
709 match(I.getOperand(1), m_Constant(C2)))
710 return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
711
Jakub Staszak538e3862012-12-09 15:37:46 +0000712 return 0;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000713}
714
715Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
Chris Lattner9e4aa022011-02-09 17:15:04 +0000716 if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1),
717 I.isExact(), TD))
Duncan Sands7f60dc12011-01-14 00:37:45 +0000718 return ReplaceInstUsesWith(I, V);
719
Chris Lattner249da5c2010-01-23 18:49:30 +0000720 if (Instruction *R = commonShiftTransforms(I))
721 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000722
Chris Lattner249da5c2010-01-23 18:49:30 +0000723 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000724
Chris Lattner6b657ae2011-02-10 05:36:31 +0000725 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
726 unsigned ShAmt = Op1C->getZExtValue();
727
Chris Lattner249da5c2010-01-23 18:49:30 +0000728 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
Chris Lattnere112ff62010-01-23 23:31:46 +0000729 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
Chris Lattner249da5c2010-01-23 18:49:30 +0000730 // ctlz.i32(x)>>5 --> zext(x == 0)
731 // cttz.i32(x)>>5 --> zext(x == 0)
732 // ctpop.i32(x)>>5 --> zext(x == -1)
733 if ((II->getIntrinsicID() == Intrinsic::ctlz ||
734 II->getIntrinsicID() == Intrinsic::cttz ||
735 II->getIntrinsicID() == Intrinsic::ctpop) &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000736 isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
Chris Lattner249da5c2010-01-23 18:49:30 +0000737 bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
Chris Lattnere112ff62010-01-23 23:31:46 +0000738 Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
Gabor Greif4a39b842010-06-24 00:44:01 +0000739 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
Chris Lattner249da5c2010-01-23 18:49:30 +0000740 return new ZExtInst(Cmp, II->getType());
741 }
742 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000743
Chris Lattner6b657ae2011-02-10 05:36:31 +0000744 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000745 if (!I.isExact() &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000746 MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt))){
747 I.setIsExact();
748 return &I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000749 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000750 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000751
Chris Lattner249da5c2010-01-23 18:49:30 +0000752 return 0;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000753}
754
755Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
Chris Lattner9e4aa022011-02-09 17:15:04 +0000756 if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1),
757 I.isExact(), TD))
Duncan Sands7f60dc12011-01-14 00:37:45 +0000758 return ReplaceInstUsesWith(I, V);
759
Chris Lattnerdc67e132010-01-05 07:44:46 +0000760 if (Instruction *R = commonShiftTransforms(I))
761 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000762
Chris Lattnera1e223e2010-01-08 19:04:21 +0000763 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000764
Chris Lattnera1e223e2010-01-08 19:04:21 +0000765 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000766 unsigned ShAmt = Op1C->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000767
Chris Lattnera1e223e2010-01-08 19:04:21 +0000768 // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
Chris Lattner43f2fa62010-01-18 22:19:16 +0000769 // have a sign-extend idiom.
Chris Lattnera1e223e2010-01-08 19:04:21 +0000770 Value *X;
Chris Lattner43f2fa62010-01-18 22:19:16 +0000771 if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000772 // If the left shift is just shifting out partial signbits, delete the
773 // extension.
774 if (cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
Chris Lattner43f2fa62010-01-18 22:19:16 +0000775 return ReplaceInstUsesWith(I, X);
776
777 // If the input is an extension from the shifted amount value, e.g.
778 // %x = zext i8 %A to i32
779 // %y = shl i32 %x, 24
780 // %z = ashr %y, 24
781 // then turn this into "z = sext i8 A to i32".
782 if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
783 uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
784 uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
785 if (Op1C->getZExtValue() == DestBits-SrcBits)
786 return new SExtInst(ZI->getOperand(0), ZI->getType());
787 }
788 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000789
790 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000791 if (!I.isExact() &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000792 MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt))){
793 I.setIsExact();
794 return &I;
795 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000796 }
797
Chris Lattnerdc67e132010-01-05 07:44:46 +0000798 // See if we can turn a signed shr into an unsigned shr.
799 if (MaskedValueIsZero(Op0,
800 APInt::getSignBit(I.getType()->getScalarSizeInBits())))
Chris Lattnera1e223e2010-01-08 19:04:21 +0000801 return BinaryOperator::CreateLShr(Op0, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000802
Chris Lattnerdc67e132010-01-05 07:44:46 +0000803 // Arithmetic shifting an all-sign-bit value is a no-op.
804 unsigned NumSignBits = ComputeNumSignBits(Op0);
805 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
806 return ReplaceInstUsesWith(I, Op0);
Jakub Staszak538e3862012-12-09 15:37:46 +0000807
Chris Lattnerdc67e132010-01-05 07:44:46 +0000808 return 0;
809}
810