blob: 995dc88bb333f4b98ba0b989abe01b346f9c1083 [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
Dan Gohman10e730a2015-06-29 23:51:55 +000030WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
Dan Gohman35bfb242015-12-04 23:22:35 +000031 : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000032 WebAssembly::ADJCALLSTACKUP,
33 WebAssembly::CATCHRET),
Dan Gohman35bfb242015-12-04 23:22:35 +000034 RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000035
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000036bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000037 const MachineInstr &MI, AliasAnalysis *AA) const {
38 switch (MI.getOpcode()) {
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000039 case WebAssembly::CONST_I32:
40 case WebAssembly::CONST_I64:
41 case WebAssembly::CONST_F32:
42 case WebAssembly::CONST_F64:
43 // isReallyTriviallyReMaterializableGeneric misses these because of the
44 // ARGUMENTS implicit def, so we manualy override it here.
45 return true;
46 default:
47 return false;
48 }
49}
50
Dan Gohman4f52e002015-09-09 00:52:47 +000051void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
52 MachineBasicBlock::iterator I,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000053 const DebugLoc &DL, unsigned DestReg,
Dan Gohman4f52e002015-09-09 00:52:47 +000054 unsigned SrcReg, bool KillSrc) const {
Derek Schuff8bb5f292015-12-16 23:21:30 +000055 // This method is called by post-RA expansion, which expects only pregs to
56 // exist. However we need to handle both here.
57 auto &MRI = MBB.getParent()->getRegInfo();
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000058 const TargetRegisterClass *RC =
59 TargetRegisterInfo::isVirtualRegister(DestReg)
60 ? MRI.getRegClass(DestReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +000061 : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(DestReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000062
Dan Gohman4fc4e422016-10-24 19:49:43 +000063 unsigned CopyOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000064 if (RC == &WebAssembly::I32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000065 CopyOpcode = WebAssembly::COPY_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000066 else if (RC == &WebAssembly::I64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000067 CopyOpcode = WebAssembly::COPY_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000068 else if (RC == &WebAssembly::F32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000069 CopyOpcode = WebAssembly::COPY_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000070 else if (RC == &WebAssembly::F64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000071 CopyOpcode = WebAssembly::COPY_F64;
Thomas Lively89717192018-11-08 02:35:28 +000072 else if (RC == &WebAssembly::V128RegClass)
73 CopyOpcode = WebAssembly::COPY_V128;
Dan Gohman4ba48162015-11-18 16:12:01 +000074 else
75 llvm_unreachable("Unexpected register class");
76
Dan Gohman4fc4e422016-10-24 19:49:43 +000077 BuildMI(MBB, I, DL, get(CopyOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000078 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
79}
Dan Gohman950a13c2015-09-16 16:51:30 +000080
Heejin Ahnf208f632018-09-05 01:27:38 +000081MachineInstr *WebAssemblyInstrInfo::commuteInstructionImpl(
82 MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const {
Dan Gohmanadf28172016-01-28 01:22:44 +000083 // If the operands are stackified, we can't reorder them.
84 WebAssemblyFunctionInfo &MFI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000085 *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
86 if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) ||
87 MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg()))
Dan Gohmanadf28172016-01-28 01:22:44 +000088 return nullptr;
89
90 // Otherwise use the default implementation.
91 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
92}
93
Dan Gohman950a13c2015-09-16 16:51:30 +000094// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +000095bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Dan Gohman950a13c2015-09-16 16:51:30 +000096 MachineBasicBlock *&TBB,
97 MachineBasicBlock *&FBB,
98 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +000099 bool /*AllowModify*/) const {
Dan Gohman950a13c2015-09-16 16:51:30 +0000100 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +0000101 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +0000102 switch (MI.getOpcode()) {
103 default:
104 // Unhandled instruction; bail out.
105 return true;
Dan Gohman231244c2015-11-13 00:46:31 +0000106 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +0000107 if (HaveCond)
108 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000109 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000110 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000111 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000112 Cond.push_back(MachineOperand::CreateImm(true));
Dan Gohman06b49582016-02-08 21:50:13 +0000113 Cond.push_back(MI.getOperand(1));
114 TBB = MI.getOperand(0).getMBB();
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000115 HaveCond = true;
116 break;
117 case WebAssembly::BR_UNLESS:
118 if (HaveCond)
119 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000120 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000121 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000122 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000123 Cond.push_back(MachineOperand::CreateImm(false));
Dan Gohman06b49582016-02-08 21:50:13 +0000124 Cond.push_back(MI.getOperand(1));
125 TBB = MI.getOperand(0).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000126 HaveCond = true;
127 break;
128 case WebAssembly::BR:
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000129 // If we're running after CFGStackify, we can't optimize further.
130 if (!MI.getOperand(0).isMBB())
131 return true;
Dan Gohman950a13c2015-09-16 16:51:30 +0000132 if (!HaveCond)
133 TBB = MI.getOperand(0).getMBB();
134 else
135 FBB = MI.getOperand(0).getMBB();
136 break;
Heejin Ahnd6f48782019-01-30 03:21:57 +0000137 case WebAssembly::BR_ON_EXN:
138 if (HaveCond)
139 return true;
140 // If we're running after CFGStackify, we can't optimize further.
141 if (!MI.getOperand(0).isMBB())
142 return true;
143 Cond.push_back(MachineOperand::CreateImm(true));
144 Cond.push_back(MI.getOperand(2));
145 TBB = MI.getOperand(0).getMBB();
146 HaveCond = true;
147 break;
Dan Gohman950a13c2015-09-16 16:51:30 +0000148 }
149 if (MI.isBarrier())
150 break;
151 }
152
153 return false;
154}
155
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000156unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000157 int *BytesRemoved) const {
158 assert(!BytesRemoved && "code size not handled");
159
Dan Gohman950a13c2015-09-16 16:51:30 +0000160 MachineBasicBlock::instr_iterator I = MBB.instr_end();
161 unsigned Count = 0;
162
163 while (I != MBB.instr_begin()) {
164 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000165 if (I->isDebugInstr())
Dan Gohman950a13c2015-09-16 16:51:30 +0000166 continue;
167 if (!I->isTerminator())
168 break;
169 // Remove the branch.
170 I->eraseFromParent();
171 I = MBB.instr_end();
172 ++Count;
173 }
174
175 return Count;
176}
177
Heejin Ahnf208f632018-09-05 01:27:38 +0000178unsigned WebAssemblyInstrInfo::insertBranch(
179 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
180 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000181 assert(!BytesAdded && "code size not handled");
182
Dan Gohman950a13c2015-09-16 16:51:30 +0000183 if (Cond.empty()) {
184 if (!TBB)
185 return 0;
186
187 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
188 return 1;
189 }
190
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000191 assert(Cond.size() == 2 && "Expected a flag and a successor block");
192
Heejin Ahnd6f48782019-01-30 03:21:57 +0000193 MachineFunction &MF = *MBB.getParent();
194 auto &MRI = MF.getRegInfo();
195 bool IsBrOnExn = Cond[1].isReg() && MRI.getRegClass(Cond[1].getReg()) ==
196 &WebAssembly::EXCEPT_REFRegClass;
197
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000198 if (Cond[0].getImm()) {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000199 if (IsBrOnExn) {
200 const char *CPPExnSymbol = MF.createExternalSymbolName("__cpp_exception");
201 BuildMI(&MBB, DL, get(WebAssembly::BR_ON_EXN))
202 .addMBB(TBB)
203 .addExternalSymbol(CPPExnSymbol, WebAssemblyII::MO_SYMBOL_EVENT)
204 .add(Cond[1]);
205 } else
206 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000207 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000208 assert(!IsBrOnExn && "br_on_exn does not have a reversed condition");
Diana Picus116bbab2017-01-13 09:58:52 +0000209 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000210 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000211 if (!FBB)
212 return 1;
213
214 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
215 return 2;
216}
217
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000218bool WebAssemblyInstrInfo::reverseBranchCondition(
Dan Gohman950a13c2015-09-16 16:51:30 +0000219 SmallVectorImpl<MachineOperand> &Cond) const {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000220 assert(Cond.size() == 2 && "Expected a flag and a condition expression");
221
222 // br_on_exn's condition cannot be reversed
223 MachineFunction &MF = *Cond[1].getParent()->getParent()->getParent();
224 auto &MRI = MF.getRegInfo();
225 if (Cond[1].isReg() &&
226 MRI.getRegClass(Cond[1].getReg()) == &WebAssembly::EXCEPT_REFRegClass)
227 return true;
228
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000229 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
230 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000231}