[PATCH] D132520: [X86] Promote i8/i16 CTTZ (BSF) instructions and remove speculation branch

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 23 18:31:23 PDT 2022


RKSimon created this revision.
RKSimon added reviewers: craig.topper, pengfei, pcordes.
Herald added subscribers: kosarev, mattd, gchakrabarti, pmatos, asb, luke957, asavonic, foad, StephenFan, frasercrmck, kerbowa, luismarques, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, atanasyan, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, kbarton, hiraditya, jgravelle-google, arichardson, sbc100, nhaehnle, jvesely, nemanjai, sdardis, dschuff, arsenm.
Herald added a project: All.
RKSimon requested review of this revision.
Herald added subscribers: pcwang-thead, MaskRay, aheejin, jholewinski.
Herald added a project: LLVM.

This patch adds a Type operand to the TLI isCheapToSpeculateCttz/isCheapToSpeculateCtlz callbacks, allowing targets to decide whether branches should occur on a type-by-type/legality basis.

For X86, this patch proposes to allow CTTZ speculation for i8/i16 types that will lower to promoted i32 BSF instructions by masking the operand above the msb (we already do something similar for i8/i16 TZCNT). This required a minor tweak to CTTZ lowering - if the src operand is known never zero (i.e. due to the promotion masking) we can remove the CMOV zero src handling.

Although BSF isn't very fast, most CPUs from the last 20 years don't do that bad a job with it, although there are some annoying passthrough EFLAGS dependencies. Additionally, now that we emit 'REP BSF' in most cases, we are tending towards assuming this will most likely be executed as a TZCNT instruction on any semi-modern CPU.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132520

Files:
  llvm/include/llvm/CodeGen/BasicTTIImpl.h
  llvm/include/llvm/CodeGen/TargetLowering.h
  llvm/lib/CodeGen/CodeGenPrepare.cpp
  llvm/lib/Target/AArch64/AArch64ISelLowering.h
  llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
  llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h
  llvm/lib/Target/ARM/ARMISelLowering.cpp
  llvm/lib/Target/ARM/ARMISelLowering.h
  llvm/lib/Target/Hexagon/HexagonISelLowering.h
  llvm/lib/Target/Mips/MipsISelLowering.cpp
  llvm/lib/Target/Mips/MipsISelLowering.h
  llvm/lib/Target/NVPTX/NVPTXISelLowering.h
  llvm/lib/Target/PowerPC/PPCISelLowering.h
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/lib/Target/RISCV/RISCVISelLowering.h
  llvm/lib/Target/SystemZ/SystemZISelLowering.h
  llvm/lib/Target/VE/VEISelLowering.h
  llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
  llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.h
  llvm/lib/Target/X86/X86ISelLowering.cpp
  llvm/lib/Target/X86/X86ISelLowering.h
  llvm/test/Analysis/CostModel/X86/cttz.ll
  llvm/test/CodeGen/X86/clz.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132520.455037.patch
Type: text/x-patch
Size: 25408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220824/60c696d5/attachment.bin>


More information about the llvm-commits mailing list