source: icGREP/icgrep-devel/icgrep/pablo/carry_manager.h @ 4723

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

Implementatation of the Pablo Count operation

File size: 5.2 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#ifndef CARRY_MANAGER_H
8#define CARRY_MANAGER_H
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Module.h>
11#include <IDISA/idisa_builder.h>
12#include <pablo/codegenstate.h>
13#include <pablo/carry_data.h>
14
15/*
16 * Carry Data Manager.
17 *
18 * Each PabloBlock (Main, If, While) has a contiguous data area for carry information.
19 * The data area may be at a fixed or variable base offset from the base of the
20 * main function carry data area.
21 * The data area for each block consists of contiguous space for the local carries and
22 * advances of the block plus the areas of any ifs/whiles nested within the block.
23
24*/
25
26enum CarryManagerStrategy {BitBlockStrategy, SequentialFullyPackedStrategy};
27
28
29using namespace llvm;
30
31namespace pablo {
32
33class PabloBlock;
34
35
36
37class CarryManager {
38public:
39 
40    CarryManager(IRBuilder <> * b, VectorType * bitBlockType, ConstantAggregateZero * zero, Constant * one, IDISA::IDISA_Builder * idb)
41    : mPACK_SIZE(BLOCK_SIZE)
42    , mITEMS_PER_PACK(1)
43    , mBuilder(b)
44    , mBitBlockType(bitBlockType)
45    , mZeroInitializer(zero)
46    , mOneInitializer(one)
47    , iBuilder(idb)
48    , mPabloRoot(nullptr)
49    , mCurrentScope(nullptr)
50    , mCarryInfo(nullptr)
51    , mCurrentFrameIndex(0)
52    , mCarryPackBasePtr(nullptr)
53    , mCarryBitBlockPtr(nullptr)
54    , mPopcountBasePtr(nullptr)
55    , mBlockNoPtr(nullptr)
56    , mBlockNo(nullptr)
57    , mPabloCountCount(0)
58    , mTotalCarryDataBitBlocks(0)
59    {
60
61    }
62
63    ~CarryManager();
64   
65    unsigned initialize(PabloBlock * blk, Value * carryDataPtr); 
66   
67    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
68   
69    void generateBlockNoIncrement();   
70    Value * getBlockNoPtr();
71   
72    /* Entering and leaving scopes. */
73   
74    void enterScope(PabloBlock * blk);
75    void leaveScope();
76   
77    /* Methods for processing individual carry-generating operations. */
78   
79    Value * getCarryOpCarryIn(int localIndex);
80    void setCarryOpCarryOut(unsigned idx, Value * carry_out);
81    Value * addCarryInCarryOut(int localIndex, Value* e1, Value* e2);
82
83
84    Value * advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
85 
86    /* Methods for getting and setting carry summary values for If statements */
87   
88    bool blockHasCarries();
89   
90    void initializeCarryDataAtIfEntry();
91   
92    Value * getCarrySummaryExpr();
93   
94    void generateCarryOutSummaryCodeIfNeeded();
95   
96    void buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
97   
98    void addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
99   
100    /* Methods for handling while statements */
101   
102    void ensureCarriesLoadedRecursive();
103
104    void initializeCarryDataPhisAtWhileEntry(BasicBlock * whileBodyFinalBlock);
105
106    void extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock);
107
108    void ensureCarriesStoredRecursive();
109
110    void ensureCarriesStoredLocal();
111   
112    Value * popCount(Value * to_count, unsigned globalIdx);
113   
114private:
115    unsigned mPACK_SIZE;
116    unsigned mITEMS_PER_PACK;
117    IRBuilder <> * mBuilder;
118    VectorType * mBitBlockType;
119    Constant * mZeroInitializer;
120    Constant * mOneInitializer;
121    IDISA::IDISA_Builder * iBuilder;
122    PabloBlock * mPabloRoot;
123    PabloBlock * mCurrentScope;
124    PabloBlockCarryData * mCarryInfo;
125    unsigned mCurrentFrameIndex;
126    Value * mCarryPackBasePtr;
127    Type * mCarryPackType;
128    Value * mCarryBitBlockPtr;
129    Value * mPopcountBasePtr;
130    Value * mBlockNoPtr;
131    Value * mBlockNo;
132    unsigned mPabloCountCount; // Number of Pablo "Count" operations
133    unsigned mTotalCarryDataBitBlocks;
134   
135    std::vector<PabloBlockCarryData *> mCarryInfoVector;
136
137
138    std::vector<Value *> mCarryPackPtr;
139    std::vector<Value *> mCarryInPack;
140    std::vector<PHINode *> mCarryInPhis; 
141    std::vector<PHINode *> mCarryOutAccumPhis; 
142    std::vector<Value *> mCarryOutPack;
143
144    Value * unitAdvanceCarryInCarryOut(int localIndex, Value * strm);
145    Value * shortAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
146    Value * longAdvanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm);
147   
148   
149    /* Helper routines */
150    Value * getCarryPack(unsigned packIndex);
151    void storeCarryPack(unsigned packIndex);
152   
153    Value * maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount);     
154    Value * getCarryInBits(unsigned carryBitPos, unsigned bits);
155    void extractAndSaveCarryOutBits(Value * strm, unsigned carryBit_lo, unsigned carryBitCount);
156    Value * pack2bitblock(Value * pack);
157    Value* genShiftLeft64(Value* e);
158
159
160    unsigned absPosition(unsigned frameOffsetinBits, unsigned relPos);
161    unsigned carryOpPosition(unsigned localIndex) ;
162    unsigned advance1Position(unsigned localIndex);
163    unsigned shortAdvancePosition(unsigned localIndex);
164    unsigned longAdvanceBitBlockPosition(unsigned localIndex);
165    unsigned localBasePack();
166    unsigned scopeBasePack();
167    unsigned summaryPackIndex();
168    unsigned summaryPosition();
169    unsigned summaryBits();
170
171};
172
173}
174
175#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.