SafeStackLayout.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===- SafeStackLayout.h - SafeStack frame layout --------------*- C++ -*--===//
  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. #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
  10. #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
  11. #include "SafeStackColoring.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. namespace llvm {
  15. class raw_ostream;
  16. class Value;
  17. namespace safestack {
  18. /// Compute the layout of an unsafe stack frame.
  19. class StackLayout {
  20. unsigned MaxAlignment;
  21. struct StackRegion {
  22. unsigned Start;
  23. unsigned End;
  24. StackColoring::LiveRange Range;
  25. StackRegion(unsigned Start, unsigned End,
  26. const StackColoring::LiveRange &Range)
  27. : Start(Start), End(End), Range(Range) {}
  28. };
  29. /// The list of current stack regions, sorted by StackRegion::Start.
  30. SmallVector<StackRegion, 16> Regions;
  31. struct StackObject {
  32. const Value *Handle;
  33. unsigned Size, Alignment;
  34. StackColoring::LiveRange Range;
  35. };
  36. SmallVector<StackObject, 8> StackObjects;
  37. DenseMap<const Value *, unsigned> ObjectOffsets;
  38. DenseMap<const Value *, unsigned> ObjectAlignments;
  39. void layoutObject(StackObject &Obj);
  40. public:
  41. StackLayout(unsigned 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, unsigned Alignment,
  45. const StackColoring::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. unsigned 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. unsigned 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