source: icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h @ 5541

Last change on this file since 5541 was 5541, checked in by cameron, 23 months ago

Dynamic Buffers - initial check-in

File size: 11.4 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#ifndef CBUILDER_H
6#define CBUILDER_H
7
8#include <IR_Gen/FunctionTypeBuilder.h>
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Constants.h>
11#include <llvm/ADT/Triple.h>
12#ifndef NDEBUG
13#include <llvm/IR/Function.h>
14#endif
15
16namespace kernels { class KernelBuilder; }
17namespace llvm { class Function; }
18namespace llvm { class IntegerType; }
19namespace llvm { class Module; }
20namespace llvm { class PointerType; }
21namespace llvm { class Type; }
22namespace llvm { class Value; }
23
24class Driver;
25
26class CBuilder : public llvm::IRBuilder<> {
27    using Predicate = llvm::CmpInst::Predicate;
28public:
29
30    CBuilder(llvm::LLVMContext & C);
31   
32    virtual ~CBuilder() {}
33
34    llvm::Module * getModule() const {
35        #ifndef NDEBUG
36        llvm::BasicBlock * const bb = GetInsertBlock();
37        if (bb) {
38            llvm::Function * const f = bb->getParent();
39            assert ("CBuilder has an insert point that is not contained within a Function" && f);
40            assert ("CBuilder module differs from insertion point module" && (mModule == f->getParent()));
41        }
42        #endif
43        return mModule;
44    }
45
46    void setModule(llvm::Module * module) {
47        mModule = module;
48        ClearInsertionPoint();
49    }
50   
51    // UDiv and URem with optimization for division by power-of-2 constants
52    llvm::Value * CreateUDiv(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
53    llvm::Value * CreateURem(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
54
55    // Division with rounding up to the ceiling
56    // Equivalent to CreateUDiv(CreateAdd(number, CreateSub(divisor, ConstantInt::get(divisor->getType(), 1))), divisor)
57    llvm::Value * CreateUDivCeil(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
58       
59    llvm::Value * CreateMalloc(llvm::Value * size);
60
61    llvm::Value * CreateAlignedMalloc(llvm::Value * size, const unsigned alignment);
62   
63    void CreateFree(llvm::Value * const ptr);
64
65    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
66
67    llvm::CallInst * CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1) {
68        return CreateMemSet(ptr, getInt8(0), size, alignment);
69    }
70
71    llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
72        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
73        instr->setAlignment(getCacheAlignment());
74        return instr;
75    }
76
77    llvm::Value * CreateCacheAlignedMalloc(llvm::Value * size) {
78        return CreateAlignedMalloc(size, getCacheAlignment());
79    }
80
81    // stdio.h functions
82    //
83    //  Create a call to:  FILE * fopen(const char *filename, const char *mode);
84    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
85    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
86    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
87    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
88    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
89    //  Create a call to:  int fclose ( FILE * stream );
90    llvm::Value * CreateFCloseCall(llvm::Value * stream);
91    //  Create a call to:  int remove(const char *path);
92    llvm::Value * CreateRemoveCall(llvm::Value * path);
93   
94    //  Create a call to:  int rename(const char *old, const char *new);
95    llvm::Value * CreateRenameCall(llvm::Value * oldName, llvm::Value * newName);
96   
97    llvm::Function * GetPrintf();
98
99    llvm::Function * GetDprintf();
100
101    //  Create calls to unistd.h functions.
102    //
103    //  Create a call to:  int open(const char *filename, int oflag, ...);
104    llvm::Value * CreateOpenCall(llvm::Value * filename, llvm::Value * oflag, llvm::Value * mode);
105    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
106    llvm::Value * CreateWriteCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
107    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
108    llvm::Value * CreateReadCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
109    //  Create a call to:  int close(int filedes);
110    llvm::Value * CreateCloseCall(llvm::Value * fileDescriptor);
111    //  Create a call to:  int unlink(const char *path);
112    llvm::Value * CreateUnlinkCall(llvm::Value * path);
113
114    llvm::Value * CreateFileSize(llvm::Value * fileDescriptor);
115
116    //  Create calls to stdlib.h functions.
117    //
118    //  Create a call to:  int mkstemp (char *template);
119    llvm::Value * CreateMkstempCall(llvm::Value * ftemplate);
120   
121    //  Create a call to:  size_t strlen(const char *str);
122    llvm::Value * CreateStrlenCall(llvm::Value * str);
123   
124    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
125
126    llvm::Value * CreateFileSourceMMap(llvm::Value * fd, llvm::Value * size);
127
128    enum Advice {
129        ADVICE_NORMAL
130        , ADVICE_RANDOM
131        , ADVICE_SEQUENTIAL
132        , ADVICE_WILLNEED
133        , ADVICE_DONTNEED
134    };
135
136    llvm::Value * CreateMAdvise(llvm::Value * addr, llvm::Value * length, Advice advice);
137
138    llvm::Value * CreateMMap(llvm::Value * const addr, llvm::Value * size, llvm::Value * const prot, llvm::Value * const flags, llvm::Value * const fd, llvm::Value * const offset);
139
140    llvm::Value * CreateMRemap(llvm::Value * addr, llvm::Value * oldSize, llvm::Value * newSize);
141
142    llvm::Value * CreateMUnmap(llvm::Value * addr, llvm::Value * size);
143
144    //  Posix thread (pthread.h) functions.
145    //
146    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
147    //                    void *(*start_routine)(void*), void *arg);
148    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
149
150    //  Create a call to:  int pthread_yield(void);
151    llvm::Value * CreatePThreadYield();
152   
153    //  Create a call to:  void pthread_exit(void *value_ptr);
154    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
155   
156    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
157    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
158   
159    void CallPrintInt(const std::string & name, llvm::Value * const value);
160   
161    void CallPrintIntToStderr(const std::string & name, llvm::Value * const value);
162   
163    llvm::Value * GetString(llvm::StringRef Str);
164   
165    void CallPrintMsgToStderr(const std::string & message);
166
167    inline llvm::IntegerType * getSizeTy() const {
168        assert (mSizeType);
169        return mSizeType;
170    }
171   
172    inline llvm::ConstantInt * getSize(const size_t value) {
173        return llvm::ConstantInt::get(getSizeTy(), value);
174    }
175   
176    llvm::PointerType * getVoidPtrTy() const;
177
178    llvm::PointerType * getFILEptrTy();
179   
180    inline unsigned getCacheAlignment() const {
181        return mCacheLineAlignment;
182    }
183   
184    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
185
186    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
187
188    void CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage) {
189        if (LLVM_UNLIKELY(assertion->getType()->isVectorTy())) {
190            assertion = CreateBitCast(assertion, getIntNTy(assertion->getType()->getPrimitiveSizeInBits()));
191        }
192        return __CreateAssert(CreateIsNotNull(assertion), failureMessage);
193    }
194
195    void CreateAssertZero(llvm::Value * assertion, llvm::StringRef failureMessage) {
196        if (LLVM_UNLIKELY(assertion->getType()->isVectorTy())) {
197            assertion = CreateBitCast(assertion, getIntNTy(assertion->getType()->getPrimitiveSizeInBits()));
198        }
199        return __CreateAssert(CreateIsNull(assertion), failureMessage);
200    }
201
202    void CreateExit(const int exitCode);
203
204    llvm::BranchInst * CreateLikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90);
205
206    llvm::BranchInst * CreateUnlikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90) {
207        return CreateLikelyCondBr(Cond, True, False, 100 - probability);
208    }
209
210    llvm::BasicBlock * CreateBasicBlock(std::string && name);
211
212    virtual bool supportsIndirectBr() const {
213        return true;
214    }
215
216    llvm::Value * CreatePopcount(llvm::Value * bits);
217
218    llvm::Value * CreateCountForwardZeroes(llvm::Value * value);
219
220    llvm::Value * CreateCountReverseZeroes(llvm::Value * value);
221   
222    // Useful bit manipulation operations 
223    llvm::Value * CreateResetLowestBit(llvm::Value * bits);   
224   
225    llvm::Value * CreateIsolateLowestBit(llvm::Value * bits);
226   
227    llvm::Value * CreateMaskToLowestBitInclusive(llvm::Value * bits);
228   
229    llvm::Value * CreateMaskToLowestBitExclusive(llvm::Value * bits);
230   
231    llvm::Value * CreateExtractBitField(llvm::Value * bits, llvm::Value * start, llvm::Value * length);
232   
233    llvm::Value * CreateCeilLog2(llvm::Value * value);
234   
235    llvm::Value * CreateReadCycleCounter();
236
237    template <typename ExternalFunctionType>
238    llvm::Function * LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const;
239
240    #ifdef HAS_ADDRESS_SANITIZER
241    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, const char * Name);
242
243    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, const llvm::Twine & Name = "");
244
245    virtual llvm::LoadInst * CreateLoad(llvm::Type * Ty, llvm::Value * Ptr, const llvm::Twine & Name = "");
246
247    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, bool isVolatile, const llvm::Twine & Name = "");
248
249    virtual llvm::StoreInst * CreateStore(llvm::Value * Val, llvm::Value * Ptr, bool isVolatile = false);
250    #endif
251
252    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, const char * Name);
253
254    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, const llvm::Twine & Name = "");
255
256    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, bool isVolatile, const llvm::Twine & Name = "");
257
258    llvm::StoreInst * CreateAlignedStore(llvm::Value * Val, llvm::Value * Ptr, unsigned Align, bool isVolatile = false);
259
260    void setDriver(Driver * const driver) {
261        mDriver = driver;
262    }
263
264protected:
265
266    bool hasAlignedAlloc() const;
267
268    bool hasPosixMemalign() const;
269
270    bool hasAddressSanitizer() const;
271
272    void __CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage);
273
274    llvm::Function * LinkFunction(llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
275
276protected:
277
278    llvm::Module *                  mModule;
279    unsigned                        mCacheLineAlignment;
280    llvm::IntegerType * const       mSizeType;
281    llvm::StructType *              mFILEtype;
282    Driver *                        mDriver;   
283    llvm::LLVMContext               mContext;
284    const std::string               mTriple;
285};
286
287template <typename ExternalFunctionType>
288llvm::Function *CBuilder::LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const {
289    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(getContext());
290    assert ("FunctionTypeBuilder did not resolve a function type." && type);
291    return LinkFunction(name, type, reinterpret_cast<void *>(functionPtr));
292}
293
294#endif
Note: See TracBrowser for help on using the repository browser.