10b57cec5SDimitry Andric //===- MachineSSAUpdater.cpp - Unstructured SSA Update Tool ---------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the MachineSSAUpdater class. It's based on SSAUpdater
100b57cec5SDimitry Andric // class in lib/Transforms/Utils.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineSSAUpdater.h"
150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
270b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
280b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
300b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
310b57cec5SDimitry Andric #include <utility>
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #define DEBUG_TYPE "machine-ssaupdater"
360b57cec5SDimitry Andric
375ffd83dbSDimitry Andric using AvailableValsTy = DenseMap<MachineBasicBlock *, Register>;
380b57cec5SDimitry Andric
getAvailableVals(void * AV)390b57cec5SDimitry Andric static AvailableValsTy &getAvailableVals(void *AV) {
400b57cec5SDimitry Andric return *static_cast<AvailableValsTy*>(AV);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
MachineSSAUpdater(MachineFunction & MF,SmallVectorImpl<MachineInstr * > * NewPHI)430b57cec5SDimitry Andric MachineSSAUpdater::MachineSSAUpdater(MachineFunction &MF,
440b57cec5SDimitry Andric SmallVectorImpl<MachineInstr*> *NewPHI)
450b57cec5SDimitry Andric : InsertedPHIs(NewPHI), TII(MF.getSubtarget().getInstrInfo()),
460b57cec5SDimitry Andric MRI(&MF.getRegInfo()) {}
470b57cec5SDimitry Andric
~MachineSSAUpdater()480b57cec5SDimitry Andric MachineSSAUpdater::~MachineSSAUpdater() {
490b57cec5SDimitry Andric delete static_cast<AvailableValsTy*>(AV);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric /// Initialize - Reset this object to get ready for a new set of SSA
53af732203SDimitry Andric /// updates.
Initialize(const TargetRegisterClass * RC)54af732203SDimitry Andric void MachineSSAUpdater::Initialize(const TargetRegisterClass *RC) {
550b57cec5SDimitry Andric if (!AV)
560b57cec5SDimitry Andric AV = new AvailableValsTy();
570b57cec5SDimitry Andric else
580b57cec5SDimitry Andric getAvailableVals(AV).clear();
590b57cec5SDimitry Andric
60af732203SDimitry Andric VRC = RC;
61af732203SDimitry Andric }
62af732203SDimitry Andric
Initialize(Register V)63af732203SDimitry Andric void MachineSSAUpdater::Initialize(Register V) {
64af732203SDimitry Andric Initialize(MRI->getRegClass(V));
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric /// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for
680b57cec5SDimitry Andric /// the specified block.
HasValueForBlock(MachineBasicBlock * BB) const690b57cec5SDimitry Andric bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const {
700b57cec5SDimitry Andric return getAvailableVals(AV).count(BB);
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric /// AddAvailableValue - Indicate that a rewritten value is available in the
740b57cec5SDimitry Andric /// specified block with the specified value.
AddAvailableValue(MachineBasicBlock * BB,Register V)755ffd83dbSDimitry Andric void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, Register V) {
760b57cec5SDimitry Andric getAvailableVals(AV)[BB] = V;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
800b57cec5SDimitry Andric /// live at the end of the specified block.
GetValueAtEndOfBlock(MachineBasicBlock * BB)815ffd83dbSDimitry Andric Register MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
820b57cec5SDimitry Andric return GetValueAtEndOfBlockInternal(BB);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric static
LookForIdenticalPHI(MachineBasicBlock * BB,SmallVectorImpl<std::pair<MachineBasicBlock *,Register>> & PredValues)865ffd83dbSDimitry Andric Register LookForIdenticalPHI(MachineBasicBlock *BB,
875ffd83dbSDimitry Andric SmallVectorImpl<std::pair<MachineBasicBlock *, Register>> &PredValues) {
880b57cec5SDimitry Andric if (BB->empty())
895ffd83dbSDimitry Andric return Register();
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric MachineBasicBlock::iterator I = BB->begin();
920b57cec5SDimitry Andric if (!I->isPHI())
935ffd83dbSDimitry Andric return Register();
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric AvailableValsTy AVals;
960b57cec5SDimitry Andric for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
970b57cec5SDimitry Andric AVals[PredValues[i].first] = PredValues[i].second;
980b57cec5SDimitry Andric while (I != BB->end() && I->isPHI()) {
990b57cec5SDimitry Andric bool Same = true;
1000b57cec5SDimitry Andric for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
1018bcb0991SDimitry Andric Register SrcReg = I->getOperand(i).getReg();
1020b57cec5SDimitry Andric MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
1030b57cec5SDimitry Andric if (AVals[SrcBB] != SrcReg) {
1040b57cec5SDimitry Andric Same = false;
1050b57cec5SDimitry Andric break;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric if (Same)
1090b57cec5SDimitry Andric return I->getOperand(0).getReg();
1100b57cec5SDimitry Andric ++I;
1110b57cec5SDimitry Andric }
1125ffd83dbSDimitry Andric return Register();
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
1160b57cec5SDimitry Andric /// a value of the given register class at the start of the specified basic
1170b57cec5SDimitry Andric /// block. It returns the virtual register defined by the instruction.
1180b57cec5SDimitry Andric static
InsertNewDef(unsigned Opcode,MachineBasicBlock * BB,MachineBasicBlock::iterator I,const TargetRegisterClass * RC,MachineRegisterInfo * MRI,const TargetInstrInfo * TII)1190b57cec5SDimitry Andric MachineInstrBuilder InsertNewDef(unsigned Opcode,
1200b57cec5SDimitry Andric MachineBasicBlock *BB, MachineBasicBlock::iterator I,
1210b57cec5SDimitry Andric const TargetRegisterClass *RC,
1220b57cec5SDimitry Andric MachineRegisterInfo *MRI,
1230b57cec5SDimitry Andric const TargetInstrInfo *TII) {
1248bcb0991SDimitry Andric Register NewVR = MRI->createVirtualRegister(RC);
1250b57cec5SDimitry Andric return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR);
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
1290b57cec5SDimitry Andric /// is live in the middle of the specified block.
1300b57cec5SDimitry Andric ///
1310b57cec5SDimitry Andric /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
1320b57cec5SDimitry Andric /// important case: if there is a definition of the rewritten value after the
1330b57cec5SDimitry Andric /// 'use' in BB. Consider code like this:
1340b57cec5SDimitry Andric ///
1350b57cec5SDimitry Andric /// X1 = ...
1360b57cec5SDimitry Andric /// SomeBB:
1370b57cec5SDimitry Andric /// use(X)
1380b57cec5SDimitry Andric /// X2 = ...
1390b57cec5SDimitry Andric /// br Cond, SomeBB, OutBB
1400b57cec5SDimitry Andric ///
1410b57cec5SDimitry Andric /// In this case, there are two values (X1 and X2) added to the AvailableVals
1420b57cec5SDimitry Andric /// set by the client of the rewriter, and those values are both live out of
1430b57cec5SDimitry Andric /// their respective blocks. However, the use of X happens in the *middle* of
1440b57cec5SDimitry Andric /// a block. Because of this, we need to insert a new PHI node in SomeBB to
1450b57cec5SDimitry Andric /// merge the appropriate values, and this value isn't live out of the block.
GetValueInMiddleOfBlock(MachineBasicBlock * BB)1465ffd83dbSDimitry Andric Register MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
1470b57cec5SDimitry Andric // If there is no definition of the renamed variable in this block, just use
1480b57cec5SDimitry Andric // GetValueAtEndOfBlock to do our work.
1490b57cec5SDimitry Andric if (!HasValueForBlock(BB))
1500b57cec5SDimitry Andric return GetValueAtEndOfBlockInternal(BB);
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric // If there are no predecessors, just return undef.
1530b57cec5SDimitry Andric if (BB->pred_empty()) {
1540b57cec5SDimitry Andric // Insert an implicit_def to represent an undef value.
1550b57cec5SDimitry Andric MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
1560b57cec5SDimitry Andric BB, BB->getFirstTerminator(),
1570b57cec5SDimitry Andric VRC, MRI, TII);
1580b57cec5SDimitry Andric return NewDef->getOperand(0).getReg();
1590b57cec5SDimitry Andric }
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric // Otherwise, we have the hard case. Get the live-in values for each
1620b57cec5SDimitry Andric // predecessor.
1635ffd83dbSDimitry Andric SmallVector<std::pair<MachineBasicBlock*, Register>, 8> PredValues;
1645ffd83dbSDimitry Andric Register SingularValue;
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric bool isFirstPred = true;
167*5f7ddb14SDimitry Andric for (MachineBasicBlock *PredBB : BB->predecessors()) {
1685ffd83dbSDimitry Andric Register PredVal = GetValueAtEndOfBlockInternal(PredBB);
1690b57cec5SDimitry Andric PredValues.push_back(std::make_pair(PredBB, PredVal));
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric // Compute SingularValue.
1720b57cec5SDimitry Andric if (isFirstPred) {
1730b57cec5SDimitry Andric SingularValue = PredVal;
1740b57cec5SDimitry Andric isFirstPred = false;
1750b57cec5SDimitry Andric } else if (PredVal != SingularValue)
1765ffd83dbSDimitry Andric SingularValue = Register();
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric // Otherwise, if all the merged values are the same, just use it.
1805ffd83dbSDimitry Andric if (SingularValue)
1810b57cec5SDimitry Andric return SingularValue;
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric // If an identical PHI is already in BB, just reuse it.
1845ffd83dbSDimitry Andric Register DupPHI = LookForIdenticalPHI(BB, PredValues);
1850b57cec5SDimitry Andric if (DupPHI)
1860b57cec5SDimitry Andric return DupPHI;
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric // Otherwise, we do need a PHI: insert one now.
1890b57cec5SDimitry Andric MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
1900b57cec5SDimitry Andric MachineInstrBuilder InsertedPHI = InsertNewDef(TargetOpcode::PHI, BB,
1910b57cec5SDimitry Andric Loc, VRC, MRI, TII);
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric // Fill in all the predecessors of the PHI.
1940b57cec5SDimitry Andric for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
1950b57cec5SDimitry Andric InsertedPHI.addReg(PredValues[i].second).addMBB(PredValues[i].first);
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric // See if the PHI node can be merged to a single value. This can happen in
1980b57cec5SDimitry Andric // loop cases when we get a PHI of itself and one other value.
1990b57cec5SDimitry Andric if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
2000b57cec5SDimitry Andric InsertedPHI->eraseFromParent();
2010b57cec5SDimitry Andric return ConstVal;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric // If the client wants to know about all new instructions, tell it.
2050b57cec5SDimitry Andric if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
2085ffd83dbSDimitry Andric return InsertedPHI.getReg(0);
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric static
findCorrespondingPred(const MachineInstr * MI,MachineOperand * U)2120b57cec5SDimitry Andric MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI,
2130b57cec5SDimitry Andric MachineOperand *U) {
2140b57cec5SDimitry Andric for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
2150b57cec5SDimitry Andric if (&MI->getOperand(i) == U)
2160b57cec5SDimitry Andric return MI->getOperand(i+1).getMBB();
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric llvm_unreachable("MachineOperand::getParent() failure?");
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
2230b57cec5SDimitry Andric /// which use their value in the corresponding predecessor.
RewriteUse(MachineOperand & U)2240b57cec5SDimitry Andric void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
2250b57cec5SDimitry Andric MachineInstr *UseMI = U.getParent();
2265ffd83dbSDimitry Andric Register NewVR;
2270b57cec5SDimitry Andric if (UseMI->isPHI()) {
2280b57cec5SDimitry Andric MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
2290b57cec5SDimitry Andric NewVR = GetValueAtEndOfBlockInternal(SourceBB);
2300b57cec5SDimitry Andric } else {
2310b57cec5SDimitry Andric NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric U.setReg(NewVR);
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric namespace llvm {
2380b57cec5SDimitry Andric
239*5f7ddb14SDimitry Andric /// SSAUpdaterTraits<MachineSSAUpdater> - Traits for the SSAUpdaterImpl
240*5f7ddb14SDimitry Andric /// template, specialized for MachineSSAUpdater.
2410b57cec5SDimitry Andric template<>
2420b57cec5SDimitry Andric class SSAUpdaterTraits<MachineSSAUpdater> {
2430b57cec5SDimitry Andric public:
2440b57cec5SDimitry Andric using BlkT = MachineBasicBlock;
2455ffd83dbSDimitry Andric using ValT = Register;
2460b57cec5SDimitry Andric using PhiT = MachineInstr;
2470b57cec5SDimitry Andric using BlkSucc_iterator = MachineBasicBlock::succ_iterator;
2480b57cec5SDimitry Andric
BlkSucc_begin(BlkT * BB)2490b57cec5SDimitry Andric static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
BlkSucc_end(BlkT * BB)2500b57cec5SDimitry Andric static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric /// Iterator for PHI operands.
2530b57cec5SDimitry Andric class PHI_iterator {
2540b57cec5SDimitry Andric private:
2550b57cec5SDimitry Andric MachineInstr *PHI;
2560b57cec5SDimitry Andric unsigned idx;
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric public:
PHI_iterator(MachineInstr * P)2590b57cec5SDimitry Andric explicit PHI_iterator(MachineInstr *P) // begin iterator
2600b57cec5SDimitry Andric : PHI(P), idx(1) {}
PHI_iterator(MachineInstr * P,bool)2610b57cec5SDimitry Andric PHI_iterator(MachineInstr *P, bool) // end iterator
2620b57cec5SDimitry Andric : PHI(P), idx(PHI->getNumOperands()) {}
2630b57cec5SDimitry Andric
operator ++()2640b57cec5SDimitry Andric PHI_iterator &operator++() { idx += 2; return *this; }
operator ==(const PHI_iterator & x) const2650b57cec5SDimitry Andric bool operator==(const PHI_iterator& x) const { return idx == x.idx; }
operator !=(const PHI_iterator & x) const2660b57cec5SDimitry Andric bool operator!=(const PHI_iterator& x) const { return !operator==(x); }
2670b57cec5SDimitry Andric
getIncomingValue()2680b57cec5SDimitry Andric unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); }
2690b57cec5SDimitry Andric
getIncomingBlock()2700b57cec5SDimitry Andric MachineBasicBlock *getIncomingBlock() {
2710b57cec5SDimitry Andric return PHI->getOperand(idx+1).getMBB();
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric };
2740b57cec5SDimitry Andric
PHI_begin(PhiT * PHI)2750b57cec5SDimitry Andric static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
2760b57cec5SDimitry Andric
PHI_end(PhiT * PHI)2770b57cec5SDimitry Andric static inline PHI_iterator PHI_end(PhiT *PHI) {
2780b57cec5SDimitry Andric return PHI_iterator(PHI, true);
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
2820b57cec5SDimitry Andric /// vector.
FindPredecessorBlocks(MachineBasicBlock * BB,SmallVectorImpl<MachineBasicBlock * > * Preds)2830b57cec5SDimitry Andric static void FindPredecessorBlocks(MachineBasicBlock *BB,
2840b57cec5SDimitry Andric SmallVectorImpl<MachineBasicBlock*> *Preds){
285*5f7ddb14SDimitry Andric append_range(*Preds, BB->predecessors());
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric /// GetUndefVal - Create an IMPLICIT_DEF instruction with a new register.
2890b57cec5SDimitry Andric /// Add it into the specified block and return the register.
GetUndefVal(MachineBasicBlock * BB,MachineSSAUpdater * Updater)2905ffd83dbSDimitry Andric static Register GetUndefVal(MachineBasicBlock *BB,
2910b57cec5SDimitry Andric MachineSSAUpdater *Updater) {
2920b57cec5SDimitry Andric // Insert an implicit_def to represent an undef value.
2930b57cec5SDimitry Andric MachineInstr *NewDef = InsertNewDef(TargetOpcode::IMPLICIT_DEF,
2948bcb0991SDimitry Andric BB, BB->getFirstNonPHI(),
2950b57cec5SDimitry Andric Updater->VRC, Updater->MRI,
2960b57cec5SDimitry Andric Updater->TII);
2970b57cec5SDimitry Andric return NewDef->getOperand(0).getReg();
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andric /// CreateEmptyPHI - Create a PHI instruction that defines a new register.
3010b57cec5SDimitry Andric /// Add it into the specified block and return the register.
CreateEmptyPHI(MachineBasicBlock * BB,unsigned NumPreds,MachineSSAUpdater * Updater)3025ffd83dbSDimitry Andric static Register CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds,
3030b57cec5SDimitry Andric MachineSSAUpdater *Updater) {
3040b57cec5SDimitry Andric MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin();
3050b57cec5SDimitry Andric MachineInstr *PHI = InsertNewDef(TargetOpcode::PHI, BB, Loc,
3060b57cec5SDimitry Andric Updater->VRC, Updater->MRI,
3070b57cec5SDimitry Andric Updater->TII);
3080b57cec5SDimitry Andric return PHI->getOperand(0).getReg();
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric /// AddPHIOperand - Add the specified value as an operand of the PHI for
3120b57cec5SDimitry Andric /// the specified predecessor block.
AddPHIOperand(MachineInstr * PHI,Register Val,MachineBasicBlock * Pred)3135ffd83dbSDimitry Andric static void AddPHIOperand(MachineInstr *PHI, Register Val,
3140b57cec5SDimitry Andric MachineBasicBlock *Pred) {
3150b57cec5SDimitry Andric MachineInstrBuilder(*Pred->getParent(), PHI).addReg(Val).addMBB(Pred);
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric /// InstrIsPHI - Check if an instruction is a PHI.
InstrIsPHI(MachineInstr * I)3190b57cec5SDimitry Andric static MachineInstr *InstrIsPHI(MachineInstr *I) {
3200b57cec5SDimitry Andric if (I && I->isPHI())
3210b57cec5SDimitry Andric return I;
3220b57cec5SDimitry Andric return nullptr;
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric
3250b57cec5SDimitry Andric /// ValueIsPHI - Check if the instruction that defines the specified register
3260b57cec5SDimitry Andric /// is a PHI instruction.
ValueIsPHI(Register Val,MachineSSAUpdater * Updater)3275ffd83dbSDimitry Andric static MachineInstr *ValueIsPHI(Register Val, MachineSSAUpdater *Updater) {
3280b57cec5SDimitry Andric return InstrIsPHI(Updater->MRI->getVRegDef(Val));
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
3320b57cec5SDimitry Andric /// operands, i.e., it was just added.
ValueIsNewPHI(Register Val,MachineSSAUpdater * Updater)3335ffd83dbSDimitry Andric static MachineInstr *ValueIsNewPHI(Register Val, MachineSSAUpdater *Updater) {
3340b57cec5SDimitry Andric MachineInstr *PHI = ValueIsPHI(Val, Updater);
3350b57cec5SDimitry Andric if (PHI && PHI->getNumOperands() <= 1)
3360b57cec5SDimitry Andric return PHI;
3370b57cec5SDimitry Andric return nullptr;
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric
3400b57cec5SDimitry Andric /// GetPHIValue - For the specified PHI instruction, return the register
3410b57cec5SDimitry Andric /// that it defines.
GetPHIValue(MachineInstr * PHI)3425ffd83dbSDimitry Andric static Register GetPHIValue(MachineInstr *PHI) {
3430b57cec5SDimitry Andric return PHI->getOperand(0).getReg();
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric };
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric } // end namespace llvm
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
3500b57cec5SDimitry Andric /// for the specified BB and if so, return it. If not, construct SSA form by
3510b57cec5SDimitry Andric /// first calculating the required placement of PHIs and then inserting new
3520b57cec5SDimitry Andric /// PHIs where needed.
GetValueAtEndOfBlockInternal(MachineBasicBlock * BB)3535ffd83dbSDimitry Andric Register MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
3540b57cec5SDimitry Andric AvailableValsTy &AvailableVals = getAvailableVals(AV);
3555ffd83dbSDimitry Andric if (Register V = AvailableVals[BB])
3560b57cec5SDimitry Andric return V;
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric SSAUpdaterImpl<MachineSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs);
3590b57cec5SDimitry Andric return Impl.GetValue(BB);
3600b57cec5SDimitry Andric }
361