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

Last change on this file since 4878 was 4878, checked in by nmedfort, 4 years ago

More work on n-ary operations.

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