LiveStacks.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LiveStacks.h - Live Stack Slot Analysis ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements the live stack slot analysis pass. It is analogous to
  15. // live interval analysis except it's analyzing liveness of stack slots rather
  16. // than registers.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_LIVESTACKS_H
  20. #define LLVM_CODEGEN_LIVESTACKS_H
  21. #include "llvm/CodeGen/LiveInterval.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/PassRegistry.h"
  25. #include <cassert>
  26. #include <map>
  27. #include <unordered_map>
  28. namespace llvm {
  29. class AnalysisUsage;
  30. class MachineFunction;
  31. class Module;
  32. class raw_ostream;
  33. class TargetRegisterClass;
  34. class TargetRegisterInfo;
  35. class LiveStacks : public MachineFunctionPass {
  36. const TargetRegisterInfo *TRI;
  37. /// Special pool allocator for VNInfo's (LiveInterval val#).
  38. ///
  39. VNInfo::Allocator VNInfoAllocator;
  40. /// S2IMap - Stack slot indices to live interval mapping.
  41. using SS2IntervalMap = std::unordered_map<int, LiveInterval>;
  42. SS2IntervalMap S2IMap;
  43. /// S2RCMap - Stack slot indices to register class mapping.
  44. std::map<int, const TargetRegisterClass *> S2RCMap;
  45. public:
  46. static char ID; // Pass identification, replacement for typeid
  47. LiveStacks() : MachineFunctionPass(ID) {
  48. initializeLiveStacksPass(*PassRegistry::getPassRegistry());
  49. }
  50. using iterator = SS2IntervalMap::iterator;
  51. using const_iterator = SS2IntervalMap::const_iterator;
  52. const_iterator begin() const { return S2IMap.begin(); }
  53. const_iterator end() const { return S2IMap.end(); }
  54. iterator begin() { return S2IMap.begin(); }
  55. iterator end() { return S2IMap.end(); }
  56. unsigned getNumIntervals() const { return (unsigned)S2IMap.size(); }
  57. LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC);
  58. LiveInterval &getInterval(int Slot) {
  59. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  60. SS2IntervalMap::iterator I = S2IMap.find(Slot);
  61. assert(I != S2IMap.end() && "Interval does not exist for stack slot");
  62. return I->second;
  63. }
  64. const LiveInterval &getInterval(int Slot) const {
  65. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  66. SS2IntervalMap::const_iterator I = S2IMap.find(Slot);
  67. assert(I != S2IMap.end() && "Interval does not exist for stack slot");
  68. return I->second;
  69. }
  70. bool hasInterval(int Slot) const { return S2IMap.count(Slot); }
  71. const TargetRegisterClass *getIntervalRegClass(int Slot) const {
  72. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  73. std::map<int, const TargetRegisterClass *>::const_iterator I =
  74. S2RCMap.find(Slot);
  75. assert(I != S2RCMap.end() &&
  76. "Register class info does not exist for stack slot");
  77. return I->second;
  78. }
  79. VNInfo::Allocator &getVNInfoAllocator() { return VNInfoAllocator; }
  80. void getAnalysisUsage(AnalysisUsage &AU) const override;
  81. void releaseMemory() override;
  82. /// runOnMachineFunction - pass entry point
  83. bool runOnMachineFunction(MachineFunction &) override;
  84. /// print - Implement the dump method.
  85. void print(raw_ostream &O, const Module * = nullptr) const override;
  86. };
  87. } // end namespace llvm
  88. #endif
  89. #ifdef __GNUC__
  90. #pragma GCC diagnostic pop
  91. #endif