blob: fff8d224e21017c80d0b6787e4efda6150702efb [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
14//===----------------------------------------------------------------------===//
15// IntervalPartition Implementation
16//===----------------------------------------------------------------------===//
17
Chris Lattner2d676c92001-06-24 04:07:44 +000018// 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) {
Chris Lattnere6850232001-07-03 15:28:35 +000028 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 Lattner7fc9fe32001-06-27 23:41:11 +000052 assert(M->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);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000056 assert(I != intervals_end(M) && "No intervals in method!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000057
58 addIntervalToPartition(RootInterval = *I);
59
Chris Lattner7fc9fe32001-06-27 23:41:11 +000060 ++I; // After the first one...
61
62 // Add the rest of the intervals to the partition...
63 for_each(I, intervals_end(M),
64 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000065
66 // Now that we know all of the successor information, propogate this to the
67 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000068 for_each(begin(), end(),
69 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +000070}
71
72
73// IntervalPartition ctor - Build a reduced interval partition from an
74// existing interval graph. This takes an additional boolean parameter to
75// distinguish it from a copy constructor. Always pass in false for now.
76//
Chris Lattner23e36622001-06-25 03:55:04 +000077IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
78 Interval *MethodStart = IP.getRootInterval();
Chris Lattner2d676c92001-06-24 04:07:44 +000079 assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
80
Chris Lattner23e36622001-06-25 03:55:04 +000081 // Pass false to intervals_begin because we take ownership of it's memory
82 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000083 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000084
85 addIntervalToPartition(RootInterval = *I);
86
Chris Lattner7fc9fe32001-06-27 23:41:11 +000087 ++I; // After the first one...
88
89 // Add the rest of the intervals to the partition...
90 for_each(I, intervals_end(IP),
91 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000092
93 // Now that we know all of the successor information, propogate this to the
94 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000095 for_each(begin(), end(),
96 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +000097}