blob: 542491eabbf29c6cbc33b7177a6916f816a4df48 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SmallVector.h"
Eugene Zelenko8fd05042017-09-11 23:00:48 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng651ea532009-12-02 22:02:52 +000020#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng229694f2009-12-03 02:31:43 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko8fd05042017-09-11 23:00:48 +000022#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng229694f2009-12-03 02:31:43 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie48319232017-11-08 01:01:31 +000024#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000025#include "llvm/CodeGen/TargetOpcodes.h"
26#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko8fd05042017-09-11 23:00:48 +000027#include "llvm/IR/DebugLoc.h"
Evan Cheng229694f2009-12-03 02:31:43 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Bob Wilson4aad88d2010-05-04 23:18:19 +000031#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
Eugene Zelenko8fd05042017-09-11 23:00:48 +000032#include <utility>
33
Evan Cheng651ea532009-12-02 22:02:52 +000034using namespace llvm;
35
Chandler Carruth283b3992014-04-21 22:55:11 +000036#define DEBUG_TYPE "machine-ssaupdater"
37
Eugene Zelenko8fd05042017-09-11 23:00:48 +000038using AvailableValsTy = DenseMap<MachineBasicBlock *, unsigned>;
39
Evan Cheng651ea532009-12-02 22:02:52 +000040static AvailableValsTy &getAvailableVals(void *AV) {
41 return *static_cast<AvailableValsTy*>(AV);
42}
43
Evan Cheng229694f2009-12-03 02:31:43 +000044MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
45 SmallVectorImpl<MachineInstr*> *NewPHI)
Eugene Zelenko8fd05042017-09-11 23:00:48 +000046 : InsertedPHIs(NewPHI), TII(MF.getSubtarget().getInstrInfo()),
47 MRI(&MF.getRegInfo()) {}
Evan Cheng651ea532009-12-02 22:02:52 +000048
49MachineSSAUpdater::~MachineSSAUpdater() {
Richard Smithbb57feb2012-08-14 05:31:26 +000050 delete static_cast<AvailableValsTy*>(AV);
Evan Cheng651ea532009-12-02 22:02:52 +000051}
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) {
Craig Topper4ba84432014-04-14 00:51:57 +000056 if (!AV)
Evan Cheng651ea532009-12-02 22:02:52 +000057 AV = new AvailableValsTy();
58 else
59 getAvailableVals(AV).clear();
60
Evan Cheng229694f2009-12-03 02:31:43 +000061 VR = V;
62 VRC = MRI->getRegClass(VR);
Evan Cheng651ea532009-12-02 22:02:52 +000063}
64
65/// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
66/// the specified block.
67bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
68 return getAvailableVals(AV).count(BB);
69}
70
71/// AddAvailableValue - Indicate that a rewritten value is available in the
72/// specified block with the specified value.
73void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) {
74 getAvailableVals(AV)[BB] = V;
75}
76
77/// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
78/// live at the end of the specified block.
79unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
80 return GetValueAtEndOfBlockInternal(BB);
81}
82
Evan Cheng3a41ddb2009-12-07 23:11:03 +000083static
84unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
Eugene Zelenko8fd05042017-09-11 23:00:48 +000085 SmallVectorImpl<std::pair<MachineBasicBlock *, unsigned>> &PredValues) {
Evan Cheng3a41ddb2009-12-07 23:11:03 +000086 if (BB->empty())
87 return 0;
88
Evan Cheng89dae972011-12-06 02:49:06 +000089 MachineBasicBlock::iterator I = BB->begin();
Chris Lattner518bb532010-02-09 19:54:29 +000090 if (!I->isPHI())
Evan Cheng3a41ddb2009-12-07 23:11:03 +000091 return 0;
92
93 AvailableValsTy AVals;
94 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
95 AVals[PredValues[i].first] = PredValues[i].second;
Chris Lattner518bb532010-02-09 19:54:29 +000096 while (I != BB->end() && I->isPHI()) {
Evan Cheng3a41ddb2009-12-07 23:11:03 +000097 bool Same = true;
98 for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
99 unsigned SrcReg = I->getOperand(i).getReg();
100 MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
101 if (AVals[SrcBB] != SrcReg) {
102 Same = false;
103 break;
104 }
105 }
106 if (Same)
107 return I->getOperand(0).getReg();
108 ++I;
109 }
110 return 0;
111}
112
Evan Cheng01306ca2009-12-03 21:51:55 +0000113/// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
114/// a value of the given register class at the start of the specified basic
115/// block. It returns the virtual register defined by the instruction.
Evan Cheng229694f2009-12-03 02:31:43 +0000116static
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +0000117MachineInstrBuilder InsertNewDef(unsigned Opcode,
Evan Cheng01306ca2009-12-03 21:51:55 +0000118 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
119 const TargetRegisterClass *RC,
Bob Wilson4aad88d2010-05-04 23:18:19 +0000120 MachineRegisterInfo *MRI,
121 const TargetInstrInfo *TII) {
Evan Cheng229694f2009-12-03 02:31:43 +0000122 unsigned NewVR = MRI->createVirtualRegister(RC);
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000123 return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
Evan Cheng229694f2009-12-03 02:31:43 +0000124}
Bob Wilson211678a2010-04-26 17:40:49 +0000125
Evan Cheng651ea532009-12-02 22:02:52 +0000126/// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
127/// is live in the middle of the specified block.
128///
129/// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
130/// important case: if there is a definition of the rewritten value after the
131/// 'use' in BB. Consider code like this:
132///
133/// X1 = ...
134/// SomeBB:
135/// use(X)
136/// X2 = ...
137/// br Cond, SomeBB, OutBB
138///
139/// In this case, there are two values (X1 and X2) added to the AvailableVals
140/// set by the client of the rewriter, and those values are both live out of
141/// their respective blocks. However, the use of X happens in the *middle* of
142/// a block. Because of this, we need to insert a new PHI node in SomeBB to
143/// merge the appropriate values, and this value isn't live out of the block.
Evan Cheng651ea532009-12-02 22:02:52 +0000144unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
Evan Cheng229694f2009-12-03 02:31:43 +0000145 // If there is no definition of the renamed variable in this block, just use
146 // GetValueAtEndOfBlock to do our work.
Bob Wilson211678a2010-04-26 17:40:49 +0000147 if (!HasValueForBlock(BB))
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000148 return GetValueAtEndOfBlockInternal(BB);
Evan Cheng229694f2009-12-03 02:31:43 +0000149
Evan Cheng01306ca2009-12-03 21:51:55 +0000150 // If there are no predecessors, just return undef.
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000151 if (BB->pred_empty()) {
152 // Insert an implicit_def to represent an undef value.
Chris Lattner518bb532010-02-09 19:54:29 +0000153 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng9d0f8bb2009-12-04 09:23:37 +0000154 BB, BB->getFirstTerminator(),
155 VRC, MRI, TII);
156 return NewDef->getOperand(0).getReg();
157 }
Evan Cheng229694f2009-12-03 02:31:43 +0000158
159 // Otherwise, we have the hard case. Get the live-in values for each
160 // predecessor.
161 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues;
162 unsigned SingularValue = 0;
163
164 bool isFirstPred = true;
165 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
166 E = BB->pred_end(); PI != E; ++PI) {
167 MachineBasicBlock *PredBB = *PI;
168 unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB);
169 PredValues.push_back(std::make_pair(PredBB, PredVal));
170
171 // Compute SingularValue.
172 if (isFirstPred) {
173 SingularValue = PredVal;
174 isFirstPred = false;
175 } else if (PredVal != SingularValue)
176 SingularValue = 0;
177 }
178
179 // Otherwise, if all the merged values are the same, just use it.
180 if (SingularValue != 0)
181 return SingularValue;
182
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000183 // If an identical PHI is already in BB, just reuse it.
184 unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
185 if (DupPHI)
186 return DupPHI;
187
Evan Cheng229694f2009-12-03 02:31:43 +0000188 // Otherwise, we do need a PHI: insert one now.
Evan Cheng89dae972011-12-06 02:49:06 +0000189 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +0000190 MachineInstrBuilder InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
191 Loc, VRC, MRI, TII);
Evan Cheng229694f2009-12-03 02:31:43 +0000192
193 // Fill in all the predecessors of the PHI.
Evan Cheng229694f2009-12-03 02:31:43 +0000194 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +0000195 InsertedPHI.addReg(PredValues[i].second).addMBB(PredValues[i].first);
Evan Cheng229694f2009-12-03 02:31:43 +0000196
197 // See if the PHI node can be merged to a single value. This can happen in
198 // loop cases when we get a PHI of itself and one other value.
199 if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
200 InsertedPHI->eraseFromParent();
201 return ConstVal;
202 }
203
204 // If the client wants to know about all new instructions, tell it.
205 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
206
Nicola Zaghen0818e782018-05-14 12:53:11 +0000207 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng229694f2009-12-03 02:31:43 +0000208 return InsertedPHI->getOperand(0).getReg();
209}
210
211static
212MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
213 MachineOperand *U) {
214 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
215 if (&MI->getOperand(i) == U)
216 return MI->getOperand(i+1).getMBB();
217 }
218
219 llvm_unreachable("MachineOperand::getParent() failure?");
Evan Cheng651ea532009-12-02 22:02:52 +0000220}
221
222/// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
223/// which use their value in the corresponding predecessor.
Evan Cheng229694f2009-12-03 02:31:43 +0000224void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
225 MachineInstr *UseMI = U.getParent();
226 unsigned NewVR = 0;
Chris Lattner518bb532010-02-09 19:54:29 +0000227 if (UseMI->isPHI()) {
Evan Cheng229694f2009-12-03 02:31:43 +0000228 MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
Evan Cheng3a41ddb2009-12-07 23:11:03 +0000229 NewVR = GetValueAtEndOfBlockInternal(SourceBB);
Evan Cheng2e65c292009-12-04 00:09:05 +0000230 } else {
231 NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
Evan Cheng01306ca2009-12-03 21:51:55 +0000232 }
Evan Cheng2e65c292009-12-04 00:09:05 +0000233
Evan Cheng229694f2009-12-03 02:31:43 +0000234 U.setReg(NewVR);
235}
Evan Cheng651ea532009-12-02 22:02:52 +0000236
Bob Wilson4aad88d2010-05-04 23:18:19 +0000237/// SSAUpdaterTraits<MachineSSAUpdater> - Traits for the SSAUpdaterImpl
238/// template, specialized for MachineSSAUpdater.
239namespace llvm {
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000240
Bob Wilson4aad88d2010-05-04 23:18:19 +0000241template<>
242class SSAUpdaterTraits<MachineSSAUpdater> {
243public:
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000244 using BlkT = MachineBasicBlock;
245 using ValT = unsigned;
246 using PhiT = MachineInstr;
247 using BlkSucc_iterator = MachineBasicBlock::succ_iterator;
Bob Wilson4aad88d2010-05-04 23:18:19 +0000248
Bob Wilson4aad88d2010-05-04 23:18:19 +0000249 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
250 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
251
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000252 /// Iterator for PHI operands.
253 class PHI_iterator {
254 private:
255 MachineInstr *PHI;
256 unsigned idx;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000257
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000258 public:
259 explicit PHI_iterator(MachineInstr *P) // begin iterator
260 : PHI(P), idx(1) {}
261 PHI_iterator(MachineInstr *P, bool) // end iterator
262 : PHI(P), idx(PHI->getNumOperands()) {}
263
Fangrui Songaf7b1832018-07-30 19:41:25 +0000264 PHI_iterator &operator++() { idx += 2; return *this; }
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000265 bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
266 bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000267
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000268 unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); }
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000269
Chandler Carruthfdc2d0f2012-06-20 08:39:30 +0000270 MachineBasicBlock *getIncomingBlock() {
271 return PHI->getOperand(idx+1).getMBB();
272 }
273 };
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000274
Bob Wilson4aad88d2010-05-04 23:18:19 +0000275 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000276
Bob Wilson4aad88d2010-05-04 23:18:19 +0000277 static inline PHI_iterator PHI_end(PhiT *PHI) {
278 return PHI_iterator(PHI, true);
279 }
280
281 /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
282 /// vector.
283 static void FindPredecessorBlocks(MachineBasicBlock *BB,
284 SmallVectorImpl<MachineBasicBlock*> *Preds){
285 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
286 E = BB->pred_end(); PI != E; ++PI)
287 Preds->push_back(*PI);
288 }
289
290 /// GetUndefVal - Create an IMPLICIT_DEF instruction with a new register.
291 /// Add it into the specified block and return the register.
292 static unsigned GetUndefVal(MachineBasicBlock *BB,
293 MachineSSAUpdater *Updater) {
294 // Insert an implicit_def to represent an undef value.
295 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
296 BB, BB->getFirstTerminator(),
297 Updater->VRC, Updater->MRI,
298 Updater->TII);
299 return NewDef->getOperand(0).getReg();
300 }
301
302 /// CreateEmptyPHI - Create a PHI instruction that defines a new register.
303 /// Add it into the specified block and return the register.
304 static unsigned CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds,
305 MachineSSAUpdater *Updater) {
Evan Cheng89dae972011-12-06 02:49:06 +0000306 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
Bob Wilson4aad88d2010-05-04 23:18:19 +0000307 MachineInstr *PHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
308 Updater->VRC, Updater->MRI,
309 Updater->TII);
310 return PHI->getOperand(0).getReg();
311 }
312
313 /// AddPHIOperand - Add the specified value as an operand of the PHI for
314 /// the specified predecessor block.
315 static void AddPHIOperand(MachineInstr *PHI, unsigned Val,
316 MachineBasicBlock *Pred) {
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000317 MachineInstrBuilder(*Pred->getParent(), PHI).addReg(Val).addMBB(Pred);
Bob Wilson4aad88d2010-05-04 23:18:19 +0000318 }
319
320 /// InstrIsPHI - Check if an instruction is a PHI.
Bob Wilson4aad88d2010-05-04 23:18:19 +0000321 static MachineInstr *InstrIsPHI(MachineInstr *I) {
Bob Wilsonfde18e52010-05-10 17:14:26 +0000322 if (I && I->isPHI())
Bob Wilson4aad88d2010-05-04 23:18:19 +0000323 return I;
Craig Topper4ba84432014-04-14 00:51:57 +0000324 return nullptr;
Bob Wilson4aad88d2010-05-04 23:18:19 +0000325 }
326
327 /// ValueIsPHI - Check if the instruction that defines the specified register
328 /// is a PHI instruction.
329 static MachineInstr *ValueIsPHI(unsigned Val, MachineSSAUpdater *Updater) {
330 return InstrIsPHI(Updater->MRI->getVRegDef(Val));
331 }
332
333 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
334 /// operands, i.e., it was just added.
335 static MachineInstr *ValueIsNewPHI(unsigned Val, MachineSSAUpdater *Updater) {
336 MachineInstr *PHI = ValueIsPHI(Val, Updater);
337 if (PHI && PHI->getNumOperands() <= 1)
338 return PHI;
Craig Topper4ba84432014-04-14 00:51:57 +0000339 return nullptr;
Bob Wilson4aad88d2010-05-04 23:18:19 +0000340 }
341
342 /// GetPHIValue - For the specified PHI instruction, return the register
343 /// that it defines.
344 static unsigned GetPHIValue(MachineInstr *PHI) {
345 return PHI->getOperand(0).getReg();
346 }
347};
348
Eugene Zelenko8fd05042017-09-11 23:00:48 +0000349} // end namespace llvm
Bob Wilson4aad88d2010-05-04 23:18:19 +0000350
Evan Cheng651ea532009-12-02 22:02:52 +0000351/// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
352/// for the specified BB and if so, return it. If not, construct SSA form by
Bob Wilson211678a2010-04-26 17:40:49 +0000353/// first calculating the required placement of PHIs and then inserting new
354/// PHIs where needed.
Evan Cheng651ea532009-12-02 22:02:52 +0000355unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
Evan Cheng229694f2009-12-03 02:31:43 +0000356 AvailableValsTy &AvailableVals = getAvailableVals(AV);
Bob Wilson211678a2010-04-26 17:40:49 +0000357 if (unsigned V = AvailableVals[BB])
358 return V;
Evan Cheng229694f2009-12-03 02:31:43 +0000359
Bob Wilson4aad88d2010-05-04 23:18:19 +0000360 SSAUpdaterImpl<MachineSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
361 return Impl.GetValue(BB);
Evan Cheng651ea532009-12-02 22:02:52 +0000362}