Search code examples
llvminline-assembly

how to insert inline assembly instruction by using llvm pass


I tried to insert an assembly instruction into each base block using pass in the IR Pass of LLVM.

Update:

LLVMContext *Ctx = nullptr;
Ctx = &M.getContext();
BasicBlock::iterator IP = BB.getFirstInsertionPt();
IRBuilder<> IRB(&(*IP));
StringRef asmString = "int3";
StringRef constraints = "~{dirflag},~{fpsr},~{flags}";
llvm::InlineAsm *IA = llvm::InlineAsm::get(Ty,asmString,constraints,true,false,InlineAsm::AD_ATT); 
ArrayRef<Value *> Args = None;
llvm::CallInst *Ptr = IRB.CreateCall(IA,Args);
Ptr->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind); 

However, when I ran the pass on one of the test files, test.bc, I found that no INT3 instructions were inserted into the file.I compared the statement I created with Ptr:

call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"() #4

And the real INT3 in IR is:

call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"() #2, !srcloc !2

I wonder how can I modify my code to make it work?


Solution

  • The type of the inline assembly certainly don't have to match with the type of function it is used in.

    For a int3 inline assembly you probably want a void (void) type, that is FunctionType::get(Type::getVoidTy(ctx), false).