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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
//===- ExpandStructRegs.cpp - Expand out variables with struct type--------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass expands out some uses of LLVM variables
// (a.k.a. registers) of struct type. It replaces loads and stores of
// structs with separate loads and stores of the structs' fields. The
// motivation is to omit struct types from PNaCl's stable ABI.
//
// ExpandStructRegs does not yet handle all possible uses of struct
// values. It is intended to handle the uses that Clang and the SROA
// pass generate. Clang generates struct loads and stores, along with
// extractvalue instructions, in its implementation of C++ method
// pointers, and the SROA pass sometimes converts this code to using
// insertvalue instructions too.
//
// ExpandStructRegs does not handle:
//
// * Nested struct types.
// * Array types.
// * Function types containing arguments or return values of struct
// type without the "byval" or "sret" attributes. Since by-value
// struct-passing generally uses "byval"/"sret", this does not
// matter.
//
// Other limitations:
//
// * ExpandStructRegs does not attempt to use memcpy() where that
// might be more appropriate than copying fields individually.
// * ExpandStructRegs does not preserve the contents of padding
// between fields when copying structs. However, the contents of
// padding fields are not defined anyway.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/NaCl.h"
using namespace llvm;
namespace {
struct ExpandStructRegs : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
ExpandStructRegs() : FunctionPass(ID) {
initializeExpandStructRegsPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnFunction(Function &F);
};
}
char ExpandStructRegs::ID = 0;
INITIALIZE_PASS(ExpandStructRegs, "expand-struct-regs",
"Expand out variables with struct types", false, false)
static void SplitUpPHINode(PHINode *Phi) {
StructType *STy = cast<StructType>(Phi->getType());
Value *NewStruct = UndefValue::get(STy);
Instruction *NewStructInsertPt = Phi->getParent()->getFirstInsertionPt();
// Create a separate PHINode for each struct field.
for (unsigned Index = 0; Index < STy->getNumElements(); ++Index) {
SmallVector<unsigned, 1> EVIndexes;
EVIndexes.push_back(Index);
PHINode *NewPhi = PHINode::Create(
STy->getElementType(Index), Phi->getNumIncomingValues(),
Phi->getName() + ".index", Phi);
CopyDebug(NewPhi, Phi);
for (unsigned PhiIndex = 0; PhiIndex < Phi->getNumIncomingValues();
++PhiIndex) {
BasicBlock *IncomingBB = Phi->getIncomingBlock(PhiIndex);
Value *EV = CopyDebug(
ExtractValueInst::Create(
Phi->getIncomingValue(PhiIndex), EVIndexes,
Phi->getName() + ".extract", IncomingBB->getTerminator()), Phi);
NewPhi->addIncoming(EV, IncomingBB);
}
// Reconstruct the original struct value.
NewStruct = CopyDebug(
InsertValueInst::Create(NewStruct, NewPhi, EVIndexes,
Phi->getName() + ".insert", NewStructInsertPt),
Phi);
}
Phi->replaceAllUsesWith(NewStruct);
Phi->eraseFromParent();
}
static void SplitUpSelect(SelectInst *Select) {
StructType *STy = cast<StructType>(Select->getType());
Value *NewStruct = UndefValue::get(STy);
// Create a separate SelectInst for each struct field.
for (unsigned Index = 0; Index < STy->getNumElements(); ++Index) {
SmallVector<unsigned, 1> EVIndexes;
EVIndexes.push_back(Index);
Value *TrueVal = CopyDebug(
ExtractValueInst::Create(Select->getTrueValue(), EVIndexes,
Select->getName() + ".extract", Select),
Select);
Value *FalseVal = CopyDebug(
ExtractValueInst::Create(Select->getFalseValue(), EVIndexes,
Select->getName() + ".extract", Select),
Select);
Value *NewSelect = CopyDebug(
SelectInst::Create(Select->getCondition(), TrueVal, FalseVal,
Select->getName() + ".index", Select), Select);
// Reconstruct the original struct value.
NewStruct = CopyDebug(
InsertValueInst::Create(NewStruct, NewSelect, EVIndexes,
Select->getName() + ".insert", Select),
Select);
}
Select->replaceAllUsesWith(NewStruct);
Select->eraseFromParent();
}
template <class InstType>
static void ProcessLoadOrStoreAttrs(InstType *Dest, InstType *Src) {
CopyDebug(Dest, Src);
Dest->setVolatile(Src->isVolatile());
if (Src->isAtomic()) {
errs() << "Use: " << *Src << "\n";
report_fatal_error("Atomic struct loads/stores not supported");
}
// Make a pessimistic assumption about alignment. Preserving
// alignment information here is tricky and is not really desirable
// for PNaCl because mistakes here could lead to non-portable
// behaviour.
Dest->setAlignment(1);
}
static void SplitUpStore(StoreInst *Store) {
StructType *STy = cast<StructType>(Store->getValueOperand()->getType());
// Create a separate store instruction for each struct field.
for (unsigned Index = 0; Index < STy->getNumElements(); ++Index) {
SmallVector<Value *, 2> Indexes;
Indexes.push_back(ConstantInt::get(Store->getContext(), APInt(32, 0)));
Indexes.push_back(ConstantInt::get(Store->getContext(), APInt(32, Index)));
Value *GEP = CopyDebug(GetElementPtrInst::Create(
Store->getPointerOperand(), Indexes,
Store->getPointerOperand()->getName() + ".index",
Store), Store);
SmallVector<unsigned, 1> EVIndexes;
EVIndexes.push_back(Index);
Value *Field = ExtractValueInst::Create(Store->getValueOperand(),
EVIndexes, "", Store);
StoreInst *NewStore = new StoreInst(Field, GEP, Store);
ProcessLoadOrStoreAttrs(NewStore, Store);
}
Store->eraseFromParent();
}
static void SplitUpLoad(LoadInst *Load) {
StructType *STy = cast<StructType>(Load->getType());
Value *NewStruct = UndefValue::get(STy);
// Create a separate load instruction for each struct field.
for (unsigned Index = 0; Index < STy->getNumElements(); ++Index) {
SmallVector<Value *, 2> Indexes;
Indexes.push_back(ConstantInt::get(Load->getContext(), APInt(32, 0)));
Indexes.push_back(ConstantInt::get(Load->getContext(), APInt(32, Index)));
Value *GEP = CopyDebug(
GetElementPtrInst::Create(Load->getPointerOperand(), Indexes,
Load->getName() + ".index", Load), Load);
LoadInst *NewLoad = new LoadInst(GEP, Load->getName() + ".field", Load);
ProcessLoadOrStoreAttrs(NewLoad, Load);
// Reconstruct the struct value.
SmallVector<unsigned, 1> EVIndexes;
EVIndexes.push_back(Index);
NewStruct = CopyDebug(
InsertValueInst::Create(NewStruct, NewLoad, EVIndexes,
Load->getName() + ".insert", Load), Load);
}
Load->replaceAllUsesWith(NewStruct);
Load->eraseFromParent();
}
static void ExpandExtractValue(ExtractValueInst *EV) {
// Search for the insertvalue instruction that inserts the struct
// field referenced by this extractvalue instruction.
Value *StructVal = EV->getAggregateOperand();
Value *ResultField;
for (;;) {
if (InsertValueInst *IV = dyn_cast<InsertValueInst>(StructVal)) {
if (EV->getNumIndices() != 1 || IV->getNumIndices() != 1) {
errs() << "Value: " << *EV << "\n";
errs() << "Value: " << *IV << "\n";
report_fatal_error("ExpandStructRegs does not handle nested structs");
}
if (EV->getIndices()[0] == IV->getIndices()[0]) {
ResultField = IV->getInsertedValueOperand();
break;
}
// No match. Try the next struct value in the chain.
StructVal = IV->getAggregateOperand();
} else if (Constant *C = dyn_cast<Constant>(StructVal)) {
ResultField = ConstantExpr::getExtractValue(C, EV->getIndices());
break;
} else {
errs() << "Value: " << *StructVal << "\n";
report_fatal_error("Unrecognized struct value");
}
}
EV->replaceAllUsesWith(ResultField);
EV->eraseFromParent();
}
bool ExpandStructRegs::runOnFunction(Function &Func) {
bool Changed = false;
// Split up aggregate loads, stores and phi nodes into operations on
// scalar types. This inserts extractvalue and insertvalue
// instructions which we will expand out later.
for (Function::iterator BB = Func.begin(), E =
|