Hash :
40afbe4b
Author :
Date :
2022-03-20T23:39:57
Translator: Fix SimplifyLoopConditions producing dead code This causes the tree to fail validation. Dead code could be generated if the loop body ended in a branch. In such a case, no additional instructions are added to the transformed loop body after the original body. Bug: chromium:1300782 Change-Id: I4ec46c170ab5a45fa9b9b73d4e2b62c9c4fa24a3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3539442 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Charlie Lao <cclao@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
//
// Copyright 2016 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.
//
// SimplifyLoopConditions is an AST traverser that converts loop conditions and loop expressions
// to regular statements inside the loop. This way further transformations that generate statements
// from loop conditions and loop expressions work correctly.
//
#include "compiler/translator/tree_ops/SimplifyLoopConditions.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
struct LoopInfo
{
const TVariable *conditionVariable = nullptr;
TIntermTyped *condition = nullptr;
TIntermTyped *expression = nullptr;
};
class SimplifyLoopConditionsTraverser : public TLValueTrackingTraverser
{
public:
SimplifyLoopConditionsTraverser(const IntermNodePatternMatcher *conditionsToSimplify,
TSymbolTable *symbolTable);
void traverseLoop(TIntermLoop *node) override;
bool visitUnary(Visit visit, TIntermUnary *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
bool visitTernary(Visit visit, TIntermTernary *node) override;
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitBranch(Visit visit, TIntermBranch *node) override;
bool foundLoopToChange() const { return mFoundLoopToChange; }
protected:
// Marked to true once an operation that needs to be hoisted out of a loop expression has been
// found.
bool mFoundLoopToChange;
bool mInsideLoopInitConditionOrExpression;
const IntermNodePatternMatcher *mConditionsToSimplify;
private:
LoopInfo mLoop;
};
SimplifyLoopConditionsTraverser::SimplifyLoopConditionsTraverser(
const IntermNodePatternMatcher *conditionsToSimplify,
TSymbolTable *symbolTable)
: TLValueTrackingTraverser(true, false, false, symbolTable),
mFoundLoopToChange(false),
mInsideLoopInitConditionOrExpression(false),
mConditionsToSimplify(conditionsToSimplify)
{}
// If we're inside a loop initialization, condition, or expression, we check for expressions that
// should be moved out of the loop condition or expression. If one is found, the loop is
// transformed.
// If we're not inside loop initialization, condition, or expression, we only need to traverse nodes
// that may contain loops.
bool SimplifyLoopConditionsTraverser::visitUnary(Visit visit, TIntermUnary *node)
{
if (!mInsideLoopInitConditionOrExpression)
return false;
if (mFoundLoopToChange)
return false; // Already decided to change this loop.
ASSERT(mConditionsToSimplify);
mFoundLoopToChange = mConditionsToSimplify->match(node);
return !mFoundLoopToChange;
}
bool SimplifyLoopConditionsTraverser::visitBinary(Visit visit, TIntermBinary *node)
{
if (!mInsideLoopInitConditionOrExpression)
return false;
if (mFoundLoopToChange)
return false; // Already decided to change this loop.
ASSERT(mConditionsToSimplify);
mFoundLoopToChange =
mConditionsToSimplify->match(node, getParentNode(), isLValueRequiredHere());
return !mFoundLoopToChange;
}
bool SimplifyLoopConditionsTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
if (!mInsideLoopInitConditionOrExpression)
return false;
if (mFoundLoopToChange)
return false; // Already decided to change this loop.
ASSERT(mConditionsToSimplify);
mFoundLoopToChange = mConditionsToSimplify->match(node, getParentNode());
return !mFoundLoopToChange;
}
bool SimplifyLoopConditionsTraverser::visitTernary(Visit visit, TIntermTernary *node)
{
if (!mInsideLoopInitConditionOrExpression)
return false;
if (mFoundLoopToChange)
return false; // Already decided to change this loop.
ASSERT(mConditionsToSimplify);
mFoundLoopToChange = mConditionsToSimplify->match(node);
return !mFoundLoopToChange;
}
bool SimplifyLoopConditionsTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
if (!mInsideLoopInitConditionOrExpression)
return false;
if (mFoundLoopToChange)
return false; // Already decided to change this loop.
ASSERT(mConditionsToSimplify);
mFoundLoopToChange = mConditionsToSimplify->match(node);
return !mFoundLoopToChange;
}
bool SimplifyLoopConditionsTraverser::visitBranch(Visit visit, TIntermBranch *node)
{
if (node->getFlowOp() == EOpContinue && (mLoop.condition || mLoop.expression))
{
TIntermBlock *parent = getParentNode()->getAsBlock();
ASSERT(parent);
TIntermSequence seq;
if (mLoop.expression)
{
seq.push_back(mLoop.expression->deepCopy());
}
if (mLoop.condition)
{
ASSERT(mLoop.conditionVariable);
seq.push_back(
CreateTempAssignmentNode(mLoop.conditionVariable, mLoop.condition->deepCopy()));
}
seq.push_back(node);
mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(parent, node, std::move(seq)));
}
return true;
}
TIntermBlock *CreateFromBody(TIntermLoop *node, bool *bodyEndsInBranchOut)
{
TIntermBlock *newBody = new TIntermBlock();
*bodyEndsInBranchOut = false;
TIntermBlock *nodeBody = node->getBody();
if (nodeBody != nullptr)
{
newBody->getSequence()->push_back(nodeBody);
*bodyEndsInBranchOut = EndsInBranch(nodeBody);
}
return newBody;
}
void SimplifyLoopConditionsTraverser::traverseLoop(TIntermLoop *node)
{
// Mark that we're inside a loop condition or expression, and determine if the loop needs to be
// transformed.
ScopedNodeInTraversalPath addToPath(this, node);
mInsideLoopInitConditionOrExpression = true;
mFoundLoopToChange = !mConditionsToSimplify;
if (!mFoundLoopToChange && node->getInit())
{
node->getInit()->traverse(this);
}
if (!mFoundLoopToChange && node->getCondition())
{
node->getCondition()->traverse(this);
}
if (!mFoundLoopToChange && node->getExpression())
{
node->getExpression()->traverse(this);
}
mInsideLoopInitConditionOrExpression = false;
const LoopInfo prevLoop = mLoop;
if (mFoundLoopToChange)
{
const TType *boolType = StaticType::Get<EbtBool, EbpUndefined, EvqTemporary, 1, 1>();
mLoop.conditionVariable = CreateTempVariable(mSymbolTable, boolType);
mLoop.condition = node->getCondition();
mLoop.expression = node->getExpression();
// Replace the loop condition with a boolean variable that's updated on each iteration.
TLoopType loopType = node->getType();
if (loopType == ELoopWhile)
{
ASSERT(!mLoop.expression);
if (mLoop.condition->getAsSymbolNode())
{
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else if (mLoop.condition->getAsConstantUnion())
{
// Transform:
// while (expr) { body; }
// into
// bool s0 = expr;
// while (s0) { body; }
TIntermDeclaration *tempInitDeclaration =
CreateTempInitDeclarationNode(mLoop.conditionVariable, mLoop.condition);
insertStatementInParentBlock(tempInitDeclaration);
node->setCondition(CreateTempSymbolNode(mLoop.conditionVariable));
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else
{
// Transform:
// while (expr) { body; }
// into
// bool s0 = expr;
// while (s0) { { body; } s0 = expr; }
//
// Local case statements are transformed into:
// s0 = expr; continue;
TIntermDeclaration *tempInitDeclaration =
CreateTempInitDeclarationNode(mLoop.conditionVariable, mLoop.condition);
insertStatementInParentBlock(tempInitDeclaration);
bool bodyEndsInBranch;
TIntermBlock *newBody = CreateFromBody(node, &bodyEndsInBranch);
if (!bodyEndsInBranch)
{
newBody->getSequence()->push_back(CreateTempAssignmentNode(
mLoop.conditionVariable, mLoop.condition->deepCopy()));
}
// Can't use queueReplacement to replace old body, since it may have been nullptr.
// It's safe to do the replacements in place here - the new body will still be
// traversed, but that won't create any problems.
node->setBody(newBody);
node->setCondition(CreateTempSymbolNode(mLoop.conditionVariable));
}
}
else if (loopType == ELoopDoWhile)
{
ASSERT(!mLoop.expression);
if (mLoop.condition->getAsSymbolNode())
{
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else if (mLoop.condition->getAsConstantUnion())
{
// Transform:
// do {
// body;
// } while (expr);
// into
// bool s0 = expr;
// do {
// body;
// } while (s0);
TIntermDeclaration *tempInitDeclaration =
CreateTempInitDeclarationNode(mLoop.conditionVariable, mLoop.condition);
insertStatementInParentBlock(tempInitDeclaration);
node->setCondition(CreateTempSymbolNode(mLoop.conditionVariable));
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else
{
// Transform:
// do {
// body;
// } while (expr);
// into
// bool s0;
// do {
// { body; }
// s0 = expr;
// } while (s0);
// Local case statements are transformed into:
// s0 = expr; continue;
TIntermDeclaration *tempInitDeclaration =
CreateTempDeclarationNode(mLoop.conditionVariable);
insertStatementInParentBlock(tempInitDeclaration);
bool bodyEndsInBranch;
TIntermBlock *newBody = CreateFromBody(node, &bodyEndsInBranch);
if (!bodyEndsInBranch)
{
newBody->getSequence()->push_back(
CreateTempAssignmentNode(mLoop.conditionVariable, mLoop.condition));
}
// Can't use queueReplacement to replace old body, since it may have been nullptr.
// It's safe to do the replacements in place here - the new body will still be
// traversed, but that won't create any problems.
node->setBody(newBody);
node->setCondition(CreateTempSymbolNode(mLoop.conditionVariable));
}
}
else if (loopType == ELoopFor)
{
if (!mLoop.condition)
{
mLoop.condition = CreateBoolNode(true);
}
TIntermLoop *whileLoop;
TIntermBlock *loopScope = new TIntermBlock();
TIntermSequence *loopScopeSequence = loopScope->getSequence();
// Insert "init;"
if (node->getInit())
{
loopScopeSequence->push_back(node->getInit());
}
if (mLoop.condition->getAsSymbolNode())
{
// Move the loop condition inside the loop.
// Transform:
// for (init; expr; exprB) { body; }
// into
// {
// init;
// while (expr) {
// { body; }
// exprB;
// }
// }
//
// Local case statements are transformed into:
// exprB; continue;
// Insert "{ body; }" in the while loop
bool bodyEndsInBranch;
TIntermBlock *whileLoopBody = CreateFromBody(node, &bodyEndsInBranch);
// Insert "exprB;" in the while loop
if (!bodyEndsInBranch && node->getExpression())
{
whileLoopBody->getSequence()->push_back(node->getExpression());
}
// Create "while(expr) { whileLoopBody }"
whileLoop =
new TIntermLoop(ELoopWhile, nullptr, mLoop.condition, nullptr, whileLoopBody);
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else if (mLoop.condition->getAsConstantUnion())
{
// Move the loop condition inside the loop.
// Transform:
// for (init; expr; exprB) { body; }
// into
// {
// init;
// bool s0 = expr;
// while (s0) {
// { body; }
// exprB;
// }
// }
//
// Local case statements are transformed into:
// exprB; continue;
// Insert "bool s0 = expr;"
loopScopeSequence->push_back(
CreateTempInitDeclarationNode(mLoop.conditionVariable, mLoop.condition));
// Insert "{ body; }" in the while loop
bool bodyEndsInBranch;
TIntermBlock *whileLoopBody = CreateFromBody(node, &bodyEndsInBranch);
// Insert "exprB;" in the while loop
if (!bodyEndsInBranch && node->getExpression())
{
whileLoopBody->getSequence()->push_back(node->getExpression());
}
// Create "while(s0) { whileLoopBody }"
whileLoop = new TIntermLoop(ELoopWhile, nullptr,
CreateTempSymbolNode(mLoop.conditionVariable), nullptr,
whileLoopBody);
// Mask continue statement condition variable update.
mLoop.condition = nullptr;
}
else
{
// Move the loop condition inside the loop.
// Transform:
// for (init; expr; exprB) { body; }
// into
// {
// init;
// bool s0 = expr;
// while (s0) {
// { body; }
// exprB;
// s0 = expr;
// }
// }
//
// Local case statements are transformed into:
// exprB; s0 = expr; continue;
// Insert "bool s0 = expr;"
loopScopeSequence->push_back(
CreateTempInitDeclarationNode(mLoop.conditionVariable, mLoop.condition));
// Insert "{ body; }" in the while loop
bool bodyEndsInBranch;
TIntermBlock *whileLoopBody = CreateFromBody(node, &bodyEndsInBranch);
// Insert "exprB;" in the while loop
if (!bodyEndsInBranch && node->getExpression())
{
whileLoopBody->getSequence()->push_back(node->getExpression());
}
// Insert "s0 = expr;" in the while loop
if (!bodyEndsInBranch)
{
whileLoopBody->getSequence()->push_back(CreateTempAssignmentNode(
mLoop.conditionVariable, mLoop.condition->deepCopy()));
}
// Create "while(s0) { whileLoopBody }"
whileLoop = new TIntermLoop(ELoopWhile, nullptr,
CreateTempSymbolNode(mLoop.conditionVariable), nullptr,
whileLoopBody);
}
loopScope->getSequence()->push_back(whileLoop);
queueReplacement(loopScope, OriginalNode::IS_DROPPED);
// After this the old body node will be traversed and loops inside it may be
// transformed. This is fine, since the old body node will still be in the AST after
// the transformation that's queued here, and transforming loops inside it doesn't
// need to know the exact post-transform path to it.
}
}
mFoundLoopToChange = false;
// We traverse the body of the loop even if the loop is transformed.
if (node->getBody())
node->getBody()->traverse(this);
mLoop = prevLoop;
}
} // namespace
bool SimplifyLoopConditions(TCompiler *compiler, TIntermNode *root, TSymbolTable *symbolTable)
{
SimplifyLoopConditionsTraverser traverser(nullptr, symbolTable);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
bool SimplifyLoopConditions(TCompiler *compiler,
TIntermNode *root,
unsigned int conditionsToSimplifyMask,
TSymbolTable *symbolTable)
{
IntermNodePatternMatcher conditionsToSimplify(conditionsToSimplifyMask);
SimplifyLoopConditionsTraverser traverser(&conditionsToSimplify, symbolTable);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
} // namespace sh