analysis: inline getters/setters before analysis
[nit.git] / src / analysis / analysis.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 analysis and optimizations
19 package analysis
20
21 # Global imports
22 import icode
23 import icode_dump
24 import program
25
26 # Local Analysis/Optimization
27 import allocate_iregister_slots
28 import inline_methods
29
30 # Global Analysis types
31 import instantiated_type_analysis
32 import reachable_method_analysis
33 import reachable_as_init
34
35 # Global Analysis implementation
36 import cha_analysis
37 import rta_analysis
38 import reachable_as_init_impl
39
40 # Global Optimizations
41 import dead_method_removal
42 import inline_get_and_set
43
44 redef class ToolContext
45 readable writable var _global_callgraph: String = "rta"
46 readable writable var _no_dead_method_removal: Bool = false
47 readable writable var _no_inline_get_set: Bool = false
48 end
49
50 redef class Program
51 # This method will analyse the program and store results (in global compilation only)
52 fun do_global_analysis do
53 assert tc.global
54
55 if tc.global_callgraph == "cha" then
56 var cha = new ChaBuilder(self)
57 cha.work
58 rma = cha.context
59 else if tc.global_callgraph == "rta" then
60 var rta = new RtaBuilder(self)
61 rta.work
62 rma = rta.context
63 ita = rta.context
64 end
65
66 # Ensure we have all analysis created
67 if rma == null then rma = new DefaultReachableMethodAnalysis
68 if ita == null then ita = new DefaultInstantiatedTypeAnalysis
69
70 var rai_builder = new ReachableAsInitBuilder(self)
71 rai_builder.work
72 rai = rai_builder.context
73 end
74 # This method will optimize the program (in global compilation only)
75 # Those are done before analysis
76 fun do_global_pre_analysis_optimizations do
77 assert tc.global
78 if not tc.no_inline_get_set then inline_get_set
79 end
80
81 # This method will optimize the program (in global compilation only)
82 # Those are done after analysis
83 fun do_global_post_analysis_optimizations do
84 assert tc.global
85 if not tc.no_dead_method_removal then optimize_dead_methods
86 end
87
88 # This method will create log files storing analysis information
89 fun dump_global_analysis_information(directory_name: String) do
90 dump_reachable_methods(directory_name, tc.global_callgraph)
91 dump_unreachable_methods(directory_name, tc.global_callgraph)
92 dump_instantiated_types(directory_name)
93 dump_not_instantiated_types(directory_name)
94 dump_reachable_as_init_methods(directory_name)
95 end
96 end
97
98 redef class IRoutine
99 # Perfom all optimizations
100 fun optimize(m: MMModule)
101 do
102 inline_methods(m)
103 allocate_iregister_slots
104 end
105 end