2016-10-22 00:38:07 +08:00
|
|
|
//===-- WebAssemblyCallIndirectFixup.cpp - Fix call_indirects -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-10-22 00:38:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file converts pseudo call_indirect instructions into real
|
2016-10-22 00:38:07 +08:00
|
|
|
/// call_indirects.
|
|
|
|
///
|
|
|
|
/// The order of arguments for a call_indirect is the arguments to the function
|
|
|
|
/// call, followed by the function pointer. There's no natural way to express
|
|
|
|
/// a machineinstr with varargs followed by one more arg, so we express it as
|
|
|
|
/// the function pointer followed by varargs, then rewrite it here.
|
|
|
|
///
|
|
|
|
/// We need to rewrite the order of the arguments on the machineinstrs
|
|
|
|
/// themselves so that register stackification knows the order they'll be
|
|
|
|
/// executed in.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2016-10-22 00:38:07 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2016-10-22 00:38:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-call-indirect-fixup"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyCallIndirectFixup final : public MachineFunctionPass {
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "WebAssembly CallIndirect Fixup";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyCallIndirectFixup() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyCallIndirectFixup::ID = 0;
|
2018-03-31 04:36:58 +08:00
|
|
|
INITIALIZE_PASS(WebAssemblyCallIndirectFixup, DEBUG_TYPE,
|
|
|
|
"Rewrite call_indirect argument orderings", false, false)
|
|
|
|
|
2016-10-22 00:38:07 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyCallIndirectFixup() {
|
|
|
|
return new WebAssemblyCallIndirectFixup();
|
|
|
|
}
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
static unsigned getNonPseudoCallIndirectOpcode(const MachineInstr &MI) {
|
2016-10-22 00:38:07 +08:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
using namespace WebAssembly;
|
2018-09-05 09:27:38 +08:00
|
|
|
case PCALL_INDIRECT_VOID:
|
|
|
|
return CALL_INDIRECT_VOID;
|
2019-06-27 00:17:15 +08:00
|
|
|
case PCALL_INDIRECT_i32:
|
|
|
|
return CALL_INDIRECT_i32;
|
|
|
|
case PCALL_INDIRECT_i64:
|
|
|
|
return CALL_INDIRECT_i64;
|
|
|
|
case PCALL_INDIRECT_f32:
|
|
|
|
return CALL_INDIRECT_f32;
|
|
|
|
case PCALL_INDIRECT_f64:
|
|
|
|
return CALL_INDIRECT_f64;
|
2018-09-05 09:27:38 +08:00
|
|
|
case PCALL_INDIRECT_v16i8:
|
|
|
|
return CALL_INDIRECT_v16i8;
|
|
|
|
case PCALL_INDIRECT_v8i16:
|
|
|
|
return CALL_INDIRECT_v8i16;
|
|
|
|
case PCALL_INDIRECT_v4i32:
|
|
|
|
return CALL_INDIRECT_v4i32;
|
|
|
|
case PCALL_INDIRECT_v2i64:
|
|
|
|
return CALL_INDIRECT_v2i64;
|
|
|
|
case PCALL_INDIRECT_v4f32:
|
|
|
|
return CALL_INDIRECT_v4f32;
|
|
|
|
case PCALL_INDIRECT_v2f64:
|
|
|
|
return CALL_INDIRECT_v2f64;
|
2019-07-16 06:49:25 +08:00
|
|
|
case PCALL_INDIRECT_exnref:
|
|
|
|
return CALL_INDIRECT_exnref;
|
2019-06-27 00:17:15 +08:00
|
|
|
case PRET_CALL_INDIRECT:
|
|
|
|
return RET_CALL_INDIRECT;
|
2018-09-05 09:27:38 +08:00
|
|
|
default:
|
|
|
|
return INSTRUCTION_LIST_END;
|
2016-10-22 00:38:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
static bool isPseudoCallIndirect(const MachineInstr &MI) {
|
|
|
|
return getNonPseudoCallIndirectOpcode(MI) !=
|
2016-10-22 00:38:07 +08:00
|
|
|
WebAssembly::INSTRUCTION_LIST_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyCallIndirectFixup::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** Fixing up CALL_INDIRECTs **********\n"
|
2019-01-10 07:05:21 +08:00
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
2016-10-22 00:38:07 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
const WebAssemblyInstrInfo *TII =
|
|
|
|
MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
for (MachineInstr &MI : MBB) {
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
if (isPseudoCallIndirect(MI)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Found call_indirect: " << MI << '\n');
|
2016-10-22 00:38:07 +08:00
|
|
|
|
|
|
|
// Rewrite pseudo to non-pseudo
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
const MCInstrDesc &Desc = TII->get(getNonPseudoCallIndirectOpcode(MI));
|
2016-10-22 00:38:07 +08:00
|
|
|
MI.setDesc(Desc);
|
|
|
|
|
|
|
|
// Rewrite argument order
|
2017-02-25 07:18:00 +08:00
|
|
|
SmallVector<MachineOperand, 8> Ops;
|
|
|
|
|
|
|
|
// Set up a placeholder for the type signature immediate.
|
|
|
|
Ops.push_back(MachineOperand::CreateImm(0));
|
2016-10-26 00:55:52 +08:00
|
|
|
|
|
|
|
// Set up the flags immediate, which currently has no defined flags
|
|
|
|
// so it's always zero.
|
2017-02-25 07:18:00 +08:00
|
|
|
Ops.push_back(MachineOperand::CreateImm(0));
|
|
|
|
|
|
|
|
for (const MachineOperand &MO :
|
2018-09-05 09:27:38 +08:00
|
|
|
make_range(MI.operands_begin() + MI.getDesc().getNumDefs() + 1,
|
|
|
|
MI.operands_begin() + MI.getNumExplicitOperands()))
|
2017-02-25 07:18:00 +08:00
|
|
|
Ops.push_back(MO);
|
|
|
|
Ops.push_back(MI.getOperand(MI.getDesc().getNumDefs()));
|
|
|
|
|
|
|
|
// Replace the instructions operands.
|
|
|
|
while (MI.getNumOperands() > MI.getDesc().getNumDefs())
|
|
|
|
MI.RemoveOperand(MI.getNumOperands() - 1);
|
|
|
|
for (const MachineOperand &MO : Ops)
|
|
|
|
MI.addOperand(MO);
|
2016-10-22 00:38:07 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " After transform: " << MI);
|
2016-10-22 00:38:07 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nDone fixing up CALL_INDIRECTs\n\n");
|
2016-10-22 00:38:07 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|