blob: 9aa679c60e47b358d6a3f28ffecc636707f7e9a4 [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
Sanjay Patel65cce202017-01-16 20:05:26 +000068/// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
69static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
70 Instruction *InnerShift, InstCombiner &IC,
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000071 Instruction *CxtI) {
Sanjay Patel65cce202017-01-16 20:05:26 +000072 assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
Sanjay Patelb91bcd72016-04-11 17:35:57 +000073
Sanjay Patelab8b32d2017-01-16 19:35:45 +000074 // We need constant scalar or constant splat shifts.
Sanjay Patel65cce202017-01-16 20:05:26 +000075 const APInt *InnerShiftConst;
76 if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000077 return false;
78
Sanjay Patel65cce202017-01-16 20:05:26 +000079 // Two logical shifts in the same direction:
80 // shl (shl X, C1), C2 --> shl X, C1 + C2
81 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
82 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
83 if (IsInnerShl == IsOuterShl)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000084 return true;
85
Sanjay Patel65cce202017-01-16 20:05:26 +000086 // Equal shift amounts in opposite directions become bitwise 'and':
87 // lshr (shl X, C), C --> and X, C'
88 // shl (lshr X, C), C --> and X, C'
89 unsigned InnerShAmt = InnerShiftConst->getZExtValue();
90 if (InnerShAmt == OuterShAmt)
Sanjay Patel6eaff5c2016-04-11 15:43:41 +000091 return true;
92
Sanjay Patelbd8b7792016-04-11 16:11:07 +000093 // If the 2nd shift is bigger than the 1st, we can fold:
Sanjay Patel65cce202017-01-16 20:05:26 +000094 // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
95 // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
Sanjay Patelbd8b7792016-04-11 16:11:07 +000096 // but it isn't profitable unless we know the and'd out bits are already zero.
Sanjay Patel65cce202017-01-16 20:05:26 +000097 // Also, check that the inner shift is valid (less than the type width) or
98 // we'll crash trying to produce the bit mask for the 'and'.
99 unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
100 if (InnerShAmt > OuterShAmt && InnerShAmt < TypeWidth) {
101 unsigned MaskShift =
102 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
103 APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
104 if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
Sanjay Patel6eaff5c2016-04-11 15:43:41 +0000105 return true;
106 }
107
108 return false;
109}
110
Sanjay Patel20aaf582017-01-15 17:55:35 +0000111/// See if we can compute the specified value, but shifted logically to the left
112/// or right by some number of bits. This should return true if the expression
113/// can be computed for the same cost as the current expression tree. This is
114/// used to eliminate extraneous shifting from things like:
Chris Lattner18d7fc82010-08-27 22:24:38 +0000115/// %C = shl i128 %A, 64
116/// %D = shl i128 %B, 96
117/// %E = or i128 %C, %D
118/// %F = lshr i128 %E, 64
Sanjay Patel20aaf582017-01-15 17:55:35 +0000119/// where the client will ask if E can be computed shifted right by 64-bits. If
120/// this succeeds, getShiftedValue() will be called to produce the value.
121static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
Hal Finkel60db0582014-09-07 18:57:58 +0000122 InstCombiner &IC, Instruction *CxtI) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000123 // We can always evaluate constants shifted.
124 if (isa<Constant>(V))
125 return true;
Jakub Staszak538e3862012-12-09 15:37:46 +0000126
Chris Lattner18d7fc82010-08-27 22:24:38 +0000127 Instruction *I = dyn_cast<Instruction>(V);
128 if (!I) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000129
Chris Lattner18d7fc82010-08-27 22:24:38 +0000130 // If this is the opposite shift, we can directly reuse the input of the shift
131 // if the needed bits are already zero in the input. This allows us to reuse
132 // the value which means that we don't care if the shift has multiple uses.
133 // TODO: Handle opposite shift by exact value.
Craig Topperf40110f2014-04-25 05:29:35 +0000134 ConstantInt *CI = nullptr;
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000135 if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
136 (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000137 if (CI->getZExtValue() == NumBits) {
138 // TODO: Check that the input bits are already zero with MaskedValueIsZero
139#if 0
140 // If this is a truncate of a logical shr, we can truncate it to a smaller
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000141 // lshr iff we know that the bits we would otherwise be shifting in are
Chris Lattner18d7fc82010-08-27 22:24:38 +0000142 // already zeros.
143 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
144 uint32_t BitWidth = Ty->getScalarSizeInBits();
145 if (MaskedValueIsZero(I->getOperand(0),
146 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
147 CI->getLimitedValue(BitWidth) < BitWidth) {
148 return CanEvaluateTruncated(I->getOperand(0), Ty);
149 }
150#endif
Jakub Staszak538e3862012-12-09 15:37:46 +0000151
Chris Lattner18d7fc82010-08-27 22:24:38 +0000152 }
153 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000154
Chris Lattner18d7fc82010-08-27 22:24:38 +0000155 // We can't mutate something that has multiple uses: doing so would
156 // require duplicating the instruction in general, which isn't profitable.
157 if (!I->hasOneUse()) return false;
Jakub Staszak538e3862012-12-09 15:37:46 +0000158
Chris Lattner18d7fc82010-08-27 22:24:38 +0000159 switch (I->getOpcode()) {
160 default: return false;
161 case Instruction::And:
162 case Instruction::Or:
163 case Instruction::Xor:
164 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Sanjay Patel20aaf582017-01-15 17:55:35 +0000165 return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
166 canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
Jakub Staszak538e3862012-12-09 15:37:46 +0000167
Sanjay Patel6eaff5c2016-04-11 15:43:41 +0000168 case Instruction::Shl:
Sanjay Patel37129072016-04-11 17:11:55 +0000169 case Instruction::LShr:
Sanjay Patel4b9c6822016-04-11 17:25:23 +0000170 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000171
Chris Lattner18d7fc82010-08-27 22:24:38 +0000172 case Instruction::Select: {
173 SelectInst *SI = cast<SelectInst>(I);
Sanjay Patel690955f2016-01-31 16:34:11 +0000174 Value *TrueVal = SI->getTrueValue();
175 Value *FalseVal = SI->getFalseValue();
Sanjay Patel20aaf582017-01-15 17:55:35 +0000176 return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
177 canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
Chris Lattner18d7fc82010-08-27 22:24:38 +0000178 }
179 case Instruction::PHI: {
180 // We can change a phi if we can change all operands. Note that we never
181 // get into trouble with cyclic PHIs here because we only consider
182 // instructions with a single use.
183 PHINode *PN = cast<PHINode>(I);
Pete Cooper833f34d2015-05-12 20:05:31 +0000184 for (Value *IncValue : PN->incoming_values())
Sanjay Patel20aaf582017-01-15 17:55:35 +0000185 if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
Chris Lattner18d7fc82010-08-27 22:24:38 +0000186 return false;
187 return true;
188 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000189 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000190}
191
Sanjay Patel646734a2017-01-16 17:27:50 +0000192/// Fold OuterShift (InnerShift X, C1), C2.
193/// See canEvaluateShiftedShift() for the constraints on these instructions.
194static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
195 bool IsOuterShl,
196 InstCombiner::BuilderTy &Builder) {
197 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
198 Type *ShType = InnerShift->getType();
199 unsigned TypeWidth = ShType->getScalarSizeInBits();
200
201 // We only accept shifts-by-a-constant in canEvaluateShifted().
Sanjay Patelab8b32d2017-01-16 19:35:45 +0000202 const APInt *C1;
203 match(InnerShift->getOperand(1), m_APInt(C1));
Sanjay Patel646734a2017-01-16 17:27:50 +0000204 unsigned InnerShAmt = C1->getZExtValue();
205
206 // Change the shift amount and clear the appropriate IR flags.
207 auto NewInnerShift = [&](unsigned ShAmt) {
208 InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
209 if (IsInnerShl) {
210 InnerShift->setHasNoUnsignedWrap(false);
211 InnerShift->setHasNoSignedWrap(false);
212 } else {
213 InnerShift->setIsExact(false);
214 }
215 return InnerShift;
216 };
217
218 // Two logical shifts in the same direction:
219 // shl (shl X, C1), C2 --> shl X, C1 + C2
220 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
221 if (IsInnerShl == IsOuterShl) {
222 // If this is an oversized composite shift, then unsigned shifts get 0.
223 if (InnerShAmt + OuterShAmt >= TypeWidth)
224 return Constant::getNullValue(ShType);
225
226 return NewInnerShift(InnerShAmt + OuterShAmt);
227 }
228
229 // Equal shift amounts in opposite directions become bitwise 'and':
230 // lshr (shl X, C), C --> and X, C'
231 // shl (lshr X, C), C --> and X, C'
232 if (InnerShAmt == OuterShAmt) {
233 APInt Mask = IsInnerShl
234 ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
235 : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
236 Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
237 ConstantInt::get(ShType, Mask));
238 if (auto *AndI = dyn_cast<Instruction>(And)) {
239 AndI->moveBefore(InnerShift);
240 AndI->takeName(InnerShift);
241 }
242 return And;
243 }
244
245 assert(InnerShAmt > OuterShAmt &&
246 "Unexpected opposite direction logical shift pair");
247
248 // In general, we would need an 'and' for this transform, but
249 // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
250 // lshr (shl X, C1), C2 --> shl X, C1 - C2
251 // shl (lshr X, C1), C2 --> lshr X, C1 - C2
252 return NewInnerShift(InnerShAmt - OuterShAmt);
253}
254
Sanjay Patel20aaf582017-01-15 17:55:35 +0000255/// When canEvaluateShifted() returns true for an expression, this function
256/// inserts the new computation that produces the shifted value.
257static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000258 InstCombiner &IC, const DataLayout &DL) {
Chris Lattner18d7fc82010-08-27 22:24:38 +0000259 // We can always evaluate constants shifted.
260 if (Constant *C = dyn_cast<Constant>(V)) {
261 if (isLeftShift)
262 V = IC.Builder->CreateShl(C, NumBits);
263 else
264 V = IC.Builder->CreateLShr(C, NumBits);
265 // If we got a constantexpr back, try to simplify it with TD info.
David Majnemerd536f232016-07-29 03:27:26 +0000266 if (auto *C = dyn_cast<Constant>(V))
267 if (auto *FoldedC =
Justin Bogner99798402016-08-05 01:06:44 +0000268 ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo()))
David Majnemerd536f232016-07-29 03:27:26 +0000269 V = FoldedC;
Chris Lattner18d7fc82010-08-27 22:24:38 +0000270 return V;
271 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000272
Chris Lattner18d7fc82010-08-27 22:24:38 +0000273 Instruction *I = cast<Instruction>(V);
274 IC.Worklist.Add(I);
275
276 switch (I->getOpcode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000277 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
Chris Lattner18d7fc82010-08-27 22:24:38 +0000278 case Instruction::And:
279 case Instruction::Or:
280 case Instruction::Xor:
281 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000282 I->setOperand(
Sanjay Patel20aaf582017-01-15 17:55:35 +0000283 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000284 I->setOperand(
Sanjay Patel20aaf582017-01-15 17:55:35 +0000285 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000286 return I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000287
Sanjay Patel646734a2017-01-16 17:27:50 +0000288 case Instruction::Shl:
289 case Instruction::LShr:
290 return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
291 *(IC.Builder));
Jakub Staszak538e3862012-12-09 15:37:46 +0000292
Chris Lattner18d7fc82010-08-27 22:24:38 +0000293 case Instruction::Select:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000294 I->setOperand(
Sanjay Patel20aaf582017-01-15 17:55:35 +0000295 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000296 I->setOperand(
Sanjay Patel20aaf582017-01-15 17:55:35 +0000297 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000298 return I;
299 case Instruction::PHI: {
300 // We can change a phi if we can change all operands. Note that we never
301 // get into trouble with cyclic PHIs here because we only consider
302 // instructions with a single use.
303 PHINode *PN = cast<PHINode>(I);
304 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
Sanjay Patel20aaf582017-01-15 17:55:35 +0000305 PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000306 isLeftShift, IC, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000307 return PN;
308 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000309 }
Chris Lattner18d7fc82010-08-27 22:24:38 +0000310}
311
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000312Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
Chris Lattnerdc67e132010-01-05 07:44:46 +0000313 BinaryOperator &I) {
314 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Jakub Staszak538e3862012-12-09 15:37:46 +0000315
Sanjay Patelda5682a2017-01-16 21:24:41 +0000316 const APInt *Op1C;
317 if (!match(Op1, m_APInt(Op1C)))
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000318 return nullptr;
Jakub Staszak538e3862012-12-09 15:37:46 +0000319
Chris Lattner18d7fc82010-08-27 22:24:38 +0000320 // See if we can propagate this shift into the input, this covers the trivial
321 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
322 if (I.getOpcode() != Instruction::AShr &&
Sanjay Patelda5682a2017-01-16 21:24:41 +0000323 canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
Chris Lattnerdd660102010-08-28 01:20:38 +0000324 DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
325 " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n");
Jakub Staszak538e3862012-12-09 15:37:46 +0000326
Sanjay Patel4b198802016-02-01 22:23:39 +0000327 return replaceInstUsesWith(
Sanjay Patelda5682a2017-01-16 21:24:41 +0000328 I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
Chris Lattner18d7fc82010-08-27 22:24:38 +0000329 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000330
Jakub Staszak538e3862012-12-09 15:37:46 +0000331 // See if we can simplify any instructions used by the instruction whose sole
Chris Lattnerdc67e132010-01-05 07:44:46 +0000332 // purpose is to compute bits we don't care about.
Sanjay Patelda5682a2017-01-16 21:24:41 +0000333 unsigned TypeBits = Op0->getType()->getScalarSizeInBits();
Jakub Staszak538e3862012-12-09 15:37:46 +0000334
Sanjay Patelda5682a2017-01-16 21:24:41 +0000335 assert(!Op1C->uge(TypeBits) &&
Matt Arsenault6b4bed42014-04-23 16:48:40 +0000336 "Shift over the type width should have been removed already");
Jakub Staszak538e3862012-12-09 15:37:46 +0000337
Sanjay Pateldb0938f2017-01-10 23:49:07 +0000338 if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I))
339 return FoldedShift;
Jakub Staszak538e3862012-12-09 15:37:46 +0000340
Chris Lattnerdc67e132010-01-05 07:44:46 +0000341 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
342 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
343 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
344 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
345 // place. Don't try to do this transformation in this case. Also, we
346 // require that the input operand is a shift-by-constant so that we have
347 // confidence that the shifts will get folded together. We could do this
348 // xform in more cases, but it is unlikely to be profitable.
Jakub Staszak538e3862012-12-09 15:37:46 +0000349 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
Chris Lattnerdc67e132010-01-05 07:44:46 +0000350 isa<ConstantInt>(TrOp->getOperand(1))) {
351 // Okay, we'll do this xform. Make the shift of shift.
Sanjay Patelda5682a2017-01-16 21:24:41 +0000352 Constant *ShAmt =
353 ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000354 // (shift2 (shift1 & 0x00FF), c2)
355 Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
356
357 // For logical shifts, the truncation has the effect of making the high
358 // part of the register be zeros. Emulate this by inserting an AND to
359 // clear the top bits as needed. This 'and' will usually be zapped by
360 // other xforms later if dead.
361 unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
362 unsigned DstSize = TI->getType()->getScalarSizeInBits();
363 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
Jakub Staszak538e3862012-12-09 15:37:46 +0000364
Chris Lattnerdc67e132010-01-05 07:44:46 +0000365 // The mask we constructed says what the trunc would do if occurring
366 // between the shifts. We want to know the effect *after* the second
367 // shift. We know that it is a logical shift by a constant, so adjust the
368 // mask as appropriate.
369 if (I.getOpcode() == Instruction::Shl)
Sanjay Patelda5682a2017-01-16 21:24:41 +0000370 MaskV <<= Op1C->getZExtValue();
Chris Lattnerdc67e132010-01-05 07:44:46 +0000371 else {
372 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
Sanjay Patelda5682a2017-01-16 21:24:41 +0000373 MaskV = MaskV.lshr(Op1C->getZExtValue());
Chris Lattnerdc67e132010-01-05 07:44:46 +0000374 }
375
376 // shift1 & 0x00FF
377 Value *And = Builder->CreateAnd(NSh,
378 ConstantInt::get(I.getContext(), MaskV),
379 TI->getName());
380
381 // Return the value truncated to the interesting size.
382 return new TruncInst(And, I.getType());
383 }
384 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000385
Chris Lattnerdc67e132010-01-05 07:44:46 +0000386 if (Op0->hasOneUse()) {
387 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
388 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
389 Value *V1, *V2;
390 ConstantInt *CC;
391 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000392 default: break;
393 case Instruction::Add:
394 case Instruction::And:
395 case Instruction::Or:
396 case Instruction::Xor: {
397 // These operators commute.
398 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
399 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
400 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
401 m_Specific(Op1)))) {
402 Value *YS = // (Y << C)
403 Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
404 // (X + (Y << C))
405 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
406 Op0BO->getOperand(1)->getName());
Sanjay Patelda5682a2017-01-16 21:24:41 +0000407 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000408
409 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
410 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
411 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
412 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
413 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000414 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000415
Chris Lattner2b459fe2010-01-10 06:59:55 +0000416 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
417 Value *Op0BOOp1 = Op0BO->getOperand(1);
418 if (isLeftShift && Op0BOOp1->hasOneUse() &&
Jakub Staszak538e3862012-12-09 15:37:46 +0000419 match(Op0BOOp1,
Jakub Staszak84321852012-12-09 16:06:44 +0000420 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
421 m_ConstantInt(CC)))) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000422 Value *YS = // (Y << C)
423 Builder->CreateShl(Op0BO->getOperand(0), Op1,
424 Op0BO->getName());
425 // X & (CC << C)
426 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
427 V1->getName()+".mask");
428 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
Chris Lattnerdc67e132010-01-05 07:44:46 +0000429 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000430 LLVM_FALLTHROUGH;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000431 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000432
Chris Lattner2b459fe2010-01-10 06:59:55 +0000433 case Instruction::Sub: {
434 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
435 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
436 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
437 m_Specific(Op1)))) {
438 Value *YS = // (Y << C)
439 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
440 // (X + (Y << C))
441 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
442 Op0BO->getOperand(0)->getName());
Sanjay Patelda5682a2017-01-16 21:24:41 +0000443 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
Matt Arsenaultfed3dc82014-04-14 21:50:37 +0000444
445 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
446 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
447 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
448 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
449 return BinaryOperator::CreateAnd(X, Mask);
Chris Lattner2b459fe2010-01-10 06:59:55 +0000450 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000451
Chris Lattner2b459fe2010-01-10 06:59:55 +0000452 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
453 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
454 match(Op0BO->getOperand(0),
Jakub Staszak84321852012-12-09 16:06:44 +0000455 m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
456 m_ConstantInt(CC))) && V2 == Op1) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000457 Value *YS = // (Y << C)
458 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
459 // X & (CC << C)
460 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
461 V1->getName()+".mask");
Jakub Staszak538e3862012-12-09 15:37:46 +0000462
Chris Lattner2b459fe2010-01-10 06:59:55 +0000463 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
464 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000465
Chris Lattner2b459fe2010-01-10 06:59:55 +0000466 break;
467 }
468 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000469
470
Sanjay Patelfc3d8f02014-07-22 04:57:06 +0000471 // If the operand is a bitwise operator with a constant RHS, and the
Chris Lattnerdc67e132010-01-05 07:44:46 +0000472 // shift is the only use, we can pull it out of the shift.
473 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
474 bool isValid = true; // Valid only for And, Or, Xor
475 bool highBitSet = false; // Transform if high bit of constant set?
Jakub Staszak538e3862012-12-09 15:37:46 +0000476
Chris Lattnerdc67e132010-01-05 07:44:46 +0000477 switch (Op0BO->getOpcode()) {
Chris Lattner2b459fe2010-01-10 06:59:55 +0000478 default: isValid = false; break; // Do not perform transform!
479 case Instruction::Add:
480 isValid = isLeftShift;
481 break;
482 case Instruction::Or:
483 case Instruction::Xor:
484 highBitSet = false;
485 break;
486 case Instruction::And:
487 highBitSet = true;
488 break;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000489 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000490
Chris Lattnerdc67e132010-01-05 07:44:46 +0000491 // If this is a signed shift right, and the high bit is modified
492 // by the logical operation, do not perform the transformation.
493 // The highBitSet boolean indicates the value of the high bit of
494 // the constant which would cause it to be modified for this
495 // operation.
496 //
497 if (isValid && I.getOpcode() == Instruction::AShr)
498 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
Jakub Staszak538e3862012-12-09 15:37:46 +0000499
Chris Lattnerdc67e132010-01-05 07:44:46 +0000500 if (isValid) {
501 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000502
Chris Lattnerdc67e132010-01-05 07:44:46 +0000503 Value *NewShift =
504 Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
505 NewShift->takeName(Op0BO);
Jakub Staszak538e3862012-12-09 15:37:46 +0000506
Chris Lattnerdc67e132010-01-05 07:44:46 +0000507 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
508 NewRHS);
509 }
510 }
511 }
512 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000513
Craig Topperf40110f2014-04-25 05:29:35 +0000514 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000515}
516
517Instruction *InstCombiner::visitShl(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000518 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000519 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000520
Sanjay Patelcf082032017-01-13 18:08:25 +0000521 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
522 if (Value *V = SimplifyShlInst(Op0, Op1, I.hasNoSignedWrap(),
523 I.hasNoUnsignedWrap(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000524 return replaceInstUsesWith(I, V);
Jakub Staszak538e3862012-12-09 15:37:46 +0000525
Chris Lattner6b657ae2011-02-10 05:36:31 +0000526 if (Instruction *V = commonShiftTransforms(I))
527 return V;
Jakub Staszak538e3862012-12-09 15:37:46 +0000528
Sanjay Patelb22f6c52017-01-13 18:39:09 +0000529 const APInt *ShAmtAPInt;
530 if (match(Op1, m_APInt(ShAmtAPInt))) {
531 unsigned ShAmt = ShAmtAPInt->getZExtValue();
Sanjay Patel50753f02017-01-26 22:08:10 +0000532 unsigned BitWidth = I.getType()->getScalarSizeInBits();
Sanjay Patel062adaa2017-01-29 17:11:18 +0000533 Type *Ty = I.getType();
Jakub Staszak538e3862012-12-09 15:37:46 +0000534
Sanjay Patelacd24c72017-01-13 18:52:10 +0000535 // shl (zext X), ShAmt --> zext (shl X, ShAmt)
536 // This is only valid if X would have zeros shifted out.
537 Value *X;
538 if (match(Op0, m_ZExt(m_Value(X)))) {
539 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
540 if (ShAmt < SrcWidth &&
541 MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
Sanjay Patel062adaa2017-01-29 17:11:18 +0000542 return new ZExtInst(Builder->CreateShl(X, ShAmt), Ty);
David Majnemercb892e92017-01-04 02:21:34 +0000543 }
544
Sanjay Patel50753f02017-01-26 22:08:10 +0000545 // (X >>u C) << C --> X & (-1 << C)
546 if (match(Op0, m_LShr(m_Value(X), m_Specific(Op1)))) {
547 APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
Sanjay Patel062adaa2017-01-29 17:11:18 +0000548 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
549 }
550
Sanjay Patel8c5f2362017-01-30 23:35:52 +0000551 // Be careful about hiding shl instructions behind bit masks. They are used
552 // to represent multiplies by a constant, and it is important that simple
553 // arithmetic expressions are still recognizable by scalar evolution.
Sanjay Patel373db5b2017-01-30 18:40:23 +0000554 // The inexact versions are deferred to DAGCombine, so we don't hide shl
555 // behind a bit mask.
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000556 const APInt *ShOp1;
557 if (match(Op0, m_CombineOr(m_Exact(m_LShr(m_Value(X), m_APInt(ShOp1))),
558 m_Exact(m_AShr(m_Value(X), m_APInt(ShOp1)))))) {
559 unsigned ShrAmt = ShOp1->getZExtValue();
Sanjay Patel373db5b2017-01-30 18:40:23 +0000560 if (ShrAmt < ShAmt) {
561 // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
562 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
563 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
564 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
565 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
566 return NewShl;
567 }
568 if (ShrAmt > ShAmt) {
569 // If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
570 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
571 auto *NewShr = BinaryOperator::Create(
572 cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
573 NewShr->setIsExact(true);
574 return NewShr;
575 }
Sanjay Patel50753f02017-01-26 22:08:10 +0000576 }
577
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000578 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
579 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
580 // Oversized shifts are simplified to zero in InstSimplify.
581 if (AmtSum < BitWidth)
582 // (X << C1) << C2 --> X << (C1 + C2)
583 return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
584 }
585
Chris Lattner6b657ae2011-02-10 05:36:31 +0000586 // If the shifted-out value is known-zero, then this is a NUW shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000587 if (!I.hasNoUnsignedWrap() &&
Sanjay Patel50753f02017-01-26 22:08:10 +0000588 MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
Sanjay Patel690955f2016-01-31 16:34:11 +0000589 I.setHasNoUnsignedWrap();
590 return &I;
591 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000592
Sanjay Patelcf082032017-01-13 18:08:25 +0000593 // If the shifted-out value is all signbits, then this is a NSW shift.
594 if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000595 I.setHasNoSignedWrap();
596 return &I;
597 }
598 }
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000599
Sanjay Patelf38bab72017-02-09 23:13:04 +0000600 Constant *C1;
601 if (match(Op1, m_Constant(C1))) {
602 Constant *C2;
603 Value *X;
604 // (C2 << X) << C1 --> (C2 << C1) << X
605 if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
606 return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
607
608 // (X * C2) << C1 --> X * (C2 << C1)
609 if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
610 return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
611 }
Benjamin Kramer16f18ed2011-04-29 08:15:41 +0000612
Craig Topperf40110f2014-04-25 05:29:35 +0000613 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000614}
615
616Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000617 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000618 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000619
Sanjay Patelcf082032017-01-13 18:08:25 +0000620 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
621 if (Value *V = SimplifyLShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000622 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000623
Chris Lattner249da5c2010-01-23 18:49:30 +0000624 if (Instruction *R = commonShiftTransforms(I))
625 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000626
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000627 Type *Ty = I.getType();
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000628 const APInt *ShAmtAPInt;
629 if (match(Op1, m_APInt(ShAmtAPInt))) {
630 unsigned ShAmt = ShAmtAPInt->getZExtValue();
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000631 unsigned BitWidth = Ty->getScalarSizeInBits();
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000632 auto *II = dyn_cast<IntrinsicInst>(Op0);
633 if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
634 (II->getIntrinsicID() == Intrinsic::ctlz ||
635 II->getIntrinsicID() == Intrinsic::cttz ||
636 II->getIntrinsicID() == Intrinsic::ctpop)) {
Chris Lattner249da5c2010-01-23 18:49:30 +0000637 // ctlz.i32(x)>>5 --> zext(x == 0)
638 // cttz.i32(x)>>5 --> zext(x == 0)
639 // ctpop.i32(x)>>5 --> zext(x == -1)
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000640 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000641 Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000642 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000643 return new ZExtInst(Cmp, Ty);
Chris Lattner249da5c2010-01-23 18:49:30 +0000644 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000645
Sanjay Patel50753f02017-01-26 22:08:10 +0000646 Value *X;
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000647 const APInt *ShOp1;
648 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
649 unsigned ShlAmt = ShOp1->getZExtValue();
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000650 if (ShlAmt < ShAmt) {
651 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
652 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
653 // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
Sanjay Patel062c14a2017-01-30 17:38:55 +0000654 auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000655 NewLShr->setIsExact(I.isExact());
656 return NewLShr;
657 }
658 // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
Sanjay Patel8e644c02017-01-30 16:53:03 +0000659 Value *NewLShr = Builder->CreateLShr(X, ShiftDiff, "", I.isExact());
Sanjay Patel1196d7c2017-01-30 16:11:40 +0000660 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
661 return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
662 }
Sanjay Patel0c39d562017-01-30 23:01:05 +0000663 if (ShlAmt > ShAmt) {
664 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
665 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
666 // (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
667 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
668 NewShl->setHasNoUnsignedWrap(true);
669 return NewShl;
670 }
671 // (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
672 Value *NewShl = Builder->CreateShl(X, ShiftDiff);
673 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
674 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
675 }
676 assert(ShlAmt == ShAmt);
677 // (X << C) >>u C --> X & (-1 >>u C)
678 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
679 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
Sanjay Patel50753f02017-01-26 22:08:10 +0000680 }
681
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000682 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
683 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
684 // Oversized shifts are simplified to zero in InstSimplify.
685 if (AmtSum < BitWidth)
686 // (X >>u C1) >>u C2 --> X >>u (C1 + C2)
687 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
688 }
689
Chris Lattner6b657ae2011-02-10 05:36:31 +0000690 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000691 if (!I.isExact() &&
Sanjay Patel2d4b4562017-01-13 23:04:10 +0000692 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000693 I.setIsExact();
694 return &I;
Jakub Staszak538e3862012-12-09 15:37:46 +0000695 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000696 }
Craig Topperf40110f2014-04-25 05:29:35 +0000697 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000698}
699
700Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000701 if (Value *V = SimplifyVectorOp(I))
Sanjay Patel4b198802016-02-01 22:23:39 +0000702 return replaceInstUsesWith(I, V);
Serge Pavlov9ef66a82014-05-11 08:46:12 +0000703
Sanjay Patelcf082032017-01-13 18:08:25 +0000704 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
705 if (Value *V = SimplifyAShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC))
Sanjay Patel4b198802016-02-01 22:23:39 +0000706 return replaceInstUsesWith(I, V);
Duncan Sands7f60dc12011-01-14 00:37:45 +0000707
Chris Lattnerdc67e132010-01-05 07:44:46 +0000708 if (Instruction *R = commonShiftTransforms(I))
709 return R;
Jakub Staszak538e3862012-12-09 15:37:46 +0000710
Sanjay Patel77732d52017-01-30 17:19:32 +0000711 Type *Ty = I.getType();
712 unsigned BitWidth = Ty->getScalarSizeInBits();
Sanjay Patel5f8451a2017-01-15 16:38:19 +0000713 const APInt *ShAmtAPInt;
714 if (match(Op1, m_APInt(ShAmtAPInt))) {
715 unsigned ShAmt = ShAmtAPInt->getZExtValue();
Jakub Staszak538e3862012-12-09 15:37:46 +0000716
Sanjay Patelca3124f2017-01-14 23:13:50 +0000717 // If the shift amount equals the difference in width of the destination
Sanjay Patel5f8451a2017-01-15 16:38:19 +0000718 // and source scalar types:
Sanjay Patelca3124f2017-01-14 23:13:50 +0000719 // ashr (shl (zext X), C), C --> sext X
Chris Lattnera1e223e2010-01-08 19:04:21 +0000720 Value *X;
Sanjay Patelca3124f2017-01-14 23:13:50 +0000721 if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
722 ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
Sanjay Patel77732d52017-01-30 17:19:32 +0000723 return new SExtInst(X, Ty);
724
725 // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
726 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000727 const APInt *ShOp1;
728 if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1)))) {
729 unsigned ShlAmt = ShOp1->getZExtValue();
Sanjay Patel8c5f2362017-01-30 23:35:52 +0000730 if (ShlAmt < ShAmt) {
731 // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
732 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
733 auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
734 NewAShr->setIsExact(I.isExact());
735 return NewAShr;
736 }
737 if (ShlAmt > ShAmt) {
738 // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
739 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
740 auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
741 NewShl->setHasNoSignedWrap(true);
742 return NewShl;
743 }
Sanjay Patel77732d52017-01-30 17:19:32 +0000744 }
Chris Lattner6b657ae2011-02-10 05:36:31 +0000745
Sanjay Patelc56d1cc2017-02-01 21:31:34 +0000746 if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1)))) {
747 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
748 // Oversized arithmetic shifts replicate the sign bit.
749 AmtSum = std::min(AmtSum, BitWidth - 1);
750 // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
751 return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
752 }
753
Chris Lattner6b657ae2011-02-10 05:36:31 +0000754 // If the shifted-out value is known-zero, then this is an exact shift.
Jakub Staszak538e3862012-12-09 15:37:46 +0000755 if (!I.isExact() &&
Sanjay Patelca3124f2017-01-14 23:13:50 +0000756 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
Chris Lattner6b657ae2011-02-10 05:36:31 +0000757 I.setIsExact();
758 return &I;
759 }
Jakub Staszak538e3862012-12-09 15:37:46 +0000760 }
761
Chris Lattnerdc67e132010-01-05 07:44:46 +0000762 // See if we can turn a signed shr into an unsigned shr.
Sanjay Patelca3124f2017-01-14 23:13:50 +0000763 if (MaskedValueIsZero(Op0, APInt::getSignBit(BitWidth), 0, &I))
Chris Lattnera1e223e2010-01-08 19:04:21 +0000764 return BinaryOperator::CreateLShr(Op0, Op1);
Jakub Staszak538e3862012-12-09 15:37:46 +0000765
Craig Topperf40110f2014-04-25 05:29:35 +0000766 return nullptr;
Chris Lattnerdc67e132010-01-05 07:44:46 +0000767}