1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 Jean Privat <jean@pryen.org>
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # Intermediate code analysis and optimizations
26 # Local Analysis/Optimization
27 private import allocate_iregister_slots
28 private import inline_methods
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
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
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
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
56 # This method will analyse the program and store results (in global compilation only)
57 fun do_global_analysis
do
60 if tc
.global_callgraph
== "cha" then
61 var cha
= new ChaBuilder(self)
64 else if tc
.global_callgraph
== "rta" then
65 var rta
= new RtaBuilder(self)
71 # Ensure we have all analysis created
72 if rma
== null then rma
= new DefaultReachableMethodAnalysis
73 if ita
== null then ita
= new DefaultInstantiatedTypeAnalysis
75 var rai_builder
= new ReachableAsInitBuilder(self)
77 rai
= rai_builder
.context
79 if not tc
.no_callgraph_from_init
then
80 var b
= new RFIMABuilder(self)
85 if rfima
== null then rfima
= new DefaultReachableFromInitMethodAnalysis
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
92 if not tc
.no_inline_get_set
then inline_get_set
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
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
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
)
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
)
122 # Perfom all optimizations
123 fun optimize
(m
: MMModule)
126 allocate_iregister_slots