Revert " [LoongArch][ISel] Check the number of sign bits in `PatGprGpr_32` (#107432)"
[llvm-project.git] / llvm / lib / CodeGen / SafeStackLayout.h
blob8614c72f305009b26907ad4cc48d91273d476aab
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"
15 #include "llvm/Support/Alignment.h"
17 namespace llvm {
19 class raw_ostream;
20 class Value;
22 namespace safestack {
24 /// Compute the layout of an unsafe stack frame.
25 class StackLayout {
26 Align MaxAlignment;
28 struct StackRegion {
29 unsigned Start;
30 unsigned End;
31 StackLifetime::LiveRange Range;
33 StackRegion(unsigned Start, unsigned End,
34 const StackLifetime::LiveRange &Range)
35 : Start(Start), End(End), Range(Range) {}
38 /// The list of current stack regions, sorted by StackRegion::Start.
39 SmallVector<StackRegion, 16> Regions;
41 struct StackObject {
42 const Value *Handle;
43 unsigned Size;
44 Align Alignment;
45 StackLifetime::LiveRange Range;
48 SmallVector<StackObject, 8> StackObjects;
50 DenseMap<const Value *, unsigned> ObjectOffsets;
51 DenseMap<const Value *, Align> ObjectAlignments;
53 void layoutObject(StackObject &Obj);
55 public:
56 StackLayout(Align StackAlignment) : MaxAlignment(StackAlignment) {}
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,
61 const StackLifetime::LiveRange &Range);
63 /// Run the layout computation for all previously added objects.
64 void computeLayout();
66 /// Returns the offset to the object start in the stack frame.
67 unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
69 /// Returns the alignment of the object
70 Align getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
72 /// Returns the size of the entire frame.
73 unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
75 /// Returns the alignment of the frame.
76 Align getFrameAlignment() { return MaxAlignment; }
78 void print(raw_ostream &OS);
81 } // end namespace safestack
83 } // end namespace llvm
85 #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H