1 //===- IntervalPartition.cpp - Interval Partition module code -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the definition of the IntervalPartition class, which
10 // calculates and represent the interval partition of a function.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/IntervalPartition.h"
15 #include "llvm/Analysis/Interval.h"
16 #include "llvm/Analysis/IntervalIterator.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
24 char IntervalPartition::ID
= 0;
26 IntervalPartition::IntervalPartition() : FunctionPass(ID
) {
27 initializeIntervalPartitionPass(*PassRegistry::getPassRegistry());
30 INITIALIZE_PASS(IntervalPartition
, "intervals",
31 "Interval Partition Construction", true, true)
33 //===----------------------------------------------------------------------===//
34 // IntervalPartition Implementation
35 //===----------------------------------------------------------------------===//
37 // releaseMemory - Reset state back to before function was analyzed
38 void IntervalPartition::releaseMemory() {
39 for (Interval
*I
: Intervals
)
43 RootInterval
= nullptr;
46 void IntervalPartition::print(raw_ostream
&O
, const Module
*) const {
47 for (const Interval
*I
: Intervals
)
51 // addIntervalToPartition - Add an interval to the internal list of intervals,
52 // and then add mappings from all of the basic blocks in the interval to the
53 // interval itself (in the IntervalMap).
54 void IntervalPartition::addIntervalToPartition(Interval
*I
) {
55 Intervals
.push_back(I
);
57 // Add mappings for all of the basic blocks in I to the IntervalPartition
58 for (Interval::node_iterator It
= I
->Nodes
.begin(), End
= I
->Nodes
.end();
60 IntervalMap
.insert(std::make_pair(*It
, I
));
63 // updatePredecessors - Interval generation only sets the successor fields of
64 // the interval data structures. After interval generation is complete,
65 // run through all of the intervals and propagate successor info as
67 void IntervalPartition::updatePredecessors(Interval
*Int
) {
68 BasicBlock
*Header
= Int
->getHeaderNode();
69 for (BasicBlock
*Successor
: Int
->Successors
)
70 getBlockInterval(Successor
)->Predecessors
.push_back(Header
);
73 // IntervalPartition ctor - Build the first level interval partition for the
74 // specified function...
75 bool IntervalPartition::runOnFunction(Function
&F
) {
76 // Pass false to intervals_begin because we take ownership of it's memory
77 function_interval_iterator I
= intervals_begin(&F
, false);
78 assert(I
!= intervals_end(&F
) && "No intervals in function!?!?!");
80 addIntervalToPartition(RootInterval
= *I
);
82 ++I
; // After the first one...
84 // Add the rest of the intervals to the partition.
85 for (function_interval_iterator E
= intervals_end(&F
); I
!= E
; ++I
)
86 addIntervalToPartition(*I
);
88 // Now that we know all of the successor information, propagate this to the
89 // predecessors for each block.
90 for (Interval
*I
: Intervals
)
91 updatePredecessors(I
);
95 // IntervalPartition ctor - Build a reduced interval partition from an
96 // existing interval graph. This takes an additional boolean parameter to
97 // distinguish it from a copy constructor. Always pass in false for now.
98 IntervalPartition::IntervalPartition(IntervalPartition
&IP
, bool)
100 assert(IP
.getRootInterval() && "Cannot operate on empty IntervalPartitions!");
102 // Pass false to intervals_begin because we take ownership of it's memory
103 interval_part_interval_iterator I
= intervals_begin(IP
, false);
104 assert(I
!= intervals_end(IP
) && "No intervals in interval partition!?!?!");
106 addIntervalToPartition(RootInterval
= *I
);
108 ++I
; // After the first one...
110 // Add the rest of the intervals to the partition.
111 for (interval_part_interval_iterator E
= intervals_end(IP
); I
!= E
; ++I
)
112 addIntervalToPartition(*I
);
114 // Now that we know all of the successor information, propagate this to the
115 // predecessors for each block.
116 for (Interval
*I
: Intervals
)
117 updatePredecessors(I
);