2019-01-09 06:35:18 +08:00
|
|
|
//== WebAssemblyMemIntrinsicResults.cpp - Optimize memory intrinsic results ==//
|
2015-11-26 00:55:01 +08:00
|
|
|
//
|
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
|
2015-11-26 00:55:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2019-01-09 06:35:18 +08:00
|
|
|
/// This file implements an optimization pass using memory intrinsic results.
|
2015-11-26 00:55:01 +08:00
|
|
|
///
|
2019-01-09 06:35:18 +08:00
|
|
|
/// Calls to memory intrinsics (memcpy, memmove, memset) return the destination
|
|
|
|
/// address. They are in the form of
|
|
|
|
/// %dst_new = call @memcpy %dst, %src, %len
|
|
|
|
/// where %dst and %dst_new registers contain the same value.
|
2015-11-26 00:55:01 +08:00
|
|
|
///
|
2019-01-09 06:35:18 +08:00
|
|
|
/// This is to enable an optimization wherein uses of the %dst register used in
|
|
|
|
/// the parameter can be replaced by uses of the %dst_new register used in the
|
|
|
|
/// result, making the %dst register more likely to be single-use, thus more
|
|
|
|
/// likely to be useful to register stackifying, and potentially also exposing
|
|
|
|
/// the call instruction itself to register stackifying. These both can reduce
|
|
|
|
/// local.get/local.set traffic.
|
|
|
|
///
|
|
|
|
/// The LLVM intrinsics for these return void so they can't use the returned
|
|
|
|
/// attribute and consequently aren't handled by the OptimizeReturned pass.
|
2016-01-26 12:01:11 +08:00
|
|
|
///
|
2015-11-26 00:55:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2015-11-26 00:55:01 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
2016-01-26 12:01:11 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2015-11-26 00:55:01 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.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;
|
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
#define DEBUG_TYPE "wasm-mem-intrinsic-results"
|
2015-11-26 00:55:01 +08:00
|
|
|
|
|
|
|
namespace {
|
2019-01-09 06:35:18 +08:00
|
|
|
class WebAssemblyMemIntrinsicResults final : public MachineFunctionPass {
|
2015-11-26 00:55:01 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2019-01-09 06:35:18 +08:00
|
|
|
WebAssemblyMemIntrinsicResults() : MachineFunctionPass(ID) {}
|
2015-11-26 00:55:01 +08:00
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "WebAssembly Memory Intrinsic Results";
|
|
|
|
}
|
2015-11-26 00:55:01 +08:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<MachineBlockFrequencyInfo>();
|
|
|
|
AU.addPreserved<MachineBlockFrequencyInfo>();
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
2016-05-10 12:24:02 +08:00
|
|
|
AU.addRequired<LiveIntervals>();
|
|
|
|
AU.addPreserved<SlotIndexes>();
|
|
|
|
AU.addPreserved<LiveIntervals>();
|
2016-01-26 12:01:11 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2015-11-26 00:55:01 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
char WebAssemblyMemIntrinsicResults::ID = 0;
|
|
|
|
INITIALIZE_PASS(WebAssemblyMemIntrinsicResults, DEBUG_TYPE,
|
|
|
|
"Optimize memory intrinsic result values for WebAssembly",
|
|
|
|
false, false)
|
2018-03-31 04:36:58 +08:00
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyMemIntrinsicResults() {
|
|
|
|
return new WebAssemblyMemIntrinsicResults();
|
2015-11-26 00:55:01 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 12:01:11 +08:00
|
|
|
// Replace uses of FromReg with ToReg if they are dominated by MI.
|
[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 replaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI,
|
2016-01-26 12:01:11 +08:00
|
|
|
unsigned FromReg, unsigned ToReg,
|
|
|
|
const MachineRegisterInfo &MRI,
|
2016-05-10 12:24:02 +08:00
|
|
|
MachineDominatorTree &MDT,
|
|
|
|
LiveIntervals &LIS) {
|
2016-01-26 12:01:11 +08:00
|
|
|
bool Changed = false;
|
2016-05-10 12:24:02 +08:00
|
|
|
|
|
|
|
LiveInterval *FromLI = &LIS.getInterval(FromReg);
|
|
|
|
LiveInterval *ToLI = &LIS.getInterval(ToReg);
|
|
|
|
|
|
|
|
SlotIndex FromIdx = LIS.getInstructionIndex(MI).getRegSlot();
|
|
|
|
VNInfo *FromVNI = FromLI->getVNInfoAt(FromIdx);
|
|
|
|
|
|
|
|
SmallVector<SlotIndex, 4> Indices;
|
|
|
|
|
2018-09-05 09:27:38 +08:00
|
|
|
for (auto I = MRI.use_nodbg_begin(FromReg), E = MRI.use_nodbg_end();
|
|
|
|
I != E;) {
|
2016-01-26 12:01:11 +08:00
|
|
|
MachineOperand &O = *I++;
|
|
|
|
MachineInstr *Where = O.getParent();
|
2016-05-10 12:24:02 +08:00
|
|
|
|
|
|
|
// Check that MI dominates the instruction in the normal way.
|
|
|
|
if (&MI == Where || !MDT.dominates(&MI, Where))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this use gets a different value, skip it.
|
|
|
|
SlotIndex WhereIdx = LIS.getInstructionIndex(*Where);
|
|
|
|
VNInfo *WhereVNI = FromLI->getVNInfoAt(WhereIdx);
|
|
|
|
if (WhereVNI && WhereVNI != FromVNI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Make sure ToReg isn't clobbered before it gets there.
|
|
|
|
VNInfo *ToVNI = ToLI->getVNInfoAt(WhereIdx);
|
|
|
|
if (ToVNI && ToVNI != FromVNI)
|
|
|
|
continue;
|
|
|
|
|
2016-01-26 12:01:11 +08:00
|
|
|
Changed = true;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from "
|
|
|
|
<< MI << "\n");
|
2016-01-26 12:01:11 +08:00
|
|
|
O.setReg(ToReg);
|
2016-05-10 12:24:02 +08:00
|
|
|
|
|
|
|
// If the store's def was previously dead, it is no longer.
|
2016-05-12 12:19:09 +08:00
|
|
|
if (!O.isUndef()) {
|
|
|
|
MI.getOperand(0).setIsDead(false);
|
2016-05-10 12:24:02 +08:00
|
|
|
|
2016-05-12 12:19:09 +08:00
|
|
|
Indices.push_back(WhereIdx.getRegSlot());
|
|
|
|
}
|
2016-05-10 12:24:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Changed) {
|
|
|
|
// Extend ToReg's liveness.
|
|
|
|
LIS.extendToIndices(*ToLI, Indices);
|
|
|
|
|
|
|
|
// Shrink FromReg's liveness.
|
|
|
|
LIS.shrinkToUses(FromLI);
|
|
|
|
|
|
|
|
// If we replaced all dominated uses, FromReg is now killed at MI.
|
|
|
|
if (!FromLI->liveAt(FromIdx.getDeadSlot()))
|
2018-09-05 09:27:38 +08:00
|
|
|
MI.addRegisterKilled(FromReg, MBB.getParent()
|
|
|
|
->getSubtarget<WebAssemblySubtarget>()
|
|
|
|
.getRegisterInfo());
|
2016-01-26 12:01:11 +08:00
|
|
|
}
|
2016-05-10 12:24:02 +08:00
|
|
|
|
2016-01-26 12:01:11 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-02-01 18:46:16 +08:00
|
|
|
static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI,
|
|
|
|
const MachineRegisterInfo &MRI,
|
2018-09-05 09:27:38 +08:00
|
|
|
MachineDominatorTree &MDT, LiveIntervals &LIS,
|
2016-02-01 18:46:16 +08:00
|
|
|
const WebAssemblyTargetLowering &TLI,
|
|
|
|
const TargetLibraryInfo &LibInfo) {
|
|
|
|
MachineOperand &Op1 = MI.getOperand(1);
|
|
|
|
if (!Op1.isSymbol())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
StringRef Name(Op1.getSymbolName());
|
[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
|
|
|
bool CallReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
|
2016-02-01 18:46:16 +08:00
|
|
|
Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
|
|
|
|
Name == TLI.getLibcallName(RTLIB::MEMSET);
|
[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 (!CallReturnsInput)
|
2016-02-01 18:46:16 +08:00
|
|
|
return false;
|
|
|
|
|
2017-01-24 08:01:18 +08:00
|
|
|
LibFunc Func;
|
2016-02-01 18:46:16 +08:00
|
|
|
if (!LibInfo.getLibFunc(Name, Func))
|
|
|
|
return false;
|
|
|
|
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 06:40:45 +08:00
|
|
|
Register FromReg = MI.getOperand(2).getReg();
|
|
|
|
Register ToReg = MI.getOperand(0).getReg();
|
2016-05-10 12:24:02 +08:00
|
|
|
if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg))
|
2019-01-09 06:35:18 +08:00
|
|
|
report_fatal_error("Memory Intrinsic results: call to builtin function "
|
|
|
|
"with wrong signature, from/to mismatch");
|
[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
|
|
|
return replaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT, LIS);
|
2016-02-01 18:46:16 +08:00
|
|
|
}
|
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
bool WebAssemblyMemIntrinsicResults::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2019-01-09 06:35:18 +08:00
|
|
|
dbgs() << "********** Memory Intrinsic Results **********\n"
|
2015-11-26 00:55:01 +08:00
|
|
|
<< "********** Function: " << MF.getName() << '\n';
|
|
|
|
});
|
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
[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
|
|
|
auto &MDT = getAnalysis<MachineDominatorTree>();
|
2016-01-26 12:01:11 +08:00
|
|
|
const WebAssemblyTargetLowering &TLI =
|
|
|
|
*MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
const auto &LibInfo =
|
|
|
|
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(MF.getFunction());
|
[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
|
|
|
auto &LIS = getAnalysis<LiveIntervals>();
|
2015-12-10 22:17:36 +08:00
|
|
|
bool Changed = false;
|
2015-11-26 00:55:01 +08:00
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
// We don't preserve SSA form.
|
|
|
|
MRI.leaveSSA();
|
|
|
|
|
2019-01-09 06:35:18 +08:00
|
|
|
assert(MRI.tracksLiveness() &&
|
|
|
|
"MemIntrinsicResults expects liveness tracking");
|
2015-12-08 11:30:42 +08:00
|
|
|
|
2015-12-03 08:50:30 +08:00
|
|
|
for (auto &MBB : MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
|
2015-11-26 00:55:01 +08:00
|
|
|
for (auto &MI : MBB)
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
2019-06-27 00:17:15 +08:00
|
|
|
case WebAssembly::CALL_i32:
|
|
|
|
case WebAssembly::CALL_i64:
|
2016-05-10 12:24:02 +08:00
|
|
|
Changed |= optimizeCall(MBB, MI, MRI, MDT, LIS, TLI, LibInfo);
|
2016-02-01 18:46:16 +08:00
|
|
|
break;
|
2016-01-26 12:01:11 +08:00
|
|
|
}
|
2015-12-03 08:50:30 +08:00
|
|
|
}
|
2015-11-26 00:55:01 +08:00
|
|
|
|
2015-12-10 22:17:36 +08:00
|
|
|
return Changed;
|
2015-11-26 00:55:01 +08:00
|
|
|
}
|