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 "InstCombine.h"
15 #include "llvm/IntrinsicInst.h"
16 #include "llvm/Support/PatternMatch.h"
18 using namespace PatternMatch;
20 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
21 assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
22 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
24 // shl X, 0 == X and shr X, 0 == X
25 // shl 0, X == 0 and shr 0, X == 0
26 if (Op1 == Constant::getNullValue(Op1->getType()) ||
27 Op0 == Constant::getNullValue(Op0->getType()))
28 return ReplaceInstUsesWith(I, Op0);
30 if (isa<UndefValue>(Op0)) {
31 if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
32 return ReplaceInstUsesWith(I, Op0);
33 else // undef << X -> 0, undef >>u X -> 0
34 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
36 if (isa<UndefValue>(Op1)) {
37 if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X
38 return ReplaceInstUsesWith(I, Op0);
39 else // X << undef, X >>u undef -> 0
40 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
43 // See if we can fold away this shift.
44 if (SimplifyDemandedInstructionBits(I))
47 // Try to fold constant and into select arguments.
48 if (isa<Constant>(Op0))
49 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
50 if (Instruction *R = FoldOpIntoSelect(I, SI))
53 if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
54 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
59 /// CanEvaluateShifted - See if we can compute the specified value, but shifted
60 /// logically to the left or right by some number of bits. This should return
61 /// true if the expression can be computed for the same cost as the current
62 /// expression tree. This is used to eliminate extraneous shifting from things
64 /// %C = shl i128 %A, 64
65 /// %D = shl i128 %B, 96
66 /// %E = or i128 %C, %D
67 /// %F = lshr i128 %E, 64
68 /// where the client will ask if E can be computed shifted right by 64-bits. If
69 /// this succeeds, the GetShiftedValue function will be called to produce the
71 static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift,
73 // We can always evaluate constants shifted.
77 Instruction *I = dyn_cast<Instruction>(V);
80 // If this is the opposite shift, we can directly reuse the input of the shift
81 // if the needed bits are already zero in the input. This allows us to reuse
82 // the value which means that we don't care if the shift has multiple uses.
83 // TODO: Handle opposite shift by exact value.
85 if ((isLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
86 (!isLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
87 if (CI->getZExtValue() == NumBits) {
88 // TODO: Check that the input bits are already zero with MaskedValueIsZero
90 // If this is a truncate of a logical shr, we can truncate it to a smaller
91 // lshr iff we know that the bits we would otherwise be shifting in are
93 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
94 uint32_t BitWidth = Ty->getScalarSizeInBits();
95 if (MaskedValueIsZero(I->getOperand(0),
96 APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
97 CI->getLimitedValue(BitWidth) < BitWidth) {
98 return CanEvaluateTruncated(I->getOperand(0), Ty);
105 // We can't mutate something that has multiple uses: doing so would
106 // require duplicating the instruction in general, which isn't profitable.
107 if (!I->hasOneUse()) return false;
109 switch (I->getOpcode()) {
110 default: return false;
111 case Instruction::And:
112 case Instruction::Or:
113 case Instruction::Xor:
114 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
115 return CanEvaluateShifted(I->getOperand(0), NumBits, isLeftShift, IC) &&
116 CanEvaluateShifted(I->getOperand(1), NumBits, isLeftShift, IC);
118 case Instruction::Shl:
119 // We can often fold the shift into shifts-by-a-constant.
120 CI = dyn_cast<ConstantInt>(I->getOperand(1));
121 if (CI == 0) return false;
123 // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
124 if (isLeftShift) return true;
126 // We can always turn shl(c)+shr(c) -> and(c2).
127 if (CI->getValue() == NumBits) return true;
128 // We can always turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but it isn't
129 // profitable unless we know the and'd out bits are already zero.
131 case Instruction::LShr:
132 // We can often fold the shift into shifts-by-a-constant.
133 CI = dyn_cast<ConstantInt>(I->getOperand(1));
134 if (CI == 0) return false;
136 // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
137 if (!isLeftShift) return true;
139 // We can always turn lshr(c)+shl(c) -> and(c2).
140 if (CI->getValue() == NumBits) return true;
142 // We can always turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but it isn't
143 // profitable unless we know the and'd out bits are already zero.
146 case Instruction::Select: {
147 SelectInst *SI = cast<SelectInst>(I);
148 return CanEvaluateShifted(SI->getTrueValue(), NumBits, isLeftShift, IC) &&
149 CanEvaluateShifted(SI->getFalseValue(), NumBits, isLeftShift, IC);
151 case Instruction::PHI: {
152 // We can change a phi if we can change all operands. Note that we never
153 // get into trouble with cyclic PHIs here because we only consider
154 // instructions with a single use.
155 PHINode *PN = cast<PHINode>(I);
156 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
157 if (!CanEvaluateShifted(PN->getIncomingValue(i), NumBits, isLeftShift,IC))
164 /// GetShiftedValue - When CanEvaluateShifted returned true for an expression,
165 /// this value inserts the new computation that produces the shifted value.
166 static Value *GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
168 // We can always evaluate constants shifted.
169 if (Constant *C = dyn_cast<Constant>(V)) {
171 V = IC.Builder->CreateShl(C, NumBits);
173 V = IC.Builder->CreateLShr(C, NumBits);
174 // If we got a constantexpr back, try to simplify it with TD info.
175 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
176 V = ConstantFoldConstantExpression(CE, IC.getTargetData());
180 Instruction *I = cast<Instruction>(V);
183 switch (I->getOpcode()) {
184 default: assert(0 && "Inconsistency with CanEvaluateShifted");
185 case Instruction::And:
186 case Instruction::Or:
187 case Instruction::Xor:
188 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
189 I->setOperand(0, GetShiftedValue(I->getOperand(0), NumBits,isLeftShift,IC));
190 I->setOperand(1, GetShiftedValue(I->getOperand(1), NumBits,isLeftShift,IC));
193 case Instruction::Shl: {
194 unsigned TypeWidth = I->getType()->getScalarSizeInBits();
196 // We only accept shifts-by-a-constant in CanEvaluateShifted.
197 ConstantInt *CI = cast<ConstantInt>(I->getOperand(1));
199 // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
201 // If this is oversized composite shift, then unsigned shifts get 0.
202 unsigned NewShAmt = NumBits+CI->getZExtValue();
203 if (NewShAmt >= TypeWidth)
204 return Constant::getNullValue(I->getType());
206 I->setOperand(1, ConstantInt::get(I->getType(), NewShAmt));
210 // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
212 assert(CI->getValue() == NumBits);
214 APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
215 V = IC.Builder->CreateAnd(I->getOperand(0),
216 ConstantInt::get(I->getContext(), Mask));
217 if (Instruction *VI = dyn_cast<Instruction>(V)) {
223 case Instruction::LShr: {
224 unsigned TypeWidth = I->getType()->getScalarSizeInBits();
225 // We only accept shifts-by-a-constant in CanEvaluateShifted.
226 ConstantInt *CI = cast<ConstantInt>(I->getOperand(1));
228 // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
230 // If this is oversized composite shift, then unsigned shifts get 0.
231 unsigned NewShAmt = NumBits+CI->getZExtValue();
232 if (NewShAmt >= TypeWidth)
233 return Constant::getNullValue(I->getType());
235 I->setOperand(1, ConstantInt::get(I->getType(), NewShAmt));
239 // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
241 assert(CI->getValue() == NumBits);
243 APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
244 V = IC.Builder->CreateAnd(I->getOperand(0),
245 ConstantInt::get(I->getContext(), Mask));
246 if (Instruction *VI = dyn_cast<Instruction>(V)) {
253 case Instruction::Select:
254 I->setOperand(1, GetShiftedValue(I->getOperand(1), NumBits,isLeftShift,IC));
255 I->setOperand(2, GetShiftedValue(I->getOperand(2), NumBits,isLeftShift,IC));
257 case Instruction::PHI: {
258 // We can change a phi if we can change all operands. Note that we never
259 // get into trouble with cyclic PHIs here because we only consider
260 // instructions with a single use.
261 PHINode *PN = cast<PHINode>(I);
262 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
263 PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i),
264 NumBits, isLeftShift, IC));
272 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1,
274 bool isLeftShift = I.getOpcode() == Instruction::Shl;
277 // See if we can propagate this shift into the input, this covers the trivial
278 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
279 if (I.getOpcode() != Instruction::AShr &&
280 CanEvaluateShifted(Op0, Op1->getZExtValue(), isLeftShift, *this)) {
281 DEBUG(dbgs() << "ICE: GetShiftedValue propagatin shift through expression"
282 " to eliminate shift:\n IN: " << *Op0 << "\nSH: " << I << "\n");
284 return ReplaceInstUsesWith(I,
285 GetShiftedValue(Op0, Op1->getZExtValue(), isLeftShift, *this));
289 // See if we can simplify any instructions used by the instruction whose sole
290 // purpose is to compute bits we don't care about.
291 uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
293 // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate
296 if (Op1->uge(TypeBits)) {
297 if (I.getOpcode() != Instruction::AShr)
298 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
299 // ashr i32 X, 32 --> ashr i32 X, 31
300 I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
304 // ((X*C1) << C2) == (X * (C1 << C2))
305 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
306 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
307 if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
308 return BinaryOperator::CreateMul(BO->getOperand(0),
309 ConstantExpr::getShl(BOOp, Op1));
311 // Try to fold constant and into select arguments.
312 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
313 if (Instruction *R = FoldOpIntoSelect(I, SI))
315 if (isa<PHINode>(Op0))
316 if (Instruction *NV = FoldOpIntoPhi(I))
319 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
320 if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
321 Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
322 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
323 // place. Don't try to do this transformation in this case. Also, we
324 // require that the input operand is a shift-by-constant so that we have
325 // confidence that the shifts will get folded together. We could do this
326 // xform in more cases, but it is unlikely to be profitable.
327 if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
328 isa<ConstantInt>(TrOp->getOperand(1))) {
329 // Okay, we'll do this xform. Make the shift of shift.
330 Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
331 // (shift2 (shift1 & 0x00FF), c2)
332 Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
334 // For logical shifts, the truncation has the effect of making the high
335 // part of the register be zeros. Emulate this by inserting an AND to
336 // clear the top bits as needed. This 'and' will usually be zapped by
337 // other xforms later if dead.
338 unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
339 unsigned DstSize = TI->getType()->getScalarSizeInBits();
340 APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
342 // The mask we constructed says what the trunc would do if occurring
343 // between the shifts. We want to know the effect *after* the second
344 // shift. We know that it is a logical shift by a constant, so adjust the
345 // mask as appropriate.
346 if (I.getOpcode() == Instruction::Shl)
347 MaskV <<= Op1->getZExtValue();
349 assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
350 MaskV = MaskV.lshr(Op1->getZExtValue());
354 Value *And = Builder->CreateAnd(NSh,
355 ConstantInt::get(I.getContext(), MaskV),
358 // Return the value truncated to the interesting size.
359 return new TruncInst(And, I.getType());
363 if (Op0->hasOneUse()) {
364 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
365 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
368 switch (Op0BO->getOpcode()) {
370 case Instruction::Add:
371 case Instruction::And:
372 case Instruction::Or:
373 case Instruction::Xor: {
374 // These operators commute.
375 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
376 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
377 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
379 Value *YS = // (Y << C)
380 Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
382 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
383 Op0BO->getOperand(1)->getName());
384 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
385 return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(),
386 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
389 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
390 Value *Op0BOOp1 = Op0BO->getOperand(1);
391 if (isLeftShift && Op0BOOp1->hasOneUse() &&
393 m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
394 m_ConstantInt(CC))) &&
395 cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
396 Value *YS = // (Y << C)
397 Builder->CreateShl(Op0BO->getOperand(0), Op1,
400 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
401 V1->getName()+".mask");
402 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
407 case Instruction::Sub: {
408 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
409 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
410 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
412 Value *YS = // (Y << C)
413 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
415 Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
416 Op0BO->getOperand(0)->getName());
417 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
418 return BinaryOperator::CreateAnd(X, ConstantInt::get(I.getContext(),
419 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
422 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
423 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
424 match(Op0BO->getOperand(0),
425 m_And(m_Shr(m_Value(V1), m_Value(V2)),
426 m_ConstantInt(CC))) && V2 == Op1 &&
427 cast<BinaryOperator>(Op0BO->getOperand(0))
428 ->getOperand(0)->hasOneUse()) {
429 Value *YS = // (Y << C)
430 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
432 Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
433 V1->getName()+".mask");
435 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
443 // If the operand is an bitwise operator with a constant RHS, and the
444 // shift is the only use, we can pull it out of the shift.
445 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
446 bool isValid = true; // Valid only for And, Or, Xor
447 bool highBitSet = false; // Transform if high bit of constant set?
449 switch (Op0BO->getOpcode()) {
450 default: isValid = false; break; // Do not perform transform!
451 case Instruction::Add:
452 isValid = isLeftShift;
454 case Instruction::Or:
455 case Instruction::Xor:
458 case Instruction::And:
463 // If this is a signed shift right, and the high bit is modified
464 // by the logical operation, do not perform the transformation.
465 // The highBitSet boolean indicates the value of the high bit of
466 // the constant which would cause it to be modified for this
469 if (isValid && I.getOpcode() == Instruction::AShr)
470 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
473 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
476 Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
477 NewShift->takeName(Op0BO);
479 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
486 // Find out if this is a shift of a shift by a constant.
487 BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
488 if (ShiftOp && !ShiftOp->isShift())
491 if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
492 ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
493 uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
494 uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits);
495 assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
496 if (ShiftAmt1 == 0) return 0; // Will be simplified in the future.
497 Value *X = ShiftOp->getOperand(0);
499 uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift.
501 const IntegerType *Ty = cast<IntegerType>(I.getType());
503 // Check for (X << c1) << c2 and (X >> c1) >> c2
504 if (I.getOpcode() == ShiftOp->getOpcode()) {
505 // If this is oversized composite shift, then unsigned shifts get 0, ashr
507 if (AmtSum >= TypeBits) {
508 if (I.getOpcode() != Instruction::AShr)
509 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
510 AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
513 return BinaryOperator::Create(I.getOpcode(), X,
514 ConstantInt::get(Ty, AmtSum));
517 if (ShiftAmt1 == ShiftAmt2) {
518 // If we have ((X >>? C) << C), turn this into X & (-1 << C).
519 if (I.getOpcode() == Instruction::Shl &&
520 ShiftOp->getOpcode() != Instruction::Shl) {
521 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
522 return BinaryOperator::CreateAnd(X,
523 ConstantInt::get(I.getContext(),Mask));
525 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
526 if (I.getOpcode() == Instruction::LShr &&
527 ShiftOp->getOpcode() == Instruction::Shl) {
528 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
529 return BinaryOperator::CreateAnd(X,
530 ConstantInt::get(I.getContext(), Mask));
532 } else if (ShiftAmt1 < ShiftAmt2) {
533 uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
535 // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2)
536 if (I.getOpcode() == Instruction::Shl &&
537 ShiftOp->getOpcode() != Instruction::Shl) {
538 assert(ShiftOp->getOpcode() == Instruction::LShr ||
539 ShiftOp->getOpcode() == Instruction::AShr);
540 Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
542 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
543 return BinaryOperator::CreateAnd(Shift,
544 ConstantInt::get(I.getContext(),Mask));
547 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
548 if (I.getOpcode() == Instruction::LShr &&
549 ShiftOp->getOpcode() == Instruction::Shl) {
550 assert(ShiftOp->getOpcode() == Instruction::Shl);
551 Value *Shift = Builder->CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
553 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
554 return BinaryOperator::CreateAnd(Shift,
555 ConstantInt::get(I.getContext(),Mask));
558 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
560 assert(ShiftAmt2 < ShiftAmt1);
561 uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
563 // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2)
564 if (I.getOpcode() == Instruction::Shl &&
565 ShiftOp->getOpcode() != Instruction::Shl) {
566 Value *Shift = Builder->CreateBinOp(ShiftOp->getOpcode(), X,
567 ConstantInt::get(Ty, ShiftDiff));
569 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
570 return BinaryOperator::CreateAnd(Shift,
571 ConstantInt::get(I.getContext(),Mask));
574 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
575 if (I.getOpcode() == Instruction::LShr &&
576 ShiftOp->getOpcode() == Instruction::Shl) {
577 Value *Shift = Builder->CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
579 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
580 return BinaryOperator::CreateAnd(Shift,
581 ConstantInt::get(I.getContext(),Mask));
584 // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
590 Instruction *InstCombiner::visitShl(BinaryOperator &I) {
591 return commonShiftTransforms(I);
594 Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
595 if (Instruction *R = commonShiftTransforms(I))
598 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
600 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1))
601 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
602 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
603 // ctlz.i32(x)>>5 --> zext(x == 0)
604 // cttz.i32(x)>>5 --> zext(x == 0)
605 // ctpop.i32(x)>>5 --> zext(x == -1)
606 if ((II->getIntrinsicID() == Intrinsic::ctlz ||
607 II->getIntrinsicID() == Intrinsic::cttz ||
608 II->getIntrinsicID() == Intrinsic::ctpop) &&
609 isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == Op1C->getZExtValue()){
610 bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
611 Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
612 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
613 return new ZExtInst(Cmp, II->getType());
620 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
621 if (Instruction *R = commonShiftTransforms(I))
624 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
626 if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) {
627 // ashr int -1, X = -1 (for any arithmetic shift rights of ~0)
628 if (CSI->isAllOnesValue())
629 return ReplaceInstUsesWith(I, CSI);
632 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
633 // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
634 // have a sign-extend idiom.
636 if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
637 // If the input value is known to already be sign extended enough, delete
639 if (ComputeNumSignBits(X) > Op1C->getZExtValue())
640 return ReplaceInstUsesWith(I, X);
642 // If the input is an extension from the shifted amount value, e.g.
643 // %x = zext i8 %A to i32
644 // %y = shl i32 %x, 24
646 // then turn this into "z = sext i8 A to i32".
647 if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
648 uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
649 uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
650 if (Op1C->getZExtValue() == DestBits-SrcBits)
651 return new SExtInst(ZI->getOperand(0), ZI->getType());
656 // See if we can turn a signed shr into an unsigned shr.
657 if (MaskedValueIsZero(Op0,
658 APInt::getSignBit(I.getType()->getScalarSizeInBits())))
659 return BinaryOperator::CreateLShr(Op0, Op1);
661 // Arithmetic shifting an all-sign-bit value is a no-op.
662 unsigned NumSignBits = ComputeNumSignBits(Op0);
663 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
664 return ReplaceInstUsesWith(I, Op0);