Using the right abstraction
As we close our journey with LLVM IR, and before we start digging into Machine IR, we wanted to give you a few recommendations on how to decide if you need to implement your optimization passes in LLVM IR or in Machine IR.
Before you decide whether you should implement something in one IR or the other, step back and think about what you are trying to achieve.
Transformations at the LLVM IR level are easier to reuse across backends and are usually easier to write. Conversely, Machine IR passes have to provide additional mechanisms, such as helper functions and classes, to abstract away target-specific constructs. For instance, things as simple as recognizing an add
instruction are not portable across targets in Machine IR, unless you push some logic on the target itself.
In contrast, LLVM IR passes are at the mercy of what later transformations will do, including the Machine IR passes. For instance, computing an estimate of the register pressure...