[TailCallElim] Preserve DT and PDT
Summary:
Previously, in the NewPM pipeline, TailCallElim recalculates the DomTree when it modifies any instruction in the Function.
For example,
```
CallInst *CI = dyn_cast<CallInst>(&I);
...
CI->setTailCall();
Modified = true;
...
if (!Modified || ...)
return PreservedAnalyses::all();
```
After applying this patch, the DomTree only recalculates if needed (plus an extra insertEdge() + an extra deleteEdge() call).
When optimizing SQLite with `-passes="default<O3>"` pipeline of the newPM, the number of DomTree recalculation decreases by 6.2%, the number of nodes visited by DFS decreases by 2.9%. The time used by DomTree will decrease approximately 1%~2.5% after applying the patch.
Statistics:
```
Before the patch:
23010 dom-tree-stats - Number of DomTree recalculations
489264 dom-tree-stats - Number of nodes visited by DFS -- DomTree
After the patch:
21581 dom-tree-stats - Number of DomTree recalculations
475088 dom-tree-stats - Number of nodes visited by DFS -- DomTree
```
Reviewers: kuhar, dmgreen, brzycki, grosser, davide
Reviewed By: kuhar, brzycki
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D49982
llvm-svn: 338954
2018-08-04 16:13:47 +08:00
|
|
|
; RUN: opt < %s -tailcallelim -verify-dom-info -S | grep call | count 4
|
2009-06-19 12:22:16 +08:00
|
|
|
; PR4323
|
|
|
|
|
|
|
|
; Several cases where tail call elimination should not move the load above the
|
|
|
|
; call, and thus can't eliminate the tail recursion.
|
|
|
|
|
|
|
|
|
|
|
|
@extern_weak_global = extern_weak global i32 ; <i32*> [#uses=1]
|
|
|
|
|
|
|
|
|
|
|
|
; This load can't be safely moved above the call because the load is from an
|
|
|
|
; extern_weak global and may trap, but the call may unwind before that happens.
|
|
|
|
define fastcc i32 @no_tailrecelim_1(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
|
|
|
|
entry:
|
|
|
|
%tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
|
|
|
|
br i1 %tmp2, label %if, label %else
|
|
|
|
|
|
|
|
if: ; preds = %entry
|
2011-09-03 06:41:11 +08:00
|
|
|
ret i32 37
|
2009-06-19 12:22:16 +08:00
|
|
|
|
|
|
|
else: ; preds = %entry
|
|
|
|
%tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
|
|
|
|
%tmp8 = call fastcc i32 @no_tailrecelim_1(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
|
2015-02-28 05:17:42 +08:00
|
|
|
%tmp9 = load i32, i32* @extern_weak_global ; <i32> [#uses=1]
|
2009-06-19 12:22:16 +08:00
|
|
|
%tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
|
|
|
|
ret i32 %tmp10
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
; This load can't be safely moved above the call because function may write to the pointer.
|
|
|
|
define fastcc i32 @no_tailrecelim_2(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind {
|
|
|
|
entry:
|
|
|
|
%tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
|
|
|
|
br i1 %tmp2, label %if, label %else
|
|
|
|
|
|
|
|
if: ; preds = %entry
|
2010-01-06 01:55:26 +08:00
|
|
|
store i32 1, i32* %a_arg
|
|
|
|
ret i32 0
|
2009-06-19 12:22:16 +08:00
|
|
|
|
|
|
|
else: ; preds = %entry
|
|
|
|
%tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
|
|
|
|
%tmp8 = call fastcc i32 @no_tailrecelim_2(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
|
2015-02-28 05:17:42 +08:00
|
|
|
%tmp9 = load i32, i32* %a_arg ; <i32> [#uses=1]
|
2009-06-19 12:22:16 +08:00
|
|
|
%tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
|
|
|
|
ret i32 %tmp10
|
|
|
|
}
|
|
|
|
|
|
|
|
; This load can't be safely moved above the call because that would change the
|
2011-11-27 14:54:59 +08:00
|
|
|
; order in which the load volatiles are performed.
|
2009-06-19 12:22:16 +08:00
|
|
|
define fastcc i32 @no_tailrecelim_3(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind {
|
|
|
|
entry:
|
|
|
|
%tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
|
|
|
|
br i1 %tmp2, label %if, label %else
|
|
|
|
|
|
|
|
if: ; preds = %entry
|
2010-01-06 01:55:26 +08:00
|
|
|
ret i32 0
|
2009-06-19 12:22:16 +08:00
|
|
|
|
|
|
|
else: ; preds = %entry
|
|
|
|
%tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
|
|
|
|
%tmp8 = call fastcc i32 @no_tailrecelim_3(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
|
2015-02-28 05:17:42 +08:00
|
|
|
%tmp9 = load volatile i32, i32* %a_arg ; <i32> [#uses=1]
|
2009-06-19 12:22:16 +08:00
|
|
|
%tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
|
|
|
|
ret i32 %tmp10
|
|
|
|
}
|
2016-01-17 20:35:29 +08:00
|
|
|
|
|
|
|
; This load can NOT be moved above the call because the a_arg is not
|
|
|
|
; sufficiently dereferenceable.
|
|
|
|
define fastcc i32 @no_tailrecelim_4(i32* dereferenceable(2) %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
|
|
|
|
entry:
|
|
|
|
%tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
|
|
|
|
br i1 %tmp2, label %if, label %else
|
|
|
|
|
|
|
|
if: ; preds = %entry
|
|
|
|
ret i32 0
|
|
|
|
|
|
|
|
else: ; preds = %entry
|
|
|
|
%tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
|
|
|
|
%tmp8 = call fastcc i32 @no_tailrecelim_4(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
|
|
|
|
%tmp9 = load i32, i32* %a_arg ; <i32> [#uses=1]
|
|
|
|
%tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
|
|
|
|
ret i32 %tmp10
|
|
|
|
}
|