LLVM: lib/CodeGen/SafeStackLayout.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
15#include
16#include
17
18using namespace llvm;
20
21#define DEBUG_TYPE "safestacklayout"
22
26
28 OS << "Stack regions:\n";
29 for (unsigned i = 0; i < Regions.size(); ++i) {
30 OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
31 << "), range " << Regions[i].Range << "\n";
32 }
33 OS << "Stack objects:\n";
34 for (auto &IT : ObjectOffsets) {
35 OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
36 }
37}
38
41 StackObjects.push_back({V, Size, Alignment, Range});
42 ObjectAlignments[V] = Alignment;
43 MaxAlignment = std::max(MaxAlignment, Alignment);
44}
45
47 Align Alignment) {
49}
50
51void StackLayout::layoutObject(StackObject &Obj) {
53
54
55 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
57 unsigned End = Start + Obj.Size;
58 Regions.emplace_back(Start, End, Obj.Range);
59 ObjectOffsets[Obj.Handle] = End;
60 return;
61 }
62
64 << Obj.Alignment.value() << ", range " << Obj.Range
65 << "\n");
66 assert(Obj.Alignment <= MaxAlignment);
68 unsigned End = Start + Obj.Size;
69 LLVM_DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n");
70 for (const StackRegion &R : Regions) {
71 LLVM_DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.End
72 << ", range " << R.Range << "\n");
74 if (Start >= R.End) {
76 continue;
77 }
78 if (Obj.Range.overlaps(R.Range)) {
79
82 LLVM_DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. "
83 << End << "\n");
84 continue;
85 }
86 if (End <= R.End) {
88 break;
89 }
90 }
91
92 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
93 if (End > LastRegionEnd) {
94
95 if (Start > LastRegionEnd) {
96 LLVM_DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. "
97 << Start << "\n");
98 Regions.emplace_back(LastRegionEnd, Start, StackLifetime::LiveRange(0));
99 LastRegionEnd = Start;
100 }
101 LLVM_DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. "
102 << End << ", range " << Obj.Range << "\n");
103 Regions.emplace_back(LastRegionEnd, End, Obj.Range);
104 LastRegionEnd = End;
105 }
106
107
108 for (unsigned i = 0; i < Regions.size(); ++i) {
109 StackRegion &R = Regions[i];
110 if (Start > R.Start && Start < R.End) {
111 StackRegion R0 = R;
113 Regions.insert(&R, R0);
114 continue;
115 }
116 if (End > R.Start && End < R.End) {
117 StackRegion R0 = R;
118 R0.End = R.Start = End;
119 Regions.insert(&R, R0);
120 break;
121 }
122 }
123
124
125 for (StackRegion &R : Regions) {
126 if (Start < R.End && End > R.Start)
128 if (End <= R.End)
129 break;
130 }
131
132 ObjectOffsets[Obj.Handle] = End;
133}
134
136
137
138
139
140
141
142 if (StackObjects.size() > 2)
144 [](const StackObject &a, const StackObject &b) {
145 return a.Size > b.Size;
146 });
147
148 for (auto &Obj : StackObjects)
149 layoutObject(Obj);
150
152}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AdjustStackOffset - Helper function used to adjust the stack frame offset.
static cl::opt< bool > ClLayout("safe-stack-layout", cl::desc("enable safe stack layout"), cl::Hidden, cl::init(true))
This class represents a set of interesting instructions where an alloca is live.
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
void computeLayout()
Run the layout computation for all previously added objects.
Definition SafeStackLayout.cpp:135
void print(raw_ostream &OS)
Definition SafeStackLayout.cpp:27
void addObject(const Value *V, unsigned Size, Align Alignment, const StackLifetime::LiveRange &Range)
Add an object to the stack frame.
Definition SafeStackLayout.cpp:39
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
void stable_sort(R &&Range)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
This struct is a compact representation of a valid (non-zero power of two) alignment.