2015-06-30 07:51:55 +08:00
|
|
|
//===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file defines the WebAssembly-specific subclass of TargetMachine.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssembly.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
|
|
|
#include "WebAssemblyTargetMachine.h"
|
2015-12-17 12:55:44 +08:00
|
|
|
#include "WebAssemblyTargetObjectFile.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "WebAssemblyTargetTransformInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2016-05-10 11:21:59 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2015-07-02 07:41:25 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2015-06-30 07:51:55 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm"
|
|
|
|
|
2016-08-02 05:34:04 +08:00
|
|
|
// Emscripten's asm.js-style exception handling
|
|
|
|
static cl::opt<bool> EnableEmExceptionHandling(
|
2016-08-09 08:29:55 +08:00
|
|
|
"enable-emscripten-cxx-exceptions",
|
2016-08-02 05:34:04 +08:00
|
|
|
cl::desc("WebAssembly Emscripten-style exception handling"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
extern "C" void LLVMInitializeWebAssemblyTarget() {
|
|
|
|
// Register the target.
|
2015-07-02 05:42:34 +08:00
|
|
|
RegisterTargetMachine<WebAssemblyTargetMachine> X(TheWebAssemblyTarget32);
|
|
|
|
RegisterTargetMachine<WebAssemblyTargetMachine> Y(TheWebAssemblyTarget64);
|
2016-08-02 05:34:04 +08:00
|
|
|
|
|
|
|
// Register exception handling pass to opt
|
|
|
|
initializeWebAssemblyLowerEmscriptenExceptionsPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// WebAssembly Lowering public interface.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-19 11:00:05 +08:00
|
|
|
static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
|
|
|
|
if (!RM.hasValue())
|
|
|
|
return Reloc::PIC_;
|
|
|
|
return *RM;
|
|
|
|
}
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
/// Create an WebAssembly architecture model.
|
|
|
|
///
|
|
|
|
WebAssemblyTargetMachine::WebAssemblyTargetMachine(
|
|
|
|
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
|
2016-05-19 11:00:05 +08:00
|
|
|
const TargetOptions &Options, Optional<Reloc::Model> RM,
|
|
|
|
CodeModel::Model CM, CodeGenOpt::Level OL)
|
2016-01-07 11:19:23 +08:00
|
|
|
: LLVMTargetMachine(T,
|
|
|
|
TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
|
|
|
|
: "e-m:e-p:32:32-i64:64-n32:64-S128",
|
2016-05-19 11:00:05 +08:00
|
|
|
TT, CPU, FS, Options, getEffectiveRelocModel(RM),
|
|
|
|
CM, OL),
|
2015-12-17 12:55:44 +08:00
|
|
|
TLOF(make_unique<WebAssemblyTargetObjectFile>()) {
|
2015-11-10 08:30:57 +08:00
|
|
|
// WebAssembly type-checks expressions, but a noreturn function with a return
|
|
|
|
// type that doesn't match the context will cause a check failure. So we lower
|
|
|
|
// LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
|
|
|
|
// 'unreachable' expression which is meant for that case.
|
|
|
|
this->Options.TrapUnreachable = true;
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
initAsmInfo();
|
|
|
|
|
2016-02-18 14:32:53 +08:00
|
|
|
// Note that we don't use setRequiresStructuredCFG(true). It disables
|
|
|
|
// optimizations than we're ok with, and want, such as critical edge
|
|
|
|
// splitting and tail merging.
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
|
|
|
|
|
|
|
|
const WebAssemblySubtarget *
|
|
|
|
WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
|
|
|
|
Attribute CPUAttr = F.getFnAttribute("target-cpu");
|
|
|
|
Attribute FSAttr = F.getFnAttribute("target-features");
|
|
|
|
|
|
|
|
std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
|
|
|
|
? CPUAttr.getValueAsString().str()
|
|
|
|
: TargetCPU;
|
|
|
|
std::string FS = !FSAttr.hasAttribute(Attribute::None)
|
|
|
|
? FSAttr.getValueAsString().str()
|
|
|
|
: TargetFS;
|
|
|
|
|
|
|
|
auto &I = SubtargetMap[CPU + FS];
|
|
|
|
if (!I) {
|
|
|
|
// This needs to be done before we create a new subtarget since any
|
|
|
|
// creation will depend on the TM and the code generation flags on the
|
|
|
|
// function that reside in TargetOptions.
|
|
|
|
resetTargetOptions(F);
|
2015-08-12 02:11:17 +08:00
|
|
|
I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
|
2015-06-30 07:51:55 +08:00
|
|
|
}
|
|
|
|
return I.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// WebAssembly Code Generator Pass Configuration Options.
|
|
|
|
class WebAssemblyPassConfig final : public TargetPassConfig {
|
|
|
|
public:
|
|
|
|
WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
|
|
|
|
: TargetPassConfig(TM, PM) {}
|
|
|
|
|
|
|
|
WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
|
|
|
|
return getTM<WebAssemblyTargetMachine>();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *createTargetRegisterAllocator(bool) override;
|
|
|
|
|
|
|
|
void addIRPasses() override;
|
|
|
|
bool addInstSelector() override;
|
|
|
|
void addPostRegAlloc() override;
|
2016-03-29 01:05:30 +08:00
|
|
|
bool addGCPasses() override { return false; }
|
2015-06-30 07:51:55 +08:00
|
|
|
void addPreEmitPass() override;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
|
2015-09-17 07:59:57 +08:00
|
|
|
return TargetIRAnalysis([this](const Function &F) {
|
2015-06-30 07:51:55 +08:00
|
|
|
return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetPassConfig *
|
|
|
|
WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
|
|
|
|
return new WebAssemblyPassConfig(this, PM);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
|
|
|
|
return nullptr; // No reg alloc
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The following functions are called from lib/CodeGen/Passes.cpp to modify
|
|
|
|
// the CodeGen pass sequence.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void WebAssemblyPassConfig::addIRPasses() {
|
2015-07-02 07:41:25 +08:00
|
|
|
if (TM->Options.ThreadModel == ThreadModel::Single)
|
2015-11-26 02:13:18 +08:00
|
|
|
// In "single" mode, atomics get lowered to non-atomics.
|
2015-07-02 07:41:25 +08:00
|
|
|
addPass(createLowerAtomicPass());
|
|
|
|
else
|
|
|
|
// Expand some atomic operations. WebAssemblyTargetLowering has hooks which
|
|
|
|
// control specifically what gets lowered.
|
|
|
|
addPass(createAtomicExpandPass(TM));
|
2015-06-30 07:51:55 +08:00
|
|
|
|
2015-11-26 00:55:01 +08:00
|
|
|
// Optimize "returned" function attributes.
|
2016-01-19 22:55:02 +08:00
|
|
|
if (getOptLevel() != CodeGenOpt::None)
|
|
|
|
addPass(createWebAssemblyOptimizeReturned());
|
2015-11-26 00:55:01 +08:00
|
|
|
|
2016-08-02 05:34:04 +08:00
|
|
|
// Handle exceptions.
|
|
|
|
if (EnableEmExceptionHandling)
|
|
|
|
addPass(createWebAssemblyLowerEmscriptenExceptions());
|
|
|
|
|
2015-06-30 07:51:55 +08:00
|
|
|
TargetPassConfig::addIRPasses();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyPassConfig::addInstSelector() {
|
2015-12-06 03:24:17 +08:00
|
|
|
(void)TargetPassConfig::addInstSelector();
|
2015-06-30 07:51:55 +08:00
|
|
|
addPass(
|
|
|
|
createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
|
2015-12-10 00:23:59 +08:00
|
|
|
// Run the argument-move pass immediately after the ScheduleDAG scheduler
|
|
|
|
// so that we can fix up the ARGUMENT instructions before anything else
|
|
|
|
// sees them in the wrong place.
|
|
|
|
addPass(createWebAssemblyArgumentMove());
|
2016-01-26 11:39:31 +08:00
|
|
|
// Set the p2align operands. This information is present during ISel, however
|
|
|
|
// it's inconvenient to collect. Collect it now, and update the immediate
|
|
|
|
// operands.
|
|
|
|
addPass(createWebAssemblySetP2AlignOperands());
|
2015-06-30 07:51:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-01 01:53:38 +08:00
|
|
|
void WebAssemblyPassConfig::addPostRegAlloc() {
|
2015-11-26 02:13:18 +08:00
|
|
|
// TODO: The following CodeGen passes don't currently support code containing
|
|
|
|
// virtual registers. Consider removing their restrictions and re-enabling
|
|
|
|
// them.
|
2016-03-29 01:05:30 +08:00
|
|
|
|
|
|
|
// Has no asserts of its own, but was not written to handle virtual regs.
|
|
|
|
disablePass(&ShrinkWrapID);
|
2016-03-29 06:52:20 +08:00
|
|
|
|
|
|
|
// These functions all require the AllVRegsAllocated property.
|
2015-08-01 01:53:38 +08:00
|
|
|
disablePass(&MachineCopyPropagationID);
|
2016-03-29 06:52:20 +08:00
|
|
|
disablePass(&PostRASchedulerID);
|
|
|
|
disablePass(&FuncletLayoutID);
|
|
|
|
disablePass(&StackMapLivenessID);
|
|
|
|
disablePass(&LiveDebugValuesID);
|
2016-04-19 14:24:58 +08:00
|
|
|
disablePass(&PatchableFunctionID);
|
2015-09-17 00:51:30 +08:00
|
|
|
|
2015-12-06 03:24:17 +08:00
|
|
|
TargetPassConfig::addPostRegAlloc();
|
2015-08-01 01:53:38 +08:00
|
|
|
}
|
2015-06-30 07:51:55 +08:00
|
|
|
|
2015-09-17 00:51:30 +08:00
|
|
|
void WebAssemblyPassConfig::addPreEmitPass() {
|
2015-12-06 03:24:17 +08:00
|
|
|
TargetPassConfig::addPreEmitPass();
|
2015-12-17 09:39:00 +08:00
|
|
|
|
2016-05-10 12:24:02 +08:00
|
|
|
// Now that we have a prologue and epilogue and all frame indices are
|
|
|
|
// rewritten, eliminate SP and FP. This allows them to be stackified,
|
|
|
|
// colored, and numbered with the rest of the registers.
|
|
|
|
addPass(createWebAssemblyReplacePhysRegs());
|
|
|
|
|
|
|
|
if (getOptLevel() != CodeGenOpt::None) {
|
|
|
|
// LiveIntervals isn't commonly run this late. Re-establish preconditions.
|
|
|
|
addPass(createWebAssemblyPrepareForLiveIntervals());
|
|
|
|
|
|
|
|
// Depend on LiveIntervals and perform some optimizations on it.
|
|
|
|
addPass(createWebAssemblyOptimizeLiveIntervals());
|
|
|
|
|
|
|
|
// Prepare store instructions for register stackifying.
|
|
|
|
addPass(createWebAssemblyStoreResults());
|
|
|
|
|
|
|
|
// Mark registers as representing wasm's expression stack. This is a key
|
|
|
|
// code-compression technique in WebAssembly. We run this pass (and
|
|
|
|
// StoreResults above) very late, so that it sees as much code as possible,
|
|
|
|
// including code emitted by PEI and expanded by late tail duplication.
|
|
|
|
addPass(createWebAssemblyRegStackify());
|
|
|
|
|
|
|
|
// Run the register coloring pass to reduce the total number of registers.
|
|
|
|
// This runs after stackification so that it doesn't consider registers
|
|
|
|
// that become stackified.
|
|
|
|
addPass(createWebAssemblyRegColoring());
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:01:14 +08:00
|
|
|
// Eliminate multiple-entry loops.
|
|
|
|
addPass(createWebAssemblyFixIrreducibleControlFlow());
|
|
|
|
|
2015-11-26 05:32:06 +08:00
|
|
|
// Put the CFG in structured form; insert BLOCK and LOOP markers.
|
2015-09-17 00:51:30 +08:00
|
|
|
addPass(createWebAssemblyCFGStackify());
|
2015-11-26 05:32:06 +08:00
|
|
|
|
2015-12-05 11:03:35 +08:00
|
|
|
// Lower br_unless into br_if.
|
|
|
|
addPass(createWebAssemblyLowerBrUnless());
|
|
|
|
|
2015-11-26 05:32:06 +08:00
|
|
|
// Perform the very last peephole optimizations on the code.
|
2016-01-19 22:55:02 +08:00
|
|
|
if (getOptLevel() != CodeGenOpt::None)
|
|
|
|
addPass(createWebAssemblyPeephole());
|
2016-05-21 08:21:56 +08:00
|
|
|
|
|
|
|
// Create a mapping from LLVM CodeGen virtual registers to wasm registers.
|
|
|
|
addPass(createWebAssemblyRegNumbering());
|
2015-09-17 00:51:30 +08:00
|
|
|
}
|