2 // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 #include "compiler/ForLoopUnroll.h"
9 void ForLoopUnroll::FillLoopIndexInfo(TIntermLoop* node, TLoopIndexInfo& info)
11 ASSERT(node->getType() == ELoopFor);
12 ASSERT(node->getUnrollFlag());
14 TIntermNode* init = node->getInit();
16 TIntermAggregate* decl = init->getAsAggregate();
17 ASSERT((decl != NULL) && (decl->getOp() == EOpDeclaration));
18 TIntermSequence& declSeq = decl->getSequence();
19 ASSERT(declSeq.size() == 1);
20 TIntermBinary* declInit = declSeq[0]->getAsBinaryNode();
21 ASSERT((declInit != NULL) && (declInit->getOp() == EOpInitialize));
22 TIntermSymbol* symbol = declInit->getLeft()->getAsSymbolNode();
23 ASSERT(symbol != NULL);
24 ASSERT(symbol->getBasicType() == EbtInt);
26 info.id = symbol->getId();
28 ASSERT(declInit->getRight() != NULL);
29 TIntermConstantUnion* initNode = declInit->getRight()->getAsConstantUnion();
30 ASSERT(initNode != NULL);
32 info.initValue = evaluateIntConstant(initNode);
33 info.currentValue = info.initValue;
35 TIntermNode* cond = node->getCondition();
37 TIntermBinary* binOp = cond->getAsBinaryNode();
38 ASSERT(binOp != NULL);
39 ASSERT(binOp->getRight() != NULL);
40 ASSERT(binOp->getRight()->getAsConstantUnion() != NULL);
42 info.incrementValue = getLoopIncrement(node);
43 info.stopValue = evaluateIntConstant(
44 binOp->getRight()->getAsConstantUnion());
45 info.op = binOp->getOp();
48 void ForLoopUnroll::Step()
50 ASSERT(mLoopIndexStack.size() > 0);
51 TLoopIndexInfo& info = mLoopIndexStack[mLoopIndexStack.size() - 1];
52 info.currentValue += info.incrementValue;
55 bool ForLoopUnroll::SatisfiesLoopCondition()
57 ASSERT(mLoopIndexStack.size() > 0);
58 TLoopIndexInfo& info = mLoopIndexStack[mLoopIndexStack.size() - 1];
59 // Relational operator is one of: > >= < <= == or !=.
62 return (info.currentValue == info.stopValue);
64 return (info.currentValue != info.stopValue);
66 return (info.currentValue < info.stopValue);
68 return (info.currentValue > info.stopValue);
69 case EOpLessThanEqual:
70 return (info.currentValue <= info.stopValue);
71 case EOpGreaterThanEqual:
72 return (info.currentValue >= info.stopValue);
79 bool ForLoopUnroll::NeedsToReplaceSymbolWithValue(TIntermSymbol* symbol)
81 for (TVector<TLoopIndexInfo>::iterator i = mLoopIndexStack.begin();
82 i != mLoopIndexStack.end();
84 if (i->id == symbol->getId())
90 int ForLoopUnroll::GetLoopIndexValue(TIntermSymbol* symbol)
92 for (TVector<TLoopIndexInfo>::iterator i = mLoopIndexStack.begin();
93 i != mLoopIndexStack.end();
95 if (i->id == symbol->getId())
96 return i->currentValue;
102 void ForLoopUnroll::Push(TLoopIndexInfo& info)
104 mLoopIndexStack.push_back(info);
107 void ForLoopUnroll::Pop()
109 mLoopIndexStack.pop_back();
112 int ForLoopUnroll::getLoopIncrement(TIntermLoop* node)
114 TIntermNode* expr = node->getExpression();
115 ASSERT(expr != NULL);
116 // for expression has one of the following forms:
119 // loop_index += constant_expression
120 // loop_index -= constant_expression
123 // The last two forms are not specified in the spec, but I am assuming
125 TIntermUnary* unOp = expr->getAsUnaryNode();
126 TIntermBinary* binOp = unOp ? NULL : expr->getAsBinaryNode();
128 TOperator op = EOpNull;
129 TIntermConstantUnion* incrementNode = NULL;
132 } else if (binOp != NULL) {
134 ASSERT(binOp->getRight() != NULL);
135 incrementNode = binOp->getRight()->getAsConstantUnion();
136 ASSERT(incrementNode != NULL);
140 // The operator is one of: ++ -- += -=.
142 case EOpPostIncrement:
143 case EOpPreIncrement:
144 ASSERT((unOp != NULL) && (binOp == NULL));
147 case EOpPostDecrement:
148 case EOpPreDecrement:
149 ASSERT((unOp != NULL) && (binOp == NULL));
153 ASSERT((unOp == NULL) && (binOp != NULL));
154 increment = evaluateIntConstant(incrementNode);
157 ASSERT((unOp == NULL) && (binOp != NULL));
158 increment = - evaluateIntConstant(incrementNode);
167 int ForLoopUnroll::evaluateIntConstant(TIntermConstantUnion* node)
169 ASSERT((node != NULL) && (node->getUnionArrayPointer() != NULL));
170 return node->getUnionArrayPointer()->getIConst();