tracking known operand bits across passes

Hello LLVM,

tl;dr: Is there a reasonable way to track known operand bits all the
way to a pre-emit pass?

Long version:

For code size optimization, targets like x86 could use operand
known-zero and known-one bits. For example, a 16-bit 'andw' can be
promoted to a cheaper 32-bit 'andl' so long as an operand has amenable
zero bits.

So far, experiments making this work in my own target have failed.

During lowering, DAG.computeKnownBits() tells me what I need to know.
I can promote the operation here and add a leading zero-extend and
trailing truncate. Subsequent DAG processing just strips ext/trunc
back off, leaving me with the original node again and in an endless
lowering loop.

Since the promotion is really an encoding trick, not a logical change
to the program, perhaps the best place to promote an andw to andl is
in a late pre-emit pass. Is that reasonable and can anyone outline
the right approach?


Time frame is within the next month.

My pass executes quite late in the CG, post RA, just a bit before it gets to encoding. Specifically it is here:

void X86PassConfig::addPreEmitPass() {
  if (getOptLevel() != CodeGenOpt::None)

  if (UseVZeroUpper)

  if (getOptLevel() != CodeGenOpt::None) {
    addPass(createX86FixupBWInsts()); // This is the new pass for byte/word instruction optimization pass.

right now. No information is preserved. It uses the register liveness information already present in the Machine Instructions IR.

Sorry, no I didn't intend to move this to llvm-commits. Fat fingered that instead of llvm-dev with auto-completion in Outlook. Moved back to llvm-dev. Thanks for pointing that out.