[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / CodeGen / SafeStackLayout.h
blob6126c7a6785472df00c0c6177a155daeb9e4483a
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
10 #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/Analysis/StackLifetime.h"
16 namespace llvm {
18 class raw_ostream;
19 class Value;
21 namespace safestack {
23 /// Compute the layout of an unsafe stack frame.
24 class StackLayout {
25 Align MaxAlignment;
27 struct StackRegion {
28 unsigned Start;
29 unsigned End;
30 StackLifetime::LiveRange Range;
32 StackRegion(unsigned Start, unsigned End,
33 const StackLifetime::LiveRange &Range)
34 : Start(Start), End(End), Range(Range) {}
37 /// The list of current stack regions, sorted by StackRegion::Start.
38 SmallVector<StackRegion, 16> Regions;
40 struct StackObject {
41 const Value *Handle;
42 unsigned Size;
43 Align Alignment;
44 StackLifetime::LiveRange Range;
47 SmallVector<StackObject, 8> StackObjects;
49 DenseMap<const Value *, unsigned> ObjectOffsets;
50 DenseMap<const Value *, Align> ObjectAlignments;
52 void layoutObject(StackObject &Obj);
54 public:
55 StackLayout(Align StackAlignment) : MaxAlignment(StackAlignment) {}
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, Align Alignment,
60 const StackLifetime::LiveRange &Range);
62 /// Run the layout computation for all previously added objects.
63 void computeLayout();
65 /// Returns the offset to the object start in the stack frame.
66 unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
68 /// Returns the alignment of the object
69 Align getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
71 /// Returns the size of the entire frame.
72 unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
74 /// Returns the alignment of the frame.
75 Align getFrameAlignment() { return MaxAlignment; }
77 void print(raw_ostream &OS);
80 } // end namespace safestack
82 } // end namespace llvm
84 #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H