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