Skip to content

Commit

Permalink
[WebAssembly] Move register stackification and coloring to a late phase.
Browse files Browse the repository at this point in the history
Move the register stackification and coloring passes to run very late, after
PEI, tail duplication, and most other passes. This means that all code emitted
and expanded by those passes is now exposed to these passes. This also
eliminates the need for prologue/epilogue code to be manually stackified,
which significantly simplifies the code.

This does require running LiveIntervals a second time. It's useful to think
of these late passes not as late optimization passes, but as a domain-specific
compression algorithm based on knowledge of liveness information. It's used to
compress the code after all conventional optimizations are complete, which is
why it uses LiveIntervals at a phase when actual optimization passes don't
typically need it.

Differential Revision: http://reviews.llvm.org/D20075

llvm-svn: 269012
  • Loading branch information
Dan Gohman committed May 10, 2016
1 parent 31d19d4 commit 0cfb5f8
Show file tree
Hide file tree
Showing 22 changed files with 751 additions and 401 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/BranchFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
if (!MO.isReg() || !MO.isDef() || MO.isDead())
continue;
unsigned Reg = MO.getReg();
if (!Reg)
if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg))
continue;
LocalDefs.push_back(Reg);
addRegAndItsAliases(Reg, TRI, LocalDefsSet);
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/WebAssembly/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ add_llvm_target(WebAssemblyCodeGen
WebAssemblyLowerBrUnless.cpp
WebAssemblyMachineFunctionInfo.cpp
WebAssemblyMCInstLower.cpp
WebAssemblyOptimizeLiveIntervals.cpp
WebAssemblyOptimizeReturned.cpp
WebAssemblyPeephole.cpp
WebAssemblyPEI.cpp
WebAssemblyPrepareForLiveIntervals.cpp
WebAssemblyRegisterInfo.cpp
WebAssemblyRegColoring.cpp
WebAssemblyRegNumbering.cpp
WebAssemblyRegStackify.cpp
WebAssemblyReplacePhysRegs.cpp
WebAssemblySelectionDAGInfo.cpp
WebAssemblySetP2AlignOperands.cpp
WebAssemblyStoreResults.cpp
Expand Down
10 changes: 9 additions & 1 deletion llvm/lib/Target/WebAssembly/WebAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ namespace llvm {
class WebAssemblyTargetMachine;
class FunctionPass;

// LLVM IR passes.
FunctionPass *createWebAssemblyOptimizeReturned();

// ISel and immediate followup passes.
FunctionPass *createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
CodeGenOpt::Level OptLevel);
FunctionPass *createWebAssemblyArgumentMove();
FunctionPass *createWebAssemblySetP2AlignOperands();

// Regalloc-time passes.
FunctionPass *createWebAssemblyPEI();

// Late passes.
FunctionPass *createWebAssemblyReplacePhysRegs();
FunctionPass *createWebAssemblyPrepareForLiveIntervals();
FunctionPass *createWebAssemblyOptimizeLiveIntervals();
FunctionPass *createWebAssemblyStoreResults();
FunctionPass *createWebAssemblyRegStackify();
FunctionPass *createWebAssemblyRegColoring();
FunctionPass *createWebAssemblyPEI();
FunctionPass *createWebAssemblyFixIrreducibleControlFlow();
FunctionPass *createWebAssemblyCFGStackify();
FunctionPass *createWebAssemblyLowerBrUnless();
Expand Down
12 changes: 1 addition & 11 deletions llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ class WebAssemblyAsmPrinter final : public AsmPrinter {
//===----------------------------------------------------------------------===//

MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
const TargetRegisterClass *TRC =
TargetRegisterInfo::isVirtualRegister(RegNo)
? MRI->getRegClass(RegNo)
: MRI->getTargetRegisterInfo()->getMinimalPhysRegClass(RegNo);
const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64})
if (TRC->hasType(T))
return T;
Expand Down Expand Up @@ -183,13 +180,6 @@ void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
LocalTypes.push_back(getRegType(VReg));
AnyWARegs = true;
}
auto &PhysRegs = MFI->getPhysRegs();
for (unsigned PReg = 0; PReg < PhysRegs.size(); ++PReg) {
if (PhysRegs[PReg] == -1U)
continue;
LocalTypes.push_back(getRegType(PReg));
AnyWARegs = true;
}
if (AnyWARegs)
getTargetStreamer()->emitLocal(LocalTypes);

Expand Down
43 changes: 17 additions & 26 deletions llvm/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,24 @@ static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF,
MachineBasicBlock::iterator &InsertStore,
DebugLoc DL) {
auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
unsigned SPAddr =
MF.getRegInfo().createVirtualRegister(&WebAssembly::I32RegClass);
MachineRegisterInfo &MRI = MF.getRegInfo();
const TargetRegisterClass *PtrRC =
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
unsigned SPAddr = MRI.createVirtualRegister(PtrRC);
unsigned Discard = MRI.createVirtualRegister(PtrRC);
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();

BuildMI(MBB, InsertAddr, DL, TII->get(WebAssembly::CONST_I32), SPAddr)
.addExternalSymbol(SPSymbol);
auto *MMO = new MachineMemOperand(MachinePointerInfo(),
MachineMemOperand::MOStore, 4, 4);
BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::STORE_I32),
SrcReg)
Discard)
.addImm(0)
.addReg(SPAddr)
.addImm(2) // p2align
.addReg(SrcReg)
.addMemOperand(MMO);
MF.getInfo<WebAssemblyFunctionInfo>()->stackifyVReg(SPAddr);
}

MachineBasicBlock::iterator
Expand All @@ -122,7 +124,6 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
auto *MFI = MF.getFrameInfo();
assert(MFI->getCalleeSavedInfo().empty() &&
"WebAssembly should not have callee-saved registers");
auto *WFI = MF.getInfo<WebAssemblyFunctionInfo>();

if (!needsSP(MF, *MFI)) return;
uint64_t StackSize = MFI->getStackSize();
Expand All @@ -133,8 +134,10 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
auto InsertPt = MBB.begin();
DebugLoc DL;

unsigned SPAddr = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
unsigned SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
const TargetRegisterClass *PtrRC =
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
unsigned SPAddr = MRI.createVirtualRegister(PtrRC);
unsigned SPReg = MRI.createVirtualRegister(PtrRC);
auto *SPSymbol = MF.createExternalSymbolName("__stack_pointer");
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), SPAddr)
.addExternalSymbol(SPSymbol);
Expand All @@ -151,25 +154,22 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF,
.addReg(SPAddr) // addr
.addImm(2) // p2align
.addMemOperand(LoadMMO);
WFI->stackifyVReg(SPAddr);

if (StackSize) {
// Subtract the frame size
unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
.addImm(StackSize);
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::SUB_I32),
WebAssembly::SP32)
.addReg(SPReg)
.addReg(OffsetReg);
WFI->stackifyVReg(OffsetReg);
WFI->stackifyVReg(SPReg);
}
if (hasFP(MF)) {
// Unlike most conventional targets (where FP points to the saved FP),
// FP points to the bottom of the fixed-size locals, so we can use positive
// offsets in load/store instructions.
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY_LOCAL_I32),
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::COPY),
WebAssembly::FP32)
.addReg(WebAssembly::SP32);
}
Expand All @@ -183,40 +183,31 @@ void WebAssemblyFrameLowering::emitEpilogue(MachineFunction &MF,
auto *MFI = MF.getFrameInfo();
uint64_t StackSize = MFI->getStackSize();
if (!needsSP(MF, *MFI) || !needsSPWriteback(MF, *MFI)) return;
auto *WFI = MF.getInfo<WebAssemblyFunctionInfo>();
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
auto &MRI = MF.getRegInfo();
auto InsertPt = MBB.getFirstTerminator();
DebugLoc DL;

if (InsertPt != MBB.end()) {
if (InsertPt != MBB.end())
DL = InsertPt->getDebugLoc();

// If code has been stackified with the return, disconnect it so that we
// don't break the tree when we insert code just before the return.
if (InsertPt->isReturn() && InsertPt->getNumExplicitOperands() != 0) {
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
MFI.unstackifyVReg(InsertPt->getOperand(0).getReg());
}
}

// Restore the stack pointer. If we had fixed-size locals, add the offset
// subtracted in the prolog.
unsigned SPReg = 0;
MachineBasicBlock::iterator InsertAddr = InsertPt;
if (StackSize) {
unsigned OffsetReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
const TargetRegisterClass *PtrRC =
MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
unsigned OffsetReg = MRI.createVirtualRegister(PtrRC);
InsertAddr =
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), OffsetReg)
.addImm(StackSize);
// In the epilog we don't need to write the result back to the SP32 physreg
// because it won't be used again. We can use a stackified register instead.
SPReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
SPReg = MRI.createVirtualRegister(PtrRC);
BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::ADD_I32), SPReg)
.addReg(hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32)
.addReg(OffsetReg);
WFI->stackifyVReg(OffsetReg);
WFI->stackifyVReg(SPReg);
} else {
SPReg = hasFP(MF) ? WebAssembly::FP32 : WebAssembly::SP32;
}
Expand Down
26 changes: 3 additions & 23 deletions llvm/lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
/// - defined and used in LIFO order with other stack registers
BitVector VRegStackified;

// One entry for each possible target reg. we expect it to be small.
std::vector<unsigned> PhysRegs;

// A virtual register holding the pointer to the vararg buffer for vararg
// functions. It is created and set in TLI::LowerFormalArguments and read by
// TLI::LowerVASTART
unsigned VarargVreg = -1U;

public:
explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {
PhysRegs.resize(WebAssembly::NUM_TARGET_REGS, -1U);
}
explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
~WebAssemblyFunctionInfo() override;

void addParam(MVT VT) { Params.push_back(VT); }
Expand All @@ -69,11 +64,6 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
VRegStackified.resize(TargetRegisterInfo::virtReg2Index(VReg) + 1);
VRegStackified.set(TargetRegisterInfo::virtReg2Index(VReg));
}
void unstackifyVReg(unsigned VReg) {
if (TargetRegisterInfo::virtReg2Index(VReg) >= VRegStackified.size())
return;
VRegStackified.reset(TargetRegisterInfo::virtReg2Index(VReg));
}
bool isVRegStackified(unsigned VReg) const {
if (TargetRegisterInfo::virtReg2Index(VReg) >= VRegStackified.size())
return false;
Expand All @@ -87,25 +77,15 @@ class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
WARegs[TargetRegisterInfo::virtReg2Index(VReg)] = WAReg;
}
unsigned getWAReg(unsigned Reg) const {
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
assert(TargetRegisterInfo::virtReg2Index(Reg) < WARegs.size());
return WARegs[TargetRegisterInfo::virtReg2Index(Reg)];
}
return PhysRegs[Reg];
assert(TargetRegisterInfo::virtReg2Index(Reg) < WARegs.size());
return WARegs[TargetRegisterInfo::virtReg2Index(Reg)];
}
// If new virtual registers are created after initWARegs has been called,
// this function can be used to add WebAssembly register mappings for them.
void addWAReg(unsigned VReg, unsigned WAReg) {
assert(VReg = WARegs.size());
WARegs.push_back(WAReg);
}

void addPReg(unsigned PReg, unsigned WAReg) {
assert(PReg < WebAssembly::NUM_TARGET_REGS);
assert(WAReg < -1U);
PhysRegs[PReg] = WAReg;
}
const std::vector<unsigned> &getPhysRegs() const { return PhysRegs; }
};

} // end namespace llvm
Expand Down
105 changes: 105 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyOptimizeLiveIntervals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Optimize LiveIntervals for use in a post-RA context.
//
/// LiveIntervals normally runs before register allocation when the code is
/// only recently lowered out of SSA form, so it's uncommon for registers to
/// have multiple defs, and then they do, the defs are usually closely related.
/// Later, after coalescing, tail duplication, and other optimizations, it's
/// more common to see registers with multiple unrelated defs. This pass
/// updates LiveIntervalAnalysis to distribute the value numbers across separate
/// LiveIntervals.
///
//===----------------------------------------------------------------------===//

#include "WebAssembly.h"
#include "WebAssemblySubtarget.h"
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
#include "llvm/CodeGen/MachineBlockFrequencyInfo.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-optimize-live-intervals"

namespace {
class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
const char *getPassName() const override {
return "WebAssembly Optimize Live Intervals";
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<LiveIntervals>();
AU.addPreserved<MachineBlockFrequencyInfo>();
AU.addPreserved<SlotIndexes>();
AU.addPreserved<LiveIntervals>();
AU.addPreservedID(LiveVariablesID);
AU.addPreservedID(MachineDominatorsID);
MachineFunctionPass::getAnalysisUsage(AU);
}

bool runOnMachineFunction(MachineFunction &MF) override;

public:
static char ID; // Pass identification, replacement for typeid
WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
};
} // end anonymous namespace

char WebAssemblyOptimizeLiveIntervals::ID = 0;
FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
return new WebAssemblyOptimizeLiveIntervals();
}

bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(MachineFunction &MF) {
DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
"********** Function: "
<< MF.getName() << '\n');

MachineRegisterInfo &MRI = MF.getRegInfo();
LiveIntervals &LIS = getAnalysis<LiveIntervals>();

// We don't preserve SSA form.
MRI.leaveSSA();

assert(MRI.tracksLiveness() &&
"OptimizeLiveIntervals expects liveness");

// Split multiple-VN LiveIntervals into multiple LiveIntervals.
SmallVector<LiveInterval*, 4> SplitLIs;
for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
if (MRI.reg_nodbg_empty(Reg))
continue;

LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
SplitLIs.clear();
}

// In PrepareForLiveIntervals, we conservatively inserted IMPLICIT_DEF
// instructions to satisfy LiveIntervals' requirement that all uses be
// dominated by defs. Now that LiveIntervals has computed which of these
// defs are actually needed and which are dead, remove the dead ones.
for (auto MII = MF.begin()->begin(), MIE = MF.begin()->end(); MII != MIE; ) {
MachineInstr *MI = &*MII++;
if (MI->isImplicitDef() && MI->getOperand(0).isDead()) {
LiveInterval &LI = LIS.getInterval(MI->getOperand(0).getReg());
LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*MI).getRegSlot());
LIS.RemoveMachineInstrFromMaps(*MI);
MI->eraseFromParent();
}
}

return false;
}
1 change: 1 addition & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyPEI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/CodeGen/StackProtector.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/WinEHFuncInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/InlineAsm.h"
Expand Down
Loading

0 comments on commit 0cfb5f8

Please sign in to comment.