[vala] codegen: Do not use temp var when accessing single-assignment locals



commit aecb457deed3e99ad3a941d247acc0e01aa7be89
Author: JÃrg Billeter <j bitron ch>
Date:   Mon Aug 1 21:14:34 2011 +0200

    codegen: Do not use temp var when accessing single-assignment locals

 codegen/valaccodememberaccessmodule.vala |   17 ++++++++++++++++-
 vala/valaflowanalyzer.vala               |    3 +++
 vala/valavariable.vala                   |    2 ++
 3 files changed, 21 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 6abce48..4e428fb 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -689,7 +689,22 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 		}
 		result.value_type.value_owned = false;
 
-		if (!(variable is Parameter && variable.name == "this") && is_lvalue_access_allowed (result.value_type)) {
+		bool use_temp = true;
+		if (!is_lvalue_access_allowed (result.value_type)) {
+			// special handling for types such as va_list
+			use_temp = false;
+		}
+		if (variable is Parameter && variable.name == "this") {
+			use_temp = false;
+		}
+		if (variable.single_assignment && !result.value_type.is_real_struct_type ()) {
+			// no need to copy values from variables that are exactly once
+			// as there is no risk of modification
+			// except for structs that are always passed by reference
+			use_temp = false;
+		}
+
+		if (use_temp) {
 			result = (GLibValue) store_temp_value (result, variable);
 		}
 
diff --git a/vala/valaflowanalyzer.vala b/vala/valaflowanalyzer.vala
index 87d6728..ada3254 100644
--- a/vala/valaflowanalyzer.vala
+++ b/vala/valaflowanalyzer.vala
@@ -514,6 +514,9 @@ public class Vala.FlowAnalyzer : CodeVisitor {
 		if (variable_stack == null) {
 			variable_stack = new ArrayList<Variable> ();
 			var_map.set (var_symbol, variable_stack);
+			var_symbol.single_assignment = true;
+		} else {
+			var_symbol.single_assignment = false;
 		}
 		Variable versioned_var;
 		if (var_symbol is LocalVariable) {
diff --git a/vala/valavariable.vala b/vala/valavariable.vala
index 9337751..e625e66 100644
--- a/vala/valavariable.vala
+++ b/vala/valavariable.vala
@@ -49,6 +49,8 @@ public class Vala.Variable : Symbol {
 		}
 	}
 
+	public bool single_assignment { get; set; }
+
 	Expression? _initializer;
 	DataType? _variable_type;
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]