This is an archive of the discontinued LLVM Phabricator instance.

[CGP] despeculate expensive cttz/ctlz intrinsics
ClosedPublic

Authored by spatel on Nov 12 2015, 4:23 PM.

Details

Summary

This is another step towards allowing SimplifyCFG to speculate harder, but then have CGP clean things up if the target doesn't like it.

Previous patches in this series:
http://reviews.llvm.org/D12882
http://reviews.llvm.org/D13297

My hope is that D13297 will catch most expensive ops, but speculation of cttz/ctlz requires special handling because of weirdness in the intrinsic definition for handling a zero input (that definition can probably be blamed on x86).

For example, if we have the usual speculated-by-select expensive op pattern like this:

%tobool = icmp eq i64 %A, 0
%0 = tail call i64 @llvm.cttz.i64(i64 %A, i1 true)   ; is_zero_undef == true
%cond = select i1 %tobool, i64 64, i64 %0
ret i64 %cond

There's an instcombine that will turn it into:

%0 = tail call i64 @llvm.cttz.i64(i64 %A, i1 false)   ; is_zero_undef == false

This CGP patch is looking for that case and despeculating it back into:

entry:
%tobool = icmp eq i64 %A, 0
br i1 %tobool, label %cond.end, label %cond.true

cond.true:
%0 = tail call i64 @llvm.cttz.i64(i64 %A, i1 true)    ; is_zero_undef == true
br label %cond.end

cond.end:
%cond = phi i64 [ %0, %cond.true ], [ 64, %entry ]
ret i64 %cond

This unfortunately may lead to poorer codegen (see the changes in the existing x86 test), but if we increase speculation in SimplifyCFG (the next step in this patch series), then we should avoid those kinds of cases in the first place.

The need for this patch was originally mentioned here:
http://reviews.llvm.org/D7506
with follow-up here:
http://reviews.llvm.org/D7554

Diff Detail

Event Timeline

spatel updated this revision to Diff 40092.Nov 12 2015, 4:23 PM
spatel retitled this revision from to [CGP] despeculate expensive cttz/ctlz intrinsics.
spatel updated this object.
spatel added reviewers: andreadb, hfinkel, jmolloy.
spatel added a subscriber: llvm-commits.
jmolloy accepted this revision.Nov 13 2015, 8:40 AM
jmolloy edited edge metadata.

Hi Sanjoy,

This looks fine to me, I think.

James

This revision is now accepted and ready to land.Nov 13 2015, 8:40 AM
This revision was automatically updated to reflect the committed changes.