d3b551bf1e3d0594238496af5aa257140d60f0df
[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
34 # Global Analysis implementation
35 import cha_analysis
36
37 # Global Optimizations
38 import dead_method_removal
39
40 redef class ToolContext
41 readable writable var _global_callgraph: String = "cha"
42 readable writable var _no_dead_method_removal: Bool = false
43 end
44
45 redef class Program
46 # This method will analyse the program and store results (in global compilation only)
47 fun do_global_analysis do
48 assert tc.global
49
50 if tc.global_callgraph == "cha" then
51 var cha = new ChaBuilder(self)
52 cha.work
53 rma = cha.context
54 end
55
56 # Ensure we have all analysis created
57 if rma == null then rma = new DefaultReachableMethodAnalysis
58 if ita == null then ita = new DefaultInstantiatedTypeAnalysis
59 end
60 # This method will optimize the program (in global compilation only)
61 fun do_global_optimizations do
62 assert tc.global
63 if not tc.no_dead_method_removal then optimize_dead_methods
64 end
65 end
66
67 redef class IRoutine
68 # Perfom all optimizations
69 fun optimize(m: MMModule)
70 do
71 inline_methods(m)
72 allocate_iregister_slots
73 end
74 end