This is an archive of the discontinued LLVM Phabricator instance.

[DAG] FoldConstantArithmetic - fold bitlogic(bitcast(x),bitcast(y)) -> bitcast(bitlogic(x,y))
ClosedPublic

Authored by RKSimon on Nov 4 2021, 9:58 AM.

Details

Summary

To constant fold bitwise logic ops where we've legalized constant build vectors to a different type (e.g. v2i64 -> v4i32), this patch adds a basic ability to peek through the bitcasts and perform the constant fold on the inner operands.

@dmgreen I'm not sure what is the best fix for the MVE predicate v2i64 regressions - its expanded the bitselect patten and constant folded the 'andnot(x,c)' to 'and(x,~c)' - this was already bad codegen, I think ideally this should be narrowed to a v4i32 equivalent, but I'm not sure how best to address this - hints welcome :)

One of the yak shaving fixes for D113192....

Diff Detail

Event Timeline

RKSimon created this revision.Nov 4 2021, 9:58 AM
RKSimon requested review of this revision.Nov 4 2021, 9:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2021, 9:58 AM
lebedev.ri added inline comments.Nov 4 2021, 10:02 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5298

Can we have cases where only one of operands is a bitcast / both are bitcasts but only one should be peeked?

RKSimon added inline comments.Nov 4 2021, 10:53 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5298

Yes - and if we ever generalize this to work with any opcode that's what we'll end up having to do (probably in a similar way to the per-element-computeKnownBits code you did a few months ago?).

Its whether such IR actually appears for just this bitlogic case - this tends to mainly occur on 32-bit targets with vXi64 BUILD_VECTORs which get legalized to v2Xi32.

@dmgreen I'm not sure what is the best fix for the MVE predicate v2i64 regressions - its expanded the bitselect patten and constant folded the 'andnot(x,c)' to 'and(x,~c)' - this was already bad codegen, I think ideally this should be narrowed to a v4i32 equivalent, but I'm not sure how best to address this - hints welcome :)

I have a task I want to get done soon which changes v2i1 to a legal type - these tests will likely all change as that happens. Feel free to ignore the MVE change until then.

lebedev.ri accepted this revision.Nov 5 2021, 4:29 AM

Seems like an improvement to me.

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5300–5301

Do we expect to have a bitcast-of-bitcast? These are folded to a single bitcast i think.

This revision is now accepted and ready to land.Nov 5 2021, 4:29 AM

Thanks @lebedev.ri - I'm still intending to look at generalizing this in the future

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5300–5301

Yes, but depending on the order of the calls to the combines these often get missed :(