【问题标题】:LLVM: Retrieving the value of constant function parameters from the C++ APILLVM:从 C++ API 检索常量函数参数的值
【发布时间】:2015-05-18 23:32:14
【问题描述】:

我正在使用 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/,但我根本不知道我应该如何得到这三个值。

【问题讨论】:

    标签: c++ llvm


    【解决方案1】:

    在您的第二个循环中,当您遍历 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,等等。

    【讨论】:

    • 这是一个很好的开始;我很想知道如何检查 Value* 是否为 ConstantInt 等?我只是 dyn_cast 到 ConstantInt 看看它是否有效?
    • @AHelps 是的,典型的事情是写例如if (ConstantInt *CI = dyn_cast<ConstantInt>(*arg)) {
    猜你喜欢
    • 2017-04-01
    • 2011-08-08
    • 2021-09-02
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多