How to prevent optimizer from reordering stuff?

Dan Olson via digitalmars-d-ldc digitalmars-d-ldc at
Sat Mar 14 11:42:44 PDT 2015

While tracking down std.math problems for ARM, I find that optimizer
will reorder instructions to get FPSCR flags before the divide

Is there is a way to force instruction ordering here?  I tried the
llvm_memory_fence, but it doesn't do the job.

real zero = 0.0;

void foo()
    import std.math, std.c.stdio, ldc.llvmasm;

    real x = 1.0 / zero;

    auto f = __asm!uint("vmrs $0, fpscr", "=r");
    IeeeFlags flags = ieeeFlags();
    printf("%f, %u %d\n", x, f, flags.divByZero);

Compiled with -O -mtriple=thumbv7-apple-ios, you can see that vdiv is
after both my inline asm and std.math ieeeFlags().

	vldr	d8, [r0]
	@ InlineAsm Start
	vmrs	r4, fpscr
	@ InlineAsm End
	mov	r0, r5
	blx	__D3std4math9ieeeFlagsFNdZS3std4math9IeeeFlags
	vmov.f64	d16, #1.000000e+00
	mov	r0, r5
	vdiv.f64	d8, d16, d8

What to do?

More information about the digitalmars-d-ldc mailing list