blob: 72f731e053d56a9960ea7610e656931071dadf56 [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"
Evan Cheng229694f2009-12-03 02:31:43 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Evan Cheng651ea532009-12-02 22:02:52 +000026using namespace llvm;
27
Evan Cheng229694f2009-12-03 02:31:43 +000028typedef DenseMap<MachineBasicBlock*, unsigned> AvailableValsTy;
Evan Cheng651ea532009-12-02 22:02:52 +000029typedef std::vector<std::pair<MachineBasicBlock*, unsigned> >
30 IncomingPredInfoTy;
31
32static AvailableValsTy &getAvailableVals(void *AV) {
33 return *static_cast<AvailableValsTy*>(AV);
34}
35
36static IncomingPredInfoTy &getIncomingPredInfo(void *IPI) {
37 return *static_cast<IncomingPredInfoTy*>(IPI);
38}
39
40
Evan Cheng229694f2009-12-03 02:31:43 +000041MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
42 SmallVectorImpl<MachineInstr*> *NewPHI)
43 : AV(0), IPI(0), InsertedPHIs(NewPHI) {
44 TII = MF.getTarget().getInstrInfo();
45 MRI = &MF.getRegInfo();
46}
Evan Cheng651ea532009-12-02 22:02:52 +000047
48MachineSSAUpdater::~MachineSSAUpdater() {
49 delete &getAvailableVals(AV);
50 delete &getIncomingPredInfo(IPI);
51}
52
53/// Initialize - Reset this object to get ready for a new set of SSA
54/// updates. ProtoValue is the value used to name PHI nodes.
Evan Cheng229694f2009-12-03 02:31:43 +000055void MachineSSAUpdater::Initialize(unsigned V) {
Evan Cheng651ea532009-12-02 22:02:52 +000056 if (AV == 0)
57 AV = new AvailableValsTy();
58 else
59 getAvailableVals(AV).clear();
60
61 if (IPI == 0)
62 IPI = new IncomingPredInfoTy();
63 else
64 getIncomingPredInfo(IPI).clear();
Evan Cheng229694f2009-12-03 02:31:43 +000065
66 VR = V;
67 VRC = MRI->getRegClass(VR);
Evan Cheng651ea532009-12-02 22:02:52 +000068}
69
70/// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
71/// the specified block.
72bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
73 return getAvailableVals(AV).count(BB);
74}
75
76/// AddAvailableValue - Indicate that a rewritten value is available in the
77/// specified block with the specified value.
78void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) {
79 getAvailableVals(AV)[BB] = V;
80}
81
82/// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
83/// live at the end of the specified block.
84unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
85 return GetValueAtEndOfBlockInternal(BB);
86}
87
Evan Cheng3a41ddb2009-12-07 23:11:03 +000088static
89unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
90 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> &PredValues) {
91 if (BB->empty())
92 return 0;
93
94 MachineBasicBlock::iterator I = BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +000095 if (!I->isPHI())
Evan Cheng3a41ddb2009-12-07 23:11:03 +000096 return 0;
97
98 AvailableValsTy AVals;
99 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
100 AVals[PredValues[i].first] = PredValues[i].second;
Chris Lattner518bb532010-02-09 19:54:29 +0000101 while (I != BB->end() && I->isPHI()) {
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000102 bool Same = true;
103 for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
104 unsigned SrcReg = I->getOperand(i).getReg();
105 MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
106 if (AVals[SrcBB] != SrcReg) {
107 Same = false;
108 break;
109 }
110 }
111 if (Same)
112 return I->getOperand(0).getReg();
113 ++I;
114 }
115 return 0;
116}
117
Evan Cheng01306ca2009-12-03 21:51:55 +0000118/// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
119/// a value of the given register class at the start of the specified basic
120/// block. It returns the virtual register defined by the instruction.
Evan Cheng229694f2009-12-03 02:31:43 +0000121static
Evan Cheng01306ca2009-12-03 21:51:55 +0000122MachineInstr *InsertNewDef(unsigned Opcode,
123 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
124 const TargetRegisterClass *RC,
125 MachineRegisterInfo *MRI, const TargetInstrInfo *TII) {
Evan Cheng229694f2009-12-03 02:31:43 +0000126 unsigned NewVR = MRI->createVirtualRegister(RC);
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000127 return BuildMI(*BB, I, DebugLoc::getUnknownLoc(), TII->get(Opcode), NewVR);
Evan Cheng229694f2009-12-03 02:31:43 +0000128}
129
Evan Cheng651ea532009-12-02 22:02:52 +0000130/// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
131/// is live in the middle of the specified block.
132///
133/// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
134/// important case: if there is a definition of the rewritten value after the
135/// 'use' in BB. Consider code like this:
136///
137/// X1 = ...
138/// SomeBB:
139/// use(X)
140/// X2 = ...
141/// br Cond, SomeBB, OutBB
142///
143/// In this case, there are two values (X1 and X2) added to the AvailableVals
144/// set by the client of the rewriter, and those values are both live out of
145/// their respective blocks. However, the use of X happens in the *middle* of
146/// a block. Because of this, we need to insert a new PHI node in SomeBB to
147/// merge the appropriate values, and this value isn't live out of the block.
148///
149unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
Evan Cheng229694f2009-12-03 02:31:43 +0000150 // If there is no definition of the renamed variable in this block, just use
151 // GetValueAtEndOfBlock to do our work.
152 if (!getAvailableVals(AV).count(BB))
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000153 return GetValueAtEndOfBlockInternal(BB);
Evan Cheng229694f2009-12-03 02:31:43 +0000154
Evan Cheng01306ca2009-12-03 21:51:55 +0000155 // If there are no predecessors, just return undef.
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000156 if (BB->pred_empty()) {
157 // Insert an implicit_def to represent an undef value.
Chris Lattner518bb532010-02-09 19:54:29 +0000158 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000159 BB, BB->getFirstTerminator(),
160 VRC, MRI, TII);
161 return NewDef->getOperand(0).getReg();
162 }
Evan Cheng229694f2009-12-03 02:31:43 +0000163
164 // Otherwise, we have the hard case. Get the live-in values for each
165 // predecessor.
166 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues;
167 unsigned SingularValue = 0;
168
169 bool isFirstPred = true;
170 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
171 E = BB->pred_end(); PI != E; ++PI) {
172 MachineBasicBlock *PredBB = *PI;
173 unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB);
174 PredValues.push_back(std::make_pair(PredBB, PredVal));
175
176 // Compute SingularValue.
177 if (isFirstPred) {
178 SingularValue = PredVal;
179 isFirstPred = false;
180 } else if (PredVal != SingularValue)
181 SingularValue = 0;
182 }
183
184 // Otherwise, if all the merged values are the same, just use it.
185 if (SingularValue != 0)
186 return SingularValue;
187
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000188 // If an identical PHI is already in BB, just reuse it.
189 unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
190 if (DupPHI)
191 return DupPHI;
192
Evan Cheng229694f2009-12-03 02:31:43 +0000193 // Otherwise, we do need a PHI: insert one now.
Evan Cheng7e572eb2009-12-07 03:07:01 +0000194 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +0000195 MachineInstr *InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
Evan Cheng7e572eb2009-12-07 03:07:01 +0000196 Loc, VRC, MRI, TII);
Evan Cheng229694f2009-12-03 02:31:43 +0000197
198 // Fill in all the predecessors of the PHI.
199 MachineInstrBuilder MIB(InsertedPHI);
200 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
201 MIB.addReg(PredValues[i].second).addMBB(PredValues[i].first);
202
203 // See if the PHI node can be merged to a single value. This can happen in
204 // loop cases when we get a PHI of itself and one other value.
205 if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
206 InsertedPHI->eraseFromParent();
207 return ConstVal;
208 }
209
210 // If the client wants to know about all new instructions, tell it.
211 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
212
David Greene6bb93ef2010-01-05 00:10:05 +0000213 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng229694f2009-12-03 02:31:43 +0000214 return InsertedPHI->getOperand(0).getReg();
215}
216
217static
218MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
219 MachineOperand *U) {
220 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
221 if (&MI->getOperand(i) == U)
222 return MI->getOperand(i+1).getMBB();
223 }
224
225 llvm_unreachable("MachineOperand::getParent() failure?");
Evan Cheng651ea532009-12-02 22:02:52 +0000226 return 0;
227}
228
229/// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
230/// which use their value in the corresponding predecessor.
Evan Cheng229694f2009-12-03 02:31:43 +0000231void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
232 MachineInstr *UseMI = U.getParent();
233 unsigned NewVR = 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000234 if (UseMI->isPHI()) {
Evan Cheng229694f2009-12-03 02:31:43 +0000235 MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000236 NewVR = GetValueAtEndOfBlockInternal(SourceBB);
Evan Cheng2e65c292009-12-04 00:09:05 +0000237 } else {
238 NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
Evan Cheng01306ca2009-12-03 21:51:55 +0000239 }
Evan Cheng2e65c292009-12-04 00:09:05 +0000240
Evan Cheng229694f2009-12-03 02:31:43 +0000241 U.setReg(NewVR);
242}
Evan Cheng651ea532009-12-02 22:02:52 +0000243
Evan Cheng75eb5352009-12-07 10:15:19 +0000244void MachineSSAUpdater::ReplaceRegWith(unsigned OldReg, unsigned NewReg) {
245 MRI->replaceRegWith(OldReg, NewReg);
246
247 AvailableValsTy &AvailableVals = getAvailableVals(AV);
248 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
249 I = AvailableVals.begin(), E = AvailableVals.end(); I != E; ++I)
250 if (I->second == OldReg)
251 I->second = NewReg;
252}
253
Evan Cheng651ea532009-12-02 22:02:52 +0000254/// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
255/// for the specified BB and if so, return it. If not, construct SSA form by
256/// walking predecessors inserting PHI nodes as needed until we get to a block
257/// where the value is available.
258///
259unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
Evan Cheng229694f2009-12-03 02:31:43 +0000260 AvailableValsTy &AvailableVals = getAvailableVals(AV);
261
262 // Query AvailableVals by doing an insertion of null.
263 std::pair<AvailableValsTy::iterator, bool> InsertRes =
264 AvailableVals.insert(std::make_pair(BB, 0));
265
266 // Handle the case when the insertion fails because we have already seen BB.
267 if (!InsertRes.second) {
268 // If the insertion failed, there are two cases. The first case is that the
269 // value is already available for the specified block. If we get this, just
270 // return the value.
271 if (InsertRes.first->second != 0)
272 return InsertRes.first->second;
273
274 // Otherwise, if the value we find is null, then this is the value is not
275 // known but it is being computed elsewhere in our recursion. This means
276 // that we have a cycle. Handle this by inserting a PHI node and returning
277 // it. When we get back to the first instance of the recursion we will fill
278 // in the PHI node.
Evan Cheng7e572eb2009-12-07 03:07:01 +0000279 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +0000280 MachineInstr *NewPHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
Evan Cheng01306ca2009-12-03 21:51:55 +0000281 VRC, MRI,TII);
Evan Cheng229694f2009-12-03 02:31:43 +0000282 unsigned NewVR = NewPHI->getOperand(0).getReg();
283 InsertRes.first->second = NewVR;
284 return NewVR;
285 }
286
Evan Cheng01306ca2009-12-03 21:51:55 +0000287 // If there are no predecessors, then we must have found an unreachable block
288 // just return 'undef'. Since there are no predecessors, InsertRes must not
289 // be invalidated.
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000290 if (BB->pred_empty()) {
291 // Insert an implicit_def to represent an undef value.
Chris Lattner518bb532010-02-09 19:54:29 +0000292 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000293 BB, BB->getFirstTerminator(),
294 VRC, MRI, TII);
295 return InsertRes.first->second = NewDef->getOperand(0).getReg();
296 }
Evan Cheng229694f2009-12-03 02:31:43 +0000297
298 // Okay, the value isn't in the map and we just inserted a null in the entry
299 // to indicate that we're processing the block. Since we have no idea what
300 // value is in this block, we have to recurse through our predecessors.
301 //
302 // While we're walking our predecessors, we keep track of them in a vector,
303 // then insert a PHI node in the end if we actually need one. We could use a
304 // smallvector here, but that would take a lot of stack space for every level
305 // of the recursion, just use IncomingPredInfo as an explicit stack.
306 IncomingPredInfoTy &IncomingPredInfo = getIncomingPredInfo(IPI);
307 unsigned FirstPredInfoEntry = IncomingPredInfo.size();
308
309 // As we're walking the predecessors, keep track of whether they are all
310 // producing the same value. If so, this value will capture it, if not, it
311 // will get reset to null. We distinguish the no-predecessor case explicitly
312 // below.
313 unsigned SingularValue = 0;
314 bool isFirstPred = true;
315 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
316 E = BB->pred_end(); PI != E; ++PI) {
317 MachineBasicBlock *PredBB = *PI;
318 unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB);
319 IncomingPredInfo.push_back(std::make_pair(PredBB, PredVal));
320
321 // Compute SingularValue.
322 if (isFirstPred) {
323 SingularValue = PredVal;
324 isFirstPred = false;
325 } else if (PredVal != SingularValue)
326 SingularValue = 0;
327 }
328
329 /// Look up BB's entry in AvailableVals. 'InsertRes' may be invalidated. If
330 /// this block is involved in a loop, a no-entry PHI node will have been
331 /// inserted as InsertedVal. Otherwise, we'll still have the null we inserted
332 /// above.
Evan Cheng2e65c292009-12-04 00:09:05 +0000333 unsigned &InsertedVal = AvailableVals[BB];
Evan Cheng229694f2009-12-03 02:31:43 +0000334
335 // If all the predecessor values are the same then we don't need to insert a
336 // PHI. This is the simple and common case.
337 if (SingularValue) {
338 // If a PHI node got inserted, replace it with the singlar value and delete
339 // it.
340 if (InsertedVal) {
341 MachineInstr *OldVal = MRI->getVRegDef(InsertedVal);
342 // Be careful about dead loops. These RAUW's also update InsertedVal.
343 assert(InsertedVal != SingularValue && "Dead loop?");
Evan Cheng75eb5352009-12-07 10:15:19 +0000344 ReplaceRegWith(InsertedVal, SingularValue);
Evan Cheng229694f2009-12-03 02:31:43 +0000345 OldVal->eraseFromParent();
Evan Cheng229694f2009-12-03 02:31:43 +0000346 }
347
Evan Cheng2e65c292009-12-04 00:09:05 +0000348 InsertedVal = SingularValue;
349
Evan Cheng229694f2009-12-03 02:31:43 +0000350 // Drop the entries we added in IncomingPredInfo to restore the stack.
351 IncomingPredInfo.erase(IncomingPredInfo.begin()+FirstPredInfoEntry,
352 IncomingPredInfo.end());
353 return InsertedVal;
354 }
355
356
357 // Otherwise, we do need a PHI: insert one now if we don't already have one.
358 MachineInstr *InsertedPHI;
359 if (InsertedVal == 0) {
Evan Cheng7e572eb2009-12-07 03:07:01 +0000360 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
Chris Lattner518bb532010-02-09 19:54:29 +0000361 InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
Evan Cheng01306ca2009-12-03 21:51:55 +0000362 VRC, MRI, TII);
Evan Cheng229694f2009-12-03 02:31:43 +0000363 InsertedVal = InsertedPHI->getOperand(0).getReg();
364 } else {
365 InsertedPHI = MRI->getVRegDef(InsertedVal);
366 }
367
368 // Fill in all the predecessors of the PHI.
369 MachineInstrBuilder MIB(InsertedPHI);
370 for (IncomingPredInfoTy::iterator I =
371 IncomingPredInfo.begin()+FirstPredInfoEntry,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000372 E = IncomingPredInfo.end(); I != E; ++I)
Evan Cheng229694f2009-12-03 02:31:43 +0000373 MIB.addReg(I->second).addMBB(I->first);
374
375 // Drop the entries we added in IncomingPredInfo to restore the stack.
376 IncomingPredInfo.erase(IncomingPredInfo.begin()+FirstPredInfoEntry,
377 IncomingPredInfo.end());
378
379 // See if the PHI node can be merged to a single value. This can happen in
380 // loop cases when we get a PHI of itself and one other value.
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000381 if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
Evan Cheng229694f2009-12-03 02:31:43 +0000382 MRI->replaceRegWith(InsertedVal, ConstVal);
383 InsertedPHI->eraseFromParent();
384 InsertedVal = ConstVal;
385 } else {
David Greene6bb93ef2010-01-05 00:10:05 +0000386 DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng229694f2009-12-03 02:31:43 +0000387
388 // If the client wants to know about all new instructions, tell it.
389 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
390 }
391
392 return InsertedVal;
Evan Cheng651ea532009-12-02 22:02:52 +0000393}