Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 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/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | #define DEBUG_TYPE "wasm-reg-numbering" |
| 31 | |
| 32 | namespace { |
| 33 | class WebAssemblyRegNumbering final : public MachineFunctionPass { |
| 34 | const char *getPassName() const override { |
| 35 | return "WebAssembly Register Numbering"; |
| 36 | } |
| 37 | |
| 38 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 39 | AU.setPreservesCFG(); |
| 40 | MachineFunctionPass::getAnalysisUsage(AU); |
| 41 | } |
| 42 | |
| 43 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 44 | |
| 45 | public: |
| 46 | static char ID; // Pass identification, replacement for typeid |
| 47 | WebAssemblyRegNumbering() : MachineFunctionPass(ID) {} |
| 48 | }; |
| 49 | } // end anonymous namespace |
| 50 | |
| 51 | char WebAssemblyRegNumbering::ID = 0; |
| 52 | FunctionPass *llvm::createWebAssemblyRegNumbering() { |
| 53 | return new WebAssemblyRegNumbering(); |
| 54 | } |
| 55 | |
| 56 | bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) { |
| 57 | DEBUG(dbgs() << "********** Register Numbering **********\n" |
| 58 | "********** Function: " |
| 59 | << MF.getName() << '\n'); |
| 60 | |
| 61 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 62 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 63 | |
| 64 | MFI.initWARegs(); |
| 65 | |
| 66 | // WebAssembly argument registers are in the same index space as local |
| 67 | // variables. Assign the numbers for them first. |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame^] | 68 | MachineBasicBlock &EntryMBB = MF.front(); |
| 69 | for (MachineInstr &MI : EntryMBB) { |
| 70 | switch (MI.getOpcode()) { |
| 71 | case WebAssembly::ARGUMENT_I32: |
| 72 | case WebAssembly::ARGUMENT_I64: |
| 73 | case WebAssembly::ARGUMENT_F32: |
| 74 | case WebAssembly::ARGUMENT_F64: |
| 75 | MFI.setWAReg(MI.getOperand(0).getReg(), MI.getOperand(1).getImm()); |
| 76 | break; |
| 77 | default: |
| 78 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // Then assign regular WebAssembly registers for all remaining used |
| 83 | // virtual registers. |
| 84 | unsigned NumArgRegs = MFI.getParams().size(); |
| 85 | unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs(); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame^] | 86 | unsigned NumStackRegs = 0; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 87 | unsigned CurReg = 0; |
| 88 | for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) { |
| 89 | unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame^] | 90 | // Handle stackified registers. |
| 91 | if (MFI.isVRegStackified(VReg)) { |
| 92 | MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++); |
| 93 | continue; |
| 94 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 95 | // Skip unused registers. |
| 96 | if (MRI.use_empty(VReg)) |
| 97 | continue; |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 98 | if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 99 | MFI.setWAReg(VReg, NumArgRegs + CurReg++); |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |