ni_nitdoc: removed abstract_compiler importation from ni_nitdoc module
[nit.git] / src / global / global.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean Privat <jean@pryen.org>
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Intermediate code global analysis and optimizations
19 package global
20
21 # Global imports
22 import icode
23 import program
24 import abstracttool
25
26 # Global Analysis types
27 private import instantiated_type_analysis
28 private import reachable_method_analysis
29 private import reachable_as_init
30 private import reachable_from_init_method_analysis
31
32 # Global Analysis implementation
33 private import cha_analysis
34 private import rta_analysis
35 private import reachable_as_init_impl
36 private import reachable_from_init_method_analysis_impl
37
38 # Global Optimizations
39 private import dead_method_removal
40 private import inline_get_and_set
41 private import remove_out_of_init_get_test
42
43 redef class ToolContext
44 readable writable var _global_callgraph: String = "rta"
45
46 readable var _opt_global: OptionBool = new OptionBool("Use global compilation", "--global")
47 readable var _opt_global_no_STF_opt: OptionBool = new OptionBool("Do not use SFT optimization", "--no-global-SFT-optimization")
48 readable var _opt_global_no_DMR_opt: OptionBool = new OptionBool("Do not use dead method removal optimization", "--no-global-DMR-optimization")
49 readable var _opt_global_no_inline_get_set: OptionBool = new OptionBool("Do not automatically inline getters/setters", "--no-global-get-set-inlining")
50 readable var _opt_global_no_out_of_init_get_test_opt: OptionBool = new OptionBool("Do not remove get tests outside object initialization", "--no-global-OOIT-optimization")
51 readable var _opt_global_no_RFIMA: OptionBool = new OptionBool("Do not use a specialized algorithm to find reachable methods from initializers", "--no-global-RFIM-analysis")
52 readable var _opt_global_callgraph: OptionEnum = new OptionEnum(["none", "cha", "rta"], "The algorithm to use to build the callgraph", 2, "--global-callgraph")
53
54 redef init
55 do
56 super
57 option_context.add_option(opt_global, opt_global_no_STF_opt, opt_global_no_DMR_opt, opt_global_callgraph, opt_global_no_inline_get_set, opt_global_no_RFIMA, opt_global_no_out_of_init_get_test_opt)
58 end
59 end
60
61 redef class AbstractCompiler
62 redef fun process_options
63 do
64 # FIXME: for some reason (a bug in the metamodel obviously) redefining process_options in ToolContext does not work: the compilation goes fine but the caal-mext-method skips it.
65 super
66 global = opt_global.value
67 use_SFT_optimization = not opt_global_no_STF_opt.value
68 global_callgraph = opt_global_callgraph.value_name
69 end
70 end
71
72 redef class Program
73 # This method will analyse the program and store results (in global compilation only)
74 fun do_global_analysis do
75 assert tc.global
76 # Pre optimizations:
77 if not tc.opt_global_no_inline_get_set.value then inline_get_set
78
79 if tc.global_callgraph == "cha" then
80 var cha = new ChaBuilder(self)
81 cha.work
82 rma = cha.context
83 else if tc.global_callgraph == "rta" then
84 var rta = new RtaBuilder(self)
85 rta.work
86 rma = rta.context
87 ita = rta.context
88 end
89
90 # Ensure we have all analysis created
91 if rma == null then rma = new DefaultReachableMethodAnalysis
92 if ita == null then ita = new DefaultInstantiatedTypeAnalysis
93
94 var rai_builder = new ReachableAsInitBuilder(self)
95 rai_builder.work
96 rai = rai_builder.context
97
98 if not tc.opt_global_no_RFIMA.value then
99 var b = new RFIMABuilder(self)
100 b.work
101 rfima = b.context
102 end
103
104 if rfima == null then rfima = new DefaultReachableFromInitMethodAnalysis
105
106 # Post optimizations
107 if not tc.opt_global_no_DMR_opt.value then optimize_dead_methods
108 if not tc.opt_global_no_out_of_init_get_test_opt.value then optimize_out_of_init_getters
109
110 # LOG
111 if tc.opt_log.value then
112 dump_global_optimizations_information(tc.log_directory)
113 dump_global_analysis_information(tc.log_directory)
114 end
115 end
116
117 fun dump_global_optimizations_information(directory_name: String) do
118 dump_out_of_init_information(directory_name)
119 dump_dead_method_optimization(directory_name)
120 dump_inline_get_set(directory_name)
121 end
122
123 # This method will create log files storing analysis information
124 fun dump_global_analysis_information(directory_name: String) do
125 dump_reachable_methods(directory_name, tc.global_callgraph)
126 dump_unreachable_methods(directory_name, tc.global_callgraph)
127 dump_instantiated_types(directory_name)
128 dump_not_instantiated_types(directory_name)
129 dump_reachable_as_init_methods(directory_name)
130 dump_reachable_methods_from_init(directory_name)
131 dump_unreachable_methods_from_init(directory_name)
132 end
133 end