[PM] Provide an initial, minimal port of the inliner to the new pass manager.
This doesn't implement *every* feature of the existing inliner, but
tries to implement the most important ones for building a functional
optimization pipeline and beginning to sort out bugs, regressions, and
other problems.
Notable, but intentional omissions:
- No alloca merging support. Why? Because it isn't clear we want to do
this at all. Active discussion and investigation is going on to remove
it, so for simplicity I omitted it.
- No support for trying to iterate on "internally" devirtualized calls.
Why? Because it adds what I suspect is inappropriate coupling for
little or no benefit. We will have an outer iteration system that
tracks devirtualization including that from function passes and
iterates already. We should improve that rather than approximate it
here.
- Optimization remarks. Why? Purely to make the patch smaller, no other
reason at all.
The last one I'll probably work on almost immediately. But I wanted to
skip it in the initial patch to try to focus the change as much as
possible as there is already a lot of code moving around and both of
these *could* be skipped without really disrupting the core logic.
A summary of the different things happening here:
1) Adding the usual new PM class and rigging.
2) Fixing minor underlying assumptions in the inline cost analysis or
inline logic that don't generally hold in the new PM world.
3) Adding the core pass logic which is in essence a loop over the calls
in the nodes in the call graph. This is a bit duplicated from the old
inliner, but only a handful of lines could realistically be shared.
(I tried at first, and it really didn't help anything.) All told,
this is only about 100 lines of code, and most of that is the
mechanics of wiring up analyses from the new PM world.
4) Updating the LazyCallGraph (in the new PM) based on the *newly
inlined* calls and references. This is very minimal because we cannot
form cycles.
5) When inlining removes the last use of a function, eagerly nuking the
body of the function so that any "one use remaining" inline cost
heuristics are immediately refined, and queuing these functions to be
completely deleted once inlining is complete and the call graph
updated to reflect that they have become dead.
6) After all the inlining for a particular function, updating the
LazyCallGraph and the CGSCC pass manager to reflect the
function-local simplifications that are done immediately and
internally by the inline utilties. These are the exact same
fundamental set of CG updates done by arbitrary function passes.
7) Adding a bunch of test cases to specifically target CGSCC and other
subtle aspects in the new PM world.
Many thanks to the careful review from Easwaran and Sanjoy and others!
Differential Revision: https://reviews.llvm.org/D24226
llvm-svn: 290161
2016-12-20 11:15:32 +08:00
|
|
|
; RUN: opt < %s -aa-pipeline=basic-aa -passes='cgscc(function-attrs,inline)' -S | FileCheck %s
|
|
|
|
; This test runs the inliner and the function attribute deduction. It ensures
|
|
|
|
; that when the inliner mutates the call graph it correctly updates the CGSCC
|
|
|
|
; iteration so that we can compute refined function attributes. In this way it
|
|
|
|
; is leveraging function attribute computation to observe correct call graph
|
|
|
|
; updates.
|
|
|
|
|
|
|
|
; Boring unknown external function call.
|
|
|
|
; CHECK: declare void @unknown()
|
|
|
|
declare void @unknown()
|
|
|
|
|
|
|
|
; Sanity check: this should get annotated as readnone.
|
|
|
|
; CHECK: Function Attrs: readnone
|
|
|
|
; CHECK-NEXT: declare void @readnone()
|
|
|
|
declare void @readnone() readnone
|
|
|
|
|
|
|
|
; The 'test1_' prefixed functions are designed to trigger forming a new direct
|
|
|
|
; call in the inlined body of the function. After that, we form a new SCC and
|
|
|
|
; using that can deduce precise function attrs.
|
|
|
|
|
|
|
|
; This function should no longer exist.
|
|
|
|
; CHECK-NOT: @test1_f()
|
|
|
|
define internal void @test1_f(void()* %p) {
|
|
|
|
entry:
|
|
|
|
call void %p()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test1_g()
|
|
|
|
define void @test1_g() noinline {
|
|
|
|
entry:
|
|
|
|
call void @test1_f(void()* @test1_h)
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test1_h()
|
|
|
|
define void @test1_h() noinline {
|
|
|
|
entry:
|
|
|
|
call void @test1_g()
|
|
|
|
call void @readnone()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
; The 'test2_' prefixed functions are designed to trigger forming a new direct
|
|
|
|
; call due to RAUW-ing the returned value of a called function into the caller.
|
|
|
|
; This too should form a new SCC which can then be reasoned about to compute
|
|
|
|
; precise function attrs.
|
|
|
|
|
|
|
|
; This function should no longer exist.
|
|
|
|
; CHECK-NOT: @test2_f()
|
|
|
|
define internal void()* @test2_f() {
|
|
|
|
entry:
|
|
|
|
ret void()* @test2_h
|
|
|
|
}
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test2_g()
|
|
|
|
define void @test2_g() noinline {
|
|
|
|
entry:
|
|
|
|
%p = call void()* @test2_f()
|
|
|
|
call void %p()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test2_h()
|
|
|
|
define void @test2_h() noinline {
|
|
|
|
entry:
|
|
|
|
call void @test2_g()
|
|
|
|
call void @readnone()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
; The 'test3_' prefixed functions are designed to inline in a way that causes
|
|
|
|
; call sites to become trivially dead during the middle of inlining callsites of
|
|
|
|
; a single function to make sure that the inliner does not get confused by this
|
|
|
|
; pattern.
|
|
|
|
|
|
|
|
; CHECK-NOT: @test3_maybe_unknown(
|
|
|
|
define internal void @test3_maybe_unknown(i1 %b) {
|
|
|
|
entry:
|
|
|
|
br i1 %b, label %then, label %exit
|
|
|
|
|
|
|
|
then:
|
|
|
|
call void @unknown()
|
|
|
|
br label %exit
|
|
|
|
|
|
|
|
exit:
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; CHECK-NOT: @test3_f(
|
|
|
|
define internal i1 @test3_f() {
|
|
|
|
entry:
|
|
|
|
ret i1 false
|
|
|
|
}
|
|
|
|
|
|
|
|
; CHECK-NOT: @test3_g(
|
|
|
|
define internal i1 @test3_g(i1 %b) {
|
|
|
|
entry:
|
|
|
|
br i1 %b, label %then1, label %if2
|
|
|
|
|
|
|
|
then1:
|
|
|
|
call void @test3_maybe_unknown(i1 true)
|
|
|
|
br label %if2
|
|
|
|
|
|
|
|
if2:
|
|
|
|
%f = call i1 @test3_f()
|
|
|
|
br i1 %f, label %then2, label %exit
|
|
|
|
|
|
|
|
then2:
|
|
|
|
call void @test3_maybe_unknown(i1 true)
|
|
|
|
br label %exit
|
|
|
|
|
|
|
|
exit:
|
|
|
|
ret i1 false
|
|
|
|
}
|
|
|
|
|
|
|
|
; FIXME: Currently the inliner doesn't successfully mark this as readnone
|
|
|
|
; because while it simplifies trivially dead CFGs when inlining callees it
|
|
|
|
; doesn't simplify the caller's trivially dead CFG and so we end with a dead
|
|
|
|
; block calling @unknown.
|
|
|
|
; CHECK-NOT: Function Attrs: readnone
|
|
|
|
; CHECK: define void @test3_h()
|
|
|
|
define void @test3_h() {
|
|
|
|
entry:
|
|
|
|
%g = call i1 @test3_g(i1 false)
|
|
|
|
br i1 %g, label %then, label %exit
|
|
|
|
|
|
|
|
then:
|
|
|
|
call void @test3_maybe_unknown(i1 true)
|
|
|
|
br label %exit
|
|
|
|
|
|
|
|
exit:
|
|
|
|
call void @test3_maybe_unknown(i1 false)
|
|
|
|
ret void
|
|
|
|
}
|
2016-12-28 11:13:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
; The 'test4_' prefixed functions are designed to trigger forming a new direct
|
|
|
|
; call in the inlined body of the function similar to 'test1_'. However, after
|
|
|
|
; that we continue to inline another edge of the graph forcing us to do a more
|
|
|
|
; interesting call graph update for the new call edge. Eventually, we still
|
|
|
|
; form a new SCC and should use that can deduce precise function attrs.
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test4_f1()
|
|
|
|
define void @test4_f1() noinline {
|
|
|
|
entry:
|
|
|
|
call void @test4_h()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; CHECK-NOT: @test4_f2
|
|
|
|
define internal void @test4_f2() {
|
|
|
|
entry:
|
|
|
|
call void @test4_f1()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; CHECK-NOT: @test4_g
|
|
|
|
define internal void @test4_g(void()* %p) {
|
|
|
|
entry:
|
|
|
|
call void %p()
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; This function should have had 'readnone' deduced for its SCC.
|
|
|
|
; CHECK: Function Attrs: noinline readnone
|
|
|
|
; CHECK-NEXT: define void @test4_h()
|
|
|
|
define void @test4_h() noinline {
|
|
|
|
entry:
|
|
|
|
call void @test4_g(void()* @test4_f2)
|
|
|
|
ret void
|
|
|
|
}
|