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