LiveStacks.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Pass.h"
  25. #include <cassert>
  26. #include <map>
  27. #include <unordered_map>
  28. namespace llvm {
  29. class TargetRegisterClass;
  30. class TargetRegisterInfo;
  31. class LiveStacks : public MachineFunctionPass {
  32. const TargetRegisterInfo *TRI;
  33. /// Special pool allocator for VNInfo's (LiveInterval val#).
  34. ///
  35. VNInfo::Allocator VNInfoAllocator;
  36. /// S2IMap - Stack slot indices to live interval mapping.
  37. using SS2IntervalMap = std::unordered_map<int, LiveInterval>;
  38. SS2IntervalMap S2IMap;
  39. /// S2RCMap - Stack slot indices to register class mapping.
  40. std::map<int, const TargetRegisterClass *> S2RCMap;
  41. public:
  42. static char ID; // Pass identification, replacement for typeid
  43. LiveStacks() : MachineFunctionPass(ID) {
  44. initializeLiveStacksPass(*PassRegistry::getPassRegistry());
  45. }
  46. using iterator = SS2IntervalMap::iterator;
  47. using const_iterator = SS2IntervalMap::const_iterator;
  48. const_iterator begin() const { return S2IMap.begin(); }
  49. const_iterator end() const { return S2IMap.end(); }
  50. iterator begin() { return S2IMap.begin(); }
  51. iterator end() { return S2IMap.end(); }
  52. unsigned getNumIntervals() const { return (unsigned)S2IMap.size(); }
  53. LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC);
  54. LiveInterval &getInterval(int Slot) {
  55. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  56. SS2IntervalMap::iterator I = S2IMap.find(Slot);
  57. assert(I != S2IMap.end() && "Interval does not exist for stack slot");
  58. return I->second;
  59. }
  60. const LiveInterval &getInterval(int Slot) const {
  61. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  62. SS2IntervalMap::const_iterator I = S2IMap.find(Slot);
  63. assert(I != S2IMap.end() && "Interval does not exist for stack slot");
  64. return I->second;
  65. }
  66. bool hasInterval(int Slot) const { return S2IMap.count(Slot); }
  67. const TargetRegisterClass *getIntervalRegClass(int Slot) const {
  68. assert(Slot >= 0 && "Spill slot indice must be >= 0");
  69. std::map<int, const TargetRegisterClass *>::const_iterator I =
  70. S2RCMap.find(Slot);
  71. assert(I != S2RCMap.end() &&
  72. "Register class info does not exist for stack slot");
  73. return I->second;
  74. }
  75. VNInfo::Allocator &getVNInfoAllocator() { return VNInfoAllocator; }
  76. void getAnalysisUsage(AnalysisUsage &AU) const override;
  77. void releaseMemory() override;
  78. /// runOnMachineFunction - pass entry point
  79. bool runOnMachineFunction(MachineFunction &) override;
  80. /// print - Implement the dump method.
  81. void print(raw_ostream &O, const Module * = nullptr) const override;
  82. };
  83. } // end namespace llvm
  84. #endif
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif