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 390b57cec5SDimitry Andric static AvailableValsTy &getAvailableVals(void *AV) { 400b57cec5SDimitry Andric return *static_cast<AvailableValsTy*>(AV); 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric 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 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 53e8d8bef9SDimitry Andric /// updates. 54*0fca6ea1SDimitry Andric void MachineSSAUpdater::Initialize(Register V) { 550b57cec5SDimitry Andric if (!AV) 560b57cec5SDimitry Andric AV = new AvailableValsTy(); 570b57cec5SDimitry Andric else 580b57cec5SDimitry Andric getAvailableVals(AV).clear(); 590b57cec5SDimitry Andric 60*0fca6ea1SDimitry Andric RegAttrs = MRI->getVRegAttrs(V); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric /// HasValueForBlock - Return true if the MachineSSAUpdater already has a value for 640b57cec5SDimitry Andric /// the specified block. 650b57cec5SDimitry Andric bool MachineSSAUpdater::HasValueForBlock(MachineBasicBlock *BB) const { 660b57cec5SDimitry Andric return getAvailableVals(AV).count(BB); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric /// AddAvailableValue - Indicate that a rewritten value is available in the 700b57cec5SDimitry Andric /// specified block with the specified value. 715ffd83dbSDimitry Andric void MachineSSAUpdater::AddAvailableValue(MachineBasicBlock *BB, Register V) { 720b57cec5SDimitry Andric getAvailableVals(AV)[BB] = V; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is 760b57cec5SDimitry Andric /// live at the end of the specified block. 775ffd83dbSDimitry Andric Register MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) { 780b57cec5SDimitry Andric return GetValueAtEndOfBlockInternal(BB); 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric static 825ffd83dbSDimitry Andric Register LookForIdenticalPHI(MachineBasicBlock *BB, 835ffd83dbSDimitry Andric SmallVectorImpl<std::pair<MachineBasicBlock *, Register>> &PredValues) { 840b57cec5SDimitry Andric if (BB->empty()) 855ffd83dbSDimitry Andric return Register(); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric MachineBasicBlock::iterator I = BB->begin(); 880b57cec5SDimitry Andric if (!I->isPHI()) 895ffd83dbSDimitry Andric return Register(); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric AvailableValsTy AVals; 92*0fca6ea1SDimitry Andric for (const auto &[SrcBB, SrcReg] : PredValues) 93*0fca6ea1SDimitry Andric AVals[SrcBB] = SrcReg; 940b57cec5SDimitry Andric while (I != BB->end() && I->isPHI()) { 950b57cec5SDimitry Andric bool Same = true; 960b57cec5SDimitry Andric for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) { 978bcb0991SDimitry Andric Register SrcReg = I->getOperand(i).getReg(); 980b57cec5SDimitry Andric MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB(); 990b57cec5SDimitry Andric if (AVals[SrcBB] != SrcReg) { 1000b57cec5SDimitry Andric Same = false; 1010b57cec5SDimitry Andric break; 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric if (Same) 1050b57cec5SDimitry Andric return I->getOperand(0).getReg(); 1060b57cec5SDimitry Andric ++I; 1070b57cec5SDimitry Andric } 1085ffd83dbSDimitry Andric return Register(); 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define 1120b57cec5SDimitry Andric /// a value of the given register class at the start of the specified basic 1130b57cec5SDimitry Andric /// block. It returns the virtual register defined by the instruction. 114*0fca6ea1SDimitry Andric static MachineInstrBuilder InsertNewDef(unsigned Opcode, MachineBasicBlock *BB, 115*0fca6ea1SDimitry Andric MachineBasicBlock::iterator I, 116*0fca6ea1SDimitry Andric MachineRegisterInfo::VRegAttrs RegAttrs, 1170b57cec5SDimitry Andric MachineRegisterInfo *MRI, 1180b57cec5SDimitry Andric const TargetInstrInfo *TII) { 119*0fca6ea1SDimitry Andric Register NewVR = MRI->createVirtualRegister(RegAttrs); 1200b57cec5SDimitry Andric return BuildMI(*BB, I, DebugLoc(), TII->get(Opcode), NewVR); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that 1240eae32dcSDimitry Andric /// is live in the middle of the specified block. If ExistingValueOnly is 1250eae32dcSDimitry Andric /// true then this will only return an existing value or $noreg; otherwise new 1260eae32dcSDimitry Andric /// instructions may be inserted to materialize a value. 1270b57cec5SDimitry Andric /// 1280b57cec5SDimitry Andric /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one 1290b57cec5SDimitry Andric /// important case: if there is a definition of the rewritten value after the 1300b57cec5SDimitry Andric /// 'use' in BB. Consider code like this: 1310b57cec5SDimitry Andric /// 1320b57cec5SDimitry Andric /// X1 = ... 1330b57cec5SDimitry Andric /// SomeBB: 1340b57cec5SDimitry Andric /// use(X) 1350b57cec5SDimitry Andric /// X2 = ... 1360b57cec5SDimitry Andric /// br Cond, SomeBB, OutBB 1370b57cec5SDimitry Andric /// 1380b57cec5SDimitry Andric /// In this case, there are two values (X1 and X2) added to the AvailableVals 1390b57cec5SDimitry Andric /// set by the client of the rewriter, and those values are both live out of 1400b57cec5SDimitry Andric /// their respective blocks. However, the use of X happens in the *middle* of 1410b57cec5SDimitry Andric /// a block. Because of this, we need to insert a new PHI node in SomeBB to 1420b57cec5SDimitry Andric /// merge the appropriate values, and this value isn't live out of the block. 1430eae32dcSDimitry Andric Register MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB, 1440eae32dcSDimitry Andric bool ExistingValueOnly) { 1450b57cec5SDimitry Andric // If there is no definition of the renamed variable in this block, just use 1460b57cec5SDimitry Andric // GetValueAtEndOfBlock to do our work. 1470b57cec5SDimitry Andric if (!HasValueForBlock(BB)) 1480eae32dcSDimitry Andric return GetValueAtEndOfBlockInternal(BB, ExistingValueOnly); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // If there are no predecessors, just return undef. 1510b57cec5SDimitry Andric if (BB->pred_empty()) { 1520eae32dcSDimitry Andric // If we cannot insert new instructions, just return $noreg. 1530eae32dcSDimitry Andric if (ExistingValueOnly) 1540eae32dcSDimitry Andric return Register(); 1550b57cec5SDimitry Andric // Insert an implicit_def to represent an undef value. 156*0fca6ea1SDimitry Andric MachineInstr *NewDef = 157*0fca6ea1SDimitry Andric InsertNewDef(TargetOpcode::IMPLICIT_DEF, BB, BB->getFirstTerminator(), 158*0fca6ea1SDimitry Andric RegAttrs, MRI, TII); 1590b57cec5SDimitry Andric return NewDef->getOperand(0).getReg(); 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric // Otherwise, we have the hard case. Get the live-in values for each 1630b57cec5SDimitry Andric // predecessor. 1645ffd83dbSDimitry Andric SmallVector<std::pair<MachineBasicBlock*, Register>, 8> PredValues; 1655ffd83dbSDimitry Andric Register SingularValue; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric bool isFirstPred = true; 168fe6060f1SDimitry Andric for (MachineBasicBlock *PredBB : BB->predecessors()) { 1690eae32dcSDimitry Andric Register PredVal = GetValueAtEndOfBlockInternal(PredBB, ExistingValueOnly); 1700b57cec5SDimitry Andric PredValues.push_back(std::make_pair(PredBB, PredVal)); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Compute SingularValue. 1730b57cec5SDimitry Andric if (isFirstPred) { 1740b57cec5SDimitry Andric SingularValue = PredVal; 1750b57cec5SDimitry Andric isFirstPred = false; 1760b57cec5SDimitry Andric } else if (PredVal != SingularValue) 1775ffd83dbSDimitry Andric SingularValue = Register(); 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // Otherwise, if all the merged values are the same, just use it. 1815ffd83dbSDimitry Andric if (SingularValue) 1820b57cec5SDimitry Andric return SingularValue; 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric // If an identical PHI is already in BB, just reuse it. 1855ffd83dbSDimitry Andric Register DupPHI = LookForIdenticalPHI(BB, PredValues); 1860b57cec5SDimitry Andric if (DupPHI) 1870b57cec5SDimitry Andric return DupPHI; 1880b57cec5SDimitry Andric 1890eae32dcSDimitry Andric // If we cannot create new instructions, return $noreg now. 1900eae32dcSDimitry Andric if (ExistingValueOnly) 1910eae32dcSDimitry Andric return Register(); 1920eae32dcSDimitry Andric 1930b57cec5SDimitry Andric // Otherwise, we do need a PHI: insert one now. 1940b57cec5SDimitry Andric MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin(); 195*0fca6ea1SDimitry Andric MachineInstrBuilder InsertedPHI = 196*0fca6ea1SDimitry Andric InsertNewDef(TargetOpcode::PHI, BB, Loc, RegAttrs, MRI, TII); 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // Fill in all the predecessors of the PHI. 199*0fca6ea1SDimitry Andric for (const auto &[SrcBB, SrcReg] : PredValues) 200*0fca6ea1SDimitry Andric InsertedPHI.addReg(SrcReg).addMBB(SrcBB); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // See if the PHI node can be merged to a single value. This can happen in 2030b57cec5SDimitry Andric // loop cases when we get a PHI of itself and one other value. 2040b57cec5SDimitry Andric if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) { 2050b57cec5SDimitry Andric InsertedPHI->eraseFromParent(); 2060b57cec5SDimitry Andric return ConstVal; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // If the client wants to know about all new instructions, tell it. 2100b57cec5SDimitry Andric if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI); 2110b57cec5SDimitry Andric 212*0fca6ea1SDimitry Andric LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI); 2135ffd83dbSDimitry Andric return InsertedPHI.getReg(0); 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric static 2170b57cec5SDimitry Andric MachineBasicBlock *findCorrespondingPred(const MachineInstr *MI, 2180b57cec5SDimitry Andric MachineOperand *U) { 2190b57cec5SDimitry Andric for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) { 2200b57cec5SDimitry Andric if (&MI->getOperand(i) == U) 2210b57cec5SDimitry Andric return MI->getOperand(i+1).getMBB(); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric llvm_unreachable("MachineOperand::getParent() failure?"); 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes, 2280b57cec5SDimitry Andric /// which use their value in the corresponding predecessor. 2290b57cec5SDimitry Andric void MachineSSAUpdater::RewriteUse(MachineOperand &U) { 2300b57cec5SDimitry Andric MachineInstr *UseMI = U.getParent(); 2315ffd83dbSDimitry Andric Register NewVR; 2320b57cec5SDimitry Andric if (UseMI->isPHI()) { 2330b57cec5SDimitry Andric MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U); 2340b57cec5SDimitry Andric NewVR = GetValueAtEndOfBlockInternal(SourceBB); 2350b57cec5SDimitry Andric } else { 2360b57cec5SDimitry Andric NewVR = GetValueInMiddleOfBlock(UseMI->getParent()); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 239*0fca6ea1SDimitry Andric // Insert a COPY if needed to satisfy register class constraints for the using 240*0fca6ea1SDimitry Andric // MO. Or, if possible, just constrain the class for NewVR to avoid the need 241*0fca6ea1SDimitry Andric // for a COPY. 242*0fca6ea1SDimitry Andric if (NewVR) { 243*0fca6ea1SDimitry Andric const TargetRegisterClass *UseRC = 244*0fca6ea1SDimitry Andric dyn_cast_or_null<const TargetRegisterClass *>(RegAttrs.RCOrRB); 245*0fca6ea1SDimitry Andric if (UseRC && !MRI->constrainRegClass(NewVR, UseRC)) { 246*0fca6ea1SDimitry Andric MachineBasicBlock *UseBB = UseMI->getParent(); 247*0fca6ea1SDimitry Andric MachineInstr *InsertedCopy = 248*0fca6ea1SDimitry Andric InsertNewDef(TargetOpcode::COPY, UseBB, UseBB->getFirstNonPHI(), 249*0fca6ea1SDimitry Andric RegAttrs, MRI, TII) 250*0fca6ea1SDimitry Andric .addReg(NewVR); 251*0fca6ea1SDimitry Andric NewVR = InsertedCopy->getOperand(0).getReg(); 252*0fca6ea1SDimitry Andric LLVM_DEBUG(dbgs() << " Inserted COPY: " << *InsertedCopy); 253*0fca6ea1SDimitry Andric } 254*0fca6ea1SDimitry Andric } 2550b57cec5SDimitry Andric U.setReg(NewVR); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric namespace llvm { 2590b57cec5SDimitry Andric 260fe6060f1SDimitry Andric /// SSAUpdaterTraits<MachineSSAUpdater> - Traits for the SSAUpdaterImpl 261fe6060f1SDimitry Andric /// template, specialized for MachineSSAUpdater. 2620b57cec5SDimitry Andric template<> 2630b57cec5SDimitry Andric class SSAUpdaterTraits<MachineSSAUpdater> { 2640b57cec5SDimitry Andric public: 2650b57cec5SDimitry Andric using BlkT = MachineBasicBlock; 2665ffd83dbSDimitry Andric using ValT = Register; 2670b57cec5SDimitry Andric using PhiT = MachineInstr; 2680b57cec5SDimitry Andric using BlkSucc_iterator = MachineBasicBlock::succ_iterator; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); } 2710b57cec5SDimitry Andric static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric /// Iterator for PHI operands. 2740b57cec5SDimitry Andric class PHI_iterator { 2750b57cec5SDimitry Andric private: 2760b57cec5SDimitry Andric MachineInstr *PHI; 2770b57cec5SDimitry Andric unsigned idx; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric public: 2800b57cec5SDimitry Andric explicit PHI_iterator(MachineInstr *P) // begin iterator 2810b57cec5SDimitry Andric : PHI(P), idx(1) {} 2820b57cec5SDimitry Andric PHI_iterator(MachineInstr *P, bool) // end iterator 2830b57cec5SDimitry Andric : PHI(P), idx(PHI->getNumOperands()) {} 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric PHI_iterator &operator++() { idx += 2; return *this; } 2860b57cec5SDimitry Andric bool operator==(const PHI_iterator& x) const { return idx == x.idx; } 2870b57cec5SDimitry Andric bool operator!=(const PHI_iterator& x) const { return !operator==(x); } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric unsigned getIncomingValue() { return PHI->getOperand(idx).getReg(); } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric MachineBasicBlock *getIncomingBlock() { 2920b57cec5SDimitry Andric return PHI->getOperand(idx+1).getMBB(); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric }; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric static inline PHI_iterator PHI_end(PhiT *PHI) { 2990b57cec5SDimitry Andric return PHI_iterator(PHI, true); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric /// FindPredecessorBlocks - Put the predecessors of BB into the Preds 3030b57cec5SDimitry Andric /// vector. 3040b57cec5SDimitry Andric static void FindPredecessorBlocks(MachineBasicBlock *BB, 3050b57cec5SDimitry Andric SmallVectorImpl<MachineBasicBlock*> *Preds){ 306fe6060f1SDimitry Andric append_range(*Preds, BB->predecessors()); 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 309*0fca6ea1SDimitry Andric /// GetPoisonVal - Create an IMPLICIT_DEF instruction with a new register. 3100b57cec5SDimitry Andric /// Add it into the specified block and return the register. 311*0fca6ea1SDimitry Andric static Register GetPoisonVal(MachineBasicBlock *BB, 3120b57cec5SDimitry Andric MachineSSAUpdater *Updater) { 313*0fca6ea1SDimitry Andric // Insert an implicit_def to represent a poison value. 314*0fca6ea1SDimitry Andric MachineInstr *NewDef = 315*0fca6ea1SDimitry Andric InsertNewDef(TargetOpcode::IMPLICIT_DEF, BB, BB->getFirstNonPHI(), 316*0fca6ea1SDimitry Andric Updater->RegAttrs, Updater->MRI, Updater->TII); 3170b57cec5SDimitry Andric return NewDef->getOperand(0).getReg(); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric /// CreateEmptyPHI - Create a PHI instruction that defines a new register. 3210b57cec5SDimitry Andric /// Add it into the specified block and return the register. 3225ffd83dbSDimitry Andric static Register CreateEmptyPHI(MachineBasicBlock *BB, unsigned NumPreds, 3230b57cec5SDimitry Andric MachineSSAUpdater *Updater) { 3240b57cec5SDimitry Andric MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->begin(); 325*0fca6ea1SDimitry Andric MachineInstr *PHI = 326*0fca6ea1SDimitry Andric InsertNewDef(TargetOpcode::PHI, BB, Loc, Updater->RegAttrs, 327*0fca6ea1SDimitry Andric Updater->MRI, Updater->TII); 3280b57cec5SDimitry Andric return PHI->getOperand(0).getReg(); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric /// AddPHIOperand - Add the specified value as an operand of the PHI for 3320b57cec5SDimitry Andric /// the specified predecessor block. 3335ffd83dbSDimitry Andric static void AddPHIOperand(MachineInstr *PHI, Register Val, 3340b57cec5SDimitry Andric MachineBasicBlock *Pred) { 3350b57cec5SDimitry Andric MachineInstrBuilder(*Pred->getParent(), PHI).addReg(Val).addMBB(Pred); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric /// InstrIsPHI - Check if an instruction is a PHI. 3390b57cec5SDimitry Andric static MachineInstr *InstrIsPHI(MachineInstr *I) { 3400b57cec5SDimitry Andric if (I && I->isPHI()) 3410b57cec5SDimitry Andric return I; 3420b57cec5SDimitry Andric return nullptr; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric /// ValueIsPHI - Check if the instruction that defines the specified register 3460b57cec5SDimitry Andric /// is a PHI instruction. 3475ffd83dbSDimitry Andric static MachineInstr *ValueIsPHI(Register Val, MachineSSAUpdater *Updater) { 3480b57cec5SDimitry Andric return InstrIsPHI(Updater->MRI->getVRegDef(Val)); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source 3520b57cec5SDimitry Andric /// operands, i.e., it was just added. 3535ffd83dbSDimitry Andric static MachineInstr *ValueIsNewPHI(Register Val, MachineSSAUpdater *Updater) { 3540b57cec5SDimitry Andric MachineInstr *PHI = ValueIsPHI(Val, Updater); 3550b57cec5SDimitry Andric if (PHI && PHI->getNumOperands() <= 1) 3560b57cec5SDimitry Andric return PHI; 3570b57cec5SDimitry Andric return nullptr; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric /// GetPHIValue - For the specified PHI instruction, return the register 3610b57cec5SDimitry Andric /// that it defines. 3625ffd83dbSDimitry Andric static Register GetPHIValue(MachineInstr *PHI) { 3630b57cec5SDimitry Andric return PHI->getOperand(0).getReg(); 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric }; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric } // end namespace llvm 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry 3700b57cec5SDimitry Andric /// for the specified BB and if so, return it. If not, construct SSA form by 3710b57cec5SDimitry Andric /// first calculating the required placement of PHIs and then inserting new 3720b57cec5SDimitry Andric /// PHIs where needed. 3730eae32dcSDimitry Andric Register 3740eae32dcSDimitry Andric MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB, 3750eae32dcSDimitry Andric bool ExistingValueOnly) { 3760b57cec5SDimitry Andric AvailableValsTy &AvailableVals = getAvailableVals(AV); 3770eae32dcSDimitry Andric Register ExistingVal = AvailableVals.lookup(BB); 3780eae32dcSDimitry Andric if (ExistingVal || ExistingValueOnly) 3790eae32dcSDimitry Andric return ExistingVal; 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric SSAUpdaterImpl<MachineSSAUpdater> Impl(this, &AvailableVals, InsertedPHIs); 3820b57cec5SDimitry Andric return Impl.GetValue(BB); 3830b57cec5SDimitry Andric } 384