#ifndef _PASS_HELLO_H #define _PASS_HELLO_H #if LLVM_VERSION >= 33 #define ATTRIBUTE_SET_TY AttributeSet #include #include #include #include #include #include #include #include #include #include #include #include #else /* LLVM_VERSION < 33 */ #define ATTRIBUTE_SET_TY AttrListPtr #include #include #include #include #include #include #include #include #include #endif /* LLVM_VERSION >= 33 */ #if LLVM_VERSION >= 32 #define DATA_LAYOUT_TY DataLayout #define ATTRIBUTE_SET_RET_IDX ATTRIBUTE_SET_TY::ReturnIndex #define ATTRIBUTE_SET_FN_IDX ATTRIBUTE_SET_TY::FunctionIndex #include #if LLVM_VERSION == 32 #include #include #endif #else /* LLVM_VERSION < 32 */ #define DATA_LAYOUT_TY TargetData #define ATTRIBUTE_SET_RET_IDX 0 #define ATTRIBUTE_SET_FN_IDX (~0U) #include #include #include #endif /* LLVM_VERSION >= 32 */ #if LLVM_VERSION >= 31 /* XXX Check. */ #define CONSTANT_ARRAY_INITIALIZER_TY ConstantDataArray #else /* LLVM_VERSION < 31 */ #define CONSTANT_ARRAY_INITIALIZER_TY ConstantArray #endif /* LLVM_VERSION >= 31 */ #if LLVM_VERSION >= 30 #define BASE_PARSER parser #define TYPECONST #else /* LLVM_VERSION < 30 */ #define BASE_PARSER basic_parser #define TYPECONST const #endif /* LLVM_VERSION >= 30 */ #if LLVM_VERSION >= 29 #define VALUE_TO_VALUE_MAP_TY ValueToValueMapTy #else /* LLVM_VERSION < 29 */ #define VALUE_TO_VALUE_MAP_TY ValueMap #endif /* LLVM_VERSION >= 29 */ #define ZERO_CONSTANT_INT(M) ConstantInt::get((M).getContext(), APInt(32, 0, 10)) #define VOID_PTR_TY(M) PointerType::get(IntegerType::get((M).getContext(), 8), 0) #define VOID_PTR_PTR_TY(M) PointerType::get(PointerType::get(IntegerType::get((M).getContext(), 8), 0), 0) #define DEBUG_LLVM_DEBUG_API 0 typedef enum PassUtilLinkageTypeE { PASS_UTIL_LINKAGE_NONE = 0, PASS_UTIL_LINKAGE_WEAK, PASS_UTIL_LINKAGE_COMMON, PASS_UTIL_LINKAGE_EXTERNAL, PASS_UTIL_LINKAGE_EXTERNAL_WEAK, PASS_UTIL_LINKAGE_WEAK_POINTER, PASS_UTIL_LINKAGE_PRIVATE, __NUM_PASS_UTIL_LINKAGE_TYPES /* Values here should only be appended at the end, external components (e.g., scripts) may be relying on them.*/ } PassUtilLinkageType; #define PASS_UTIL_LINKAGE_TYPE_STRINGS \ "NONE", \ "WEAK", \ "COMMON", \ "EXTERNAL", \ "EXTERNAL_WEAK", \ "WEAK_POINTER", \ "PRIVATE" typedef enum PassUtilPropE { PASS_UTIL_PROP_NONE, PASS_UTIL_PROP_NOINLINE, PASS_UTIL_PROP_USED, PASS_UTIL_PROP_PRESERVE, __NUM_PASS_UTIL_PROPS } PassUtilProp; #define PASS_UTIL_FLAG(F) (1 << F) #define PASS_COMMON_INIT_ONCE() \ Module *PassUtil::M = NULL; \ using namespace llvm; namespace llvm { class PassUtil { public: static Constant* getGetElementPtrConstant(Constant *constant, std::vector &indexes); static CallInst* createCallInstruction(Value *F, std::vector &args, const Twine &NameStr="", Instruction *InsertBefore=0); static CallInst* createCallInstruction(Value *F, std::vector &args, const Twine &NameStr="", BasicBlock *InsertAtEnd=0); static FunctionType* getFunctionType(TYPECONST Type* Result, std::vector &argsTy, bool isVarArg=false); static Constant* getStringConstantArray(Module &M, const std::string &string); static GlobalVariable* getStringGlobalVariable(Module &M, const std::string &string, const std::string &varName, const std::string &varSection = "", Constant **getElementPtrExpr=NULL, bool cacheable=false); private: static Module *M; }; inline Constant* PassUtil::getGetElementPtrConstant(Constant *constant, std::vector &indexes) { #if LLVM_VERSION >= 30 ArrayRef ref(indexes); return ConstantExpr::getGetElementPtr(constant, ref); #else return ConstantExpr::getGetElementPtr(constant, &indexes[0], indexes.size()); #endif } inline CallInst* PassUtil::createCallInstruction(Value *F, std::vector &args, const Twine &NameStr, Instruction *InsertBefore) { #if LLVM_VERSION >= 30 ArrayRef ref(args); return CallInst::Create(F, ref, NameStr, InsertBefore); #else return CallInst::Create(F, args.begin(), args.end(), NameStr, InsertBefore); #endif } inline CallInst* PassUtil::createCallInstruction(Value *F, std::vector &args, const Twine &NameStr, BasicBlock *InsertAtEnd) { #if LLVM_VERSION >= 30 ArrayRef ref(args); return CallInst::Create(F, ref, NameStr, InsertAtEnd); #else return CallInst::Create(F, args.begin(), args.end(), NameStr, InsertAtEnd); #endif } inline FunctionType* PassUtil::getFunctionType(TYPECONST Type* Result, std::vector &argsTy, bool isVarArg) { #if LLVM_VERSION >= 30 ArrayRef ref(argsTy); return FunctionType::get(Result, ref, isVarArg); #else return FunctionType::get(Result, argsTy, isVarArg); #endif } inline Constant* PassUtil::getStringConstantArray(Module &M, const std::string &string) { std::vector elements; elements.reserve(string.size() + 1); for (unsigned i = 0; i < string.size(); ++i) elements.push_back(ConstantInt::get(Type::getInt8Ty(M.getContext()), string[i])); // Add a null terminator to the string... elements.push_back(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0)); ArrayType *ATy = ArrayType::get(Type::getInt8Ty(M.getContext()), elements.size()); return ConstantArray::get(ATy, elements); } inline GlobalVariable* PassUtil::getStringGlobalVariable(Module &M, const std::string &string, const std::string &varName, const std::string &varSection, Constant **getElementPtrExpr, bool cacheable) { static std::map stringCache; std::map::iterator stringCacheIt; std::string stringCacheKey; GlobalVariable *strGV = NULL; if (cacheable) { stringCacheKey = string + "~!~!" + varName + "~!~!" + varSection; stringCacheIt = stringCache.find(stringCacheKey); if (stringCacheIt != stringCache.end()) { strGV = stringCacheIt->second; cacheable = false; } } if (!strGV) { //create a constant internal string reference Constant *stringValue = PassUtil::getStringConstantArray(M, string); //create the global variable, cache it, and record it in the module strGV = new GlobalVariable(M, stringValue->getType(), true, GlobalValue::InternalLinkage, stringValue, varName); if (varSection.compare("")) { strGV->setSection(varSection); } } if (getElementPtrExpr) { std::vector strConstantIndices; strConstantIndices.push_back(ZERO_CONSTANT_INT(M)); strConstantIndices.push_back(ZERO_CONSTANT_INT(M)); *getElementPtrExpr = PassUtil::getGetElementPtrConstant(strGV, strConstantIndices); } if (cacheable) { stringCache.insert(std::pair(stringCacheKey, strGV)); } return strGV; } } #endif /* _PASS_HELLO_H */