blob: f820d7ad1671fa258a319cd008dd8756ce8244f3 [file] [log] [blame]
Chris Lattner2d676c92001-06-24 04:07:44 +00001//===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
2//
3// This file contains the definition of the cfg::IntervalPartition class, which
4// calculates and represent the interval partition of a method.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Analysis/IntervalIterator.h"
9
10using namespace cfg;
11
12//===----------------------------------------------------------------------===//
13// IntervalPartition Implementation
14//===----------------------------------------------------------------------===//
15
16template <class T> static inline void deleter(T *Ptr) { delete Ptr; }
17
18// Destructor - Free memory
19IntervalPartition::~IntervalPartition() {
20 for_each(begin(), end(), deleter<cfg::Interval>);
21}
22
Chris Lattner23e36622001-06-25 03:55:04 +000023// addIntervalToPartition - Add an interval to the internal list of intervals,
24// and then add mappings from all of the basic blocks in the interval to the
25// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000026//
Chris Lattner23e36622001-06-25 03:55:04 +000027void IntervalPartition::addIntervalToPartition(Interval *I) {
28 IntervalList.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000029
30 // Add mappings for all of the basic blocks in I to the IntervalPartition
31 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
32 It != End; ++It)
Chris Lattner23e36622001-06-25 03:55:04 +000033 IntervalMap.insert(make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000034}
35
Chris Lattner2d676c92001-06-24 04:07:44 +000036// updatePredecessors - Interval generation only sets the successor fields of
37// the interval data structures. After interval generation is complete,
38// run through all of the intervals and propogate successor info as
39// predecessor info.
40//
41void IntervalPartition::updatePredecessors(cfg::Interval *Int) {
42 BasicBlock *Header = Int->getHeaderNode();
43 for (Interval::succ_iterator I = Int->Successors.begin(),
44 E = Int->Successors.end(); I != E; ++I)
45 getBlockInterval(*I)->Predecessors.push_back(Header);
46}
47
Chris Lattner2d676c92001-06-24 04:07:44 +000048// IntervalPartition ctor - Build the first level interval partition for the
49// specified method...
50//
51IntervalPartition::IntervalPartition(Method *M) {
Chris Lattner23e36622001-06-25 03:55:04 +000052 assert(M->getBasicBlocks().front() && "Cannot operate on prototypes!");
Chris Lattner2d676c92001-06-24 04:07:44 +000053
Chris Lattner23e36622001-06-25 03:55:04 +000054 // Pass false to intervals_begin because we take ownership of it's memory
55 method_interval_iterator I = intervals_begin(M, false);
56 method_interval_iterator End = intervals_end(M);
57 assert(I != End && "No intervals in method!?!?!");
58
59 addIntervalToPartition(RootInterval = *I);
60
61 for (++I; I != End; ++I)
62 addIntervalToPartition(*I);
Chris Lattner2d676c92001-06-24 04:07:44 +000063
64 // Now that we know all of the successor information, propogate this to the
65 // predecessors for each block...
Chris Lattner23e36622001-06-25 03:55:04 +000066 for(iterator It = begin(), E = end(); It != E; ++It)
67 updatePredecessors(*It);
Chris Lattner2d676c92001-06-24 04:07:44 +000068}
69
70
71// IntervalPartition ctor - Build a reduced interval partition from an
72// existing interval graph. This takes an additional boolean parameter to
73// distinguish it from a copy constructor. Always pass in false for now.
74//
Chris Lattner23e36622001-06-25 03:55:04 +000075IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
76 Interval *MethodStart = IP.getRootInterval();
Chris Lattner2d676c92001-06-24 04:07:44 +000077 assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
78
Chris Lattner23e36622001-06-25 03:55:04 +000079 // Pass false to intervals_begin because we take ownership of it's memory
80 interval_part_interval_iterator I = intervals_begin(IP, false);
81 interval_part_interval_iterator End = intervals_end(IP);
82 assert(I != End && "No intervals in interval partition!?!?!");
83
84 addIntervalToPartition(RootInterval = *I);
85
86 for (++I; I != End; ++I)
87 addIntervalToPartition(*I);
Chris Lattner2d676c92001-06-24 04:07:44 +000088
89 // Now that we know all of the successor information, propogate this to the
90 // predecessors for each block...
91 for(iterator I = begin(), E = end(); I != E; ++I)
92 updatePredecessors(*I);
93}