19using namespace PatternMatch;
21#define DEBUG_TYPE "instcombine"
39 unsigned MaximalPossibleTotalShiftAmount =
42 APInt MaximalRepresentableShiftAmount =
44 return MaximalRepresentableShiftAmount.
uge(MaximalPossibleTotalShiftAmount);
60 bool AnalyzeForSignBitExtraction) {
72 Value *Trunc =
nullptr;
91 if (AnalyzeForSignBitExtraction && !HadTwoRightShifts)
98 if (!IdenticalShOpcodes && !AnalyzeForSignBitExtraction)
104 if (Trunc && !AnalyzeForSignBitExtraction &&
109 auto *NewShAmt = dyn_cast_or_null<Constant>(
114 unsigned NewShAmtBitWidth = NewShAmt->getType()->getScalarSizeInBits();
115 unsigned XBitWidth =
X->getType()->getScalarSizeInBits();
118 APInt(NewShAmtBitWidth, XBitWidth))))
126 if (HadTwoRightShifts && (Trunc || AnalyzeForSignBitExtraction)) {
130 APInt(NewShAmtBitWidth, XBitWidth - 1))))
133 if (AnalyzeForSignBitExtraction)
137 assert(IdenticalShOpcodes &&
"Should not get here with different shifts.");
139 if (NewShAmt->getType() !=
X->getType()) {
141 X->getType(),
SQ.
DL);
153 if (ShiftOpcode == Instruction::BinaryOps::Shl) {
193 "The input must be 'shl'!");
195 Value *Masked, *ShiftShAmt;
208 bool HadTrunc = WidestTy != NarrowestTy;
240 MaskShAmt, ShiftShAmt,
false,
false, Q));
251 SumOfShAmts, ConstantInt::get(SumOfShAmts->getType()->getScalarType(),
254 Instruction::ZExt, SumOfShAmts, ExtendedTy, Q.
DL);
255 if (!ExtendedSumOfShAmts)
260 auto *ExtendedInvertedMask =
276 ShiftShAmt, MaskShAmt,
false,
false, Q));
288 ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(),
297 if (!ExtendedNumHighBitsToClear)
303 ExtendedNumHighBitsToClear, Q.
DL);
347 assert(
I.isShift() &&
"Expected a shift as input");
348 auto *BinInst = dyn_cast<BinaryOperator>(
I.getOperand(0));
350 (!BinInst->isBitwiseLogicOp() &&
351 BinInst->getOpcode() != Instruction::Add &&
352 BinInst->getOpcode() != Instruction::Sub) ||
353 !BinInst->hasOneUse())
362 if ((BinInst->getOpcode() == Instruction::Add ||
363 BinInst->getOpcode() == Instruction::Sub) &&
364 ShiftOpcode != Instruction::Shl)
367 Type *Ty =
I.getType();
372 auto matchFirstShift = [&](
Value *V,
Value *W) {
384 bool FirstShiftIsOp1 =
false;
385 if (matchFirstShift(BinInst->getOperand(0), BinInst->getOperand(1)))
386 Y = BinInst->getOperand(1);
387 else if (matchFirstShift(BinInst->getOperand(1), BinInst->getOperand(0))) {
388 Y = BinInst->getOperand(0);
389 FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub;
397 Value *Op1 = FirstShiftIsOp1 ? NewShift2 : NewShift1;
398 Value *Op2 = FirstShiftIsOp1 ? NewShift1 : NewShift2;
406 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
408 Type *Ty =
I.getType();
422 if (isa<Constant>(Op0))
423 if (
SelectInst *SI = dyn_cast<SelectInst>(Op1))
427 if (
Constant *CUI = dyn_cast<Constant>(Op1))
431 if (
auto *NewShift = cast_or_null<Instruction>(
443 if (
I.getOpcode() == Instruction::Shl) {
462 assert(!
AC->isZero() &&
"Expected simplify of shifted zero");
463 unsigned PosOffset = (-*AddC).getZExtValue();
465 auto isSuitableForPreShift = [PosOffset, &
I,
AC]() {
466 switch (
I.getOpcode()) {
469 case Instruction::Shl:
470 return (
I.hasNoSignedWrap() ||
I.hasNoUnsignedWrap()) &&
471 AC->eq(
AC->lshr(PosOffset).shl(PosOffset));
472 case Instruction::LShr:
473 return I.isExact() &&
AC->eq(
AC->shl(PosOffset).lshr(PosOffset));
474 case Instruction::AShr:
475 return I.isExact() &&
AC->eq(
AC->shl(PosOffset).ashr(PosOffset));
478 if (isSuitableForPreShift()) {
479 Constant *NewC = ConstantInt::get(Ty,
I.getOpcode() == Instruction::Shl
480 ?
AC->lshr(PosOffset)
481 :
AC->shl(PosOffset));
484 if (
I.getOpcode() == Instruction::Shl) {
522 const APInt *InnerShiftConst;
529 bool IsInnerShl = InnerShift->
getOpcode() == Instruction::Shl;
530 if (IsInnerShl == IsOuterShl)
536 if (*InnerShiftConst == OuterShAmt)
546 if (InnerShiftConst->
ugt(OuterShAmt) && InnerShiftConst->
ult(TypeWidth)) {
549 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
575 if (!
I)
return false;
579 if (!
I->hasOneUse())
return false;
581 switch (
I->getOpcode()) {
582 default:
return false;
583 case Instruction::And:
584 case Instruction::Or:
585 case Instruction::Xor:
590 case Instruction::Shl:
591 case Instruction::LShr:
594 case Instruction::Select: {
596 Value *TrueVal = SI->getTrueValue();
597 Value *FalseVal = SI->getFalseValue();
601 case Instruction::PHI: {
611 case Instruction::Mul: {
612 const APInt *MulConst;
614 return !IsLeftShift &&
match(
I->getOperand(1),
m_APInt(MulConst)) &&
625 bool IsInnerShl = InnerShift->
getOpcode() == Instruction::Shl;
635 auto NewInnerShift = [&](
unsigned ShAmt) {
636 InnerShift->
setOperand(1, ConstantInt::get(ShType, ShAmt));
649 if (IsInnerShl == IsOuterShl) {
651 if (InnerShAmt + OuterShAmt >= TypeWidth)
654 return NewInnerShift(InnerShAmt + OuterShAmt);
660 if (InnerShAmt == OuterShAmt) {
661 APInt Mask = IsInnerShl
665 ConstantInt::get(ShType, Mask));
666 if (
auto *AndI = dyn_cast<Instruction>(
And)) {
667 AndI->moveBefore(InnerShift);
673 assert(InnerShAmt > OuterShAmt &&
674 "Unexpected opposite direction logical shift pair");
680 return NewInnerShift(InnerShAmt - OuterShAmt);
688 if (
Constant *
C = dyn_cast<Constant>(V)) {
698 switch (
I->getOpcode()) {
700 case Instruction::And:
701 case Instruction::Or:
702 case Instruction::Xor:
710 case Instruction::Shl:
711 case Instruction::LShr:
715 case Instruction::Select:
721 case Instruction::PHI: {
728 isLeftShift, IC,
DL));
731 case Instruction::Mul: {
732 assert(!isLeftShift &&
"Unexpected shift direction!");
735 unsigned TypeWidth =
I->getType()->getScalarSizeInBits();
737 auto *
And = BinaryOperator::CreateAnd(Neg,
738 ConstantInt::get(
I->getType(), Mask));
752 case Instruction::Add:
753 return Shift.
getOpcode() == Instruction::Shl;
754 case Instruction::Or:
755 case Instruction::And:
757 case Instruction::Xor:
774 bool IsLeftShift =
I.getOpcode() == Instruction::Shl;
775 Type *Ty =
I.getType();
784 Constant *NegDivC = ConstantInt::get(Ty, -(*DivC));
788 auto ExtOpcode = (
I.getOpcode() == Instruction::AShr) ? Instruction::SExt
798 "Shift over the type width should have been removed already");
802 if (
I.getOpcode() != Instruction::AShr &&
805 dbgs() <<
"ICE: GetShiftedValue propagating shift through expression"
806 " to eliminate shift:\n IN: "
807 << *Op0 <<
"\n SH: " <<
I <<
"\n");
819 if (
auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
851 if (!isa<Constant>(FalseVal) && TBO->
getOperand(0) == FalseVal &&
868 if (!isa<Constant>(TrueVal) && FBO->
getOperand(0) == TrueVal &&
895 assert(
I.getOpcode() == Instruction::LShr);
898 Value *ShiftAmt =
I.getOperand(1);
899 Type *Ty =
I.getType();
904 const APInt *ShAmtAPInt =
nullptr;
905 Value *
X =
nullptr, *
Y =
nullptr;
916 if (
X->getType()->getScalarSizeInBits() != ShAmt ||
917 Y->getType()->getScalarSizeInBits() != ShAmt)
921 if (!
Add->hasOneUse()) {
926 TruncInst *Trunc = dyn_cast<TruncInst>(U);
944 if (!
Add->hasOneUse()) {
955 assert(
I.isShift() &&
"Expected a shift as input");
957 if (
I.getOpcode() == Instruction::Shl) {
958 if (
I.hasNoUnsignedWrap() &&
I.hasNoSignedWrap())
979 bool Changed =
false;
981 if (
I.getOpcode() == Instruction::Shl) {
984 I.setHasNoUnsignedWrap();
988 if (!
I.hasNoSignedWrap()) {
992 I.setHasNoSignedWrap();
1002 I.setIsExact(Changed);
1011 I.hasNoSignedWrap(),
I.hasNoUnsignedWrap(), Q))
1023 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1024 Type *Ty =
I.getType();
1029 unsigned ShAmtC =
C->getZExtValue();
1035 unsigned SrcWidth =
X->getType()->getScalarSizeInBits();
1036 if (ShAmtC < SrcWidth &&
1044 return BinaryOperator::CreateAnd(
X, ConstantInt::get(Ty, Mask));
1051 if (ShrAmt < ShAmtC) {
1053 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1054 auto *NewShl = BinaryOperator::CreateShl(
X, ShiftDiff);
1055 NewShl->setHasNoUnsignedWrap(
1056 I.hasNoUnsignedWrap() ||
1058 cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1059 I.hasNoSignedWrap()));
1060 NewShl->setHasNoSignedWrap(
I.hasNoSignedWrap());
1063 if (ShrAmt > ShAmtC) {
1065 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1067 cast<BinaryOperator>(Op0)->
getOpcode(),
X, ShiftDiff);
1068 NewShr->setIsExact(
true);
1076 if (ShrAmt < ShAmtC) {
1078 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt);
1079 auto *NewShl = BinaryOperator::CreateShl(
X, ShiftDiff);
1080 NewShl->setHasNoUnsignedWrap(
1081 I.hasNoUnsignedWrap() ||
1083 cast<Instruction>(Op0)->getOpcode() == Instruction::LShr &&
1084 I.hasNoSignedWrap()));
1085 NewShl->setHasNoSignedWrap(
I.hasNoSignedWrap());
1088 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1090 if (ShrAmt > ShAmtC) {
1092 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC);
1093 auto *OldShr = cast<BinaryOperator>(Op0);
1096 NewShr->setIsExact(OldShr->isExact());
1099 return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask));
1109 unsigned ShDiff = ShrAmtC > ShAmtC ? ShrAmtC - ShAmtC : ShAmtC - ShrAmtC;
1110 Constant *ShiftDiffC = ConstantInt::get(
X->getType(), ShDiff);
1111 auto ShiftOpc = ShrAmtC > ShAmtC ? Shr->
getOpcode() : Instruction::Shl;
1120 return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, Mask));
1128 return BinaryOperator::CreateShl(
X, ConstantInt::get(Ty, AmtSum));
1134 switch (BinOpcode) {
1137 case Instruction::Add:
1138 case Instruction::And:
1139 case Instruction::Or:
1140 case Instruction::Xor:
1141 case Instruction::Sub:
1149 isSuitableBinOpcode(Op0BO->
getOpcode())) {
1170 unsigned Op1Val =
C->getLimitedValue(
BitWidth);
1172 Constant *Mask = ConstantInt::get(Ty, Bits);
1173 return BinaryOperator::CreateAnd(
B, Mask);
1184 X->getName() +
".mask");
1193 return BinaryOperator::CreateSub(NewLHS, NewShift);
1206 return BinaryOperator::CreateAnd(Mask,
X);
1212 return BinaryOperator::CreateShl(
AllOnes, Op1);
1233 return BinaryOperator::CreateLShr(
1241 return BinaryOperator::CreateAnd(NegX,
X);
1259 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1260 Type *Ty =
I.getType();
1271 unsigned ShAmtC =
C->getZExtValue();
1272 auto *II = dyn_cast<IntrinsicInst>(Op0);
1274 (II->getIntrinsicID() == Intrinsic::ctlz ||
1275 II->getIntrinsicID() == Intrinsic::cttz ||
1276 II->getIntrinsicID() == Intrinsic::ctpop)) {
1280 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
1289 if (C1->
ult(ShAmtC)) {
1291 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShlAmtC);
1294 auto *NewLShr = BinaryOperator::CreateLShr(
X, ShiftDiff);
1295 NewLShr->setIsExact(
I.isExact());
1302 return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
1304 }
else if (C1->
ugt(ShAmtC)) {
1306 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC);
1309 auto *NewShl = BinaryOperator::CreateShl(
X, ShiftDiff);
1310 NewShl->setHasNoUnsignedWrap(
true);
1311 NewShl->setHasNoSignedWrap(ShAmtC > 0);
1318 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1324 return BinaryOperator::CreateAnd(
X, ConstantInt::get(Ty, Mask));
1337 unsigned Op1Val =
C->getLimitedValue(
BitWidth);
1339 Constant *Mask = ConstantInt::get(Ty, Bits);
1340 return BinaryOperator::CreateAnd(NewAdd, Mask);
1344 (!Ty->
isIntegerTy() || shouldChangeType(Ty,
X->getType()))) {
1346 "Big shift not simplified to zero?");
1353 unsigned SrcTyBitWidth =
X->getType()->getScalarSizeInBits();
1355 if (SrcTyBitWidth == 1) {
1356 auto *NewC = ConstantInt::get(
1361 if ((!Ty->
isIntegerTy() || shouldChangeType(Ty,
X->getType())) &&
1371 if (ShAmtC ==
BitWidth - SrcTyBitWidth) {
1373 unsigned NewShAmt = std::min(ShAmtC, SrcTyBitWidth - 1);
1393 return BinaryOperator::CreateAnd(Signbit,
X);
1402 return BinaryOperator::CreateLShr(
X, ConstantInt::get(Ty, AmtSum));
1408 unsigned SrcWidth =
X->getType()->getScalarSizeInBits();
1416 if (AmtSum < SrcWidth &&
1424 return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, MaskC));
1436 return BinaryOperator::CreateAnd(
X, ConstantInt::get(Ty, *MulC - 2));
1445 if (MulC->
eq(NewMulC.
shl(ShAmtC))) {
1447 BinaryOperator::CreateNUWMul(
X, ConstantInt::get(Ty, NewMulC));
1449 "lshr X, 0 should be handled by simplifyLShrInst.");
1450 NewMul->setHasNoSignedWrap(
true);
1461 unsigned SrcWidth =
X->getType()->getScalarSizeInBits();
1462 unsigned WidthDiff =
BitWidth - SrcWidth;
1463 if (SrcWidth % 16 == 0) {
1465 if (ShAmtC >= WidthDiff) {
1472 Constant *ShiftDiff = ConstantInt::get(Ty, WidthDiff - ShAmtC);
1473 return BinaryOperator::CreateShl(NewZExt, ShiftDiff);
1480 Value *BoolX, *BoolY;
1485 (
X->hasOneUse() ||
Y->hasOneUse() || Op0->
hasOneUse())) {
1499 return BinaryOperator::CreateAnd(Mask,
X);
1505 return BinaryOperator::CreateLShr(
AllOnes, Op1);
1518 "Must be called with arithmetic right-shift instruction only.");
1524 APInt(
C->getType()->getScalarSizeInBits(),
1525 V->getType()->getScalarSizeInBits())));
1533 if (!
match(&OldAShr,
1539 !BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr))
1545 bool HadTrunc = MaybeTrunc != HighBitExtract;
1548 Value *
X, *NumLowBitsToSkip;
1554 if (!
match(NumLowBitsToSkip,
1557 !BitWidthSplat(C0, HighBitExtract))
1594 Value *Op0 =
I.getOperand(0), *Op1 =
I.getOperand(1);
1595 Type *Ty =
I.getType();
1597 const APInt *ShAmtAPInt;
1606 ShAmt ==
BitWidth -
X->getType()->getScalarSizeInBits())
1615 if (ShlAmt < ShAmt) {
1617 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
1618 auto *NewAShr = BinaryOperator::CreateAShr(
X, ShiftDiff);
1619 NewAShr->setIsExact(
I.isExact());
1622 if (ShlAmt > ShAmt) {
1624 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
1626 NewShl->setHasNoSignedWrap(
true);
1635 AmtSum = std::min(AmtSum,
BitWidth - 1);
1637 return BinaryOperator::CreateAShr(
X, ConstantInt::get(Ty, AmtSum));
1641 (Ty->
isVectorTy() || shouldChangeType(Ty,
X->getType()))) {
1643 Type *SrcTy =
X->getType();
1672 Constant *Mask = ConstantInt::get(Ty, 1);
1676 cast<Constant>(cast<Instruction>(Op0)->getOperand(1)));
1686 Instruction *Lshr = BinaryOperator::CreateLShr(Op0, Op1);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file provides internal interfaces used to implement the InstCombine.
static Value * foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, bool IsOuterShl, InstCombiner::BuilderTy &Builder)
Fold OuterShift (InnerShift X, C1), C2.
static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q)
static Instruction * dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift, const SimplifyQuery &Q, InstCombiner::BuilderTy &Builder)
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, InstCombinerImpl &IC, Instruction *CxtI)
See if we can compute the specified value, but shifted logically to the left or right by some number ...
bool canTryToConstantAddTwoShiftAmounts(Value *Sh0, Value *ShAmt0, Value *Sh1, Value *ShAmt1)
static Instruction * foldShiftOfShiftedBinOp(BinaryOperator &I, InstCombiner::BuilderTy &Builder)
If we have a shift-by-constant of a bin op (bitwise logic op or add/sub w/ shl) that itself has a shi...
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, Instruction *InnerShift, InstCombinerImpl &IC, Instruction *CxtI)
Return true if we can simplify two logical (either left or right) shifts that have constant shift amo...
static Value * getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombinerImpl &IC, const DataLayout &DL)
When canEvaluateShifted() returns true for an expression, this function inserts the new computation t...
static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift, BinaryOperator *BO)
This file provides the interface for the instcombine pass implementation.
static bool hasNoUnsignedWrap(BinaryOperator &I)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static SymbolRef::Type getType(const Symbol *Sym)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Class for arbitrary precision integers.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
bool isNegative() const
Determine sign of this APInt.
bool eq(const APInt &RHS) const
Equality comparison.
unsigned countr_zero() const
Count the number of trailing zero bits.
unsigned logBase2() const
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
APInt shl(unsigned shiftAmt) const
Left-shift function.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a binary instruction, given the opcode and the two operands.
BinaryOps getOpcode() const
static BinaryOperator * CreateNeg(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static BinaryOperator * CreateNot(Value *Op, const Twine &Name, BasicBlock::iterator InsertBefore)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Create a Trunc or BitCast cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ ICMP_SLE
signed less or equal
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getNot(Constant *C)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
This is an important base class in LLVM.
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
static Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateIsNotNeg(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg > -1.
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitLShr(BinaryOperator &I)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Value * reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0, const SimplifyQuery &SQ, bool AnalyzeForSignBitExtraction=false)
Instruction * visitAShr(BinaryOperator &I)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitShl(BinaryOperator &I)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Instruction * foldVariableSignZeroExtensionOfVariableHighBitExtract(BinaryOperator &OldAShr)
Instruction * commonShiftTransforms(BinaryOperator &I)
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
void addToWorklist(Instruction *I)
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
op_range incoming_values()
void setIncomingValue(unsigned i, Value *V)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr, BasicBlock::iterator InsertBefore, Instruction *MDFrom=nullptr)
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getExtendedType() const
Given scalar/vector integer type, returns a type with elements twice as wide as in the original type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
StringRef getName() const
Return a constant reference to the value's name.
void takeName(Value *V)
Transfer the name from V to this value.
This class represents zero extension of integer types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
OneUse_match< T > m_OneUse(const T &SubPattern)
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< CastOperator_match< OpTy, Instruction::Trunc >, OpTy > m_TruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Exact_match< T > m_Exact(const T &SubPattern)
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
specific_intval< true > m_SpecificIntAllowUndef(const APInt &V)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
This is an optimization pass for GlobalISel generic memory operations.
Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
@ And
Bitwise or logical AND of integers.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
constexpr unsigned BitWidth
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
unsigned getBitWidth() const
Get the bit width of this value.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
SimplifyQuery getWithInstruction(const Instruction *I) const