blob: 5e6bf9ca4da127bd07e4e1f07fcf8053ca855d7d [file] [log] [blame]
Chris Lattner2d676c92001-06-24 04:07:44 +00001//===- IntervalPartition.cpp - Interval Partition module code ----*- C++ -*--=//
2//
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 Lattner697954c2002-01-20 22:54:45 +000011using std::make_pair;
Chris Lattner2d676c92001-06-24 04:07:44 +000012
Chris Lattner1e435162002-07-26 21:12:44 +000013static RegisterAnalysis<IntervalPartition>
14X("intervals", "Interval Partition Construction");
15
Chris Lattner07a228d2002-05-06 19:32:07 +000016AnalysisID IntervalPartition::ID(AnalysisID::create<IntervalPartition>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +000017
Chris Lattner2d676c92001-06-24 04:07:44 +000018//===----------------------------------------------------------------------===//
19// IntervalPartition Implementation
20//===----------------------------------------------------------------------===//
21
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022// destroy - Reset state back to before function was analyzed
Chris Lattner93193f82002-01-31 00:42:27 +000023void IntervalPartition::destroy() {
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000024 for_each(begin(), end(), deleter<Interval>);
Chris Lattner93193f82002-01-31 00:42:27 +000025 IntervalMap.clear();
26 RootInterval = 0;
Chris Lattner2d676c92001-06-24 04:07:44 +000027}
28
Chris Lattner23e36622001-06-25 03:55:04 +000029// addIntervalToPartition - Add an interval to the internal list of intervals,
30// and then add mappings from all of the basic blocks in the interval to the
31// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000032//
Chris Lattner23e36622001-06-25 03:55:04 +000033void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattnere6850232001-07-03 15:28:35 +000034 push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000035
36 // Add mappings for all of the basic blocks in I to the IntervalPartition
37 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
38 It != End; ++It)
Chris Lattner23e36622001-06-25 03:55:04 +000039 IntervalMap.insert(make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000040}
41
Chris Lattner2d676c92001-06-24 04:07:44 +000042// updatePredecessors - Interval generation only sets the successor fields of
43// the interval data structures. After interval generation is complete,
44// run through all of the intervals and propogate successor info as
45// predecessor info.
46//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000047void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000048 BasicBlock *Header = Int->getHeaderNode();
49 for (Interval::succ_iterator I = Int->Successors.begin(),
50 E = Int->Successors.end(); I != E; ++I)
51 getBlockInterval(*I)->Predecessors.push_back(Header);
52}
53
Chris Lattner2d676c92001-06-24 04:07:44 +000054// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000055// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000056//
Chris Lattner7e708292002-06-25 16:13:24 +000057bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000058 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000059 function_interval_iterator I = intervals_begin(&F, false);
60 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000061
62 addIntervalToPartition(RootInterval = *I);
63
Chris Lattner7fc9fe32001-06-27 23:41:11 +000064 ++I; // After the first one...
65
66 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000067 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000068 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000069
70 // Now that we know all of the successor information, propogate this to the
71 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +000072 for_each(begin(), end(),
73 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000074 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000075}
76
77
78// IntervalPartition ctor - Build a reduced interval partition from an
79// existing interval graph. This takes an additional boolean parameter to
80// distinguish it from a copy constructor. Always pass in false for now.
81//
Chris Lattner23e36622001-06-25 03:55:04 +000082IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000083 Interval *FunctionStart = IP.getRootInterval();
84 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000085
Chris Lattner23e36622001-06-25 03:55:04 +000086 // Pass false to intervals_begin because we take ownership of it's memory
87 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000088 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000089
90 addIntervalToPartition(RootInterval = *I);
91
Chris Lattner7fc9fe32001-06-27 23:41:11 +000092 ++I; // After the first one...
93
94 // Add the rest of the intervals to the partition...
95 for_each(I, intervals_end(IP),
96 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000097
98 // Now that we know all of the successor information, propogate this to the
99 // predecessors for each block...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000100 for_each(begin(), end(),
101 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000102}