[PATCH] D147789: [CodeGenPrepare][RISCV] Reverse transform in CGP to use zero-compare branch

Yingwei Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 8 00:11:55 PDT 2023


dtcxzyw added a comment.

In D147789#4251449 <https://reviews.llvm.org/D147789#4251449>, @nikic wrote:

> Can you base this on nowrap flags instead of re-deriving them?

We cannot deduce that Add/Sub instructions with NSW flags do not overflow after hoisting.
For example:
https://alive2.llvm.org/ce/z/GVZbvc

  define i32 @src(i32 %0) {
    %2 = add nsw i32 %0, -13
    %3 = icmp slt i32 %2, 0
    br i1 %3, label %5, label %4
  
  4:                                                
    ret i32 %2
  
  5:                                               
    ret i32 0
  }
  
  define i32 @tgt(i32 %0) {
    %2 = icmp slt i32 %0, 13
    br i1 %2, label %5, label %3
  
  3:                                               
    %4 = add nsw i32 %0, -13
    ret i32 %4
  
  5:                                                
    ret i32 0
  }

This transformation in InstCombine is safe but loses the information that `add i32 %0, -13` does not overflow before the branch.
Thus, the reverse transformation in CodeGenPrepare depending on the NSW flag will fail:
https://alive2.llvm.org/ce/z/woAW5v

Therefore the re-derivation is necessary as a "mitigation" for the loss of information.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147789



More information about the llvm-commits mailing list