forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGen_PowerPC.cpp
More file actions
264 lines (222 loc) · 8.06 KB
/
CodeGen_PowerPC.cpp
File metadata and controls
264 lines (222 loc) · 8.06 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
#include "CodeGen_PowerPC.h"
#include "IROperator.h"
#include "IRMatch.h"
#include "Util.h"
#include "LLVM_Headers.h"
namespace Halide {
namespace Internal {
using std::vector;
using std::string;
using namespace llvm;
CodeGen_PowerPC::CodeGen_PowerPC(Target t) : CodeGen_Posix(t) {
#if !(WITH_POWERPC)
user_error << "llvm build not configured with PowerPC target enabled.\n";
#endif
user_assert(llvm_PowerPC_enabled) << "llvm build not configured with PowerPC target enabled.\n";
}
const char* CodeGen_PowerPC::altivec_int_type_name(const Type& t) {
if (t.is_int()) {
switch (t.bits()) {
case 8: return "sb";
case 16: return "sh";
case 32: return "sw";
case 64: return "sd";
}
} else if (t.is_uint()) {
switch (t.bits()) {
case 8: return "ub";
case 16: return "uh";
case 32: return "uw";
case 64: return "ud";
}
}
return nullptr; // not a recognized int type.
}
namespace {
Expr _i64(Expr e) {
return cast(Int(64, e.type().lanes()), e);
}
Expr _u64(Expr e) {
return cast(UInt(64, e.type().lanes()), e);
}
Expr _i32(Expr e) {
return cast(Int(32, e.type().lanes()), e);
}
Expr _u32(Expr e) {
return cast(UInt(32, e.type().lanes()), e);
}
Expr _i16(Expr e) {
return cast(Int(16, e.type().lanes()), e);
}
Expr _u16(Expr e) {
return cast(UInt(16, e.type().lanes()), e);
}
Expr _i8(Expr e) {
return cast(Int(8, e.type().lanes()), e);
}
Expr _u8(Expr e) {
return cast(UInt(8, e.type().lanes()), e);
}
Expr _f32(Expr e) {
return cast(Float(32, e.type().lanes()), e);
}
Expr _f64(Expr e) {
return cast(Float(64, e.type().lanes()), e);
}
}
void CodeGen_PowerPC::visit(const Cast *op) {
if (!op->type.is_vector()) {
// We only have peephole optimizations for vectors in here.
CodeGen_Posix::visit(op);
return;
}
vector<Expr> matches;
struct Pattern {
bool needs_vsx;
bool wide_op;
Type type;
string intrin;
Expr pattern;
};
static Pattern patterns[] = {
{false, true, Int(8, 16), "llvm.ppc.altivec.vaddsbs",
_i8(clamp(wild_i16x_ + wild_i16x_, -128, 127))},
{false, true, Int(8, 16), "llvm.ppc.altivec.vsubsbs",
_i8(clamp(wild_i16x_ - wild_i16x_, -128, 127))},
{false, true, UInt(8, 16), "llvm.ppc.altivec.vaddubs",
_u8(min(wild_u16x_ + wild_u16x_, 255))},
{false, true, UInt(8, 16), "llvm.ppc.altivec.vsububs",
_u8(max(wild_i16x_ - wild_i16x_, 0))},
{false, true, Int(16, 8), "llvm.ppc.altivec.vaddshs",
_i16(clamp(wild_i32x_ + wild_i32x_, -32768, 32767))},
{false, true, Int(16, 8), "llvm.ppc.altivec.vsubshs",
_i16(clamp(wild_i32x_ - wild_i32x_, -32768, 32767))},
{false, true, UInt(16, 8), "llvm.ppc.altivec.vadduhs",
_u16(min(wild_u32x_ + wild_u32x_, 65535))},
{false, true, UInt(16, 8), "llvm.ppc.altivec.vsubuhs",
_u16(max(wild_i32x_ - wild_i32x_, 0))},
{false, true, Int(32, 4), "llvm.ppc.altivec.vaddsws",
_i32(clamp(wild_i64x_ + wild_i64x_, min_i32, max_i32))},
{false, true, Int(32, 4), "llvm.ppc.altivec.vsubsws",
_i32(clamp(wild_i64x_ - wild_i64x_, min_i32, max_i32))},
{false, true, UInt(32, 4), "llvm.ppc.altivec.vadduws",
_u32(min(wild_u64x_ + wild_u64x_, max_u32))},
{false, true, UInt(32, 4), "llvm.ppc.altivec.vsubuws",
_u32(max(wild_i64x_ - wild_i64x_, 0))},
{false, true, Int(8, 16), "llvm.ppc.altivec.vavgsb",
_i8(((wild_i16x_ + wild_i16x_) + 1) / 2)},
{false, true, UInt(8, 16), "llvm.ppc.altivec.vavgub",
_u8(((wild_u16x_ + wild_u16x_) + 1) / 2)},
{false, true, Int(16, 8), "llvm.ppc.altivec.vavgsh",
_i16(((wild_i32x_ + wild_i32x_) + 1) / 2)},
{false, true, UInt(16, 8), "llvm.ppc.altivec.vavguh",
_u16(((wild_u32x_ + wild_u32x_) + 1) / 2)},
{false, true, Int(32, 4), "llvm.ppc.altivec.vavgsw",
_i32(((wild_i64x_ + wild_i64x_) + 1) / 2)},
{false, true, UInt(32, 4), "llvm.ppc.altivec.vavguw",
_u32(((wild_u64x_ + wild_u64x_) + 1) / 2)},
};
for (size_t i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++) {
const Pattern &pattern = patterns[i];
if (!target.has_feature(Target::VSX) && pattern.needs_vsx) {
continue;
}
if (expr_match(pattern.pattern, op, matches)) {
bool match = true;
if (pattern.wide_op) {
// Try to narrow the matches to the target type.
for (size_t i = 0; i < matches.size(); i++) {
matches[i] = lossless_cast(op->type, matches[i]);
if (!matches[i].defined()) match = false;
}
}
if (match) {
value = call_intrin(op->type, pattern.type.lanes(), pattern.intrin, matches);
return;
}
}
}
CodeGen_Posix::visit(op);
}
void CodeGen_PowerPC::visit(const Min *op) {
if (!op->type.is_vector()) {
CodeGen_Posix::visit(op);
return;
}
bool vsx = target.has_feature(Target::VSX);
bool arch_2_07 = target.has_feature(Target::POWER_ARCH_2_07);
const Type& element_type = op->type.element_of();
const char* element_type_name = altivec_int_type_name(element_type);
if (element_type_name != nullptr &&
(element_type.bits() < 64 || arch_2_07)) {
value = call_intrin(op->type, (128 / element_type.bits()),
std::string("llvm.ppc.altivec.vmin") + element_type_name,
{op->a, op->b});
} else if (op->type.element_of() == Float(32)) {
value = call_intrin(op->type, 4, "llvm.ppc.altivec.vminfp", {op->a, op->b});
} else if (vsx && op->type.element_of() == Float(64)) {
value = call_intrin(op->type, 2, "llvm.ppc.vsx.xvmindp", {op->a, op->b});
} else {
CodeGen_Posix::visit(op);
}
}
void CodeGen_PowerPC::visit(const Max *op) {
if (!op->type.is_vector()) {
CodeGen_Posix::visit(op);
return;
}
bool vsx = target.has_feature(Target::VSX);
bool arch_2_07 = target.has_feature(Target::POWER_ARCH_2_07);
const Type& element_type = op->type.element_of();
const char* element_type_name = altivec_int_type_name(element_type);
if (element_type_name != nullptr &&
(element_type.bits() < 64 || arch_2_07)) {
value = call_intrin(op->type, (128 / element_type.bits()),
std::string("llvm.ppc.altivec.vmax") + element_type_name,
{op->a, op->b});
} else if (op->type.element_of() == Float(32)) {
value = call_intrin(op->type, 4, "llvm.ppc.altivec.vmaxfp", {op->a, op->b});
} else if (vsx && op->type.element_of() == Float(64)) {
value = call_intrin(op->type, 2, "llvm.ppc.vsx.xvmaxdp", {op->a, op->b});
} else {
CodeGen_Posix::visit(op);
}
}
string CodeGen_PowerPC::mcpu() const {
if (target.bits == 32) {
return "ppc32";
} else {
if (target.has_feature(Target::POWER_ARCH_2_07))
return "pwr8";
else if (target.has_feature(Target::VSX))
return "pwr7";
else
return "ppc64";
}
}
string CodeGen_PowerPC::mattrs() const {
std::string features;
std::string separator;
std::string enable;
features += "+altivec";
separator = ",";
enable = target.has_feature(Target::VSX) ? "+" : "-";
features += separator + enable + "vsx";
separator = ",";
enable = target.has_feature(Target::POWER_ARCH_2_07) ? "+" : "-";
features += separator + enable + "power8-altivec";
separator = ",";
// These move instructions are defined in POWER ISA 2.06 but we do
// not check for 2.06 currently. So disable this for anything
// lower than ISA 2.07
features += separator + enable + "direct-move";
separator = ",";
return features;
}
bool CodeGen_PowerPC::use_soft_float_abi() const {
return false;
}
int CodeGen_PowerPC::native_vector_bits() const {
return 128;
}
}}