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

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

More work on n-ary operations.

File size: 2.0 KB
RevLine 
[4404]1#include <pablo/ps_if.h>
2#include <pablo/codegenstate.h>
[4416]3#include <pablo/ps_assign.h>
[4404]4
5namespace pablo {
6
[4870]7If::If(PabloAST * expr, const std::initializer_list<Assign *> definedVars, PabloBlock * body)
[4650]8: Statement(ClassTypeId::If, {expr}, nullptr)
[4404]9, mBody(body)
[4878]10, mDefined(definedVars.begin(), definedVars.end(), reinterpret_cast<DefinedAllocator &>(mAllocator)) {
[4415]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
[4419]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.
[4414]21
[4878]22    mBody->setParent(getParent());
[4876]23    for (Assign * def : mDefined) {
[4764]24        def->addUser(this);
[4856]25        this->addUser(def);
[4415]26    }
[4404]27}
28
[4870]29If::If(PabloAST * expr, const std::vector<Assign *> & definedVars, PabloBlock * body)
[4650]30: Statement(ClassTypeId::If, {expr}, nullptr)
[4511]31, mBody(body)
[4878]32, mDefined(definedVars.begin(), definedVars.end(), reinterpret_cast<DefinedAllocator &>(mAllocator)) {
33    mBody->setParent(getParent());
[4876]34    for (Assign * def : mDefined) {
[4764]35        def->addUser(this);
[4856]36        this->addUser(def);
[4511]37    }
38}
[4416]39
[4764]40void If::addDefined(Assign * def) {
[4856]41    if (LLVM_LIKELY(std::find(mDefined.begin(), mDefined.end(), def) == mDefined.end())) {
[4764]42        mDefined.push_back(def);
43        def->addUser(this);
[4856]44        this->addUser(def);
[4764]45    }
46}
[4419]47
[4868]48If::DefinedVars::iterator If::removeDefined(Assign * def) {
[4856]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);
[4868]53        return mDefined.erase(f);
[4856]54    }
[4868]55    return mDefined.end();
[4416]56}
[4856]57
[4870]58PabloBlock * If::setBody(PabloBlock * body) {
59    body->setParent(mBody->getParent());
60    std::swap(mBody, body);
61    body->setParent(nullptr);
62    return body;
[4856]63}
[4870]64
65}
Note: See TracBrowser for help on using the repository browser.