Generate correctly structured do-while loops.

The loop test is always emitted before the loop body.

For do-while loops, use a phi node to track whether we're
on the first loop iteration, and only check the loop test
on the second and subsequent iterations.

For do-while loops, the loop test branch no longer occurs
at the top of the loop, so it must get its own selection
merge instruction.

A block can't be the target of more than one merge instruction.
So when the loop test executes after the body (as in do-while in GLSL)
we need to introduce a dummy block to be the target of the selection
merge just before the loop test conditional branch.

The other arm of the branch exits the loop and hence is the
"break block" exception in the structured control flow rules.
This commit is contained in:
David Neto
2015-07-15 16:21:26 -04:00
parent 51b31b5785
commit c22f37cfb4
8 changed files with 1486 additions and 1373 deletions

View File

@@ -1736,16 +1736,29 @@ void Builder::endSwitch(std::vector<Block*>& /*segmentBlock*/)
}
// Comments in header
void Builder::makeNewLoop()
void Builder::makeNewLoop(bool loopTestFirst)
{
Loop loop = { };
loops.push({ });
Loop& loop = loops.top();
loop.function = &getBuildPoint()->getParent();
loop.header = new Block(getUniqueId(), *loop.function);
loop.merge = new Block(getUniqueId(), *loop.function);
loop.test = NULL;
loop.function = &getBuildPoint()->getParent();
loop.header = new Block(getUniqueId(), *loop.function);
loop.merge = new Block(getUniqueId(), *loop.function);
// Delaying creation of the loop body perturbs test results less,
// which makes for easier patch review.
// TODO(dneto): Create the loop body block here, instead of
// upon first use.
loop.body = 0;
loop.testFirst = loopTestFirst;
loop.isFirstIteration = 0;
loops.push(loop);
// The loop test is always emitted before the loop body.
// But if the loop test executes at the bottom of the loop, then
// execute the test only on the second and subsequent iterations.
// Remember the block that branches to the loop header. This
// is required for the test-after-body case.
Block* preheader = getBuildPoint();
// Branch into the loop
createBranch(loop.header);
@@ -1753,56 +1766,95 @@ void Builder::makeNewLoop()
// Set ourselves inside the loop
loop.function->addBlock(loop.header);
setBuildPoint(loop.header);
if (!loopTestFirst) {
// Generate code to defer the loop test until the second and
// subsequent iterations.
// A phi node determines whether we're on the first iteration.
loop.isFirstIteration = new Instruction(getUniqueId(), makeBoolType(), OpPhi);
// It's always the first iteration when coming from the preheader.
// All other branches to this loop header will need to indicate "false",
// but we don't yet know where they will come from.
loop.isFirstIteration->addIdOperand(makeBoolConstant(true));
loop.isFirstIteration->addIdOperand(preheader->getId());
getBuildPoint()->addInstruction(loop.isFirstIteration);
// Mark the end of the structured loop. This must exist in the loop header block.
createMerge(OpLoopMerge, loop.merge, LoopControlMaskNone);
// Generate code to see if this is the first iteration of the loop.
// It needs to be in its own block, since the loop merge and
// the selection merge instructions can't both be in the same
// (header) block.
Block* firstIterationCheck = new Block(getUniqueId(), *loop.function);
createBranch(firstIterationCheck);
loop.function->addBlock(firstIterationCheck);
setBuildPoint(firstIterationCheck);
loop.body = new Block(getUniqueId(), *loop.function);
// Control flow after this "if" normally reconverges at the loop body.
// However, the loop test has a "break branch" out of this selection
// construct because it can transfer control to the loop merge block.
createMerge(OpSelectionMerge, loop.body, SelectionControlMaskNone);
Block* loopTest = new Block(getUniqueId(), *loop.function);
createConditionalBranch(loop.isFirstIteration->getResultId(), loop.body, loopTest);
loop.function->addBlock(loopTest);
setBuildPoint(loopTest);
}
}
void Builder::createLoopTestBranch(Id condition)
{
Loop& loop = loops.top();
// If loop.test exists, then we've already generated the LoopMerge
// for this loop.
if (!loop.test)
createMerge(OpLoopMerge, loop.merge, LoopControlMaskNone);
// Generate the merge instruction. If the loop test executes before
// the body, then this is a loop merge. Otherwise the loop merge
// has already been generated and this is a conditional merge.
if (loop.testFirst) {
createMerge(OpLoopMerge, loop.merge, LoopControlMaskNone);
loop.body = new Block(getUniqueId(), *loop.function);
// Branching to the "body" block will keep control inside
// the loop.
createConditionalBranch(condition, loop.body, loop.merge);
loop.function->addBlock(loop.body);
setBuildPoint(loop.body);
} else {
// The branch to the loop merge block is the allowed exception
// to the structured control flow. Otherwise, control flow will
// continue to loop.body block. Since that is already the target
// of a merge instruction, and a block can't be the target of more
// than one merge instruction, we need to make an intermediate block.
Block* stayInLoopBlock = new Block(getUniqueId(), *loop.function);
createMerge(OpSelectionMerge, stayInLoopBlock, SelectionControlMaskNone);
// Branching to the "body" block will keep control inside
// the loop.
Block* body = new Block(getUniqueId(), *loop.function);
createConditionalBranch(condition, body, loop.merge);
loop.function->addBlock(body);
setBuildPoint(body);
// This is the loop test.
createConditionalBranch(condition, stayInLoopBlock, loop.merge);
// The dummy block just branches to the real loop body.
loop.function->addBlock(stayInLoopBlock);
setBuildPoint(stayInLoopBlock);
createBranchToBody();
}
}
void Builder::endLoopHeaderWithoutTest()
void Builder::createBranchToBody()
{
Loop& loop = loops.top();
assert(loop.body);
createMerge(OpLoopMerge, loop.merge, LoopControlMaskNone);
Block* body = new Block(getUniqueId(), *loop.function);
createBranch(body);
loop.function->addBlock(body);
setBuildPoint(body);
assert(!loop.test);
loop.test = new Block(getUniqueId(), *loop.function);
}
void Builder::createBranchToLoopTest()
{
Loop& loop = loops.top();
Block* testBlock = loop.test;
assert(testBlock);
createBranch(testBlock);
loop.function->addBlock(testBlock);
setBuildPoint(testBlock);
// This is a reconvergence of control flow, so no merge instruction
// is required.
createBranch(loop.body);
loop.function->addBlock(loop.body);
setBuildPoint(loop.body);
}
void Builder::createLoopContinue()
{
Loop& loop = loops.top();
if (loop.test)
createBranch(loop.test);
else
createBranch(loop.header);
createBranchToLoopHeaderFromInside(loops.top());
// Set up a block for dead code.
createAndSetNoPredecessorBlock("post-loop-continue");
}
@@ -1821,7 +1873,7 @@ void Builder::closeLoop()
Loop& loop = loops.top();
// Branch back to the top
createBranch(loop.header);
createBranchToLoopHeaderFromInside(loop);
// Add the merge block and set the build point to it
loop.function->addBlock(loop.merge);
@@ -1830,6 +1882,18 @@ void Builder::closeLoop()
loops.pop();
}
// Create a branch to the header of the given loop, from inside
// the loop body.
// Adjusts the phi node for the first-iteration value if needeed.
void Builder::createBranchToLoopHeaderFromInside(const Loop& loop)
{
createBranch(loop.header);
if (loop.isFirstIteration) {
loop.isFirstIteration->addIdOperand(makeBoolConstant(false));
loop.isFirstIteration->addIdOperand(getBuildPoint()->getId());
}
}
void Builder::clearAccessChain()
{
accessChain.base = 0;