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 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 17 | #include "WebAssembly.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 18 | #include "WebAssemblyMachineFunctionInfo.h" |
| 19 | #include "WebAssemblySubtarget.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 20 | #include "WebAssemblyUtilities.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SCCIterator.h" |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | 8394756 | 2016-01-20 05:54:22 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 27 | #include "llvm/CodeGen/Passes.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "wasm-reg-numbering" |
| 33 | |
| 34 | namespace { |
| 35 | class WebAssemblyRegNumbering final : public MachineFunctionPass { |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 36 | StringRef getPassName() const override { |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 37 | return "WebAssembly Register Numbering"; |
| 38 | } |
| 39 | |
| 40 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 41 | AU.setPreservesCFG(); |
| 42 | MachineFunctionPass::getAnalysisUsage(AU); |
| 43 | } |
| 44 | |
| 45 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 46 | |
| 47 | public: |
| 48 | static char ID; // Pass identification, replacement for typeid |
| 49 | WebAssemblyRegNumbering() : MachineFunctionPass(ID) {} |
| 50 | }; |
| 51 | } // end anonymous namespace |
| 52 | |
| 53 | char WebAssemblyRegNumbering::ID = 0; |
| 54 | FunctionPass *llvm::createWebAssemblyRegNumbering() { |
| 55 | return new WebAssemblyRegNumbering(); |
| 56 | } |
| 57 | |
| 58 | bool WebAssemblyRegNumbering::runOnMachineFunction(MachineFunction &MF) { |
| 59 | DEBUG(dbgs() << "********** Register Numbering **********\n" |
| 60 | "********** Function: " |
| 61 | << MF.getName() << '\n'); |
| 62 | |
| 63 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 64 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 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) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 72 | if (!WebAssembly::isArgument(MI)) |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 73 | break; |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 74 | |
| 75 | int64_t Imm = MI.getOperand(1).getImm(); |
| 76 | DEBUG(dbgs() << "Arg VReg " << MI.getOperand(0).getReg() << " -> WAReg " |
| 77 | << Imm << "\n"); |
| 78 | MFI.setWAReg(MI.getOperand(0).getReg(), Imm); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Then assign regular WebAssembly registers for all remaining used |
Dan Gohman | 08d58bc | 2015-12-23 00:22:04 +0000 | [diff] [blame] | 82 | // virtual registers. TODO: Consider sorting the registers by frequency of |
| 83 | // use, to maximize usage of small immediate fields. |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 84 | unsigned NumVRegs = MF.getRegInfo().getNumVirtRegs(); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 85 | unsigned NumStackRegs = 0; |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 86 | // Start the numbering for locals after the arg regs |
| 87 | unsigned CurReg = MFI.getParams().size(); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 88 | for (unsigned VRegIdx = 0; VRegIdx < NumVRegs; ++VRegIdx) { |
| 89 | unsigned VReg = TargetRegisterInfo::index2VirtReg(VRegIdx); |
Dan Gohman | 899cb5a | 2016-01-25 16:48:44 +0000 | [diff] [blame] | 90 | // Skip unused registers. |
| 91 | if (MRI.use_empty(VReg)) |
| 92 | continue; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 93 | // Handle stackified registers. |
| 94 | if (MFI.isVRegStackified(VReg)) { |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 95 | DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " |
| 96 | << (INT32_MIN | NumStackRegs) << "\n"); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 97 | MFI.setWAReg(VReg, INT32_MIN | NumStackRegs++); |
| 98 | continue; |
| 99 | } |
Derek Schuff | 6519468 | 2016-01-23 01:20:43 +0000 | [diff] [blame] | 100 | if (MFI.getWAReg(VReg) == WebAssemblyFunctionInfo::UnusedReg) { |
| 101 | DEBUG(dbgs() << "VReg " << VReg << " -> WAReg " << CurReg << "\n"); |
| 102 | MFI.setWAReg(VReg, CurReg++); |
| 103 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 104 | } |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 105 | |
| 106 | return true; |
| 107 | } |