Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file converts pseudo call_indirect instructions into real |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 12 | /// call_indirects. |
| 13 | /// |
| 14 | /// The order of arguments for a call_indirect is the arguments to the function |
| 15 | /// call, followed by the function pointer. There's no natural way to express |
| 16 | /// a machineinstr with varargs followed by one more arg, so we express it as |
| 17 | /// the function pointer followed by varargs, then rewrite it here. |
| 18 | /// |
| 19 | /// We need to rewrite the order of the arguments on the machineinstrs |
| 20 | /// themselves so that register stackification knows the order they'll be |
| 21 | /// executed in. |
| 22 | /// |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 25 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_* |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 26 | #include "WebAssembly.h" |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 27 | #include "WebAssemblyMachineFunctionInfo.h" |
| 28 | #include "WebAssemblySubtarget.h" |
| 29 | #include "llvm/Analysis/AliasAnalysis.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/LiveIntervals.h" |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 32 | #include "llvm/CodeGen/MachineDominators.h" |
| 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/Passes.h" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | using namespace llvm; |
| 39 | |
| 40 | #define DEBUG_TYPE "wasm-call-indirect-fixup" |
| 41 | |
| 42 | namespace { |
| 43 | class WebAssemblyCallIndirectFixup final : public MachineFunctionPass { |
| 44 | StringRef getPassName() const override { |
| 45 | return "WebAssembly CallIndirect Fixup"; |
| 46 | } |
| 47 | |
| 48 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 49 | |
| 50 | public: |
| 51 | static char ID; // Pass identification, replacement for typeid |
| 52 | WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {} |
| 53 | }; |
| 54 | } // end anonymous namespace |
| 55 | |
| 56 | char WebAssemblyCallIndirectFixup::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 57 | INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE, |
| 58 | "Rewrite call_indirect argument orderings", false, false) |
| 59 | |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 60 | FunctionPass *llvm::createWebAssemblyCallIndirectFixup() { |
| 61 | return new WebAssemblyCallIndirectFixup(); |
| 62 | } |
| 63 | |
| 64 | static unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI) { |
| 65 | switch (MI.getOpcode()) { |
| 66 | using namespace WebAssembly; |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 67 | case PCALL_INDIRECT_VOID: |
| 68 | return CALL_INDIRECT_VOID; |
| 69 | case PCALL_INDIRECT_I32: |
| 70 | return CALL_INDIRECT_I32; |
| 71 | case PCALL_INDIRECT_I64: |
| 72 | return CALL_INDIRECT_I64; |
| 73 | case PCALL_INDIRECT_F32: |
| 74 | return CALL_INDIRECT_F32; |
| 75 | case PCALL_INDIRECT_F64: |
| 76 | return CALL_INDIRECT_F64; |
| 77 | case PCALL_INDIRECT_v16i8: |
| 78 | return CALL_INDIRECT_v16i8; |
| 79 | case PCALL_INDIRECT_v8i16: |
| 80 | return CALL_INDIRECT_v8i16; |
| 81 | case PCALL_INDIRECT_v4i32: |
| 82 | return CALL_INDIRECT_v4i32; |
| 83 | case PCALL_INDIRECT_v2i64: |
| 84 | return CALL_INDIRECT_v2i64; |
| 85 | case PCALL_INDIRECT_v4f32: |
| 86 | return CALL_INDIRECT_v4f32; |
| 87 | case PCALL_INDIRECT_v2f64: |
| 88 | return CALL_INDIRECT_v2f64; |
| 89 | default: |
| 90 | return INSTRUCTION_LIST_END; |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | static bool IsPseudoCallIndirect(const MachineInstr &MI) { |
| 95 | return GetNonPseudoCallIndirectOpcode(MI) != |
| 96 | WebAssembly::INSTRUCTION_LIST_END; |
| 97 | } |
| 98 | |
| 99 | bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 100 | LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n" |
Heejin Ahn | 569f090 | 2019-01-09 23:05:21 +0000 | [diff] [blame^] | 101 | << "********** Function: " << MF.getName() << '\n'); |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 102 | |
| 103 | bool Changed = false; |
| 104 | const WebAssemblyInstrInfo *TII = |
| 105 | MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 106 | |
| 107 | for (MachineBasicBlock &MBB : MF) { |
| 108 | for (MachineInstr &MI : MBB) { |
| 109 | if (IsPseudoCallIndirect(MI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 110 | LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n'); |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 111 | |
| 112 | // Rewrite pseudo to non-pseudo |
| 113 | const MCInstrDesc &Desc = TII->get(GetNonPseudoCallIndirectOpcode(MI)); |
| 114 | MI.setDesc(Desc); |
| 115 | |
| 116 | // Rewrite argument order |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 117 | SmallVector<MachineOperand, 8> Ops; |
| 118 | |
| 119 | // Set up a placeholder for the type signature immediate. |
| 120 | Ops.push_back(MachineOperand::CreateImm(0)); |
Dan Gohman | f50d964 | 2016-10-25 16:55:52 +0000 | [diff] [blame] | 121 | |
| 122 | // Set up the flags immediate, which currently has no defined flags |
| 123 | // so it's always zero. |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 124 | Ops.push_back(MachineOperand::CreateImm(0)); |
Dan Gohman | f50d964 | 2016-10-25 16:55:52 +0000 | [diff] [blame] | 125 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 126 | for (const MachineOperand &MO : |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 127 | make_range(MI.operands_begin() + MI.getDesc().getNumDefs() + 1, |
| 128 | MI.operands_begin() + MI.getNumExplicitOperands())) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 129 | Ops.push_back(MO); |
| 130 | Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs())); |
| 131 | |
| 132 | // Replace the instructions operands. |
| 133 | while (MI.getNumOperands() > MI.getDesc().getNumDefs()) |
| 134 | MI.RemoveOperand(MI.getNumOperands() - 1); |
| 135 | for (const MachineOperand &MO : Ops) |
| 136 | MI.addOperand(MO); |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 137 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 138 | LLVM_DEBUG(dbgs() << " After transform: " << MI); |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 139 | Changed = true; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 144 | LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n"); |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 145 | |
| 146 | return Changed; |
| 147 | } |