This is an archive of the discontinued LLVM Phabricator instance.

[X86ISelLowering] Replace uint64_t with APInt to prevent overflow
AbandonedPublic

Authored by chenli on Dec 7 2015, 3:49 PM.

Details

Reviewers
RKSimon
Summary

This is a prerequisite patch for http://reviews.llvm.org/D14603

Diff Detail

Event Timeline

chenli updated this revision to Diff 42119.Dec 7 2015, 3:49 PM
chenli retitled this revision from to [X86ISelLowering] Replace uint64_t with APInt to prevent overflow .
chenli updated this object.
chenli added a reviewer: RKSimon.
chenli added a subscriber: llvm-commits.
RKSimon edited edge metadata.Dec 8 2015, 6:15 AM

Just to be clear - APInt operators don't prevent or assert on overflow, it just provides *_ov methods that make it easier to recognise when overflow occurs. Please can you confirm that this approach will work for D14603?

Just to be clear - APInt operators don't prevent or assert on overflow, it just provides *_ov methods that make it easier to recognise when overflow occurs. Please can you confirm that this approach will work for D14603?

No, it doesn't prevent overflow. But APInt makes it easy to add checks of MulAmt + 1 and MulAmt - 1 in D14603 as you mentioned.
What do you mean by confirming this works for D14603? I think I need to modify D14603 once this get checked in and add new test cases to show overflow doesn't happen.

Just to be clear - APInt operators don't prevent or assert on overflow, it just provides *_ov methods that make it easier to recognise when overflow occurs. Please can you confirm that this approach will work for D14603?

No, it doesn't prevent overflow. But APInt makes it easy to add checks of MulAmt + 1 and MulAmt - 1 in D14603 as you mentioned.
What do you mean by confirming this works for D14603? I think I need to modify D14603 once this get checked in and add new test cases to show overflow doesn't happen.

I just want to make sure that you've been able to use this patch locally with a suitably updated D14603 to deal with the overflow test cases that we discussed.

I applied the patch locally and it worked with 0xFFFFFFFF. However, in practice, neither 0xFFFFFFFF nor 0 will flow into this function because llvm handles them with special cases. For 0, it returns a 0 directly, and for 0xFFFFFFFF, it returns 0 - (the other operand). I've hacked to bypass the special cases in order to test the overflow case could be handled correctly by this code.

I applied the patch locally and it worked with 0xFFFFFFFF. However, in practice, neither 0xFFFFFFFF nor 0 will flow into this function because llvm handles them with special cases. For 0, it returns a 0 directly, and for 0xFFFFFFFF, it returns 0 - (the other operand). I've hacked to bypass the special cases in order to test the overflow case could be handled correctly by this code.

If that is true - might it be better just to add suitable asserts / early returns to D14603 that check for cases that would overflow. In which case, I feel rather guilty as the APInt conversion work is probably unnecessary.....

I applied the patch locally and it worked with 0xFFFFFFFF. However, in practice, neither 0xFFFFFFFF nor 0 will flow into this function because llvm handles them with special cases. For 0, it returns a 0 directly, and for 0xFFFFFFFF, it returns 0 - (the other operand). I've hacked to bypass the special cases in order to test the overflow case could be handled correctly by this code.

I applied the patch locally and it worked with 0xFFFFFFFF. However, in practice, neither 0xFFFFFFFF nor 0 will flow into this function because llvm handles them with special cases. For 0, it returns a 0 directly, and for 0xFFFFFFFF, it returns 0 - (the other operand). I've hacked to bypass the special cases in order to test the overflow case could be handled correctly by this code.

If that is true - might it be better just to add suitable asserts / early returns to D14603 that check for cases that would overflow. In which case, I feel rather guilty as the APInt conversion work is probably unnecessary.....

Sure. I will discard this patch and update D14603.

chenli abandoned this revision.Dec 10 2015, 10:55 PM