[LLVMdev] insert nop instruction

1,215 views
Skip to first unread message

Zahra Marj

unread,
Jul 9, 2015, 4:28:04 AM7/9/15
to LLVM Developers Mailing List
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
I need a clear example about inserting new instruction. Anyone can help me?
Thanks.

Evgeny Astigeevich

unread,
Jul 9, 2015, 4:38:59 AM7/9/15
to Zahra Marj, LLVM Developers Mailing List

Hi Zahra,

 

What the problem do you have?

Could you provide your code?

 

Kind regards,

Evgeny Astigeevich

David Chisnall

unread,
Jul 9, 2015, 4:42:15 AM7/9/15
to Zahra Marj, LLVM Developers Mailing List
Hi,

What are you trying to achieve? Inserting NOPs into LLVM IR is likely to be pointless, as optimisations (in the IR or SelectionDAG) will remove them before machine code generation. If you want to insert NOPs into the generated machine code, then this will not help you (you could insert inline assembly containing nops into the IR).

David
> _______________________________________________
> LLVM Developers mailing list
> LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Zahra Marj

unread,
Jul 9, 2015, 7:37:38 AM7/9/15
to Evgeny Astigeevich, LLVM Developers Mailing List
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.

Zahra Marj

unread,
Jul 9, 2015, 7:39:53 AM7/9/15
to David Chisnall, LLVM Developers Mailing List
My pass runs after optimization passes.

Evgeny Astigeevich

unread,
Jul 9, 2015, 10:21:29 AM7/9/15
to Zahra Marj, LLVM Developers Mailing List

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

John Criswell

unread,
Jul 9, 2015, 10:33:54 AM7/9/15
to Daniel Sanders, Zahra Marj, David Chisnall, LLVM Developers Mailing List
Dear All,

To add to this, you can find examples of inserting NOPs for X86 in the CFI pass originally written at Lehigh University that we ported to 64-bit X86 for SVA:

https://github.com/jtcriswell/SVA/blob/master/llvm/lib/Target/X86/X86CFIOptPass.cpp

Alternatively, you could use an InlineAsm call at the LLVM IR level (which I think would be easier to implement).

Regards,

John Criswell

On 7/9/15 8:56 AM, Daniel Sanders wrote:

Hi,

 

Given that you are using LLVM-IR, I expect that instruction selection occurs after your pass and instruction selection also applies optimizations. In particular, when the SelectionDAG is built it will eliminate (via constant folding) the '%nop = add i1 0, 0' you added to the LLVM-IR.

 

If you are trying to insert nop's into the resulting assembly then you should probably be trying to insert them at a very late stage of the backend instead. Different IR's based on MachineInstr and MCInst are used in these later stages.

 

From: llvmdev...@cs.uiuc.edu [mailto:llvmdev...@cs.uiuc.edu] On Behalf Of Zahra Marj
Sent: 09 July 2015 12:33
To: David Chisnall
Cc: LLVM Developers Mailing List
Subject: Re: [LLVMdev] insert nop instruction

 

My pass runs after optimization passes.



-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
http://www.cs.rochester.edu/u/criswell

Daniel Sanders

unread,
Jul 9, 2015, 11:14:56 AM7/9/15
to Zahra Marj, David Chisnall, LLVM Developers Mailing List

Hi,

 

Given that you are using LLVM-IR, I expect that instruction selection occurs after your pass and instruction selection also applies optimizations. In particular, when the SelectionDAG is built it will eliminate (via constant folding) the '%nop = add i1 0, 0' you added to the LLVM-IR.

 

If you are trying to insert nop's into the resulting assembly then you should probably be trying to insert them at a very late stage of the backend instead. Different IR's based on MachineInstr and MCInst are used in these later stages.

 

From: llvmdev...@cs.uiuc.edu [mailto:llvmdev...@cs.uiuc.edu] On Behalf Of Zahra Marj
Sent: 09 July 2015 12:33
To: David Chisnall
Cc: LLVM Developers Mailing List
Subject: Re: [LLVMdev] insert nop instruction

 

My pass runs after optimization passes.

Zahra Marj

unread,
Jul 10, 2015, 5:33:05 PM7/10/15
to John Criswell, LLVM Developers Mailing List
Thanks all. Your comments help me very much.

Stephen Crane

unread,
Jul 10, 2015, 5:45:00 PM7/10/15
to Zahra Marj, LLVM Developers Mailing List
I've got a pending patch for LLVM (which has been stalled for a while,
totally my fault) to insert NOPs for artificial software diversity.
You can check out the patch at http://reviews.llvm.org/D6983

- stephen
Reply all
Reply to author
Forward
0 new messages