xref: /llvm-project/llvm/lib/Target/ARM/ARMLegalizerInfo.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
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/LowLevelType.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/TargetOpcodes.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/Type.h"
23 
24 using namespace llvm;
25 using namespace LegalizeActions;
26 
27 /// FIXME: The following static functions are SizeChangeStrategy functions
28 /// that are meant to temporarily mimic the behaviour of the old legalization
29 /// based on doubling/halving non-legal types as closely as possible. This is
30 /// not entirly possible as only legalizing the types that are exactly a power
31 /// of 2 times the size of the legal types would require specifying all those
32 /// sizes explicitly.
33 /// In practice, not specifying those isn't a problem, and the below functions
34 /// should disappear quickly as we add support for legalizing non-power-of-2
35 /// sized types further.
36 static void
37 addAndInterleaveWithUnsupported(LegalizerInfo::SizeAndActionsVec &result,
38                                 const LegalizerInfo::SizeAndActionsVec &v) {
39   for (unsigned i = 0; i < v.size(); ++i) {
40     result.push_back(v[i]);
41     if (i + 1 < v[i].first && i + 1 < v.size() &&
42         v[i + 1].first != v[i].first + 1)
43       result.push_back({v[i].first + 1, Unsupported});
44   }
45 }
46 
47 static LegalizerInfo::SizeAndActionsVec
48 widen_8_16(const LegalizerInfo::SizeAndActionsVec &v) {
49   assert(v.size() >= 1);
50   assert(v[0].first > 17);
51   LegalizerInfo::SizeAndActionsVec result = {{1, Unsupported},
52                                              {8, WidenScalar},
53                                              {9, Unsupported},
54                                              {16, WidenScalar},
55                                              {17, Unsupported}};
56   addAndInterleaveWithUnsupported(result, v);
57   auto Largest = result.back().first;
58   result.push_back({Largest + 1, Unsupported});
59   return result;
60 }
61 
62 static bool AEABI(const ARMSubtarget &ST) {
63   return ST.isTargetAEABI() || ST.isTargetGNUAEABI() || ST.isTargetMuslAEABI();
64 }
65 
66 ARMLegalizerInfo::ARMLegalizerInfo(const ARMSubtarget &ST) {
67   using namespace TargetOpcode;
68 
69   const LLT p0 = LLT::pointer(0, 32);
70 
71   const LLT s1 = LLT::scalar(1);
72   const LLT s8 = LLT::scalar(8);
73   const LLT s16 = LLT::scalar(16);
74   const LLT s32 = LLT::scalar(32);
75   const LLT s64 = LLT::scalar(64);
76 
77   if (ST.isThumb1Only()) {
78     // Thumb1 is not supported yet.
79     computeTables();
80     verify(*ST.getInstrInfo());
81     return;
82   }
83 
84   getActionDefinitionsBuilder({G_SEXT, G_ZEXT, G_ANYEXT})
85       .legalForCartesianProduct({s32}, {s1, s8, s16});
86 
87   getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
88       .legalFor({s32})
89       .minScalar(0, s32);
90 
91   getActionDefinitionsBuilder(G_INTTOPTR).legalFor({{p0, s32}});
92   getActionDefinitionsBuilder(G_PTRTOINT).legalFor({{s32, p0}});
93 
94   getActionDefinitionsBuilder(G_CONSTANT)
95       .legalFor({s32, p0})
96       .clampScalar(0, s32, s32);
97 
98   // We're keeping these builders around because we'll want to add support for
99   // floating point to them.
100   auto &LoadStoreBuilder =
101       getActionDefinitionsBuilder({G_LOAD, G_STORE})
102           .legalForTypesWithMemSize({
103               {s1, p0, 8},
104               {s8, p0, 8},
105               {s16, p0, 16},
106               {s32, p0, 32},
107               {p0, p0, 32}});
108 
109   if (ST.isThumb()) {
110     // FIXME: merge with the code for non-Thumb.
111     computeTables();
112     verify(*ST.getInstrInfo());
113     return;
114   }
115 
116   getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0});
117   getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0});
118 
119   if (ST.hasDivideInARMMode())
120     getActionDefinitionsBuilder({G_SDIV, G_UDIV})
121         .legalFor({s32})
122         .clampScalar(0, s32, s32);
123   else
124     getActionDefinitionsBuilder({G_SDIV, G_UDIV})
125         .libcallFor({s32})
126         .clampScalar(0, s32, s32);
127 
128   for (unsigned Op : {G_SREM, G_UREM}) {
129     setLegalizeScalarToDifferentSizeStrategy(Op, 0, widen_8_16);
130     if (ST.hasDivideInARMMode())
131       setAction({Op, s32}, Lower);
132     else if (AEABI(ST))
133       setAction({Op, s32}, Custom);
134     else
135       setAction({Op, s32}, Libcall);
136   }
137 
138   getActionDefinitionsBuilder({G_ASHR, G_LSHR, G_SHL}).legalFor({s32});
139 
140   if (ST.hasV5TOps()) {
141     getActionDefinitionsBuilder(G_CTLZ)
142         .legalFor({s32})
143         .clampScalar(0, s32, s32);
144     getActionDefinitionsBuilder(G_CTLZ_ZERO_UNDEF)
145         .lowerFor({s32})
146         .clampScalar(0, s32, s32);
147   } else {
148     getActionDefinitionsBuilder(G_CTLZ_ZERO_UNDEF)
149         .libcallFor({s32})
150         .clampScalar(0, s32, s32);
151     getActionDefinitionsBuilder(G_CTLZ)
152         .lowerFor({s32})
153         .clampScalar(0, s32, s32);
154   }
155 
156   getActionDefinitionsBuilder(G_GEP).legalFor({{p0, s32}});
157 
158   getActionDefinitionsBuilder(G_SELECT).legalForCartesianProduct({s32, p0},
159                                                                  {s1});
160 
161   getActionDefinitionsBuilder(G_BRCOND).legalFor({s1});
162 
163   getActionDefinitionsBuilder(G_ICMP)
164       .legalForCartesianProduct({s1}, {s32, p0})
165       .minScalar(1, s32);
166 
167   // We're keeping these builders around because we'll want to add support for
168   // floating point to them.
169   auto &PhiBuilder =
170       getActionDefinitionsBuilder(G_PHI).legalFor({s32, p0}).minScalar(0, s32);
171 
172   if (!ST.useSoftFloat() && ST.hasVFP2()) {
173     getActionDefinitionsBuilder(
174         {G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FCONSTANT, G_FNEG})
175         .legalFor({s32, s64});
176 
177     LoadStoreBuilder.legalFor({{s64, p0}});
178     PhiBuilder.legalFor({s64});
179 
180     getActionDefinitionsBuilder(G_FCMP).legalForCartesianProduct({s1},
181                                                                  {s32, s64});
182 
183     getActionDefinitionsBuilder(G_MERGE_VALUES).legalFor({{s64, s32}});
184     getActionDefinitionsBuilder(G_UNMERGE_VALUES).legalFor({{s32, s64}});
185 
186     getActionDefinitionsBuilder(G_FPEXT).legalFor({{s64, s32}});
187     getActionDefinitionsBuilder(G_FPTRUNC).legalFor({{s32, s64}});
188 
189     getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
190         .legalForCartesianProduct({s32}, {s32, s64});
191     getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
192         .legalForCartesianProduct({s32, s64}, {s32});
193   } else {
194     getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMUL, G_FDIV})
195         .libcallFor({s32, s64});
196 
197     LoadStoreBuilder.maxScalar(0, s32);
198 
199     for (auto Ty : {s32, s64})
200       setAction({G_FNEG, Ty}, Lower);
201 
202     getActionDefinitionsBuilder(G_FCONSTANT).customFor({s32, s64});
203 
204     getActionDefinitionsBuilder(G_FCMP).customForCartesianProduct({s1},
205                                                                   {s32, s64});
206 
207     if (AEABI(ST))
208       setFCmpLibcallsAEABI();
209     else
210       setFCmpLibcallsGNU();
211 
212     getActionDefinitionsBuilder(G_FPEXT).libcallFor({{s64, s32}});
213     getActionDefinitionsBuilder(G_FPTRUNC).libcallFor({{s32, s64}});
214 
215     getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI})
216         .libcallForCartesianProduct({s32}, {s32, s64});
217     getActionDefinitionsBuilder({G_SITOFP, G_UITOFP})
218         .libcallForCartesianProduct({s32, s64}, {s32});
219   }
220 
221   if (!ST.useSoftFloat() && ST.hasVFP4())
222     getActionDefinitionsBuilder(G_FMA).legalFor({s32, s64});
223   else
224     getActionDefinitionsBuilder(G_FMA).libcallFor({s32, s64});
225 
226   getActionDefinitionsBuilder({G_FREM, G_FPOW}).libcallFor({s32, s64});
227 
228   computeTables();
229   verify(*ST.getInstrInfo());
230 }
231 
232 void ARMLegalizerInfo::setFCmpLibcallsAEABI() {
233   // FCMP_TRUE and FCMP_FALSE don't need libcalls, they should be
234   // default-initialized.
235   FCmp32Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
236   FCmp32Libcalls[CmpInst::FCMP_OEQ] = {
237       {RTLIB::OEQ_F32, CmpInst::BAD_ICMP_PREDICATE}};
238   FCmp32Libcalls[CmpInst::FCMP_OGE] = {
239       {RTLIB::OGE_F32, CmpInst::BAD_ICMP_PREDICATE}};
240   FCmp32Libcalls[CmpInst::FCMP_OGT] = {
241       {RTLIB::OGT_F32, CmpInst::BAD_ICMP_PREDICATE}};
242   FCmp32Libcalls[CmpInst::FCMP_OLE] = {
243       {RTLIB::OLE_F32, CmpInst::BAD_ICMP_PREDICATE}};
244   FCmp32Libcalls[CmpInst::FCMP_OLT] = {
245       {RTLIB::OLT_F32, CmpInst::BAD_ICMP_PREDICATE}};
246   FCmp32Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::O_F32, CmpInst::ICMP_EQ}};
247   FCmp32Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F32, CmpInst::ICMP_EQ}};
248   FCmp32Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F32, CmpInst::ICMP_EQ}};
249   FCmp32Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F32, CmpInst::ICMP_EQ}};
250   FCmp32Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F32, CmpInst::ICMP_EQ}};
251   FCmp32Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::UNE_F32, CmpInst::ICMP_EQ}};
252   FCmp32Libcalls[CmpInst::FCMP_UNO] = {
253       {RTLIB::UO_F32, CmpInst::BAD_ICMP_PREDICATE}};
254   FCmp32Libcalls[CmpInst::FCMP_ONE] = {
255       {RTLIB::OGT_F32, CmpInst::BAD_ICMP_PREDICATE},
256       {RTLIB::OLT_F32, CmpInst::BAD_ICMP_PREDICATE}};
257   FCmp32Libcalls[CmpInst::FCMP_UEQ] = {
258       {RTLIB::OEQ_F32, CmpInst::BAD_ICMP_PREDICATE},
259       {RTLIB::UO_F32, CmpInst::BAD_ICMP_PREDICATE}};
260 
261   FCmp64Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
262   FCmp64Libcalls[CmpInst::FCMP_OEQ] = {
263       {RTLIB::OEQ_F64, CmpInst::BAD_ICMP_PREDICATE}};
264   FCmp64Libcalls[CmpInst::FCMP_OGE] = {
265       {RTLIB::OGE_F64, CmpInst::BAD_ICMP_PREDICATE}};
266   FCmp64Libcalls[CmpInst::FCMP_OGT] = {
267       {RTLIB::OGT_F64, CmpInst::BAD_ICMP_PREDICATE}};
268   FCmp64Libcalls[CmpInst::FCMP_OLE] = {
269       {RTLIB::OLE_F64, CmpInst::BAD_ICMP_PREDICATE}};
270   FCmp64Libcalls[CmpInst::FCMP_OLT] = {
271       {RTLIB::OLT_F64, CmpInst::BAD_ICMP_PREDICATE}};
272   FCmp64Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::O_F64, CmpInst::ICMP_EQ}};
273   FCmp64Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F64, CmpInst::ICMP_EQ}};
274   FCmp64Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F64, CmpInst::ICMP_EQ}};
275   FCmp64Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F64, CmpInst::ICMP_EQ}};
276   FCmp64Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F64, CmpInst::ICMP_EQ}};
277   FCmp64Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::UNE_F64, CmpInst::ICMP_EQ}};
278   FCmp64Libcalls[CmpInst::FCMP_UNO] = {
279       {RTLIB::UO_F64, CmpInst::BAD_ICMP_PREDICATE}};
280   FCmp64Libcalls[CmpInst::FCMP_ONE] = {
281       {RTLIB::OGT_F64, CmpInst::BAD_ICMP_PREDICATE},
282       {RTLIB::OLT_F64, CmpInst::BAD_ICMP_PREDICATE}};
283   FCmp64Libcalls[CmpInst::FCMP_UEQ] = {
284       {RTLIB::OEQ_F64, CmpInst::BAD_ICMP_PREDICATE},
285       {RTLIB::UO_F64, CmpInst::BAD_ICMP_PREDICATE}};
286 }
287 
288 void ARMLegalizerInfo::setFCmpLibcallsGNU() {
289   // FCMP_TRUE and FCMP_FALSE don't need libcalls, they should be
290   // default-initialized.
291   FCmp32Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
292   FCmp32Libcalls[CmpInst::FCMP_OEQ] = {{RTLIB::OEQ_F32, CmpInst::ICMP_EQ}};
293   FCmp32Libcalls[CmpInst::FCMP_OGE] = {{RTLIB::OGE_F32, CmpInst::ICMP_SGE}};
294   FCmp32Libcalls[CmpInst::FCMP_OGT] = {{RTLIB::OGT_F32, CmpInst::ICMP_SGT}};
295   FCmp32Libcalls[CmpInst::FCMP_OLE] = {{RTLIB::OLE_F32, CmpInst::ICMP_SLE}};
296   FCmp32Libcalls[CmpInst::FCMP_OLT] = {{RTLIB::OLT_F32, CmpInst::ICMP_SLT}};
297   FCmp32Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::O_F32, CmpInst::ICMP_EQ}};
298   FCmp32Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F32, CmpInst::ICMP_SGE}};
299   FCmp32Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F32, CmpInst::ICMP_SGT}};
300   FCmp32Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F32, CmpInst::ICMP_SLE}};
301   FCmp32Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F32, CmpInst::ICMP_SLT}};
302   FCmp32Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::UNE_F32, CmpInst::ICMP_NE}};
303   FCmp32Libcalls[CmpInst::FCMP_UNO] = {{RTLIB::UO_F32, CmpInst::ICMP_NE}};
304   FCmp32Libcalls[CmpInst::FCMP_ONE] = {{RTLIB::OGT_F32, CmpInst::ICMP_SGT},
305                                        {RTLIB::OLT_F32, CmpInst::ICMP_SLT}};
306   FCmp32Libcalls[CmpInst::FCMP_UEQ] = {{RTLIB::OEQ_F32, CmpInst::ICMP_EQ},
307                                        {RTLIB::UO_F32, CmpInst::ICMP_NE}};
308 
309   FCmp64Libcalls.resize(CmpInst::LAST_FCMP_PREDICATE + 1);
310   FCmp64Libcalls[CmpInst::FCMP_OEQ] = {{RTLIB::OEQ_F64, CmpInst::ICMP_EQ}};
311   FCmp64Libcalls[CmpInst::FCMP_OGE] = {{RTLIB::OGE_F64, CmpInst::ICMP_SGE}};
312   FCmp64Libcalls[CmpInst::FCMP_OGT] = {{RTLIB::OGT_F64, CmpInst::ICMP_SGT}};
313   FCmp64Libcalls[CmpInst::FCMP_OLE] = {{RTLIB::OLE_F64, CmpInst::ICMP_SLE}};
314   FCmp64Libcalls[CmpInst::FCMP_OLT] = {{RTLIB::OLT_F64, CmpInst::ICMP_SLT}};
315   FCmp64Libcalls[CmpInst::FCMP_ORD] = {{RTLIB::O_F64, CmpInst::ICMP_EQ}};
316   FCmp64Libcalls[CmpInst::FCMP_UGE] = {{RTLIB::OLT_F64, CmpInst::ICMP_SGE}};
317   FCmp64Libcalls[CmpInst::FCMP_UGT] = {{RTLIB::OLE_F64, CmpInst::ICMP_SGT}};
318   FCmp64Libcalls[CmpInst::FCMP_ULE] = {{RTLIB::OGT_F64, CmpInst::ICMP_SLE}};
319   FCmp64Libcalls[CmpInst::FCMP_ULT] = {{RTLIB::OGE_F64, CmpInst::ICMP_SLT}};
320   FCmp64Libcalls[CmpInst::FCMP_UNE] = {{RTLIB::UNE_F64, CmpInst::ICMP_NE}};
321   FCmp64Libcalls[CmpInst::FCMP_UNO] = {{RTLIB::UO_F64, CmpInst::ICMP_NE}};
322   FCmp64Libcalls[CmpInst::FCMP_ONE] = {{RTLIB::OGT_F64, CmpInst::ICMP_SGT},
323                                        {RTLIB::OLT_F64, CmpInst::ICMP_SLT}};
324   FCmp64Libcalls[CmpInst::FCMP_UEQ] = {{RTLIB::OEQ_F64, CmpInst::ICMP_EQ},
325                                        {RTLIB::UO_F64, CmpInst::ICMP_NE}};
326 }
327 
328 ARMLegalizerInfo::FCmpLibcallsList
329 ARMLegalizerInfo::getFCmpLibcalls(CmpInst::Predicate Predicate,
330                                   unsigned Size) const {
331   assert(CmpInst::isFPPredicate(Predicate) && "Unsupported FCmp predicate");
332   if (Size == 32)
333     return FCmp32Libcalls[Predicate];
334   if (Size == 64)
335     return FCmp64Libcalls[Predicate];
336   llvm_unreachable("Unsupported size for FCmp predicate");
337 }
338 
339 bool ARMLegalizerInfo::legalizeCustom(MachineInstr &MI,
340                                       MachineRegisterInfo &MRI,
341                                       MachineIRBuilder &MIRBuilder,
342                                       GISelChangeObserver &Observer) const {
343   using namespace TargetOpcode;
344 
345   MIRBuilder.setInstr(MI);
346   LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
347 
348   switch (MI.getOpcode()) {
349   default:
350     return false;
351   case G_SREM:
352   case G_UREM: {
353     unsigned OriginalResult = MI.getOperand(0).getReg();
354     auto Size = MRI.getType(OriginalResult).getSizeInBits();
355     if (Size != 32)
356       return false;
357 
358     auto Libcall =
359         MI.getOpcode() == G_SREM ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32;
360 
361     // Our divmod libcalls return a struct containing the quotient and the
362     // remainder. We need to create a virtual register for it.
363     Type *ArgTy = Type::getInt32Ty(Ctx);
364     StructType *RetTy = StructType::get(Ctx, {ArgTy, ArgTy}, /* Packed */ true);
365     auto RetVal = MRI.createGenericVirtualRegister(
366         getLLTForType(*RetTy, MIRBuilder.getMF().getDataLayout()));
367 
368     auto Status = createLibcall(MIRBuilder, Libcall, {RetVal, RetTy},
369                                 {{MI.getOperand(1).getReg(), ArgTy},
370                                  {MI.getOperand(2).getReg(), ArgTy}});
371     if (Status != LegalizerHelper::Legalized)
372       return false;
373 
374     // The remainder is the second result of divmod. Split the return value into
375     // a new, unused register for the quotient and the destination of the
376     // original instruction for the remainder.
377     MIRBuilder.buildUnmerge(
378         {MRI.createGenericVirtualRegister(LLT::scalar(32)), OriginalResult},
379         RetVal);
380     break;
381   }
382   case G_FCMP: {
383     assert(MRI.getType(MI.getOperand(2).getReg()) ==
384                MRI.getType(MI.getOperand(3).getReg()) &&
385            "Mismatched operands for G_FCMP");
386     auto OpSize = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
387 
388     auto OriginalResult = MI.getOperand(0).getReg();
389     auto Predicate =
390         static_cast<CmpInst::Predicate>(MI.getOperand(1).getPredicate());
391     auto Libcalls = getFCmpLibcalls(Predicate, OpSize);
392 
393     if (Libcalls.empty()) {
394       assert((Predicate == CmpInst::FCMP_TRUE ||
395               Predicate == CmpInst::FCMP_FALSE) &&
396              "Predicate needs libcalls, but none specified");
397       MIRBuilder.buildConstant(OriginalResult,
398                                Predicate == CmpInst::FCMP_TRUE ? 1 : 0);
399       MI.eraseFromParent();
400       return true;
401     }
402 
403     assert((OpSize == 32 || OpSize == 64) && "Unsupported operand size");
404     auto *ArgTy = OpSize == 32 ? Type::getFloatTy(Ctx) : Type::getDoubleTy(Ctx);
405     auto *RetTy = Type::getInt32Ty(Ctx);
406 
407     SmallVector<unsigned, 2> Results;
408     for (auto Libcall : Libcalls) {
409       auto LibcallResult = MRI.createGenericVirtualRegister(LLT::scalar(32));
410       auto Status =
411           createLibcall(MIRBuilder, Libcall.LibcallID, {LibcallResult, RetTy},
412                         {{MI.getOperand(2).getReg(), ArgTy},
413                          {MI.getOperand(3).getReg(), ArgTy}});
414 
415       if (Status != LegalizerHelper::Legalized)
416         return false;
417 
418       auto ProcessedResult =
419           Libcalls.size() == 1
420               ? OriginalResult
421               : MRI.createGenericVirtualRegister(MRI.getType(OriginalResult));
422 
423       // We have a result, but we need to transform it into a proper 1-bit 0 or
424       // 1, taking into account the different peculiarities of the values
425       // returned by the comparison functions.
426       CmpInst::Predicate ResultPred = Libcall.Predicate;
427       if (ResultPred == CmpInst::BAD_ICMP_PREDICATE) {
428         // We have a nice 0 or 1, and we just need to truncate it back to 1 bit
429         // to keep the types consistent.
430         MIRBuilder.buildTrunc(ProcessedResult, LibcallResult);
431       } else {
432         // We need to compare against 0.
433         assert(CmpInst::isIntPredicate(ResultPred) && "Unsupported predicate");
434         auto Zero = MRI.createGenericVirtualRegister(LLT::scalar(32));
435         MIRBuilder.buildConstant(Zero, 0);
436         MIRBuilder.buildICmp(ResultPred, ProcessedResult, LibcallResult, Zero);
437       }
438       Results.push_back(ProcessedResult);
439     }
440 
441     if (Results.size() != 1) {
442       assert(Results.size() == 2 && "Unexpected number of results");
443       MIRBuilder.buildOr(OriginalResult, Results[0], Results[1]);
444     }
445     break;
446   }
447   case G_FCONSTANT: {
448     // Convert to integer constants, while preserving the binary representation.
449     auto AsInteger =
450         MI.getOperand(1).getFPImm()->getValueAPF().bitcastToAPInt();
451     MIRBuilder.buildConstant(MI.getOperand(0).getReg(),
452                              *ConstantInt::get(Ctx, AsInteger));
453     break;
454   }
455   }
456 
457   MI.eraseFromParent();
458   return true;
459 }
460