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
17
18using namespace llvm;
19
20template class LLVM_ABI_FOR_TEST
22
24
27
29 "Machine Cycle Info Analysis", true, true)
31 "Machine Cycle Info Analysis", true, true)
32
37
39 CI.clear();
40
41 F = &Func;
42 CI.compute(Func);
43 return false;
44}
45
47 OS << "MachineCycleInfo for function: " << F->getName() << "\n";
48 CI.print(OS);
49}
50
53 CI.verify();
54}
55
57 CI.clear();
58 F = nullptr;
59}
60
61AnalysisKey MachineCycleAnalysis::Key;
62
70
73 MachineFunctionAnalysisManager::Invalidator &) {
74 // Check whether the analysis, all analyses on functions, or the function's
75 // CFG have been preserved.
76 auto PAC = PA.getChecker<MachineCycleAnalysis>();
77 return !(PAC.preserved() ||
78 PAC.preservedSet<AllAnalysesOn<MachineFunction>>() ||
79 PAC.preservedSet<CFGAnalyses>());
80}
81
82namespace {
83class MachineCycleInfoPrinterLegacy : public MachineFunctionPass {
84public:
85 static char ID;
86
87 MachineCycleInfoPrinterLegacy();
88
89 bool runOnMachineFunction(MachineFunction &F) override;
90 void getAnalysisUsage(AnalysisUsage &AU) const override;
91};
92} // namespace
93
94char MachineCycleInfoPrinterLegacy::ID = 0;
95
96MachineCycleInfoPrinterLegacy::MachineCycleInfoPrinterLegacy()
98
99INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
100 "Print Machine Cycle Info Analysis", true, true)
102INITIALIZE_PASS_END(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
103 "Print Machine Cycle Info Analysis", true, true)
104
105void MachineCycleInfoPrinterLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesAll();
107 AU.addRequired<MachineCycleInfoWrapperPass>();
109}
110
111bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) {
112 auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
113 CI.print(errs());
114 return false;
115}
116
120 auto &MCI = MFAM.getResult<MachineCycleAnalysis>(MF);
121 MCI.print(OS);
122 return PreservedAnalyses::all();
123}
124
131
133 MachineInstr &I) {
134 MachineFunction *MF = I.getParent()->getParent();
136 const TargetSubtargetInfo &ST = MF->getSubtarget();
137 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
138 const TargetInstrInfo *TII = ST.getInstrInfo();
139
140 // The instruction is cycle invariant if all of its operands are.
141 for (const MachineOperand &MO : I.operands()) {
142 if (!MO.isReg())
143 continue;
144
145 Register Reg = MO.getReg();
146 if (Reg == 0)
147 continue;
148
149 // An instruction that uses or defines a physical register can't e.g. be
150 // hoisted, so mark this as not invariant.
151 if (Reg.isPhysical()) {
152 if (MO.isUse()) {
153 // If the physreg has no defs anywhere, it's just an ambient register
154 // and we can freely move its uses. Alternatively, if it's allocatable,
155 // it could get allocated to something with a def during allocation.
156 // However, if the physreg is known to always be caller saved/restored
157 // then this use is safe to hoist.
158 if (!MRI->isConstantPhysReg(Reg) &&
159 !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
160 !TII->isIgnorableUse(I, I.getOperandNo(&MO)))
161 return false;
162 // Otherwise it's safe to move.
163 continue;
164 } else if (!MO.isDead()) {
165 // A def that isn't dead can't be moved.
166 return false;
167 } else if (any_of(CI.getEntries(Cycle),
168 [&](const MachineBasicBlock *Block) {
169 return Block->isLiveIn(Reg);
170 })) {
171 // If the reg is live into any header of the cycle we can't hoist an
172 // instruction which would clobber it.
173 return false;
174 }
175 }
176
177 if (!MO.isUse())
178 continue;
179
180 assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
181
182 // If the cycle contains the definition of an operand, then the instruction
183 // isn't cycle invariant.
184 if (CI.contains(Cycle, MRI->getDefBlock(Reg)))
185 return false;
186 }
187
188 // If we got this far, the instruction is cycle invariant!
189 return true;
190}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:216
This file declares an analysis pass that computes CycleInfo for LLVM IR, specialized from GenericCycl...
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
ppc ctr loops verify
#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
MachineRegisterInfo & MRI
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 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 verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
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: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.
LLVM_ABI bool VerifyCycleInfo
Enable verification of cycle info.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)