This is an archive of the discontinued LLVM Phabricator instance.

[X86] Define __LAHF_SAHF__ if feature 'sahf' is set or 32-bit mode
ClosedPublic

Authored by MaskRay on Oct 10 2020, 5:06 PM.

Details

Summary

GCC 11 will define this macro.

In LLVM, the feature flag only applies to 64-bit mode and we always define the
macro in 32-bit mode. This is different from GCC -m32 in which -mno-sahf can
suppress the macro. The discrepancy can unlikely cause trouble.

Diff Detail

Event Timeline

MaskRay created this revision.Oct 10 2020, 5:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 10 2020, 5:06 PM
MaskRay requested review of this revision.Oct 10 2020, 5:06 PM
craig.topper added inline comments.Oct 10 2020, 5:48 PM
clang/lib/Basic/Targets/X86.cpp
564

It looks like gcc may define this always in 32-bit mode?

craig.topper added inline comments.Oct 10 2020, 5:53 PM
clang/lib/Basic/Targets/X86.cpp
564

More confusingly, they define it in 32-bit mode unless -mno-sahf is on the command line. Despite 32-bit mode always having LAHF/SAHF instructions.

MaskRay added inline comments.Oct 10 2020, 7:22 PM
clang/lib/Basic/Targets/X86.cpp
564

I think this means we fail to set sahf for all 32-bit CPUs. GCC gcc/config/i386/i386-options.c sets OPTION_MASK_ISA_SAHF on all 32-bit CPUs unless -mno-sahf.

According to https://git.noc.ruhr-uni-bochum.de/kostea7x/ghidra/-/commit/239106a356ef8555575a64cc2cfd3fd71b415406?view=parallel
some pre-2005 Intel and AMD 64-bit CPUs had problems so 'sahf' is disabled for some CPUs.

craig.topper added inline comments.Oct 10 2020, 7:49 PM
clang/lib/Basic/Targets/X86.cpp
564

The X86Subtarget and X86.td file calls "sahf" as HasLAHFSAHF64. And contains we have this predicate for codegen uses.

{code}

bool hasLAHFSAHF() const { return HasLAHFSAHF64 || !is64Bit(); }

{code}

Does -mno-sahf prevent gcc from using LAHF/SAHF in 32-bit mode?

MaskRay updated this revision to Diff 297441.EditedOct 10 2020, 10:24 PM
MaskRay edited the summary of this revision. (Show Details)

Always define __LAHF_SAHF__ in 32-bit mode

MaskRay added inline comments.Oct 10 2020, 10:38 PM
clang/lib/Basic/Targets/X86.cpp
564

From gcc/config/i386/i386-options.c sets OPTION_MASK_ISA_SAHF, I think so, but there isn't any codegen test... (like many other components of GCC)

This discrepancy is not a big problem so let's always define it for 32-bit mode for now.

This revision is now accepted and ready to land.Oct 10 2020, 11:41 PM
MaskRay retitled this revision from [X86] Define __LAHF_SAHF__ if feature 'sahf' is set to [X86] Define __LAHF_SAHF__ if feature 'sahf' is set or 32-bit mode.Oct 11 2020, 9:45 AM
This revision was landed with ongoing or failed builds.Oct 11 2020, 9:46 AM
This revision was automatically updated to reflect the committed changes.