Ignore:
Timestamp:
Sep 27, 2015, 1:32:27 AM (4 years ago)
Author:
nmedfort
Message:

Progress on multi-target UCD compiler.

File:
1 edited

Legend:

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

    r4788 r4797  
    44#include <pablo/function.h>
    55#include <pablo/printer_pablos.h>
     6#include <pablo/analysis/pabloverifier.hpp>
    67#include <unordered_map>
    78#include <iostream>
     
    1213    eliminateRedundantCode(function.getEntryBlock());
    1314    deadCodeElimination(function.getEntryBlock());
    14     eliminateRedundantComplexStatements(function.getEntryBlock());
     15    // eliminateRedundantComplexStatements(function.getEntryBlock());
     16    #ifndef NDEBUG
     17    PabloVerifier::verify(function, "post-simplification");
     18    #endif
    1519    return true;
    1620}
     
    2731    return false;
    2832}
    29 
    30 #ifndef NDEBUG
    31 static bool verifyStatementIsInSameBlock(const Statement * const stmt, const PabloBlock & block) {
    32     Statement * curr = block.front();
    33     while (curr) {
    34         if (curr == stmt) {
    35             return true;
    36         }
    37         curr = curr->getNextNode();
    38     }
    39     return false;
    40 }
    41 #endif
    4233
    4334inline bool Simplifier::isSuperfluous(const Assign * const assign) {
     
    6758inline bool demoteDefinedVar(const If * ifNode, const Assign * def) {
    6859    // If this value isn't used outside of this scope then there is no reason to allow it to escape it.
    69     if (!escapes(def) || (ifNode->getCondition() == def->getExpression())) {
     60    if (!escapes(def)) {
    7061        return true;
    7162    }
     
    7465        return true;
    7566    }
    76     // Finally, if the assignment is a constant Zero or One, it's already known.
     67    // Finally, if the assignment is a constant, it's already known.
    7768    if (isa<Zeroes>(def->getExpression()) || isa<Ones>(def->getExpression()))  {
    7869        return true;
     
    8374void Simplifier::eliminateRedundantCode(PabloBlock & block, ExpressionTable * predecessor) {
    8475    ExpressionTable encountered(predecessor);
    85 
    8676    Statement * stmt = block.front();
    87 
    8877    while (stmt) {
    89 
    90         assert (stmt);
    91         assert (verifyStatementIsInSameBlock(stmt, block));
    92 
    9378        if (Assign * assign = dyn_cast<Assign>(stmt)) {
    9479            // If we have an Assign whose users do not contain an If or Next node, we can replace its users with
     
    10287                continue;
    10388            }
    104         }
    105         else if (If * ifNode = dyn_cast<If>(stmt)) {
     89            // Force the uses of an local Assign to be the expression instead.
     90            for (PabloAST * use : assign->users()) {
     91                if (Statement * user = dyn_cast<Statement>(use)) {
     92                    if (LLVM_UNLIKELY(user->getParent() == &block)) {
     93                        user->replaceUsesOfWith(assign, assign->getExpression());
     94                    }
     95                }
     96            }
     97        } else if (If * ifNode = dyn_cast<If>(stmt)) {
    10698            // Check to see if the Cond is Zero and delete the loop.
    10799            if (LLVM_UNLIKELY(isa<Zeroes>(ifNode->getCondition()))) {
     
    151143                }
    152144            }
    153         }
    154         else if (isa<While>(stmt)) {
     145        } else if (isa<While>(stmt)) {
    155146            eliminateRedundantCode(cast<While>(stmt)->getBody(), &encountered);
    156         }       
    157         else if (stmt->getNumUses() == 0) {
    158             // If this statement has no users, we can discard it. If a future "identical" statement occurs that does
    159             // have users, we can use that statement instead.
    160             stmt = stmt->eraseFromParent();
    161             continue;
    162         }
    163         else if (canTriviallyFold(stmt)) { // non-Assign node
    164 
     147        } else if (canTriviallyFold(stmt)) { // non-Assign node
    165148            // Do a trivial folding test to see if we're using all 0s or 1s as an operand.
    166149            PabloAST * expr = nullptr;
     
    202185            }
    203186            stmt = stmt->replaceWith(expr);
    204             // the next statement could be an Assign; just restart the loop.
     187            block.setInsertPoint(block.back());
    205188            continue;
    206         }
    207         else {
     189        } else {
    208190            // When we're creating the Pablo program, it's possible to have multiple instances of an "identical"
    209191            // statement. By recording which statements have already been seen, we can detect the redundant statements
     
    211193            // and erase this statement from the AST
    212194            const auto f = encountered.findOrAdd(stmt);
    213             if (!std::get<1>(f)) {
    214                 stmt = stmt->replaceWith(std::get<0>(f), true, true);
     195            if (f.second == false) {
     196                stmt = stmt->replaceWith(f.first, true, true);
    215197                continue;
    216198            }
    217199        }
    218         assert (stmt);
    219200        stmt = stmt->getNextNode();
    220201    }
    221     block.setInsertPoint(block.back());
    222202}
    223203
Note: See TracChangeset for help on using the changeset viewer.