This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][GlobalISel] Select immediate forms of compares by wiggling constants
ClosedPublic

Authored by paquette on Apr 23 2020, 3:52 PM.

Details

Summary

Similar to code in getAArch64Cmp in AArch64ISelLowering.

When we get a compare against a constant, sometimes, that constant isn't valid for selecting an immediate form.

However, sometimes, you can get a valid constant by adding 1 or subtracting 1, and updating the condition code.

This implements the following transformations when valid:

  • x slt c => x sle c - 1
  • x sge c => x sgt c - 1
  • x ult c => x ule c - 1
  • x uge c => x ugt c - 1
  • x sle c => x slt c + 1
  • x sgt c => s sge c + 1
  • x ule c => x ult c + 1
  • x ugt c => s uge c + 1

Valid meaning the constant doesn't wrap around when we fudge it, and the result gives us a compare which can be selected into an immediate form.

This also moves getImmedFromMO higher up in the file so we can use it.

Diff Detail

Event Timeline

paquette created this revision.Apr 23 2020, 3:52 PM
paquette updated this revision to Diff 260016.Apr 24 2020, 3:55 PM

Fix a bug I missed and refactor a bit. Also add testcases that cover the bug.

Issue was that we need to make sure that anything that uses emitIntegerCompare recognizes that it may change the predicate. Otherwise, we'll end up using the old predicate in, say, a conditional branch.

I'm a bit uneasy about the API here, it seems very easy to forget to update the predicate in the caller.

What if we returned a pair<MachineInstr*, Predicate> from that function? That way callers will be forced to somewhat deal with the predicate returned in order to access the MI, making it less likely it'll be forgotten.

The other approach could be to have a separate emitIntegerCompareWithPredUpdate() call that does this behavior as a wrapper around emitIntegerCompare().

paquette updated this revision to Diff 260694.Apr 28 2020, 10:32 AM

Return a std::pair from emitIntegerCompare.

Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2020, 10:32 AM
This revision is now accepted and ready to land.Apr 28 2020, 10:42 AM
This revision was automatically updated to reflect the committed changes.