1 //===-- WebAssemblyRegNumbering.cpp - Register Numbering ------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief This file implements a pass which assigns WebAssembly register 12 /// numbers for CodeGen virtual registers. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "WebAssembly.h" 17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 18 #include "WebAssemblyMachineFunctionInfo.h" 19 #include "WebAssemblySubtarget.h" 20 #include "llvm/ADT/SCCIterator.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineFrameInfo.h" 23 #include "llvm/CodeGen/MachineInstrBuilder.h" 24 #include "llvm/CodeGen/MachineLoopInfo.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/Passes.h" 27 #include "llvm/Support/Debug.h" 28 #include "llvm/Support/raw_ostream.h" 29 using namespace llvm; 30 31 #define DEBUG_TYPE "wasm-reg-numbering" 32 33 namespace { 34 class WebAssemblyRegNumbering final : public MachineFunctionPass { getPassName() const35 const char *getPassName() const override { 36 return "WebAssembly Register Numbering"; 37 } 38 getAnalysisUsage(AnalysisUsage & AU) const39 void getAnalysisUsage(AnalysisUsage &AU) const override { 40 AU.setPreservesCFG(); 41 MachineFunctionPass::getAnalysisUsage(AU); 42 } 43 44 bool runOnMachineFunction(MachineFunction &MF) override; 45 46 public: 47 static char ID; // Pass identification, replacement for typeid WebAssemblyRegNumbering()48 WebAssemblyRegNumbering() : MachineFunctionPass(ID) {} 49 }; 50 } // end anonymous namespace 51 52 char WebAssemblyRegNumbering::ID = 0; createWebAssemblyRegNumbering()53FunctionPass *llvm::createWebAssemblyRegNumbering() { 54 return new WebAssemblyRegNumbering(); 55 } 56 runOnMachineFunction(MachineFunction & MF)57bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) { 58 DEBUG(dbgs() << "********** Register Numbering **********\n" 59 "********** Function: " 60 << MF.getName() << '\n'); 61 62 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); 63 MachineRegisterInfo &MRI = MF.getRegInfo(); 64 const MachineFrameInfo &FrameInfo = *MF.getFrameInfo(); 65 66 MFI.initWARegs(); 67 68 // WebAssembly argument registers are in the same index space as local 69 // variables. Assign the numbers for them first. 70 MachineBasicBlock &EntryMBB = MF.front(); 71 for (MachineInstr &MI : EntryMBB) { 72 switch (MI.getOpcode()) { 73 case WebAssembly::ARGUMENT_I32: 74 case WebAssembly::ARGUMENT_I64: 75 case WebAssembly::ARGUMENT_F32: 76 case WebAssembly::ARGUMENT_F64: 77 MFI.setWAReg(MI.getOperand(0).getReg(), MI.getOperand(1).getImm()); 78 break; 79 default: 80 break; 81 } 82 } 83 84 // Then assign regular WebAssembly registers for all remaining used 85 // virtual registers. 86 unsigned NumArgRegs = MFI.getParams().size(); 87 unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs(); 88 unsigned NumStackRegs = 0; 89 unsigned CurReg = 0; 90 for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) { 91 unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx); 92 // Handle stackified registers. 93 if (MFI.isVRegStackified(VReg)) { 94 MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++); 95 continue; 96 } 97 // Skip unused registers. 98 if (MRI.use_empty(VReg)) 99 continue; 100 if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) 101 MFI.setWAReg(VReg, NumArgRegs + CurReg++); 102 } 103 // Allocate locals for used physical registers 104 if (FrameInfo.getStackSize() > 0) 105 MFI.addPReg(WebAssembly::SP32, CurReg++); 106 107 return true; 108 } 109