Util.h 3.46 KB
Newer Older
guruhegde's avatar
guruhegde committed
1 2 3
#ifndef TAS_UTIL_H
#define TAS_UTIL_H

4
#include <llvm/ADT/SmallVector.h>
guruhegde's avatar
guruhegde committed
5
#include <llvm/Analysis/LoopInfo.h>
6
#include <llvm/IR/Instruction.h>
guruhegde's avatar
guruhegde committed
7
#include <llvm/IR/Module.h>
8
#include <llvm/IR/Function.h>
guruhegde's avatar
guruhegde committed
9
#include <llvm/IR/Value.h>
guruhegde's avatar
guruhegde committed
10
#include <llvm/Transforms/Utils/ValueMapper.h>
11

guruhegde's avatar
guruhegde committed
12 13 14
#include <map>
#include <string>

guruhegde's avatar
guruhegde committed
15 16
namespace tas {

guruhegde's avatar
guruhegde committed
17 18 19 20 21 22 23
class TasException {
public:
  llvm::Function * F_state;
  TasException(llvm::Function * F_) : F_state(F_) {}
  void dump() { F_state->print(llvm::errs()); }
};

24 25 26 27 28 29
enum class AnnotationType {
  UNKNOWN,
  EXPENSIVE_PTR,
  BATCH_ARG
};

guruhegde's avatar
guruhegde committed
30 31
void getAnnotatedFnList(llvm::Module * M, std::map<llvm::Function *,
                              std::string> & FnList);
guruhegde's avatar
guruhegde committed
32

guruhegde's avatar
guruhegde committed
33 34
void setAnnotationInFunctionObject(llvm::Module * M);

guruhegde's avatar
guruhegde committed
35 36
void cloneLoopBasicBlocks(llvm::Function * F, llvm::Loop * L,
                          llvm::ValueToValueMapTy & VMap);
37

38
void insertLLVMPrefetchIntrinsic(llvm::Function * F, llvm::Instruction * I);
guruhegde's avatar
guruhegde committed
39

guruhegde's avatar
guruhegde committed
40 41
void replaceUsesWithinBB(llvm::Value * From, llvm::Value * To,
                         llvm::BasicBlock * BB);
42 43

unsigned getTypeSizeInBits(llvm::Type * Ty);
44

45
llvm::SmallVector<llvm::Value *, 4> detectExpPtrVars(llvm::Function * F);
46

guruhegde's avatar
guruhegde committed
47 48 49
llvm::SmallVector<llvm::LoadInst *, 4>
detectExpPtrUses(llvm::SmallVectorImpl<llvm::Value *> & AnnotatedVars);

guruhegde's avatar
guruhegde committed
50 51
void detectBatchParameters(llvm::Function * F,
                          llvm::SmallPtrSet<llvm::Value *, 4> & BatchParameters);
52

guruhegde's avatar
guruhegde committed
53 54
llvm::Instruction * findBatchBeginMarkerInstruction(llvm::Function * F);

55
llvm::Value * createArray(llvm::Function * F, llvm::Type * Ty, unsigned size);
56 57 58

template <typename InstType>
llvm::Instruction * findValueFirstUseInInstruction(llvm::Value * V);
guruhegde's avatar
guruhegde committed
59 60

unsigned getGEPIndex(const llvm::GetElementPtrInst * GEP);
guruhegde's avatar
guruhegde committed
61 62

llvm::StoreInst * findFirstUseInStoreInst(llvm::Value * V);
guruhegde's avatar
guruhegde committed
63

guruhegde's avatar
guruhegde committed
64 65
void setSuccessor(llvm::BasicBlock * BB, llvm::BasicBlock * SuccBB,
                  unsigned Idx = 0);
guruhegde's avatar
guruhegde committed
66

guruhegde's avatar
guruhegde committed
67 68
void cloneBasicBlocksInto(llvm::Function * From, llvm::Function * To);

69 70 71 72

void getReturnBlocks(llvm::Function * F,
                     llvm::SmallVectorImpl<llvm::BasicBlock *> & Returns);

guruhegde's avatar
guruhegde committed
73 74
void getReturnInstList(llvm::Function * F,
                       llvm::SmallVectorImpl<llvm::ReturnInst *> & Result);
75

guruhegde's avatar
guruhegde committed
76
std::string writeToBitCodeFile(llvm::Module & M);
guruhegde's avatar
guruhegde committed
77 78

std::string writeToAsmFile(llvm::Module & M);
79

guruhegde's avatar
guruhegde committed
80 81
llvm::Value * addIncrementIndexOp(llvm::AllocaInst * IdxPtr,
                                  llvm::Instruction * InsertBefore);
guruhegde's avatar
guruhegde committed
82 83

void printRegeionInfo(llvm::Function * F);
guruhegde's avatar
guruhegde committed
84

85 86
std::pair<llvm::BasicBlock *, llvm::BasicBlock *>
unifyFunctionExitNodes(llvm::Function & F);
guruhegde's avatar
guruhegde committed
87 88

template <typename InstType>
89 90 91 92 93 94 95
auto findLastUseOfValueInInstType(llvm::Value * V) {
  auto FU = find_if(V->users(),
            [&] (const auto * U) {
              return llvm::isa<InstType>(U)?true:false;
            });
  return llvm::cast<InstType>(*FU);
}
guruhegde's avatar
guruhegde committed
96 97

template <typename InstType>
98 99 100 101 102 103 104 105
auto findFirstUseOfValueInInstType(llvm::Value * V) {
  const InstType * LU = nullptr;
  for_each(V->users(),
          [&] (const auto * U) {
          if (llvm::isa<InstType>(U)) LU = llvm::cast<InstType>(U);
        });
  return LU;
}
guruhegde's avatar
guruhegde committed
106

guruhegde's avatar
guruhegde committed
107
llvm::AllocaInst * getLoopIndexVar(llvm::Loop * L);
108

guruhegde's avatar
guruhegde committed
109
const llvm::LoadInst * findEarliestPointerDerefInstruction(llvm::Value * V);
110

guruhegde's avatar
guruhegde committed
111 112 113
void visitSuccessor(llvm::SmallVectorImpl<llvm::BasicBlock *> & Blocks,
                    llvm::BasicBlock * CurBlock,
                    llvm::BasicBlock * EndBlock);
guruhegde's avatar
guruhegde committed
114

guruhegde's avatar
guruhegde committed
115
llvm::AllocaInst * getLoopTripCount(llvm::Loop * L0);
116

guruhegde's avatar
guruhegde committed
117
llvm::BasicBlock * getPreLoopBlock(llvm::Loop * L);
118

guruhegde's avatar
guruhegde committed
119 120 121
} // namespace tas

#endif