1 //===- InstCombineShifts.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the visitShl, visitLShr, and visitAShr functions.
12 //===----------------------------------------------------------------------===//
14 #include "InstCombineInternal.h"
15 #include "llvm/Analysis/ConstantFolding.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PatternMatch.h"
20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
24 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
25 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
26 assert(Op0->getType() == Op1->getType());
28 // See if we can fold away this shift.
29 if (SimplifyDemandedInstructionBits(I))
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))
38 if (Constant *CUI = dyn_cast<Constant>(Op1))
39 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
42 // (C1 shift (A add C2)) -> (C1 shift C2) shift A)
43 // iff A and C2 are both positive.
46 if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
47 if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
48 isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
49 return BinaryOperator::Create(
50 I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
52 // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
53 // Because shifts by negative values (which could occur if A were negative)
56 if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
57 // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
58 // demand the sign bit (and many others) here??
59 Value *Rem = Builder.CreateAnd(A, ConstantInt::get(I.getType(), *B - 1),
68 /// Return true if we can simplify two logical (either left or right) shifts
69 /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
70 static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
71 Instruction *InnerShift, InstCombiner &IC,
73 assert(InnerShift->isLogicalShift() && "Unexpected instruction type");
75 // We need constant scalar or constant splat shifts.
76 const APInt *InnerShiftConst;
77 if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
80 // Two logical shifts in the same direction:
81 // shl (shl X, C1), C2 --> shl X, C1 + C2
82 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
83 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
84 if (IsInnerShl == IsOuterShl)
87 // Equal shift amounts in opposite directions become bitwise 'and':
88 // lshr (shl X, C), C --> and X, C'
89 // shl (lshr X, C), C --> and X, C'
90 if (*InnerShiftConst == OuterShAmt)
93 // If the 2nd shift is bigger than the 1st, we can fold:
94 // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
95 // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
96 // but it isn't profitable unless we know the and'd out bits are already zero.
97 // 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 (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
101 unsigned InnerShAmt = InnerShiftConst->getZExtValue();
103 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
104 APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
105 if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
112 /// See if we can compute the specified value, but shifted logically to the left
113 /// or right by some number of bits. This should return true if the expression
114 /// can be computed for the same cost as the current expression tree. This is
115 /// used to eliminate extraneous shifting from things like:
116 /// %C = shl i128 %A, 64
117 /// %D = shl i128 %B, 96
118 /// %E = or i128 %C, %D
119 /// %F = lshr i128 %E, 64
120 /// where the client will ask if E can be computed shifted right by 64-bits. If
121 /// this succeeds, getShiftedValue() will be called to produce the value.
122 static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
123 InstCombiner &IC, Instruction *CxtI) {
124 // We can always evaluate constants shifted.
125 if (isa<Constant>(V))
128 Instruction *I = dyn_cast<Instruction>(V);
129 if (!I) return false;
131 // If this is the opposite shift, we can directly reuse the input of the shift
132 // if the needed bits are already zero in the input. This allows us to reuse
133 // the value which means that we don't care if the shift has multiple uses.
134 // TODO: Handle opposite shift by exact value.
135 ConstantInt *CI = nullptr;
136 if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
137 (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
138 if (CI->getValue() == NumBits) {
139 // TODO: Check that the input bits are already zero with MaskedValueIsZero
141 // If this is a truncate of a logical shr, we can truncate it to a smaller
142 // lshr iff we know that the bits we would otherwise be shifting in are
144 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
145 uint32_t BitWidth = Ty->getScalarSizeInBits();
146 if (MaskedValueIsZero(I->getOperand(0),
147 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
148 CI->getLimitedValue(BitWidth) < BitWidth) {
149 return CanEvaluateTruncated(I->getOperand(0), Ty);
156 // We can't mutate something that has multiple uses: doing so would
157 // require duplicating the instruction in general, which isn't profitable.
158 if (!I->hasOneUse()) return false;
160 switch (I->getOpcode()) {
161 default: return false;
162 case Instruction::And:
163 case Instruction::Or:
164 case Instruction::Xor:
165 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
166 return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
167 canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
169 case Instruction::Shl:
170 case Instruction::LShr:
171 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
173 case Instruction::Select: {
174 SelectInst *SI = cast<SelectInst>(I);
175 Value *TrueVal = SI->getTrueValue();
176 Value *FalseVal = SI->getFalseValue();
177 return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
178 canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
180 case Instruction::PHI: {
181 // We can change a phi if we can change all operands. Note that we never
182 // get into trouble with cyclic PHIs here because we only consider
183 // instructions with a single use.
184 PHINode *PN = cast<PHINode>(I);
185 for (Value *IncValue : PN->incoming_values())
186 if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
193 /// Fold OuterShift (InnerShift X, C1), C2.
194 /// See canEvaluateShiftedShift() for the constraints on these instructions.
195 static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
197 InstCombiner::BuilderTy &Builder) {
198 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
199 Type *ShType = InnerShift->getType();
200 unsigned TypeWidth = ShType->getScalarSizeInBits();
202 // We only accept shifts-by-a-constant in canEvaluateShifted().
204 match(InnerShift->getOperand(1), m_APInt(C1));
205 unsigned InnerShAmt = C1->getZExtValue();
207 // Change the shift amount and clear the appropriate IR flags.
208 auto NewInnerShift = [&](unsigned ShAmt) {
209 InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
211 InnerShift->setHasNoUnsignedWrap(false);
212 InnerShift->setHasNoSignedWrap(false);
214 InnerShift->setIsExact(false);
219 // Two logical shifts in the same direction:
220 // shl (shl X, C1), C2 --> shl X, C1 + C2
221 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
222 if (IsInnerShl == IsOuterShl) {
223 // If this is an oversized composite shift, then unsigned shifts get 0.
224 if (InnerShAmt + OuterShAmt >= TypeWidth)
225 return Constant::getNullValue(ShType);
227 return NewInnerShift(InnerShAmt + OuterShAmt);
230 // Equal shift amounts in opposite directions become bitwise 'and':
231 // lshr (shl X, C), C --> and X, C'
232 // shl (lshr X, C), C --> and X, C'
233 if (InnerShAmt == OuterShAmt) {
234 APInt Mask = IsInnerShl
235 ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
236 : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
237 Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
238 ConstantInt::get(ShType, Mask));
239 if (auto *AndI = dyn_cast<Instruction>(And)) {
240 AndI->moveBefore(InnerShift);
241 AndI->takeName(InnerShift);
246 assert(InnerShAmt > OuterShAmt &&
247 "Unexpected opposite direction logical shift pair");
249 // In general, we would need an 'and' for this transform, but
250 // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
251 // lshr (shl X, C1), C2 --> shl X, C1 - C2
252 // shl (lshr X, C1), C2 --> lshr X, C1 - C2
253 return NewInnerShift(InnerShAmt - OuterShAmt);
256 /// When canEvaluateShifted() returns true for an expression, this function
257 /// inserts the new computation that produces the shifted value.
258 static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
259 InstCombiner &IC, const DataLayout &DL) {
260 // We can always evaluate constants shifted.
261 if (Constant *C = dyn_cast<Constant>(V)) {
263 V = IC.Builder.CreateShl(C, NumBits);
265 V = IC.Builder.CreateLShr(C, NumBits);
266 // If we got a constantexpr back, try to simplify it with TD info.
267 if (auto *C = dyn_cast<Constant>(V))
269 ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo()))
274 Instruction *I = cast<Instruction>(V);
277 switch (I->getOpcode()) {
278 default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
279 case Instruction::And:
280 case Instruction::Or:
281 case Instruction::Xor:
282 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
284 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
286 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
289 case Instruction::Shl:
290 case Instruction::LShr:
291 return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
294 case Instruction::Select:
296 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
298 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
300 case Instruction::PHI: {
301 // We can change a phi if we can change all operands. Note that we never
302 // get into trouble with cyclic PHIs here because we only consider
303 // instructions with a single use.
304 PHINode *PN = cast<PHINode>(I);
305 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
306 PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
307 isLeftShift, IC, DL));
313 // If this is a bitwise operator or add with a constant RHS we might be able
314 // to pull it through a shift.
315 static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift,
318 bool IsValid = true; // Valid only for And, Or Xor,
319 bool HighBitSet = false; // Transform ifhigh bit of constant set?
321 switch (BO->getOpcode()) {
322 default: IsValid = false; break; // Do not perform transform!
323 case Instruction::Add:
324 IsValid = Shift.getOpcode() == Instruction::Shl;
326 case Instruction::Or:
327 case Instruction::Xor:
330 case Instruction::And:
335 // If this is a signed shift right, and the high bit is modified
336 // by the logical operation, do not perform the transformation.
337 // The HighBitSet boolean indicates the value of the high bit of
338 // the constant which would cause it to be modified for this
341 if (IsValid && Shift.getOpcode() == Instruction::AShr)
342 IsValid = C.isNegative() == HighBitSet;
347 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
349 bool isLeftShift = I.getOpcode() == Instruction::Shl;
352 if (!match(Op1, m_APInt(Op1C)))
355 // See if we can propagate this shift into the input, this covers the trivial
356 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
357 if (I.getOpcode() != Instruction::AShr &&
358 canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
360 dbgs() << "ICE: GetShiftedValue propagating shift through expression"
361 " to eliminate shift:\n IN: "
362 << *Op0 << "\n SH: " << I << "\n");
364 return replaceInstUsesWith(
365 I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
368 // See if we can simplify any instructions used by the instruction whose sole
369 // purpose is to compute bits we don't care about.
370 unsigned TypeBits = Op0->getType()->getScalarSizeInBits();
372 assert(!Op1C->uge(TypeBits) &&
373 "Shift over the type width should have been removed already");
375 if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
378 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
379 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
380 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
381 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
382 // place. Don't try to do this transformation in this case. Also, we
383 // require that the input operand is a shift-by-constant so that we have
384 // confidence that the shifts will get folded together. We could do this
385 // xform in more cases, but it is unlikely to be profitable.
386 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
387 isa<ConstantInt>(TrOp->getOperand(1))) {
388 // Okay, we'll do this xform. Make the shift of shift.
390 ConstantExpr::getZExt(cast<Constant>(Op1), TrOp->getType());
391 // (shift2 (shift1 & 0x00FF), c2)
392 Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
394 // For logical shifts, the truncation has the effect of making the high
395 // part of the register be zeros. Emulate this by inserting an AND to
396 // clear the top bits as needed. This 'and' will usually be zapped by
397 // other xforms later if dead.
398 unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
399 unsigned DstSize = TI->getType()->getScalarSizeInBits();
400 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
402 // The mask we constructed says what the trunc would do if occurring
403 // between the shifts. We want to know the effect *after* the second
404 // shift. We know that it is a logical shift by a constant, so adjust the
405 // mask as appropriate.
406 if (I.getOpcode() == Instruction::Shl)
407 MaskV <<= Op1C->getZExtValue();
409 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
410 MaskV.lshrInPlace(Op1C->getZExtValue());
414 Value *And = Builder.CreateAnd(NSh,
415 ConstantInt::get(I.getContext(), MaskV),
418 // Return the value truncated to the interesting size.
419 return new TruncInst(And, I.getType());
423 if (Op0->hasOneUse()) {
424 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
425 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
428 switch (Op0BO->getOpcode()) {
430 case Instruction::Add:
431 case Instruction::And:
432 case Instruction::Or:
433 case Instruction::Xor: {
434 // These operators commute.
435 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
436 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
437 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
439 Value *YS = // (Y << C)
440 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
442 Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
443 Op0BO->getOperand(1)->getName());
444 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
446 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
447 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
448 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
449 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
450 return BinaryOperator::CreateAnd(X, Mask);
453 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
454 Value *Op0BOOp1 = Op0BO->getOperand(1);
455 if (isLeftShift && Op0BOOp1->hasOneUse() &&
457 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
458 m_ConstantInt(CC)))) {
459 Value *YS = // (Y << C)
460 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
462 Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
463 V1->getName()+".mask");
464 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
469 case Instruction::Sub: {
470 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
471 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
472 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
474 Value *YS = // (Y << C)
475 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
477 Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
478 Op0BO->getOperand(0)->getName());
479 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
481 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
482 Constant *Mask = ConstantInt::get(I.getContext(), Bits);
483 if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
484 Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
485 return BinaryOperator::CreateAnd(X, Mask);
488 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
489 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
490 match(Op0BO->getOperand(0),
491 m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
492 m_ConstantInt(CC))) && V2 == Op1) {
493 Value *YS = // (Y << C)
494 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
496 Value *XM = Builder.CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
497 V1->getName()+".mask");
499 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
507 // If the operand is a bitwise operator with a constant RHS, and the
508 // shift is the only use, we can pull it out of the shift.
510 if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
511 if (canShiftBinOpWithConstantRHS(I, Op0BO, *Op0C)) {
512 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
513 cast<Constant>(Op0BO->getOperand(1)), Op1);
516 Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
517 NewShift->takeName(Op0BO);
519 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
524 // If the operand is a subtract with a constant LHS, and the shift
525 // is the only use, we can pull it out of the shift.
526 // This folds (shl (sub C1, X), C2) -> (sub (C1 << C2), (shl X, C2))
527 if (isLeftShift && Op0BO->getOpcode() == Instruction::Sub &&
528 match(Op0BO->getOperand(0), m_APInt(Op0C))) {
529 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
530 cast<Constant>(Op0BO->getOperand(0)), Op1);
532 Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
533 NewShift->takeName(Op0BO);
535 return BinaryOperator::CreateSub(NewRHS, NewShift);
539 // If we have a select that conditionally executes some binary operator,
540 // see if we can pull it the select and operator through the shift.
542 // For example, turning:
543 // shl (select C, (add X, C1), X), C2
546 // select C, (add Y, C1 << C2), Y
550 if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
551 m_Value(FalseVal)))) {
553 if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
554 match(TBO->getOperand(1), m_APInt(C)) &&
555 canShiftBinOpWithConstantRHS(I, TBO, *C)) {
556 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
557 cast<Constant>(TBO->getOperand(1)), Op1);
560 Builder.CreateBinOp(I.getOpcode(), FalseVal, Op1);
561 Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift,
563 return SelectInst::Create(Cond, NewOp, NewShift);
569 if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
570 m_OneUse(m_BinOp(FBO))))) {
572 if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
573 match(FBO->getOperand(1), m_APInt(C)) &&
574 canShiftBinOpWithConstantRHS(I, FBO, *C)) {
575 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
576 cast<Constant>(FBO->getOperand(1)), Op1);
579 Builder.CreateBinOp(I.getOpcode(), TrueVal, Op1);
580 Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift,
582 return SelectInst::Create(Cond, NewShift, NewOp);
590 Instruction *InstCombiner::visitShl(BinaryOperator &I) {
591 if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
592 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
593 SQ.getWithInstruction(&I)))
594 return replaceInstUsesWith(I, V);
596 if (Instruction *X = foldVectorBinop(I))
599 if (Instruction *V = commonShiftTransforms(I))
602 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
603 Type *Ty = I.getType();
604 const APInt *ShAmtAPInt;
605 if (match(Op1, m_APInt(ShAmtAPInt))) {
606 unsigned ShAmt = ShAmtAPInt->getZExtValue();
607 unsigned BitWidth = Ty->getScalarSizeInBits();
609 // shl (zext X), ShAmt --> zext (shl X, ShAmt)
610 // This is only valid if X would have zeros shifted out.
612 if (match(Op0, m_ZExt(m_Value(X)))) {
613 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
614 if (ShAmt < SrcWidth &&
615 MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
616 return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
619 // (X >> C) << C --> X & (-1 << C)
620 if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
621 APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
622 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
625 // FIXME: we do not yet transform non-exact shr's. The backend (DAGCombine)
626 // needs a few fixes for the rotate pattern recognition first.
628 if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(ShOp1))))) {
629 unsigned ShrAmt = ShOp1->getZExtValue();
630 if (ShrAmt < ShAmt) {
631 // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
632 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
633 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
634 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
635 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
638 if (ShrAmt > ShAmt) {
639 // If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
640 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
641 auto *NewShr = BinaryOperator::Create(
642 cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
643 NewShr->setIsExact(true);
648 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1)))) {
649 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
650 // Oversized shifts are simplified to zero in InstSimplify.
651 if (AmtSum < BitWidth)
652 // (X << C1) << C2 --> X << (C1 + C2)
653 return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
656 // If the shifted-out value is known-zero, then this is a NUW shift.
657 if (!I.hasNoUnsignedWrap() &&
658 MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
659 I.setHasNoUnsignedWrap();
663 // If the shifted-out value is all signbits, then this is a NSW shift.
664 if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
665 I.setHasNoSignedWrap();
670 // Transform (x >> y) << y to x & (-1 << y)
671 // Valid for any type of right-shift.
673 if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
674 Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
675 Value *Mask = Builder.CreateShl(AllOnes, Op1);
676 return BinaryOperator::CreateAnd(Mask, X);
680 if (match(Op1, m_Constant(C1))) {
683 // (C2 << X) << C1 --> (C2 << C1) << X
684 if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
685 return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
687 // (X * C2) << C1 --> X * (C2 << C1)
688 if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
689 return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
695 Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
696 if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
697 SQ.getWithInstruction(&I)))
698 return replaceInstUsesWith(I, V);
700 if (Instruction *X = foldVectorBinop(I))
703 if (Instruction *R = commonShiftTransforms(I))
706 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
707 Type *Ty = I.getType();
708 const APInt *ShAmtAPInt;
709 if (match(Op1, m_APInt(ShAmtAPInt))) {
710 unsigned ShAmt = ShAmtAPInt->getZExtValue();
711 unsigned BitWidth = Ty->getScalarSizeInBits();
712 auto *II = dyn_cast<IntrinsicInst>(Op0);
713 if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
714 (II->getIntrinsicID() == Intrinsic::ctlz ||
715 II->getIntrinsicID() == Intrinsic::cttz ||
716 II->getIntrinsicID() == Intrinsic::ctpop)) {
717 // ctlz.i32(x)>>5 --> zext(x == 0)
718 // cttz.i32(x)>>5 --> zext(x == 0)
719 // ctpop.i32(x)>>5 --> zext(x == -1)
720 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
721 Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
722 Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
723 return new ZExtInst(Cmp, Ty);
728 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
729 if (ShOp1->ult(ShAmt)) {
730 unsigned ShlAmt = ShOp1->getZExtValue();
731 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
732 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
733 // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
734 auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
735 NewLShr->setIsExact(I.isExact());
738 // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
739 Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
740 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
741 return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
743 if (ShOp1->ugt(ShAmt)) {
744 unsigned ShlAmt = ShOp1->getZExtValue();
745 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
746 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
747 // (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
748 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
749 NewShl->setHasNoUnsignedWrap(true);
752 // (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
753 Value *NewShl = Builder.CreateShl(X, ShiftDiff);
754 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
755 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
757 assert(*ShOp1 == ShAmt);
758 // (X << C) >>u C --> X & (-1 >>u C)
759 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
760 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
763 if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
764 (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
765 assert(ShAmt < X->getType()->getScalarSizeInBits() &&
766 "Big shift not simplified to zero?");
767 // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
768 Value *NewLShr = Builder.CreateLShr(X, ShAmt);
769 return new ZExtInst(NewLShr, Ty);
772 if (match(Op0, m_SExt(m_Value(X))) &&
773 (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
774 // Are we moving the sign bit to the low bit and widening with high zeros?
775 unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
776 if (ShAmt == BitWidth - 1) {
777 // lshr (sext i1 X to iN), N-1 --> zext X to iN
778 if (SrcTyBitWidth == 1)
779 return new ZExtInst(X, Ty);
781 // lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
782 if (Op0->hasOneUse()) {
783 Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
784 return new ZExtInst(NewLShr, Ty);
788 // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
789 if (ShAmt == BitWidth - SrcTyBitWidth && Op0->hasOneUse()) {
790 // The new shift amount can't be more than the narrow source type.
791 unsigned NewShAmt = std::min(ShAmt, SrcTyBitWidth - 1);
792 Value *AShr = Builder.CreateAShr(X, NewShAmt);
793 return new ZExtInst(AShr, Ty);
797 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
798 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
799 // Oversized shifts are simplified to zero in InstSimplify.
800 if (AmtSum < BitWidth)
801 // (X >>u C1) >>u C2 --> X >>u (C1 + C2)
802 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
805 // If the shifted-out value is known-zero, then this is an exact shift.
807 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
813 // Transform (x << y) >> y to x & (-1 >> y)
815 if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
816 Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
817 Value *Mask = Builder.CreateLShr(AllOnes, Op1);
818 return BinaryOperator::CreateAnd(Mask, X);
824 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
825 if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
826 SQ.getWithInstruction(&I)))
827 return replaceInstUsesWith(I, V);
829 if (Instruction *X = foldVectorBinop(I))
832 if (Instruction *R = commonShiftTransforms(I))
835 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
836 Type *Ty = I.getType();
837 unsigned BitWidth = Ty->getScalarSizeInBits();
838 const APInt *ShAmtAPInt;
839 if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
840 unsigned ShAmt = ShAmtAPInt->getZExtValue();
842 // If the shift amount equals the difference in width of the destination
843 // and source scalar types:
844 // ashr (shl (zext X), C), C --> sext X
846 if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
847 ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
848 return new SExtInst(X, Ty);
850 // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
851 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
853 if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
854 ShOp1->ult(BitWidth)) {
855 unsigned ShlAmt = ShOp1->getZExtValue();
856 if (ShlAmt < ShAmt) {
857 // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
858 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
859 auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
860 NewAShr->setIsExact(I.isExact());
863 if (ShlAmt > ShAmt) {
864 // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
865 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
866 auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
867 NewShl->setHasNoSignedWrap(true);
872 if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
873 ShOp1->ult(BitWidth)) {
874 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
875 // Oversized arithmetic shifts replicate the sign bit.
876 AmtSum = std::min(AmtSum, BitWidth - 1);
877 // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
878 return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
881 if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
882 (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
883 // ashr (sext X), C --> sext (ashr X, C')
884 Type *SrcTy = X->getType();
885 ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
886 Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
887 return new SExtInst(NewSh, Ty);
890 // If the shifted-out value is known-zero, then this is an exact shift.
892 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
898 // See if we can turn a signed shr into an unsigned shr.
899 if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
900 return BinaryOperator::CreateLShr(Op0, Op1);