[llvm] [JumpThreading] Thread over BB with only an unconditional branch (PR #86312)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 23:56:07 PDT 2024


================
@@ -8,11 +8,11 @@ define i1 @if_else(i1 %c, i1 %c1) {
 ; CHECK-NEXT:    br i1 [[C]], label [[THEN:%.*]], label [[RETURN:%.*]]
 ; CHECK:       then:
 ; CHECK-NEXT:    call void @dummy()
-; CHECK-NEXT:    br i1 [[C1]], label [[RETURN]], label [[RETURN1:%.*]]
+; CHECK-NEXT:    br i1 [[C1]], label [[ELSE:%.*]], label [[RETURN]]
----------------
dtcxzyw wrote:

Could you please add some tests to demonstrate that this patch will save some instructions?

An example:
```
define i32 @test(...) {
   %tobool.not = icmp eq ptr %0, null
   br i1 %tobool.not, label %do.end, label %if.then

 if.then:                                          ; preds = %entry
   %call2 = tail call i32 %visit(ptr noundef nonnull %0, ptr noundef %arg) #6
   %tobool3.not = icmp eq i32 %call2, 0
   br i1 %tobool3.not, label %do.end, label %return

 do.end:                                           ; preds = %entry, %if.then
   br label %return

 return:                                           ; preds = %if.then, %do.end
   %retval.0 = phi i32 [ 0, %do.end ], [ %call2, %if.then ]
   ret i32 %retval.0
}
```
It will be folded into:
```
define i32 @test(...) {
   %tobool.not = icmp eq ptr %0, null
   br i1 %tobool.not, label %return, label %if.then

 if.then:                                          ; preds = %entry
   %call2 = tail call i32 %visit(ptr noundef nonnull %0, ptr noundef %arg) #6
   br label %return

 return:                                           ; preds = %if.then, %do.end
   %retval.0 = phi i32 [ 0, %entry ], [ %call2, %if.then ]
   ret i32 %retval.0
}
```

https://github.com/llvm/llvm-project/pull/86312


More information about the llvm-commits mailing list