22 #define DEBUG_TYPE "ppc-loop-preinc-prep"
62 cl::desc(
"Potential PHI threshold for PPC preinc loop prep"));
93 bool runOnLoop(
Loop *
L);
94 void simplifyLoopLatch(
Loop *
L);
95 bool rotateLoop(
Loop *
L);
108 static const char *
name =
"Prepare loop for pre-inc. addressing modes";
115 return new PPCLoopPreIncPrep(TM);
120 struct BucketElement {
130 Elements(1, BucketElement(I)) {}
132 const SCEV *BaseSCEV;
139 Value *StrippedBasePtr = BasePtr;
140 while (
BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
141 StrippedBasePtr = BC->getOperand(0);
143 return GEP->isInBounds();
149 if (
LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
150 return LMemI->getPointerOperand();
151 }
else if (
StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
152 return SMemI->getPointerOperand();
153 }
else if (
IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) {
155 return IMemI->getArgOperand(0);
161 bool PPCLoopPreIncPrep::runOnFunction(
Function &
F) {
165 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
166 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
167 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
168 DT = DTWP ? &DTWP->getDomTree() :
nullptr;
169 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
171 bool MadeChange =
false;
173 for (
auto I = LI->begin(),
IE = LI->end();
I !=
IE; ++
I)
175 MadeChange |= runOnLoop(*
L);
180 bool PPCLoopPreIncPrep::runOnLoop(
Loop *
L) {
181 bool MadeChange =
false;
187 DEBUG(
dbgs() <<
"PIP: Examining: " << *L <<
"\n");
194 unsigned HeaderLoopPredCount =
206 if (
LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
208 PtrValue = LMemI->getPointerOperand();
209 }
else if (
StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
211 PtrValue = SMemI->getPointerOperand();
212 }
else if (
IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(J)) {
215 PtrValue = IMemI->getArgOperand(0);
231 const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
232 if (
const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV)) {
233 if (LARSCEV->getLoop() !=
L)
239 bool FoundBucket =
false;
240 for (
auto &
B : Buckets) {
241 const SCEV *Diff = SE->getMinusSCEV(LSCEV,
B.BaseSCEV);
242 if (
const auto *CDiff = dyn_cast<SCEVConstant>(Diff)) {
243 B.Elements.push_back(BucketElement(CDiff, MemI));
252 Buckets.push_back(Bucket(LSCEV, MemI));
264 if (!LoopPredecessor ||
270 if (!LoopPredecessor)
273 DEBUG(
dbgs() <<
"PIP: Found " << Buckets.size() <<
" buckets\n");
276 for (
unsigned i = 0, e = Buckets.size(); i != e; ++i) {
289 for (
int j = 0, je = Buckets[i].Elements.size(); j != je; ++j) {
290 if (
auto *II = dyn_cast<IntrinsicInst>(Buckets[i].Elements[j].Instr))
300 if (!Buckets[i].Elements[j].
Offset ||
301 Buckets[i].Elements[j].
Offset->isZero())
304 const SCEV *
Offset = Buckets[i].Elements[j].Offset;
305 Buckets[i].BaseSCEV = SE->getAddExpr(Buckets[i].BaseSCEV, Offset);
306 for (
auto &
E : Buckets[i].Elements) {
308 E.Offset = cast<SCEVConstant>(SE->getMinusSCEV(
E.Offset, Offset));
310 E.Offset = cast<SCEVConstant>(SE->getNegativeSCEV(Offset));
313 std::swap(Buckets[i].Elements[j], Buckets[i].Elements[0]);
318 cast<SCEVAddRecExpr>(Buckets[i].BaseSCEV);
322 DEBUG(
dbgs() <<
"PIP: Transforming: " << *BasePtrSCEV <<
"\n");
324 "AddRec for the wrong loop?");
328 Instruction *MemI = Buckets[i].Elements.begin()->Instr;
330 assert(BasePtr &&
"No pointer operand");
337 if (!SE->isLoopInvariant(BasePtrStartSCEV, L))
344 BasePtrStartSCEV = SE->getMinusSCEV(BasePtrStartSCEV, BasePtrIncSCEV);
348 DEBUG(
dbgs() <<
"PIP: New start is: " << *BasePtrStartSCEV <<
"\n");
351 MemI->hasName() ? MemI->getName() +
".phi" :
"",
355 Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
362 if (*PI != LoopPredecessor)
365 NewPHI->
addIncoming(BasePtrStart, LoopPredecessor);
370 I8Ty, NewPHI, BasePtrIncSCEV->
getValue(),
371 MemI->
hasName() ? MemI->getName() +
".inc" :
"", InsPoint);
375 if (*PI == LoopPredecessor)
388 if (
Instruction *IDel = dyn_cast<Instruction>(BasePtr))
389 BBChanged.insert(IDel->getParent());
396 NewPtrs.
insert( NewBasePtr);
398 for (
auto I = std::next(Buckets[i].Elements.begin()),
399 IE = Buckets[i].Elements.end();
I !=
IE; ++
I) {
401 assert(Ptr &&
"No pointer operand");
402 if (NewPtrs.
count(Ptr))
406 if (!
I->Offset ||
I->Offset->getValue()->isZero()) {
407 RealNewPtr = NewBasePtr;
410 if (PtrIP && isa<Instruction>(NewBasePtr) &&
413 else if (isa<PHINode>(PtrIP))
419 I8Ty, PtrInc,
I->Offset->getValue(),
420 I->Instr->hasName() ?
I->Instr->getName() +
".off" :
"", PtrIP);
427 if (
Instruction *IDel = dyn_cast<Instruction>(Ptr))
428 BBChanged.insert(IDel->getParent());
436 ReplNewPtr = RealNewPtr;
441 NewPtrs.
insert(RealNewPtr);
449 if (BBChanged.count(*
I))
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
The main scalar evolution driver.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
const Function * getParent() const
Return the enclosing method, or null if none.
An instruction for reading from memory.
BlockT * getHeader() const
Type * getPointerElementType() const
const SCEV * getStart() const
StringRef getName() const
Return a constant reference to the value's name.
static Value * GetPointerOperand(Value *MemI)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
void initializePPCLoopPreIncPrepPass(PassRegistry &)
This node represents a polynomial recurrence on the trip count of the specified loop.
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Examine each PHI in the given block and delete it if it is dead.
This class represents a no-op cast from one type to another.
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Function Alias Analysis false
initializer< Ty > init(const Ty &Val)
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
LLVM Basic Block Representation.
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.
df_iterator< T > df_end(const T &G)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
Common code between 32-bit and 64-bit PowerPC targets.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
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...
ConstantInt * getValue() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
df_iterator< T > df_begin(const T &G)
This class uses information about analyze scalars to rewrite expressions in canonical form...
std::vector< BlockT * >::const_iterator block_iterator
block_iterator block_end() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Loop * getLoop() const
static bool IsPtrInBounds(Value *BasePtr)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunctionPass * createPPCLoopPreIncPrepPass(PPCTargetMachine &TM)
LLVM Value Representation.
static const Function * getParent(const Value *V)
block_iterator block_begin() const
static cl::opt< unsigned > MaxVars("ppc-preinc-prep-max-vars", cl::Hidden, cl::init(16), cl::desc("Potential PHI threshold for PPC preinc loop prep"))
The legacy pass manager's analysis pass to compute loop information.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Legacy analysis pass which computes a DominatorTree.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static IntegerType * getInt8Ty(LLVMContext &C)
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.
bool isVoidTy() const
Return true if this is 'void'.
This class represents a constant integer value.