blob: ac531d800f6e7aa89a5af3c986c84f064e3e344a [file] [log] [blame]
Eugene Zelenko887aef72017-10-10 22:33:29 +00001//===- SafeStackLayout.h - SafeStack frame layout --------------*- C++ -*--===//
Evgeniy Stepanov6a405042016-06-29 20:37:43 +00002//
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"
Eugene Zelenko887aef72017-10-10 22:33:29 +000014#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/SmallVector.h"
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000016
17namespace llvm {
Eugene Zelenko887aef72017-10-10 22:33:29 +000018
19class raw_ostream;
20class Value;
21
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000022namespace safestack {
23
24/// Compute the layout of an unsafe stack frame.
25class StackLayout {
26 unsigned MaxAlignment;
27
28 struct StackRegion {
29 unsigned Start;
30 unsigned End;
31 StackColoring::LiveRange Range;
Eugene Zelenko887aef72017-10-10 22:33:29 +000032
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000033 StackRegion(unsigned Start, unsigned End,
34 const StackColoring::LiveRange &Range)
35 : Start(Start), End(End), Range(Range) {}
36 };
Eugene Zelenko887aef72017-10-10 22:33:29 +000037
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000038 /// The list of current stack regions, sorted by StackRegion::Start.
39 SmallVector<StackRegion, 16> Regions;
40
41 struct StackObject {
42 const Value *Handle;
43 unsigned Size, Alignment;
44 StackColoring::LiveRange Range;
45 };
Eugene Zelenko887aef72017-10-10 22:33:29 +000046
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000047 SmallVector<StackObject, 8> StackObjects;
48
49 DenseMap<const Value *, unsigned> ObjectOffsets;
Daniel Neilson5b0a1fc2018-02-12 22:39:47 +000050 DenseMap<const Value *, unsigned> ObjectAlignments;
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000051
52 void layoutObject(StackObject &Obj);
53
54public:
55 StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
Eugene Zelenko887aef72017-10-10 22:33:29 +000056
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000057 /// 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
Daniel Neilson5b0a1fc2018-02-12 22:39:47 +000068 /// Returns the alignment of the object
69 unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
70
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000071 /// 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; }
Eugene Zelenko887aef72017-10-10 22:33:29 +000076
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000077 void print(raw_ostream &OS);
78};
79
Eugene Zelenko887aef72017-10-10 22:33:29 +000080} // end namespace safestack
81
82} // end namespace llvm
Evgeniy Stepanov6a405042016-06-29 20:37:43 +000083
84#endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H