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