Ignore:
Timestamp:
Jan 16, 2018, 4:22:13 PM (18 months ago)
Author:
nmedfort
Message:

Added PabloBlock/Builder? createScope() methods + minor code changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/pabloAST.cpp

    r5834 r5836  
    3535 *  false may be returned i some cases when the exprs are equivalent.
    3636 ** ------------------------------------------------------------------------------------------------------------- */
    37 bool equals(const PabloAST * const expr1, const PabloAST * const expr2) {
     37bool equals(const PabloAST * const expr1, const PabloAST * const expr2) noexcept {
    3838    assert (expr1 && expr2);
    3939    if (LLVM_UNLIKELY(expr1 == expr2)) {
     
    136136
    137137/** ------------------------------------------------------------------------------------------------------------- *
    138  * @brief replaceUsesOfWith
    139  ** ------------------------------------------------------------------------------------------------------------- */
    140 void Statement::replaceUsesOfWith(PabloAST * const from, PabloAST * const to) {
    141     if (LLVM_LIKELY(from != to)) {
    142         for (unsigned i = 0; i != getNumOperands(); ++i) {
    143            if (getOperand(i) == from) {
    144                setOperand(i, to);
    145            }
    146         }
    147     }
    148 }
    149 
    150 /** ------------------------------------------------------------------------------------------------------------- *
    151  * @brief setOperand
    152  ** ------------------------------------------------------------------------------------------------------------- */
    153 void Statement::setOperand(const unsigned index, PabloAST * const value) {   
    154     assert ("Operand cannot be null!" && value);
    155     assert (index < getNumOperands());
    156     PabloAST * const prior = getOperand(index);
    157     assert ("Operand cannot be null!" && prior);
    158     if (LLVM_UNLIKELY(prior == value)) {
    159         return;
    160     }     
    161     if (LLVM_UNLIKELY(prior->getType() != value->getType())) {
    162         std::string tmp;
    163         raw_string_ostream out(tmp);
    164         out << "Type mismatch replacing operand ";
    165         prior->print(out);
    166         out << " with ";
    167         value->print(out);
    168         out << " in statement ";
    169         this->print(out);
    170         llvm::report_fatal_error(out.str());
    171     }
    172     prior->removeUser(this);
    173     mOperand[index] = value;
    174     value->addUser(this);
    175 }
    176 
    177 /** ------------------------------------------------------------------------------------------------------------- *
    178  * @brief insertBefore
    179  ** ------------------------------------------------------------------------------------------------------------- */
    180 void Statement::insertBefore(Statement * const statement) {
    181     if (LLVM_UNLIKELY(statement == this)) {
    182         return;
    183     } else if (LLVM_UNLIKELY(statement == nullptr)) {
    184         llvm::report_fatal_error("cannot insert before null statement!");
    185     } else if (LLVM_UNLIKELY(statement->mParent == nullptr)) {
    186         llvm::report_fatal_error("statement is not contained in a pablo block!");
    187     }
    188     removeFromParent();
    189     mParent = statement->mParent;
    190     if (LLVM_UNLIKELY(mParent->mFirst == statement)) {
    191         mParent->mFirst = this;
    192     }
    193     mNext = statement;
    194     mPrev = statement->mPrev;
    195     statement->mPrev = this;
    196     if (LLVM_LIKELY(mPrev != nullptr)) {
    197         mPrev->mNext = this;
    198     }
    199 }
    200 
    201 /** ------------------------------------------------------------------------------------------------------------- *
    202  * @brief insertAfter
    203  ** ------------------------------------------------------------------------------------------------------------- */
    204 void Statement::insertAfter(Statement * const statement) {
    205     if (LLVM_UNLIKELY(statement == this)) {
    206         return;
    207     } else if (LLVM_UNLIKELY(statement == nullptr)) {
    208         llvm::report_fatal_error("cannot insert after null statement!");
    209     } else if (LLVM_UNLIKELY(statement->mParent == nullptr)) {
    210         llvm::report_fatal_error("statement is not contained in a pablo block!");
    211     }
    212     removeFromParent();
    213     mParent = statement->mParent;
    214     if (LLVM_UNLIKELY(mParent->mLast == statement)) {
    215         mParent->mLast = this;
    216     }
    217     mPrev = statement;
    218     mNext = statement->mNext;
    219     statement->mNext = this;
    220     if (LLVM_LIKELY(mNext != nullptr)) {
    221         mNext->mPrev = this;
    222     }
    223 }
    224 
    225 /** ------------------------------------------------------------------------------------------------------------- *
    226  * @brief removeFromParent
    227  ** ------------------------------------------------------------------------------------------------------------- */
    228 Statement * Statement::removeFromParent() noexcept {
    229     Statement * next = mNext;
    230     if (LLVM_LIKELY(mParent != nullptr)) {
    231         if (LLVM_UNLIKELY(mParent->mFirst == this)) {
    232             mParent->mFirst = mNext;
    233         }
    234         if (LLVM_UNLIKELY(mParent->mLast == this)) {
    235             mParent->mLast = mPrev;
    236         }
    237         if (LLVM_UNLIKELY(mParent->mInsertionPoint == this)) {
    238             mParent->mInsertionPoint = mPrev;
    239         }
    240         if (LLVM_LIKELY(mPrev != nullptr)) {
    241             mPrev->mNext = mNext;
    242         }
    243         if (LLVM_LIKELY(mNext != nullptr)) {
    244             mNext->mPrev = mPrev;
    245         }
    246     }
    247     mPrev = nullptr;
    248     mNext = nullptr;
    249     mParent = nullptr;
    250     return next;
    251 }
    252 
    253 /** ------------------------------------------------------------------------------------------------------------- *
    254  * @brief eraseFromParent
    255  ** ------------------------------------------------------------------------------------------------------------- */
    256 Statement * Statement::eraseFromParent(const bool recursively) noexcept {
    257 
    258     if (LLVM_UNLIKELY(getParent() == nullptr)) {
    259         return nullptr;
    260     }
    261 
    262     if (LLVM_UNLIKELY(isa<Branch>(this))) {
    263         cast<Branch>(this)->getBody()->eraseFromParent(recursively);
    264     } else if (LLVM_LIKELY(!isa<Assign>(this))) {
    265         replaceAllUsesWith(getParent()->createZeroes(getType()));
    266     }
    267 
    268     Statement * const next = removeFromParent();
    269     for (unsigned i = 0; i != mOperands; ++i) {
    270         PabloAST * const op = mOperand[i]; assert (op);
    271         op->removeUser(this);
    272         if (LLVM_UNLIKELY(recursively && op->getNumUses() == 0)) {
    273             if (LLVM_LIKELY(isa<Statement>(op))) {
    274                 cast<Statement>(op)->eraseFromParent(true);
    275             }
    276         }
    277         mOperand[i] = nullptr;
    278     }
    279 
    280     return next;
    281 }
    282 
    283 /** ------------------------------------------------------------------------------------------------------------- *
    284  * @brief replaceWith
    285  ** ------------------------------------------------------------------------------------------------------------- */
    286 Statement * Statement::replaceWith(PabloAST * const expr, const bool rename, const bool recursively) noexcept {
    287     assert (expr);
    288     if (LLVM_UNLIKELY(expr == this)) {
    289         return getNextNode();
    290     }
    291     if (LLVM_LIKELY(rename && isa<Statement>(expr))) {
    292         if (mName && cast<Statement>(expr)->mName == nullptr) {
    293             cast<Statement>(expr)->setName(mName);
    294         }
    295     }
    296     replaceAllUsesWith(expr);
    297     return eraseFromParent(recursively);
     138 * @brief setName
     139 ** ------------------------------------------------------------------------------------------------------------- */
     140void NamedPabloAST::setName(const String * const name) {
     141    if (LLVM_UNLIKELY(name == nullptr)) {
     142        llvm::report_fatal_error("name cannot be null");
     143    }
     144    mName = name;
    298145}
    299146
     
    344191
    345192/** ------------------------------------------------------------------------------------------------------------- *
    346  * @brief setName
    347  ** ------------------------------------------------------------------------------------------------------------- */
    348 void Statement::setName(const String * const name) {
    349     if (LLVM_UNLIKELY(name == nullptr)) {
    350         llvm::report_fatal_error("Statement name cannot be null!");
    351     }
    352     mName = name;
     193 * @brief replaceUsesOfWith
     194 ** ------------------------------------------------------------------------------------------------------------- */
     195void Statement::replaceUsesOfWith(PabloAST * const from, PabloAST * const to) {
     196    if (LLVM_LIKELY(from != to)) {
     197        for (unsigned i = 0; i != getNumOperands(); ++i) {
     198           if (getOperand(i) == from) {
     199               setOperand(i, to);
     200           }
     201        }
     202    }
     203}
     204
     205/** ------------------------------------------------------------------------------------------------------------- *
     206 * @brief setOperand
     207 ** ------------------------------------------------------------------------------------------------------------- */
     208void Statement::setOperand(const unsigned index, PabloAST * const value) {   
     209    assert ("Operand cannot be null!" && value);
     210    assert (index < getNumOperands());
     211    PabloAST * const prior = getOperand(index);
     212    assert ("Operand cannot be null!" && prior);
     213    if (LLVM_UNLIKELY(prior == value)) {
     214        return;
     215    }     
     216    if (LLVM_UNLIKELY(prior->getType() != value->getType())) {
     217        std::string tmp;
     218        raw_string_ostream out(tmp);
     219        out << "Type mismatch replacing operand ";
     220        prior->print(out);
     221        out << " with ";
     222        value->print(out);
     223        out << " in statement ";
     224        this->print(out);
     225        llvm::report_fatal_error(out.str());
     226    }
     227    prior->removeUser(this);
     228    mOperand[index] = value;
     229    value->addUser(this);
     230}
     231
     232/** ------------------------------------------------------------------------------------------------------------- *
     233 * @brief insertBefore
     234 ** ------------------------------------------------------------------------------------------------------------- */
     235void Statement::insertBefore(Statement * const statement) {
     236    if (LLVM_UNLIKELY(statement == this)) {
     237        return;
     238    } else if (LLVM_UNLIKELY(statement == nullptr)) {
     239        llvm::report_fatal_error("cannot insert before null statement!");
     240    } else if (LLVM_UNLIKELY(statement->mParent == nullptr)) {
     241        llvm::report_fatal_error("statement is not contained in a pablo block!");
     242    }
     243    removeFromParent();
     244    mParent = statement->mParent;
     245    if (LLVM_UNLIKELY(mParent->mFirst == statement)) {
     246        mParent->mFirst = this;
     247    }
     248    mNext = statement;
     249    mPrev = statement->mPrev;
     250    statement->mPrev = this;
     251    if (LLVM_LIKELY(mPrev != nullptr)) {
     252        mPrev->mNext = this;
     253    }
     254}
     255
     256/** ------------------------------------------------------------------------------------------------------------- *
     257 * @brief insertAfter
     258 ** ------------------------------------------------------------------------------------------------------------- */
     259void Statement::insertAfter(Statement * const statement) {
     260    if (LLVM_UNLIKELY(statement == this)) {
     261        return;
     262    } else if (LLVM_UNLIKELY(statement == nullptr)) {
     263        llvm::report_fatal_error("cannot insert after null statement!");
     264    } else if (LLVM_UNLIKELY(statement->mParent == nullptr)) {
     265        llvm::report_fatal_error("statement is not contained in a pablo block!");
     266    }
     267    removeFromParent();
     268    mParent = statement->mParent;
     269    if (LLVM_UNLIKELY(mParent->mLast == statement)) {
     270        mParent->mLast = this;
     271    }
     272    mPrev = statement;
     273    mNext = statement->mNext;
     274    statement->mNext = this;
     275    if (LLVM_LIKELY(mNext != nullptr)) {
     276        mNext->mPrev = this;
     277    }
     278}
     279
     280/** ------------------------------------------------------------------------------------------------------------- *
     281 * @brief removeFromParent
     282 ** ------------------------------------------------------------------------------------------------------------- */
     283Statement * Statement::removeFromParent() noexcept {
     284    Statement * next = mNext;
     285    if (LLVM_LIKELY(mParent != nullptr)) {
     286        if (LLVM_UNLIKELY(mParent->mFirst == this)) {
     287            mParent->mFirst = mNext;
     288        }
     289        if (LLVM_UNLIKELY(mParent->mLast == this)) {
     290            mParent->mLast = mPrev;
     291        }
     292        if (LLVM_UNLIKELY(mParent->mInsertionPoint == this)) {
     293            mParent->mInsertionPoint = mPrev;
     294        }
     295        if (LLVM_LIKELY(mPrev != nullptr)) {
     296            mPrev->mNext = mNext;
     297        }
     298        if (LLVM_LIKELY(mNext != nullptr)) {
     299            mNext->mPrev = mPrev;
     300        }
     301    }
     302    mPrev = nullptr;
     303    mNext = nullptr;
     304    mParent = nullptr;
     305    return next;
     306}
     307
     308/** ------------------------------------------------------------------------------------------------------------- *
     309 * @brief eraseFromParent
     310 ** ------------------------------------------------------------------------------------------------------------- */
     311Statement * Statement::eraseFromParent(const bool recursively) noexcept {
     312
     313    if (LLVM_UNLIKELY(getParent() == nullptr)) {
     314        return nullptr;
     315    }
     316
     317    if (LLVM_UNLIKELY(isa<Branch>(this))) {
     318        cast<Branch>(this)->getBody()->eraseFromParent(recursively);
     319    } else if (LLVM_LIKELY(!isa<Assign>(this))) {
     320        replaceAllUsesWith(getParent()->createZeroes(getType()));
     321    }
     322
     323    Statement * const next = removeFromParent();
     324    for (unsigned i = 0; i != mOperands; ++i) {
     325        PabloAST * const op = mOperand[i]; assert (op);
     326        op->removeUser(this);
     327        if (LLVM_UNLIKELY(recursively && op->getNumUses() == 0)) {
     328            if (LLVM_LIKELY(isa<Statement>(op))) {
     329                cast<Statement>(op)->eraseFromParent(true);
     330            }
     331        }
     332        mOperand[i] = nullptr;
     333    }
     334
     335    return next;
     336}
     337
     338/** ------------------------------------------------------------------------------------------------------------- *
     339 * @brief replaceWith
     340 ** ------------------------------------------------------------------------------------------------------------- */
     341Statement * Statement::replaceWith(PabloAST * const expr, const bool rename, const bool recursively) noexcept {
     342    assert (expr);
     343    if (LLVM_UNLIKELY(expr == this)) {
     344        return getNextNode();
     345    }
     346    if (LLVM_UNLIKELY(mName && rename)) {
     347        if (LLVM_LIKELY(isa<Statement>(expr) && cast<Statement>(expr)->mName == nullptr)) {
     348            cast<Statement>(expr)->setName(mName);
     349            mName = nullptr;
     350        }
     351    }
     352    replaceAllUsesWith(expr);
     353    return eraseFromParent(recursively);
    353354}
    354355
     
    356357 * @brief contains
    357358 ** ------------------------------------------------------------------------------------------------------------- */
    358 bool StatementList::contains(const Statement * const statement) const {
     359bool StatementList::contains(const Statement * const statement) const noexcept {
    359360    for (const Statement * stmt : *this) {
    360361        if (statement == stmt) {
     
    379380 * 6. Assign a, t           6 >> 6
    380381 ** ------------------------------------------------------------------------------------------------------------- */
    381 bool dominates(const PabloAST * const expr1, const PabloAST * const expr2) {
     382bool dominates(const PabloAST * const expr1, const PabloAST * const expr2) noexcept {
    382383    if (LLVM_UNLIKELY(expr1 == nullptr || expr2 == nullptr)) {
    383384        return (expr2 == nullptr);
     
    428429 * 6. Assign a, t           6 << 1, 2, 3, 4, 5, 6
    429430 ** ------------------------------------------------------------------------------------------------------------- */
    430 bool postdominates(const PabloAST * const expr1, const PabloAST * const expr2) {
     431bool postdominates(const PabloAST * const expr1, const PabloAST * const expr2) noexcept {
    431432    throw std::runtime_error("not implemented yet!");
    432433}
Note: See TracChangeset for help on using the changeset viewer.