1 //===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
3 // This file contains the definition of the cfg::IntervalPartition class, which
4 // calculates and represent the interval partition of a function.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/IntervalIterator.h"
9 #include "Support/STLExtras.h"
14 AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>());
16 //===----------------------------------------------------------------------===//
17 // IntervalPartition Implementation
18 //===----------------------------------------------------------------------===//
20 // destroy - Reset state back to before function was analyzed
21 void IntervalPartition::destroy() {
22 for_each(begin(), end(), deleter<cfg::Interval>);
27 // addIntervalToPartition - Add an interval to the internal list of intervals,
28 // and then add mappings from all of the basic blocks in the interval to the
29 // interval itself (in the IntervalMap).
31 void IntervalPartition::addIntervalToPartition(Interval *I) {
34 // Add mappings for all of the basic blocks in I to the IntervalPartition
35 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
37 IntervalMap.insert(make_pair(*It, I));
40 // updatePredecessors - Interval generation only sets the successor fields of
41 // the interval data structures. After interval generation is complete,
42 // run through all of the intervals and propogate successor info as
45 void IntervalPartition::updatePredecessors(cfg::Interval *Int) {
46 BasicBlock *Header = Int->getHeaderNode();
47 for (Interval::succ_iterator I = Int->Successors.begin(),
48 E = Int->Successors.end(); I != E; ++I)
49 getBlockInterval(*I)->Predecessors.push_back(Header);
52 // IntervalPartition ctor - Build the first level interval partition for the
53 // specified function...
55 bool IntervalPartition::runOnFunction(Function *F) {
56 assert(F->front() && "Cannot operate on prototypes!");
58 // Pass false to intervals_begin because we take ownership of it's memory
59 function_interval_iterator I = intervals_begin(F, false);
60 assert(I != intervals_end(F) && "No intervals in function!?!?!");
62 addIntervalToPartition(RootInterval = *I);
64 ++I; // After the first one...
66 // Add the rest of the intervals to the partition...
67 for_each(I, intervals_end(F),
68 bind_obj(this, &IntervalPartition::addIntervalToPartition));
70 // Now that we know all of the successor information, propogate this to the
71 // predecessors for each block...
72 for_each(begin(), end(),
73 bind_obj(this, &IntervalPartition::updatePredecessors));
78 // IntervalPartition ctor - Build a reduced interval partition from an
79 // existing interval graph. This takes an additional boolean parameter to
80 // distinguish it from a copy constructor. Always pass in false for now.
82 IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
83 Interval *FunctionStart = IP.getRootInterval();
84 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
86 // Pass false to intervals_begin because we take ownership of it's memory
87 interval_part_interval_iterator I = intervals_begin(IP, false);
88 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
90 addIntervalToPartition(RootInterval = *I);
92 ++I; // After the first one...
94 // Add the rest of the intervals to the partition...
95 for_each(I, intervals_end(IP),
96 bind_obj(this, &IntervalPartition::addIntervalToPartition));
98 // Now that we know all of the successor information, propogate this to the
99 // predecessors for each block...
100 for_each(begin(), end(),
101 bind_obj(this, &IntervalPartition::updatePredecessors));