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

Last change on this file since 5347 was 5347, checked in by nmedfort, 3 years ago

Added enable asserts (-ea) command line flag + restructured BlockOrientedKernels? to allow for inlined code.

File size: 4.6 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
[5260]8#include <string>
[5239]9#include <llvm/IR/IRBuilder.h>
[5260]10#include <llvm/IR/Constants.h>
11namespace llvm { class Function; }
12namespace llvm { class IntegerType; }
13namespace llvm { class Module; }
14namespace llvm { class PointerType; }
15namespace llvm { class Type; }
16namespace llvm { class Value; }
[5239]17
[5260]18class CBuilder : public llvm::IRBuilder<> {
[5239]19   
20public:
21   
[5267]22    CBuilder(llvm::Module * m, unsigned GeneralRegisterWidthInBits, unsigned CacheLineAlignmentInBytes = 64);
[5239]23   
24    virtual ~CBuilder() {}
25
[5260]26    llvm::Module * getModule() const {
[5239]27        return mMod;
28    }
29   
[5260]30    void setModule(llvm::Module * m)  {
[5239]31        mMod = m;
32    }
[5280]33   
[5260]34    llvm::Value * CreateMalloc(llvm::Type * type, llvm::Value * size);
[5340]35
[5260]36    llvm::Value * CreateAlignedMalloc(llvm::Type * type, llvm::Value * size, const unsigned alignment);
[5280]37   
[5260]38    void CreateFree(llvm::Value * const ptr);
[5280]39   
[5283]40    void CreateAlignedFree(llvm::Value * const ptr, const bool testForNullAddress = false);
[5280]41   
[5260]42    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
[5245]43
[5337]44    llvm::CallInst * CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1) {
45        return CreateMemSet(CreatePointerCast(ptr, getInt8PtrTy()), getInt8(0), size, alignment);
46    }
47
48    llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
[5245]49        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
50        instr->setAlignment(getCacheAlignment());
51        return instr;
52    }
[5239]53   
[5280]54    // stdio.h functions
55    //
[5308]56    //  Create a call to:  FILE * fopen(const char *filename, const char *mode);
[5280]57    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
[5281]58    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
59    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
[5280]60    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
61    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
62    //  Create a call to:  int fclose ( FILE * stream );
63    llvm::Value * CreateFCloseCall(llvm::Value * stream);
64   
[5267]65    llvm::Function * GetPrintf();
66
[5280]67    //  Create calls to unistd.h functions.
68    //
[5308]69    //  Create a call to:  int open(const char *filename, int oflag, ...);
70    llvm::Value * CreateOpenCall(llvm::Value * filename, llvm::Value * oflag, llvm::Value * mode);
[5280]71    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
[5260]72    llvm::Value * CreateWriteCall(llvm::Value * fildes, llvm::Value * buf, llvm::Value * nbyte);
[5281]73    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
74    llvm::Value * CreateReadCall(llvm::Value * fildes, llvm::Value * buf, llvm::Value * nbyte);
[5308]75    //  Create a call to:  int close(int filedes);
76    llvm::Value * CreateCloseCall(llvm::Value * fildes);
[5281]77
[5280]78    //  Posix thread (pthread.h) functions.
79    //
80    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
81    //                    void *(*start_routine)(void*), void *arg);
[5260]82    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
[5280]83   
84    //  Create a call to:  void pthread_exit(void *value_ptr);
[5260]85    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
[5280]86   
87    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
[5260]88    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
[5242]89   
[5260]90    void CallPrintInt(const std::string & name, llvm::Value * const value);
[5239]91   
92    inline llvm::IntegerType * getSizeTy() const {
93        return mSizeType;
94    }
95   
[5267]96    inline llvm::ConstantInt * getSize(const size_t value) {
[5260]97        return llvm::ConstantInt::get(getSizeTy(), value);
[5239]98    }
99   
[5260]100    llvm::PointerType * getVoidPtrTy() const;
[5280]101    llvm::PointerType * getFILEptrTy();
[5239]102   
103    inline unsigned getCacheAlignment() const {
104        return mCacheLineAlignment;
105    }
106   
[5260]107    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
108
109    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
[5239]110   
[5347]111    void CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage);
[5340]112
[5347]113    void CreateExit(const int exitCode);
114
[5239]115protected:
[5260]116    llvm::Module *      mMod;
[5239]117    unsigned            mCacheLineAlignment;
[5260]118    llvm::IntegerType * mSizeType;
[5280]119    llvm::StructType *  mFILEtype;
[5239]120};
121
122#endif
Note: See TracBrowser for help on using the repository browser.