gpt4 book ai didi

c++ - LLVM如何获取指令的返回值

转载 作者:行者123 更新时间:2023-11-30 01:42:34 25 4
gpt4 key购买 nike

我有一个程序像这样从栈中分配内存:

%x = alloca i32, align 4

在我的传递中,我想在运行时获取指向该分配内存的实际内存指针。这应该是 %x。如何在我的通行证中获取指针?

Instruction* I;
if (AllocaInst* AI = dyn_cast<AllocaInst>(I)) {
//How to get %x?
}

最佳答案

您可以将 Instruction* 用作 Value*(并且 Instruction 继承自 Value),然后您将使用该指令的结果/返回值。我从我的 LLVM Pass 中改编了一些代码来演示使用 alloca 分配空间然后存储到该位置。请注意,指令的结果可以直接传递给其他指令,因为它们是值。

    // M is the module
// ci is the current instruction
LLVMContext &ctx = M.getContext();
Type* int32Ty = Type::getInt32Ty(ctx);
Type* int8Ty = Type::getInt8Ty(ctx);
Type* voidPtrTy = int8Ty->getPointerTo();

// Get an identifier for rand()
Constant* = M.getOrInsertFunction("rand", FunctionType::get(cct.int32Ty, false));

// Construct the struct and allocate space
Type* strTy[] = {int32Ty, voidPtrTy};
Type* t = StructType::create(strTy);
Instruction* nArg = new AllocaInst(t, "Wrapper Struct", ci);

// Add Store insts here
Value* gepArgs[2] = {ConstantInt::get(int32Ty, 0), ConstantInt::get(int32Ty, 0)};
Instruction* prand = GetElementPtrInst::Create(NULL, nArg, ArrayRef<Value*>(gepArgs, 2), "RandPtr", ci);

// Get a random number
Instruction* tRand = CallInst::Create(getRand, "", ci);

// Store the random number into the struct
Instruction* stPRand = new StoreInst(tRand, prand, ci);

关于c++ - LLVM如何获取指令的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39436498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com