Repository navigation
Expand file tree
/
Copy pathaggregate_boundaries.cpp
More file actions
278 lines (272 loc) · 9.9 KB
/
Copy pathaggregate_boundaries.cpp
File metadata and controls
278 lines (272 loc) · 9.9 KB
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
#include "lowir/builder.h"
#include "opt/passes.h"
namespace opt {
using namespace lowir;
namespace {
// Integer-class aggregates use scalar values at register boundaries. Their
// storage accesses still use the original extent, including partial words.
bool word_object(Type t)
{
return t.is_object() && (storage_bytes(t) >= 1 && storage_bytes(t) <= 16) && t.align_log2 <= 3;
}
bool word_boundary(target_model::Target target, Type t, uint8_t classes, ir_model::AggregateShape shape)
{
if (target_model::is_aarch64(target))
{
// A general-bank record occupies one full x register or stack word
// on both Arm ABIs, including records with subword alignment.
using ir_model::AggregateElement;
const bool general = shape.element == AggregateElement::Mixed || shape.element == AggregateElement::Pointer ||
(shape.element >= AggregateElement::I1 && shape.element <= AggregateElement::I128);
return word_object(t) && storage_bytes(t) <= 8 && general;
}
return word_object(t) &&
(classes == 0 || classes == (OC_STATED | EB_INTEGER | (storage_bytes(t) > 8 ? (EB_INTEGER << 2) : 0)));
}
Type word_type(Type t)
{
return Type::scalar(storage_bytes(t) > 8 ? TypeKind::I128 : TypeKind::I64);
}
} // namespace
void scalarize_aggregate_boundaries(Program& program)
{
if (!target_model::aggregate_integer_carriers(program.target) && !target_model::is_aarch64(program.target))
return;
const std::vector<Signature> old_signatures = program.signatures;
const std::vector<Parameter> old_parameters = program.parameters;
bool changed = false;
for (Signature& sig : program.signatures)
{
if (word_boundary(program.target, sig.result, sig.boundary.result_classes, sig.boundary.result_shape))
{
sig.result = word_type(sig.result);
sig.boundary.result_shape = ir_model::AggregateShape();
sig.boundary.result_classes = sig.result.kind() == TypeKind::I128
? (OC_STATED | EB_INTEGER | (EB_INTEGER << 2))
: 0;
changed = true;
}
for (uint32_t p = 0; p < sig.parameter_count; ++p)
{
Parameter& param = program.parameters[sig.first_parameter + p];
if (param.pass == ir_model::PPM_DIRECT &&
word_boundary(program.target, param.type, param.classes, param.shape))
{
param.type = word_type(param.type);
param.shape = ir_model::AggregateShape();
param.classes = param.type.kind() == TypeKind::I128 ? (OC_STATED | EB_INTEGER | (EB_INTEGER << 2)) : 0;
changed = true;
}
}
}
if (!changed)
return;
for (SymbolId symbol : program.order)
{
if (program.symbols[symbol].kind != SymbolKind::FunctionDefinition)
continue;
Function& function = program.functions[program.symbols[symbol].detail];
bool wide = false;
if (function.body == nullptr)
continue;
FunctionBody& body = *function.body;
const Signature& sig = old_signatures[function.signature];
const uint32_t temps = body.temps.size();
std::vector<SlotId> homes(temps, kNoId);
auto home = [&](TempId t) {
homes[t] = body.slots.size();
body.slots.push_back(Slot{ 0, SlotKind::Aggregate, 0, 0,
Type::object(storage_bytes(body.temps[t].type) > 8 ? 16 : 8, body.temps[t].type.align_log2) });
body.temps[t].type = word_type(body.temps[t].type);
wide = wide || body.temps[t].type.kind() == TypeKind::I128;
};
for (uint32_t p = 0; p < body.parameter_count; ++p)
if (old_parameters[sig.first_parameter + p].type != program.parameters[sig.first_parameter + p].type)
home(p);
// The signatures retain their ids, so explicit and symbol-based calls
// use the same ABI choice, including calls through function pointers.
for (BlockId b : body.layout)
for (InstId i = body.blocks[b].first; i != kNoId; i = body.instructions[i].next)
{
Instruction& inst = body.instructions[i];
if (inst.op != Opcode::Call || !word_object(inst.type))
continue;
const Signature* target = program.call_boundary(inst);
if (target != nullptr &&
(target->result.kind() == TypeKind::I64 || target->result.kind() == TypeKind::I128))
{
inst.type = target->result;
if (inst.result != kNoId)
home(inst.result);
}
}
auto emit = [&](BlockId b, InstId& prev, Instruction inst) {
const InstId added = append_instruction(body, inst);
insert_after(body, b, prev, added);
prev = added;
return added;
};
auto save = [&](BlockId b, InstId& prev, TempId t, uint32_t location) {
Instruction st = make_instruction(Opcode::Store, body.temps[t].type);
st.ops[0] = Value::temp(t);
st.ops[1] = Value::slot(homes[t]);
st.location = location;
emit(b, prev, st);
};
auto load = [&](BlockId b, InstId& prev, Value storage, Type original, uint32_t location) {
const Type type = word_type(original);
wide = wide || type.kind() == TypeKind::I128;
auto value = [&](Instruction inst, Type result) {
inst.location = location;
const InstId id = emit(b, prev, inst);
const TempId t = append_temp(body, result, id);
body.instructions[id].result = t;
return Value::temp(t);
};
auto literal = [&](uint64_t bits) {
const uint32_t id = body.literals.size();
body.literals.push_back(Literal{ bits, 0 });
return Value::make(ValueKind::Integer, id);
};
const uint64_t size = storage_bytes(original);
if (storage.is(ValueKind::Temp) && body.temps[storage.index].type.is_object())
{
// An object value is a snapshot, not a pointer to its original
// source. Give it explicit storage before reading its bits.
const SlotId slot = body.slots.size();
body.slots.push_back(Slot{ 0, SlotKind::Aggregate, 0, 0, original });
Instruction copy = make_instruction(Opcode::CopyObject, Type::scalar(TypeKind::Void));
copy.ops[0] = storage;
copy.ops[1] = Value::slot(slot);
copy.immediate = size;
copy.sub = original.align_log2;
copy.location = location;
emit(b, prev, copy);
storage = Value::slot(slot);
}
if (size == 8 || size == 16)
{
Instruction ld = make_instruction(Opcode::Load, type);
ld.ops[0] = storage;
return value(ld, type);
}
Value address = storage;
if (address.is(ValueKind::Slot))
{
Instruction addr = make_instruction(Opcode::Addr, body.pointer_type());
addr.ops[0] = storage;
address = value(addr, addr.type);
}
Value result = Value::none();
for (uint64_t offset = 0; offset < size;)
{
const uint64_t left = size - offset;
const uint64_t bytes = left >= 8 ? 8 : left >= 4 ? 4 : left >= 2 ? 2 : 1;
const Type field = Type::scalar(bytes == 8 ? TypeKind::I64
: bytes == 4 ? TypeKind::U32
: bytes == 2 ? TypeKind::U16
: TypeKind::U8);
Value at = address;
if (offset != 0)
{
Instruction index = make_instruction(Opcode::Index, Type::scalar(TypeKind::I8));
index.sub = ir_model::IPK_FIELD;
index.ops[0] = address;
index.ops[1] = literal(offset);
at = value(index, body.pointer_type());
}
Instruction ld = make_instruction(Opcode::Load, field);
ld.ops[0] = at;
Value part = value(ld, field);
Instruction extend = make_instruction(Opcode::Convert, type);
extend.sub = static_cast<uint8_t>(ConvertOp::ZExt);
extend.ops[0] = part;
set_convert_source_type(extend, field);
part = value(extend, type);
if (offset != 0)
{
Instruction shift = make_instruction(Opcode::Binary, type);
shift.sub = static_cast<uint8_t>(BinaryOp::Shl);
shift.ops[0] = part;
shift.ops[1] = literal(offset * 8);
part = value(shift, type);
}
if (result.kind == ValueKind::None)
result = part;
else
{
Instruction join = make_instruction(Opcode::Binary, type);
join.sub = static_cast<uint8_t>(BinaryOp::Or);
join.ops[0] = result;
join.ops[1] = part;
result = value(join, type);
}
offset += bytes;
}
return result;
};
for (BlockId b : body.layout)
{
InstId prev = kNoId;
for (InstId i = body.blocks[b].first; i != kNoId;)
{
Instruction inst = body.instructions[i];
const InstId next = inst.next;
for_each_operand(body, inst, [&](Value& v) {
if (v.is(ValueKind::Temp) && v.index < temps && homes[v.index] != kNoId)
v = Value::slot(homes[v.index]);
});
if (inst.op == Opcode::Call)
{
const Signature* target = program.call_boundary(inst);
if (target != nullptr)
{
const Signature& old = old_signatures[target - program.signatures.data()];
for (uint32_t p = 0; p < inst.extra_count && p < old.parameter_count; ++p)
if (old_parameters[old.first_parameter + p].type !=
program.parameters[target->first_parameter + p].type)
{
Value arg = body.operands[inst.extra_first + p];
body.operands[inst.extra_first + p] = load(
b, prev, arg, old_parameters[old.first_parameter + p].type, inst.location);
}
}
}
else if (inst.op == Opcode::Return && sig.result != program.signatures[function.signature].result)
{
inst.type = word_type(sig.result);
wide = wide || inst.type.kind() == TypeKind::I128;
inst.ops[0] = load(b, prev, inst.ops[0], sig.result, inst.location);
}
else if (inst.op == Opcode::Store && word_object(inst.type) && inst.ops[0].is(ValueKind::Slot))
{
inst.op = Opcode::CopyObject;
inst.immediate = storage_bytes(inst.type);
inst.sub = inst.type.align_log2;
inst.type = Type::scalar(TypeKind::Void);
}
else if (inst.op == Opcode::Copy && word_object(inst.type) && inst.ops[0].is(ValueKind::Slot))
inst.op = Opcode::Load;
body.instructions[i] = inst;
prev = i;
if (inst.result != kNoId && inst.result < temps && homes[inst.result] != kNoId)
save(b, prev, inst.result, inst.location);
i = next;
}
}
if (!body.layout.empty())
{
InstId prev = kNoId;
for (uint32_t p = 0; p < body.parameter_count; ++p)
if (homes[p] != kNoId)
save(body.layout.front(), prev, p, 0);
}
if (wide)
{
OptimizerStats stats;
FunctionContext context(program, symbol, stats, 2);
expand_wide_integers(context);
}
}
}
} // namespace opt