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