llvm.org GIT mirror llvm / 4aad88d
Combine the implementations of the core part of the SSAUpdater and MachineSSAUpdater to avoid duplicating all the code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@103060 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Wilson 10 years ago
5 changed file(s) with 709 addition(s) and 929 deletion(s). Raw diff Collapse all Expand all
2222 class TargetInstrInfo;
2323 class TargetRegisterClass;
2424 template class SmallVectorImpl;
25 template class SSAUpdaterTraits;
2526 class BumpPtrAllocator;
2627
2728 /// MachineSSAUpdater - This class updates SSA form for a set of virtual
2930 /// or another unstructured transformation wants to rewrite a set of uses of one
3031 /// vreg with uses of a set of vregs.
3132 class MachineSSAUpdater {
32 public:
33 class BBInfo;
34 typedef SmallVectorImpl BlockListTy;
33 friend class SSAUpdaterTraits;
3534
3635 private:
3736 /// AvailableVals - This keeps track of which value to use on a per-block
3837 /// basis. When we insert PHI nodes, we keep track of them here.
3938 //typedef DenseMap AvailableValsTy;
4039 void *AV;
41
42 /// BBMap - The GetValueAtEndOfBlock method maintains this mapping from
43 /// basic blocks to BBInfo structures.
44 /// typedef DenseMap BBMapTy;
45 void *BM;
4640
4741 /// VR - Current virtual register whose uses are being updated.
4842 unsigned VR;
110104 private:
111105 void ReplaceRegWith(unsigned OldReg, unsigned NewReg);
112106 unsigned GetValueAtEndOfBlockInternal(MachineBasicBlock *BB);
113 void BuildBlockList(MachineBasicBlock *BB, BlockListTy *BlockList,
114 BumpPtrAllocator *Allocator);
115 void FindDominators(BlockListTy *BlockList);
116 void FindPHIPlacement(BlockListTy *BlockList);
117 void FindAvailableVals(BlockListTy *BlockList);
118 void FindExistingPHI(MachineBasicBlock *BB, BlockListTy *BlockList);
119 bool CheckIfPHIMatches(MachineInstr *PHI);
120 void RecordMatchingPHI(MachineInstr *PHI);
121107
122108 void operator=(const MachineSSAUpdater&); // DO NOT IMPLEMENT
123109 MachineSSAUpdater(const MachineSSAUpdater&); // DO NOT IMPLEMENT
1818 class BasicBlock;
1919 class Use;
2020 class PHINode;
21 template
22 class SmallVectorImpl;
21 template class SmallVectorImpl;
22 template class SSAUpdaterTraits;
2323 class BumpPtrAllocator;
2424
2525 /// SSAUpdater - This class updates SSA form for a set of values defined in
2727 /// transformation wants to rewrite a set of uses of one value with uses of a
2828 /// set of values.
2929 class SSAUpdater {
30 public:
31 class BBInfo;
32 typedef SmallVectorImpl BlockListTy;
30 friend class SSAUpdaterTraits;
3331
3432 private:
3533 /// AvailableVals - This keeps track of which value to use on a per-block
4139 /// and a type for PHI nodes.
4240 Value *PrototypeValue;
4341
44 /// BBMap - The GetValueAtEndOfBlock method maintains this mapping from
45 /// basic blocks to BBInfo structures.
46 /// typedef DenseMap BBMapTy;
47 void *BM;
48
4942 /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that
5043 /// it creates to the vector.
5144 SmallVectorImpl *InsertedPHIs;
45
5246 public:
5347 /// SSAUpdater constructor. If InsertedPHIs is specified, it will be filled
5448 /// in with all PHI Nodes created by rewriting.
10195
10296 private:
10397 Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
104 void BuildBlockList(BasicBlock *BB, BlockListTy *BlockList,
105 BumpPtrAllocator *Allocator);
106 void FindDominators(BlockListTy *BlockList);
107 void FindPHIPlacement(BlockListTy *BlockList);
108 void FindAvailableVals(BlockListTy *BlockList);
109 void FindExistingPHI(BasicBlock *BB, BlockListTy *BlockList);
110 bool CheckIfPHIMatches(PHINode *PHI);
111 void RecordMatchingPHI(PHINode *PHI);
11298
11399 void operator=(const SSAUpdater&); // DO NOT IMPLEMENT
114100 SSAUpdater(const SSAUpdater&); // DO NOT IMPLEMENT
0 //===-- SSAUpdaterImpl.h - SSA Updater Implementation -----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides a template that implements the core algorithm for the
10 // SSAUpdater and MachineSSAUpdater.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
15 #define LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
16
17 namespace llvm {
18
19 template class SSAUpdaterTraits;
20
21 template
22 class SSAUpdaterImpl {
23 private:
24 UpdaterT *Updater;
25
26 typedef SSAUpdaterTraits Traits;
27 typedef typename Traits::BlkT BlkT;
28 typedef typename Traits::ValT ValT;
29 typedef typename Traits::PhiT PhiT;
30
31 /// BBInfo - Per-basic block information used internally by SSAUpdaterImpl.
32 /// The predecessors of each block are cached here since pred_iterator is
33 /// slow and we need to iterate over the blocks at least a few times.
34 class BBInfo {
35 public:
36 BlkT *BB; // Back-pointer to the corresponding block.
37 ValT AvailableVal; // Value to use in this block.
38 BBInfo *DefBB; // Block that defines the available value.
39 int BlkNum; // Postorder number.
40 BBInfo *IDom; // Immediate dominator.
41 unsigned NumPreds; // Number of predecessor blocks.
42 BBInfo **Preds; // Array[NumPreds] of predecessor blocks.
43 PhiT *PHITag; // Marker for existing PHIs that match.
44
45 BBInfo(BlkT *ThisBB, ValT V)
46 : BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0),
47 NumPreds(0), Preds(0), PHITag(0) { }
48 };
49
50 typedef DenseMap AvailableValsTy;
51 AvailableValsTy *AvailableVals;
52
53 SmallVectorImpl *InsertedPHIs;
54
55 typedef SmallVectorImpl BlockListTy;
56 typedef DenseMap BBMapTy;
57 BBMapTy BBMap;
58 BumpPtrAllocator Allocator;
59
60 public:
61 explicit SSAUpdaterImpl(UpdaterT *U, AvailableValsTy *A,
62 SmallVectorImpl *Ins) :
63 Updater(U), AvailableVals(A), InsertedPHIs(Ins) { }
64
65 /// GetValue - Check to see if AvailableVals has an entry for the specified
66 /// BB and if so, return it. If not, construct SSA form by first
67 /// calculating the required placement of PHIs and then inserting new PHIs
68 /// where needed.
69 ValT GetValue(BlkT *BB) {
70 SmallVector BlockList;
71 BuildBlockList(BB, &BlockList);
72
73 // Special case: bail out if BB is unreachable.
74 if (BlockList.size() == 0) {
75 ValT V = Traits::GetUndefVal(BB, Updater);
76 (*AvailableVals)[BB] = V;
77 return V;
78 }
79
80 FindDominators(&BlockList);
81 FindPHIPlacement(&BlockList);
82 FindAvailableVals(&BlockList);
83
84 return BBMap[BB]->DefBB->AvailableVal;
85 }
86
87 /// BuildBlockList - Starting from the specified basic block, traverse back
88 /// through its predecessors until reaching blocks with known values.
89 /// Create BBInfo structures for the blocks and append them to the block
90 /// list.
91 void BuildBlockList(BlkT *BB, BlockListTy *BlockList) {
92 SmallVector RootList;
93 SmallVector WorkList;
94
95 BBInfo *Info = new (Allocator) BBInfo(BB, 0);
96 BBMap[BB] = Info;
97 WorkList.push_back(Info);
98
99 // Search backward from BB, creating BBInfos along the way and stopping
100 // when reaching blocks that define the value. Record those defining
101 // blocks on the RootList.
102 SmallVector Preds;
103 while (!WorkList.empty()) {
104 Info = WorkList.pop_back_val();
105 Preds.clear();
106 Traits::FindPredecessorBlocks(Info->BB, &Preds);
107 Info->NumPreds = Preds.size();
108 Info->Preds = static_cast
109 (Allocator.Allocate(Info->NumPreds * sizeof(BBInfo*),
110 AlignOf::Alignment));
111
112 // Treat an unreachable predecessor as a definition with 'undef'.
113 if (Info->NumPreds == 0) {
114 Info->AvailableVal = Traits::GetUndefVal(Info->BB, Updater);
115 Info->DefBB = Info;
116 RootList.push_back(Info);
117 continue;
118 }
119
120 for (unsigned p = 0; p != Info->NumPreds; ++p) {
121 BlkT *Pred = Preds[p];
122 // Check if BBMap already has a BBInfo for the predecessor block.
123 typename BBMapTy::value_type &BBMapBucket =
124 BBMap.FindAndConstruct(Pred);
125 if (BBMapBucket.second) {
126 Info->Preds[p] = BBMapBucket.second;
127 continue;
128 }
129
130 // Create a new BBInfo for the predecessor.
131 ValT PredVal = AvailableVals->lookup(Pred);
132 BBInfo *PredInfo = new (Allocator) BBInfo(Pred, PredVal);
133 BBMapBucket.second = PredInfo;
134 Info->Preds[p] = PredInfo;
135
136 if (PredInfo->AvailableVal) {
137 RootList.push_back(PredInfo);
138 continue;
139 }
140 WorkList.push_back(PredInfo);
141 }
142 }
143
144 // Now that we know what blocks are backwards-reachable from the starting
145 // block, do a forward depth-first traversal to assign postorder numbers
146 // to those blocks.
147 BBInfo *PseudoEntry = new (Allocator) BBInfo(0, 0);
148 unsigned BlkNum = 1;
149
150 // Initialize the worklist with the roots from the backward traversal.
151 while (!RootList.empty()) {
152 Info = RootList.pop_back_val();
153 Info->IDom = PseudoEntry;
154 Info->BlkNum = -1;
155 WorkList.push_back(Info);
156 }
157
158 while (!WorkList.empty()) {
159 Info = WorkList.back();
160
161 if (Info->BlkNum == -2) {
162 // All the successors have been handled; assign the postorder number.
163 Info->BlkNum = BlkNum++;
164 // If not a root, put it on the BlockList.
165 if (!Info->AvailableVal)
166 BlockList->push_back(Info);
167 WorkList.pop_back();
168 continue;
169 }
170
171 // Leave this entry on the worklist, but set its BlkNum to mark that its
172 // successors have been put on the worklist. When it returns to the top
173 // the list, after handling its successors, it will be assigned a
174 // number.
175 Info->BlkNum = -2;
176
177 // Add unvisited successors to the work list.
178 for (typename Traits::BlkSucc_iterator SI =
179 Traits::BlkSucc_begin(Info->BB),
180 E = Traits::BlkSucc_end(Info->BB); SI != E; ++SI) {
181 BBInfo *SuccInfo = BBMap[*SI];
182 if (!SuccInfo || SuccInfo->BlkNum)
183 continue;
184 SuccInfo->BlkNum = -1;
185 WorkList.push_back(SuccInfo);
186 }
187 }
188 PseudoEntry->BlkNum = BlkNum;
189 }
190
191 /// IntersectDominators - This is the dataflow lattice "meet" operation for
192 /// finding dominators. Given two basic blocks, it walks up the dominator
193 /// tree until it finds a common dominator of both. It uses the postorder
194 /// number of the blocks to determine how to do that.
195 BBInfo *IntersectDominators(BBInfo *Blk1, BBInfo *Blk2) {
196 while (Blk1 != Blk2) {
197 while (Blk1->BlkNum < Blk2->BlkNum) {
198 Blk1 = Blk1->IDom;
199 if (!Blk1)
200 return Blk2;
201 }
202 while (Blk2->BlkNum < Blk1->BlkNum) {
203 Blk2 = Blk2->IDom;
204 if (!Blk2)
205 return Blk1;
206 }
207 }
208 return Blk1;
209 }
210
211 /// FindDominators - Calculate the dominator tree for the subset of the CFG
212 /// corresponding to the basic blocks on the BlockList. This uses the
213 /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey
214 /// and Kennedy, published in Software--Practice and Experience, 2001,
215 /// 4:1-10. Because the CFG subset does not include any edges leading into
216 /// blocks that define the value, the results are not the usual dominator
217 /// tree. The CFG subset has a single pseudo-entry node with edges to a set
218 /// of root nodes for blocks that define the value. The dominators for this
219 /// subset CFG are not the standard dominators but they are adequate for
220 /// placing PHIs within the subset CFG.
221 void FindDominators(BlockListTy *BlockList) {
222 bool Changed;
223 do {
224 Changed = false;
225 // Iterate over the list in reverse order, i.e., forward on CFG edges.
226 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
227 E = BlockList->rend(); I != E; ++I) {
228 BBInfo *Info = *I;
229
230 // Start with the first predecessor.
231 assert(Info->NumPreds > 0 && "unreachable block");
232 BBInfo *NewIDom = Info->Preds[0];
233
234 // Iterate through the block's other predecessors.
235 for (unsigned p = 1; p != Info->NumPreds; ++p) {
236 BBInfo *Pred = Info->Preds[p];
237 NewIDom = IntersectDominators(NewIDom, Pred);
238 }
239
240 // Check if the IDom value has changed.
241 if (NewIDom != Info->IDom) {
242 Info->IDom = NewIDom;
243 Changed = true;
244 }
245 }
246 } while (Changed);
247 }
248
249 /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for
250 /// any blocks containing definitions of the value. If one is found, then
251 /// the successor of Pred is in the dominance frontier for the definition,
252 /// and this function returns true.
253 bool IsDefInDomFrontier(const BBInfo *Pred, const BBInfo *IDom) {
254 for (; Pred != IDom; Pred = Pred->IDom) {
255 if (Pred->DefBB == Pred)
256 return true;
257 }
258 return false;
259 }
260
261 /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers
262 /// of the known definitions. Iteratively add PHIs in the dom frontiers
263 /// until nothing changes. Along the way, keep track of the nearest
264 /// dominating definitions for non-PHI blocks.
265 void FindPHIPlacement(BlockListTy *BlockList) {
266 bool Changed;
267 do {
268 Changed = false;
269 // Iterate over the list in reverse order, i.e., forward on CFG edges.
270 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
271 E = BlockList->rend(); I != E; ++I) {
272 BBInfo *Info = *I;
273
274 // If this block already needs a PHI, there is nothing to do here.
275 if (Info->DefBB == Info)
276 continue;
277
278 // Default to use the same def as the immediate dominator.
279 BBInfo *NewDefBB = Info->IDom->DefBB;
280 for (unsigned p = 0; p != Info->NumPreds; ++p) {
281 if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) {
282 // Need a PHI here.
283 NewDefBB = Info;
284 break;
285 }
286 }
287
288 // Check if anything changed.
289 if (NewDefBB != Info->DefBB) {
290 Info->DefBB = NewDefBB;
291 Changed = true;
292 }
293 }
294 } while (Changed);
295 }
296
297 /// FindAvailableVal - If this block requires a PHI, first check if an
298 /// existing PHI matches the PHI placement and reaching definitions computed
299 /// earlier, and if not, create a new PHI. Visit all the block's
300 /// predecessors to calculate the available value for each one and fill in
301 /// the incoming values for a new PHI.
302 void FindAvailableVals(BlockListTy *BlockList) {
303 // Go through the worklist in forward order (i.e., backward through the CFG)
304 // and check if existing PHIs can be used. If not, create empty PHIs where
305 // they are needed.
306 for (typename BlockListTy::iterator I = BlockList->begin(),
307 E = BlockList->end(); I != E; ++I) {
308 BBInfo *Info = *I;
309 // Check if there needs to be a PHI in BB.
310 if (Info->DefBB != Info)
311 continue;
312
313 // Look for an existing PHI.
314 FindExistingPHI(Info->BB, BlockList);
315 if (Info->AvailableVal)
316 continue;
317
318 ValT PHI = Traits::CreateEmptyPHI(Info->BB, Info->NumPreds, Updater);
319 Info->AvailableVal = PHI;
320 (*AvailableVals)[Info->BB] = PHI;
321 }
322
323 // Now go back through the worklist in reverse order to fill in the
324 // arguments for any new PHIs added in the forward traversal.
325 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
326 E = BlockList->rend(); I != E; ++I) {
327 BBInfo *Info = *I;
328
329 if (Info->DefBB != Info) {
330 // Record the available value at join nodes to speed up subsequent
331 // uses of this SSAUpdater for the same value.
332 if (Info->NumPreds > 1)
333 (*AvailableVals)[Info->BB] = Info->DefBB->AvailableVal;
334 continue;
335 }
336
337 // Check if this block contains a newly added PHI.
338 PhiT *PHI = Traits::ValueIsNewPHI(Info->AvailableVal, Updater);
339 if (!PHI)
340 continue;
341
342 // Iterate through the block's predecessors.
343 for (unsigned p = 0; p != Info->NumPreds; ++p) {
344 BBInfo *PredInfo = Info->Preds[p];
345 BlkT *Pred = PredInfo->BB;
346 // Skip to the nearest preceding definition.
347 if (PredInfo->DefBB != PredInfo)
348 PredInfo = PredInfo->DefBB;
349 Traits::AddPHIOperand(PHI, PredInfo->AvailableVal, Pred);
350 }
351
352 DEBUG(dbgs() << " Inserted PHI: " << *PHI << "\n");
353
354 // If the client wants to know about all new instructions, tell it.
355 if (InsertedPHIs) InsertedPHIs->push_back(PHI);
356 }
357 }
358
359 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
360 /// them match what is needed.
361 void FindExistingPHI(BlkT *BB, BlockListTy *BlockList) {
362 for (typename BlkT::iterator BBI = BB->begin(), BBE = BB->end();
363 BBI != BBE; ++BBI) {
364 PhiT *SomePHI = Traits::InstrIsPHI(BBI);
365 if (!SomePHI)
366 break;
367 if (CheckIfPHIMatches(SomePHI)) {
368 RecordMatchingPHI(SomePHI);
369 break;
370 }
371 // Match failed: clear all the PHITag values.
372 for (typename BlockListTy::iterator I = BlockList->begin(),
373 E = BlockList->end(); I != E; ++I)
374 (*I)->PHITag = 0;
375 }
376 }
377
378 /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
379 /// in the BBMap.
380 bool CheckIfPHIMatches(PhiT *PHI) {
381 SmallVector WorkList;
382 WorkList.push_back(PHI);
383
384 // Mark that the block containing this PHI has been visited.
385 BBMap[PHI->getParent()]->PHITag = PHI;
386
387 while (!WorkList.empty()) {
388 PHI = WorkList.pop_back_val();
389
390 // Iterate through the PHI's incoming values.
391 for (typename Traits::PHI_iterator I = Traits::PHI_begin(PHI),
392 E = Traits::PHI_end(PHI); I != E; ++I) {
393 ValT IncomingVal = I.getIncomingValue();
394 BBInfo *PredInfo = BBMap[I.getIncomingBlock()];
395 // Skip to the nearest preceding definition.
396 if (PredInfo->DefBB != PredInfo)
397 PredInfo = PredInfo->DefBB;
398
399 // Check if it matches the expected value.
400 if (PredInfo->AvailableVal) {
401 if (IncomingVal == PredInfo->AvailableVal)
402 continue;
403 return false;
404 }
405
406 // Check if the value is a PHI in the correct block.
407 PhiT *IncomingPHIVal = Traits::ValueIsPHI(IncomingVal, Updater);
408 if (!IncomingPHIVal || IncomingPHIVal->getParent() != PredInfo->BB)
409 return false;
410
411 // If this block has already been visited, check if this PHI matches.
412 if (PredInfo->PHITag) {
413 if (IncomingPHIVal == PredInfo->PHITag)
414 continue;
415 return false;
416 }
417 PredInfo->PHITag = IncomingPHIVal;
418
419 WorkList.push_back(IncomingPHIVal);
420 }
421 }
422 return true;
423 }
424
425 /// RecordMatchingPHI - For a PHI node that matches, record it and its input
426 /// PHIs in both the BBMap and the AvailableVals mapping.
427 void RecordMatchingPHI(PhiT *PHI) {
428 SmallVector WorkList;
429 WorkList.push_back(PHI);
430
431 // Record this PHI.
432 BlkT *BB = PHI->getParent();
433 ValT PHIVal = Traits::GetPHIValue(PHI);
434 (*AvailableVals)[BB] = PHIVal;
435 BBMap[BB]->AvailableVal = PHIVal;
436
437 while (!WorkList.empty()) {
438 PHI = WorkList.pop_back_val();
439
440 // Iterate through the PHI's incoming values.
441 for (typename Traits::PHI_iterator I = Traits::PHI_begin(PHI),
442 E = Traits::PHI_end(PHI); I != E; ++I) {
443 ValT IncomingVal = I.getIncomingValue();
444 PhiT *IncomingPHI = Traits::ValueIsPHI(IncomingVal, Updater);
445 if (!IncomingPHI) continue;
446 BB = IncomingPHI->getParent();
447 BBInfo *Info = BBMap[BB];
448 if (!Info || Info->AvailableVal)
449 continue;
450
451 // Record the PHI and add it to the worklist.
452 (*AvailableVals)[BB] = IncomingVal;
453 Info->AvailableVal = IncomingVal;
454 WorkList.push_back(IncomingPHI);
455 }
456 }
457 }
458 };
459
460 } // End llvm namespace
461
462 #endif
2525 #include "llvm/Support/Debug.h"
2626 #include "llvm/Support/ErrorHandling.h"
2727 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
2829 using namespace llvm;
29
30 /// BBInfo - Per-basic block information used internally by MachineSSAUpdater.
31 class MachineSSAUpdater::BBInfo {
32 public:
33 MachineBasicBlock *BB; // Back-pointer to the corresponding block.
34 unsigned AvailableVal; // Value to use in this block.
35 BBInfo *DefBB; // Block that defines the available value.
36 int BlkNum; // Postorder number.
37 BBInfo *IDom; // Immediate dominator.
38 unsigned NumPreds; // Number of predecessor blocks.
39 BBInfo **Preds; // Array[NumPreds] of predecessor blocks.
40 MachineInstr *PHITag; // Marker for existing PHIs that match.
41
42 BBInfo(MachineBasicBlock *ThisBB, unsigned V)
43 : BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0),
44 NumPreds(0), Preds(0), PHITag(0) { }
45 };
46
47 typedef DenseMap BBMapTy;
4830
4931 typedef DenseMap AvailableValsTy;
5032 static AvailableValsTy &getAvailableVals(void *AV) {
5133 return *static_cast(AV);
5234 }
5335
54 static BBMapTy *getBBMap(void *BM) {
55 return static_cast(BM);
56 }
57
5836 MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
5937 SmallVectorImpl *NewPHI)
60 : AV(0), BM(0), InsertedPHIs(NewPHI) {
38 : AV(0), InsertedPHIs(NewPHI) {
6139 TII = MF.getTarget().getInstrInfo();
6240 MRI = &MF.getRegInfo();
6341 }
133111 MachineInstr *InsertNewDef(unsigned Opcode,
134112 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
135113 const TargetRegisterClass *RC,
136 MachineRegisterInfo *MRI, const TargetInstrInfo *TII) {
114 MachineRegisterInfo *MRI,
115 const TargetInstrInfo *TII) {
137116 unsigned NewVR = MRI->createVirtualRegister(RC);
138117 return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
139118 }
262241 I->second = NewReg;
263242 }
264243
244 /// MachinePHIiter - Iterator for PHI operands. This is used for the
245 /// PHI_iterator in the SSAUpdaterImpl template.
246 namespace {
247 class MachinePHIiter {
248 private:
249 MachineInstr *PHI;
250 unsigned idx;
251
252 public:
253 explicit MachinePHIiter(MachineInstr *P) // begin iterator
254 : PHI(P), idx(1) {}
255 MachinePHIiter(MachineInstr *P, bool) // end iterator
256 : PHI(P), idx(PHI->getNumOperands()) {}
257
258 MachinePHIiter &operator++() { idx += 2; return *this; }
259 bool operator==(const MachinePHIiter& x) const { return idx == x.idx; }
260 bool operator!=(const MachinePHIiter& x) const { return !operator==(x); }
261 unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); }
262 MachineBasicBlock *getIncomingBlock() {
263 return PHI->getOperand(idx+1).getMBB();
264 }
265 };
266 }
267
268 /// SSAUpdaterTraits - Traits for the SSAUpdaterImpl
269 /// template, specialized for MachineSSAUpdater.
270 namespace llvm {
271 template<>
272 class SSAUpdaterTraits {
273 public:
274 typedef MachineBasicBlock BlkT;
275 typedef unsigned ValT;
276 typedef MachineInstr PhiT;
277
278 typedef MachineBasicBlock::succ_iterator BlkSucc_iterator;
279 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
280 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
281
282 typedef MachinePHIiter PHI_iterator;
283 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
284 static inline PHI_iterator PHI_end(PhiT *PHI) {
285 return PHI_iterator(PHI, true);
286 }
287
288 /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
289 /// vector.
290 static void FindPredecessorBlocks(MachineBasicBlock *BB,
291 SmallVectorImpl *Preds){
292 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
293 E = BB->pred_end(); PI != E; ++PI)
294 Preds->push_back(*PI);
295 }
296
297 /// GetUndefVal - Create an IMPLICIT_DEF instruction with a new register.
298 /// Add it into the specified block and return the register.
299 static unsigned GetUndefVal(MachineBasicBlock *BB,
300 MachineSSAUpdater *Updater) {
301 // Insert an implicit_def to represent an undef value.
302 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
303 BB, BB->getFirstTerminator(),
304 Updater->VRC, Updater->MRI,
305 Updater->TII);
306 return NewDef->getOperand(0).getReg();
307 }
308
309 /// CreateEmptyPHI - Create a PHI instruction that defines a new register.
310 /// Add it into the specified block and return the register.
311 static unsigned CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds,
312 MachineSSAUpdater *Updater) {
313 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
314 MachineInstr *PHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
315 Updater->VRC, Updater->MRI,
316 Updater->TII);
317 return PHI->getOperand(0).getReg();
318 }
319
320 /// AddPHIOperand - Add the specified value as an operand of the PHI for
321 /// the specified predecessor block.
322 static void AddPHIOperand(MachineInstr *PHI, unsigned Val,
323 MachineBasicBlock *Pred) {
324 PHI->addOperand(MachineOperand::CreateReg(Val, false));
325 PHI->addOperand(MachineOperand::CreateMBB(Pred));
326 }
327
328 /// InstrIsPHI - Check if an instruction is a PHI.
329 ///
330 static MachineInstr *InstrIsPHI(MachineInstr *I) {
331 if (I->isPHI())
332 return I;
333 return 0;
334 }
335
336 /// ValueIsPHI - Check if the instruction that defines the specified register
337 /// is a PHI instruction.
338 static MachineInstr *ValueIsPHI(unsigned Val, MachineSSAUpdater *Updater) {
339 return InstrIsPHI(Updater->MRI->getVRegDef(Val));
340 }
341
342 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
343 /// operands, i.e., it was just added.
344 static MachineInstr *ValueIsNewPHI(unsigned Val, MachineSSAUpdater *Updater) {
345 MachineInstr *PHI = ValueIsPHI(Val, Updater);
346 if (PHI && PHI->getNumOperands() <= 1)
347 return PHI;
348 return 0;
349 }
350
351 /// GetPHIValue - For the specified PHI instruction, return the register
352 /// that it defines.
353 static unsigned GetPHIValue(MachineInstr *PHI) {
354 return PHI->getOperand(0).getReg();
355 }
356 };
357
358 } // End llvm namespace
359
265360 /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
266361 /// for the specified BB and if so, return it. If not, construct SSA form by
267362 /// first calculating the required placement of PHIs and then inserting new
271366 if (unsigned V = AvailableVals[BB])
272367 return V;
273368
274 // Pool allocation used internally by GetValueAtEndOfBlock.
275 BumpPtrAllocator Allocator;
276 BBMapTy BBMapObj;
277 BM = &BBMapObj;
278
279 SmallVector BlockList;
280 BuildBlockList(BB, &BlockList, &Allocator);
281
282 // Special case: bail out if BB is unreachable.
283 if (BlockList.size() == 0) {
284 BM = 0;
285 // Insert an implicit_def to represent an undef value.
286 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
287 BB, BB->getFirstTerminator(),
288 VRC, MRI, TII);
289 unsigned V = NewDef->getOperand(0).getReg();
290 AvailableVals[BB] = V;
291 return V;
292 }
293
294 FindDominators(&BlockList);
295 FindPHIPlacement(&BlockList);
296 FindAvailableVals(&BlockList);
297
298 BM = 0;
299 return BBMapObj[BB]->DefBB->AvailableVal;
300 }
301
302 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
303 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
304 static void FindPredecessorBlocks(MachineSSAUpdater::BBInfo *Info,
305 SmallVectorImpl *Preds,
306 BumpPtrAllocator *Allocator) {
307 MachineBasicBlock *BB = Info->BB;
308 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
309 E = BB->pred_end(); PI != E; ++PI)
310 Preds->push_back(*PI);
311
312 Info->NumPreds = Preds->size();
313 Info->Preds = static_cast
314 (Allocator->Allocate(Info->NumPreds * sizeof(MachineSSAUpdater::BBInfo*),
315 AlignOf::Alignment));
316 }
317
318 /// BuildBlockList - Starting from the specified basic block, traverse back
319 /// through its predecessors until reaching blocks with known values. Create
320 /// BBInfo structures for the blocks and append them to the block list.
321 void MachineSSAUpdater::BuildBlockList(MachineBasicBlock *BB,
322 BlockListTy *BlockList,
323 BumpPtrAllocator *Allocator) {
324 AvailableValsTy &AvailableVals = getAvailableVals(AV);
325 BBMapTy *BBMap = getBBMap(BM);
326 SmallVector RootList;
327 SmallVector WorkList;
328
329 BBInfo *Info = new (*Allocator) BBInfo(BB, 0);
330 (*BBMap)[BB] = Info;
331 WorkList.push_back(Info);
332
333 // Search backward from BB, creating BBInfos along the way and stopping when
334 // reaching blocks that define the value. Record those defining blocks on
335 // the RootList.
336 SmallVector Preds;
337 while (!WorkList.empty()) {
338 Info = WorkList.pop_back_val();
339 Preds.clear();
340 FindPredecessorBlocks(Info, &Preds, Allocator);
341
342 // Treat an unreachable predecessor as a definition with 'undef'.
343 if (Info->NumPreds == 0) {
344 // Insert an implicit_def to represent an undef value.
345 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
346 Info->BB,
347 Info->BB->getFirstTerminator(),
348 VRC, MRI, TII);
349 Info->AvailableVal = NewDef->getOperand(0).getReg();
350 Info->DefBB = Info;
351 RootList.push_back(Info);
352 continue;
353 }
354
355 for (unsigned p = 0; p != Info->NumPreds; ++p) {
356 MachineBasicBlock *Pred = Preds[p];
357 // Check if BBMap already has a BBInfo for the predecessor block.
358 BBMapTy::value_type &BBMapBucket = BBMap->FindAndConstruct(Pred);
359 if (BBMapBucket.second) {
360 Info->Preds[p] = BBMapBucket.second;
361 continue;
362 }
363
364 // Create a new BBInfo for the predecessor.
365 unsigned PredVal = AvailableVals.lookup(Pred);
366 BBInfo *PredInfo = new (*Allocator) BBInfo(Pred, PredVal);
367 BBMapBucket.second = PredInfo;
368 Info->Preds[p] = PredInfo;
369
370 if (PredInfo->AvailableVal) {
371 RootList.push_back(PredInfo);
372 continue;
373 }
374 WorkList.push_back(PredInfo);
375 }
376 }
377
378 // Now that we know what blocks are backwards-reachable from the starting
379 // block, do a forward depth-first traversal to assign postorder numbers
380 // to those blocks.
381 BBInfo *PseudoEntry = new (*Allocator) BBInfo(0, 0);
382 unsigned BlkNum = 1;
383
384 // Initialize the worklist with the roots from the backward traversal.
385 while (!RootList.empty()) {
386 Info = RootList.pop_back_val();
387 Info->IDom = PseudoEntry;
388 Info->BlkNum = -1;
389 WorkList.push_back(Info);
390 }
391
392 while (!WorkList.empty()) {
393 Info = WorkList.back();
394
395 if (Info->BlkNum == -2) {
396 // All the successors have been handled; assign the postorder number.
397 Info->BlkNum = BlkNum++;
398 // If not a root, put it on the BlockList.
399 if (!Info->AvailableVal)
400 BlockList->push_back(Info);
401 WorkList.pop_back();
402 continue;
403 }
404
405 // Leave this entry on the worklist, but set its BlkNum to mark that its
406 // successors have been put on the worklist. When it returns to the top
407 // the list, after handling its successors, it will be assigned a number.
408 Info->BlkNum = -2;
409
410 // Add unvisited successors to the work list.
411 for (MachineBasicBlock::succ_iterator SI = Info->BB->succ_begin(),
412 E = Info->BB->succ_end(); SI != E; ++SI) {
413 BBInfo *SuccInfo = (*BBMap)[*SI];
414 if (!SuccInfo || SuccInfo->BlkNum)
415 continue;
416 SuccInfo->BlkNum = -1;
417 WorkList.push_back(SuccInfo);
418 }
419 }
420 PseudoEntry->BlkNum = BlkNum;
421 }
422
423 /// IntersectDominators - This is the dataflow lattice "meet" operation for
424 /// finding dominators. Given two basic blocks, it walks up the dominator
425 /// tree until it finds a common dominator of both. It uses the postorder
426 /// number of the blocks to determine how to do that.
427 static MachineSSAUpdater::BBInfo *
428 IntersectDominators(MachineSSAUpdater::BBInfo *Blk1,
429 MachineSSAUpdater::BBInfo *Blk2) {
430 while (Blk1 != Blk2) {
431 while (Blk1->BlkNum < Blk2->BlkNum) {
432 Blk1 = Blk1->IDom;
433 if (!Blk1)
434 return Blk2;
435 }
436 while (Blk2->BlkNum < Blk1->BlkNum) {
437 Blk2 = Blk2->IDom;
438 if (!Blk2)
439 return Blk1;
440 }
441 }
442 return Blk1;
443 }
444
445 /// FindDominators - Calculate the dominator tree for the subset of the CFG
446 /// corresponding to the basic blocks on the BlockList. This uses the
447 /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey and
448 /// Kennedy, published in Software--Practice and Experience, 2001, 4:1-10.
449 /// Because the CFG subset does not include any edges leading into blocks that
450 /// define the value, the results are not the usual dominator tree. The CFG
451 /// subset has a single pseudo-entry node with edges to a set of root nodes
452 /// for blocks that define the value. The dominators for this subset CFG are
453 /// not the standard dominators but they are adequate for placing PHIs within
454 /// the subset CFG.
455 void MachineSSAUpdater::FindDominators(BlockListTy *BlockList) {
456 bool Changed;
457 do {
458 Changed = false;
459 // Iterate over the list in reverse order, i.e., forward on CFG edges.
460 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
461 E = BlockList->rend(); I != E; ++I) {
462 BBInfo *Info = *I;
463
464 // Start with the first predecessor.
465 assert(Info->NumPreds > 0 && "unreachable block");
466 BBInfo *NewIDom = Info->Preds[0];
467
468 // Iterate through the block's other predecessors.
469 for (unsigned p = 1; p != Info->NumPreds; ++p) {
470 BBInfo *Pred = Info->Preds[p];
471 NewIDom = IntersectDominators(NewIDom, Pred);
472 }
473
474 // Check if the IDom value has changed.
475 if (NewIDom != Info->IDom) {
476 Info->IDom = NewIDom;
477 Changed = true;
478 }
479 }
480 } while (Changed);
481 }
482
483 /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for
484 /// any blocks containing definitions of the value. If one is found, then the
485 /// successor of Pred is in the dominance frontier for the definition, and
486 /// this function returns true.
487 static bool IsDefInDomFrontier(const MachineSSAUpdater::BBInfo *Pred,
488 const MachineSSAUpdater::BBInfo *IDom) {
489 for (; Pred != IDom; Pred = Pred->IDom) {
490 if (Pred->DefBB == Pred)
491 return true;
492 }
493 return false;
494 }
495
496 /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers of
497 /// the known definitions. Iteratively add PHIs in the dom frontiers until
498 /// nothing changes. Along the way, keep track of the nearest dominating
499 /// definitions for non-PHI blocks.
500 void MachineSSAUpdater::FindPHIPlacement(BlockListTy *BlockList) {
501 bool Changed;
502 do {
503 Changed = false;
504 // Iterate over the list in reverse order, i.e., forward on CFG edges.
505 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
506 E = BlockList->rend(); I != E; ++I) {
507 BBInfo *Info = *I;
508
509 // If this block already needs a PHI, there is nothing to do here.
510 if (Info->DefBB == Info)
511 continue;
512
513 // Default to use the same def as the immediate dominator.
514 BBInfo *NewDefBB = Info->IDom->DefBB;
515 for (unsigned p = 0; p != Info->NumPreds; ++p) {
516 if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) {
517 // Need a PHI here.
518 NewDefBB = Info;
519 break;
520 }
521 }
522
523 // Check if anything changed.
524 if (NewDefBB != Info->DefBB) {
525 Info->DefBB = NewDefBB;
526 Changed = true;
527 }
528 }
529 } while (Changed);
530 }
531
532 /// FindAvailableVal - If this block requires a PHI, first check if an existing
533 /// PHI matches the PHI placement and reaching definitions computed earlier,
534 /// and if not, create a new PHI. Visit all the block's predecessors to
535 /// calculate the available value for each one and fill in the incoming values
536 /// for a new PHI.
537 void MachineSSAUpdater::FindAvailableVals(BlockListTy *BlockList) {
538 AvailableValsTy &AvailableVals = getAvailableVals(AV);
539
540 // Go through the worklist in forward order (i.e., backward through the CFG)
541 // and check if existing PHIs can be used. If not, create empty PHIs where
542 // they are needed.
543 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
544 I != E; ++I) {
545 BBInfo *Info = *I;
546 // Check if there needs to be a PHI in BB.
547 if (Info->DefBB != Info)
548 continue;
549
550 // Look for an existing PHI.
551 FindExistingPHI(Info->BB, BlockList);
552 if (Info->AvailableVal)
553 continue;
554
555 MachineBasicBlock::iterator Loc =
556 Info->BB->empty() ? Info->BB->end() : Info->BB->front();
557 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, Info->BB, Loc,
558 VRC, MRI, TII);
559 unsigned PHI = InsertedPHI->getOperand(0).getReg();
560 Info->AvailableVal = PHI;
561 AvailableVals[Info->BB] = PHI;
562 }
563
564 // Now go back through the worklist in reverse order to fill in the arguments
565 // for any new PHIs added in the forward traversal.
566 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
567 E = BlockList->rend(); I != E; ++I) {
568 BBInfo *Info = *I;
569
570 if (Info->DefBB != Info) {
571 // Record the available value at join nodes to speed up subsequent
572 // uses of this SSAUpdater for the same value.
573 if (Info->NumPreds > 1)
574 AvailableVals[Info->BB] = Info->DefBB->AvailableVal;
575 continue;
576 }
577
578 // Check if this block contains a newly added PHI.
579 unsigned PHI = Info->AvailableVal;
580 MachineInstr *InsertedPHI = MRI->getVRegDef(PHI);
581 if (!InsertedPHI->isPHI() || InsertedPHI->getNumOperands() > 1)
582 continue;
583
584 // Iterate through the block's predecessors.
585 MachineInstrBuilder MIB(InsertedPHI);
586 for (unsigned p = 0; p != Info->NumPreds; ++p) {
587 BBInfo *PredInfo = Info->Preds[p];
588 MachineBasicBlock *Pred = PredInfo->BB;
589 // Skip to the nearest preceding definition.
590 if (PredInfo->DefBB != PredInfo)
591 PredInfo = PredInfo->DefBB;
592 MIB.addReg(PredInfo->AvailableVal).addMBB(Pred);
593 }
594
595 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
596
597 // If the client wants to know about all new instructions, tell it.
598 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
599 }
600 }
601
602 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
603 /// them match what is needed.
604 void MachineSSAUpdater::FindExistingPHI(MachineBasicBlock *BB,
605 BlockListTy *BlockList) {
606 for (MachineBasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
607 BBI != BBE && BBI->isPHI(); ++BBI) {
608 if (CheckIfPHIMatches(BBI)) {
609 RecordMatchingPHI(BBI);
610 break;
611 }
612 // Match failed: clear all the PHITag values.
613 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
614 I != E; ++I)
615 (*I)->PHITag = 0;
616 }
617 }
618
619 /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
620 /// in the BBMap.
621 bool MachineSSAUpdater::CheckIfPHIMatches(MachineInstr *PHI) {
622 BBMapTy *BBMap = getBBMap(BM);
623 SmallVector WorkList;
624 WorkList.push_back(PHI);
625
626 // Mark that the block containing this PHI has been visited.
627 (*BBMap)[PHI->getParent()]->PHITag = PHI;
628
629 while (!WorkList.empty()) {
630 PHI = WorkList.pop_back_val();
631
632 // Iterate through the PHI's incoming values.
633 for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) {
634 unsigned IncomingVal = PHI->getOperand(i).getReg();
635 BBInfo *PredInfo = (*BBMap)[PHI->getOperand(i+1).getMBB()];
636 // Skip to the nearest preceding definition.
637 if (PredInfo->DefBB != PredInfo)
638 PredInfo = PredInfo->DefBB;
639
640 // Check if it matches the expected value.
641 if (PredInfo->AvailableVal) {
642 if (IncomingVal == PredInfo->AvailableVal)
643 continue;
644 return false;
645 }
646
647 // Check if the value is a PHI in the correct block.
648 MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal);
649 if (!IncomingPHIVal->isPHI() ||
650 IncomingPHIVal->getParent() != PredInfo->BB)
651 return false;
652
653 // If this block has already been visited, check if this PHI matches.
654 if (PredInfo->PHITag) {
655 if (IncomingPHIVal == PredInfo->PHITag)
656 continue;
657 return false;
658 }
659 PredInfo->PHITag = IncomingPHIVal;
660
661 WorkList.push_back(IncomingPHIVal);
662 }
663 }
664 return true;
665 }
666
667 /// RecordMatchingPHI - For a PHI node that matches, record it and its input
668 /// PHIs in both the BBMap and the AvailableVals mapping.
669 void MachineSSAUpdater::RecordMatchingPHI(MachineInstr *PHI) {
670 BBMapTy *BBMap = getBBMap(BM);
671 AvailableValsTy &AvailableVals = getAvailableVals(AV);
672 SmallVector WorkList;
673 WorkList.push_back(PHI);
674
675 // Record this PHI.
676 MachineBasicBlock *BB = PHI->getParent();
677 AvailableVals[BB] = PHI->getOperand(0).getReg();
678 (*BBMap)[BB]->AvailableVal = PHI->getOperand(0).getReg();
679
680 while (!WorkList.empty()) {
681 PHI = WorkList.pop_back_val();
682
683 // Iterate through the PHI's incoming values.
684 for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) {
685 unsigned IncomingVal = PHI->getOperand(i).getReg();
686 MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal);
687 if (!IncomingPHIVal->isPHI()) continue;
688 BB = IncomingPHIVal->getParent();
689 BBInfo *Info = (*BBMap)[BB];
690 if (!Info || Info->AvailableVal)
691 continue;
692
693 // Record the PHI and add it to the worklist.
694 AvailableVals[BB] = IncomingVal;
695 Info->AvailableVal = IncomingVal;
696 WorkList.push_back(IncomingPHIVal);
697 }
698 }
699 }
369 SSAUpdaterImpl Impl(this, &AvailableVals, InsertedPHIs);
370 return Impl.GetValue(BB);
371 }
1111 //===----------------------------------------------------------------------===//
1212
1313 #define DEBUG_TYPE "ssaupdater"
14 #include "llvm/Transforms/Utils/SSAUpdater.h"
1514 #include "llvm/Instructions.h"
1615 #include "llvm/ADT/DenseMap.h"
1716 #include "llvm/Support/AlignOf.h"
1918 #include "llvm/Support/CFG.h"
2019 #include "llvm/Support/Debug.h"
2120 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Transforms/Utils/SSAUpdater.h"
22 #include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
2223 using namespace llvm;
23
24 /// BBInfo - Per-basic block information used internally by SSAUpdater.
25 /// The predecessors of each block are cached here since pred_iterator is
26 /// slow and we need to iterate over the blocks at least a few times.
27 class SSAUpdater::BBInfo {
28 public:
29 BasicBlock *BB; // Back-pointer to the corresponding block.
30 Value *AvailableVal; // Value to use in this block.
31 BBInfo *DefBB; // Block that defines the available value.
32 int BlkNum; // Postorder number.
33 BBInfo *IDom; // Immediate dominator.
34 unsigned NumPreds; // Number of predecessor blocks.
35 BBInfo **Preds; // Array[NumPreds] of predecessor blocks.
36 PHINode *PHITag; // Marker for existing PHIs that match.
37
38 BBInfo(BasicBlock *ThisBB, Value *V)
39 : BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0),
40 NumPreds(0), Preds(0), PHITag(0) { }
41 };
42
43 typedef DenseMap BBMapTy;
4424
4525 typedef DenseMap AvailableValsTy;
4626 static AvailableValsTy &getAvailableVals(void *AV) {
4727 return *static_cast(AV);
4828 }
4929
50 static BBMapTy *getBBMap(void *BM) {
51 return static_cast(BM);
52 }
53
5430 SSAUpdater::SSAUpdater(SmallVectorImpl *NewPHI)
55 : AV(0), PrototypeValue(0), BM(0), InsertedPHIs(NewPHI) {}
31 : AV(0), PrototypeValue(0), InsertedPHIs(NewPHI) {}
5632
5733 SSAUpdater::~SSAUpdater() {
5834 delete &getAvailableVals(AV);
10480 /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
10581 /// live at the end of the specified block.
10682 Value *SSAUpdater::GetValueAtEndOfBlock(BasicBlock *BB) {
107 assert(BM == 0 && "Unexpected Internal State");
10883 Value *Res = GetValueAtEndOfBlockInternal(BB);
109 assert(BM == 0 && "Unexpected Internal State");
11084 return Res;
11185 }
11286
230204 U.set(V);
231205 }
232206
207 /// PHIiter - Iterator for PHI operands. This is used for the PHI_iterator
208 /// in the SSAUpdaterImpl template.
209 namespace {
210 class PHIiter {
211 private:
212 PHINode *PHI;
213 unsigned idx;
214
215 public:
216 explicit PHIiter(PHINode *P) // begin iterator
217 : PHI(P), idx(0) {}
218 PHIiter(PHINode *P, bool) // end iterator
219 : PHI(P), idx(PHI->getNumIncomingValues()) {}
220
221 PHIiter &operator++() { ++idx; return *this; }
222 bool operator==(const PHIiter& x) const { return idx == x.idx; }
223 bool operator!=(const PHIiter& x) const { return !operator==(x); }
224 Value *getIncomingValue() { return PHI->getIncomingValue(idx); }
225 BasicBlock *getIncomingBlock() { return PHI->getIncomingBlock(idx); }
226 };
227 }
228
229 /// SSAUpdaterTraits - Traits for the SSAUpdaterImpl template,
230 /// specialized for SSAUpdater.
231 namespace llvm {
232 template<>
233 class SSAUpdaterTraits {
234 public:
235 typedef BasicBlock BlkT;
236 typedef Value *ValT;
237 typedef PHINode PhiT;
238
239 typedef succ_iterator BlkSucc_iterator;
240 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return succ_begin(BB); }
241 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return succ_end(BB); }
242
243 typedef PHIiter PHI_iterator;
244 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
245 static inline PHI_iterator PHI_end(PhiT *PHI) {
246 return PHI_iterator(PHI, true);
247 }
248
249 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
250 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
251 static void FindPredecessorBlocks(BasicBlock *BB,
252 SmallVectorImpl *Preds) {
253 // We can get our predecessor info by walking the pred_iterator list,
254 // but it is relatively slow. If we already have PHI nodes in this
255 // block, walk one of them to get the predecessor list instead.
256 if (PHINode *SomePhi = dyn_cast(BB->begin())) {
257 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
258 Preds->push_back(SomePhi->getIncomingBlock(PI));
259 } else {
260 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
261 Preds->push_back(*PI);
262 }
263 }
264
265 /// GetUndefVal - Get an undefined value of the same type as the value
266 /// being handled.
267 static Value *GetUndefVal(BasicBlock *BB, SSAUpdater *Updater) {
268 return UndefValue::get(Updater->PrototypeValue->getType());
269 }
270
271 /// CreateEmptyPHI - Create a new PHI instruction in the specified block.
272 /// Reserve space for the operands but do not fill them in yet.
273 static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds,
274 SSAUpdater *Updater) {
275 PHINode *PHI = PHINode::Create(Updater->PrototypeValue->getType(),
276 Updater->PrototypeValue->getName(),
277 &BB->front());
278 PHI->reserveOperandSpace(NumPreds);
279 return PHI;
280 }
281
282 /// AddPHIOperand - Add the specified value as an operand of the PHI for
283 /// the specified predecessor block.
284 static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred) {
285 PHI->addIncoming(Val, Pred);
286 }
287
288 /// InstrIsPHI - Check if an instruction is a PHI.
289 ///
290 static PHINode *InstrIsPHI(Instruction *I) {
291 return dyn_cast(I);
292 }
293
294 /// ValueIsPHI - Check if a value is a PHI.
295 ///
296 static PHINode *ValueIsPHI(Value *Val, SSAUpdater *Updater) {
297 return dyn_cast(Val);
298 }
299
300 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
301 /// operands, i.e., it was just added.
302 static PHINode *ValueIsNewPHI(Value *Val, SSAUpdater *Updater) {
303 PHINode *PHI = ValueIsPHI(Val, Updater);
304 if (PHI && PHI->getNumIncomingValues() == 0)
305 return PHI;
306 return 0;
307 }
308
309 /// GetPHIValue - For the specified PHI instruction, return the value
310 /// that it defines.
311 static Value *GetPHIValue(PHINode *PHI) {
312 return PHI;
313 }
314 };
315
316 } // End llvm namespace
317
233318 /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
234319 /// for the specified BB and if so, return it. If not, construct SSA form by
235320 /// first calculating the required placement of PHIs and then inserting new
239324 if (Value *V = AvailableVals[BB])
240325 return V;
241326
242 // Pool allocation used internally by GetValueAtEndOfBlock.
243 BumpPtrAllocator Allocator;
244 BBMapTy BBMapObj;
245 BM = &BBMapObj;
246
247 SmallVector BlockList;
248 BuildBlockList(BB, &BlockList, &Allocator);
249
250 // Special case: bail out if BB is unreachable.
251 if (BlockList.size() == 0) {
252 BM = 0;
253 return UndefValue::get(PrototypeValue->getType());
254 }
255
256 FindDominators(&BlockList);
257 FindPHIPlacement(&BlockList);
258 FindAvailableVals(&BlockList);
259
260 BM = 0;
261 return BBMapObj[BB]->DefBB->AvailableVal;
262 }
263
264 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
265 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
266 static void FindPredecessorBlocks(SSAUpdater::BBInfo *Info,
267 SmallVectorImpl *Preds,
268 BumpPtrAllocator *Allocator) {
269 // We can get our predecessor info by walking the pred_iterator list,
270 // but it is relatively slow. If we already have PHI nodes in this
271 // block, walk one of them to get the predecessor list instead.
272 BasicBlock *BB = Info->BB;
273 if (PHINode *SomePhi = dyn_cast(BB->begin())) {
274 for (unsigned PI = 0, E = SomePhi->getNumIncomingValues(); PI != E; ++PI)
275 Preds->push_back(SomePhi->getIncomingBlock(PI));
276 } else {
277 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
278 Preds->push_back(*PI);
279 }
280
281 Info->NumPreds = Preds->size();
282 Info->Preds = static_cast
283 (Allocator->Allocate(Info->NumPreds * sizeof(SSAUpdater::BBInfo*),
284 AlignOf::Alignment));
285 }
286
287 /// BuildBlockList - Starting from the specified basic block, traverse back
288 /// through its predecessors until reaching blocks with known values. Create
289 /// BBInfo structures for the blocks and append them to the block list.
290 void SSAUpdater::BuildBlockList(BasicBlock *BB, BlockListTy *BlockList,
291 BumpPtrAllocator *Allocator) {
292 AvailableValsTy &AvailableVals = getAvailableVals(AV);
293 BBMapTy *BBMap = getBBMap(BM);
294 SmallVector RootList;
295 SmallVector WorkList;
296
297 BBInfo *Info = new (*Allocator) BBInfo(BB, 0);
298 (*BBMap)[BB] = Info;
299 WorkList.push_back(Info);
300
301 // Search backward from BB, creating BBInfos along the way and stopping when
302 // reaching blocks that define the value. Record those defining blocks on
303 // the RootList.
304 SmallVector Preds;
305 while (!WorkList.empty()) {
306 Info = WorkList.pop_back_val();
307 Preds.clear();
308 FindPredecessorBlocks(Info, &Preds, Allocator);
309
310 // Treat an unreachable predecessor as a definition with 'undef'.
311 if (Info->NumPreds == 0) {
312 Info->AvailableVal = UndefValue::get(PrototypeValue->getType());
313 Info->DefBB = Info;
314 RootList.push_back(Info);
315 continue;
316 }
317
318 for (unsigned p = 0; p != Info->NumPreds; ++p) {
319 BasicBlock *Pred = Preds[p];
320 // Check if BBMap already has a BBInfo for the predecessor block.
321 BBMapTy::value_type &BBMapBucket = BBMap->FindAndConstruct(Pred);
322 if (BBMapBucket.second) {
323 Info->Preds[p] = BBMapBucket.second;
324 continue;
325 }
326
327 // Create a new BBInfo for the predecessor.
328 Value *PredVal = AvailableVals.lookup(Pred);
329 BBInfo *PredInfo = new (*Allocator) BBInfo(Pred, PredVal);
330 BBMapBucket.second = PredInfo;
331 Info->Preds[p] = PredInfo;
332
333 if (PredInfo->AvailableVal) {
334 RootList.push_back(PredInfo);
335 continue;
336 }
337 WorkList.push_back(PredInfo);
338 }
339 }
340
341 // Now that we know what blocks are backwards-reachable from the starting
342 // block, do a forward depth-first traversal to assign postorder numbers
343 // to those blocks.
344 BBInfo *PseudoEntry = new (*Allocator) BBInfo(0, 0);
345 unsigned BlkNum = 1;
346
347 // Initialize the worklist with the roots from the backward traversal.
348 while (!RootList.empty()) {
349 Info = RootList.pop_back_val();
350 Info->IDom = PseudoEntry;
351 Info->BlkNum = -1;
352 WorkList.push_back(Info);
353 }
354
355 while (!WorkList.empty()) {
356 Info = WorkList.back();
357
358 if (Info->BlkNum == -2) {
359 // All the successors have been handled; assign the postorder number.
360 Info->BlkNum = BlkNum++;
361 // If not a root, put it on the BlockList.
362 if (!Info->AvailableVal)
363 BlockList->push_back(Info);
364 WorkList.pop_back();
365 continue;
366 }
367
368 // Leave this entry on the worklist, but set its BlkNum to mark that its
369 // successors have been put on the worklist. When it returns to the top
370 // the list, after handling its successors, it will be assigned a number.
371 Info->BlkNum = -2;
372
373 // Add unvisited successors to the work list.
374 for (succ_iterator SI = succ_begin(Info->BB), E = succ_end(Info->BB);
375 SI != E; ++SI) {
376 BBInfo *SuccInfo = (*BBMap)[*SI];
377 if (!SuccInfo || SuccInfo->BlkNum)
378 continue;
379 SuccInfo->BlkNum = -1;
380 WorkList.push_back(SuccInfo);
381 }
382 }
383 PseudoEntry->BlkNum = BlkNum;
384 }
385
386 /// IntersectDominators - This is the dataflow lattice "meet" operation for
387 /// finding dominators. Given two basic blocks, it walks up the dominator
388 /// tree until it finds a common dominator of both. It uses the postorder
389 /// number of the blocks to determine how to do that.
390 static SSAUpdater::BBInfo *IntersectDominators(SSAUpdater::BBInfo *Blk1,
391 SSAUpdater::BBInfo *Blk2) {
392 while (Blk1 != Blk2) {
393 while (Blk1->BlkNum < Blk2->BlkNum) {
394 Blk1 = Blk1->IDom;
395 if (!Blk1)
396 return Blk2;
397 }
398 while (Blk2->BlkNum < Blk1->BlkNum) {
399 Blk2 = Blk2->IDom;
400 if (!Blk2)
401 return Blk1;
402 }
403 }
404 return Blk1;
405 }
406
407 /// FindDominators - Calculate the dominator tree for the subset of the CFG
408 /// corresponding to the basic blocks on the BlockList. This uses the
409 /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey and
410 /// Kennedy, published in Software--Practice and Experience, 2001, 4:1-10.
411 /// Because the CFG subset does not include any edges leading into blocks that
412 /// define the value, the results are not the usual dominator tree. The CFG
413 /// subset has a single pseudo-entry node with edges to a set of root nodes
414 /// for blocks that define the value. The dominators for this subset CFG are
415 /// not the standard dominators but they are adequate for placing PHIs within
416 /// the subset CFG.
417 void SSAUpdater::FindDominators(BlockListTy *BlockList) {
418 bool Changed;
419 do {
420 Changed = false;
421 // Iterate over the list in reverse order, i.e., forward on CFG edges.
422 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
423 E = BlockList->rend(); I != E; ++I) {
424 BBInfo *Info = *I;
425
426 // Start with the first predecessor.
427 assert(Info->NumPreds > 0 && "unreachable block");
428 BBInfo *NewIDom = Info->Preds[0];
429
430 // Iterate through the block's other predecessors.
431 for (unsigned p = 1; p != Info->NumPreds; ++p) {
432 BBInfo *Pred = Info->Preds[p];
433 NewIDom = IntersectDominators(NewIDom, Pred);
434 }
435
436 // Check if the IDom value has changed.
437 if (NewIDom != Info->IDom) {
438 Info->IDom = NewIDom;
439 Changed = true;
440 }
441 }
442 } while (Changed);
443 }
444
445 /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for
446 /// any blocks containing definitions of the value. If one is found, then the
447 /// successor of Pred is in the dominance frontier for the definition, and
448 /// this function returns true.
449 static bool IsDefInDomFrontier(const SSAUpdater::BBInfo *Pred,
450 const SSAUpdater::BBInfo *IDom) {
451 for (; Pred != IDom; Pred = Pred->IDom) {
452 if (Pred->DefBB == Pred)
453 return true;
454 }
455 return false;
456 }
457
458 /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers of
459 /// the known definitions. Iteratively add PHIs in the dom frontiers until
460 /// nothing changes. Along the way, keep track of the nearest dominating
461 /// definitions for non-PHI blocks.
462 void SSAUpdater::FindPHIPlacement(BlockListTy *BlockList) {
463 bool Changed;
464 do {
465 Changed = false;
466 // Iterate over the list in reverse order, i.e., forward on CFG edges.
467 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
468 E = BlockList->rend(); I != E; ++I) {
469 BBInfo *Info = *I;
470
471 // If this block already needs a PHI, there is nothing to do here.
472 if (Info->DefBB == Info)
473 continue;
474
475 // Default to use the same def as the immediate dominator.
476 BBInfo *NewDefBB = Info->IDom->DefBB;
477 for (unsigned p = 0; p != Info->NumPreds; ++p) {
478 if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) {
479 // Need a PHI here.
480 NewDefBB = Info;
481 break;
482 }
483 }
484
485 // Check if anything changed.
486 if (NewDefBB != Info->DefBB) {
487 Info->DefBB = NewDefBB;
488 Changed = true;
489 }
490 }
491 } while (Changed);
492 }
493
494 /// FindAvailableVal - If this block requires a PHI, first check if an existing
495 /// PHI matches the PHI placement and reaching definitions computed earlier,
496 /// and if not, create a new PHI. Visit all the block's predecessors to
497 /// calculate the available value for each one and fill in the incoming values
498 /// for a new PHI.
499 void SSAUpdater::FindAvailableVals(BlockListTy *BlockList) {
500 AvailableValsTy &AvailableVals = getAvailableVals(AV);
501
502 // Go through the worklist in forward order (i.e., backward through the CFG)
503 // and check if existing PHIs can be used. If not, create empty PHIs where
504 // they are needed.
505 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
506 I != E; ++I) {
507 BBInfo *Info = *I;
508 // Check if there needs to be a PHI in BB.
509 if (Info->DefBB != Info)
510 continue;
511
512 // Look for an existing PHI.
513 FindExistingPHI(Info->BB, BlockList);
514 if (Info->AvailableVal)
515 continue;
516
517 PHINode *PHI = PHINode::Create(PrototypeValue->getType(),
518 PrototypeValue->getName(),
519 &Info->BB->front());
520 PHI->reserveOperandSpace(Info->NumPreds);
521 Info->AvailableVal = PHI;
522 AvailableVals[Info->BB] = PHI;
523 }
524
525 // Now go back through the worklist in reverse order to fill in the arguments
526 // for any new PHIs added in the forward traversal.
527 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
528 E = BlockList->rend(); I != E; ++I) {
529 BBInfo *Info = *I;
530
531 if (Info->DefBB != Info) {
532 // Record the available value at join nodes to speed up subsequent
533 // uses of this SSAUpdater for the same value.
534 if (Info->NumPreds > 1)
535 AvailableVals[Info->BB] = Info->DefBB->AvailableVal;
536 continue;
537 }
538
539 // Check if this block contains a newly added PHI.
540 PHINode *PHI = dyn_cast(Info->AvailableVal);
541 if (!PHI || PHI->getNumIncomingValues() == Info->NumPreds)
542 continue;
543
544 // Iterate through the block's predecessors.
545 for (unsigned p = 0; p != Info->NumPreds; ++p) {
546 BBInfo *PredInfo = Info->Preds[p];
547 BasicBlock *Pred = PredInfo->BB;
548 // Skip to the nearest preceding definition.
549 if (PredInfo->DefBB != PredInfo)
550 PredInfo = PredInfo->DefBB;
551 PHI->addIncoming(PredInfo->AvailableVal, Pred);
552 }
553
554 DEBUG(dbgs() << " Inserted PHI: " << *PHI << "\n");
555
556 // If the client wants to know about all new instructions, tell it.
557 if (InsertedPHIs) InsertedPHIs->push_back(PHI);
558 }
559 }
560
561 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
562 /// them match what is needed.
563 void SSAUpdater::FindExistingPHI(BasicBlock *BB, BlockListTy *BlockList) {
564 PHINode *SomePHI;
565 for (BasicBlock::iterator It = BB->begin();
566 (SomePHI = dyn_cast(It)); ++It) {
567 if (CheckIfPHIMatches(SomePHI)) {
568 RecordMatchingPHI(SomePHI);
569 break;
570 }
571 // Match failed: clear all the PHITag values.
572 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
573 I != E; ++I)
574 (*I)->PHITag = 0;
575 }
576 }
577
578 /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
579 /// in the BBMap.
580 bool SSAUpdater::CheckIfPHIMatches(PHINode *PHI) {
581 BBMapTy *BBMap = getBBMap(BM);
582 SmallVector WorkList;
583 WorkList.push_back(PHI);
584
585 // Mark that the block containing this PHI has been visited.
586 (*BBMap)[PHI->getParent()]->PHITag = PHI;
587
588 while (!WorkList.empty()) {
589 PHI = WorkList.pop_back_val();
590
591 // Iterate through the PHI's incoming values.
592 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
593 Value *IncomingVal = PHI->getIncomingValue(i);
594 BBInfo *PredInfo = (*BBMap)[PHI->getIncomingBlock(i)];
595 // Skip to the nearest preceding definition.
596 if (PredInfo->DefBB != PredInfo)
597 PredInfo = PredInfo->DefBB;
598
599 // Check if it matches the expected value.
600 if (PredInfo->AvailableVal) {
601 if (IncomingVal == PredInfo->AvailableVal)
602 continue;
603 return false;
604 }
605
606 // Check if the value is a PHI in the correct block.
607 PHINode *IncomingPHIVal = dyn_cast(IncomingVal);
608 if (!IncomingPHIVal || IncomingPHIVal->getParent() != PredInfo->BB)
609 return false;
610
611 // If this block has already been visited, check if this PHI matches.
612 if (PredInfo->PHITag) {
613 if (IncomingPHIVal == PredInfo->PHITag)
614 continue;
615 return false;
616 }
617 PredInfo->PHITag = IncomingPHIVal;
618
619 WorkList.push_back(IncomingPHIVal);
620 }
621 }
622 return true;
623 }
624
625 /// RecordMatchingPHI - For a PHI node that matches, record it and its input
626 /// PHIs in both the BBMap and the AvailableVals mapping.
627 void SSAUpdater::RecordMatchingPHI(PHINode *PHI) {
628 BBMapTy *BBMap = getBBMap(BM);
629 AvailableValsTy &AvailableVals = getAvailableVals(AV);
630 SmallVector WorkList;
631 WorkList.push_back(PHI);
632
633 // Record this PHI.
634 BasicBlock *BB = PHI->getParent();
635 AvailableVals[BB] = PHI;
636 (*BBMap)[BB]->AvailableVal = PHI;
637
638 while (!WorkList.empty()) {
639 PHI = WorkList.pop_back_val();
640
641 // Iterate through the PHI's incoming values.
642 for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
643 PHINode *IncomingPHIVal = dyn_cast(PHI->getIncomingValue(i));
644 if (!IncomingPHIVal) continue;
645 BB = IncomingPHIVal->getParent();
646 BBInfo *Info = (*BBMap)[BB];
647 if (!Info || Info->AvailableVal)
648 continue;
649
650 // Record the PHI and add it to the worklist.
651 AvailableVals[BB] = IncomingPHIVal;
652 Info->AvailableVal = IncomingPHIVal;
653 WorkList.push_back(IncomingPHIVal);
654 }
655 }
656 }
327 SSAUpdaterImpl Impl(this, &AvailableVals, InsertedPHIs);
328 return Impl.GetValue(BB);
329 }