Hi, I have a question about ConstantRange::sub(const ConstantRange &Other) at lib/Support/ConstantRange.cpp:524. The code computes the new bounds as follows. APInt NewLower = getLower() - Other.getLower(); APInt NewUpper = getUpper() - Other.getUpper() + 1; Could someone explain this to me? I was expecting something like APInt NewLower = getLower() - Other.getUpper() + 1; APInt NewUpper = getUpper() - Other.getLower(); Thanks. - xi
Thanks, I think you've found a serious bug! Would you be willing to fix it? Please add a test to unittests/Support/ConstantRangeTest.cpp and then mail llvm-commits with the patch to fix it and add the test. On 20 June 2011 23:09, Xi Wang <xi.wang at gmail.com> wrote:> Hi, > > I have a question about ConstantRange::sub(const ConstantRange &Other) at > lib/Support/ConstantRange.cpp:524. The code computes the new bounds as > follows. > > APInt NewLower = getLower() - Other.getLower(); > APInt NewUpper = getUpper() - Other.getUpper() + 1; > > Could someone explain this to me? I was expecting something like > > APInt NewLower = getLower() - Other.getUpper() + 1; > APInt NewUpper = getUpper() - Other.getLower(); >These aren't quite right, I think it should be: NewLower = Lower - (Upper-1) NewUpper = (Upper-1) - Lower + 1 Constant ranges are stored half-open, [lower, upper) which means that the upper value is one past the end of the range. I often think of the formula as newmax = max - min --> newupper - 1 = ((getUpper() - 1) - Other.getLower(). min = lower, while max = upper - 1. Nick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110622/baa1bc98/attachment.html>
Sure. I will submit a patch. BTW, what's the difference between the bounds I was expecting APInt NewLower = getLower() - Other.getUpper() + 1; APInt NewUpper = getUpper() - Other.getLower(); and the two you mentioned NewLower = Lower - (Upper-1) NewUpper = (Upper-1) - Lower + 1 They look equivalent to me. Did I miss anything? Thanks. - xi On Jun 22, 2011, at 2:39 PM, Nick Lewycky wrote:> Thanks, I think you've found a serious bug! > > Would you be willing to fix it? Please add a test to unittests/Support/ConstantRangeTest.cpp and then mail llvm-commits with the patch to fix it and add the test. > > On 20 June 2011 23:09, Xi Wang <xi.wang at gmail.com> wrote: > Hi, > > I have a question about ConstantRange::sub(const ConstantRange &Other) at lib/Support/ConstantRange.cpp:524. The code computes the new bounds as follows. > > APInt NewLower = getLower() - Other.getLower(); > APInt NewUpper = getUpper() - Other.getUpper() + 1; > > Could someone explain this to me? I was expecting something like > > APInt NewLower = getLower() - Other.getUpper() + 1; > APInt NewUpper = getUpper() - Other.getLower(); > > These aren't quite right, I think it should be: > > NewLower = Lower - (Upper-1) > NewUpper = (Upper-1) - Lower + 1 > > Constant ranges are stored half-open, [lower, upper) which means that the upper value is one past the end of the range. I often think of the formula as newmax = max - min --> newupper - 1 = ((getUpper() - 1) - Other.getLower(). min = lower, while max = upper - 1. > > Nick