summaryrefslogtreecommitdiff
path: root/lib/Target/CppBackend
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2011-07-09 17:41:24 +0000
committerChris Lattner <sabre@nondot.org>2011-07-09 17:41:24 +0000
commit1afcace3a3a138b1b18e5c6270caa8dae2261ae2 (patch)
tree2fed26ec8965151524b81246c7fa7c3e2382fd31 /lib/Target/CppBackend
parentc36ed70ec5c3c99f9559cfaa199373f60219a2be (diff)
downloadllvm-1afcace3a3a138b1b18e5c6270caa8dae2261ae2.tar.gz
llvm-1afcace3a3a138b1b18e5c6270caa8dae2261ae2.tar.bz2
llvm-1afcace3a3a138b1b18e5c6270caa8dae2261ae2.tar.xz
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it is through diffstat: 109 files changed, 3005 insertions(+), 5906 deletions(-) Removing almost 3K lines of code is a good thing. Other advantages include: 1. Value::getType() is a simple load that can be CSE'd, not a mutating union-find operation. 2. Types a uniqued and never move once created, defining away PATypeHolder. 3. Structs can be "named" now, and their name is part of the identity that uniques them. This means that the compiler doesn't merge them structurally which makes the IR much less confusing. 4. Now that there is no way to get a cycle in a type graph without a named struct type, "upreferences" go away. 5. Type refinement is completely gone, which should make LTO much MUCH faster in some common cases with C++ code. 6. Types are now generally immutable, so we can use "Type *" instead "const Type *" everywhere. Downsides of this patch are that it removes some functions from the C API, so people using those will have to upgrade to (not yet added) new API. "LLVM 3.0" is the right time to do this. There are still some cleanups pending after this, this patch is large enough as-is. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Target/CppBackend')
-rw-r--r--lib/Target/CppBackend/CPPBackend.cpp88
1 files changed, 25 insertions, 63 deletions
diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp
index 351f12225c..c4280ef5a2 100644
--- a/lib/Target/CppBackend/CPPBackend.cpp
+++ b/lib/Target/CppBackend/CPPBackend.cpp
@@ -22,7 +22,7 @@
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/PassManager.h"
-#include "llvm/TypeSymbolTable.h"
+#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/CommandLine.h"
@@ -33,7 +33,7 @@
#include "llvm/Config/config.h"
#include <algorithm>
#include <set>
-
+#include <map>
using namespace llvm;
static cl::opt<std::string>
@@ -193,26 +193,11 @@ static std::string getTypePrefix(const Type *Ty) {
case Type::ArrayTyID: return "array_";
case Type::PointerTyID: return "ptr_";
case Type::VectorTyID: return "packed_";
- case Type::OpaqueTyID: return "opaque_";
default: return "other_";
}
return "unknown_";
}
-// Looks up the type in the symbol table and returns a pointer to its name or
-// a null pointer if it wasn't found. Note that this isn't the same as the
-// Mode::getTypeName function which will return an empty string, not a null
-// pointer if the name is not found.
-static const std::string *
-findTypeName(const TypeSymbolTable& ST, const Type* Ty) {
- TypeSymbolTable::const_iterator TI = ST.begin();
- TypeSymbolTable::const_iterator TE = ST.end();
- for (;TI != TE; ++TI)
- if (TI->second == Ty)
- return &(TI->first);
- return 0;
-}
-
void CppWriter::error(const std::string& msg) {
report_fatal_error(msg);
}
@@ -384,18 +369,20 @@ std::string CppWriter::getCppName(const Type* Ty) {
case Type::StructTyID: prefix = "StructTy_"; break;
case Type::ArrayTyID: prefix = "ArrayTy_"; break;
case Type::PointerTyID: prefix = "PointerTy_"; break;
- case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
case Type::VectorTyID: prefix = "VectorTy_"; break;
default: prefix = "OtherTy_"; break; // prevent breakage
}
// See if the type has a name in the symboltable and build accordingly
- const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
std::string name;
- if (tName)
- name = std::string(prefix) + *tName;
- else
- name = std::string(prefix) + utostr(uniqueNum++);
+ if (const StructType *STy = dyn_cast<StructType>(Ty))
+ if (STy->hasName())
+ name = STy->getName();
+
+ if (name.empty())
+ name = utostr(uniqueNum++);
+
+ name = std::string(prefix) + name;
sanitize(name);
// Save the name
@@ -590,8 +577,18 @@ bool CppWriter::printTypeInternal(const Type* Ty) {
Out << ");";
nl(Out);
}
- Out << "StructType* " << typeName << " = StructType::get("
- << typeName << "_fields, /*isPacked=*/"
+
+ Out << "StructType *" << typeName << " = ";
+ if (ST->isAnonymous()) {
+ Out << "StructType::get(" << "mod->getContext(), ";
+ } else {
+ Out << "StructType::createNamed(mod->getContext(), \"";
+ printEscapedString(ST->getName());
+ Out << "\");";
+ nl(Out);
+ Out << typeName << "->setBody(";
+ }
+ Out << typeName << "_fields, /*isPacked=*/"
<< (ST->isPacked() ? "true" : "false") << ");";
nl(Out);
break;
@@ -629,25 +626,10 @@ bool CppWriter::printTypeInternal(const Type* Ty) {
nl(Out);
break;
}
- case Type::OpaqueTyID: {
- Out << "OpaqueType* " << typeName;
- Out << " = OpaqueType::get(mod->getContext());";
- nl(Out);
- break;
- }
default:
error("Invalid TypeID");
}
- // If the type had a name, make sure we recreate it.
- const std::string* progTypeName =
- findTypeName(TheModule->getTypeSymbolTable(),Ty);
- if (progTypeName) {
- Out << "mod->addTypeName(\"" << *progTypeName << "\", "
- << typeName << ");";
- nl(Out);
- }
-
// Pop us off the type stack
TypeStack.pop_back();
@@ -670,7 +652,6 @@ bool CppWriter::printTypeInternal(const Type* Ty) {
case Type::StructTyID: Out << "StructType"; break;
case Type::VectorTyID: Out << "VectorType"; break;
case Type::PointerTyID: Out << "PointerType"; break;
- case Type::OpaqueTyID: Out << "OpaqueType"; break;
default: Out << "NoSuchDerivedType"; break;
}
Out << ">(" << I->second << "_fwd.get());";
@@ -695,26 +676,7 @@ void CppWriter::printType(const Type* Ty) {
}
void CppWriter::printTypes(const Module* M) {
- // Walk the symbol table and print out all its types
- const TypeSymbolTable& symtab = M->getTypeSymbolTable();
- for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
- TI != TE; ++TI) {
-
- // For primitive types and types already defined, just add a name
- TypeMap::const_iterator TNI = TypeNames.find(TI->second);
- if (TI->second->isIntegerTy() || TI->second->isPrimitiveType() ||
- TNI != TypeNames.end()) {
- Out << "mod->addTypeName(\"";
- printEscapedString(TI->first);
- Out << "\", " << getCppName(TI->second) << ");";
- nl(Out);
- // For everything else, define the type
- } else {
- printType(TI->second);
- }
- }
-
- // Add all of the global variables to the value table...
+ // Add all of the global variables to the value table.
for (Module::const_global_iterator I = TheModule->global_begin(),
E = TheModule->global_end(); I != E; ++I) {
if (I->hasInitializer())
@@ -1959,8 +1921,8 @@ void CppWriter::printVariable(const std::string& fname,
Out << "}\n";
}
-void CppWriter::printType(const std::string& fname,
- const std::string& typeName) {
+void CppWriter::printType(const std::string &fname,
+ const std::string &typeName) {
const Type* Ty = TheModule->getTypeByName(typeName);
if (!Ty) {
error(std::string("Type '") + typeName + "' not found in input module");