SafeStackLayout.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===- SafeStackLayout.h - SafeStack frame layout --------------*- C++ -*--===//
  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. #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
  9. #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/Analysis/StackLifetime.h"
  13. namespace llvm {
  14. class raw_ostream;
  15. class Value;
  16. namespace safestack {
  17. /// Compute the layout of an unsafe stack frame.
  18. class StackLayout {
  19. Align MaxAlignment;
  20. struct StackRegion {
  21. unsigned Start;
  22. unsigned End;
  23. StackLifetime::LiveRange Range;
  24. StackRegion(unsigned Start, unsigned End,
  25. const StackLifetime::LiveRange &Range)
  26. : Start(Start), End(End), Range(Range) {}
  27. };
  28. /// The list of current stack regions, sorted by StackRegion::Start.
  29. SmallVector<StackRegion, 16> Regions;
  30. struct StackObject {
  31. const Value *Handle;
  32. unsigned Size;
  33. Align Alignment;
  34. StackLifetime::LiveRange Range;
  35. };
  36. SmallVector<StackObject, 8> StackObjects;
  37. DenseMap<const Value *, unsigned> ObjectOffsets;
  38. DenseMap<const Value *, Align> ObjectAlignments;
  39. void layoutObject(StackObject &Obj);
  40. public:
  41. StackLayout(uint64_t StackAlignment) : MaxAlignment(StackAlignment) {}
  42. /// Add an object to the stack frame. Value pointer is opaque and used as a
  43. /// handle to retrieve the object's offset in the frame later.
  44. void addObject(const Value *V, unsigned Size, Align Alignment,
  45. const StackLifetime::LiveRange &Range);
  46. /// Run the layout computation for all previously added objects.
  47. void computeLayout();
  48. /// Returns the offset to the object start in the stack frame.
  49. unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
  50. /// Returns the alignment of the object
  51. Align getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
  52. /// Returns the size of the entire frame.
  53. unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
  54. /// Returns the alignment of the frame.
  55. Align getFrameAlignment() { return MaxAlignment; }
  56. void print(raw_ostream &OS);
  57. };
  58. } // end namespace safestack
  59. } // end namespace llvm
  60. #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H