From 95af592a631f403e1458ec1155f89fc31011572c Mon Sep 17 00:00:00 2001 From: Jim Laskey Date: Wed, 7 Feb 2007 20:38:26 +0000 Subject: [PATCH] Automatically generating intrinsic declarations from Dan Gohman. Modified to construct FunctionType in separate function, and, have getDeclaration return a Function instead of a Constant. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34008 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Intrinsics.h | 13 ++++++ include/llvm/Intrinsics.td | 24 +++++++--- lib/VMCore/Function.cpp | 19 ++++++++ utils/TableGen/IntrinsicEmitter.cpp | 71 +++++++++++++++++++++++++++++ utils/TableGen/IntrinsicEmitter.h | 2 + 5 files changed, 122 insertions(+), 7 deletions(-) diff --git a/include/llvm/Intrinsics.h b/include/llvm/Intrinsics.h index e439476914f..8275b72676f 100644 --- a/include/llvm/Intrinsics.h +++ b/include/llvm/Intrinsics.h @@ -18,6 +18,10 @@ namespace llvm { +class FunctionType; +class Function; +class Module; + /// Intrinsic Namespace - This namespace contains an enum with a value for /// every intrinsic/builtin function known by LLVM. These enum values are /// returned by Function::getIntrinsicID(). @@ -36,6 +40,15 @@ namespace Intrinsic { /// Intrinsic::getName(ID) - Return the LLVM name for an intrinsic, such as /// "llvm.ppc.altivec.lvx". const char *getName(ID id); + + /// Intrinsic::getType(ID) - Return the function type for an intrinsic. + /// + const FunctionType *getType(ID id); + + /// Intrinsic::getDeclaration(M, ID) - Create or insert an LLVM Function + /// declaration for an intrinsic, and return it. + Function *getDeclaration(Module *M, ID id); + } // End Intrinsic namespace } // End llvm namespace diff --git a/include/llvm/Intrinsics.td b/include/llvm/Intrinsics.td index 126205a5b55..b00eec46630 100644 --- a/include/llvm/Intrinsics.td +++ b/include/llvm/Intrinsics.td @@ -68,6 +68,15 @@ class LLVMPackedType LLVMType ElTy = elty; } +class LLVMPointerType + : LLVMType{ + LLVMType ElTy = elty; +} + +class LLVMEmptyStructType + : LLVMType{ +} + def llvm_void_ty : LLVMType; def llvm_bool_ty : LLVMIntegerType; def llvm_i8_ty : LLVMIntegerType; @@ -76,9 +85,10 @@ def llvm_i32_ty : LLVMIntegerType; def llvm_i64_ty : LLVMIntegerType; def llvm_float_ty : LLVMType; def llvm_double_ty : LLVMType; -def llvm_ptr_ty : LLVMType; // i8* -def llvm_ptrptr_ty : LLVMType; // i8** -def llvm_descriptor_ty : LLVMType; // global* +def llvm_ptr_ty : LLVMPointerType; // i8* +def llvm_ptrptr_ty : LLVMPointerType; // i8** +def llvm_empty_ty : LLVMEmptyStructType; // { } +def llvm_descriptor_ty : LLVMPointerType; // { }* def llvm_v16i8_ty : LLVMPackedType; // 16 x i8 def llvm_v8i16_ty : LLVMPackedType; // 8 x i16 @@ -123,10 +133,10 @@ class GCCBuiltin { //===--------------- Variable Argument Handling Intrinsics ----------------===// // -def int_vastart : Intrinsic<[llvm_void_ty, llvm_ptrptr_ty], [], "llvm.va_start">; -def int_vacopy : Intrinsic<[llvm_void_ty, llvm_ptrptr_ty, llvm_ptrptr_ty], [], +def int_vastart : Intrinsic<[llvm_void_ty, llvm_ptr_ty], [], "llvm.va_start">; +def int_vacopy : Intrinsic<[llvm_void_ty, llvm_ptr_ty, llvm_ptr_ty], [], "llvm.va_copy">; -def int_vaend : Intrinsic<[llvm_void_ty, llvm_ptrptr_ty], [], "llvm.va_end">; +def int_vaend : Intrinsic<[llvm_void_ty, llvm_ptr_ty], [], "llvm.va_end">; //===------------------- Garbage Collection Intrinsics --------------------===// // @@ -216,7 +226,7 @@ def int_dbg_stoppoint : Intrinsic<[llvm_void_ty, def int_dbg_region_start : Intrinsic<[llvm_void_ty, llvm_descriptor_ty]>; def int_dbg_region_end : Intrinsic<[llvm_void_ty, llvm_descriptor_ty]>; def int_dbg_func_start : Intrinsic<[llvm_void_ty, llvm_descriptor_ty]>; -def int_dbg_declare : Intrinsic<[llvm_void_ty, llvm_ptr_ty, +def int_dbg_declare : Intrinsic<[llvm_void_ty, llvm_descriptor_ty, llvm_descriptor_ty]>; //===------------------ Exception Handling Intrinsics----------------------===// diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index cc0cefad66b..72237eb4404 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -183,6 +183,25 @@ const char *Intrinsic::getName(ID id) { return Table[id]; } +const FunctionType *Intrinsic::getType(ID id) { + const Type *ResultTy = NULL; + std::vector ArgTys; + std::vector Attrs; + bool IsVarArg = false; + +#define GET_INTRINSIC_GENERATOR +#include "llvm/Intrinsics.gen" +#undef GET_INTRINSIC_GENERATOR + + return FunctionType::get(ResultTy, ArgTys, IsVarArg, Attrs); +} + +Function *Intrinsic::getDeclaration(Module *M, ID id) { +// There can never be multiple globals with the same name of different types, +// because intrinsics must be a specific type. + return cast(M->getOrInsertFunction(getName(id), getType(id))); +} + Value *IntrinsicInst::StripPointerCasts(Value *Ptr) { if (ConstantExpr *CE = dyn_cast(Ptr)) { if (CE->getOpcode() == Instruction::BitCast) { diff --git a/utils/TableGen/IntrinsicEmitter.cpp b/utils/TableGen/IntrinsicEmitter.cpp index ab527521dc7..1a70b257f11 100644 --- a/utils/TableGen/IntrinsicEmitter.cpp +++ b/utils/TableGen/IntrinsicEmitter.cpp @@ -38,6 +38,9 @@ void IntrinsicEmitter::run(std::ostream &OS) { // Emit the intrinsic verifier. EmitVerifier(Ints, OS); + // Emit the intrinsic declaration generator. + EmitGenerator(Ints, OS); + // Emit mod/ref info for each function. EmitModRefInfo(Ints, OS); @@ -125,6 +128,25 @@ static bool EmitTypeVerify(std::ostream &OS, Record *ArgType) { return false; } +static void EmitTypeGenerate(std::ostream &OS, Record *ArgType) { + if (ArgType->isSubClassOf("LLVMIntegerType")) { + OS << "IntegerType::get(" << ArgType->getValueAsInt("Width") << ")"; + } else if (ArgType->isSubClassOf("LLVMPackedType")) { + OS << "PackedType::get("; + EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy")); + OS << ", " << ArgType->getValueAsInt("NumElts") << ")"; + } else if (ArgType->isSubClassOf("LLVMPointerType")) { + OS << "PointerType::get("; + EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy")); + OS << ")"; + } else if (ArgType->isSubClassOf("LLVMEmptyStructType")) { + OS << "StructType::get(std::vector())"; + } else { + OS << "Type::getPrimitiveType("; + OS << ArgType->getValueAsString("TypeVal") << ")"; + } +} + /// RecordListComparator - Provide a determinstic comparator for lists of /// records. namespace { @@ -188,6 +210,55 @@ void IntrinsicEmitter::EmitVerifier(const std::vector &Ints, OS << "#endif\n\n"; } +void IntrinsicEmitter::EmitGenerator(const std::vector &Ints, + std::ostream &OS) { + OS << "// Code for generating Intrinsic function declarations.\n"; + OS << "#ifdef GET_INTRINSIC_GENERATOR\n"; + OS << " switch (id) {\n"; + OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; + + // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical + // types. + typedef std::map, std::vector, + RecordListComparator> MapTy; + MapTy UniqueArgInfos; + + // Compute the unique argument type info. + for (unsigned i = 0, e = Ints.size(); i != e; ++i) + UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); + + // Loop through the array, emitting one generator for each batch. + for (MapTy::iterator I = UniqueArgInfos.begin(), + E = UniqueArgInfos.end(); I != E; ++I) { + for (unsigned i = 0, e = I->second.size(); i != e; ++i) { + OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " + << Ints[I->second[i]].Name << "\n"; + } + + const std::vector &ArgTypes = I->first; + unsigned N = ArgTypes.size(); + + if (ArgTypes[N-1]->getValueAsString("TypeVal") == "...") { + OS << " IsVarArg = true;\n"; + --N; + } + + OS << " ResultTy = "; + EmitTypeGenerate(OS, ArgTypes[0]); + OS << ";\n"; + + for (unsigned j = 1; j != N; ++j) { + OS << " ArgTys.push_back("; + EmitTypeGenerate(OS, ArgTypes[j]); + OS << ");\n"; + } + + OS << " break;\n"; + } + OS << " }\n"; + OS << "#endif\n\n"; +} + void IntrinsicEmitter::EmitModRefInfo(const std::vector &Ints, std::ostream &OS) { OS << "// BasicAliasAnalysis code.\n"; diff --git a/utils/TableGen/IntrinsicEmitter.h b/utils/TableGen/IntrinsicEmitter.h index ff97b4fe116..fa483ce6745 100644 --- a/utils/TableGen/IntrinsicEmitter.h +++ b/utils/TableGen/IntrinsicEmitter.h @@ -35,6 +35,8 @@ namespace llvm { std::ostream &OS); void EmitVerifier(const std::vector &Ints, std::ostream &OS); + void EmitGenerator(const std::vector &Ints, + std::ostream &OS); void EmitModRefInfo(const std::vector &Ints, std::ostream &OS); void EmitNoMemoryInfo(const std::vector &Ints,