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