Inline assembly constant zero optimizations

This question came up on URLO the other day, and I haven't been able to find any discussions that address the need. The ask is for a peephole optimization that selects the constant-zero register on supported architectures when the register input expression evaluates to zero.

The best I was able to come up with was a declarative macro that can specialize when constant zero is its input. This has a shortcoming that expressions are not evaluated, so you get the less optimal instruction pair that assigns zero to a GPR [1]:

GCC is capable of evaluating the input expression for inline assembly:

Clang does not have support for this feature, AFAICT.

LLVM itself does constant zero optimizations, but that's the full extent of my knowledge in this area. I have no clue how the MIR -> LLVMIR pipeline handles inline assembly or what this kind of feature would look like from an implementation perspective. It appears to be desirable, though.


  1. A procedural macro could hypothetically do the expression evaluation itself. That's a lot more work that duplicates functionality that already exists in the compiler. ↩ī¸Ž

1 Like