blob: eb4975ffdabcb5a17ca3c4c775aa5ff143318e67 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
Chris Lattner2d676c92001-06-24 04:07:44 +00002//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +00003// This file contains the definition of the IntervalPartition class, which
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00004// calculates and represent the interval partition of a function.
Chris Lattner2d676c92001-06-24 04:07:44 +00005//
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
Chris Lattner1e435162002-07-26 21:12:44 +000011static RegisterAnalysis<IntervalPartition>
Chris Lattner17689df2002-07-30 16:27:52 +000012X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000013
Chris Lattner2d676c92001-06-24 04:07:44 +000014//===----------------------------------------------------------------------===//
15// IntervalPartition Implementation
16//===----------------------------------------------------------------------===//
17
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000019void IntervalPartition::destroy() {
Chris Lattner38969482002-08-09 22:52:08 +000020 for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000021 IntervalMap.clear();
22 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000023}
24
Chris Lattner1b0a63f2002-07-31 19:32:01 +000025void IntervalPartition::print(std::ostream &O) const {
Chris Lattner38969482002-08-09 22:52:08 +000026 std::copy(Intervals.begin(), Intervals.end(),
Chris Lattnera59cbb22002-07-27 01:12:17 +000027 std::ostream_iterator<const Interval *>(O, "\n"));
28}
29
Chris Lattner23e36622001-06-25 03:55:04 +000030// addIntervalToPartition - Add an interval to the internal list of intervals,
31// and then add mappings from all of the basic blocks in the interval to the
32// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000033//
Chris Lattner23e36622001-06-25 03:55:04 +000034void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000035 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000036
37 // Add mappings for all of the basic blocks in I to the IntervalPartition
38 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
39 It != End; ++It)
Chris Lattnercf3056d2003-10-13 03:32:08 +000040 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000041}
42
Chris Lattner2d676c92001-06-24 04:07:44 +000043// updatePredecessors - Interval generation only sets the successor fields of
44// the interval data structures. After interval generation is complete,
Misha Brukmana3bbcb52002-10-29 23:06:16 +000045// run through all of the intervals and propagate successor info as
Chris Lattner2d676c92001-06-24 04:07:44 +000046// predecessor info.
47//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000048void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000049 BasicBlock *Header = Int->getHeaderNode();
50 for (Interval::succ_iterator I = Int->Successors.begin(),
51 E = Int->Successors.end(); I != E; ++I)
52 getBlockInterval(*I)->Predecessors.push_back(Header);
53}
54
Chris Lattner2d676c92001-06-24 04:07:44 +000055// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000056// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000057//
Chris Lattner7e708292002-06-25 16:13:24 +000058bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000059 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000060 function_interval_iterator I = intervals_begin(&F, false);
61 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000062
63 addIntervalToPartition(RootInterval = *I);
64
Chris Lattner7fc9fe32001-06-27 23:41:11 +000065 ++I; // After the first one...
66
67 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000068 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000069 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000070
Misha Brukmana3bbcb52002-10-29 23:06:16 +000071 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +000072 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +000073 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000074 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000075 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000076}
77
78
79// IntervalPartition ctor - Build a reduced interval partition from an
80// existing interval graph. This takes an additional boolean parameter to
81// distinguish it from a copy constructor. Always pass in false for now.
82//
Chris Lattner23e36622001-06-25 03:55:04 +000083IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 Interval *FunctionStart = IP.getRootInterval();
85 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000086
Chris Lattner23e36622001-06-25 03:55:04 +000087 // Pass false to intervals_begin because we take ownership of it's memory
88 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000089 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000090
91 addIntervalToPartition(RootInterval = *I);
92
Chris Lattner7fc9fe32001-06-27 23:41:11 +000093 ++I; // After the first one...
94
95 // Add the rest of the intervals to the partition...
96 for_each(I, intervals_end(IP),
97 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000098
Misha Brukmana3bbcb52002-10-29 23:06:16 +000099 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +0000100 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +0000101 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000102 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000103}