RegisterUsageInfo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "llvm/CodeGen/RegisterUsageInfo.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/CodeGen/MachineOperand.h"
  16. #include "llvm/CodeGen/TargetRegisterInfo.h"
  17. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. #include <algorithm>
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <utility>
  28. #include <vector>
  29. using namespace llvm;
  30. static cl::opt<bool> DumpRegUsage(
  31. "print-regusage", cl::init(false), cl::Hidden,
  32. cl::desc("print register usage details collected for analysis."));
  33. INITIALIZE_PASS(PhysicalRegisterUsageInfo, "reg-usage-info",
  34. "Register Usage Information Storage", false, true)
  35. char PhysicalRegisterUsageInfo::ID = 0;
  36. void PhysicalRegisterUsageInfo::setTargetMachine(const LLVMTargetMachine &TM) {
  37. this->TM = &TM;
  38. }
  39. bool PhysicalRegisterUsageInfo::doInitialization(Module &M) {
  40. RegMasks.grow(M.size());
  41. return false;
  42. }
  43. bool PhysicalRegisterUsageInfo::doFinalization(Module &M) {
  44. if (DumpRegUsage)
  45. print(errs());
  46. RegMasks.shrink_and_clear();
  47. return false;
  48. }
  49. void PhysicalRegisterUsageInfo::storeUpdateRegUsageInfo(
  50. const Function &FP, ArrayRef<uint32_t> RegMask) {
  51. RegMasks[&FP] = RegMask;
  52. }
  53. ArrayRef<uint32_t>
  54. PhysicalRegisterUsageInfo::getRegUsageInfo(const Function &FP) {
  55. auto It = RegMasks.find(&FP);
  56. if (It != RegMasks.end())
  57. return makeArrayRef<uint32_t>(It->second);
  58. return ArrayRef<uint32_t>();
  59. }
  60. void PhysicalRegisterUsageInfo::print(raw_ostream &OS, const Module *M) const {
  61. using FuncPtrRegMaskPair = std::pair<const Function *, std::vector<uint32_t>>;
  62. SmallVector<const FuncPtrRegMaskPair *, 64> FPRMPairVector;
  63. // Create a vector of pointer to RegMasks entries
  64. for (const auto &RegMask : RegMasks)
  65. FPRMPairVector.push_back(&RegMask);
  66. // sort the vector to print analysis in alphabatic order of function name.
  67. llvm::sort(
  68. FPRMPairVector,
  69. [](const FuncPtrRegMaskPair *A, const FuncPtrRegMaskPair *B) -> bool {
  70. return A->first->getName() < B->first->getName();
  71. });
  72. for (const FuncPtrRegMaskPair *FPRMPair : FPRMPairVector) {
  73. OS << FPRMPair->first->getName() << " "
  74. << "Clobbered Registers: ";
  75. const TargetRegisterInfo *TRI
  76. = TM->getSubtarget<TargetSubtargetInfo>(*(FPRMPair->first))
  77. .getRegisterInfo();
  78. for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
  79. if (MachineOperand::clobbersPhysReg(&(FPRMPair->second[0]), PReg))
  80. OS << printReg(PReg, TRI) << " ";
  81. }
  82. OS << "\n";
  83. }
  84. }