[PATCH] D61238: [ConstantRange] Add sdiv() support

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 28 03:41:57 PDT 2019


nikic created this revision.
nikic added a reviewer: lebedev.ri.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Add the last missing binary op to ConstantRange: sdiv. This was a real struggle, both in terms of implementation and testing.

The implementation is conceptually simple: We separate the LHS and RHS into positive and negative components and then also compute the positive and negative components of the result, taking into account that e.g. only pos/pos and neg/neg will give a positive result.

However, there's one complication: SignedMin / -1 is UB for sdiv, and we can't just ignore it, because the APInt result of SignedMin would break the sign segregation. Instead we drop SignedMin or -1 from the corresponding ranges, taking into account some edge cases with wrapped ranges.

Because of the sign segregation, the implementation ends up being nearly fully precise even for wrapped ranges (the remaining imprecision is due to ranges that are both signed and unsigned wrapping and are divided by a trivial divisor like 1). This means that the testing cannot just check the signed envelope as we usually do. Instead we collect all possible results in a bitvector and construct a better sign wrapped range (than the full envelope).


Repository:
  rL LLVM

https://reviews.llvm.org/D61238

Files:
  llvm/include/llvm/IR/ConstantRange.h
  llvm/lib/IR/ConstantRange.cpp
  llvm/unittests/IR/ConstantRangeTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61238.197015.patch
Type: text/x-patch
Size: 7560 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190428/11c51b4e/attachment.bin>


More information about the llvm-commits mailing list