LLVM:使用空指针操作数创建 CallInst

Uma*_*shi 5 c++ llvm

我正在尝试使用 LLVM C++ 绑定来编写生成以下 IR 的传递

%1 = call i64 @time(i64* null) #3
Run Code Online (Sandbox Code Playgroud)

@time这是 C 标准库time()函数。

这是我写的代码

void Pass::Insert(BasicBlock *bb, Type *timety, Module *m) {
  Type *timetype[1];
  timetype[0] = timety;
  ArrayRef<Type *> timeTypeAref(timetype, 1);
  Value *args[1];
  args[0] = ConstantInt::get(timety, 0, false);
  ArrayRef<Value *> argsRef(args, 1);
  FunctionType *signature = FunctionType::get(timety, false);
  Function *timeFunc =
      Function::Create(signature, Function::ExternalLinkage, "time", m);
  IRBuilder<> Builder(&*(bb->getFirstInsertionPt()));
  AllocaInst *a1 = Builder.CreateAlloca(timety, nullptr, Twine("a1"));
  CallInst *c1 = Builder.CreateCall(timeFunc, args, Twine("time"));
}
Run Code Online (Sandbox Code Playgroud)

这编译,但运行时导致以下错误

Incorrect number of arguments passed to called function!
  %time = call i64 @time(i64 0)
Run Code Online (Sandbox Code Playgroud)

据我了解,我需要传递一个遵循 的 int64 指针nullptr,但我无法弄清楚如何做到这一点。

Uma*_*shi 6

LLVM 提供了一个ConstantPointerNull完全符合我要求的类 - 它返回所需类型的空指针。

所有需要更改的是以args[0] = ...to 开头的行args[0] = ConstantPointerNull::get(PointerType::get(timety, 0));。

  • 另一种方法是使用 [`Constant::getNullValue`](http://llvm.org/docs/doxygen/html/classllvm_1_1Constant.html#aa6574d526b3e38a28f688a7bb4325c2c) (2认同)