1//===- ARMLegalizerInfo.cpp --------------------------------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements the targeting of the Machinelegalizer class for ARM.
10/// \todo This should be generated by TableGen.
11//===----------------------------------------------------------------------===//
12
13#include "ARMLegalizerInfo.h"
14#include "ARMCallLowering.h"
15#include "ARMSubtarget.h"
16#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
17#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
18#include "llvm/CodeGen/LowLevelTypeUtils.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/TargetOpcodes.h"
21#include "llvm/CodeGen/ValueTypes.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Type.h"
24
25using namespace llvm;
26using namespace LegalizeActions;
27
28static bool AEABI(const ARMSubtarget &ST) {
29 return ST.isTargetAEABI() || ST.isTargetGNUAEABI() || ST.isTargetMuslAEABI();
30}
31
32ARMLegalizerInfo::ARMLegalizerInfo(const ARMSubtarget &ST) : ST(ST) {
33 using namespace TargetOpcode;
34
35 const LLT p0 = LLT::pointer(AddressSpace: 0, SizeInBits: 32);
36
37 const LLT s1 = LLT::scalar(SizeInBits: 1);
38 const LLT s8 = LLT::scalar(SizeInBits: 8);
39 const LLT s16 = LLT::scalar(SizeInBits: 16);
40 const LLT s32 = LLT::scalar(SizeInBits: 32);
41 const LLT s64 = LLT::scalar(SizeInBits: 64);
42
43 const LLT i32 = LLT::integer(SizeInBits: 32);
44 const LLT i64 = LLT::integer(SizeInBits: 64);
45 const LLT f32 = LLT::float32();
46 const LLT f64 = LLT::float64();
47
48 if (ST.isThumb1Only()) {
49 // Thumb1 is not supported yet.
50 verify(MII: *ST.getInstrInfo());
51 return;
52 }
53
54 getActionDefinitionsBuilder(Opcode: G_BITCAST)
55 .legalFor(Types: {{s32, s32}})
56 .legalFor(Pred: ST.hasFPRegs(), Types: {{s64, s64}})
57 .maxScalar(TypeIdx: 0, Ty: ST.hasFPRegs() ? i64 : i32);
58
59 getActionDefinitionsBuilder(Opcode: G_MERGE_VALUES).legalFor(Types: {{s64, s32}});
60 getActionDefinitionsBuilder(Opcode: G_UNMERGE_VALUES).legalFor(Types: {{s32, s64}});
61
62 getActionDefinitionsBuilder(Opcodes: {G_SEXT, G_ZEXT, G_ANYEXT})
63 .legalForCartesianProduct(Types0: {s8, s16, s32}, Types1: {s1, s8, s16});
64
65 getActionDefinitionsBuilder(Opcode: G_TRUNC).legalForCartesianProduct(Types0: {s1, s8, s16},
66 Types1: {s8, s16, s32});
67
68 getActionDefinitionsBuilder(Opcode: G_SEXT_INREG).lower();
69
70 getActionDefinitionsBuilder(Opcodes: {G_MUL, G_AND, G_OR, G_XOR})
71 .legalFor(Types: {s32})
72 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
73
74 if (ST.hasNEON())
75 getActionDefinitionsBuilder(Opcodes: {G_ADD, G_SUB})
76 .legalFor(Types: {s32, s64})
77 .minScalar(TypeIdx: 0, Ty: s32);
78 else
79 getActionDefinitionsBuilder(Opcodes: {G_ADD, G_SUB})
80 .legalFor(Types: {s32})
81 .minScalar(TypeIdx: 0, Ty: s32);
82
83 getActionDefinitionsBuilder(Opcodes: {G_ASHR, G_LSHR, G_SHL})
84 .legalFor(Types: {{s32, s32}})
85 .minScalar(TypeIdx: 0, Ty: s32)
86 .clampScalar(TypeIdx: 1, MinTy: s32, MaxTy: s32);
87
88 bool HasHWDivide = (!ST.isThumb() && ST.hasDivideInARMMode()) ||
89 (ST.isThumb() && ST.hasDivideInThumbMode());
90 if (HasHWDivide)
91 getActionDefinitionsBuilder(Opcodes: {G_SDIV, G_UDIV})
92 .legalFor(Types: {s32})
93 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
94 else
95 getActionDefinitionsBuilder(Opcodes: {G_SDIV, G_UDIV})
96 .libcallFor(Types: {s32})
97 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
98
99 auto &REMBuilder =
100 getActionDefinitionsBuilder(Opcodes: {G_SREM, G_UREM}).minScalar(TypeIdx: 0, Ty: s32);
101 if (HasHWDivide)
102 REMBuilder.lowerFor(Types: {s32});
103 else if (AEABI(ST))
104 REMBuilder.customFor(Types: {s32});
105 else
106 REMBuilder.libcallFor(Types: {s32});
107
108 getActionDefinitionsBuilder(Opcode: G_INTTOPTR)
109 .legalFor(Types: {{p0, s32}})
110 .minScalar(TypeIdx: 1, Ty: s32);
111 getActionDefinitionsBuilder(Opcode: G_PTRTOINT)
112 .legalFor(Types: {{s32, p0}})
113 .minScalar(TypeIdx: 0, Ty: s32);
114
115 getActionDefinitionsBuilder(Opcode: G_CONSTANT)
116 .customFor(Types: {s32, p0})
117 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
118
119 getActionDefinitionsBuilder(Opcode: G_CONSTANT_POOL).legalFor(Types: {p0});
120
121 getActionDefinitionsBuilder(Opcode: G_ICMP)
122 .legalForCartesianProduct(Types0: {s1}, Types1: {s32, p0})
123 .minScalar(TypeIdx: 1, Ty: s32);
124
125 getActionDefinitionsBuilder(Opcode: G_SELECT)
126 .legalForCartesianProduct(Types0: {s32, p0}, Types1: {s1})
127 .minScalar(TypeIdx: 0, Ty: s32);
128
129 // We're keeping these builders around because we'll want to add support for
130 // floating point to them.
131 auto &LoadStoreBuilder = getActionDefinitionsBuilder(Opcodes: {G_LOAD, G_STORE})
132 .legalForTypesWithMemDesc(TypesAndMemDesc: {{.Type0: s8, .Type1: p0, .MemTy: s8, .Align: 8},
133 {.Type0: s16, .Type1: p0, .MemTy: s16, .Align: 8},
134 {.Type0: s32, .Type1: p0, .MemTy: s32, .Align: 8},
135 {.Type0: p0, .Type1: p0, .MemTy: p0, .Align: 8}})
136 .unsupportedIfMemSizeNotPow2();
137
138 getActionDefinitionsBuilder(Opcode: G_FRAME_INDEX).legalFor(Types: {p0});
139 getActionDefinitionsBuilder(Opcode: G_GLOBAL_VALUE).legalFor(Types: {p0});
140
141 auto &PhiBuilder =
142 getActionDefinitionsBuilder(Opcode: G_PHI)
143 .legalFor(Types: {s32, p0})
144 .minScalar(TypeIdx: 0, Ty: s32);
145
146 getActionDefinitionsBuilder(Opcode: G_PTR_ADD)
147 .legalFor(Types: {{p0, i32}})
148 .minScalar(TypeIdx: 1, Ty: i32);
149
150 getActionDefinitionsBuilder(Opcode: G_BR).alwaysLegal();
151 getActionDefinitionsBuilder(Opcode: G_BRCOND).legalFor(Types: {s1});
152
153 if (!ST.useSoftFloat() && ST.hasVFP2Base()) {
154 getActionDefinitionsBuilder(
155 Opcodes: {G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FCONSTANT, G_FNEG})
156 .legalFor(Types: {f32, f64});
157
158 LoadStoreBuilder
159 .legalForTypesWithMemDesc(TypesAndMemDesc: {{.Type0: s64, .Type1: p0, .MemTy: s64, .Align: 32}})
160 .maxScalar(TypeIdx: 0, Ty: s32);
161 PhiBuilder.legalFor(Types: {s64});
162
163 getActionDefinitionsBuilder(Opcode: G_FCMP).legalForCartesianProduct(Types0: {s1},
164 Types1: {f32, f64});
165
166 getActionDefinitionsBuilder(Opcode: G_FPEXT).legalFor(Types: {{f64, f32}});
167 getActionDefinitionsBuilder(Opcode: G_FPTRUNC).legalFor(Types: {{f32, f64}});
168
169 getActionDefinitionsBuilder(Opcodes: {G_FPTOSI, G_FPTOUI})
170 .legalForCartesianProduct(Types0: {i32}, Types1: {f32, f64});
171 getActionDefinitionsBuilder(Opcodes: {G_SITOFP, G_UITOFP})
172 .legalForCartesianProduct(Types0: {f32, f64}, Types1: {i32});
173
174 getActionDefinitionsBuilder(Opcodes: {G_GET_FPENV, G_SET_FPENV, G_GET_FPMODE})
175 .legalFor(Types: {s32});
176 getActionDefinitionsBuilder(Opcode: G_RESET_FPENV).alwaysLegal();
177 getActionDefinitionsBuilder(Opcode: G_SET_FPMODE).customFor(Types: {s32});
178 getActionDefinitionsBuilder(Opcode: G_RESET_FPMODE).custom();
179 } else {
180 getActionDefinitionsBuilder(Opcodes: {G_FADD, G_FSUB, G_FMUL, G_FDIV})
181 .libcallFor(Types: {f32, f64});
182
183 LoadStoreBuilder.maxScalar(TypeIdx: 0, Ty: s32);
184
185 getActionDefinitionsBuilder(Opcode: G_FNEG).lowerFor(Types: {s32, s64});
186
187 getActionDefinitionsBuilder(Opcode: G_FCONSTANT).lowerFor(Types: {s32, s64});
188
189 getActionDefinitionsBuilder(Opcode: G_FCMP).customForCartesianProduct(Types0: {s1},
190 Types1: {f32, f64});
191
192 if (AEABI(ST))
193 setFCmpLibcallsAEABI();
194 else
195 setFCmpLibcallsGNU();
196
197 getActionDefinitionsBuilder(Opcode: G_FPEXT).libcallFor(Types: {{f64, f32}});
198 getActionDefinitionsBuilder(Opcode: G_FPTRUNC).libcallFor(Types: {{f32, f64}});
199
200 getActionDefinitionsBuilder(Opcodes: {G_FPTOSI, G_FPTOUI})
201 .libcallForCartesianProduct(Types0: {i32}, Types1: {f32, f64});
202 getActionDefinitionsBuilder(Opcodes: {G_SITOFP, G_UITOFP})
203 .libcallForCartesianProduct(Types0: {f32, f64}, Types1: {i32});
204
205 getActionDefinitionsBuilder(Opcodes: {G_GET_FPENV, G_SET_FPENV, G_RESET_FPENV})
206 .libcall();
207 getActionDefinitionsBuilder(Opcodes: {G_GET_FPMODE, G_SET_FPMODE, G_RESET_FPMODE})
208 .libcall();
209 }
210
211 // Just expand whatever loads and stores are left.
212 LoadStoreBuilder.lower();
213
214 if (!ST.useSoftFloat() && ST.hasVFP4Base())
215 getActionDefinitionsBuilder(Opcode: G_FMA).legalFor(Types: {f32, f64});
216 else
217 getActionDefinitionsBuilder(Opcode: G_FMA).libcallFor(Types: {f32, f64});
218
219 getActionDefinitionsBuilder(Opcodes: {G_FREM, G_FPOW}).libcallFor(Types: {f32, f64});
220
221 if (ST.hasV5TOps() && !ST.isThumb1Only()) {
222 getActionDefinitionsBuilder(Opcode: G_CTLZ)
223 .legalFor(Types: {s32, s32})
224 .clampScalar(TypeIdx: 1, MinTy: s32, MaxTy: s32)
225 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
226 getActionDefinitionsBuilder(Opcode: G_CTLZ_ZERO_POISON)
227 .lowerFor(Types: {s32, s32})
228 .clampScalar(TypeIdx: 1, MinTy: s32, MaxTy: s32)
229 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
230 } else {
231 getActionDefinitionsBuilder(Opcode: G_CTLZ_ZERO_POISON)
232 .libcallFor(Types: {s32, s32})
233 .clampScalar(TypeIdx: 1, MinTy: s32, MaxTy: s32)
234 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
235 getActionDefinitionsBuilder(Opcode: G_CTLZ)
236 .lowerFor(Types: {s32, s32})
237 .clampScalar(TypeIdx: 1, MinTy: s32, MaxTy: s32)
238 .clampScalar(TypeIdx: 0, MinTy: s32, MaxTy: s32);
239 }
240
241 verify(MII: *ST.getInstrInfo());
242}
243
244void ARMLegalizerInfo::setFCmpLibcallsAEABI() {
245 // FCMP_TRUE and FCMP_FALSE don't need libcalls, they should be
246 // default-initialized.
247 FCmp32Libcalls.resize(S: CmpInst::LAST_FCMP_PREDICATE + 1);
248 FCmp32Libcalls[CmpInst::FCMP_OEQ] = {
249 {.LibcallID: RTLIB::OEQ_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
250 FCmp32Libcalls[CmpInst::FCMP_OGE] = {
251 {.LibcallID: RTLIB::OGE_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
252 FCmp32Libcalls[CmpInst::FCMP_OGT] = {
253 {.LibcallID: RTLIB::OGT_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
254 FCmp32Libcalls[CmpInst::FCMP_OLE] = {
255 {.LibcallID: RTLIB::OLE_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
256 FCmp32Libcalls[CmpInst::FCMP_OLT] = {
257 {.LibcallID: RTLIB::OLT_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
258 FCmp32Libcalls[CmpInst::FCMP_ORD] = {{.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::ICMP_EQ}};
259 FCmp32Libcalls[CmpInst::FCMP_UGE] = {{.LibcallID: RTLIB::OLT_F32, .Predicate: CmpInst::ICMP_EQ}};
260 FCmp32Libcalls[CmpInst::FCMP_UGT] = {{.LibcallID: RTLIB::OLE_F32, .Predicate: CmpInst::ICMP_EQ}};
261 FCmp32Libcalls[CmpInst::FCMP_ULE] = {{.LibcallID: RTLIB::OGT_F32, .Predicate: CmpInst::ICMP_EQ}};
262 FCmp32Libcalls[CmpInst::FCMP_ULT] = {{.LibcallID: RTLIB::OGE_F32, .Predicate: CmpInst::ICMP_EQ}};
263 // AEABI only provides ordered-equal; not-equal is the same call, inverted.
264 FCmp32Libcalls[CmpInst::FCMP_UNE] = {{.LibcallID: RTLIB::OEQ_F32, .Predicate: CmpInst::ICMP_EQ}};
265 FCmp32Libcalls[CmpInst::FCMP_UNO] = {
266 {.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
267 FCmp32Libcalls[CmpInst::FCMP_ONE] = {
268 {.LibcallID: RTLIB::OGT_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE},
269 {.LibcallID: RTLIB::OLT_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
270 FCmp32Libcalls[CmpInst::FCMP_UEQ] = {
271 {.LibcallID: RTLIB::OEQ_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE},
272 {.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
273
274 FCmp64Libcalls.resize(S: CmpInst::LAST_FCMP_PREDICATE + 1);
275 FCmp64Libcalls[CmpInst::FCMP_OEQ] = {
276 {.LibcallID: RTLIB::OEQ_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
277 FCmp64Libcalls[CmpInst::FCMP_OGE] = {
278 {.LibcallID: RTLIB::OGE_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
279 FCmp64Libcalls[CmpInst::FCMP_OGT] = {
280 {.LibcallID: RTLIB::OGT_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
281 FCmp64Libcalls[CmpInst::FCMP_OLE] = {
282 {.LibcallID: RTLIB::OLE_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
283 FCmp64Libcalls[CmpInst::FCMP_OLT] = {
284 {.LibcallID: RTLIB::OLT_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
285 FCmp64Libcalls[CmpInst::FCMP_ORD] = {{.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::ICMP_EQ}};
286 FCmp64Libcalls[CmpInst::FCMP_UGE] = {{.LibcallID: RTLIB::OLT_F64, .Predicate: CmpInst::ICMP_EQ}};
287 FCmp64Libcalls[CmpInst::FCMP_UGT] = {{.LibcallID: RTLIB::OLE_F64, .Predicate: CmpInst::ICMP_EQ}};
288 FCmp64Libcalls[CmpInst::FCMP_ULE] = {{.LibcallID: RTLIB::OGT_F64, .Predicate: CmpInst::ICMP_EQ}};
289 FCmp64Libcalls[CmpInst::FCMP_ULT] = {{.LibcallID: RTLIB::OGE_F64, .Predicate: CmpInst::ICMP_EQ}};
290 // AEABI only provides ordered-equal; not-equal is the same call, inverted.
291 FCmp64Libcalls[CmpInst::FCMP_UNE] = {{.LibcallID: RTLIB::OEQ_F64, .Predicate: CmpInst::ICMP_EQ}};
292 FCmp64Libcalls[CmpInst::FCMP_UNO] = {
293 {.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
294 FCmp64Libcalls[CmpInst::FCMP_ONE] = {
295 {.LibcallID: RTLIB::OGT_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE},
296 {.LibcallID: RTLIB::OLT_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
297 FCmp64Libcalls[CmpInst::FCMP_UEQ] = {
298 {.LibcallID: RTLIB::OEQ_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE},
299 {.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::BAD_ICMP_PREDICATE}};
300}
301
302void ARMLegalizerInfo::setFCmpLibcallsGNU() {
303 // FCMP_TRUE and FCMP_FALSE don't need libcalls, they should be
304 // default-initialized.
305 FCmp32Libcalls.resize(S: CmpInst::LAST_FCMP_PREDICATE + 1);
306 FCmp32Libcalls[CmpInst::FCMP_OEQ] = {
307 {.LibcallID: RTLIB::FCMP3_PRED_OEQ_F32, .Predicate: CmpInst::ICMP_EQ}};
308 FCmp32Libcalls[CmpInst::FCMP_OGE] = {
309 {.LibcallID: RTLIB::FCMP3_PRED_OGE_F32, .Predicate: CmpInst::ICMP_SGE}};
310 FCmp32Libcalls[CmpInst::FCMP_OGT] = {
311 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F32, .Predicate: CmpInst::ICMP_SGT}};
312 FCmp32Libcalls[CmpInst::FCMP_OLE] = {
313 {.LibcallID: RTLIB::FCMP3_PRED_OLE_F32, .Predicate: CmpInst::ICMP_SLE}};
314 FCmp32Libcalls[CmpInst::FCMP_OLT] = {
315 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F32, .Predicate: CmpInst::ICMP_SLT}};
316 FCmp32Libcalls[CmpInst::FCMP_ORD] = {{.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::ICMP_EQ}};
317 FCmp32Libcalls[CmpInst::FCMP_UGE] = {
318 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F32, .Predicate: CmpInst::ICMP_SGE}};
319 FCmp32Libcalls[CmpInst::FCMP_UGT] = {
320 {.LibcallID: RTLIB::FCMP3_PRED_OLE_F32, .Predicate: CmpInst::ICMP_SGT}};
321 FCmp32Libcalls[CmpInst::FCMP_ULE] = {
322 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F32, .Predicate: CmpInst::ICMP_SLE}};
323 FCmp32Libcalls[CmpInst::FCMP_ULT] = {
324 {.LibcallID: RTLIB::FCMP3_PRED_OGE_F32, .Predicate: CmpInst::ICMP_SLT}};
325 FCmp32Libcalls[CmpInst::FCMP_UNE] = {
326 {.LibcallID: RTLIB::FCMP3_PRED_UNE_F32, .Predicate: CmpInst::ICMP_NE}};
327 FCmp32Libcalls[CmpInst::FCMP_UNO] = {{.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::ICMP_NE}};
328 FCmp32Libcalls[CmpInst::FCMP_ONE] = {
329 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F32, .Predicate: CmpInst::ICMP_SGT},
330 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F32, .Predicate: CmpInst::ICMP_SLT}};
331 FCmp32Libcalls[CmpInst::FCMP_UEQ] = {
332 {.LibcallID: RTLIB::FCMP3_PRED_OEQ_F32, .Predicate: CmpInst::ICMP_EQ},
333 {.LibcallID: RTLIB::UO_F32, .Predicate: CmpInst::ICMP_NE}};
334
335 FCmp64Libcalls.resize(S: CmpInst::LAST_FCMP_PREDICATE + 1);
336 FCmp64Libcalls[CmpInst::FCMP_OEQ] = {
337 {.LibcallID: RTLIB::FCMP3_PRED_OEQ_F64, .Predicate: CmpInst::ICMP_EQ}};
338 FCmp64Libcalls[CmpInst::FCMP_OGE] = {
339 {.LibcallID: RTLIB::FCMP3_PRED_OGE_F64, .Predicate: CmpInst::ICMP_SGE}};
340 FCmp64Libcalls[CmpInst::FCMP_OGT] = {
341 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F64, .Predicate: CmpInst::ICMP_SGT}};
342 FCmp64Libcalls[CmpInst::FCMP_OLE] = {
343 {.LibcallID: RTLIB::FCMP3_PRED_OLE_F64, .Predicate: CmpInst::ICMP_SLE}};
344 FCmp64Libcalls[CmpInst::FCMP_OLT] = {
345 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F64, .Predicate: CmpInst::ICMP_SLT}};
346 FCmp64Libcalls[CmpInst::FCMP_ORD] = {{.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::ICMP_EQ}};
347 FCmp64Libcalls[CmpInst::FCMP_UGE] = {
348 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F64, .Predicate: CmpInst::ICMP_SGE}};
349 FCmp64Libcalls[CmpInst::FCMP_UGT] = {
350 {.LibcallID: RTLIB::FCMP3_PRED_OLE_F64, .Predicate: CmpInst::ICMP_SGT}};
351 FCmp64Libcalls[CmpInst::FCMP_ULE] = {
352 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F64, .Predicate: CmpInst::ICMP_SLE}};
353 FCmp64Libcalls[CmpInst::FCMP_ULT] = {
354 {.LibcallID: RTLIB::FCMP3_PRED_OGE_F64, .Predicate: CmpInst::ICMP_SLT}};
355 FCmp64Libcalls[CmpInst::FCMP_UNE] = {
356 {.LibcallID: RTLIB::FCMP3_PRED_UNE_F64, .Predicate: CmpInst::ICMP_NE}};
357 FCmp64Libcalls[CmpInst::FCMP_UNO] = {{.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::ICMP_NE}};
358 FCmp64Libcalls[CmpInst::FCMP_ONE] = {
359 {.LibcallID: RTLIB::FCMP3_PRED_OGT_F64, .Predicate: CmpInst::ICMP_SGT},
360 {.LibcallID: RTLIB::FCMP3_PRED_OLT_F64, .Predicate: CmpInst::ICMP_SLT}};
361 FCmp64Libcalls[CmpInst::FCMP_UEQ] = {
362 {.LibcallID: RTLIB::FCMP3_PRED_OEQ_F64, .Predicate: CmpInst::ICMP_EQ},
363 {.LibcallID: RTLIB::UO_F64, .Predicate: CmpInst::ICMP_NE}};
364}
365
366ARMLegalizerInfo::FCmpLibcallsList
367ARMLegalizerInfo::getFCmpLibcalls(CmpInst::Predicate Predicate,
368 unsigned Size) const {
369 assert(CmpInst::isFPPredicate(Predicate) && "Unsupported FCmp predicate");
370 if (Size == 32)
371 return FCmp32Libcalls[Predicate];
372 if (Size == 64)
373 return FCmp64Libcalls[Predicate];
374 llvm_unreachable("Unsupported size for FCmp predicate");
375}
376
377bool ARMLegalizerInfo::legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI,
378 LostDebugLocObserver &LocObserver) const {
379 using namespace TargetOpcode;
380
381 MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
382 MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
383 LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
384
385 switch (MI.getOpcode()) {
386 default:
387 return false;
388 case G_SREM:
389 case G_UREM: {
390 Register OriginalResult = MI.getOperand(i: 0).getReg();
391 auto Size = MRI.getType(Reg: OriginalResult).getSizeInBits();
392 if (Size != 32)
393 return false;
394
395 auto Libcall =
396 MI.getOpcode() == G_SREM ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32;
397
398 // Our divmod libcalls return a struct containing the quotient and the
399 // remainder. Create a new, unused register for the quotient and use the
400 // destination of the original instruction for the remainder.
401 Type *ArgTy = Type::getInt32Ty(C&: Ctx);
402 StructType *RetTy = StructType::get(Context&: Ctx, Elements: {ArgTy, ArgTy}, /* Packed */ isPacked: true);
403 Register RetRegs[] = {MRI.createGenericVirtualRegister(Ty: LLT::integer(SizeInBits: 32)),
404 OriginalResult};
405 auto Status = Helper.createLibcall(Libcall, Result: {RetRegs, RetTy, 0},
406 Args: {{MI.getOperand(i: 1).getReg(), ArgTy, 0},
407 {MI.getOperand(i: 2).getReg(), ArgTy, 0}},
408 LocObserver, MI: &MI);
409 if (Status != LegalizerHelper::Legalized)
410 return false;
411 break;
412 }
413 case G_FCMP: {
414 assert(MRI.getType(MI.getOperand(2).getReg()) ==
415 MRI.getType(MI.getOperand(3).getReg()) &&
416 "Mismatched operands for G_FCMP");
417 auto OpSize = MRI.getType(Reg: MI.getOperand(i: 2).getReg()).getSizeInBits();
418
419 auto OriginalResult = MI.getOperand(i: 0).getReg();
420 auto Predicate =
421 static_cast<CmpInst::Predicate>(MI.getOperand(i: 1).getPredicate());
422 auto Libcalls = getFCmpLibcalls(Predicate, Size: OpSize);
423
424 if (Libcalls.empty()) {
425 assert((Predicate == CmpInst::FCMP_TRUE ||
426 Predicate == CmpInst::FCMP_FALSE) &&
427 "Predicate needs libcalls, but none specified");
428 MIRBuilder.buildConstant(Res: OriginalResult,
429 Val: Predicate == CmpInst::FCMP_TRUE ? 1 : 0);
430 MI.eraseFromParent();
431 return true;
432 }
433
434 assert((OpSize == 32 || OpSize == 64) && "Unsupported operand size");
435 auto *ArgTy = OpSize == 32 ? Type::getFloatTy(C&: Ctx) : Type::getDoubleTy(C&: Ctx);
436 auto *RetTy = Type::getInt32Ty(C&: Ctx);
437
438 SmallVector<Register, 2> Results;
439 for (auto Libcall : Libcalls) {
440 auto LibcallResult = MRI.createGenericVirtualRegister(Ty: LLT::integer(SizeInBits: 32));
441 auto Status =
442 Helper.createLibcall(Libcall: Libcall.LibcallID, Result: {LibcallResult, RetTy, 0},
443 Args: {{MI.getOperand(i: 2).getReg(), ArgTy, 0},
444 {MI.getOperand(i: 3).getReg(), ArgTy, 0}},
445 LocObserver, MI: &MI);
446
447 if (Status != LegalizerHelper::Legalized)
448 return false;
449
450 auto ProcessedResult =
451 Libcalls.size() == 1
452 ? OriginalResult
453 : MRI.createGenericVirtualRegister(Ty: MRI.getType(Reg: OriginalResult));
454
455 // We have a result, but we need to transform it into a proper 1-bit 0 or
456 // 1, taking into account the different peculiarities of the values
457 // returned by the comparison functions.
458 CmpInst::Predicate ResultPred = Libcall.Predicate;
459 if (ResultPred == CmpInst::BAD_ICMP_PREDICATE) {
460 // We have a nice 0 or 1, and we just need to truncate it back to 1 bit
461 // to keep the types consistent.
462 MIRBuilder.buildTrunc(Res: ProcessedResult, Op: LibcallResult);
463 } else {
464 // We need to compare against 0.
465 assert(CmpInst::isIntPredicate(ResultPred) && "Unsupported predicate");
466 auto Zero = MIRBuilder.buildConstant(Res: LLT::integer(SizeInBits: 32), Val: 0);
467 MIRBuilder.buildICmp(Pred: ResultPred, Res: ProcessedResult, Op0: LibcallResult, Op1: Zero);
468 }
469 Results.push_back(Elt: ProcessedResult);
470 }
471
472 if (Results.size() != 1) {
473 assert(Results.size() == 2 && "Unexpected number of results");
474 MIRBuilder.buildOr(Dst: OriginalResult, Src0: Results[0], Src1: Results[1]);
475 }
476 break;
477 }
478 case G_CONSTANT: {
479 const ConstantInt *ConstVal = MI.getOperand(i: 1).getCImm();
480 uint64_t ImmVal = ConstVal->getZExtValue();
481 if (ConstantMaterializationCost(Val: ImmVal, Subtarget: &ST) > 2 && !ST.genExecuteOnly())
482 return Helper.lowerConstant(MI) == LegalizerHelper::Legalized;
483 return true;
484 }
485 case G_SET_FPMODE: {
486 // New FPSCR = (FPSCR & FPStatusBits) | (Modes & ~FPStatusBits)
487 LLT FPEnvTy = LLT::integer(SizeInBits: 32);
488 auto FPEnv = MRI.createGenericVirtualRegister(Ty: FPEnvTy);
489 Register Modes = MI.getOperand(i: 0).getReg();
490 MIRBuilder.buildGetFPEnv(Dst: FPEnv);
491 auto StatusBitMask = MIRBuilder.buildConstant(Res: FPEnvTy, Val: ARM::FPStatusBits);
492 auto StatusBits = MIRBuilder.buildAnd(Dst: FPEnvTy, Src0: FPEnv, Src1: StatusBitMask);
493 auto NotStatusBitMask =
494 MIRBuilder.buildConstant(Res: FPEnvTy, Val: ~ARM::FPStatusBits);
495 auto FPModeBits = MIRBuilder.buildAnd(Dst: FPEnvTy, Src0: Modes, Src1: NotStatusBitMask);
496 auto NewFPSCR = MIRBuilder.buildOr(Dst: FPEnvTy, Src0: StatusBits, Src1: FPModeBits);
497 MIRBuilder.buildSetFPEnv(Src: NewFPSCR);
498 break;
499 }
500 case G_RESET_FPMODE: {
501 // To get the default FP mode all control bits are cleared:
502 // FPSCR = FPSCR & (FPStatusBits | FPReservedBits)
503 LLT FPEnvTy = LLT::integer(SizeInBits: 32);
504 auto FPEnv = MIRBuilder.buildGetFPEnv(Dst: FPEnvTy);
505 auto NotModeBitMask = MIRBuilder.buildConstant(
506 Res: FPEnvTy, Val: ARM::FPStatusBits | ARM::FPReservedBits);
507 auto NewFPSCR = MIRBuilder.buildAnd(Dst: FPEnvTy, Src0: FPEnv, Src1: NotModeBitMask);
508 MIRBuilder.buildSetFPEnv(Src: NewFPSCR);
509 break;
510 }
511 }
512
513 MI.eraseFromParent();
514 return true;
515}
516