source: icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp @ 4898

Last change on this file since 4898 was 4898, checked in by cameron, 3 years ago

Add IRBuilder functionality to IDISA_Builder; eliminate separate mBuilder

File size: 21.1 KB
RevLine 
[3850]1/*
[4533]2 *  Copyright (c) 2014-15 International Characters.
[3850]3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
[4237]7#include <pablo/pablo_compiler.h>
8#include <pablo/codegenstate.h>
[4566]9#include <pablo/carry_data.h>
[4647]10#include <pablo/carry_manager.h>
[4237]11#include <pablo/printer_pablos.h>
[4657]12#include <pablo/function.h>
[4249]13#include <re/re_name.h>
[4237]14#include <stdexcept>
[4240]15#include <include/simd-lib/bitblock.hpp>
[4416]16#include <sstream>
[4663]17#include <IDISA/idisa_builder.h>
[4892]18#include <IDISA/idisa_avx_builder.h>
[4274]19#include <llvm/IR/Verifier.h>
20#include <llvm/Pass.h>
21#include <llvm/PassManager.h>
22#include <llvm/ADT/SmallVector.h>
23#include <llvm/Analysis/Passes.h>
24#include <llvm/IR/BasicBlock.h>
25#include <llvm/IR/CallingConv.h>
26#include <llvm/IR/DataLayout.h>
27#include <llvm/IR/DerivedTypes.h>
28#include <llvm/IR/Function.h>
29#include <llvm/IR/GlobalVariable.h>
30#include <llvm/IR/InlineAsm.h>
31#include <llvm/IR/Instructions.h>
32#include <llvm/IR/LLVMContext.h>
33#include <llvm/IR/Module.h>
34#include <llvm/Support/FormattedStream.h>
35#include <llvm/Support/MathExtras.h>
36#include <llvm/Support/Casting.h>
[4280]37#include <llvm/Support/Compiler.h>
[4274]38#include <llvm/Support/Debug.h>
39#include <llvm/Support/TargetSelect.h>
40#include <llvm/Support/Host.h>
41#include <llvm/Transforms/Scalar.h>
42#include <llvm/IRReader/IRReader.h>
43#include <llvm/Bitcode/ReaderWriter.h>
44#include <llvm/Support/MemoryBuffer.h>
45#include <llvm/IR/IRBuilder.h>
[4510]46#include <llvm/Support/CommandLine.h>
47#include <llvm/ADT/Twine.h>
[4438]48#include <iostream>
[4237]49
[4544]50static cl::OptionCategory eIRDumpOptions("LLVM IR Dump Options", "These options control dumping of LLVM IR.");
[4686]51static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(eIRDumpOptions));
[4378]52
[4544]53static cl::OptionCategory fTracingOptions("Run-time Tracing Options", "These options control execution traces.");
54static cl::opt<bool> DumpTrace("dump-trace", cl::init(false), cl::desc("Generate dynamic traces of executed assignments."), cl::cat(fTracingOptions));
55
[4237]56namespace pablo {
57
[4839]58PabloCompiler::PabloCompiler(Type * bitBlockType)
[4665]59: mMod(nullptr)
[4647]60, mCarryManager(nullptr)
[4839]61, mBitBlockType(bitBlockType)
[4892]62, iBuilder(nullptr)
[4665]63, mInputType(nullptr)
[4545]64, mWhileDepth(0)
65, mIfDepth(0)
[4274]66, mFunction(nullptr)
[4659]67, mInputAddressPtr(nullptr)
68, mOutputAddressPtr(nullptr)
[4843]69, mMaxWhileDepth(0) {
[4892]70   
71   
[4661]72
[4237]73}
74
[4665]75PabloCompiler::~PabloCompiler() {
[4237]76}
[4382]77   
[4237]78
[4734]79llvm::Function * PabloCompiler::compile(PabloFunction * function) {
[4665]80    Module * module = new Module("", getGlobalContext());
[4730]81   
82    auto func = compile(function, module);
83   
[4665]84    //Display the IR that has been generated by this module.
85    if (LLVM_UNLIKELY(DumpGeneratedIR)) {
86        module->dump();
87    }
88    //Create a verifier.  The verifier will print an error message if our module is malformed in any way.
89    verifyModule(*module, &dbgs());
90
[4730]91    return func;
[4665]92}
93
[4892]94// Dynamic AVX2 confirmation
95#if (BLOCK_SIZE == 256)
96#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
97#include <ispc.cpp>
98#endif
99   
[4734]100llvm::Function * PabloCompiler::compile(PabloFunction * function, Module * module) {
[4665]101
[4687]102 
[4870]103    PabloBlock * const mainScope = function->getEntryBlock();
[4726]104
[4870]105    mainScope->enumerateScopes(0);
[4687]106   
[4734]107    Examine(*function);
[4665]108
109    mMod = module;
110
[4892]111#if (BLOCK_SIZE == 256)
112    if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
[4898]113        iBuilder = new IDISA::IDISA_AVX2_Builder(mMod, mBitBlockType);
[4892]114        //std::cerr << "IDISA_AVX2_Builder selected\n";
115    }
116    else{
[4898]117        iBuilder = new IDISA::IDISA_Builder(mMod, mBitBlockType);
[4892]118        //std::cerr << "Generic IDISA_Builder selected\n";
119    }
120#else   
[4898]121    iBuilder = new IDISA::IDISA_Builder(mMod, mBitBlockType);
[4892]122#endif
[4665]123
[4898]124    mCarryManager = new CarryManager(iBuilder);
[4726]125   
[4734]126    GenerateFunction(*function);
[4731]127   
[4898]128    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", mFunction,0));
[4237]129
130    //The basis bits structure
[4734]131    for (unsigned i = 0; i != function->getNumOfParameters(); ++i) {
[4898]132        Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(i)};
133        Value * gep = iBuilder->CreateGEP(mInputAddressPtr, indices);
134        LoadInst * basisBit = iBuilder->CreateAlignedLoad(gep, iBuilder->getBitBlockWidth()/8, false, function->getParameter(i)->getName()->to_string());
[4734]135        mMarkerMap[function->getParameter(i)] = basisBit;
[4659]136        if (DumpTrace) {
[4892]137            iBuilder->genPrintRegister(function->getParameter(i)->getName()->to_string(), basisBit);
[4659]138        }
[4237]139    }
[4690]140     
[4237]141    //Generate the IR instructions for the function.
[4628]142   
[4870]143    mCarryManager->initialize(mMod, mainScope);
[4726]144   
[4690]145    compileBlock(mainScope);
146   
[4710]147    mCarryManager->ensureCarriesStoredLocal();
[4703]148    mCarryManager->leaveScope();
149   
150   
[4647]151    mCarryManager->generateBlockNoIncrement();
[4257]152
[4717]153    if (DumpTrace) {
[4898]154        iBuilder->genPrintRegister("mBlockNo", iBuilder->CreateAlignedLoad(iBuilder->CreateBitCast(mCarryManager->getBlockNoPtr(), PointerType::get(mBitBlockType, 0)), iBuilder->getBitBlockWidth()/8, false));
[4548]155    }
[4647]156   
[4657]157    // Write the output values out
[4734]158    for (unsigned i = 0; i != function->getNumOfResults(); ++i) {
[4741]159        assert (function->getResult(i));
[4734]160        SetOutputValue(mMarkerMap[function->getResult(i)], i);
[4657]161    }
162
[4237]163    //Terminate the block
[4898]164    ReturnInst::Create(mMod->getContext(), iBuilder->GetInsertBlock());
[4726]165   
[4665]166    // Clean up
167    delete mCarryManager; mCarryManager = nullptr;
[4892]168    delete iBuilder; iBuilder = nullptr;
[4898]169    delete iBuilder; iBuilder = nullptr;
[4665]170    mMod = nullptr; // don't delete this. It's either owned by the ExecutionEngine or the calling function.
[4237]171
[4726]172    return mFunction;
[4237]173}
174
[4659]175inline void PabloCompiler::GenerateFunction(PabloFunction & function) {
[4680]176    mInputType = PointerType::get(StructType::get(mMod->getContext(), std::vector<Type *>(function.getNumOfParameters(), mBitBlockType)), 0);
177    Type * outputType = PointerType::get(StructType::get(mMod->getContext(), std::vector<Type *>(function.getNumOfResults(), mBitBlockType)), 0);
[4750]178    FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>({mInputType, outputType}), false);
[4657]179
[4237]180
181    //Starts on process_block
[4726]182    SmallVector<AttributeSet, 3> Attrs;
[4750]183    Attrs.push_back(AttributeSet::get(mMod->getContext(), ~0U, std::vector<Attribute::AttrKind>({ Attribute::NoUnwind, Attribute::UWTable })));
184    Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
185    Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
[4237]186    AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
187
[4659]188    // Create the function that will be generated.
189    mFunction = Function::Create(functionType, GlobalValue::ExternalLinkage, function.getName()->value(), mMod);
190    mFunction->setCallingConv(CallingConv::C);
[4274]191    mFunction->setAttributes(AttrSet);
[4659]192
193    Function::arg_iterator args = mFunction->arg_begin();
194    mInputAddressPtr = args++;
195    mInputAddressPtr->setName("input");
196    mOutputAddressPtr = args++;
197    mOutputAddressPtr->setName("output");
[4237]198}
[4659]199
[4665]200inline void PabloCompiler::Examine(PabloFunction & function) {
[4692]201    mWhileDepth = 0;
202    mIfDepth = 0;
203    mMaxWhileDepth = 0;
204    Examine(function.getEntryBlock());
205    if (LLVM_UNLIKELY(mWhileDepth != 0 || mIfDepth != 0)) {
206        throw std::runtime_error("Malformed Pablo AST: Unbalanced If or While nesting depth!");
[4659]207    }
208}
[4665]209
210
[4870]211void PabloCompiler::Examine(PabloBlock * block) {
212    for (Statement * stmt : *block) {
[4686]213        if (If * ifStatement = dyn_cast<If>(stmt)) {
[4566]214            Examine(ifStatement->getBody());
[4237]215        }
[4257]216        else if (While * whileStatement = dyn_cast<While>(stmt)) {
[4545]217            mMaxWhileDepth = std::max(mMaxWhileDepth, ++mWhileDepth);
[4566]218            Examine(whileStatement->getBody());
[4545]219            --mWhileDepth;
[4237]220        }
221    }
222}
223
[4870]224void PabloCompiler::compileBlock(PabloBlock * block) {
225    mPabloBlock = block;
226    for (const Statement * statement : *block) {
[4343]227        compileStatement(statement);
[4237]228    }
[4870]229    mPabloBlock = block->getParent();
[4237]230}
231
[4827]232void PabloCompiler::compileIf(const If * ifStatement) {       
[4647]233    //
234    //  The If-ElseZero stmt:
235    //  if <predicate:expr> then <body:stmt>* elsezero <defined:var>* endif
236    //  If the value of the predicate is nonzero, then determine the values of variables
237    //  <var>* by executing the given statements.  Otherwise, the value of the
238    //  variables are all zero.  Requirements: (a) no variable that is defined within
239    //  the body of the if may be accessed outside unless it is explicitly
240    //  listed in the variable list, (b) every variable in the defined list receives
241    //  a value within the body, and (c) the logical consequence of executing
242    //  the statements in the event that the predicate is zero is that the
243    //  values of all defined variables indeed work out to be 0.
244    //
245    //  Simple Implementation with Phi nodes:  a phi node in the if exit block
246    //  is inserted for each variable in the defined variable list.  It receives
247    //  a zero value from the ifentry block and the defined value from the if
248    //  body.
249    //
[4665]250
[4898]251    BasicBlock * ifEntryBlock = iBuilder->GetInsertBlock();
[4647]252    BasicBlock * ifBodyBlock = BasicBlock::Create(mMod->getContext(), "if.body", mFunction, 0);
253    BasicBlock * ifEndBlock = BasicBlock::Create(mMod->getContext(), "if.end", mFunction, 0);
[4541]254   
[4870]255    PabloBlock * ifBody = ifStatement->getBody();
[4628]256   
[4663]257    Value * if_test_value = compileExpression(ifStatement->getCondition());
[4670]258   
[4870]259    mCarryManager->enterScope(ifBody);
[4898]260    iBuilder->CreateCondBr(mCarryManager->generateBitBlockOrSummaryTest(if_test_value), ifBodyBlock, ifEndBlock);
[4838]261   
[4647]262    // Entry processing is complete, now handle the body of the if.
[4898]263    iBuilder->SetInsertPoint(ifBodyBlock);
[4647]264   
[4703]265    mCarryManager->initializeCarryDataAtIfEntry();
[4647]266    compileBlock(ifBody);
[4670]267    if (mCarryManager->blockHasCarries()) {
[4676]268        mCarryManager->generateCarryOutSummaryCodeIfNeeded();
[4647]269    }
[4898]270    BasicBlock * ifBodyFinalBlock = iBuilder->GetInsertBlock();
[4710]271    mCarryManager->ensureCarriesStoredLocal();
[4898]272    iBuilder->CreateBr(ifEndBlock);
[4647]273    //End Block
[4898]274    iBuilder->SetInsertPoint(ifEndBlock);
[4647]275    for (const PabloAST * node : ifStatement->getDefined()) {
276        const Assign * assign = cast<Assign>(node);
[4898]277        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, assign->getName()->value());
[4647]278        auto f = mMarkerMap.find(assign);
279        assert (f != mMarkerMap.end());
[4892]280        phi->addIncoming(iBuilder->allZeroes(), ifEntryBlock);
[4647]281        phi->addIncoming(f->second, ifBodyFinalBlock);
282        mMarkerMap[assign] = phi;
283    }
284    // Create the phi Node for the summary variable, if needed.
[4703]285    mCarryManager->buildCarryDataPhisAfterIfBody(ifEntryBlock, ifBodyFinalBlock);
[4670]286    mCarryManager->leaveScope();
[4535]287}
288
[4647]289void PabloCompiler::compileWhile(const While * whileStatement) {
[4595]290
[4870]291    PabloBlock * const whileBody = whileStatement->getBody();
[4647]292   
[4898]293    BasicBlock * whileEntryBlock = iBuilder->GetInsertBlock();
[4647]294    BasicBlock * whileBodyBlock = BasicBlock::Create(mMod->getContext(), "while.body", mFunction, 0);
295    BasicBlock * whileEndBlock = BasicBlock::Create(mMod->getContext(), "while.end", mFunction, 0);
[4640]296
[4870]297    mCarryManager->enterScope(whileBody);
[4670]298    mCarryManager->ensureCarriesLoadedRecursive();
[4640]299
[4647]300    const auto & nextNodes = whileStatement->getVariants();
301    std::vector<PHINode *> nextPhis;
302    nextPhis.reserve(nextNodes.size());
[4640]303
[4647]304    // On entry to the while structure, proceed to execute the first iteration
305    // of the loop body unconditionally.   The while condition is tested at the end of
306    // the loop.
[4640]307
[4898]308    iBuilder->CreateBr(whileBodyBlock);
309    iBuilder->SetInsertPoint(whileBodyBlock);
[4640]310
[4647]311    //
312    // There are 3 sets of Phi nodes for the while loop.
313    // (1) Carry-ins: (a) incoming carry data first iterations, (b) zero thereafter
314    // (2) Carry-out accumulators: (a) zero first iteration, (b) |= carry-out of each iteration
315    // (3) Next nodes: (a) values set up before loop, (b) modified values calculated in loop.
[4640]316
[4670]317    mCarryManager->initializeCarryDataPhisAtWhileEntry(whileEntryBlock);
[4640]318
[4647]319    // for any Next nodes in the loop body, initialize to (a) pre-loop value.
320    for (const Next * n : nextNodes) {
[4898]321        PHINode * phi = iBuilder->CreatePHI(mBitBlockType, 2, n->getName()->value());
[4647]322        auto f = mMarkerMap.find(n->getInitial());
323        assert (f != mMarkerMap.end());
324        phi->addIncoming(f->second, whileEntryBlock);
325        mMarkerMap[n->getInitial()] = phi;
326        nextPhis.push_back(phi);
327    }
[4595]328
[4647]329    //
330    // Now compile the loop body proper.  Carry-out accumulated values
331    // and iterated values of Next nodes will be computed.
332    ++mWhileDepth;
333    compileBlock(whileBody);
[4640]334
[4898]335    BasicBlock * whileBodyFinalBlock = iBuilder->GetInsertBlock();
[4640]336
[4688]337    if (mCarryManager->blockHasCarries()) {
338        mCarryManager->generateCarryOutSummaryCodeIfNeeded();
339    }
[4670]340    mCarryManager->extendCarryDataPhisAtWhileBodyFinalBlock(whileBodyFinalBlock);
[4264]341
[4647]342    // Terminate the while loop body with a conditional branch back.
[4898]343    iBuilder->CreateCondBr(iBuilder->bitblock_any(compileExpression(whileStatement->getCondition())), whileBodyBlock, whileEndBlock);
[4576]344
[4647]345    // and for any Next nodes in the loop body
346    for (unsigned i = 0; i < nextNodes.size(); i++) {
347        const Next * n = nextNodes[i];
348        auto f = mMarkerMap.find(n->getExpr());
349        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
350            throw std::runtime_error("Next node expression was not compiled!");
[4264]351        }
[4647]352        nextPhis[i]->addIncoming(f->second, whileBodyFinalBlock);
353    }
[4237]354
[4898]355    iBuilder->SetInsertPoint(whileEndBlock);
[4647]356    --mWhileDepth;
[4640]357
[4670]358    mCarryManager->ensureCarriesStoredRecursive();
359    mCarryManager->leaveScope();
[4535]360}
361
[4640]362
[4643]363void PabloCompiler::compileStatement(const Statement * stmt) {
364    Value * expr = nullptr;
[4535]365    if (const Assign * assign = dyn_cast<const Assign>(stmt)) {
[4657]366        expr = compileExpression(assign->getExpression());
[4237]367    }
[4535]368    else if (const Next * next = dyn_cast<const Next>(stmt)) {
[4643]369        expr = compileExpression(next->getExpr());
[4535]370    }
[4643]371    else if (const If * ifStatement = dyn_cast<const If>(stmt)) {
[4535]372        compileIf(ifStatement);
[4643]373        return;
[4535]374    }
[4643]375    else if (const While * whileStatement = dyn_cast<const While>(stmt)) {
[4535]376        compileWhile(whileStatement);
[4643]377        return;
[4535]378    }
[4410]379    else if (const Call* call = dyn_cast<Call>(stmt)) {
[4237]380        //Call the callee once and store the result in the marker map.
[4643]381        if (mMarkerMap.count(call) != 0) {
382            return;
[4237]383        }
[4674]384
[4684]385        const Prototype * proto = call->getPrototype();
386        const String * callee = proto->getName();
387
388        Type * inputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfParameters(), mBitBlockType});
389        Type * outputType = StructType::get(mMod->getContext(), std::vector<Type *>{proto->getNumOfResults(), mBitBlockType});
[4726]390        FunctionType * functionType = FunctionType::get(Type::getVoidTy(mMod->getContext()), std::vector<Type *>{PointerType::get(inputType, 0), PointerType::get(outputType, 0)}, false);
[4684]391
392        //Starts on process_block
393        SmallVector<AttributeSet, 3> Attrs;
[4750]394        Attrs.push_back(AttributeSet::get(mMod->getContext(), 1U, std::vector<Attribute::AttrKind>({ Attribute::ReadOnly, Attribute::NoCapture })));
395        Attrs.push_back(AttributeSet::get(mMod->getContext(), 2U, std::vector<Attribute::AttrKind>({ Attribute::ReadNone, Attribute::NoCapture })));
[4684]396        AttributeSet AttrSet = AttributeSet::get(mMod->getContext(), Attrs);
397
398        Function * externalFunction = cast<Function>(mMod->getOrInsertFunction(callee->value(), functionType, AttrSet));
399        if (LLVM_UNLIKELY(externalFunction == nullptr)) {
400            throw std::runtime_error("Could not create static method call for external function \"" + callee->to_string() + "\"");
401        }
402        externalFunction->setCallingConv(llvm::CallingConv::C);
403
404
[4898]405        AllocaInst * outputStruct = iBuilder->CreateAlloca(outputType);
406        iBuilder->CreateCall2(externalFunction, mInputAddressPtr, outputStruct);
407        Value * outputPtr = iBuilder->CreateGEP(outputStruct, std::vector<Value *>({ iBuilder->getInt32(0), iBuilder->getInt32(0) }));
408        expr = iBuilder->CreateAlignedLoad(outputPtr, iBuilder->getBitBlockWidth() / 8, false);
[4237]409    }
[4410]410    else if (const And * pablo_and = dyn_cast<And>(stmt)) {
[4892]411        expr = iBuilder->simd_and(compileExpression(pablo_and->getOperand(0)), compileExpression(pablo_and->getOperand(1)));
[4237]412    }
[4410]413    else if (const Or * pablo_or = dyn_cast<Or>(stmt)) {
[4892]414        expr = iBuilder->simd_or(compileExpression(pablo_or->getOperand(0)), compileExpression(pablo_or->getOperand(1)));
[4237]415    }
[4410]416    else if (const Xor * pablo_xor = dyn_cast<Xor>(stmt)) {
[4892]417        expr = iBuilder->simd_xor(compileExpression(pablo_xor->getOperand(0)), compileExpression(pablo_xor->getOperand(1)));
[4237]418    }
[4410]419    else if (const Sel * sel = dyn_cast<Sel>(stmt)) {
[4264]420        Value* ifMask = compileExpression(sel->getCondition());
[4892]421        Value* ifTrue = iBuilder->simd_and(ifMask, compileExpression(sel->getTrueExpr()));
422        Value* ifFalse = iBuilder->simd_and(iBuilder->simd_not(ifMask), compileExpression(sel->getFalseExpr()));
423        expr = iBuilder->simd_or(ifTrue, ifFalse);
[4237]424    }
[4410]425    else if (const Not * pablo_not = dyn_cast<Not>(stmt)) {
[4892]426        expr = iBuilder->simd_not(compileExpression(pablo_not->getExpr()));
[4237]427    }
[4410]428    else if (const Advance * adv = dyn_cast<Advance>(stmt)) {
[4647]429        Value* strm_value = compileExpression(adv->getExpr());
[4270]430        int shift = adv->getAdvanceAmount();
[4717]431        unsigned advance_index = adv->getLocalAdvanceIndex();
432        expr = mCarryManager->advanceCarryInCarryOut(advance_index, shift, strm_value);
[4237]433    }
[4717]434    else if (const Mod64Advance * adv = dyn_cast<Mod64Advance>(stmt)) {
435        Value* strm_value = compileExpression(adv->getExpr());
436        int shift = adv->getAdvanceAmount();
[4892]437        expr = iBuilder->simd_slli(64, strm_value, shift);
[4717]438    }
[4643]439    else if (const MatchStar * mstar = dyn_cast<MatchStar>(stmt)) {
[4410]440        Value * marker = compileExpression(mstar->getMarker());
441        Value * cc = compileExpression(mstar->getCharClass());
[4892]442        Value * marker_and_cc = iBuilder->simd_and(marker, cc);
[4717]443        unsigned carry_index = mstar->getLocalCarryIndex();
444        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_and_cc, cc);
[4892]445        expr = iBuilder->simd_or(iBuilder->simd_xor(sum, cc), marker);
[4237]446    }
[4717]447    else if (const Mod64MatchStar * mstar = dyn_cast<Mod64MatchStar>(stmt)) {
448        Value * marker = compileExpression(mstar->getMarker());
449        Value * cc = compileExpression(mstar->getCharClass());
[4892]450        Value * marker_and_cc = iBuilder->simd_and(marker, cc);
451        Value * sum = iBuilder->simd_add(64, marker_and_cc, cc);
452        expr = iBuilder->simd_or(iBuilder->simd_xor(sum, cc), marker);
[4717]453    }
[4643]454    else if (const ScanThru * sthru = dyn_cast<ScanThru>(stmt)) {
[4410]455        Value * marker_expr = compileExpression(sthru->getScanFrom());
456        Value * cc_expr = compileExpression(sthru->getScanThru());
[4717]457        unsigned carry_index = sthru->getLocalCarryIndex();
458        Value * sum = mCarryManager->addCarryInCarryOut(carry_index, marker_expr, cc_expr);
[4892]459        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
[4237]460    }
[4717]461    else if (const Mod64ScanThru * sthru = dyn_cast<Mod64ScanThru>(stmt)) {
462        Value * marker_expr = compileExpression(sthru->getScanFrom());
463        Value * cc_expr = compileExpression(sthru->getScanThru());
[4892]464        Value * sum = iBuilder->simd_add(64, marker_expr, cc_expr);
465        expr = iBuilder->simd_and(sum, iBuilder->simd_not(cc_expr));
[4717]466    }
[4720]467    else if (const Count * c = dyn_cast<Count>(stmt)) {
468        unsigned count_index = c->getGlobalCountIndex();
469        Value * to_count = compileExpression(c->getExpr());
470        expr = mCarryManager->popCount(to_count, count_index);
471    }
[4359]472    else {
[4567]473        llvm::raw_os_ostream cerr(std::cerr);
474        PabloPrinter::print(stmt, cerr);
[4410]475        throw std::runtime_error("Unrecognized Pablo Statement! can't compile.");
476    }
[4643]477    mMarkerMap[stmt] = expr;
[4647]478    if (DumpTrace) {
[4892]479        iBuilder->genPrintRegister(stmt->getName()->to_string(), expr);
[4647]480    }
481   
[4410]482}
483
484Value * PabloCompiler::compileExpression(const PabloAST * expr) {
485    if (isa<Ones>(expr)) {
[4892]486        return iBuilder->allOnes();
[4410]487    }
488    else if (isa<Zeroes>(expr)) {
[4892]489        return iBuilder->allZeroes();
[4410]490    }
491    auto f = mMarkerMap.find(expr);
[4643]492    if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
[4567]493        std::string o;
494        llvm::raw_string_ostream str(o);
[4416]495        str << "\"";
496        PabloPrinter::print(expr, str);
497        str << "\" was used before definition!";
498        throw std::runtime_error(str.str());
[4359]499    }
[4410]500    return f->second;
[4237]501}
502
[4268]503void PabloCompiler::SetOutputValue(Value * marker, const unsigned index) {
[4684]504    if (LLVM_UNLIKELY(marker == nullptr)) {
505        throw std::runtime_error("Cannot set result " + std::to_string(index) + " to Null");
506    }
507    if (LLVM_UNLIKELY(marker->getType()->isPointerTy())) {
[4898]508        marker = iBuilder->CreateAlignedLoad(marker, iBuilder->getBitBlockWidth()/8, false);
[4268]509    }
[4898]510    Value* indices[] = {iBuilder->getInt64(0), iBuilder->getInt32(index)};
511    Value* gep = iBuilder->CreateGEP(mOutputAddressPtr, indices);
[4837]512    if (marker->getType() != mBitBlockType) {
[4898]513        marker = iBuilder->CreateBitCast(marker, mBitBlockType);
[4837]514    }
[4898]515    iBuilder->CreateAlignedStore(marker, gep, iBuilder->getBitBlockWidth()/8, false);
[4237]516}
[4268]517
518}
Note: See TracBrowser for help on using the repository browser.