LLVM 22.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"
16
17namespace llvm {
18
19class raw_ostream;
20class Value;
21
22namespace safestack {
23
24/// Compute the layout of an unsafe stack frame.
26 Align MaxAlignment;
27
28 struct StackRegion {
29 unsigned Start;
30 unsigned End;
32
33 StackRegion(unsigned Start, unsigned End,
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;
44 Align Alignment;
46 };
47
48 SmallVector<StackObject, 8> StackObjects;
49
51 DenseMap<const Value *, Align> ObjectAlignments;
52
53 void layoutObject(StackObject &Obj);
54
55public:
56 StackLayout(Align StackAlignment) : MaxAlignment(StackAlignment) {}
57
58 /// Add an object to the stack frame. Value pointer is opaque and used as a
59 /// handle to retrieve the object's offset in the frame later.
60 void addObject(const Value *V, unsigned Size, Align Alignment,
62
63 /// Run the layout computation for all previously added objects.
64 void computeLayout();
65
66 /// Returns the offset to the object start in the stack frame.
67 unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
68
69 /// Returns the alignment of the object
70 Align getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
71
72 /// Returns the size of the entire frame.
73 unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
74
75 /// Returns the alignment of the frame.
76 Align getFrameAlignment() { return MaxAlignment; }
77
78 void print(raw_ostream &OS);
79};
80
81} // end namespace safestack
82
83} // end namespace llvm
84
85#endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
This file defines the DenseMap class.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
This file defines the SmallVector class.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class represents a set of interesting instructions where an alloca is live.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned getFrameSize()
Returns the size of the entire frame.
void computeLayout()
Run the layout computation for all previously added objects.
Align getObjectAlignment(const Value *V)
Returns the alignment of the object.
unsigned getObjectOffset(const Value *V)
Returns the offset to the object start in the stack frame.
Align getFrameAlignment()
Returns the alignment of the frame.
void print(raw_ostream &OS)
StackLayout(Align StackAlignment)
void addObject(const Value *V, unsigned Size, Align Alignment, const StackLifetime::LiveRange &Range)
Add an object to the stack frame.
This is an optimization pass for GlobalISel generic memory operations.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39