blob: f74e499397ee26385bdccf987d84d5c8b864f383 [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) {
Chris Lattnerdc67e132010-01-05 07:44:46 +000025 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Sanjay Patelcf082032017-01-13 18:08:25 +000026 assert(Op0->getType() == Op1->getType());
Chris Lattnerdc67e132010-01-05 07:44:46 +000027
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
Simon Pilgrim6dd8fab2016-11-01 15:40:30 +000042 // (C1 shift (A add C2)) -> (C1 shift C2) shift A)
43 // iff A and C2 are both positive.
44 Value *A;
45 Constant *C;
46 if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
47 if (isKnownNonNegative(A, DL) && isKnownNonNegative(C, DL))
48 return BinaryOperator::Create(
49 I.getOpcode(), Builder->CreateBinOp(I.getOpcode(), Op0, C), A);
50
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000051 // 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 +000052 // Because shifts by negative values (which could occur if A were negative)
53 // are undefined.
Simon Pilgrim6dd8fab2016-11-01 15:40:30 +000054 const APInt *B;
Chris Lattner6b657ae2011-02-10 05:36:31 +000055 if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
56 // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
57 // demand the sign bit (and many others) here??
58 Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
59 Op1->getName());
60 I.setOperand(1, Rem);
61 return &I;
62 }
Jakub Staszak538e3862012-12-09 15:37:46 +000063
Craig Topperf40110f2014-04-25 05:29:35 +000064 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +000065}
66
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000067/// Return true if we can simplify two logical (either left or right) shifts
68/// that have constant shift amounts.
Sanjay Patelbd8b7792016-04-11 16:11:07 +000069static bool canEvaluateShiftedShift(unsigned FirstShiftAmt,
70 bool IsFirstShiftLeft,
71 Instruction *SecondShift, InstCombiner &IC,
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000072 Instruction *CxtI) {
Sanjay Patel3a48e982016-04-11 17:27:44 +000073 assert(SecondShift->isLogicalShift() && "Unexpected instruction type");
Sanjay Patelb91bcd72016-04-11 17:35:57 +000074
Sanjay Patelbd8b7792016-04-11 16:11:07 +000075 // We need constant shifts.
76 auto *SecondShiftConst = dyn_cast<ConstantInt>(SecondShift->getOperand(1));
77 if (!SecondShiftConst)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000078 return false;
79
Sanjay Patelbd8b7792016-04-11 16:11:07 +000080 unsigned SecondShiftAmt = SecondShiftConst->getZExtValue();
Sanjay Patel37129072016-04-11 17:11:55 +000081 bool IsSecondShiftLeft = SecondShift->getOpcode() == Instruction::Shl;
Sanjay Patelbd8b7792016-04-11 16:11:07 +000082
Sanjay Patel37129072016-04-11 17:11:55 +000083 // We can always fold shl(c1) + shl(c2) -> shl(c1+c2).
84 // We can always fold lshr(c1) + lshr(c2) -> lshr(c1+c2).
85 if (IsFirstShiftLeft == IsSecondShiftLeft)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000086 return true;
87
Sanjay Patel37129072016-04-11 17:11:55 +000088 // We can always fold lshr(c) + shl(c) -> and(c2).
89 // We can always fold shl(c) + lshr(c) -> and(c2).
90 if (FirstShiftAmt == SecondShiftAmt)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000091 return true;
92
Sanjay Patelbd8b7792016-04-11 16:11:07 +000093 unsigned TypeWidth = SecondShift->getType()->getScalarSizeInBits();
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000094
Sanjay Patelbd8b7792016-04-11 16:11:07 +000095 // If the 2nd shift is bigger than the 1st, we can fold:
Sanjay Patel37129072016-04-11 17:11:55 +000096 // lshr(c1) + shl(c2) -> shl(c3) + and(c4) or
97 // shl(c1) + lshr(c2) -> lshr(c3) + and(c4),
Sanjay Patelbd8b7792016-04-11 16:11:07 +000098 // but it isn't profitable unless we know the and'd out bits are already zero.
Sanjay Patel816ec882016-04-11 16:50:32 +000099 // Also check that the 2nd shift is valid (less than the type width) or we'll
100 // crash trying to produce the bit mask for the 'and'.
101 if (SecondShiftAmt > FirstShiftAmt && SecondShiftAmt < TypeWidth) {
Sanjay Patel37129072016-04-11 17:11:55 +0000102 unsigned MaskShift = IsSecondShiftLeft ? TypeWidth - SecondShiftAmt
103 : SecondShiftAmt - FirstShiftAmt;
Sanjay Patelbd8b7792016-04-11 16:11:07 +0000104 APInt Mask = APInt::getLowBitsSet(TypeWidth, FirstShiftAmt) << MaskShift;
105 if (IC.MaskedValueIsZero(SecondShift->getOperand(0), Mask, 0, CxtI))
Sanjay Patel6eaff5c2016-04-11 15:43:41 +0000106 return true;
107 }
108
109 return false;
110}
111
Sanjay Patele6e84172015-11-02 22:34:55 +0000112/// See if we can compute the specified value, but shifted
Chris Lattner18d7fc82010-08-27 22:24:38 +0000113/// logically to the left or right by some number of bits. This should return
114/// true if the expression can be computed for the same cost as the current
115/// expression tree. This is used to eliminate extraneous shifting from things
116/// like:
117/// %C = shl i128 %A, 64
118/// %D = shl i128 %B, 96
119/// %E = or i128 %C, %D
120/// %F = lshr i128 %E, 64
121/// where the client will ask if E can be computed shifted right by 64-bits. If
122/// this succeeds, the GetShiftedValue function will be called to produce the
123/// value.
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000124static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
Hal Finkel60db0582014-09-07 18:57:58 +0000125 InstCombiner &IC, Instruction *CxtI) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000126 // We can always evaluate constants shifted.
127 if (isa<Constant>(V))
128 return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000129
Chris Lattner18d7fc82010-08-27 22:24:38 +0000130 Instruction *I = dyn_cast<Instruction>(V);
131 if (!I) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000132
Chris Lattner18d7fc82010-08-27 22:24:38 +0000133 // If this is the opposite shift, we can directly reuse the input of the shift
134 // if the needed bits are already zero in the input. This allows us to reuse
135 // the value which means that we don't care if the shift has multiple uses.
136 // TODO: Handle opposite shift by exact value.
Craig Topperf40110f2014-04-25 05:29:35 +0000137 ConstantInt *CI = nullptr;
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000138 if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
139 (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000140 if (CI->getZExtValue() == NumBits) {
141 // TODO: Check that the input bits are already zero with MaskedValueIsZero
142#if 0
143 // If this is a truncate of a logical shr, we can truncate it to a smaller
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000144 // lshr iff we know that the bits we would otherwise be shifting in are
Chris Lattner18d7fc82010-08-27 22:24:38 +0000145 // already zeros.
146 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
147 uint32_t BitWidth = Ty->getScalarSizeInBits();
148 if (MaskedValueIsZero(I->getOperand(0),
149 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
150 CI->getLimitedValue(BitWidth) < BitWidth) {
151 return CanEvaluateTruncated(I->getOperand(0), Ty);
152 }
153#endif
Jakub Staszak538e3862012-12-09 15:37:46 +0000154
Chris Lattner18d7fc82010-08-27 22:24:38 +0000155 }
156 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000157
Chris Lattner18d7fc82010-08-27 22:24:38 +0000158 // We can't mutate something that has multiple uses: doing so would
159 // require duplicating the instruction in general, which isn't profitable.
160 if (!I->hasOneUse()) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000161
Chris Lattner18d7fc82010-08-27 22:24:38 +0000162 switch (I->getOpcode()) {
163 default: return false;
164 case Instruction::And:
165 case Instruction::Or:
166 case Instruction::Xor:
167 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000168 return CanEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
169 CanEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
Jakub Staszak538e3862012-12-09 15:37:46 +0000170
Sanjay Patel6eaff5c2016-04-11 15:43:41 +0000171 case Instruction::Shl:
Sanjay Patel37129072016-04-11 17:11:55 +0000172 case Instruction::LShr:
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000173 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000174
Chris Lattner18d7fc82010-08-27 22:24:38 +0000175 case Instruction::Select: {
176 SelectInst *SI = cast<SelectInst>(I);
Sanjay Patel690955f2016-01-31 16:34:11 +0000177 Value *TrueVal = SI->getTrueValue();
178 Value *FalseVal = SI->getFalseValue();
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000179 return CanEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
180 CanEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000181 }
182 case Instruction::PHI: {
183 // We can change a phi if we can change all operands. Note that we never
184 // get into trouble with cyclic PHIs here because we only consider
185 // instructions with a single use.
186 PHINode *PN = cast<PHINode>(I);
Pete Cooper833f34d2015-05-12 20:05:31 +0000187 for (Value *IncValue : PN->incoming_values())
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000188 if (!CanEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
Chris Lattner18d7fc82010-08-27 22:24:38 +0000189 return false;
190 return true;
191 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000192 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000193}
194
Sanjay Patele6e84172015-11-02 22:34:55 +0000195/// When CanEvaluateShifted returned true for an expression,
Chris Lattner18d7fc82010-08-27 22:24:38 +0000196/// this value inserts the new computation that produces the shifted value.
197static Value *GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000198 InstCombiner &IC, const DataLayout &DL) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000199 // We can always evaluate constants shifted.
200 if (Constant *C = dyn_cast<Constant>(V)) {
201 if (isLeftShift)
202 V = IC.Builder->CreateShl(C, NumBits);
203 else
204 V = IC.Builder->CreateLShr(C, NumBits);
205 // If we got a constantexpr back, try to simplify it with TD info.
David Majnemerd536f232016-07-29 03:27:26 +0000206 if (auto *C = dyn_cast<Constant>(V))
207 if (auto *FoldedC =
Justin Bogner99798402016-08-05 01:06:44 +0000208 ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo()))
David Majnemerd536f232016-07-29 03:27:26 +0000209 V = FoldedC;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000210 return V;
211 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000212
Chris Lattner18d7fc82010-08-27 22:24:38 +0000213 Instruction *I = cast<Instruction>(V);
214 IC.Worklist.Add(I);
215
216 switch (I->getOpcode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000217 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
Chris Lattner18d7fc82010-08-27 22:24:38 +0000218 case Instruction::And:
219 case Instruction::Or:
220 case Instruction::Xor:
221 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000222 I->setOperand(
223 0, GetShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
224 I->setOperand(
225 1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000226 return I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000227
Chris Lattner18d7fc82010-08-27 22:24:38 +0000228 case Instruction::Shl: {
Eli Friedman530341d2011-07-29 00:18:19 +0000229 BinaryOperator *BO = cast<BinaryOperator>(I);
230 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000231
232 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000233 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
234
Chris Lattner18d7fc82010-08-27 22:24:38 +0000235 // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
236 if (isLeftShift) {
237 // If this is oversized composite shift, then unsigned shifts get 0.
238 unsigned NewShAmt = NumBits+CI->getZExtValue();
239 if (NewShAmt >= TypeWidth)
240 return Constant::getNullValue(I->getType());
241
Eli Friedman530341d2011-07-29 00:18:19 +0000242 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
243 BO->setHasNoUnsignedWrap(false);
244 BO->setHasNoSignedWrap(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000245 return I;
246 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000247
Chris Lattner18d7fc82010-08-27 22:24:38 +0000248 // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
249 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000250 if (CI->getValue() == NumBits) {
251 APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
Eli Friedman530341d2011-07-29 00:18:19 +0000252 V = IC.Builder->CreateAnd(BO->getOperand(0),
253 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000254 if (Instruction *VI = dyn_cast<Instruction>(V)) {
Eli Friedman530341d2011-07-29 00:18:19 +0000255 VI->moveBefore(BO);
256 VI->takeName(BO);
Chris Lattner6c1395f2010-08-27 22:53:44 +0000257 }
258 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000259 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000260
Chris Lattner6c1395f2010-08-27 22:53:44 +0000261 // We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that
262 // the and won't be needed.
263 assert(CI->getZExtValue() > NumBits);
Eli Friedman530341d2011-07-29 00:18:19 +0000264 BO->setOperand(1, ConstantInt::get(BO->getType(),
265 CI->getZExtValue() - NumBits));
266 BO->setHasNoUnsignedWrap(false);
267 BO->setHasNoSignedWrap(false);
268 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000269 }
Sanjay Patelb91bcd72016-04-11 17:35:57 +0000270 // FIXME: This is almost identical to the SHL case. Refactor both cases into
271 // a helper function.
Chris Lattner18d7fc82010-08-27 22:24:38 +0000272 case Instruction::LShr: {
Eli Friedman530341d2011-07-29 00:18:19 +0000273 BinaryOperator *BO = cast<BinaryOperator>(I);
274 unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000275 // We only accept shifts-by-a-constant in CanEvaluateShifted.
Eli Friedman530341d2011-07-29 00:18:19 +0000276 ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
Jakub Staszak538e3862012-12-09 15:37:46 +0000277
Chris Lattner18d7fc82010-08-27 22:24:38 +0000278 // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
279 if (!isLeftShift) {
280 // If this is oversized composite shift, then unsigned shifts get 0.
281 unsigned NewShAmt = NumBits+CI->getZExtValue();
282 if (NewShAmt >= TypeWidth)
Eli Friedman530341d2011-07-29 00:18:19 +0000283 return Constant::getNullValue(BO->getType());
Jakub Staszak538e3862012-12-09 15:37:46 +0000284
Eli Friedman530341d2011-07-29 00:18:19 +0000285 BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
286 BO->setIsExact(false);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000287 return I;
288 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000289
Chris Lattner18d7fc82010-08-27 22:24:38 +0000290 // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
291 // zeros.
Chris Lattner6c1395f2010-08-27 22:53:44 +0000292 if (CI->getValue() == NumBits) {
293 APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
294 V = IC.Builder->CreateAnd(I->getOperand(0),
Eli Friedman530341d2011-07-29 00:18:19 +0000295 ConstantInt::get(BO->getContext(), Mask));
Chris Lattner6c1395f2010-08-27 22:53:44 +0000296 if (Instruction *VI = dyn_cast<Instruction>(V)) {
297 VI->moveBefore(I);
298 VI->takeName(I);
299 }
300 return V;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000301 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000302
Chris Lattner6c1395f2010-08-27 22:53:44 +0000303 // We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that
304 // the and won't be needed.
305 assert(CI->getZExtValue() > NumBits);
Eli Friedman530341d2011-07-29 00:18:19 +0000306 BO->setOperand(1, ConstantInt::get(BO->getType(),
307 CI->getZExtValue() - NumBits));
308 BO->setIsExact(false);
309 return BO;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000310 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000311
Chris Lattner18d7fc82010-08-27 22:24:38 +0000312 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000313 I->setOperand(
314 1, GetShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
315 I->setOperand(
316 2, GetShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000317 return I;
318 case Instruction::PHI: {
319 // We can change a phi if we can change all operands. Note that we never
320 // get into trouble with cyclic PHIs here because we only consider
321 // instructions with a single use.
322 PHINode *PN = cast<PHINode>(I);
323 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000324 PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i), NumBits,
325 isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000326 return PN;
327 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000328 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000329}
330
Sanjay Patel4c48bbe2016-12-10 22:16:29 +0000331/// Try to fold (X << C1) << C2, where the shifts are some combination of
332/// shl/ashr/lshr.
333static Instruction *
334foldShiftByConstOfShiftByConst(BinaryOperator &I, ConstantInt *COp1,
335 InstCombiner::BuilderTy *Builder) {
336 Value *Op0 = I.getOperand(0);
337 uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
Chris Lattner18d7fc82010-08-27 22:24:38 +0000338
Sanjay Patel4c48bbe2016-12-10 22:16:29 +0000339 // Find out if this is a shift of a shift by a constant.
340 BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
341 if (ShiftOp && !ShiftOp->isShift())
342 ShiftOp = nullptr;
343
344 if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
345
346 // This is a constant shift of a constant shift. Be careful about hiding
347 // shl instructions behind bit masks. They are used to represent multiplies
348 // by a constant, and it is important that simple arithmetic expressions
349 // are still recognizable by scalar evolution.
350 //
351 // The transforms applied to shl are very similar to the transforms applied
352 // to mul by constant. We can be more aggressive about optimizing right
353 // shifts.
354 //
355 // Combinations of right and left shifts will still be optimized in
356 // DAGCombine where scalar evolution no longer applies.
357
358 ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
359 uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
360 uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
361 assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
362 if (ShiftAmt1 == 0)
363 return nullptr; // Will be simplified in the future.
364 Value *X = ShiftOp->getOperand(0);
365
366 IntegerType *Ty = cast<IntegerType>(I.getType());
367
368 // Check for (X << c1) << c2 and (X >> c1) >> c2
369 if (I.getOpcode() == ShiftOp->getOpcode()) {
370 uint32_t AmtSum = ShiftAmt1 + ShiftAmt2; // Fold into one big shift.
371 // If this is an oversized composite shift, then unsigned shifts become
372 // zero (handled in InstSimplify) and ashr saturates.
373 if (AmtSum >= TypeBits) {
374 if (I.getOpcode() != Instruction::AShr)
375 return nullptr;
376 AmtSum = TypeBits - 1; // Saturate to 31 for i32 ashr.
377 }
378
379 return BinaryOperator::Create(I.getOpcode(), X,
380 ConstantInt::get(Ty, AmtSum));
381 }
382
383 if (ShiftAmt1 == ShiftAmt2) {
384 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
385 if (I.getOpcode() == Instruction::LShr &&
386 ShiftOp->getOpcode() == Instruction::Shl) {
387 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
388 return BinaryOperator::CreateAnd(
389 X, ConstantInt::get(I.getContext(), Mask));
390 }
391 } else if (ShiftAmt1 < ShiftAmt2) {
392 uint32_t ShiftDiff = ShiftAmt2 - ShiftAmt1;
393
394 // (X >>?,exact C1) << C2 --> X << (C2-C1)
395 // The inexact version is deferred to DAGCombine so we don't hide shl
396 // behind a bit mask.
397 if (I.getOpcode() == Instruction::Shl &&
398 ShiftOp->getOpcode() != Instruction::Shl && ShiftOp->isExact()) {
399 assert(ShiftOp->getOpcode() == Instruction::LShr ||
400 ShiftOp->getOpcode() == Instruction::AShr);
401 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
402 BinaryOperator *NewShl =
403 BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst);
404 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
405 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
406 return NewShl;
407 }
408
409 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
410 if (I.getOpcode() == Instruction::LShr &&
411 ShiftOp->getOpcode() == Instruction::Shl) {
412 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
413 // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
414 if (ShiftOp->hasNoUnsignedWrap()) {
415 BinaryOperator *NewLShr =
416 BinaryOperator::Create(Instruction::LShr, X, ShiftDiffCst);
417 NewLShr->setIsExact(I.isExact());
418 return NewLShr;
419 }
420 Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
421
422 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
423 return BinaryOperator::CreateAnd(
424 Shift, ConstantInt::get(I.getContext(), Mask));
425 }
426
427 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
428 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
429 if (I.getOpcode() == Instruction::AShr &&
430 ShiftOp->getOpcode() == Instruction::Shl) {
431 if (ShiftOp->hasNoSignedWrap()) {
432 // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
433 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
434 BinaryOperator *NewAShr =
435 BinaryOperator::Create(Instruction::AShr, X, ShiftDiffCst);
436 NewAShr->setIsExact(I.isExact());
437 return NewAShr;
438 }
439 }
440 } else {
441 assert(ShiftAmt2 < ShiftAmt1);
442 uint32_t ShiftDiff = ShiftAmt1 - ShiftAmt2;
443
444 // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
445 // The inexact version is deferred to DAGCombine so we don't hide shl
446 // behind a bit mask.
447 if (I.getOpcode() == Instruction::Shl &&
448 ShiftOp->getOpcode() != Instruction::Shl && ShiftOp->isExact()) {
449 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
450 BinaryOperator *NewShr =
451 BinaryOperator::Create(ShiftOp->getOpcode(), X, ShiftDiffCst);
452 NewShr->setIsExact(true);
453 return NewShr;
454 }
455
456 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
457 if (I.getOpcode() == Instruction::LShr &&
458 ShiftOp->getOpcode() == Instruction::Shl) {
459 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
460 if (ShiftOp->hasNoUnsignedWrap()) {
461 // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
462 BinaryOperator *NewShl =
463 BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst);
464 NewShl->setHasNoUnsignedWrap(true);
465 return NewShl;
466 }
467 Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
468
469 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
470 return BinaryOperator::CreateAnd(
471 Shift, ConstantInt::get(I.getContext(), Mask));
472 }
473
474 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
475 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
476 if (I.getOpcode() == Instruction::AShr &&
477 ShiftOp->getOpcode() == Instruction::Shl) {
478 if (ShiftOp->hasNoSignedWrap()) {
479 // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
480 ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
481 BinaryOperator *NewShl =
482 BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst);
483 NewShl->setHasNoSignedWrap(true);
484 return NewShl;
485 }
486 }
487 }
488 }
489
490 return nullptr;
491}
Chris Lattner18d7fc82010-08-27 22:24:38 +0000492
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000493Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattnerdc67e132010-01-05 07:44:46 +0000494 BinaryOperator &I) {
495 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Jakub Staszak538e3862012-12-09 15:37:46 +0000496
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000497 ConstantInt *COp1 = nullptr;
498 if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
499 COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
500 else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
501 COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
502 else
503 COp1 = dyn_cast<ConstantInt>(Op1);
504
505 if (!COp1)
506 return nullptr;
Jakub Staszak538e3862012-12-09 15:37:46 +0000507
Chris Lattner18d7fc82010-08-27 22:24:38 +0000508 // See if we can propagate this shift into the input, this covers the trivial
509 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
510 if (I.getOpcode() != Instruction::AShr &&
Hal Finkel60db0582014-09-07 18:57:58 +0000511 CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)) {
Chris Lattnerdd660102010-08-28 01:20:38 +0000512 DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
513 " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n");
Jakub Staszak538e3862012-12-09 15:37:46 +0000514
Sanjay Patel4b198802016-02-01 22:23:39 +0000515 return replaceInstUsesWith(
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000516 I, GetShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000517 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000518
Jakub Staszak538e3862012-12-09 15:37:46 +0000519 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattnerdc67e132010-01-05 07:44:46 +0000520 // purpose is to compute bits we don't care about.
521 uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
Jakub Staszak538e3862012-12-09 15:37:46 +0000522
Matt Arsenault6b4bed42014-04-23 16:48:40 +0000523 assert(!COp1->uge(TypeBits) &&
524 "Shift over the type width should have been removed already");
Jakub Staszak538e3862012-12-09 15:37:46 +0000525
Chris Lattnerdc67e132010-01-05 07:44:46 +0000526 // ((X*C1) << C2) == (X * (C1 << C2))
527 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
528 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
529 if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
530 return BinaryOperator::CreateMul(BO->getOperand(0),
Sanjay Patel690955f2016-01-31 16:34:11 +0000531 ConstantExpr::getShl(BOOp, Op1));
Jakub Staszak538e3862012-12-09 15:37:46 +0000532
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000533 if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I))
534 return FoldedShift;
Jakub Staszak538e3862012-12-09 15:37:46 +0000535
Chris Lattnerdc67e132010-01-05 07:44:46 +0000536 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
537 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
538 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
539 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
540 // place. Don't try to do this transformation in this case. Also, we
541 // require that the input operand is a shift-by-constant so that we have
542 // confidence that the shifts will get folded together. We could do this
543 // xform in more cases, but it is unlikely to be profitable.
Jakub Staszak538e3862012-12-09 15:37:46 +0000544 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
Chris Lattnerdc67e132010-01-05 07:44:46 +0000545 isa<ConstantInt>(TrOp->getOperand(1))) {
546 // Okay, we'll do this xform. Make the shift of shift.
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000547 Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000548 // (shift2 (shift1 & 0x00FF), c2)
549 Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
550
551 // For logical shifts, the truncation has the effect of making the high
552 // part of the register be zeros. Emulate this by inserting an AND to
553 // clear the top bits as needed. This 'and' will usually be zapped by
554 // other xforms later if dead.
555 unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
556 unsigned DstSize = TI->getType()->getScalarSizeInBits();
557 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
Jakub Staszak538e3862012-12-09 15:37:46 +0000558
Chris Lattnerdc67e132010-01-05 07:44:46 +0000559 // The mask we constructed says what the trunc would do if occurring
560 // between the shifts. We want to know the effect *after* the second
561 // shift. We know that it is a logical shift by a constant, so adjust the
562 // mask as appropriate.
563 if (I.getOpcode() == Instruction::Shl)
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000564 MaskV <<= COp1->getZExtValue();
Chris Lattnerdc67e132010-01-05 07:44:46 +0000565 else {
566 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000567 MaskV = MaskV.lshr(COp1->getZExtValue());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000568 }
569
570 // shift1 & 0x00FF
571 Value *And = Builder->CreateAnd(NSh,
572 ConstantInt::get(I.getContext(), MaskV),
573 TI->getName());
574
575 // Return the value truncated to the interesting size.
576 return new TruncInst(And, I.getType());
577 }
578 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000579
Chris Lattnerdc67e132010-01-05 07:44:46 +0000580 if (Op0->hasOneUse()) {
581 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
582 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
583 Value *V1, *V2;
584 ConstantInt *CC;
585 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000586 default: break;
587 case Instruction::Add:
588 case Instruction::And:
589 case Instruction::Or:
590 case Instruction::Xor: {
591 // These operators commute.
592 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
593 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
594 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
595 m_Specific(Op1)))) {
596 Value *YS = // (Y << C)
597 Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
598 // (X + (Y << C))
599 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
600 Op0BO->getOperand(1)->getName());
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000601 uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
602
603 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
604 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
605 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
606 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
607 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000608 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000609
Chris Lattner2b459fe2010-01-10 06:59:55 +0000610 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
611 Value *Op0BOOp1 = Op0BO->getOperand(1);
612 if (isLeftShift && Op0BOOp1->hasOneUse() &&
Jakub Staszak538e3862012-12-09 15:37:46 +0000613 match(Op0BOOp1,
Jakub Staszak84321852012-12-09 16:06:44 +0000614 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
615 m_ConstantInt(CC)))) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000616 Value *YS = // (Y << C)
617 Builder->CreateShl(Op0BO->getOperand(0), Op1,
618 Op0BO->getName());
619 // X & (CC << C)
620 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
621 V1->getName()+".mask");
622 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000623 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000624 LLVM_FALLTHROUGH;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000625 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000626
Chris Lattner2b459fe2010-01-10 06:59:55 +0000627 case Instruction::Sub: {
628 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
629 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
630 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
631 m_Specific(Op1)))) {
632 Value *YS = // (Y << C)
633 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
634 // (X + (Y << C))
635 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
636 Op0BO->getOperand(0)->getName());
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000637 uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
638
639 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
640 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
641 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
642 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
643 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattner2b459fe2010-01-10 06:59:55 +0000644 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000645
Chris Lattner2b459fe2010-01-10 06:59:55 +0000646 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
647 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
648 match(Op0BO->getOperand(0),
Jakub Staszak84321852012-12-09 16:06:44 +0000649 m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
650 m_ConstantInt(CC))) && V2 == Op1) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000651 Value *YS = // (Y << C)
652 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
653 // X & (CC << C)
654 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
655 V1->getName()+".mask");
Jakub Staszak538e3862012-12-09 15:37:46 +0000656
Chris Lattner2b459fe2010-01-10 06:59:55 +0000657 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
658 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000659
Chris Lattner2b459fe2010-01-10 06:59:55 +0000660 break;
661 }
662 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000663
664
Sanjay Patelfc3d8f02014-07-22 04:57:06 +0000665 // If the operand is a bitwise operator with a constant RHS, and the
Chris Lattnerdc67e132010-01-05 07:44:46 +0000666 // shift is the only use, we can pull it out of the shift.
667 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
668 bool isValid = true; // Valid only for And, Or, Xor
669 bool highBitSet = false; // Transform if high bit of constant set?
Jakub Staszak538e3862012-12-09 15:37:46 +0000670
Chris Lattnerdc67e132010-01-05 07:44:46 +0000671 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000672 default: isValid = false; break; // Do not perform transform!
673 case Instruction::Add:
674 isValid = isLeftShift;
675 break;
676 case Instruction::Or:
677 case Instruction::Xor:
678 highBitSet = false;
679 break;
680 case Instruction::And:
681 highBitSet = true;
682 break;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000683 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000684
Chris Lattnerdc67e132010-01-05 07:44:46 +0000685 // If this is a signed shift right, and the high bit is modified
686 // by the logical operation, do not perform the transformation.
687 // The highBitSet boolean indicates the value of the high bit of
688 // the constant which would cause it to be modified for this
689 // operation.
690 //
691 if (isValid && I.getOpcode() == Instruction::AShr)
692 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
Jakub Staszak538e3862012-12-09 15:37:46 +0000693
Chris Lattnerdc67e132010-01-05 07:44:46 +0000694 if (isValid) {
695 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000696
Chris Lattnerdc67e132010-01-05 07:44:46 +0000697 Value *NewShift =
698 Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
699 NewShift->takeName(Op0BO);
Jakub Staszak538e3862012-12-09 15:37:46 +0000700
Chris Lattnerdc67e132010-01-05 07:44:46 +0000701 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
702 NewRHS);
703 }
704 }
705 }
706 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000707
Sanjay Patel4c48bbe2016-12-10 22:16:29 +0000708 if (Instruction *Folded = foldShiftByConstOfShiftByConst(I, COp1, Builder))
709 return Folded;
Jakub Staszak538e3862012-12-09 15:37:46 +0000710
Craig Topperf40110f2014-04-25 05:29:35 +0000711 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000712}
713
714Instruction *InstCombiner::visitShl(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000715 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000716 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000717
Sanjay Patelcf082032017-01-13 18:08:25 +0000718 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
719 if (Value *V = SimplifyShlInst(Op0, Op1, I.hasNoSignedWrap(),
720 I.hasNoUnsignedWrap(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000721 return replaceInstUsesWith(I, V);
Jakub Staszak538e3862012-12-09 15:37:46 +0000722
Chris Lattner6b657ae2011-02-10 05:36:31 +0000723 if (Instruction *V = commonShiftTransforms(I))
724 return V;
Jakub Staszak538e3862012-12-09 15:37:46 +0000725
Sanjay Patelb22f6c52017-01-13 18:39:09 +0000726 const APInt *ShAmtAPInt;
727 if (match(Op1, m_APInt(ShAmtAPInt))) {
728 unsigned ShAmt = ShAmtAPInt->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000729
Sanjay Patelacd24c72017-01-13 18:52:10 +0000730 // shl (zext X), ShAmt --> zext (shl X, ShAmt)
731 // This is only valid if X would have zeros shifted out.
732 Value *X;
733 if (match(Op0, m_ZExt(m_Value(X)))) {
734 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
735 if (ShAmt < SrcWidth &&
736 MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
737 return new ZExtInst(Builder->CreateShl(X, ShAmt), I.getType());
David Majnemercb892e92017-01-04 02:21:34 +0000738 }
739
Chris Lattner6b657ae2011-02-10 05:36:31 +0000740 // If the shifted-out value is known-zero, then this is a NUW shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000741 if (!I.hasNoUnsignedWrap() &&
Sanjay Patelcf082032017-01-13 18:08:25 +0000742 MaskedValueIsZero(
Sanjay Patelb22f6c52017-01-13 18:39:09 +0000743 Op0, APInt::getHighBitsSet(ShAmtAPInt->getBitWidth(), ShAmt), 0,
744 &I)) {
Sanjay Patel690955f2016-01-31 16:34:11 +0000745 I.setHasNoUnsignedWrap();
746 return &I;
747 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000748
Sanjay Patelcf082032017-01-13 18:08:25 +0000749 // If the shifted-out value is all signbits, then this is a NSW shift.
750 if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000751 I.setHasNoSignedWrap();
752 return &I;
753 }
754 }
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000755
Benjamin Kramerf0e3f042011-04-29 08:41:23 +0000756 // (C1 << A) << C2 -> (C1 << C2) << A
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000757 Constant *C1, *C2;
758 Value *A;
Sanjay Patelcf082032017-01-13 18:08:25 +0000759 if (match(Op0, m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
760 match(Op1, m_Constant(C2)))
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000761 return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
762
Craig Topperf40110f2014-04-25 05:29:35 +0000763 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000764}
765
766Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000767 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000768 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000769
Sanjay Patelcf082032017-01-13 18:08:25 +0000770 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
771 if (Value *V = SimplifyLShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000772 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000773
Chris Lattner249da5c2010-01-23 18:49:30 +0000774 if (Instruction *R = commonShiftTransforms(I))
775 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000776
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000777 const APInt *ShAmtAPInt;
778 if (match(Op1, m_APInt(ShAmtAPInt))) {
779 unsigned ShAmt = ShAmtAPInt->getZExtValue();
780 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
781 auto *II = dyn_cast<IntrinsicInst>(Op0);
782 if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
783 (II->getIntrinsicID() == Intrinsic::ctlz ||
784 II->getIntrinsicID() == Intrinsic::cttz ||
785 II->getIntrinsicID() == Intrinsic::ctpop)) {
Chris Lattner249da5c2010-01-23 18:49:30 +0000786 // ctlz.i32(x)>>5 --> zext(x == 0)
787 // cttz.i32(x)>>5 --> zext(x == 0)
788 // ctpop.i32(x)>>5 --> zext(x == -1)
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000789 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
790 Constant *RHS = ConstantInt::getSigned(Op0->getType(), IsPop ? -1 : 0);
791 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
792 return new ZExtInst(Cmp, II->getType());
Chris Lattner249da5c2010-01-23 18:49:30 +0000793 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000794
Chris Lattner6b657ae2011-02-10 05:36:31 +0000795 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000796 if (!I.isExact() &&
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000797 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000798 I.setIsExact();
799 return &I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000800 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000801 }
Craig Topperf40110f2014-04-25 05:29:35 +0000802 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000803}
804
805Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000806 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000807 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000808
Sanjay Patelcf082032017-01-13 18:08:25 +0000809 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
810 if (Value *V = SimplifyAShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000811 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000812
Chris Lattnerdc67e132010-01-05 07:44:46 +0000813 if (Instruction *R = commonShiftTransforms(I))
814 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000815
Sanjay Patelca3124f2017-01-14 23:13:50 +0000816 unsigned BitWidth = I.getType()->getScalarSizeInBits();
Chris Lattnera1e223e2010-01-08 19:04:21 +0000817 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000818 unsigned ShAmt = Op1C->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000819
Sanjay Patelca3124f2017-01-14 23:13:50 +0000820 // If the shift amount equals the difference in width of the destination
821 // and source types:
822 // ashr (shl (zext X), C), C --> sext X
Chris Lattnera1e223e2010-01-08 19:04:21 +0000823 Value *X;
Sanjay Patelca3124f2017-01-14 23:13:50 +0000824 if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
825 ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
826 return new SExtInst(X, I.getType());
Chris Lattner6b657ae2011-02-10 05:36:31 +0000827
828 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000829 if (!I.isExact() &&
Sanjay Patelca3124f2017-01-14 23:13:50 +0000830 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000831 I.setIsExact();
832 return &I;
833 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000834 }
835
Chris Lattnerdc67e132010-01-05 07:44:46 +0000836 // See if we can turn a signed shr into an unsigned shr.
Sanjay Patelca3124f2017-01-14 23:13:50 +0000837 if (MaskedValueIsZero(Op0, APInt::getSignBit(BitWidth), 0, &I))
Chris Lattnera1e223e2010-01-08 19:04:21 +0000838 return BinaryOperator::CreateLShr(Op0, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000839
Craig Topperf40110f2014-04-25 05:29:35 +0000840 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000841}