LLVM  10.0.0svn
SafeStackLayout.h
Go to the documentation of this file.
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 
9 #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
10 #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
11 
12 #include "SafeStackColoring.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallVector.h"
15 
16 namespace llvm {
17 
18 class raw_ostream;
19 class Value;
20 
21 namespace safestack {
22 
23 /// Compute the layout of an unsafe stack frame.
24 class StackLayout {
25  unsigned MaxAlignment;
26 
27  struct StackRegion {
28  unsigned Start;
29  unsigned End;
31 
32  StackRegion(unsigned Start, unsigned End,
33  const StackColoring::LiveRange &Range)
34  : Start(Start), End(End), Range(Range) {}
35  };
36 
37  /// The list of current stack regions, sorted by StackRegion::Start.
39 
40  struct StackObject {
41  const Value *Handle;
42  unsigned Size, Alignment;
44  };
45 
46  SmallVector<StackObject, 8> StackObjects;
47 
49  DenseMap<const Value *, unsigned> ObjectAlignments;
50 
51  void layoutObject(StackObject &Obj);
52 
53 public:
54  StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
55 
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 alignment of the object
68  unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
69 
70  /// Returns the size of the entire frame.
71  unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
72 
73  /// Returns the alignment of the frame.
74  unsigned getFrameAlignment() { return MaxAlignment; }
75 
76  void print(raw_ostream &OS);
77 };
78 
79 } // end namespace safestack
80 
81 } // end namespace llvm
82 
83 #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:23
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:55
uint32_t Size
Definition: Profile.cpp:46
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45