This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Add icmp UNDEF handling to SelectionDAG::FoldSetCC
ClosedPublic

Authored by RKSimon on Mar 14 2019, 6:55 AM.

Details

Summary

First half of PR40800, this patch adds DAG undef handling to icmp instructions to match the behaviour in SimplifyICmpInst, this permits constant folding of vector comparisons where some elements had been reduced to UNDEF (by SimplifyDemandedVectorElts etc.).

This involves a lot of tweaking to reduced tests as bugpoint loves to reduce icmp arguments to undef........

I've gone through these tests best I can, and AFAICT I haven't changed their purposes in any critical way - many are fragile and difficult to reduce from scratch as they are often 5+ years old.... I'd appreciate it if the respective target specialists could check my changes. I'd like to pre-commit some of these changes where its just a case of 'unreducing' lost arguments etc.

Diff Detail

Repository
rL LLVM

Event Timeline

RKSimon created this revision.Mar 14 2019, 6:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 14 2019, 6:55 AM
efriedma added inline comments.Mar 14 2019, 11:51 AM
test/CodeGen/ARM/ifcvt-branch-weight-bug.ll
1

LGTM to commit separately.

test/CodeGen/ARM/struct-byval-frame-index.ll
1

Also LGTM to commit separately. (It's sort of hard to tell what this is aiming for, but I think you're fine as long as you preserve the CFG.)

tlively added inline comments.Mar 14 2019, 1:00 PM
test/CodeGen/WebAssembly/cfg-stackify.ll
370

WebAssembly changes LGTM. Thanks for checking!

RKSimon updated this revision to Diff 190805.Mar 15 2019, 4:17 AM

rebase - thanks @efriedma and @tlively

RKSimon updated this revision to Diff 190806.Mar 15 2019, 4:25 AM

rebase after regenerating sparc test in rL356253

The Hexagon changes look good.

RKSimon updated this revision to Diff 190830.Mar 15 2019, 8:13 AM

rebase - thanks @kparzysz

I think the SystemZ test changes look ok (by replacing the undef operands with an argument the tests are more or less unaffected by this patch), but as usual I will let Uli do the formal approval.

@craig.topper Are you happy with the x86 changes?

I think the SystemZ test changes look ok (by replacing the undef operands with an argument the tests are more or less unaffected by this patch), but as usual I will let Uli do the formal approval.

The SystemZ test changes LGTM. It's probably better anyway to have real arguments instead of undefs ...

I think the SystemZ test changes look ok (by replacing the undef operands with an argument the tests are more or less unaffected by this patch), but as usual I will let Uli do the formal approval.

The SystemZ test changes LGTM. It's probably better anyway to have real arguments instead of undefs ...

Thanks @uweigand - once this patch goes in bugpoint should be less likely to reduce icmp arguments.

RKSimon updated this revision to Diff 191085.Mar 18 2019, 7:30 AM

rebase - thanks @uweigand

lebedev.ri added inline comments.
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686

Hmm, but -instsimplify says it's 0 https://godbolt.org/z/1dpyQG
Either answer is correct?

RKSimon marked an inline comment as done.Mar 18 2019, 8:01 AM
RKSimon added inline comments.
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686

Interesting (please can you raise a bug?) - the patch isn't actually changing the result in DAG, its just managing to constant fold.

spatel added inline comments.Mar 18 2019, 8:09 AM
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686

Probably not necessary? The urem invokes UB:
https://llvm.org/docs/LangRef.html#urem-instruction

So it's not that either answer is correct...*any* answer is correct. :)

lebedev.ri added inline comments.Mar 18 2019, 8:10 AM
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686
spatel added inline comments.Mar 18 2019, 12:35 PM
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686

So there are 2 questions of UB/undef/poison:

  1. The urem causes immediate UB (same as div-by-0), so anything goes in this example.
  2. Independent of that, what is the output of an icmp with undef operand? PR41125 shows that we have potentially 2 different answers in IR, so we need to decide which behavior is correct/optimal to mimic here in the DAG.
RKSimon marked an inline comment as not done.Mar 18 2019, 2:32 PM
RKSimon added inline comments.
test/CodeGen/X86/urem-seteq-vec-nonsplat.ll
686

What I think I'm going to do is add the llvm::ConstantFoldCompareInstruction logic to both SimplifyICmpInst (in a new patch) and to FoldSetCC (in this patch):

If the predicate is eq/ne and any operand is undef then return undef - else return a bool constant using isTrueWhenEqual. That way we are consistent for all cases where the other operand is constant/non-constant,

Does that make sense?

RKSimon updated this revision to Diff 191281.Mar 19 2019, 6:27 AM

Updated undef behaviour to match llvm::ConstantFoldCompareInstruction (see D59541 as well).

RKSimon marked an inline comment as done.Mar 21 2019, 9:38 AM
RKSimon added inline comments.
test/CodeGen/SPARC/missinglabel.ll
7–23

@dcederman Are you OK with this change please?

The x86 regression test changes to preserve behavior look good. Pre-commit those, so we're just left actual diffs here?

spatel accepted this revision.Mar 25 2019, 10:19 AM

LGTM

This revision is now accepted and ready to land.Mar 25 2019, 10:19 AM
This revision was automatically updated to reflect the committed changes.