LLVM 23.0.0git
MachineSSAContext.cpp
Go to the documentation of this file.
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
22
23using namespace llvm;
24
25template <>
27 const MachineBasicBlock &block) {
28 for (auto &instr : block.instrs()) {
29 for (auto &op : instr.all_defs())
30 defs.push_back(op.getReg());
31 }
32}
33
34template <>
37 for (auto &T : block.terminators())
38 terms.push_back(&T);
39}
40
41template <>
44 const MachineBasicBlock &block) {
45 for (auto &T : block.terminators())
46 terms.push_back(&T);
47}
48
49/// Get the defining block of a value.
50template <>
52 if (!value)
53 return nullptr;
54 return F->getRegInfo().getVRegDef(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 <>
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(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(*MRI.getVRegDef(Incoming))) {
81 if (ConstantValue && ConstantValue != Incoming)
82 return false;
83 ConstantValue = Incoming;
84 }
85 }
86 return true;
87}
88
89template <>
91 if (auto *GI = dyn_cast<GIntrinsic>(&MI))
92 return GI->getIntrinsicID();
94}
95
96template <>
98 if (!Block)
99 return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
100 return Printable([Block](raw_ostream &Out) { Block->printName(Out); });
101}
102
103template <> Printable MachineSSAContext::print(const MachineInstr *I) const {
104 return Printable([I](raw_ostream &Out) { I->print(Out); });
105}
106
108 auto *MRI = &F->getRegInfo();
109 return Printable([MRI, Value](raw_ostream &Out) {
110 Out << printReg(Value, MRI->getTargetRegisterInfo(), 0, MRI);
111
112 if (Value) {
113 // Try to print the definition.
114 if (auto *Instr = MRI->getUniqueVRegDef(Value)) {
115 Out << ": ";
116 Instr->print(Out);
117 }
118 }
119 });
120}
121
122template <>
124 return Printable([BB](raw_ostream &Out) { BB->printAsOperand(Out); });
125}
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
#define op(i)
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
static bool isUndef(const MachineInstr &MI)
This file declares a specialization of the GenericSSAContext<X> template class for Machine IR.
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
unify loop Fixup each natural loop to have a single exit block
static void appendBlockDefs(SmallVectorImpl< ValueRefT > &defs, BlockT &block)
static bool isConstantOrUndefValuePhi(const InstructionT &Instr)
static void appendBlockTerms(SmallVectorImpl< InstructionT * > &terms, BlockT &block)
static Intrinsic::ID getIntrinsicID(const InstructionT &I)
Printable print(const BlockT *block) const
static bool isAlwaysUniform(ConstValueRefT V)
const BlockT * getDefBlock(ConstValueRefT value) const
Printable printAsOperand(const BlockT *BB) const
LLVM_ABI void printAsOperand(raw_ostream &OS, bool PrintType=true) const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Representation of each machine instruction.
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
Simple wrapper around std::function<void(raw_ostream&)>.
Definition Printable.h:38
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
LLVM Value Representation.
Definition Value.h:75
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.