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

Last change on this file since 4927 was 4927, checked in by nmedfort, 4 years ago

Bug fixes

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