Ignore:
Timestamp:
Jan 13, 2018, 11:57:43 AM (14 months ago)
Author:
nmedfort
Message:

Pablo support for byte comparisions; LineFeed? kernel processes byte streams directly. Some clean up of PabloBuilder? functionality.

File:
1 edited

Legend:

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

    r5821 r5828  
    77#include <pablo/pe_matchstar.h>
    88#include <pablo/pe_scanthru.h>
     9#include <pablo/pe_repeat.h>
    910#include <pablo/pe_infile.h>
    1011#include <pablo/pe_count.h>
     
    1516#include <pablo/pe_var.h>
    1617#include <pablo/ps_assign.h>
     18#include <boost/preprocessor/variadic/elem.hpp>
    1719
    1820using namespace llvm;
    1921
    2022namespace pablo {
     23
     24#define GET(I, ...) \
     25    reinterpret_cast<decltype(BOOST_PP_VARIADIC_ELEM(I, __VA_ARGS__))>(arg##I)
    2126
    2227#define MAKE_UNARY(NAME, TYPE, ARGS...) \
    2328struct __##NAME { \
    24     inline PabloAST * operator()(PabloAST * arg) { \
    25         return mPb->NAME(arg); \
     29    inline PabloAST * operator()(void * const arg0) { \
     30        return mPb->NAME(GET(0, ARGS)); \
    2631    } \
    27     inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
     32    inline __##NAME(PabloBlock * const pb) : mPb(pb) {} \
    2833private: \
    29     PabloBlock * mPb; \
     34    PabloBlock * const mPb; \
    3035}; \
    3136__##NAME functor(mPb); \
     
    3439#define MAKE_NAMED_UNARY(NAME, TYPE, PREFIX, ARGS...) \
    3540struct __##NAME { \
    36     inline PabloAST * operator()(PabloAST * arg) { \
    37         return mPb->NAME(arg, mPrefix); \
     41    inline PabloAST * operator()(void * const arg0) { \
     42        return mPb->NAME(GET(0, ARGS), mPrefix); \
    3843    } \
    39     inline __##NAME(PabloBlock * pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
     44    inline __##NAME(PabloBlock * const pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
    4045private: \
    41     PabloBlock * mPb; \
     46    PabloBlock * const mPb; \
    4247    const llvm::StringRef & mPrefix; \
    4348}; \
     
    4752#define MAKE_BINARY(NAME, TYPE, ARGS...) \
    4853struct __##NAME { \
    49     inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2) { \
    50         return mPb->NAME(arg1, arg2); \
     54    inline PabloAST * operator()(void * const arg0, void * const arg1) { \
     55        return mPb->NAME(GET(0, ARGS), GET(1, ARGS)); \
    5156    } \
    52     inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
     57    inline __##NAME(PabloBlock * const pb) : mPb(pb) {} \
    5358private: \
    54     PabloBlock * mPb; \
     59    PabloBlock * const mPb; \
    5560}; \
    5661__##NAME functor(mPb); \
     
    5964#define MAKE_NAMED_BINARY(NAME, TYPE, PREFIX, ARGS...) \
    6065struct __##NAME { \
    61     inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2) { \
    62         return mPb->NAME(arg1, arg2, mPrefix); \
     66    inline PabloAST * operator()(void * const arg0, void * const arg1) { \
     67        return mPb->NAME(GET(0, ARGS), GET(1, ARGS), mPrefix); \
    6368    } \
    64     inline __##NAME(PabloBlock * pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
     69    inline __##NAME(PabloBlock * const pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
    6570private: \
    66     PabloBlock * mPb; \
     71    PabloBlock * const mPb; \
    6772    const llvm::StringRef & mPrefix; \
    6873}; \
     
    7277#define MAKE_TERNARY(NAME, TYPE, ARGS...) \
    7378struct __##NAME { \
    74     inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3) { \
    75         return mPb->NAME(arg1, arg2, arg3); \
     79    inline PabloAST * operator()(void * const arg0, void * const arg1, void * const arg2) { \
     80        return mPb->NAME(GET(0, ARGS), GET(1, ARGS), GET(2, ARGS)); \
    7681    } \
    77     inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
     82    inline __##NAME(PabloBlock * const pb) : mPb(pb) {} \
    7883private: \
    79     PabloBlock * mPb; \
     84    PabloBlock * const mPb; \
    8085}; \
    8186__##NAME functor(mPb); \
     
    8489#define MAKE_NAMED_TERNARY(NAME, TYPE, PREFIX, ARGS...) \
    8590struct __##NAME { \
    86     inline PabloAST * operator()(PabloAST * arg1, PabloAST * arg2, PabloAST * arg3) { \
    87         return mPb->NAME(arg1, arg2, arg3, mPrefix); \
     91    inline PabloAST * operator()(void * const arg0, void * const arg1, void * const arg2) { \
     92        return mPb->NAME(GET(0, ARGS), GET(1, ARGS), GET(2, ARGS), mPrefix); \
    8893    } \
    89     inline __##NAME(PabloBlock * pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
     94    inline __##NAME(PabloBlock * const pb, const llvm::StringRef & prefix) : mPb(pb), mPrefix(prefix) {} \
    9095private: \
    91     PabloBlock * mPb; \
     96    PabloBlock * const mPb; \
    9297    const llvm::StringRef & mPrefix; \
    9398}; \
     
    95100PabloAST * result = mExprTable.findTernaryOrCall(std::move(functor), TYPE, ARGS)
    96101
    97 #define MAKE_VARIABLE(NAME, TYPE, ARGS...) \
    98 struct __##NAME { \
    99     inline PabloAST * operator()(const std::vector<PabloAST *> & args, PabloAST * prototype) { \
    100         return mPb->NAME(prototype, args); \
    101     } \
    102     inline __##NAME(PabloBlock * pb) : mPb(pb) {} \
    103 private: \
    104     PabloBlock * mPb; \
    105 }; \
    106 __##NAME functor(mPb); \
    107 PabloAST * result = mExprTable.findVariadicOrCall(std::move(functor), TYPE, ARGS)
    108 
    109 template<typename Type>
    110 static inline Type * isBinary(PabloAST * expr) {
    111     if (isa<Type>(expr) && cast<Type>(expr)->getNumOperands() == 2) {
    112         return cast<Type>(expr);
    113     }
    114     return nullptr;
    115 }
    116 
    117102using TypeId = PabloAST::ClassTypeId;
    118103
    119 PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount) {
    120     if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0) {
     104PabloAST * PabloBuilder::createAdvance(PabloAST * expr, not_null<Integer *> shiftAmount) {
     105    if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0) {
    121106        return expr;
    122107    }
    123     MAKE_BINARY(createAdvance, TypeId::Advance, expr, shiftAmount);
    124     return result;
    125 }
    126 
    127 PabloAST * PabloBuilder::createAdvance(PabloAST * expr, PabloAST * shiftAmount, const llvm::StringRef & prefix) {
    128     if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0) {
     108    MAKE_BINARY(createAdvance, TypeId::Advance, expr, shiftAmount.get());
     109    return result;
     110}
     111
     112PabloAST * PabloBuilder::createAdvance(PabloAST * expr, not_null<Integer *> shiftAmount, const llvm::StringRef & prefix) {
     113    if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0) {
    129114        return expr;
    130115    }
    131     MAKE_NAMED_BINARY(createAdvance, TypeId::Advance, prefix, expr, shiftAmount);
    132     return result;
    133 }
    134 
    135 PabloAST * PabloBuilder::createIndexedAdvance(PabloAST * expr, PabloAST * indexStream, PabloAST * shiftAmount) {
    136     if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0) {
     116    MAKE_NAMED_BINARY(createAdvance, TypeId::Advance, prefix, expr, shiftAmount.get());
     117    return result;
     118}
     119
     120PabloAST * PabloBuilder::createIndexedAdvance(PabloAST * expr, PabloAST * indexStream, not_null<Integer *> shiftAmount) {
     121    if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0) {
    137122        return expr;
    138123    }
     
    140125        return createAdvance(expr, shiftAmount);
    141126    }
    142     else if (cast<Integer>(shiftAmount)->value() == 1) {
     127    else if (cast<Integer>(shiftAmount.get())->value() == 1) {
    143128        return createAdvanceThenScanTo(createAnd(expr, indexStream), indexStream);
    144129    }
    145     MAKE_TERNARY(createIndexedAdvance, TypeId::IndexedAdvance, expr, indexStream, shiftAmount);
    146     return result;
    147 }
    148 
    149 PabloAST * PabloBuilder::createIndexedAdvance(PabloAST * expr, PabloAST * indexStream, PabloAST * shiftAmount, const llvm::StringRef & prefix) {
    150     if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0) {
     130    MAKE_TERNARY(createIndexedAdvance, TypeId::IndexedAdvance, expr, indexStream, shiftAmount.get());
     131    return result;
     132}
     133
     134PabloAST * PabloBuilder::createIndexedAdvance(PabloAST * expr, PabloAST * indexStream, not_null<Integer *> shiftAmount, const llvm::StringRef & prefix) {
     135    if (isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0) {
    151136        return expr;
    152137    }
     
    154139        return createAdvance(expr, shiftAmount, prefix);
    155140    }
    156     else if (cast<Integer>(shiftAmount)->value() == 1) {
     141    else if (cast<Integer>(shiftAmount.get())->value() == 1) {
    157142        return createAdvanceThenScanTo(expr, indexStream, prefix);
    158143    }
    159     MAKE_NAMED_TERNARY(createIndexedAdvance, TypeId::IndexedAdvance, prefix, expr, indexStream, shiftAmount);
     144    MAKE_NAMED_TERNARY(createIndexedAdvance, TypeId::IndexedAdvance, prefix, expr, indexStream, shiftAmount.get());
    160145    return result;
    161146}
    162147   
    163 Extract * PabloBuilder::createExtract(PabloAST * value, not_null<PabloAST *> index) {
    164     MAKE_BINARY(createExtract, TypeId::Extract, value, index);
     148Extract * PabloBuilder::createExtract(Var * array, not_null<Integer *> index) {
     149    MAKE_BINARY(createExtract, TypeId::Extract, array, index.get());
    165150    return cast<Extract>(result);
    166151}
    167152
    168 Extract * PabloBuilder::createExtract(PabloAST * value, not_null<PabloAST *> index, const llvm::StringRef & prefix) {
    169     MAKE_NAMED_BINARY(createExtract, TypeId::Extract, prefix, value, index);
    170     return cast<Extract>(result);
    171 }
    172 
    173 PabloAST * PabloBuilder::createLookahead(PabloAST * expr, PabloAST * shiftAmount) {
    174     if (LLVM_UNLIKELY(isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0)) {
     153PabloAST * PabloBuilder::createLookahead(PabloAST * expr, not_null<Integer *> shiftAmount) {
     154    if (LLVM_UNLIKELY(isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0)) {
    175155        return expr;
    176156    }
    177     MAKE_BINARY(createLookahead, TypeId::Lookahead, expr, shiftAmount);
    178     return result;
    179 }
    180 
    181 PabloAST * PabloBuilder::createLookahead(PabloAST * expr, PabloAST * shiftAmount, const llvm::StringRef & prefix) {
    182     if (LLVM_UNLIKELY(isa<Zeroes>(expr) || cast<Integer>(shiftAmount)->value() == 0)) {
     157    MAKE_BINARY(createLookahead, TypeId::Lookahead, expr, shiftAmount.get());
     158    return result;
     159}
     160
     161PabloAST * PabloBuilder::createLookahead(PabloAST * expr, not_null<Integer *> shiftAmount, const llvm::StringRef & prefix) {
     162    if (LLVM_UNLIKELY(isa<Zeroes>(expr) || cast<Integer>(shiftAmount.get())->value() == 0)) {
    183163        return expr;
    184164    }
    185     MAKE_NAMED_BINARY(createLookahead, TypeId::Lookahead, prefix, expr, shiftAmount);
     165    MAKE_NAMED_BINARY(createLookahead, TypeId::Lookahead, prefix, expr, shiftAmount.get());
    186166    return result;
    187167}
    188168
    189169PabloAST * PabloBuilder::createNot(PabloAST * expr) {
    190     if (isa<Ones>(expr)) {
     170    if (LLVM_UNLIKELY(isa<Ones>(expr))) {
    191171        return createZeroes(expr->getType());
    192172    }
    193     else if (isa<Zeroes>(expr)){
     173    else if (LLVM_UNLIKELY(isa<Zeroes>(expr))){
    194174        return createOnes(expr->getType());
    195175    }
     
    202182
    203183PabloAST * PabloBuilder::createNot(PabloAST * expr, const llvm::StringRef & prefix) {
    204     if (isa<Ones>(expr)) {
     184    if (LLVM_UNLIKELY(isa<Ones>(expr))) {
    205185        return createZeroes(expr->getType());
    206186    }
    207     else if (isa<Zeroes>(expr)){
     187    else if (LLVM_UNLIKELY(isa<Zeroes>(expr))){
    208188        return createOnes(expr->getType());
    209189    }
     
    225205}
    226206
    227 PabloAST * PabloBuilder::createAssign(PabloAST * const variable, PabloAST * const value) {
    228     return mPb->createAssign(variable, value);
     207PabloAST * PabloBuilder::createRepeat(not_null<Integer *> fieldWidth, PabloAST * value) {
     208    MAKE_BINARY(createRepeat, TypeId::Fill, fieldWidth.get(), value);
     209    return result;
     210}
     211
     212PabloAST * PabloBuilder::createRepeat(not_null<Integer *> fieldWidth, PabloAST * value, const llvm::StringRef & prefix) {
     213    MAKE_NAMED_BINARY(createRepeat, TypeId::Fill, prefix, fieldWidth.get(), value);
     214    return result;
    229215}
    230216
     
    244230            return createZeroes(expr1->getType());
    245231        }
    246     } else if (Or * or1 = isBinary<Or>(expr1)) {
     232    } else if (Or * or1 = dyn_cast<Or>(expr1)) {
    247233        if (equals(or1->getOperand(0), expr2) || equals(or1->getOperand(1), expr2)) {
    248234            return expr2;
    249235        }
    250     } else if (Or * or2 = isBinary<Or>(expr2)) {
     236    } else if (Or * or2 = dyn_cast<Or>(expr2)) {
    251237        if (equals(or2->getOperand(0), expr1) || equals(or2->getOperand(1), expr1)) {
    252238            return expr1;
     
    275261            return createZeroes(expr1->getType());
    276262        }
    277     } else if (Or * or1 = isBinary<Or>(expr1)) {
     263    } else if (Or * or1 = dyn_cast<Or>(expr1)) {
    278264        if (equals(or1->getOperand(0), expr2) || equals(or1->getOperand(1), expr2)) {
    279265            return expr2;
    280266        }
    281     } else if (Or * or2 = isBinary<Or>(expr2)) {
     267    } else if (Or * or2 = dyn_cast<Or>(expr2)) {
    282268        if (equals(or2->getOperand(0), expr1) || equals(or2->getOperand(1), expr1)) {
    283269            return expr1;
     
    305291    } else if (equals(expr1, expr2)) {
    306292        return expr1;
    307     } else if (And * and1 = isBinary<And>(expr1)) {
     293    } else if (And * and1 = dyn_cast<And>(expr1)) {
    308294        PabloAST * const expr1a = and1->getOperand(0);
    309295        PabloAST * const expr1b = and1->getOperand(1);
    310         if (And * and2 = isBinary<And>(expr2)) {
     296        if (And * and2 = dyn_cast<And>(expr2)) {
    311297            PabloAST * const expr2a = and2->getOperand(0);
    312298            PabloAST * const expr2b = and2->getOperand(1);
     
    326312            return expr2;
    327313        }
    328     } else if (And * and2 = isBinary<And>(expr2)) {
     314    } else if (And * and2 = dyn_cast<And>(expr2)) {
    329315        if (equals(and2->getOperand(0), expr1) || equals(and2->getOperand(1), expr1)) {
    330316            return expr1;
     
    352338    } else if (equals(expr1, expr2)) {
    353339        return expr1;
    354     } else if (And * and1 = isBinary<And>(expr1)) {
     340    } else if (And * and1 = dyn_cast<And>(expr1)) {
    355341        PabloAST * const expr1a = and1->getOperand(0);
    356342        PabloAST * const expr1b = and1->getOperand(1);
    357         if (And * and2 = isBinary<And>(expr2)) {
     343        if (And * and2 = dyn_cast<And>(expr2)) {
    358344            PabloAST * const expr2a = and2->getOperand(0);
    359345            PabloAST * const expr2b = and2->getOperand(1);
     
    373359            return expr2;
    374360        }
    375     } else if (And * and2 = isBinary<And>(expr2)) {
     361    } else if (And * and2 = dyn_cast<And>(expr2)) {
    376362        if (equals(and2->getOperand(0), expr1) || equals(and2->getOperand(1), expr1)) {
    377363            return expr1;
     
    468454    }
    469455    MAKE_BINARY(createLessThan, TypeId::LessThan, expr1, expr2);
     456    return result;
     457}
     458
     459PabloAST * PabloBuilder::createEquals(PabloAST * expr1, PabloAST * expr2) {
     460    if (isa<Integer>(expr1) && isa<Integer>(expr2)) {
     461        return getInteger(cast<Integer>(expr1)->value() == cast<Integer>(expr2)->value() ? 1 : 0);
     462    }
     463    MAKE_BINARY(createEquals, TypeId::Equals, expr1, expr2);
    470464    return result;
    471465}
Note: See TracChangeset for help on using the changeset viewer.