xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineSSAContext.cpp (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
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 void MachineSSAContext::setFunction(MachineFunction &Fn) {
25   MF = &Fn;
26   RegInfo = &MF->getRegInfo();
27 }
28 
29 MachineBasicBlock *MachineSSAContext::getEntryBlock(MachineFunction &F) {
30   return &F.front();
31 }
32 
33 void MachineSSAContext::appendBlockTerms(
34     SmallVectorImpl<const MachineInstr *> &terms,
35     const MachineBasicBlock &block) {
36   for (auto &T : block.terminators())
37     terms.push_back(&T);
38 }
39 
40 void MachineSSAContext::appendBlockDefs(SmallVectorImpl<Register> &defs,
41                                         const MachineBasicBlock &block) {
42   for (const MachineInstr &instr : block.instrs()) {
43     for (const MachineOperand &op : instr.all_defs())
44       defs.push_back(op.getReg());
45   }
46 }
47 
48 /// Get the defining block of a value.
49 MachineBasicBlock *MachineSSAContext::getDefBlock(Register value) const {
50   if (!value)
51     return nullptr;
52   return RegInfo->getVRegDef(value)->getParent();
53 }
54 
55 bool MachineSSAContext::isConstantOrUndefValuePhi(const MachineInstr &Phi) {
56   return Phi.isConstantValuePHI();
57 }
58 
59 Printable MachineSSAContext::print(const MachineBasicBlock *Block) const {
60   if (!Block)
61     return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
62   return Printable([Block](raw_ostream &Out) { Block->printName(Out); });
63 }
64 
65 Printable MachineSSAContext::print(const MachineInstr *I) const {
66   return Printable([I](raw_ostream &Out) { I->print(Out); });
67 }
68 
69 Printable MachineSSAContext::print(Register Value) const {
70   auto *MRI = RegInfo;
71   return Printable([MRI, Value](raw_ostream &Out) {
72     Out << printReg(Value, MRI->getTargetRegisterInfo(), 0, MRI);
73 
74     if (Value) {
75       // Try to print the definition.
76       if (auto *Instr = MRI->getUniqueVRegDef(Value)) {
77         Out << ": ";
78         Instr->print(Out);
79       }
80     }
81   });
82 }
83