22 #define DEBUG_TYPE "ppc-loop-preinc-prep"
54 cl::desc(
"Potential PHI threshold for PPC preinc loop prep"));
81 bool runOnLoop(
Loop *L);
82 void simplifyLoopLatch(
Loop *L);
83 bool rotateLoop(
Loop *L);
93 static const char *
name =
"Prepare loop for pre-inc. addressing modes";
100 return new PPCLoopPreIncPrep(TM);
104 struct SCEVLess : std::binary_function<const SCEV *, const SCEV *, bool>
108 bool operator() (
const SCEV *
X,
const SCEV *
Y)
const {
109 const SCEV *Diff = SE->getMinusSCEV(X, Y);
110 return cast<SCEVConstant>(Diff)->getValue()->getSExtValue() < 0;
119 Value *StrippedBasePtr = BasePtr;
120 while (
BitCastInst *BC = dyn_cast<BitCastInst>(StrippedBasePtr))
121 StrippedBasePtr = BC->getOperand(0);
123 return GEP->isInBounds();
129 if (
LoadInst *LMemI = dyn_cast<LoadInst>(MemI)) {
130 return LMemI->getPointerOperand();
131 }
else if (
StoreInst *SMemI = dyn_cast<StoreInst>(MemI)) {
132 return SMemI->getPointerOperand();
133 }
else if (
IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(MemI)) {
135 return IMemI->getArgOperand(0);
141 bool PPCLoopPreIncPrep::runOnFunction(
Function &
F) {
142 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
143 SE = &getAnalysis<ScalarEvolution>();
145 bool MadeChange =
false;
147 for (
auto I = LI->begin(),
IE = LI->end();
I !=
IE; ++
I)
149 MadeChange |= runOnLoop(*L);
154 bool PPCLoopPreIncPrep::runOnLoop(
Loop *L) {
155 bool MadeChange =
false;
161 DEBUG(
dbgs() <<
"PIP: Examining: " << *L <<
"\n");
168 unsigned HeaderLoopPredCount =
172 typedef std::multimap<const SCEV *, Instruction *, SCEVLess> Bucket;
181 if (
LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
183 PtrValue = LMemI->getPointerOperand();
184 }
else if (
StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
186 PtrValue = SMemI->getPointerOperand();
187 }
else if (
IntrinsicInst *IMemI = dyn_cast<IntrinsicInst>(J)) {
190 PtrValue = IMemI->getArgOperand(0);
206 const SCEV *LSCEV = SE->getSCEVAtScope(PtrValue, L);
207 if (
const SCEVAddRecExpr *LARSCEV = dyn_cast<SCEVAddRecExpr>(LSCEV)) {
208 if (LARSCEV->getLoop() != L)
214 bool FoundBucket =
false;
215 for (
unsigned i = 0, e = Buckets.
size(); i != e; ++i)
216 for (Bucket::iterator K = Buckets[i].
begin(), KE = Buckets[i].
end();
218 const SCEV *Diff = SE->getMinusSCEV(K->first, LSCEV);
219 if (isa<SCEVConstant>(Diff)) {
220 Buckets[i].
insert(std::make_pair(LSCEV, MemI));
228 Buckets[Buckets.
size()-1].
insert(std::make_pair(LSCEV, MemI));
240 if (!LoopPredecessor ||
246 if (!LoopPredecessor)
249 DEBUG(
dbgs() <<
"PIP: Found " << Buckets.
size() <<
" buckets\n");
252 for (
unsigned i = 0, e = Buckets.
size(); i != e; ++i) {
257 cast<SCEVAddRecExpr>(Buckets[i].
begin()->first);
261 DEBUG(
dbgs() <<
"PIP: Transforming: " << *BasePtrSCEV <<
"\n");
262 assert(BasePtrSCEV->
getLoop() == L &&
263 "AddRec for the wrong loop?");
267 assert(BasePtr &&
"No pointer operand");
274 if (!SE->isLoopInvariant(BasePtrStartSCEV, L))
281 BasePtrStartSCEV = SE->getMinusSCEV(BasePtrStartSCEV, BasePtrIncSCEV);
285 DEBUG(
dbgs() <<
"PIP: New start is: " << *BasePtrStartSCEV <<
"\n");
288 MemI->hasName() ? MemI->getName() +
".phi" :
"",
292 Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy,
299 if (*PI != LoopPredecessor)
302 NewPHI->
addIncoming(BasePtrStart, LoopPredecessor);
307 I8Ty, NewPHI, BasePtrIncSCEV->
getValue(),
308 MemI->
hasName() ? MemI->getName() +
".inc" :
"", InsPoint);
312 if (*PI == LoopPredecessor)
325 if (
Instruction *IDel = dyn_cast<Instruction>(BasePtr))
326 BBChanged.insert(IDel->getParent());
330 Value *LastNewPtr = NewBasePtr;
331 for (Bucket::iterator
I = std::next(Buckets[i].
begin()),
334 assert(Ptr &&
"No pointer operand");
335 if (Ptr == LastNewPtr)
340 cast<SCEVConstant>(SE->getMinusSCEV(
I->first, BasePtrSCEV));
342 RealNewPtr = NewBasePtr;
345 if (PtrIP && isa<Instruction>(NewBasePtr) &&
348 else if (isa<PHINode>(PtrIP))
355 I->second->hasName() ?
I->second->getName() +
".off" :
"", PtrIP);
362 if (
Instruction *IDel = dyn_cast<Instruction>(Ptr))
363 BBChanged.insert(IDel->getParent());
371 ReplNewPtr = RealNewPtr;
376 LastNewPtr = RealNewPtr;
384 if (BBChanged.count(*
I))
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - 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...
bool isZero() const
isZero - Return true if the expression is a constant zero.
ScalarEvolution - This class is the main scalar evolution driver.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
getStepRecurrence - This method constructs and returns the recurrence indicating how much this expres...
const_iterator begin(StringRef path)
Get begin iterator over path.
const Function * getParent() const
Return the enclosing method, or null if none.
LoadInst - an instruction for reading from memory.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
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)
BasicBlock * InsertPreheaderForLoop(Loop *L, Pass *P)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
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)
setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool isLoopInvariant(const Value *V) const
isLoopInvariant - Return true if the specified value is loop invariant
void initializePPCLoopPreIncPrepPass(PassRegistry &)
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
DeleteDeadPHIs - 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.
StoreInst - an instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
initializer< Ty > init(const Ty &Val)
bool isAffine() const
isAffine - Return true if this represents an expression A + B*x where A and B are loop invariant valu...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isVectorTy() const
isVectorTy - 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...
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.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
PPCTargetMachine - Common code between 32-bit and 64-bit PowerPC targets.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a trivially dead instruction...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
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.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
SequentialType * getType() const
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.
df_iterator< T > df_begin(const T &G)
This class uses information about analyze scalars to rewrite expressions in canonical form...
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)
std::vector< BlockT * >::const_iterator block_iterator
iterator insert(iterator I, T &&Elt)
block_iterator block_end() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
SCEV - This class represents an analyzed expression in the program.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
BlockT * getLoopPredecessor() const
getLoopPredecessor - If the given loop's header has exactly one unique predecessor outside the loop...
const Loop * getLoop() const
static bool IsPtrInBounds(Value *BasePtr)
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.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
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...
static IntegerType * getInt8Ty(LLVMContext &C)
const BasicBlock * getParent() const
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
SCEVConstant - This class represents a constant integer value.