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
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 MachineFunction *MF = I.getParent()->getParent();
121 MachineRegisterInfo *MRI = &MF->getRegInfo();
122 const TargetSubtargetInfo &ST = MF->getSubtarget();
123 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
124 const TargetInstrInfo *TII = ST.getInstrInfo();
125
126 // The instruction is cycle invariant if all of its operands are.
127 for (const MachineOperand &MO : I.operands()) {
128 if (!MO.isReg())
129 continue;
130
131 Register Reg = MO.getReg();
132 if (Reg == 0)
133 continue;
134
135 // An instruction that uses or defines a physical register can't e.g. be
136 // hoisted, so mark this as not invariant.
137 if (Reg.isPhysical()) {
138 if (MO.isUse()) {
139 // If the physreg has no defs anywhere, it's just an ambient register
140 // and we can freely move its uses. Alternatively, if it's allocatable,
141 // it could get allocated to something with a def during allocation.
142 // However, if the physreg is known to always be caller saved/restored
143 // then this use is safe to hoist.
144 if (!MRI->isConstantPhysReg(Reg) &&
145 !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
146 !TII->isIgnorableUse(MO))
147 return false;
148 // Otherwise it's safe to move.
149 continue;
150 } else if (!MO.isDead()) {
151 // A def that isn't dead can't be moved.
152 return false;
153 } else if (any_of(Cycle->getEntries(),
154 [&](const MachineBasicBlock *Block) {
155 return Block->isLiveIn(Reg);
156 })) {
157 // If the reg is live into any header of the cycle we can't hoist an
158 // instruction which would clobber it.
159 return false;
160 }
161 }
162
163 if (!MO.isUse())
164 continue;
165
166 assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
167
168 // If the cycle contains the definition of an operand, then the instruction
169 // isn't cycle invariant.
170 if (Cycle->contains(MRI->getVRegDef(Reg)->getParent()))
171 return false;
172 }
173
174 // If we got this far, the instruction is cycle invariant!
175 return true;
176}
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
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
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 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.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
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: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
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
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.
LLVM_ABI bool isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
CycleInfo::CycleT Cycle
Definition CycleInfo.h:26
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:1746
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineCycleInfo::CycleT MachineCycle
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29