Skip to content

Commit 4945d21

Browse files
committed
Implement ppc/ppc64 preserves_flags option for inline asm
Implemented preserves_flags on powerpc by making it do nothing. This prevents having two different ways to mark `cr0` as clobbered. clang and gcc alias `cr0` to `cc`. The gcc inline documentation does not state what this does on powerpc* targets, but inspection of the source shows it is equivalent to condition register field `cr0`, so it should not be added.
1 parent 3c09d4a commit 4945d21

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

compiler/rustc_codegen_gcc/src/asm.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,16 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
546546
}
547547

548548
if !options.contains(InlineAsmOptions::PRESERVES_FLAGS) {
549-
// TODO(@Commeownist): I'm not 100% sure this one clobber is sufficient
550-
// on all architectures. For instance, what about FP stack?
551-
extended_asm.add_clobber("cc");
549+
match asm_arch {
550+
InlineAsmArch::PowerPC | InlineAsmArch::PowerPC64 => {
551+
// "cc" is cr0 on powerpc.
552+
}
553+
// TODO(@Commeownist): I'm not 100% sure this one clobber is sufficient
554+
// on all architectures. For instance, what about FP stack?
555+
_ => {
556+
extended_asm.add_clobber("cc");
557+
}
558+
}
552559
}
553560
if !options.contains(InlineAsmOptions::NOMEM) {
554561
extended_asm.add_clobber("memory");

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,38 @@ 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", ""()
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}"()
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 "nop", "={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}"()
108+
// powerpc64: asm sideeffect "nop", "={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}"()
109+
// powerpc64le: asm sideeffect "nop", "={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}"()
110+
// aix64: asm sideeffect "nop", "={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}"()
111+
pub unsafe fn clobber_abi_no_preserves_flags() {
112+
// Use a nop to prevent aliasing of identical functions here.
113+
asm!("nop", clobber_abi("C"), options(nostack, nomem));
114+
}

0 commit comments

Comments
 (0)