blob: a1e0516a53bf211a4f4490b34ce0fde900e06d03 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file contains the WebAssembly implementation of the
Dan Gohman10e730a2015-06-29 23:51:55 +000012/// TargetInstrInfo class.
13///
14//===----------------------------------------------------------------------===//
15
16#include "WebAssemblyInstrInfo.h"
17#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmanadf28172016-01-28 01:22:44 +000018#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000019#include "WebAssemblySubtarget.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineMemOperand.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-instr-info"
27
JF Bastienb9073fb2015-07-22 21:28:15 +000028#define GET_INSTRINFO_CTOR_DTOR
29#include "WebAssemblyGenInstrInfo.inc"
30
Dan Gohman10e730a2015-06-29 23:51:55 +000031WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
Dan Gohman35bfb242015-12-04 23:22:35 +000032 : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000033 WebAssembly::ADJCALLSTACKUP,
34 WebAssembly::CATCHRET),
Dan Gohman35bfb242015-12-04 23:22:35 +000035 RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000036
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000037bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000038 const MachineInstr &MI, AliasAnalysis *AA) const {
39 switch (MI.getOpcode()) {
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000040 case WebAssembly::CONST_I32:
41 case WebAssembly::CONST_I64:
42 case WebAssembly::CONST_F32:
43 case WebAssembly::CONST_F64:
44 // isReallyTriviallyReMaterializableGeneric misses these because of the
45 // ARGUMENTS implicit def, so we manualy override it here.
46 return true;
47 default:
48 return false;
49 }
50}
51
Dan Gohman4f52e002015-09-09 00:52:47 +000052void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
53 MachineBasicBlock::iterator I,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000054 const DebugLoc &DL, unsigned DestReg,
Dan Gohman4f52e002015-09-09 00:52:47 +000055 unsigned SrcReg, bool KillSrc) const {
Derek Schuff8bb5f292015-12-16 23:21:30 +000056 // This method is called by post-RA expansion, which expects only pregs to
57 // exist. However we need to handle both here.
58 auto &MRI = MBB.getParent()->getRegInfo();
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000059 const TargetRegisterClass *RC =
60 TargetRegisterInfo::isVirtualRegister(DestReg)
61 ? MRI.getRegClass(DestReg)
Derek Schuff6ea637a2016-01-29 18:37:49 +000062 : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(DestReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000063
Dan Gohman4fc4e422016-10-24 19:49:43 +000064 unsigned CopyOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000065 if (RC == &WebAssembly::I32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000066 CopyOpcode = WebAssembly::COPY_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000067 else if (RC == &WebAssembly::I64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000068 CopyOpcode = WebAssembly::COPY_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000069 else if (RC == &WebAssembly::F32RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000070 CopyOpcode = WebAssembly::COPY_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000071 else if (RC == &WebAssembly::F64RegClass)
Dan Gohman4fc4e422016-10-24 19:49:43 +000072 CopyOpcode = WebAssembly::COPY_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000073 else
74 llvm_unreachable("Unexpected register class");
75
Dan Gohman4fc4e422016-10-24 19:49:43 +000076 BuildMI(MBB, I, DL, get(CopyOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000077 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
78}
Dan Gohman950a13c2015-09-16 16:51:30 +000079
Heejin Ahnf208f632018-09-05 01:27:38 +000080MachineInstr *WebAssemblyInstrInfo::commuteInstructionImpl(
81 MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const {
Dan Gohmanadf28172016-01-28 01:22:44 +000082 // If the operands are stackified, we can't reorder them.
83 WebAssemblyFunctionInfo &MFI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000084 *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
85 if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) ||
86 MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg()))
Dan Gohmanadf28172016-01-28 01:22:44 +000087 return nullptr;
88
89 // Otherwise use the default implementation.
90 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
91}
92
Dan Gohman950a13c2015-09-16 16:51:30 +000093// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +000094bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Dan Gohman950a13c2015-09-16 16:51:30 +000095 MachineBasicBlock *&TBB,
96 MachineBasicBlock *&FBB,
97 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +000098 bool /*AllowModify*/) const {
Dan Gohman950a13c2015-09-16 16:51:30 +000099 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +0000100 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +0000101 switch (MI.getOpcode()) {
102 default:
103 // Unhandled instruction; bail out.
104 return true;
Dan Gohman231244c2015-11-13 00:46:31 +0000105 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +0000106 if (HaveCond)
107 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000108 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000109 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000110 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000111 Cond.push_back(MachineOperand::CreateImm(true));
Dan Gohman06b49582016-02-08 21:50:13 +0000112 Cond.push_back(MI.getOperand(1));
113 TBB = MI.getOperand(0).getMBB();
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000114 HaveCond = true;
115 break;
116 case WebAssembly::BR_UNLESS:
117 if (HaveCond)
118 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000119 // If we're running after CFGStackify, we can't optimize further.
Dan Gohman06b49582016-02-08 21:50:13 +0000120 if (!MI.getOperand(0).isMBB())
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000121 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000122 Cond.push_back(MachineOperand::CreateImm(false));
Dan Gohman06b49582016-02-08 21:50:13 +0000123 Cond.push_back(MI.getOperand(1));
124 TBB = MI.getOperand(0).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000125 HaveCond = true;
126 break;
127 case WebAssembly::BR:
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000128 // If we're running after CFGStackify, we can't optimize further.
129 if (!MI.getOperand(0).isMBB())
130 return true;
Dan Gohman950a13c2015-09-16 16:51:30 +0000131 if (!HaveCond)
132 TBB = MI.getOperand(0).getMBB();
133 else
134 FBB = MI.getOperand(0).getMBB();
135 break;
136 }
137 if (MI.isBarrier())
138 break;
139 }
140
141 return false;
142}
143
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000144unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000145 int *BytesRemoved) const {
146 assert(!BytesRemoved && "code size not handled");
147
Dan Gohman950a13c2015-09-16 16:51:30 +0000148 MachineBasicBlock::instr_iterator I = MBB.instr_end();
149 unsigned Count = 0;
150
151 while (I != MBB.instr_begin()) {
152 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000153 if (I->isDebugInstr())
Dan Gohman950a13c2015-09-16 16:51:30 +0000154 continue;
155 if (!I->isTerminator())
156 break;
157 // Remove the branch.
158 I->eraseFromParent();
159 I = MBB.instr_end();
160 ++Count;
161 }
162
163 return Count;
164}
165
Heejin Ahnf208f632018-09-05 01:27:38 +0000166unsigned WebAssemblyInstrInfo::insertBranch(
167 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
168 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000169 assert(!BytesAdded && "code size not handled");
170
Dan Gohman950a13c2015-09-16 16:51:30 +0000171 if (Cond.empty()) {
172 if (!TBB)
173 return 0;
174
175 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
176 return 1;
177 }
178
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000179 assert(Cond.size() == 2 && "Expected a flag and a successor block");
180
181 if (Cond[0].getImm()) {
Diana Picus116bbab2017-01-13 09:58:52 +0000182 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000183 } else {
Diana Picus116bbab2017-01-13 09:58:52 +0000184 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000185 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000186 if (!FBB)
187 return 1;
188
189 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
190 return 2;
191}
192
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000193bool WebAssemblyInstrInfo::reverseBranchCondition(
Dan Gohman950a13c2015-09-16 16:51:30 +0000194 SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000195 assert(Cond.size() == 2 && "Expected a flag and a successor block");
196 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
197 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000198}