blob: a7c120963e010fbfd9dfdd3ba6360565c1a4af85 [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 {
Dan Gohman950a13c2015-09-16 16:51:30 +0000104 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +0000105 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +0000106 switch (MI.getOpcode()) {
107 default:
108 // Unhandled instruction; bail out.
109 return true;
Dan Gohman231244c2015-11-13 00:46:31 +0000110 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +0000111 if (HaveCond)
112 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000113 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000114 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000115 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000116 Cond.push_back(MachineOperand::CreateImm(true));
Dan Gohman06b49582016-02-08 21:50:13 +0000117 Cond.push_back(MI.getOperand(1));
118 TBB = MI.getOperand(0).getMBB();
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000119 HaveCond = true;
120 break;
121 case WebAssembly::BR_UNLESS:
122 if (HaveCond)
123 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000124 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000125 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000126 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000127 Cond.push_back(MachineOperand::CreateImm(false));
Dan Gohman06b49582016-02-08 21:50:13 +0000128 Cond.push_back(MI.getOperand(1));
129 TBB = MI.getOperand(0).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000130 HaveCond = true;
131 break;
132 case WebAssembly::BR:
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000133 // If we're running after CFGStackify, we can't optimize further.
134 if (!MI.getOperand(0).isMBB())
135 return true;
Dan Gohman950a13c2015-09-16 16:51:30 +0000136 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;
144 // If we're running after CFGStackify, we can't optimize further.
145 if (!MI.getOperand(0).isMBB())
146 return true;
147 Cond.push_back(MachineOperand::CreateImm(true));
148 Cond.push_back(MI.getOperand(2));
149 TBB = MI.getOperand(0).getMBB();
150 HaveCond = true;
151 break;
Dan Gohman950a13c2015-09-16 16:51:30 +0000152 }
153 if (MI.isBarrier())
154 break;
155 }
156
157 return false;
158}
159
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000160unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000161 int *BytesRemoved) const {
162 assert(!BytesRemoved && "code size not handled");
163
Dan Gohman950a13c2015-09-16 16:51:30 +0000164 MachineBasicBlock::instr_iterator I = MBB.instr_end();
165 unsigned Count = 0;
166
167 while (I != MBB.instr_begin()) {
168 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000169 if (I->isDebugInstr())
Dan Gohman950a13c2015-09-16 16:51:30 +0000170 continue;
171 if (!I->isTerminator())
172 break;
173 // Remove the branch.
174 I->eraseFromParent();
175 I = MBB.instr_end();
176 ++Count;
177 }
178
179 return Count;
180}
181
Heejin Ahnf208f632018-09-05 01:27:38 +0000182unsigned WebAssemblyInstrInfo::insertBranch(
183 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
184 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000185 assert(!BytesAdded && "code size not handled");
186
Dan Gohman950a13c2015-09-16 16:51:30 +0000187 if (Cond.empty()) {
188 if (!TBB)
189 return 0;
190
191 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
192 return 1;
193 }
194
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000195 assert(Cond.size() == 2 && "Expected a flag and a successor block");
196
Heejin Ahnd6f48782019-01-30 03:21:57 +0000197 MachineFunction &MF = *MBB.getParent();
198 auto &MRI = MF.getRegInfo();
199 bool IsBrOnExn = Cond[1].isReg() && MRI.getRegClass(Cond[1].getReg()) ==
200 &WebAssembly::EXCEPT_REFRegClass;
201
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000202 if (Cond[0].getImm()) {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000203 if (IsBrOnExn) {
204 const char *CPPExnSymbol = MF.createExternalSymbolName("__cpp_exception");
205 BuildMI(&MBB, DL, get(WebAssembly::BR_ON_EXN))
206 .addMBB(TBB)
207 .addExternalSymbol(CPPExnSymbol, WebAssemblyII::MO_SYMBOL_EVENT)
208 .add(Cond[1]);
209 } else
210 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000211 } else {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000212 assert(!IsBrOnExn && "br_on_exn does not have a reversed condition");
Diana Picus116bbab2017-01-13 09:58:52 +0000213 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000214 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000215 if (!FBB)
216 return 1;
217
218 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
219 return 2;
220}
221
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000222bool WebAssemblyInstrInfo::reverseBranchCondition(
Dan Gohman950a13c2015-09-16 16:51:30 +0000223 SmallVectorImpl<MachineOperand> &Cond) const {
Heejin Ahnd6f48782019-01-30 03:21:57 +0000224 assert(Cond.size() == 2 && "Expected a flag and a condition expression");
225
226 // br_on_exn's condition cannot be reversed
227 MachineFunction &MF = *Cond[1].getParent()->getParent()->getParent();
228 auto &MRI = MF.getRegInfo();
229 if (Cond[1].isReg() &&
230 MRI.getRegClass(Cond[1].getReg()) == &WebAssembly::EXCEPT_REFRegClass)
231 return true;
232
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000233 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
234 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000235}