Hi Zahra,
What the problem do you have?
Could you provide your code?
Kind regards,
Evgeny Astigeevich
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
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
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.