xref: /llvm-project/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp (revision 4b980cc9ca08a0b95b4ac6994770155a48881729)
1 //===-- lib/CodeGen/GlobalISel/InlineAsmLowering.cpp ----------------------===//
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 ///
9 /// \file
10 /// This file implements the lowering from LLVM IR inline asm to MIR INLINEASM
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/GlobalISel/InlineAsmLowering.h"
15 #include "llvm/CodeGen/Analysis.h"
16 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
17 #include "llvm/CodeGen/GlobalISel/Utils.h"
18 #include "llvm/CodeGen/MachineOperand.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 
26 #define DEBUG_TYPE "inline-asm-lowering"
27 
28 using namespace llvm;
29 
30 void InlineAsmLowering::anchor() {}
31 
32 namespace {
33 
34 /// GISelAsmOperandInfo - This contains information for each constraint that we
35 /// are lowering.
36 class GISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
37 public:
38   /// Regs - If this is a register or register class operand, this
39   /// contains the set of assigned registers corresponding to the operand.
40   SmallVector<Register, 1> Regs;
41 
42   explicit GISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &Info)
43       : TargetLowering::AsmOperandInfo(Info) {}
44 };
45 
46 using GISelAsmOperandInfoVector = SmallVector<GISelAsmOperandInfo, 16>;
47 
48 class ExtraFlags {
49   unsigned Flags = 0;
50 
51 public:
52   explicit ExtraFlags(const CallBase &CB) {
53     const InlineAsm *IA = cast<InlineAsm>(CB.getCalledOperand());
54     if (IA->hasSideEffects())
55       Flags |= InlineAsm::Extra_HasSideEffects;
56     if (IA->isAlignStack())
57       Flags |= InlineAsm::Extra_IsAlignStack;
58     if (CB.isConvergent())
59       Flags |= InlineAsm::Extra_IsConvergent;
60     Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
61   }
62 
63   void update(const TargetLowering::AsmOperandInfo &OpInfo) {
64     // Ideally, we would only check against memory constraints.  However, the
65     // meaning of an Other constraint can be target-specific and we can't easily
66     // reason about it.  Therefore, be conservative and set MayLoad/MayStore
67     // for Other constraints as well.
68     if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
69         OpInfo.ConstraintType == TargetLowering::C_Other) {
70       if (OpInfo.Type == InlineAsm::isInput)
71         Flags |= InlineAsm::Extra_MayLoad;
72       else if (OpInfo.Type == InlineAsm::isOutput)
73         Flags |= InlineAsm::Extra_MayStore;
74       else if (OpInfo.Type == InlineAsm::isClobber)
75         Flags |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
76     }
77   }
78 
79   unsigned get() const { return Flags; }
80 };
81 
82 } // namespace
83 
84 /// Assign virtual/physical registers for the specified register operand.
85 static void getRegistersForValue(MachineFunction &MF,
86                                  MachineIRBuilder &MIRBuilder,
87                                  GISelAsmOperandInfo &OpInfo,
88                                  GISelAsmOperandInfo &RefOpInfo) {
89 
90   const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
91   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
92 
93   // No work to do for memory operations.
94   if (OpInfo.ConstraintType == TargetLowering::C_Memory)
95     return;
96 
97   // If this is a constraint for a single physreg, or a constraint for a
98   // register class, find it.
99   Register AssignedReg;
100   const TargetRegisterClass *RC;
101   std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
102       &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
103   // RC is unset only on failure. Return immediately.
104   if (!RC)
105     return;
106 
107   // No need to allocate a matching input constraint since the constraint it's
108   // matching to has already been allocated.
109   if (OpInfo.isMatchingInputConstraint())
110     return;
111 
112   // Initialize NumRegs.
113   unsigned NumRegs = 1;
114   if (OpInfo.ConstraintVT != MVT::Other)
115     NumRegs =
116         TLI.getNumRegisters(MF.getFunction().getContext(), OpInfo.ConstraintVT);
117 
118   // If this is a constraint for a specific physical register, but the type of
119   // the operand requires more than one register to be passed, we allocate the
120   // required amount of physical registers, starting from the selected physical
121   // register.
122   // For this, first retrieve a register iterator for the given register class
123   TargetRegisterClass::iterator I = RC->begin();
124   MachineRegisterInfo &RegInfo = MF.getRegInfo();
125 
126   // Advance the iterator to the assigned register (if set)
127   if (AssignedReg) {
128     for (; *I != AssignedReg; ++I)
129       assert(I != RC->end() && "AssignedReg should be a member of provided RC");
130   }
131 
132   // Finally, assign the registers. If the AssignedReg isn't set, create virtual
133   // registers with the provided register class
134   for (; NumRegs; --NumRegs, ++I) {
135     assert(I != RC->end() && "Ran out of registers to allocate!");
136     Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
137     OpInfo.Regs.push_back(R);
138   }
139 }
140 
141 /// Return an integer indicating how general CT is.
142 static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
143   switch (CT) {
144   case TargetLowering::C_Immediate:
145   case TargetLowering::C_Other:
146   case TargetLowering::C_Unknown:
147     return 0;
148   case TargetLowering::C_Register:
149     return 1;
150   case TargetLowering::C_RegisterClass:
151     return 2;
152   case TargetLowering::C_Memory:
153     return 3;
154   }
155   llvm_unreachable("Invalid constraint type");
156 }
157 
158 static void chooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
159                              const TargetLowering *TLI) {
160   assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
161   unsigned BestIdx = 0;
162   TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
163   int BestGenerality = -1;
164 
165   // Loop over the options, keeping track of the most general one.
166   for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
167     TargetLowering::ConstraintType CType =
168         TLI->getConstraintType(OpInfo.Codes[i]);
169 
170     // Indirect 'other' or 'immediate' constraints are not allowed.
171     if (OpInfo.isIndirect && !(CType == TargetLowering::C_Memory ||
172                                CType == TargetLowering::C_Register ||
173                                CType == TargetLowering::C_RegisterClass))
174       continue;
175 
176     // If this is an 'other' or 'immediate' constraint, see if the operand is
177     // valid for it. For example, on X86 we might have an 'rI' constraint. If
178     // the operand is an integer in the range [0..31] we want to use I (saving a
179     // load of a register), otherwise we must use 'r'.
180     if (CType == TargetLowering::C_Other ||
181         CType == TargetLowering::C_Immediate) {
182       assert(OpInfo.Codes[i].size() == 1 &&
183              "Unhandled multi-letter 'other' constraint");
184       // FIXME: prefer immediate constraints if the target allows it
185     }
186 
187     // Things with matching constraints can only be registers, per gcc
188     // documentation.  This mainly affects "g" constraints.
189     if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
190       continue;
191 
192     // This constraint letter is more general than the previous one, use it.
193     int Generality = getConstraintGenerality(CType);
194     if (Generality > BestGenerality) {
195       BestType = CType;
196       BestIdx = i;
197       BestGenerality = Generality;
198     }
199   }
200 
201   OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
202   OpInfo.ConstraintType = BestType;
203 }
204 
205 static void computeConstraintToUse(const TargetLowering *TLI,
206                                    TargetLowering::AsmOperandInfo &OpInfo) {
207   assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
208 
209   // Single-letter constraints ('r') are very common.
210   if (OpInfo.Codes.size() == 1) {
211     OpInfo.ConstraintCode = OpInfo.Codes[0];
212     OpInfo.ConstraintType = TLI->getConstraintType(OpInfo.ConstraintCode);
213   } else {
214     chooseConstraint(OpInfo, TLI);
215   }
216 
217   // 'X' matches anything.
218   if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
219     // Labels and constants are handled elsewhere ('X' is the only thing
220     // that matches labels).  For Functions, the type here is the type of
221     // the result, which is not what we want to look at; leave them alone.
222     Value *Val = OpInfo.CallOperandVal;
223     if (isa<BasicBlock>(Val) || isa<ConstantInt>(Val) || isa<Function>(Val))
224       return;
225 
226     // Otherwise, try to resolve it to something we know about by looking at
227     // the actual operand type.
228     if (const char *Repl = TLI->LowerXConstraint(OpInfo.ConstraintVT)) {
229       OpInfo.ConstraintCode = Repl;
230       OpInfo.ConstraintType = TLI->getConstraintType(OpInfo.ConstraintCode);
231     }
232   }
233 }
234 
235 static unsigned getNumOpRegs(const MachineInstr &I, unsigned OpIdx) {
236   unsigned Flag = I.getOperand(OpIdx).getImm();
237   return InlineAsm::getNumOperandRegisters(Flag);
238 }
239 
240 bool InlineAsmLowering::lowerInlineAsm(
241     MachineIRBuilder &MIRBuilder, const CallBase &Call,
242     std::function<ArrayRef<Register>(const Value &Val)> GetOrCreateVRegs)
243     const {
244   const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
245 
246   /// ConstraintOperands - Information about all of the constraints.
247   GISelAsmOperandInfoVector ConstraintOperands;
248 
249   MachineFunction &MF = MIRBuilder.getMF();
250   const Function &F = MF.getFunction();
251   const DataLayout &DL = F.getParent()->getDataLayout();
252   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
253 
254   MachineRegisterInfo *MRI = MIRBuilder.getMRI();
255 
256   TargetLowering::AsmOperandInfoVector TargetConstraints =
257       TLI->ParseConstraints(DL, TRI, Call);
258 
259   ExtraFlags ExtraInfo(Call);
260   unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
261   unsigned ResNo = 0; // ResNo - The result number of the next output.
262   for (auto &T : TargetConstraints) {
263     ConstraintOperands.push_back(GISelAsmOperandInfo(T));
264     GISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
265 
266     // Compute the value type for each operand.
267     if (OpInfo.Type == InlineAsm::isInput ||
268         (OpInfo.Type == InlineAsm::isOutput && OpInfo.isIndirect)) {
269 
270       OpInfo.CallOperandVal = const_cast<Value *>(Call.getArgOperand(ArgNo++));
271 
272       if (isa<BasicBlock>(OpInfo.CallOperandVal)) {
273         LLVM_DEBUG(dbgs() << "Basic block input operands not supported yet\n");
274         return false;
275       }
276 
277       Type *OpTy = OpInfo.CallOperandVal->getType();
278 
279       // If this is an indirect operand, the operand is a pointer to the
280       // accessed type.
281       if (OpInfo.isIndirect) {
282         PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
283         if (!PtrTy)
284           report_fatal_error("Indirect operand for inline asm not a pointer!");
285         OpTy = PtrTy->getElementType();
286       }
287 
288       // FIXME: Support aggregate input operands
289       if (!OpTy->isSingleValueType()) {
290         LLVM_DEBUG(
291             dbgs() << "Aggregate input operands are not supported yet\n");
292         return false;
293       }
294 
295       OpInfo.ConstraintVT = TLI->getValueType(DL, OpTy, true).getSimpleVT();
296 
297     } else if (OpInfo.Type == InlineAsm::isOutput && !OpInfo.isIndirect) {
298       assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
299       if (StructType *STy = dyn_cast<StructType>(Call.getType())) {
300         OpInfo.ConstraintVT =
301             TLI->getSimpleValueType(DL, STy->getElementType(ResNo));
302       } else {
303         assert(ResNo == 0 && "Asm only has one result!");
304         OpInfo.ConstraintVT = TLI->getSimpleValueType(DL, Call.getType());
305       }
306       ++ResNo;
307     } else {
308       OpInfo.ConstraintVT = MVT::Other;
309     }
310 
311     // Compute the constraint code and ConstraintType to use.
312     computeConstraintToUse(TLI, OpInfo);
313 
314     // The selected constraint type might expose new sideeffects
315     ExtraInfo.update(OpInfo);
316   }
317 
318   // At this point, all operand types are decided.
319   // Create the MachineInstr, but don't insert it yet since input
320   // operands still need to insert instructions before this one
321   auto Inst = MIRBuilder.buildInstrNoInsert(TargetOpcode::INLINEASM)
322                   .addExternalSymbol(IA->getAsmString().c_str())
323                   .addImm(ExtraInfo.get());
324 
325   // Starting from this operand: flag followed by register(s) will be added as
326   // operands to Inst for each constraint. Used for matching input constraints.
327   unsigned StartIdx = Inst->getNumOperands();
328 
329   // Collects the output operands for later processing
330   GISelAsmOperandInfoVector OutputOperands;
331 
332   for (auto &OpInfo : ConstraintOperands) {
333     GISelAsmOperandInfo &RefOpInfo =
334         OpInfo.isMatchingInputConstraint()
335             ? ConstraintOperands[OpInfo.getMatchedOperand()]
336             : OpInfo;
337 
338     // Assign registers for register operands
339     getRegistersForValue(MF, MIRBuilder, OpInfo, RefOpInfo);
340 
341     switch (OpInfo.Type) {
342     case InlineAsm::isOutput:
343       if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
344         unsigned ConstraintID =
345             TLI->getInlineAsmMemConstraint(OpInfo.ConstraintCode);
346         assert(ConstraintID != InlineAsm::Constraint_Unknown &&
347                "Failed to convert memory constraint code to constraint id.");
348 
349         // Add information to the INLINEASM instruction to know about this
350         // output.
351         unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
352         OpFlags = InlineAsm::getFlagWordForMem(OpFlags, ConstraintID);
353         Inst.addImm(OpFlags);
354         ArrayRef<Register> SourceRegs =
355             GetOrCreateVRegs(*OpInfo.CallOperandVal);
356         assert(
357             SourceRegs.size() == 1 &&
358             "Expected the memory output to fit into a single virtual register");
359         Inst.addReg(SourceRegs[0]);
360       } else {
361         // Otherwise, this outputs to a register (directly for C_Register /
362         // C_RegisterClass. Find a register that we can use.
363         assert(OpInfo.ConstraintType == TargetLowering::C_Register ||
364                OpInfo.ConstraintType == TargetLowering::C_RegisterClass);
365 
366         if (OpInfo.Regs.empty()) {
367           LLVM_DEBUG(dbgs()
368                      << "Couldn't allocate output register for constraint\n");
369           return false;
370         }
371 
372         // Add information to the INLINEASM instruction to know that this
373         // register is set.
374         unsigned Flag = InlineAsm::getFlagWord(
375             OpInfo.isEarlyClobber ? InlineAsm::Kind_RegDefEarlyClobber
376                                   : InlineAsm::Kind_RegDef,
377             OpInfo.Regs.size());
378         if (OpInfo.Regs.front().isVirtual()) {
379           // Put the register class of the virtual registers in the flag word.
380           // That way, later passes can recompute register class constraints for
381           // inline assembly as well as normal instructions. Don't do this for
382           // tied operands that can use the regclass information from the def.
383           const TargetRegisterClass *RC = MRI->getRegClass(OpInfo.Regs.front());
384           Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
385         }
386 
387         Inst.addImm(Flag);
388 
389         for (Register Reg : OpInfo.Regs) {
390           Inst.addReg(Reg,
391                       RegState::Define | getImplRegState(Reg.isPhysical()) |
392                           (OpInfo.isEarlyClobber ? RegState::EarlyClobber : 0));
393         }
394 
395         // Remember this output operand for later processing
396         OutputOperands.push_back(OpInfo);
397       }
398 
399       break;
400     case InlineAsm::isInput: {
401       if (OpInfo.isMatchingInputConstraint()) {
402         unsigned DefIdx = OpInfo.getMatchedOperand();
403         // Find operand with register def that corresponds to DefIdx.
404         unsigned InstFlagIdx = StartIdx;
405         for (unsigned i = 0; i < DefIdx; ++i)
406           InstFlagIdx += getNumOpRegs(*Inst, InstFlagIdx) + 1;
407         assert(getNumOpRegs(*Inst, InstFlagIdx) == 1 && "Wrong flag");
408 
409         // We want to tie input to register in next operand.
410         unsigned DefRegIdx = InstFlagIdx + 1;
411         Register Def = Inst->getOperand(DefRegIdx).getReg();
412 
413         // Copy input to new vreg with same reg class as Def
414         const TargetRegisterClass *RC = MRI->getRegClass(Def);
415         ArrayRef<Register> SrcRegs = GetOrCreateVRegs(*OpInfo.CallOperandVal);
416         assert(SrcRegs.size() == 1 && "Single register is expected here");
417         Register Tmp = MRI->createVirtualRegister(RC);
418         MIRBuilder.buildCopy(Tmp, SrcRegs[0]);
419 
420         // Add Flag and input register operand (Tmp) to Inst. Tie Tmp to Def.
421         unsigned UseFlag = InlineAsm::getFlagWord(InlineAsm::Kind_RegUse, 1);
422         unsigned Flag = InlineAsm::getFlagWordForMatchingOp(UseFlag, DefIdx);
423         Inst.addImm(Flag);
424         Inst.addReg(Tmp);
425         Inst->tieOperands(DefRegIdx, Inst->getNumOperands() - 1);
426         break;
427       }
428 
429       if (OpInfo.ConstraintType == TargetLowering::C_Other &&
430           OpInfo.isIndirect) {
431         LLVM_DEBUG(dbgs() << "Indirect input operands with unknown constraint "
432                              "not supported yet\n");
433         return false;
434       }
435 
436       if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
437           OpInfo.ConstraintType == TargetLowering::C_Other) {
438 
439         std::vector<MachineOperand> Ops;
440         if (!lowerAsmOperandForConstraint(OpInfo.CallOperandVal,
441                                           OpInfo.ConstraintCode, Ops,
442                                           MIRBuilder)) {
443           LLVM_DEBUG(dbgs() << "Don't support constraint: "
444                             << OpInfo.ConstraintCode << " yet\n");
445           return false;
446         }
447 
448         assert(Ops.size() > 0 &&
449                "Expected constraint to be lowered to at least one operand");
450 
451         // Add information to the INLINEASM node to know about this input.
452         unsigned OpFlags =
453             InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
454         Inst.addImm(OpFlags);
455         Inst.add(Ops);
456         break;
457       }
458 
459       if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
460 
461         if (!OpInfo.isIndirect) {
462           LLVM_DEBUG(dbgs()
463                      << "Cannot indirectify memory input operands yet\n");
464           return false;
465         }
466 
467         assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
468 
469         unsigned ConstraintID =
470             TLI->getInlineAsmMemConstraint(OpInfo.ConstraintCode);
471         unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
472         OpFlags = InlineAsm::getFlagWordForMem(OpFlags, ConstraintID);
473         Inst.addImm(OpFlags);
474         ArrayRef<Register> SourceRegs =
475             GetOrCreateVRegs(*OpInfo.CallOperandVal);
476         assert(
477             SourceRegs.size() == 1 &&
478             "Expected the memory input to fit into a single virtual register");
479         Inst.addReg(SourceRegs[0]);
480         break;
481       }
482 
483       assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
484               OpInfo.ConstraintType == TargetLowering::C_Register) &&
485              "Unknown constraint type!");
486 
487       if (OpInfo.isIndirect) {
488         LLVM_DEBUG(dbgs() << "Can't handle indirect register inputs yet "
489                              "for constraint '"
490                           << OpInfo.ConstraintCode << "'\n");
491         return false;
492       }
493 
494       // Copy the input into the appropriate registers.
495       if (OpInfo.Regs.empty()) {
496         LLVM_DEBUG(
497             dbgs()
498             << "Couldn't allocate input register for register constraint\n");
499         return false;
500       }
501 
502       unsigned NumRegs = OpInfo.Regs.size();
503       ArrayRef<Register> SourceRegs = GetOrCreateVRegs(*OpInfo.CallOperandVal);
504       assert(NumRegs == SourceRegs.size() &&
505              "Expected the number of input registers to match the number of "
506              "source registers");
507 
508       if (NumRegs > 1) {
509         LLVM_DEBUG(dbgs() << "Input operands with multiple input registers are "
510                              "not supported yet\n");
511         return false;
512       }
513 
514       unsigned Flag = InlineAsm::getFlagWord(InlineAsm::Kind_RegUse, NumRegs);
515       Inst.addImm(Flag);
516       MIRBuilder.buildCopy(OpInfo.Regs[0], SourceRegs[0]);
517       Inst.addReg(OpInfo.Regs[0]);
518       break;
519     }
520 
521     case InlineAsm::isClobber: {
522 
523       unsigned NumRegs = OpInfo.Regs.size();
524       if (NumRegs > 0) {
525         unsigned Flag =
526             InlineAsm::getFlagWord(InlineAsm::Kind_Clobber, NumRegs);
527         Inst.addImm(Flag);
528 
529         for (Register Reg : OpInfo.Regs) {
530           Inst.addReg(Reg, RegState::Define | RegState::EarlyClobber |
531                                getImplRegState(Reg.isPhysical()));
532         }
533       }
534       break;
535     }
536     }
537   }
538 
539   if (const MDNode *SrcLoc = Call.getMetadata("srcloc"))
540     Inst.addMetadata(SrcLoc);
541 
542   // All inputs are handled, insert the instruction now
543   MIRBuilder.insertInstr(Inst);
544 
545   // Finally, copy the output operands into the output registers
546   ArrayRef<Register> ResRegs = GetOrCreateVRegs(Call);
547   if (ResRegs.size() != OutputOperands.size()) {
548     LLVM_DEBUG(dbgs() << "Expected the number of output registers to match the "
549                          "number of destination registers\n");
550     return false;
551   }
552   for (unsigned int i = 0, e = ResRegs.size(); i < e; i++) {
553     GISelAsmOperandInfo &OpInfo = OutputOperands[i];
554 
555     if (OpInfo.Regs.empty())
556       continue;
557 
558     switch (OpInfo.ConstraintType) {
559     case TargetLowering::C_Register:
560     case TargetLowering::C_RegisterClass: {
561       if (OpInfo.Regs.size() > 1) {
562         LLVM_DEBUG(dbgs() << "Output operands with multiple defining "
563                              "registers are not supported yet\n");
564         return false;
565       }
566 
567       Register SrcReg = OpInfo.Regs[0];
568       unsigned SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
569       if (MRI->getType(ResRegs[i]).getSizeInBits() < SrcSize) {
570         // First copy the non-typed virtual register into a generic virtual
571         // register
572         Register Tmp1Reg =
573             MRI->createGenericVirtualRegister(LLT::scalar(SrcSize));
574         MIRBuilder.buildCopy(Tmp1Reg, SrcReg);
575         // Need to truncate the result of the register
576         MIRBuilder.buildTrunc(ResRegs[i], Tmp1Reg);
577       } else {
578         MIRBuilder.buildCopy(ResRegs[i], SrcReg);
579       }
580       break;
581     }
582     case TargetLowering::C_Immediate:
583     case TargetLowering::C_Other:
584       LLVM_DEBUG(
585           dbgs() << "Cannot lower target specific output constraints yet\n");
586       return false;
587     case TargetLowering::C_Memory:
588       break; // Already handled.
589     case TargetLowering::C_Unknown:
590       LLVM_DEBUG(dbgs() << "Unexpected unknown constraint\n");
591       return false;
592     }
593   }
594 
595   return true;
596 }
597 
598 bool InlineAsmLowering::lowerAsmOperandForConstraint(
599     Value *Val, StringRef Constraint, std::vector<MachineOperand> &Ops,
600     MachineIRBuilder &MIRBuilder) const {
601   if (Constraint.size() > 1)
602     return false;
603 
604   char ConstraintLetter = Constraint[0];
605   switch (ConstraintLetter) {
606   default:
607     return false;
608   case 'i': // Simple Integer or Relocatable Constant
609     if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
610       assert(CI->getBitWidth() <= 64 &&
611              "expected immediate to fit into 64-bits");
612       // Boolean constants should be zero-extended, others are sign-extended
613       bool IsBool = CI->getBitWidth() == 1;
614       int64_t ExtVal = IsBool ? CI->getZExtValue() : CI->getSExtValue();
615       Ops.push_back(MachineOperand::CreateImm(ExtVal));
616       return true;
617     }
618     return false;
619   }
620 }
621