[llvm-dev] Changing the Opcode of an Instruction

Julian Nagele via llvm-dev llvm-dev at lists.llvm.org
Fri May 12 06:45:05 PDT 2017


Dear LLVM experts,

I'm currently working on some optimizations to InstCombine and saw that,
whenever InstCombine rewrites an Instruction, a new Instruction is created
instead of reusing the existing one.  For instance, in InstCombineAddSub.cpp 
one finds the following code:

Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
...
  Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
...
  // A+B --> A|B iff A and B have no bits set in common.
  if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT))
      return BinaryOperator::CreateOr(LHS, RHS);

I'm wondering whether it wouldn't be more efficient to reuse the existing Add
and simply 'change' it into an Or, instead of allocating a new Instruction.

When I wanted to try what would happen if I do that, I realized that changing
the Opcode of an Instruction is not readily possible. In fact, if I'm not
mistaken, an Instruction's Opcode is stored in

  const unsigned char SubclassID;

which is inherited from Value.

So now my question is: Is there is a deeper reason for making this field const
And consequently disallowing to change the Opcode of an Instruction?
And, if not, would it be worth considering adding functionality to change the
Opcode of an Instruction, assuming it does result in a noticeable performance
gain for InstCombine?

I realize that this might be dangerous to do in general (e.g. what if the number
of operands changes?), but in cases like the one above it seems OK to me.

Thanks, Julian


More information about the llvm-dev mailing list