1 //===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===//
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 implements the MachineSSAUpdater class. It's based on SSAUpdater
11 // class in lib/Transforms/Utils.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MachineSSAUpdater.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/AlignOf.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
31 /// BBInfo - Per-basic block information used internally by MachineSSAUpdater.
32 class MachineSSAUpdater::BBInfo {
34 MachineBasicBlock *BB; // Back-pointer to the corresponding block.
35 unsigned AvailableVal; // Value to use in this block.
36 BBInfo *DefBB; // Block that defines the available value.
37 int BlkNum; // Postorder number.
38 BBInfo *IDom; // Immediate dominator.
39 unsigned NumPreds; // Number of predecessor blocks.
40 BBInfo **Preds; // Array[NumPreds] of predecessor blocks.
41 MachineInstr *PHITag; // Marker for existing PHIs that match.
43 BBInfo(MachineBasicBlock *ThisBB, unsigned V)
44 : BB(ThisBB), AvailableVal(V), DefBB(V ? this : 0), BlkNum(0), IDom(0),
45 NumPreds(0), Preds(0), PHITag(0) { }
48 typedef DenseMap<MachineBasicBlock*, MachineSSAUpdater::BBInfo*> BBMapTy;
50 typedef DenseMap<MachineBasicBlock*, unsigned> AvailableValsTy;
51 static AvailableValsTy &getAvailableVals(void *AV) {
52 return *static_cast<AvailableValsTy*>(AV);
55 static BBMapTy *getBBMap(void *BM) {
56 return static_cast<BBMapTy*>(BM);
59 MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
60 SmallVectorImpl<MachineInstr*> *NewPHI)
61 : AV(0), BM(0), InsertedPHIs(NewPHI) {
62 TII = MF.getTarget().getInstrInfo();
63 MRI = &MF.getRegInfo();
66 MachineSSAUpdater::~MachineSSAUpdater() {
67 delete &getAvailableVals(AV);
70 /// Initialize - Reset this object to get ready for a new set of SSA
71 /// updates. ProtoValue is the value used to name PHI nodes.
72 void MachineSSAUpdater::Initialize(unsigned V) {
74 AV = new AvailableValsTy();
76 getAvailableVals(AV).clear();
79 VRC = MRI->getRegClass(VR);
82 /// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
83 /// the specified block.
84 bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
85 return getAvailableVals(AV).count(BB);
88 /// AddAvailableValue - Indicate that a rewritten value is available in the
89 /// specified block with the specified value.
90 void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) {
91 getAvailableVals(AV)[BB] = V;
94 /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
95 /// live at the end of the specified block.
96 unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
97 return GetValueAtEndOfBlockInternal(BB);
101 unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
102 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> &PredValues) {
106 MachineBasicBlock::iterator I = BB->front();
110 AvailableValsTy AVals;
111 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
112 AVals[PredValues[i].first] = PredValues[i].second;
113 while (I != BB->end() && I->isPHI()) {
115 for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
116 unsigned SrcReg = I->getOperand(i).getReg();
117 MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
118 if (AVals[SrcBB] != SrcReg) {
124 return I->getOperand(0).getReg();
130 /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
131 /// a value of the given register class at the start of the specified basic
132 /// block. It returns the virtual register defined by the instruction.
134 MachineInstr *InsertNewDef(unsigned Opcode,
135 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
136 const TargetRegisterClass *RC,
137 MachineRegisterInfo *MRI, const TargetInstrInfo *TII) {
138 unsigned NewVR = MRI->createVirtualRegister(RC);
139 return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
142 /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
143 /// is live in the middle of the specified block.
145 /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
146 /// important case: if there is a definition of the rewritten value after the
147 /// 'use' in BB. Consider code like this:
153 /// br Cond, SomeBB, OutBB
155 /// In this case, there are two values (X1 and X2) added to the AvailableVals
156 /// set by the client of the rewriter, and those values are both live out of
157 /// their respective blocks. However, the use of X happens in the *middle* of
158 /// a block. Because of this, we need to insert a new PHI node in SomeBB to
159 /// merge the appropriate values, and this value isn't live out of the block.
161 unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
162 // If there is no definition of the renamed variable in this block, just use
163 // GetValueAtEndOfBlock to do our work.
164 if (!HasValueForBlock(BB))
165 return GetValueAtEndOfBlockInternal(BB);
167 // If there are no predecessors, just return undef.
168 if (BB->pred_empty()) {
169 // Insert an implicit_def to represent an undef value.
170 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
171 BB, BB->getFirstTerminator(),
173 return NewDef->getOperand(0).getReg();
176 // Otherwise, we have the hard case. Get the live-in values for each
178 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues;
179 unsigned SingularValue = 0;
181 bool isFirstPred = true;
182 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
183 E = BB->pred_end(); PI != E; ++PI) {
184 MachineBasicBlock *PredBB = *PI;
185 unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB);
186 PredValues.push_back(std::make_pair(PredBB, PredVal));
188 // Compute SingularValue.
190 SingularValue = PredVal;
192 } else if (PredVal != SingularValue)
196 // Otherwise, if all the merged values are the same, just use it.
197 if (SingularValue != 0)
198 return SingularValue;
200 // If an identical PHI is already in BB, just reuse it.
201 unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
205 // Otherwise, we do need a PHI: insert one now.
206 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
207 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
210 // Fill in all the predecessors of the PHI.
211 MachineInstrBuilder MIB(InsertedPHI);
212 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
213 MIB.addReg(PredValues[i].second).addMBB(PredValues[i].first);
215 // See if the PHI node can be merged to a single value. This can happen in
216 // loop cases when we get a PHI of itself and one other value.
217 if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
218 InsertedPHI->eraseFromParent();
222 // If the client wants to know about all new instructions, tell it.
223 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
225 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
226 return InsertedPHI->getOperand(0).getReg();
230 MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
232 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
233 if (&MI->getOperand(i) == U)
234 return MI->getOperand(i+1).getMBB();
237 llvm_unreachable("MachineOperand::getParent() failure?");
241 /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
242 /// which use their value in the corresponding predecessor.
243 void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
244 MachineInstr *UseMI = U.getParent();
246 if (UseMI->isPHI()) {
247 MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
248 NewVR = GetValueAtEndOfBlockInternal(SourceBB);
250 NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
256 void MachineSSAUpdater::ReplaceRegWith(unsigned OldReg, unsigned NewReg) {
257 MRI->replaceRegWith(OldReg, NewReg);
259 AvailableValsTy &AvailableVals = getAvailableVals(AV);
260 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
261 I = AvailableVals.begin(), E = AvailableVals.end(); I != E; ++I)
262 if (I->second == OldReg)
266 /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
267 /// for the specified BB and if so, return it. If not, construct SSA form by
268 /// first calculating the required placement of PHIs and then inserting new
269 /// PHIs where needed.
270 unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
271 AvailableValsTy &AvailableVals = getAvailableVals(AV);
272 if (unsigned V = AvailableVals[BB])
275 // Pool allocation used internally by GetValueAtEndOfBlock.
276 BumpPtrAllocator Allocator;
280 SmallVector<BBInfo*, 100> BlockList;
281 BuildBlockList(BB, &BlockList, &Allocator);
283 // Special case: bail out if BB is unreachable.
284 if (BlockList.size() == 0) {
286 // Insert an implicit_def to represent an undef value.
287 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
288 BB, BB->getFirstTerminator(),
290 unsigned V = NewDef->getOperand(0).getReg();
291 AvailableVals[BB] = V;
295 FindDominators(&BlockList);
296 FindPHIPlacement(&BlockList);
297 FindAvailableVals(&BlockList);
300 return BBMapObj[BB]->DefBB->AvailableVal;
303 /// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
304 /// vector, set Info->NumPreds, and allocate space in Info->Preds.
305 static void FindPredecessorBlocks(MachineSSAUpdater::BBInfo *Info,
306 SmallVectorImpl<MachineBasicBlock*> *Preds,
307 BumpPtrAllocator *Allocator) {
308 MachineBasicBlock *BB = Info->BB;
309 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
310 E = BB->pred_end(); PI != E; ++PI)
311 Preds->push_back(*PI);
313 Info->NumPreds = Preds->size();
314 Info->Preds = static_cast<MachineSSAUpdater::BBInfo**>
315 (Allocator->Allocate(Info->NumPreds * sizeof(MachineSSAUpdater::BBInfo*),
316 AlignOf<MachineSSAUpdater::BBInfo*>::Alignment));
319 /// BuildBlockList - Starting from the specified basic block, traverse back
320 /// through its predecessors until reaching blocks with known values. Create
321 /// BBInfo structures for the blocks and append them to the block list.
322 void MachineSSAUpdater::BuildBlockList(MachineBasicBlock *BB,
323 BlockListTy *BlockList,
324 BumpPtrAllocator *Allocator) {
325 AvailableValsTy &AvailableVals = getAvailableVals(AV);
326 BBMapTy *BBMap = getBBMap(BM);
327 SmallVector<BBInfo*, 10> RootList;
328 SmallVector<BBInfo*, 64> WorkList;
330 BBInfo *Info = new (*Allocator) BBInfo(BB, 0);
332 WorkList.push_back(Info);
334 // Search backward from BB, creating BBInfos along the way and stopping when
335 // reaching blocks that define the value. Record those defining blocks on
337 SmallVector<MachineBasicBlock*, 10> Preds;
338 while (!WorkList.empty()) {
339 Info = WorkList.pop_back_val();
341 FindPredecessorBlocks(Info, &Preds, Allocator);
343 // Treat an unreachable predecessor as a definition with 'undef'.
344 if (Info->NumPreds == 0) {
345 // Insert an implicit_def to represent an undef value.
346 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
348 Info->BB->getFirstTerminator(),
350 Info->AvailableVal = NewDef->getOperand(0).getReg();
352 RootList.push_back(Info);
356 for (unsigned p = 0; p != Info->NumPreds; ++p) {
357 MachineBasicBlock *Pred = Preds[p];
358 // Check if BBMap already has a BBInfo for the predecessor block.
359 BBMapTy::value_type &BBMapBucket = BBMap->FindAndConstruct(Pred);
360 if (BBMapBucket.second) {
361 Info->Preds[p] = BBMapBucket.second;
365 // Create a new BBInfo for the predecessor.
366 unsigned PredVal = AvailableVals.lookup(Pred);
367 BBInfo *PredInfo = new (*Allocator) BBInfo(Pred, PredVal);
368 BBMapBucket.second = PredInfo;
369 Info->Preds[p] = PredInfo;
371 if (PredInfo->AvailableVal) {
372 RootList.push_back(PredInfo);
375 WorkList.push_back(PredInfo);
379 // Now that we know what blocks are backwards-reachable from the starting
380 // block, do a forward depth-first traversal to assign postorder numbers
382 BBInfo *PseudoEntry = new (*Allocator) BBInfo(0, 0);
385 // Initialize the worklist with the roots from the backward traversal.
386 while (!RootList.empty()) {
387 Info = RootList.pop_back_val();
388 Info->IDom = PseudoEntry;
390 WorkList.push_back(Info);
393 while (!WorkList.empty()) {
394 Info = WorkList.back();
396 if (Info->BlkNum == -2) {
397 // All the successors have been handled; assign the postorder number.
398 Info->BlkNum = BlkNum++;
399 // If not a root, put it on the BlockList.
400 if (!Info->AvailableVal)
401 BlockList->push_back(Info);
406 // Leave this entry on the worklist, but set its BlkNum to mark that its
407 // successors have been put on the worklist. When it returns to the top
408 // the list, after handling its successors, it will be assigned a number.
411 // Add unvisited successors to the work list.
412 for (MachineBasicBlock::succ_iterator SI = Info->BB->succ_begin(),
413 E = Info->BB->succ_end(); SI != E; ++SI) {
414 BBInfo *SuccInfo = (*BBMap)[*SI];
415 if (!SuccInfo || SuccInfo->BlkNum)
417 SuccInfo->BlkNum = -1;
418 WorkList.push_back(SuccInfo);
421 PseudoEntry->BlkNum = BlkNum;
424 /// IntersectDominators - This is the dataflow lattice "meet" operation for
425 /// finding dominators. Given two basic blocks, it walks up the dominator
426 /// tree until it finds a common dominator of both. It uses the postorder
427 /// number of the blocks to determine how to do that.
428 static MachineSSAUpdater::BBInfo *
429 IntersectDominators(MachineSSAUpdater::BBInfo *Blk1,
430 MachineSSAUpdater::BBInfo *Blk2) {
431 while (Blk1 != Blk2) {
432 while (Blk1->BlkNum < Blk2->BlkNum) {
437 while (Blk2->BlkNum < Blk1->BlkNum) {
446 /// FindDominators - Calculate the dominator tree for the subset of the CFG
447 /// corresponding to the basic blocks on the BlockList. This uses the
448 /// algorithm from: "A Simple, Fast Dominance Algorithm" by Cooper, Harvey and
449 /// Kennedy, published in Software--Practice and Experience, 2001, 4:1-10.
450 /// Because the CFG subset does not include any edges leading into blocks that
451 /// define the value, the results are not the usual dominator tree. The CFG
452 /// subset has a single pseudo-entry node with edges to a set of root nodes
453 /// for blocks that define the value. The dominators for this subset CFG are
454 /// not the standard dominators but they are adequate for placing PHIs within
456 void MachineSSAUpdater::FindDominators(BlockListTy *BlockList) {
460 // Iterate over the list in reverse order, i.e., forward on CFG edges.
461 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
462 E = BlockList->rend(); I != E; ++I) {
465 // Start with the first predecessor.
466 assert(Info->NumPreds > 0 && "unreachable block");
467 BBInfo *NewIDom = Info->Preds[0];
469 // Iterate through the block's other predecessors.
470 for (unsigned p = 1; p != Info->NumPreds; ++p) {
471 BBInfo *Pred = Info->Preds[p];
472 NewIDom = IntersectDominators(NewIDom, Pred);
475 // Check if the IDom value has changed.
476 if (NewIDom != Info->IDom) {
477 Info->IDom = NewIDom;
484 /// IsDefInDomFrontier - Search up the dominator tree from Pred to IDom for
485 /// any blocks containing definitions of the value. If one is found, then the
486 /// successor of Pred is in the dominance frontier for the definition, and
487 /// this function returns true.
488 static bool IsDefInDomFrontier(const MachineSSAUpdater::BBInfo *Pred,
489 const MachineSSAUpdater::BBInfo *IDom) {
490 for (; Pred != IDom; Pred = Pred->IDom) {
491 if (Pred->DefBB == Pred)
497 /// FindPHIPlacement - PHIs are needed in the iterated dominance frontiers of
498 /// the known definitions. Iteratively add PHIs in the dom frontiers until
499 /// nothing changes. Along the way, keep track of the nearest dominating
500 /// definitions for non-PHI blocks.
501 void MachineSSAUpdater::FindPHIPlacement(BlockListTy *BlockList) {
505 // Iterate over the list in reverse order, i.e., forward on CFG edges.
506 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
507 E = BlockList->rend(); I != E; ++I) {
510 // If this block already needs a PHI, there is nothing to do here.
511 if (Info->DefBB == Info)
514 // Default to use the same def as the immediate dominator.
515 BBInfo *NewDefBB = Info->IDom->DefBB;
516 for (unsigned p = 0; p != Info->NumPreds; ++p) {
517 if (IsDefInDomFrontier(Info->Preds[p], Info->IDom)) {
524 // Check if anything changed.
525 if (NewDefBB != Info->DefBB) {
526 Info->DefBB = NewDefBB;
533 /// FindAvailableVal - If this block requires a PHI, first check if an existing
534 /// PHI matches the PHI placement and reaching definitions computed earlier,
535 /// and if not, create a new PHI. Visit all the block's predecessors to
536 /// calculate the available value for each one and fill in the incoming values
538 void MachineSSAUpdater::FindAvailableVals(BlockListTy *BlockList) {
539 AvailableValsTy &AvailableVals = getAvailableVals(AV);
541 // Go through the worklist in forward order (i.e., backward through the CFG)
542 // and check if existing PHIs can be used. If not, create empty PHIs where
544 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
547 // Check if there needs to be a PHI in BB.
548 if (Info->DefBB != Info)
551 // Look for an existing PHI.
552 FindExistingPHI(Info->BB, BlockList);
553 if (Info->AvailableVal)
556 MachineBasicBlock::iterator Loc =
557 Info->BB->empty() ? Info->BB->end() : Info->BB->front();
558 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, Info->BB, Loc,
560 unsigned PHI = InsertedPHI->getOperand(0).getReg();
561 Info->AvailableVal = PHI;
562 AvailableVals[Info->BB] = PHI;
565 // Now go back through the worklist in reverse order to fill in the arguments
566 // for any new PHIs added in the forward traversal.
567 for (BlockListTy::reverse_iterator I = BlockList->rbegin(),
568 E = BlockList->rend(); I != E; ++I) {
571 if (Info->DefBB != Info) {
572 // Record the available value at join nodes to speed up subsequent
573 // uses of this SSAUpdater for the same value.
574 if (Info->NumPreds > 1)
575 AvailableVals[Info->BB] = Info->DefBB->AvailableVal;
579 // Check if this block contains a newly added PHI.
580 unsigned PHI = Info->AvailableVal;
581 MachineInstr *InsertedPHI = MRI->getVRegDef(PHI);
582 if (!InsertedPHI->isPHI() || InsertedPHI->getNumOperands() > 1)
585 // Iterate through the block's predecessors.
586 MachineInstrBuilder MIB(InsertedPHI);
587 for (unsigned p = 0; p != Info->NumPreds; ++p) {
588 BBInfo *PredInfo = Info->Preds[p];
589 MachineBasicBlock *Pred = PredInfo->BB;
590 // Skip to the nearest preceding definition.
591 if (PredInfo->DefBB != PredInfo)
592 PredInfo = PredInfo->DefBB;
593 MIB.addReg(PredInfo->AvailableVal).addMBB(Pred);
596 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
598 // If the client wants to know about all new instructions, tell it.
599 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
603 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
604 /// them match what is needed.
605 void MachineSSAUpdater::FindExistingPHI(MachineBasicBlock *BB,
606 BlockListTy *BlockList) {
607 for (MachineBasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
608 BBI != BBE && BBI->isPHI(); ++BBI) {
609 if (CheckIfPHIMatches(BBI)) {
610 RecordMatchingPHI(BBI);
613 // Match failed: clear all the PHITag values.
614 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
620 /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
622 bool MachineSSAUpdater::CheckIfPHIMatches(MachineInstr *PHI) {
623 BBMapTy *BBMap = getBBMap(BM);
624 SmallVector<MachineInstr*, 20> WorkList;
625 WorkList.push_back(PHI);
627 // Mark that the block containing this PHI has been visited.
628 (*BBMap)[PHI->getParent()]->PHITag = PHI;
630 while (!WorkList.empty()) {
631 PHI = WorkList.pop_back_val();
633 // Iterate through the PHI's incoming values.
634 for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) {
635 unsigned IncomingVal = PHI->getOperand(i).getReg();
636 BBInfo *PredInfo = (*BBMap)[PHI->getOperand(i+1).getMBB()];
637 // Skip to the nearest preceding definition.
638 if (PredInfo->DefBB != PredInfo)
639 PredInfo = PredInfo->DefBB;
641 // Check if it matches the expected value.
642 if (PredInfo->AvailableVal) {
643 if (IncomingVal == PredInfo->AvailableVal)
648 // Check if the value is a PHI in the correct block.
649 MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal);
650 if (!IncomingPHIVal->isPHI() ||
651 IncomingPHIVal->getParent() != PredInfo->BB)
654 // If this block has already been visited, check if this PHI matches.
655 if (PredInfo->PHITag) {
656 if (IncomingPHIVal == PredInfo->PHITag)
660 PredInfo->PHITag = IncomingPHIVal;
662 WorkList.push_back(IncomingPHIVal);
668 /// RecordMatchingPHI - For a PHI node that matches, record it and its input
669 /// PHIs in both the BBMap and the AvailableVals mapping.
670 void MachineSSAUpdater::RecordMatchingPHI(MachineInstr *PHI) {
671 BBMapTy *BBMap = getBBMap(BM);
672 AvailableValsTy &AvailableVals = getAvailableVals(AV);
673 SmallVector<MachineInstr*, 20> WorkList;
674 WorkList.push_back(PHI);
677 MachineBasicBlock *BB = PHI->getParent();
678 AvailableVals[BB] = PHI->getOperand(0).getReg();
679 (*BBMap)[BB]->AvailableVal = PHI->getOperand(0).getReg();
681 while (!WorkList.empty()) {
682 PHI = WorkList.pop_back_val();
684 // Iterate through the PHI's incoming values.
685 for (unsigned i = 1, e = PHI->getNumOperands(); i != e; i += 2) {
686 unsigned IncomingVal = PHI->getOperand(i).getReg();
687 MachineInstr *IncomingPHIVal = MRI->getVRegDef(IncomingVal);
688 if (!IncomingPHIVal->isPHI()) continue;
689 BB = IncomingPHIVal->getParent();
690 BBInfo *Info = (*BBMap)[BB];
691 if (!Info || Info->AvailableVal)
694 // Record the PHI and add it to the worklist.
695 AvailableVals[BB] = IncomingVal;
696 Info->AvailableVal = IncomingVal;
697 WorkList.push_back(IncomingPHIVal);