blob: e73b40b63ed7ae7ba6a475899e79f1d53c1ed9ef [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner13d01082005-02-22 23:27:21 +000016using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000017
Devang Patel19974732007-05-03 01:11:54 +000018char IntervalPartition::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000019static RegisterPass<IntervalPartition>
Chris Lattner17689df2002-07-30 16:27:52 +000020X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000021
Chris Lattner2d676c92001-06-24 04:07:44 +000022//===----------------------------------------------------------------------===//
23// IntervalPartition Implementation
24//===----------------------------------------------------------------------===//
25
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000027void IntervalPartition::destroy() {
Chris Lattner13d01082005-02-22 23:27:21 +000028 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
29 delete Intervals[i];
Chris Lattner93193f82002-01-31 00:42:27 +000030 IntervalMap.clear();
31 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000032}
33
Reid Spencerce9653c2004-12-07 04:03:45 +000034void IntervalPartition::print(std::ostream &O, const Module*) const {
Chris Lattner318c1492005-04-26 14:48:28 +000035 for(unsigned i = 0, e = Intervals.size(); i != e; ++i)
36 Intervals[i]->print(O);
Chris Lattnera59cbb22002-07-27 01:12:17 +000037}
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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000059 for (Interval::succ_iterator I = Int->Successors.begin(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +000060 E = Int->Successors.end(); I != E; ++I)
Chris Lattner2d676c92001-06-24 04:07:44 +000061 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
Chris Lattner13d01082005-02-22 23:27:21 +000076 // Add the rest of the intervals to the partition.
77 for (function_interval_iterator E = intervals_end(&F); I != E; ++I)
78 addIntervalToPartition(*I);
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 Lattner13d01082005-02-22 23:27:21 +000081 // predecessors for each block.
82 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
83 updatePredecessors(Intervals[i]);
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//
Devang Patel794fd752007-05-01 21:15:47 +000092IntervalPartition::IntervalPartition(IntervalPartition &IP, bool)
93 : FunctionPass((intptr_t) &ID) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000094 Interval *FunctionStart = IP.getRootInterval();
95 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000096
Chris Lattner23e36622001-06-25 03:55:04 +000097 // Pass false to intervals_begin because we take ownership of it's memory
98 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000099 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +0000100
101 addIntervalToPartition(RootInterval = *I);
102
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000103 ++I; // After the first one...
104
Chris Lattner13d01082005-02-22 23:27:21 +0000105 // Add the rest of the intervals to the partition.
106 for (interval_part_interval_iterator E = intervals_end(IP); I != E; ++I)
107 addIntervalToPartition(*I);
Chris Lattner2d676c92001-06-24 04:07:44 +0000108
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000109 // Now that we know all of the successor information, propagate this to the
Chris Lattner13d01082005-02-22 23:27:21 +0000110 // predecessors for each block.
111 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
112 updatePredecessors(Intervals[i]);
Chris Lattner2d676c92001-06-24 04:07:44 +0000113}
Brian Gaeked0fde302003-11-11 22:41:34 +0000114