This is an archive of the discontinued LLVM Phabricator instance.

[X86] Add peephole to convert `(Cmp Op32/Op64, Imm8)` -> `(Cmp Op16/Op8, Imm8)`
AbandonedPublic

Authored by goldstein.w.n on Apr 23 2023, 11:30 AM.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Apr 23 2023, 11:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2023, 11:30 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
goldstein.w.n requested review of this revision.Apr 23 2023, 11:30 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2023, 11:30 AM
RKSimon added inline comments.Apr 23 2023, 12:22 PM
llvm/test/CodeGen/X86/vector-reduce-and-cmp.ll
691 ↗(On Diff #516194)

This looks suspicous?

goldstein.w.n added inline comments.Apr 23 2023, 12:33 PM
llvm/test/CodeGen/X86/vector-reduce-and-cmp.ll
691 ↗(On Diff #516194)

I think its from the C.trunc(NewVT.getScalarSizeInBits()).getZExtValue().

craig.topper added inline comments.
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5688

OpVT.isScalarInteger() should always bee true for X86ISD::CMP I think. The isa<ConstantSDNode>(N1) would guarantee it too.

5690

Do we need to check overflow flag uses too?

5694

aswell -> as well

5697

Space after if

5700

space after if

craig.topper added inline comments.Apr 23 2023, 2:15 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5690

Maybe carry flag too?

craig.topper added inline comments.Apr 23 2023, 2:17 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5709

Shouldn't the type for the constant be NewVT?

craig.topper added inline comments.Apr 23 2023, 2:18 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5697

Shouldn't we check that OpVT.getScalarSizeInBits() is more than 8 bits before calling MaskedValueIsZero?

goldstein.w.n marked 2 inline comments as done.Apr 23 2023, 3:45 PM
goldstein.w.n added inline comments.
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5688

OpVT.isScalarInteger() should always bee true for X86ISD::CMP I think. The isa<ConstantSDNode>(N1) would guarantee it too.

There is CMPrr?

5690

I think yes for overflow flag, but I don't think we need for carry flag.

5709

Was doing that originally. Was running into:

MachineOperand.h:370: llvm::Register llvm::MachineOperand::getReg() const: Assertion `isReg() && "This is not a register operand!"' failed.

Phoebe suggested this.

Fix some nits + also check for OF flag usage

craig.topper added inline comments.Apr 23 2023, 5:17 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5688

I may have written that poorly. I meant that if isa<ConstantSDNode>(N1) was true, it implied N1 had scalar integer type. Thus making the type isScalarInteger redundant even if X86ISD::CMP didn't guarantee it.

5709

The types of the 2 arguments to X86ISD::CMP are supposed to be the same so there's some other issue going on.

llvm/test/CodeGen/X86/combine-movmsk.ll
44

Is this only a smaller encoding because it uses %al? For any other register I don't think it's smaller.

craig.topper added inline comments.Apr 23 2023, 5:37 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5695

I think this can be isInt<8>(C.getSExtValue())

craig.topper added inline comments.Apr 23 2023, 5:46 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5695

The alive proof is checking the constant is zero extended from 8 bits, right? But this check is a signed checked.

goldstein.w.n added inline comments.Apr 23 2023, 5:51 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5695

This is checking if C can be encoded as imm8.

llvm/test/CodeGen/X86/combine-movmsk.ll
44

Is this only a smaller encoding because it uses %al? For any other register I don't think it's smaller.

You're right:

	cmpl	$-1, %eax
    cmpw    $-1, %ax
    cmpb    $-1, %al

	cmpl	$-1, %ecx
    cmpw    $-1, %cx
    cmpb    $-1, %cl

Probably if anything we should do something to get rid of imm16 encoding. But think its fair to abandon this.

pengfei added inline comments.Apr 23 2023, 6:01 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5709

The problem was getConstant vs. getTargetConstant. I tried i8 vs. i32 before, but forgot to change it back. Seems the type here doesn't matter.

craig.topper added inline comments.Apr 23 2023, 6:10 PM
llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
5695

So it needs to be uint<8> to make the transform valid and an sint<8> to make the transform profitable. But we only checked the sint<8> condition?

goldstein.w.n abandoned this revision.Apr 23 2023, 6:33 PM

Craig pointed out, this only benefits for %eax -> %al. Given how niche it is, going to abandon.