[Mips] Simplify loadImmediate (NFC)
authorKazu Hirata <kazu@google.com>
Thu, 26 Jan 2023 04:54:43 +0000 (20:54 -0800)
committerKazu Hirata <kazu@google.com>
Thu, 26 Jan 2023 04:54:43 +0000 (20:54 -0800)
commitcdc2a0473e90732245387344248b5487778f9b6f
tree2aa47a65f633284cd5618484fc1dd33dc4ce883d
parent3b53c6020052215be97898a4312fc41ca626a0ad
[Mips] Simplify loadImmediate (NFC)

loadImmediate computes ShiftAmount in an unnecessarily complicated
manner.  We just need to know the minimum right shift amount to bring
the immediate down to an unsigned 16-bit value, so

  unsigned ShiftAmount = llvm::bit_width((uint64_t)ImmValue) - 16;

is sufficient.  In other words, the following are all equivalent:

  unsigned ShiftAmount = FirstSet - (15 - (LastSet - FirstSet));
  unsigned ShiftAmount = llvm::countr_zero(IV) - (15 - (63 - llvm::countl_zero(IV) - llvm::countr_zero(IV)));
  unsigned ShiftAmount = llvm::countr_zero(IV) - 15 + (63 - llvm::countl_zero(IV) - llvm::countr_zero(IV));
  unsigned ShiftAmount = 48 - llvm::countl_zero(IV);
  unsigned ShiftAmount = 64 - llvm::countl_zero(IV) - 16;
  unsigned ShiftAmount = llvm::bit_width(IV) - 16;

where IV represents (uint64)ImmValue.  I've also checked the
equivalence empirically up to 2u << 32.
llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp