llvm get called function name using llvm instruction - llvm

I want to know if an llvm::intruction is a function call and if so what is the name of the fucntion it is calling to.
Also is there a way to get llvm::CallInst from an llvm::Instruction?

Instruction* I = ...
if (isa<CallInst>(I)) {
StringRef name = cast<CallInst>(I).getCalledFunction().getName();
...
}
For more information on this, see the relevant section in LLVM Programmer's Manual. In general, I wholeheartedly recommend this guide for beginners.

Instruction is the common base class for all LLVM instructions.
CallInst is a subclass of Instruction for call instructions.
If you have Instruction *inst, you can get a CallInst by
CallInst *ci = cast<CallInst>(inst);

Related

llvm - fastest way of checking if function A is calling function B

I need to check if function A in my file is calling function B. My current approach is to go through all the instructions in function A and see if any of the call/invoke instructions are calling B. Can anyone suggest a better approach?
LLVM Provides easy to use method to traverse in-memory IR's use-def/def-use chain using Users/Uses.
You can traverse B's Uses and then check if its parent function is A or not.
for(Value::Use_iterator ui = B.Use_Begin(); ui != B.Use_end(); ++ui) {
if(instruction* call = dyn_cast<Instruction>(ui->getUser())) {
Function* caller = call->getParent()->getParent();
// check if caller is A or not
}
}
above code snippet may work with few modifications.
See: LLVM Use Ref for more info.

Given an LLVM Instruction, how can we obtain the pointer to its BasicBlock?

Suppose I have an llvm::Instruction* inst, how can I obtain the pointer to its basicblock? I searched in LLVM API and have found no such interface such like inst.getBasicBlock(). Any help?
In well formed LLVM IR each Instruction is embedded in a BasicBlock. You can get the BasicBlock from getParent().
getParent() will always go one step up in the LLVM IR hierarchy, i.e., you get a Function as parent from a BasicBlock, and the Module from a Function.

Get parent of register in llvm

In llvm used to be this great function (I don't know which version they are using here):
const unsigned* llvm::TargetRegisterInfo::getSuperRegisters(unsigned RegNo)
http://legup.eecg.utoronto.ca/doxygen/classllvm_1_1TargetRegisterInfo.html#90b85b889ff636c6bdd40b7543343473
Unfortunately I am using llvm 3.4, where this function does not exist. Is there something with similar functionality? Or is there an easy workaround to get all the parent registers of a given register?
Should have read the docu more carefully. Here is the answer:
http://llvm.org/docs/doxygen/html/classllvm_1_1MCSuperRegIterator.html
llvm::MCSuperRegIterator expects a physical register in its constructor and then iterates over all its parents.

convert to LLVM IR: how to create virtual register instead of allocate a stack variable?

I am working on converting another IR to llvm IR.
My IR is like this:
a = 1;
b = a;
a = a + 1;
For now, I am using alloca to create variable in my IR (Here for "a" and "b").
However, alloca probably is too heavy, it will introduce lots of load store instructions. This will be a problem if the function is huge. Actually, for my case, most of the variables are register-width. So I just want them be a virtual register with name.
Anybody know how to create a virtual register(variable) instead of memory variable?
I mean how to avoid using "alloca"?
You are not supposed to. Generating SSA code is a quite hard problem, so it's solved once for all frontends in LLVM passes. You are supposed to use alloca and load/store, and then run the mem2reg pass to convert those into SSA variables. Clang also does this (stick your example code in a C function, and compile it with no optimizations).

How to invoke an Objective-C Block via the LLVM C++ API?

Say, for example, I have an Objective-C compiled Module that contains something like the following:
typedef bool (^BoolBlock)(void);
BoolBlock returnABlock(void)
{
return Block_copy(^bool(void){
printf("Block executing.\n");
return YES;
});
}
...then, using the LLVM C++ API, I load that Module and create a CallInst to call the returnABlock() function:
Function *returnABlockFunction = returnABlockModule->getFunction(std::string("returnABlock"));
CallInst *returnABlockCall = CallInst::Create(returnABlockFunction, "returnABlockCall", entryBlock);
How can I then invoke the Block returned via the returnABlockCall object?
Not an easy answer here, I'm afraid. Blocks are lowered by the front-end into calls into the blocks runtime. In the case of clang, the relevant code is at clang/lib/CodeGen/CGBlocks.[h|cpp].
It would be worth asking on the cfe-dev list if there's a way to factor this code out for reuse in other front-ends.
In C, I just act as if the var I assigned the block to was a function pointer. Using your code as an example, after you assign the result of the function to "returnABlockCall", you could just write:
returnABlockCall();
and it should work.
Warning, this is untested in C++, but I see no reason why it wouldn't work.