[cfe-commits] r56899 - in /cfe/trunk/lib/CodeGen: CodeGenModule.cpp CodeGenModule.h

Daniel Dunbar daniel at zuster.org
Tue Sep 30 17:49:24 PDT 2008


Author: ddunbar
Date: Tue Sep 30 19:49:24 2008
New Revision: 56899

URL: http://llvm.org/viewvc/llvm-project?rev=56899&view=rev
Log:
Add simple interface for protecting runtime functions from name
collisions.
 - Provide CodeGenModule::CreateRuntimeFunction which guarantees that
   the function it creates will have the provided name in the final
   module. This allows the runtime to have its functions protected
   from declarations of the same name in the source code.

 - One could argue that this is a reason to abuse the llvm::Module
   namespace for dealing with function redeclarations. However, that
   approach seems conceptually flawed to me. This one also happens to
   be somewhat more efficient.

No functionality change.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=56899&r1=56898&r2=56899&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Sep 30 19:49:24 2008
@@ -63,6 +63,7 @@
   EmitCtorList(GlobalCtors, "llvm.global_ctors");
   EmitCtorList(GlobalDtors, "llvm.global_dtors");
   EmitAnnotations();
+  BindRuntimeFunctions();
   // Run the verifier to check that the generated code is consistent.
   if (verifyModule(TheModule, llvm::PrintMessageAction)) {
     TheModule.dump();
@@ -70,6 +71,38 @@
   }
 }
 
+void CodeGenModule::BindRuntimeFunctions() {
+  // Deal with protecting runtime function names.
+  for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) {
+    llvm::Function *Fn = RuntimeFunctions[i].first;
+    const std::string &Name = RuntimeFunctions[i].second;
+    
+    // See if there is a conflict against a function.
+    llvm::Function *Conflict = TheModule.getFunction(Name);
+    if (Conflict) {
+      // Decide which version to take. If the conflict is a definition
+      // we are forced to take that, otherwise assume the runtime
+      // knows best.
+      if (!Conflict->isDeclaration()) {
+        llvm::Value *Casted = 
+          llvm::ConstantExpr::getBitCast(Conflict, Fn->getType());
+        Fn->replaceAllUsesWith(Casted);
+        Fn->eraseFromParent();
+      } else {
+        Fn->takeName(Conflict);
+        llvm::Value *Casted = 
+          llvm::ConstantExpr::getBitCast(Fn, Conflict->getType());
+        Conflict->replaceAllUsesWith(Casted);
+        Conflict->eraseFromParent();
+      }
+    } else {
+      // FIXME: There still may be conflicts with aliases and
+      // variables. 
+      Fn->setName(Name);
+    }
+  }
+}
+
 /// ErrorUnsupported - Print out an error that codegen doesn't support the
 /// specified stmt yet.
 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type,
@@ -655,6 +688,16 @@
   }
 }
 
+llvm::Function *
+CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy,
+                                     const std::string &Name) {
+  llvm::Function *Fn = llvm::Function::Create(FTy, 
+                                              llvm::Function::ExternalLinkage,
+                                              "", &TheModule);
+  RuntimeFunctions.push_back(std::make_pair(Fn, Name));
+  return Fn;
+}
+
 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
   // Make sure that this type is translated.
   Types.UpdateCompletedType(TD);

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=56899&r1=56898&r2=56899&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Sep 30 19:49:24 2008
@@ -73,6 +73,12 @@
   llvm::Function *MemMoveFn;
   llvm::Function *MemSetFn;
 
+  /// RuntimeFunctions - List of runtime functions whose names must be
+  /// protected from introducing conflicts. These functions should be
+  /// created unnamed, we will name them and patch up conflicts when
+  /// we release the module.
+  std::vector< std::pair<llvm::Function*, std::string> > RuntimeFunctions;
+
   /// GlobalDeclMap - Mapping of decl names global variables we have
   /// already emitted. Note that the entries in this map are the
   /// actual globals and therefore may not be of the same type as the
@@ -195,6 +201,11 @@
 
   void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
 
+  /// CreateRuntimeFunction - Create a new runtime function whose name
+  /// must be protected from collisions.
+  llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty, 
+                                        const std::string &Name);
+
   void UpdateCompletedType(const TagDecl *D);
   llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
   llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
@@ -263,6 +274,8 @@
   void EmitAliases(void);
   void EmitAnnotations(void);
   void EmitStatics(void);
+
+  void BindRuntimeFunctions();
 };
 }  // end namespace CodeGen
 }  // end namespace clang





More information about the cfe-commits mailing list