LLVM 23.0.0git
MachineCycleAnalysis.cpp
Go to the documentation of this file.
1//===- MachineCycleAnalysis.cpp - Compute CycleInfo for Machine IR --------===//
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
16
17using namespace llvm;
18
21
23
26
28 "Machine Cycle Info Analysis", true, true)
30 "Machine Cycle Info Analysis", true, true)
31
36
38 CI.clear();
39
40 F = &Func;
41 CI.compute(Func);
42 return false;
43}
44
46 OS << "MachineCycleInfo for function: " << F->getName() << "\n";
47 CI.print(OS);
48}
49
51 CI.clear();
52 F = nullptr;
53}
54
55AnalysisKey MachineCycleAnalysis::Key;
56
64
65namespace {
66class MachineCycleInfoPrinterLegacy : public MachineFunctionPass {
67public:
68 static char ID;
69
70 MachineCycleInfoPrinterLegacy();
71
72 bool runOnMachineFunction(MachineFunction &F) override;
73 void getAnalysisUsage(AnalysisUsage &AU) const override;
74};
75} // namespace
76
77char MachineCycleInfoPrinterLegacy::ID = 0;
78
79MachineCycleInfoPrinterLegacy::MachineCycleInfoPrinterLegacy()
81
82INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
83 "Print Machine Cycle Info Analysis", true, true)
85INITIALIZE_PASS_END(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
86 "Print Machine Cycle Info Analysis", true, true)
87
88void MachineCycleInfoPrinterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
89 AU.setPreservesAll();
90 AU.addRequired<MachineCycleInfoWrapperPass>();
92}
93
94bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) {
95 auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
96 CI.print(errs());
97 return false;
98}
99
103 auto &MCI = MFAM.getResult<MachineCycleAnalysis>(MF);
104 MCI.print(OS);
105 return PreservedAnalyses::all();
106}
107
109 MachineFunction *MF = I.getParent()->getParent();
111 const TargetSubtargetInfo &ST = MF->getSubtarget();
112 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
113 const TargetInstrInfo *TII = ST.getInstrInfo();
114
115 // The instruction is cycle invariant if all of its operands are.
116 for (const MachineOperand &MO : I.operands()) {
117 if (!MO.isReg())
118 continue;
119
120 Register Reg = MO.getReg();
121 if (Reg == 0)
122 continue;
123
124 // An instruction that uses or defines a physical register can't e.g. be
125 // hoisted, so mark this as not invariant.
126 if (Reg.isPhysical()) {
127 if (MO.isUse()) {
128 // If the physreg has no defs anywhere, it's just an ambient register
129 // and we can freely move its uses. Alternatively, if it's allocatable,
130 // it could get allocated to something with a def during allocation.
131 // However, if the physreg is known to always be caller saved/restored
132 // then this use is safe to hoist.
133 if (!MRI->isConstantPhysReg(Reg) &&
134 !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
135 !TII->isIgnorableUse(MO))
136 return false;
137 // Otherwise it's safe to move.
138 continue;
139 } else if (!MO.isDead()) {
140 // A def that isn't dead can't be moved.
141 return false;
142 } else if (any_of(Cycle->getEntries(),
143 [&](const MachineBasicBlock *Block) {
144 return Block->isLiveIn(Reg);
145 })) {
146 // If the reg is live into any header of the cycle we can't hoist an
147 // instruction which would clobber it.
148 return false;
149 }
150 }
151
152 if (!MO.isUse())
153 continue;
154
155 assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
156
157 // If the cycle contains the definition of an operand, then the instruction
158 // isn't cycle invariant.
159 if (Cycle->contains(MRI->getVRegDef(Reg)->getParent()))
160 return false;
161 }
162
163 // If we got this far, the instruction is cycle invariant!
164 return true;
165}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This template implementation resides in a separate file so that it does not get injected into every ....
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares a specialization of the GenericSSAContext<X> template class for Machine IR.
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
Cycle information for a function.
void compute(FunctionT &F)
Compute the cycle info for a function.
A possibly irreducible generalization of a Loop.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Legacy analysis pass which computes a MachineCycleInfo.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
bool runOnMachineFunction(MachineFunction &F) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI bool isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
CycleInfo::CycleT Cycle
Definition CycleInfo.h:24
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GenericCycleInfo< MachineSSAContext > MachineCycleInfo
MachineCycleInfo::CycleT MachineCycle
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29