[PATCH] D54994: [simplifycfg][NFC] add tests for cross block compare instructions fold and optimization.

luo xionghu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 27 23:35:51 PST 2018


yinyuefengyi created this revision.
yinyuefengyi added reviewers: hfinkel, nemanjai, spatel, echristo, majnemer, jsji, nikic, jedilyn, hiraditya.

e.g. for 3 consecutive nested condition blocks,

  B1: if(a >= b) {
  B2:  if( a !=b ) {
  B3:    if( a<=b ) {
      }
    }
  }

If a >= b in B1 <https://reviews.llvm.org/B1>, and a != b in B2 <https://reviews.llvm.org/B2>, then we can infer a > b, so B3 <https://reviews.llvm.org/B3> can be eliminated if B3 <https://reviews.llvm.org/B3>'s condition is a<=b or merged to B2 <https://reviews.llvm.org/B2> if it is a > b. Also there other AND ICMP combinations can be folded not listed here.
LLVM IR before opt:

entry:

  %cmp = icmp sge i32 %s, %t
  br i1 %cmp, label %if.then, label %if.end6

if.then:                                          ; preds = %entry

  call void @_Z4bar1ii(i32 signext %s, i32 signext %t)
  %cmp1 = icmp ne i32 %s, %t
  
  br i1 %cmp1, label %if.then2, label %if.end6

if.then2:                                         ; preds = %if.then

  call void @_Z4bar2ii(i32 signext %s, i32 signext %t)
  %cmp3 = icmp sle i32 %s, %t
  br i1 %cmp3, label %if.then4, label %if.end6

if.then4:                                         ; preds = %if.then2

  call void @_Z4bar3ii(i32 signext %s, i32 signext %t)
  br label %if.end6

if.end6:                                          ; preds = %entry, %if.then2, %if.then4, %if.then

  ret void

the target code should be:

entry:

  %cmp = icmp sge i32 %s, %t
  br i1 %cmp, label %if.then, label %if.end6

if.then:                                          ; preds = %entry

  call void @_Z4bar1ii(i32 signext %s, i32 signext %t)
  %cmp1 = icmp ne i32 %s, %t
  br i1 %cmp1, label %if.then2, label %if.end6

if.then2:                                         ; preds = %if.then

  call void @_Z4bar2ii(i32 signext %s, i32 signext %t)
  br label %if.end6

if.end6:                                          ; preds = %if.then2, %if.then, %entry

  ret void


Repository:
  rL LLVM

https://reviews.llvm.org/D54994

Files:
  llvm/test/Transforms/SimplifyCFG/branch-fold-three.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54994.175631.patch
Type: text/x-patch
Size: 10844 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181128/f81a2775/attachment.bin>


More information about the llvm-commits mailing list