xref: /src/contrib/llvm-project/llvm/lib/CodeGen/MachineCycleAnalysis.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
177fc4c14SDimitry Andric //===- MachineCycleAnalysis.cpp - Compute CycleInfo for Machine IR --------===//
277fc4c14SDimitry Andric //
377fc4c14SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477fc4c14SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
577fc4c14SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677fc4c14SDimitry Andric //
777fc4c14SDimitry Andric //===----------------------------------------------------------------------===//
877fc4c14SDimitry Andric 
977fc4c14SDimitry Andric #include "llvm/CodeGen/MachineCycleAnalysis.h"
1077fc4c14SDimitry Andric #include "llvm/ADT/GenericCycleImpl.h"
11145449b1SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
12e3b55780SDimitry Andric #include "llvm/CodeGen/MachineSSAContext.h"
13145449b1SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
14145449b1SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
15e3b55780SDimitry Andric #include "llvm/InitializePasses.h"
1677fc4c14SDimitry Andric 
1777fc4c14SDimitry Andric using namespace llvm;
1877fc4c14SDimitry Andric 
1977fc4c14SDimitry Andric template class llvm::GenericCycleInfo<llvm::MachineSSAContext>;
2077fc4c14SDimitry Andric template class llvm::GenericCycle<llvm::MachineSSAContext>;
2177fc4c14SDimitry Andric 
2277fc4c14SDimitry Andric char MachineCycleInfoWrapperPass::ID = 0;
2377fc4c14SDimitry Andric 
MachineCycleInfoWrapperPass()2477fc4c14SDimitry Andric MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
2577fc4c14SDimitry Andric     : MachineFunctionPass(ID) {
2677fc4c14SDimitry Andric   initializeMachineCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
2777fc4c14SDimitry Andric }
2877fc4c14SDimitry Andric 
2977fc4c14SDimitry Andric INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles",
3077fc4c14SDimitry Andric                       "Machine Cycle Info Analysis", true, true)
3177fc4c14SDimitry Andric INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles",
3277fc4c14SDimitry Andric                     "Machine Cycle Info Analysis", true, true)
3377fc4c14SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const3477fc4c14SDimitry Andric void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
3577fc4c14SDimitry Andric   AU.setPreservesAll();
3677fc4c14SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
3777fc4c14SDimitry Andric }
3877fc4c14SDimitry Andric 
runOnMachineFunction(MachineFunction & Func)3977fc4c14SDimitry Andric bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) {
4077fc4c14SDimitry Andric   CI.clear();
4177fc4c14SDimitry Andric 
4277fc4c14SDimitry Andric   F = &Func;
4377fc4c14SDimitry Andric   CI.compute(Func);
4477fc4c14SDimitry Andric   return false;
4577fc4c14SDimitry Andric }
4677fc4c14SDimitry Andric 
print(raw_ostream & OS,const Module *) const4777fc4c14SDimitry Andric void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
4877fc4c14SDimitry Andric   OS << "MachineCycleInfo for function: " << F->getName() << "\n";
4977fc4c14SDimitry Andric   CI.print(OS);
5077fc4c14SDimitry Andric }
5177fc4c14SDimitry Andric 
releaseMemory()5277fc4c14SDimitry Andric void MachineCycleInfoWrapperPass::releaseMemory() {
5377fc4c14SDimitry Andric   CI.clear();
5477fc4c14SDimitry Andric   F = nullptr;
5577fc4c14SDimitry Andric }
5677fc4c14SDimitry Andric 
57e3b55780SDimitry Andric namespace {
58145449b1SDimitry Andric class MachineCycleInfoPrinterPass : public MachineFunctionPass {
59145449b1SDimitry Andric public:
60145449b1SDimitry Andric   static char ID;
61145449b1SDimitry Andric 
62145449b1SDimitry Andric   MachineCycleInfoPrinterPass();
63145449b1SDimitry Andric 
64145449b1SDimitry Andric   bool runOnMachineFunction(MachineFunction &F) override;
65145449b1SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override;
66145449b1SDimitry Andric };
67e3b55780SDimitry Andric } // namespace
68145449b1SDimitry Andric 
6977fc4c14SDimitry Andric char MachineCycleInfoPrinterPass::ID = 0;
7077fc4c14SDimitry Andric 
MachineCycleInfoPrinterPass()7177fc4c14SDimitry Andric MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
7277fc4c14SDimitry Andric     : MachineFunctionPass(ID) {
7377fc4c14SDimitry Andric   initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
7477fc4c14SDimitry Andric }
7577fc4c14SDimitry Andric 
7677fc4c14SDimitry Andric INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass, "print-machine-cycles",
7777fc4c14SDimitry Andric                       "Print Machine Cycle Info Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)7877fc4c14SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
7977fc4c14SDimitry Andric INITIALIZE_PASS_END(MachineCycleInfoPrinterPass, "print-machine-cycles",
8077fc4c14SDimitry Andric                     "Print Machine Cycle Info Analysis", true, true)
8177fc4c14SDimitry Andric 
8277fc4c14SDimitry Andric void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage &AU) const {
8377fc4c14SDimitry Andric   AU.setPreservesAll();
8477fc4c14SDimitry Andric   AU.addRequired<MachineCycleInfoWrapperPass>();
8577fc4c14SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
8677fc4c14SDimitry Andric }
8777fc4c14SDimitry Andric 
runOnMachineFunction(MachineFunction & F)8877fc4c14SDimitry Andric bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction &F) {
8977fc4c14SDimitry Andric   auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
9077fc4c14SDimitry Andric   CI.print(errs());
9177fc4c14SDimitry Andric   return false;
9277fc4c14SDimitry Andric }
93145449b1SDimitry Andric 
isCycleInvariant(const MachineCycle * Cycle,MachineInstr & I)94145449b1SDimitry Andric bool llvm::isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I) {
95145449b1SDimitry Andric   MachineFunction *MF = I.getParent()->getParent();
96145449b1SDimitry Andric   MachineRegisterInfo *MRI = &MF->getRegInfo();
97145449b1SDimitry Andric   const TargetSubtargetInfo &ST = MF->getSubtarget();
98145449b1SDimitry Andric   const TargetRegisterInfo *TRI = ST.getRegisterInfo();
99145449b1SDimitry Andric   const TargetInstrInfo *TII = ST.getInstrInfo();
100145449b1SDimitry Andric 
101145449b1SDimitry Andric   // The instruction is cycle invariant if all of its operands are.
102145449b1SDimitry Andric   for (const MachineOperand &MO : I.operands()) {
103145449b1SDimitry Andric     if (!MO.isReg())
104145449b1SDimitry Andric       continue;
105145449b1SDimitry Andric 
106145449b1SDimitry Andric     Register Reg = MO.getReg();
107145449b1SDimitry Andric     if (Reg == 0)
108145449b1SDimitry Andric       continue;
109145449b1SDimitry Andric 
110145449b1SDimitry Andric     // An instruction that uses or defines a physical register can't e.g. be
111145449b1SDimitry Andric     // hoisted, so mark this as not invariant.
112e3b55780SDimitry Andric     if (Reg.isPhysical()) {
113145449b1SDimitry Andric       if (MO.isUse()) {
114145449b1SDimitry Andric         // If the physreg has no defs anywhere, it's just an ambient register
115145449b1SDimitry Andric         // and we can freely move its uses. Alternatively, if it's allocatable,
116145449b1SDimitry Andric         // it could get allocated to something with a def during allocation.
117145449b1SDimitry Andric         // However, if the physreg is known to always be caller saved/restored
118145449b1SDimitry Andric         // then this use is safe to hoist.
119145449b1SDimitry Andric         if (!MRI->isConstantPhysReg(Reg) &&
120145449b1SDimitry Andric             !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
121145449b1SDimitry Andric             !TII->isIgnorableUse(MO))
122145449b1SDimitry Andric           return false;
123145449b1SDimitry Andric         // Otherwise it's safe to move.
124145449b1SDimitry Andric         continue;
125145449b1SDimitry Andric       } else if (!MO.isDead()) {
126145449b1SDimitry Andric         // A def that isn't dead can't be moved.
127145449b1SDimitry Andric         return false;
128145449b1SDimitry Andric       } else if (any_of(Cycle->getEntries(),
129145449b1SDimitry Andric                         [&](const MachineBasicBlock *Block) {
130145449b1SDimitry Andric                           return Block->isLiveIn(Reg);
131145449b1SDimitry Andric                         })) {
132145449b1SDimitry Andric         // If the reg is live into any header of the cycle we can't hoist an
133145449b1SDimitry Andric         // instruction which would clobber it.
134145449b1SDimitry Andric         return false;
135145449b1SDimitry Andric       }
136145449b1SDimitry Andric     }
137145449b1SDimitry Andric 
138145449b1SDimitry Andric     if (!MO.isUse())
139145449b1SDimitry Andric       continue;
140145449b1SDimitry Andric 
141145449b1SDimitry Andric     assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
142145449b1SDimitry Andric 
143145449b1SDimitry Andric     // If the cycle contains the definition of an operand, then the instruction
144145449b1SDimitry Andric     // isn't cycle invariant.
145145449b1SDimitry Andric     if (Cycle->contains(MRI->getVRegDef(Reg)->getParent()))
146145449b1SDimitry Andric       return false;
147145449b1SDimitry Andric   }
148145449b1SDimitry Andric 
149145449b1SDimitry Andric   // If we got this far, the instruction is cycle invariant!
150145449b1SDimitry Andric   return true;
151145449b1SDimitry Andric }
152