blob: 349d9a8b595c41ddccdc0546c7ccd04b77a4d2c4 [file] [log] [blame]
Eugene Zelenko149178d2017-10-10 22:33:29 +00001//===- SafeStackLayout.h - SafeStack frame layout --------------*- C++ -*--===//
Evgeniy Stepanova5da2562016-06-29 20:37:43 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Evgeniy Stepanova5da2562016-06-29 20:37:43 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
10#define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
11
12#include "SafeStackColoring.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000013#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/SmallVector.h"
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000015
16namespace llvm {
Eugene Zelenko149178d2017-10-10 22:33:29 +000017
18class raw_ostream;
19class Value;
20
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000021namespace safestack {
22
23/// Compute the layout of an unsafe stack frame.
24class StackLayout {
25 unsigned MaxAlignment;
26
27 struct StackRegion {
28 unsigned Start;
29 unsigned End;
30 StackColoring::LiveRange Range;
Eugene Zelenko149178d2017-10-10 22:33:29 +000031
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000032 StackRegion(unsigned Start, unsigned End,
33 const StackColoring::LiveRange &Range)
34 : Start(Start), End(End), Range(Range) {}
35 };
Eugene Zelenko149178d2017-10-10 22:33:29 +000036
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000037 /// The list of current stack regions, sorted by StackRegion::Start.
38 SmallVector<StackRegion, 16> Regions;
39
40 struct StackObject {
41 const Value *Handle;
42 unsigned Size, Alignment;
43 StackColoring::LiveRange Range;
44 };
Eugene Zelenko149178d2017-10-10 22:33:29 +000045
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000046 SmallVector<StackObject, 8> StackObjects;
47
48 DenseMap<const Value *, unsigned> ObjectOffsets;
Daniel Neilson095d7292018-02-12 22:39:47 +000049 DenseMap<const Value *, unsigned> ObjectAlignments;
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000050
51 void layoutObject(StackObject &Obj);
52
53public:
54 StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
Eugene Zelenko149178d2017-10-10 22:33:29 +000055
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000056 /// 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
Daniel Neilson095d7292018-02-12 22:39:47 +000067 /// Returns the alignment of the object
68 unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
69
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000070 /// 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; }
Eugene Zelenko149178d2017-10-10 22:33:29 +000075
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000076 void print(raw_ostream &OS);
77};
78
Eugene Zelenko149178d2017-10-10 22:33:29 +000079} // end namespace safestack
80
81} // end namespace llvm
Evgeniy Stepanova5da2562016-06-29 20:37:43 +000082
83#endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H