blob: 8e8126c90e72405433760adf451cd7735b229754 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file contains the WebAssembly implementation of the
Dan Gohman10e730a2015-06-29 23:51:55 +000011/// TargetInstrInfo class.
12///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyInstrInfo.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmanadf28172016-01-28 01:22:44 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000018#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"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-instr-info"
26
JF Bastienb9073fb2015-07-22 21:28:15 +000027#define GET_INSTRINFO_CTOR_DTOR
28#include "WebAssemblyGenInstrInfo.inc"
29
Thomas Lively972d7d52019-03-09 04:31:37 +000030// defines WebAssembly::getNamedOperandIdx
31#define GET_INSTRINFO_NAMED_OPS
32#include "WebAssemblyGenInstrInfo.inc"
33
Dan Gohman10e730a2015-06-29 23:51:55 +000034WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
Dan Gohman35bfb242015-12-04 23:22:35 +000035 : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000036 WebAssembly::ADJCALLSTACKUP,
37 WebAssembly::CATCHRET),
Dan Gohman35bfb242015-12-04 23:22:35 +000038 RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000039
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000040bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable(
Reid Kleckner0ad6c192019-10-19 01:07:48 +000041 const MachineInstr &MI, AAResults *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000042 switch (MI.getOpcode()) {
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000043 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 Gohman4f52e002015-09-09 00:52:47 +000055void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator I,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000057 const DebugLoc &DL, unsigned DestReg,
Dan Gohman4f52e002015-09-09 00:52:47 +000058 unsigned SrcReg, bool KillSrc) const {
Derek Schuff8bb5f292015-12-16 23:21:30 +000059 // 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 Gohmanb6fd39a2016-01-19 16:59:23 +000062 const TargetRegisterClass *RC =
Daniel Sanders2bea69b2019-08-01 23:27:28 +000063 Register::isVirtualRegister(DestReg)
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000064 ? MRI.getRegClass(DestReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +000065 : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(DestReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000066
Dan Gohman4fc4e422016-10-24 19:49:43 +000067 unsigned CopyOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000068 if (RC == &WebAssembly::I32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000069 CopyOpcode = WebAssembly::COPY_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000070 else if (RC == &WebAssembly::I64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000071 CopyOpcode = WebAssembly::COPY_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000072 else if (RC == &WebAssembly::F32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000073 CopyOpcode = WebAssembly::COPY_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000074 else if (RC == &WebAssembly::F64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000075 CopyOpcode = WebAssembly::COPY_F64;
Thomas Lively89717192018-11-08 02:35:28 +000076 else if (RC == &WebAssembly::V128RegClass)
77 CopyOpcode = WebAssembly::COPY_V128;
Heejin Ahn1cf69222019-07-15 23:04:00 +000078 else if (RC == &WebAssembly::EXNREFRegClass)
79 CopyOpcode = WebAssembly::COPY_EXNREF;
Dan Gohman4ba48162015-11-18 16:12:01 +000080 else
81 llvm_unreachable("Unexpected register class");
82
Dan Gohman4fc4e422016-10-24 19:49:43 +000083 BuildMI(MBB, I, DL, get(CopyOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000084 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
85}
Dan Gohman950a13c2015-09-16 16:51:30 +000086
Heejin Ahnf208f632018-09-05 01:27:38 +000087MachineInstr *WebAssemblyInstrInfo::commuteInstructionImpl(
88 MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const {
Dan Gohmanadf28172016-01-28 01:22:44 +000089 // If the operands are stackified, we can't reorder them.
90 WebAssemblyFunctionInfo &MFI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000091 *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
92 if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) ||
93 MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg()))
Dan Gohmanadf28172016-01-28 01:22:44 +000094 return nullptr;
95
96 // Otherwise use the default implementation.
97 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
98}
99
Dan Gohman950a13c2015-09-16 16:51:30 +0000100// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000101bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Dan Gohman950a13c2015-09-16 16:51:30 +0000102 MachineBasicBlock *&TBB,
103 MachineBasicBlock *&FBB,
104 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +0000105 bool /*AllowModify*/) const {
Heejin Ahn54551c12019-03-26 18:21:20 +0000106 const auto &MFI = *MBB.getParent()->getInfo<WebAssemblyFunctionInfo>();
107 // WebAssembly has control flow that doesn't have explicit branches or direct
108 // fallthrough (e.g. try/catch), which can't be modeled by analyzeBranch. It
109 // is created after CFGStackify.
110 if (MFI.isCFGStackified())
111 return true;
112
Dan Gohman950a13c2015-09-16 16:51:30 +0000113 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +0000114 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +0000115 switch (MI.getOpcode()) {
116 default:
117 // Unhandled instruction; bail out.
118 return true;
Dan Gohman231244c2015-11-13 00:46:31 +0000119 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +0000120 if (HaveCond)
121 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000122 Cond.push_back(MachineOperand::CreateImm(true));
Dan Gohman06b49582016-02-08 21:50:13 +0000123 Cond.push_back(MI.getOperand(1));
124 TBB = MI.getOperand(0).getMBB();
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000125 HaveCond = true;
126 break;
127 case WebAssembly::BR_UNLESS:
128 if (HaveCond)
129 return true;
130 Cond.push_back(MachineOperand::CreateImm(false));
Dan Gohman06b49582016-02-08 21:50:13 +0000131 Cond.push_back(MI.getOperand(1));
132 TBB = MI.getOperand(0).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000133 HaveCond = true;
134 break;
135 case WebAssembly::BR:
136 if (!HaveCond)
137 TBB = MI.getOperand(0).getMBB();
138 else
139 FBB = MI.getOperand(0).getMBB();
140 break;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000141 case WebAssembly::BR_ON_EXN:
142 if (HaveCond)
143 return true;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000144 Cond.push_back(MachineOperand::CreateImm(true));
145 Cond.push_back(MI.getOperand(2));
146 TBB = MI.getOperand(0).getMBB();
147 HaveCond = true;
148 break;
Dan Gohman950a13c2015-09-16 16:51:30 +0000149 }
150 if (MI.isBarrier())
151 break;
152 }
153
154 return false;
155}
156
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000157unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000158 int *BytesRemoved) const {
159 assert(!BytesRemoved && "code size not handled");
160
Dan Gohman950a13c2015-09-16 16:51:30 +0000161 MachineBasicBlock::instr_iterator I = MBB.instr_end();
162 unsigned Count = 0;
163
164 while (I != MBB.instr_begin()) {
165 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000166 if (I->isDebugInstr())
Dan Gohman950a13c2015-09-16 16:51:30 +0000167 continue;
168 if (!I->isTerminator())
169 break;
170 // Remove the branch.
171 I->eraseFromParent();
172 I = MBB.instr_end();
173 ++Count;
174 }
175
176 return Count;
177}
178
Heejin Ahnf208f632018-09-05 01:27:38 +0000179unsigned WebAssemblyInstrInfo::insertBranch(
180 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
181 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000182 assert(!BytesAdded && "code size not handled");
183
Dan Gohman950a13c2015-09-16 16:51:30 +0000184 if (Cond.empty()) {
185 if (!TBB)
186 return 0;
187
188 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
189 return 1;
190 }
191
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000192 assert(Cond.size() == 2 && "Expected a flag and a successor block");
193
Heejin Ahnd6f48782019-01-30 03:21:57 +0000194 MachineFunction &MF = *MBB.getParent();
195 auto &MRI = MF.getRegInfo();
196 bool IsBrOnExn = Cond[1].isReg() && MRI.getRegClass(Cond[1].getReg()) ==
Heejin Ahn9f96a582019-07-15 22:49:25 +0000197 &WebAssembly::EXNREFRegClass;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000198
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000199 if (Cond[0].getImm()) {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000200 if (IsBrOnExn) {
201 const char *CPPExnSymbol = MF.createExternalSymbolName("__cpp_exception");
202 BuildMI(&MBB, DL, get(WebAssembly::BR_ON_EXN))
203 .addMBB(TBB)
Sam Cleggef4c66c2019-04-03 00:17:29 +0000204 .addExternalSymbol(CPPExnSymbol)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000205 .add(Cond[1]);
206 } else
207 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000208 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000209 assert(!IsBrOnExn && "br_on_exn does not have a reversed condition");
Diana Picus116bbab2017-01-13 09:58:52 +0000210 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000211 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000212 if (!FBB)
213 return 1;
214
215 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
216 return 2;
217}
218
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000219bool WebAssemblyInstrInfo::reverseBranchCondition(
Dan Gohman950a13c2015-09-16 16:51:30 +0000220 SmallVectorImpl<MachineOperand> &Cond) const {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000221 assert(Cond.size() == 2 && "Expected a flag and a condition expression");
222
223 // br_on_exn's condition cannot be reversed
224 MachineFunction &MF = *Cond[1].getParent()->getParent()->getParent();
225 auto &MRI = MF.getRegInfo();
226 if (Cond[1].isReg() &&
Heejin Ahn9f96a582019-07-15 22:49:25 +0000227 MRI.getRegClass(Cond[1].getReg()) == &WebAssembly::EXNREFRegClass)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000228 return true;
229
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000230 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
231 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000232}