Only initialize through a temporary variable if the same symbol name is used in the initialization expression.
TRAC #13627
The previous patch can generate a lot of unnecessary temporary variables. By first checking whether the same symbol name is reused the clutter is reduced to an absolute minimum (typical shaders won't rely on this odd GLSL semantic behavior so the workaround is hardly ever needed).
Signed-off-by: Daniel Koch

Author:    Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/trunk@479 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/build_angle.gyp b/src/build_angle.gyp
index 812339a..8e14cb4 100644
--- a/src/build_angle.gyp
+++ b/src/build_angle.gyp
@@ -144,6 +144,8 @@
         'compiler/TranslatorHLSL.h',
         'compiler/UnfoldSelect.cpp',
         'compiler/UnfoldSelect.h',
+        'compiler/SearchSymbol.cpp',
+        'compiler/SearchSymbol.h',
       ],
     },
   ],
diff --git a/src/compiler/OutputHLSL.cpp b/src/compiler/OutputHLSL.cpp
index e306552..77ea944 100644
--- a/src/compiler/OutputHLSL.cpp
+++ b/src/compiler/OutputHLSL.cpp
@@ -9,6 +9,7 @@
 #include "compiler/debug.h"
 #include "compiler/InfoSink.h"
 #include "compiler/UnfoldSelect.h"
+#include "compiler/SearchSymbol.h"
 
 #include <stdio.h>
 #include <algorithm>
@@ -661,16 +662,29 @@
             // new variable is created before the assignment is evaluated), so we need to convert
             // this to "float t = x, x = t;".
 
-            // Type already printed
-            out << "t" + str(mUniqueIndex) + " = ";
-            node->getRight()->traverse(this);
-            out << ", ";
-            node->getLeft()->traverse(this);
-            out << " = t" + str(mUniqueIndex);
+            TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
+            TIntermTyped *expression = node->getRight();
 
-            mUniqueIndex++;
+            sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
+            expression->traverse(&searchSymbol);
+            bool sameSymbol = searchSymbol.foundMatch();
 
-            return false;
+            if (sameSymbol)
+            {
+                // Type already printed
+                out << "t" + str(mUniqueIndex) + " = ";
+                expression->traverse(this);
+                out << ", ";
+                symbolNode->traverse(this);
+                out << " = t" + str(mUniqueIndex);
+
+                mUniqueIndex++;
+                return false;
+            }
+        }
+        else if (visit == InVisit)
+        {
+            out << " = ";
         }
         break;
       case EOpAddAssign:               outputTriplet(visit, "(", " += ", ")");          break;
diff --git a/src/compiler/SearchSymbol.cpp b/src/compiler/SearchSymbol.cpp
new file mode 100644
index 0000000..9368f1a
--- /dev/null
+++ b/src/compiler/SearchSymbol.cpp
@@ -0,0 +1,38 @@
+//
+// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// SearchSymbol is an AST traverser to detect the use of a given symbol name
+//
+
+#include "compiler/SearchSymbol.h"
+
+#include "compiler/InfoSink.h"
+#include "compiler/OutputHLSL.h"
+
+namespace sh
+{
+SearchSymbol::SearchSymbol(const TString &symbol) : mSymbol(symbol)
+{
+    match = false;
+}
+
+void SearchSymbol::traverse(TIntermNode *node)
+{
+    node->traverse(this);
+}
+
+void SearchSymbol::visitSymbol(TIntermSymbol *symbolNode)
+{
+    if (symbolNode->getSymbol() == mSymbol)
+    {
+        match = true;
+    }
+}
+
+bool SearchSymbol::foundMatch() const
+{
+    return match;
+}
+}
diff --git a/src/compiler/SearchSymbol.h b/src/compiler/SearchSymbol.h
new file mode 100644
index 0000000..6bc0b90
--- /dev/null
+++ b/src/compiler/SearchSymbol.h
@@ -0,0 +1,33 @@
+//
+// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// SearchSymbol is an AST traverser to detect the use of a given symbol name
+//
+
+#ifndef COMPILER_SEARCHSYMBOL_H_
+#define COMPILER_SEARCHSYMBOL_H_
+
+#include "compiler/intermediate.h"
+#include "compiler/ParseHelper.h"
+
+namespace sh
+{
+class SearchSymbol : public TIntermTraverser
+{
+  public:
+    SearchSymbol(const TString &symbol);
+
+    void traverse(TIntermNode *node);
+    void visitSymbol(TIntermSymbol *symbolNode);
+
+    bool foundMatch() const;
+
+  protected:
+    const TString &mSymbol;
+    bool match;
+};
+}
+
+#endif   // COMPILER_SEARCHSYMBOL_H_
diff --git a/src/compiler/UnfoldSelect.cpp b/src/compiler/UnfoldSelect.cpp
index b3337fe..a36c393 100644
--- a/src/compiler/UnfoldSelect.cpp
+++ b/src/compiler/UnfoldSelect.cpp
@@ -3,6 +3,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 //
+// UnfoldSelect is an AST traverser to output the select operator ?: as if-else statements
+//
 
 #include "compiler/UnfoldSelect.h"
 
diff --git a/src/compiler/UnfoldSelect.h b/src/compiler/UnfoldSelect.h
index 68b2e8a..de296e4 100644
--- a/src/compiler/UnfoldSelect.h
+++ b/src/compiler/UnfoldSelect.h
@@ -3,6 +3,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 //
+// UnfoldSelect is an AST traverser to output the select operator ?: as if-else statements
+//
 
 #ifndef COMPILER_UNFOLDSELECT_H_
 #define COMPILER_UNFOLDSELECT_H_
diff --git a/src/compiler/translator_hlsl.vcproj b/src/compiler/translator_hlsl.vcproj
index 609b45b..7ea7472 100644
--- a/src/compiler/translator_hlsl.vcproj
+++ b/src/compiler/translator_hlsl.vcproj
@@ -160,6 +160,10 @@
 				>

 			</File>

 			<File

+				RelativePath=".\SearchSymbol.cpp"

+				>

+			</File>

+			<File

 				RelativePath=".\TranslatorHLSL.cpp"

 				>

 			</File>

@@ -178,6 +182,10 @@
 				>

 			</File>

 			<File

+				RelativePath=".\SearchSymbol.h"

+				>

+			</File>

+			<File

 				RelativePath=".\TranslatorHLSL.h"

 				>

 			</File>