32#define DEBUG_TYPE "wasm-explicit-locals"
37 return "WebAssembly Explicit Locals";
54char WebAssemblyExplicitLocals::ID = 0;
56 "Convert registers to WebAssembly locals",
false,
false)
59 return new WebAssemblyExplicitLocals();
67 dbgs() <<
"Allocating local " <<
Local <<
"for VReg "
79 auto P = Reg2Local.
insert(std::make_pair(
Reg, CurLocal));
84 return P.first->second;
89 if (RC == &WebAssembly::I32RegClass)
90 return WebAssembly::DROP_I32;
91 if (RC == &WebAssembly::I64RegClass)
92 return WebAssembly::DROP_I64;
93 if (RC == &WebAssembly::F32RegClass)
94 return WebAssembly::DROP_F32;
95 if (RC == &WebAssembly::F64RegClass)
96 return WebAssembly::DROP_F64;
97 if (RC == &WebAssembly::V128RegClass)
98 return WebAssembly::DROP_V128;
99 if (RC == &WebAssembly::FUNCREFRegClass)
100 return WebAssembly::DROP_FUNCREF;
101 if (RC == &WebAssembly::EXTERNREFRegClass)
102 return WebAssembly::DROP_EXTERNREF;
103 if (RC == &WebAssembly::EXNREFRegClass)
104 return WebAssembly::DROP_EXNREF;
110 if (RC == &WebAssembly::I32RegClass)
111 return WebAssembly::LOCAL_GET_I32;
112 if (RC == &WebAssembly::I64RegClass)
113 return WebAssembly::LOCAL_GET_I64;
114 if (RC == &WebAssembly::F32RegClass)
115 return WebAssembly::LOCAL_GET_F32;
116 if (RC == &WebAssembly::F64RegClass)
117 return WebAssembly::LOCAL_GET_F64;
118 if (RC == &WebAssembly::V128RegClass)
119 return WebAssembly::LOCAL_GET_V128;
120 if (RC == &WebAssembly::FUNCREFRegClass)
121 return WebAssembly::LOCAL_GET_FUNCREF;
122 if (RC == &WebAssembly::EXTERNREFRegClass)
123 return WebAssembly::LOCAL_GET_EXTERNREF;
124 if (RC == &WebAssembly::EXNREFRegClass)
125 return WebAssembly::LOCAL_GET_EXNREF;
131 if (RC == &WebAssembly::I32RegClass)
132 return WebAssembly::LOCAL_SET_I32;
133 if (RC == &WebAssembly::I64RegClass)
134 return WebAssembly::LOCAL_SET_I64;
135 if (RC == &WebAssembly::F32RegClass)
136 return WebAssembly::LOCAL_SET_F32;
137 if (RC == &WebAssembly::F64RegClass)
138 return WebAssembly::LOCAL_SET_F64;
139 if (RC == &WebAssembly::V128RegClass)
140 return WebAssembly::LOCAL_SET_V128;
141 if (RC == &WebAssembly::FUNCREFRegClass)
142 return WebAssembly::LOCAL_SET_FUNCREF;
143 if (RC == &WebAssembly::EXTERNREFRegClass)
144 return WebAssembly::LOCAL_SET_EXTERNREF;
145 if (RC == &WebAssembly::EXNREFRegClass)
146 return WebAssembly::LOCAL_SET_EXNREF;
152 if (RC == &WebAssembly::I32RegClass)
153 return WebAssembly::LOCAL_TEE_I32;
154 if (RC == &WebAssembly::I64RegClass)
155 return WebAssembly::LOCAL_TEE_I64;
156 if (RC == &WebAssembly::F32RegClass)
157 return WebAssembly::LOCAL_TEE_F32;
158 if (RC == &WebAssembly::F64RegClass)
159 return WebAssembly::LOCAL_TEE_F64;
160 if (RC == &WebAssembly::V128RegClass)
161 return WebAssembly::LOCAL_TEE_V128;
162 if (RC == &WebAssembly::FUNCREFRegClass)
163 return WebAssembly::LOCAL_TEE_FUNCREF;
164 if (RC == &WebAssembly::EXTERNREFRegClass)
165 return WebAssembly::LOCAL_TEE_EXTERNREF;
166 if (RC == &WebAssembly::EXNREFRegClass)
167 return WebAssembly::LOCAL_TEE_EXNREF;
173 if (RC == &WebAssembly::I32RegClass)
175 if (RC == &WebAssembly::I64RegClass)
177 if (RC == &WebAssembly::F32RegClass)
179 if (RC == &WebAssembly::F64RegClass)
181 if (RC == &WebAssembly::V128RegClass)
183 if (RC == &WebAssembly::FUNCREFRegClass)
185 if (RC == &WebAssembly::EXTERNREFRegClass)
186 return MVT::externref;
187 if (RC == &WebAssembly::EXNREFRegClass)
202 for (
auto DefReg : Def->defs()) {
227 for (
auto *
MI : ToDelete)
228 MI->eraseFromParent();
231bool WebAssemblyExplicitLocals::runOnMachineFunction(
MachineFunction &MF) {
232 LLVM_DEBUG(
dbgs() <<
"********** Make Locals Explicit **********\n"
233 "********** Function: "
238 WebAssemblyFunctionInfo &MFI = *MF.
getInfo<WebAssemblyFunctionInfo>();
239 const auto *
TII = MF.
getSubtarget<WebAssemblySubtarget>().getInstrInfo();
244 DenseMap<unsigned, unsigned> Reg2Local;
250 MachineInstr &
MI = *
I++;
251 if (!WebAssembly::isArgument(
MI.getOpcode()))
255 auto Local =
static_cast<unsigned>(
MI.getOperand(1).
getImm());
260 WebAssemblyDebugValueManager(&
MI).replaceWithLocal(
Local);
262 MI.eraseFromParent();
268 unsigned CurLocal =
static_cast<unsigned>(MFI.
getParams().
size());
286 for (MachineBasicBlock &
MBB : MF) {
288 assert(!WebAssembly::isArgument(
MI.getOpcode()));
290 if (
MI.isDebugInstr() ||
MI.isLabel())
293 if (
MI.getOpcode() == WebAssembly::IMPLICIT_DEF) {
294 MI.eraseFromParent();
329 if (WebAssembly::isTee(
MI.getOpcode())) {
337 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, DefReg);
342 MI.getOperand(2).setReg(NewReg);
348 getLocalId(Reg2Local, MFI, CurLocal,
MI.getOperand(1).getReg());
351 MI.getOperand(0).getReg())
355 WebAssemblyDebugValueManager(&
MI).replaceWithLocal(LocalId);
357 MI.eraseFromParent();
363 for (
auto &Def :
MI.defs()) {
368 auto InsertPt = std::next(
MI.getIterator());
372 bool NeedsRegForDebug =
374 MF.getFunction().getSubprogram() &&
375 MF.getSubtarget<WebAssemblySubtarget>().hasLibcallThreadContext();
376 if (UseEmpty[OldReg.
virtRegIndex()] && !NeedsRegForDebug) {
382 Drop->getOperand(0).setIsKill();
386 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
389 WebAssemblyDebugValueManager(&
MI).replaceWithLocal(LocalId);
399 Def.setIsDead(
false);
406 MachineInstr *InsertPt = &
MI;
407 for (MachineOperand &MO :
reverse(
MI.explicit_uses())) {
418 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
421 MI.untieRegOperand(MO.getOperandNo());
422 MO.ChangeToImmediate(LocalId);
435 if (
MI.isInlineAsm()) {
436 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
438 MI.untieRegOperand(MO.getOperandNo());
439 MO.ChangeToImmediate(LocalId);
444 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
462 if (WebAssembly::isCopy(
MI.getOpcode())) {
464 MI.getOperand(0).getReg());
465 MI.eraseFromParent();
477 if (RL == Reg2Local.
end() || RL->second < MFI.
getParams().size())
487 for (
const MachineBasicBlock &
MBB : MF) {
488 for (
const MachineInstr &
MI :
MBB) {
489 if (
MI.isDebugInstr() ||
MI.isLabel())
491 for (
const MachineOperand &MO :
MI.explicit_operands()) {
493 (!MO.isReg() || MRI.
use_empty(MO.getReg()) ||
495 "WebAssemblyExplicitLocals failed to stackify a register operand");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file contains the declaration of the WebAssembly-specific manager for DebugValues associated wit...
static unsigned getLocalGetOpcode(const TargetRegisterClass *RC)
Get the appropriate local.get opcode for the given register class.
static unsigned getLocalId(DenseMap< unsigned, unsigned > &Reg2Local, WebAssemblyFunctionInfo &MFI, unsigned &CurLocal, unsigned Reg)
Return a local id number for the given register, assigning it a new one if it doesn't yet have one.
static MachineInstr * findStartOfTree(MachineOperand &MO, MachineRegisterInfo &MRI, const WebAssemblyFunctionInfo &MFI)
Given a MachineOperand of a stackified vreg, return the instruction at the start of the expression tr...
static MVT typeForRegClass(const TargetRegisterClass *RC)
Get the type associated with the given register class.
static unsigned getLocalTeeOpcode(const TargetRegisterClass *RC)
Get the appropriate local.tee opcode for the given register class.
static void checkFrameBase(WebAssemblyFunctionInfo &MFI, unsigned Local, unsigned Reg)
static unsigned getLocalSetOpcode(const TargetRegisterClass *RC)
Get the appropriate local.set opcode for the given register class.
static unsigned getDropOpcode(const TargetRegisterClass *RC)
Get the appropriate drop opcode for the given register class.
static void removeFakeUses(MachineFunction &MF)
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
FunctionPass class - This class is used to implement most global optimizations.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
bool use_empty(Register RegNo) const
use_empty - Return true if there are no instructions using the specified register.
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
Wrapper class representing virtual and physical registers.
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
void stackifyVReg(MachineRegisterInfo &MRI, Register VReg)
void setLocal(size_t i, MVT VT)
void setNumLocals(size_t NumLocals)
unsigned getFrameBaseVreg() const
void setFrameBaseLocal(unsigned Local)
bool isVRegStackified(Register VReg) const
const std::vector< MVT > & getLocals() const
void clearFrameBaseVreg()
bool isFrameBaseVirtual() const
void unstackifyVReg(Register VReg)
const std::vector< MVT > & getParams() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
auto reverse(ContainerTy &&C)
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createWebAssemblyExplicitLocals()
MCRegisterClass TargetRegisterClass