This is an archive of the discontinued LLVM Phabricator instance.

[X86][NFC] Minor improvement in X86InstrInfo::optimizeCompareInstr
ClosedPublic

Authored by skan on Nov 18 2022, 9:46 PM.

Details

Summary

Before this patch, the code enumerated getCondFromBranch, getCondFromSETCC and getCondFromFromCMov to get the condition code of a MachineInstr, and assigned the result to variable OldCC when MI || IsSwapped || ImmDelta != 0 was satisfiled.

After this patch, the if-else structure is eliminated by using getCondFromMI. Since OldCC is only used when MI || IsSwapped || ImmDelta != 0 is true, it is initialized with getCondFromMI directly outside the scope of if now.

Diff Detail

Event Timeline

skan created this revision.Nov 18 2022, 9:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2022, 9:46 PM
skan requested review of this revision.Nov 18 2022, 9:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2022, 9:46 PM
skan retitled this revision from [X86] Minor improvement in X86InstrInfo::optimizeCompareInstr to [X86][NFC] Minor improvement in X86InstrInfo::optimizeCompareInstr.Nov 18 2022, 10:14 PM
pengfei added inline comments.Nov 18 2022, 10:17 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
4501–4504

Maybe they are not equal? getCondFromMI checks for isJCC, isSETCC and isCMOVCC, but getCondFromBranch only checks JCC_1.

skan added inline comments.Nov 18 2022, 10:29 PM
llvm/lib/Target/X86/X86InstrInfo.cpp
4501–4504

They should be equal. There is neither JCC_2 nor JCC_4 before assembler relaxation if the MIR is not written manually . Furthermore, if someone write the the MIR with JCC_2, JCC4 manually, the optimization can apply too.

pengfei added inline comments.Nov 19 2022, 1:07 AM
llvm/lib/Target/X86/X86InstrInfo.cpp
4501–4504

OK. Is it possible we get a valid CC but MI || IsSwapped || ImmDelta != 0 is false? I'd suggest below for conservative.

X86::CondCode OldCC = X86::COND_INVALID;
if (MI || IsSwapped || ImmDelta != 0) {
  OldCC = X86::getCondFromMI(Instr);
  if (OldCC == X86::COND_INVALID)
    return false;
}
skan added inline comments.Nov 19 2022, 2:20 AM
llvm/lib/Target/X86/X86InstrInfo.cpp
4501–4504

Good question! You suggested code is absolutely right. After looking into these code, I found that OldCC is used only when ShouldUpdateCC is true and ShouldUpdateCC is set to true only when MI || IsSwapped || ImmDelta != 0 is true. So we can simply your code further, namely, my code should be correct too.

please can you add a patch summary

skan edited the summary of this revision. (Show Details)Nov 20 2022, 5:42 PM

please can you add a patch summary

Sure! Done.

pengfei accepted this revision.Nov 21 2022, 4:48 AM

LGTM.

This revision is now accepted and ready to land.Nov 21 2022, 4:48 AM
This revision was landed with ongoing or failed builds.Nov 21 2022, 5:00 AM
This revision was automatically updated to reflect the committed changes.
skan added a comment.Nov 21 2022, 5:01 AM

Thanks for the review @pengfei @RKSimon !