blob: 9253f721ab1e0495b375baf4e04ac9377cdd1916 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2d676c92001-06-24 04:07:44 +00009//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000010// This file contains the definition of the IntervalPartition class, which
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000011// calculates and represent the interval partition of a function.
Chris Lattner2d676c92001-06-24 04:07:44 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/IntervalIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/ADT/STLExtras.h"
Chris Lattner2d676c92001-06-24 04:07:44 +000017
Brian Gaeked0fde302003-11-11 22:41:34 +000018namespace llvm {
19
Chris Lattner1e435162002-07-26 21:12:44 +000020static RegisterAnalysis<IntervalPartition>
Chris Lattner17689df2002-07-30 16:27:52 +000021X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000022
Chris Lattner2d676c92001-06-24 04:07:44 +000023//===----------------------------------------------------------------------===//
24// IntervalPartition Implementation
25//===----------------------------------------------------------------------===//
26
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000028void IntervalPartition::destroy() {
Chris Lattner38969482002-08-09 22:52:08 +000029 for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000030 IntervalMap.clear();
31 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000032}
33
Chris Lattner1b0a63f2002-07-31 19:32:01 +000034void IntervalPartition::print(std::ostream &O) const {
Chris Lattner38969482002-08-09 22:52:08 +000035 std::copy(Intervals.begin(), Intervals.end(),
Chris Lattnera59cbb22002-07-27 01:12:17 +000036 std::ostream_iterator<const Interval *>(O, "\n"));
37}
38
Chris Lattner23e36622001-06-25 03:55:04 +000039// addIntervalToPartition - Add an interval to the internal list of intervals,
40// and then add mappings from all of the basic blocks in the interval to the
41// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000042//
Chris Lattner23e36622001-06-25 03:55:04 +000043void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000044 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000045
46 // Add mappings for all of the basic blocks in I to the IntervalPartition
47 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
48 It != End; ++It)
Chris Lattnercf3056d2003-10-13 03:32:08 +000049 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000050}
51
Chris Lattner2d676c92001-06-24 04:07:44 +000052// updatePredecessors - Interval generation only sets the successor fields of
53// the interval data structures. After interval generation is complete,
Misha Brukmana3bbcb52002-10-29 23:06:16 +000054// run through all of the intervals and propagate successor info as
Chris Lattner2d676c92001-06-24 04:07:44 +000055// predecessor info.
56//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000057void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000058 BasicBlock *Header = Int->getHeaderNode();
59 for (Interval::succ_iterator I = Int->Successors.begin(),
60 E = Int->Successors.end(); I != E; ++I)
61 getBlockInterval(*I)->Predecessors.push_back(Header);
62}
63
Chris Lattner2d676c92001-06-24 04:07:44 +000064// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000065// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000066//
Chris Lattner7e708292002-06-25 16:13:24 +000067bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000068 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000069 function_interval_iterator I = intervals_begin(&F, false);
70 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000071
72 addIntervalToPartition(RootInterval = *I);
73
Chris Lattner7fc9fe32001-06-27 23:41:11 +000074 ++I; // After the first one...
75
76 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000077 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000078 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000079
Misha Brukmana3bbcb52002-10-29 23:06:16 +000080 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +000081 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +000082 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000083 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000084 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000085}
86
87
88// IntervalPartition ctor - Build a reduced interval partition from an
89// existing interval graph. This takes an additional boolean parameter to
90// distinguish it from a copy constructor. Always pass in false for now.
91//
Chris Lattner23e36622001-06-25 03:55:04 +000092IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000093 Interval *FunctionStart = IP.getRootInterval();
94 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000095
Chris Lattner23e36622001-06-25 03:55:04 +000096 // Pass false to intervals_begin because we take ownership of it's memory
97 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000098 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000099
100 addIntervalToPartition(RootInterval = *I);
101
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000102 ++I; // After the first one...
103
104 // Add the rest of the intervals to the partition...
105 for_each(I, intervals_end(IP),
106 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +0000107
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000108 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +0000109 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +0000110 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000111 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000112}
Brian Gaeked0fde302003-11-11 22:41:34 +0000113
114} // End llvm namespace