[LLVMdev] Replacing instruction in LLVM IR by an intrinsics

ihusar ihusar at fit.vutbr.cz
Wed Jun 24 08:32:29 PDT 2009

Hi everyone,

  I am trying to write a pass, that finds some instructions and replaces them with my intrinsics,
but I am having problem understanding, how this should be done. 

Let's say I have this instruction:

 %tmp14 = load i32* getelementptr ([32 x i32]* @gpregs, i32 0, i64 28) 

and i need to read the load's operands and replace it by let's say:

 %tmp14 = call i32 @llvm.regread_i32.i32(i32 0, i32 1)

Here is what I have:

  //for each instruction of a function
  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
	Instruction* i = &(*I);
	//if this is a load
	if (isa<LoadInst>(i))
		//now i need to create an instruction that represents a call to a intrinsic
		Function* FIntr = Intrinsic::getDeclaration(&M, Intrinsic::regread_i32); 

		// here it fails: void llvm::CallInst::init(llvm::Value*): 
		//Assertion `FTy->getNumParams() == 0 && "Calling a function with bad signature"' failed.
		Instruction* Instr = CallInst::Create(FIntr); 

		//do some stuff with the operands

		//and replace it
		ReplaceInstWithInst(i, Instr);

Intrinsic regread_i32 is defined in Intrinsics.td as follows:

//represents register value read, as arguments takes 1) register class number (determined from the acessed variable, here 'regs') and 2) register operand index (not important now)
def int_regread_i32          : Intrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;

The problem is, that I have not found yet, how to correctly create an intrinsic instruction and
how do I access and set operands. Is there some documentation, how is the LLVM IR API designed?
Or, has someone already tried to solve similar problem? (I am already trying to learn from the source code how it works, 
but I thought that asking you would be faster:)

Thank you

btw.: Is there a way how to search archived messages from this mailing list? Google with site:lists.cs.uiuc.edu
does not work very much.

More information about the llvm-dev mailing list