LLVM  4.0.0
WebAssemblyCallIndirectFixup.cpp
Go to the documentation of this file.
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
11 /// \brief This file converts pseudo call_indirect instructions into real
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 
25 #include "WebAssembly.h"
26 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
28 #include "WebAssemblySubtarget.h"
35 #include "llvm/CodeGen/Passes.h"
36 #include "llvm/Support/Debug.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 
58  return new WebAssemblyCallIndirectFixup();
59 }
60 
62  switch (MI.getOpcode()) {
63  using namespace WebAssembly;
64  case PCALL_INDIRECT_VOID: return CALL_INDIRECT_VOID;
65  case PCALL_INDIRECT_I32: return CALL_INDIRECT_I32;
66  case PCALL_INDIRECT_I64: return CALL_INDIRECT_I64;
67  case PCALL_INDIRECT_F32: return CALL_INDIRECT_F32;
68  case PCALL_INDIRECT_F64: return CALL_INDIRECT_F64;
69  case PCALL_INDIRECT_v16i8: return CALL_INDIRECT_v16i8;
70  case PCALL_INDIRECT_v8i16: return CALL_INDIRECT_v8i16;
71  case PCALL_INDIRECT_v4i32: return CALL_INDIRECT_v4i32;
72  case PCALL_INDIRECT_v4f32: return CALL_INDIRECT_v4f32;
73  default: return INSTRUCTION_LIST_END;
74  }
75 }
76 
77 static bool IsPseudoCallIndirect(const MachineInstr &MI) {
78  return GetNonPseudoCallIndirectOpcode(MI) !=
79  WebAssembly::INSTRUCTION_LIST_END;
80 }
81 
82 bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
83  DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
84  << MF.getName() << '\n');
85 
86  bool Changed = false;
87  const WebAssemblyInstrInfo *TII =
88  MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
89 
90  for (MachineBasicBlock &MBB : MF) {
91  for (MachineInstr &MI : MBB) {
92  if (IsPseudoCallIndirect(MI)) {
93  DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
94 
95  // Rewrite pseudo to non-pseudo
97  MI.setDesc(Desc);
98 
99  // Rewrite argument order
100  auto Uses = MI.explicit_uses();
101  MachineInstr::mop_iterator it = Uses.begin();
102  const MachineOperand MO = *it;
103 
104  // Set up the flags immediate, which currently has no defined flags
105  // so it's always zero.
106  it->ChangeToImmediate(0);
107 
108  MI.addOperand(MF, MO);
109 
110  DEBUG(dbgs() << " After transform: " << MI);
111  Changed = true;
112  }
113  }
114  }
115 
116  DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
117 
118  return Changed;
119 }
120 
static unsigned GetNonPseudoCallIndirectOpcode(const MachineInstr &MI)
FunctionPass * createWebAssemblyCallIndirectFixup()
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static bool IsPseudoCallIndirect(const MachineInstr &MI)
MachineBasicBlock * MBB
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:273
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
This file provides WebAssembly-specific target descriptions.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
This file declares the WebAssembly-specific subclass of TargetSubtarget.
MachineOperand class - Representation of each machine instruction operand.
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:52
This file declares WebAssembly-specific per-machine-function information.
#define DEBUG(X)
Definition: Debug.h:100
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
StringRef getName() const
getName - Return the name of the corresponding LLVM function.