2016-05-10 12:24:02 +08:00
|
|
|
//===- WebAssemblyPrepareForLiveIntervals.cpp - Prepare for LiveIntervals -===//
|
|
|
|
//
|
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-05-10 12:24:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Fix up code to meet LiveInterval's requirements.
|
2016-05-10 12:24:02 +08:00
|
|
|
///
|
|
|
|
/// Some CodeGen passes don't preserve LiveInterval's requirements, because
|
|
|
|
/// they run after register allocation and it isn't important. However,
|
|
|
|
/// WebAssembly runs LiveIntervals in a late pass. This pass transforms code
|
|
|
|
/// to meet LiveIntervals' requirements; primarily, it ensures that all
|
|
|
|
/// virtual register uses have definitions (IMPLICIT_DEF definitions if
|
|
|
|
/// nothing else).
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2016-05-10 12:24:02 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
2016-10-25 03:49:43 +08:00
|
|
|
#include "WebAssemblyUtilities.h"
|
2016-05-10 12:24:02 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.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-prepare-for-live-intervals"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyPrepareForLiveIntervals final : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyPrepareForLiveIntervals() : MachineFunctionPass(ID) {}
|
|
|
|
|
|
|
|
private:
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2016-05-10 12:24:02 +08:00
|
|
|
return "WebAssembly Prepare For LiveIntervals";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyPrepareForLiveIntervals::ID = 0;
|
2018-03-31 04:36:58 +08:00
|
|
|
INITIALIZE_PASS(WebAssemblyPrepareForLiveIntervals, DEBUG_TYPE,
|
|
|
|
"Fix up code for LiveIntervals", false, false)
|
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyPrepareForLiveIntervals() {
|
|
|
|
return new WebAssemblyPrepareForLiveIntervals();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test whether the given register has an ARGUMENT def.
|
[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 hasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
|
2016-08-03 07:16:09 +08:00
|
|
|
for (const auto &Def : MRI.def_instructions(Reg))
|
2019-07-13 06:08:25 +08:00
|
|
|
if (WebAssembly::isArgument(Def.getOpcode()))
|
2016-05-10 12:24:02 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
bool WebAssemblyPrepareForLiveIntervals::runOnMachineFunction(
|
|
|
|
MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2016-05-10 12:24:02 +08:00
|
|
|
dbgs() << "********** Prepare For LiveIntervals **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n';
|
|
|
|
});
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
MachineBasicBlock &Entry = *MF.begin();
|
|
|
|
|
|
|
|
assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
|
|
|
|
"LiveIntervals shouldn't be active yet!");
|
|
|
|
|
|
|
|
// We don't preserve SSA form.
|
|
|
|
MRI.leaveSSA();
|
|
|
|
|
|
|
|
// BranchFolding and perhaps other passes don't preserve IMPLICIT_DEF
|
|
|
|
// instructions. LiveIntervals requires that all paths to virtual register
|
|
|
|
// uses provide a definition. Insert IMPLICIT_DEFs in the entry block to
|
|
|
|
// conservatively satisfy this.
|
|
|
|
//
|
|
|
|
// TODO: This is fairly heavy-handed; find a better approach.
|
|
|
|
//
|
[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
|
|
|
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
|
2019-08-02 07:27:28 +08:00
|
|
|
unsigned Reg = Register::index2VirtReg(I);
|
2016-05-10 12:24:02 +08:00
|
|
|
|
|
|
|
// Skip unused registers.
|
|
|
|
if (MRI.use_nodbg_empty(Reg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip registers that have an ARGUMENT definition.
|
[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 (hasArgumentDef(Reg, MRI))
|
2016-05-10 12:24:02 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
BuildMI(Entry, Entry.begin(), DebugLoc(),
|
|
|
|
TII.get(WebAssembly::IMPLICIT_DEF), Reg);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move ARGUMENT_* instructions to the top of the entry block, so that their
|
|
|
|
// liveness reflects the fact that these really are live-in values.
|
2018-09-05 09:27:38 +08:00
|
|
|
for (auto MII = Entry.begin(), MIE = Entry.end(); MII != MIE;) {
|
2016-10-25 03:49:43 +08:00
|
|
|
MachineInstr &MI = *MII++;
|
2019-07-13 06:08:25 +08:00
|
|
|
if (WebAssembly::isArgument(MI.getOpcode())) {
|
2016-10-25 03:49:43 +08:00
|
|
|
MI.removeFromParent();
|
|
|
|
Entry.insert(Entry.begin(), &MI);
|
2016-05-10 12:24:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 10:51:04 +08:00
|
|
|
// Ok, we're now ready to run the LiveIntervals analysis again.
|
2016-05-10 12:24:02 +08:00
|
|
|
MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|