【问题标题】:"Ptr must be a pointer to Val type!" error while creating a new Store Instruction“Ptr 必须是指向 Val 类型的指针!”创建新的商店指令时出错
【发布时间】:2026-01-16 17:25:02
【问题描述】:

我正在尝试使用以下代码创建一个新的Store 指令:

AllocaInst* newTemp = new AllocaInst(llvm::Type::getInt32Ty(Context), 0, 4,tVname);
bb->getInstList().insert(original, newTemp);
Value* dest = inst->getOperand(1);
StoreInst *strTwo = new StoreInst(newTemp, dest,0,4); //newTemp creates error
bb->getInstList().insert(bfrInst, strTwo);

当我运行代码时,它会抛出异常:

void llvm::StoreInst::AssertOK(): Assertion getOperand(0)->getType() == cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be a pointer to Val type!"' failed.

我应该如何修改代码以使其无错误?

【问题讨论】:

  • @IsmailBadawi 这是一个add 指令。我正在获取它的第二个操作数。

标签: compiler-construction llvm compiler-optimization llvm-clang llvm-ir


【解决方案1】:

根据http://llvm.org/docs/doxygen/html/classllvm_1_1StoreInst.html

StoreInst 将第一个参数值和第二个作为指针。 好像你在做相反的事情

 StoreInst *strTwo = new StoreInst(newTemp, dest,0,4); //newTemp creates error

如果您尝试将 dest 存储到 newtemp alloca 指针,请尝试:

 StoreInst *strTwo = new StoreInst(dest, newtemp,0,4); //newTemp creates error

【讨论】: