LLVM  10.0.0svn
WebAssemblyCallIndirectFixup.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file converts pseudo call_indirect instructions into real
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 
24 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
25 #include "WebAssembly.h"
27 #include "WebAssemblySubtarget.h"
34 #include "llvm/CodeGen/Passes.h"
35 #include "llvm/Support/Debug.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 
56 INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE,
57  "Rewrite call_indirect argument orderings", false, false)
58 
60  return new WebAssemblyCallIndirectFixup();
61 }
62 
64  switch (MI.getOpcode()) {
65  using namespace WebAssembly;
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  case PCALL_INDIRECT_exnref:
89  return CALL_INDIRECT_exnref;
90  case PRET_CALL_INDIRECT:
91  return RET_CALL_INDIRECT;
92  default:
93  return INSTRUCTION_LIST_END;
94  }
95 }
96 
97 static bool isPseudoCallIndirect(const MachineInstr &MI) {
98  return getNonPseudoCallIndirectOpcode(MI) !=
99  WebAssembly::INSTRUCTION_LIST_END;
100 }
101 
102 bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
103  LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
104  << "********** Function: " << MF.getName() << '\n');
105 
106  bool Changed = false;
107  const WebAssemblyInstrInfo *TII =
108  MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
109 
110  for (MachineBasicBlock &MBB : MF) {
111  for (MachineInstr &MI : MBB) {
112  if (isPseudoCallIndirect(MI)) {
113  LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
114 
115  // Rewrite pseudo to non-pseudo
116  const MCInstrDesc &Desc = TII->get(getNonPseudoCallIndirectOpcode(MI));
117  MI.setDesc(Desc);
118 
119  // Rewrite argument order
121 
122  // Set up a placeholder for the type signature immediate.
124 
125  // Set up the flags immediate, which currently has no defined flags
126  // so it's always zero.
128 
129  for (const MachineOperand &MO :
130  make_range(MI.operands_begin() + MI.getDesc().getNumDefs() + 1,
131  MI.operands_begin() + MI.getNumExplicitOperands()))
132  Ops.push_back(MO);
133  Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
134 
135  // Replace the instructions operands.
136  while (MI.getNumOperands() > MI.getDesc().getNumDefs())
137  MI.RemoveOperand(MI.getNumOperands() - 1);
138  for (const MachineOperand &MO : Ops)
139  MI.addOperand(MO);
140 
141  LLVM_DEBUG(dbgs() << " After transform: " << MI);
142  Changed = true;
143  }
144  }
145  }
146 
147  LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
148 
149  return Changed;
150 }
static bool isPseudoCallIndirect(const MachineInstr &MI)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:179
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:410
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
FunctionPass * createWebAssemblyCallIndirectFixup()
This file provides WebAssembly-specific target descriptions.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static unsigned getNonPseudoCallIndirectOpcode(const MachineInstr &MI)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
#define DEBUG_TYPE
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Representation of each machine instruction.
Definition: MachineInstr.h:63
static MachineOperand CreateImm(int64_t Val)
This file declares WebAssembly-specific per-machine-function information.
INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE, "Rewrite call_indirect argument orderings", false, false) FunctionPass *llvm
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122