First cut at new loop codegen.

Change-Id: Id3bdf8b7a5606e7ce5d856ef225d5ddbe59a584b
diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 0fff30e..769cf50 100755
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -1342,33 +1342,51 @@
 
 bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit /* visit */, glslang::TIntermLoop* node)
 {
-    // body emission needs to know what the for-loop terminal is when it sees a "continue"
-    loopTerminal.push(node->getTerminal());
+    auto blocks = builder.makeNewLoop();
+    if (node->testFirst() && node->getTest()) {
+        spv::Block& head = builder.makeNewBlock();
+        builder.createBranch(&head);
 
-    builder.makeNewLoop(node->testFirst());
-
-    if (node->getTest()) {
+        builder.setBuildPoint(&head);
         node->getTest()->traverse(this);
-        // the AST only contained the test computation, not the branch, we have to add it
-        spv::Id condition = builder.accessChainLoad(convertGlslangToSpvType(node->getTest()->getType()));
-        builder.createLoopTestBranch(condition);
+        spv::Id condition =
+            builder.accessChainLoad(convertGlslangToSpvType(node->getTest()->getType()));
+        builder.createLoopMerge(&blocks.merge, &blocks.continue_target, spv::LoopControlMaskNone);
+        builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
+
+        builder.setBuildPoint(&blocks.body);
+        if (node->getBody())
+            node->getBody()->traverse(this);  // continue->cont, break->exit
+        builder.createBranch(&blocks.continue_target);
+
+        builder.setBuildPoint(&blocks.continue_target);
+        if (node->getTerminal())
+            node->getTerminal()->traverse(this);
+        builder.createBranch(&head);
     } else {
-        builder.createBranchToBody();
+        builder.createBranch(&blocks.body);
+
+        builder.setBuildPoint(&blocks.body);
+        if (node->getBody())
+            node->getBody()->traverse(this);  // continue->cont, break->exit
+        builder.createBranch(&blocks.continue_target);
+
+        builder.setBuildPoint(&blocks.continue_target);
+        if (node->getTerminal())
+            node->getTerminal()->traverse(this);
+        if (node->getTest()) {
+            node->getTest()->traverse(this);
+            spv::Id condition =
+                builder.accessChainLoad(convertGlslangToSpvType(node->getTest()->getType()));
+            builder.createLoopMerge(&blocks.merge, &blocks.continue_target,
+                                    spv::LoopControlMaskNone);
+            builder.createConditionalBranch(condition, &blocks.body, &blocks.merge);
+        } else {
+            builder.createBranch(&blocks.body);
+        }
     }
 
-    if (node->getBody()) {
-        breakForLoop.push(true);
-        node->getBody()->traverse(this);
-        breakForLoop.pop();
-    }
-
-    if (loopTerminal.top())
-        loopTerminal.top()->traverse(this);
-
-    builder.closeLoop();
-
-    loopTerminal.pop();
-
+    builder.setBuildPoint(&blocks.merge);
     return false;
 }
 
diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp
index a46c924..57e27e0 100755
--- a/SPIRV/SpvBuilder.cpp
+++ b/SPIRV/SpvBuilder.cpp
@@ -1753,6 +1753,19 @@
     switchMerges.pop();
 }
 
+Block& Builder::makeNewBlock()
+{
+    Function& function = buildPoint->getParent();
+    auto block = new Block(getUniqueId(), function);
+    function.addBlock(block);
+    return *block;
+}
+
+Builder::LoopBlocks Builder::makeNewLoop()
+{
+    return {makeNewBlock(), makeNewBlock(), makeNewBlock()};
+}
+
 // Comments in header
 void Builder::makeNewLoop(bool loopTestFirst)
 {
diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h
index 5750b00..d3e7ad9 100755
--- a/SPIRV/SpvBuilder.h
+++ b/SPIRV/SpvBuilder.h
@@ -378,6 +378,13 @@
     // The loopTestFirst parameter is true when the loop test executes before
     // the body.  (It is false for do-while loops.)
     void makeNewLoop(bool loopTestFirst);
+    struct LoopBlocks {
+        Block &body, &merge, &continue_target;
+    };
+    LoopBlocks makeNewLoop();
+
+    // Create a new block in the function containing the build point.
+    Block& makeNewBlock();
 
     // Add the branch for the loop test, based on the given condition.
     // The true branch goes to the first block in the loop body, and
@@ -494,7 +501,11 @@
 
     void dump(std::vector<unsigned int>&) const;
 
-protected:
+    void createBranch(Block* block);
+    void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
+    void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control);
+
+ protected:
     Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
     Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value) const;
     Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2) const;
@@ -503,10 +514,7 @@
     void transferAccessChainSwizzle(bool dynamic);
     void simplifyAccessChainSwizzle();
     void createAndSetNoPredecessorBlock(const char*);
-    void createBranch(Block* block);
     void createSelectionMerge(Block* mergeBlock, unsigned int control);
-    void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control);
-    void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
     void dumpInstructions(std::vector<unsigned int>&, const std::vector<Instruction*>&) const;
 
     struct Loop; // Defined below.