blob: 9cd408428ad1c54d60d327131d7b4ed6ef404e02 [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
Chandler Carrutha9174582015-01-22 05:25:13 +000014#include "InstCombineInternal.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 Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth820a9082014-03-04 11:08:18 +000018#include "llvm/IR/PatternMatch.h"
Chris Lattnerdc67e132010-01-05 07:44:46 +000019using namespace llvm;
20using namespace PatternMatch;
21
Chandler Carruth964daaa2014-04-22 02:55:47 +000022#define DEBUG_TYPE "instcombine"
23
Chris Lattnerdc67e132010-01-05 07:44:46 +000024Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
25 assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
26 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
27
Chris Lattnerdc67e132010-01-05 07:44:46 +000028 // See if we can fold away this shift.
29 if (SimplifyDemandedInstructionBits(I))
30 return &I;
31
32 // Try to fold constant and into select arguments.
33 if (isa<Constant>(Op0))
34 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
35 if (Instruction *R = FoldOpIntoSelect(I, SI))
36 return R;
37
Matt Arsenaultfed3dc82014-04-14 21:50:37 +000038 if (Constant *CUI = dyn_cast<Constant>(Op1))
Chris Lattnerdc67e132010-01-05 07:44:46 +000039 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
40 return Res;
Benjamin Kramerb5afa652010-11-23 18:52:42 +000041
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000042 // 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 +000043 // Because shifts by negative values (which could occur if A were negative)
44 // are undefined.
45 Value *A; const APInt *B;
46 if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
47 // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
48 // demand the sign bit (and many others) here??
49 Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
50 Op1->getName());
51 I.setOperand(1, Rem);
52 return &I;
53 }
Jakub Staszak538e3862012-12-09 15:37:46 +000054
Craig Topperf40110f2014-04-25 05:29:35 +000055 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +000056}
57
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000058/// Return true if we can simplify two logical (either left or right) shifts
59/// that have constant shift amounts.
Sanjay Patelbd8b7792016-04-11 16:11:07 +000060static bool canEvaluateShiftedShift(unsigned FirstShiftAmt,
61 bool IsFirstShiftLeft,
62 Instruction *SecondShift, InstCombiner &IC,
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000063 Instruction *CxtI) {
Sanjay Patelbd8b7792016-04-11 16:11:07 +000064 // We need constant shifts.
65 auto *SecondShiftConst = dyn_cast<ConstantInt>(SecondShift->getOperand(1));
66 if (!SecondShiftConst)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000067 return false;
68
Sanjay Patelbd8b7792016-04-11 16:11:07 +000069 unsigned SecondShiftAmt = SecondShiftConst->getZExtValue();
Sanjay Patel37129072016-04-11 17:11:55 +000070 bool IsSecondShiftLeft = SecondShift->getOpcode() == Instruction::Shl;
Sanjay Patelbd8b7792016-04-11 16:11:07 +000071
Sanjay Patel37129072016-04-11 17:11:55 +000072 // We can always fold shl(c1) + shl(c2) -> shl(c1+c2).
73 // We can always fold lshr(c1) + lshr(c2) -> lshr(c1+c2).
74 if (IsFirstShiftLeft == IsSecondShiftLeft)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000075 return true;
76
Sanjay Patel37129072016-04-11 17:11:55 +000077 // We can always fold lshr(c) + shl(c) -> and(c2).
78 // We can always fold shl(c) + lshr(c) -> and(c2).
79 if (FirstShiftAmt == SecondShiftAmt)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000080 return true;
81
Sanjay Patelbd8b7792016-04-11 16:11:07 +000082 unsigned TypeWidth = SecondShift->getType()->getScalarSizeInBits();
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000083
Sanjay Patelbd8b7792016-04-11 16:11:07 +000084 // If the 2nd shift is bigger than the 1st, we can fold:
Sanjay Patel37129072016-04-11 17:11:55 +000085 // lshr(c1) + shl(c2) -> shl(c3) + and(c4) or
86 // shl(c1) + lshr(c2) -> lshr(c3) + and(c4),
Sanjay Patelbd8b7792016-04-11 16:11:07 +000087 // but it isn't profitable unless we know the and'd out bits are already zero.
Sanjay Patel816ec882016-04-11 16:50:32 +000088 // Also check that the 2nd shift is valid (less than the type width) or we'll
89 // crash trying to produce the bit mask for the 'and'.
90 if (SecondShiftAmt > FirstShiftAmt && SecondShiftAmt < TypeWidth) {
Sanjay Patel37129072016-04-11 17:11:55 +000091 unsigned MaskShift = IsSecondShiftLeft ? TypeWidth - SecondShiftAmt
92 : SecondShiftAmt - FirstShiftAmt;
Sanjay Patelbd8b7792016-04-11 16:11:07 +000093 APInt Mask = APInt::getLowBitsSet(TypeWidth, FirstShiftAmt) << MaskShift;
94 if (IC.MaskedValueIsZero(SecondShift->getOperand(0), Mask, 0, CxtI))
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000095 return true;
96 }
97
98 return false;
99}
100
Sanjay Patele6e84172015-11-02 22:34:55 +0000101/// See if we can compute the specified value, but shifted
Chris Lattner18d7fc82010-08-27 22:24:38 +0000102/// logically to the left or right by some number of bits. This should return
103/// true if the expression can be computed for the same cost as the current
104/// expression tree. This is used to eliminate extraneous shifting from things
105/// like:
106/// %C = shl i128 %A, 64
107/// %D = shl i128 %B, 96
108/// %E = or i128 %C, %D
109/// %F = lshr i128 %E, 64
110/// where the client will ask if E can be computed shifted right by 64-bits. If
111/// this succeeds, the GetShiftedValue function will be called to produce the
112/// value.
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000113static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
Hal Finkel60db0582014-09-07 18:57:58 +0000114 InstCombiner &IC, Instruction *CxtI) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000115 // We can always evaluate constants shifted.
116 if (isa<Constant>(V))
117 return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000118
Chris Lattner18d7fc82010-08-27 22:24:38 +0000119 Instruction *I = dyn_cast<Instruction>(V);
120 if (!I) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000121
Chris Lattner18d7fc82010-08-27 22:24:38 +0000122 // If this is the opposite shift, we can directly reuse the input of the shift
123 // if the needed bits are already zero in the input. This allows us to reuse
124 // the value which means that we don't care if the shift has multiple uses.
125 // TODO: Handle opposite shift by exact value.
Craig Topperf40110f2014-04-25 05:29:35 +0000126 ConstantInt *CI = nullptr;
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000127 if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
128 (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000129 if (CI->getZExtValue() == NumBits) {
130 // TODO: Check that the input bits are already zero with MaskedValueIsZero
131#if 0
132 // If this is a truncate of a logical shr, we can truncate it to a smaller
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000133 // lshr iff we know that the bits we would otherwise be shifting in are
Chris Lattner18d7fc82010-08-27 22:24:38 +0000134 // already zeros.
135 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
136 uint32_t BitWidth = Ty->getScalarSizeInBits();
137 if (MaskedValueIsZero(I->getOperand(0),
138 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
139 CI->getLimitedValue(BitWidth) < BitWidth) {
140 return CanEvaluateTruncated(I->getOperand(0), Ty);
141 }
142#endif
Jakub Staszak538e3862012-12-09 15:37:46 +0000143
Chris Lattner18d7fc82010-08-27 22:24:38 +0000144 }
145 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000146
Chris Lattner18d7fc82010-08-27 22:24:38 +0000147 // We can't mutate something that has multiple uses: doing so would
148 // require duplicating the instruction in general, which isn't profitable.
149 if (!I->hasOneUse()) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000150
Chris Lattner18d7fc82010-08-27 22:24:38 +0000151 switch (I->getOpcode()) {
152 default: return false;
153 case Instruction::And:
154 case Instruction::Or:
155 case Instruction::Xor:
156 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000157 return CanEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
158 CanEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
Jakub Staszak538e3862012-12-09 15:37:46 +0000159
Sanjay Patel6eaff5c2016-04-11 15:43:41 +0000160 case Instruction::Shl:
Sanjay Patel37129072016-04-11 17:11:55 +0000161 case Instruction::LShr:
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000162 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000163
Chris Lattner18d7fc82010-08-27 22:24:38 +0000164 case Instruction::Select: {
165 SelectInst *SI = cast<SelectInst>(I);
Sanjay Patel690955f2016-01-31 16:34:11 +0000166 Value *TrueVal = SI->getTrueValue();
167 Value *FalseVal = SI->getFalseValue();
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000168 return CanEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
169 CanEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000170 }
171 case Instruction::PHI: {
172 // We can change a phi if we can change all operands. Note that we never
173 // get into trouble with cyclic PHIs here because we only consider
174 // instructions with a single use.
175 PHINode *PN = cast<PHINode>(I);
Pete Cooper833f34d2015-05-12 20:05:31 +0000176 for (Value *IncValue : PN->incoming_values())
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000177 if (!CanEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
Chris Lattner18d7fc82010-08-27 22:24:38 +0000178 return false;
179 return true;
180 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000181 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000182}
183
Sanjay Patele6e84172015-11-02 22:34:55 +0000184/// When CanEvaluateShifted returned true for an expression,
Chris Lattner18d7fc82010-08-27 22:24:38 +0000185/// this value inserts the new computation that produces the shifted value.
186static Value *GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000187 InstCombiner &IC, const DataLayout &DL) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000188 // We can always evaluate constants shifted.
189 if (Constant *C = dyn_cast<Constant>(V)) {
190 if (isLeftShift)
191 V = IC.Builder->CreateShl(C, NumBits);
192 else
193 V = IC.Builder->CreateLShr(C, NumBits);
194 // If we got a constantexpr back, try to simplify it with TD info.
195 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000196 V = ConstantFoldConstantExpression(CE, DL, IC.getTargetLibraryInfo());
Chris Lattner18d7fc82010-08-27 22:24:38 +0000197 return V;
198 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000199
Chris Lattner18d7fc82010-08-27 22:24:38 +0000200 Instruction *I = cast<Instruction>(V);
201 IC.Worklist.Add(I);
202
203 switch (I->getOpcode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000204 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
Chris Lattner18d7fc82010-08-27 22:24:38 +0000205 case Instruction::And:
206 case Instruction::Or:
207 case Instruction::Xor:
208 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000209 I->setOperand(
210 0, GetShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
211 I->setOperand(
212 1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000213 return I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000214
Chris Lattner18d7fc82010-08-27 22:24:38 +0000215 case Instruction::Shl: {
Eli Friedman530341d2011-07-29 00:18:19 +0000216 BinaryOperator *BO = cast<BinaryOperator>(I);
217 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000218
219 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000220 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
221
Chris Lattner18d7fc82010-08-27 22:24:38 +0000222 // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
223 if (isLeftShift) {
224 // If this is oversized composite shift, then unsigned shifts get 0.
225 unsigned NewShAmt = NumBits+CI->getZExtValue();
226 if (NewShAmt >= TypeWidth)
227 return Constant::getNullValue(I->getType());
228
Eli Friedman530341d2011-07-29 00:18:19 +0000229 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
230 BO->setHasNoUnsignedWrap(false);
231 BO->setHasNoSignedWrap(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000232 return I;
233 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000234
Chris Lattner18d7fc82010-08-27 22:24:38 +0000235 // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
236 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000237 if (CI->getValue() == NumBits) {
238 APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
Eli Friedman530341d2011-07-29 00:18:19 +0000239 V = IC.Builder->CreateAnd(BO->getOperand(0),
240 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000241 if (Instruction *VI = dyn_cast<Instruction>(V)) {
Eli Friedman530341d2011-07-29 00:18:19 +0000242 VI->moveBefore(BO);
243 VI->takeName(BO);
Chris Lattner6c1395f2010-08-27 22:53:44 +0000244 }
245 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000246 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000247
Chris Lattner6c1395f2010-08-27 22:53:44 +0000248 // We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that
249 // the and won't be needed.
250 assert(CI->getZExtValue() > NumBits);
Eli Friedman530341d2011-07-29 00:18:19 +0000251 BO->setOperand(1, ConstantInt::get(BO->getType(),
252 CI->getZExtValue() - NumBits));
253 BO->setHasNoUnsignedWrap(false);
254 BO->setHasNoSignedWrap(false);
255 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000256 }
257 case Instruction::LShr: {
Eli Friedman530341d2011-07-29 00:18:19 +0000258 BinaryOperator *BO = cast<BinaryOperator>(I);
259 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000260 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000261 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
Jakub Staszak538e3862012-12-09 15:37:46 +0000262
Chris Lattner18d7fc82010-08-27 22:24:38 +0000263 // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
264 if (!isLeftShift) {
265 // If this is oversized composite shift, then unsigned shifts get 0.
266 unsigned NewShAmt = NumBits+CI->getZExtValue();
267 if (NewShAmt >= TypeWidth)
Eli Friedman530341d2011-07-29 00:18:19 +0000268 return Constant::getNullValue(BO->getType());
Jakub Staszak538e3862012-12-09 15:37:46 +0000269
Eli Friedman530341d2011-07-29 00:18:19 +0000270 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
271 BO->setIsExact(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000272 return I;
273 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000274
Chris Lattner18d7fc82010-08-27 22:24:38 +0000275 // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
276 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000277 if (CI->getValue() == NumBits) {
278 APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
279 V = IC.Builder->CreateAnd(I->getOperand(0),
Eli Friedman530341d2011-07-29 00:18:19 +0000280 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000281 if (Instruction *VI = dyn_cast<Instruction>(V)) {
282 VI->moveBefore(I);
283 VI->takeName(I);
284 }
285 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000286 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000287
Chris Lattner6c1395f2010-08-27 22:53:44 +0000288 // We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that
289 // the and won't be needed.
290 assert(CI->getZExtValue() > NumBits);
Eli Friedman530341d2011-07-29 00:18:19 +0000291 BO->setOperand(1, ConstantInt::get(BO->getType(),
292 CI->getZExtValue() - NumBits));
293 BO->setIsExact(false);
294 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000295 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000296
Chris Lattner18d7fc82010-08-27 22:24:38 +0000297 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000298 I->setOperand(
299 1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
300 I->setOperand(
301 2, GetShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000302 return I;
303 case Instruction::PHI: {
304 // We can change a phi if we can change all operands. Note that we never
305 // get into trouble with cyclic PHIs here because we only consider
306 // instructions with a single use.
307 PHINode *PN = cast<PHINode>(I);
308 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000309 PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i), NumBits,
310 isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000311 return PN;
312 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000313 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000314}
315
316
317
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000318Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattnerdc67e132010-01-05 07:44:46 +0000319 BinaryOperator &I) {
320 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Jakub Staszak538e3862012-12-09 15:37:46 +0000321
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000322 ConstantInt *COp1 = nullptr;
323 if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
324 COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
325 else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
326 COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
327 else
328 COp1 = dyn_cast<ConstantInt>(Op1);
329
330 if (!COp1)
331 return nullptr;
Jakub Staszak538e3862012-12-09 15:37:46 +0000332
Chris Lattner18d7fc82010-08-27 22:24:38 +0000333 // See if we can propagate this shift into the input, this covers the trivial
334 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
335 if (I.getOpcode() != Instruction::AShr &&
Hal Finkel60db0582014-09-07 18:57:58 +0000336 CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)) {
Chris Lattnerdd660102010-08-28 01:20:38 +0000337 DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
338 " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n");
Jakub Staszak538e3862012-12-09 15:37:46 +0000339
Sanjay Patel4b198802016-02-01 22:23:39 +0000340 return replaceInstUsesWith(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000341 I, GetShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000342 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000343
Jakub Staszak538e3862012-12-09 15:37:46 +0000344 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattnerdc67e132010-01-05 07:44:46 +0000345 // purpose is to compute bits we don't care about.
346 uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
Jakub Staszak538e3862012-12-09 15:37:46 +0000347
Matt Arsenault6b4bed42014-04-23 16:48:40 +0000348 assert(!COp1->uge(TypeBits) &&
349 "Shift over the type width should have been removed already");
Jakub Staszak538e3862012-12-09 15:37:46 +0000350
Chris Lattnerdc67e132010-01-05 07:44:46 +0000351 // ((X*C1) << C2) == (X * (C1 << C2))
352 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
353 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
354 if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
355 return BinaryOperator::CreateMul(BO->getOperand(0),
Sanjay Patel690955f2016-01-31 16:34:11 +0000356 ConstantExpr::getShl(BOOp, Op1));
Jakub Staszak538e3862012-12-09 15:37:46 +0000357
Chris Lattnerdc67e132010-01-05 07:44:46 +0000358 // Try to fold constant and into select arguments.
359 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
360 if (Instruction *R = FoldOpIntoSelect(I, SI))
361 return R;
362 if (isa<PHINode>(Op0))
363 if (Instruction *NV = FoldOpIntoPhi(I))
364 return NV;
Jakub Staszak538e3862012-12-09 15:37:46 +0000365
Chris Lattnerdc67e132010-01-05 07:44:46 +0000366 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
367 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
368 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
369 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
370 // place. Don't try to do this transformation in this case. Also, we
371 // require that the input operand is a shift-by-constant so that we have
372 // confidence that the shifts will get folded together. We could do this
373 // xform in more cases, but it is unlikely to be profitable.
Jakub Staszak538e3862012-12-09 15:37:46 +0000374 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
Chris Lattnerdc67e132010-01-05 07:44:46 +0000375 isa<ConstantInt>(TrOp->getOperand(1))) {
376 // Okay, we'll do this xform. Make the shift of shift.
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000377 Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000378 // (shift2 (shift1 & 0x00FF), c2)
379 Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
380
381 // For logical shifts, the truncation has the effect of making the high
382 // part of the register be zeros. Emulate this by inserting an AND to
383 // clear the top bits as needed. This 'and' will usually be zapped by
384 // other xforms later if dead.
385 unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
386 unsigned DstSize = TI->getType()->getScalarSizeInBits();
387 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
Jakub Staszak538e3862012-12-09 15:37:46 +0000388
Chris Lattnerdc67e132010-01-05 07:44:46 +0000389 // The mask we constructed says what the trunc would do if occurring
390 // between the shifts. We want to know the effect *after* the second
391 // shift. We know that it is a logical shift by a constant, so adjust the
392 // mask as appropriate.
393 if (I.getOpcode() == Instruction::Shl)
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000394 MaskV <<= COp1->getZExtValue();
Chris Lattnerdc67e132010-01-05 07:44:46 +0000395 else {
396 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000397 MaskV = MaskV.lshr(COp1->getZExtValue());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000398 }
399
400 // shift1 & 0x00FF
401 Value *And = Builder->CreateAnd(NSh,
402 ConstantInt::get(I.getContext(), MaskV),
403 TI->getName());
404
405 // Return the value truncated to the interesting size.
406 return new TruncInst(And, I.getType());
407 }
408 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000409
Chris Lattnerdc67e132010-01-05 07:44:46 +0000410 if (Op0->hasOneUse()) {
411 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
412 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
413 Value *V1, *V2;
414 ConstantInt *CC;
415 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000416 default: break;
417 case Instruction::Add:
418 case Instruction::And:
419 case Instruction::Or:
420 case Instruction::Xor: {
421 // These operators commute.
422 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
423 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
424 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
425 m_Specific(Op1)))) {
426 Value *YS = // (Y << C)
427 Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
428 // (X + (Y << C))
429 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
430 Op0BO->getOperand(1)->getName());
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000431 uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
432
433 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
434 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
435 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
436 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
437 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000438 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000439
Chris Lattner2b459fe2010-01-10 06:59:55 +0000440 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
441 Value *Op0BOOp1 = Op0BO->getOperand(1);
442 if (isLeftShift && Op0BOOp1->hasOneUse() &&
Jakub Staszak538e3862012-12-09 15:37:46 +0000443 match(Op0BOOp1,
Jakub Staszak84321852012-12-09 16:06:44 +0000444 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
445 m_ConstantInt(CC)))) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000446 Value *YS = // (Y << C)
447 Builder->CreateShl(Op0BO->getOperand(0), Op1,
448 Op0BO->getName());
449 // X & (CC << C)
450 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
451 V1->getName()+".mask");
452 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000453 }
454 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000455
Chris Lattner2b459fe2010-01-10 06:59:55 +0000456 // FALL THROUGH.
457 case Instruction::Sub: {
458 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
459 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
460 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
461 m_Specific(Op1)))) {
462 Value *YS = // (Y << C)
463 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
464 // (X + (Y << C))
465 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
466 Op0BO->getOperand(0)->getName());
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000467 uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
468
469 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
470 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
471 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
472 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
473 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattner2b459fe2010-01-10 06:59:55 +0000474 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000475
Chris Lattner2b459fe2010-01-10 06:59:55 +0000476 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
477 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
478 match(Op0BO->getOperand(0),
Jakub Staszak84321852012-12-09 16:06:44 +0000479 m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
480 m_ConstantInt(CC))) && V2 == Op1) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000481 Value *YS = // (Y << C)
482 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
483 // X & (CC << C)
484 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
485 V1->getName()+".mask");
Jakub Staszak538e3862012-12-09 15:37:46 +0000486
Chris Lattner2b459fe2010-01-10 06:59:55 +0000487 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
488 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000489
Chris Lattner2b459fe2010-01-10 06:59:55 +0000490 break;
491 }
492 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000493
494
Sanjay Patelfc3d8f02014-07-22 04:57:06 +0000495 // If the operand is a bitwise operator with a constant RHS, and the
Chris Lattnerdc67e132010-01-05 07:44:46 +0000496 // shift is the only use, we can pull it out of the shift.
497 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
498 bool isValid = true; // Valid only for And, Or, Xor
499 bool highBitSet = false; // Transform if high bit of constant set?
Jakub Staszak538e3862012-12-09 15:37:46 +0000500
Chris Lattnerdc67e132010-01-05 07:44:46 +0000501 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000502 default: isValid = false; break; // Do not perform transform!
503 case Instruction::Add:
504 isValid = isLeftShift;
505 break;
506 case Instruction::Or:
507 case Instruction::Xor:
508 highBitSet = false;
509 break;
510 case Instruction::And:
511 highBitSet = true;
512 break;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000513 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000514
Chris Lattnerdc67e132010-01-05 07:44:46 +0000515 // If this is a signed shift right, and the high bit is modified
516 // by the logical operation, do not perform the transformation.
517 // The highBitSet boolean indicates the value of the high bit of
518 // the constant which would cause it to be modified for this
519 // operation.
520 //
521 if (isValid && I.getOpcode() == Instruction::AShr)
522 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
Jakub Staszak538e3862012-12-09 15:37:46 +0000523
Chris Lattnerdc67e132010-01-05 07:44:46 +0000524 if (isValid) {
525 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000526
Chris Lattnerdc67e132010-01-05 07:44:46 +0000527 Value *NewShift =
528 Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
529 NewShift->takeName(Op0BO);
Jakub Staszak538e3862012-12-09 15:37:46 +0000530
Chris Lattnerdc67e132010-01-05 07:44:46 +0000531 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
532 NewRHS);
533 }
534 }
535 }
536 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000537
Chris Lattnerdc67e132010-01-05 07:44:46 +0000538 // Find out if this is a shift of a shift by a constant.
539 BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
540 if (ShiftOp && !ShiftOp->isShift())
Craig Topperf40110f2014-04-25 05:29:35 +0000541 ShiftOp = nullptr;
Jakub Staszak538e3862012-12-09 15:37:46 +0000542
Chris Lattnerdc67e132010-01-05 07:44:46 +0000543 if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000544
545 // This is a constant shift of a constant shift. Be careful about hiding
546 // shl instructions behind bit masks. They are used to represent multiplies
547 // by a constant, and it is important that simple arithmetic expressions
548 // are still recognizable by scalar evolution.
549 //
550 // The transforms applied to shl are very similar to the transforms applied
551 // to mul by constant. We can be more aggressive about optimizing right
552 // shifts.
553 //
554 // Combinations of right and left shifts will still be optimized in
555 // DAGCombine where scalar evolution no longer applies.
556
Chris Lattnerdc67e132010-01-05 07:44:46 +0000557 ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
558 uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000559 uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000560 assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
Craig Topperf40110f2014-04-25 05:29:35 +0000561 if (ShiftAmt1 == 0) return nullptr; // Will be simplified in the future.
Chris Lattnerdc67e132010-01-05 07:44:46 +0000562 Value *X = ShiftOp->getOperand(0);
Jakub Staszak538e3862012-12-09 15:37:46 +0000563
Chris Lattner229907c2011-07-18 04:54:35 +0000564 IntegerType *Ty = cast<IntegerType>(I.getType());
Jakub Staszak538e3862012-12-09 15:37:46 +0000565
Chris Lattnerdc67e132010-01-05 07:44:46 +0000566 // Check for (X << c1) << c2 and (X >> c1) >> c2
567 if (I.getOpcode() == ShiftOp->getOpcode()) {
Nick Lewyckyb59008c2011-12-31 21:30:22 +0000568 uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
Chris Lattnerdc67e132010-01-05 07:44:46 +0000569 // If this is oversized composite shift, then unsigned shifts get 0, ashr
570 // saturates.
571 if (AmtSum >= TypeBits) {
572 if (I.getOpcode() != Instruction::AShr)
Sanjay Patel4b198802016-02-01 22:23:39 +0000573 return replaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerdc67e132010-01-05 07:44:46 +0000574 AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
575 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000576
Chris Lattnerdc67e132010-01-05 07:44:46 +0000577 return BinaryOperator::Create(I.getOpcode(), X,
578 ConstantInt::get(Ty, AmtSum));
579 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000580
Chris Lattnerdc67e132010-01-05 07:44:46 +0000581 if (ShiftAmt1 == ShiftAmt2) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000582 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
Chris Lattner25a198e2010-08-27 21:04:34 +0000583 if (I.getOpcode() == Instruction::LShr &&
584 ShiftOp->getOpcode() == Instruction::Shl) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000585 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
586 return BinaryOperator::CreateAnd(X,
587 ConstantInt::get(I.getContext(), Mask));
588 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000589 } else if (ShiftAmt1 < ShiftAmt2) {
590 uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000591
592 // (X >>?,exact C1) << C2 --> X << (C2-C1)
593 // The inexact version is deferred to DAGCombine so we don't hide shl
594 // behind a bit mask.
Chris Lattner25a198e2010-08-27 21:04:34 +0000595 if (I.getOpcode() == Instruction::Shl &&
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000596 ShiftOp->getOpcode() != Instruction::Shl &&
597 ShiftOp->isExact()) {
Chris Lattnerdc67e132010-01-05 07:44:46 +0000598 assert(ShiftOp->getOpcode() == Instruction::LShr ||
599 ShiftOp->getOpcode() == Instruction::AShr);
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000600 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000601 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
602 X, ShiftDiffCst);
603 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
604 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
605 return NewShl;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000606 }
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000607
Chris Lattnerdc67e132010-01-05 07:44:46 +0000608 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
Chris Lattner25a198e2010-08-27 21:04:34 +0000609 if (I.getOpcode() == Instruction::LShr &&
610 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000611 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
612 // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
613 if (ShiftOp->hasNoUnsignedWrap()) {
614 BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
615 X, ShiftDiffCst);
616 NewLShr->setIsExact(I.isExact());
617 return NewLShr;
618 }
619 Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
Jakub Staszak538e3862012-12-09 15:37:46 +0000620
Chris Lattnerdc67e132010-01-05 07:44:46 +0000621 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
622 return BinaryOperator::CreateAnd(Shift,
623 ConstantInt::get(I.getContext(),Mask));
624 }
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000625
626 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
627 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
628 if (I.getOpcode() == Instruction::AShr &&
629 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000630 if (ShiftOp->hasNoSignedWrap()) {
631 // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
632 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
633 BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
634 X, ShiftDiffCst);
635 NewAShr->setIsExact(I.isExact());
636 return NewAShr;
637 }
638 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000639 } else {
640 assert(ShiftAmt2 < ShiftAmt1);
641 uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
642
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000643 // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
644 // The inexact version is deferred to DAGCombine so we don't hide shl
645 // behind a bit mask.
Chris Lattner25a198e2010-08-27 21:04:34 +0000646 if (I.getOpcode() == Instruction::Shl &&
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000647 ShiftOp->getOpcode() != Instruction::Shl &&
648 ShiftOp->isExact()) {
Nick Lewyckyb59008c2011-12-31 21:30:22 +0000649 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000650 BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
651 X, ShiftDiffCst);
652 NewShr->setIsExact(true);
653 return NewShr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000654 }
Jakob Stoklund Olesen43bcb972012-04-23 17:39:52 +0000655
Chris Lattnerdc67e132010-01-05 07:44:46 +0000656 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
Chris Lattner25a198e2010-08-27 21:04:34 +0000657 if (I.getOpcode() == Instruction::LShr &&
658 ShiftOp->getOpcode() == Instruction::Shl) {
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000659 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
660 if (ShiftOp->hasNoUnsignedWrap()) {
661 // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
662 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
663 X, ShiftDiffCst);
664 NewShl->setHasNoUnsignedWrap(true);
665 return NewShl;
666 }
667 Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
Jakub Staszak538e3862012-12-09 15:37:46 +0000668
Chris Lattnerdc67e132010-01-05 07:44:46 +0000669 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
670 return BinaryOperator::CreateAnd(Shift,
671 ConstantInt::get(I.getContext(),Mask));
672 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000673
Nick Lewycky0c48afa2012-01-04 09:28:29 +0000674 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
675 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
676 if (I.getOpcode() == Instruction::AShr &&
677 ShiftOp->getOpcode() == Instruction::Shl) {
678 if (ShiftOp->hasNoSignedWrap()) {
679 // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
680 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
681 BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
682 X, ShiftDiffCst);
683 NewShl->setHasNoSignedWrap(true);
684 return NewShl;
685 }
686 }
Chris Lattnerdc67e132010-01-05 07:44:46 +0000687 }
688 }
Craig Topperf40110f2014-04-25 05:29:35 +0000689 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000690}
691
692Instruction *InstCombiner::visitShl(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000693 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000694 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000695
Chandler Carruth66b31302015-01-04 12:03:27 +0000696 if (Value *V =
697 SimplifyShlInst(I.getOperand(0), I.getOperand(1), I.hasNoSignedWrap(),
698 I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000699 return replaceInstUsesWith(I, V);
Jakub Staszak538e3862012-12-09 15:37:46 +0000700
Chris Lattner6b657ae2011-02-10 05:36:31 +0000701 if (Instruction *V = commonShiftTransforms(I))
702 return V;
Jakub Staszak538e3862012-12-09 15:37:46 +0000703
Chris Lattner6b657ae2011-02-10 05:36:31 +0000704 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
705 unsigned ShAmt = Op1C->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000706
Chris Lattner6b657ae2011-02-10 05:36:31 +0000707 // If the shifted-out value is known-zero, then this is a NUW shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000708 if (!I.hasNoUnsignedWrap() &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000709 MaskedValueIsZero(I.getOperand(0),
Sanjay Patel690955f2016-01-31 16:34:11 +0000710 APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt), 0,
711 &I)) {
712 I.setHasNoUnsignedWrap();
713 return &I;
714 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000715
Chris Lattner6b657ae2011-02-10 05:36:31 +0000716 // If the shifted out value is all signbits, this is a NSW shift.
717 if (!I.hasNoSignedWrap() &&
Hal Finkel60db0582014-09-07 18:57:58 +0000718 ComputeNumSignBits(I.getOperand(0), 0, &I) > ShAmt) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000719 I.setHasNoSignedWrap();
720 return &I;
721 }
722 }
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000723
Benjamin Kramerf0e3f042011-04-29 08:41:23 +0000724 // (C1 << A) << C2 -> (C1 << C2) << A
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000725 Constant *C1, *C2;
726 Value *A;
727 if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
728 match(I.getOperand(1), m_Constant(C2)))
729 return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
730
Craig Topperf40110f2014-04-25 05:29:35 +0000731 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000732}
733
734Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000735 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000736 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000737
Chandler Carruth66b31302015-01-04 12:03:27 +0000738 if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
739 DL, TLI, DT, AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000740 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000741
Chris Lattner249da5c2010-01-23 18:49:30 +0000742 if (Instruction *R = commonShiftTransforms(I))
743 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000744
Chris Lattner249da5c2010-01-23 18:49:30 +0000745 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000746
Chris Lattner6b657ae2011-02-10 05:36:31 +0000747 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
748 unsigned ShAmt = Op1C->getZExtValue();
749
Chris Lattner249da5c2010-01-23 18:49:30 +0000750 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
Chris Lattnere112ff62010-01-23 23:31:46 +0000751 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
Chris Lattner249da5c2010-01-23 18:49:30 +0000752 // ctlz.i32(x)>>5 --> zext(x == 0)
753 // cttz.i32(x)>>5 --> zext(x == 0)
754 // ctpop.i32(x)>>5 --> zext(x == -1)
755 if ((II->getIntrinsicID() == Intrinsic::ctlz ||
756 II->getIntrinsicID() == Intrinsic::cttz ||
757 II->getIntrinsicID() == Intrinsic::ctpop) &&
Chris Lattner6b657ae2011-02-10 05:36:31 +0000758 isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
Chris Lattner249da5c2010-01-23 18:49:30 +0000759 bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
Chris Lattnere112ff62010-01-23 23:31:46 +0000760 Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
Gabor Greif4a39b842010-06-24 00:44:01 +0000761 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
Chris Lattner249da5c2010-01-23 18:49:30 +0000762 return new ZExtInst(Cmp, II->getType());
763 }
764 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000765
Chris Lattner6b657ae2011-02-10 05:36:31 +0000766 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000767 if (!I.isExact() &&
Hal Finkel60db0582014-09-07 18:57:58 +0000768 MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), ShAmt),
769 0, &I)){
Chris Lattner6b657ae2011-02-10 05:36:31 +0000770 I.setIsExact();
771 return &I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000772 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000773 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000774
Craig Topperf40110f2014-04-25 05:29:35 +0000775 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000776}
777
778Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000779 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000780 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000781
Chandler Carruth66b31302015-01-04 12:03:27 +0000782 if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
783 DL, TLI, DT, AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000784 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000785
Chris Lattnerdc67e132010-01-05 07:44:46 +0000786 if (Instruction *R = commonShiftTransforms(I))
787 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000788
Chris Lattnera1e223e2010-01-08 19:04:21 +0000789 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000790
Chris Lattnera1e223e2010-01-08 19:04:21 +0000791 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000792 unsigned ShAmt = Op1C->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000793
Chris Lattnera1e223e2010-01-08 19:04:21 +0000794 // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
Chris Lattner43f2fa62010-01-18 22:19:16 +0000795 // have a sign-extend idiom.
Chris Lattnera1e223e2010-01-08 19:04:21 +0000796 Value *X;
Chris Lattner43f2fa62010-01-18 22:19:16 +0000797 if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
Chris Lattner43f2fa62010-01-18 22:19:16 +0000798 // If the input is an extension from the shifted amount value, e.g.
799 // %x = zext i8 %A to i32
800 // %y = shl i32 %x, 24
801 // %z = ashr %y, 24
802 // then turn this into "z = sext i8 A to i32".
803 if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
804 uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
805 uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
806 if (Op1C->getZExtValue() == DestBits-SrcBits)
807 return new SExtInst(ZI->getOperand(0), ZI->getType());
808 }
809 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000810
811 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000812 if (!I.isExact() &&
Sanjay Patel690955f2016-01-31 16:34:11 +0000813 MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), ShAmt),
814 0, &I)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000815 I.setIsExact();
816 return &I;
817 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000818 }
819
Chris Lattnerdc67e132010-01-05 07:44:46 +0000820 // See if we can turn a signed shr into an unsigned shr.
821 if (MaskedValueIsZero(Op0,
Hal Finkel60db0582014-09-07 18:57:58 +0000822 APInt::getSignBit(I.getType()->getScalarSizeInBits()),
823 0, &I))
Chris Lattnera1e223e2010-01-08 19:04:21 +0000824 return BinaryOperator::CreateLShr(Op0, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000825
Craig Topperf40110f2014-04-25 05:29:35 +0000826 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000827}