Ignore:
Timestamp:
Nov 30, 2015, 4:30:02 PM (4 years ago)
Author:
nmedfort
Message:

More work on n-ary operations. Unresolved bug in DistributionPass?.

File:
1 edited

Legend:

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

    r4880 r4885  
    77#include <pablo/analysis/pabloverifier.hpp>
    88#include <pablo/optimizers/distributivepass.h>
    9 
     9#include <pablo/optimizers/codemotionpass.h>
    1010
    1111#include <pablo/printer_pablos.h>
     
    114114
    115115/** ------------------------------------------------------------------------------------------------------------- *
    116  * @brief removeCommonLiterals
     116 * @brief extractNegationsOutwards
    117117 ** ------------------------------------------------------------------------------------------------------------- */
    118 inline void FlattenAssociativeDFG::removeCommonLiterals(Assign * const def) {
    119     PabloAST * op = def->getOperand(0);
    120     if (isa<And>(op) || isa<Or>(op) || isa<Xor>(op)) {
    121         removeCommonLiterals(def, cast<Variadic>(op));
    122     }
    123 }
    124 
    125 /** ------------------------------------------------------------------------------------------------------------- *
    126  * @brief removeCommonLiterals
    127  ** ------------------------------------------------------------------------------------------------------------- */
    128 void FlattenAssociativeDFG::removeCommonLiterals(Statement * input, Variadic * var) {
    129     std::vector<PabloAST *> common(var->begin(), var->end());
    130     std::vector<PabloAST *> temp;
    131     temp.reserve(common.size());
    132     for (PabloAST * user : input->users()) {
    133         if (user->getClassTypeId() != var->getClassTypeId()) {
    134             if (isa<If>(user) && (input != cast<If>(user)->getCondition())) {
    135                 continue;
    136             }
    137             return;
    138         }
    139         std::set_intersection(common.begin(), common.end(), cast<Variadic>(user)->begin(), cast<Variadic>(user)->end(), std::back_inserter(temp));
    140         common.swap(temp);
    141         temp.clear();
    142     }
    143     for (PabloAST * op : common) {
    144         var->removeOperand(op);
    145     }
    146 }
    147 
    148 /** ------------------------------------------------------------------------------------------------------------- *
    149  * @brief removeCommonLiterals
    150  ** ------------------------------------------------------------------------------------------------------------- */
    151 inline void FlattenAssociativeDFG::removeCommonLiterals(PabloBlock * const block) {
     118inline void FlattenAssociativeDFG::extractNegationsOutwards(PabloBlock * const block) {
    152119    for (Statement * stmt : *block) {
    153120        if (isa<If>(stmt) || isa<While>(stmt)) {
    154             removeCommonLiterals(isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody());
    155         } else if (isa<And>(stmt) || isa<Or>(stmt)) {
    156             removeCommonLiterals(cast<Variadic>(stmt), cast<Variadic>(stmt));
    157         } else if (isa<Assign>(stmt)) {
    158             removeCommonLiterals(cast<Assign>(stmt));
    159         }
    160     }
    161 }
    162 
    163 /** ------------------------------------------------------------------------------------------------------------- *
    164  * @brief extract
    165  ** ------------------------------------------------------------------------------------------------------------- */
    166 inline void FlattenAssociativeDFG::extract(PabloBlock * const block) {
    167     for (Statement * stmt : *block) {
    168         if (isa<If>(stmt) || isa<While>(stmt)) {
    169             extract(isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody());
     121            extractNegationsOutwards(isa<If>(stmt) ? cast<If>(stmt)->getBody() : cast<While>(stmt)->getBody());
    170122        } else if (isa<And>(stmt) || isa<Or>(stmt)) {
    171123            extractNegationsOutwards(cast<Variadic>(stmt), block);
     
    175127
    176128/** ------------------------------------------------------------------------------------------------------------- *
    177  * @brief process
     129 * @brief transform
    178130 ** ------------------------------------------------------------------------------------------------------------- */
    179131void FlattenAssociativeDFG::transform(PabloFunction & function) {
    180132
    181     for (;;) {
     133    FlattenAssociativeDFG::flatten(function.getEntryBlock());
     134    #ifndef NDEBUG
     135    PabloVerifier::verify(function, "post-flatten");
     136    #endif
    182137
    183         FlattenAssociativeDFG::flatten(function.getEntryBlock());
    184         #ifndef NDEBUG
    185         PabloVerifier::verify(function, "post-flatten");
    186         #endif
    187         FlattenAssociativeDFG::removeCommonLiterals(function.getEntryBlock());
    188         #ifndef NDEBUG
    189         PabloVerifier::verify(function, "post-remove-common-literals");
    190         #endif
     138    Simplifier::optimize(function);
     139    DistributivePass::optimize(function);
    191140
    192         Simplifier::optimize(function);
     141    FlattenAssociativeDFG::extractNegationsOutwards(function.getEntryBlock());
     142    #ifndef NDEBUG
     143    PabloVerifier::verify(function, "post-extract-negations-outwards");
     144    #endif
    193145
    194         const bool distributed = DistributivePass::optimize(function);
    195 
    196         FlattenAssociativeDFG::extract(function.getEntryBlock());
    197         #ifndef NDEBUG
    198         PabloVerifier::verify(function, "post-extract");
    199         #endif
    200         Simplifier::optimize(function);
    201 
    202         if (distributed == 0) {
    203             break;
    204         }
    205     }
    206 
    207     if (DistributivePass::optimize(function)) {
    208         throw std::runtime_error("Some distributions remained!");
    209     }
     146    Simplifier::optimize(function);
    210147
    211148}
Note: See TracChangeset for help on using the changeset viewer.