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