| 1 | //===- RegisterUsageInfo.cpp - Register Usage Information Storage ---------===// |
| 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 | /// This pass is required to take advantage of the interprocedural register |
| 10 | /// allocation infrastructure. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/RegisterUsageInfo.h" |
| 15 | #include "CodeGenOptions.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/CodeGen/MachineOperand.h" |
| 18 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 19 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 20 | #include "llvm/IR/Analysis.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/PassManager.h" |
| 24 | #include "llvm/InitializePasses.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include <cstdint> |
| 29 | #include <vector> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | INITIALIZE_PASS(PhysicalRegisterUsageInfoWrapperLegacy, "reg-usage-info" , |
| 34 | "Register Usage Information Storage" , false, true) |
| 35 | |
| 36 | char PhysicalRegisterUsageInfoWrapperLegacy::ID = 0; |
| 37 | |
| 38 | void PhysicalRegisterUsageInfo::setTargetMachine(const TargetMachine &TM) { |
| 39 | this->TM = &TM; |
| 40 | } |
| 41 | |
| 42 | bool PhysicalRegisterUsageInfo::doInitialization(Module &M) { |
| 43 | RegMasks.reserve(NumEntries: M.size()); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | bool PhysicalRegisterUsageInfo::doFinalization(Module &M) { |
| 48 | if (getCodeGenOptions(M).print_regusage) |
| 49 | print(OS&: errs()); |
| 50 | |
| 51 | RegMasks.shrink_and_clear(); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | void PhysicalRegisterUsageInfo::storeUpdateRegUsageInfo( |
| 56 | const Function &FP, ArrayRef<uint32_t> RegMask) { |
| 57 | RegMasks[&FP] = RegMask; |
| 58 | } |
| 59 | |
| 60 | ArrayRef<uint32_t> |
| 61 | PhysicalRegisterUsageInfo::getRegUsageInfo(const Function &FP) { |
| 62 | auto It = RegMasks.find(Val: &FP); |
| 63 | if (It != RegMasks.end()) |
| 64 | return ArrayRef<uint32_t>(It->second); |
| 65 | return ArrayRef<uint32_t>(); |
| 66 | } |
| 67 | |
| 68 | void PhysicalRegisterUsageInfo::print(raw_ostream &OS, const Module *M) const { |
| 69 | using FuncPtrRegMaskPair = decltype(RegMasks)::value_type; |
| 70 | |
| 71 | // Create a vector of pointer to RegMasks entries |
| 72 | SmallVector<const FuncPtrRegMaskPair *, 64> FPRMPairVector( |
| 73 | llvm::make_pointer_range(Range: RegMasks)); |
| 74 | |
| 75 | // sort the vector to print analysis in alphabatic order of function name. |
| 76 | llvm::sort( |
| 77 | C&: FPRMPairVector, |
| 78 | Comp: [](const FuncPtrRegMaskPair *A, const FuncPtrRegMaskPair *B) -> bool { |
| 79 | return A->first->getName() < B->first->getName(); |
| 80 | }); |
| 81 | |
| 82 | for (const FuncPtrRegMaskPair *FPRMPair : FPRMPairVector) { |
| 83 | OS << FPRMPair->first->getName() << " " |
| 84 | << "Clobbered Registers: " ; |
| 85 | const TargetRegisterInfo *TRI |
| 86 | = TM->getSubtarget<TargetSubtargetInfo>(F: *(FPRMPair->first)) |
| 87 | .getRegisterInfo(); |
| 88 | |
| 89 | for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) { |
| 90 | if (MachineOperand::clobbersPhysReg(RegMask: &(FPRMPair->second[0]), PhysReg: PReg)) |
| 91 | OS << printReg(Reg: PReg, TRI) << " " ; |
| 92 | } |
| 93 | OS << "\n" ; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool PhysicalRegisterUsageInfo::invalidate( |
| 98 | Module &M, const PreservedAnalyses &PA, |
| 99 | ModuleAnalysisManager::Invalidator &) { |
| 100 | auto PAC = PA.getChecker<PhysicalRegisterUsageAnalysis>(); |
| 101 | return !PAC.preservedWhenStateless(); |
| 102 | } |
| 103 | |
| 104 | AnalysisKey PhysicalRegisterUsageAnalysis::Key; |
| 105 | PhysicalRegisterUsageInfo |
| 106 | PhysicalRegisterUsageAnalysis::run(Module &M, ModuleAnalysisManager &) { |
| 107 | PhysicalRegisterUsageInfo PRUI; |
| 108 | PRUI.doInitialization(M); |
| 109 | return PRUI; |
| 110 | } |
| 111 | |
| 112 | PreservedAnalyses |
| 113 | PhysicalRegisterUsageInfoPrinterPass::run(Module &M, |
| 114 | ModuleAnalysisManager &AM) { |
| 115 | auto *PRUI = &AM.getResult<PhysicalRegisterUsageAnalysis>(IR&: M); |
| 116 | PRUI->print(OS, M: &M); |
| 117 | return PreservedAnalyses::all(); |
| 118 | } |
| 119 | |