从c++ API中检索常量函数参数的值
LLVM: Retrieving the value of constant function parameters from the C++ API
我正在使用LLVM API编写一些代码。我正在使用llvm::CallGraph对象来循环通过父函数调用的所有子函数:
CallGraph cg( *mod );
for( auto i = cg.begin(); i != cg.end(); ++i )
{
const Function *parent = i->first;
if( !parent )
continue;
for( auto j = i->second->begin(); j != i->second->end(); ++j )
{
Function *child = j->second->getFunction();
if( !child )
continue;
for( auto iarg = child->arg_begin(); iarg != child->arg_end(); ++iarg )
{
// Print values here!
}
}
o.flush();
我感兴趣的IR中的实际函数调用看起来像这样:
call void @_ZN3abc3FooC1Ejjj(%"struct.abc::Foo"* %5, i32 4, i32 2, i32 0)
,我想做的是检索最后三个常量整数值:4,2,0。如果我也能检索%5,那就加分了,但这并不那么重要。我花了大约两个小时盯着http://llvm.org/docs/doxygen/,但我就是不知道我应该如何得到这三个值
在第二个循环中,当您遍历CallGraphNode
时,您将返回std::pair<WeakVH, CallGraphNode*>
的实例。WeakVH
表示调用指令,CallGraphNode*
表示被调用函数。代码的问题在于,您正在查看被调用的函数,并在其定义中的形式参数上迭代,而不是查看调用位置。你想要这样的东西(注意,我还没有测试过这个,只是根据签名):
CallGraph cg( *mod );
for( auto i = cg.begin(); i != cg.end(); ++i )
{
for( auto j = i->second->begin(); j != i->second->end(); ++j )
{
CallSite CS(*j->first);
for (auto arg = CS.arg_begin(); arg != CS.arg_end(); ++arg) {
// Print values here!
}
}
o.flush();
从那里你可以得到代表参数的Value*
指针,检查它们是否为ConstantInt
,等等。
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数