This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][Atomic] Canonicalize sub of immediate to add of -immediate.
AbandonedPublic

Authored by mcrosier on Jul 13 2017, 11:38 AM.

Details

Summary

Please let me know if I've missed something or if there's an easier way to do this in tablegen.

This avoids ugliness such as:

test_atomic_sub_fetch:                  // @test_atomic_sub_fetch
// BB#0:                                // %entry
        mov     w8, #-1
        neg      w8, w8
        ldaddal w8, w8, [x0]
        add     w0, w8, #1              // =1
        ret

and instead generates:

test_atomic_sub_fetch:                  // @test_atomic_sub_fetch
// BB#0:                                // %entry
        orr     w8, wzr, #0x1
        ldaddal w8, w8, [x0]
        add     w0, w8, #1              // =1
        ret

Chad

Diff Detail

Event Timeline

mcrosier created this revision.Jul 13 2017, 11:38 AM
mcrosier edited the summary of this revision. (Show Details)Jul 13 2017, 11:38 AM
gberry edited edge metadata.Jul 13 2017, 1:48 PM

Perhaps this would be better done at the IR level, in InstCombine?

Perhaps this would be better done at the IR level, in InstCombine?

Seems reasonable, but we'd have to know this is a good canonicalization for all targets, right?

Perhaps this would be better done at the IR level, in InstCombine?

Seems reasonable, but we'd have to know this is a good canonicalization for all targets, right?

It seems like it is to me. Worst case (e.g. the target supports 'add' but not 'sub'), the sub would ideally just be transformed back and a negate added during ISel legalization.

christof edited edge metadata.Jul 14 2017, 6:20 AM

Perhaps this would be better done at the IR level, in InstCombine?

Seems reasonable, but we'd have to know this is a good canonicalization for all targets, right?

It seems like it is to me. Worst case (e.g. the target supports 'add' but not 'sub'), the sub would ideally just be transformed back and a negate added during ISel legalization.

Surely it depends on the immediate range and add/sub which are supported by the target whether or not it is good for that target? Negative immediate might be harder for some targets, I would expect. I won't be surprised if some code generators give a regression if that normalisation is done in general. They might generate worse code if they see add -1 instead of sub 1. Just my two cents.

I am not really well known with all the tricks you can do with tablegen, but I would think this particular rewrite could be done with a pattern match in tablegen, could it not? I think there is already something for add -imm to sub imm.

I suppose the analogous InstCombine canonicalization in this case is to convert all 'sub's of immediates to 'add's of negated immediates doesn't really apply, since there isn't much additional optimization that can be done on the atomic operations themselves.

Is there any reason to restrict this to cases where the sub operand is a constant? Since we're always going to add a sub 0, x in ISel, adding it a bit earlier in the DAG combine phase would allow for additional optimizations in the non-constant case as well (e.g. folding a double negate away).

mcrosier abandoned this revision.Jul 24 2017, 8:26 AM

I'm not going to pursue this further, but if someone wants to pick up this work I'd be more than happy to assist.