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
9#include "llvm/CodeGen/MachineCycleAnalysis.h"
10#include "llvm/ADT/GenericCycleImpl.h"
11#include "llvm/Analysis/CycleAnalysis.h"
12#include "llvm/CodeGen/MachineRegisterInfo.h"
13#include "llvm/CodeGen/MachineSSAContext.h"
14#include "llvm/CodeGen/TargetInstrInfo.h"
15#include "llvm/CodeGen/TargetSubtargetInfo.h"
16#include "llvm/InitializePasses.h"
17
18using namespace llvm;
19
20template class LLVM_ABI_FOR_TEST
21 llvm::GenericCycleInfo<llvm::MachineSSAContext>;
22
23char MachineCycleInfoWrapperPass::ID = 0;
24
25MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
26 : MachineFunctionPass(ID) {}
27
28INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles",
29 "Machine Cycle Info Analysis", true, true)
30INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles",
31 "Machine Cycle Info Analysis", true, true)
32
33void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll();
35 MachineFunctionPass::getAnalysisUsage(AU);
36}
37
38bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) {
39 CI.clear();
40
41 F = &Func;
42 CI.compute(F&: Func);
43 return false;
44}
45
46void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
47 OS << "MachineCycleInfo for function: " << F->getName() << "\n";
48 CI.print(Out&: OS);
49}
50
51void MachineCycleInfoWrapperPass::verifyAnalysis() const {
52 if (VerifyCycleInfo)
53 CI.verify();
54}
55
56void MachineCycleInfoWrapperPass::releaseMemory() {
57 CI.clear();
58 F = nullptr;
59}
60
61AnalysisKey MachineCycleAnalysis::Key;
62
63MachineCycleInfo
64MachineCycleAnalysis::run(MachineFunction &MF,
65 MachineFunctionAnalysisManager &MFAM) {
66 MachineCycleInfo MCI;
67 MCI.compute(F&: MF);
68 return MCI;
69}
70
71bool MachineCycleInfo::invalidate(
72 MachineFunction &, const PreservedAnalyses &PA,
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()
97 : MachineFunctionPass(ID) {}
98
99INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles",
100 "Print Machine Cycle Info Analysis", true, true)
101INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
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>();
108 MachineFunctionPass::getAnalysisUsage(AU);
109}
110
111bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) {
112 auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
113 CI.print(OS&: errs());
114 return false;
115}
116
117PreservedAnalyses
118MachineCycleInfoPrinterPass::run(MachineFunction &MF,
119 MachineFunctionAnalysisManager &MFAM) {
120 auto &MCI = MFAM.getResult<MachineCycleAnalysis>(IR&: MF);
121 MCI.print(Out&: OS);
122 return PreservedAnalyses::all();
123}
124
125PreservedAnalyses
126MachineCycleInfoVerifierPass::run(MachineFunction &MF,
127 MachineFunctionAnalysisManager &MFAM) {
128 MFAM.getResult<MachineCycleAnalysis>(IR&: MF).verify();
129 return PreservedAnalyses::all();
130}
131
132bool llvm::isCycleInvariant(const MachineCycleInfo &CI, CycleRef Cycle,
133 MachineInstr &I) {
134 MachineFunction *MF = I.getParent()->getParent();
135 MachineRegisterInfo *MRI = &MF->getRegInfo();
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(PhysReg: Reg) &&
159 !(TRI->isCallerPreservedPhysReg(PhysReg: Reg.asMCReg(), MF: *I.getMF())) &&
160 !TII->isIgnorableUse(MI: I, OpIdx: I.getOperandNo(I: &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(Range: CI.getEntries(C: Cycle),
168 P: [&](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(C: Cycle, Block: MRI->getDefBlock(Reg)))
185 return false;
186 }
187
188 // If we got this far, the instruction is cycle invariant!
189 return true;
190}
191