20 using namespace PatternMatch;
27 if (SimplifyDemandedInstructionBits(I))
31 if (isa<Constant>(Op0))
32 if (
SelectInst *SI = dyn_cast<SelectInst>(Op1))
37 if (
Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
90 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
91 uint32_t BitWidth = Ty->getScalarSizeInBits();
107 default:
return false;
115 case Instruction::Shl: {
118 if (CI == 0)
return false;
121 if (isLeftShift)
return true;
124 if (CI->
getValue() == NumBits)
return true;
139 case Instruction::LShr: {
142 if (CI == 0)
return false;
145 if (!isLeftShift)
return true;
148 if (CI->
getValue() == NumBits)
return true;
186 if (
Constant *
C = dyn_cast<Constant>(V)) {
211 case Instruction::Shl: {
222 if (NewShAmt >= TypeWidth)
253 case Instruction::LShr: {
263 if (NewShAmt >= TypeWidth)
304 NumBits, isLeftShift, IC));
314 bool isLeftShift = I.
getOpcode() == Instruction::Shl;
319 if (I.
getOpcode() != Instruction::AShr &&
321 DEBUG(
dbgs() <<
"ICE: GetShiftedValue propagating shift through expression"
322 " to eliminate shift:\n IN: " << *Op0 <<
"\n SH: " << I <<
"\n");
324 return ReplaceInstUsesWith(I,
336 if (Op1->
uge(TypeBits)) {
346 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
347 if (
Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
348 return BinaryOperator::CreateMul(BO->getOperand(0),
352 if (
SelectInst *SI = dyn_cast<SelectInst>(Op0))
355 if (isa<PHINode>(Op0))
360 if (
TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
379 unsigned DstSize = TI->getType()->getScalarSizeInBits();
389 assert(I.
getOpcode() == Instruction::LShr &&
"Unknown logical shift");
394 Value *
And = Builder->CreateAnd(NSh,
408 switch (Op0BO->getOpcode()) {
410 case Instruction::Add:
416 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
420 Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->
getName());
422 Value *
X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
423 Op0BO->getOperand(1)->
getName());
430 Value *Op0BOOp1 = Op0BO->getOperand(1);
431 if (isLeftShift && Op0BOOp1->
hasOneUse() &&
436 Builder->CreateShl(Op0BO->getOperand(0), Op1,
446 case Instruction::Sub: {
448 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
452 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->
getName());
454 Value *
X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
455 Op0BO->getOperand(0)->
getName());
462 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
463 match(Op0BO->getOperand(0),
467 Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->
getName());
482 if (
ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
484 bool highBitSet =
false;
486 switch (Op0BO->getOpcode()) {
487 default: isValid =
false;
break;
488 case Instruction::Add:
489 isValid = isLeftShift;
506 if (isValid && I.
getOpcode() == Instruction::AShr)
507 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
513 Builder->CreateBinOp(I.
getOpcode(), Op0BO->getOperand(0), Op1);
525 if (ShiftOp && !ShiftOp->
isShift())
528 if (ShiftOp && isa<ConstantInt>(ShiftOp->
getOperand(1))) {
545 assert(ShiftAmt2 != 0 &&
"Should have been simplified earlier");
546 if (ShiftAmt1 == 0)
return 0;
553 uint32_t AmtSum = ShiftAmt1+ShiftAmt2;
556 if (AmtSum >= TypeBits) {
566 if (ShiftAmt1 == ShiftAmt2) {
568 if (I.
getOpcode() == Instruction::LShr &&
569 ShiftOp->
getOpcode() == Instruction::Shl) {
571 return BinaryOperator::CreateAnd(X,
574 }
else if (ShiftAmt1 < ShiftAmt2) {
575 uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
581 ShiftOp->
getOpcode() != Instruction::Shl &&
583 assert(ShiftOp->
getOpcode() == Instruction::LShr ||
584 ShiftOp->
getOpcode() == Instruction::AShr);
594 if (I.
getOpcode() == Instruction::LShr &&
595 ShiftOp->
getOpcode() == Instruction::Shl) {
604 Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
607 return BinaryOperator::CreateAnd(Shift,
613 if (I.
getOpcode() == Instruction::AShr &&
614 ShiftOp->
getOpcode() == Instruction::Shl) {
625 assert(ShiftAmt2 < ShiftAmt1);
626 uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
632 ShiftOp->
getOpcode() != Instruction::Shl &&
642 if (I.
getOpcode() == Instruction::LShr &&
643 ShiftOp->
getOpcode() == Instruction::Shl) {
652 Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
655 return BinaryOperator::CreateAnd(Shift,
661 if (I.
getOpcode() == Instruction::AShr &&
662 ShiftOp->
getOpcode() == Instruction::Shl) {
681 return ReplaceInstUsesWith(I, V);
687 unsigned ShAmt = Op1C->getZExtValue();
718 return ReplaceInstUsesWith(I, V);
725 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
726 unsigned ShAmt = Op1C->getZExtValue();
739 Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
740 return new ZExtInst(Cmp, II->getType());
758 return ReplaceInstUsesWith(I, V);
765 if (
ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
766 unsigned ShAmt = Op1C->getZExtValue();
774 if (cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
775 return ReplaceInstUsesWith(I, X);
782 if (
ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
783 uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
784 uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
785 if (Op1C->getZExtValue() == DestBits-SrcBits)
786 return new SExtInst(ZI->getOperand(0), ZI->getType());
801 return BinaryOperator::CreateLShr(Op0, Op1);
806 return ReplaceInstUsesWith(I, Op0);
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
void setHasNoSignedWrap(bool b=true)
static APInt getSignBit(unsigned BitWidth)
Get the SignBit for a specific bit width.
unsigned getScalarSizeInBits()
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
This class represents zero extension of integer types.
bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout *TD=0, unsigned Depth=0)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
class_match< Constant > m_Constant()
static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC)
This class represents a sign extension of integer types.
static Constant * getNullValue(Type *Ty)
StringRef getName() const
bool uge(uint64_t Num) const
Determine if the value is greater or equal to the given number.
bool match(Val *V, const Pattern &P)
DataLayout * getDataLayout() const
Instruction * commonShiftTransforms(BinaryOperator &I)
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0)
const APInt & getValue() const
Return the constant's value.
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::AShr > m_Shr(const LHS &L, const RHS &R)
m_Shr - Matches LShr or AShr.
#define llvm_unreachable(msg)
void setHasNoUnsignedWrap(bool b=true)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
InstCombiner - The -instcombine pass.
uint64_t getZExtValue() const
Return the zero extended value.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
static bool CanEvaluateTruncated(Value *V, Type *Ty)
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
Get the constant's value with a saturation limit.
bool isLogicalShift() const
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_power2 > m_Power2()
m_Power2() - Match an integer or vector power of 2.
This class represents a truncation of integer types.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
unsigned getNumIncomingValues() const
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Get a value with high bits set.
OneUse_match< T > m_OneUse(const T &SubPattern)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
LLVM Constant Representation.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Value * getOperand(unsigned i) const
Integer representation type.
InstCombineWorklist Worklist
Worklist - All of the instructions that need to be simplified.
Instruction * visitLShr(BinaryOperator &I)
LLVMContext & getContext() const
All values hold a context through their type.
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
const Value * getTrueValue() const
void setIsExact(bool b=true)
BinaryOps getOpcode() const
Class for constant integers.
Value * getIncomingValue(unsigned i) const
static Value * GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC)
Instruction * FoldShiftByConstant(Value *Op0, ConstantInt *Op1, BinaryOperator &I)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Get a ConstantInt for a specific signed value.
bool isExact() const
isExact - Determine whether the exact flag is set.
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
unsigned Log2_32(uint32_t Value)
Class for arbitrary precision integers.
TargetLibraryInfo * getTargetLibraryInfo() const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static Constant * getZExt(Constant *C, Type *Ty)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=0)
unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD=0, unsigned Depth=0)
LLVM Value Representation.
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isPowerOf2_32(uint32_t Value)
const Value * getFalseValue() const
Instruction * visitAShr(BinaryOperator &I)
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
void setIncomingValue(unsigned i, Value *V)
Instruction * visitShl(BinaryOperator &I)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD