OLD | NEW |
1 //===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===// | 1 //===- NVVMReflect.cpp - NVVM Emulate conditional compilation -------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This pass replaces occurences of __nvvm_reflect("string") with an | 10 // This pass replaces occurences of __nvvm_reflect("string") with an |
11 // integer based on -nvvm-reflect-list string=<int> option given to this pass. | 11 // integer based on -nvvm-reflect-list string=<int> option given to this pass. |
12 // If an undefined string value is seen in a call to __nvvm_reflect("string"), | 12 // If an undefined string value is seen in a call to __nvvm_reflect("string"), |
13 // a default value of 0 will be used. | 13 // a default value of 0 will be used. |
14 // | 14 // |
15 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
16 | 16 |
17 #include "NVPTX.h" | |
18 #include "llvm/ADT/DenseMap.h" | 17 #include "llvm/ADT/DenseMap.h" |
19 #include "llvm/ADT/SmallVector.h" | 18 #include "llvm/ADT/SmallVector.h" |
20 #include "llvm/ADT/StringMap.h" | 19 #include "llvm/ADT/StringMap.h" |
21 #include "llvm/Pass.h" | 20 #include "llvm/Pass.h" |
22 #include "llvm/IR/Function.h" | 21 #include "llvm/IR/Function.h" |
23 #include "llvm/IR/Module.h" | 22 #include "llvm/IR/Module.h" |
24 #include "llvm/IR/Type.h" | 23 #include "llvm/IR/Type.h" |
25 #include "llvm/IR/DerivedTypes.h" | 24 #include "llvm/IR/DerivedTypes.h" |
26 #include "llvm/IR/Instructions.h" | 25 #include "llvm/IR/Instructions.h" |
27 #include "llvm/IR/Constants.h" | 26 #include "llvm/IR/Constants.h" |
28 #include "llvm/Support/CommandLine.h" | 27 #include "llvm/Support/CommandLine.h" |
29 #include "llvm/Support/Debug.h" | 28 #include "llvm/Support/Debug.h" |
30 #include "llvm/Support/raw_os_ostream.h" | 29 #include "llvm/Support/raw_os_ostream.h" |
31 #include "llvm/Transforms/Scalar.h" | 30 #include "llvm/Transforms/Scalar.h" |
32 #include <map> | 31 #include <map> |
33 #include <sstream> | 32 #include <sstream> |
34 #include <string> | 33 #include <string> |
35 #include <vector> | 34 #include <vector> |
36 | 35 |
37 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect" | 36 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect" |
38 | 37 |
39 using namespace llvm; | 38 using namespace llvm; |
40 | 39 |
41 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); } | 40 namespace llvm { void initializeNVVMReflectPass(PassRegistry &); } |
42 | 41 |
43 namespace { | 42 namespace { |
44 class NVVMReflect : public ModulePass { | 43 class LLVM_LIBRARY_VISIBILITY NVVMReflect : public ModulePass { |
45 private: | 44 private: |
46 StringMap<int> VarMap; | 45 StringMap<int> VarMap; |
47 typedef DenseMap<std::string, int>::iterator VarMapIter; | 46 typedef DenseMap<std::string, int>::iterator VarMapIter; |
48 Function *ReflectFunction; | 47 Function *ReflectFunction; |
49 | 48 |
50 public: | 49 public: |
51 static char ID; | 50 static char ID; |
52 NVVMReflect() : ModulePass(ID), ReflectFunction(0) { | 51 NVVMReflect() : ModulePass(ID) { |
53 initializeNVVMReflectPass(*PassRegistry::getPassRegistry()); | |
54 VarMap.clear(); | 52 VarMap.clear(); |
55 } | 53 ReflectFunction = 0; |
56 | |
57 NVVMReflect(const StringMap<int> &Mapping) | |
58 : ModulePass(ID), ReflectFunction(0) { | |
59 initializeNVVMReflectPass(*PassRegistry::getPassRegistry()); | |
60 for (StringMap<int>::const_iterator I = Mapping.begin(), E = Mapping.end(); | |
61 I != E; ++I) { | |
62 VarMap[(*I).getKey()] = (*I).getValue(); | |
63 } | |
64 } | 54 } |
65 | 55 |
66 void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); } | 56 void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); } |
67 virtual bool runOnModule(Module &); | 57 virtual bool runOnModule(Module &); |
68 | 58 |
69 void setVarMap(); | 59 void setVarMap(); |
70 }; | 60 }; |
71 } | 61 } |
72 | 62 |
73 ModulePass *llvm::createNVVMReflectPass() { | |
74 return new NVVMReflect(); | |
75 } | |
76 | |
77 ModulePass *llvm::createNVVMReflectPass(const StringMap<int>& Mapping) { | |
78 return new NVVMReflect(Mapping); | |
79 } | |
80 | |
81 static cl::opt<bool> | 63 static cl::opt<bool> |
82 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), | 64 NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), |
83 cl::desc("NVVM reflection, enabled by default")); | 65 cl::desc("NVVM reflection, enabled by default")); |
84 | 66 |
85 char NVVMReflect::ID = 0; | 67 char NVVMReflect::ID = 0; |
86 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", | 68 INITIALIZE_PASS(NVVMReflect, "nvvm-reflect", |
87 "Replace occurences of __nvvm_reflect() calls with 0/1", false, | 69 "Replace occurences of __nvvm_reflect() calls with 0/1", false, |
88 false) | 70 false) |
89 | 71 |
90 static cl::list<std::string> | 72 static cl::list<std::string> |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 ConstantInt::get(Reflect->getType(), ReflectVal)); | 168 ConstantInt::get(Reflect->getType(), ReflectVal)); |
187 ToRemove.push_back(Reflect); | 169 ToRemove.push_back(Reflect); |
188 } | 170 } |
189 if (ToRemove.size() == 0) | 171 if (ToRemove.size() == 0) |
190 return false; | 172 return false; |
191 | 173 |
192 for (unsigned i = 0, e = ToRemove.size(); i != e; ++i) | 174 for (unsigned i = 0, e = ToRemove.size(); i != e; ++i) |
193 ToRemove[i]->eraseFromParent(); | 175 ToRemove[i]->eraseFromParent(); |
194 return true; | 176 return true; |
195 } | 177 } |
OLD | NEW |