LLVM  3.7.0
PPCLoopDataPrefetch.cpp
Go to the documentation of this file.
1 //===-------- PPCLoopDataPrefetch.cpp - Loop Data Prefetching Pass --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a Loop Data Prefetching Pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #define DEBUG_TYPE "ppc-loop-data-prefetch"
15 #include "PPC.h"
16 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/CFG.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Module.h"
34 #include "llvm/Support/Debug.h"
38 using namespace llvm;
39 
40 // By default, we limit this to creating 16 PHIs (which is a little over half
41 // of the allocatable register set).
42 static cl::opt<bool>
43 PrefetchWrites("ppc-loop-prefetch-writes", cl::Hidden, cl::init(false),
44  cl::desc("Prefetch write addresses"));
45 
46 // This seems like a reasonable default for the BG/Q (this pass is enabled, by
47 // default, only on the BG/Q).
48 static cl::opt<unsigned>
49 PrefDist("ppc-loop-prefetch-distance", cl::Hidden, cl::init(300),
50  cl::desc("The loop prefetch distance"));
51 
52 static cl::opt<unsigned>
53 CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64),
54  cl::desc("The loop prefetch cache line size"));
55 
56 namespace llvm {
58 }
59 
60 namespace {
61 
62  class PPCLoopDataPrefetch : public FunctionPass {
63  public:
64  static char ID; // Pass ID, replacement for typeid
65  PPCLoopDataPrefetch() : FunctionPass(ID) {
67  }
68 
69  void getAnalysisUsage(AnalysisUsage &AU) const override {
75  // FIXME: For some reason, preserving SE here breaks LSR (even if
76  // this pass changes nothing).
77  // AU.addPreserved<ScalarEvolution>();
79  }
80 
81  bool runOnFunction(Function &F) override;
82  bool runOnLoop(Loop *L);
83 
84  private:
85  AssumptionCache *AC;
86  LoopInfo *LI;
87  ScalarEvolution *SE;
88  const TargetTransformInfo *TTI;
89  const DataLayout *DL;
90  };
91 }
92 
94 INITIALIZE_PASS_BEGIN(PPCLoopDataPrefetch, "ppc-loop-data-prefetch",
95  "PPC Loop Data Prefetch", false, false)
100 INITIALIZE_PASS_END(PPCLoopDataPrefetch, "ppc-loop-data-prefetch",
101  "PPC Loop Data Prefetch", false, false)
102 
103 FunctionPass *llvm::createPPCLoopDataPrefetchPass() { return new PPCLoopDataPrefetch(); }
104 
105 bool PPCLoopDataPrefetch::runOnFunction(Function &F) {
106  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
107  SE = &getAnalysis<ScalarEvolution>();
108  DL = &F.getParent()->getDataLayout();
109  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
110  TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
111 
112  bool MadeChange = false;
113 
114  for (auto I = LI->begin(), IE = LI->end(); I != IE; ++I)
115  for (auto L = df_begin(*I), LE = df_end(*I); L != LE; ++L)
116  MadeChange |= runOnLoop(*L);
117 
118  return MadeChange;
119 }
120 
121 bool PPCLoopDataPrefetch::runOnLoop(Loop *L) {
122  bool MadeChange = false;
123 
124  // Only prefetch in the inner-most loop
125  if (!L->empty())
126  return MadeChange;
127 
129  CodeMetrics::collectEphemeralValues(L, AC, EphValues);
130 
131  // Calculate the number of iterations ahead to prefetch
133  for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
134  I != IE; ++I) {
135 
136  // If the loop already has prefetches, then assume that the user knows
137  // what he or she is doing and don't add any more.
138  for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
139  J != JE; ++J)
140  if (CallInst *CI = dyn_cast<CallInst>(J))
141  if (Function *F = CI->getCalledFunction())
143  return MadeChange;
144 
145  Metrics.analyzeBasicBlock(*I, *TTI, EphValues);
146  }
147  unsigned LoopSize = Metrics.NumInsts;
148  if (!LoopSize)
149  LoopSize = 1;
150 
151  unsigned ItersAhead = PrefDist/LoopSize;
152  if (!ItersAhead)
153  ItersAhead = 1;
154 
156  for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
157  I != IE; ++I) {
158  for (BasicBlock::iterator J = (*I)->begin(), JE = (*I)->end();
159  J != JE; ++J) {
160  Value *PtrValue;
161  Instruction *MemI;
162 
163  if (LoadInst *LMemI = dyn_cast<LoadInst>(J)) {
164  MemI = LMemI;
165  PtrValue = LMemI->getPointerOperand();
166  } else if (StoreInst *SMemI = dyn_cast<StoreInst>(J)) {
167  if (!PrefetchWrites) continue;
168  MemI = SMemI;
169  PtrValue = SMemI->getPointerOperand();
170  } else continue;
171 
172  unsigned PtrAddrSpace = PtrValue->getType()->getPointerAddressSpace();
173  if (PtrAddrSpace)
174  continue;
175 
176  if (L->isLoopInvariant(PtrValue))
177  continue;
178 
179  const SCEV *LSCEV = SE->getSCEV(PtrValue);
180  const SCEVAddRecExpr *LSCEVAddRec = dyn_cast<SCEVAddRecExpr>(LSCEV);
181  if (!LSCEVAddRec)
182  continue;
183 
184  // We don't want to double prefetch individual cache lines. If this load
185  // is known to be within one cache line of some other load that has
186  // already been prefetched, then don't prefetch this one as well.
187  bool DupPref = false;
188  for (SmallVector<std::pair<Instruction *, const SCEVAddRecExpr *>,
189  16>::iterator K = PrefLoads.begin(), KE = PrefLoads.end();
190  K != KE; ++K) {
191  const SCEV *PtrDiff = SE->getMinusSCEV(LSCEVAddRec, K->second);
192  if (const SCEVConstant *ConstPtrDiff =
193  dyn_cast<SCEVConstant>(PtrDiff)) {
194  int64_t PD = std::abs(ConstPtrDiff->getValue()->getSExtValue());
195  if (PD < (int64_t) CacheLineSize) {
196  DupPref = true;
197  break;
198  }
199  }
200  }
201  if (DupPref)
202  continue;
203 
204  const SCEV *NextLSCEV = SE->getAddExpr(LSCEVAddRec, SE->getMulExpr(
205  SE->getConstant(LSCEVAddRec->getType(), ItersAhead),
206  LSCEVAddRec->getStepRecurrence(*SE)));
207  if (!isSafeToExpand(NextLSCEV, *SE))
208  continue;
209 
210  PrefLoads.push_back(std::make_pair(MemI, LSCEVAddRec));
211 
212  Type *I8Ptr = Type::getInt8PtrTy((*I)->getContext(), PtrAddrSpace);
213  SCEVExpander SCEVE(*SE, J->getModule()->getDataLayout(), "prefaddr");
214  Value *PrefPtrValue = SCEVE.expandCodeFor(NextLSCEV, I8Ptr, MemI);
215 
216  IRBuilder<> Builder(MemI);
217  Module *M = (*I)->getParent()->getParent();
218  Type *I32 = Type::getInt32Ty((*I)->getContext());
220  Builder.CreateCall(
221  PrefetchFunc,
222  {PrefPtrValue,
223  ConstantInt::get(I32, MemI->mayReadFromMemory() ? 0 : 1),
224  ConstantInt::get(I32, 3), ConstantInt::get(I32, 1)});
225 
226  MadeChange = true;
227  }
228  }
229 
230  return MadeChange;
231 }
232 
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
static cl::opt< bool > PrefetchWrites("ppc-loop-prefetch-writes", cl::Hidden, cl::init(false), cl::desc("Prefetch write addresses"))
ScalarEvolution - This class is the main scalar evolution driver.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of .assume calls within a function.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
getStepRecurrence - This method constructs and returns the recurrence indicating how much this expres...
F(f)
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: Type.cpp:216
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
bool isLoopInvariant(const Value *V) const
isLoopInvariant - Return true if the specified value is loop invariant
Definition: LoopInfo.cpp:59
bool mayReadFromMemory() const
mayReadFromMemory - Return true if this instruction may read memory.
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
FunctionPass * createPPCLoopDataPrefetchPass()
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:866
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
ppc loop data PPC Loop Data Prefetch
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, SmallPtrSetImpl< const Value * > &EphValues)
Add information about a block to the current state.
static cl::opt< unsigned > PrefDist("ppc-loop-prefetch-distance", cl::Hidden, cl::init(300), cl::desc("The loop prefetch distance"))
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
Wrapper pass for TargetTransformInfo.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
df_iterator< T > df_end(const T &G)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:283
INITIALIZE_PASS_BEGIN(PPCLoopDataPrefetch,"ppc-loop-data-prefetch","PPC Loop Data Prefetch", false, false) INITIALIZE_PASS_END(PPCLoopDataPrefetch
machine trace Machine Trace Metrics
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Utility to calculate the size and a few similar metrics for a set of basic blocks.
Definition: CodeMetrics.h:42
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.
Definition: Constants.cpp:582
ppc loop data prefetch
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:159
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)
Definition: Casting.h:285
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:140
block_iterator block_end() const
Definition: LoopInfo.h:142
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
SCEV - This class represents an analyzed expression in the program.
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
void initializePPCLoopDataPrefetchPass(PassRegistry &)
#define I(x, y, z)
Definition: MD5.cpp:54
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:651
ppc loop data PPC Loop Data false
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
bool empty() const
Definition: LoopInfo.h:135
block_iterator block_begin() const
Definition: LoopInfo.h:141
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
This pass exposes codegen information to IR-level passes.
static cl::opt< unsigned > CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64), cl::desc("The loop prefetch cache line size"))
static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop)...
Definition: CodeMetrics.cpp:70
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 NumInsts
Number of instructions in the analyzed blocks.
Definition: CodeMetrics.h:60
SCEVConstant - This class represents a constant integer value.