Instruction simplification example

In this section, we will see how we fold instructions into simpler forms in LLVM. Here, the creation of new instructions will not take place. Instruction simplification does constant folding:

sub i32 2, 1 -> 1

That is, it simplifies the sub instruction to a constant value 1.

It can handle non-constant operands as well:

or i32 %x, 0 -> %x

It returns a value of variable %x

and i32 %x %x -> %x

In this case, it returns an already existing value.

The implementations for the methods that simplify instructions are located in lib/Analysis/InstructionSimplify.cpp.

Some of the important methods of dealing with the simplification of instructions are:

  • SimplifyBinOp method: This is used to simplify binary operations such as addition, ...

Get LLVM Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.