Changeset 4628 for icGREP


Ignore:
Timestamp:
Jul 2, 2015, 8:57:52 AM (4 years ago)
Author:
cameron
Message:

Use a single IRBuilder, calling SetInsertPoint? as appropriate

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

Legend:

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

    r4595 r4628  
    7171, mMod(mModOwner.get())
    7272#endif
    73 , mBasicBlock(nullptr)
     73, mBuilder(&LLVM_Builder)
    7474, mExecutionEngine(nullptr)
    7575, mBitBlockType(VectorType::get(IntegerType::get(mMod->getContext(), 64), BLOCK_SIZE / 64))
     
    105105
    106106void PabloCompiler::genPrintRegister(std::string regName, Value * bitblockValue) {
    107     IRBuilder <> b(mBasicBlock);
    108107    Constant * regNameData = ConstantDataArray::getString(mMod->getContext(), regName);
    109108    GlobalVariable *regStrVar = new GlobalVariable(*mMod,
     
    112111                                                   /*Linkage=*/ GlobalValue::PrivateLinkage,
    113112                                                   /*Initializer=*/ regNameData);
    114     Value * regStrPtr = b.CreateGEP(regStrVar, {b.getInt64(0), b.getInt32(0)});
    115     b.CreateCall(mPrintRegisterFunction, {regStrPtr, bitblockValue});
     113    Value * regStrPtr = mBuilder->CreateGEP(regStrVar, {mBuilder->getInt64(0), mBuilder->getInt32(0)});
     114    mBuilder->CreateCall(mPrintRegisterFunction, {regStrPtr, bitblockValue});
    116115}
    117116
     
    158157    mIfDepth = 0;
    159158    mMaxWhileDepth = 0;
    160     mBasicBlock = BasicBlock::Create(mMod->getContext(), "parabix_entry", mFunction,0);
    161     IRBuilder<> b(mBasicBlock);
     159    BasicBlock * b = BasicBlock::Create(mMod->getContext(), "parabix_entry", mFunction,0);
     160    mBuilder->SetInsertPoint(b);
    162161
    163162    //The basis bits structure
    164163    for (unsigned i = 0; i != mBasisBits.size(); ++i) {
    165         Value* indices[] = {b.getInt64(0), b.getInt32(i)};
    166         Value * gep = b.CreateGEP(mBasisBitsAddr, indices);
    167         LoadInst * basisBit = b.CreateAlignedLoad(gep, BLOCK_SIZE/8, false, mBasisBits[i]->getName()->to_string());
     164        Value* indices[] = {mBuilder->getInt64(0), mBuilder->getInt32(i)};
     165        Value * gep = mBuilder->CreateGEP(mBasisBitsAddr, indices);
     166        LoadInst * basisBit = mBuilder->CreateAlignedLoad(gep, BLOCK_SIZE/8, false, mBasisBits[i]->getName()->to_string());
    168167        mMarkerMap.insert(std::make_pair(mBasisBits[i], basisBit));
    169168    }
    170169   
    171170    // The block number is a 64-bit integer at the end of the carry data area.
    172     Value * blockNoPtr = b.CreateBitCast(b.CreateGEP(mCarryDataPtr, b.getInt64(totalCarryDataSize - 1)), Type::getInt64PtrTy(b.getContext()));
    173     mBlockNo = b.CreateLoad(blockNoPtr);
     171    Value * blockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(totalCarryDataSize - 1)), Type::getInt64PtrTy(mBuilder->getContext()));
     172    mBlockNo = mBuilder->CreateLoad(blockNoPtr);
    174173    //Generate the IR instructions for the function.
    175174    compileBlock(pb);
    176     {   IRBuilder<> b(mBasicBlock);  // may be in new basic block, set builder
    177         b.CreateStore(b.CreateAdd(mBlockNo, b.getInt64(1)), blockNoPtr);
    178     }
     175   
     176    mBuilder->CreateStore(mBuilder->CreateAdd(mBlockNo, mBuilder->getInt64(1)), blockNoPtr);
    179177
    180178    if (DumpTrace || TraceNext) {
     
    186184
    187185    //Terminate the block
    188     ReturnInst::Create(mMod->getContext(), mBasicBlock);
     186    ReturnInst::Create(mMod->getContext(), mBuilder->GetInsertBlock());
    189187
    190188    //Display the IR that has been generated by this module.
     
    251249    mPrintRegisterFunction = mMod->getOrInsertFunction("wrapped_print_register", Type::getVoidTy(getGlobalContext()), Type::getInt8PtrTy(getGlobalContext()), mBitBlockType, NULL);
    252250    mExecutionEngine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
    253     // to call->  b.CreateCall(mFunc_print_register, unicode_category);
     251    // to call->  mBuilder->CreateCall(mFunc_print_register, unicode_category);
    254252
    255253#ifdef USE_UADD_OVERFLOW
     
    444442        //  body.
    445443        //
    446         BasicBlock * ifEntryBlock = mBasicBlock;  // The block we are in.
     444        BasicBlock * ifEntryBlock = mBuilder->GetInsertBlock();
    447445        BasicBlock * ifBodyBlock = BasicBlock::Create(mMod->getContext(), "if.body", mFunction, 0);
    448446        BasicBlock * ifEndBlock = BasicBlock::Create(mMod->getContext(), "if.end", mFunction, 0);
    449447       
    450         IRBuilder<> b_entry(ifEntryBlock);
    451         mBasicBlock = ifEntryBlock;
    452448        const PabloBlockCarryData & cd = ifStatement -> getBody().carryData;
    453449   
     
    459455            // load the summary variable
    460456            Value* last_if_pending_data = genCarryDataLoad(carrySummaryIndex);
    461             if_test_value = b_entry.CreateOr(if_test_value, last_if_pending_data);
    462         }
    463         b_entry.CreateCondBr(genBitBlockAny(if_test_value), ifEndBlock, ifBodyBlock);
     457            if_test_value = mBuilder->CreateOr(if_test_value, last_if_pending_data);
     458        }
     459        mBuilder->CreateCondBr(genBitBlockAny(if_test_value), ifEndBlock, ifBodyBlock);
    464460
    465461        // Entry processing is complete, now handle the body of the if.
    466         mBasicBlock = ifBodyBlock;
     462        mBuilder->SetInsertPoint(ifBodyBlock);
    467463        compileBlock(ifStatement -> getBody());
    468 
    469         // If we compiled an If or a While statement, we won't be in the same basic block as before.
    470         // Create the branch from the current basic block to the end block.
    471         IRBuilder<> bIfBody(mBasicBlock);
    472         // After the recursive compile, now insert the code to compute the summary
    473         // carry over variable.
    474        
     464   
    475465        if (cd.explicitSummaryRequired()) {
    476466            // If there was only one carry entry, then it also serves as the summary variable.
     
    485475                    }
    486476                    else {
    487                         carry_summary = bIfBody.CreateOr(carry_summary, carryq_value);
     477                        carry_summary = mBuilder->CreateOr(carry_summary, carryq_value);
    488478                    }
    489479                    mCarryDataSummaryIdx[c] = carrySummaryIndex;
     
    492482            genCarryDataStore(carry_summary, carrySummaryIndex);
    493483        }
    494         bIfBody.CreateBr(ifEndBlock);
     484        BasicBlock * ifBodyFinalBlock = mBuilder->GetInsertBlock();
     485        mBuilder->CreateBr(ifEndBlock);
    495486        //End Block
    496         IRBuilder<> bEnd(ifEndBlock);
     487        mBuilder->SetInsertPoint(ifEndBlock);
    497488        for (const PabloAST * node : ifStatement->getDefined()) {
    498489            const Assign * assign = cast<Assign>(node);
    499             PHINode * phi = bEnd.CreatePHI(mBitBlockType, 2, assign->getName()->value());
     490            PHINode * phi = mBuilder->CreatePHI(mBitBlockType, 2, assign->getName()->value());
    500491            auto f = mMarkerMap.find(assign);
    501492            assert (f != mMarkerMap.end());
    502493            phi->addIncoming(mZeroInitializer, ifEntryBlock);
    503             phi->addIncoming(f->second, mBasicBlock);
     494            phi->addIncoming(f->second, ifBodyFinalBlock);
    504495            mMarkerMap[assign] = phi;
    505496        }
    506497        // Create the phi Node for the summary variable, if needed.
    507498        if (cd.summaryNeededInParentBlock()) {
    508             PHINode * summary_phi = bEnd.CreatePHI(mBitBlockType, 2, "summary");
     499            PHINode * summary_phi = mBuilder->CreatePHI(mBitBlockType, 2, "summary");
    509500            summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
    510             summary_phi->addIncoming(mCarryOutVector[carrySummaryIndex], mBasicBlock);
     501            summary_phi->addIncoming(mCarryOutVector[carrySummaryIndex], ifBodyFinalBlock);
    511502            mCarryOutVector[carrySummaryIndex] = summary_phi;
    512503        }
    513504       
    514         // Set the basic block to the new end block
    515         mBasicBlock = ifEndBlock;
    516505}
    517506
     
    523512
    524513void PabloCompiler::compileWhile(const While * whileStatement) {
    525         BasicBlock* whileEntryBlock = mBasicBlock;
    526         BasicBlock* whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
    527         BasicBlock* whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
     514        //BasicBlock* whileEntryBlock = mBasicBlock;
     515        BasicBlock * whileEntryBlock = mBuilder->GetInsertBlock();
     516        BasicBlock * whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
     517        BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
    528518   
    529519   
     
    539529        {
    540530            for (auto i = baseCarryDataIdx; i < baseCarryDataIdx + carryDataSize; ++i) {
    541                 IRBuilder<> b(mBasicBlock);
    542                 mCarryInVector[i] = b.CreateAlignedLoad(b.CreateGEP(mCarryDataPtr, b.getInt64(i)), BLOCK_SIZE/8, false);
     531                mCarryInVector[i] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(i)), BLOCK_SIZE/8, false);
    543532            }
    544533        }
     
    555544        // the loop.
    556545
    557         IRBuilder<> bEntry(mBasicBlock);
    558         // Jump to the while body block immediately.
    559         bEntry.CreateBr(whileBodyBlock);
    560         mBasicBlock = whileBodyBlock;
    561         IRBuilder<> bBody(whileBodyBlock);
     546        mBuilder->CreateBr(whileBodyBlock);
     547        mBuilder->SetInsertPoint(whileBodyBlock);
    562548   
    563549        //
     
    575561        for (unsigned index = 0; index < carryDataSize; ++index) {
    576562#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
    577             PHINode * phi_in = bBody.CreatePHI(mBitBlockType, 2);
     563            PHINode * phi_in = mBuilder->CreatePHI(mBitBlockType, 2);
    578564            phi_in->addIncoming(mCarryInVector[baseCarryDataIdx + index], whileEntryBlock);
    579565            carryInPhis[index] = phi_in;
    580566            mCarryInVector[baseCarryDataIdx + index] = phi_in;
    581567#endif
    582             PHINode * phi_out = bBody.CreatePHI(mBitBlockType, 2);
     568            PHINode * phi_out = mBuilder->CreatePHI(mBitBlockType, 2);
    583569            phi_out->addIncoming(mZeroInitializer, whileEntryBlock);
    584570            carryOutAccumPhis[index] = phi_out;
     
    588574        // for any Next nodes in the loop body, initialize to (a) pre-loop value.
    589575        for (const Next * n : nextNodes) {
    590             PHINode * phi = bBody.CreatePHI(mBitBlockType, 2, n->getName()->value());
     576            PHINode * phi = mBuilder->CreatePHI(mBitBlockType, 2, n->getName()->value());
    591577            auto f = mMarkerMap.find(n->getInitial());
    592578            assert (f != mMarkerMap.end());
     
    602588        compileBlock(whileStatement->getBody());
    603589   
    604         //  The while body might involve separate blocks depending on compile;
    605         //  identify the final block generated.
    606         BasicBlock * whileBodyFinalBlock = mBasicBlock;
    607         IRBuilder<> bBodyFinal(mBasicBlock);
    608    
     590        BasicBlock * whileBodyFinalBlock = mBuilder->GetInsertBlock();
    609591        // Add the phiNode branches for carry in, carry out nodes.
    610592        for (unsigned index = 0; index < carryDataSize; ++index) {
     
    613595#endif
    614596            PHINode * phi = carryOutAccumPhis[index];
    615             Value * carryOut = bBodyFinal.CreateOr(phi, mCarryOutVector[baseCarryDataIdx + index]);
     597            Value * carryOut = mBuilder->CreateOr(phi, mCarryOutVector[baseCarryDataIdx + index]);
    616598            phi->addIncoming(carryOut, whileBodyFinalBlock);
    617599            mCarryOutVector[baseCarryDataIdx + index] = carryOut;
     
    619601
    620602        // Terminate the while loop body with a conditional branch back.
    621         bBodyFinal.CreateCondBr(genBitBlockAny(compileExpression(whileStatement->getCondition())), whileEndBlock, whileBodyBlock);
     603        mBuilder->CreateCondBr(genBitBlockAny(compileExpression(whileStatement->getCondition())), whileEndBlock, whileBodyBlock);
    622604
    623605        // and for any Next nodes in the loop body
     
    635617
    636618        // EXIT BLOCK
    637         mBasicBlock = whileEndBlock;
    638         IRBuilder<> bEnd(whileEndBlock);
     619        mBuilder->SetInsertPoint(whileEndBlock);
    639620        --mWhileDepth;
    640621
     
    644625        {
    645626            for (unsigned index = baseCarryDataIdx; index < baseCarryDataIdx + carryDataSize; ++index) {
    646                 bEnd.CreateAlignedStore(mCarryOutVector[index], bEnd.CreateGEP(mCarryDataPtr, bEnd.getInt64(index)), BLOCK_SIZE/8, false);
     627                mBuilder->CreateAlignedStore(mCarryOutVector[index], mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(index)), BLOCK_SIZE/8, false);
    647628            }
    648629        }
     
    651632void PabloCompiler::compileStatement(const Statement * stmt)
    652633{
    653     IRBuilder<> b(mBasicBlock);
    654634    if (const Assign * assign = dyn_cast<const Assign>(stmt)) {
    655635        Value * expr = compileExpression(assign->getExpr());
     
    685665                throw std::runtime_error("Unexpected error locating static function for \"" + call->getCallee()->to_string() + "\"");
    686666            }
    687             mi = mMarkerMap.insert(std::make_pair(call, b.CreateCall(ci->second, mBasisBitsAddr))).first;
     667            mi = mMarkerMap.insert(std::make_pair(call, mBuilder->CreateCall(ci->second, mBasisBitsAddr))).first;
    688668        }
    689669        // return mi->second;
    690670    }
    691671    else if (const And * pablo_and = dyn_cast<And>(stmt)) {
    692         Value * expr = b.CreateAnd(compileExpression(pablo_and->getExpr1()), compileExpression(pablo_and->getExpr2()), "and");
     672        Value * expr = mBuilder->CreateAnd(compileExpression(pablo_and->getExpr1()), compileExpression(pablo_and->getExpr2()), "and");
    693673        if (DumpTrace) {
    694674            genPrintRegister(stmt->getName()->to_string(), expr);
     
    698678    }
    699679    else if (const Or * pablo_or = dyn_cast<Or>(stmt)) {
    700         Value * expr = b.CreateOr(compileExpression(pablo_or->getExpr1()), compileExpression(pablo_or->getExpr2()), "or");
     680        Value * expr = mBuilder->CreateOr(compileExpression(pablo_or->getExpr1()), compileExpression(pablo_or->getExpr2()), "or");
    701681        if (DumpTrace) {
    702682            genPrintRegister(stmt->getName()->to_string(), expr);
     
    706686    }
    707687    else if (const Xor * pablo_xor = dyn_cast<Xor>(stmt)) {
    708         Value * expr = b.CreateXor(compileExpression(pablo_xor->getExpr1()), compileExpression(pablo_xor->getExpr2()), "xor");
     688        Value * expr = mBuilder->CreateXor(compileExpression(pablo_xor->getExpr1()), compileExpression(pablo_xor->getExpr2()), "xor");
    709689        mMarkerMap[pablo_xor] = expr;
    710690        // return expr;
     
    712692    else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
    713693        Value* ifMask = compileExpression(sel->getCondition());
    714         Value* ifTrue = b.CreateAnd(ifMask, compileExpression(sel->getTrueExpr()));
    715         Value* ifFalse = b.CreateAnd(genNot(ifMask), compileExpression(sel->getFalseExpr()));
    716         Value * expr = b.CreateOr(ifTrue, ifFalse);
     694        Value* ifTrue = mBuilder->CreateAnd(ifMask, compileExpression(sel->getTrueExpr()));
     695        Value* ifFalse = mBuilder->CreateAnd(genNot(ifMask), compileExpression(sel->getFalseExpr()));
     696        Value * expr = mBuilder->CreateOr(ifTrue, ifFalse);
    717697        if (DumpTrace) {
    718698            genPrintRegister(stmt->getName()->to_string(), expr);
     
    744724        Value * marker = compileExpression(mstar->getMarker());
    745725        Value * cc = compileExpression(mstar->getCharClass());
    746         Value * marker_and_cc = b.CreateAnd(marker, cc);
     726        Value * marker_and_cc = mBuilder->CreateAnd(marker, cc);
    747727        unsigned carry_index = mstar->getLocalCarryIndex();
    748         Value * expr = b.CreateOr(b.CreateXor(genAddWithCarry(marker_and_cc, cc, carry_index, stmt->getParent()), cc), marker, "matchstar");
     728        Value * expr = mBuilder->CreateOr(mBuilder->CreateXor(genAddWithCarry(marker_and_cc, cc, carry_index, stmt->getParent()), cc), marker, "matchstar");
    749729        if (DumpTrace) {
    750730            genPrintRegister(stmt->getName()->to_string(), expr);
     
    758738        Value * cc_expr = compileExpression(sthru->getScanThru());
    759739        unsigned carry_index = sthru->getLocalCarryIndex();
    760         Value * expr = b.CreateAnd(genAddWithCarry(marker_expr, cc_expr, carry_index, stmt->getParent()), genNot(cc_expr), "scanthru");
     740        Value * expr = mBuilder->CreateAnd(genAddWithCarry(marker_expr, cc_expr, carry_index, stmt->getParent()), genNot(cc_expr), "scanthru");
    761741        if (DumpTrace) {
    762742            genPrintRegister(stmt->getName()->to_string(), expr);
     
    848828
    849829Value* PabloCompiler::genAddWithCarry(Value* e1, Value* e2, unsigned localIndex, const PabloBlock * blk) {
    850     IRBuilder<> b(mBasicBlock);
    851830    const PabloBlockCarryData & cd = blk->carryData;
    852831    const unsigned carryIdx = cd.carryOpCarryDataOffset(localIndex);
     
    855834    //This is the ideal implementation, which uses two uadd.with.overflow
    856835    //The back end should be able to recognize this pattern and combine it into uadd.with.overflow.carryin
    857     CastInst* int128_e1 = new BitCastInst(e1, b.getIntNTy(BLOCK_SIZE), "e1_128", mBasicBlock);
    858     CastInst* int128_e2 = new BitCastInst(e2, b.getIntNTy(BLOCK_SIZE), "e2_128", mBasicBlock);
    859     CastInst* int128_carryq_value = new BitCastInst(carryq_value, b.getIntNTy(BLOCK_SIZE), "carryq_128", mBasicBlock);
     836    CastInst* int128_e1 = new BitCastInst(e1, mBuilder->getIntNTy(BLOCK_SIZE), "e1_128", mBasicBlock);
     837    CastInst* int128_e2 = new BitCastInst(e2, mBuilder->getIntNTy(BLOCK_SIZE), "e2_128", mBasicBlock);
     838    CastInst* int128_carryq_value = new BitCastInst(carryq_value, mBuilder->getIntNTy(BLOCK_SIZE), "carryq_128", mBasicBlock);
    860839
    861840    SumWithOverflowPack sumpack0, sumpack1;
     
    864843    sumpack1 = callUaddOverflow(sumpack0.sum, int128_carryq_value);
    865844
    866     Value* obit = b.CreateOr(sumpack0.obit, sumpack1.obit, "carry_bit");
    867     Value* sum = b.CreateBitCast(sumpack1.sum, mBitBlockType, "ret_sum");
     845    Value* obit = mBuilder->CreateOr(sumpack0.obit, sumpack1.obit, "carry_bit");
     846    Value* sum = mBuilder->CreateBitCast(sumpack1.sum, mBitBlockType, "ret_sum");
    868847
    869848    /*obit is the i1 carryout, zero extend and insert it into a v2i64 or v4i64 vector.*/
     
    875854#elif defined USE_UADD_OVERFLOW
    876855    //use llvm.uadd.with.overflow.i128 or i256
    877     CastInst* int128_e1 = new BitCastInst(e1, b.getIntNTy(BLOCK_SIZE), "e1_128", mBasicBlock);
    878     CastInst* int128_e2 = new BitCastInst(e2, b.getIntNTy(BLOCK_SIZE), "e2_128", mBasicBlock);
     856    CastInst* int128_e1 = new BitCastInst(e1, mBuilder->getIntNTy(BLOCK_SIZE), "e1_128", mBasicBlock);
     857    CastInst* int128_e2 = new BitCastInst(e2, mBuilder->getIntNTy(BLOCK_SIZE), "e2_128", mBasicBlock);
    879858
    880859    //get i1 carryin from iBLOCK_SIZE
     
    886865    sumpack0 = callUaddOverflow(int128_e1, int128_e2, int1_carryq_value);
    887866    Value* obit = sumpack0.obit;
    888     Value* sum = b.CreateBitCast(sumpack0.sum, mBitBlockType, "sum");
     867    Value* sum = mBuilder->CreateBitCast(sumpack0.sum, mBitBlockType, "sum");
    889868
    890869    /*obit is the i1 carryout, zero extend and insert it into a v2i64 or v4i64 vector.*/
     
    894873#elif (BLOCK_SIZE == 128)
    895874    //calculate carry through logical ops
    896     Value* carrygen = b.CreateAnd(e1, e2, "carrygen");
    897     Value* carryprop = b.CreateOr(e1, e2, "carryprop");
    898     Value* digitsum = b.CreateAdd(e1, e2, "digitsum");
    899     Value* partial = b.CreateAdd(digitsum, carryq_value, "partial");
    900     Value* digitcarry = b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(partial)));
    901     Value* mid_carry_in = genShiftLeft64(b.CreateLShr(digitcarry, 63), "mid_carry_in");
    902 
    903     Value* sum = b.CreateAdd(partial, mid_carry_in, "sum");
    904     Value* carry_out = genShiftHighbitToLow(BLOCK_SIZE, b.CreateOr(carrygen, b.CreateAnd(carryprop, genNot(sum))));
     875    Value* carrygen = mBuilder->CreateAnd(e1, e2, "carrygen");
     876    Value* carryprop = mBuilder->CreateOr(e1, e2, "carryprop");
     877    Value* digitsum = mBuilder->CreateAdd(e1, e2, "digitsum");
     878    Value* partial = mBuilder->CreateAdd(digitsum, carryq_value, "partial");
     879    Value* digitcarry = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, genNot(partial)));
     880    Value* mid_carry_in = genShiftLeft64(mBuilder->CreateLShr(digitcarry, 63), "mid_carry_in");
     881
     882    Value* sum = mBuilder->CreateAdd(partial, mid_carry_in, "sum");
     883    Value* carry_out = genShiftHighbitToLow(BLOCK_SIZE, mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, genNot(sum))));
    905884#else
    906885    //BLOCK_SIZE == 256, there is no other implementation
     
    915894    assert (index < mCarryInVector.size());
    916895    if (mWhileDepth == 0) {
    917         IRBuilder<> b(mBasicBlock);
    918         mCarryInVector[index] = b.CreateAlignedLoad(b.CreateGEP(mCarryDataPtr, b.getInt64(index)), BLOCK_SIZE/8, false);
     896        mCarryInVector[index] = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(index)), BLOCK_SIZE/8, false);
    919897    }
    920898#ifdef CARRY_DEBUG
     
    929907    assert (index < mCarryOutVector.size());
    930908    if (mWhileDepth == 0) {
    931         IRBuilder<> b(mBasicBlock);
    932         b.CreateAlignedStore(carryOut, b.CreateGEP(mCarryDataPtr, b.getInt64(index)), BLOCK_SIZE/8, false);
     909        mBuilder->CreateAlignedStore(carryOut, mBuilder->CreateGEP(mCarryDataPtr, mBuilder->getInt64(index)), BLOCK_SIZE/8, false);
    933910    }
    934911    mCarryDataSummaryIdx[index] = -1;
     
    942919
    943920inline Value* PabloCompiler::genBitBlockAny(Value* test) {
    944     IRBuilder<> b(mBasicBlock);
    945     Value* cast_marker_value_1 = b.CreateBitCast(test, b.getIntNTy(BLOCK_SIZE));
    946     return b.CreateICmpEQ(cast_marker_value_1, ConstantInt::get(b.getIntNTy(BLOCK_SIZE), 0));
     921    Value* cast_marker_value_1 = mBuilder->CreateBitCast(test, mBuilder->getIntNTy(BLOCK_SIZE));
     922    return mBuilder->CreateICmpEQ(cast_marker_value_1, ConstantInt::get(mBuilder->getIntNTy(BLOCK_SIZE), 0));
    947923}
    948924
    949925Value * PabloCompiler::genShiftHighbitToLow(unsigned FieldWidth, Value * op) {
    950926    unsigned FieldCount = BLOCK_SIZE/FieldWidth;
    951     IRBuilder<> b(mBasicBlock);
    952927    VectorType * vType = VectorType::get(IntegerType::get(mMod->getContext(), FieldWidth), FieldCount);
    953     Value * v = b.CreateBitCast(op, vType);
    954     return b.CreateBitCast(b.CreateLShr(v, FieldWidth - 1), mBitBlockType);
     928    Value * v = mBuilder->CreateBitCast(op, vType);
     929    return mBuilder->CreateBitCast(mBuilder->CreateLShr(v, FieldWidth - 1), mBitBlockType);
    955930}
    956931
    957932Value* PabloCompiler::genShiftLeft64(Value* e, const Twine &namehint) {
    958     IRBuilder<> b(mBasicBlock);
    959     Value* i128_val = b.CreateBitCast(e, b.getIntNTy(BLOCK_SIZE));
    960     return b.CreateBitCast(b.CreateShl(i128_val, 64, namehint), mBitBlockType);
     933    Value* i128_val = mBuilder->CreateBitCast(e, mBuilder->getIntNTy(BLOCK_SIZE));
     934    return mBuilder->CreateBitCast(mBuilder->CreateShl(i128_val, 64, namehint), mBitBlockType);
    961935}
    962936
    963937inline Value* PabloCompiler::genNot(Value* expr) {
    964     IRBuilder<> b(mBasicBlock);
    965     return b.CreateXor(expr, mOneInitializer, "not");
     938    return mBuilder->CreateXor(expr, mOneInitializer, "not");
    966939}
    967940
     
    973946        return genUnitAdvanceWithCarry(strm_value, localIndex, blk);
    974947    }
    975     IRBuilder<> b(mBasicBlock);
    976948    const PabloBlockCarryData & cd = blk->carryData;
    977949    const auto advanceIndex = cd.shortAdvanceCarryDataOffset(localIndex);
     
    982954    }
    983955    else {
    984         Value* advanceq_longint = b.CreateBitCast(genCarryDataLoad(advanceIndex), b.getIntNTy(BLOCK_SIZE));
    985         Value* strm_longint = b.CreateBitCast(strm_value, b.getIntNTy(BLOCK_SIZE));
    986         Value* adv_longint = b.CreateOr(b.CreateShl(strm_longint, shift_amount), b.CreateLShr(advanceq_longint, BLOCK_SIZE - shift_amount), "advance");
    987         result_value = b.CreateBitCast(adv_longint, mBitBlockType);
     956        Value* advanceq_longint = mBuilder->CreateBitCast(genCarryDataLoad(advanceIndex), mBuilder->getIntNTy(BLOCK_SIZE));
     957        Value* strm_longint = mBuilder->CreateBitCast(strm_value, mBuilder->getIntNTy(BLOCK_SIZE));
     958        Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - shift_amount), "advance");
     959        result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
    988960    }
    989961    genCarryDataStore(strm_value, advanceIndex);
     
    992964                   
    993965Value* PabloCompiler::genUnitAdvanceWithCarry(Value* strm_value, unsigned localIndex, const PabloBlock * blk) {
    994     IRBuilder<> b(mBasicBlock);
    995966    const PabloBlockCarryData & cd = blk->carryData;
    996967    const auto advanceIndex = cd.unitAdvanceCarryDataOffset(localIndex);
     
    999970#if (BLOCK_SIZE == 128) && !defined(USE_LONG_INTEGER_SHIFT)
    1000971    Value* advanceq_value = genShiftHighbitToLow(BLOCK_SIZE, genCarryDataLoad(advanceIndex));
    1001     Value* srli_1_value = b.CreateLShr(strm_value, 63);
     972    Value* srli_1_value = mBuilder->CreateLShr(strm_value, 63);
    1002973    Value* packed_shuffle;
    1003     Constant* const_packed_1_elems [] = {b.getInt32(0), b.getInt32(2)};
     974    Constant* const_packed_1_elems [] = {mBuilder->getInt32(0), mBuilder->getInt32(2)};
    1004975    Constant* const_packed_1 = ConstantVector::get(const_packed_1_elems);
    1005     packed_shuffle = b.CreateShuffleVector(advanceq_value, srli_1_value, const_packed_1);
    1006    
    1007     Constant* const_packed_2_elems[] = {b.getInt64(1), b.getInt64(1)};
     976    packed_shuffle = mBuilder->CreateShuffleVector(advanceq_value, srli_1_value, const_packed_1);
     977   
     978    Constant* const_packed_2_elems[] = {mBuilder->getInt64(1), mBuilder->getInt64(1)};
    1008979    Constant* const_packed_2 = ConstantVector::get(const_packed_2_elems);
    1009980   
    1010     Value* shl_value = b.CreateShl(strm_value, const_packed_2);
    1011     result_value = b.CreateOr(shl_value, packed_shuffle, "advance");
     981    Value* shl_value = mBuilder->CreateShl(strm_value, const_packed_2);
     982    result_value = mBuilder->CreateOr(shl_value, packed_shuffle, "advance");
    1012983#else
    1013     Value* advanceq_longint = b.CreateBitCast(genCarryDataLoad(advanceIndex), b.getIntNTy(BLOCK_SIZE));
    1014     Value* strm_longint = b.CreateBitCast(strm_value, b.getIntNTy(BLOCK_SIZE));
    1015     Value* adv_longint = b.CreateOr(b.CreateShl(strm_longint, 1), b.CreateLShr(advanceq_longint, BLOCK_SIZE - 1), "advance");
    1016     result_value = b.CreateBitCast(adv_longint, mBitBlockType);
     984    Value* advanceq_longint = mBuilder->CreateBitCast(genCarryDataLoad(advanceIndex), mBuilder->getIntNTy(BLOCK_SIZE));
     985    Value* strm_longint = mBuilder->CreateBitCast(strm_value, mBuilder->getIntNTy(BLOCK_SIZE));
     986    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, 1), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - 1), "advance");
     987    result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
    1017988   
    1018989#endif
     
    1025996//
    1026997Value* PabloCompiler::genLongAdvanceWithCarry(Value* strm_value, int shift_amount, unsigned localIndex, const PabloBlock * blk) {
    1027     IRBuilder<> b(mBasicBlock);
    1028998    const PabloBlockCarryData & cd = blk->carryData;
    1029999    const unsigned block_shift = shift_amount % BLOCK_SIZE;
     
    10311001    const unsigned bufsize = cd.longAdvanceBufferSize(shift_amount);
    10321002    //std::cerr << "shift_amount = " << shift_amount << " bufsize = " << bufsize << std::endl;
    1033     Value * indexMask = b.getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
    1034     Value * advBaseIndex = b.getInt64(cd.longAdvanceCarryDataOffset(localIndex));
    1035     Value * storeIndex = b.CreateAdd(b.CreateAnd(mBlockNo, indexMask), advBaseIndex);
    1036     Value * loadIndex = b.CreateAdd(b.CreateAnd(b.CreateSub(mBlockNo, b.getInt64(advanceEntries)), indexMask), advBaseIndex);
    1037     Value * storePtr = b.CreateGEP(mCarryDataPtr, storeIndex);
    1038     Value * loadPtr = b.CreateGEP(mCarryDataPtr, loadIndex);
     1003    Value * indexMask = mBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
     1004    Value * advBaseIndex = mBuilder->getInt64(cd.longAdvanceCarryDataOffset(localIndex));
     1005    Value * storeIndex = mBuilder->CreateAdd(mBuilder->CreateAnd(mBlockNo, indexMask), advBaseIndex);
     1006    Value * loadIndex = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
     1007    Value * storePtr = mBuilder->CreateGEP(mCarryDataPtr, storeIndex);
     1008    Value * loadPtr = mBuilder->CreateGEP(mCarryDataPtr, loadIndex);
    10391009    Value* result_value;
    10401010
    10411011    if (block_shift == 0) {
    1042         result_value = b.CreateAlignedLoad(loadPtr, BLOCK_SIZE/8);
     1012        result_value = mBuilder->CreateAlignedLoad(loadPtr, BLOCK_SIZE/8);
    10431013    }
    10441014    else if (advanceEntries == 1) {
    1045         Value* advanceq_longint = b.CreateBitCast(b.CreateAlignedLoad(loadPtr, BLOCK_SIZE/8), b.getIntNTy(BLOCK_SIZE));
    1046         Value* strm_longint = b.CreateBitCast(strm_value, b.getIntNTy(BLOCK_SIZE));
    1047         Value* adv_longint = b.CreateOr(b.CreateShl(strm_longint, block_shift), b.CreateLShr(advanceq_longint, BLOCK_SIZE - block_shift), "advance");
    1048         result_value = b.CreateBitCast(adv_longint, mBitBlockType);
     1015        Value* advanceq_longint = mBuilder->CreateBitCast(mBuilder->CreateAlignedLoad(loadPtr, BLOCK_SIZE/8), mBuilder->getIntNTy(BLOCK_SIZE));
     1016        Value* strm_longint = mBuilder->CreateBitCast(strm_value, mBuilder->getIntNTy(BLOCK_SIZE));
     1017        Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, block_shift), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - block_shift), "advance");
     1018        result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
    10491019    }
    10501020    else {
     
    10521022        // The buffer is maintained as a circular buffer of size bufsize.
    10531023        // Indexes within the buffer are computed by bitwise and with the indexMask.
    1054         Value * loadIndex2 = b.CreateAdd(b.CreateAnd(b.CreateSub(mBlockNo, b.getInt64(advanceEntries-1)), indexMask), advBaseIndex);
    1055         Value * loadPtr2 = b.CreateGEP(mCarryDataPtr, loadIndex2);
    1056         Value* advanceq_longint = b.CreateBitCast(b.CreateAlignedLoad(loadPtr, BLOCK_SIZE/8), b.getIntNTy(BLOCK_SIZE));
    1057         //genPrintRegister("advanceq_longint", b.CreateBitCast(advanceq_longint, mBitBlockType));
    1058         Value* strm_longint = b.CreateBitCast(b.CreateAlignedLoad(loadPtr2, BLOCK_SIZE/8), b.getIntNTy(BLOCK_SIZE));
    1059         //genPrintRegister("strm_longint", b.CreateBitCast(strm_longint, mBitBlockType));
    1060         Value* adv_longint = b.CreateOr(b.CreateShl(strm_longint, block_shift), b.CreateLShr(advanceq_longint, BLOCK_SIZE - block_shift), "longadvance");
    1061         result_value = b.CreateBitCast(adv_longint, mBitBlockType);
    1062     }
    1063     b.CreateAlignedStore(strm_value, storePtr, BLOCK_SIZE/8);
     1024        Value * loadIndex2 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
     1025        Value * loadPtr2 = mBuilder->CreateGEP(mCarryDataPtr, loadIndex2);
     1026        Value* advanceq_longint = mBuilder->CreateBitCast(mBuilder->CreateAlignedLoad(loadPtr, BLOCK_SIZE/8), mBuilder->getIntNTy(BLOCK_SIZE));
     1027        //genPrintRegister("advanceq_longint", mBuilder->CreateBitCast(advanceq_longint, mBitBlockType));
     1028        Value* strm_longint = mBuilder->CreateBitCast(mBuilder->CreateAlignedLoad(loadPtr2, BLOCK_SIZE/8), mBuilder->getIntNTy(BLOCK_SIZE));
     1029        //genPrintRegister("strm_longint", mBuilder->CreateBitCast(strm_longint, mBitBlockType));
     1030        Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, block_shift), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - block_shift), "longadvance");
     1031        result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
     1032    }
     1033    mBuilder->CreateAlignedStore(strm_value, storePtr, BLOCK_SIZE/8);
    10641034    return result_value;
    10651035}
    10661036   
    10671037void PabloCompiler::SetOutputValue(Value * marker, const unsigned index) {
    1068     IRBuilder<> b(mBasicBlock);
    10691038    if (marker->getType()->isPointerTy()) {
    1070         marker = b.CreateAlignedLoad(marker, BLOCK_SIZE/8, false);
    1071     }
    1072     Value* indices[] = {b.getInt64(0), b.getInt32(index)};
    1073     Value* gep = b.CreateGEP(mOutputAddrPtr, indices);
    1074     b.CreateAlignedStore(marker, gep, BLOCK_SIZE/8, false);
     1039        marker = mBuilder->CreateAlignedLoad(marker, BLOCK_SIZE/8, false);
     1040    }
     1041    Value* indices[] = {mBuilder->getInt64(0), mBuilder->getInt32(index)};
     1042    Value* gep = mBuilder->CreateGEP(mOutputAddrPtr, indices);
     1043    mBuilder->CreateAlignedStore(marker, gep, BLOCK_SIZE/8, false);
    10751044}
    10761045
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4572 r4628  
    2626#include <pablo/pe_string.h>
    2727#include <llvm/ADT/Twine.h>
     28#include <llvm/IR/IRBuilder.h>
    2829
    2930namespace llvm {
     
    5253class If;
    5354class While;
     55
     56static IRBuilder<> LLVM_Builder(getGlobalContext());
    5457
    5558struct CompiledPabloFunction {
     
    144147    Module *                            mMod;
    145148#endif
    146     BasicBlock*                         mBasicBlock;
     149    IRBuilder <> *                      mBuilder;
    147150    ExecutionEngine*                    mExecutionEngine;
    148151
Note: See TracChangeset for help on using the changeset viewer.