blob: cb8a85da552a39d93ab9ca777e75cc113f923def [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- IntervalPartition.cpp - Interval Partition module code -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the definition of the IntervalPartition class, which
11// calculates and represent the interval partition of a function.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/IntervalIterator.h"
16using namespace llvm;
17
18char IntervalPartition::ID = 0;
19static RegisterPass<IntervalPartition>
Devang Patele43f5132008-03-20 23:27:18 +000020X("intervals", "Interval Partition Construction", true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021
22//===----------------------------------------------------------------------===//
23// IntervalPartition Implementation
24//===----------------------------------------------------------------------===//
25
Chris Lattner1553bf72008-08-28 22:56:53 +000026// releaseMemory - Reset state back to before function was analyzed
27void IntervalPartition::releaseMemory() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
29 delete Intervals[i];
30 IntervalMap.clear();
Chris Lattner974d54e2008-08-28 03:33:03 +000031 Intervals.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 RootInterval = 0;
33}
34
35void IntervalPartition::print(std::ostream &O, const Module*) const {
36 for(unsigned i = 0, e = Intervals.size(); i != e; ++i)
37 Intervals[i]->print(O);
38}
39
40// addIntervalToPartition - Add an interval to the internal list of intervals,
41// and then add mappings from all of the basic blocks in the interval to the
42// interval itself (in the IntervalMap).
43//
44void IntervalPartition::addIntervalToPartition(Interval *I) {
45 Intervals.push_back(I);
46
47 // Add mappings for all of the basic blocks in I to the IntervalPartition
48 for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
49 It != End; ++It)
50 IntervalMap.insert(std::make_pair(*It, I));
51}
52
53// updatePredecessors - Interval generation only sets the successor fields of
54// the interval data structures. After interval generation is complete,
55// run through all of the intervals and propagate successor info as
56// predecessor info.
57//
58void IntervalPartition::updatePredecessors(Interval *Int) {
59 BasicBlock *Header = Int->getHeaderNode();
60 for (Interval::succ_iterator I = Int->Successors.begin(),
61 E = Int->Successors.end(); I != E; ++I)
62 getBlockInterval(*I)->Predecessors.push_back(Header);
63}
64
65// IntervalPartition ctor - Build the first level interval partition for the
66// specified function...
67//
68bool IntervalPartition::runOnFunction(Function &F) {
69 // Pass false to intervals_begin because we take ownership of it's memory
70 function_interval_iterator I = intervals_begin(&F, false);
71 assert(I != intervals_end(&F) && "No intervals in function!?!?!");
72
73 addIntervalToPartition(RootInterval = *I);
74
75 ++I; // After the first one...
76
77 // Add the rest of the intervals to the partition.
78 for (function_interval_iterator E = intervals_end(&F); I != E; ++I)
79 addIntervalToPartition(*I);
80
81 // Now that we know all of the successor information, propagate this to the
82 // predecessors for each block.
83 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
84 updatePredecessors(Intervals[i]);
85 return false;
86}
87
88
89// IntervalPartition ctor - Build a reduced interval partition from an
90// existing interval graph. This takes an additional boolean parameter to
91// distinguish it from a copy constructor. Always pass in false for now.
92//
93IntervalPartition::IntervalPartition(IntervalPartition &IP, bool)
Dan Gohman26f8c272008-09-04 17:05:41 +000094 : FunctionPass(&ID) {
Chris Lattner45f255d2008-06-21 19:48:22 +000095 assert(IP.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // Pass false to intervals_begin because we take ownership of it's memory
98 interval_part_interval_iterator I = intervals_begin(IP, false);
99 assert(I != intervals_end(IP) && "No intervals in interval partition!?!?!");
100
101 addIntervalToPartition(RootInterval = *I);
102
103 ++I; // After the first one...
104
105 // 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);
108
109 // Now that we know all of the successor information, propagate this to the
110 // predecessors for each block.
111 for (unsigned i = 0, e = Intervals.size(); i != e; ++i)
112 updatePredecessors(Intervals[i]);
113}
114