Expand inline asm with "sbr" instruction

Hello. I’m compiling the following code:

unsigned
lg_floor(size_t x) {
	size_t ret;
	assert(x != 0);

	asm ("bsr %1, %0"
	    : "=r"(ret) // Outputs.
	    : "r"(x)    // Inputs.
	    );
	assert(ret < UINT_MAX);
	return (unsigned)ret;
}

The resulting IR contains an inline asm instruction, which I want to lift into LLVM IR. From what I can tell, the proper way to do this would be

  • Get a TargetMachine
  • Call TM->getSubtargetImpl() to get a X86TargetLowering.
  • Call TL->expandInlineAsm()
  • Teach X86TargetLowering::ExpandInlineAsm() to handle “sbr”.

Is my analysis correct? Can that change be accepted upstream?

Use __builtin_clzll instead? Compiler Explorer

I can not touch the code in question, otherwise I’d just replaced it with generic non-asm version, which I also have by the hand.

Is there any special reason to lift it into LLVM IR? The method you listed is called during CodeGenPrepare. It is supposed to used for optimizations in the backend.
Not sure if you can hack here if you just want to emit the IR.

Is there any special reason to lift it into LLVM IR?

I’m running analysis on KLEE and it doesn’t support inline asm. It has a special pass called RaiseAsm, that calls expandInlineAsm(), which is why I thought it’d be better to implement this on LLVM side.

Not sure if you can hack here if you just want to emit the IR.

How would AsmParser help me?

How would AsmParser help me?

I don’t have a clear thought either. Currently it doesn’t have the ability to turn assembly into LLVM IR. Parsing it in a separate pass looks more concise if we only have a few assemble to handle.
Why not expanding them directly in the pass? How about constraints other than “r”?

Why not expanding them directly in the pass?

I thought expandInlineAsm is more appropriate place for this code, since it already raises bswap to the IR. But I guess I will just do it on the KLEE side.

Yeah. The code was there for more than 10 years Copy ExpandInlineAsm to TargetLowering from TargetAsmInfo. · llvm/llvm-project@5849d22 · GitHub. No new expansion added since then.
From the PoW of codegen, the expansion doesn’t help much. Users are supposed to use builtins for flexibility and inline asm for special purpose, i.e., register constraints, asm optimizations etc. Given that, it is also not a good idea to arbitrarily do such work, especially under -O0.
For example, we can lower builtins according to the targets feature. Compiler Explorer
But lowering an inline bsf into tzcnt under -O0 does seem compiler issue.