Repository: llvm/llvm-project
File: llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp (C++)

Code before:
static unsigned getSExtLoadOpcode(unsigned Opc, bool A64) {
  switch (Opc) {
  default:
    return WebAssembly::INSTRUCTION_LIST_END;
  case WebAssembly::I32_EXTEND8_S_I32:
    Opc = A64 ? WebAssembly::LOAD8_S_I32_A64 : WebAssembly::LOAD8_S_I32_A32;
    break;
  case WebAssembly::I32_EXTEND16_S_I32:
    Opc = A64 ? WebAssembly::LOAD16_S_I32_A64 : WebAssembly::LOAD16_S_I32_A32;
    break;
  case WebAssembly::I64_EXTEND8_S_I64:
    Opc = A64 ? WebAssembly::LOAD8_S_I64_A64 : WebAssembly::LOAD8_S_I64_A32;
    break;
  case WebAssembly::I64_EXTEND16_S_I64:
    Opc = A64 ? WebAssembly::LOAD16_S_I64_A64 : WebAssembly::LOAD16_S_I64_A32;
    break;
  case WebAssembly::I64_EXTEND32_S_I64:
  case WebAssembly::I64_EXTEND_S_I32:
    Opc = A64 ? WebAssembly::LOAD32_S_I64_A64 : WebAssembly::LOAD32_S_I64_A32;
    break;
  }

  return Opc;
}

bool WebAssemblyFastISel::WebAssemblyEmitLoad(unsigned Opc, Register &ResultReg,
                                              Address &Addr,
                                              MachineMemOperand *MMO) {
  MachineInstrBuilder MIB =
      BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg);
  addLoadStoreOperands(Addr, MIB, MMO);
  return true;
}

bool WebAssemblyFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
                                              const LoadInst *LI) {
  bool A64 = Subtarget->hasAddr64();
  unsigned NewOpc;
  if ((NewOpc = getSExtLoadOpcode(MI->getOpcode(), A64)) ==
      WebAssembly::INSTRUCTION_LIST_END)
    return false;

  Address Addr;
  if (!computeAddress(LI->getPointerOperand(), Addr))
    return false;

  Register ResultReg = MI->getOperand(0).getReg();
  if (!WebAssemblyEmitLoad(NewOpc, ResultReg, Addr,
                           createMachineMemOperandFor(LI)))
    return false;


Review comment (suggestion):
It looks like there's only one call to this function and it's just 2 lines. It looks like you could maybe reuse it down in `selectLoad`? Or otherwise, it probably makes sense to just move the lines directly into `tryToFoldLoadIntoMI`

Code after:
  default:
    return WebAssembly::INSTRUCTION_LIST_END;
  case WebAssembly::I32_EXTEND8_S_I32:
    Opc = A64 ? WebAssembly::LOAD8_S_I32_A64 : WebAssembly::LOAD8_S_I32_A32;
    break;
  case WebAssembly::I32_EXTEND16_S_I32:
    Opc = A64 ? WebAssembly::LOAD16_S_I32_A64 : WebAssembly::LOAD16_S_I32_A32;
    break;
  case WebAssembly::I64_EXTEND8_S_I64:
    Opc = A64 ? WebAssembly::LOAD8_S_I64_A64 : WebAssembly::LOAD8_S_I64_A32;
    break;
  case WebAssembly::I64_EXTEND16_S_I64:
    Opc = A64 ? WebAssembly::LOAD16_S_I64_A64 : WebAssembly::LOAD16_S_I64_A32;
    break;
  case WebAssembly::I64_EXTEND32_S_I64:
  case WebAssembly::I64_EXTEND_S_I32:
    Opc = A64 ? WebAssembly::LOAD32_S_I64_A64 : WebAssembly::LOAD32_S_I64_A32;
    break;
  }

  return Opc;
}

bool WebAssemblyFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
                                              const LoadInst *LI) {
  bool A64 = Subtarget->hasAddr64();
  unsigned NewOpc;
  if ((NewOpc = getSExtLoadOpcode(MI->getOpcode(), A64)) ==
      WebAssembly::INSTRUCTION_LIST_END)
    return false;

  Address Addr;
  if (!computeAddress(LI->getPointerOperand(), Addr))
    return false;

  Register ResultReg = MI->getOperand(0).getReg();
  MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
                                    TII.get(NewOpc), ResultReg);
  addLoadStoreOperands(Addr, MIB, createMachineMemOperandFor(LI));
  MachineBasicBlock::iterator Iter(MI);
  removeDeadCode(Iter, std::next(Iter));
  return true;
}

bool WebAssemblyFastISel::selectLoad(const Instruction *I) {
  const auto *Load = cast<LoadInst>(I);
  if (Load->isAtomic())
    return false;
  if (!WebAssembly::isDefaultAddressSpace(Load->getPointerAddressSpace()))
    return false;
  if (!Subtarget->hasSIMD128() && Load->getType()->isVectorTy())