llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

inline asm "rm" constraint lowered "m" when "r" would be preferable

Open d0k opened this issue 12 years ago • 73 comments

Bugzilla Link 20197
Version trunk
OS Linux
Blocks llvm/llvm-project#4440
CC @legrosbuffle,@echristo,@isanbard,@josephcsible,@nickdesaulniers,@zygoloid,@tstellar

Extended Description

When multiple alternatives in an inline asm constraint are given we ignore all of them but the most "general". This gives nasty artifacts in the code.

int bsr(unsigned v) {
  int ret;
  __asm__("bsr %1, %0" : "=&r"(ret) : "rm"(v) : "cc");
  return ret;
}

$ clang -O3 -S -o - t.c
bsr:
	movl	%edi, -4(%rsp)
	#APP
	bsrl	-4(%rsp), %eax
	#NO_APP
	retq

The spilling is totally unnecessary. GCC gets this one right. On 32 bit x86 it's even worse:

$ clang -O3 -S -o - t.c -m32
bsr:
	pushl	%eax
	movl	8(%esp), %eax
	movl	%eax, (%esp)
	#APP
	bsrl	(%esp), %eax
	#NO_APP
	popl	%edx
	retl

GCC knows a better way:

$ gcc-4.8 -O3 -S -o - t.c -m32
bsr:
#APP
	bsr 4(%esp), %eax
#NO_APP
	ret

The constraint "g" is just as bad, being translated into "imr" internally.

d0k avatar Jul 03 '14 16:07 d0k

mentioned in issue llvm/llvm-bugzilla-archive#49406

efriedma-quic avatar Nov 27 '21 04:11 efriedma-quic

mentioned in issue llvm/llvm-bugzilla-archive#47530

efriedma-quic avatar Nov 27 '21 03:11 efriedma-quic

mentioned in issue llvm/llvm-project#4440

edwintorok avatar Nov 27 '21 01:11 edwintorok

mentioned in issue llvm/llvm-bugzilla-archive#37583

llvmbot avatar Nov 27 '21 00:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#35489

llvmbot avatar Nov 27 '21 00:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#31525

llvmbot avatar Nov 26 '21 22:11 llvmbot

*** Bug #48750 has been marked as a duplicate of this bug. ***

efriedma-quic avatar Mar 03 '21 23:03 efriedma-quic

[Copy-n-paste of my harebrained idea here]

The major issue with supporting multiple constraints is how we model those constraints until register allocation is complete. (Thank you, Capt. Obvious!) The decision of which constraint to use is made during DAG selection. So there's no (easy) way to change this during register allocation.

Half-baked idea (all very hand-wavy):

Different constraints use different set ups (pre-asm code) and tear downs (post-asm code) for the inline asm. What if we created pseudo-instructions to represent inline asm set up and tear down? Something like this:

INLINEASM_SETUP <representing register/memory setup> INLINEASM <...> INLINEASM_TEARDOWN <representing copy of results into vregs/pregs>

The register allocator could then try different constraints (going from most restrictive to least restrictive) until it find one that works.

One drawback is that the RA needs to process INLINEASM before it can generate the correct code for INLINEASM_SETUP. That might be doable if the three instructions are treated as a single unit.

isanbard avatar Sep 15 '20 22:09 isanbard

*** Bug #46874 has been marked as a duplicate of this bug. ***

efriedma-quic avatar Sep 15 '20 22:09 efriedma-quic

Would not it be better to at least treat 'mr' as 'r' rather than 'm'? This will probably yield better code in many cases.

"mr" isn't being treated as "m". Consider this C function:

void f(int x) {
    asm volatile("# x is in %0" :: "mr"(x));
}

With "clang -O3 -m32", it compiles into this mess:

f:
        pushl   %eax
        movl    8(%esp), %eax
        movl    %eax, (%esp)
        # x is in (%esp)
        popl    %eax
        retl

But if I use "m" instead of "mr", then it compiles into what I wanted:

f:
        # x is in 4(%esp)
        retl

So the presence of "r" is somehow making the codegen worse even though it's putting the value in memory anyway.

josephcsible avatar Jun 06 '20 18:06 josephcsible

*** Bug #30873 has been marked as a duplicate of this bug. ***

llvmbot avatar Jun 01 '18 14:06 llvmbot

*** Bug #36931 has been marked as a duplicate of this bug. ***

llvmbot avatar Jun 01 '18 14:06 llvmbot

Would not it be better to at least treat 'mr' as 'r' rather than 'm'? This will probably yield better code in many cases.

llvmbot avatar Mar 20 '18 17:03 llvmbot

*** Bug #34837 has been marked as a duplicate of this bug. ***

llvmbot avatar Mar 20 '18 15:03 llvmbot

Agreed.

echristo avatar Jul 03 '14 19:07 echristo

I was thinking of this issue while reading ChooseConstraint in llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp.

nickdesaulniers avatar Dec 13 '21 22:12 nickdesaulniers

I was thinking of this issue while reading ChooseConstraint in llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp.

We need to talk about your reading habits. :-P

bwendling avatar Dec 13 '21 22:12 bwendling

@llvm/issue-subscribers-backend-x86

llvmbot avatar Aug 02 '22 08:08 llvmbot

At the llvm dev conf '22 @topperc or @compnerd or @arsenm mentioned that perhaps we could add a new register flag (akin to early-clobber, def, or kill) and then another pass ("memfold"? or possibly just regalloc) could try to DTRT.

EDIT: I think @topperc was referring to TargetInstrInfo::foldMemoryOperand or InlineSpiller::foldMemoryOperand.

nickdesaulniers avatar Nov 28 '22 18:11 nickdesaulniers

Ah, looks like we haven't made any progress on this since my last report above 9mo ago. Still an issue though. https://lore.kernel.org/lkml/CAHk-=whVvD05T0yD5DQj803uETLD6qDq-Vx-SiLPcrL=eO77LQ@mail.gmail.com/

nickdesaulniers avatar Aug 25 '23 22:08 nickdesaulniers