blob: f0ed32881e408185760e0c472269a076f6838e06 [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>
Chris Lattner17689df2002-07-30 16:27:52 +000014X("intervals", "Interval Partition Construction", true);
Chris Lattner1e435162002-07-26 21:12:44 +000015
Chris Lattnera59cbb22002-07-27 01:12:17 +000016AnalysisID IntervalPartition::ID = X;
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 Lattner38969482002-08-09 22:52:08 +000024 for_each(Intervals.begin(), Intervals.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 Lattner1b0a63f2002-07-31 19:32:01 +000029void IntervalPartition::print(std::ostream &O) const {
Chris Lattner38969482002-08-09 22:52:08 +000030 std::copy(Intervals.begin(), Intervals.end(),
Chris Lattnera59cbb22002-07-27 01:12:17 +000031 std::ostream_iterator<const Interval *>(O, "\n"));
32}
33
Chris Lattner23e36622001-06-25 03:55:04 +000034// addIntervalToPartition - Add an interval to the internal list of intervals,
35// and then add mappings from all of the basic blocks in the interval to the
36// interval itself (in the IntervalMap).
Chris Lattner2d676c92001-06-24 04:07:44 +000037//
Chris Lattner23e36622001-06-25 03:55:04 +000038void IntervalPartition::addIntervalToPartition(Interval *I) {
Chris Lattner38969482002-08-09 22:52:08 +000039 Intervals.push_back(I);
Chris Lattner2d676c92001-06-24 04:07:44 +000040
41 // Add mappings for all of the basic blocks in I to the IntervalPartition
42 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
43 It != End; ++It)
Chris Lattner23e36622001-06-25 03:55:04 +000044 IntervalMap.insert(make_pair(*It, I));
Chris Lattner2d676c92001-06-24 04:07:44 +000045}
46
Chris Lattner2d676c92001-06-24 04:07:44 +000047// updatePredecessors - Interval generation only sets the successor fields of
48// the interval data structures. After interval generation is complete,
49// run through all of the intervals and propogate successor info as
50// predecessor info.
51//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000052void IntervalPartition::updatePredecessors(Interval *Int) {
Chris Lattner2d676c92001-06-24 04:07:44 +000053 BasicBlock *Header = Int->getHeaderNode();
54 for (Interval::succ_iterator I = Int->Successors.begin(),
55 E = Int->Successors.end(); I != E; ++I)
56 getBlockInterval(*I)->Predecessors.push_back(Header);
57}
58
Chris Lattner2d676c92001-06-24 04:07:44 +000059// IntervalPartition ctor - Build the first level interval partition for the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000060// specified function...
Chris Lattner2d676c92001-06-24 04:07:44 +000061//
Chris Lattner7e708292002-06-25 16:13:24 +000062bool IntervalPartition::runOnFunction(Function &F) {
Chris Lattner23e36622001-06-25 03:55:04 +000063 // Pass false to intervals_begin because we take ownership of it's memory
Chris Lattner7e708292002-06-25 16:13:24 +000064 function_interval_iterator I = intervals_begin(&F, false);
65 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000066
67 addIntervalToPartition(RootInterval = *I);
68
Chris Lattner7fc9fe32001-06-27 23:41:11 +000069 ++I; // After the first one...
70
71 // Add the rest of the intervals to the partition...
Chris Lattner7e708292002-06-25 16:13:24 +000072 for_each(I, intervals_end(&F),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000073 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +000074
75 // Now that we know all of the successor information, propogate this to the
76 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +000077 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +000078 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner93193f82002-01-31 00:42:27 +000079 return false;
Chris Lattner2d676c92001-06-24 04:07:44 +000080}
81
82
83// IntervalPartition ctor - Build a reduced interval partition from an
84// existing interval graph. This takes an additional boolean parameter to
85// distinguish it from a copy constructor. Always pass in false for now.
86//
Chris Lattner23e36622001-06-25 03:55:04 +000087IntervalPartition::IntervalPartition(IntervalPartition &IP, bool) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000088 Interval *FunctionStart = IP.getRootInterval();
89 assert(FunctionStart && "Cannot operate on empty IntervalPartitions!");
Chris Lattner2d676c92001-06-24 04:07:44 +000090
Chris Lattner23e36622001-06-25 03:55:04 +000091 // Pass false to intervals_begin because we take ownership of it's memory
92 interval_part_interval_iterator I = intervals_begin(IP, false);
Chris Lattner7fc9fe32001-06-27 23:41:11 +000093 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
Chris Lattner23e36622001-06-25 03:55:04 +000094
95 addIntervalToPartition(RootInterval = *I);
96
Chris Lattner7fc9fe32001-06-27 23:41:11 +000097 ++I; // After the first one...
98
99 // Add the rest of the intervals to the partition...
100 for_each(I, intervals_end(IP),
101 bind_obj(this, &IntervalPartition::addIntervalToPartition));
Chris Lattner2d676c92001-06-24 04:07:44 +0000102
103 // Now that we know all of the successor information, propogate this to the
104 // predecessors for each block...
Chris Lattner38969482002-08-09 22:52:08 +0000105 for_each(Intervals.begin(), Intervals.end(),
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000106 bind_obj(this, &IntervalPartition::updatePredecessors));
Chris Lattner2d676c92001-06-24 04:07:44 +0000107}