LLVM  14.0.0git
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 "llvm/ADT/DenseMap.h"
13 #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  uint64_t MaxAlignment;
26 
27  struct StackRegion {
28  unsigned Start;
29  unsigned End;
31 
32  StackRegion(unsigned Start, unsigned End,
33  const StackLifetime::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;
43  uint64_t Alignment;
45  };
46 
47  SmallVector<StackObject, 8> StackObjects;
48 
50  DenseMap<const Value *, uint64_t> ObjectAlignments;
51 
52  void layoutObject(StackObject &Obj);
53 
54 public:
55  StackLayout(uint64_t 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, uint64_t Alignment,
60  const StackLifetime::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  uint64_t 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  uint64_t 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
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::safestack::StackLayout::getObjectAlignment
uint64_t getObjectAlignment(const Value *V)
Returns the alignment of the object.
Definition: SafeStackLayout.h:69
llvm::SmallVector< StackRegion, 16 >
DenseMap.h
llvm::safestack::StackLayout::addObject
void addObject(const Value *V, unsigned Size, uint64_t Alignment, const StackLifetime::LiveRange &Range)
Add an object to the stack frame.
Definition: SafeStackLayout.cpp:40
llvm::safestack::StackLayout::getFrameAlignment
uint64_t getFrameAlignment()
Returns the alignment of the frame.
Definition: SafeStackLayout.h:75
llvm::safestack::StackLayout::computeLayout
void computeLayout()
Run the layout computation for all previously added objects.
Definition: SafeStackLayout.cpp:135
llvm::safestack::StackLayout
Compute the layout of an unsafe stack frame.
Definition: SafeStackLayout.h:24
llvm::safestack::StackLayout::getObjectOffset
unsigned getObjectOffset(const Value *V)
Returns the offset to the object start in the stack frame.
Definition: SafeStackLayout.h:66
StackLifetime.h
llvm::safestack::StackLayout::getFrameSize
unsigned getFrameSize()
Returns the size of the entire frame.
Definition: SafeStackLayout.h:72
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
uint64_t
llvm::DenseMap< const Value *, unsigned >
llvm::StackLifetime::LiveRange
This class represents a set of interesting instructions where an alloca is live.
Definition: StackLifetime.h:64
llvm::safestack::StackLayout::StackLayout
StackLayout(uint64_t StackAlignment)
Definition: SafeStackLayout.h:55
llvm::safestack::StackLayout::print
void print(raw_ostream &OS)
Definition: SafeStackLayout.cpp:28
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
SmallVector.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75