llvm.org GIT mirror llvm / dac9131
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9320 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
1 changed file(s) with 25 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
2626 #include "llvm/iOther.h"
2727 #include "llvm/DerivedTypes.h"
2828 #include "llvm/Pass.h"
29 #include "Support/CommandLine.h"
3029 #include
3130
3231 namespace {
33 //===--------------------------------------------------------------------===//
34 // SelectDebugLevel - Allow command line control over debugging.
35 //
36 enum PreSelectDebugLevel_t {
37 PreSelect_NoDebugInfo,
38 PreSelect_PrintOutput,
39 };
40
41 // Enable Debug Options to be specified on the command line
42 cl::opt
43 PreSelectDebugLevel("dpreselect", cl::Hidden,
44 cl::desc("debug information for target-dependent pre-selection"),
45 cl::values(
46 clEnumValN(PreSelect_NoDebugInfo, "n", "disable debug output (default)"),
47 clEnumValN(PreSelect_PrintOutput, "y", "print generated machine code"),
48 /* default level = */ PreSelect_NoDebugInfo));
49
50
51 //===--------------------------------------------------------------------===//
52 // class ConstantPoolForModule:
53 //
54 // The pool of constants that must be emitted for a module.
55 // This is a single pool for the entire module and is shared by
56 // all invocations of the PreSelection pass for this module by putting
57 // this as an annotation on the Module object.
58 // A single GlobalVariable is created for each constant in the pool
59 // representing the memory for that constant.
60 //
61 AnnotationID CPFM_AID(
62 AnnotationManager::getID("CodeGen::ConstantPoolForModule"));
63
64 class ConstantPoolForModule : private Annotation {
65 Module* myModule;
66 std::map gvars;
67 std::map origGVars;
68 ConstantPoolForModule(Module* M); // called only by annotation builder
69 ConstantPoolForModule(); // DO NOT IMPLEMENT
70 void operator=(const ConstantPoolForModule&); // DO NOT IMPLEMENT
71 public:
72 static ConstantPoolForModule& get(Module* M) {
73 ConstantPoolForModule* cpool =
74 (ConstantPoolForModule*) M->getAnnotation(CPFM_AID);
75 if (cpool == NULL) // create a new annotation and add it to the Module
76 M->addAnnotation(cpool = new ConstantPoolForModule(M));
77 return *cpool;
78 }
79
80 GlobalVariable* getGlobalForConstant(Constant* CV) {
81 std::map::iterator I = gvars.find(CV);
82 if (I != gvars.end())
83 return I->second; // global exists so return it
84 return addToConstantPool(CV); // create a new global and return it
85 }
86
87 GlobalVariable* addToConstantPool(Constant* CV) {
88 GlobalVariable*& GV = gvars[CV]; // handle to global var entry in map
89 if (GV == NULL)
90 { // check if a global constant already existed; otherwise create one
91 std::map::iterator PI =
92 origGVars.find(CV);
93 if (PI != origGVars.end())
94 GV = PI->second; // put in map
95 else
96 {
97 GV = new GlobalVariable(CV->getType(), true, //put in map
98 GlobalValue::InternalLinkage, CV);
99 myModule->getGlobalList().push_back(GV); // GV owned by module now
100 }
101 }
102 return GV;
103 }
104 };
105
106 /* ctor */
107 ConstantPoolForModule::ConstantPoolForModule(Module* M)
108 : Annotation(CPFM_AID), myModule(M)
109 {
110 // Build reverse map for pre-existing global constants so we can find them
111 for (Module::giterator GI = M->gbegin(), GE = M->gend(); GI != GE; ++GI)
112 if (GI->hasInitializer() && GI->isConstant())
113 origGVars[GI->getInitializer()] = GI;
114 }
11532
11633 //===--------------------------------------------------------------------===//
11734 // PreSelection Pass - Specialize LLVM code for the current target machine.
11835 //
119 class PreSelection : public BasicBlockPass, public InstVisitor
120 {
121 const TargetMachine ⌖
36 class PreSelection : public Pass, public InstVisitor {
12237 const TargetInstrInfo &instrInfo;
123 Function* function;
38 Module *TheModule;
39
40 std::map gvars;
12441
12542 GlobalVariable* getGlobalForConstant(Constant* CV) {
126 Module* M = function->getParent();
127 return ConstantPoolForModule::get(M).getGlobalForConstant(CV);
43 std::map::iterator I = gvars.find(CV);
44 if (I != gvars.end()) return I->second; // global exists so return it
45
46 return I->second = new GlobalVariable(CV->getType(), true,
47 GlobalValue::InternalLinkage, CV,
48 "immcst", TheModule);
12849 }
12950
13051 public:
131 PreSelection (const TargetMachine &T):
132 target(T), instrInfo(T.getInstrInfo()), function(NULL) {}
52 PreSelection(const TargetMachine &T)
53 : instrInfo(T.getInstrInfo()), TheModule(0) {}
13354
13455 // runOnBasicBlock - apply this pass to each BB
135 bool runOnBasicBlock(BasicBlock &BB) {
136 function = BB.getParent();
137 this->visit(BB);
56 bool run(Module &M) {
57 TheModule = &M;
58
59 // Build reverse map for pre-existing global constants so we can find them
60 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
61 if (I->hasInitializer() && I->isConstant())
62 gvars[I->getInitializer()] = I;
63
64 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
65 visit(*I);
66
67 gvars.clear();
13868 return true;
139 }
140
141 bool doFinalization(Function &F) {
142 if (PreSelectDebugLevel >= PreSelect_PrintOutput)
143 std::cerr << "\n\n*** LLVM code after pre-selection for function "
144 << F.getName() << ":\n\n" << F;
145 return false;
14669 }
14770
14871 // These methods do the actual work of specializing code
356279 // createPreSelectionPass - Public entrypoint for pre-selection pass
357280 // and this file as a whole...
358281 //
359 Pass*
360 createPreSelectionPass(TargetMachine &T)
361 {
282 Pass* createPreSelectionPass(TargetMachine &T) {
362283 return new PreSelection(T);
363284 }
364285