Reputation: 5858
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?
Upvotes: 6
Views: 5750
Reputation: 510
Instead of a noop or a statement that optimizes to a noop, create a label and a branch for your if condition.
For example:
%2 = icmp sgt i32 %0, %1
br i1 %2, label %3, label %7
; <label>:3
; ;; [instructions in block redacted for simplicity]
br label %8
; <label>:7
br label %8
; <label>:8
%9 = load i32* %x, align 4 ;; continue on our merry way
Upvotes: 0
Reputation: 1417
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.
Upvotes: 8
Reputation: 30301
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.
Upvotes: 7