35#define DEBUG_TYPE "wasm-explicit-locals"
40 return "WebAssembly Explicit Locals";
56char WebAssemblyExplicitLocalsLegacy::ID = 0;
58 "Convert registers to WebAssembly locals",
false,
false)
61 return new WebAssemblyExplicitLocalsLegacy();
69 dbgs() <<
"Allocating local " <<
Local <<
"for VReg "
81 auto P = Reg2Local.
insert(std::make_pair(
Reg, CurLocal));
86 return P.first->second;
91 if (RC == &WebAssembly::I32RegClass)
92 return WebAssembly::DROP_I32;
93 if (RC == &WebAssembly::I64RegClass)
94 return WebAssembly::DROP_I64;
95 if (RC == &WebAssembly::F32RegClass)
96 return WebAssembly::DROP_F32;
97 if (RC == &WebAssembly::F64RegClass)
98 return WebAssembly::DROP_F64;
99 if (RC == &WebAssembly::V128RegClass)
100 return WebAssembly::DROP_V128;
101 if (RC == &WebAssembly::FUNCREFRegClass)
102 return WebAssembly::DROP_FUNCREF;
103 if (RC == &WebAssembly::EXTERNREFRegClass)
104 return WebAssembly::DROP_EXTERNREF;
105 if (RC == &WebAssembly::EXNREFRegClass)
106 return WebAssembly::DROP_EXNREF;
112 if (RC == &WebAssembly::I32RegClass)
113 return WebAssembly::LOCAL_GET_I32;
114 if (RC == &WebAssembly::I64RegClass)
115 return WebAssembly::LOCAL_GET_I64;
116 if (RC == &WebAssembly::F32RegClass)
117 return WebAssembly::LOCAL_GET_F32;
118 if (RC == &WebAssembly::F64RegClass)
119 return WebAssembly::LOCAL_GET_F64;
120 if (RC == &WebAssembly::V128RegClass)
121 return WebAssembly::LOCAL_GET_V128;
122 if (RC == &WebAssembly::FUNCREFRegClass)
123 return WebAssembly::LOCAL_GET_FUNCREF;
124 if (RC == &WebAssembly::EXTERNREFRegClass)
125 return WebAssembly::LOCAL_GET_EXTERNREF;
126 if (RC == &WebAssembly::EXNREFRegClass)
127 return WebAssembly::LOCAL_GET_EXNREF;
133 if (RC == &WebAssembly::I32RegClass)
134 return WebAssembly::LOCAL_SET_I32;
135 if (RC == &WebAssembly::I64RegClass)
136 return WebAssembly::LOCAL_SET_I64;
137 if (RC == &WebAssembly::F32RegClass)
138 return WebAssembly::LOCAL_SET_F32;
139 if (RC == &WebAssembly::F64RegClass)
140 return WebAssembly::LOCAL_SET_F64;
141 if (RC == &WebAssembly::V128RegClass)
142 return WebAssembly::LOCAL_SET_V128;
143 if (RC == &WebAssembly::FUNCREFRegClass)
144 return WebAssembly::LOCAL_SET_FUNCREF;
145 if (RC == &WebAssembly::EXTERNREFRegClass)
146 return WebAssembly::LOCAL_SET_EXTERNREF;
147 if (RC == &WebAssembly::EXNREFRegClass)
148 return WebAssembly::LOCAL_SET_EXNREF;
154 if (RC == &WebAssembly::I32RegClass)
155 return WebAssembly::LOCAL_TEE_I32;
156 if (RC == &WebAssembly::I64RegClass)
157 return WebAssembly::LOCAL_TEE_I64;
158 if (RC == &WebAssembly::F32RegClass)
159 return WebAssembly::LOCAL_TEE_F32;
160 if (RC == &WebAssembly::F64RegClass)
161 return WebAssembly::LOCAL_TEE_F64;
162 if (RC == &WebAssembly::V128RegClass)
163 return WebAssembly::LOCAL_TEE_V128;
164 if (RC == &WebAssembly::FUNCREFRegClass)
165 return WebAssembly::LOCAL_TEE_FUNCREF;
166 if (RC == &WebAssembly::EXTERNREFRegClass)
167 return WebAssembly::LOCAL_TEE_EXTERNREF;
168 if (RC == &WebAssembly::EXNREFRegClass)
169 return WebAssembly::LOCAL_TEE_EXNREF;
175 if (RC == &WebAssembly::I32RegClass)
177 if (RC == &WebAssembly::I64RegClass)
179 if (RC == &WebAssembly::F32RegClass)
181 if (RC == &WebAssembly::F64RegClass)
183 if (RC == &WebAssembly::V128RegClass)
185 if (RC == &WebAssembly::FUNCREFRegClass)
187 if (RC == &WebAssembly::EXTERNREFRegClass)
188 return MVT::externref;
189 if (RC == &WebAssembly::EXNREFRegClass)
204 for (
auto DefReg : Def->defs()) {
229 for (
auto *
MI : ToDelete)
230 MI->eraseFromParent();
234 LLVM_DEBUG(
dbgs() <<
"********** Make Locals Explicit **********\n"
235 "********** Function: "
257 auto Local =
static_cast<unsigned>(
MI.getOperand(1).
getImm());
264 MI.eraseFromParent();
270 unsigned CurLocal =
static_cast<unsigned>(MFI.
getParams().
size());
292 if (
MI.isDebugInstr() ||
MI.isLabel())
295 if (
MI.getOpcode() == WebAssembly::IMPLICIT_DEF) {
296 MI.eraseFromParent();
339 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, DefReg);
344 MI.getOperand(2).setReg(NewReg);
350 getLocalId(Reg2Local, MFI, CurLocal,
MI.getOperand(1).getReg());
353 MI.getOperand(0).getReg())
359 MI.eraseFromParent();
365 for (
auto &Def :
MI.defs()) {
370 auto InsertPt = std::next(
MI.getIterator());
374 bool NeedsRegForDebug =
378 if (UseEmpty[OldReg.
virtRegIndex()] && !NeedsRegForDebug) {
384 Drop->getOperand(0).setIsKill();
388 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
401 Def.setIsDead(
false);
420 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
423 MI.untieRegOperand(MO.getOperandNo());
424 MO.ChangeToImmediate(LocalId);
437 if (
MI.isInlineAsm()) {
438 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
440 MI.untieRegOperand(MO.getOperandNo());
441 MO.ChangeToImmediate(LocalId);
446 unsigned LocalId =
getLocalId(Reg2Local, MFI, CurLocal, OldReg);
466 MI.getOperand(0).getReg());
467 MI.eraseFromParent();
479 if (RL == Reg2Local.
end() || RL->second < MFI.
getParams().size())
491 if (
MI.isDebugInstr() ||
MI.isLabel())
495 (!MO.isReg() || MRI.
use_empty(MO.getReg()) ||
497 "WebAssemblyExplicitLocals failed to stackify a register operand");
506bool WebAssemblyExplicitLocalsLegacy::runOnMachineFunction(
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 bool explicitLocals(MachineFunction &MF)
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
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.
DISubprogram * getSubprogram() const
Get the attached subprogram.
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
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.
void replaceWithLocal(unsigned LocalId)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isArgument(unsigned Opc)
bool isCopy(unsigned Opc)
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createWebAssemblyExplicitLocalsLegacyPass()
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...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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.
MCRegisterClass TargetRegisterClass