[PATCH] D119839: [SimplifyCFG] Propagating case value when turning switch range into icmp

weiwei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 15 06:14:59 PST 2022


wwei created this revision.
wwei added reviewers: nikic, lebedev.ri, hans, mkazantsev, bkramer.
wwei added a project: LLVM.
Herald added a subscriber: hiraditya.
wwei requested review of this revision.
Herald added a subscriber: llvm-commits.

When converting switch range into an icmp, the initial case value may be lost,
which may prevent instructions in successors from being optimized.
For example:

  define i64 @demo(i64 %x) {
    entry:
        switch i64 %x, label %bb3 [
            i64 0, label %bb1
            i64 1, label %bb2
        ]
    bb1:
        ret i64 0
    bb2:
        ; this will necessarily be false because %x == 1
        %0 = icmp eq i64 %x, 100
        br i1 %0, label %bb4, label %bb5
    bb3:
        unreachable
    bb4:
        ret i64 200
    bb5:
        ret i64 10
  }

Ideally, the result after SimplifyCFG should be as follows:

  define i64 @demo(i64 %x) {
    %entry:
      %switch = icmp eq i64 %x, 0
      %. = select i1 %switch, i64 0, i64 10
      ret i64 %.
  }

After applying this patch, the case value will be Propagated to the instructions in successors.
issue: SimplifyCFG Need to remove useless comparison after turning switch to icmp <https://github.com/llvm/llvm-project/issues/53853>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119839

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/switch-to-icmp-with-value-propagate.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119839.408845.patch
Type: text/x-patch
Size: 5175 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220215/62cd9d10/attachment.bin>


More information about the llvm-commits mailing list