blob: 71c96cc94cdc2f693881a93055966da5113416ca [file] [log] [blame]
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00001//===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of the LiveRangeCalc class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "regalloc"
15#include "LiveRangeCalc.h"
16#include "llvm/CodeGen/MachineDominators.h"
17
18using namespace llvm;
19
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000020void LiveRangeCalc::reset(const MachineFunction *MF,
21 SlotIndexes *SI,
22 MachineDominatorTree *MDT,
23 VNInfo::Allocator *VNIA) {
24 MRI = &MF->getRegInfo();
25 Indexes = SI;
26 DomTree = MDT;
27 Alloc = VNIA;
28
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000029 unsigned N = MF->getNumBlockIDs();
30 Seen.clear();
31 Seen.resize(N);
32 LiveOut.resize(N);
33 LiveIn.clear();
34}
35
36
37// Transfer information from the LiveIn vector to the live ranges.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000038void LiveRangeCalc::updateLiveIns(VNInfo *OverrideVNI) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000039 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
40 E = LiveIn.end(); I != E; ++I) {
41 if (!I->DomNode)
42 continue;
43 MachineBasicBlock *MBB = I->DomNode->getBlock();
44
45 VNInfo *VNI = OverrideVNI ? OverrideVNI : I->Value;
46 assert(VNI && "No live-in value found");
47
48 SlotIndex Start, End;
49 tie(Start, End) = Indexes->getMBBRange(MBB);
50
51 if (I->Kill.isValid())
52 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
53 else {
54 I->LI->addRange(LiveRange(Start, End, VNI));
55 // The value is live-through, update LiveOut as well. Defer the Domtree
56 // lookup until it is needed.
57 assert(Seen.test(MBB->getNumber()));
NAKAMURA Takumiedd4f8b2011-09-13 03:58:34 +000058 LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000059 }
60 }
61 LiveIn.clear();
62}
63
64
65void LiveRangeCalc::extend(LiveInterval *LI,
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000066 SlotIndex Kill) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000067 assert(LI && "Missing live range");
68 assert(Kill.isValid() && "Invalid SlotIndex");
69 assert(Indexes && "Missing SlotIndexes");
70 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000071
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +000072 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hamesaa134822011-12-20 20:23:40 +000073 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000074
75 // Is there a def in the same MBB we can extend?
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +000076 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000077 return;
78
79 // Find the single reaching def, or determine if Kill is jointly dominated by
80 // multiple values, and we may need to create even more phi-defs to preserve
81 // VNInfo SSA form. Perform a search for all predecessor blocks where we
82 // know the dominating VNInfo.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000083 VNInfo *VNI = findReachingDefs(LI, KillMBB, Kill);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000084
85 // When there were multiple different values, we may need new PHIs.
86 if (!VNI)
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000087 updateSSA();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000088
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000089 updateLiveIns(VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000090}
91
92
93// This function is called by a client after using the low-level API to add
94// live-out and live-in blocks. The unique value optimization is not
95// available, SplitEditor::transferValues handles that case directly anyway.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000096void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000097 assert(Indexes && "Missing SlotIndexes");
98 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000099 updateSSA();
100 updateLiveIns(0);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000101}
102
103
104VNInfo *LiveRangeCalc::findReachingDefs(LiveInterval *LI,
105 MachineBasicBlock *KillMBB,
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000106 SlotIndex Kill) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000107 // Blocks where LI should be live-in.
108 SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB);
109
110 // Remember if we have seen more than one value.
111 bool UniqueVNI = true;
112 VNInfo *TheVNI = 0;
113
114 // Using Seen as a visited set, perform a BFS for all reaching defs.
115 for (unsigned i = 0; i != WorkList.size(); ++i) {
116 MachineBasicBlock *MBB = WorkList[i];
117 assert(!MBB->pred_empty() && "Value live-in to entry block?");
118 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
119 PE = MBB->pred_end(); PI != PE; ++PI) {
120 MachineBasicBlock *Pred = *PI;
121
122 // Is this a known live-out block?
123 if (Seen.test(Pred->getNumber())) {
124 if (VNInfo *VNI = LiveOut[Pred].first) {
125 if (TheVNI && TheVNI != VNI)
126 UniqueVNI = false;
127 TheVNI = VNI;
128 }
129 continue;
130 }
131
132 SlotIndex Start, End;
133 tie(Start, End) = Indexes->getMBBRange(Pred);
134
135 // First time we see Pred. Try to determine the live-out value, but set
136 // it as null if Pred is live-through with an unknown value.
Jakob Stoklund Olesenee5655d2011-09-13 16:47:56 +0000137 VNInfo *VNI = LI->extendInBlock(Start, End);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000138 setLiveOutValue(Pred, VNI);
139 if (VNI) {
140 if (TheVNI && TheVNI != VNI)
141 UniqueVNI = false;
142 TheVNI = VNI;
143 continue;
144 }
145
146 // No, we need a live-in value for Pred as well
147 if (Pred != KillMBB)
148 WorkList.push_back(Pred);
149 else
150 // Loopback to KillMBB, so value is really live through.
151 Kill = SlotIndex();
152 }
153 }
154
155 // Transfer WorkList to LiveInBlocks in reverse order.
156 // This ordering works best with updateSSA().
157 LiveIn.clear();
158 LiveIn.reserve(WorkList.size());
159 while(!WorkList.empty())
160 addLiveInBlock(LI, DomTree->getNode(WorkList.pop_back_val()));
161
162 // The kill block may not be live-through.
163 assert(LiveIn.back().DomNode->getBlock() == KillMBB);
164 LiveIn.back().Kill = Kill;
165
166 return UniqueVNI ? TheVNI : 0;
167}
168
169
170// This is essentially the same iterative algorithm that SSAUpdater uses,
171// except we already have a dominator tree, so we don't have to recompute it.
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +0000172void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000173 assert(Indexes && "Missing SlotIndexes");
174 assert(DomTree && "Missing dominator tree");
175
176 // Interate until convergence.
177 unsigned Changes;
178 do {
179 Changes = 0;
180 // Propagate live-out values down the dominator tree, inserting phi-defs
181 // when necessary.
182 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
183 E = LiveIn.end(); I != E; ++I) {
184 MachineDomTreeNode *Node = I->DomNode;
185 // Skip block if the live-in value has already been determined.
186 if (!Node)
187 continue;
188 MachineBasicBlock *MBB = Node->getBlock();
189 MachineDomTreeNode *IDom = Node->getIDom();
190 LiveOutPair IDomValue;
191
192 // We need a live-in value to a block with no immediate dominator?
193 // This is probably an unreachable block that has survived somehow.
194 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
195
196 // IDom dominates all of our predecessors, but it may not be their
197 // immediate dominator. Check if any of them have live-out values that are
198 // properly dominated by IDom. If so, we need a phi-def here.
199 if (!needPHI) {
200 IDomValue = LiveOut[IDom->getBlock()];
201
202 // Cache the DomTree node that defined the value.
203 if (IDomValue.first && !IDomValue.second)
204 LiveOut[IDom->getBlock()].second = IDomValue.second =
205 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
206
207 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
208 PE = MBB->pred_end(); PI != PE; ++PI) {
209 LiveOutPair &Value = LiveOut[*PI];
210 if (!Value.first || Value.first == IDomValue.first)
211 continue;
212
213 // Cache the DomTree node that defined the value.
214 if (!Value.second)
215 Value.second =
216 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
217
218 // This predecessor is carrying something other than IDomValue.
219 // It could be because IDomValue hasn't propagated yet, or it could be
220 // because MBB is in the dominance frontier of that value.
221 if (DomTree->dominates(IDom, Value.second)) {
222 needPHI = true;
223 break;
224 }
225 }
226 }
227
228 // The value may be live-through even if Kill is set, as can happen when
229 // we are called from extendRange. In that case LiveOutSeen is true, and
230 // LiveOut indicates a foreign or missing value.
231 LiveOutPair &LOP = LiveOut[MBB];
232
233 // Create a phi-def if required.
234 if (needPHI) {
235 ++Changes;
236 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
237 SlotIndex Start, End;
238 tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000239 VNInfo *VNI = I->LI->getNextValue(Start, *Alloc);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000240 VNI->setIsPHIDef(true);
241 I->Value = VNI;
242 // This block is done, we know the final value.
243 I->DomNode = 0;
244
245 // Add liveness since updateLiveIns now skips this node.
246 if (I->Kill.isValid())
247 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
248 else {
249 I->LI->addRange(LiveRange(Start, End, VNI));
250 LOP = LiveOutPair(VNI, Node);
251 }
252 } else if (IDomValue.first) {
253 // No phi-def here. Remember incoming value.
254 I->Value = IDomValue.first;
255
256 // If the IDomValue is killed in the block, don't propagate through.
257 if (I->Kill.isValid())
258 continue;
259
260 // Propagate IDomValue if it isn't killed:
261 // MBB is live-out and doesn't define its own value.
262 if (LOP.first == IDomValue.first)
263 continue;
264 ++Changes;
265 LOP = IDomValue;
266 }
267 }
268 } while (Changes);
269}