analysis: inline getters/setters before analysis
authorJean-Sebastien Gelinas <calestar@gmail.com>
Mon, 23 Nov 2009 06:00:03 +0000 (01:00 -0500)
committerJean Privat <jean@pryen.org>
Mon, 11 Jan 2010 21:52:30 +0000 (16:52 -0500)
Signed-off-by: Jean-Sebastien Gelinas <calestar@gmail.com>
Signed-off-by: Jean Privat <jean@pryen.org>

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

index 6b0ec15..45af7fd 100644 (file)
@@ -39,10 +39,12 @@ import reachable_as_init_impl
 
 # Global Optimizations
 import dead_method_removal
+import inline_get_and_set
 
 redef class ToolContext
        readable writable var _global_callgraph: String = "rta"
        readable writable var _no_dead_method_removal: Bool = false
+       readable writable var _no_inline_get_set: Bool = false
 end
 
 redef class Program
@@ -73,6 +75,7 @@ redef class Program
        # Those are done before analysis
        fun do_global_pre_analysis_optimizations do
                assert tc.global
+               if not tc.no_inline_get_set then inline_get_set
        end
 
        # This method will optimize the program (in global compilation only)
diff --git a/src/analysis/inline_get_and_set.nit b/src/analysis/inline_get_and_set.nit
new file mode 100644 (file)
index 0000000..6f47b4a
--- /dev/null
@@ -0,0 +1,68 @@
+# 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.
+
+# Inline every automatic 'getters' and 'setters'
+# This optimization is done BEFORE analysis
+package inline_get_and_set
+
+import icode
+import syntax
+import program
+
+redef class Program
+       fun inline_get_set do
+               with_each_iroutines !action(i, m) do
+                       var v = new InlineGetSetVisitor(m, i)
+                       v.visit_iroutine(i)
+               end
+       end
+end
+
+private class InlineGetSetVisitor
+special ICodeVisitor
+       var _icb: ICodeBuilder
+
+       redef fun visit_icode(ic)
+       do
+               # Algo mostly from inline_methods.nit, by Jean Privat
+               if ic isa ICall then
+                       var m = ic.property
+                       var ir = m.iroutine
+                       if ir != null and m isa MMAttrImplementationMethod then
+                               var icb = _icb
+                               var seq = new ISeq
+                               var old_seq = icb.seq
+                               icb.seq = seq
+                               current_icode.insert_before(seq)
+                               var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
+                               var r = ic.result
+                               if r != null then
+                                       assert e != null
+                                       current_icode.insert_before(new IMove(r, e))
+                               end
+                               current_icode.delete
+                               icb.seq = old_seq
+                               visit_icode(seq)
+                       end
+               end
+               super
+       end
+
+       init(m: MMModule, r: IRoutine)
+       do
+               _icb = new ICodeBuilder(m, r)
+       end
+end
index 938c7bb..f671cbe 100644 (file)
@@ -32,6 +32,7 @@ special AbstractCompiler
        readable var _opt_global: OptionBool = new OptionBool("Use global compilation", "--global")
        readable var _opt_global_no_STF_opt: OptionBool = new OptionBool("Do not use SFT optimization", "--no-global-SFT-optimization")
        readable var _opt_global_no_DMR_opt: OptionBool = new OptionBool("Do not use dead method removal optimization", "--no-global-DMR-optimization")
+       readable var _opt_global_no_inline_get_set: OptionBool = new OptionBool("Do not automatically inline getters/setters", "--no-global-get-set-inlining")
        readable var _opt_global_callgraph: OptionEnum = new OptionEnum(["none", "cha", "rta"], "The algorithm to use to build the callgraph", 2, "--global-callgraph")
        readable var _opt_clibdir: OptionString = new OptionString("NIT C library directory", "--clibdir")
        readable var _opt_bindir: OptionString = new OptionString("NIT tools directory", "--bindir")
@@ -42,7 +43,7 @@ special AbstractCompiler
        init
        do
                super("nitc")
-               option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix, opt_dump, opt_global_no_STF_opt, opt_global_no_DMR_opt, opt_global_callgraph)
+               option_context.add_option(opt_output, opt_boost, opt_no_cc, opt_global, opt_clibdir, opt_bindir, opt_compdir, opt_extension_prefix, opt_dump, opt_global_no_STF_opt, opt_global_no_DMR_opt, opt_global_callgraph, opt_global_no_inline_get_set)
        end
 
        redef fun process_options
@@ -56,6 +57,7 @@ special AbstractCompiler
                global = opt_global.value
                use_SFT_optimization = not opt_global_no_STF_opt.value
                no_dead_method_removal = opt_global_no_DMR_opt.value
+               no_inline_get_set = opt_global_no_inline_get_set.value
                global_callgraph = opt_global_callgraph.value_name
                compdir = opt_compdir.value
                if compdir == null then