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