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

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

Work on coalescing algorithm + minor changes.

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.
[4896]21    mBody->setBranch(this);
[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)) {
[4896]33    mBody->setBranch(this);
[4878]34    mBody->setParent(getParent());
[4876]35    for (Assign * def : mDefined) {
[4764]36        def->addUser(this);
[4856]37        this->addUser(def);
[4511]38    }
39}
[4416]40
[4764]41void If::addDefined(Assign * def) {
[4856]42    if (LLVM_LIKELY(std::find(mDefined.begin(), mDefined.end(), def) == mDefined.end())) {
[4764]43        mDefined.push_back(def);
44        def->addUser(this);
[4856]45        this->addUser(def);
[4764]46    }
47}
[4419]48
[4868]49If::DefinedVars::iterator If::removeDefined(Assign * def) {
[4856]50    auto f = std::find(mDefined.begin(), mDefined.end(), def);
51    if (LLVM_LIKELY(f != mDefined.end())) {
52        def->removeUser(this);
53        this->removeUser(def);
[4868]54        return mDefined.erase(f);
[4856]55    }
[4868]56    return mDefined.end();
[4416]57}
[4856]58
[4870]59PabloBlock * If::setBody(PabloBlock * body) {
60    body->setParent(mBody->getParent());
61    std::swap(mBody, body);
62    body->setParent(nullptr);
63    return body;
[4856]64}
[4870]65
66}
Note: See TracBrowser for help on using the repository browser.