Revert "Use the AST analyses to narrow the usage of [[loop]] and [[unroll]]"

Caused linking failures on mac because the unit test asks for TranslatorHLSL which is not compiled.

This reverts commit 3342e01f2a29343ea95961f0194f9d4f422cb840.

Change-Id: I02b2f54ca5b90611f11b7a549e75bf2e8310639d
Reviewed-on: https://chromium-review.googlesource.com/264790
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/compiler/translator/OutputHLSL.cpp b/src/compiler/translator/OutputHLSL.cpp
index f63a47a..80782e2 100644
--- a/src/compiler/translator/OutputHLSL.cpp
+++ b/src/compiler/translator/OutputHLSL.cpp
@@ -14,6 +14,7 @@
 #include "common/utilities.h"
 #include "compiler/translator/BuiltInFunctionEmulator.h"
 #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
+#include "compiler/translator/DetectDiscontinuity.h"
 #include "compiler/translator/FlagStd140Structs.h"
 #include "compiler/translator/InfoSink.h"
 #include "compiler/translator/NodeSearch.h"
@@ -108,8 +109,7 @@
       mSourcePath(sourcePath),
       mOutputType(outputType),
       mNumRenderTargets(numRenderTargets),
-      mCompileOptions(compileOptions),
-      mCurrentFunctionMetadata(nullptr)
+      mCompileOptions(compileOptions)
 {
     mUnfoldShortCircuit = new UnfoldShortCircuit(this);
     mInsideFunction = false;
@@ -130,6 +130,8 @@
 
     mUniqueIndex = 0;
 
+    mContainsLoopDiscontinuity = false;
+    mContainsAnyLoop = false;
     mOutputLod0Function = false;
     mInsideDiscontinuousLoop = false;
     mNestedLoopDepth = 0;
@@ -168,6 +170,9 @@
 
 void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
 {
+    mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot);
+    mContainsAnyLoop = containsAnyLoop(treeRoot);
+
     const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
     makeFlaggedStructMaps(flaggedStructs);
 
@@ -183,9 +188,12 @@
     builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
 
     // Now that we are done changing the AST, do the analyses need for HLSL generation
-    CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
-    ASSERT(success == CallDAG::INITDAG_SUCCESS);
-    mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
+    {
+        CallDAG dag;
+        CallDAG::InitResult success = dag.init(treeRoot, &objSink);
+        ASSERT(success == CallDAG::INITDAG_SUCCESS);
+        mASTAnalyses = CreateASTMetadataHLSL(treeRoot, dag);
+    }
 
     // Output the body and footer first to determine what has to go in the header
     mInfoSinkStack.push(&mBody);
@@ -1955,13 +1963,6 @@
       case EOpPrototype:
         if (visit == PreVisit)
         {
-            size_t index = mCallDag.findIndex(node);
-            // Skip the prototype if it is not implemented (and thus not used)
-            if (index == CallDAG::InvalidIndex)
-            {
-                return false;
-            }
-
             out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "(");
 
             TIntermSequence *arguments = node->getSequence();
@@ -1985,8 +1986,7 @@
             out << ");\n";
 
             // Also prototype the Lod0 variant if needed
-            bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
-            if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
+            if (mContainsLoopDiscontinuity && !mOutputLod0Function)
             {
                 mOutputLod0Function = true;
                 node->traverse(this);
@@ -1999,13 +1999,8 @@
       case EOpComma:            outputTriplet(visit, "(", ", ", ")");                break;
       case EOpFunction:
         {
-            ASSERT(mCurrentFunctionMetadata == nullptr);
             TString name = TFunction::unmangleName(node->getName());
 
-            size_t index = mCallDag.findIndex(node);
-            ASSERT(index != CallDAG::InvalidIndex);
-            mCurrentFunctionMetadata = &mASTMetadataList[index];
-
             out << TypeString(node->getType()) << " ";
 
             if (name == "main")
@@ -2055,15 +2050,14 @@
 
             out << "}\n";
 
-            mCurrentFunctionMetadata = nullptr;
-
-            bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
-            if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
+            if (mContainsLoopDiscontinuity && !mOutputLod0Function)
             {
-                ASSERT(name != "main");
-                mOutputLod0Function = true;
-                node->traverse(this);
-                mOutputLod0Function = false;
+                if (name != "main")
+                {
+                    mOutputLod0Function = true;
+                    node->traverse(this);
+                    mOutputLod0Function = false;
+                }
             }
 
             return false;
@@ -2072,15 +2066,11 @@
       case EOpFunctionCall:
         {
             TString name = TFunction::unmangleName(node->getName());
+            bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
             TIntermSequence *arguments = node->getSequence();
 
-            bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
             if (node->isUserDefined())
             {
-                size_t index = mCallDag.findIndex(node);
-                ASSERT(index != CallDAG::InvalidIndex);
-                lod0 &= mASTMetadataList[index].mNeedsLod0;
-
                 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
             }
             else
@@ -2311,10 +2301,11 @@
     {
         mUnfoldShortCircuit->traverse(node->getCondition());
 
-        // D3D errors when there is a gradient operation in a loop in an unflattened if.
-        if (mShaderType == GL_FRAGMENT_SHADER
-            && mCurrentFunctionMetadata->hasDiscontinuousLoop(node)
-            && mCurrentFunctionMetadata->hasGradientInCallGraph(node))
+        // D3D errors when there is a gradient operation in a loop in an unflattened if
+        // however flattening all the ifs in branch heavy shaders made D3D error too.
+        // As a temporary workaround we flatten the ifs only if there is at least a loop
+        // present somewhere in the shader.
+        if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop)
         {
             out << "FLATTEN ";
         }
@@ -2409,8 +2400,11 @@
     mNestedLoopDepth++;
 
     bool wasDiscontinuous = mInsideDiscontinuousLoop;
-    mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
-    mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) >= 0;
+
+    if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
+    {
+        mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
+    }
 
     if (mOutputType == SH_HLSL9_OUTPUT)
     {
@@ -2425,17 +2419,16 @@
 
     TInfoSinkBase &out = getInfoSink();
 
-    const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
     if (node->getType() == ELoopDoWhile)
     {
-        out << "{" << unroll << " do\n";
+        out << "{LOOP do\n";
 
         outputLineDirective(node->getLine().first_line);
         out << "{\n";
     }
     else
     {
-        out << "{" << unroll << " for(";
+        out << "{LOOP for(";
 
         if (node->getInit())
         {
@@ -2741,9 +2734,8 @@
                 }
 
                 // for(int index = initial; index < clampedLimit; index += increment)
-                const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
 
-                out << unroll << " for(";
+                out << "LOOP for(";
                 index->traverse(this);
                 out << " = ";
                 out << initial;