]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/asm.rs
Auto merge of #90842 - pierwill:localdefid-indexmap, r=wesleywiser
[rust.git] / compiler / rustc_codegen_llvm / src / asm.rs
1 use crate::builder::Builder;
2 use crate::common::Funclet;
3 use crate::context::CodegenCx;
4 use crate::llvm;
5 use crate::llvm_util;
6 use crate::type_::Type;
7 use crate::type_of::LayoutLlvmExt;
8 use crate::value::Value;
9
10 use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
11 use rustc_codegen_ssa::mir::operand::OperandValue;
12 use rustc_codegen_ssa::traits::*;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_middle::ty::layout::TyAndLayout;
15 use rustc_middle::{bug, span_bug, ty::Instance};
16 use rustc_span::{Pos, Span};
17 use rustc_target::abi::*;
18 use rustc_target::asm::*;
19
20 use libc::{c_char, c_uint};
21 use tracing::debug;
22
23 impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
24     fn codegen_inline_asm(
25         &mut self,
26         template: &[InlineAsmTemplatePiece],
27         operands: &[InlineAsmOperandRef<'tcx, Self>],
28         options: InlineAsmOptions,
29         line_spans: &[Span],
30         instance: Instance<'_>,
31         dest_catch_funclet: Option<(Self::BasicBlock, Self::BasicBlock, Option<&Self::Funclet>)>,
32     ) {
33         let asm_arch = self.tcx.sess.asm_arch.unwrap();
34
35         // Collect the types of output operands
36         let mut constraints = vec![];
37         let mut clobbers = vec![];
38         let mut output_types = vec![];
39         let mut op_idx = FxHashMap::default();
40         let mut clobbered_x87 = false;
41         for (idx, op) in operands.iter().enumerate() {
42             match *op {
43                 InlineAsmOperandRef::Out { reg, late, place } => {
44                     let is_target_supported = |reg_class: InlineAsmRegClass| {
45                         for &(_, feature) in reg_class.supported_types(asm_arch) {
46                             if let Some(feature) = feature {
47                                 let codegen_fn_attrs = self.tcx.codegen_fn_attrs(instance.def_id());
48                                 if self.tcx.sess.target_features.contains(&feature)
49                                     || codegen_fn_attrs.target_features.contains(&feature)
50                                 {
51                                     return true;
52                                 }
53                             } else {
54                                 // Register class is unconditionally supported
55                                 return true;
56                             }
57                         }
58                         false
59                     };
60
61                     let mut layout = None;
62                     let ty = if let Some(ref place) = place {
63                         layout = Some(&place.layout);
64                         llvm_fixup_output_type(self.cx, reg.reg_class(), &place.layout)
65                     } else if matches!(
66                         reg.reg_class(),
67                         InlineAsmRegClass::X86(
68                             X86InlineAsmRegClass::mmx_reg | X86InlineAsmRegClass::x87_reg
69                         )
70                     ) {
71                         // Special handling for x87/mmx registers: we always
72                         // clobber the whole set if one register is marked as
73                         // clobbered. This is due to the way LLVM handles the
74                         // FP stack in inline assembly.
75                         if !clobbered_x87 {
76                             clobbered_x87 = true;
77                             clobbers.push("~{st}".to_string());
78                             for i in 1..=7 {
79                                 clobbers.push(format!("~{{st({})}}", i));
80                             }
81                         }
82                         continue;
83                     } else if !is_target_supported(reg.reg_class())
84                         || reg.reg_class().is_clobber_only(asm_arch)
85                     {
86                         // We turn discarded outputs into clobber constraints
87                         // if the target feature needed by the register class is
88                         // disabled. This is necessary otherwise LLVM will try
89                         // to actually allocate a register for the dummy output.
90                         assert!(matches!(reg, InlineAsmRegOrRegClass::Reg(_)));
91                         clobbers.push(format!("~{}", reg_to_llvm(reg, None)));
92                         continue;
93                     } else {
94                         // If the output is discarded, we don't really care what
95                         // type is used. We're just using this to tell LLVM to
96                         // reserve the register.
97                         dummy_output_type(self.cx, reg.reg_class())
98                     };
99                     output_types.push(ty);
100                     op_idx.insert(idx, constraints.len());
101                     let prefix = if late { "=" } else { "=&" };
102                     constraints.push(format!("{}{}", prefix, reg_to_llvm(reg, layout)));
103                 }
104                 InlineAsmOperandRef::InOut { reg, late, in_value, out_place } => {
105                     let layout = if let Some(ref out_place) = out_place {
106                         &out_place.layout
107                     } else {
108                         // LLVM required tied operands to have the same type,
109                         // so we just use the type of the input.
110                         &in_value.layout
111                     };
112                     let ty = llvm_fixup_output_type(self.cx, reg.reg_class(), layout);
113                     output_types.push(ty);
114                     op_idx.insert(idx, constraints.len());
115                     let prefix = if late { "=" } else { "=&" };
116                     constraints.push(format!("{}{}", prefix, reg_to_llvm(reg, Some(layout))));
117                 }
118                 _ => {}
119             }
120         }
121
122         // Collect input operands
123         let mut inputs = vec![];
124         for (idx, op) in operands.iter().enumerate() {
125             match *op {
126                 InlineAsmOperandRef::In { reg, value } => {
127                     let llval =
128                         llvm_fixup_input(self, value.immediate(), reg.reg_class(), &value.layout);
129                     inputs.push(llval);
130                     op_idx.insert(idx, constraints.len());
131                     constraints.push(reg_to_llvm(reg, Some(&value.layout)));
132                 }
133                 InlineAsmOperandRef::InOut { reg, late: _, in_value, out_place: _ } => {
134                     let value = llvm_fixup_input(
135                         self,
136                         in_value.immediate(),
137                         reg.reg_class(),
138                         &in_value.layout,
139                     );
140                     inputs.push(value);
141                     constraints.push(format!("{}", op_idx[&idx]));
142                 }
143                 InlineAsmOperandRef::SymFn { instance } => {
144                     inputs.push(self.cx.get_fn(instance));
145                     op_idx.insert(idx, constraints.len());
146                     constraints.push("s".to_string());
147                 }
148                 InlineAsmOperandRef::SymStatic { def_id } => {
149                     inputs.push(self.cx.get_static(def_id));
150                     op_idx.insert(idx, constraints.len());
151                     constraints.push("s".to_string());
152                 }
153                 _ => {}
154             }
155         }
156
157         // Build the template string
158         let mut template_str = String::new();
159         for piece in template {
160             match *piece {
161                 InlineAsmTemplatePiece::String(ref s) => {
162                     if s.contains('$') {
163                         for c in s.chars() {
164                             if c == '$' {
165                                 template_str.push_str("$$");
166                             } else {
167                                 template_str.push(c);
168                             }
169                         }
170                     } else {
171                         template_str.push_str(s)
172                     }
173                 }
174                 InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span: _ } => {
175                     match operands[operand_idx] {
176                         InlineAsmOperandRef::In { reg, .. }
177                         | InlineAsmOperandRef::Out { reg, .. }
178                         | InlineAsmOperandRef::InOut { reg, .. } => {
179                             let modifier = modifier_to_llvm(asm_arch, reg.reg_class(), modifier);
180                             if let Some(modifier) = modifier {
181                                 template_str.push_str(&format!(
182                                     "${{{}:{}}}",
183                                     op_idx[&operand_idx], modifier
184                                 ));
185                             } else {
186                                 template_str.push_str(&format!("${{{}}}", op_idx[&operand_idx]));
187                             }
188                         }
189                         InlineAsmOperandRef::Const { ref string } => {
190                             // Const operands get injected directly into the template
191                             template_str.push_str(string);
192                         }
193                         InlineAsmOperandRef::SymFn { .. }
194                         | InlineAsmOperandRef::SymStatic { .. } => {
195                             // Only emit the raw symbol name
196                             template_str.push_str(&format!("${{{}:c}}", op_idx[&operand_idx]));
197                         }
198                     }
199                 }
200             }
201         }
202
203         constraints.append(&mut clobbers);
204         if !options.contains(InlineAsmOptions::PRESERVES_FLAGS) {
205             match asm_arch {
206                 InlineAsmArch::AArch64 | InlineAsmArch::Arm => {
207                     constraints.push("~{cc}".to_string());
208                 }
209                 InlineAsmArch::X86 | InlineAsmArch::X86_64 => {
210                     constraints.extend_from_slice(&[
211                         "~{dirflag}".to_string(),
212                         "~{fpsr}".to_string(),
213                         "~{flags}".to_string(),
214                     ]);
215                 }
216                 InlineAsmArch::RiscV32 | InlineAsmArch::RiscV64 => {
217                     constraints.extend_from_slice(&[
218                         "~{vtype}".to_string(),
219                         "~{vl}".to_string(),
220                         "~{vxsat}".to_string(),
221                         "~{vxrm}".to_string(),
222                     ]);
223                 }
224                 InlineAsmArch::Avr => {
225                     constraints.push("~{sreg}".to_string());
226                 }
227                 InlineAsmArch::Nvptx64 => {}
228                 InlineAsmArch::PowerPC | InlineAsmArch::PowerPC64 => {}
229                 InlineAsmArch::Hexagon => {}
230                 InlineAsmArch::Mips | InlineAsmArch::Mips64 => {}
231                 InlineAsmArch::S390x => {}
232                 InlineAsmArch::SpirV => {}
233                 InlineAsmArch::Wasm32 | InlineAsmArch::Wasm64 => {}
234                 InlineAsmArch::Bpf => {}
235                 InlineAsmArch::Msp430 => {
236                     constraints.push("~{sr}".to_string());
237                 }
238             }
239         }
240         if !options.contains(InlineAsmOptions::NOMEM) {
241             // This is actually ignored by LLVM, but it's probably best to keep
242             // it just in case. LLVM instead uses the ReadOnly/ReadNone
243             // attributes on the call instruction to optimize.
244             constraints.push("~{memory}".to_string());
245         }
246         let volatile = !options.contains(InlineAsmOptions::PURE);
247         let alignstack = !options.contains(InlineAsmOptions::NOSTACK);
248         let output_type = match &output_types[..] {
249             [] => self.type_void(),
250             [ty] => ty,
251             tys => self.type_struct(tys, false),
252         };
253         let dialect = match asm_arch {
254             InlineAsmArch::X86 | InlineAsmArch::X86_64
255                 if !options.contains(InlineAsmOptions::ATT_SYNTAX) =>
256             {
257                 llvm::AsmDialect::Intel
258             }
259             _ => llvm::AsmDialect::Att,
260         };
261         let result = inline_asm_call(
262             self,
263             &template_str,
264             &constraints.join(","),
265             &inputs,
266             output_type,
267             volatile,
268             alignstack,
269             dialect,
270             line_spans,
271             options.contains(InlineAsmOptions::MAY_UNWIND),
272             dest_catch_funclet,
273         )
274         .unwrap_or_else(|| span_bug!(line_spans[0], "LLVM asm constraint validation failed"));
275
276         if options.contains(InlineAsmOptions::PURE) {
277             if options.contains(InlineAsmOptions::NOMEM) {
278                 llvm::Attribute::ReadNone.apply_callsite(llvm::AttributePlace::Function, result);
279             } else if options.contains(InlineAsmOptions::READONLY) {
280                 llvm::Attribute::ReadOnly.apply_callsite(llvm::AttributePlace::Function, result);
281             }
282             llvm::Attribute::WillReturn.apply_callsite(llvm::AttributePlace::Function, result);
283         } else if options.contains(InlineAsmOptions::NOMEM) {
284             llvm::Attribute::InaccessibleMemOnly
285                 .apply_callsite(llvm::AttributePlace::Function, result);
286         } else {
287             // LLVM doesn't have an attribute to represent ReadOnly + SideEffect
288         }
289
290         // Write results to outputs
291         for (idx, op) in operands.iter().enumerate() {
292             if let InlineAsmOperandRef::Out { reg, place: Some(place), .. }
293             | InlineAsmOperandRef::InOut { reg, out_place: Some(place), .. } = *op
294             {
295                 let value = if output_types.len() == 1 {
296                     result
297                 } else {
298                     self.extract_value(result, op_idx[&idx] as u64)
299                 };
300                 let value = llvm_fixup_output(self, value, reg.reg_class(), &place.layout);
301                 OperandValue::Immediate(value).store(self, place);
302             }
303         }
304     }
305 }
306
307 impl AsmMethods for CodegenCx<'_, '_> {
308     fn codegen_global_asm(
309         &self,
310         template: &[InlineAsmTemplatePiece],
311         operands: &[GlobalAsmOperandRef],
312         options: InlineAsmOptions,
313         _line_spans: &[Span],
314     ) {
315         let asm_arch = self.tcx.sess.asm_arch.unwrap();
316
317         // Default to Intel syntax on x86
318         let intel_syntax = matches!(asm_arch, InlineAsmArch::X86 | InlineAsmArch::X86_64)
319             && !options.contains(InlineAsmOptions::ATT_SYNTAX);
320
321         // Build the template string
322         let mut template_str = String::new();
323         if intel_syntax {
324             template_str.push_str(".intel_syntax\n");
325         }
326         for piece in template {
327             match *piece {
328                 InlineAsmTemplatePiece::String(ref s) => template_str.push_str(s),
329                 InlineAsmTemplatePiece::Placeholder { operand_idx, modifier: _, span: _ } => {
330                     match operands[operand_idx] {
331                         GlobalAsmOperandRef::Const { ref string } => {
332                             // Const operands get injected directly into the
333                             // template. Note that we don't need to escape $
334                             // here unlike normal inline assembly.
335                             template_str.push_str(string);
336                         }
337                     }
338                 }
339             }
340         }
341         if intel_syntax {
342             template_str.push_str("\n.att_syntax\n");
343         }
344
345         unsafe {
346             llvm::LLVMRustAppendModuleInlineAsm(
347                 self.llmod,
348                 template_str.as_ptr().cast(),
349                 template_str.len(),
350             );
351         }
352     }
353 }
354
355 pub(crate) fn inline_asm_call<'ll>(
356     bx: &mut Builder<'_, 'll, '_>,
357     asm: &str,
358     cons: &str,
359     inputs: &[&'ll Value],
360     output: &'ll llvm::Type,
361     volatile: bool,
362     alignstack: bool,
363     dia: llvm::AsmDialect,
364     line_spans: &[Span],
365     unwind: bool,
366     dest_catch_funclet: Option<(
367         &'ll llvm::BasicBlock,
368         &'ll llvm::BasicBlock,
369         Option<&Funclet<'ll>>,
370     )>,
371 ) -> Option<&'ll Value> {
372     let volatile = if volatile { llvm::True } else { llvm::False };
373     let alignstack = if alignstack { llvm::True } else { llvm::False };
374     let can_throw = if unwind { llvm::True } else { llvm::False };
375
376     let argtys = inputs
377         .iter()
378         .map(|v| {
379             debug!("Asm Input Type: {:?}", *v);
380             bx.cx.val_ty(*v)
381         })
382         .collect::<Vec<_>>();
383
384     debug!("Asm Output Type: {:?}", output);
385     let fty = bx.cx.type_func(&argtys, output);
386     unsafe {
387         // Ask LLVM to verify that the constraints are well-formed.
388         let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr().cast(), cons.len());
389         debug!("constraint verification result: {:?}", constraints_ok);
390         if constraints_ok {
391             if unwind && llvm_util::get_version() < (13, 0, 0) {
392                 bx.cx.sess().span_fatal(
393                     line_spans[0],
394                     "unwinding from inline assembly is only supported on llvm >= 13.",
395                 );
396             }
397
398             let v = llvm::LLVMRustInlineAsm(
399                 fty,
400                 asm.as_ptr().cast(),
401                 asm.len(),
402                 cons.as_ptr().cast(),
403                 cons.len(),
404                 volatile,
405                 alignstack,
406                 dia,
407                 can_throw,
408             );
409
410             let call = if let Some((dest, catch, funclet)) = dest_catch_funclet {
411                 bx.invoke(fty, v, inputs, dest, catch, funclet)
412             } else {
413                 bx.call(fty, v, inputs, None)
414             };
415
416             // Store mark in a metadata node so we can map LLVM errors
417             // back to source locations.  See #17552.
418             let key = "srcloc";
419             let kind = llvm::LLVMGetMDKindIDInContext(
420                 bx.llcx,
421                 key.as_ptr() as *const c_char,
422                 key.len() as c_uint,
423             );
424
425             // srcloc contains one integer for each line of assembly code.
426             // Unfortunately this isn't enough to encode a full span so instead
427             // we just encode the start position of each line.
428             // FIXME: Figure out a way to pass the entire line spans.
429             let mut srcloc = vec![];
430             if dia == llvm::AsmDialect::Intel && line_spans.len() > 1 {
431                 // LLVM inserts an extra line to add the ".intel_syntax", so add
432                 // a dummy srcloc entry for it.
433                 //
434                 // Don't do this if we only have 1 line span since that may be
435                 // due to the asm template string coming from a macro. LLVM will
436                 // default to the first srcloc for lines that don't have an
437                 // associated srcloc.
438                 srcloc.push(bx.const_i32(0));
439             }
440             srcloc.extend(line_spans.iter().map(|span| bx.const_i32(span.lo().to_u32() as i32)));
441             let md = llvm::LLVMMDNodeInContext(bx.llcx, srcloc.as_ptr(), srcloc.len() as u32);
442             llvm::LLVMSetMetadata(call, kind, md);
443
444             Some(call)
445         } else {
446             // LLVM has detected an issue with our constraints, bail out
447             None
448         }
449     }
450 }
451
452 /// If the register is an xmm/ymm/zmm register then return its index.
453 fn xmm_reg_index(reg: InlineAsmReg) -> Option<u32> {
454     match reg {
455         InlineAsmReg::X86(reg)
456             if reg as u32 >= X86InlineAsmReg::xmm0 as u32
457                 && reg as u32 <= X86InlineAsmReg::xmm15 as u32 =>
458         {
459             Some(reg as u32 - X86InlineAsmReg::xmm0 as u32)
460         }
461         InlineAsmReg::X86(reg)
462             if reg as u32 >= X86InlineAsmReg::ymm0 as u32
463                 && reg as u32 <= X86InlineAsmReg::ymm15 as u32 =>
464         {
465             Some(reg as u32 - X86InlineAsmReg::ymm0 as u32)
466         }
467         InlineAsmReg::X86(reg)
468             if reg as u32 >= X86InlineAsmReg::zmm0 as u32
469                 && reg as u32 <= X86InlineAsmReg::zmm31 as u32 =>
470         {
471             Some(reg as u32 - X86InlineAsmReg::zmm0 as u32)
472         }
473         _ => None,
474     }
475 }
476
477 /// If the register is an AArch64 vector register then return its index.
478 fn a64_vreg_index(reg: InlineAsmReg) -> Option<u32> {
479     match reg {
480         InlineAsmReg::AArch64(reg)
481             if reg as u32 >= AArch64InlineAsmReg::v0 as u32
482                 && reg as u32 <= AArch64InlineAsmReg::v31 as u32 =>
483         {
484             Some(reg as u32 - AArch64InlineAsmReg::v0 as u32)
485         }
486         _ => None,
487     }
488 }
489
490 /// Converts a register class to an LLVM constraint code.
491 fn reg_to_llvm(reg: InlineAsmRegOrRegClass, layout: Option<&TyAndLayout<'_>>) -> String {
492     match reg {
493         // For vector registers LLVM wants the register name to match the type size.
494         InlineAsmRegOrRegClass::Reg(reg) => {
495             if let Some(idx) = xmm_reg_index(reg) {
496                 let class = if let Some(layout) = layout {
497                     match layout.size.bytes() {
498                         64 => 'z',
499                         32 => 'y',
500                         _ => 'x',
501                     }
502                 } else {
503                     // We use f32 as the type for discarded outputs
504                     'x'
505                 };
506                 format!("{{{}mm{}}}", class, idx)
507             } else if let Some(idx) = a64_vreg_index(reg) {
508                 let class = if let Some(layout) = layout {
509                     match layout.size.bytes() {
510                         16 => 'q',
511                         8 => 'd',
512                         4 => 's',
513                         2 => 'h',
514                         1 => 'd', // We fixup i8 to i8x8
515                         _ => unreachable!(),
516                     }
517                 } else {
518                     // We use i64x2 as the type for discarded outputs
519                     'q'
520                 };
521                 format!("{{{}{}}}", class, idx)
522             } else if reg == InlineAsmReg::AArch64(AArch64InlineAsmReg::x30) {
523                 // LLVM doesn't recognize x30
524                 "{lr}".to_string()
525             } else if reg == InlineAsmReg::Arm(ArmInlineAsmReg::r14) {
526                 // LLVM doesn't recognize r14
527                 "{lr}".to_string()
528             } else {
529                 format!("{{{}}}", reg.name())
530             }
531         }
532         InlineAsmRegOrRegClass::RegClass(reg) => match reg {
533             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => "r",
534             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg) => "w",
535             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => "x",
536             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => {
537                 unreachable!("clobber-only")
538             }
539             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg) => "r",
540             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
541             | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
542             | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8) => "t",
543             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16)
544             | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8)
545             | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => "x",
546             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
547             | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg) => "w",
548             InlineAsmRegClass::Hexagon(HexagonInlineAsmRegClass::reg) => "r",
549             InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg) => "r",
550             InlineAsmRegClass::Mips(MipsInlineAsmRegClass::freg) => "f",
551             InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg16) => "h",
552             InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg32) => "r",
553             InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg64) => "l",
554             InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg) => "r",
555             InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => "b",
556             InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => "f",
557             InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
558             | InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
559                 unreachable!("clobber-only")
560             }
561             InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => "r",
562             InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => "f",
563             InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => {
564                 unreachable!("clobber-only")
565             }
566             InlineAsmRegClass::X86(X86InlineAsmRegClass::reg) => "r",
567             InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => "Q",
568             InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => "q",
569             InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg)
570             | InlineAsmRegClass::X86(X86InlineAsmRegClass::ymm_reg) => "x",
571             InlineAsmRegClass::X86(X86InlineAsmRegClass::zmm_reg) => "v",
572             InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => "^Yk",
573             InlineAsmRegClass::X86(
574                 X86InlineAsmRegClass::x87_reg | X86InlineAsmRegClass::mmx_reg,
575             ) => unreachable!("clobber-only"),
576             InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => "r",
577             InlineAsmRegClass::Bpf(BpfInlineAsmRegClass::reg) => "r",
578             InlineAsmRegClass::Bpf(BpfInlineAsmRegClass::wreg) => "w",
579             InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg) => "r",
580             InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_upper) => "d",
581             InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_pair) => "r",
582             InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_iw) => "w",
583             InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_ptr) => "e",
584             InlineAsmRegClass::S390x(S390xInlineAsmRegClass::reg) => "r",
585             InlineAsmRegClass::S390x(S390xInlineAsmRegClass::freg) => "f",
586             InlineAsmRegClass::Msp430(Msp430InlineAsmRegClass::reg) => "r",
587             InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
588                 bug!("LLVM backend does not support SPIR-V")
589             }
590             InlineAsmRegClass::Err => unreachable!(),
591         }
592         .to_string(),
593     }
594 }
595
596 /// Converts a modifier into LLVM's equivalent modifier.
597 fn modifier_to_llvm(
598     arch: InlineAsmArch,
599     reg: InlineAsmRegClass,
600     modifier: Option<char>,
601 ) -> Option<char> {
602     match reg {
603         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => modifier,
604         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg)
605         | InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => {
606             if modifier == Some('v') { None } else { modifier }
607         }
608         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => {
609             unreachable!("clobber-only")
610         }
611         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg) => None,
612         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
613         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16) => None,
614         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
615         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
616         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8) => Some('P'),
617         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg)
618         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8)
619         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => {
620             if modifier.is_none() {
621                 Some('q')
622             } else {
623                 modifier
624             }
625         }
626         InlineAsmRegClass::Hexagon(_) => None,
627         InlineAsmRegClass::Mips(_) => None,
628         InlineAsmRegClass::Nvptx(_) => None,
629         InlineAsmRegClass::PowerPC(_) => None,
630         InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg)
631         | InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => None,
632         InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => {
633             unreachable!("clobber-only")
634         }
635         InlineAsmRegClass::X86(X86InlineAsmRegClass::reg)
636         | InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => match modifier {
637             None if arch == InlineAsmArch::X86_64 => Some('q'),
638             None => Some('k'),
639             Some('l') => Some('b'),
640             Some('h') => Some('h'),
641             Some('x') => Some('w'),
642             Some('e') => Some('k'),
643             Some('r') => Some('q'),
644             _ => unreachable!(),
645         },
646         InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => None,
647         InlineAsmRegClass::X86(reg @ X86InlineAsmRegClass::xmm_reg)
648         | InlineAsmRegClass::X86(reg @ X86InlineAsmRegClass::ymm_reg)
649         | InlineAsmRegClass::X86(reg @ X86InlineAsmRegClass::zmm_reg) => match (reg, modifier) {
650             (X86InlineAsmRegClass::xmm_reg, None) => Some('x'),
651             (X86InlineAsmRegClass::ymm_reg, None) => Some('t'),
652             (X86InlineAsmRegClass::zmm_reg, None) => Some('g'),
653             (_, Some('x')) => Some('x'),
654             (_, Some('y')) => Some('t'),
655             (_, Some('z')) => Some('g'),
656             _ => unreachable!(),
657         },
658         InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => None,
659         InlineAsmRegClass::X86(X86InlineAsmRegClass::x87_reg | X86InlineAsmRegClass::mmx_reg) => {
660             unreachable!("clobber-only")
661         }
662         InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => None,
663         InlineAsmRegClass::Bpf(_) => None,
664         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_pair)
665         | InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_iw)
666         | InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_ptr) => match modifier {
667             Some('h') => Some('B'),
668             Some('l') => Some('A'),
669             _ => None,
670         },
671         InlineAsmRegClass::Avr(_) => None,
672         InlineAsmRegClass::S390x(_) => None,
673         InlineAsmRegClass::Msp430(_) => None,
674         InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
675             bug!("LLVM backend does not support SPIR-V")
676         }
677         InlineAsmRegClass::Err => unreachable!(),
678     }
679 }
680
681 /// Type to use for outputs that are discarded. It doesn't really matter what
682 /// the type is, as long as it is valid for the constraint code.
683 fn dummy_output_type<'ll>(cx: &CodegenCx<'ll, '_>, reg: InlineAsmRegClass) -> &'ll Type {
684     match reg {
685         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::reg) => cx.type_i32(),
686         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg)
687         | InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16) => {
688             cx.type_vector(cx.type_i64(), 2)
689         }
690         InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::preg) => {
691             unreachable!("clobber-only")
692         }
693         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::reg) => cx.type_i32(),
694         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg)
695         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg_low16) => cx.type_f32(),
696         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg)
697         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low16)
698         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::dreg_low8) => cx.type_f64(),
699         InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg)
700         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low8)
701         | InlineAsmRegClass::Arm(ArmInlineAsmRegClass::qreg_low4) => {
702             cx.type_vector(cx.type_i64(), 2)
703         }
704         InlineAsmRegClass::Hexagon(HexagonInlineAsmRegClass::reg) => cx.type_i32(),
705         InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg) => cx.type_i32(),
706         InlineAsmRegClass::Mips(MipsInlineAsmRegClass::freg) => cx.type_f32(),
707         InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg16) => cx.type_i16(),
708         InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg32) => cx.type_i32(),
709         InlineAsmRegClass::Nvptx(NvptxInlineAsmRegClass::reg64) => cx.type_i64(),
710         InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg) => cx.type_i32(),
711         InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => cx.type_i32(),
712         InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => cx.type_f64(),
713         InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
714         | InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
715             unreachable!("clobber-only")
716         }
717         InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),
718         InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::freg) => cx.type_f32(),
719         InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::vreg) => {
720             unreachable!("clobber-only")
721         }
722         InlineAsmRegClass::X86(X86InlineAsmRegClass::reg)
723         | InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd) => cx.type_i32(),
724         InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_byte) => cx.type_i8(),
725         InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg)
726         | InlineAsmRegClass::X86(X86InlineAsmRegClass::ymm_reg)
727         | InlineAsmRegClass::X86(X86InlineAsmRegClass::zmm_reg) => cx.type_f32(),
728         InlineAsmRegClass::X86(X86InlineAsmRegClass::kreg) => cx.type_i16(),
729         InlineAsmRegClass::X86(X86InlineAsmRegClass::x87_reg | X86InlineAsmRegClass::mmx_reg) => {
730             unreachable!("clobber-only")
731         }
732         InlineAsmRegClass::Wasm(WasmInlineAsmRegClass::local) => cx.type_i32(),
733         InlineAsmRegClass::Bpf(BpfInlineAsmRegClass::reg) => cx.type_i64(),
734         InlineAsmRegClass::Bpf(BpfInlineAsmRegClass::wreg) => cx.type_i32(),
735         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg) => cx.type_i8(),
736         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_upper) => cx.type_i8(),
737         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_pair) => cx.type_i16(),
738         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_iw) => cx.type_i16(),
739         InlineAsmRegClass::Avr(AvrInlineAsmRegClass::reg_ptr) => cx.type_i16(),
740         InlineAsmRegClass::S390x(S390xInlineAsmRegClass::reg) => cx.type_i32(),
741         InlineAsmRegClass::S390x(S390xInlineAsmRegClass::freg) => cx.type_f64(),
742         InlineAsmRegClass::Msp430(Msp430InlineAsmRegClass::reg) => cx.type_i16(),
743         InlineAsmRegClass::SpirV(SpirVInlineAsmRegClass::reg) => {
744             bug!("LLVM backend does not support SPIR-V")
745         }
746         InlineAsmRegClass::Err => unreachable!(),
747     }
748 }
749
750 /// Helper function to get the LLVM type for a Scalar. Pointers are returned as
751 /// the equivalent integer type.
752 fn llvm_asm_scalar_type<'ll>(cx: &CodegenCx<'ll, '_>, scalar: Scalar) -> &'ll Type {
753     match scalar.value {
754         Primitive::Int(Integer::I8, _) => cx.type_i8(),
755         Primitive::Int(Integer::I16, _) => cx.type_i16(),
756         Primitive::Int(Integer::I32, _) => cx.type_i32(),
757         Primitive::Int(Integer::I64, _) => cx.type_i64(),
758         Primitive::F32 => cx.type_f32(),
759         Primitive::F64 => cx.type_f64(),
760         Primitive::Pointer => cx.type_isize(),
761         _ => unreachable!(),
762     }
763 }
764
765 /// Fix up an input value to work around LLVM bugs.
766 fn llvm_fixup_input<'ll, 'tcx>(
767     bx: &mut Builder<'_, 'll, 'tcx>,
768     mut value: &'ll Value,
769     reg: InlineAsmRegClass,
770     layout: &TyAndLayout<'tcx>,
771 ) -> &'ll Value {
772     match (reg, layout.abi) {
773         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg), Abi::Scalar(s)) => {
774             if let Primitive::Int(Integer::I8, _) = s.value {
775                 let vec_ty = bx.cx.type_vector(bx.cx.type_i8(), 8);
776                 bx.insert_element(bx.const_undef(vec_ty), value, bx.const_i32(0))
777             } else {
778                 value
779             }
780         }
781         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16), Abi::Scalar(s)) => {
782             let elem_ty = llvm_asm_scalar_type(bx.cx, s);
783             let count = 16 / layout.size.bytes();
784             let vec_ty = bx.cx.type_vector(elem_ty, count);
785             if let Primitive::Pointer = s.value {
786                 value = bx.ptrtoint(value, bx.cx.type_isize());
787             }
788             bx.insert_element(bx.const_undef(vec_ty), value, bx.const_i32(0))
789         }
790         (
791             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16),
792             Abi::Vector { element, count },
793         ) if layout.size.bytes() == 8 => {
794             let elem_ty = llvm_asm_scalar_type(bx.cx, element);
795             let vec_ty = bx.cx.type_vector(elem_ty, count);
796             let indices: Vec<_> = (0..count * 2).map(|x| bx.const_i32(x as i32)).collect();
797             bx.shuffle_vector(value, bx.const_undef(vec_ty), bx.const_vector(&indices))
798         }
799         (InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
800             if s.value == Primitive::F64 =>
801         {
802             bx.bitcast(value, bx.cx.type_i64())
803         }
804         (
805             InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg | X86InlineAsmRegClass::zmm_reg),
806             Abi::Vector { .. },
807         ) if layout.size.bytes() == 64 => bx.bitcast(value, bx.cx.type_vector(bx.cx.type_f64(), 8)),
808         (
809             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg | ArmInlineAsmRegClass::sreg_low16),
810             Abi::Scalar(s),
811         ) => {
812             if let Primitive::Int(Integer::I32, _) = s.value {
813                 bx.bitcast(value, bx.cx.type_f32())
814             } else {
815                 value
816             }
817         }
818         (
819             InlineAsmRegClass::Arm(
820                 ArmInlineAsmRegClass::dreg
821                 | ArmInlineAsmRegClass::dreg_low8
822                 | ArmInlineAsmRegClass::dreg_low16,
823             ),
824             Abi::Scalar(s),
825         ) => {
826             if let Primitive::Int(Integer::I64, _) = s.value {
827                 bx.bitcast(value, bx.cx.type_f64())
828             } else {
829                 value
830             }
831         }
832         (InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg), Abi::Scalar(s)) => match s.value {
833             // MIPS only supports register-length arithmetics.
834             Primitive::Int(Integer::I8 | Integer::I16, _) => bx.zext(value, bx.cx.type_i32()),
835             Primitive::F32 => bx.bitcast(value, bx.cx.type_i32()),
836             Primitive::F64 => bx.bitcast(value, bx.cx.type_i64()),
837             _ => value,
838         },
839         _ => value,
840     }
841 }
842
843 /// Fix up an output value to work around LLVM bugs.
844 fn llvm_fixup_output<'ll, 'tcx>(
845     bx: &mut Builder<'_, 'll, 'tcx>,
846     mut value: &'ll Value,
847     reg: InlineAsmRegClass,
848     layout: &TyAndLayout<'tcx>,
849 ) -> &'ll Value {
850     match (reg, layout.abi) {
851         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg), Abi::Scalar(s)) => {
852             if let Primitive::Int(Integer::I8, _) = s.value {
853                 bx.extract_element(value, bx.const_i32(0))
854             } else {
855                 value
856             }
857         }
858         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16), Abi::Scalar(s)) => {
859             value = bx.extract_element(value, bx.const_i32(0));
860             if let Primitive::Pointer = s.value {
861                 value = bx.inttoptr(value, layout.llvm_type(bx.cx));
862             }
863             value
864         }
865         (
866             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16),
867             Abi::Vector { element, count },
868         ) if layout.size.bytes() == 8 => {
869             let elem_ty = llvm_asm_scalar_type(bx.cx, element);
870             let vec_ty = bx.cx.type_vector(elem_ty, count * 2);
871             let indices: Vec<_> = (0..count).map(|x| bx.const_i32(x as i32)).collect();
872             bx.shuffle_vector(value, bx.const_undef(vec_ty), bx.const_vector(&indices))
873         }
874         (InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
875             if s.value == Primitive::F64 =>
876         {
877             bx.bitcast(value, bx.cx.type_f64())
878         }
879         (
880             InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg | X86InlineAsmRegClass::zmm_reg),
881             Abi::Vector { .. },
882         ) if layout.size.bytes() == 64 => bx.bitcast(value, layout.llvm_type(bx.cx)),
883         (
884             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg | ArmInlineAsmRegClass::sreg_low16),
885             Abi::Scalar(s),
886         ) => {
887             if let Primitive::Int(Integer::I32, _) = s.value {
888                 bx.bitcast(value, bx.cx.type_i32())
889             } else {
890                 value
891             }
892         }
893         (
894             InlineAsmRegClass::Arm(
895                 ArmInlineAsmRegClass::dreg
896                 | ArmInlineAsmRegClass::dreg_low8
897                 | ArmInlineAsmRegClass::dreg_low16,
898             ),
899             Abi::Scalar(s),
900         ) => {
901             if let Primitive::Int(Integer::I64, _) = s.value {
902                 bx.bitcast(value, bx.cx.type_i64())
903             } else {
904                 value
905             }
906         }
907         (InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg), Abi::Scalar(s)) => match s.value {
908             // MIPS only supports register-length arithmetics.
909             Primitive::Int(Integer::I8, _) => bx.trunc(value, bx.cx.type_i8()),
910             Primitive::Int(Integer::I16, _) => bx.trunc(value, bx.cx.type_i16()),
911             Primitive::F32 => bx.bitcast(value, bx.cx.type_f32()),
912             Primitive::F64 => bx.bitcast(value, bx.cx.type_f64()),
913             _ => value,
914         },
915         _ => value,
916     }
917 }
918
919 /// Output type to use for llvm_fixup_output.
920 fn llvm_fixup_output_type<'ll, 'tcx>(
921     cx: &CodegenCx<'ll, 'tcx>,
922     reg: InlineAsmRegClass,
923     layout: &TyAndLayout<'tcx>,
924 ) -> &'ll Type {
925     match (reg, layout.abi) {
926         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg), Abi::Scalar(s)) => {
927             if let Primitive::Int(Integer::I8, _) = s.value {
928                 cx.type_vector(cx.type_i8(), 8)
929             } else {
930                 layout.llvm_type(cx)
931             }
932         }
933         (InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16), Abi::Scalar(s)) => {
934             let elem_ty = llvm_asm_scalar_type(cx, s);
935             let count = 16 / layout.size.bytes();
936             cx.type_vector(elem_ty, count)
937         }
938         (
939             InlineAsmRegClass::AArch64(AArch64InlineAsmRegClass::vreg_low16),
940             Abi::Vector { element, count },
941         ) if layout.size.bytes() == 8 => {
942             let elem_ty = llvm_asm_scalar_type(cx, element);
943             cx.type_vector(elem_ty, count * 2)
944         }
945         (InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
946             if s.value == Primitive::F64 =>
947         {
948             cx.type_i64()
949         }
950         (
951             InlineAsmRegClass::X86(X86InlineAsmRegClass::xmm_reg | X86InlineAsmRegClass::zmm_reg),
952             Abi::Vector { .. },
953         ) if layout.size.bytes() == 64 => cx.type_vector(cx.type_f64(), 8),
954         (
955             InlineAsmRegClass::Arm(ArmInlineAsmRegClass::sreg | ArmInlineAsmRegClass::sreg_low16),
956             Abi::Scalar(s),
957         ) => {
958             if let Primitive::Int(Integer::I32, _) = s.value {
959                 cx.type_f32()
960             } else {
961                 layout.llvm_type(cx)
962             }
963         }
964         (
965             InlineAsmRegClass::Arm(
966                 ArmInlineAsmRegClass::dreg
967                 | ArmInlineAsmRegClass::dreg_low8
968                 | ArmInlineAsmRegClass::dreg_low16,
969             ),
970             Abi::Scalar(s),
971         ) => {
972             if let Primitive::Int(Integer::I64, _) = s.value {
973                 cx.type_f64()
974             } else {
975                 layout.llvm_type(cx)
976             }
977         }
978         (InlineAsmRegClass::Mips(MipsInlineAsmRegClass::reg), Abi::Scalar(s)) => match s.value {
979             // MIPS only supports register-length arithmetics.
980             Primitive::Int(Integer::I8 | Integer::I16, _) => cx.type_i32(),
981             Primitive::F32 => cx.type_i32(),
982             Primitive::F64 => cx.type_i64(),
983             _ => layout.llvm_type(cx),
984         },
985         _ => layout.llvm_type(cx),
986     }
987 }