1 //===- MachineSSAContext.cpp ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines a specialization of the GenericSSAContext<X>
11 /// template class for Machine IR.
13 //===----------------------------------------------------------------------===//
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"
26 void 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(op
.getReg());
35 void MachineSSAContext::appendBlockTerms(SmallVectorImpl
<MachineInstr
*> &terms
,
36 MachineBasicBlock
&block
) {
37 for (auto &T
: block
.terminators())
42 void MachineSSAContext::appendBlockTerms(
43 SmallVectorImpl
<const MachineInstr
*> &terms
,
44 const MachineBasicBlock
&block
) {
45 for (auto &T
: block
.terminators())
49 /// Get the defining block of a value.
51 const MachineBasicBlock
*MachineSSAContext::getDefBlock(Register value
) const {
54 return F
->getRegInfo().getVRegDef(value
)->getParent();
57 static bool isUndef(const MachineInstr
&MI
) {
58 return MI
.getOpcode() == TargetOpcode::G_IMPLICIT_DEF
||
59 MI
.getOpcode() == TargetOpcode::IMPLICIT_DEF
;
62 /// MachineInstr equivalent of PHINode::hasConstantOrUndefValue() for G_PHI.
64 bool MachineSSAContext::isConstantOrUndefValuePhi(const MachineInstr
&Phi
) {
68 // In later passes PHI may appear with an undef operand, getVRegDef can fail.
69 if (Phi
.getOpcode() == TargetOpcode::PHI
)
70 return Phi
.isConstantValuePHI();
72 // For G_PHI we do equivalent of PHINode::hasConstantOrUndefValue().
73 const MachineRegisterInfo
&MRI
= Phi
.getMF()->getRegInfo();
74 Register This
= Phi
.getOperand(0).getReg();
75 Register ConstantValue
;
76 for (unsigned i
= 1, e
= Phi
.getNumOperands(); i
< e
; i
+= 2) {
77 Register Incoming
= Phi
.getOperand(i
).getReg();
78 if (Incoming
!= This
&& !isUndef(*MRI
.getVRegDef(Incoming
))) {
79 if (ConstantValue
&& ConstantValue
!= Incoming
)
81 ConstantValue
= Incoming
;
88 Intrinsic::ID
MachineSSAContext::getIntrinsicID(const MachineInstr
&MI
) {
89 if (auto *GI
= dyn_cast
<GIntrinsic
>(&MI
))
90 return GI
->getIntrinsicID();
91 return Intrinsic::not_intrinsic
;
95 Printable
MachineSSAContext::print(const MachineBasicBlock
*Block
) const {
97 return Printable([](raw_ostream
&Out
) { Out
<< "<nullptr>"; });
98 return Printable([Block
](raw_ostream
&Out
) { Block
->printName(Out
); });
101 template <> Printable
MachineSSAContext::print(const MachineInstr
*I
) const {
102 return Printable([I
](raw_ostream
&Out
) { I
->print(Out
); });
105 template <> Printable
MachineSSAContext::print(Register Value
) const {
106 auto *MRI
= &F
->getRegInfo();
107 return Printable([MRI
, Value
](raw_ostream
&Out
) {
108 Out
<< printReg(Value
, MRI
->getTargetRegisterInfo(), 0, MRI
);
111 // Try to print the definition.
112 if (auto *Instr
= MRI
->getUniqueVRegDef(Value
)) {
121 Printable
MachineSSAContext::printAsOperand(const MachineBasicBlock
*BB
) const {
122 return Printable([BB
](raw_ostream
&Out
) { BB
->printAsOperand(Out
); });