blob: e8b42047b49f5952cb0d528674cd90bce35cd179 [file] [log] [blame]
Evan Cheng20e9d032009-12-02 22:02:52 +00001//===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Evan Cheng20e9d032009-12-02 22:02:52 +00006//
7//===----------------------------------------------------------------------===//
8//
Evan Cheng71453822009-12-03 02:31:43 +00009// This file implements the MachineSSAUpdater class. It's based on SSAUpdater
10// class in lib/Transforms/Utils.
Evan Cheng20e9d032009-12-02 22:02:52 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineSSAUpdater.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SmallVector.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng20e9d032009-12-02 22:02:52 +000019#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng71453822009-12-03 02:31:43 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000021#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng71453822009-12-03 02:31:43 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000023#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000024#include "llvm/CodeGen/TargetOpcodes.h"
25#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000026#include "llvm/IR/DebugLoc.h"
Evan Cheng71453822009-12-03 02:31:43 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Bob Wilsond1b38e32010-05-04 23:18:19 +000030#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000031#include <utility>
32
Evan Cheng20e9d032009-12-02 22:02:52 +000033using namespace llvm;
34
Chandler Carruthe96dd892014-04-21 22:55:11 +000035#define DEBUG_TYPE "machine-ssaupdater"
36
Eugene Zelenko32a40562017-09-11 23:00:48 +000037using AvailableValsTy = DenseMap<MachineBasicBlock *, unsigned>;
38
Evan Cheng20e9d032009-12-02 22:02:52 +000039static AvailableValsTy &getAvailableVals(void *AV) {
40 return *static_cast<AvailableValsTy*>(AV);
41}
42
Evan Cheng71453822009-12-03 02:31:43 +000043MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
44 SmallVectorImpl<MachineInstr*> *NewPHI)
Eugene Zelenko32a40562017-09-11 23:00:48 +000045 : InsertedPHIs(NewPHI), TII(MF.getSubtarget().getInstrInfo()),
46 MRI(&MF.getRegInfo()) {}
Evan Cheng20e9d032009-12-02 22:02:52 +000047
48MachineSSAUpdater::~MachineSSAUpdater() {
Richard Smith0ff8f0e2012-08-14 05:31:26 +000049 delete static_cast<AvailableValsTy*>(AV);
Evan Cheng20e9d032009-12-02 22:02:52 +000050}
51
52/// Initialize - Reset this object to get ready for a new set of SSA
53/// updates. ProtoValue is the value used to name PHI nodes.
Evan Cheng71453822009-12-03 02:31:43 +000054void MachineSSAUpdater::Initialize(unsigned V) {
Craig Topperc0196b12014-04-14 00:51:57 +000055 if (!AV)
Evan Cheng20e9d032009-12-02 22:02:52 +000056 AV = new AvailableValsTy();
57 else
58 getAvailableVals(AV).clear();
59
Evan Cheng71453822009-12-03 02:31:43 +000060 VR = V;
61 VRC = MRI->getRegClass(VR);
Evan Cheng20e9d032009-12-02 22:02:52 +000062}
63
64/// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
65/// the specified block.
66bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
67 return getAvailableVals(AV).count(BB);
68}
69
70/// AddAvailableValue - Indicate that a rewritten value is available in the
71/// specified block with the specified value.
72void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, unsigned V) {
73 getAvailableVals(AV)[BB] = V;
74}
75
76/// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
77/// live at the end of the specified block.
78unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
79 return GetValueAtEndOfBlockInternal(BB);
80}
81
Evan Chengfb1654d2009-12-07 23:11:03 +000082static
83unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
Eugene Zelenko32a40562017-09-11 23:00:48 +000084 SmallVectorImpl<std::pair<MachineBasicBlock *, unsigned>> &PredValues) {
Evan Chengfb1654d2009-12-07 23:11:03 +000085 if (BB->empty())
86 return 0;
87
Evan Chengc1610be2011-12-06 02:49:06 +000088 MachineBasicBlock::iterator I = BB->begin();
Chris Lattnerb06015a2010-02-09 19:54:29 +000089 if (!I->isPHI())
Evan Chengfb1654d2009-12-07 23:11:03 +000090 return 0;
91
92 AvailableValsTy AVals;
93 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
94 AVals[PredValues[i].first] = PredValues[i].second;
Chris Lattnerb06015a2010-02-09 19:54:29 +000095 while (I != BB->end() && I->isPHI()) {
Evan Chengfb1654d2009-12-07 23:11:03 +000096 bool Same = true;
97 for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
98 unsigned SrcReg = I->getOperand(i).getReg();
99 MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
100 if (AVals[SrcBB] != SrcReg) {
101 Same = false;
102 break;
103 }
104 }
105 if (Same)
106 return I->getOperand(0).getReg();
107 ++I;
108 }
109 return 0;
110}
111
Evan Chengb2c15292009-12-03 21:51:55 +0000112/// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
113/// a value of the given register class at the start of the specified basic
114/// block. It returns the virtual register defined by the instruction.
Evan Cheng71453822009-12-03 02:31:43 +0000115static
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +0000116MachineInstrBuilder InsertNewDef(unsigned Opcode,
Evan Chengb2c15292009-12-03 21:51:55 +0000117 MachineBasicBlock *BB, MachineBasicBlock::iterator I,
118 const TargetRegisterClass *RC,
Bob Wilsond1b38e32010-05-04 23:18:19 +0000119 MachineRegisterInfo *MRI,
120 const TargetInstrInfo *TII) {
Evan Cheng71453822009-12-03 02:31:43 +0000121 unsigned NewVR = MRI->createVirtualRegister(RC);
Chris Lattnerbd009d62010-04-02 20:17:23 +0000122 return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
Evan Cheng71453822009-12-03 02:31:43 +0000123}
Bob Wilsond561daf2010-04-26 17:40:49 +0000124
Evan Cheng20e9d032009-12-02 22:02:52 +0000125/// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
126/// is live in the middle of the specified block.
127///
128/// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
129/// important case: if there is a definition of the rewritten value after the
130/// 'use' in BB. Consider code like this:
131///
132/// X1 = ...
133/// SomeBB:
134/// use(X)
135/// X2 = ...
136/// br Cond, SomeBB, OutBB
137///
138/// In this case, there are two values (X1 and X2) added to the AvailableVals
139/// set by the client of the rewriter, and those values are both live out of
140/// their respective blocks. However, the use of X happens in the *middle* of
141/// a block. Because of this, we need to insert a new PHI node in SomeBB to
142/// merge the appropriate values, and this value isn't live out of the block.
Evan Cheng20e9d032009-12-02 22:02:52 +0000143unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
Evan Cheng71453822009-12-03 02:31:43 +0000144 // If there is no definition of the renamed variable in this block, just use
145 // GetValueAtEndOfBlock to do our work.
Bob Wilsond561daf2010-04-26 17:40:49 +0000146 if (!HasValueForBlock(BB))
Evan Chengfb1654d2009-12-07 23:11:03 +0000147 return GetValueAtEndOfBlockInternal(BB);
Evan Cheng71453822009-12-03 02:31:43 +0000148
Evan Chengb2c15292009-12-03 21:51:55 +0000149 // If there are no predecessors, just return undef.
Evan Cheng0f1cc352009-12-04 09:23:37 +0000150 if (BB->pred_empty()) {
151 // Insert an implicit_def to represent an undef value.
Chris Lattnerb06015a2010-02-09 19:54:29 +0000152 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
Evan Cheng0f1cc352009-12-04 09:23:37 +0000153 BB, BB->getFirstTerminator(),
154 VRC, MRI, TII);
155 return NewDef->getOperand(0).getReg();
156 }
Evan Cheng71453822009-12-03 02:31:43 +0000157
158 // Otherwise, we have the hard case. Get the live-in values for each
159 // predecessor.
160 SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> PredValues;
161 unsigned SingularValue = 0;
162
163 bool isFirstPred = true;
164 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
165 E = BB->pred_end(); PI != E; ++PI) {
166 MachineBasicBlock *PredBB = *PI;
167 unsigned PredVal = GetValueAtEndOfBlockInternal(PredBB);
168 PredValues.push_back(std::make_pair(PredBB, PredVal));
169
170 // Compute SingularValue.
171 if (isFirstPred) {
172 SingularValue = PredVal;
173 isFirstPred = false;
174 } else if (PredVal != SingularValue)
175 SingularValue = 0;
176 }
177
178 // Otherwise, if all the merged values are the same, just use it.
179 if (SingularValue != 0)
180 return SingularValue;
181
Evan Chengfb1654d2009-12-07 23:11:03 +0000182 // If an identical PHI is already in BB, just reuse it.
183 unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
184 if (DupPHI)
185 return DupPHI;
186
Evan Cheng71453822009-12-03 02:31:43 +0000187 // Otherwise, we do need a PHI: insert one now.
Evan Chengc1610be2011-12-06 02:49:06 +0000188 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +0000189 MachineInstrBuilder InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
190 Loc, VRC, MRI, TII);
Evan Cheng71453822009-12-03 02:31:43 +0000191
192 // Fill in all the predecessors of the PHI.
Evan Cheng71453822009-12-03 02:31:43 +0000193 for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +0000194 InsertedPHI.addReg(PredValues[i].second).addMBB(PredValues[i].first);
Evan Cheng71453822009-12-03 02:31:43 +0000195
196 // See if the PHI node can be merged to a single value. This can happen in
197 // loop cases when we get a PHI of itself and one other value.
198 if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
199 InsertedPHI->eraseFromParent();
200 return ConstVal;
201 }
202
203 // If the client wants to know about all new instructions, tell it.
204 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
205
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000206 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
Evan Cheng71453822009-12-03 02:31:43 +0000207 return InsertedPHI->getOperand(0).getReg();
208}
209
210static
211MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
212 MachineOperand *U) {
213 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
214 if (&MI->getOperand(i) == U)
215 return MI->getOperand(i+1).getMBB();
216 }
217
218 llvm_unreachable("MachineOperand::getParent() failure?");
Evan Cheng20e9d032009-12-02 22:02:52 +0000219}
220
221/// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
222/// which use their value in the corresponding predecessor.
Evan Cheng71453822009-12-03 02:31:43 +0000223void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
224 MachineInstr *UseMI = U.getParent();
225 unsigned NewVR = 0;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000226 if (UseMI->isPHI()) {
Evan Cheng71453822009-12-03 02:31:43 +0000227 MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
Evan Chengfb1654d2009-12-07 23:11:03 +0000228 NewVR = GetValueAtEndOfBlockInternal(SourceBB);
Evan Chenge156f612009-12-04 00:09:05 +0000229 } else {
230 NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
Evan Chengb2c15292009-12-03 21:51:55 +0000231 }
Evan Chenge156f612009-12-04 00:09:05 +0000232
Evan Cheng71453822009-12-03 02:31:43 +0000233 U.setReg(NewVR);
234}
Evan Cheng20e9d032009-12-02 22:02:52 +0000235
Bob Wilsond1b38e32010-05-04 23:18:19 +0000236/// SSAUpdaterTraits<MachineSSAUpdater> - Traits for the SSAUpdaterImpl
237/// template, specialized for MachineSSAUpdater.
238namespace llvm {
Eugene Zelenko32a40562017-09-11 23:00:48 +0000239
Bob Wilsond1b38e32010-05-04 23:18:19 +0000240template<>
241class SSAUpdaterTraits<MachineSSAUpdater> {
242public:
Eugene Zelenko32a40562017-09-11 23:00:48 +0000243 using BlkT = MachineBasicBlock;
244 using ValT = unsigned;
245 using PhiT = MachineInstr;
246 using BlkSucc_iterator = MachineBasicBlock::succ_iterator;
Bob Wilsond1b38e32010-05-04 23:18:19 +0000247
Bob Wilsond1b38e32010-05-04 23:18:19 +0000248 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
249 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
250
Chandler Carruthc60fbe62012-06-20 08:39:30 +0000251 /// Iterator for PHI operands.
252 class PHI_iterator {
253 private:
254 MachineInstr *PHI;
255 unsigned idx;
Fangrui Songf78650a2018-07-30 19:41:25 +0000256
Chandler Carruthc60fbe62012-06-20 08:39:30 +0000257 public:
258 explicit PHI_iterator(MachineInstr *P) // begin iterator
259 : PHI(P), idx(1) {}
260 PHI_iterator(MachineInstr *P, bool) // end iterator
261 : PHI(P), idx(PHI->getNumOperands()) {}
262
Fangrui Songf78650a2018-07-30 19:41:25 +0000263 PHI_iterator &operator++() { idx += 2; return *this; }
Chandler Carruthc60fbe62012-06-20 08:39:30 +0000264 bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
265 bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
Eugene Zelenko32a40562017-09-11 23:00:48 +0000266
Chandler Carruthc60fbe62012-06-20 08:39:30 +0000267 unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); }
Eugene Zelenko32a40562017-09-11 23:00:48 +0000268
Chandler Carruthc60fbe62012-06-20 08:39:30 +0000269 MachineBasicBlock *getIncomingBlock() {
270 return PHI->getOperand(idx+1).getMBB();
271 }
272 };
Eugene Zelenko32a40562017-09-11 23:00:48 +0000273
Bob Wilsond1b38e32010-05-04 23:18:19 +0000274 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
Eugene Zelenko32a40562017-09-11 23:00:48 +0000275
Bob Wilsond1b38e32010-05-04 23:18:19 +0000276 static inline PHI_iterator PHI_end(PhiT *PHI) {
277 return PHI_iterator(PHI, true);
278 }
279
280 /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
281 /// vector.
282 static void FindPredecessorBlocks(MachineBasicBlock *BB,
283 SmallVectorImpl<MachineBasicBlock*> *Preds){
284 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
285 E = BB->pred_end(); PI != E; ++PI)
286 Preds->push_back(*PI);
287 }
288
289 /// GetUndefVal - Create an IMPLICIT_DEF instruction with a new register.
290 /// Add it into the specified block and return the register.
291 static unsigned GetUndefVal(MachineBasicBlock *BB,
292 MachineSSAUpdater *Updater) {
293 // Insert an implicit_def to represent an undef value.
294 MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
295 BB, BB->getFirstTerminator(),
296 Updater->VRC, Updater->MRI,
297 Updater->TII);
298 return NewDef->getOperand(0).getReg();
299 }
300
301 /// CreateEmptyPHI - Create a PHI instruction that defines a new register.
302 /// Add it into the specified block and return the register.
303 static unsigned CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds,
304 MachineSSAUpdater *Updater) {
Evan Chengc1610be2011-12-06 02:49:06 +0000305 MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
Bob Wilsond1b38e32010-05-04 23:18:19 +0000306 MachineInstr *PHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
307 Updater->VRC, Updater->MRI,
308 Updater->TII);
309 return PHI->getOperand(0).getReg();
310 }
311
312 /// AddPHIOperand - Add the specified value as an operand of the PHI for
313 /// the specified predecessor block.
314 static void AddPHIOperand(MachineInstr *PHI, unsigned Val,
315 MachineBasicBlock *Pred) {
Jakob Stoklund Olesenf623e982012-12-20 18:08:06 +0000316 MachineInstrBuilder(*Pred->getParent(), PHI).addReg(Val).addMBB(Pred);
Bob Wilsond1b38e32010-05-04 23:18:19 +0000317 }
318
319 /// InstrIsPHI - Check if an instruction is a PHI.
Bob Wilsond1b38e32010-05-04 23:18:19 +0000320 static MachineInstr *InstrIsPHI(MachineInstr *I) {
Bob Wilson01fcdaa2010-05-10 17:14:26 +0000321 if (I && I->isPHI())
Bob Wilsond1b38e32010-05-04 23:18:19 +0000322 return I;
Craig Topperc0196b12014-04-14 00:51:57 +0000323 return nullptr;
Bob Wilsond1b38e32010-05-04 23:18:19 +0000324 }
325
326 /// ValueIsPHI - Check if the instruction that defines the specified register
327 /// is a PHI instruction.
328 static MachineInstr *ValueIsPHI(unsigned Val, MachineSSAUpdater *Updater) {
329 return InstrIsPHI(Updater->MRI->getVRegDef(Val));
330 }
331
332 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
333 /// operands, i.e., it was just added.
334 static MachineInstr *ValueIsNewPHI(unsigned Val, MachineSSAUpdater *Updater) {
335 MachineInstr *PHI = ValueIsPHI(Val, Updater);
336 if (PHI && PHI->getNumOperands() <= 1)
337 return PHI;
Craig Topperc0196b12014-04-14 00:51:57 +0000338 return nullptr;
Bob Wilsond1b38e32010-05-04 23:18:19 +0000339 }
340
341 /// GetPHIValue - For the specified PHI instruction, return the register
342 /// that it defines.
343 static unsigned GetPHIValue(MachineInstr *PHI) {
344 return PHI->getOperand(0).getReg();
345 }
346};
347
Eugene Zelenko32a40562017-09-11 23:00:48 +0000348} // end namespace llvm
Bob Wilsond1b38e32010-05-04 23:18:19 +0000349
Evan Cheng20e9d032009-12-02 22:02:52 +0000350/// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
351/// for the specified BB and if so, return it. If not, construct SSA form by
Bob Wilsond561daf2010-04-26 17:40:49 +0000352/// first calculating the required placement of PHIs and then inserting new
353/// PHIs where needed.
Evan Cheng20e9d032009-12-02 22:02:52 +0000354unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
Evan Cheng71453822009-12-03 02:31:43 +0000355 AvailableValsTy &AvailableVals = getAvailableVals(AV);
Bob Wilsond561daf2010-04-26 17:40:49 +0000356 if (unsigned V = AvailableVals[BB])
357 return V;
Evan Cheng71453822009-12-03 02:31:43 +0000358
Bob Wilsond1b38e32010-05-04 23:18:19 +0000359 SSAUpdaterImpl<MachineSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
360 return Impl.GetValue(BB);
Evan Cheng20e9d032009-12-02 22:02:52 +0000361}