LLVM  8.0.1
SafeStackLayout.h
Go to the documentation of this file.
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 
10 #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
11 #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
12 
13 #include "SafeStackColoring.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallVector.h"
16 
17 namespace llvm {
18 
19 class raw_ostream;
20 class Value;
21 
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;
32 
33  StackRegion(unsigned Start, unsigned End,
34  const StackColoring::LiveRange &Range)
35  : Start(Start), End(End), Range(Range) {}
36  };
37 
38  /// The list of current stack regions, sorted by StackRegion::Start.
40 
41  struct StackObject {
42  const Value *Handle;
43  unsigned Size, Alignment;
45  };
46 
47  SmallVector<StackObject, 8> StackObjects;
48 
50  DenseMap<const Value *, unsigned> ObjectAlignments;
51 
52  void layoutObject(StackObject &Obj);
53 
54 public:
55  StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
56 
57  /// Add an object to the stack frame. Value pointer is opaque and used as a
58  /// handle to retrieve the object's offset in the frame later.
59  void addObject(const Value *V, unsigned Size, unsigned Alignment,
60  const StackColoring::LiveRange &Range);
61 
62  /// Run the layout computation for all previously added objects.
63  void computeLayout();
64 
65  /// Returns the offset to the object start in the stack frame.
66  unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
67 
68  /// Returns the alignment of the object
69  unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
70 
71  /// Returns the size of the entire frame.
72  unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
73 
74  /// Returns the alignment of the frame.
75  unsigned getFrameAlignment() { return MaxAlignment; }
76 
77  void print(raw_ostream &OS);
78 };
79 
80 } // end namespace safestack
81 
82 } // end namespace llvm
83 
84 #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
This class represents a set of interesting instructions where an alloca is live.
void addObject(const Value *V, unsigned Size, unsigned Alignment, const StackColoring::LiveRange &Range)
Add an object to the stack frame.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned getFrameSize()
Returns the size of the entire frame.
unsigned getObjectOffset(const Value *V)
Returns the offset to the object start in the stack frame.
StackLayout(unsigned StackAlignment)
Compute the layout of an unsafe stack frame.
unsigned getObjectAlignment(const Value *V)
Returns the alignment of the object.
void computeLayout()
Run the layout computation for all previously added objects.
unsigned getFrameAlignment()
Returns the alignment of the frame.
void print(raw_ostream &OS)
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
uint32_t Size
Definition: Profile.cpp:47
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46