1 //===- IntervalPartition.cpp - Interval Partition module code -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the definition of the IntervalPartition class, which
11 // calculates and represent the interval partition of a function.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/IntervalPartition.h"
16 #include "llvm/Analysis/Interval.h"
17 #include "llvm/Analysis/IntervalIterator.h"
18 #include "llvm/Pass.h"
24 char IntervalPartition::ID
= 0;
26 INITIALIZE_PASS(IntervalPartition
, "intervals",
27 "Interval Partition Construction", true, true)
29 //===----------------------------------------------------------------------===//
30 // IntervalPartition Implementation
31 //===----------------------------------------------------------------------===//
33 // releaseMemory - Reset state back to before function was analyzed
34 void IntervalPartition::releaseMemory() {
35 for (unsigned i
= 0, e
= Intervals
.size(); i
!= e
; ++i
)
39 RootInterval
= nullptr;
42 void IntervalPartition::print(raw_ostream
&O
, const Module
*) const {
43 for(unsigned i
= 0, e
= Intervals
.size(); i
!= e
; ++i
)
44 Intervals
[i
]->print(O
);
47 // addIntervalToPartition - Add an interval to the internal list of intervals,
48 // and then add mappings from all of the basic blocks in the interval to the
49 // interval itself (in the IntervalMap).
50 void IntervalPartition::addIntervalToPartition(Interval
*I
) {
51 Intervals
.push_back(I
);
53 // Add mappings for all of the basic blocks in I to the IntervalPartition
54 for (Interval::node_iterator It
= I
->Nodes
.begin(), End
= I
->Nodes
.end();
56 IntervalMap
.insert(std::make_pair(*It
, I
));
59 // updatePredecessors - Interval generation only sets the successor fields of
60 // the interval data structures. After interval generation is complete,
61 // run through all of the intervals and propagate successor info as
63 void IntervalPartition::updatePredecessors(Interval
*Int
) {
64 BasicBlock
*Header
= Int
->getHeaderNode();
65 for (BasicBlock
*Successor
: Int
->Successors
)
66 getBlockInterval(Successor
)->Predecessors
.push_back(Header
);
69 // IntervalPartition ctor - Build the first level interval partition for the
70 // specified function...
71 bool IntervalPartition::runOnFunction(Function
&F
) {
72 // Pass false to intervals_begin because we take ownership of it's memory
73 function_interval_iterator I
= intervals_begin(&F
, false);
74 assert(I
!= intervals_end(&F
) && "No intervals in function!?!?!");
76 addIntervalToPartition(RootInterval
= *I
);
78 ++I
; // After the first one...
80 // Add the rest of the intervals to the partition.
81 for (function_interval_iterator E
= intervals_end(&F
); I
!= E
; ++I
)
82 addIntervalToPartition(*I
);
84 // Now that we know all of the successor information, propagate this to the
85 // predecessors for each block.
86 for (unsigned i
= 0, e
= Intervals
.size(); i
!= e
; ++i
)
87 updatePredecessors(Intervals
[i
]);
91 // IntervalPartition ctor - Build a reduced interval partition from an
92 // existing interval graph. This takes an additional boolean parameter to
93 // distinguish it from a copy constructor. Always pass in false for now.
94 IntervalPartition::IntervalPartition(IntervalPartition
&IP
, bool)
96 assert(IP
.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
98 // Pass false to intervals_begin because we take ownership of it's memory
99 interval_part_interval_iterator I
= intervals_begin(IP
, false);
100 assert(I
!= intervals_end(IP
) && "No intervals in interval partition!?!?!");
102 addIntervalToPartition(RootInterval
= *I
);
104 ++I
; // After the first one...
106 // Add the rest of the intervals to the partition.
107 for (interval_part_interval_iterator E
= intervals_end(IP
); I
!= E
; ++I
)
108 addIntervalToPartition(*I
);
110 // Now that we know all of the successor information, propagate this to the
111 // predecessors for each block.
112 for (unsigned i
= 0, e
= Intervals
.size(); i
!= e
; ++i
)
113 updatePredecessors(Intervals
[i
]);