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();
74 if (!LHSVal && !RHSVal)
81 PHINode *NewLHS = 0, *NewRHS = 0;
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;
162 for (
unsigned op = 0, e = FirstInst->
getNumOperands(); op != e; ++op) {
171 if (isa<ConstantInt>(FirstInst->
getOperand(op)) ||
185 FixedOperands[op] = 0;
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)
225 if (
PHINode *OpPhi = OperandPhis[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;
261 if (isa<LoadInst>(U))
continue;
262 if (
StoreInst *SI = dyn_cast<StoreInst>(U)) {
264 if (SI->getOperand(1) == AI)
continue;
266 isAddressTaken =
true;
270 if (!isAddressTaken && AI->isStaticAlloca())
336 LoadAlignment = std::min(LoadAlignment, LI->
getAlignment());
358 if (NewInVal != InVal)
394 if (isa<GetElementPtrInst>(FirstInst))
395 return FoldPHIArgGEPIntoPHI(PN);
396 if (isa<LoadInst>(FirstInst))
397 return FoldPHIArgLoadIntoPHI(PN);
405 bool isNUW =
false, isNSW =
false, isExact =
false;
407 if (isa<CastInst>(FirstInst)) {
413 if (!ShouldChangeType(PN.
getType(), CastSrcTy))
416 }
else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
421 return FoldPHIArgBinOpIntoPHI(PN);
424 dyn_cast<OverflowingBinaryOperator>(FirstInst)) {
425 isNUW = BO->hasNoUnsignedWrap();
426 isNSW = BO->hasNoSignedWrap();
428 dyn_cast<PossiblyExactOperator>(FirstInst))
429 isExact = PEO->isExact();
447 isNUW = cast<OverflowingBinaryOperator>(
I)->hasNoUnsignedWrap();
449 isNSW = cast<OverflowingBinaryOperator>(
I)->hasNoSignedWrap();
451 isExact = cast<PossiblyExactOperator>(
I)->isExact();
466 if (NewInVal != InVal)
483 if (
CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
490 if (
BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
499 CmpInst *CIOp = cast<CmpInst>(FirstInst);
514 if (!PotentiallyDeadPHIs.
insert(PN))
518 if (PotentiallyDeadPHIs.
size() == 16)
533 if (!ValueEqualPHIs.
insert(PN))
537 if (ValueEqualPHIs.
size() == 16)
544 if (
PHINode *OpPN = dyn_cast<PHINode>(Op)) {
547 }
else if (Op != NonPhiInVal)
556 struct PHIUsageRecord {
562 : PHIId(pn), Shift(Sh), Inst(User) {}
564 bool operator<(
const PHIUsageRecord &RHS)
const {
565 if (PHIId < RHS.PHIId)
return true;
566 if (PHIId > RHS.PHIId)
return false;
567 if (Shift < RHS.Shift)
return true;
568 if (Shift > RHS.Shift)
return false;
569 return Inst->getType()->getPrimitiveSizeInBits() <
570 RHS.Inst->getType()->getPrimitiveSizeInBits();
574 struct LoweredPHIRecord {
579 LoweredPHIRecord(
PHINode *pn,
unsigned Sh,
Type *Ty)
580 : PN(pn), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
583 LoweredPHIRecord(
PHINode *pn,
unsigned Sh)
584 : PN(pn), Shift(Sh), Width(0) {}
592 return LoweredPHIRecord(0, 0);
595 return LoweredPHIRecord(0, 1);
601 static bool isEqual(
const LoweredPHIRecord &LHS,
602 const LoweredPHIRecord &RHS) {
603 return LHS.PN == RHS.PN && LHS.Shift == RHS.Shift &&
604 LHS.Width == RHS.Width;
631 PHIsInspected.
insert(&FirstPhi);
633 for (
unsigned PHIId = 0; PHIId != PHIsToSlice.
size(); ++PHIId) {
634 PHINode *PN = PHIsToSlice[PHIId];
642 if (II == 0)
continue;
658 if (
PHINode *UserPN = dyn_cast<PHINode>(User)) {
659 if (PHIsInspected.
insert(UserPN))
665 if (isa<TruncInst>(User)) {
666 PHIUsers.
push_back(PHIUsageRecord(PHIId, 0, User));
671 if (User->
getOpcode() != Instruction::LShr ||
676 unsigned Shift = cast<ConstantInt>(User->
getOperand(1))->getZExtValue();
682 if (PHIUsers.
empty())
689 DEBUG(
dbgs() <<
"SLICING UP PHI: " << FirstPhi <<
'\n';
690 for (
unsigned i = 1, e = PHIsToSlice.
size(); i != e; ++i)
691 dbgs() <<
"AND USER PHI #" << i <<
": " << *PHIsToSlice[i] <<
'\n';
702 for (
unsigned UserI = 0, UserE = PHIUsers.
size(); UserI != UserE; ++UserI) {
703 unsigned PHIId = PHIUsers[UserI].PHIId;
704 PHINode *PN = PHIsToSlice[PHIId];
705 unsigned Offset = PHIUsers[UserI].Shift;
706 Type *Ty = PHIUsers[UserI].Inst->getType();
712 if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)]) == 0) {
718 "Truncate didn't shrink phi?");
722 Value *&PredVal = PredValues[Pred];
738 if (
PHINode *InPHI = dyn_cast<PHINode>(PN)) {
741 if (
Value *Res = ExtractedVals[LoweredPHIRecord(InPHI, Offset, Ty)]) {
763 if (PHIsInspected.
count(OldInVal)) {
764 unsigned RefPHIId = std::find(PHIsToSlice.begin(),PHIsToSlice.end(),
765 OldInVal)-PHIsToSlice.begin();
766 PHIUsers.
push_back(PHIUsageRecord(RefPHIId, Offset,
767 cast<Instruction>(Res)));
773 DEBUG(
dbgs() <<
" Made element PHI for offset " << Offset <<
": "
775 ExtractedVals[LoweredPHIRecord(PN, Offset, Ty)] = EltPHI;
785 for (
unsigned i = 1, e = PHIsToSlice.size(); i != e; ++i)
813 if (
PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
815 PotentiallyDeadPHIs.
insert(&PN);
827 (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) &&
842 while (InValNo != NumIncomingVals &&
846 if (InValNo != NumIncomingVals) {
851 for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
853 if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
860 if (InValNo == NumIncomingVals) {
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
void push_back(const T &Elt)
void setHasNoSignedWrap(bool b=true)
Abstract base class of comparison instructions.
void addIncoming(Value *V, BasicBlock *BB)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getNumOperands() const
static LoweredPHIRecord getEmptyKey()
void operator<(const Optional< T > &X, const Optional< U > &Y)
Poison comparison between two Optional objects. Clients needs to explicitly compare the underlying va...
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
LoopInfoBase< BlockT, LoopT > * LI
static bool isSafeAndProfitableToSinkLoad(LoadInst *L)
StringRef getName() const
static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, SmallPtrSet< PHINode *, 16 > &ValueEqualPHIs)
Base class of casting instructions.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void setHasNoUnsignedWrap(bool b=true)
void setIsInBounds(bool b=true)
Instruction * SliceUpIllegalIntegerPHI(PHINode &PN)
static bool isEqual(const LoweredPHIRecord &LHS, const LoweredPHIRecord &RHS)
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
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
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Construct any of the CastInst subclasses.
unsigned getNumSuccessors() const
void array_pod_sort(IteratorTy Start, IteratorTy End)
LLVM Basic Block Representation.
LLVM Constant Representation.
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
BasicBlock * getIncomingBlock(unsigned i) const
Value * getOperand(unsigned i) const
Predicate getPredicate() const
Return the predicate for this instruction.
static UndefValue * get(Type *T)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool hasAllConstantIndices() const
Instruction * visitPHINode(PHINode &PN)
void setIsExact(bool b=true)
BinaryOps getOpcode() const
static bool DeadPHICycle(PHINode *PN, SmallPtrSet< PHINode *, 16 > &PotentiallyDeadPHIs)
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
SequentialType * getType() const
static LoweredPHIRecord getTombstoneKey()
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=0)
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
static CmpInst * Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=0)
Create a CmpInst.
bool isLegalInteger(unsigned Width) const
unsigned getAlignment() const
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=0)
unsigned getPrimitiveSizeInBits() const
OtherOps getOpcode() const
Get the opcode casted to the right type.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
LLVM Value Representation.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
static unsigned getHashValue(const LoweredPHIRecord &Val)
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
Determine if one instruction is the same operation as another.
void setIncomingValue(unsigned i, Value *V)
int getBasicBlockIndex(const BasicBlock *BB) const
const BasicBlock * getParent() const