Ignore:
Timestamp:
Jan 15, 2018, 4:07:07 PM (17 months ago)
Author:
nmedfort
Message:

Removed Variadic functionality; allowed for deferred creation of statement names

Location:
icGREP/icgrep-devel/icgrep/pablo
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/boolean.h

    r5454 r5834  
    66namespace pablo {
    77
    8 class And final : public Variadic {
     8class And final : public Statement {
    99    friend class PabloBlock;
    1010public:
     
    1818protected:
    1919    And(llvm::Type * const type, PabloAST * expr1, PabloAST * expr2, const String * name, Allocator & allocator)
    20     : Variadic(ClassTypeId::And, type, {expr1, expr2}, name, allocator)
     20    : Statement(ClassTypeId::And, type, {expr1, expr2}, name, allocator)
    2121    {
    22 
    23     }
    24     And(llvm::Type * const type, const unsigned reserved, const String * name, Allocator & allocator)
    25     : Variadic(ClassTypeId::And, type, reserved, name, allocator)
    26     {
    27 
    28     }
    29     template<typename iterator>
    30     And(llvm::Type * const type, iterator begin, iterator end, const String * name, Allocator & allocator)
    31     : Variadic(ClassTypeId::And, type, begin, end, name, allocator) {
    3222
    3323    }
    3424};
    3525
    36 class Or final : public Variadic {
     26class Or final : public Statement {
    3727    friend class PabloBlock;
    3828public:
     
    4636protected:
    4737    Or(llvm::Type * const type, PabloAST * expr1, PabloAST * expr2, const String * name, Allocator & allocator)
    48     : Variadic(ClassTypeId::Or, type, {expr1, expr2}, name, allocator)
     38    : Statement(ClassTypeId::Or, type, {expr1, expr2}, name, allocator)
    4939    {
    50 
    51     }
    52     Or(llvm::Type * const type, const unsigned reserved, const String * name, Allocator & allocator)
    53     : Variadic(ClassTypeId::Or, type, reserved, name, allocator)
    54     {
    55 
    56     }
    57     template<typename iterator>
    58     Or(llvm::Type * const type, iterator begin, iterator end, const String * name, Allocator & allocator)
    59     : Variadic(ClassTypeId::Or, type, begin, end, name, allocator) {
    6040
    6141    }
    6242};
    6343
    64 class Xor final : public Variadic {
     44class Xor final : public Statement {
    6545    friend class PabloBlock;
    6646public:
     
    7353protected:
    7454    Xor(llvm::Type * const type, PabloAST * expr1, PabloAST * expr2, const String * name, Allocator & allocator)
    75     : Variadic(ClassTypeId::Xor, type, {expr1, expr2}, name, allocator)
     55    : Statement(ClassTypeId::Xor, type, {expr1, expr2}, name, allocator)
    7656    {
    77 
    78     }
    79     Xor(llvm::Type * const type, const unsigned reserved, const String * name, Allocator & allocator)
    80     : Variadic(ClassTypeId::Xor, type, reserved, name, allocator)
    81     {
    82 
    83     }
    84     template<typename iterator>
    85     Xor(llvm::Type * const type, iterator begin, iterator end, const String * name, Allocator & allocator)
    86     : Variadic(ClassTypeId::Xor, type, begin, end, name, allocator) {
    8757
    8858    }
  • icGREP/icgrep-devel/icgrep/pablo/builder.cpp

    r5828 r5834  
    206206
    207207PabloAST * PabloBuilder::createRepeat(not_null<Integer *> fieldWidth, PabloAST * value) {
    208     MAKE_BINARY(createRepeat, TypeId::Fill, fieldWidth.get(), value);
     208    MAKE_BINARY(createRepeat, TypeId::Repeat, fieldWidth.get(), value);
    209209    return result;
    210210}
    211211
    212212PabloAST * PabloBuilder::createRepeat(not_null<Integer *> fieldWidth, PabloAST * value, const llvm::StringRef & prefix) {
    213     MAKE_NAMED_BINARY(createRepeat, TypeId::Fill, prefix, fieldWidth.get(), value);
     213    MAKE_NAMED_BINARY(createRepeat, TypeId::Repeat, prefix, fieldWidth.get(), value);
    214214    return result;
    215215}
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.cpp

    r5828 r5834  
    4949///
    5050
    51 Count * PabloBlock::createCount(PabloAST * expr) {
     51Count * PabloBlock::createCount(PabloAST * const expr) {
    5252    IntegerType * const type = getParent()->getSizeTy();
    5353    return insertAtInsertionPoint(new (mAllocator) Count(expr, makeName("count"), type, mAllocator));
     
    6161Not * PabloBlock::createNot(PabloAST * expr, String * name) {
    6262    assert (expr);
    63     if (name == nullptr) {
    64         name = makeName("not");
    65     }
    6663    return insertAtInsertionPoint(new (mAllocator) Not(expr, name, mAllocator));
    6764}
    6865
    69 Var * PabloBlock::createVar(PabloAST * name, Type * type) {
     66Var * PabloBlock::createVar(String * name, Type * type) {
    7067    if (type == nullptr) {
    7168        type = getParent()->getStreamTy();
    7269    }
    73     if (LLVM_UNLIKELY(name == nullptr || !isa<String>(name))) {
     70    if (LLVM_UNLIKELY(name == nullptr)) {
    7471        throw std::runtime_error("Var objects must have a String name");
    7572    }
     
    7976InFile * PabloBlock::createInFile(PabloAST * expr, String * name) {
    8077    assert (expr);
    81     if (name == nullptr) {
    82         name = makeName("inFile");
    83     }
    8478    return insertAtInsertionPoint(new (mAllocator) InFile(expr, name, mAllocator));
    8579}
     
    8781AtEOF * PabloBlock::createAtEOF(PabloAST * expr, String * name) {
    8882    assert (expr);
    89     if (name == nullptr) {
    90         name = makeName("atEOF");
    91     }
    9283    return insertAtInsertionPoint(new (mAllocator) AtEOF(expr, name, mAllocator));
    9384}
     
    9687
    9788Advance * PabloBlock::createAdvance(PabloAST * expr, Integer * shiftAmount, String * name) {
    98     if (name == nullptr) {
    99         name = makeName("advance");
    100     }
    10189    return insertAtInsertionPoint(new (mAllocator) Advance(expr, shiftAmount, name, mAllocator));
    10290}
    10391
    10492Lookahead * PabloBlock::createLookahead(PabloAST * expr, Integer * shiftAmount, String * name) {
    105     if (name == nullptr) {
    106         name = makeName("lookahead");
    107     }
    10893    return insertAtInsertionPoint(new (mAllocator) Lookahead(expr, shiftAmount, name, mAllocator));
    10994}
     
    129114And * PabloBlock::createAnd(PabloAST * expr1, PabloAST * expr2, String * name) {
    130115    CHECK_SAME_TYPE(expr1, expr2);
    131     if (name == nullptr) {
    132         name = makeName("and");
    133     }
    134116    return insertAtInsertionPoint(new (mAllocator) And(expr1->getType(), expr1, expr2, name, mAllocator));
    135117}
    136118
    137 And * PabloBlock::createAnd(Type * const type, const unsigned reserved, String * name) {
    138     if (name == nullptr) {
    139         name = makeName("and");
    140     }
    141     return insertAtInsertionPoint(new (mAllocator) And(type, reserved, name, mAllocator));
    142 }
    143 
    144119Or * PabloBlock::createOr(PabloAST * expr1, PabloAST * expr2, String * name) {
    145120    CHECK_SAME_TYPE(expr1, expr2);
    146     if (name == nullptr) {
    147         name = makeName("or");
    148     }
    149121    return insertAtInsertionPoint(new (mAllocator) Or(expr1->getType(), expr1, expr2, name, mAllocator));
    150122}
    151123
    152 Or * PabloBlock::createOr(Type * const type, const unsigned reserved, String * name) {
    153     if (name == nullptr) {
    154         name = makeName("or");
    155     }
    156     return insertAtInsertionPoint(new (mAllocator) Or(type, reserved, name, mAllocator));
    157 }
    158 
    159124Xor * PabloBlock::createXor(PabloAST * expr1, PabloAST * expr2, String * name) {
    160125    CHECK_SAME_TYPE(expr1, expr2);
    161     if (name == nullptr) {
    162         name = makeName("xor");
    163     }
    164126    return insertAtInsertionPoint(new (mAllocator) Xor(expr1->getType(), expr1, expr2, name, mAllocator));
    165 }
    166 
    167 Xor * PabloBlock::createXor(Type * const type, const unsigned reserved, String * name) {
    168     if (name == nullptr) {
    169         name = makeName("xor");
    170     }
    171     return insertAtInsertionPoint(new (mAllocator) Xor(type, reserved, name, mAllocator));
    172127}
    173128
     
    233188MatchStar * PabloBlock::createMatchStar(PabloAST * marker, PabloAST * charclass, String * name) {
    234189    CHECK_SAME_TYPE(marker, charclass);
    235     if (name == nullptr) {
    236         name = makeName("matchstar");
    237     }
    238190    return insertAtInsertionPoint(new (mAllocator) MatchStar(marker, charclass, name, mAllocator));
    239191}
     
    241193ScanThru * PabloBlock::createScanThru(PabloAST * from, PabloAST * thru, String * name) {
    242194    CHECK_SAME_TYPE(from, thru);
    243     if (name == nullptr) {
    244         name = makeName("scanthru");
    245     }
    246195    return insertAtInsertionPoint(new (mAllocator) ScanThru(from, thru, name, mAllocator));
    247196}
     
    249198ScanTo * PabloBlock::createScanTo(PabloAST * from, PabloAST * to, String * name) {
    250199    CHECK_SAME_TYPE(from, to);
    251     if (name == nullptr) {
    252         name = makeName("scanto");
    253     }
    254200    return insertAtInsertionPoint(new (mAllocator) ScanTo(from, to, name, mAllocator));
    255201}
     
    257203AdvanceThenScanThru * PabloBlock::createAdvanceThenScanThru(PabloAST * from, PabloAST * thru, String * name) {
    258204    CHECK_SAME_TYPE(from, thru);
    259     if (name == nullptr) {
    260         name = makeName("advscanthru");
    261     }
    262205    return insertAtInsertionPoint(new (mAllocator) AdvanceThenScanThru(from, thru, name, mAllocator));
    263206}
     
    265208AdvanceThenScanTo * PabloBlock::createAdvanceThenScanTo(PabloAST * from, PabloAST * to, String * name) {
    266209    CHECK_SAME_TYPE(from, to);
    267     if (name == nullptr) {
    268         name = makeName("advscanto");
    269     }
    270210    return insertAtInsertionPoint(new (mAllocator) AdvanceThenScanTo(from, to, name, mAllocator));
    271211}
     
    287227Repeat * PabloBlock::createRepeat(Integer * fieldWidth, PabloAST * value, String * name) {
    288228    assert (fieldWidth && value);
    289     if (name == nullptr) {
    290         name = makeName("fill");
    291     }
    292229    Type * const type = VectorType::get(IntegerType::get(value->getType()->getContext(), fieldWidth->value()), 0);
    293230    return insertAtInsertionPoint(new (mAllocator) Repeat(fieldWidth, value, type, name, mAllocator));
     
    296233PackH * PabloBlock::createPackH(Integer * fieldWidth, PabloAST * value, String * name) {
    297234    assert (fieldWidth && value);
    298     if (name == nullptr) {
    299         name = makeName("packh");
    300     }
    301235    Type * const type = VectorType::get(IntegerType::get(value->getType()->getContext(), fieldWidth->value()), 0);
    302236    return insertAtInsertionPoint(new (mAllocator) PackH(fieldWidth, value, name, type, mAllocator));
     
    305239PackL * PabloBlock::createPackL(Integer * fieldWidth, PabloAST * value, String * name) {
    306240    assert (fieldWidth && value);
    307     if (name == nullptr) {
    308         name = makeName("packl");
    309     }
    310241    Type * const type = VectorType::get(IntegerType::get(value->getType()->getContext(), fieldWidth->value()), 0);
    311242    return insertAtInsertionPoint(new (mAllocator) PackL(fieldWidth, value, name, type, mAllocator));
     
    316247Sel * PabloBlock::createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, String * name) {
    317248    CHECK_SAME_TYPE(trueExpr, falseExpr);
    318     if (name == nullptr) {
    319         name = makeName("sel");
    320     }
    321249    return insertAtInsertionPoint(new (mAllocator) Sel(condition, trueExpr, falseExpr, name, mAllocator));
    322250}
    323251
    324252IndexedAdvance * PabloBlock::createIndexedAdvance(PabloAST * expr, PabloAST * indexStream, Integer * shiftAmount, String * name) {
    325     if (name == nullptr) {
    326         name = makeName("indexed_advance");
    327     }
    328253    return insertAtInsertionPoint(new (mAllocator) IndexedAdvance(expr, indexStream, shiftAmount, name, mAllocator));
    329254}
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r5828 r5834  
    123123    }
    124124
    125     inline Var * createVar(String * name, llvm::Type * const type = nullptr) {
    126         return createVar(reinterpret_cast<PabloAST *>(name), type);
    127     }
     125    Var * createVar(String * name, llvm::Type * const type = nullptr);
    128126
    129127    Count * createCount(PabloAST * expr);
     
    165163    And * createAnd(PabloAST * expr1, PabloAST * expr2, String * name);
    166164
    167     And * createAnd(llvm::Type * const type, const unsigned reserved) {
    168         return createAnd(type, reserved, nullptr);
    169     }
    170 
    171     And * createAnd(llvm::Type * const type, const unsigned reserved, String * name);
    172 
    173165    Or * createOr(PabloAST * expr1, PabloAST * expr2) {
    174166        return createOr(expr1, expr2, nullptr);
     
    181173    Or * createOr(PabloAST * expr1, PabloAST * expr2, String * name);
    182174
    183     Or * createOr(llvm::Type * const type, const unsigned reserved) {
    184         return createOr(type, reserved, nullptr);
    185     }
    186 
    187     Or * createOr(llvm::Type * const type, const unsigned reserved, String * name);
    188 
    189175    Xor * createXor(PabloAST * expr1, PabloAST * expr2) {
    190176        return createXor(expr1, expr2, nullptr);
     
    196182
    197183    Xor * createXor(PabloAST * expr1, PabloAST * expr2, String * name);
    198 
    199     Xor * createXor(llvm::Type * const type, const unsigned reserved) {
    200         return createXor(type, reserved, nullptr);
    201     }
    202 
    203     Xor * createXor(llvm::Type * const type, const unsigned reserved, String * name);
    204184
    205185    Sel * createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr) {
     
    357337    }
    358338
    359     Var * createVar(PabloAST * name, llvm::Type * const type);
    360 
    361339private:       
    362340    PabloKernel * const         mParent;
  • icGREP/icgrep-devel/icgrep/pablo/expression_map.hpp

    r5828 r5834  
    152152            case PabloAST::ClassTypeId::Extract:
    153153            return mBinary.findOrAdd(stmt, stmt->getClassTypeId(), stmt->getOperand(0), stmt->getOperand(1));
    154             case PabloAST::ClassTypeId::Fill:
     154            case PabloAST::ClassTypeId::Repeat:
    155155                return mBinary.findOrAdd(stmt, stmt->getClassTypeId(), stmt->getOperand(0), stmt->getType());
    156156            case PabloAST::ClassTypeId::Sel:
  • icGREP/icgrep-devel/icgrep/pablo/optimizers/pablo_simplifier.cpp

    r5821 r5834  
    2020#include <llvm/IR/Type.h>
    2121
     22#include <llvm/Support/raw_ostream.h>
     23
    2224using namespace boost;
    2325using namespace boost::container;
     
    271273            return block->createZeroes(stmt->getType()); // ¬1 ⇔ 0
    272274        }
    273     } else if (isa<Variadic>(stmt)) {
    274         std::sort(cast<Variadic>(stmt)->begin(), cast<Variadic>(stmt)->end());
    275         for (unsigned i = 1; i < stmt->getNumOperands(); ) {
    276             if (LLVM_UNLIKELY(stmt->getOperand(i - 1) == stmt->getOperand(i))) {
    277                 if (LLVM_UNLIKELY(isa<Xor>(stmt))) {
    278                     if (LLVM_LIKELY(stmt->getNumOperands() == 2)) {
     275    } else if (isa<And>(stmt) || isa<Or>(stmt)) {
     276        PabloAST * op[2];
     277        op[0] = stmt->getOperand(0);
     278        op[1] = stmt->getOperand(1);
     279        for (unsigned i = 0; i < 2; ++i) {
     280            if (const Not * const n = dyn_cast<Not>(op[i])) {
     281                if (LLVM_UNLIKELY(n->getExpr() == op[1 - i])) {
     282                    if (isa<And>(stmt)) {
    279283                        return block->createZeroes(stmt->getType());
    280284                    } else {
    281                         cast<Variadic>(stmt)->removeOperand(i);
    282                         cast<Variadic>(stmt)->removeOperand(i - 1);
    283                         continue;
    284                     }
     285                        return block->createOnes(stmt->getType());
     286                    }
     287                }
     288            } else if (LLVM_UNLIKELY(isa<Zeroes>(op[i]) || isa<Ones>(op[i]))) {
     289                if (isa<And>(stmt) ^ isa<Zeroes>(op)) {
     290                    return op[1 - i];
    285291                } else {
    286                     if (LLVM_LIKELY(stmt->getNumOperands() == 2)) {
    287                         return stmt->getOperand(1 - i);
    288                     } else {
    289                         cast<Variadic>(stmt)->removeOperand(i);
    290                         continue;
    291                     }
    292                 }
    293             }
    294             ++i;
    295         }
    296         if (LLVM_UNLIKELY(stmt->getNumOperands() < 2)) {
    297             if (LLVM_LIKELY(stmt->getNumOperands() == 1)) {
    298                 return stmt->getOperand(0);
     292                    return op[i];
     293                }
     294            }
     295        }
     296        if (LLVM_UNLIKELY(op[0] == op[1])) {
     297            return op[0];
     298        } else {
     299            if (op[1] < op[0]) {
     300                stmt->setOperand(0, op[1]);
     301                stmt->setOperand(1, op[0]);
     302            }
     303            return nullptr;
     304        }
     305    } else if (isa<Xor>(stmt)) {
     306        PabloAST * op[2];
     307        op[0] = stmt->getOperand(0);
     308        op[1] = stmt->getOperand(1);
     309        bool negated = false;
     310        PabloAST * expr = nullptr;
     311        for (unsigned i = 0; i < 2; ++i) {
     312            if (Not * const n = dyn_cast<Not>(op[i])) {
     313                negated ^= true;
     314                op[i] = n->getExpr();
     315            } else if (LLVM_UNLIKELY(isa<Zeroes>(op[i]) || isa<Ones>(op[i]))) {
     316                negated ^= isa<Ones>(op);
     317                expr = op[1 - i];
     318            }
     319        }
     320        if (LLVM_LIKELY(expr == nullptr)) {
     321            if (LLVM_UNLIKELY(op[0] == op[1])) {
     322                if (LLVM_UNLIKELY(negated)) {
     323                    return block->createOnes(stmt->getType());
     324                } else {
     325                    return block->createZeroes(stmt->getType());
     326                }
    299327            } else {
    300                 return block->createZeroes(stmt->getType());
    301             }
    302         }
    303         if (LLVM_UNLIKELY(isa<Xor>(stmt))) {
    304             bool negated = false;
    305             PabloAST * expr = nullptr;
    306             for (unsigned i = 0; i < stmt->getNumOperands(); ) {
    307                 const PabloAST * const op = stmt->getOperand(i);
    308                 if (isa<Not>(op)) {
    309                     negated ^= true;
    310                     stmt->setOperand(i, cast<Not>(op)->getExpr());
    311                 } else if (LLVM_UNLIKELY(isa<Zeroes>(op) || isa<Ones>(op))) {
    312                     negated ^= isa<Ones>(op);
    313                     if (LLVM_LIKELY(stmt->getNumOperands() == 2)) {
    314                         expr = stmt->getOperand(1 - i);
    315                         break;
    316                     } else {
    317                         cast<Variadic>(stmt)->removeOperand(i);
    318                         continue;
    319                     }
    320                 }
    321                 ++i;
    322             }
    323             if (LLVM_UNLIKELY(negated)) {
    324                 block->setInsertPoint(stmt);
    325                 expr = triviallyFold(block->createNot(expr ? expr : stmt), block);
    326             }
    327             return expr;
    328         } else { // if (isa<And>(stmt) || isa<Or>(stmt))
    329             for (unsigned i = 0; i < stmt->getNumOperands(); ++i) {
    330                 const PabloAST * const op = stmt->getOperand(i);
    331                 if (LLVM_UNLIKELY(isa<Zeroes>(op) || isa<Ones>(op))) {
    332                     if (isa<And>(stmt) ^ isa<Zeroes>(op)) {
    333                         if (LLVM_LIKELY(stmt->getNumOperands() == 2)) {
    334                             return stmt->getOperand(1 - i);
    335                         } else {
    336                             cast<Variadic>(stmt)->removeOperand(i);
    337                             continue;
    338                         }
    339                     } else {
    340                         return stmt->getOperand(i);
    341                     }
    342                 }
    343                 ++i;
    344             }
    345         }
     328                if (op[1] < op[0]) {
     329                    std::swap(op[0], op[1]);
     330                }
     331                stmt->setOperand(0, op[0]);
     332                stmt->setOperand(1, op[1]);
     333            }
     334        }
     335        if (LLVM_UNLIKELY(negated)) {
     336            block->setInsertPoint(stmt);
     337            expr = triviallyFold(block->createNot(expr ? expr : stmt), block);
     338        }
     339        return expr;
    346340    } else if (isa<Advance>(stmt)) {
    347341        Advance * const adv = cast<Advance>(stmt);
  • icGREP/icgrep-devel/icgrep/pablo/pabloAST.cpp

    r5828 r5834  
    2424
    2525using TypeId = PabloAST::ClassTypeId;
     26
     27size_t constexpr __length(const char * const str) {
     28    return *str ? 1 + __length(str + 1) : 0;
     29}
    2630
    2731/** ------------------------------------------------------------------------------------------------------------- *
     
    4751            } else if (isa<AtEOF>(expr1)) {
    4852                return equals(cast<AtEOF>(expr1)->getOperand(0), cast<AtEOF>(expr2)->getOperand(0));
    49             } else if (isa<Variadic>(expr1)) {
    50                 const Variadic * const var1 = cast<Variadic>(expr1);
    51                 const Variadic * const var2 = cast<Variadic>(expr2);
    52                 if (var1->getNumOperands() == var2->getNumOperands()) {
    53                     const unsigned operands = var1->getNumOperands();
    54                     for (unsigned i = 0; i != operands; ++i) {
    55                         bool missing = true;
    56                         for (unsigned j = 0; j != operands; ++j) {
    57                             // odds are both variadics will be sorted; optimize towards testing them in order.
    58                             unsigned k = i + j;
    59                             if (LLVM_UNLIKELY(k >= operands)) {
    60                                 k -= operands;
    61                             }
    62                             if (equals(var1->getOperand(i), var2->getOperand(k))) {
    63                                 missing = false;
    64                                 break;
    65                             }
    66                         }
    67                         if (missing) {
    68                             return false;
    69                         }
    70                     }
    71                     return true;
     53            } else if (isa<And>(expr1) || isa<Or>(expr1) || isa<Xor>(expr1)) {
     54                PabloAST * op1[2];
     55                PabloAST * op2[2];
     56                op1[0] = cast<Statement>(expr1)->getOperand(0);
     57                op1[1] = cast<Statement>(expr1)->getOperand(1);
     58                if (op1[1] < op1[0]) {
     59                    std::swap(op1[0], op1[1]);
    7260                }
     61                op2[0] = cast<Statement>(expr2)->getOperand(0);
     62                op2[1] = cast<Statement>(expr2)->getOperand(1);
     63                if (op2[1] < op2[0]) {
     64                    std::swap(op2[0], op2[1]);
     65                }
     66                return (op1[0] == op2[0]) && (op1[1] == op2[1]);
    7367            } else if (isa<Statement>(expr1)) {
    7468                const Statement * stmt1 = cast<Statement>(expr1);
     
    305299
    306300/** ------------------------------------------------------------------------------------------------------------- *
     301 * @brief getName
     302 ** ------------------------------------------------------------------------------------------------------------- */
     303const String & Statement::getName() const {
     304    if (mName == nullptr) {
     305        if (LLVM_UNLIKELY(mParent == nullptr)) {
     306            llvm::report_fatal_error("cannot assign a default name to a statement that is not within a PabloBlock");
     307        }
     308        const char * prefix = nullptr;
     309        size_t length = 0;
     310        #define MAKE_PREFIX(type_id, name) \
     311            case ClassTypeId:: type_id : prefix = name; length = __length(name); break
     312        switch (mClassTypeId) {
     313            // Boolean operations
     314            MAKE_PREFIX(And, "and");
     315            MAKE_PREFIX(Or, "or");
     316            MAKE_PREFIX(Xor, "xor");
     317            MAKE_PREFIX(Not, "not");
     318            MAKE_PREFIX(Sel, "sel");
     319            // Stream operations
     320            MAKE_PREFIX(Advance, "advance");
     321            MAKE_PREFIX(IndexedAdvance, "indexed_advance");
     322            MAKE_PREFIX(ScanThru, "scanthru");
     323            MAKE_PREFIX(AdvanceThenScanThru, "advscanthru");
     324            MAKE_PREFIX(ScanTo, "scanto");
     325            MAKE_PREFIX(AdvanceThenScanTo, "advscanto");
     326            MAKE_PREFIX(Lookahead, "lookahead");
     327            MAKE_PREFIX(MatchStar, "matchstar");
     328            MAKE_PREFIX(InFile, "inFile");
     329            MAKE_PREFIX(AtEOF, "atEOF");
     330            // Statistics operations
     331            MAKE_PREFIX(Count, "count");
     332            // Misc. operations
     333            MAKE_PREFIX(Repeat, "repeat");
     334            MAKE_PREFIX(PackH, "packh");
     335            MAKE_PREFIX(PackL, "packl");
     336            default: llvm_unreachable("invalid statement type");
     337        }
     338        #undef MAKE_PREFIX
     339        const StringRef __prefix(prefix, length);
     340        mName = mParent->makeName(__prefix);
     341    }
     342    return *mName;
     343}
     344
     345/** ------------------------------------------------------------------------------------------------------------- *
    307346 * @brief setName
    308347 ** ------------------------------------------------------------------------------------------------------------- */
    309 void Statement::setName(const String * const name) noexcept {
     348void Statement::setName(const String * const name) {
    310349    if (LLVM_UNLIKELY(name == nullptr)) {
    311350        llvm::report_fatal_error("Statement name cannot be null!");
    312351    }
    313352    mName = name;
    314 }
    315 
    316 /** ------------------------------------------------------------------------------------------------------------- *
    317  * @brief addOperand
    318  ** ------------------------------------------------------------------------------------------------------------- */
    319 void Variadic::addOperand(PabloAST * const expr) noexcept {
    320     if (LLVM_UNLIKELY(mOperands == mCapacity)) {
    321         mCapacity = std::max<unsigned>(mCapacity * 2, 2);
    322         PabloAST ** expandedOperandSpace = mAllocator.allocate(mCapacity);
    323         for (unsigned i = 0; i != mOperands; ++i) {
    324             expandedOperandSpace[i] = mOperand[i];
    325         }
    326         mAllocator.deallocate(mOperand);
    327         mOperand = expandedOperandSpace;
    328     }
    329     mOperand[mOperands++] = expr;
    330     expr->addUser(this);
    331 }
    332 
    333 /** ------------------------------------------------------------------------------------------------------------- *
    334  * @brief removeOperand
    335  ** ------------------------------------------------------------------------------------------------------------- */
    336 PabloAST * Variadic::removeOperand(const unsigned index) noexcept {
    337     assert (index < mOperands);
    338     PabloAST * const expr = mOperand[index];
    339     assert (expr);
    340     --mOperands;
    341     for (unsigned i = index; i != mOperands; ++i) {
    342         mOperand[i] = mOperand[i + 1];
    343     }
    344     mOperand[mOperands] = nullptr;
    345     expr->removeUser(this);
    346     return expr;
    347353}
    348354
  • icGREP/icgrep-devel/icgrep/pablo/pabloAST.h

    r5828 r5834  
    2323class PabloAST {
    2424    friend class Statement;
    25     friend class Variadic;
    2625    friend class StatementList;
    2726    friend class Branch;
     
    4544    }
    4645
     46    // NOTE: when adding new statement types, update Statement::getName() to generate
     47    // a default name for the class.
    4748    enum class ClassTypeId : unsigned {
    4849        /** Expressions and Constants **/
     
    9394        , While
    9495        // Misc. operations
    95         , Fill
     96        , Repeat
    9697        , PackH
    9798        , PackL
     
    206207    void replaceUsesOfWith(PabloAST * const from, PabloAST * const to);
    207208
    208     const String & getName() const noexcept {
    209         return *mName;
    210     }
    211 
    212     void setName(const String * const name) noexcept;
     209    // NOTE: getName() can generate a default name if one is does not exist for it
     210    const String & getName() const;
    213211
    214212    inline PabloAST * getOperand(const unsigned index) const noexcept {
     
    238236        return mParent;
    239237    }
     238
    240239    virtual ~Statement() = default;
     240
     241    void setName(const String * const name);
    241242
    242243protected:
     
    259260    }
    260261
    261     explicit Statement(const ClassTypeId id, llvm::Type * const type, const unsigned reserved, const String * const name, Allocator & allocator)
    262     : PabloAST(id, type, allocator)
    263     , mOperands(0)
    264     , mOperand(allocator.allocate(reserved))
    265     , mNext(nullptr)
    266     , mPrev(nullptr)
    267     , mName(name)
    268     , mParent(nullptr) {
    269         std::memset(mOperand, 0, reserved * sizeof(PabloAST *));
    270     }
    271 
    272     template<typename iterator>
    273     explicit Statement(const ClassTypeId id, llvm::Type * const type, iterator begin, iterator end, const String * const name, Allocator & allocator)
    274     : PabloAST(id, type, allocator)
    275     , mOperands(std::distance(begin, end))
    276     , mOperand(allocator.allocate(mOperands))
    277     , mNext(nullptr)
    278     , mPrev(nullptr)
    279     , mName(name)
    280     , mParent(nullptr) {
    281         unsigned i = 0;
    282         for (auto value = begin; value != end; ++value, ++i) {
    283             assert (*value);
    284             mOperand[i] = *value;
    285             (*value)->addUser(this);
    286         }
    287     }
    288 
    289262protected:   
    290     unsigned        mOperands;
    291     PabloAST **     mOperand;
    292     Statement *     mNext;
    293     Statement *     mPrev;
    294     const String *  mName;
    295     PabloBlock *    mParent;
     263    unsigned                mOperands;
     264    PabloAST **             mOperand;
     265    Statement *             mNext;
     266    Statement *             mPrev;
     267    mutable const String *  mName;
     268    PabloBlock *            mParent;
    296269};
    297270
     
    346319    }
    347320
    348     explicit CarryProducingStatement(const ClassTypeId id, llvm::Type * const type, const unsigned reserved, const String * name, Allocator & allocator)
    349     : Statement(id, type, reserved, name, allocator)
    350     , mCarryGroup(0)
    351     , mCarryWidth(0) {
    352 
    353     }
    354 
    355     template<typename iterator>
    356     explicit CarryProducingStatement(const ClassTypeId id, llvm::Type * const type, iterator begin, iterator end, const String * name, Allocator & allocator)
    357     : Statement(id, type, begin, end, name, allocator)
    358     , mCarryGroup(0)
    359     , mCarryWidth(0) {
    360 
    361     }
    362 
    363321private:
    364322
    365323    unsigned mCarryGroup;
    366324    unsigned mCarryWidth;
    367 };
    368 
    369 
    370 class Variadic : public Statement {
    371 public:
    372 
    373     static inline bool classof(const PabloAST * e) {
    374         switch (e->getClassTypeId()) {
    375             case PabloAST::ClassTypeId::And:
    376             case PabloAST::ClassTypeId::Or:
    377             case PabloAST::ClassTypeId::Xor:
    378                 return true;
    379             default: return false;
    380         }
    381     }
    382     static inline bool classof(const Variadic *) {
    383         return true;
    384     }
    385     static inline bool classof(const void *) {
    386         return false;
    387     }
    388 
    389     class iterator : public boost::iterator_facade<iterator, PabloAST *, boost::random_access_traversal_tag> {
    390         friend class Variadic;
    391         friend class boost::iterator_core_access;
    392     protected:
    393 
    394         iterator(PabloAST ** pointer) : mCurrent(pointer) { }
    395         inline void increment() { ++mCurrent; }
    396         inline void decrement() { --mCurrent; }
    397         inline void advance(const std::ptrdiff_t n) { mCurrent += n; }
    398         inline std::ptrdiff_t distance_to(const iterator & other) const { return other.mCurrent - mCurrent; }
    399         inline PabloAST *& dereference() const { return *mCurrent; }
    400 
    401         inline bool equal(const iterator & other) const { return (mCurrent == other.mCurrent); }
    402 
    403     private:
    404         PabloAST **        mCurrent;
    405     };
    406 
    407     using const_iterator = iterator;
    408 
    409     void addOperand(PabloAST * const expr) noexcept;
    410 
    411     PabloAST * removeOperand(const unsigned index) noexcept;
    412 
    413     bool deleteOperand(const PabloAST * const expr) noexcept;
    414 
    415     iterator begin() {
    416         return iterator(mOperand);
    417     }
    418 
    419     const_iterator begin() const {
    420         return iterator(mOperand);
    421     }
    422 
    423     iterator end() {
    424         return iterator(mOperand + mOperands);
    425     }
    426 
    427     const_iterator end() const {
    428         return iterator(mOperand + mOperands);
    429     }
    430 
    431     virtual ~Variadic() = default;
    432 
    433 protected:
    434     explicit Variadic(const ClassTypeId id, llvm::Type * const type, std::initializer_list<PabloAST *> operands, const String * const name, Allocator & allocator)
    435     : Statement(id, type, operands, name, allocator)
    436     , mCapacity(operands.size())
    437     , mAllocator(allocator) {
    438 
    439     }
    440     explicit Variadic(const ClassTypeId id, llvm::Type * const type, const unsigned reserved, const String * name, Allocator & allocator)
    441     : Statement(id, type, reserved, name, allocator)
    442     , mCapacity(reserved)
    443     , mAllocator(allocator) {
    444 
    445     }
    446     template<typename iterator>
    447     explicit Variadic(const ClassTypeId id, llvm::Type * const type, iterator begin, iterator end, const String * name, Allocator & allocator)
    448     : Statement(id, type, begin, end, name, allocator)
    449     , mCapacity(std::distance(begin, end))
    450     , mAllocator(allocator) {
    451 
    452     }
    453 private:
    454     unsigned        mCapacity;
    455     Allocator &     mAllocator;
    456325};
    457326
     
    713582};
    714583
    715 /** ------------------------------------------------------------------------------------------------------------- *
    716  * @brief deleteOperand
    717  ** ------------------------------------------------------------------------------------------------------------- */
    718 inline bool Variadic::deleteOperand(const PabloAST * const expr) noexcept {
    719     for (unsigned i = 0; i != getNumOperands(); ++i) {
    720         if (LLVM_UNLIKELY(getOperand(i) == expr)) {
    721             removeOperand(i);
    722             return true;
    723         }
    724     }
    725     return false;
    726584}
    727585
    728 }
    729 
    730586#endif // PE_PabloAST_H
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r5832 r5834  
    423423        Value * value = nullptr;
    424424        if (isa<And>(stmt)) {
    425             value = compileExpression(b, stmt->getOperand(0));
    426             for (unsigned i = 1; i < stmt->getNumOperands(); ++i) {
    427                 value = b->simd_and(value, compileExpression(b, stmt->getOperand(1)));
    428             }
     425            Value * const op0 = compileExpression(b, stmt->getOperand(0));
     426            Value * const op1 = compileExpression(b, stmt->getOperand(1));
     427            value = b->simd_and(op0, op1);
    429428        } else if (isa<Or>(stmt)) {
    430             value = compileExpression(b, stmt->getOperand(0));
    431             for (unsigned i = 1; i < stmt->getNumOperands(); ++i) {
    432                 value = b->simd_or(value, compileExpression(b, stmt->getOperand(1)));
    433             }
     429            Value * const op0 = compileExpression(b, stmt->getOperand(0));
     430            Value * const op1 = compileExpression(b, stmt->getOperand(1));
     431            value = b->simd_or(op0, op1);
    434432        } else if (isa<Xor>(stmt)) {
    435             value = compileExpression(b, stmt->getOperand(0));
    436             for (unsigned i = 1; i < stmt->getNumOperands(); ++i) {
    437                 value = b->simd_xor(value, compileExpression(b, stmt->getOperand(1)));
    438             }
     433            Value * const op0 = compileExpression(b, stmt->getOperand(0));
     434            Value * const op1 = compileExpression(b, stmt->getOperand(1));
     435            value = b->simd_xor(op0, op1);
    439436        } else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
    440437            Value* ifMask = compileExpression(b, sel->getCondition());
  • icGREP/icgrep-devel/icgrep/pablo/pe_repeat.h

    r5829 r5834  
    1212public:
    1313    static bool classof(const PabloAST * e) {
    14         return e->getClassTypeId() == ClassTypeId::Fill;
     14        return e->getClassTypeId() == ClassTypeId::Repeat;
    1515    }
    1616    static bool classof(const void *) {
     
    2727protected:
    2828    Repeat(Integer * const fieldWidth, PabloAST * const value, llvm::Type * type, const String * name, Allocator & allocator)
    29     : Statement(ClassTypeId::Fill, type, { fieldWidth, value }, name, allocator) {
     29    : Statement(ClassTypeId::Repeat, type, { fieldWidth, value }, name, allocator) {
    3030
    3131    }
  • icGREP/icgrep-devel/icgrep/pablo/printer_pablos.cpp

    r5828 r5834  
    5757        }
    5858    } else {
     59
    5960        print(cast<PabloAST>(stmt), out);
    6061
Note: See TracChangeset for help on using the changeset viewer.