LLVM 24.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
19template class LLVM_ABI_FOR_TEST
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
67 MachineFunctionAnalysisManager::Invalidator &) {
68 // Check whether the analysis, all analyses on functions, or the function's
69 // CFG have been preserved.
70 auto PAC = PA.getChecker<MachineCycleAnalysis>();
71 return !(PAC.preserved() ||
72 PAC.preservedSet<AllAnalysesOn<MachineFunction>>() ||
73 PAC.preservedSet<CFGAnalyses>());
74}
75
76namespace {
77class MachineCycleInfoPrinterLegacy : public MachineFunctionPass {
78public:
79 static char ID;
80
81 MachineCycleInfoPrinterLegacy();
82
83 bool runOnMachineFunction(MachineFunction &F) override;
84 void getAnalysisUsage(AnalysisUsage &AU) const override;
85};
86} // namespace
87
88char MachineCycleInfoPrinterLegacy::ID = 0;
89
90MachineCycleInfoPrinterLegacy::MachineCycleInfoPrinterLegacy()
92
93INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
94 "Print Machine Cycle Info Analysis", true, true)
96INITIALIZE_PASS_END(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
97 "Print Machine Cycle Info Analysis", true, true)
98
99void MachineCycleInfoPrinterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
100 AU.setPreservesAll();
101 AU.addRequired<MachineCycleInfoWrapperPass>();
103}
104
105bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) {
106 auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
107 CI.print(errs());
108 return false;
109}
110
114 auto &MCI = MFAM.getResult<MachineCycleAnalysis>(MF);
115 MCI.print(OS);
116 return PreservedAnalyses::all();
117}
118
120 MachineInstr &I) {
121 MachineFunction *MF = I.getParent()->getParent();
122 MachineRegisterInfo *MRI = &MF->getRegInfo();
123 const TargetSubtargetInfo &ST = MF->getSubtarget();
124 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
125 const TargetInstrInfo *TII = ST.getInstrInfo();
126
127 // The instruction is cycle invariant if all of its operands are.
128 for (const MachineOperand &MO : I.operands()) {
129 if (!MO.isReg())
130 continue;
131
132 Register Reg = MO.getReg();
133 if (Reg == 0)
134 continue;
135
136 // An instruction that uses or defines a physical register can't e.g. be
137 // hoisted, so mark this as not invariant.
138 if (Reg.isPhysical()) {
139 if (MO.isUse()) {
140 // If the physreg has no defs anywhere, it's just an ambient register
141 // and we can freely move its uses. Alternatively, if it's allocatable,
142 // it could get allocated to something with a def during allocation.
143 // However, if the physreg is known to always be caller saved/restored
144 // then this use is safe to hoist.
145 if (!MRI->isConstantPhysReg(Reg) &&
146 !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
147 !TII->isIgnorableUse(I, I.getOperandNo(&MO)))
148 return false;
149 // Otherwise it's safe to move.
150 continue;
151 } else if (!MO.isDead()) {
152 // A def that isn't dead can't be moved.
153 return false;
154 } else if (any_of(CI.getEntries(Cycle),
155 [&](const MachineBasicBlock *Block) {
156 return Block->isLiveIn(Reg);
157 })) {
158 // If the reg is live into any header of the cycle we can't hoist an
159 // instruction which would clobber it.
160 return false;
161 }
162 }
163
164 if (!MO.isUse())
165 continue;
166
167 assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
168
169 // If the cycle contains the definition of an operand, then the instruction
170 // isn't cycle invariant.
171 if (CI.contains(Cycle, MRI->getDefBlock(Reg)))
172 return false;
173 }
174
175 // If we got this far, the instruction is cycle invariant!
176 return true;
177}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:220
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
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
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.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Opaque handle to a cycle within a GenericCycleInfo that wraps the cycle's preorder index.
Cycle information for a function.
ArrayRef< BlockT * > getEntries(CycleRef C) const
void compute(FunctionT &F)
Compute the cycle info for a function.
bool contains(CycleRef Outer, CycleRef Inner) const
Returns true iff Outer contains Inner. O(1). Non-strict.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI bool invalidate(MachineFunction &, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
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,...
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
MachineBasicBlock * getDefBlock(Register Reg) const
Return the machine basic block in which the specified virtual register is defined,...
LLVM_ABI bool isConstantPhysReg(MCRegister PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
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
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
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
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
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:1762
LLVM_ABI bool isCycleInvariant(const MachineCycleInfo &CI, CycleRef Cycle, MachineInstr &I)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29