[llvm-commits] CVS: llvm/lib/Target/Sparc/PreSelection.cpp

Chris Lattner lattner at cs.uiuc.edu
Tue Oct 21 09:50:03 PDT 2003


Changes in directory llvm/lib/Target/Sparc:

PreSelection.cpp updated: 1.18 -> 1.19

---
Log message:

Preselection is _not_ a basicblock pass, because it adds global variables to 
the module.  This change converts it from being a basic block pass to being
a simple pass.  This allows elimination of the annotation and simplification
of the logic for moving constants into global variables.


---
Diffs of the changes:  (+22 -101)

Index: llvm/lib/Target/Sparc/PreSelection.cpp
diff -u llvm/lib/Target/Sparc/PreSelection.cpp:1.18 llvm/lib/Target/Sparc/PreSelection.cpp:1.19
--- llvm/lib/Target/Sparc/PreSelection.cpp:1.18	Mon Oct 20 14:43:17 2003
+++ llvm/lib/Target/Sparc/PreSelection.cpp	Tue Oct 21 09:49:19 2003
@@ -27,125 +27,48 @@
 #include "llvm/iOther.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Pass.h"
-#include "Support/CommandLine.h"
 #include <algorithm>
 
 namespace {
-  //===--------------------------------------------------------------------===//
-  // SelectDebugLevel - Allow command line control over debugging.
-  //
-  enum PreSelectDebugLevel_t {
-    PreSelect_NoDebugInfo,
-    PreSelect_PrintOutput, 
-  };
-
-  // Enable Debug Options to be specified on the command line
-  cl::opt<PreSelectDebugLevel_t>
-  PreSelectDebugLevel("dpreselect", cl::Hidden,
-     cl::desc("debug information for target-dependent pre-selection"),
-     cl::values(
-       clEnumValN(PreSelect_NoDebugInfo, "n", "disable debug output (default)"),
-       clEnumValN(PreSelect_PrintOutput, "y", "print generated machine code"),
-       /* default level = */ PreSelect_NoDebugInfo));
-
 
   //===--------------------------------------------------------------------===//
-  // class ConstantPoolForModule:
-  // 
-  // The pool of constants that must be emitted for a module.
-  // This is a single pool for the entire module and is shared by
-  // all invocations of the PreSelection pass for this module by putting
-  // this as an annotation on the Module object.
-  // A single GlobalVariable is created for each constant in the pool
-  // representing the memory for that constant.  
+  // PreSelection Pass - Specialize LLVM code for the current target machine.
   // 
-  AnnotationID CPFM_AID(
-                 AnnotationManager::getID("CodeGen::ConstantPoolForModule"));
+  class PreSelection : public Pass, public InstVisitor<PreSelection> {
+    const TargetInstrInfo &instrInfo;
+    Module *TheModule;
 
-  class ConstantPoolForModule : private Annotation {
-    Module* myModule;
     std::map<const Constant*, GlobalVariable*> gvars;
-    std::map<const Constant*, GlobalVariable*> origGVars;
-    ConstantPoolForModule(Module* M);   // called only by annotation builder
-    ConstantPoolForModule();                      // DO NOT IMPLEMENT
-    void operator=(const ConstantPoolForModule&); // DO NOT IMPLEMENT
-  public:
-    static ConstantPoolForModule& get(Module* M) {
-      ConstantPoolForModule* cpool =
-        (ConstantPoolForModule*) M->getAnnotation(CPFM_AID);
-      if (cpool == NULL) // create a new annotation and add it to the Module
-        M->addAnnotation(cpool = new ConstantPoolForModule(M));
-      return *cpool;
-    }
 
     GlobalVariable* getGlobalForConstant(Constant* CV) {
       std::map<const Constant*, GlobalVariable*>::iterator I = gvars.find(CV);
-      if (I != gvars.end())
-        return I->second;               // global exists so return it
-      return addToConstantPool(CV);     // create a new global and return it
-    }
+      if (I != gvars.end()) return I->second;    // global exists so return it
 
-    GlobalVariable*  addToConstantPool(Constant* CV) {
-      GlobalVariable*& GV = gvars[CV];  // handle to global var entry in map
-      if (GV == NULL)
-        { // check if a global constant already existed; otherwise create one
-          std::map<const Constant*, GlobalVariable*>::iterator PI =
-            origGVars.find(CV);
-          if (PI != origGVars.end())
-            GV = PI->second;            // put in map
-          else
-            {
-              GV = new GlobalVariable(CV->getType(), true, //put in map
-                                      GlobalValue::InternalLinkage, CV);
-              myModule->getGlobalList().push_back(GV); // GV owned by module now
-            }
-        }
-      return GV;
+      return I->second = new GlobalVariable(CV->getType(), true,
+                                            GlobalValue::InternalLinkage, CV,
+                                            "immcst", TheModule);
     }
-  };
 
-  /* ctor */
-  ConstantPoolForModule::ConstantPoolForModule(Module* M)
-    : Annotation(CPFM_AID), myModule(M)
-  {
-    // Build reverse map for pre-existing global constants so we can find them
-    for (Module::giterator GI = M->gbegin(), GE = M->gend(); GI != GE; ++GI)
-      if (GI->hasInitializer() && GI->isConstant())
-        origGVars[GI->getInitializer()] = GI;
-  }
+  public:
+    PreSelection(const TargetMachine &T)
+      : instrInfo(T.getInstrInfo()), TheModule(0) {}
 
-  //===--------------------------------------------------------------------===//
-  // PreSelection Pass - Specialize LLVM code for the current target machine.
-  // 
-  class PreSelection : public BasicBlockPass, public InstVisitor<PreSelection>
-  {
-    const TargetMachine ⌖
-    const TargetInstrInfo &instrInfo;
-    Function* function;
+    // runOnBasicBlock - apply this pass to each BB
+    bool run(Module &M) {
+      TheModule = &M;
 
-    GlobalVariable* getGlobalForConstant(Constant* CV) {
-      Module* M = function->getParent();
-      return ConstantPoolForModule::get(M).getGlobalForConstant(CV);
-    }
+      // Build reverse map for pre-existing global constants so we can find them
+      for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
+        if (I->hasInitializer() && I->isConstant())
+          gvars[I->getInitializer()] = I;
 
-  public:
-    PreSelection (const TargetMachine &T):
-      target(T), instrInfo(T.getInstrInfo()), function(NULL) {}
+      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+        visit(*I);
 
-    // runOnBasicBlock - apply this pass to each BB
-    bool runOnBasicBlock(BasicBlock &BB) {
-      function = BB.getParent();
-      this->visit(BB);
+      gvars.clear();
       return true;
     }
 
-    bool doFinalization(Function &F) {
-      if (PreSelectDebugLevel >= PreSelect_PrintOutput)
-        std::cerr << "\n\n*** LLVM code after pre-selection for function "
-                  << F.getName() << ":\n\n" << F;
-      return false;
-    }
-
     // These methods do the actual work of specializing code
     void visitInstruction(Instruction &I);   // common work for every instr. 
     void visitGetElementPtrInst(GetElementPtrInst &I);
@@ -357,9 +280,7 @@
 // createPreSelectionPass - Public entrypoint for pre-selection pass
 // and this file as a whole...
 //
-Pass*
-createPreSelectionPass(TargetMachine &T)
-{
+Pass* createPreSelectionPass(TargetMachine &T) {
   return new PreSelection(T);
 }
 





More information about the llvm-commits mailing list