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

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

Add IRBuilder functionality to IDISA_Builder; eliminate separate mBuilder

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