37 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
62 VarMap[(*I).getKey()] = (*I).getValue();
67 virtual bool runOnModule(
Module &);
74 return new NVVMReflect();
78 return new NVVMReflect(Mapping);
83 cl::desc(
"NVVM reflection, enabled by default"));
87 "Replace occurences of __nvvm_reflect() calls with 0/1",
false,
90 static cl::list<std::
string>
91 ReflectList("nvvm-reflect-list", cl::value_desc("name=<
int>"), cl::
Hidden,
92 cl::desc("
A list of
string=num assignments"),
101 void NVVMReflect::setVarMap() {
102 for (
unsigned i = 0, e = ReflectList.size(); i != e; ++i) {
103 DEBUG(
dbgs() <<
"Option : " << ReflectList[i] <<
"\n");
106 for (
unsigned j = 0, ej = NameValList.
size(); j != ej; ++j) {
108 NameValList[j].split(NameValPair,
"=");
109 assert(NameValPair.
size() == 2 &&
"name=val expected");
110 std::stringstream ValStream(NameValPair[1]);
113 assert((!(ValStream.fail())) &&
"integer value expected");
114 VarMap[NameValPair[0]] = Val;
119 bool NVVMReflect::runOnModule(
Module &M) {
129 if (ReflectFunction == 0)
133 assert(ReflectFunction->isDeclaration() &&
134 "_reflect function should not have a body");
135 assert(ReflectFunction->getReturnType()->isIntegerTy() &&
136 "_reflect's return type should be integer");
138 std::vector<Instruction *> ToRemove;
147 E = ReflectFunction->use_end();
149 assert(isa<CallInst>(*
I) &&
"Only a call instruction can use _reflect");
150 CallInst *Reflect = cast<CallInst>(*I);
153 "Only one operand expect for _reflect function");
157 assert(isa<CallInst>(conv) &&
"Expected a const-to-gen conversion");
158 const CallInst *ConvCall = cast<CallInst>(conv);
160 assert(isa<ConstantExpr>(str) &&
161 "Format of _reflect function not recognized");
165 assert(isa<Constant>(Sym) &&
"Format of _reflect function not recognized");
167 const Constant *SymStr = cast<Constant>(Sym);
169 assert(isa<ConstantDataSequential>(SymStr->
getOperand(0)) &&
170 "Format of _reflect function not recognized");
172 assert(cast<ConstantDataSequential>(SymStr->
getOperand(0))->isCString() &&
173 "Format of _reflect function not recognized");
175 std::string ReflectArg =
176 cast<ConstantDataSequential>(SymStr->
getOperand(0))->getAsString();
178 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
179 DEBUG(
dbgs() <<
"Arg of _reflect : " << ReflectArg <<
"\n");
182 if (VarMap.find(ReflectArg) != VarMap.end()) {
183 ReflectVal = VarMap[ReflectArg];
187 ToRemove.push_back(Reflect);
189 if (ToRemove.size() == 0)
192 for (
unsigned i = 0, e = ToRemove.size(); i != e; ++i)
193 ToRemove[i]->eraseFromParent();
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
unsigned getNumOperands() const
std::pair< StringRef, StringRef > split(char Separator) const
ID
LLVM Calling Convention Representation.
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
void replaceAllUsesWith(Value *V)
Function * getFunction(StringRef Name) const
initializer< Ty > init(const Ty &Val)
void initializeNVVMReflectPass(PassRegistry &)
LLVM Constant Representation.
#define NVVM_REFLECT_FUNCTION
Value * getOperand(unsigned i) const
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
ModulePass * createNVVMReflectPass()
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Value * getArgOperand(unsigned i) const
LLVM Value Representation.