blob: f524d016f16843c9f4fe3a176068d359590efd56 [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"
Chris Lattnercee8f9a2001-11-27 00:03:19 +00009#include "Support/STLExtras.h"
Chris Lattner2d676c92001-06-24 04:07:44 +000010
11using namespace cfg;
Chris Lattner697954c2002-01-20 22:54:45 +000012using std::make_pair;
Chris Lattner2d676c92001-06-24 04:07:44 +000013
Chris Lattner93193f82002-01-31 00:42:27 +000014AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>());
15
Chris Lattner2d676c92001-06-24 04:07:44 +000016//===----------------------------------------------------------------------===//
17// IntervalPartition Implementation
18//===----------------------------------------------------------------------===//
19
Chris Lattner93193f82002-01-31 00:42:27 +000020// destroy - Reset state back to before method was analyzed
21void IntervalPartition::destroy() {
Chris Lattner2d676c92001-06-24 04:07:44 +000022 for_each(begin(), end(), deleter<cfg::Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000023 IntervalMap.clear();
24 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000025}
26
Chris Lattner23e36622001-06-25 03:55:04 +000027// 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).
Chris Lattner2d676c92001-06-24 04:07:44 +000030//
Chris Lattner23e36622001-06-25 03:55:04 +000031void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattnere6850232001-07-03 15:28:35 +000032 push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000033
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();
36 It != End; ++It)
Chris Lattner23e36622001-06-25 03:55:04 +000037 IntervalMap.insert(make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000038}
39
Chris Lattner2d676c92001-06-24 04:07:44 +000040// 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
43// predecessor info.
44//
45void 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);
50}
51
Chris Lattner2d676c92001-06-24 04:07:44 +000052// IntervalPartition ctor - Build the first level interval partition for the
53// specified method...
54//
Chris Lattner93193f82002-01-31 00:42:27 +000055bool IntervalPartition::runOnMethod(Method *M) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +000056 assert(M->front() && "Cannot operate on prototypes!");
Chris Lattner2d676c92001-06-24 04:07:44 +000057
Chris Lattner23e36622001-06-25 03:55:04 +000058 // Pass false to intervals_begin because we take ownership of it's memory
59 method_interval_iterator I = intervals_begin(M, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000060 assert(I != intervals_end(M) && "No intervals in method!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000061
62 addIntervalToPartition(RootInterval = *I);
63
Chris Lattner7fc9fe32001-06-27 23:41:11 +000064 ++I; // After the first one...
65
66 // Add the rest of the intervals to the partition...
67 for_each(I, intervals_end(M),
68 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000069
70 // Now that we know all of the successor information, propogate this to the
71 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000072 for_each(begin(), end(),
73 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000074 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000075}
76
77
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.
81//
Chris Lattner23e36622001-06-25 03:55:04 +000082IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
83 Interval *MethodStart = IP.getRootInterval();
Chris Lattner2d676c92001-06-24 04:07:44 +000084 assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
85
Chris Lattner23e36622001-06-25 03:55:04 +000086 // Pass false to intervals_begin because we take ownership of it's memory
87 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000088 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000089
90 addIntervalToPartition(RootInterval = *I);
91
Chris Lattner7fc9fe32001-06-27 23:41:11 +000092 ++I; // After the first one...
93
94 // Add the rest of the intervals to the partition...
95 for_each(I, intervals_end(IP),
96 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000097
98 // Now that we know all of the successor information, propogate this to the
99 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000100 for_each(begin(), end(),
101 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000102}