[PATCH] D14630: [CGP] despeculate expensive cttz/ctlz intrinsics

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 12 16:23:50 PST 2015


spatel created this revision.
spatel added reviewers: andreadb, hfinkel, jmolloy.
spatel added a subscriber: llvm-commits.

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

http://reviews.llvm.org/D14630

Files:
  lib/CodeGen/CodeGenPrepare.cpp
  test/CodeGen/X86/clz.ll
  test/Transforms/CodeGenPrepare/X86/cttz-ctlz.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14630.40092.patch
Type: text/x-patch
Size: 9492 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151113/2f5c5c69/attachment.bin>


More information about the llvm-commits mailing list