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

[AArch64] Redundant mov generated for return value

Open jmolloy opened this issue 11 years ago • 6 comments

Bugzilla Link 22852
Resolution INVALID
Resolved on Aug 14, 2015 14:11
Version trunk
OS All

Extended Description

The following code:

unsigned long baz (unsigned long a, unsigned long b) { return (a & 0xffffffff) | (b & 0xffffffff00000000); }

Produces:

baz: // @​baz // BB#0: // %entry bfxil x1, x0, #​0, #​32 mov x0, x1 ret

The mov here is redundant; we can just emit "bfxil x0, x0, #​0, #​32".

jmolloy avatar Mar 09 '15 14:03 jmolloy

Closing this as invalid as Jun and I have come to the same conclusion; the mov is necessary.

llvmbot avatar Aug 14 '15 21:08 llvmbot

As far as I check, "extr x0, x1, x0, 32" doesn't seem to be a correct instruction for the original code sequence because it extracts a bit seqence<32+64-1,32> from the concatenated x1 and x0 like : concat = x1:x0 x0 = concat<95,32>. Therefore, Xd of extr will contain x1<31:0> in Xd<63:32> and x0<63:32> in Xd<31:0>, which is the opposite as Chad mentioned in comment #​4.

I wasn't able to find a single instruction that matches with the original code sequence. So, for me, looks like the mov is necessary. Please let me know if you can see any other good match.

llvmbot avatar Aug 14 '15 20:08 llvmbot

Rightly pointed out by my GCC colleague, bfxil does require a mov here as it reads Xd.

"extr x0, x1, x0, 32" looks like a better match.

James, Perhaps I don't understand how the extr instruction works, but I don't think "extr x0, x1, x0, 32" is equivalent to the original code sequence.

AFAIK, the extr instruction concatenates x1(b) and x0(a) together and then right shifts by a constant. I believe the low 32 bits of x0 will be the high 32 bits of a and the high 32 bits of x0 will be the low 32 bits of b. We want the opposite.. the low 32 bits of x0 will be the low 32 bits of a and the high 32 bits of x0 will be the *high 32 bits of b.

Note that "ror Wd, Ws, #imm" is the same as "extr Wd, Ws, Ws, #imm". Please let me know if I've missed something.

llvmbot avatar Jun 03 '15 17:06 llvmbot

Any suggestion as to how this can be fixed? My first thought was a target-specific DAG combine, but perhaps it could be handled with some pattern matching in a td file.

I gather isBitfieldInsertOpFromOr() from AArch64ISelDAGToDAG.cpp is the right place.

llvmbot avatar May 29 '15 21:05 llvmbot

Any suggestion as to how this can be fixed? My first thought was a target-specific DAG combine, but perhaps it could be handled with some pattern matching in a td file.

llvmbot avatar May 28 '15 23:05 llvmbot

Rightly pointed out by my GCC colleague, bfxil does require a mov here as it reads Xd.

"extr x0, x1, x0, 32" looks like a better match.

jmolloy avatar Mar 09 '15 17:03 jmolloy