aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/LoopDepth.cpp
blob: ac73fda26f89e569e9906951609cf1a438d2b8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//===- LoopDepth.cpp - Loop Depth Calculation --------------------*- C++ -*--=//
//
// This file provides a simple class to calculate the loop depth of a 
// BasicBlock.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/LoopDepth.h"
#include "llvm/Analysis/IntervalPartition.h"
#include "llvm/Tools/STLExtras.h"
#include <algorithm>

inline void LoopDepthCalculator::AddBB(const BasicBlock *BB) {
  ++LoopDepth[BB];   // Increment the loop depth count for the specified BB
}

inline void LoopDepthCalculator::ProcessInterval(cfg::Interval *I) {
  if (!I->isLoop()) return;  // Ignore nonlooping intervals...

  for_each(I->Nodes.begin(), I->Nodes.end(), 
	   bind_obj(this, &LoopDepthCalculator::AddBB));
}

LoopDepthCalculator::LoopDepthCalculator(Method *M) {
  //map<const BasicBlock*, unsigned> LoopDepth;

  cfg::IntervalPartition *IP = new cfg::IntervalPartition(M);
  while (!IP->isDegeneratePartition()) {
    for_each(IP->begin(), IP->end(), 
	     bind_obj(this, &LoopDepthCalculator::ProcessInterval));

    // Calculate the reduced version of this graph until we get to an 
    // irreducible graph or a degenerate graph...
    //
    cfg::IntervalPartition *NewIP = new cfg::IntervalPartition(*IP, true);
    if (NewIP->size() == IP->size()) {
      cerr << "IRREDUCIBLE GRAPH FOUND!!!\n";
      // TODO: fix irreducible graph
      return;
    }
    delete IP;
    IP = NewIP;
  }

  delete IP;
}