forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionLayout.cpp
263 lines (220 loc) · 8.06 KB
/
FunctionLayout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//===- bolt/Core/FunctionLayout.cpp - Fragmented Function Layout -*- C++ -*-==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "bolt/Core/FunctionLayout.h"
#include "bolt/Core/BinaryBasicBlock.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/edit_distance.h"
#include <algorithm>
#include <iterator>
using namespace llvm;
using namespace bolt;
FunctionFragment::FunctionFragment(FunctionLayout &Layout,
const FragmentNum Num)
: Layout(&Layout), Num(Num), StartIndex(Layout.block_size()) {}
FunctionFragment::iterator FunctionFragment::begin() {
return iterator(Layout->block_begin() + StartIndex);
}
FunctionFragment::const_iterator FunctionFragment::begin() const {
return const_iterator(Layout->block_begin() + StartIndex);
}
FunctionFragment::iterator FunctionFragment::end() {
return iterator(Layout->block_begin() + StartIndex + Size);
}
FunctionFragment::const_iterator FunctionFragment::end() const {
return const_iterator(Layout->block_begin() + StartIndex + Size);
}
BinaryBasicBlock *FunctionFragment::front() const { return *begin(); }
BinaryBasicBlock *FunctionFragment::back() const { return *std::prev(end()); }
FunctionLayout::FunctionLayout() { addFragment(); }
FunctionLayout::FunctionLayout(const FunctionLayout &Other)
: Blocks(Other.Blocks) {
for (FunctionFragment *const FF : Other.Fragments) {
auto *Copy = new FunctionFragment(*FF);
Copy->Layout = this;
Fragments.emplace_back(Copy);
}
}
FunctionLayout::FunctionLayout(FunctionLayout &&Other)
: Fragments(std::move(Other.Fragments)), Blocks(std::move(Other.Blocks)) {
for (FunctionFragment *const F : Fragments)
F->Layout = this;
}
FunctionLayout &FunctionLayout::operator=(const FunctionLayout &Other) {
Blocks = Other.Blocks;
for (FunctionFragment *const FF : Other.Fragments) {
auto *const Copy = new FunctionFragment(*FF);
Copy->Layout = this;
Fragments.emplace_back(Copy);
}
return *this;
}
FunctionLayout &FunctionLayout::operator=(FunctionLayout &&Other) {
Fragments = std::move(Other.Fragments);
Blocks = std::move(Other.Blocks);
for (FunctionFragment *const FF : Fragments)
FF->Layout = this;
return *this;
}
FunctionLayout::~FunctionLayout() {
for (FunctionFragment *const F : Fragments) {
delete F;
}
}
FunctionFragment &FunctionLayout::addFragment() {
FunctionFragment *const FF =
new FunctionFragment(*this, FragmentNum(Fragments.size()));
Fragments.emplace_back(FF);
return *FF;
}
FunctionFragment &FunctionLayout::getFragment(FragmentNum Num) {
return *Fragments[Num.get()];
}
const FunctionFragment &FunctionLayout::getFragment(FragmentNum Num) const {
return *Fragments[Num.get()];
}
const FunctionFragment &
FunctionLayout::findFragment(const BinaryBasicBlock *const BB) const {
return getFragment(BB->getFragmentNum());
}
void FunctionLayout::addBasicBlock(BinaryBasicBlock *const BB) {
BB->setLayoutIndex(Blocks.size());
Blocks.emplace_back(BB);
Fragments.back()->Size++;
}
void FunctionLayout::insertBasicBlocks(
const BinaryBasicBlock *const InsertAfter,
const ArrayRef<BinaryBasicBlock *> NewBlocks) {
block_iterator InsertBeforePos = Blocks.begin();
FragmentNum InsertFragmentNum = FragmentNum::main();
unsigned LayoutIndex = 0;
if (InsertAfter) {
InsertBeforePos = std::next(findBasicBlockPos(InsertAfter));
InsertFragmentNum = InsertAfter->getFragmentNum();
LayoutIndex = InsertAfter->getLayoutIndex();
}
llvm::copy(NewBlocks, std::inserter(Blocks, InsertBeforePos));
for (BinaryBasicBlock *const BB : NewBlocks) {
BB->setFragmentNum(InsertFragmentNum);
BB->setLayoutIndex(LayoutIndex++);
}
const fragment_iterator InsertFragment =
fragment_begin() + InsertFragmentNum.get();
InsertFragment->Size += NewBlocks.size();
const fragment_iterator TailBegin = std::next(InsertFragment);
auto const UpdateFragment = [&](FunctionFragment &FF) {
FF.StartIndex += NewBlocks.size();
for (BinaryBasicBlock *const BB : FF)
BB->setLayoutIndex(LayoutIndex++);
};
std::for_each(TailBegin, fragment_end(), UpdateFragment);
}
void FunctionLayout::eraseBasicBlocks(
const DenseSet<const BinaryBasicBlock *> ToErase) {
const auto IsErased = [&](const BinaryBasicBlock *const BB) {
return ToErase.contains(BB);
};
unsigned TotalErased = 0;
for (FunctionFragment &FF : fragments()) {
unsigned Erased = count_if(FF, IsErased);
FF.Size -= Erased;
FF.StartIndex -= TotalErased;
TotalErased += Erased;
}
llvm::erase_if(Blocks, IsErased);
// Remove empty fragments at the end
const auto IsEmpty = [](const FunctionFragment *const FF) {
return FF->empty();
};
const FragmentListType::iterator EmptyTailBegin =
llvm::find_if_not(reverse(Fragments), IsEmpty).base();
for (FunctionFragment *const FF :
llvm::make_range(EmptyTailBegin, Fragments.end()))
delete FF;
Fragments.erase(EmptyTailBegin, Fragments.end());
updateLayoutIndices();
}
void FunctionLayout::updateLayoutIndices() const {
unsigned BlockIndex = 0;
for (const FunctionFragment &FF : fragments()) {
for (BinaryBasicBlock *const BB : FF) {
BB->setLayoutIndex(BlockIndex++);
BB->setFragmentNum(FF.getFragmentNum());
}
}
}
void FunctionLayout::updateLayoutIndices(
ArrayRef<BinaryBasicBlock *> Order) const {
for (auto [Index, BB] : llvm::enumerate(Order))
BB->setLayoutIndex(Index);
}
bool FunctionLayout::update(const ArrayRef<BinaryBasicBlock *> NewLayout) {
const bool EqualBlockOrder = llvm::equal(Blocks, NewLayout);
if (EqualBlockOrder) {
const bool EqualPartitioning =
llvm::all_of(fragments(), [](const FunctionFragment &FF) {
return llvm::all_of(FF, [&](const BinaryBasicBlock *const BB) {
return FF.Num == BB->getFragmentNum();
});
});
if (EqualPartitioning)
return false;
}
clear();
// Generate fragments
for (BinaryBasicBlock *const BB : NewLayout) {
FragmentNum Num = BB->getFragmentNum();
// Add empty fragments if necessary
while (Fragments.back()->getFragmentNum() < Num)
addFragment();
// Set the next fragment to point one past the current BB
addBasicBlock(BB);
}
return true;
}
void FunctionLayout::clear() {
Blocks = BasicBlockListType();
// If the binary does not have relocations and is not split, the function will
// be written to the output stream at its original file offset (see
// `RewriteInstance::rewriteFile`). Hence, when the layout is cleared, retain
// the main fragment, so that this information is not lost.
for (FunctionFragment *const FF : llvm::drop_begin(Fragments))
delete FF;
Fragments = FragmentListType{Fragments.front()};
getMainFragment().Size = 0;
}
const BinaryBasicBlock *
FunctionLayout::getBasicBlockAfter(const BinaryBasicBlock *BB,
bool IgnoreSplits) const {
const block_const_iterator BBPos = find(blocks(), BB);
if (BBPos == block_end())
return nullptr;
const block_const_iterator BlockAfter = std::next(BBPos);
if (BlockAfter == block_end())
return nullptr;
if (!IgnoreSplits)
if (BlockAfter == getFragment(BB->getFragmentNum()).end())
return nullptr;
return *BlockAfter;
}
bool FunctionLayout::isSplit() const {
const unsigned NonEmptyFragCount = llvm::count_if(
fragments(), [](const FunctionFragment &FF) { return !FF.empty(); });
return NonEmptyFragCount >= 2;
}
uint64_t FunctionLayout::getEditDistance(
const ArrayRef<const BinaryBasicBlock *> OldBlockOrder) const {
return ComputeEditDistance<const BinaryBasicBlock *>(OldBlockOrder, Blocks);
}
FunctionLayout::block_const_iterator
FunctionLayout::findBasicBlockPos(const BinaryBasicBlock *BB) const {
return block_const_iterator(find(Blocks, BB));
}
FunctionLayout::block_iterator
FunctionLayout::findBasicBlockPos(const BinaryBasicBlock *BB) {
return find(Blocks, BB);
}