OpenASIP  2.0
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes | List of all members
InnerLoopFinder Struct Reference

#include <InnerLoopFinder.hh>

Inheritance diagram for InnerLoopFinder:
Inheritance graph
Collaboration diagram for InnerLoopFinder:
Collaboration graph

Classes

class  InnerLoopInfo
 

Public Types

typedef std::map< std::string, std::ostream * > DumpFileIndex
 
typedef std::map< const llvm::BasicBlock *, InnerLoopInfoInnerLoopInfoIndex
 

Public Member Functions

 InnerLoopFinder ()
 
 ~InnerLoopFinder ()
 
virtual void getAnalysisUsage (llvm::AnalysisUsage &AU) const
 
std::ostream & out (llvm::Loop *l)
 
virtual std::string loopDescription (llvm::Loop *l)
 
virtual bool runOnLoop (llvm::Loop *l, llvm::LPPassManager &LPM)
 
unsigned getSmallConstantTripCount (llvm::Loop *loop)
 
InnerLoopInfoIndex innerLoopInfo ()
 

Public Attributes

DumpFileIndex dumpFiles
 
bool dump
 

Static Public Attributes

static char ID = 0
 

Private Attributes

InnerLoopInfoIndex loopInfos_
 

Detailed Description

Definition at line 28 of file InnerLoopFinder.hh.

Member Typedef Documentation

◆ DumpFileIndex

typedef std::map<std::string, std::ostream*> InnerLoopFinder::DumpFileIndex

Definition at line 30 of file InnerLoopFinder.hh.

◆ InnerLoopInfoIndex

typedef std::map<const llvm::BasicBlock*, InnerLoopInfo> InnerLoopFinder::InnerLoopInfoIndex

Definition at line 42 of file InnerLoopFinder.hh.

Constructor & Destructor Documentation

◆ InnerLoopFinder()

InnerLoopFinder::InnerLoopFinder ( )

Definition at line 93 of file InnerLoopFinder.cc.

93  :
94  LoopPass(ID) {
96 }

References DumpLoopInfo().

Here is the call graph for this function:

◆ ~InnerLoopFinder()

InnerLoopFinder::~InnerLoopFinder ( )

Definition at line 110 of file InnerLoopFinder.cc.

110  {
111  // flush and close all loop info dump files
112  if (dump) {
113  for (DumpFileIndex::iterator i = dumpFiles.begin();
114  i != dumpFiles.end(); ++i) {
115  std::ostream* stream = (*i).second;
116  stream->flush();
117  delete stream;
118  }
119  dumpFiles.clear();
120  }
121 }

Member Function Documentation

◆ getAnalysisUsage()

void InnerLoopFinder::getAnalysisUsage ( llvm::AnalysisUsage &  AU) const
virtual

Definition at line 99 of file InnerLoopFinder.cc.

99  {
100  AU.setPreservesCFG();
101  AU.addRequiredID(llvm::LoopSimplifyID);
102  AU.addRequiredID(llvm::LCSSAID);
103  AU.addPreservedID(llvm::LCSSAID);
104  AU.addRequired<llvm::LoopInfoWrapperPass>();
105  AU.addRequired<llvm::ScalarEvolutionWrapperPass>();
106  AU.addPreserved<llvm::LoopInfoWrapperPass>();
107  AU.addPreserved<llvm::ScalarEvolutionWrapperPass>();
108 }

◆ getSmallConstantTripCount()

unsigned InnerLoopFinder::getSmallConstantTripCount ( llvm::Loop *  loop)

Returns the trip count of the loop as a normal unsigned value, if possible.

Forwardported from LLVM 2.4. Returns 0 if the trip count is unknown or not constant. Will also return 0 if the trip count is very large (>= 2^32).

Definition at line 254 of file InnerLoopFinder.cc.

254  {
255  ScalarEvolution *SE =
256  &getAnalysis<llvm::ScalarEvolutionWrapperPass>().getSE();
257  llvm::BasicBlock* loopLatch = loop->getLoopLatch();
258  // In case of the forever loop in the exit() the latch does
259  // not jump out of the loop and SE cannot analyze the trip count,
260  // which is infinite in that case.
261  if (loopLatch == NULL || !loop->isLoopExiting(loopLatch))
262  return 0;
263  return SE->getSmallConstantTripCount(loop, loopLatch);
264 }

◆ innerLoopInfo()

InnerLoopInfoIndex InnerLoopFinder::innerLoopInfo ( )
inline

Definition at line 58 of file InnerLoopFinder.hh.

58 { return loopInfos_; }

References loopInfos_.

Referenced by llvm::LLVMTCEIRBuilder::buildTCECFG().

◆ loopDescription()

std::string InnerLoopFinder::loopDescription ( llvm::Loop *  l)
virtual

Returns a textual description for the given loop.

Definition at line 152 of file InnerLoopFinder.cc.

152  {
153 
154  if (l == NULL)
155  return "[none]";
156 
157  std::ostringstream ss;
158  assert(l->getHeader() != NULL);
159  assert(l->getHeader()->getParent() != NULL);
160  std::string curProcName = l->getHeader()->getParent()->getName().str();
161  ss << "in " << curProcName << "()";
162 
163  return ss.str();
164 }

References assert.

◆ out()

std::ostream & InnerLoopFinder::out ( llvm::Loop *  l)

Returns a stream to dump the loop info to.

Definition at line 127 of file InnerLoopFinder.cc.

127  {
128 
129  assert(l != NULL);
130 
131  std::string moduleName =
132  l->getHeader()->getParent()->getParent()->getModuleIdentifier();
133  std::string fName = moduleName + ".loopinfo.txt";
134  if (moduleName == "<stdin>") {
135  fName = "loopinfo.txt";
136  }
137 
138  if (dumpFiles.find(moduleName) == dumpFiles.end()) {
139  std::fstream* outStream =
140  new std::fstream(
141  fName.c_str(),
142  std::ios_base::out | std::ios_base::trunc);
143  dumpFiles[moduleName] = outStream;
144  }
145  return *dumpFiles[moduleName];
146 }

References assert.

◆ runOnLoop()

bool InnerLoopFinder::runOnLoop ( llvm::Loop *  l,
llvm::LPPassManager &  LPM 
)
virtual

Saves trip counts of single basic block inner loops.

early exits?

Definition at line 170 of file InnerLoopFinder.cc.

170  {
171 
172  const std::vector<llvm::Loop*>& subLoops = l->getSubLoops();
173 
174  unsigned subLoopCount = subLoops.size();
175  unsigned tripCount = getSmallConstantTripCount(l);
176  unsigned bbCount = l->getBlocks().size();
177 
178 
179  /// early exits?
180  llvm::SmallVector<llvm::BasicBlock*, 10> exitingBlocks;
181 
182 #if 0
183  /*
184  This caused an LLVM crash in test cases
185  llvm-frontend/tests/{FloatEmulationTest,NewLib,softloat_lowering,HelloWorld}:
186 
187  /home/visit0r/src/llvm-2.3/include/llvm/Support/Casting.h:199:
188  typename llvm::cast_retty<To, From>::ret_type llvm::cast(const Y&)
189  [with X = llvm::BasicBlock, Y = llvm::Value*]:
190  Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
191  */
192  l->getExitingBlocks(exitingBlocks);
193  unsigned exitingBlockCount = exitingBlocks.size();
194 #endif
195 
196  unsigned callCount = 0;
197  if (subLoopCount == 0) {
198  for (llvm::LoopBase<llvm::BasicBlock, llvm::Loop>::
199  block_iterator bb =
200  l->block_begin(); bb != l->block_end(); ++bb) {
201  llvm::BasicBlock& basicBlock = **bb;
202  for (llvm::BasicBlock::iterator i = basicBlock.begin();
203  i != basicBlock.end(); ++i) {
204  llvm::Instruction& instruction = *i;
205  if (instruction.getOpcode() == llvm::Instruction::Call)
206  ++callCount;
207  }
208  }
209  }
210 
211  const bool innerLoop =
212  bbCount == 1 && subLoopCount == 0 && callCount == 0;
213 
214  if (innerLoop) {
215  if (dump && tripCount > 0) {
216  out(l) << "found an inner loop with trip count "
217  << tripCount << std::endl;
218  }
219  assert(l->getBlocks().size() > 0);
220 
221  InnerLoopInfo loopInfo(tripCount);
222  loopInfos_[l->getBlocks()[0]] = loopInfo;
223  }
224 
225  if (dump) {
226  out(l) << "loop: " << loopDescription(l) << std::endl
227  << "depth: " << l->getLoopDepth() << std::endl;
228  out(l) << "sub loops: " << subLoopCount << std::endl;
229  out(l) << "trip count: ";
230  if (tripCount != 0) {
231  out(l) << "constant: " << tripCount;
232  } else {
233  out(l) << "unknown ";
234  }
235  out(l) << std::endl;
236  out(l) << "basic blocks: " << bbCount << std::endl;
237 // out(l) << "exiting blocks: " << exitingBlocks.size() << std::endl;
238  if (subLoopCount == 0)
239  out(l) << "calls: " << callCount << std::endl;
240  out(l) << std::endl;
241  }
242  return false; // loop not modified
243 }

References assert.

Member Data Documentation

◆ dump

bool InnerLoopFinder::dump

Definition at line 45 of file InnerLoopFinder.hh.

◆ dumpFiles

DumpFileIndex InnerLoopFinder::dumpFiles

Definition at line 44 of file InnerLoopFinder.hh.

◆ ID

find Finds info of the inner loops in the true char InnerLoopFinder::ID = 0
static

Definition at line 29 of file InnerLoopFinder.hh.

◆ loopInfos_

InnerLoopInfoIndex InnerLoopFinder::loopInfos_
private

Definition at line 60 of file InnerLoopFinder.hh.

Referenced by innerLoopInfo().


The documentation for this struct was generated from the following files:
InnerLoopFinder::loopInfos_
InnerLoopInfoIndex loopInfos_
Definition: InnerLoopFinder.hh:60
DumpLoopInfo
static llvm::cl::opt< bool > DumpLoopInfo("dump-loop-info", llvm::cl::init(false), llvm::cl::Hidden, llvm::cl::desc("Dump information about loops to files named [modulename].loopinfo.txt."))
InnerLoopFinder::dump
bool dump
Definition: InnerLoopFinder.hh:45
assert
#define assert(condition)
Definition: Application.hh:86
InnerLoopFinder::getSmallConstantTripCount
unsigned getSmallConstantTripCount(llvm::Loop *loop)
Definition: InnerLoopFinder.cc:254
InnerLoopFinder::ID
static char ID
Definition: InnerLoopFinder.hh:29
InnerLoopFinder::loopDescription
virtual std::string loopDescription(llvm::Loop *l)
Definition: InnerLoopFinder.cc:152
InnerLoopFinder::dumpFiles
DumpFileIndex dumpFiles
Definition: InnerLoopFinder.hh:44
InnerLoopFinder::out
std::ostream & out(llvm::Loop *l)
Definition: InnerLoopFinder.cc:127