source: icGREP/icgrep-devel/icgrep/pablo/passes/flattenif.cpp @ 5124

Last change on this file since 5124 was 5032, checked in by xuedongx, 3 years ago

Add a Pablo option to flatten all the Ifs in the Pablo AST.

File size: 2.1 KB
Line 
1#include <pablo/passes/flattenif.hpp>
2#include <pablo/codegenstate.h>
3#include <pablo/expression_map.hpp>
4#include <pablo/function.h>
5#include <pablo/printer_pablos.h>
6#include <pablo/analysis/pabloverifier.hpp>
7#include <boost/container/flat_set.hpp>
8
9#include <pablo/printer_pablos.h>
10#include <iostream>
11
12namespace pablo {
13   
14bool has_if(PabloBlock * const block){
15    Statement * stmt = block->front();
16    int if_flag = false;
17    while(stmt){
18        if (isa<If>(stmt)){
19            if_flag = true;
20            return if_flag;
21        }
22        stmt = stmt->getNextNode();
23    }
24    return if_flag;
25}
26
27void flattenIf_helper(PabloBlock * const block){
28    Statement * stmt = block->front();
29    /*std::string tmp;
30     llvm::raw_string_ostream msg(tmp);
31     msg << "stmt: ";
32     PabloPrinter::print(stmt, msg);*/
33    while(stmt){
34        if (If * ifNode = dyn_cast<If>(stmt)){
35            Statement * prior = stmt->getPrevNode();
36            PabloBlock * body = ifNode->getBody();
37            //Simplifier::flattenIf(body);
38            Statement * body_stmt = body->front();
39            Statement * next;
40            while(body_stmt){
41                next = body_stmt->getNextNode();
42                if (isa<Assign>(body_stmt)) {
43                    ifNode->removeDefined(cast<Assign>(body_stmt));
44                }
45                body_stmt->insertAfter(prior);
46                prior = body_stmt;
47                body_stmt = next;
48            }
49            stmt = stmt->eraseFromParent();
50           
51        }
52        stmt = stmt->getNextNode();
53    }
54}
55
56void FlattenIf::flattenIf(PabloBlock * const block){
57    bool flag = has_if(block);
58    while(flag){
59        flattenIf_helper(block);
60        flag = has_if(block);
61    }
62}
63
64/** ------------------------------------------------------------------------------------------------------------- *
65 * @brief transform
66 ** ------------------------------------------------------------------------------------------------------------- */
67void FlattenIf::transform(PabloFunction & function) {
68    flattenIf(function.getEntryBlock());
69    #ifndef NDEBUG
70    PabloVerifier::verify(function, "flatten-if");
71    #endif
72}
73
74}
Note: See TracBrowser for help on using the repository browser.