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/GlobalISel/GenericMachineInstrs.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace llvm;
24
25template <>
26void MachineSSAContext::appendBlockDefs(SmallVectorImpl<Register> &defs,
27 const MachineBasicBlock &block) {
28 for (auto &instr : block.instrs()) {
29 for (auto &op : instr.all_defs())
30 defs.push_back(Elt: op.getReg());
31 }
32}
33
34template <>
35void MachineSSAContext::appendBlockTerms(SmallVectorImpl<MachineInstr *> &terms,
36 MachineBasicBlock &block) {
37 for (auto &T : block.terminators())
38 terms.push_back(Elt: &T);
39}
40
41template <>
42void MachineSSAContext::appendBlockTerms(
43 SmallVectorImpl<const MachineInstr *> &terms,
44 const MachineBasicBlock &block) {
45 for (auto &T : block.terminators())
46 terms.push_back(Elt: &T);
47}
48
49/// Get the defining block of a value.
50template <>
51const MachineBasicBlock *MachineSSAContext::getDefBlock(Register value) const {
52 if (!value)
53 return nullptr;
54 return F->getRegInfo().getVRegDef(Reg: value)->getParent();
55}
56
57static bool isUndef(const MachineInstr &MI) {
58 return MI.getOpcode() == TargetOpcode::G_IMPLICIT_DEF ||
59 MI.getOpcode() == TargetOpcode::IMPLICIT_DEF;
60}
61
62template <> bool MachineSSAContext::isAlwaysUniform(Register) { return false; }
63
64/// MachineInstr equivalent of PHINode::hasConstantOrUndefValue() for G_PHI.
65template <>
66bool MachineSSAContext::isConstantOrUndefValuePhi(const MachineInstr &Phi) {
67 if (!Phi.isPHI())
68 return false;
69
70 // In later passes PHI may appear with an undef operand, getVRegDef can fail.
71 if (Phi.getOpcode() == TargetOpcode::PHI)
72 return Phi.isConstantValuePHI().isValid();
73
74 // For G_PHI we do equivalent of PHINode::hasConstantOrUndefValue().
75 const MachineRegisterInfo &MRI = Phi.getMF()->getRegInfo();
76 Register This = Phi.getOperand(i: 0).getReg();
77 Register ConstantValue;
78 for (unsigned i = 1, e = Phi.getNumOperands(); i < e; i += 2) {
79 Register Incoming = Phi.getOperand(i).getReg();
80 if (Incoming != This && !isUndef(MI: *MRI.getVRegDef(Reg: Incoming))) {
81 if (ConstantValue && ConstantValue != Incoming)
82 return false;
83 ConstantValue = Incoming;
84 }
85 }
86 return true;
87}
88
89template <>
90Intrinsic::ID MachineSSAContext::getIntrinsicID(const MachineInstr &MI) {
91 if (auto *GI = dyn_cast<GIntrinsic>(Val: &MI))
92 return GI->getIntrinsicID();
93 return Intrinsic::not_intrinsic;
94}
95
96template <>
97Printable MachineSSAContext::print(const MachineBasicBlock *Block) const {
98 if (!Block)
99 return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
100 return Printable([Block](raw_ostream &Out) { Block->printName(os&: Out); });
101}
102
103template <> Printable MachineSSAContext::print(const MachineInstr *I) const {
104 return Printable([I](raw_ostream &Out) { I->print(OS&: Out); });
105}
106
107template <> Printable MachineSSAContext::print(Register Value) const {
108 auto *MRI = &F->getRegInfo();
109 return Printable([MRI, Value](raw_ostream &Out) {
110 Out << printReg(Reg: Value, TRI: MRI->getTargetRegisterInfo(), SubIdx: 0, MRI);
111
112 if (Value) {
113 // Try to print the definition.
114 if (auto *Instr = MRI->getUniqueVRegDef(Reg: Value)) {
115 Out << ": ";
116 Instr->print(OS&: Out);
117 }
118 }
119 });
120}
121
122template <>
123Printable MachineSSAContext::printAsOperand(const MachineBasicBlock *BB) const {
124 return Printable([BB](raw_ostream &Out) { BB->printAsOperand(OS&: Out); });
125}
126