Skip to content

Commit 05ad94b

Browse files
committed
Implement ppc/ppc64 preserves_flags option for inline asm
Implemented preserves_flags similar to the `cc` pseudo-register on gcc and clang. The gcc inline documentation doesn't actually state what this does on powerpc* targets, but inspection of the source shows it is equivalent to condition register field `cr0`.
1 parent fb006ee commit 05ad94b

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
251251
constraints.push("~{sreg}".to_string());
252252
}
253253
InlineAsmArch::Nvptx64 => {}
254-
InlineAsmArch::PowerPC | InlineAsmArch::PowerPC64 => {}
254+
InlineAsmArch::PowerPC | InlineAsmArch::PowerPC64 => {
255+
constraints.push("~{cr0}".to_string());
256+
}
255257
InlineAsmArch::Hexagon => {}
256258
InlineAsmArch::LoongArch32 | InlineAsmArch::LoongArch64 => {
257259
constraints.extend_from_slice(&[

src/doc/unstable-book/src/language-features/asm-experimental-arch.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ These flags registers must be restored upon exiting the asm block if the `preser
201201
- The status register `r2`.
202202
- M68k
203203
- The condition code register `ccr`.
204+
- PowerPC
205+
- The condition register field `cr0`
204206
- SPARC
205207
- Integer condition codes (`icc` and `xcc`)
206208
- Floating-point condition codes (`fcc[0-3]`)

tests/codegen-llvm/asm/powerpc-clobbers.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,37 @@ pub unsafe fn vs32_clobber() {
7777
pub unsafe fn clobber_abi() {
7878
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));
7979
}
80+
81+
// CHECK-LABEL: @clobber_no_preserves_flags
82+
// CHECK: call void asm sideeffect "nop", "~{cr0}"()
83+
#[no_mangle]
84+
pub unsafe fn clobber_no_preserves_flags() {
85+
// Use a nop to prevent aliasing of identical functions here.
86+
asm!("nop", options(nostack, nomem));
87+
}
88+
89+
// CHECK-LABEL: @cr0_clobber_no_preserves_flags
90+
// CHECK: call void asm sideeffect "nop; nop", "~{cr0},~{cr0}"()
91+
#[no_mangle]
92+
pub unsafe fn cr0_clobber_no_preserves_flags() {
93+
// Use nop; nop to prevent aliasing of identical functions here.
94+
asm!("nop; nop", out("cr0") _, options(nostack, nomem));
95+
}
96+
97+
// CHECK-LABEL: @clobber_preservesflags
98+
// CHECK: call void asm sideeffect "", "~{memory}"()
99+
#[no_mangle]
100+
pub unsafe fn clobber_preservesflags() {
101+
asm!("", options(nostack, preserves_flags));
102+
}
103+
104+
// Output format depends on the availability of altivec and vsx
105+
// CHECK-LABEL: @clobber_abi_no_preserves_flags
106+
#[no_mangle]
107+
// powerpc: asm sideeffect "", "={r0},={r3},={r4},={r5},={r6},={r7},={r8},={r9},={r10},={r11},={r12},={f0},={f1},={f2},={f3},={f4},={f5},={f6},={f7},={f8},={f9},={f10},={f11},={f12},={f13},~{vs0},~{vs1},~{vs2},~{vs3},~{vs4},~{vs5},~{vs6},~{vs7},~{vs8},~{vs9},~{vs10},~{vs11},~{vs12},~{vs13},~{vs14},~{vs15},~{vs16},~{vs17},~{vs18},~{vs19},~{vs20},~{vs21},~{vs22},~{vs23},~{vs24},~{vs25},~{vs26},~{vs27},~{vs28},~{vs29},~{vs30},~{vs31},~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19},~{cr0},~{cr1},~{cr5},~{cr6},~{cr7},~{ctr},~{lr},~{xer},~{cr0}"()
108+
// powerpc64: asm sideeffect "", "={r0},={r3},={r4},={r5},={r6},={r7},={r8},={r9},={r10},={r11},={r12},={f0},={f1},={f2},={f3},={f4},={f5},={f6},={f7},={f8},={f9},={f10},={f11},={f12},={f13},={v0},={v1},={v2},={v3},={v4},={v5},={v6},={v7},={v8},={v9},={v10},={v11},={v12},={v13},={v14},={v15},={v16},={v17},={v18},={v19},~{vs0},~{vs1},~{vs2},~{vs3},~{vs4},~{vs5},~{vs6},~{vs7},~{vs8},~{vs9},~{vs10},~{vs11},~{vs12},~{vs13},~{vs14},~{vs15},~{vs16},~{vs17},~{vs18},~{vs19},~{vs20},~{vs21},~{vs22},~{vs23},~{vs24},~{vs25},~{vs26},~{vs27},~{vs28},~{vs29},~{vs30},~{vs31},~{cr0},~{cr1},~{cr5},~{cr6},~{cr7},~{ctr},~{lr},~{xer},~{cr0}"()
109+
// powerpc64le: asm sideeffect "", "={r0},={r3},={r4},={r5},={r6},={r7},={r8},={r9},={r10},={r11},={r12},={f0},={f1},={f2},={f3},={f4},={f5},={f6},={f7},={f8},={f9},={f10},={f11},={f12},={f13},={vs0},={vs1},={vs2},={vs3},={vs4},={vs5},={vs6},={vs7},={vs8},={vs9},={vs10},={vs11},={vs12},={vs13},={vs14},={vs15},={vs16},={vs17},={vs18},={vs19},={vs20},={vs21},={vs22},={vs23},={vs24},={vs25},={vs26},={vs27},={vs28},={vs29},={vs30},={vs31},={v0},={v1},={v2},={v3},={v4},={v5},={v6},={v7},={v8},={v9},={v10},={v11},={v12},={v13},={v14},={v15},={v16},={v17},={v18},={v19},~{cr0},~{cr1},~{cr5},~{cr6},~{cr7},~{ctr},~{lr},~{xer},~{cr0}"()
110+
// aix64: asm sideeffect "", "={r0},={r3},={r4},={r5},={r6},={r7},={r8},={r9},={r10},={r11},={r12},={f0},={f1},={f2},={f3},={f4},={f5},={f6},={f7},={f8},={f9},={f10},={f11},={f12},={f13},={vs0},={vs1},={vs2},={vs3},={vs4},={vs5},={vs6},={vs7},={vs8},={vs9},={vs10},={vs11},={vs12},={vs13},={vs14},={vs15},={vs16},={vs17},={vs18},={vs19},={vs20},={vs21},={vs22},={vs23},={vs24},={vs25},={vs26},={vs27},={vs28},={vs29},={vs30},={vs31},={v0},={v1},={v2},={v3},={v4},={v5},={v6},={v7},={v8},={v9},={v10},={v11},={v12},={v13},={v14},={v15},={v16},={v17},={v18},={v19},~{cr0},~{cr1},~{cr5},~{cr6},~{cr7},~{ctr},~{lr},~{xer},~{cr0}"()
111+
pub unsafe fn clobber_abi_no_preserves_flags() {
112+
asm!("", clobber_abi("C"), options(nostack, nomem));
113+
}

0 commit comments

Comments
 (0)