SafeStackLayout.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "SafeStackColoring.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/SmallVector.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. unsigned MaxAlignment;
  20. struct StackRegion {
  21. unsigned Start;
  22. unsigned End;
  23. StackColoring::LiveRange Range;
  24. StackRegion(unsigned Start, unsigned End,
  25. const StackColoring::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, Alignment;
  33. StackColoring::LiveRange Range;
  34. };
  35. SmallVector<StackObject, 8> StackObjects;
  36. DenseMap<const Value *, unsigned> ObjectOffsets;
  37. DenseMap<const Value *, unsigned> ObjectAlignments;
  38. void layoutObject(StackObject &Obj);
  39. public:
  40. StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
  41. /// Add an object to the stack frame. Value pointer is opaque and used as a
  42. /// handle to retrieve the object's offset in the frame later.
  43. void addObject(const Value *V, unsigned Size, unsigned Alignment,
  44. const StackColoring::LiveRange &Range);
  45. /// Run the layout computation for all previously added objects.
  46. void computeLayout();
  47. /// Returns the offset to the object start in the stack frame.
  48. unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
  49. /// Returns the alignment of the object
  50. unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
  51. /// Returns the size of the entire frame.
  52. unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
  53. /// Returns the alignment of the frame.
  54. unsigned getFrameAlignment() { return MaxAlignment; }
  55. void print(raw_ostream &OS);
  56. };
  57. } // end namespace safestack
  58. } // end namespace llvm
  59. #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H