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

Last change on this file since 4941 was 4941, checked in by lindanl, 4 years ago

Some clean up.

File size: 5.1 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#include "../kernels/kernel.h"
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
28using namespace llvm;
29
30namespace pablo {
31
32class PabloBlock;
33
34
35
36class CarryManager {
37public:
38 
39    CarryManager(IDISA::IDISA_Builder * idb)
40    : iBuilder(idb)
41    , mBitBlockType(idb->getBitBlockType())
42    , mBitBlockWidth(idb->getBitBlockWidth())
43    , mRootScope(nullptr)
44    , mCurrentScope(nullptr)
45    , mCarryInfo(nullptr)
46    , mCurrentFrameIndex(0)
47    , mCarryPackBasePtr(nullptr)
48    , mCarryBitBlockPtr(nullptr)
49    , mPopcountBasePtr(nullptr)
50    , mBlockNoPtr(nullptr)
51    , mBlockNo(nullptr)
52    , mPabloCountCount(0)
53    , mTotalCarryDataBitBlocks(0)
54    , mCarryDataAllocationSize(0)
55    , mFilePosIdx(2)
56    {
57
58    }
59
60    ~CarryManager();
61   
62    void initialize(PabloBlock * blk, KernelBuilder * kBuilder);
63
64    void initialize_setPtrs(KernelBuilder * kBuilder);
65
66    void set_BlockNo(KernelBuilder * kBuilder);
67   
68    unsigned enumerate(PabloBlock * blk, unsigned ifDepth, unsigned whileDepth);
69       
70    Value * getBlockNoPtr() const;
71   
72    /* Entering and leaving scopes. */
73   
74    void enterScope(PabloBlock * const scope);
75    void leaveScope();
76   
77    /* Methods for processing individual carry-generating operations. */
78   
79    Value * addCarryInCarryOut(const unsigned localIndex, Value * const e1, Value * const e2);
80
81    Value * advanceCarryInCarryOut(const unsigned localIndex, const unsigned shiftAmount, Value * const strm);
82 
83    /* Methods for getting and setting carry summary values for If statements */
84   
85    bool hasCarries() const;
86       
87    Value * generateSummaryTest(Value * condition);
88   
89    void storeCarryOutSummary();
90
91    void addOuterSummaryToNestedSummary();
92
93    void buildCarryDataPhisAfterIfBody(BasicBlock * const entry, BasicBlock * const end);
94       
95    /* Methods for handling while statements */
96   
97    void ensureCarriesLoadedRecursive();
98
99    void initializeWhileEntryCarryDataPhis(BasicBlock * const end);
100
101    void finalizeWhileBlockCarryDataPhis(BasicBlock * const end);
102
103    void ensureCarriesStoredRecursive();
104   
105    Value * popCount(Value * to_count, unsigned globalIdx);
106   
107    Value * declareCarryDataArray(Module * m);
108
109protected:
110
111    Value * shortAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
112    Value * longAdvanceCarryInCarryOut(const unsigned index, const unsigned shiftAmount, Value * const value);
113
114    /* Methods for processing individual carry-generating operations. */
115
116    Value * getCarryIn(const unsigned localIndex);
117    void setCarryOut(const unsigned idx, Value * carryOut);
118
119    /* Helper routines */
120    Value * getCarryPack(const unsigned packIndex);
121    void storeCarryOut(const unsigned packIndex);
122   
123    Value * addToSummary(Value * const value);
124
125    bool hasSummary() const;
126    unsigned relativeFrameOffset(const unsigned frameOffset, const unsigned index) const;
127    unsigned addPosition(const unsigned localIndex) const;
128    unsigned unitAdvancePosition(const unsigned localIndex) const;
129    unsigned shortAdvancePosition(const unsigned localIndex) const;
130    unsigned longAdvancePosition(const unsigned localIndex) const;
131    unsigned localBasePack() const;
132    unsigned scopeBasePack() const;
133    unsigned summaryPack() const;
134
135private:
136    IDISA::IDISA_Builder * const iBuilder;
137    Type * const mBitBlockType;
138    const unsigned mBitBlockWidth;
139    PabloBlock * mRootScope;
140    PabloBlock * mCurrentScope;
141    CarryData * mCarryInfo;
142    unsigned mCurrentFrameIndex;
143    Value * mCarryPackBasePtr;
144    Type * mCarryPackType;
145    Value * mCarryBitBlockPtr;
146    Value * mPopcountBasePtr;
147    Value * mBlockNoPtr;
148    Value * mBlockNo;
149    unsigned mPabloCountCount; // Number of Pablo "Count" operations
150    unsigned mTotalCarryDataBitBlocks;
151    unsigned mCarryDataAllocationSize;
152    std::vector<CarryData *> mCarryInfoVector;
153    std::vector<Value *> mCarryPackPtr;
154    std::vector<Value *> mCarryInPack;
155    std::vector<PHINode *> mCarryInPhis;
156    std::vector<PHINode *> mCarryOutAccumPhis;
157    std::vector<Value *> mCarryOutPack;
158    std::vector<Value *> mCarrySummary;
159    int mCdArrayIdx;
160    int mPcArrayIdx;
161    int mFilePosIdx;
162};
163
164inline bool CarryManager::hasCarries() const {
165    return mCarryInfo->hasCarries();
166}
167
168inline Value * CarryManager::getBlockNoPtr() const {
169    return mBlockNoPtr;
170}
171
172}
173
174#endif // CARRY_MANAGER_H
Note: See TracBrowser for help on using the repository browser.