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" |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 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 { |
| 35 | const char *getPassName() const override { |
| 36 | return "WebAssembly Register Numbering"; |
| 37 | } |
| 38 | |
| 39 | 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 |
| 48 | WebAssemblyRegNumbering() : MachineFunctionPass(ID) {} |
| 49 | }; |
| 50 | } // end anonymous namespace |
| 51 | |
| 52 | char WebAssemblyRegNumbering::ID = 0; |
| 53 | FunctionPass *llvm::createWebAssemblyRegNumbering() { |
| 54 | return new WebAssemblyRegNumbering(); |
| 55 | } |
| 56 | |
| 57 | bool 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(); |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 64 | const MachineFrameInfo &FrameInfo = *MF.getFrameInfo(); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 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. |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 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; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 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(); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 88 | unsigned NumStackRegs = 0; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 89 | unsigned CurReg = 0; |
| 90 | for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) { |
| 91 | unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 92 | // Handle stackified registers. |
| 93 | if (MFI.isVRegStackified(VReg)) { |
| 94 | MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++); |
| 95 | continue; |
| 96 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 97 | // Skip unused registers. |
| 98 | if (MRI.use_empty(VReg)) |
| 99 | continue; |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 100 | if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 101 | MFI.setWAReg(VReg, NumArgRegs + CurReg++); |
| 102 | } |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 103 | // Allocate locals for used physical registers |
| 104 | if (FrameInfo.getStackSize() > 0) |
| 105 | MFI.addPReg(WebAssembly::SP32, CurReg++); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 106 | |
| 107 | return true; |
| 108 | } |