[PATCH] D59218: [LSR] Fix signed overflow in GenerateCrossUseConstantOffsets.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 25 12:14:22 PDT 2019


efriedma added inline comments.


================
Comment at: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:4152
+        //        See PR41034.
+        Imms.lower_bound(((uint64_t)Imms.begin()->first + std::prev(Imms.end())->first) /
                          2)
----------------
fhahn wrote:
> efriedma wrote:
> > This code is trying to compute (x+y)/2.  Mathematically, this can't overflow, so this shouldn't require a bitwidth to produce a meaningful result.
> Unless I am missing something, the cast addresses a potential overflow for the x + y part of the expression. Running the attached test with opt+ubsan crashes here.
Sorry, I guess I wasn't clear.

By "mathematically, this can't overflow", I meant that if x and y are in the range [INT_MIN, INT_MAX], (x+y)/2 is also in the range [INT_MIN, INT_MAX].  If you try to compute the result by just writing "(x+y)/2" in C, sure, it'll overflow, but you can rewrite the formula some other way.  (Not sure what the most efficient way to write that in C is, off the top of my head.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59218/new/

https://reviews.llvm.org/D59218





More information about the llvm-commits mailing list