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

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

Clean-up and encapsulate if/summary test in carry_manager

File size: 5.3 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(IRBuilder <> * b, IDISA::IDISA_Builder * idb)
43    : mBuilder(b)
44    , iBuilder(idb)
45    , mPackBuilder(idb)
46    , mBitBlockType(idb->getBitBlockType())
47    , mBITBLOCK_WIDTH(idb->getBitBlockWidth())
48    , mPACK_SIZE(mBITBLOCK_WIDTH)
49    , mITEMS_PER_PACK(1)
50    , mPabloRoot(nullptr)
51    , mCurrentScope(nullptr)
52    , mCarryInfo(nullptr)
53    , mCurrentFrameIndex(0)
54    , mCarryPackBasePtr(nullptr)
55    , mCarryBitBlockPtr(nullptr)
56    , mPopcountBasePtr(nullptr)
57    , mBlockNoPtr(nullptr)
58    , mBlockNo(nullptr)
59    , mPabloCountCount(0)
60    , mTotalCarryDataBitBlocks(0)
61    , mCarryDataAllocationSize(0)
62    {
63
64    }
65
66    ~CarryManager();
67   
68    void initialize(Module * m, PabloBlock * blk);
69   
70    void generateCarryDataInitializer(Module * m);
71   
72    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
73   
74    void generateBlockNoIncrement();   
75    Value * getBlockNoPtr();
76   
77    /* Entering and leaving scopes. */
78   
79    void enterScope(PabloBlock * blk);
80    void leaveScope();
81   
82    /* Methods for processing individual carry-generating operations. */
83   
84    Value * getCarryOpCarryIn(int localIndex);
85    void setCarryOpCarryOut(unsigned idx, Value * carry_out);
86    Value * addCarryInCarryOut(int localIndex, Value* e1, Value* e2);
87
88
89    Value * advanceCarryInCarryOut(int localIndex, unsigned shift_amount, Value * strm);
90 
91    /* Methods for getting and setting carry summary values for If statements */
92   
93    bool blockHasCarries();
94   
95    void initializeCarryDataAtIfEntry();
96   
97    Value * generateBitBlockOrSummaryTest(Value * bitblock);
98   
99    void generateCarryOutSummaryCodeIfNeeded();
100   
101    void buildCarryDataPhisAfterIfBody(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
102   
103    void addSummaryPhiIfNeeded(BasicBlock * ifEntryBlock, BasicBlock * ifBodyFinalBlock);
104   
105    /* Methods for handling while statements */
106   
107    void ensureCarriesLoadedRecursive();
108
109    void initializeCarryDataPhisAtWhileEntry(BasicBlock * whileBodyFinalBlock);
110
111    void extendCarryDataPhisAtWhileBodyFinalBlock(BasicBlock * whileBodyFinalBlock);
112
113    void ensureCarriesStoredRecursive();
114
115    void ensureCarriesStoredLocal();
116   
117    Value * popCount(Value * to_count, unsigned globalIdx);
118   
119    Value * declareCarryDataArray(Module * m);
120
121   
122private:
123    IRBuilder <> * mBuilder;
124    IDISA::IDISA_Builder * iBuilder;
125    IDISA::IDISA_Builder * mPackBuilder;
126    Type * mBitBlockType;
127    unsigned mBITBLOCK_WIDTH;
128    unsigned mPACK_SIZE;
129    unsigned mITEMS_PER_PACK;
130    PabloBlock * mPabloRoot;
131    PabloBlock * mCurrentScope;
132    PabloBlockCarryData * mCarryInfo;
133    unsigned mCurrentFrameIndex;
134    Value * mCarryPackBasePtr;
135    Type * mCarryPackType;
136    Value * mCarryBitBlockPtr;
137    Value * mPopcountBasePtr;
138    Value * mBlockNoPtr;
139    Value * mBlockNo;
140    unsigned mPabloCountCount; // Number of Pablo "Count" operations
141    unsigned mTotalCarryDataBitBlocks;
142    unsigned mCarryDataAllocationSize;
143   
144    std::vector<PabloBlockCarryData *> mCarryInfoVector;
145
146
147    std::vector<Value *> mCarryPackPtr;
148    std::vector<Value *> mCarryInPack;
149    std::vector<PHINode *> mCarryInPhis; 
150    std::vector<PHINode *> mCarryOutAccumPhis; 
151    std::vector<Value *> mCarryOutPack;
152
153    Value * unitAdvanceCarryInCarryOut(int localIndex, Value * strm);
154    Value * shortAdvanceCarryInCarryOut(int localIndex, unsigned shift_amount, Value * strm);
155    Value * longAdvanceCarryInCarryOut(int localIndex, unsigned shift_amount, Value * strm);
156   
157   
158    /* Helper routines */
159    Value * getCarryPack(unsigned packIndex);
160    void storeCarryPack(unsigned packIndex);
161   
162    Value * maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount);     
163    Value * getCarryInBits(unsigned carryBitPos, unsigned bits);
164    void extractAndSaveCarryOutBits(Value * strm, unsigned carryBit_lo, unsigned carryBitCount);
165    Value * pack2bitblock(Value * pack);
166
167
168    unsigned absPosition(unsigned frameOffsetinBits, unsigned relPos);
169    unsigned carryOpPosition(unsigned localIndex) ;
170    unsigned advance1Position(unsigned localIndex);
171    unsigned shortAdvancePosition(unsigned localIndex);
172    unsigned longAdvanceBitBlockPosition(unsigned localIndex);
173    unsigned localBasePack();
174    unsigned scopeBasePack();
175    unsigned summaryPackIndex();
176    unsigned summaryPosition();
177    unsigned summaryBits();
178};
179
180}
181
182#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.