nit: restrict some module visibility
[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 private import allocate_iregister_slots
28 private import inline_methods
29
30 # Global Analysis types
31 private import instantiated_type_analysis
32 private import reachable_method_analysis
33 private import reachable_as_init
34 private import reachable_from_init_method_analysis
35
36 # Global Analysis implementation
37 private import cha_analysis
38 private import rta_analysis
39 private import reachable_as_init_impl
40 private import reachable_from_init_method_analysis_impl
41
42 # Global Optimizations
43 private import dead_method_removal
44 private import inline_get_and_set
45 private import remove_out_of_init_get_test
46
47 redef class ToolContext
48 readable writable var _global_callgraph: String = "rta"
49 readable writable var _no_dead_method_removal: Bool = false
50 readable writable var _no_inline_get_set: Bool = false
51 readable writable var _no_callgraph_from_init: Bool = false
52 readable writable var _no_out_of_init_get_test_removal: Bool = false
53 end
54
55 redef class Program
56 # This method will analyse the program and store results (in global compilation only)
57 fun do_global_analysis do
58 assert tc.global
59
60 if tc.global_callgraph == "cha" then
61 var cha = new ChaBuilder(self)
62 cha.work
63 rma = cha.context
64 else if tc.global_callgraph == "rta" then
65 var rta = new RtaBuilder(self)
66 rta.work
67 rma = rta.context
68 ita = rta.context
69 end
70
71 # Ensure we have all analysis created
72 if rma == null then rma = new DefaultReachableMethodAnalysis
73 if ita == null then ita = new DefaultInstantiatedTypeAnalysis
74
75 var rai_builder = new ReachableAsInitBuilder(self)
76 rai_builder.work
77 rai = rai_builder.context
78
79 if not tc.no_callgraph_from_init then
80 var b = new RFIMABuilder(self)
81 b.work
82 rfima = b.context
83 end
84
85 if rfima == null then rfima = new DefaultReachableFromInitMethodAnalysis
86 end
87
88 # This method will optimize the program (in global compilation only)
89 # Those are done before analysis
90 fun do_global_pre_analysis_optimizations do
91 assert tc.global
92 if not tc.no_inline_get_set then inline_get_set
93 end
94
95 # This method will optimize the program (in global compilation only)
96 # Those are done after analysis
97 fun do_global_post_analysis_optimizations do
98 assert tc.global
99 if not tc.no_dead_method_removal then optimize_dead_methods
100 if not tc.no_out_of_init_get_test_removal then optimize_out_of_init_getters
101 end
102
103 fun dump_global_optimizations_information(directory_name: String) do
104 dump_out_of_init_information(directory_name)
105 dump_dead_method_optimization(directory_name)
106 dump_inline_get_set(directory_name)
107 end
108
109 # This method will create log files storing analysis information
110 fun dump_global_analysis_information(directory_name: String) do
111 dump_reachable_methods(directory_name, tc.global_callgraph)
112 dump_unreachable_methods(directory_name, tc.global_callgraph)
113 dump_instantiated_types(directory_name)
114 dump_not_instantiated_types(directory_name)
115 dump_reachable_as_init_methods(directory_name)
116 dump_reachable_methods_from_init(directory_name)
117 dump_unreachable_methods_from_init(directory_name)
118 end
119 end
120
121 redef class IRoutine
122 # Perfom all optimizations
123 fun optimize(m: MMModule)
124 do
125 inline_methods(m)
126 allocate_iregister_slots
127 end
128 end