IRBuilder and "ad hoc" optimizations

Hi LLVM,

IRBuilder can fold constants (that behaviour can be controlled by Folder type). What do you think about optionally allow IRBuilder to eliminate no-op instructions like add %a, 0 or memcpy(%a, %b, 0)?

  • Paweł

You mean in cases when the builder is creating an instruction that is a no-op? The problem may be with handling cases like this:

MachineInstr *MI = BuildMI(ADD, DstReg).addReg(SrcReg).addImm(0);
if (C != 0)
   MI->getOperand(2).setImm(C);

If the builder doesn't create the initial SrcReg+0, then this code will fail.

-Krzysztof

Wrong builder. Please disregard. :slight_smile:

-K

The constant folding in IRBuilder is always a trade between the cost of checking for the special cases vs the cost of creating instructions that are later going to be deleted. I’d imagine that add of 0 is something that is sufficiently rare that the cost (in terms of both run time and code size, as IRBuilder’s CreateAdd is often inlined) would be more than the cost of very rarely creating these instructions and having a later pass delete them.

That said, the Folder is a template parameter. For some front ends (particularly those used with source-to-source transforms), it might be significantly more common to have nop expressions and having an AggressiveFolder or similar might be beneficial.

Profiling is likely to be needed to determine whether it’s a real win.

David

The constant folding in IRBuilder is always a trade between the cost of checking for the special cases vs the cost of creating instructions that are later going to be deleted. I’d imagine that add of 0 is something that is sufficiently rare that the cost (in terms of both run time and code size, as IRBuilder’s CreateAdd is often inlined) would be more than the cost of very rarely creating these instructions and having a later pass delete them.

That said, the Folder is a template parameter. For some front ends (particularly those used with source-to-source transforms), it might be significantly more common to have nop expressions and having an AggressiveFolder or similar might be beneficial.

Replacing default Folder with some other will not allow you to remove “no-op” instructions. Folder is triggered only if both operands are constant, what is not the case i.e. in add %a, 0. To allow that “optimization” IRBuilder redesign is needed.