| 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 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | template class LLVM_ABI_FOR_TEST |
| 21 | llvm::GenericCycleInfo<llvm::MachineSSAContext>; |
| 22 | |
| 23 | char MachineCycleInfoWrapperPass::ID = 0; |
| 24 | |
| 25 | MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass() |
| 26 | : MachineFunctionPass(ID) {} |
| 27 | |
| 28 | INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles" , |
| 29 | "Machine Cycle Info Analysis" , true, true) |
| 30 | INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles" , |
| 31 | "Machine Cycle Info Analysis" , true, true) |
| 32 | |
| 33 | void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 34 | AU.setPreservesAll(); |
| 35 | MachineFunctionPass::getAnalysisUsage(AU); |
| 36 | } |
| 37 | |
| 38 | bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) { |
| 39 | CI.clear(); |
| 40 | |
| 41 | F = &Func; |
| 42 | CI.compute(F&: Func); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 47 | OS << "MachineCycleInfo for function: " << F->getName() << "\n" ; |
| 48 | CI.print(Out&: OS); |
| 49 | } |
| 50 | |
| 51 | void MachineCycleInfoWrapperPass::verifyAnalysis() const { |
| 52 | if (VerifyCycleInfo) |
| 53 | CI.verify(); |
| 54 | } |
| 55 | |
| 56 | void MachineCycleInfoWrapperPass::releaseMemory() { |
| 57 | CI.clear(); |
| 58 | F = nullptr; |
| 59 | } |
| 60 | |
| 61 | AnalysisKey MachineCycleAnalysis::Key; |
| 62 | |
| 63 | MachineCycleInfo |
| 64 | MachineCycleAnalysis::run(MachineFunction &MF, |
| 65 | MachineFunctionAnalysisManager &MFAM) { |
| 66 | MachineCycleInfo MCI; |
| 67 | MCI.compute(F&: MF); |
| 68 | return MCI; |
| 69 | } |
| 70 | |
| 71 | bool 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 | |
| 82 | namespace { |
| 83 | class MachineCycleInfoPrinterLegacy : public MachineFunctionPass { |
| 84 | public: |
| 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 | |
| 94 | char MachineCycleInfoPrinterLegacy::ID = 0; |
| 95 | |
| 96 | MachineCycleInfoPrinterLegacy::MachineCycleInfoPrinterLegacy() |
| 97 | : MachineFunctionPass(ID) {} |
| 98 | |
| 99 | INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterLegacy, "print-machine-cycles" , |
| 100 | "Print Machine Cycle Info Analysis" , true, true) |
| 101 | INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass) |
| 102 | INITIALIZE_PASS_END(MachineCycleInfoPrinterLegacy, "print-machine-cycles" , |
| 103 | "Print Machine Cycle Info Analysis" , true, true) |
| 104 | |
| 105 | void MachineCycleInfoPrinterLegacy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 106 | AU.setPreservesAll(); |
| 107 | AU.addRequired<MachineCycleInfoWrapperPass>(); |
| 108 | MachineFunctionPass::getAnalysisUsage(AU); |
| 109 | } |
| 110 | |
| 111 | bool MachineCycleInfoPrinterLegacy::runOnMachineFunction(MachineFunction &F) { |
| 112 | auto &CI = getAnalysis<MachineCycleInfoWrapperPass>(); |
| 113 | CI.print(OS&: errs()); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | PreservedAnalyses |
| 118 | MachineCycleInfoPrinterPass::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 | |
| 125 | PreservedAnalyses |
| 126 | MachineCycleInfoVerifierPass::run(MachineFunction &MF, |
| 127 | MachineFunctionAnalysisManager &MFAM) { |
| 128 | MFAM.getResult<MachineCycleAnalysis>(IR&: MF).verify(); |
| 129 | return PreservedAnalyses::all(); |
| 130 | } |
| 131 | |
| 132 | bool 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 | |