20 #define DEBUG_TYPE "instcombine"
26 assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
34 bool isNUW =
false, isNSW =
false, isExact =
false;
36 dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
37 isNUW = BO->hasNoUnsignedWrap();
38 isNSW = BO->hasNoSignedWrap();
40 dyn_cast<PossiblyExactOperator>(FirstInst))
41 isExact = PEO->isExact();
54 if (
CmpInst *CI = dyn_cast<CmpInst>(I))
55 if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate())
59 isNUW = cast<OverflowingBinaryOperator>(
I)->hasNoUnsignedWrap();
61 isNSW = cast<OverflowingBinaryOperator>(
I)->hasNoSignedWrap();
63 isExact = cast<PossiblyExactOperator>(
I)->isExact();
66 if (I->
getOperand(0) != LHSVal) LHSVal =
nullptr;
67 if (I->
getOperand(1) != RHSVal) RHSVal =
nullptr;
74 if (!LHSVal && !RHSVal)
81 PHINode *NewLHS =
nullptr, *NewRHS =
nullptr;
99 if (NewLHS || NewRHS) {
113 if (
CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) {
124 if (isNSW) NewBinOp->setHasNoSignedWrap();
125 if (isExact) NewBinOp->setIsExact();
137 bool AllBasePointersAreAllocas =
true;
142 bool NeededPhi =
false;
144 bool AllInBounds =
true;
156 if (AllBasePointersAreAllocas &&
159 AllBasePointersAreAllocas =
false;
185 FixedOperands[
op] =
nullptr;
196 if (AllBasePointersAreAllocas)
203 bool HasAnyPHIs =
false;
204 for (
unsigned i = 0, e = FixedOperands.size(); i != e; ++i) {
205 if (FixedOperands[i])
continue;
212 OperandPhis[i] = NewPN;
213 FixedOperands[i] = NewPN;
224 for (
unsigned op = 0, e = OperandPhis.size();
op != e; ++
op)
226 OpPhi->addIncoming(InGEP->
getOperand(op), InBB);
230 Value *Base = FixedOperands[0];
250 for (++BBI; BBI != E; ++BBI)
251 if (BBI->mayWriteToMemory())
257 bool isAddressTaken =
false;
259 if (isa<LoadInst>(U))
continue;
262 if (
SI->getOperand(1) == AI)
continue;
264 isAddressTaken =
true;
268 if (!isAddressTaken && AI->isStaticAlloca())
356 if (NewInVal != InVal)
377 cast<LoadInst>(IncValue)->setVolatile(
false);
392 if (isa<GetElementPtrInst>(FirstInst))
393 return FoldPHIArgGEPIntoPHI(PN);
394 if (isa<LoadInst>(FirstInst))
395 return FoldPHIArgLoadIntoPHI(PN);
402 Type *CastSrcTy =
nullptr;
403 bool isNUW =
false, isNSW =
false, isExact =
false;
405 if (isa<CastInst>(FirstInst)) {
411 if (!ShouldChangeType(PN.
getType(), CastSrcTy))
414 }
else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
419 return FoldPHIArgBinOpIntoPHI(PN);
422 dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
423 isNUW = BO->hasNoUnsignedWrap();
424 isNSW = BO->hasNoSignedWrap();
426 dyn_cast<PossiblyExactOperator>(FirstInst))
427 isExact = PEO->isExact();
445 isNUW = cast<OverflowingBinaryOperator>(
I)->hasNoUnsignedWrap();
447 isNSW = cast<OverflowingBinaryOperator>(
I)->hasNoSignedWrap();
449 isExact = cast<PossiblyExactOperator>(
I)->isExact();
464 if (NewInVal != InVal)
481 if (
CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
488 if (
BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
491 if (isNSW) BinOp->setHasNoSignedWrap();
492 if (isExact) BinOp->setIsExact();
497 CmpInst *CIOp = cast<CmpInst>(FirstInst);
511 if (!PotentiallyDeadPHIs.
insert(PN).second)
515 if (PotentiallyDeadPHIs.
size() == 16)
530 if (!ValueEqualPHIs.
insert(PN).second)
534 if (ValueEqualPHIs.
size() == 16)
540 if (
PHINode *OpPN = dyn_cast<PHINode>(Op)) {
543 }
else if (Op != NonPhiInVal)
552 struct PHIUsageRecord {
558 : PHIId(pn), Shift(Sh), Inst(User) {}
560 bool operator<(
const PHIUsageRecord &RHS)
const {
561 if (PHIId < RHS.PHIId)
return true;
562 if (PHIId > RHS.PHIId)
return false;
563 if (Shift < RHS.Shift)
return true;
564 if (Shift > RHS.Shift)
return false;
565 return Inst->getType()->getPrimitiveSizeInBits() <
566 RHS.Inst->getType()->getPrimitiveSizeInBits();
570 struct LoweredPHIRecord {
575 LoweredPHIRecord(
PHINode *pn,
unsigned Sh,
Type *Ty)
576 : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
579 LoweredPHIRecord(
PHINode *pn,
unsigned Sh)
580 : PN(pn), Shift(Sh), Width(0) {}
588 return LoweredPHIRecord(
nullptr, 0);
591 return LoweredPHIRecord(
nullptr, 1);
597 static bool isEqual(
const LoweredPHIRecord &LHS,
598 const LoweredPHIRecord &RHS) {
599 return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
600 LHS.Width == RHS.Width;
627 PHIsInspected.
insert(&FirstPhi);
629 for (
unsigned PHIId = 0; PHIId != PHIsToSlice.
size(); ++PHIId) {
630 PHINode *PN = PHIsToSlice[PHIId];
652 if (
PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
653 if (PHIsInspected.
insert(UserPN).second)
659 if (isa<TruncInst>(UserI)) {
660 PHIUsers.
push_back(PHIUsageRecord(PHIId, 0, UserI));
665 if (UserI->
getOpcode() != Instruction::LShr ||
670 unsigned Shift = cast<ConstantInt>(UserI->
getOperand(1))->getZExtValue();
676 if (PHIUsers.
empty())
683 DEBUG(
dbgs() <<
"SLICING UP PHI: " << FirstPhi <<
'\n';
684 for (
unsigned i = 1, e = PHIsToSlice.
size(); i != e; ++i)
685 dbgs() <<
"AND USER PHI #" << i <<
": " << *PHIsToSlice[i] <<
'\n';
696 for (
unsigned UserI = 0, UserE = PHIUsers.
size(); UserI != UserE; ++UserI) {
697 unsigned PHIId = PHIUsers[UserI].PHIId;
698 PHINode *PN = PHIsToSlice[PHIId];
699 unsigned Offset = PHIUsers[UserI].Shift;
700 Type *Ty = PHIUsers[UserI].Inst->getType();
706 if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) ==
nullptr) {
712 "Truncate didn't shrink phi?");
716 Value *&PredVal = PredValues[Pred];
732 if (
PHINode *InPHI = dyn_cast<PHINode>(PN)) {
735 if (
Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
757 if (PHIsInspected.
count(OldInVal)) {
758 unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
759 OldInVal)-PHIsToSlice.begin();
760 PHIUsers.
push_back(PHIUsageRecord(RefPHIId, Offset,
761 cast<Instruction>(Res)));
767 DEBUG(
dbgs() <<
" Made element PHI for offset " << Offset <<
": "
769 ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
779 for (
unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
807 if (
PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
809 PotentiallyDeadPHIs.
insert(&PN);
821 (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
836 while (InValNo != NumIncomingVals &&
840 if (InValNo != NumIncomingVals) {
845 for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
847 if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
854 if (InValNo == NumIncomingVals) {
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
void push_back(const T &Elt)
This class is the base class for the comparison instructions.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
Type * getSourceElementType() const
unsigned getNumOperands() const
static LoweredPHIRecord getEmptyKey()
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
LoadInst - an instruction for reading from memory.
static bool isSafeAndProfitableToSinkLoad(LoadInst *L)
Return true if we know that it is safe to sink the load out of the block that defines it...
StringRef getName() const
Return a constant reference to the value's name.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, SmallPtrSetImpl< PHINode * > &ValueEqualPHIs)
Return true if this phi node is always equal to NonPhiInVal.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
void setIsInBounds(bool b=true)
setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
Instruction * SliceUpIllegalIntegerPHI(PHINode &PN)
This is an integer PHI and we know that it has an illegal type: see if it is only used by trunc or tr...
static bool isEqual(const LoweredPHIRecord &LHS, const LoweredPHIRecord &RHS)
static bool DeadPHICycle(PHINode *PN, SmallPtrSetImpl< PHINode * > &PotentiallyDeadPHIs)
Return true if this PHI node is only used by a PHI node cycle that is dead.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
StoreInst - an instruction for storing to memory.
bool isAtomic() const
isAtomic - Return true if this instruction has an AtomicOrdering of unordered or higher.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
void setDebugLoc(DebugLoc Loc)
setDebugLoc - 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...
This is an important base class in LLVM.
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed...
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
Value * getOperand(unsigned i) const
Predicate getPredicate() const
Return the predicate for this instruction.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
bool hasAllConstantIndices() const
hasAllConstantIndices - Return true if all of the indices of this GEP are constant integers...
Instruction * visitPHINode(PHINode &PN)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
BinaryOps getOpcode() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Instruction * user_back()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
bool isVolatile() const
isVolatile - Return true if this is a load from a volatile memory location.
SequentialType * getType() const
static LoweredPHIRecord getTombstoneKey()
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 PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isIntegerTy() const
isIntegerTy - 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.
iterator_range< user_iterator > users()
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
bool isLegalInteger(unsigned Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU...
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool hasOneUse() const
Return true if there is exactly one user of this value.
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Inserts an instruction New before instruction Old.
OtherOps getOpcode() const
Get the opcode casted to the right type.
bool operator<(int64_t V1, const APSInt &V2)
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - Return the basic size of this type if it is a primitive type.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static unsigned getHashValue(const LoweredPHIRecord &Val)
InvokeInst - Invoke instruction.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
static bool isVolatile(Instruction *Inst)
void setIncomingValue(unsigned i, Value *V)
op_range incoming_values()
int getBasicBlockIndex(const BasicBlock *BB) const
getBasicBlockIndex - Return the first index of the specified basic block in the value list for this P...
const BasicBlock * getParent() const
AllocaInst - an instruction to allocate memory on the stack.