[PATCH] D100095: [InstCombine] Conditionally emitting nsw/nuw flags when combining two add operations

Mehrnoosh Heidarpour via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 8 03:13:55 PDT 2021


MehrHeidar created this revision.
Herald added a subscriber: hiraditya.
MehrHeidar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, the InstCombineCompare is combining two add operations into a single add operation which always has a `nsw` flag, without checking the conditions to see if this flag should be present according to the original two add operations or not. This flag can cause future passes in the pipeline to apply incorrect optimization and generate an incorrect IR.
The following figure shows the IR before, on the right, and after, on the left, this wrong optimization and is the main motivation for this patch:
F16193975: InstCombineEarlyStag.png <https://reviews.llvm.org/F16193975>
As we can see non of the add operations have the `nsw` falg and it was added incorrectly by InstCombine pass. A correct IR for this combined add should be `%2 = add i8 %m, 2`.

With the wrong IR presented in previous figure, later on the pipeline because of the misleading `nsw` the conditional branch instruction has changed to an unconditional branch and causing the InstCombine to delete the instructions inside a BasicBlock that contains load and store instructions. This can be seen in the following figure:
F16194038: InstCombineFinal.png <https://reviews.llvm.org/F16194038>

This patch will change the InstCombineCompare to emit the `nsw` or `nuw` only when these flags are allowed to be generated according to the original add operations and delete the possibility of applying wrong optimization with passes that will perform on the IR later in the pipeline.


https://reviews.llvm.org/D100095

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/icmp-emit-nsw-nuw.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100095.336042.patch
Type: text/x-patch
Size: 3211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210408/5f9d0eb2/attachment.bin>


More information about the llvm-commits mailing list