1*0b57cec5SDimitry Andric //===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the MachineSSAUpdater class. It's based on SSAUpdater
10*0b57cec5SDimitry Andric // class in lib/Transforms/Utils.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineSSAUpdater.h"
15*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
17*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
18*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
19*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
23*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
24*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
26*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
27*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
28*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
29*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
30*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
31*0b57cec5SDimitry Andric #include <utility>
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric using namespace llvm;
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric #define DEBUG_TYPE "machine-ssaupdater"
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric using AvailableValsTy = DenseMap<MachineBasicBlock *, Register>;
38*0b57cec5SDimitry Andric 
getAvailableVals(void * AV)39*0b57cec5SDimitry Andric static AvailableValsTy &getAvailableVals(void *AV) {
40*0b57cec5SDimitry Andric   return *static_cast<AvailableValsTy*>(AV);
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric 
MachineSSAUpdater(MachineFunction & MF,SmallVectorImpl<MachineInstr * > * NewPHI)43*0b57cec5SDimitry Andric MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
44*0b57cec5SDimitry Andric                                      SmallVectorImpl<MachineInstr*> *NewPHI)
45*0b57cec5SDimitry Andric   : InsertedPHIs(NewPHI), TII(MF.getSubtarget().getInstrInfo()),
46*0b57cec5SDimitry Andric     MRI(&MF.getRegInfo()) {}
47*0b57cec5SDimitry Andric 
~MachineSSAUpdater()48*0b57cec5SDimitry Andric MachineSSAUpdater::~MachineSSAUpdater() {
49*0b57cec5SDimitry Andric   delete static_cast<AvailableValsTy*>(AV);
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric /// Initialize - Reset this object to get ready for a new set of SSA
53*0b57cec5SDimitry Andric /// updates.
Initialize(const TargetRegisterClass * RC)54*0b57cec5SDimitry Andric void MachineSSAUpdater::Initialize(const TargetRegisterClass *RC) {
55*0b57cec5SDimitry Andric   if (!AV)
56*0b57cec5SDimitry Andric     AV = new AvailableValsTy();
57*0b57cec5SDimitry Andric   else
58*0b57cec5SDimitry Andric     getAvailableVals(AV).clear();
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric   VRC = RC;
61*0b57cec5SDimitry Andric }
62*0b57cec5SDimitry Andric 
Initialize(Register V)63*0b57cec5SDimitry Andric void MachineSSAUpdater::Initialize(Register V) {
64*0b57cec5SDimitry Andric   Initialize(MRI->getRegClass(V));
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric /// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
68*0b57cec5SDimitry Andric /// the specified block.
HasValueForBlock(MachineBasicBlock * BB) const69*0b57cec5SDimitry Andric bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
70*0b57cec5SDimitry Andric   return getAvailableVals(AV).count(BB);
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric 
73*0b57cec5SDimitry Andric /// AddAvailableValue - Indicate that a rewritten value is available in the
74*0b57cec5SDimitry Andric /// specified block with the specified value.
AddAvailableValue(MachineBasicBlock * BB,Register V)75*0b57cec5SDimitry Andric void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, Register V) {
76*0b57cec5SDimitry Andric   getAvailableVals(AV)[BB] = V;
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
80*0b57cec5SDimitry Andric /// live at the end of the specified block.
GetValueAtEndOfBlock(MachineBasicBlock * BB)81*0b57cec5SDimitry Andric Register MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
82*0b57cec5SDimitry Andric   return GetValueAtEndOfBlockInternal(BB);
83*0b57cec5SDimitry Andric }
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric static
LookForIdenticalPHI(MachineBasicBlock * BB,SmallVectorImpl<std::pair<MachineBasicBlock *,Register>> & PredValues)86*0b57cec5SDimitry Andric Register LookForIdenticalPHI(MachineBasicBlock *BB,
87*0b57cec5SDimitry Andric         SmallVectorImpl<std::pair<MachineBasicBlock *, Register>> &PredValues) {
88*0b57cec5SDimitry Andric   if (BB->empty())
89*0b57cec5SDimitry Andric     return Register();
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric   MachineBasicBlock::iterator I = BB->begin();
92*0b57cec5SDimitry Andric   if (!I->isPHI())
93*0b57cec5SDimitry Andric     return Register();
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   AvailableValsTy AVals;
96*0b57cec5SDimitry Andric   for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
97*0b57cec5SDimitry Andric     AVals[PredValues[i].first] = PredValues[i].second;
98*0b57cec5SDimitry Andric   while (I != BB->end() && I->isPHI()) {
99*0b57cec5SDimitry Andric     bool Same = true;
100*0b57cec5SDimitry Andric     for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
101*0b57cec5SDimitry Andric       Register SrcReg = I->getOperand(i).getReg();
102*0b57cec5SDimitry Andric       MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
103*0b57cec5SDimitry Andric       if (AVals[SrcBB] != SrcReg) {
104*0b57cec5SDimitry Andric         Same = false;
105*0b57cec5SDimitry Andric         break;
106*0b57cec5SDimitry Andric       }
107*0b57cec5SDimitry Andric     }
108*0b57cec5SDimitry Andric     if (Same)
109*0b57cec5SDimitry Andric       return I->getOperand(0).getReg();
110*0b57cec5SDimitry Andric     ++I;
111*0b57cec5SDimitry Andric   }
112*0b57cec5SDimitry Andric   return Register();
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
116*0b57cec5SDimitry Andric /// a value of the given register class at the start of the specified basic
117*0b57cec5SDimitry Andric /// block. It returns the virtual register defined by the instruction.
118*0b57cec5SDimitry Andric static
InsertNewDef(unsigned Opcode,MachineBasicBlock * BB,MachineBasicBlock::iterator I,const TargetRegisterClass * RC,MachineRegisterInfo * MRI,const TargetInstrInfo * TII)119*0b57cec5SDimitry Andric MachineInstrBuilder InsertNewDef(unsigned Opcode,
120*0b57cec5SDimitry Andric                            MachineBasicBlock *BB, MachineBasicBlock::iterator I,
121*0b57cec5SDimitry Andric                            const TargetRegisterClass *RC,
122*0b57cec5SDimitry Andric                            MachineRegisterInfo *MRI,
123*0b57cec5SDimitry Andric                            const TargetInstrInfo *TII) {
124*0b57cec5SDimitry Andric   Register NewVR = MRI->createVirtualRegister(RC);
125*0b57cec5SDimitry Andric   return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
126*0b57cec5SDimitry Andric }
127*0b57cec5SDimitry Andric 
128*0b57cec5SDimitry Andric /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
129*0b57cec5SDimitry Andric /// is live in the middle of the specified block. If ExistingValueOnly is
130*0b57cec5SDimitry Andric /// true then this will only return an existing value or $noreg; otherwise new
131*0b57cec5SDimitry Andric /// instructions may be inserted to materialize a value.
132*0b57cec5SDimitry Andric ///
133*0b57cec5SDimitry Andric /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
134*0b57cec5SDimitry Andric /// important case: if there is a definition of the rewritten value after the
135*0b57cec5SDimitry Andric /// 'use' in BB.  Consider code like this:
136*0b57cec5SDimitry Andric ///
137*0b57cec5SDimitry Andric ///      X1 = ...
138*0b57cec5SDimitry Andric ///   SomeBB:
139*0b57cec5SDimitry Andric ///      use(X)
140*0b57cec5SDimitry Andric ///      X2 = ...
141*0b57cec5SDimitry Andric ///      br Cond, SomeBB, OutBB
142*0b57cec5SDimitry Andric ///
143*0b57cec5SDimitry Andric /// In this case, there are two values (X1 and X2) added to the AvailableVals
144*0b57cec5SDimitry Andric /// set by the client of the rewriter, and those values are both live out of
145*0b57cec5SDimitry Andric /// their respective blocks.  However, the use of X happens in the *middle* of
146*0b57cec5SDimitry Andric /// a block.  Because of this, we need to insert a new PHI node in SomeBB to
147*0b57cec5SDimitry Andric /// merge the appropriate values, and this value isn't live out of the block.
GetValueInMiddleOfBlock(MachineBasicBlock * BB,bool ExistingValueOnly)148*0b57cec5SDimitry Andric Register MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB,
149*0b57cec5SDimitry Andric                                                     bool ExistingValueOnly) {
150*0b57cec5SDimitry Andric   // If there is no definition of the renamed variable in this block, just use
151*0b57cec5SDimitry Andric   // GetValueAtEndOfBlock to do our work.
152*0b57cec5SDimitry Andric   if (!HasValueForBlock(BB))
153*0b57cec5SDimitry Andric     return GetValueAtEndOfBlockInternal(BB, ExistingValueOnly);
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric   // If there are no predecessors, just return undef.
156*0b57cec5SDimitry Andric   if (BB->pred_empty()) {
157*0b57cec5SDimitry Andric     // If we cannot insert new instructions, just return $noreg.
158*0b57cec5SDimitry Andric     if (ExistingValueOnly)
159*0b57cec5SDimitry Andric       return Register();
160*0b57cec5SDimitry Andric     // Insert an implicit_def to represent an undef value.
161*0b57cec5SDimitry Andric     MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
162*0b57cec5SDimitry Andric                                         BB, BB->getFirstTerminator(),
163*0b57cec5SDimitry Andric                                         VRC, MRI, TII);
164*0b57cec5SDimitry Andric     return NewDef->getOperand(0).getReg();
165*0b57cec5SDimitry Andric   }
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric   // Otherwise, we have the hard case.  Get the live-in values for each
168*0b57cec5SDimitry Andric   // predecessor.
169*0b57cec5SDimitry Andric   SmallVector<std::pair<MachineBasicBlock*, Register>, 8> PredValues;
170*0b57cec5SDimitry Andric   Register SingularValue;
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric   bool isFirstPred = true;
173*0b57cec5SDimitry Andric   for (MachineBasicBlock *PredBB : BB->predecessors()) {
174*0b57cec5SDimitry Andric     Register PredVal = GetValueAtEndOfBlockInternal(PredBB, ExistingValueOnly);
175*0b57cec5SDimitry Andric     PredValues.push_back(std::make_pair(PredBB, PredVal));
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric     // Compute SingularValue.
178*0b57cec5SDimitry Andric     if (isFirstPred) {
179*0b57cec5SDimitry Andric       SingularValue = PredVal;
180*0b57cec5SDimitry Andric       isFirstPred = false;
181*0b57cec5SDimitry Andric     } else if (PredVal != SingularValue)
182*0b57cec5SDimitry Andric       SingularValue = Register();
183*0b57cec5SDimitry Andric   }
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric   // Otherwise, if all the merged values are the same, just use it.
186*0b57cec5SDimitry Andric   if (SingularValue)
187*0b57cec5SDimitry Andric     return SingularValue;
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric   // If an identical PHI is already in BB, just reuse it.
190*0b57cec5SDimitry Andric   Register DupPHI = LookForIdenticalPHI(BB, PredValues);
191*0b57cec5SDimitry Andric   if (DupPHI)
192*0b57cec5SDimitry Andric     return DupPHI;
193*0b57cec5SDimitry Andric 
194*0b57cec5SDimitry Andric   // If we cannot create new instructions, return $noreg now.
195*0b57cec5SDimitry Andric   if (ExistingValueOnly)
196*0b57cec5SDimitry Andric     return Register();
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric   // Otherwise, we do need a PHI: insert one now.
199*0b57cec5SDimitry Andric   MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
200*0b57cec5SDimitry Andric   MachineInstrBuilder InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
201*0b57cec5SDimitry Andric                                                  Loc, VRC, MRI, TII);
202*0b57cec5SDimitry Andric 
203*0b57cec5SDimitry Andric   // Fill in all the predecessors of the PHI.
204*0b57cec5SDimitry Andric   for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
205*0b57cec5SDimitry Andric     InsertedPHI.addReg(PredValues[i].second).addMBB(PredValues[i].first);
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric   // See if the PHI node can be merged to a single value.  This can happen in
208*0b57cec5SDimitry Andric   // loop cases when we get a PHI of itself and one other value.
209*0b57cec5SDimitry Andric   if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
210*0b57cec5SDimitry Andric     InsertedPHI->eraseFromParent();
211*0b57cec5SDimitry Andric     return ConstVal;
212*0b57cec5SDimitry Andric   }
213*0b57cec5SDimitry Andric 
214*0b57cec5SDimitry Andric   // If the client wants to know about all new instructions, tell it.
215*0b57cec5SDimitry Andric   if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
216*0b57cec5SDimitry Andric 
217*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "  Inserted PHI: " << *InsertedPHI << "\n");
218*0b57cec5SDimitry Andric   return InsertedPHI.getReg(0);
219*0b57cec5SDimitry Andric }
220*0b57cec5SDimitry Andric 
221*0b57cec5SDimitry Andric static
findCorrespondingPred(const MachineInstr * MI,MachineOperand * U)222*0b57cec5SDimitry Andric MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
223*0b57cec5SDimitry Andric                                          MachineOperand *U) {
224*0b57cec5SDimitry Andric   for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
225*0b57cec5SDimitry Andric     if (&MI->getOperand(i) == U)
226*0b57cec5SDimitry Andric       return MI->getOperand(i+1).getMBB();
227*0b57cec5SDimitry Andric   }
228*0b57cec5SDimitry Andric 
229*0b57cec5SDimitry Andric   llvm_unreachable("MachineOperand::getParent() failure?");
230*0b57cec5SDimitry Andric }
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric /// RewriteUse - Rewrite a use of the symbolic value.  This handles PHI nodes,
233*0b57cec5SDimitry Andric /// which use their value in the corresponding predecessor.
RewriteUse(MachineOperand & U)234*0b57cec5SDimitry Andric void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
235*0b57cec5SDimitry Andric   MachineInstr *UseMI = U.getParent();
236*0b57cec5SDimitry Andric   Register NewVR;
237*0b57cec5SDimitry Andric   if (UseMI->isPHI()) {
238*0b57cec5SDimitry Andric     MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
239*0b57cec5SDimitry Andric     NewVR = GetValueAtEndOfBlockInternal(SourceBB);
240*0b57cec5SDimitry Andric   } else {
241*0b57cec5SDimitry Andric     NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
242*0b57cec5SDimitry Andric   }
243*0b57cec5SDimitry Andric 
244*0b57cec5SDimitry Andric   U.setReg(NewVR);
245*0b57cec5SDimitry Andric }
246*0b57cec5SDimitry Andric 
247*0b57cec5SDimitry Andric namespace llvm {
248*0b57cec5SDimitry Andric 
249*0b57cec5SDimitry Andric /// SSAUpdaterTraits<MachineSSAUpdater> - Traits for the SSAUpdaterImpl
250*0b57cec5SDimitry Andric /// template, specialized for MachineSSAUpdater.
251*0b57cec5SDimitry Andric template<>
252*0b57cec5SDimitry Andric class SSAUpdaterTraits<MachineSSAUpdater> {
253*0b57cec5SDimitry Andric public:
254*0b57cec5SDimitry Andric   using BlkT = MachineBasicBlock;
255*0b57cec5SDimitry Andric   using ValT = Register;
256*0b57cec5SDimitry Andric   using PhiT = MachineInstr;
257*0b57cec5SDimitry Andric   using BlkSucc_iterator = MachineBasicBlock::succ_iterator;
258*0b57cec5SDimitry Andric 
BlkSucc_begin(BlkT * BB)259*0b57cec5SDimitry Andric   static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
BlkSucc_end(BlkT * BB)260*0b57cec5SDimitry Andric   static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
261*0b57cec5SDimitry Andric 
262*0b57cec5SDimitry Andric   /// Iterator for PHI operands.
263*0b57cec5SDimitry Andric   class PHI_iterator {
264*0b57cec5SDimitry Andric   private:
265*0b57cec5SDimitry Andric     MachineInstr *PHI;
266*0b57cec5SDimitry Andric     unsigned idx;
267*0b57cec5SDimitry Andric 
268*0b57cec5SDimitry Andric   public:
PHI_iterator(MachineInstr * P)269*0b57cec5SDimitry Andric     explicit PHI_iterator(MachineInstr *P) // begin iterator
270*0b57cec5SDimitry Andric       : PHI(P), idx(1) {}
PHI_iterator(MachineInstr * P,bool)271*0b57cec5SDimitry Andric     PHI_iterator(MachineInstr *P, bool) // end iterator
272*0b57cec5SDimitry Andric       : PHI(P), idx(PHI->getNumOperands()) {}
273*0b57cec5SDimitry Andric 
operator ++()274*0b57cec5SDimitry Andric     PHI_iterator &operator++() { idx += 2; return *this; }
operator ==(const PHI_iterator & x) const275*0b57cec5SDimitry Andric     bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
operator !=(const PHI_iterator & x) const276*0b57cec5SDimitry Andric     bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
277*0b57cec5SDimitry Andric 
getIncomingValue()278*0b57cec5SDimitry Andric     unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); }
279*0b57cec5SDimitry Andric 
getIncomingBlock()280*0b57cec5SDimitry Andric     MachineBasicBlock *getIncomingBlock() {
281*0b57cec5SDimitry Andric       return PHI->getOperand(idx+1).getMBB();
282*0b57cec5SDimitry Andric     }
283*0b57cec5SDimitry Andric   };
284*0b57cec5SDimitry Andric 
PHI_begin(PhiT * PHI)285*0b57cec5SDimitry Andric   static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
286*0b57cec5SDimitry Andric 
PHI_end(PhiT * PHI)287*0b57cec5SDimitry Andric   static inline PHI_iterator PHI_end(PhiT *PHI) {
288*0b57cec5SDimitry Andric     return PHI_iterator(PHI, true);
289*0b57cec5SDimitry Andric   }
290*0b57cec5SDimitry Andric 
291*0b57cec5SDimitry Andric   /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
292*0b57cec5SDimitry Andric   /// vector.
FindPredecessorBlocks(MachineBasicBlock * BB,SmallVectorImpl<MachineBasicBlock * > * Preds)293*0b57cec5SDimitry Andric   static void FindPredecessorBlocks(MachineBasicBlock *BB,
294*0b57cec5SDimitry Andric                                     SmallVectorImpl<MachineBasicBlock*> *Preds){
295*0b57cec5SDimitry Andric     append_range(*Preds, BB->predecessors());
296*0b57cec5SDimitry Andric   }
297*0b57cec5SDimitry Andric 
298*0b57cec5SDimitry Andric   /// GetUndefVal - Create an IMPLICIT_DEF instruction with a new register.
299*0b57cec5SDimitry Andric   /// Add it into the specified block and return the register.
GetUndefVal(MachineBasicBlock * BB,MachineSSAUpdater * Updater)300*0b57cec5SDimitry Andric   static Register GetUndefVal(MachineBasicBlock *BB,
301*0b57cec5SDimitry Andric                               MachineSSAUpdater *Updater) {
302*0b57cec5SDimitry Andric     // Insert an implicit_def to represent an undef value.
303*0b57cec5SDimitry Andric     MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
304*0b57cec5SDimitry Andric                                         BB, BB->getFirstNonPHI(),
305*0b57cec5SDimitry Andric                                         Updater->VRC, Updater->MRI,
306*0b57cec5SDimitry Andric                                         Updater->TII);
307*0b57cec5SDimitry Andric     return NewDef->getOperand(0).getReg();
308*0b57cec5SDimitry Andric   }
309*0b57cec5SDimitry Andric 
310*0b57cec5SDimitry Andric   /// CreateEmptyPHI - Create a PHI instruction that defines a new register.
311*0b57cec5SDimitry Andric   /// Add it into the specified block and return the register.
CreateEmptyPHI(MachineBasicBlock * BB,unsigned NumPreds,MachineSSAUpdater * Updater)312*0b57cec5SDimitry Andric   static Register CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds,
313*0b57cec5SDimitry Andric                                  MachineSSAUpdater *Updater) {
314*0b57cec5SDimitry Andric     MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
315*0b57cec5SDimitry Andric     MachineInstr *PHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
316*0b57cec5SDimitry Andric                                      Updater->VRC, Updater->MRI,
317*0b57cec5SDimitry Andric                                      Updater->TII);
318*0b57cec5SDimitry Andric     return PHI->getOperand(0).getReg();
319*0b57cec5SDimitry Andric   }
320*0b57cec5SDimitry Andric 
321*0b57cec5SDimitry Andric   /// AddPHIOperand - Add the specified value as an operand of the PHI for
322*0b57cec5SDimitry Andric   /// the specified predecessor block.
AddPHIOperand(MachineInstr * PHI,Register Val,MachineBasicBlock * Pred)323*0b57cec5SDimitry Andric   static void AddPHIOperand(MachineInstr *PHI, Register Val,
324*0b57cec5SDimitry Andric                             MachineBasicBlock *Pred) {
325*0b57cec5SDimitry Andric     MachineInstrBuilder(*Pred->getParent(), PHI).addReg(Val).addMBB(Pred);
326*0b57cec5SDimitry Andric   }
327*0b57cec5SDimitry Andric 
328*0b57cec5SDimitry Andric   /// InstrIsPHI - Check if an instruction is a PHI.
InstrIsPHI(MachineInstr * I)329*0b57cec5SDimitry Andric   static MachineInstr *InstrIsPHI(MachineInstr *I) {
330*0b57cec5SDimitry Andric     if (I && I->isPHI())
331*0b57cec5SDimitry Andric       return I;
332*0b57cec5SDimitry Andric     return nullptr;
333*0b57cec5SDimitry Andric   }
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric   /// ValueIsPHI - Check if the instruction that defines the specified register
336*0b57cec5SDimitry Andric   /// is a PHI instruction.
ValueIsPHI(Register Val,MachineSSAUpdater * Updater)337*0b57cec5SDimitry Andric   static MachineInstr *ValueIsPHI(Register Val, MachineSSAUpdater *Updater) {
338*0b57cec5SDimitry Andric     return InstrIsPHI(Updater->MRI->getVRegDef(Val));
339*0b57cec5SDimitry Andric   }
340*0b57cec5SDimitry Andric 
341*0b57cec5SDimitry Andric   /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
342*0b57cec5SDimitry Andric   /// operands, i.e., it was just added.
ValueIsNewPHI(Register Val,MachineSSAUpdater * Updater)343*0b57cec5SDimitry Andric   static MachineInstr *ValueIsNewPHI(Register Val, MachineSSAUpdater *Updater) {
344*0b57cec5SDimitry Andric     MachineInstr *PHI = ValueIsPHI(Val, Updater);
345*0b57cec5SDimitry Andric     if (PHI && PHI->getNumOperands() <= 1)
346*0b57cec5SDimitry Andric       return PHI;
347*0b57cec5SDimitry Andric     return nullptr;
348*0b57cec5SDimitry Andric   }
349*0b57cec5SDimitry Andric 
350*0b57cec5SDimitry Andric   /// GetPHIValue - For the specified PHI instruction, return the register
351*0b57cec5SDimitry Andric   /// that it defines.
GetPHIValue(MachineInstr * PHI)352*0b57cec5SDimitry Andric   static Register GetPHIValue(MachineInstr *PHI) {
353*0b57cec5SDimitry Andric     return PHI->getOperand(0).getReg();
354*0b57cec5SDimitry Andric   }
355*0b57cec5SDimitry Andric };
356*0b57cec5SDimitry Andric 
357*0b57cec5SDimitry Andric } // end namespace llvm
358*0b57cec5SDimitry Andric 
359*0b57cec5SDimitry Andric /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
360*0b57cec5SDimitry Andric /// for the specified BB and if so, return it.  If not, construct SSA form by
361*0b57cec5SDimitry Andric /// first calculating the required placement of PHIs and then inserting new
362 /// PHIs where needed.
363 Register
GetValueAtEndOfBlockInternal(MachineBasicBlock * BB,bool ExistingValueOnly)364 MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB,
365                                                 bool ExistingValueOnly) {
366   AvailableValsTy &AvailableVals = getAvailableVals(AV);
367   Register ExistingVal = AvailableVals.lookup(BB);
368   if (ExistingVal || ExistingValueOnly)
369     return ExistingVal;
370 
371   SSAUpdaterImpl<MachineSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
372   return Impl.GetValue(BB);
373 }
374