1 //===- MachineSSAContext.cpp ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// 10 /// This file defines a specialization of the GenericSSAContext<X> 11 /// template class for Machine IR. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/MachineSSAContext.h" 16 #include "llvm/CodeGen/MachineBasicBlock.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineInstr.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace llvm; 23 24 template <> 25 void MachineSSAContext::appendBlockDefs(SmallVectorImpl<Register> &defs, 26 const MachineBasicBlock &block) { 27 for (auto &instr : block.instrs()) { 28 for (auto &op : instr.all_defs()) 29 defs.push_back(op.getReg()); 30 } 31 } 32 33 template <> 34 void MachineSSAContext::appendBlockTerms(SmallVectorImpl<MachineInstr *> &terms, 35 MachineBasicBlock &block) { 36 for (auto &T : block.terminators()) 37 terms.push_back(&T); 38 } 39 40 template <> 41 void MachineSSAContext::appendBlockTerms( 42 SmallVectorImpl<const MachineInstr *> &terms, 43 const MachineBasicBlock &block) { 44 for (auto &T : block.terminators()) 45 terms.push_back(&T); 46 } 47 48 /// Get the defining block of a value. 49 template <> 50 const MachineBasicBlock *MachineSSAContext::getDefBlock(Register value) const { 51 if (!value) 52 return nullptr; 53 return F->getRegInfo().getVRegDef(value)->getParent(); 54 } 55 56 template <> 57 bool MachineSSAContext::isConstantOrUndefValuePhi(const MachineInstr &Phi) { 58 return Phi.isConstantValuePHI(); 59 } 60 61 template <> 62 Printable MachineSSAContext::print(const MachineBasicBlock *Block) const { 63 if (!Block) 64 return Printable([](raw_ostream &Out) { Out << "<nullptr>"; }); 65 return Printable([Block](raw_ostream &Out) { Block->printName(Out); }); 66 } 67 68 template <> Printable MachineSSAContext::print(const MachineInstr *I) const { 69 return Printable([I](raw_ostream &Out) { I->print(Out); }); 70 } 71 72 template <> Printable MachineSSAContext::print(Register Value) const { 73 auto *MRI = &F->getRegInfo(); 74 return Printable([MRI, Value](raw_ostream &Out) { 75 Out << printReg(Value, MRI->getTargetRegisterInfo(), 0, MRI); 76 77 if (Value) { 78 // Try to print the definition. 79 if (auto *Instr = MRI->getUniqueVRegDef(Value)) { 80 Out << ": "; 81 Instr->print(Out); 82 } 83 } 84 }); 85 } 86