blob: b8996d46f94050604d9ab188c429d934c36c1296 [file] [log] [blame]
Evan Cheng651ea532009-12-02 22:02:52 +00001//===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Evan Cheng229694f2009-12-03 02:31:43 +000010// This file implements the MachineSSAUpdater class. It's based on SSAUpdater
11// class in lib/Transforms/Utils.
Evan Cheng651ea532009-12-02 22:02:52 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/MachineSSAUpdater.h"
16#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng229694f2009-12-03 02:31:43 +000017#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"
Evan Cheng651ea532009-12-02 22:02:52 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattner3143e902010-02-10 01:17:36 +000023#include "llvm/ADT/SmallVector.h"
Bob Wilson211678a2010-04-26 17:40:49 +000024#include "llvm/Support/AlignOf.h"
25#include "llvm/Support/Allocator.h"
Evan Cheng229694f2009-12-03 02:31:43 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Evan Cheng651ea532009-12-02 22:02:52 +000029using namespace llvm;
30
Bob Wilson211678a2010-04-26 17:40:49 +000031/// BBInfo - Per-basic block information used internally by MachineSSAUpdater.
32class MachineSSAUpdater::BBInfo {
33public:
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.
Evan Cheng651ea532009-12-02 22:02:52 +000042
Bob Wilson211678a2010-04-26 17:40:49 +000043 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) { }
46};
47
48typedef DenseMap<MachineBasicBlock*, MachineSSAUpdater::BBInfo*> BBMapTy;
49
50typedef DenseMap<MachineBasicBlock*, unsigned> AvailableValsTy;
Evan Cheng651ea532009-12-02 22:02:52 +000051static AvailableValsTy &getAvailableVals(void *AV) {
52 return *static_cast<AvailableValsTy*>(AV);
53}
54
Bob Wilson211678a2010-04-26 17:40:49 +000055static BBMapTy *getBBMap(void *BM) {
56 return static_cast<BBMapTy*>(BM);
Evan Cheng651ea532009-12-02 22:02:52 +000057}
58
Evan Cheng229694f2009-12-03 02:31:43 +000059MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
60 SmallVectorImpl<MachineInstr*> *NewPHI)
Bob Wilson211678a2010-04-26 17:40:49 +000061 : AV(0), BM(0), InsertedPHIs(NewPHI) {
Evan Cheng229694f2009-12-03 02:31:43 +000062 TII = MF.getTarget().getInstrInfo();
63 MRI = &MF.getRegInfo();
64}
Evan Cheng651ea532009-12-02 22:02:52 +000065
66MachineSSAUpdater::~MachineSSAUpdater() {
67 delete &getAvailableVals(AV);
Evan Cheng651ea532009-12-02 22:02:52 +000068}
69
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.
Evan Cheng229694f2009-12-03 02:31:43 +000072void MachineSSAUpdater::Initialize(unsigned V) {
Evan Cheng651ea532009-12-02 22:02:52 +000073 if (AV == 0)
74 AV = new AvailableValsTy();
75 else
76 getAvailableVals(AV).clear();
77
Evan Cheng229694f2009-12-03 02:31:43 +000078 VR = V;
79 VRC = MRI->getRegClass(VR);
Evan Cheng651ea532009-12-02 22:02:52 +000080}
81
82/// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
83/// the specified block.
84bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
85 return getAvailableVals(AV).count(BB);
86}
87
88/// AddAvailableValue - Indicate that a rewritten value is available in the
89/// specified block with the specified value.
90void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) {
91 getAvailableVals(AV)[BB] = V;
92}
93
94/// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
95/// live at the end of the specified block.
96unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
97 return GetValueAtEndOfBlockInternal(BB);
98}
99
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000100static
101unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
102 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> &PredValues) {
103 if (BB->empty())
104 return 0;
105
106 MachineBasicBlock::iterator I = BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +0000107 if (!I->isPHI())
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000108 return 0;
109
110 AvailableValsTy AVals;
111 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
112 AVals[PredValues[i].first] = PredValues[i].second;
Chris Lattner518bb532010-02-09 19:54:29 +0000113 while (I != BB->end() && I->isPHI()) {
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000114 bool Same = true;
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) {
119 Same = false;
120 break;
121 }
122 }
123 if (Same)
124 return I->getOperand(0).getReg();
125 ++I;
126 }
127 return 0;
128}
129
Evan Cheng01306ca2009-12-03 21:51:55 +0000130/// 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.
Evan Cheng229694f2009-12-03 02:31:43 +0000133static
Evan Cheng01306ca2009-12-03 21:51:55 +0000134MachineInstr *InsertNewDef(unsigned Opcode,
135 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
136 const TargetRegisterClass *RC,
137 MachineRegisterInfo *MRI, const TargetInstrInfo *TII) {
Evan Cheng229694f2009-12-03 02:31:43 +0000138 unsigned NewVR = MRI->createVirtualRegister(RC);
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000139 return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
Evan Cheng229694f2009-12-03 02:31:43 +0000140}
Bob Wilson211678a2010-04-26 17:40:49 +0000141
Evan Cheng651ea532009-12-02 22:02:52 +0000142/// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
143/// is live in the middle of the specified block.
144///
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:
148///
149/// X1 = ...
150/// SomeBB:
151/// use(X)
152/// X2 = ...
153/// br Cond, SomeBB, OutBB
154///
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.
160///
161unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
Evan Cheng229694f2009-12-03 02:31:43 +0000162 // If there is no definition of the renamed variable in this block, just use
163 // GetValueAtEndOfBlock to do our work.
Bob Wilson211678a2010-04-26 17:40:49 +0000164 if (!HasValueForBlock(BB))
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000165 return GetValueAtEndOfBlockInternal(BB);
Evan Cheng229694f2009-12-03 02:31:43 +0000166
Evan Cheng01306ca2009-12-03 21:51:55 +0000167 // If there are no predecessors, just return undef.
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000168 if (BB->pred_empty()) {
169 // Insert an implicit_def to represent an undef value.
Chris Lattner518bb532010-02-09 19:54:29 +0000170 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000171 BB, BB->getFirstTerminator(),
172 VRC, MRI, TII);
173 return NewDef->getOperand(0).getReg();
174 }
Evan Cheng229694f2009-12-03 02:31:43 +0000175
176 // Otherwise, we have the hard case. Get the live-in values for each
177 // predecessor.
178 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues;
179 unsigned SingularValue = 0;
180
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));
187
188 // Compute SingularValue.
189 if (isFirstPred) {
190 SingularValue = PredVal;
191 isFirstPred = false;
192 } else if (PredVal != SingularValue)
193 SingularValue = 0;
194 }
195
196 // Otherwise, if all the merged values are the same, just use it.
197 if (SingularValue != 0)
198 return SingularValue;
199
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000200 // If an identical PHI is already in BB, just reuse it.
201 unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
202 if (DupPHI)
203 return DupPHI;
204
Evan Cheng229694f2009-12-03 02:31:43 +0000205 // Otherwise, we do need a PHI: insert one now.
Evan Cheng7e572eb2009-12-07 03:07:01 +0000206 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +0000207 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
Evan Cheng7e572eb2009-12-07 03:07:01 +0000208 Loc, VRC, MRI, TII);
Evan Cheng229694f2009-12-03 02:31:43 +0000209
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);
214
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();
219 return ConstVal;
220 }
221
222 // If the client wants to know about all new instructions, tell it.
223 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
224
David Greene6bb93ef2010-01-05 00:10:05 +0000225 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng229694f2009-12-03 02:31:43 +0000226 return InsertedPHI->getOperand(0).getReg();
227}
228
229static
230MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
231 MachineOperand *U) {
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();
235 }
236
237 llvm_unreachable("MachineOperand::getParent() failure?");
Evan Cheng651ea532009-12-02 22:02:52 +0000238 return 0;
239}
240
241/// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
242/// which use their value in the corresponding predecessor.
Evan Cheng229694f2009-12-03 02:31:43 +0000243void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
244 MachineInstr *UseMI = U.getParent();
245 unsigned NewVR = 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000246 if (UseMI->isPHI()) {
Evan Cheng229694f2009-12-03 02:31:43 +0000247 MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000248 NewVR = GetValueAtEndOfBlockInternal(SourceBB);
Evan Cheng2e65c292009-12-04 00:09:05 +0000249 } else {
250 NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
Evan Cheng01306ca2009-12-03 21:51:55 +0000251 }
Evan Cheng2e65c292009-12-04 00:09:05 +0000252
Evan Cheng229694f2009-12-03 02:31:43 +0000253 U.setReg(NewVR);
254}
Evan Cheng651ea532009-12-02 22:02:52 +0000255
Evan Cheng75eb5352009-12-07 10:15:19 +0000256void MachineSSAUpdater::ReplaceRegWith(unsigned OldReg, unsigned NewReg) {
257 MRI->replaceRegWith(OldReg, NewReg);
258
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)
263 I->second = NewReg;
264}
265
Evan Cheng651ea532009-12-02 22:02:52 +0000266/// 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
Bob Wilson211678a2010-04-26 17:40:49 +0000268/// first calculating the required placement of PHIs and then inserting new
269/// PHIs where needed.
Evan Cheng651ea532009-12-02 22:02:52 +0000270unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
Evan Cheng229694f2009-12-03 02:31:43 +0000271 AvailableValsTy &AvailableVals = getAvailableVals(AV);
Bob Wilson211678a2010-04-26 17:40:49 +0000272 if (unsigned V = AvailableVals[BB])
273 return V;
Evan Cheng229694f2009-12-03 02:31:43 +0000274
Bob Wilson211678a2010-04-26 17:40:49 +0000275 // Pool allocation used internally by GetValueAtEndOfBlock.
276 BumpPtrAllocator Allocator;
277 BBMapTy BBMapObj;
278 BM = &BBMapObj;
Evan Cheng229694f2009-12-03 02:31:43 +0000279
Bob Wilson211678a2010-04-26 17:40:49 +0000280 SmallVector<BBInfo*, 100> BlockList;
281 BuildBlockList(BB, &BlockList, &Allocator);
Evan Cheng229694f2009-12-03 02:31:43 +0000282
Bob Wilson211678a2010-04-26 17:40:49 +0000283 // Special case: bail out if BB is unreachable.
284 if (BlockList.size() == 0) {
285 BM = 0;
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000286 // Insert an implicit_def to represent an undef value.
Chris Lattner518bb532010-02-09 19:54:29 +0000287 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000288 BB, BB->getFirstTerminator(),
289 VRC, MRI, TII);
Bob Wilson211678a2010-04-26 17:40:49 +0000290 unsigned V = NewDef->getOperand(0).getReg();
291 AvailableVals[BB] = V;
292 return V;
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000293 }
Evan Cheng229694f2009-12-03 02:31:43 +0000294
Bob Wilson211678a2010-04-26 17:40:49 +0000295 FindDominators(&BlockList);
296 FindPHIPlacement(&BlockList);
297 FindAvailableVals(&BlockList);
Evan Cheng229694f2009-12-03 02:31:43 +0000298
Bob Wilson211678a2010-04-26 17:40:49 +0000299 BM = 0;
300 return BBMapObj[BB]->DefBB->AvailableVal;
301}
302
303/// FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds
304/// vector, set Info->NumPreds, and allocate space in Info->Preds.
305static void FindPredecessorBlocks(MachineSSAUpdater::BBInfo *Info,
306 SmallVectorImpl<MachineBasicBlock*> *Preds,
307 BumpPtrAllocator *Allocator) {
308 MachineBasicBlock *BB = Info->BB;
Evan Cheng229694f2009-12-03 02:31:43 +0000309 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
Bob Wilson211678a2010-04-26 17:40:49 +0000310 E = BB->pred_end(); PI != E; ++PI)
311 Preds->push_back(*PI);
Evan Cheng229694f2009-12-03 02:31:43 +0000312
Bob Wilson211678a2010-04-26 17:40:49 +0000313 Info->NumPreds = Preds->size();
314 Info->Preds = static_cast<MachineSSAUpdater::BBInfo**>
315 (Allocator->Allocate(Info->NumPreds * sizeof(MachineSSAUpdater::BBInfo*),
316 AlignOf<MachineSSAUpdater::BBInfo*>::Alignment));
317}
Evan Cheng229694f2009-12-03 02:31:43 +0000318
Bob Wilson211678a2010-04-26 17:40:49 +0000319/// 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.
322void 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;
Evan Cheng229694f2009-12-03 02:31:43 +0000329
Bob Wilson211678a2010-04-26 17:40:49 +0000330 BBInfo *Info = new (*Allocator) BBInfo(BB, 0);
331 (*BBMap)[BB] = Info;
332 WorkList.push_back(Info);
333
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
336 // the RootList.
337 SmallVector<MachineBasicBlock*, 10> Preds;
338 while (!WorkList.empty()) {
339 Info = WorkList.pop_back_val();
340 Preds.clear();
341 FindPredecessorBlocks(Info, &Preds, Allocator);
342
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,
347 Info->BB,
348 Info->BB->getFirstTerminator(),
349 VRC, MRI, TII);
350 Info->AvailableVal = NewDef->getOperand(0).getReg();
351 Info->DefBB = Info;
352 RootList.push_back(Info);
353 continue;
Evan Cheng229694f2009-12-03 02:31:43 +0000354 }
355
Bob Wilson211678a2010-04-26 17:40:49 +0000356 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;
362 continue;
363 }
Evan Cheng2e65c292009-12-04 00:09:05 +0000364
Bob Wilson211678a2010-04-26 17:40:49 +0000365 // 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;
370
371 if (PredInfo->AvailableVal) {
372 RootList.push_back(PredInfo);
373 continue;
374 }
375 WorkList.push_back(PredInfo);
376 }
Evan Cheng229694f2009-12-03 02:31:43 +0000377 }
378
Bob Wilson211678a2010-04-26 17:40:49 +0000379 // Now that we know what blocks are backwards-reachable from the starting
380 // block, do a forward depth-first traversal to assign postorder numbers
381 // to those blocks.
382 BBInfo *PseudoEntry = new (*Allocator) BBInfo(0, 0);
383 unsigned BlkNum = 1;
Evan Cheng229694f2009-12-03 02:31:43 +0000384
Bob Wilson211678a2010-04-26 17:40:49 +0000385 // Initialize the worklist with the roots from the backward traversal.
386 while (!RootList.empty()) {
387 Info = RootList.pop_back_val();
388 Info->IDom = PseudoEntry;
389 Info->BlkNum = -1;
390 WorkList.push_back(Info);
Evan Cheng229694f2009-12-03 02:31:43 +0000391 }
392
Bob Wilson211678a2010-04-26 17:40:49 +0000393 while (!WorkList.empty()) {
394 Info = WorkList.back();
Evan Cheng229694f2009-12-03 02:31:43 +0000395
Bob Wilson211678a2010-04-26 17:40:49 +0000396 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);
402 WorkList.pop_back();
403 continue;
404 }
Evan Cheng229694f2009-12-03 02:31:43 +0000405
Bob Wilson211678a2010-04-26 17:40:49 +0000406 // 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.
409 Info->BlkNum = -2;
410
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)
416 continue;
417 SuccInfo->BlkNum = -1;
418 WorkList.push_back(SuccInfo);
419 }
420 }
421 PseudoEntry->BlkNum = BlkNum;
422}
423
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.
428static MachineSSAUpdater::BBInfo *
429IntersectDominators(MachineSSAUpdater::BBInfo *Blk1,
430 MachineSSAUpdater::BBInfo *Blk2) {
431 while (Blk1 != Blk2) {
432 while (Blk1->BlkNum < Blk2->BlkNum) {
433 Blk1 = Blk1->IDom;
434 if (!Blk1)
435 return Blk2;
436 }
437 while (Blk2->BlkNum < Blk1->BlkNum) {
438 Blk2 = Blk2->IDom;
439 if (!Blk2)
440 return Blk1;
441 }
442 }
443 return Blk1;
444}
445
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
455/// the subset CFG.
456void MachineSSAUpdater::FindDominators(BlockListTy *BlockList) {
457 bool Changed;
458 do {
459 Changed = false;
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) {
463 BBInfo *Info = *I;
464
465 // Start with the first predecessor.
466 assert(Info->NumPreds > 0 && "unreachable block");
467 BBInfo *NewIDom = Info->Preds[0];
468
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);
473 }
474
475 // Check if the IDom value has changed.
476 if (NewIDom != Info->IDom) {
477 Info->IDom = NewIDom;
478 Changed = true;
479 }
480 }
481 } while (Changed);
482}
483
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.
488static bool IsDefInDomFrontier(const MachineSSAUpdater::BBInfo *Pred,
489 const MachineSSAUpdater::BBInfo *IDom) {
490 for (; Pred != IDom; Pred = Pred->IDom) {
491 if (Pred->DefBB == Pred)
492 return true;
493 }
494 return false;
495}
496
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.
501void MachineSSAUpdater::FindPHIPlacement(BlockListTy *BlockList) {
502 bool Changed;
503 do {
504 Changed = false;
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) {
508 BBInfo *Info = *I;
509
510 // If this block already needs a PHI, there is nothing to do here.
511 if (Info->DefBB == Info)
512 continue;
513
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)) {
518 // Need a PHI here.
519 NewDefBB = Info;
520 break;
521 }
522 }
523
524 // Check if anything changed.
525 if (NewDefBB != Info->DefBB) {
526 Info->DefBB = NewDefBB;
527 Changed = true;
528 }
529 }
530 } while (Changed);
531}
532
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
537/// for a new PHI.
538void MachineSSAUpdater::FindAvailableVals(BlockListTy *BlockList) {
539 AvailableValsTy &AvailableVals = getAvailableVals(AV);
540
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
543 // they are needed.
544 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
545 I != E; ++I) {
546 BBInfo *Info = *I;
547 // Check if there needs to be a PHI in BB.
548 if (Info->DefBB != Info)
549 continue;
550
551 // Look for an existing PHI.
552 FindExistingPHI(Info->BB, BlockList);
553 if (Info->AvailableVal)
554 continue;
555
556 MachineBasicBlock::iterator Loc =
557 Info->BB->empty() ? Info->BB->end() : Info->BB->front();
558 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, Info->BB, Loc,
559 VRC, MRI, TII);
560 unsigned PHI = InsertedPHI->getOperand(0).getReg();
561 Info->AvailableVal = PHI;
562 AvailableVals[Info->BB] = PHI;
563 }
564
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) {
569 BBInfo *Info = *I;
570
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;
576 continue;
577 }
578
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)
583 continue;
584
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);
594 }
595
David Greene6bb93ef2010-01-05 00:10:05 +0000596 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng229694f2009-12-03 02:31:43 +0000597
598 // If the client wants to know about all new instructions, tell it.
599 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
600 }
Bob Wilson211678a2010-04-26 17:40:49 +0000601}
Evan Cheng229694f2009-12-03 02:31:43 +0000602
Bob Wilson211678a2010-04-26 17:40:49 +0000603/// FindExistingPHI - Look through the PHI nodes in a block to see if any of
604/// them match what is needed.
605void 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);
611 break;
612 }
613 // Match failed: clear all the PHITag values.
614 for (BlockListTy::iterator I = BlockList->begin(), E = BlockList->end();
615 I != E; ++I)
616 (*I)->PHITag = 0;
617 }
618}
619
620/// CheckIfPHIMatches - Check if a PHI node matches the placement and values
621/// in the BBMap.
622bool MachineSSAUpdater::CheckIfPHIMatches(MachineInstr *PHI) {
623 BBMapTy *BBMap = getBBMap(BM);
624 SmallVector<MachineInstr*, 20> WorkList;
625 WorkList.push_back(PHI);
626
627 // Mark that the block containing this PHI has been visited.
628 (*BBMap)[PHI->getParent()]->PHITag = PHI;
629
630 while (!WorkList.empty()) {
631 PHI = WorkList.pop_back_val();
632
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;
640
641 // Check if it matches the expected value.
642 if (PredInfo->AvailableVal) {
643 if (IncomingVal == PredInfo->AvailableVal)
644 continue;
645 return false;
646 }
647
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)
652 return false;
653
654 // If this block has already been visited, check if this PHI matches.
655 if (PredInfo->PHITag) {
656 if (IncomingPHIVal == PredInfo->PHITag)
657 continue;
658 return false;
659 }
660 PredInfo->PHITag = IncomingPHIVal;
661
662 WorkList.push_back(IncomingPHIVal);
663 }
664 }
665 return true;
666}
667
668/// RecordMatchingPHI - For a PHI node that matches, record it and its input
669/// PHIs in both the BBMap and the AvailableVals mapping.
670void MachineSSAUpdater::RecordMatchingPHI(MachineInstr *PHI) {
671 BBMapTy *BBMap = getBBMap(BM);
672 AvailableValsTy &AvailableVals = getAvailableVals(AV);
673 SmallVector<MachineInstr*, 20> WorkList;
674 WorkList.push_back(PHI);
675
676 // Record this PHI.
677 MachineBasicBlock *BB = PHI->getParent();
678 AvailableVals[BB] = PHI->getOperand(0).getReg();
679 (*BBMap)[BB]->AvailableVal = PHI->getOperand(0).getReg();
680
681 while (!WorkList.empty()) {
682 PHI = WorkList.pop_back_val();
683
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)
692 continue;
693
694 // Record the PHI and add it to the worklist.
695 AvailableVals[BB] = IncomingVal;
696 Info->AvailableVal = IncomingVal;
697 WorkList.push_back(IncomingPHIVal);
698 }
699 }
Evan Cheng651ea532009-12-02 22:02:52 +0000700}