This is an archive of the discontinued LLVM Phabricator instance.

[MIPS] add overrides for isCheapToSpeculateCttz() and isCheapToSpeculateCtlz()
ClosedPublic

Authored by spatel on Nov 9 2015, 9:27 AM.

Details

Summary

MIPS is using the default TLI cost settings for count-leading/trailing-zeros. I think these are considered cheap operations (and therefore fair game for speculation) for any MIPS implementation.

The net result of allowing this speculation for the MIPS regression tests in this patch is that we get this branchless code:

ctlz:
  jr  $ra
  clz  $2, $4
cttz:
  addiu  $1, $4, -1
  not  $2, $4
  and  $1, $2, $1
  clz  $1, $1
  addiu  $2, $zero, 32
  jr  $ra
  subu  $2, $2, $1

Instead of:

ctlz:
  beqz  $4, $BB0_2
  addiu  $2, $zero, 32
  clz  $2, $4
$BB0_2:
  jr  $ra
  nop
cttz:
  beqz  $4, $BB1_2
  addiu  $2, $zero, 32
  addiu  $1, $4, -1
  not  $2, $4
  and  $1, $2, $1
  clz  $1, $1
  addiu  $2, $zero, 32
  subu  $2, $2, $1
$BB1_2:
  jr  $ra
  nop

See D14469 for the larger motivation.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 39708.Nov 9 2015, 9:27 AM
spatel retitled this revision from to [MIPS] add overrides for isCheapToSpeculateCttz() and isCheapToSpeculateCtlz().
spatel updated this object.
spatel added a reviewer: dsanders.
spatel added a subscriber: llvm-commits.

Thank you for tackling this. I have only two minor comments.

lib/Target/Mips/MipsISelLowering.h
238–244 ↗(On Diff #39708)

pre-MIPS32 ISAs don't have dedicated leading/trailing bit counting instructions. We should return true only if Subtarget.hasMips32() is true.

test/Transforms/SimplifyCFG/Mips/cttz-ctlz.ll
1 ↗(On Diff #39708)

Can you provide a full triple, eg. mips-linux-gnu?

dsanders accepted this revision.Nov 11 2015, 5:12 AM
dsanders edited edge metadata.

It will LGTM with the changes Vasileios has requested.

test/Transforms/SimplifyCFG/Mips/cttz-ctlz.ll
1 ↗(On Diff #39708)

If you don't want to specify the full triple you could use -march=mips

This revision is now accepted and ready to land.Nov 11 2015, 5:12 AM
spatel updated this revision to Diff 39933.Nov 11 2015, 8:53 AM
spatel edited edge metadata.

Thanks, Vasileios and Daniel!

Patch updated:

  1. Fixed to check that Subtarget.hasMips32().
  2. Specified triple as "mips-linux-gnu"; note: I don't think we can just specify an arch here because that won't initialize the target machine for 'opt'.

LGTM, thanks.

  1. Specified triple as "mips-linux-gnu"; note: I don't think we can just specify an arch here because that won't initialize the target machine for 'opt'.

It will but the way it does it is a bit weird in my opinion. It takes the default target triple and swaps the first component for the value of -march as long as llvm::Triple recognizes it. This can potentially lead to some odd triples like mips-pc-windows-msvc but they work in many parts of LLVM.

This revision was automatically updated to reflect the committed changes.