RegisterUsageInfo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===- RegisterUsageInfo.cpp - Register Usage Informartion Storage --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// This pass is required to take advantage of the interprocedural register
  11. /// allocation infrastructure.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/CodeGen/RegisterUsageInfo.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/Target/TargetMachine.h"
  23. #include "llvm/Target/TargetRegisterInfo.h"
  24. #include "llvm/Target/TargetSubtargetInfo.h"
  25. #include <algorithm>
  26. #include <cassert>
  27. #include <cstdint>
  28. #include <utility>
  29. #include <vector>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "ip-regalloc"
  32. static cl::opt<bool> DumpRegUsage(
  33. "print-regusage", cl::init(false), cl::Hidden,
  34. cl::desc("print register usage details collected for analysis."));
  35. INITIALIZE_PASS(PhysicalRegisterUsageInfo, "reg-usage-info",
  36. "Register Usage Informartion Stroage", false, true)
  37. char PhysicalRegisterUsageInfo::ID = 0;
  38. void PhysicalRegisterUsageInfo::anchor() {}
  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, std::vector<uint32_t> RegMask) {
  51. assert(FP != nullptr && "Function * can't be nullptr.");
  52. RegMasks[FP] = std::move(RegMask);
  53. }
  54. const std::vector<uint32_t> *
  55. PhysicalRegisterUsageInfo::getRegUsageInfo(const Function *FP) {
  56. auto It = RegMasks.find(FP);
  57. if (It != RegMasks.end())
  58. return &(It->second);
  59. return nullptr;
  60. }
  61. void PhysicalRegisterUsageInfo::print(raw_ostream &OS, const Module *M) const {
  62. const TargetRegisterInfo *TRI;
  63. using FuncPtrRegMaskPair = std::pair<const Function *, std::vector<uint32_t>>;
  64. SmallVector<const FuncPtrRegMaskPair *, 64> FPRMPairVector;
  65. // Create a vector of pointer to RegMasks entries
  66. for (const auto &RegMask : RegMasks)
  67. FPRMPairVector.push_back(&RegMask);
  68. // sort the vector to print analysis in alphabatic order of function name.
  69. std::sort(
  70. FPRMPairVector.begin(), FPRMPairVector.end(),
  71. [](const FuncPtrRegMaskPair *A, const FuncPtrRegMaskPair *B) -> bool {
  72. return A->first->getName() < B->first->getName();
  73. });
  74. for (const FuncPtrRegMaskPair *FPRMPair : FPRMPairVector) {
  75. OS << FPRMPair->first->getName() << " "
  76. << "Clobbered Registers: ";
  77. TRI = TM->getSubtarget<TargetSubtargetInfo>(*(FPRMPair->first))
  78. .getRegisterInfo();
  79. for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
  80. if (MachineOperand::clobbersPhysReg(&(FPRMPair->second[0]), PReg))
  81. OS << TRI->getName(PReg) << " ";
  82. }
  83. OS << "\n";
  84. }
  85. }