通过命令 go tool compile -G=3 -m -json "0,file:///tmp/main.json" main.go 进行编译,可以看到如下输出:
main.go:3:6: can inline C
main.go:8:6: can inline D
main.go:10:3: inlining call to C
main.go:13:6: can inline main
main.go:14:3: inlining call to C
main.go:14:3: inlining call to D
main.go:3:6: can inline C with cost 61 as: func() { println(string(“C”)); D() }
main.go:8:6: can inline D with cost 65 as: func() { println(string(“D”)); C() }
main.go:10:3: inlining call to C func() { println(string(“C”)); D() }
main.go:13:6: can inline main with cost 63 as: func() { C() }
main.go:14:3: inlining call to C func() { println(string(“C”)); D() }
main.go:14:3: inlining call to D func() { println(string(“D”)); C() }
main.go:14:3: cannot inline C into main: repeated recursive cycle
其中 -m -m 与 -m=2 效果一样,当 -m=4 时,日志还会打印出内联前后的 IR Tree 结构,读者可以自己尝试。