optimize: add optimization basis
authorJean-Sebastien Gelinas <calestar@gmail.com>
Wed, 19 Aug 2009 18:59:07 +0000 (14:59 -0400)
committerJean Privat <jean@pryen.org>
Mon, 14 Sep 2009 18:50:33 +0000 (14:50 -0400)
Signed-off-by: Jean-Sebastien Gelinas <calestar@gmail.com>
Signed-off-by: Jean Privat <jean@pryen.org>

src/analysis/analysis.nit
src/analysis/instantiated_type_analysis.nit [new file with mode: 0644]
src/analysis/reachable_method_analysis.nit [new file with mode: 0644]
src/nitc.nit

index d04ae96..bf61eb4 100644 (file)
@@ -1,6 +1,7 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2009 Jean Privat <jean@pryen.org>
+# Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # Intermediate code analysis and optimizations
 package analysis
 
+# Global imports
 import icode
 import icode_dump
+import program
+
+# Local Analysis/Optimization
 import allocate_iregister_slots
 import inline_methods
 
+# Global Analysis types
+import instantiated_type_analysis
+import reachable_method_analysis
+
+redef class Program
+       # This method will analyse the program and store results (in global compilation only)
+       fun do_global_analysis do
+               assert tc.global
+
+               # Ensure we have all analysis created
+               if rma == null then rma = new DefaultReachableMethodAnalysis
+               if ita == null then ita = new DefaultInstantiatedTypeAnalysis
+       end
+       # This method will optimize the program (in global compilation only)
+       fun do_global_optimizations do
+               assert tc.global
+       end
+end
+
 redef class IRoutine
        # Perfom all optimizations
        fun optimize(m: MMModule)
diff --git a/src/analysis/instantiated_type_analysis.nit b/src/analysis/instantiated_type_analysis.nit
new file mode 100644 (file)
index 0000000..f3fb22e
--- /dev/null
@@ -0,0 +1,40 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Package containing all bases for instantiated type analysis
+package instantiated_type_analysis
+
+import program
+
+redef class Program
+       # This attribute is the InstantiatedTypeAnalysis results
+       readable writable var _ita: nullable InstantiatedTypeAnalysis = null
+end
+
+# Subclasses of this class would represent an analysis that produces
+# at least a way of knowing if a class is instantiated somewhere in a
+# method that is reachable from the entry point of the program
+class InstantiatedTypeAnalysis
+       fun is_class_instantiated(local_class: MMLocalClass): Bool is abstract
+end
+
+# Default behavior is to say that all types are instantiated
+class DefaultInstantiatedTypeAnalysis
+special InstantiatedTypeAnalysis
+       redef fun is_class_instantiated(local_class: MMLocalClass): Bool do return true
+
+       init do end
+end
diff --git a/src/analysis/reachable_method_analysis.nit b/src/analysis/reachable_method_analysis.nit
new file mode 100644 (file)
index 0000000..75c91ea
--- /dev/null
@@ -0,0 +1,43 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Package containing all bases for the reachable method analysis
+package reachable_method_analysis
+
+import icode
+import program
+
+redef class Program
+       # This attribute is the ReachableMethodAnalysis results 
+       readable writable var _rma: nullable ReachableMethodAnalysis = null
+end
+
+# Subclasses of this class would represent an analysis that produces
+# at least a way of knowing if a property is reachable from the entry
+# point of the program
+class ReachableMethodAnalysis
+       fun is_iroutine_reachable(ir: nullable IRoutine): Bool is abstract
+       fun is_method_reachable(method: MMMethod): Bool is abstract
+end
+
+# Default behavior is to say that all methods/iroutines are reachable
+class DefaultReachableMethodAnalysis
+special ReachableMethodAnalysis
+       redef fun is_iroutine_reachable(ir: nullable IRoutine): Bool do return true
+       redef fun is_method_reachable(method: MMMethod): Bool do return true
+
+       init do end
+end
index a199749..db466fb 100644 (file)
@@ -130,6 +130,10 @@ special AbstractCompiler
                        var p = new Program(mod, self)
                        p.compute_main_method
                        p.generate_allocation_iroutines
+                       if global then
+                               p.do_global_analysis
+                               p.do_global_optimizations
+                       end
                        p.do_table_computation
                        p.compile_prog_to_c
                end