1 //===- SSAUpdaterImpl.h - SSA Updater Implementation ------------*- C++ -*-===//
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 provides a template that implements the core algorithm for the
11 // SSAUpdater and MachineSSAUpdater.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
16 #define LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "ssaupdater"
28 template<typename T> class SSAUpdaterTraits;
30 template<typename UpdaterT>
31 class SSAUpdaterImpl {
35 using Traits = SSAUpdaterTraits<UpdaterT>;
36 using BlkT = typename Traits::BlkT;
37 using ValT = typename Traits::ValT;
38 using PhiT = typename Traits::PhiT;
40 /// BBInfo - Per-basic block information used internally by SSAUpdaterImpl.
41 /// The predecessors of each block are cached here since pred_iterator is
42 /// slow and we need to iterate over the blocks at least a few times.
45 // Back-pointer to the corresponding block.
48 // Value to use in this block.
51 // Block that defines the available value.
57 // Immediate dominator.
58 BBInfo *IDom = nullptr;
60 // Number of predecessor blocks.
61 unsigned NumPreds = 0;
63 // Array[NumPreds] of predecessor blocks.
64 BBInfo **Preds = nullptr;
66 // Marker for existing PHIs that match.
67 PhiT *PHITag = nullptr;
69 BBInfo(BlkT *ThisBB, ValT V)
70 : BB(ThisBB), AvailableVal(V), DefBB(V ? this : nullptr) {}
73 using AvailableValsTy = DenseMap<BlkT *, ValT>;
75 AvailableValsTy *AvailableVals;
77 SmallVectorImpl<PhiT *> *InsertedPHIs;
79 using BlockListTy = SmallVectorImpl<BBInfo *>;
80 using BBMapTy = DenseMap<BlkT *, BBInfo *>;
83 BumpPtrAllocator Allocator;
86 explicit SSAUpdaterImpl(UpdaterT *U, AvailableValsTy *A,
87 SmallVectorImpl<PhiT *> *Ins) :
88 Updater(U), AvailableVals(A), InsertedPHIs(Ins) {}
90 /// GetValue - Check to see if AvailableVals has an entry for the specified
91 /// BB and if so, return it. If not, construct SSA form by first
92 /// calculating the required placement of PHIs and then inserting new PHIs
94 ValT GetValue(BlkT *BB) {
95 SmallVector<BBInfo *, 100> BlockList;
96 BBInfo *PseudoEntry = BuildBlockList(BB, &BlockList);
98 // Special case: bail out if BB is unreachable.
99 if (BlockList.size() == 0) {
100 ValT V = Traits::GetUndefVal(BB, Updater);
101 (*AvailableVals)[BB] = V;
105 FindDominators(&BlockList, PseudoEntry);
106 FindPHIPlacement(&BlockList);
107 FindAvailableVals(&BlockList);
109 return BBMap[BB]->DefBB->AvailableVal;
112 /// BuildBlockList - Starting from the specified basic block, traverse back
113 /// through its predecessors until reaching blocks with known values.
114 /// Create BBInfo structures for the blocks and append them to the block
116 BBInfo *BuildBlockList(BlkT *BB, BlockListTy *BlockList) {
117 SmallVector<BBInfo *, 10> RootList;
118 SmallVector<BBInfo *, 64> WorkList;
120 BBInfo *Info = new (Allocator) BBInfo(BB, 0);
122 WorkList.push_back(Info);
124 // Search backward from BB, creating BBInfos along the way and stopping
125 // when reaching blocks that define the value. Record those defining
126 // blocks on the RootList.
127 SmallVector<BlkT *, 10> Preds;
128 while (!WorkList.empty()) {
129 Info = WorkList.pop_back_val();
131 Traits::FindPredecessorBlocks(Info->BB, &Preds);
132 Info->NumPreds = Preds.size();
133 if (Info->NumPreds == 0)
134 Info->Preds = nullptr;
136 Info->Preds = static_cast<BBInfo **>(Allocator.Allocate(
137 Info->NumPreds * sizeof(BBInfo *), alignof(BBInfo *)));
139 for (unsigned p = 0; p != Info->NumPreds; ++p) {
140 BlkT *Pred = Preds[p];
141 // Check if BBMap already has a BBInfo for the predecessor block.
142 typename BBMapTy::value_type &BBMapBucket =
143 BBMap.FindAndConstruct(Pred);
144 if (BBMapBucket.second) {
145 Info->Preds[p] = BBMapBucket.second;
149 // Create a new BBInfo for the predecessor.
150 ValT PredVal = AvailableVals->lookup(Pred);
151 BBInfo *PredInfo = new (Allocator) BBInfo(Pred, PredVal);
152 BBMapBucket.second = PredInfo;
153 Info->Preds[p] = PredInfo;
155 if (PredInfo->AvailableVal) {
156 RootList.push_back(PredInfo);
159 WorkList.push_back(PredInfo);
163 // Now that we know what blocks are backwards-reachable from the starting
164 // block, do a forward depth-first traversal to assign postorder numbers
166 BBInfo *PseudoEntry = new (Allocator) BBInfo(nullptr, 0);
169 // Initialize the worklist with the roots from the backward traversal.
170 while (!RootList.empty()) {
171 Info = RootList.pop_back_val();
172 Info->IDom = PseudoEntry;
174 WorkList.push_back(Info);
177 while (!WorkList.empty()) {
178 Info = WorkList.back();
180 if (Info->BlkNum == -2) {
181 // All the successors have been handled; assign the postorder number.
182 Info->BlkNum = BlkNum++;
183 // If not a root, put it on the BlockList.
184 if (!Info->AvailableVal)
185 BlockList->push_back(Info);
190 // Leave this entry on the worklist, but set its BlkNum to mark that its
191 // successors have been put on the worklist. When it returns to the top
192 // the list, after handling its successors, it will be assigned a
196 // Add unvisited successors to the work list.
197 for (typename Traits::BlkSucc_iterator SI =
198 Traits::BlkSucc_begin(Info->BB),
199 E = Traits::BlkSucc_end(Info->BB); SI != E; ++SI) {
200 BBInfo *SuccInfo = BBMap[*SI];
201 if (!SuccInfo || SuccInfo->BlkNum)
203 SuccInfo->BlkNum = -1;
204 WorkList.push_back(SuccInfo);
207 PseudoEntry->BlkNum = BlkNum;
211 /// IntersectDominators - This is the dataflow lattice "meet" operation for
212 /// finding dominators. Given two basic blocks, it walks up the dominator
213 /// tree until it finds a common dominator of both. It uses the postorder
214 /// number of the blocks to determine how to do that.
215 BBInfo *IntersectDominators(BBInfo *Blk1, BBInfo *Blk2) {
216 while (Blk1 != Blk2) {
217 while (Blk1->BlkNum < Blk2->BlkNum) {
222 while (Blk2->BlkNum < Blk1->BlkNum) {
231 /// FindDominators - Calculate the dominator tree for the subset of the CFG
232 /// corresponding to the basic blocks on the BlockList. This uses the
233 /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey
234 /// and Kennedy, published in Software--Practice and Experience, 2001,
235 /// 4:1-10. Because the CFG subset does not include any edges leading into
236 /// blocks that define the value, the results are not the usual dominator
237 /// tree. The CFG subset has a single pseudo-entry node with edges to a set
238 /// of root nodes for blocks that define the value. The dominators for this
239 /// subset CFG are not the standard dominators but they are adequate for
240 /// placing PHIs within the subset CFG.
241 void FindDominators(BlockListTy *BlockList, BBInfo *PseudoEntry) {
245 // Iterate over the list in reverse order, i.e., forward on CFG edges.
246 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
247 E = BlockList->rend(); I != E; ++I) {
249 BBInfo *NewIDom = nullptr;
251 // Iterate through the block's predecessors.
252 for (unsigned p = 0; p != Info->NumPreds; ++p) {
253 BBInfo *Pred = Info->Preds[p];
255 // Treat an unreachable predecessor as a definition with 'undef'.
256 if (Pred->BlkNum == 0) {
257 Pred->AvailableVal = Traits::GetUndefVal(Pred->BB, Updater);
258 (*AvailableVals)[Pred->BB] = Pred->AvailableVal;
260 Pred->BlkNum = PseudoEntry->BlkNum;
261 PseudoEntry->BlkNum++;
267 NewIDom = IntersectDominators(NewIDom, Pred);
270 // Check if the IDom value has changed.
271 if (NewIDom && NewIDom != Info->IDom) {
272 Info->IDom = NewIDom;
279 /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for
280 /// any blocks containing definitions of the value. If one is found, then
281 /// the successor of Pred is in the dominance frontier for the definition,
282 /// and this function returns true.
283 bool IsDefInDomFrontier(const BBInfo *Pred, const BBInfo *IDom) {
284 for (; Pred != IDom; Pred = Pred->IDom) {
285 if (Pred->DefBB == Pred)
291 /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers
292 /// of the known definitions. Iteratively add PHIs in the dom frontiers
293 /// until nothing changes. Along the way, keep track of the nearest
294 /// dominating definitions for non-PHI blocks.
295 void FindPHIPlacement(BlockListTy *BlockList) {
299 // Iterate over the list in reverse order, i.e., forward on CFG edges.
300 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
301 E = BlockList->rend(); I != E; ++I) {
304 // If this block already needs a PHI, there is nothing to do here.
305 if (Info->DefBB == Info)
308 // Default to use the same def as the immediate dominator.
309 BBInfo *NewDefBB = Info->IDom->DefBB;
310 for (unsigned p = 0; p != Info->NumPreds; ++p) {
311 if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) {
318 // Check if anything changed.
319 if (NewDefBB != Info->DefBB) {
320 Info->DefBB = NewDefBB;
327 /// FindAvailableVal - If this block requires a PHI, first check if an
328 /// existing PHI matches the PHI placement and reaching definitions computed
329 /// earlier, and if not, create a new PHI. Visit all the block's
330 /// predecessors to calculate the available value for each one and fill in
331 /// the incoming values for a new PHI.
332 void FindAvailableVals(BlockListTy *BlockList) {
333 // Go through the worklist in forward order (i.e., backward through the CFG)
334 // and check if existing PHIs can be used. If not, create empty PHIs where
336 for (typename BlockListTy::iterator I = BlockList->begin(),
337 E = BlockList->end(); I != E; ++I) {
339 // Check if there needs to be a PHI in BB.
340 if (Info->DefBB != Info)
343 // Look for an existing PHI.
344 FindExistingPHI(Info->BB, BlockList);
345 if (Info->AvailableVal)
348 ValT PHI = Traits::CreateEmptyPHI(Info->BB, Info->NumPreds, Updater);
349 Info->AvailableVal = PHI;
350 (*AvailableVals)[Info->BB] = PHI;
353 // Now go back through the worklist in reverse order to fill in the
354 // arguments for any new PHIs added in the forward traversal.
355 for (typename BlockListTy::reverse_iterator I = BlockList->rbegin(),
356 E = BlockList->rend(); I != E; ++I) {
359 if (Info->DefBB != Info) {
360 // Record the available value to speed up subsequent uses of this
361 // SSAUpdater for the same value.
362 (*AvailableVals)[Info->BB] = Info->DefBB->AvailableVal;
366 // Check if this block contains a newly added PHI.
367 PhiT *PHI = Traits::ValueIsNewPHI(Info->AvailableVal, Updater);
371 // Iterate through the block's predecessors.
372 for (unsigned p = 0; p != Info->NumPreds; ++p) {
373 BBInfo *PredInfo = Info->Preds[p];
374 BlkT *Pred = PredInfo->BB;
375 // Skip to the nearest preceding definition.
376 if (PredInfo->DefBB != PredInfo)
377 PredInfo = PredInfo->DefBB;
378 Traits::AddPHIOperand(PHI, PredInfo->AvailableVal, Pred);
381 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *PHI << "\n");
383 // If the client wants to know about all new instructions, tell it.
384 if (InsertedPHIs) InsertedPHIs->push_back(PHI);
388 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
389 /// them match what is needed.
390 void FindExistingPHI(BlkT *BB, BlockListTy *BlockList) {
391 for (auto &SomePHI : BB->phis()) {
392 if (CheckIfPHIMatches(&SomePHI)) {
393 RecordMatchingPHIs(BlockList);
396 // Match failed: clear all the PHITag values.
397 for (typename BlockListTy::iterator I = BlockList->begin(),
398 E = BlockList->end(); I != E; ++I)
399 (*I)->PHITag = nullptr;
403 /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
405 bool CheckIfPHIMatches(PhiT *PHI) {
406 SmallVector<PhiT *, 20> WorkList;
407 WorkList.push_back(PHI);
409 // Mark that the block containing this PHI has been visited.
410 BBMap[PHI->getParent()]->PHITag = PHI;
412 while (!WorkList.empty()) {
413 PHI = WorkList.pop_back_val();
415 // Iterate through the PHI's incoming values.
416 for (typename Traits::PHI_iterator I = Traits::PHI_begin(PHI),
417 E = Traits::PHI_end(PHI); I != E; ++I) {
418 ValT IncomingVal = I.getIncomingValue();
419 BBInfo *PredInfo = BBMap[I.getIncomingBlock()];
420 // Skip to the nearest preceding definition.
421 if (PredInfo->DefBB != PredInfo)
422 PredInfo = PredInfo->DefBB;
424 // Check if it matches the expected value.
425 if (PredInfo->AvailableVal) {
426 if (IncomingVal == PredInfo->AvailableVal)
431 // Check if the value is a PHI in the correct block.
432 PhiT *IncomingPHIVal = Traits::ValueIsPHI(IncomingVal, Updater);
433 if (!IncomingPHIVal || IncomingPHIVal->getParent() != PredInfo->BB)
436 // If this block has already been visited, check if this PHI matches.
437 if (PredInfo->PHITag) {
438 if (IncomingPHIVal == PredInfo->PHITag)
442 PredInfo->PHITag = IncomingPHIVal;
444 WorkList.push_back(IncomingPHIVal);
450 /// RecordMatchingPHIs - For each PHI node that matches, record it in both
451 /// the BBMap and the AvailableVals mapping.
452 void RecordMatchingPHIs(BlockListTy *BlockList) {
453 for (typename BlockListTy::iterator I = BlockList->begin(),
454 E = BlockList->end(); I != E; ++I)
455 if (PhiT *PHI = (*I)->PHITag) {
456 BlkT *BB = PHI->getParent();
457 ValT PHIVal = Traits::GetPHIValue(PHI);
458 (*AvailableVals)[BB] = PHIVal;
459 BBMap[BB]->AvailableVal = PHIVal;
464 } // end namespace llvm
466 #undef DEBUG_TYPE // "ssaupdater"
468 #endif // LLVM_TRANSFORMS_UTILS_SSAUPDATERIMPL_H