在LLVM IR中插入GetElementpointer指令 [英] Inserting GetElementpointer Instruction in LLVM IR

查看:58
本文介绍了在LLVM IR中插入GetElementpointer指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过LLVM Pass在LLVM IR中插入GetElementPointer指令,假设我有一个数组

I am wondering how to insert a GetElementPointer instruction in LLVM IR through LLVM Pass, say suppose I have an array

%arr4 = alloca [100000 x i32], align 4

并且想要插入像

 %arrayidx = getelementptr inbounds [100000 x i32]* %arr, i32 0, i32 %some value

要编写的指令序列是什么,就像在IRBuilder类中一样,创建getelementpointer的指令太多了.使用哪个,它的参数是什么.谁能举例说明任何帮助将不胜感激.

the what will be the sequence of instructions to write as in IRBuilder Class there are so many instructions to create getelementpointer. Which One to use and what will be its parameters. can anyone explain it with example Any help would be appreciated.

推荐答案

让我们从 GetElementPtrInst ,因为IRBuilder正在为其构造函数提供包装器.如果要添加此指令,我通常会直接调用create.

Let's start with the documentation for GetElementPtrInst, since the IRBuilder is providing a wrapper to its constructors. If we want to add this instruction, I usually go direct and call create.

GetElementPtrInst::Create(ptr, IdxList, name, insertpoint)

  • Ptr:这是一个Value *,它是传递给GetElementPtr(GEP)的初始ptr值.就您而言,%arr.
  • IdxList:这是值的列表,这些值是传递给GEP的偏移量的序列.您的示例具有0和%some值.
  • 名称:这是IR中的名称.如果要%arrayidx",则应提供"arrayidx".
  • insertpoint:在没有IRBuilder的情况下,您必须指定在何处插入指令(在另一条指令之前或在基本块末尾).
  • 将这些片段放在一起,我们有以下代码序列:

    Putting these pieces together, we have the following code sequence:

    Value* arr = ...; // This is the instruction producing %arr
    Value* someValue = ...; // This is the instruction producing %some value
    
    // We need an array of index values
    //   Note - we need a type for constants, so use someValue's type
    Value* indexList[2] = {ConstantInt::get(someValue->getType(), 0), someValue};
    GetElementPtrInst* gepInst = GetElementPtrInst::Create(arr, ArrayRef<Value*>(indexList, 2), "arrayIdx", <some location to insert>);
    

    现在,您问有关使用IRBuilder的问题,它具有非常相似的:

    Now, you asked about using IRBuilder, which has a very similar function:

    IRBuilder::CreateGEP(ptr, idxList, name)
    

    如果要使用IRBuilder,则可以用类似的IRBuilder调用替换代码段的最后一行.

    If you want to use the IRBuilder, then you can replace the last line of the code snippet with the similar IRBuilder's call.

    这篇关于在LLVM IR中插入GetElementpointer指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆