Denis
Denis

Reputation: 301

LLVM insert opcode before instruction

I want to insert specific opcode before BasicBlock terminator, in my case before ReturnInst.

It is possible?

Example:

TerminatorInst* terminator = BasicBlock->getTerminator();
if (isa<ReturnInst>(terminator))  
{
//Insert opcode here.    
}

Upvotes: 3

Views: 1412

Answers (2)

Denis
Denis

Reputation: 301

Problem solved by using llvm::InlineAsm

llvm::InlineAsm *AsmCode = llvm::InlineAsm::get(Asm, nopInstruction, "", true, false, llvm::InlineAsm::AD_Intel);

where Asm - llvm::FunctionType, nopInstruction - llvm::StringRef(char*)

Upvotes: 2

Oak
Oak

Reputation: 26868

By "opcode" I guess you mean "instruction".

All instructions have a constructor which receives another instruction as its last parameter; that constructor creates the new instruction and then inserts it right before the instruction that was given as the last argument, precisely what you're looking for.

For more information about this, see the "creating and inserting new instruction" section of the user guide.

Upvotes: 1

Related Questions