Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file contains the WebAssembly implementation of the |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 11 | /// TargetInstrInfo class. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "WebAssemblyInstrInfo.h" |
| 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 17 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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 | |
Thomas Lively | 972d7d5 | 2019-03-09 04:31:37 +0000 | [diff] [blame] | 30 | // defines WebAssembly::getNamedOperandIdx |
| 31 | #define GET_INSTRINFO_NAMED_OPS |
| 32 | #include "WebAssemblyGenInstrInfo.inc" |
| 33 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 34 | WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI) |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 35 | : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN, |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 36 | WebAssembly::ADJCALLSTACKUP, |
| 37 | WebAssembly::CATCHRET), |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 38 | RI(STI.getTargetTriple()) {} |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 39 | |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 40 | bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable( |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 41 | const MachineInstr &MI, AliasAnalysis *AA) const { |
| 42 | switch (MI.getOpcode()) { |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 43 | case WebAssembly::CONST_I32: |
| 44 | case WebAssembly::CONST_I64: |
| 45 | case WebAssembly::CONST_F32: |
| 46 | case WebAssembly::CONST_F64: |
| 47 | // isReallyTriviallyReMaterializableGeneric misses these because of the |
| 48 | // ARGUMENTS implicit def, so we manualy override it here. |
| 49 | return true; |
| 50 | default: |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 55 | void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 56 | MachineBasicBlock::iterator I, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 57 | const DebugLoc &DL, unsigned DestReg, |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 58 | unsigned SrcReg, bool KillSrc) const { |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 59 | // This method is called by post-RA expansion, which expects only pregs to |
| 60 | // exist. However we need to handle both here. |
| 61 | auto &MRI = MBB.getParent()->getRegInfo(); |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 62 | const TargetRegisterClass *RC = |
| 63 | TargetRegisterInfo::isVirtualRegister(DestReg) |
| 64 | ? MRI.getRegClass(DestReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 65 | : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(DestReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 66 | |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 67 | unsigned CopyOpcode; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 68 | if (RC == &WebAssembly::I32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 69 | CopyOpcode = WebAssembly::COPY_I32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 70 | else if (RC == &WebAssembly::I64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 71 | CopyOpcode = WebAssembly::COPY_I64; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 72 | else if (RC == &WebAssembly::F32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 73 | CopyOpcode = WebAssembly::COPY_F32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 74 | else if (RC == &WebAssembly::F64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 75 | CopyOpcode = WebAssembly::COPY_F64; |
Thomas Lively | 8971719 | 2018-11-08 02:35:28 +0000 | [diff] [blame] | 76 | else if (RC == &WebAssembly::V128RegClass) |
| 77 | CopyOpcode = WebAssembly::COPY_V128; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 78 | else |
| 79 | llvm_unreachable("Unexpected register class"); |
| 80 | |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 81 | BuildMI(MBB, I, DL, get(CopyOpcode), DestReg) |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 82 | .addReg(SrcReg, KillSrc ? RegState::Kill : 0); |
| 83 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 84 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 85 | MachineInstr *WebAssemblyInstrInfo::commuteInstructionImpl( |
| 86 | MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 87 | // If the operands are stackified, we can't reorder them. |
| 88 | WebAssemblyFunctionInfo &MFI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 89 | *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 90 | if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) || |
| 91 | MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg())) |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 92 | return nullptr; |
| 93 | |
| 94 | // Otherwise use the default implementation. |
| 95 | return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); |
| 96 | } |
| 97 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 98 | // Branch analysis. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 99 | bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 100 | MachineBasicBlock *&TBB, |
| 101 | MachineBasicBlock *&FBB, |
| 102 | SmallVectorImpl<MachineOperand> &Cond, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 103 | bool /*AllowModify*/) const { |
Heejin Ahn | 54551c1 | 2019-03-26 18:21:20 +0000 | [diff] [blame] | 104 | const auto &MFI = *MBB.getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 105 | // WebAssembly has control flow that doesn't have explicit branches or direct |
| 106 | // fallthrough (e.g. try/catch), which can't be modeled by analyzeBranch. It |
| 107 | // is created after CFGStackify. |
| 108 | if (MFI.isCFGStackified()) |
| 109 | return true; |
| 110 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 111 | bool HaveCond = false; |
Dan Gohman | d544e0c | 2015-12-21 17:22:02 +0000 | [diff] [blame] | 112 | for (MachineInstr &MI : MBB.terminators()) { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 113 | switch (MI.getOpcode()) { |
| 114 | default: |
| 115 | // Unhandled instruction; bail out. |
| 116 | return true; |
Dan Gohman | 231244c | 2015-11-13 00:46:31 +0000 | [diff] [blame] | 117 | case WebAssembly::BR_IF: |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 118 | if (HaveCond) |
| 119 | return true; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 120 | Cond.push_back(MachineOperand::CreateImm(true)); |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 121 | Cond.push_back(MI.getOperand(1)); |
| 122 | TBB = MI.getOperand(0).getMBB(); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 123 | HaveCond = true; |
| 124 | break; |
| 125 | case WebAssembly::BR_UNLESS: |
| 126 | if (HaveCond) |
| 127 | return true; |
| 128 | Cond.push_back(MachineOperand::CreateImm(false)); |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 129 | Cond.push_back(MI.getOperand(1)); |
| 130 | TBB = MI.getOperand(0).getMBB(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 131 | HaveCond = true; |
| 132 | break; |
| 133 | case WebAssembly::BR: |
| 134 | if (!HaveCond) |
| 135 | TBB = MI.getOperand(0).getMBB(); |
| 136 | else |
| 137 | FBB = MI.getOperand(0).getMBB(); |
| 138 | break; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 139 | case WebAssembly::BR_ON_EXN: |
| 140 | if (HaveCond) |
| 141 | return true; |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 142 | Cond.push_back(MachineOperand::CreateImm(true)); |
| 143 | Cond.push_back(MI.getOperand(2)); |
| 144 | TBB = MI.getOperand(0).getMBB(); |
| 145 | HaveCond = true; |
| 146 | break; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 147 | } |
| 148 | if (MI.isBarrier()) |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 155 | unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 156 | int *BytesRemoved) const { |
| 157 | assert(!BytesRemoved && "code size not handled"); |
| 158 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 159 | MachineBasicBlock::instr_iterator I = MBB.instr_end(); |
| 160 | unsigned Count = 0; |
| 161 | |
| 162 | while (I != MBB.instr_begin()) { |
| 163 | --I; |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 164 | if (I->isDebugInstr()) |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 165 | continue; |
| 166 | if (!I->isTerminator()) |
| 167 | break; |
| 168 | // Remove the branch. |
| 169 | I->eraseFromParent(); |
| 170 | I = MBB.instr_end(); |
| 171 | ++Count; |
| 172 | } |
| 173 | |
| 174 | return Count; |
| 175 | } |
| 176 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 177 | unsigned WebAssemblyInstrInfo::insertBranch( |
| 178 | MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, |
| 179 | ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const { |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 180 | assert(!BytesAdded && "code size not handled"); |
| 181 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 182 | if (Cond.empty()) { |
| 183 | if (!TBB) |
| 184 | return 0; |
| 185 | |
| 186 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB); |
| 187 | return 1; |
| 188 | } |
| 189 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 190 | assert(Cond.size() == 2 && "Expected a flag and a successor block"); |
| 191 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 192 | MachineFunction &MF = *MBB.getParent(); |
| 193 | auto &MRI = MF.getRegInfo(); |
| 194 | bool IsBrOnExn = Cond[1].isReg() && MRI.getRegClass(Cond[1].getReg()) == |
| 195 | &WebAssembly::EXCEPT_REFRegClass; |
| 196 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 197 | if (Cond[0].getImm()) { |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 198 | if (IsBrOnExn) { |
| 199 | const char *CPPExnSymbol = MF.createExternalSymbolName("__cpp_exception"); |
| 200 | BuildMI(&MBB, DL, get(WebAssembly::BR_ON_EXN)) |
| 201 | .addMBB(TBB) |
| 202 | .addExternalSymbol(CPPExnSymbol, WebAssemblyII::MO_SYMBOL_EVENT) |
| 203 | .add(Cond[1]); |
| 204 | } else |
| 205 | BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 206 | } else { |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 207 | assert(!IsBrOnExn && "br_on_exn does not have a reversed condition"); |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 208 | BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 209 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 210 | if (!FBB) |
| 211 | return 1; |
| 212 | |
| 213 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB); |
| 214 | return 2; |
| 215 | } |
| 216 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 217 | bool WebAssemblyInstrInfo::reverseBranchCondition( |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 218 | SmallVectorImpl<MachineOperand> &Cond) const { |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 219 | assert(Cond.size() == 2 && "Expected a flag and a condition expression"); |
| 220 | |
| 221 | // br_on_exn's condition cannot be reversed |
| 222 | MachineFunction &MF = *Cond[1].getParent()->getParent()->getParent(); |
| 223 | auto &MRI = MF.getRegInfo(); |
| 224 | if (Cond[1].isReg() && |
| 225 | MRI.getRegClass(Cond[1].getReg()) == &WebAssembly::EXCEPT_REFRegClass) |
| 226 | return true; |
| 227 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 228 | Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm()); |
| 229 | return false; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 230 | } |