Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM: Creating a CallInst with a null pointer operand

Tags:

c++

llvm

I'm trying to use the LLVM C++ bindings to write a pass which generates the following IR

%1 = call i64 @time(i64* null) #3

@time here is the C standard library time() function.

Here's the code I've written

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"));
}

This compiles, but results in the following error when run

Incorrect number of arguments passed to called function!
  %time = call i64 @time(i64 0)

As I understand this, I need to pass an int64 pointer which deferences to nullptr, but I'm unable to figure out how to do that.

like image 920
Umang Raghuvanshi Avatar asked Nov 17 '16 14:11

Umang Raghuvanshi


1 Answers

LLVM provides a ConstantPointerNull class which does exactly what I want - it returns a null pointer of the required type.

All that needs to be changed is the line beginning with args[0] = ... to args[0] = ConstantPointerNull::get(PointerType::get(timety, 0));.

like image 68
Umang Raghuvanshi Avatar answered Oct 31 '22 06:10

Umang Raghuvanshi