source: icGREP/icgrep-devel/icgrep/pablo/ps_if.cpp @ 5124

Last change on this file since 5124 was 5037, checked in by nmedfort, 3 years ago

UnicodeSet? bug fix and compile warning clean-up.

File size: 2.0 KB
Line 
1#include <pablo/ps_if.h>
2#include <pablo/codegenstate.h>
3#include <pablo/ps_assign.h>
4
5namespace pablo {
6
7If::DefinedAllocator If::mDefinedAllocator;
8
9If::If(PabloAST * expr, const std::initializer_list<Assign *> definedVars, PabloBlock * body)
10: Statement(ClassTypeId::If, {expr}, nullptr)
11, mBody(body)
12, mDefined(definedVars.begin(), definedVars.end(), mDefinedAllocator) {
13    // Conceptually, having a defined var X is identical to having:
14    //
15    // Assign(X, 0)
16    // If (...)
17    //    Next(Assign(X), ...)
18    //
19    // Since the implied 'Next' node is a user of the Assign node, and the Assign node is
20    // embedded into the If, the defined var is a user of the If node. However, since the
21    // Assign's value is also dependant on the 'Next' value, the If node is also a user
22    // of it.
23    mBody->setBranch(this);
24    mBody->setParent(getParent());
25    for (Assign * def : mDefined) {
26        def->addUser(this);
27        this->addUser(def);
28    }
29}
30
31If::If(PabloAST * expr, const std::vector<Assign *> & definedVars, PabloBlock * body)
32: Statement(ClassTypeId::If, {expr}, nullptr)
33, mBody(body)
34, mDefined(definedVars.begin(), definedVars.end(), mDefinedAllocator) {
35    mBody->setBranch(this);
36    mBody->setParent(getParent());
37    for (Assign * def : mDefined) {
38        def->addUser(this);
39        this->addUser(def);
40    }
41}
42
43void If::addDefined(Assign * def) {
44    if (LLVM_LIKELY(std::find(mDefined.begin(), mDefined.end(), def) == mDefined.end())) {
45        mDefined.push_back(def);
46        def->addUser(this);
47        this->addUser(def);
48    }
49}
50
51If::DefinedVars::iterator If::removeDefined(Assign * def) {
52    auto f = std::find(mDefined.begin(), mDefined.end(), def);
53    if (LLVM_LIKELY(f != mDefined.end())) {
54        def->removeUser(this);
55        this->removeUser(def);
56        return mDefined.erase(f);
57    }
58    return mDefined.end();
59}
60
61PabloBlock * If::setBody(PabloBlock * body) {
62    body->setParent(mBody->getParent());
63    std::swap(mBody, body);
64    body->setParent(nullptr);
65    return body;
66}
67
68}
Note: See TracBrowser for help on using the repository browser.