blob: 27299309984124d8e03e604acf56c29afc99c2a2 [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"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/STLExtras.h"
Chris Lattner2d676c92001-06-24 04:07:44 +000017
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterAnalysis<IntervalPartition>
Chris Lattner17689df2002-07-30 16:27:52 +000019X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000020
Chris Lattner2d676c92001-06-24 04:07:44 +000021//===----------------------------------------------------------------------===//
22// IntervalPartition Implementation
23//===----------------------------------------------------------------------===//
24
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000025// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000026void IntervalPartition::destroy() {
Chris Lattner38969482002-08-09 22:52:08 +000027 for_each(Intervals.begin(), Intervals.end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000028 IntervalMap.clear();
29 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000030}
31
Chris Lattner1b0a63f2002-07-31 19:32:01 +000032void IntervalPartition::print(std::ostream &O) const {
Chris Lattner38969482002-08-09 22:52:08 +000033 std::copy(Intervals.begin(), Intervals.end(),
Chris Lattnera59cbb22002-07-27 01:12:17 +000034 std::ostream_iterator<const Interval *>(O, "\n"));
35}
36
Chris Lattner23e36622001-06-25 03:55:04 +000037// addIntervalToPartition - Add an interval to the internal list of intervals,
38// and then add mappings from all of the basic blocks in the interval to the
39// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000040//
Chris Lattner23e36622001-06-25 03:55:04 +000041void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000042 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000043
44 // Add mappings for all of the basic blocks in I to the IntervalPartition
45 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
46 It != End; ++It)
Chris Lattnercf3056d2003-10-13 03:32:08 +000047 IntervalMap.insert(std::make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000048}
49
Chris Lattner2d676c92001-06-24 04:07:44 +000050// updatePredecessors - Interval generation only sets the successor fields of
51// the interval data structures. After interval generation is complete,
Misha Brukmana3bbcb52002-10-29 23:06:16 +000052// run through all of the intervals and propagate successor info as
Chris Lattner2d676c92001-06-24 04:07:44 +000053// predecessor info.
54//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000055void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000056 BasicBlock *Header = Int->getHeaderNode();
57 for (Interval::succ_iterator I = Int->Successors.begin(),
58 E = Int->Successors.end(); I != E; ++I)
59 getBlockInterval(*I)->Predecessors.push_back(Header);
60}
61
Chris Lattner2d676c92001-06-24 04:07:44 +000062// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000063// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000064//
Chris Lattner7e708292002-06-25 16:13:24 +000065bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000066 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000067 function_interval_iterator I = intervals_begin(&F, false);
68 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000069
70 addIntervalToPartition(RootInterval = *I);
71
Chris Lattner7fc9fe32001-06-27 23:41:11 +000072 ++I; // After the first one...
73
74 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000075 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000076 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000077
Misha Brukmana3bbcb52002-10-29 23:06:16 +000078 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +000079 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +000080 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000081 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000082 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000083}
84
85
86// IntervalPartition ctor - Build a reduced interval partition from an
87// existing interval graph. This takes an additional boolean parameter to
88// distinguish it from a copy constructor. Always pass in false for now.
89//
Chris Lattner23e36622001-06-25 03:55:04 +000090IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 Interval *FunctionStart = IP.getRootInterval();
92 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000093
Chris Lattner23e36622001-06-25 03:55:04 +000094 // Pass false to intervals_begin because we take ownership of it's memory
95 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000096 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000097
98 addIntervalToPartition(RootInterval = *I);
99
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000100 ++I; // After the first one...
101
102 // Add the rest of the intervals to the partition...
103 for_each(I, intervals_end(IP),
104 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +0000105
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000106 // Now that we know all of the successor information, propagate this to the
Chris Lattner2d676c92001-06-24 04:07:44 +0000107 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +0000108 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000109 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000110}