forked from swiftlang/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeGenPrepare] Delete intrinsic call to llvm.assume to enable more …
…tailcall The attached test case is simplified from tcmalloc. Both function calls should be optimized as tailcall. But llvm can only optimize the first call. The second call can't be optimized because function dupRetToEnableTailCallOpts failed to duplicate ret into block case2. There 2 problems blocked the duplication: 1 Intrinsic call llvm.assume is not handled by dupRetToEnableTailCallOpts. 2 The control flow is more complex than expected, dupRetToEnableTailCallOpts can only duplicate ret into its predecessor, but here we have an intermediate block between call and ret. The solutions: 1 Since CodeGenPrepare is already at the end of LLVM IR phase, we can simply delete the intrinsic call to llvm.assume. 2 A general solution to the complex control flow is hard, but for this case, after exit2 is duplicated into case1, exit2 is the only successor of exit1 and exit1 is the only predecessor of exit2, so they can be combined through eliminateFallThrough. But this function is called too late, there is no more dupRetToEnableTailCallOpts after it. We can add an earlier call to eliminateFallThrough to solve it. Differential Revision: https://reviews.llvm.org/D76539
- Loading branch information
Showing
4 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
llvm/test/Transforms/CodeGenPrepare/X86/tailcall-assume-xbb.ll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
; RUN: opt -codegenprepare -S -mtriple=x86_64-linux < %s | FileCheck %s | ||
|
||
; The ret instruction can be duplicated into BB case2 even though there is an | ||
; intermediate BB exit1 and call to llvm.assume. | ||
|
||
@ptr = external global i8*, align 8 | ||
|
||
; CHECK: %ret1 = tail call i8* @qux() | ||
; CHECK-NEXT: ret i8* %ret1 | ||
|
||
; CHECK: %ret2 = tail call i8* @bar() | ||
; CHECK-NEXT: ret i8* %ret2 | ||
|
||
define i8* @foo(i64 %size, i64 %v1, i64 %v2) { | ||
entry: | ||
%cmp1 = icmp ult i64 %size, 1025 | ||
br i1 %cmp1, label %if.end, label %case1 | ||
|
||
case1: | ||
%ret1 = tail call i8* @qux() | ||
br label %exit2 | ||
|
||
if.end: | ||
%cmp2 = icmp ult i64 %v1, %v2 | ||
br i1 %cmp2, label %case3, label %case2 | ||
|
||
case2: | ||
%ret2 = tail call i8* @bar() | ||
br label %exit1 | ||
|
||
case3: | ||
%ret3 = load i8*, i8** @ptr, align 8 | ||
br label %exit1 | ||
|
||
exit1: | ||
%retval1 = phi i8* [ %ret2, %case2 ], [ %ret3, %case3 ] | ||
%cmp3 = icmp ne i8* %retval1, null | ||
tail call void @llvm.assume(i1 %cmp3) | ||
br label %exit2 | ||
|
||
exit2: | ||
%retval2 = phi i8* [ %ret1, %case1 ], [ %retval1, %exit1 ] | ||
ret i8* %retval2 | ||
} | ||
|
||
declare void @llvm.assume(i1) | ||
declare i8* @qux() | ||
declare i8* @bar() |