summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/ForLoopUnroll.cpp
blob: fdc3f44431e014dd6be835ce3056265e31225906 (plain)
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
//
// Copyright (c) 2002-2011 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.
//

#include "compiler/ForLoopUnroll.h"

namespace {

class IntegerForLoopUnrollMarker : public TIntermTraverser {
public:

    virtual bool visitLoop(Visit, TIntermLoop* node)
    {
        // This is called after ValidateLimitations pass, so all the ASSERT
        // should never fail.
        // See ValidateLimitations::validateForLoopInit().
        ASSERT(node);
        ASSERT(node->getType() == ELoopFor);
        ASSERT(node->getInit());
        TIntermAggregate* decl = node->getInit()->getAsAggregate();
        ASSERT(decl && decl->getOp() == EOpDeclaration);
        TIntermSequence& declSeq = decl->getSequence();
        ASSERT(declSeq.size() == 1);
        TIntermBinary* declInit = declSeq[0]->getAsBinaryNode();
        ASSERT(declInit && declInit->getOp() == EOpInitialize);
        ASSERT(declInit->getLeft());
        TIntermSymbol* symbol = declInit->getLeft()->getAsSymbolNode();
        ASSERT(symbol);
        TBasicType type = symbol->getBasicType();
        ASSERT(type == EbtInt || type == EbtFloat);
        if (type == EbtInt)
            node->setUnrollFlag(true);
        return true;
    }

};

}  // anonymous namepsace

void ForLoopUnroll::FillLoopIndexInfo(TIntermLoop* node, TLoopIndexInfo& info)
{
    ASSERT(node->getType() == ELoopFor);
    ASSERT(node->getUnrollFlag());

    TIntermNode* init = node->getInit();
    ASSERT(init != NULL);
    TIntermAggregate* decl = init->getAsAggregate();
    ASSERT((decl != NULL) && (decl->getOp() == EOpDeclaration));
    TIntermSequence& declSeq = decl->getSequence();
    ASSERT(declSeq.size() == 1);
    TIntermBinary* declInit = declSeq[0]->getAsBinaryNode();
    ASSERT((declInit != NULL) && (declInit->getOp() == EOpInitialize));
    TIntermSymbol* symbol = declInit->getLeft()->getAsSymbolNode();
    ASSERT(symbol != NULL);
    ASSERT(symbol->getBasicType() == EbtInt);

    info.id = symbol->getId();

    ASSERT(declInit->getRight() != NULL);
    TIntermConstantUnion* initNode = declInit->getRight()->getAsConstantUnion();
    ASSERT(initNode != NULL);

    info.initValue = evaluateIntConstant(initNode);
    info.currentValue = info.initValue;

    TIntermNode* cond = node->getCondition();
    ASSERT(cond != NULL);
    TIntermBinary* binOp = cond->getAsBinaryNode();
    ASSERT(binOp != NULL);
    ASSERT(binOp->getRight() != NULL);
    ASSERT(binOp->getRight()->getAsConstantUnion() != NULL);

    info.incrementValue = getLoopIncrement(node);
    info.stopValue = evaluateIntConstant(
        binOp->getRight()->getAsConstantUnion());
    info.op = binOp->getOp();
}

void ForLoopUnroll::Step()
{
    ASSERT(mLoopIndexStack.size() > 0);
    TLoopIndexInfo& info = mLoopIndexStack[mLoopIndexStack.size() - 1];
    info.currentValue += info.incrementValue;
}

bool ForLoopUnroll::SatisfiesLoopCondition()
{
    ASSERT(mLoopIndexStack.size() > 0);
    TLoopIndexInfo& info = mLoopIndexStack[mLoopIndexStack.size() - 1];
    // Relational operator is one of: > >= < <= == or !=.
    switch (info.op) {
      case EOpEqual:
        return (info.currentValue == info.stopValue);
      case EOpNotEqual:
        return (info.currentValue != info.stopValue);
      case EOpLessThan:
        return (info.currentValue < info.stopValue);
      case EOpGreaterThan:
        return (info.currentValue > info.stopValue);
      case EOpLessThanEqual:
        return (info.currentValue <= info.stopValue);
      case EOpGreaterThanEqual:
        return (info.currentValue >= info.stopValue);
      default:
        UNREACHABLE();
    }
    return false;
}

bool ForLoopUnroll::NeedsToReplaceSymbolWithValue(TIntermSymbol* symbol)
{
    for (TVector<TLoopIndexInfo>::iterator i = mLoopIndexStack.begin();
         i != mLoopIndexStack.end();
         ++i) {
        if (i->id == symbol->getId())
            return true;
    }
    return false;
}

int ForLoopUnroll::GetLoopIndexValue(TIntermSymbol* symbol)
{
    for (TVector<TLoopIndexInfo>::iterator i = mLoopIndexStack.begin();
         i != mLoopIndexStack.end();
         ++i) {
        if (i->id == symbol->getId())
            return i->currentValue;
    }
    UNREACHABLE();
    return false;
}

void ForLoopUnroll::Push(TLoopIndexInfo& info)
{
    mLoopIndexStack.push_back(info);
}

void ForLoopUnroll::Pop()
{
    mLoopIndexStack.pop_back();
}

// static
void ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(
    TIntermNode* root)
{
    ASSERT(root);

    IntegerForLoopUnrollMarker marker;
    root->traverse(&marker);
}

int ForLoopUnroll::getLoopIncrement(TIntermLoop* node)
{
    TIntermNode* expr = node->getExpression();
    ASSERT(expr != NULL);
    // for expression has one of the following forms:
    //     loop_index++
    //     loop_index--
    //     loop_index += constant_expression
    //     loop_index -= constant_expression
    //     ++loop_index
    //     --loop_index
    // The last two forms are not specified in the spec, but I am assuming
    // its an oversight.
    TIntermUnary* unOp = expr->getAsUnaryNode();
    TIntermBinary* binOp = unOp ? NULL : expr->getAsBinaryNode();

    TOperator op = EOpNull;
    TIntermConstantUnion* incrementNode = NULL;
    if (unOp != NULL) {
        op = unOp->getOp();
    } else if (binOp != NULL) {
        op = binOp->getOp();
        ASSERT(binOp->getRight() != NULL);
        incrementNode = binOp->getRight()->getAsConstantUnion();
        ASSERT(incrementNode != NULL);
    }

    int increment = 0;
    // The operator is one of: ++ -- += -=.
    switch (op) {
        case EOpPostIncrement:
        case EOpPreIncrement:
            ASSERT((unOp != NULL) && (binOp == NULL));
            increment = 1;
            break;
        case EOpPostDecrement:
        case EOpPreDecrement:
            ASSERT((unOp != NULL) && (binOp == NULL));
            increment = -1;
            break;
        case EOpAddAssign:
            ASSERT((unOp == NULL) && (binOp != NULL));
            increment = evaluateIntConstant(incrementNode);
            break;
        case EOpSubAssign:
            ASSERT((unOp == NULL) && (binOp != NULL));
            increment = - evaluateIntConstant(incrementNode);
            break;
        default:
            ASSERT(false);
    }

    return increment;
}

int ForLoopUnroll::evaluateIntConstant(TIntermConstantUnion* node)
{
    ASSERT((node != NULL) && (node->getUnionArrayPointer() != NULL));
    return node->getUnionArrayPointer()->getIConst();
}