I am writing a compiler for an assignment for a language that has empty statements. For structures like if-then-else it could be convenient if I could use a no-op statement in llvm but I didnt find any "official" nop. I have some ideas but ideally I would like to have llvm to optimize that part of the code and remove the instruction; any advice?
You'll need to include llvm/Support/InstIterator.h, and then instantiate InstIterators explicitly in your code. Here's a small example that shows how to dump all instructions in a function to stderr (Note:Dereferencing an InstIteratoryields an Instruction*, notan Instruction&!):
References llvm::And, and llvm::Or. Return true if the specified instruction is exactly identical to the current one. This means that all operands match and any extra information (e.g. load is volatile) agree. Definition at line 499 of file Instruction.cpp.
Deleting Instructions Replacing an Instruction with another Value Replacing individual instructions Deleting Instructions Replacing multiple uses of Users and Values Deleting GlobalVariables Threads and LLVM Ending Execution with llvm_shutdown() Lazy Initialization with ManagedStatic Achieving Isolation with LLVMContext Threads and the JIT
Determine if the OpCode is one of the FuncletPadInst instructions. Definition at line 228 of file Instruction.h. Return true if the instruction is idempotent: In LLVM, the And and Or operators are idempotent. Definition at line 574 of file Instruction.h. References getOpcode (), and isIdempotent ().
There is no no-op opcode in the IR. But you can safely use any side-effects-free dead instruction as a replacement (that is, if you really need to emit a no-op) because the optimizers will delete them easily enough. E.g. %nop = add i1 0, 0
or %nop = alloca i1, i1 0
could work.
Look at this intrinsic:
declare void @llvm.donothing() nounwind readnone
from LLVM Language Reference Manual:
The llvm.donothing intrinsic doesn’t perform any operation. It’s one of only two intrinsics (besides llvm.experimental.patchpoint) that can be called with an invoke instruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With