39 #define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
43 #define DEBUG_TYPE "nvptx-reflect"
65 VarMap[(*I).getKey()] = (*I).getValue();
72 bool runOnModule(
Module &)
override;
75 bool handleFunction(
Function *ReflectFunction);
81 return new NVVMReflect();
85 return new NVVMReflect(Mapping);
90 cl::desc(
"NVVM reflection, enabled by default"));
94 "Replace occurrences of __nvvm_reflect() calls with 0/1",
false,
97 static cl::list<std::
string>
98 ReflectList("nvvm-reflect-list", cl::value_desc("
name=<
int>"), cl::
Hidden,
99 cl::desc("
A list of
string=num assignments"),
108 void NVVMReflect::setVarMap() {
109 for (
unsigned i = 0, e = ReflectList.size(); i != e; ++i) {
110 DEBUG(
dbgs() <<
"Option : " << ReflectList[i] <<
"\n");
113 for (
unsigned j = 0, ej = NameValList.
size(); j != ej; ++j) {
115 NameValList[j].split(NameValPair,
"=");
116 assert(NameValPair.
size() == 2 &&
"name=val expected");
117 std::stringstream ValStream(NameValPair[1]);
120 assert((!(ValStream.fail())) &&
"integer value expected");
121 VarMap[NameValPair[0]] = Val;
126 bool NVVMReflect::handleFunction(
Function *ReflectFunction) {
129 "_reflect function should not have a body");
131 "_reflect's return type should be integer");
133 std::vector<Instruction *> ToRemove;
161 for (
User *U : ReflectFunction->
users()) {
162 assert(isa<CallInst>(U) &&
"Only a call instruction can use _reflect");
163 CallInst *Reflect = cast<CallInst>(U);
166 "Only one operand expect for _reflect function");
170 if (isa<CallInst>(Str)) {
172 const CallInst *ConvCall = cast<CallInst>(Str);
175 assert(isa<ConstantExpr>(Str) &&
176 "Format of _reflect function not recognized");
180 assert(isa<Constant>(Sym) &&
"Format of _reflect function not recognized");
182 const Value *Operand = cast<Constant>(Sym)->getOperand(0);
183 if (
const GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) {
186 assert(GV->hasInitializer() &&
187 "Format of _reflect function not recognized");
188 const Constant *Initializer = GV->getInitializer();
189 Operand = Initializer;
192 assert(isa<ConstantDataSequential>(Operand) &&
193 "Format of _reflect function not recognized");
194 assert(cast<ConstantDataSequential>(Operand)->isCString() &&
195 "Format of _reflect function not recognized");
197 std::string ReflectArg =
198 cast<ConstantDataSequential>(Operand)->getAsString();
200 ReflectArg = ReflectArg.substr(0, ReflectArg.size() - 1);
201 DEBUG(
dbgs() <<
"Arg of _reflect : " << ReflectArg <<
"\n");
204 if (VarMap.find(ReflectArg) != VarMap.end()) {
205 ReflectVal = VarMap[ReflectArg];
209 ToRemove.push_back(Reflect);
211 if (ToRemove.size() == 0)
214 for (
unsigned i = 0, e = ToRemove.size(); i != e; ++i)
215 ToRemove[i]->eraseFromParent();
219 bool NVVMReflect::runOnModule(
Module &M) {
234 for (
unsigned i = 0; i != 5; ++i) {
238 if(ReflectFunction != 0) {
239 Res |= handleFunction(ReflectFunction);
246 if (ReflectFunction != 0)
247 Res |= handleFunction(ReflectFunction);
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumOperands() const
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Type * getReturnType() const
static cl::opt< bool > NVVMReflectEnabled("nvvm-reflect-enable", cl::init(true), cl::Hidden, cl::desc("NVVM reflection, enabled by default"))
ConstantExpr - a constant value that is initialized with an expression using other constant values...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
initializer< Ty > init(const Ty &Val)
void initializeNVVMReflectPass(PassRegistry &)
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
#define NVVM_REFLECT_FUNCTION
Value * getOperand(unsigned i) const
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
ModulePass * createNVVMReflectPass()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
std::string getName(ID id, ArrayRef< Type * > Tys=None)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static IntegerType * getInt8Ty(LLVMContext &C)
LLVMContext & getContext() const
Get the global data context.