[PATCH] D59916: [CorrelatedValuePropagation][InstCombine] Teach CorrelatedValuePropagation to also mark sub as no wrap and use that in InstCombine to fold icmp+sub.

Luqman Aden via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 27 22:54:29 PDT 2019


luqmana created this revision.
luqmana added reviewers: majnemer, apilipenko, sanjoy.
Herald added subscribers: llvm-commits, jdoerfert, jfb, JDevlieghere, hiraditya.
Herald added a project: LLVM.

Originally reported in Rust: https://github.com/rust-lang/rust/issues/58602

  llvm
  define i64 @test_icmp_sub_elide(i64 %offset) {
  start:
    %0 = icmp ult i64 %offset, 8
    br i1 %0, label %body, label %end
  
  body:
    %1 = sub i64 10, %offset
    %2 = icmp ult i64 %1, 3
    br i1 %2, label %dead_branch, label %end
  
  dead_branch:
    ret i64 %1
  
  end:
    ret i64 0
  }

In the above snippet, we already check that offset < 8 in the first block but can't figure out that 10-offset < 3 is redundant. This changes fixes it in 2 parts:

1. Teach CorrelatedValuePropagation to also handle sub instructions in addition to add. Relatively simple since makeGuaranteedNoWrapRegion already understood sub instructions. Only subtle change is which range is passed as "Other" to that function, since sub isn't commutative.
2. Teach InstCombine the transformation `(icmp P (sub nuw|nsw C2, Y), C) -> (icmp swap(P) Y, C2-C)`

Note that `CorrelatedValuePropagation::processAddSub` is still hidden behind a default off flag as IndVarSimplify hasn't yet been fixed to strip the added nsw/nuw flags and causes a miscompile. (PR31181)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D59916

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
  llvm/test/Transforms/CorrelatedValuePropagation/add.ll
  llvm/test/Transforms/CorrelatedValuePropagation/sub.ll
  llvm/test/Transforms/InstCombine/icmp-sub.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59916.192564.patch
Type: text/x-patch
Size: 13434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190328/2e70f25b/attachment.bin>


More information about the llvm-commits mailing list