1 //===- MachineCycleAnalysis.cpp - Compute CycleInfo for Machine IR --------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineCycleAnalysis.h"
10 #include "llvm/ADT/GenericCycleImpl.h"
11 #include "llvm/CodeGen/MachineRegisterInfo.h"
12 #include "llvm/CodeGen/MachineSSAContext.h"
13 #include "llvm/CodeGen/TargetInstrInfo.h"
14 #include "llvm/CodeGen/TargetSubtargetInfo.h"
15 #include "llvm/InitializePasses.h"
19 template class llvm::GenericCycleInfo
<llvm::MachineSSAContext
>;
20 template class llvm::GenericCycle
<llvm::MachineSSAContext
>;
22 char MachineCycleInfoWrapperPass::ID
= 0;
24 MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
25 : MachineFunctionPass(ID
) {
26 initializeMachineCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
29 INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass
, "machine-cycles",
30 "Machine Cycle Info Analysis", true, true)
31 INITIALIZE_PASS_END(MachineCycleInfoWrapperPass
, "machine-cycles",
32 "Machine Cycle Info Analysis", true, true)
34 void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
36 MachineFunctionPass::getAnalysisUsage(AU
);
39 bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction
&Func
) {
47 void MachineCycleInfoWrapperPass::print(raw_ostream
&OS
, const Module
*) const {
48 OS
<< "MachineCycleInfo for function: " << F
->getName() << "\n";
52 void MachineCycleInfoWrapperPass::releaseMemory() {
58 class MachineCycleInfoPrinterPass
: public MachineFunctionPass
{
62 MachineCycleInfoPrinterPass();
64 bool runOnMachineFunction(MachineFunction
&F
) override
;
65 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
69 char MachineCycleInfoPrinterPass::ID
= 0;
71 MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
72 : MachineFunctionPass(ID
) {
73 initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
76 INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass
, "print-machine-cycles",
77 "Print Machine Cycle Info Analysis", true, true)
78 INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass
)
79 INITIALIZE_PASS_END(MachineCycleInfoPrinterPass
, "print-machine-cycles",
80 "Print Machine Cycle Info Analysis", true, true)
82 void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
84 AU
.addRequired
<MachineCycleInfoWrapperPass
>();
85 MachineFunctionPass::getAnalysisUsage(AU
);
88 bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction
&F
) {
89 auto &CI
= getAnalysis
<MachineCycleInfoWrapperPass
>();
94 bool llvm::isCycleInvariant(const MachineCycle
*Cycle
, MachineInstr
&I
) {
95 MachineFunction
*MF
= I
.getParent()->getParent();
96 MachineRegisterInfo
*MRI
= &MF
->getRegInfo();
97 const TargetSubtargetInfo
&ST
= MF
->getSubtarget();
98 const TargetRegisterInfo
*TRI
= ST
.getRegisterInfo();
99 const TargetInstrInfo
*TII
= ST
.getInstrInfo();
101 // The instruction is cycle invariant if all of its operands are.
102 for (const MachineOperand
&MO
: I
.operands()) {
106 Register Reg
= MO
.getReg();
110 // An instruction that uses or defines a physical register can't e.g. be
111 // hoisted, so mark this as not invariant.
112 if (Reg
.isPhysical()) {
114 // If the physreg has no defs anywhere, it's just an ambient register
115 // and we can freely move its uses. Alternatively, if it's allocatable,
116 // it could get allocated to something with a def during allocation.
117 // However, if the physreg is known to always be caller saved/restored
118 // then this use is safe to hoist.
119 if (!MRI
->isConstantPhysReg(Reg
) &&
120 !(TRI
->isCallerPreservedPhysReg(Reg
.asMCReg(), *I
.getMF())) &&
121 !TII
->isIgnorableUse(MO
))
123 // Otherwise it's safe to move.
125 } else if (!MO
.isDead()) {
126 // A def that isn't dead can't be moved.
128 } else if (any_of(Cycle
->getEntries(),
129 [&](const MachineBasicBlock
*Block
) {
130 return Block
->isLiveIn(Reg
);
132 // If the reg is live into any header of the cycle we can't hoist an
133 // instruction which would clobber it.
141 assert(MRI
->getVRegDef(Reg
) && "Machine instr not mapped for this vreg?!");
143 // If the cycle contains the definition of an operand, then the instruction
144 // isn't cycle invariant.
145 if (Cycle
->contains(MRI
->getVRegDef(Reg
)->getParent()))
149 // If we got this far, the instruction is cycle invariant!