thanos
thanos

Reputation: 5858

LLVM no-op instruction?

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

Answers (3)

Frederick F. Kautz IV
Frederick F. Kautz IV

Reputation: 510

Instead of a noop or a statement that optimizes to a noop, create a label and a branch for your if condition.

  1. This simplifies code generation, no special cases for when else is or is not present.
  2. The optimizer will convert these to phi instructions.
  3. It makes the intention of the code explicit to the developer. Future you or others will not need to work out why this extra instruction was inserted in.

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

Maxim Reznik
Maxim Reznik

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

CAFxX
CAFxX

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

Related Questions