[LLVMdev] insert nop instruction

Evgeny Astigeevich evgeny.astigeevich at arm.com
Thu Jul 9 07:14:36 PDT 2015


Hi Zahra,

 

The following simple pass inserts NOP:

 

///////////////////////////////////////

#include "llvm/Transforms/Scalar.h"

#include "llvm/IR/BasicBlock.h"

#include "llvm/IR/Instructions.h"

#include "llvm/Pass.h"

#include "llvm/IR/Constants.h"

 

using namespace llvm;

 

namespace {

class NopI : public BasicBlockPass {

public:

  static char ID;

 

  NopI() : BasicBlockPass(ID) {

    initializeNopIPass(*PassRegistry::getPassRegistry());

  }

 

  bool runOnBasicBlock(BasicBlock &BB) override;

};

}

 

bool NopI::runOnBasicBlock(BasicBlock &BB) {

  Instruction *I = BB.getFirstNonPHIOrDbg();

  assert(I);

  Value* V = ConstantInt::get(Type::getInt8Ty(BB.getContext()), 0);

  BinaryOperator::Create(Instruction::Add, V, V, "nop", I);

 

  return true;

}

 

char NopI::ID = 0;

 

BasicBlockPass *llvm::createNopIPass() { return new NopI(); }

 

INITIALIZE_PASS_BEGIN(NopI, "nopi", "NOP", false, false)

INITIALIZE_PASS_END(NopI, "nopi", "NOP", false, false)

///////////////////////////////////////

 

I checked:

 

Before:

; <label>:2                                       ; preds = %13, %0

  %3 = load i32, i32* %i, align 4

  %4 = icmp ult i32 %3, 32

  br i1 %4, label %5, label %16

 

After:  opt -S -nopi -o test_.ll test.ll

; <label>:2                                       ; preds = %13, %0

  %nop1 = add i8 0, 0

  %3 = load i32, i32* %i, align 4

  %4 = icmp ult i32 %3, 32

  br i1 %4, label %5, label %16

 

 

Kind regards,

Evgeny Astigeevich

 

 

From: Zahra Marj [mailto:zahrafatehimarj at gmail.com] 
Sent: 09 July 2015 12:31
To: Evgeny Astigeevich
Cc: LLVM Developers Mailing List
Subject: Re: [LLVMdev] insert nop instruction

 

I would like to use "new instruction()" or similar instruction that generate an NOP instruction and then insert it, randomly, between instructions of function basic blocks. But link doesn't provide a good example and limits to alloca instruction.

 

On Thu, Jul 9, 2015 at 1:05 PM, Evgeny Astigeevich <evgeny.astigeevich at arm.com> wrote:

Hi Zahra,

 

What the problem do you have?

Could you provide your code?

 

Kind regards,

Evgeny Astigeevich

 

From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] On Behalf Of Zahra Marj
Sent: 09 July 2015 09:25
To: LLVM Developers Mailing List
Subject: [LLVMdev] insert nop instruction

 

Hi.

I need to write a function pass that insert nop instruction in function. Examples of these instructions are: %nop = add i1 0, 0 or %nop = alloca i1, i1 0. This link couldn't help me: http://llvm.org/docs/ProgrammersManual.html#creating-and-inserting-new-instructions <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_ProgrammersManual.html-23creating-2Dand-2Dinserting-2Dnew-2Dinstructions&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=qXjJRaqkQWeOVtPcUv65c1zR2hwz2FyxkyPKUDcEsBM&s=CnMC85PvQDV1ouDHE2aQz5LytBiguUJ3JiOkMez73dg&e=> 

I need a clear example about inserting new instruction. Anyone can help me?

Thanks.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150709/e75bdc45/attachment.html>


More information about the llvm-dev mailing list