source: icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp @ 4709

Last change on this file since 4709 was 4709, checked in by cameron, 4 years ago

Fix masking for short advance to use uint64_t

File size: 26.7 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
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
7
8#include <include/simd-lib/bitblock.hpp>
9#include <stdexcept>
10#include <pablo/carry_data.h>
11#include <pablo/codegenstate.h>
12#include <pablo/carry_manager.h>
13#include <pablo/pabloAST.h>
14#include <iostream>
15
16namespace pablo {
17 
18    unsigned doScopeCount(PabloBlock * pb) {
19        unsigned count = 1;
20       
21        for (Statement * stmt : *pb) {
22            if (If * ifStatement = dyn_cast<If>(stmt)) {
23                count += doScopeCount(&ifStatement->getBody());
24            }
25            else if (While * whileStatement = dyn_cast<While>(stmt)) {
26                count += doScopeCount(&whileStatement->getBody());
27            }
28        }
29        return count;
30       
31    }
32
33unsigned CarryManager::initialize(PabloBlock * pb, Value * carryPtr) {
34    mPabloRoot = pb;
35#ifdef PACKING
36    mCarryPackType = mBuilder->getIntNTy(PACK_SIZE);
37    mCarryPackBasePtr = mBuilder->CreateBitCast(carryPtr, Type::getInt64PtrTy(mBuilder->getContext()));
38    mCarryBitBlockPtr = carryPtr;
39    mZeroInitializer = mBuilder->getInt64(0);
40    mOneInitializer = mBuilder->getInt64(-1);
41#else
42#define mCarryPackType mBitBlockType
43    mCarryPackBasePtr = carryPtr;
44#define mCarryBitBlockPtr mCarryPackBasePtr
45#endif
46    unsigned scopeCount = doScopeCount(pb);
47    mCarryInfoVector.resize(scopeCount);
48   
49    unsigned totalCarryDataSize = enumerate(pb, 0, 0);
50#ifdef PACKING
51    mTotalCarryDataBitBlocks = (totalCarryDataSize + BLOCK_SIZE - 1)/BLOCK_SIZE;
52#else
53    mTotalCarryDataBitBlocks = totalCarryDataSize;
54#endif
55    // Carry Data area will have one extra bit block to store the block number.
56    mBlockNoPtr = mBuilder->CreateBitCast(mBuilder->CreateGEP(carryPtr, mBuilder->getInt64(mTotalCarryDataBitBlocks)), Type::getInt64PtrTy(mBuilder->getContext()));
57    mBlockNo = mBuilder->CreateLoad(mBlockNoPtr);
58#ifdef PACKING
59    unsigned totalPackCount = (totalCarryDataSize + PACK_SIZE - 1)/PACK_SIZE; 
60    mCarryPackPtr.resize(totalPackCount);
61    mCarryInPack.resize(totalPackCount);
62    mCarryOutPack.resize(totalPackCount);
63    for (auto i = 0; i < totalPackCount; i++) mCarryInPack[i]=nullptr;
64#else
65    mCarryPackPtr.resize(mTotalCarryDataBitBlocks);
66    mCarryInPack.resize(mTotalCarryDataBitBlocks);
67    mCarryOutPack.resize(mTotalCarryDataBitBlocks);
68    for (auto i = 0; i < mTotalCarryDataBitBlocks; i++) mCarryInPack[i]=nullptr;
69#endif   
70    /*  Set the current scope to PabloRoot */
71    mCurrentScope = mPabloRoot;
72    mCurrentFrameIndex = 0;
73    mCarryInfo = mCarryInfoVector[0];
74
75    return mTotalCarryDataBitBlocks + 1; // One extra element for the block no.
76}
77   
78void CarryManager::generateBlockNoIncrement() {
79    mBuilder->CreateStore(mBuilder->CreateAdd(mBlockNo, mBuilder->getInt64(1)), mBlockNoPtr);
80}
81
82Value * CarryManager::getBlockNoPtr() {
83    return mBlockNoPtr;
84}
85
86
87unsigned CarryManager::enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth) {
88    llvm::raw_os_ostream cerr(std::cerr);
89    unsigned idx = blk->getScopeIndex();
90    PabloBlockCarryData * cd = new PabloBlockCarryData(blk);
91    mCarryInfoVector[idx] = cd;
92
93    cd->setIfDepth(ifDepth);
94    cd->setWhileDepth(whileDepth);
95    unsigned nestedOffset = cd->nested.frameOffset;
96 
97    for (Statement * stmt : *blk) {
98        if (If * ifStatement = dyn_cast<If>(stmt)) {
99            const unsigned ifCarryDataBits = enumerate(&ifStatement->getBody(), ifDepth+1, whileDepth);
100            PabloBlockCarryData * nestedBlockData = mCarryInfoVector[ifStatement->getBody().getScopeIndex()];
101#ifdef PACKING
102            EnsurePackHasSpace(nestedOffset, ifCarryDataBits);
103#endif
104            nestedBlockData->setFramePosition(nestedOffset);
105
106            nestedOffset += ifCarryDataBits;
107            if (cd->maxNestingDepth <= nestedBlockData->maxNestingDepth) cd->maxNestingDepth = nestedBlockData->maxNestingDepth + 1;
108            cd->nested.entries++;
109#ifndef NDEBUG
110            nestedBlockData->dumpCarryData(cerr);
111#endif
112        }
113        else if (While * whileStatement = dyn_cast<While>(stmt)) {
114            const unsigned whileCarryDataBits = enumerate(&whileStatement->getBody(), ifDepth, whileDepth+1);
115            PabloBlockCarryData * nestedBlockData = mCarryInfoVector[whileStatement->getBody().getScopeIndex()];
116            //if (whileStatement->isMultiCarry()) whileCarryDataBits *= whileStatement->getMaxIterations();
117#ifdef PACKING
118            EnsurePackHasSpace(nestedOffset, whileCarryDataBits);
119#endif
120            nestedBlockData->setFramePosition(nestedOffset);
121            nestedOffset += whileCarryDataBits;
122            if (cd->maxNestingDepth <= nestedBlockData->maxNestingDepth) cd->maxNestingDepth = nestedBlockData->maxNestingDepth + 1;
123            cd->nested.entries++;
124#ifndef NDEBUG
125            nestedBlockData->dumpCarryData(cerr);
126#endif
127        }
128    }
129   
130    cd->scopeCarryDataSize = nestedOffset;
131   
132    if (cd->explicitSummaryRequired()) {
133        // Need extra space for the summary variable, always the last
134        // entry within an if block.
135#ifdef PACKING
136        cd->scopeCarryDataSize = alignCeiling(cd->scopeCarryDataSize, PACK_SIZE);
137        cd->summary.frameOffset = cd->scopeCarryDataSize;
138        cd->scopeCarryDataSize += PACK_SIZE;
139#else
140        cd->summary.frameOffset = cd->scopeCarryDataSize;
141        cd->scopeCarryDataSize++;
142#endif
143    }
144    else {
145        cd->summary.frameOffset = 0;
146    }
147    return cd->scopeCarryDataSize;
148}
149
150
151/* Entering and leaving blocks. */
152
153void CarryManager::enterScope(PabloBlock * blk) {
154   
155    mCurrentScope = blk;
156    mCarryInfo = mCarryInfoVector[blk->getScopeIndex()];
157    mCurrentFrameIndex += mCarryInfo->getFrameIndex();
158    //std::cerr << "enterScope:  blk->getScopeIndex() = " << blk->getScopeIndex() << ", mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
159}
160
161void CarryManager::leaveScope() {
162#ifdef PACKING
163    if ((mCurrentFrameIndex % PACK_SIZE) == 0) {
164        // Write out all local packs.
165        auto localCarryIndex = localBasePack();
166        auto localCarryPacks = mCarryInfo->getLocalCarryPackCount();
167        for (auto i = localCarryIndex; i < localCarryIndex + localCarryPacks; i++) {
168            storeCarryPack(i);
169        }
170    }
171#endif
172    mCurrentFrameIndex -= mCarryInfo->getFrameIndex();
173    if (mCurrentScope != mPabloRoot) {
174        mCurrentScope = mCurrentScope->getParent();
175        mCarryInfo = mCarryInfoVector[mCurrentScope->getScopeIndex()];
176    }
177    //std::cerr << "leaveScope:  mCurrentFrameIndex = " << mCurrentFrameIndex << std::endl;
178}
179
180
181/* Helper routines */
182
183unsigned CarryManager::absPosition(unsigned frameOffset, unsigned relPos) {
184    return mCurrentFrameIndex + frameOffset + relPos;
185}
186
187
188unsigned CarryManager::carryOpPosition(unsigned localIndex) {
189    //std::cerr << "carryOpPosition: addWithCarry.frameOffset = " << mCarryInfo->addWithCarry.frameOffset << ", localIndex = " <<localIndex << std::endl;
190    return absPosition(mCarryInfo->addWithCarry.frameOffset, localIndex);
191}
192
193unsigned CarryManager::advance1Position(unsigned localIndex) {
194    //std::cerr << "unsigned CarryManager::advance1Position: advance1.frameOffset = " << mCarryInfo->advance1.frameOffset << ", localIndex = " <<localIndex << std::endl;
195    return absPosition(mCarryInfo->advance1.frameOffset, localIndex);
196}
197
198unsigned CarryManager::shortAdvancePosition(unsigned localIndex) {
199    return absPosition(mCarryInfo->shortAdvance.frameOffset, localIndex);
200}
201
202unsigned CarryManager::longAdvanceBitBlockPosition(unsigned localIndex) {
203#ifdef PACKING
204    return (mCurrentFrameIndex + mCarryInfo->longAdvance.frameOffset) / BLOCK_SIZE + localIndex;
205#else
206    return mCurrentFrameIndex + mCarryInfo->longAdvance.frameOffset + localIndex;
207#endif
208}
209   
210unsigned CarryManager::localBasePack() {
211#ifdef PACKING
212    return (mCurrentFrameIndex + mCarryInfo->shortAdvance.frameOffset) / PACK_SIZE;
213#else
214    return mCurrentFrameIndex + mCarryInfo->shortAdvance.frameOffset;
215#endif
216}
217   
218unsigned CarryManager::scopeBasePack() {
219#ifdef PACKING
220    return mCurrentFrameIndex / PACK_SIZE;
221#else
222    return mCurrentFrameIndex;
223#endif
224}
225   
226
227
228unsigned CarryManager::summaryPosition() {
229    return absPosition(mCarryInfo->summary.frameOffset, 0);
230}
231
232unsigned CarryManager::summaryBits() {
233#ifdef PACKING
234    if (mCarryInfo->scopeCarryDataSize > PACK_SIZE) return PACK_SIZE;
235    else return mCarryInfo->scopeCarryDataSize;
236#else
237    if (mCarryInfo->scopeCarryDataSize > 1) return PACK_SIZE;
238    else return mCarryInfo->scopeCarryDataSize;
239#endif
240}
241
242
243
244Value * CarryManager::getCarryPack(unsigned packIndex) {
245    if (mCarryInPack[packIndex] == nullptr) {
246        Value * packPtr = mBuilder->CreateGEP(mCarryPackBasePtr, mBuilder->getInt64(packIndex));
247        mCarryPackPtr[packIndex] = packPtr;
248        mCarryInPack[packIndex] = mBuilder->CreateAlignedLoad(packPtr, PACK_SIZE/8);
249    }
250    return mCarryInPack[packIndex];
251}
252
253void CarryManager::storeCarryPack(unsigned packIndex) {
254    //std::cerr << "storeCarryPack , pack = " << packIndex << std::endl;
255    mBuilder->CreateAlignedStore(mCarryOutPack[packIndex], mCarryPackPtr[packIndex], PACK_SIZE/8);
256}
257
258   
259/* maskSelectBitRange selects the bits of a pack from lo_bit through
260   lo_bit + bitCount - 1, setting all other bits to zero.  */
261   
262Value * CarryManager::maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount) {
263    if (bitCount == PACK_SIZE) {
264        assert(lo_bit == 0);
265        return pack;
266    }
267    uint64_t mask = ((((uint64_t) 1) << bitCount) - 1) << lo_bit;
268    return mBuilder->CreateAnd(pack, mBuilder->getInt64(mask));
269}
270   
271Value * CarryManager::getCarryInBits(unsigned carryBitPos, unsigned carryBitCount) {
272    unsigned packIndex = carryBitPos / PACK_SIZE;
273    unsigned packOffset = carryBitPos % PACK_SIZE;
274    Value * selected = maskSelectBitRange(getCarryPack(packIndex), packOffset, carryBitCount);
275    if (packOffset == 0) return selected;
276    return mBuilder->CreateLShr(selected, packOffset);
277}
278
279void CarryManager::extractAndSaveCarryOutBits(Value * bitblock, unsigned carryBit_pos, unsigned carryBitCount) {
280    unsigned packIndex = carryBit_pos / PACK_SIZE;
281    unsigned packOffset = carryBit_pos % PACK_SIZE;
282    unsigned rshift = PACK_SIZE - packOffset - carryBitCount;
283    uint64_t mask = ((((uint64_t) 1) << carryBitCount) - 1)  << packOffset;
284    //std::cerr << "extractAndSaveCarryOutBits: packIndex =" << packIndex << ", packOffset = " << packOffset << ", mask = " << mask << std::endl;
285    Value * field = iBuilder->mvmd_extract(PACK_SIZE, bitblock, BLOCK_SIZE/PACK_SIZE - 1);
286    //Value * field = maskSelectBitRange(field, PACK_SIZE - carryBitCount, carryBitCount);
287    if (rshift != 0) {
288        field = mBuilder->CreateLShr(field, mBuilder->getInt64(rshift));
289    }
290    if (packOffset != 0) {
291        field = mBuilder->CreateAnd(field, mBuilder->getInt64(mask));
292    }
293    if (mCarryOutPack[packIndex] == nullptr) {
294        mCarryOutPack[packIndex] = field;
295    }
296    else {
297        mCarryOutPack[packIndex] = mBuilder->CreateOr(mCarryOutPack[packIndex], field);
298    }
299}
300
301Value * CarryManager::pack2bitblock(Value * pack) {
302    return mBuilder->CreateBitCast(mBuilder->CreateZExt(pack, mBuilder->getIntNTy(BLOCK_SIZE)), mBitBlockType);
303}
304   
305   
306/* Methods for getting and setting individual carry values. */
307   
308Value * CarryManager::getCarryOpCarryIn(int localIndex) {
309    unsigned posn = carryOpPosition(localIndex);
310#ifdef PACKING
311    return pack2bitblock(getCarryInBits(posn, 1));
312#else
313    return getCarryPack(posn);
314#endif
315}
316
317   
318void CarryManager::setCarryOpCarryOut(unsigned localIndex, Value * carry_out_strm) {
319    unsigned posn = carryOpPosition(localIndex);
320#ifdef PACKING
321    extractAndSaveCarryOutBits(carry_out_strm, posn, 1);
322#else
323    Value * carry_bit = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_out_strm, mBuilder->getIntNTy(BLOCK_SIZE)), 127);
324    mCarryOutPack[posn] = mBuilder->CreateBitCast(carry_bit, mBitBlockType);
325    if (mCarryInfo->getWhileDepth() == 0) {
326        storeCarryPack(posn);
327    }
328#endif
329}
330
331Value* CarryManager::genShiftLeft64(Value* e) {
332    Value* i128_val = mBuilder->CreateBitCast(e, mBuilder->getIntNTy(BLOCK_SIZE));
333    return mBuilder->CreateBitCast(mBuilder->CreateShl(i128_val, 64), mBitBlockType);
334}
335
336Value * CarryManager::addCarryInCarryOut(int localIndex, Value* e1, Value* e2) {
337    Value * carryq_value = getCarryOpCarryIn(localIndex);
338    #if (BLOCK_SIZE == 128)
339    //calculate carry through logical ops
340    Value* carrygen = mBuilder->CreateAnd(e1, e2, "carrygen");
341    Value* carryprop = mBuilder->CreateOr(e1, e2, "carryprop");
342    Value* digitsum = mBuilder->CreateAdd(e1, e2, "digitsum");
343    Value* partial = mBuilder->CreateAdd(digitsum, carryq_value, "partial");
344    Value* digitcarry = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(partial)));
345    Value* mid_carry_in = genShiftLeft64(mBuilder->CreateLShr(digitcarry, 63));
346    Value* sum = mBuilder->CreateAdd(partial, mBuilder->CreateBitCast(mid_carry_in, mBitBlockType), "sum");
347    Value* carry_out_strm = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(sum)));
348
349    #else
350    //BLOCK_SIZE == 256, there is no other implementation
351    static_assert(false, "Add with carry for 256-bit bitblock requires USE_UADD_OVERFLOW");
352    #endif         
353    setCarryOpCarryOut(localIndex, carry_out_strm);
354    return sum;
355}
356
357
358Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
359    if (shift_amount == 1) {
360        return unitAdvanceCarryInCarryOut(localIndex, strm);
361    }
362    else if (shift_amount < LongAdvanceBase) {
363        return shortAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
364    }
365    else {
366        return longAdvanceCarryInCarryOut(localIndex, shift_amount, strm);
367    }
368}
369
370Value * CarryManager::unitAdvanceCarryInCarryOut(int localIndex, Value * strm) {
371    unsigned posn = advance1Position(localIndex);
372#ifdef PACKING
373    extractAndSaveCarryOutBits(strm, posn, 1);
374    Value* carry_longint = mBuilder->CreateZExt(getCarryInBits(posn, 1), mBuilder->getIntNTy(BLOCK_SIZE));
375    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
376    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, 1), carry_longint);
377    Value* result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
378    return result_value;
379#else
380    mCarryOutPack[posn] = strm; 
381    Value * carry_in = getCarryPack(posn);
382    if (mCarryInfo->getWhileDepth() == 0) {
383        storeCarryPack(posn);
384    }
385    Value* result_value;
386   
387#if (BLOCK_SIZE == 128) && !defined(USE_LONG_INTEGER_SHIFT)
388    Value * ahead64 = iBuilder->mvmd_dslli(64, carry_in, strm, 1);
389    result_value = mBuilder->CreateOr(iBuilder->simd_srli(64, ahead64, 63), iBuilder->simd_slli(64, strm, 1));
390#else
391    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
392    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
393    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, 1), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - 1), "advance");
394    result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
395   
396#endif
397    return result_value;
398#endif
399}
400
401Value * CarryManager::shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
402    unsigned posn = shortAdvancePosition(localIndex);
403#ifdef PACKING
404    extractAndSaveCarryOutBits(strm, posn, shift_amount);
405    //std::cerr << "shortAdvanceCarryInCarryOut: posn = " << posn << ", shift_amount = " << shift_amount << std::endl;
406    Value* carry_longint = mBuilder->CreateZExt(getCarryInBits(posn, shift_amount), mBuilder->getIntNTy(BLOCK_SIZE));
407    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
408    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), carry_longint);
409    Value* result_value = mBuilder->CreateBitCast(adv_longint, mBitBlockType);
410    return result_value;
411#else
412    mCarryOutPack[posn] = strm; 
413    Value * carry_in = getCarryPack(posn);
414    if (mCarryInfo->getWhileDepth() == 0) {
415        storeCarryPack(posn);
416    }
417    Value* advanceq_longint = mBuilder->CreateBitCast(carry_in, mBuilder->getIntNTy(BLOCK_SIZE));
418    Value* strm_longint = mBuilder->CreateBitCast(strm, mBuilder->getIntNTy(BLOCK_SIZE));
419    Value* adv_longint = mBuilder->CreateOr(mBuilder->CreateShl(strm_longint, shift_amount), mBuilder->CreateLShr(advanceq_longint, BLOCK_SIZE - shift_amount), "advance");
420    return mBuilder->CreateBitCast(adv_longint, mBitBlockType);
421#endif
422}
423   
424
425/*  currently defined in carry_data.h
426 
427 static unsigned power2ceil (unsigned v) {
428 unsigned ceil = 1;
429 while (ceil < v) ceil *= 2;
430 return ceil;
431 }
432 
433 unsigned longAdvanceEntries(unsigned shift_amount) const {
434 return (shift_amount + BLOCK_SIZE - 1)/BLOCK_SIZE;
435 }
436 
437 unsigned longAdvanceBufferSize(unsigned shift_amount)  const {
438 return power2ceil(longAdvanceEntries(shift_amount));
439 }
440 */
441
442   
443Value * CarryManager::longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * carry_out) {
444    unsigned carryDataIndex = longAdvanceBitBlockPosition(localIndex);
445    Value * advBaseIndex = mBuilder->getInt64(carryDataIndex);
446    if (shift_amount <= BLOCK_SIZE) {
447        // special case using a single buffer entry and the carry_out value.
448        Value * advanceDataPtr = mBuilder->CreateGEP(mCarryBitBlockPtr, advBaseIndex);
449        Value * carry_block0 = mBuilder->CreateAlignedLoad(advanceDataPtr, BLOCK_SIZE/8);
450        mBuilder->CreateAlignedStore(carry_out, advanceDataPtr, BLOCK_SIZE/8);
451        /* Very special case - no combine */
452        if (shift_amount == BLOCK_SIZE) return carry_block0;
453        Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - shift_amount);
454        Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_out, mBuilder->getIntNTy(BLOCK_SIZE)), shift_amount);
455        return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
456    }
457    // We need a buffer of at least two elements for storing the advance data.
458    const unsigned block_shift = shift_amount % BLOCK_SIZE;
459    const unsigned advanceEntries = mCarryInfo->longAdvanceEntries(shift_amount);
460    const unsigned bufsize = mCarryInfo->longAdvanceBufferSize(shift_amount);
461    Value * indexMask = mBuilder->getInt64(bufsize - 1);  // A mask to implement circular buffer indexing
462    Value * loadIndex0 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries)), indexMask), advBaseIndex);
463    Value * storeIndex = mBuilder->CreateAdd(mBuilder->CreateAnd(mBlockNo, indexMask), advBaseIndex);
464    Value * carry_block0 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryBitBlockPtr, loadIndex0), BLOCK_SIZE/8);
465    // If the long advance is an exact multiple of BLOCK_SIZE, we simply return the oldest
466    // block in the long advance carry data area. 
467    if (block_shift == 0) {
468        mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryBitBlockPtr, storeIndex), BLOCK_SIZE/8);
469        return carry_block0;
470    }
471    // Otherwise we need to combine data from the two oldest blocks.
472    Value * loadIndex1 = mBuilder->CreateAdd(mBuilder->CreateAnd(mBuilder->CreateSub(mBlockNo, mBuilder->getInt64(advanceEntries-1)), indexMask), advBaseIndex);
473    Value * carry_block1 = mBuilder->CreateAlignedLoad(mBuilder->CreateGEP(mCarryBitBlockPtr, loadIndex1), BLOCK_SIZE/8);
474    Value* block0_shr = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_block0, mBuilder->getIntNTy(BLOCK_SIZE)), BLOCK_SIZE - block_shift);
475    Value* block1_shl = mBuilder->CreateShl(mBuilder->CreateBitCast(carry_block1, mBuilder->getIntNTy(BLOCK_SIZE)), block_shift);
476    mBuilder->CreateAlignedStore(carry_out, mBuilder->CreateGEP(mCarryBitBlockPtr, storeIndex), BLOCK_SIZE/8);
477    return mBuilder->CreateBitCast(mBuilder->CreateOr(block1_shl, block0_shr), mBitBlockType);
478}
479   
480
481/* Methods for getting and setting carry summary values */
482   
483bool CarryManager::blockHasCarries(){
484    return mCarryInfo->blockHasCarries();
485} 
486
487
488Value * CarryManager::getCarrySummaryExpr() {
489    unsigned summary_posn = summaryPosition();
490#ifdef PACKING
491    Value * pack = getCarryPack(summary_posn/PACK_SIZE);
492    Value * summary_bits = maskSelectBitRange(pack, summary_posn % PACK_SIZE, summaryBits());
493    return mBuilder->CreateBitCast(mBuilder->CreateZExt(summary_bits, mBuilder->getIntNTy(BLOCK_SIZE)), mBitBlockType);
494#else
495    return getCarryPack(summary_posn);
496#endif
497}
498
499void CarryManager::initializeCarryDataAtIfEntry() {
500    if (mCarryOutPack[scopeBasePack()] == nullptr) {
501        mCarryInfo->ifEntryPack = mZeroInitializer;
502    }
503    else {
504        mCarryInfo->ifEntryPack = mCarryOutPack[scopeBasePack()];
505    }
506}
507   
508void CarryManager::buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
509    unsigned const ifScopeCarrySize = mCarryInfo->scopeCarryDataSize;
510    if (ifScopeCarrySize == 0) {
511        // No carry data, therefore no phi nodes.
512        return;
513    }
514#ifdef PACKING
515    if (ifScopeCarrySize <= PACK_SIZE) {
516        unsigned const ifPackIndex = scopeBasePack();
517        PHINode * ifPack_phi = mBuilder->CreatePHI(mCarryPackType, 2, "ifPack");
518        ifPack_phi->addIncoming(mCarryInfo->ifEntryPack, ifEntryBlock);
519        ifPack_phi->addIncoming(mCarryOutPack[ifPackIndex], ifBodyFinalBlock);
520        mCarryOutPack[ifPackIndex] = ifPack_phi;
521        return;
522    }
523#endif
524    if (mCarryInfo->getIfDepth() > 1) {
525        const unsigned summaryPackIndex = summaryPosition();
526        PHINode * summary_phi = mBuilder->CreatePHI(mCarryPackType, 2, "summary");
527        summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
528        summary_phi->addIncoming(mCarryOutPack[summaryPackIndex], ifBodyFinalBlock);
529        mCarryOutPack[summaryPackIndex] = summary_phi;
530    }
531}
532   
533void CarryManager::addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock) {
534    if ((mCarryInfo->getIfDepth() <= 1) || !mCarryInfo->blockHasCarries()){
535        // For ifDepth == 1, the parent does not need a summary as it is not itself within an if.
536        // Therefore, it doesn't need access to this block's summary in building its own.
537        return;
538    }
539    const unsigned carrySummaryIndex = summaryPosition();
540    PHINode * summary_phi = mBuilder->CreatePHI(mCarryPackType, 2, "summary");
541    summary_phi->addIncoming(mZeroInitializer, ifEntryBlock);
542    summary_phi->addIncoming(mCarryOutPack[carrySummaryIndex], ifBodyFinalBlock);
543    mCarryOutPack[carrySummaryIndex] = summary_phi;
544}
545   
546void CarryManager::generateCarryOutSummaryCodeIfNeeded() {
547   
548    if (!mCarryInfo->explicitSummaryRequired()) {
549        // An explicit summary may not be required, if there is a single carry
550        // operation within the block, or the carries are packed and all carry
551        // bits fit within a single pack.
552        return;
553    }
554   
555    const unsigned carrySummaryIndex = summaryPosition();
556   
557    Value * carry_summary = mZeroInitializer;
558    if (mCarryInfo->blockHasLongAdvances()) { // Force if entry
559        carry_summary = mOneInitializer;
560    }
561    else {
562        auto localCarryIndex = localBasePack();
563        auto localCarryPacks = mCarryInfo->getLocalCarryPackCount();
564        if (localCarryPacks > 0) {
565            carry_summary = mCarryOutPack[localCarryIndex];
566            for (auto i = 1; i < localCarryPacks; i++) {
567                //carry_summary = mBuilder->CreateOr(carry_summary, mPabloBlock->mCarryOutPack[i]);           
568                carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[localCarryIndex+i]);
569            }
570        }
571        for (Statement * stmt : *mCurrentScope) {
572            if (If * innerIf = dyn_cast<If>(stmt)) {
573                PabloBlock * inner_blk = & innerIf->getBody();
574                enterScope(inner_blk);
575                if (blockHasCarries()) {
576                  carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[summaryPosition()]);
577                }
578                leaveScope();
579            }
580            else if (While * innerWhile = dyn_cast<While>(stmt)) {
581                PabloBlock * inner_blk = & innerWhile->getBody();
582                enterScope(inner_blk);
583                if (blockHasCarries()) {
584                    carry_summary = mBuilder->CreateOr(carry_summary, mCarryOutPack[summaryPosition()]);
585                }
586                leaveScope();
587            }
588        }
589    }
590    // Calculation of the carry out summary is complete.   Store it and make it
591    // available in case it must included by parent blocks.
592    mCarryOutPack[carrySummaryIndex] = carry_summary;
593    storeCarryPack(carrySummaryIndex);
594}
595
596void CarryManager::ensureCarriesLoadedRecursive() {
597    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
598    const unsigned currentScopeBase = scopeBasePack();
599    if (mCarryInfo->getWhileDepth() == 1) {
600        for (auto i = currentScopeBase; i < currentScopeBase + scopeCarryPacks; ++i) {
601            getCarryPack(i);
602        }
603    }
604}
605
606
607void CarryManager::initializeCarryDataPhisAtWhileEntry(BasicBlock * whileEntryBlock) {
608    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
609    mCarryOutAccumPhis.resize(scopeCarryPacks);
610#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
611    const unsigned currentScopeBase = scopeBasePack();
612    mCarryInPhis.resize(scopeCarryPacks);
613#endif
614    for (unsigned index = 0; index < scopeCarryPacks; ++index) {
615#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
616        PHINode * phi_in = mBuilder->CreatePHI(mCarryPackType, 2);
617        phi_in->addIncoming(mCarryInPack[currentScopeBase+index], whileEntryBlock);
618        mCarryInPhis[index] = phi_in;
619#endif
620        PHINode * phi_out = mBuilder->CreatePHI(mCarryPackType, 2);
621        phi_out->addIncoming(mZeroInitializer, whileEntryBlock);
622        mCarryOutAccumPhis[index] = phi_out;
623    }
624}
625
626
627void CarryManager::extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock) {
628    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
629    const unsigned currentScopeBase = scopeBasePack();
630    for (unsigned index = 0; index < scopeCarryPacks; ++index) {
631#ifdef SET_WHILE_CARRY_IN_TO_ZERO_AFTER_FIRST_ITERATION
632        mCarryInPhis[index]->addIncoming(mZeroInitializer, whileBodyFinalBlock);
633#endif
634        PHINode * phi = mCarryOutAccumPhis[index];
635        Value * carryOut = mBuilder->CreateOr(phi, mCarryOutPack[currentScopeBase+index]);
636        phi->addIncoming(carryOut, whileBodyFinalBlock);
637        mCarryOutPack[currentScopeBase+index] = carryOut;
638    }
639}
640
641void CarryManager::ensureCarriesStoredRecursive() {
642    const unsigned scopeCarryPacks = mCarryInfo->getScopeCarryPackCount();
643    const unsigned currentScopeBase = scopeBasePack();
644    if (mCarryInfo->getWhileDepth() == 1) {
645        for (auto i = currentScopeBase; i < currentScopeBase + scopeCarryPacks; ++i) {
646            storeCarryPack(i);
647        }
648    }
649}
650
651}
652
Note: See TracBrowser for help on using the repository browser.