15 #ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16 #define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
35 #define DEBUG_TYPE "instcombine"
41 class TargetLibraryInfo;
64 if (isa<Instruction>(V)) {
72 return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
100 if (
auto *BitCast = dyn_cast<BitCastInst>(V))
101 if (!OneUseOnly || BitCast->hasOneUse())
102 return BitCast->getOperand(0);
128 if (isa<ConstantInt>(V))
134 for (
unsigned i = 0; i != NumElts; ++i) {
135 Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
139 if (isa<UndefValue>(Elt))
142 if (!isa<ConstantInt>(Elt))
151 return WillInvertAllUses;
157 BO->getOpcode() == Instruction::Sub)
158 if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
159 return WillInvertAllUses;
184 case Intrinsic::uadd_with_overflow:
186 case Intrinsic::sadd_with_overflow:
188 case Intrinsic::usub_with_overflow:
190 case Intrinsic::ssub_with_overflow:
192 case Intrinsic::umul_with_overflow:
194 case Intrinsic::smul_with_overflow:
204 :
public InstVisitor<InstCombiner, Instruction *> {
217 const bool MinimizeSize;
219 const bool ExpensiveCombines;
237 bool MinimizeSize,
bool ExpensiveCombines,
AliasAnalysis *AA,
240 : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
241 ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
242 DL(DL), SQ(DL, &TLI, &DT, &AC), LI(LI), MadeIRChange(
false) {}
350 const unsigned SIOpd);
358 bool shouldChangeType(
unsigned FromBitWidth,
unsigned ToBitWidth)
const;
359 bool shouldChangeType(
Type *From,
Type *To)
const;
361 Value *dyn_castFNegVal(
Value *V,
bool NoSignedZero =
false)
const;
374 bool shouldOptimizeCast(
CastInst *CI);
394 bool transformConstExprCastCall(
CallSite CS);
411 bool DoTransform =
true);
414 bool willNotOverflowSignedAdd(
const Value *LHS,
const Value *RHS,
419 bool willNotOverflowUnsignedAdd(
const Value *LHS,
const Value *RHS,
420 const Instruction &CxtI)
const {
424 bool willNotOverflowSignedSub(
const Value *LHS,
const Value *RHS,
425 const Instruction &CxtI)
const;
426 bool willNotOverflowUnsignedSub(
const Value *LHS,
const Value *RHS,
427 const Instruction &CxtI)
const;
428 bool willNotOverflowSignedMul(
const Value *LHS,
const Value *RHS,
429 const Instruction &CxtI)
const;
430 bool willNotOverflowUnsignedMul(
const Value *LHS,
const Value *RHS,
431 const Instruction &CxtI)
const {
436 Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
437 Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int>
Mask);
438 Instruction *foldCastedBitwiseLogic(BinaryOperator &
I);
439 Instruction *shrinkBitwiseLogic(TruncInst &Trunc);
440 Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
453 const CastInst *CI2);
455 Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
456 Value *foldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
457 Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
458 Value *foldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
459 Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS);
461 Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
462 bool JoinedByAnd, Instruction &CxtI);
470 "New instruction already inserted into a basic block!");
480 return InsertNewInstBefore(New, Old);
494 Worklist.AddUsersToWorkList(I);
501 DEBUG(
dbgs() <<
"IC: Replacing " << I <<
"\n"
502 <<
" with " << *V <<
'\n');
524 DEBUG(
dbgs() <<
"IC: ERASE " << I <<
'\n');
532 if (
auto *Inst = dyn_cast<Instruction>(Operand))
622 const APInt &DemandedMask,
627 Value *simplifyShrShlDemandedBits(
633 bool SimplifyDemandedInstructionBits(
Instruction &Inst);
738 bool isSigned,
bool Inside);
741 bool SimplifyStoreAtEndOfBlock(
StoreInst &SI);
748 Value *EvaluateInDifferentType(
Value *V,
Type *Ty,
bool isSigned);
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Same as InsertNewInstBefore, but also sets the debug loc.
A parsed version of the target data layout string in and methods for querying it. ...
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to have exactly one bit set when defined. ...
This class is the base class for the comparison instructions.
InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder, bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT, const DataLayout &DL, LoopInfo *LI)
static bool IsFreeToInvert(Value *V, bool WillInvertAllUses)
Return true if the specified value is free to invert (apply ~ to).
Base class for instruction visitors.
An instruction for ordering other memory operations.
This class represents zero extension of integer types.
unsigned getNumOperands() const
This class represents a function call, abstracting a target machine's calling convention.
static bool isCanonicalPredicate(CmpInst::Predicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
const DataLayout & getDataLayout() const
A cache of .assume calls within a function.
This instruction constructs a fixed permutation of two input vectors.
This class wraps the llvm.memset intrinsic.
This class represents a sign extension of integer types.
An instruction for reading from memory.
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static OverflowCheckFlavor IntrinsicIDToOverflowCheckFlavor(unsigned ID)
Returns the OverflowCheckFlavor corresponding to a overflow_with_op intrinsic.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
This class represents a conversion between pointers from one address space to another.
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
This class represents the LLVM 'select' instruction.
OverflowCheckFlavor
Specific patterns of overflow check idioms that we match.
This is the base class for all instructions that perform data casts.
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
Instruction * eraseInstFromFunction(Instruction &I)
Combiner aware instruction erasure.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The core instruction combiner logic.
static Constant * AddOne(Constant *C)
Add one to a Constant.
0 1 0 1 True if ordered and less than or equal
unsigned ComputeNumSignBits(const Value *Op, unsigned Depth=0, const Instruction *CxtI=nullptr) const
This class represents a cast from a pointer to an integer.
static Value * peekThroughBitcast(Value *V, bool OneUseOnly=false)
Return the source operand of a potentially bitcasted value while optionally checking if it has one us...
This represents the llvm.va_start intrinsic.
void salvageDebugInfo(Instruction &I)
Assuming the instruction I is going to be deleted, attempt to salvage any dbg.value intrinsics referr...
This instruction compares its operands according to the predicate given to the constructor.
TargetLibraryInfo & getTargetLibraryInfo() const
This class represents a no-op cast from one type to another.
An instruction for storing to memory.
Instruction * visitInstruction(Instruction &I)
Specify what to return for unhandled instructions.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
This class represents a cast from floating point to signed integer.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
This class represents a truncation of integer types.
Class to represent pointers.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Function Alias Analysis false
This instruction inserts a single (scalar) element into a VectorType value.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Conditional or Unconditional Branch instruction.
bool isVectorTy() const
True if this is an instance of VectorType.
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
This is an important base class in LLVM.
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero=false, unsigned Depth=0, const Instruction *CxtI=nullptr)
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.
OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
const InstListType & getInstList() const
Return the underlying instruction list container.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Instruction * CreateOverflowTuple(IntrinsicInst *II, Value *Result, Constant *Overflow)
Creates a result tuple for an overflow intrinsic II with a given Result and a constant Overflow value...
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
static bool isNot(const Value *V)
self_iterator getIterator()
static Constant * get(StructType *T, ArrayRef< Constant * > V)
DominatorTree & getDominatorTree() const
This class represents a cast from an integer to a pointer.
InstCombineWorklist - This is the worklist management logic for InstCombine.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
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.
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
LoopInfo * getLoopInfo() const
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...
Type * getType() const
All values are typed, get the type of this value.
SelectPatternFlavor
Specific patterns of select instructions we can match.
Provides information about what library functions are available for the current target.
This class represents a cast from floating point to unsigned integer.
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.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
This class represents atomic memcpy intrinsic TODO: Integrate this class into MemIntrinsic hierarchy;...
KnownBits computeKnownBits(const Value *V, unsigned Depth, const Instruction *CxtI) const
static bool isNeg(const Value *V)
Check if the given Value is a NEG, FNeg, or NOT instruction.
Class for arbitrary precision integers.
unsigned getVectorNumElements() const
iterator insert(iterator where, pointer New)
unsigned greater or equal
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
AssumptionCache & getAssumptionCache() const
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
0 1 1 0 True if ordered and operands are unequal
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Inserts an instruction New before instruction Old.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
This represents the llvm.va_copy intrinsic.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
This class represents a truncation of floating point types.
LLVM Value Representation.
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.
OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, const Value *RHS, const Instruction *CxtI) const
0 0 1 1 True if ordered and greater than or equal
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
const BasicBlock * getParent() const
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
A wrapper class for inspecting calls to intrinsic functions.
an instruction to allocate memory on the stack
This instruction inserts a struct field of array element value into an aggregate value.