Ignore:
Timestamp:
Nov 19, 2015, 4:47:28 PM (4 years ago)
Author:
nmedfort
Message:

More work towards n-ary And/Or/Xor? functions.

File:
1 edited

Legend:

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

    r4873 r4876  
    1010#include <pablo/printer_pablos.h>
    1111#include <llvm/ADT/SmallVector.h>
     12#include <iostream>
    1213
    1314namespace pablo {
     
    1516PabloAST::Allocator PabloAST::mAllocator;
    1617
    17 /*
    18 
    19     Return true if expr1 and expr2 can be proven equivalent according to some rules,
    20     false otherwise.  Note that false may be returned i some cases when the exprs are
    21     equivalent.
    22 
    23 */
    24 
    2518/** ------------------------------------------------------------------------------------------------------------- *
    2619 * @brief equals
     20 *
     21 *  Return true if expr1 and expr2 can be proven equivalent according to some rules, false otherwise.  Note that
     22 *  false may be returned i some cases when the exprs are equivalent.
    2723 ** ------------------------------------------------------------------------------------------------------------- */
    2824bool equals(const PabloAST * expr1, const PabloAST * expr2) {
     
    8783 ** ------------------------------------------------------------------------------------------------------------- */
    8884void PabloAST::replaceAllUsesWith(PabloAST * expr) {
     85    assert (expr);
    8986    if (LLVM_UNLIKELY(this == expr)) {
    9087        return;
    9188    }
    92     Statement * replacements[mUsers.size()];
    93     Vector::size_type users = 0;
    94     bool exprIsAUser = false;
    95     assert (expr);
     89    Statement * replacement[mUsers.size()];
     90    Users::size_type replacements = 0;
     91    PabloAST * last = nullptr;
    9692    for (PabloAST * user : mUsers) {
    97         if (LLVM_UNLIKELY(user == expr)) {
    98             exprIsAUser = true;
     93        if (LLVM_UNLIKELY(user == expr || user == last)) {
    9994            continue;
    100         }
    101         replacements[users++] = cast<Statement>(user);
    102     }
    103     mUsers.clear();
    104     if (LLVM_UNLIKELY(exprIsAUser)) {
    105         mUsers.push_back(expr);
    106     }
    107     for (Vector::size_type i = 0; i != users; ++i) {
    108         replacements[i]->replaceUsesOfWith(this, expr);
     95        } else if (isa<Statement>(user)) {
     96            replacement[replacements++] = cast<Statement>(user);
     97            last = user;
     98        }
     99    }
     100    for (Users::size_type i = 0; i != replacements; ++i) {
     101        replacement[i]->replaceUsesOfWith(this, expr);
    109102    }
    110103}
     
    161154 ** ------------------------------------------------------------------------------------------------------------- */
    162155void Statement::setOperand(const unsigned index, PabloAST * const value) {
    163     assert (value);
     156    assert ("No operand can be null!" && value);
    164157    assert (index < getNumOperands());
    165     PabloAST * const priorValue = getOperand(index);
    166     if (LLVM_UNLIKELY(priorValue == value)) {
     158    PabloAST * const prior = getOperand(index);
     159    assert ("No operand can be null!" && prior);
     160    if (LLVM_UNLIKELY(prior == value)) {
    167161        return;
    168162    }   
    169     if (LLVM_LIKELY(priorValue != nullptr)) {
    170         // Test just to be sure that we don't have multiple operands pointing to
    171         // what we're replacing. If not, remove this from the prior value's
    172         // user list.
    173         unsigned count = 0;
    174         for (unsigned i = 0; i != getNumOperands(); ++i) {
    175             count += (getOperand(i) == priorValue) ? 1 : 0;
    176         }
    177         assert (count >= 1);
    178         if (LLVM_LIKELY(count == 1)) {
    179             priorValue->removeUser(this);
    180         }
    181     }
     163    prior->removeUser(this);
    182164    mOperand[index] = value;
    183165    value->addUser(this);
     
    336318        if (LLVM_UNLIKELY(redundantBranches.size() != 0)) {
    337319            // By eliminating this redundant branch, we may inadvertantly delete the scope block this statement
    338             // resides within. Check and return null if so.
     320            // resides within. Return null if so.
    339321            bool eliminatedScope = false;
    340322            for (Statement * br : redundantBranches) {
     
    376358 * @brief addOperand
    377359 ** ------------------------------------------------------------------------------------------------------------- */
    378 void Variadic::addOperand(PabloAST * expr) {
    379     unsigned index = 0;
    380     for (; index != mOperands; ++index) {
    381         if (mOperand[index] > expr) {
    382             break;
    383         }
    384     }
    385     if (mOperands == mCapacity) {
     360void Variadic::addOperand(PabloAST * const expr) {
     361    if (LLVM_UNLIKELY(mOperands == mCapacity)) {
    386362        mCapacity = std::max<unsigned>(mCapacity * 2, 2);
    387         PabloAST ** expandedArray = reinterpret_cast<PabloAST**>(mAllocator.allocate(mOperands * sizeof(PabloAST *)));
    388         std::memcpy(expandedArray, mOperand, mOperands * sizeof(PabloAST *));
     363        PabloAST ** expandedOperandSpace = reinterpret_cast<PabloAST**>(mAllocator.allocate(mCapacity * sizeof(PabloAST *)));
     364        for (unsigned i = 0; i != mOperands; ++i) {
     365            expandedOperandSpace[i] = mOperand[i];
     366        }
    389367        mAllocator.deallocate(reinterpret_cast<Allocator::pointer>(mOperand));
    390         mOperand = expandedArray;
    391     }
    392     std::memcpy(mOperand + index + 1, mOperand + index, (mOperands - index) * sizeof(PabloAST *));
    393     mOperand[index] = expr;
    394     ++mOperands;
     368        mOperand = expandedOperandSpace;
     369    }
     370    mOperand[mOperands++] = expr;
     371    expr->addUser(this);
    395372}
    396373
     
    398375 * @brief removeOperand
    399376 ** ------------------------------------------------------------------------------------------------------------- */
    400 void Variadic::removeOperand(const unsigned index) {
    401     assert (index < getNumOperands());
    402     std::memcpy(mOperand + index, mOperand + index + 1, (mOperands - index - 1) * sizeof(PabloAST *));
     377PabloAST * Variadic::removeOperand(const unsigned index) {
     378    assert (index < mOperands);
     379    PabloAST * const expr = mOperand[index];
    403380    --mOperands;
     381    for (unsigned i = index; i != mOperands; ++i) {
     382        mOperand[i] = mOperand[i + 1];
     383    }
     384    mOperand[mOperands] = nullptr;
     385    expr->removeUser(this);
     386    return expr;
    404387}
    405388
     
    414397    }
    415398    return false;
    416 }
    417 
    418 StatementList::~StatementList() {
    419 
    420399}
    421400
Note: See TracChangeset for help on using the changeset viewer.