add a way to have multiple modules in a JIT :)
[oota-llvm.git] / include / llvm / PassSupport.h
index 16115cf5f3518b2a60d7fb7d8892f59b264b6d76..c802fe2f0d58073decb3cd2896474ff740695fff 100644 (file)
@@ -1,5 +1,12 @@
 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
 // This file defines stuff that is used to define and "use" Passes.  This file
 // is automatically #included by Pass.h, so:
 //
 #ifndef LLVM_PASS_SUPPORT_H
 #define LLVM_PASS_SUPPORT_H
 
+#include "llvm/System/IncludeFile.h"
 // No need to include Pass.h, we are being included by it!
 
+namespace llvm {
+
 class TargetMachine;
 
 //===---------------------------------------------------------------------------
@@ -41,13 +51,13 @@ public:
   /// AnalysisGroup flag with others.
   ///
   enum {
-    Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
+    Analysis = 1, Optimization = 2, AnalysisGroup = 4
   };
 
   /// PassInfo ctor - Do not call this directly, this should only be invoked
   /// through RegisterPass.
-  PassInfo(const char *name, const char *arg, const std::type_info &ti, 
-           unsigned pt, Pass *(*normal)() = 0,
+  PassInfo(const char *name, const char *arg, const std::type_info &ti,
+           unsigned char pt, Pass *(*normal)() = 0,
            Pass *(*targetctor)(TargetMachine &) = 0)
     : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
       NormalCtor(normal), TargetCtor(targetctor)  {
@@ -77,7 +87,7 @@ public:
   /// getNormalCtor - Return a pointer to a function, that when called, creates
   /// an instance of the pass and returns it.  This pointer may be null if there
   /// is no default constructor for the pass.
-  /// 
+  ///
   Pass *(*getNormalCtor() const)() {
     return NormalCtor;
   }
@@ -126,7 +136,7 @@ public:
 /// for example will not be able to see the pass and attempts to create the pass
 /// will fail. This template is used in the follow manner (at global scope, in
 /// your .cpp file):
-/// 
+///
 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
 ///
 /// This statement will cause your pass to be created by calling the default
@@ -136,26 +146,40 @@ public:
 ///
 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); }
 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
-/// 
+///
 struct RegisterPassBase {
   /// getPassInfo - Get the pass info for the registered class...
   ///
-  const PassInfo *getPassInfo() const { return PIObj; }
+  const PassInfo *getPassInfo() const { return &PIObj; }
 
-  RegisterPassBase() : PIObj(0) {}
-  ~RegisterPassBase() {   // Intentionally non-virtual...
-    if (PIObj) unregisterPass(PIObj);
+  RegisterPassBase(const char *Name, const char *Arg, const std::type_info &TI,
+                   unsigned char PT, Pass *(*Normal)() = 0,
+                   Pass *(*TargetCtor)(TargetMachine &) = 0)
+    : PIObj(Name, Arg, TI, PT, Normal, TargetCtor) {
+    registerPass();
+  }
+  RegisterPassBase(const std::type_info &TI, unsigned char PT)
+    : PIObj("", "", TI, PT, 0, 0) {
+    // This ctor may only be used for analysis groups: it does not auto-register
+    // the pass.
+    assert(PT == PassInfo::AnalysisGroup && "Not an AnalysisGroup!");
+  }
+  
+  ~RegisterPassBase() {   // Intentionally non-virtual.
+    // Analysis groups are registered/unregistered by their dtor.
+    if (PIObj.getPassType() != PassInfo::AnalysisGroup)
+      unregisterPass();
   }
 
 protected:
-  PassInfo *PIObj;       // The PassInfo object for this pass
-  void registerPass(PassInfo *);
-  void unregisterPass(PassInfo *);
+  PassInfo PIObj;       // The PassInfo object for this pass
+  void registerPass();
+  void unregisterPass();
 
-  /// setPreservesCFG - Notice that this pass only depends on the CFG, so
+  /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so
   /// transformations that do not modify the CFG do not invalidate this pass.
   ///
-  void setPreservesCFG();
+  void setOnlyUsesCFG();
 };
 
 template<typename PassName>
@@ -163,32 +187,28 @@ Pass *callDefaultCtor() { return new PassName(); }
 
 template<typename PassName>
 struct RegisterPass : public RegisterPassBase {
-  
+
   // Register Pass using default constructor...
-  RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
-                              callDefaultCtor<PassName>));
-  }
+  RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy = 0)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy,
+                     callDefaultCtor<PassName>) {}
 
   // Register Pass using default constructor explicitly...
-  RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
-               Pass *(*ctor)()) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
-  }
+  RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy,
+               Pass *(*ctor)()) 
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, ctor) {}
 
   // Register Pass using TargetMachine constructor...
-  RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
-               Pass *(*targetctor)(TargetMachine &)) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
-                              0, targetctor));
-  }
+  RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy,
+               Pass *(*targetctor)(TargetMachine &))
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy,
+                     0, targetctor) {}
 
   // Generic constructor version that has an unknown ctor type...
   template<typename CtorType>
-  RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
-               CtorType *Fn) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
-  }
+  RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy,
+               CtorType *Fn)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 0) {}
 };
 
 /// RegisterOpt - Register something that is to show up in Opt, this is just a
@@ -196,25 +216,47 @@ struct RegisterPass : public RegisterPassBase {
 ///
 template<typename PassName>
 struct RegisterOpt : public RegisterPassBase {
-  RegisterOpt(const char *PassArg, const char *Name) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::Optimization,
-                              callDefaultCtor<PassName>));
+  RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization,
+                     callDefaultCtor<PassName>) {
+    if (CFGOnly) setOnlyUsesCFG();
   }
 
   /// Register Pass using default constructor explicitly...
   ///
-  RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::Optimization, ctor));
+  RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(),
+              bool CFGOnly = false) 
+  : RegisterPassBase(Name, PassArg, typeid(PassName),
+                     PassInfo::Optimization, ctor) {
+    if (CFGOnly) setOnlyUsesCFG();
+  }
+
+  /// Register FunctionPass using default constructor explicitly...
+  ///
+  RegisterOpt(const char *PassArg, const char *Name, FunctionPass *(*ctor)(),
+              bool CFGOnly = false)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization,
+                     static_cast<Pass*(*)()>(ctor)) {
+    if (CFGOnly) setOnlyUsesCFG();
   }
 
   /// Register Pass using TargetMachine constructor...
   ///
   RegisterOpt(const char *PassArg, const char *Name,
-               Pass *(*targetctor)(TargetMachine &)) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::Optimization, 0, targetctor));
+               Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false)
+  : RegisterPassBase(Name, PassArg, typeid(PassName),
+                     PassInfo::Optimization, 0, targetctor) {
+    if (CFGOnly) setOnlyUsesCFG();
+  }
+
+  /// Register FunctionPass using TargetMachine constructor...
+  ///
+  RegisterOpt(const char *PassArg, const char *Name,
+              FunctionPass *(*targetctor)(TargetMachine &),
+              bool CFGOnly = false)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 0,
+                     static_cast<Pass*(*)(TargetMachine&)>(targetctor)) {
+    if (CFGOnly) setOnlyUsesCFG();
   }
 };
 
@@ -227,39 +269,10 @@ struct RegisterOpt : public RegisterPassBase {
 template<typename PassName>
 struct RegisterAnalysis : public RegisterPassBase {
   RegisterAnalysis(const char *PassArg, const char *Name,
-                   bool CFGOnly = false) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::Analysis,
-                              callDefaultCtor<PassName>));
-    if (CFGOnly)
-      setPreservesCFG();
-  }
-};
-
-/// RegisterLLC - Register something that is to show up in LLC, this is just a
-/// shortcut for specifying RegisterPass...
-///
-template<typename PassName>
-struct RegisterLLC : public RegisterPassBase {
-  RegisterLLC(const char *PassArg, const char *Name) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::LLC,
-                              callDefaultCtor<PassName>));
-  }
-
-  /// Register Pass using default constructor explicitly...
-  ///
-  RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::LLC, ctor));
-  }
-
-  /// Register Pass using TargetMachine constructor...
-  ///
-  RegisterLLC(const char *PassArg, const char *Name,
-               Pass *(*datactor)(TargetMachine &)) {
-    registerPass(new PassInfo(Name, PassArg, typeid(PassName),
-                              PassInfo::LLC));
+                   bool CFGOnly = false)
+  : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Analysis,
+                     callDefaultCtor<PassName>) {
+    if (CFGOnly) setOnlyUsesCFG();
   }
 };
 
@@ -356,13 +369,6 @@ struct PassRegistrationListener {
 };
 
 
-//===---------------------------------------------------------------------------
-/// IncludeFile class - This class is used as a hack to make sure that the
-/// implementation of a header file is included into a tool that uses the
-/// header.  This is solely to overcome problems linking .a files and not
-/// getting the implementation of passes we need.
-///
-struct IncludeFile {
-  IncludeFile(void *);
-};
+} // End llvm namespace
+
 #endif