20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
29 if (SimplifyDemandedInstructionBits(I))
33 if (isa<Constant>(Op0))
38 if (
Constant *CUI = dyn_cast<Constant>(Op1))
39 if (
Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
76 const APInt *InnerShiftConst;
83 bool IsInnerShl = InnerShift->
getOpcode() == Instruction::Shl;
84 if (IsInnerShl == IsOuterShl)
90 unsigned InnerShAmt = InnerShiftConst->getZExtValue();
91 if (InnerShAmt == OuterShAmt)
101 if (InnerShAmt > OuterShAmt && InnerShAmt < TypeWidth) {
103 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
125 if (isa<Constant>(V))
129 if (!I)
return false;
144 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
145 uint32_t BitWidth = Ty->getScalarSizeInBits();
149 return CanEvaluateTruncated(I->
getOperand(0), Ty);
161 default:
return false;
162 case Instruction::And:
163 case Instruction::Or:
164 case Instruction::Xor:
169 case Instruction::Shl:
170 case Instruction::LShr:
180 case Instruction::PHI: {
198 bool IsInnerShl = InnerShift->
getOpcode() == Instruction::Shl;
208 auto NewInnerShift = [&](
unsigned ShAmt) {
222 if (IsInnerShl == IsOuterShl) {
224 if (InnerShAmt + OuterShAmt >= TypeWidth)
227 return NewInnerShift(InnerShAmt + OuterShAmt);
233 if (InnerShAmt == OuterShAmt) {
239 if (
auto *AndI = dyn_cast<Instruction>(And)) {
240 AndI->moveBefore(InnerShift);
246 assert(InnerShAmt > OuterShAmt &&
247 "Unexpected opposite direction logical shift pair");
253 return NewInnerShift(InnerShAmt - OuterShAmt);
261 if (
Constant *
C = dyn_cast<Constant>(V)) {
267 if (
auto *
C = dyn_cast<Constant>(V))
279 case Instruction::And:
280 case Instruction::Or:
281 case Instruction::Xor:
289 case Instruction::Shl:
290 case Instruction::LShr:
300 case Instruction::PHI: {
307 isLeftShift, IC, DL));
315 bool isLeftShift = I.
getOpcode() == Instruction::Shl;
323 if (I.
getOpcode() != Instruction::AShr &&
325 DEBUG(
dbgs() <<
"ICE: GetShiftedValue propagating shift through expression"
326 " to eliminate shift:\n IN: " << *Op0 <<
"\n SH: " << I <<
"\n");
328 return replaceInstUsesWith(
337 "Shift over the type width should have been removed already");
339 if (
Instruction *FoldedShift = foldOpWithConstantIntoOperand(I))
343 if (
TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
363 unsigned DstSize = TI->getType()->getScalarSizeInBits();
378 Value *And = Builder.CreateAnd(NSh,
392 switch (Op0BO->getOpcode()) {
395 case Instruction::And:
396 case Instruction::Or:
397 case Instruction::Xor: {
400 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
404 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->
getName());
406 Value *
X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
407 Op0BO->getOperand(1)->
getName());
414 return BinaryOperator::CreateAnd(X, Mask);
418 Value *Op0BOOp1 = Op0BO->getOperand(1);
419 if (isLeftShift && Op0BOOp1->
hasOneUse() &&
424 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->
getName());
433 case Instruction::Sub: {
435 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
439 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->
getName());
441 Value *
X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
442 Op0BO->getOperand(0)->
getName());
449 return BinaryOperator::CreateAnd(X, Mask);
453 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
454 match(Op0BO->getOperand(0),
458 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->
getName());
473 if (
ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
475 bool highBitSet =
false;
477 switch (Op0BO->getOpcode()) {
478 default: isValid =
false;
break;
480 isValid = isLeftShift;
482 case Instruction::Or:
483 case Instruction::Xor:
486 case Instruction::And:
497 if (isValid && I.
getOpcode() == Instruction::AShr)
498 isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
504 Builder.CreateBinOp(I.
getOpcode(), Op0BO->getOperand(0), Op1);
518 if (
Value *V = SimplifyVectorOp(I))
519 return replaceInstUsesWith(I, V);
524 SQ.getWithInstruction(&I)))
525 return replaceInstUsesWith(I, V);
530 const APInt *ShAmtAPInt;
541 if (ShAmt < SrcWidth &&
543 return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
560 if (ShrAmt < ShAmt) {
563 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
568 if (ShrAmt > ShAmt) {
572 cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
573 NewShr->setIsExact(
true);
581 if (AmtSum < BitWidth)
617 if (
Value *V = SimplifyVectorOp(I))
618 return replaceInstUsesWith(I, V);
623 return replaceInstUsesWith(I, V);
629 const APInt *ShAmtAPInt;
635 (II->getIntrinsicID() == Intrinsic::ctlz ||
636 II->getIntrinsicID() == Intrinsic::cttz ||
637 II->getIntrinsicID() == Intrinsic::ctpop)) {
643 Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
651 if (ShlAmt < ShAmt) {
653 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
655 auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
656 NewLShr->setIsExact(I.
isExact());
660 Value *NewLShr = Builder.CreateLShr(X, ShiftDiff,
"", I.
isExact());
664 if (ShlAmt > ShAmt) {
666 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
668 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
669 NewShl->setHasNoUnsignedWrap(
true);
673 Value *NewShl = Builder.CreateShl(X, ShiftDiff);
687 if (ShAmt == BitWidth - 1) {
689 if (SrcTyBitWidth == 1)
694 Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
700 if (ShAmt == BitWidth - SrcTyBitWidth && Op0->
hasOneUse()) {
702 unsigned NewShAmt =
std::min(ShAmt, SrcTyBitWidth - 1);
703 Value *AShr = Builder.CreateAShr(X, NewShAmt);
711 if (AmtSum < BitWidth)
727 if (
Value *V = SimplifyVectorOp(I))
728 return replaceInstUsesWith(I, V);
733 return replaceInstUsesWith(I, V);
740 const APInt *ShAmtAPInt;
757 if (ShlAmt < ShAmt) {
760 auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
761 NewAShr->setIsExact(I.
isExact());
764 if (ShlAmt > ShAmt) {
768 NewShl->setHasNoSignedWrap(
true);
776 AmtSum =
std::min(AmtSum, BitWidth - 1);
791 return BinaryOperator::CreateLShr(Op0, Op1);
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
A parsed version of the target data layout string in and methods for querying it. ...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
uint64_t getZExtValue() const
Get zero extended value.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
This class represents zero extension of integer types.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, InstCombiner &IC, Instruction *CxtI)
See if we can compute the specified value, but shifted logically to the left or right by some number ...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
void Add(Instruction *I)
Add - Add the specified instruction to the worklist if it isn't already in it.
This class represents a sign extension of integer types.
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
bool match(Val *V, const Pattern &P)
This class represents the LLVM 'select' instruction.
Instruction * commonShiftTransforms(BinaryOperator &I)
Exact_match< T > m_Exact(const T &SubPattern)
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...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The core instruction combiner logic.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
static Constant * getZExt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
TargetLibraryInfo & getTargetLibraryInfo() const
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power of 2.
void takeName(Value *V)
Transfer the name from V to this value.
This class represents a truncation of integer types.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
bool isExact() const
Determine whether the exact flag is set.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isKnownNonNegative(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Returns true if the give value is known to be non-negative.
This is an important base class in LLVM.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if 'V & Mask' is known to be zero.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Value * getOperand(unsigned i) const
Instruction * visitLShr(BinaryOperator &I)
LLVMContext & getContext() const
All values hold a context through their type.
const Value * getTrueValue() const
bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth=0, const Instruction *CxtI=nullptr) const
InstCombineWorklist & Worklist
A worklist of the instructions that need to be simplified.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
BinaryOps getOpcode() const
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This is the shared class of boolean and integer constants.
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Type * getType() const
All values are typed, get the type of this value.
static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, Instruction *InnerShift, InstCombiner &IC, Instruction *CxtI)
Return true if we can simplify two logical (either left or right) shifts that have constant shift amo...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Class to represent vector types.
Class for arbitrary precision integers.
bool isIntegerTy() const
True if this is an instance of IntegerType.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
llvm_unreachable("Unknown RegBank!")
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
bool hasOneUse() const
Return true if there is exactly one user of this value.
static Value * getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, InstCombiner &IC, const DataLayout &DL)
When canEvaluateShifted() returns true for an expression, this function inserts the new computation t...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
INITIALIZE_PASS(HexagonGenMux,"hexagon-gen-mux","Hexagon generate mux instructions", false, false) void HexagonGenMux I isValid()
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Value * foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, bool IsOuterShl, InstCombiner::BuilderTy &Builder)
Fold OuterShift (InnerShift X, C1), C2.
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
const Value * getFalseValue() const
Instruction * visitAShr(BinaryOperator &I)
void setIncomingValue(unsigned i, Value *V)
Instruction * visitShl(BinaryOperator &I)
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
op_range incoming_values()
Instruction * FoldShiftByConstant(Value *Op0, Constant *Op1, BinaryOperator &I)
A wrapper class for inspecting calls to intrinsic functions.