LLVM 24.0.0git
ARMLegalizerInfo.cpp
Go to the documentation of this file.
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"
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
33 using namespace TargetOpcode;
34
35 const LLT p0 = LLT::pointer(0, 32);
36
37 const LLT s1 = LLT::scalar(1);
38 const LLT s8 = LLT::scalar(8);
39 const LLT s16 = LLT::scalar(16);
40 const LLT s32 = LLT::scalar(32);
41 const LLT s64 = LLT::scalar(64);
42
43 const LLT i32 = LLT::integer(32);
44 const LLT i64 = LLT::integer(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(*ST.getInstrInfo());
51 return;
52 }
53
55 .legalFor({{s32, s32}})
56 .legalFor(ST.hasFPRegs(), {{s64, s64}})
57 .maxScalar(0, ST.hasFPRegs() ? i64 : i32);
58
59 getActionDefinitionsBuilder(G_MERGE_VALUES).legalFor({{s64, s32}});
60 getActionDefinitionsBuilder(G_UNMERGE_VALUES).legalFor({{s32, s64}});
61
62 getActionDefinitionsBuilder({G_SEXT, G_ZEXT, G_ANYEXT})
63 .legalForCartesianProduct({s8, s16, s32}, {s1, s8, s16});
64
65 getActionDefinitionsBuilder(G_TRUNC).legalForCartesianProduct({s1, s8, s16},
66 {s8, s16, s32});
67
68 getActionDefinitionsBuilder(G_SEXT_INREG).lower();
69
70 getActionDefinitionsBuilder({G_MUL, G_AND, G_OR, G_XOR})
71 .legalFor({s32})
72 .clampScalar(0, s32, s32);
73
74 if (ST.hasNEON())
75 getActionDefinitionsBuilder({G_ADD, G_SUB})
76 .legalFor({s32, s64})
77 .minScalar(0, s32);
78 else
79 getActionDefinitionsBuilder({G_ADD, G_SUB})
80 .legalFor({s32})
81 .minScalar(0, s32);
82
83 getActionDefinitionsBuilder({G_ASHR, G_LSHR, G_SHL})
84 .legalFor({{s32, s32}})
85 .minScalar(0, s32)
86 .clampScalar(1, s32, s32);
87
88 bool HasHWDivide = (!ST.isThumb() && ST.hasDivideInARMMode()) ||
89 (ST.isThumb() && ST.hasDivideInThumbMode());
90 if (HasHWDivide)
91 getActionDefinitionsBuilder({G_SDIV, G_UDIV})
92 .legalFor({s32})
93 .clampScalar(0, s32, s32);
94 else
95 getActionDefinitionsBuilder({G_SDIV, G_UDIV})
96 .libcallFor({s32})
97 .clampScalar(0, s32, s32);
98
99 auto &REMBuilder =
100 getActionDefinitionsBuilder({G_SREM, G_UREM}).minScalar(0, s32);
101 if (HasHWDivide)
102 REMBuilder.lowerFor({s32});
103 else if (AEABI(ST))
104 REMBuilder.customFor({s32});
105 else
106 REMBuilder.libcallFor({s32});
107
108 getActionDefinitionsBuilder(G_INTTOPTR)
109 .legalFor({{p0, s32}})
110 .minScalar(1, s32);
111 getActionDefinitionsBuilder(G_PTRTOINT)
112 .legalFor({{s32, p0}})
113 .minScalar(0, s32);
114
115 getActionDefinitionsBuilder(G_CONSTANT)
116 .customFor({s32, p0})
117 .clampScalar(0, s32, s32);
118
119 getActionDefinitionsBuilder(G_CONSTANT_POOL).legalFor({p0});
120
121 getActionDefinitionsBuilder(G_ICMP)
122 .legalForCartesianProduct({s1}, {s32, p0})
123 .minScalar(1, s32);
124
125 getActionDefinitionsBuilder(G_SELECT)
126 .legalForCartesianProduct({s32, p0}, {s1})
127 .minScalar(0, 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({G_LOAD, G_STORE})
132 .legalForTypesWithMemDesc({{s8, p0, s8, 8},
133 {s16, p0, s16, 8},
134 {s32, p0, s32, 8},
135 {p0, p0, p0, 8}})
136 .unsupportedIfMemSizeNotPow2();
137
138 getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
139 getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
140
141 auto &PhiBuilder =
142 getActionDefinitionsBuilder(G_PHI)
143 .legalFor({s32, p0})
144 .minScalar(0, s32);
145
146 getActionDefinitionsBuilder(G_PTR_ADD)
147 .legalFor({{p0, i32}})
148 .minScalar(1, i32);
149
150 getActionDefinitionsBuilder(G_BR).alwaysLegal();
151 getActionDefinitionsBuilder(G_BRCOND).legalFor({s1});
152
153 if (!ST.useSoftFloat() && ST.hasVFP2Base()) {
154 getActionDefinitionsBuilder(
155 {G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FCONSTANT, G_FNEG})
156 .legalFor({f32, f64});
157
158 LoadStoreBuilder
159 .legalForTypesWithMemDesc({{s64, p0, s64, 32}})
160 .maxScalar(0, s32);
161 PhiBuilder.legalFor({s64});
162
163 getActionDefinitionsBuilder(G_FCMP).legalForCartesianProduct({s1},
164 {f32, f64});
165
166 getActionDefinitionsBuilder(G_FPEXT).legalFor({{f64, f32}});
167 getActionDefinitionsBuilder(G_FPTRUNC).legalFor({{f32, f64}});
168
169 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
170 .legalForCartesianProduct({i32}, {f32, f64});
171 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
172 .legalForCartesianProduct({f32, f64}, {i32});
173
174 getActionDefinitionsBuilder({G_GET_FPENV, G_SET_FPENV, G_GET_FPMODE})
175 .legalFor({s32});
176 getActionDefinitionsBuilder(G_RESET_FPENV).alwaysLegal();
177 getActionDefinitionsBuilder(G_SET_FPMODE).customFor({s32});
178 getActionDefinitionsBuilder(G_RESET_FPMODE).custom();
179 } else {
180 getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV})
181 .libcallFor({f32, f64});
182
183 LoadStoreBuilder.maxScalar(0, s32);
184
185 getActionDefinitionsBuilder(G_FNEG).lowerFor({s32, s64});
186
187 getActionDefinitionsBuilder(G_FCONSTANT).lowerFor({s32, s64});
188
189 getActionDefinitionsBuilder(G_FCMP).customForCartesianProduct({s1},
190 {f32, f64});
191
192 if (AEABI(ST))
193 setFCmpLibcallsAEABI();
194 else
195 setFCmpLibcallsGNU();
196
197 getActionDefinitionsBuilder(G_FPEXT).libcallFor({{f64, f32}});
198 getActionDefinitionsBuilder(G_FPTRUNC).libcallFor({{f32, f64}});
199
200 getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
201 .libcallForCartesianProduct({i32}, {f32, f64});
202 getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
203 .libcallForCartesianProduct({f32, f64}, {i32});
204
205 getActionDefinitionsBuilder({G_GET_FPENV, G_SET_FPENV, G_RESET_FPENV})
206 .libcall();
207 getActionDefinitionsBuilder({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(G_FMA).legalFor({f32, f64});
216 else
217 getActionDefinitionsBuilder(G_FMA).libcallFor({f32, f64});
218
219 getActionDefinitionsBuilder({G_FREM, G_FPOW}).libcallFor({f32, f64});
220
221 if (ST.hasV5TOps() && !ST.isThumb1Only()) {
222 getActionDefinitionsBuilder(G_CTLZ)
223 .legalFor({s32, s32})
224 .clampScalar(1, s32, s32)
225 .clampScalar(0, s32, s32);
226 getActionDefinitionsBuilder(G_CTLZ_ZERO_POISON)
227 .lowerFor({s32, s32})
228 .clampScalar(1, s32, s32)
229 .clampScalar(0, s32, s32);
230 } else {
231 getActionDefinitionsBuilder(G_CTLZ_ZERO_POISON)
232 .libcallFor({s32, s32})
233 .clampScalar(1, s32, s32)
234 .clampScalar(0, s32, s32);
235 getActionDefinitionsBuilder(G_CTLZ)
236 .lowerFor({s32, s32})
237 .clampScalar(1, s32, s32)
238 .clampScalar(0, s32, s32);
239 }
240
241 verify(*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(CmpInst::LAST_FCMP_PREDICATE + 1);
248 FCmp32Libcalls[CmpInst::FCMP_OEQ] = {
249 {RTLIB::OEQ_F32, CmpInst::BAD_ICMP_PREDICATE}};
250 FCmp32Libcalls[CmpInst::FCMP_OGE] = {
251 {RTLIB::OGE_F32, CmpInst::BAD_ICMP_PREDICATE}};
252 FCmp32Libcalls[CmpInst::FCMP_OGT] = {
253 {RTLIB::OGT_F32, CmpInst::BAD_ICMP_PREDICATE}};
254 FCmp32Libcalls[CmpInst::FCMP_OLE] = {
255 {RTLIB::OLE_F32, CmpInst::BAD_ICMP_PREDICATE}};
256 FCmp32Libcalls[CmpInst::FCMP_OLT] = {
257 {RTLIB::OLT_F32, CmpInst::BAD_ICMP_PREDICATE}};
258 FCmp32Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::UO_F32, CmpInst::ICMP_EQ}};
259 FCmp32Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F32, CmpInst::ICMP_EQ}};
260 FCmp32Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F32, CmpInst::ICMP_EQ}};
261 FCmp32Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F32, CmpInst::ICMP_EQ}};
262 FCmp32Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F32, CmpInst::ICMP_EQ}};
263 // AEABI only provides ordered-equal; not-equal is the same call, inverted.
264 FCmp32Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::OEQ_F32, CmpInst::ICMP_EQ}};
265 FCmp32Libcalls[CmpInst::FCMP_UNO] = {
266 {RTLIB::UO_F32, CmpInst::BAD_ICMP_PREDICATE}};
267 FCmp32Libcalls[CmpInst::FCMP_ONE] = {
268 {RTLIB::OGT_F32, CmpInst::BAD_ICMP_PREDICATE},
269 {RTLIB::OLT_F32, CmpInst::BAD_ICMP_PREDICATE}};
270 FCmp32Libcalls[CmpInst::FCMP_UEQ] = {
271 {RTLIB::OEQ_F32, CmpInst::BAD_ICMP_PREDICATE},
272 {RTLIB::UO_F32, CmpInst::BAD_ICMP_PREDICATE}};
273
274 FCmp64Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
275 FCmp64Libcalls[CmpInst::FCMP_OEQ] = {
276 {RTLIB::OEQ_F64, CmpInst::BAD_ICMP_PREDICATE}};
277 FCmp64Libcalls[CmpInst::FCMP_OGE] = {
278 {RTLIB::OGE_F64, CmpInst::BAD_ICMP_PREDICATE}};
279 FCmp64Libcalls[CmpInst::FCMP_OGT] = {
280 {RTLIB::OGT_F64, CmpInst::BAD_ICMP_PREDICATE}};
281 FCmp64Libcalls[CmpInst::FCMP_OLE] = {
282 {RTLIB::OLE_F64, CmpInst::BAD_ICMP_PREDICATE}};
283 FCmp64Libcalls[CmpInst::FCMP_OLT] = {
284 {RTLIB::OLT_F64, CmpInst::BAD_ICMP_PREDICATE}};
285 FCmp64Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::UO_F64, CmpInst::ICMP_EQ}};
286 FCmp64Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F64, CmpInst::ICMP_EQ}};
287 FCmp64Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F64, CmpInst::ICMP_EQ}};
288 FCmp64Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F64, CmpInst::ICMP_EQ}};
289 FCmp64Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F64, CmpInst::ICMP_EQ}};
290 // AEABI only provides ordered-equal; not-equal is the same call, inverted.
291 FCmp64Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::OEQ_F64, CmpInst::ICMP_EQ}};
292 FCmp64Libcalls[CmpInst::FCMP_UNO] = {
293 {RTLIB::UO_F64, CmpInst::BAD_ICMP_PREDICATE}};
294 FCmp64Libcalls[CmpInst::FCMP_ONE] = {
295 {RTLIB::OGT_F64, CmpInst::BAD_ICMP_PREDICATE},
296 {RTLIB::OLT_F64, CmpInst::BAD_ICMP_PREDICATE}};
297 FCmp64Libcalls[CmpInst::FCMP_UEQ] = {
298 {RTLIB::OEQ_F64, CmpInst::BAD_ICMP_PREDICATE},
299 {RTLIB::UO_F64, 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(CmpInst::LAST_FCMP_PREDICATE + 1);
306 FCmp32Libcalls[CmpInst::FCMP_OEQ] = {
307 {RTLIB::FCMP3_PRED_OEQ_F32, CmpInst::ICMP_EQ}};
308 FCmp32Libcalls[CmpInst::FCMP_OGE] = {
309 {RTLIB::FCMP3_PRED_OGE_F32, CmpInst::ICMP_SGE}};
310 FCmp32Libcalls[CmpInst::FCMP_OGT] = {
311 {RTLIB::FCMP3_PRED_OGT_F32, CmpInst::ICMP_SGT}};
312 FCmp32Libcalls[CmpInst::FCMP_OLE] = {
313 {RTLIB::FCMP3_PRED_OLE_F32, CmpInst::ICMP_SLE}};
314 FCmp32Libcalls[CmpInst::FCMP_OLT] = {
315 {RTLIB::FCMP3_PRED_OLT_F32, CmpInst::ICMP_SLT}};
316 FCmp32Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::UO_F32, CmpInst::ICMP_EQ}};
317 FCmp32Libcalls[CmpInst::FCMP_UGE] = {
318 {RTLIB::FCMP3_PRED_OLT_F32, CmpInst::ICMP_SGE}};
319 FCmp32Libcalls[CmpInst::FCMP_UGT] = {
320 {RTLIB::FCMP3_PRED_OLE_F32, CmpInst::ICMP_SGT}};
321 FCmp32Libcalls[CmpInst::FCMP_ULE] = {
322 {RTLIB::FCMP3_PRED_OGT_F32, CmpInst::ICMP_SLE}};
323 FCmp32Libcalls[CmpInst::FCMP_ULT] = {
324 {RTLIB::FCMP3_PRED_OGE_F32, CmpInst::ICMP_SLT}};
325 FCmp32Libcalls[CmpInst::FCMP_UNE] = {
326 {RTLIB::FCMP3_PRED_UNE_F32, CmpInst::ICMP_NE}};
327 FCmp32Libcalls[CmpInst::FCMP_UNO] = {{RTLIB::UO_F32, CmpInst::ICMP_NE}};
328 FCmp32Libcalls[CmpInst::FCMP_ONE] = {
329 {RTLIB::FCMP3_PRED_OGT_F32, CmpInst::ICMP_SGT},
330 {RTLIB::FCMP3_PRED_OLT_F32, CmpInst::ICMP_SLT}};
331 FCmp32Libcalls[CmpInst::FCMP_UEQ] = {
332 {RTLIB::FCMP3_PRED_OEQ_F32, CmpInst::ICMP_EQ},
333 {RTLIB::UO_F32, CmpInst::ICMP_NE}};
334
335 FCmp64Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
336 FCmp64Libcalls[CmpInst::FCMP_OEQ] = {
337 {RTLIB::FCMP3_PRED_OEQ_F64, CmpInst::ICMP_EQ}};
338 FCmp64Libcalls[CmpInst::FCMP_OGE] = {
339 {RTLIB::FCMP3_PRED_OGE_F64, CmpInst::ICMP_SGE}};
340 FCmp64Libcalls[CmpInst::FCMP_OGT] = {
341 {RTLIB::FCMP3_PRED_OGT_F64, CmpInst::ICMP_SGT}};
342 FCmp64Libcalls[CmpInst::FCMP_OLE] = {
343 {RTLIB::FCMP3_PRED_OLE_F64, CmpInst::ICMP_SLE}};
344 FCmp64Libcalls[CmpInst::FCMP_OLT] = {
345 {RTLIB::FCMP3_PRED_OLT_F64, CmpInst::ICMP_SLT}};
346 FCmp64Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::UO_F64, CmpInst::ICMP_EQ}};
347 FCmp64Libcalls[CmpInst::FCMP_UGE] = {
348 {RTLIB::FCMP3_PRED_OLT_F64, CmpInst::ICMP_SGE}};
349 FCmp64Libcalls[CmpInst::FCMP_UGT] = {
350 {RTLIB::FCMP3_PRED_OLE_F64, CmpInst::ICMP_SGT}};
351 FCmp64Libcalls[CmpInst::FCMP_ULE] = {
352 {RTLIB::FCMP3_PRED_OGT_F64, CmpInst::ICMP_SLE}};
353 FCmp64Libcalls[CmpInst::FCMP_ULT] = {
354 {RTLIB::FCMP3_PRED_OGE_F64, CmpInst::ICMP_SLT}};
355 FCmp64Libcalls[CmpInst::FCMP_UNE] = {
356 {RTLIB::FCMP3_PRED_UNE_F64, CmpInst::ICMP_NE}};
357 FCmp64Libcalls[CmpInst::FCMP_UNO] = {{RTLIB::UO_F64, CmpInst::ICMP_NE}};
358 FCmp64Libcalls[CmpInst::FCMP_ONE] = {
359 {RTLIB::FCMP3_PRED_OGT_F64, CmpInst::ICMP_SGT},
360 {RTLIB::FCMP3_PRED_OLT_F64, CmpInst::ICMP_SLT}};
361 FCmp64Libcalls[CmpInst::FCMP_UEQ] = {
362 {RTLIB::FCMP3_PRED_OEQ_F64, CmpInst::ICMP_EQ},
363 {RTLIB::UO_F64, 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
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(0).getReg();
391 auto Size = MRI.getType(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(Ctx);
402 StructType *RetTy = StructType::get(Ctx, {ArgTy, ArgTy}, /* Packed */ true);
403 Register RetRegs[] = {MRI.createGenericVirtualRegister(LLT::integer(32)),
404 OriginalResult};
405 auto Status = Helper.createLibcall(Libcall, {RetRegs, RetTy, 0},
406 {{MI.getOperand(1).getReg(), ArgTy, 0},
407 {MI.getOperand(2).getReg(), ArgTy, 0}},
408 LocObserver, &MI);
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(MI.getOperand(2).getReg()).getSizeInBits();
418
419 auto OriginalResult = MI.getOperand(0).getReg();
420 auto Predicate =
421 static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
422 auto Libcalls = getFCmpLibcalls(Predicate, 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(OriginalResult,
429 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(Ctx) : Type::getDoubleTy(Ctx);
436 auto *RetTy = Type::getInt32Ty(Ctx);
437
439 for (auto Libcall : Libcalls) {
440 auto LibcallResult = MRI.createGenericVirtualRegister(LLT::integer(32));
441 auto Status =
442 Helper.createLibcall(Libcall.LibcallID, {LibcallResult, RetTy, 0},
443 {{MI.getOperand(2).getReg(), ArgTy, 0},
444 {MI.getOperand(3).getReg(), ArgTy, 0}},
445 LocObserver, &MI);
446
448 return false;
449
450 auto ProcessedResult =
451 Libcalls.size() == 1
452 ? OriginalResult
453 : MRI.createGenericVirtualRegister(MRI.getType(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(ProcessedResult, LibcallResult);
463 } else {
464 // We need to compare against 0.
465 assert(CmpInst::isIntPredicate(ResultPred) && "Unsupported predicate");
466 auto Zero = MIRBuilder.buildConstant(LLT::integer(32), 0);
467 MIRBuilder.buildICmp(ResultPred, ProcessedResult, LibcallResult, Zero);
468 }
469 Results.push_back(ProcessedResult);
470 }
471
472 if (Results.size() != 1) {
473 assert(Results.size() == 2 && "Unexpected number of results");
474 MIRBuilder.buildOr(OriginalResult, Results[0], Results[1]);
475 }
476 break;
477 }
478 case G_CONSTANT: {
479 const ConstantInt *ConstVal = MI.getOperand(1).getCImm();
480 uint64_t ImmVal = ConstVal->getZExtValue();
481 if (ConstantMaterializationCost(ImmVal, &ST) > 2 && !ST.genExecuteOnly())
483 return true;
484 }
485 case G_SET_FPMODE: {
486 // New FPSCR = (FPSCR & FPStatusBits) | (Modes & ~FPStatusBits)
487 LLT FPEnvTy = LLT::integer(32);
488 auto FPEnv = MRI.createGenericVirtualRegister(FPEnvTy);
489 Register Modes = MI.getOperand(0).getReg();
490 MIRBuilder.buildGetFPEnv(FPEnv);
491 auto StatusBitMask = MIRBuilder.buildConstant(FPEnvTy, ARM::FPStatusBits);
492 auto StatusBits = MIRBuilder.buildAnd(FPEnvTy, FPEnv, StatusBitMask);
493 auto NotStatusBitMask =
494 MIRBuilder.buildConstant(FPEnvTy, ~ARM::FPStatusBits);
495 auto FPModeBits = MIRBuilder.buildAnd(FPEnvTy, Modes, NotStatusBitMask);
496 auto NewFPSCR = MIRBuilder.buildOr(FPEnvTy, StatusBits, FPModeBits);
497 MIRBuilder.buildSetFPEnv(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(32);
504 auto FPEnv = MIRBuilder.buildGetFPEnv(FPEnvTy);
505 auto NotModeBitMask = MIRBuilder.buildConstant(
507 auto NewFPSCR = MIRBuilder.buildAnd(FPEnvTy, FPEnv, NotModeBitMask);
508 MIRBuilder.buildSetFPEnv(NewFPSCR);
509 break;
510 }
511 }
512
513 MI.eraseFromParent();
514 return true;
515}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file describes how to lower LLVM calls to machine code calls.
static bool AEABI(const ARMSubtarget &ST)
This file declares the targeting of the Machinelegalizer class for ARM.
Function Alias Analysis Results
IRTranslator LLVM IR MI
Implement a low-level type suitable for MachineInstr level instruction selection.
This file declares the MachineIRBuilder class.
ppc ctr loops verify
MachineRegisterInfo & MRI
ARMLegalizerInfo(const ARMSubtarget &ST)
bool legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI, LostDebugLocObserver &LocObserver) const override
Called for instructions with the Custom LegalizationAction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
bool isFPPredicate() const
Definition InstrTypes.h:845
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:839
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:352
static constexpr LLT float64()
Get a 64-bit IEEE double value.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static LLT integer(unsigned SizeInBits)
static constexpr LLT float32()
Get a 32-bit IEEE float value.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LegalizeRuleSet & legalFor(std::initializer_list< LLT > Types)
The instruction is legal when type index 0 is any type in the given list.
LegalizeRuleSet & lower()
The instruction is lowered.
LegalizeRuleSet & legalForCartesianProduct(std::initializer_list< LLT > Types)
The instruction is legal when type indexes 0 and 1 are both in the given list.
@ Legalized
Instruction has been legalized and the MachineFunction changed.
LLVM_ABI LegalizeResult createLibcall(const char *Name, const CallLowering::ArgInfo &Result, ArrayRef< CallLowering::ArgInfo > Args, CallingConv::ID CC, LostDebugLocObserver &LocObserver, MachineInstr *MI=nullptr) const
Helper function that creates a libcall to the given Name using the given calling convention CC.
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions.
LLVM_ABI LegalizeResult lowerConstant(MachineInstr &MI)
LegalizeRuleSet & getActionDefinitionsBuilder(unsigned Opcode)
Get the action definition builder for the given opcode.
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_TRUNC Op.
MachineInstrBuilder buildGetFPEnv(const DstOp &Dst)
Build and insert Dst = G_GET_FPENV.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_OR Op0, Op1.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
MachineInstrBuilder buildSetFPEnv(const SrcOp &Src)
Build and insert G_SET_FPENV Src.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:467
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:277
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:276
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const unsigned FPStatusBits
const unsigned FPReservedBits
@ Libcall
The operation should be implemented as a call to some kind of runtime support library.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Invariant opcodes: All instruction sets have these as their low opcodes.
This is an optimization pass for GlobalISel generic memory operations.
unsigned ConstantMaterializationCost(unsigned Val, const ARMSubtarget *Subtarget, bool ForCodesize=false)
Returns the number of instructions required to materialize the given constant in a register,...