[PATCH] D116276: [IndVarS] Keep the nsw/nuw flags after simplifyAndExtend

guopeilin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 31 01:13:34 PST 2021


guopeilin added a comment.

In D116276#3212829 <https://reviews.llvm.org/D116276#3212829>, @nikic wrote:

> @guopeilin This doesn't really answer my question. You say that the SCEV for the wide IV at this point is `{((sext i32 %arg1 to i64) + (sext i32 %arg2 to i64)),+,(sext i32 %arg2 to i64)}<nsw><%body>`, which has the `<nsw>` flag. My baseline expectation would be that SCEVExpander will also add the nsw flag to the expanded IR (see the code around https://github.com/llvm/llvm-project/blob/015ff729cb90317e4e75cf48b1e5dd7850f0cbd0/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp#L1300-L1304). My question is why that doesn't happen because making sure SCEVExpander propagates the flag from SCEV to IR seems like the more principled way to address this problem.

Sorry that I misunderstand your question before. And I guess the code you show here may be the exact root reason for the omission of nowarp flags. I have dump the SCEV value of `OpAfterExtend ` and `ExtendAfterOp ` within the function `IsIncrementNSW`(https://github.com/llvm/llvm-project/blob/015ff729cb90317e4e75cf48b1e5dd7850f0cbd0/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp#L1142-L1144), which is like the following:

  OpAfterExtend = {((sext i32 %arg1 to i128) + (sext i32 %arg2 to i128)),+,(sext i32 %arg2 to i128)}<nw><%body>
  ExtendAfterOp = {(sext i64 ((sext i32 %arg1 to i64) + (sext i32 %arg2 to i64)) to i128),+,(sext i32 %arg2 to i128)}<nsw><%body>

They are different from each other, which makes function `IsIncrementNSW` return false, and thus SCEVExpander stops propagating the flag.
I will keep debugging it and to see the reason for the difference and try to upstream a new patch. Please wait.


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

https://reviews.llvm.org/D116276



More information about the llvm-commits mailing list