WebAssemblyCallIndirectFixup.cpp revision 341825
1193323Sed//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed/// 10193323Sed/// \file 11193323Sed/// This file converts pseudo call_indirect instructions into real 12193323Sed/// call_indirects. 13193323Sed/// 14193323Sed/// The order of arguments for a call_indirect is the arguments to the function 15193323Sed/// call, followed by the function pointer. There's no natural way to express 16193323Sed/// a machineinstr with varargs followed by one more arg, so we express it as 17193323Sed/// the function pointer followed by varargs, then rewrite it here. 18193323Sed/// 19193323Sed/// We need to rewrite the order of the arguments on the machineinstrs 20193323Sed/// themselves so that register stackification knows the order they'll be 21193323Sed/// executed in. 22193323Sed/// 23193323Sed//===----------------------------------------------------------------------===// 24193323Sed 25193323Sed#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_* 26193323Sed#include "WebAssembly.h" 27193323Sed#include "WebAssemblyMachineFunctionInfo.h" 28193323Sed#include "WebAssemblySubtarget.h" 29193323Sed#include "llvm/Analysis/AliasAnalysis.h" 30193323Sed#include "llvm/CodeGen/LiveIntervals.h" 31193323Sed#include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 32193323Sed#include "llvm/CodeGen/MachineDominators.h" 33202375Srdivacky#include "llvm/CodeGen/MachineInstrBuilder.h" 34193323Sed#include "llvm/CodeGen/MachineRegisterInfo.h" 35193323Sed#include "llvm/CodeGen/Passes.h" 36193323Sed#include "llvm/Support/Debug.h" 37193323Sed#include "llvm/Support/raw_ostream.h" 38193323Sedusing namespace llvm; 39193323Sed 40193323Sed#define DEBUG_TYPE "wasm-call-indirect-fixup" 41193323Sed 42193323Sednamespace { 43193323Sedclass WebAssemblyCallIndirectFixup final : public MachineFunctionPass { 44193323Sed StringRef getPassName() const override { 45193323Sed return "WebAssembly CallIndirect Fixup"; 46193323Sed } 47193323Sed 48193323Sed bool runOnMachineFunction(MachineFunction &MF) override; 49193323Sed 50193323Sedpublic: 51198090Srdivacky static char ID; // Pass identification, replacement for typeid 52193323Sed WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {} 53193323Sed}; 54199481Srdivacky} // end anonymous namespace 55199481Srdivacky 56199481Srdivackychar WebAssemblyCallIndirectFixup::ID = 0; 57199481SrdivackyINITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE, 58199481Srdivacky "Rewrite call_indirect argument orderings", false, false) 59199481Srdivacky 60199481SrdivackyFunctionPass *llvm::createWebAssemblyCallIndirectFixup() { 61199481Srdivacky return new WebAssemblyCallIndirectFixup(); 62193323Sed} 63202375Srdivacky 64193323Sedstatic unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI) { 65193323Sed switch (MI.getOpcode()) { 66202375Srdivacky using namespace WebAssembly; 67202375Srdivacky case PCALL_INDIRECT_VOID: return CALL_INDIRECT_VOID; 68193323Sed case PCALL_INDIRECT_I32: return CALL_INDIRECT_I32; 69202375Srdivacky case PCALL_INDIRECT_I64: return CALL_INDIRECT_I64; 70193323Sed case PCALL_INDIRECT_F32: return CALL_INDIRECT_F32; 71193323Sed case PCALL_INDIRECT_F64: return CALL_INDIRECT_F64; 72202375Srdivacky case PCALL_INDIRECT_v16i8: return CALL_INDIRECT_v16i8; 73202375Srdivacky case PCALL_INDIRECT_v8i16: return CALL_INDIRECT_v8i16; 74193323Sed case PCALL_INDIRECT_v4i32: return CALL_INDIRECT_v4i32; 75193323Sed case PCALL_INDIRECT_v4f32: return CALL_INDIRECT_v4f32; 76193323Sed default: return INSTRUCTION_LIST_END; 77193323Sed } 78193323Sed} 79193323Sed 80193323Sedstatic bool IsPseudoCallIndirect(const MachineInstr &MI) { 81193323Sed return GetNonPseudoCallIndirectOpcode(MI) != 82193323Sed WebAssembly::INSTRUCTION_LIST_END; 83193323Sed} 84193323Sed 85193323Sedbool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) { 86193323Sed LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n" 87193323Sed << MF.getName() << '\n'); 88193323Sed 89193323Sed bool Changed = false; 90193323Sed const WebAssemblyInstrInfo *TII = 91193323Sed MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 92193323Sed 93193323Sed for (MachineBasicBlock &MBB : MF) { 94193323Sed for (MachineInstr &MI : MBB) { 95193323Sed if (IsPseudoCallIndirect(MI)) { 96193323Sed LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n'); 97193323Sed 98193323Sed // Rewrite pseudo to non-pseudo 99193323Sed const MCInstrDesc &Desc = TII->get(GetNonPseudoCallIndirectOpcode(MI)); 100193323Sed MI.setDesc(Desc); 101193323Sed 102193323Sed // Rewrite argument order 103193323Sed SmallVector<MachineOperand, 8> Ops; 104193323Sed 105193323Sed // Set up a placeholder for the type signature immediate. 106193323Sed Ops.push_back(MachineOperand::CreateImm(0)); 107193323Sed 108193323Sed // Set up the flags immediate, which currently has no defined flags 109193323Sed // so it's always zero. 110193323Sed Ops.push_back(MachineOperand::CreateImm(0)); 111193323Sed 112193323Sed for (const MachineOperand &MO : 113193323Sed make_range(MI.operands_begin() + 114193323Sed MI.getDesc().getNumDefs() + 1, 115193323Sed MI.operands_begin() + 116193323Sed MI.getNumExplicitOperands())) 117193323Sed Ops.push_back(MO); 118193323Sed Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs())); 119193323Sed 120193323Sed // Replace the instructions operands. 121193323Sed while (MI.getNumOperands() > MI.getDesc().getNumDefs()) 122193323Sed MI.RemoveOperand(MI.getNumOperands() - 1); 123193323Sed for (const MachineOperand &MO : Ops) 124193323Sed MI.addOperand(MO); 125193323Sed 126193323Sed LLVM_DEBUG(dbgs() << " After transform: " << MI); 127193323Sed Changed = true; 128193323Sed } 129193323Sed } 130193323Sed } 131193323Sed 132193323Sed LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n"); 133193323Sed 134193323Sed return Changed; 135193323Sed} 136193323Sed 137193323Sed