Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===// |
| 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 contains the WebAssembly implementation of the |
| 12 | /// TargetInstrInfo class. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "WebAssemblyInstrInfo.h" |
| 17 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 18 | #include "WebAssemblySubtarget.h" |
| 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #define DEBUG_TYPE "wasm-instr-info" |
| 26 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 27 | #define GET_INSTRINFO_CTOR_DTOR |
| 28 | #include "WebAssemblyGenInstrInfo.inc" |
| 29 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 30 | WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI) |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 31 | : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN, |
| 32 | WebAssembly::ADJCALLSTACKUP), |
| 33 | RI(STI.getTargetTriple()) {} |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 34 | |
| 35 | void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 36 | MachineBasicBlock::iterator I, |
| 37 | DebugLoc DL, unsigned DestReg, |
| 38 | unsigned SrcReg, bool KillSrc) const { |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 39 | // This method is called by post-RA expansion, which expects only pregs to |
| 40 | // exist. However we need to handle both here. |
| 41 | auto &MRI = MBB.getParent()->getRegInfo(); |
| 42 | const TargetRegisterClass *RC = TargetRegisterInfo::isVirtualRegister(DestReg) ? |
| 43 | MRI.getRegClass(DestReg) : |
| 44 | MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(SrcReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 45 | |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 46 | unsigned CopyLocalOpcode; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 47 | if (RC == &WebAssembly::I32RegClass) |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 48 | CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 49 | else if (RC == &WebAssembly::I64RegClass) |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 50 | CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 51 | else if (RC == &WebAssembly::F32RegClass) |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 52 | CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 53 | else if (RC == &WebAssembly::F64RegClass) |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 54 | CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 55 | else |
| 56 | llvm_unreachable("Unexpected register class"); |
| 57 | |
Dan Gohman | aa0a4bd | 2015-11-23 19:30:43 +0000 | [diff] [blame] | 58 | BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg) |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 59 | .addReg(SrcReg, KillSrc ? RegState::Kill : 0); |
| 60 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 61 | |
| 62 | // Branch analysis. |
| 63 | bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, |
| 64 | MachineBasicBlock *&TBB, |
| 65 | MachineBasicBlock *&FBB, |
| 66 | SmallVectorImpl<MachineOperand> &Cond, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 67 | bool /*AllowModify*/) const { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 68 | bool HaveCond = false; |
Dan Gohman | d544e0c | 2015-12-21 17:22:02 +0000 | [diff] [blame] | 69 | for (MachineInstr &MI : MBB.terminators()) { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 70 | switch (MI.getOpcode()) { |
| 71 | default: |
| 72 | // Unhandled instruction; bail out. |
| 73 | return true; |
Dan Gohman | 231244c | 2015-11-13 00:46:31 +0000 | [diff] [blame] | 74 | case WebAssembly::BR_IF: |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 75 | if (HaveCond) |
| 76 | return true; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 77 | // If we're running after CFGStackify, we can't optimize further. |
| 78 | if (!MI.getOperand(1).isMBB()) |
| 79 | return true; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 80 | Cond.push_back(MachineOperand::CreateImm(true)); |
| 81 | Cond.push_back(MI.getOperand(0)); |
| 82 | TBB = MI.getOperand(1).getMBB(); |
| 83 | HaveCond = true; |
| 84 | break; |
| 85 | case WebAssembly::BR_UNLESS: |
| 86 | if (HaveCond) |
| 87 | return true; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 88 | // If we're running after CFGStackify, we can't optimize further. |
| 89 | if (!MI.getOperand(1).isMBB()) |
| 90 | return true; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 91 | Cond.push_back(MachineOperand::CreateImm(false)); |
Derek Schuff | 4ed4778 | 2015-11-16 21:04:51 +0000 | [diff] [blame] | 92 | Cond.push_back(MI.getOperand(0)); |
| 93 | TBB = MI.getOperand(1).getMBB(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 94 | HaveCond = true; |
| 95 | break; |
| 96 | case WebAssembly::BR: |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame^] | 97 | // If we're running after CFGStackify, we can't optimize further. |
| 98 | if (!MI.getOperand(0).isMBB()) |
| 99 | return true; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 100 | if (!HaveCond) |
| 101 | TBB = MI.getOperand(0).getMBB(); |
| 102 | else |
| 103 | FBB = MI.getOperand(0).getMBB(); |
| 104 | break; |
| 105 | } |
| 106 | if (MI.isBarrier()) |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 114 | MachineBasicBlock::instr_iterator I = MBB.instr_end(); |
| 115 | unsigned Count = 0; |
| 116 | |
| 117 | while (I != MBB.instr_begin()) { |
| 118 | --I; |
| 119 | if (I->isDebugValue()) |
| 120 | continue; |
| 121 | if (!I->isTerminator()) |
| 122 | break; |
| 123 | // Remove the branch. |
| 124 | I->eraseFromParent(); |
| 125 | I = MBB.instr_end(); |
| 126 | ++Count; |
| 127 | } |
| 128 | |
| 129 | return Count; |
| 130 | } |
| 131 | |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 132 | unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB, |
| 133 | MachineBasicBlock *TBB, |
| 134 | MachineBasicBlock *FBB, |
| 135 | ArrayRef<MachineOperand> Cond, |
| 136 | DebugLoc DL) const { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 137 | if (Cond.empty()) { |
| 138 | if (!TBB) |
| 139 | return 0; |
| 140 | |
| 141 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB); |
| 142 | return 1; |
| 143 | } |
| 144 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 145 | assert(Cond.size() == 2 && "Expected a flag and a successor block"); |
| 146 | |
| 147 | if (Cond[0].getImm()) { |
| 148 | BuildMI(&MBB, DL, get(WebAssembly::BR_IF)) |
| 149 | .addOperand(Cond[1]) |
| 150 | .addMBB(TBB); |
| 151 | } else { |
| 152 | BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)) |
| 153 | .addOperand(Cond[1]) |
| 154 | .addMBB(TBB); |
| 155 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 156 | if (!FBB) |
| 157 | return 1; |
| 158 | |
| 159 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB); |
| 160 | return 2; |
| 161 | } |
| 162 | |
| 163 | bool WebAssemblyInstrInfo::ReverseBranchCondition( |
| 164 | SmallVectorImpl<MachineOperand> &Cond) const { |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 165 | assert(Cond.size() == 2 && "Expected a flag and a successor block"); |
| 166 | Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm()); |
| 167 | return false; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 168 | } |