blob: 3f5aba087f0581c320b79e98dd7e804c1e9cee98 [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(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000041 const MachineInstr &MI, AliasAnalysis *AA) const {
42 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 =
63 TargetRegisterInfo::isVirtualRegister(DestReg)
64 ? 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;
Dan Gohman4ba48162015-11-18 16:12:01 +000078 else
79 llvm_unreachable("Unexpected register class");
80
Dan Gohman4fc4e422016-10-24 19:49:43 +000081 BuildMI(MBB, I, DL, get(CopyOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000082 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
83}
Dan Gohman950a13c2015-09-16 16:51:30 +000084
Heejin Ahnf208f632018-09-05 01:27:38 +000085MachineInstr *WebAssemblyInstrInfo::commuteInstructionImpl(
86 MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const {
Dan Gohmanadf28172016-01-28 01:22:44 +000087 // If the operands are stackified, we can't reorder them.
88 WebAssemblyFunctionInfo &MFI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000089 *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
90 if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) ||
91 MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg()))
Dan Gohmanadf28172016-01-28 01:22:44 +000092 return nullptr;
93
94 // Otherwise use the default implementation.
95 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
96}
97
Dan Gohman950a13c2015-09-16 16:51:30 +000098// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +000099bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Dan Gohman950a13c2015-09-16 16:51:30 +0000100 MachineBasicBlock *&TBB,
101 MachineBasicBlock *&FBB,
102 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +0000103 bool /*AllowModify*/) const {
Heejin Ahn54551c12019-03-26 18:21:20 +0000104 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 Gohman950a13c2015-09-16 16:51:30 +0000111 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +0000112 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +0000113 switch (MI.getOpcode()) {
114 default:
115 // Unhandled instruction; bail out.
116 return true;
Dan Gohman231244c2015-11-13 00:46:31 +0000117 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +0000118 if (HaveCond)
119 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000120 Cond.push_back(MachineOperand::CreateImm(true));
Dan Gohman06b49582016-02-08 21:50:13 +0000121 Cond.push_back(MI.getOperand(1));
122 TBB = MI.getOperand(0).getMBB();
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000123 HaveCond = true;
124 break;
125 case WebAssembly::BR_UNLESS:
126 if (HaveCond)
127 return true;
128 Cond.push_back(MachineOperand::CreateImm(false));
Dan Gohman06b49582016-02-08 21:50:13 +0000129 Cond.push_back(MI.getOperand(1));
130 TBB = MI.getOperand(0).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000131 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 Ahnd6f48782019-01-30 03:21:57 +0000139 case WebAssembly::BR_ON_EXN:
140 if (HaveCond)
141 return true;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000142 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 Gohman950a13c2015-09-16 16:51:30 +0000147 }
148 if (MI.isBarrier())
149 break;
150 }
151
152 return false;
153}
154
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000155unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000156 int *BytesRemoved) const {
157 assert(!BytesRemoved && "code size not handled");
158
Dan Gohman950a13c2015-09-16 16:51:30 +0000159 MachineBasicBlock::instr_iterator I = MBB.instr_end();
160 unsigned Count = 0;
161
162 while (I != MBB.instr_begin()) {
163 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000164 if (I->isDebugInstr())
Dan Gohman950a13c2015-09-16 16:51:30 +0000165 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 Ahnf208f632018-09-05 01:27:38 +0000177unsigned WebAssemblyInstrInfo::insertBranch(
178 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
179 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000180 assert(!BytesAdded && "code size not handled");
181
Dan Gohman950a13c2015-09-16 16:51:30 +0000182 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 Gohmanf0b165a2015-12-05 03:03:35 +0000190 assert(Cond.size() == 2 && "Expected a flag and a successor block");
191
Heejin Ahnd6f48782019-01-30 03:21:57 +0000192 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 Gohmanf0b165a2015-12-05 03:03:35 +0000197 if (Cond[0].getImm()) {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000198 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 Gohmanf0b165a2015-12-05 03:03:35 +0000206 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000207 assert(!IsBrOnExn && "br_on_exn does not have a reversed condition");
Diana Picus116bbab2017-01-13 09:58:52 +0000208 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000209 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000210 if (!FBB)
211 return 1;
212
213 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
214 return 2;
215}
216
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000217bool WebAssemblyInstrInfo::reverseBranchCondition(
Dan Gohman950a13c2015-09-16 16:51:30 +0000218 SmallVectorImpl<MachineOperand> &Cond) const {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000219 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 Gohmanf0b165a2015-12-05 03:03:35 +0000228 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
229 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000230}