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

Last change on this file since 5425 was 5425, checked in by nmedfort, 2 years ago

Changes towards separate compilation

File size: 8.3 KB
RevLine 
[5239]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
[5425]8#include <IR_Gen/FunctionTypeBuilder.h>
[5239]9#include <llvm/IR/IRBuilder.h>
[5260]10#include <llvm/IR/Constants.h>
[5425]11#include <llvm/ADT/Triple.h>
12
13namespace kernels { class KernelBuilder; }
[5260]14namespace llvm { class Function; }
15namespace llvm { class IntegerType; }
16namespace llvm { class Module; }
17namespace llvm { class PointerType; }
18namespace llvm { class Type; }
19namespace llvm { class Value; }
[5239]20
[5425]21class ParabixDriver;
22
[5260]23class CBuilder : public llvm::IRBuilder<> {
[5425]24    friend class ParabixDriver;
[5239]25public:
[5386]26
[5425]27    CBuilder(llvm::Module * const module, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes = 64);
[5239]28   
29    virtual ~CBuilder() {}
30
[5260]31    llvm::Module * getModule() const {
[5239]32        return mMod;
33    }
34   
[5425]35    void setModule(llvm::Module * m) {
36        assert (m);
[5239]37        mMod = m;
38    }
[5340]39
[5425]40
[5361]41    llvm::Value * CreateMalloc(llvm::Value * size);
42
43    llvm::Value * CreateAlignedMalloc(llvm::Value * size, const unsigned alignment);
[5280]44   
[5260]45    void CreateFree(llvm::Value * const ptr);
[5280]46   
[5283]47    void CreateAlignedFree(llvm::Value * const ptr, const bool testForNullAddress = false);
[5280]48   
[5260]49    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
[5245]50
[5337]51    llvm::CallInst * CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1) {
[5353]52        return CreateMemSet(ptr, getInt8(0), size, alignment);
[5337]53    }
54
55    llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
[5245]56        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
57        instr->setAlignment(getCacheAlignment());
58        return instr;
59    }
[5386]60
[5280]61    // stdio.h functions
62    //
[5308]63    //  Create a call to:  FILE * fopen(const char *filename, const char *mode);
[5280]64    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
[5281]65    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
66    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
[5280]67    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
68    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
69    //  Create a call to:  int fclose ( FILE * stream );
70    llvm::Value * CreateFCloseCall(llvm::Value * stream);
[5415]71    //  Create a call to:  int remove(const char *path);
72    llvm::Value * CreateRemoveCall(llvm::Value * path);
[5280]73   
[5415]74    //  Create a call to:  int rename(const char *old, const char *new);
75    llvm::Value * CreateRenameCall(llvm::Value * oldName, llvm::Value * newName);
76   
[5267]77    llvm::Function * GetPrintf();
78
[5422]79    llvm::Function * GetDprintf();
80
[5280]81    //  Create calls to unistd.h functions.
82    //
[5308]83    //  Create a call to:  int open(const char *filename, int oflag, ...);
84    llvm::Value * CreateOpenCall(llvm::Value * filename, llvm::Value * oflag, llvm::Value * mode);
[5280]85    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
[5418]86    llvm::Value * CreateWriteCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
[5281]87    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
[5418]88    llvm::Value * CreateReadCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
[5308]89    //  Create a call to:  int close(int filedes);
[5418]90    llvm::Value * CreateCloseCall(llvm::Value * fileDescriptor);
[5415]91    //  Create a call to:  int unlink(const char *path);
92    llvm::Value * CreateUnlinkCall(llvm::Value * path);
[5281]93
[5418]94    llvm::Value * CreateFileSize(llvm::Value * fileDescriptor);
95
[5415]96    //  Create calls to stdlib.h functions.
97    //
98    //  Create a call to:  int mkstemp (char *template);
99    llvm::Value * CreateMkstempCall(llvm::Value * ftemplate);
100   
101    //  Create a call to:  size_t strlen(const char *str);
102    llvm::Value * CreateStrlenCall(llvm::Value * str);
103   
[5386]104    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
105
106    llvm::Value * CreateFileSourceMMap(llvm::Value * fd, llvm::Value * size);
107
[5419]108    enum Advice {
109        ADVICE_NORMAL
110        , ADVICE_RANDOM
111        , ADVICE_SEQUENTIAL
112        , ADVICE_WILLNEED
113        , ADVICE_DONTNEED
[5411]114    };
115
[5419]116    llvm::Value * CreateMAdvise(llvm::Value * addr, llvm::Value * length, Advice advice);
[5411]117
[5389]118    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);
119
120    llvm::Value * CheckMMapSuccess(llvm::Value * const addr);
121
[5411]122    llvm::Value * CreateMRemap(llvm::Value * addr, llvm::Value * oldSize, llvm::Value * newSize);
[5386]123
124    llvm::Value * CreateMUnmap(llvm::Value * addr, llvm::Value * size);
125
[5280]126    //  Posix thread (pthread.h) functions.
127    //
128    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
129    //                    void *(*start_routine)(void*), void *arg);
[5260]130    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
[5280]131   
132    //  Create a call to:  void pthread_exit(void *value_ptr);
[5260]133    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
[5280]134   
135    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
[5260]136    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
[5242]137   
[5260]138    void CallPrintInt(const std::string & name, llvm::Value * const value);
[5239]139   
[5422]140    void CallPrintIntToStderr(const std::string & name, llvm::Value * const value);
141   
[5398]142    llvm::Value * GetString(llvm::StringRef Str);
[5422]143   
144    void CallPrintMsgToStderr(const std::string & message);
[5398]145
[5239]146    inline llvm::IntegerType * getSizeTy() const {
[5425]147        assert (mSizeType);
[5239]148        return mSizeType;
149    }
150   
[5267]151    inline llvm::ConstantInt * getSize(const size_t value) {
[5260]152        return llvm::ConstantInt::get(getSizeTy(), value);
[5239]153    }
154   
[5260]155    llvm::PointerType * getVoidPtrTy() const;
[5280]156    llvm::PointerType * getFILEptrTy();
[5239]157   
158    inline unsigned getCacheAlignment() const {
159        return mCacheLineAlignment;
160    }
161   
[5260]162    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
163
164    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
[5398]165
[5347]166    void CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage);
[5340]167
[5347]168    void CreateExit(const int exitCode);
169
[5350]170    llvm::BranchInst * CreateLikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90);
171
172    llvm::BranchInst * CreateUnlikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90) {
173        return CreateLikelyCondBr(Cond, True, False, 100 - probability);
174    }
175
176    bool supportsIndirectBr() const {
177        return mSupportsIndirectBr;
178    }
179
[5398]180    llvm::Value * CreatePopcount(llvm::Value * bits);
181
182    llvm::Value * CreateCountForwardZeroes(llvm::Value * value);
183
184    llvm::Value * CreateCountReverseZeroes(llvm::Value * value);
185
[5361]186    llvm::Value * CreateCeilLog2(llvm::Value * value);
[5424]187   
188    llvm::Value * CreateReadCycleCounter();
[5361]189
[5425]190    template <typename ExternalFunctionType>
191    llvm::Function * LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const;
192
[5239]193protected:
[5425]194
195    llvm::Function * LinkFunction(llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
196
197    void setDriver(ParabixDriver * driver) {
198        mDriver = driver;
199    }
200
201protected:
202    llvm::Module *                  mMod;   
[5368]203    unsigned                        mCacheLineAlignment;
204    llvm::IntegerType *             mSizeType;
205    llvm::StructType *              mFILEtype;
206    const bool                      mSupportsIndirectBr;
[5425]207    ParabixDriver *                 mDriver;
208    llvm::LLVMContext               mContext;
209    const std::string               mTriple;
[5239]210};
211
[5425]212template <typename ExternalFunctionType>
213llvm::Function *CBuilder::LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const {
214    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(getContext());
215    assert ("FunctionTypeBuilder did not resolve a function type." && type);
216    return LinkFunction(name, type, reinterpret_cast<void *>(functionPtr));
217}
218
[5239]219#endif
Note: See TracBrowser for help on using the repository browser.