This is an archive of the discontinued LLVM Phabricator instance.

bmiintrin.h: Allow using the tzcnt intrinsics for non-BMI targets
ClosedPublic

Authored by hans on Nov 17 2015, 8:49 AM.

Details

Summary

The tzcnt intrinsics are used non non-BMI targets by code (e.g. ffmpeg) that uses it as a potentially faster BSF.

The TZCNT instruction is special in that it's encoded in a backward-compatible way and behaves as BSF on non-BMI targets.

Diff Detail

Repository
rL LLVM

Event Timeline

hans updated this revision to Diff 40399.Nov 17 2015, 8:49 AM
hans retitled this revision from to bmiintrin.h: Allow using the tzcnt intrinsics for non-BMI targets.
hans updated this object.
hans added reviewers: thakis, echristo.
hans added a subscriber: cfe-commits.
thakis edited edge metadata.Nov 17 2015, 10:42 AM

Looks good. The reasoning is that without this, projects will put in an #if __clang__ no tzcnt path (e.g. http://ffmpeg.org/pipermail/ffmpeg-devel/2015-November/183408.html), and with this patch they won't and in a few years when people use build flags that allow BMI instructions, we'll magically start doing the fast thing.

(This is Eric's thing, but he sounded fine with this approach on IRC yesterday.)

echristo accepted this revision.Nov 17 2015, 10:44 AM
echristo edited edge metadata.

Sounds good to me. Weird, but fine :)

-eric

This revision is now accepted and ready to land.Nov 17 2015, 10:44 AM
This revision was automatically updated to reflect the committed changes.

The summary of why this is ok is slightly misleading. The backend won't try to encode the TZCNT instruction when the BMI feature is not enabled. Notice this just maps the to the generic non-x86 specific __builtin_ctz. The backend will emit a regular BSF instruction if BMI is not supported. There will be a zero check around it unless the backend can prove that the input is not 0.