This is an archive of the discontinued LLVM Phabricator instance.

[simplifycfg][NFC] add tests for cross block compare instructions fold and optimization.
ClosedPublic

Authored by yinyuefengyi on Nov 27 2018, 11:35 PM.

Details

Summary

e.g. for 3 consecutive nested condition blocks,

B1: if(s >= t) {
     bar1(s, t);
B2:  if( s != t ) {
      bar2(s, t);
B3:    if( s<=t ) {
      bar3(s, t);
    }
  }
}

If s >= t in B1, and s != t in B2, then we can infer s > t, so B3 can be eliminated if B3's condition is s<=t or merged to B2 if it is s > t. 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

Diff Detail

Repository
rL LLVM

Event Timeline

yinyuefengyi created this revision.Nov 27 2018, 11:35 PM
yinyuefengyi edited the summary of this revision. (Show Details)Nov 27 2018, 11:38 PM
spatel added inline comments.Nov 28 2018, 6:27 AM
llvm/test/Transforms/SimplifyCFG/branch-fold-three.ll
2 ↗(On Diff #175631)

This should only be testing with a single -simplifycfg.
If there's a cross-pass dependency that you want to confirm is resolved, then you probably want to add a test to
test/Transforms/PhaseOrdering, but ideally, we will show the key transform using just a single pass.

4 ↗(On Diff #175631)

You can remove the empty #0/#1 attribute specifiers.

use simplifycfg only to avoid pass dependency; remove symbol '#0' and '#1'.

spatel accepted this revision.Nov 28 2018, 6:00 PM

LGTM

This revision is now accepted and ready to land.Nov 28 2018, 6:00 PM
This revision was automatically updated to reflect the committed changes.