blob: eaec137ff10495c584e4d02dc66ff96030b48be9 [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
11/// \brief This file contains the WebAssembly implementation of the
12/// 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,
33 WebAssembly::ADJCALLSTACKUP),
34 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 Gohmanaa0a4bd2015-11-23 19:30:43 +000063 unsigned CopyLocalOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000064 if (RC == &WebAssembly::I32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000065 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000066 else if (RC == &WebAssembly::I64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000067 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000068 else if (RC == &WebAssembly::F32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000069 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000070 else if (RC == &WebAssembly::F64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000071 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000072 else
73 llvm_unreachable("Unexpected register class");
74
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000075 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000076 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
77}
Dan Gohman950a13c2015-09-16 16:51:30 +000078
Dan Gohmanadf28172016-01-28 01:22:44 +000079MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +000080WebAssemblyInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
Dan Gohmanadf28172016-01-28 01:22:44 +000081 unsigned OpIdx1,
82 unsigned OpIdx2) const {
83 // 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.
95bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
96 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;
137 }
138 if (MI.isBarrier())
139 break;
140 }
141
142 return false;
143}
144
145unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
146 MachineBasicBlock::instr_iterator I = MBB.instr_end();
147 unsigned Count = 0;
148
149 while (I != MBB.instr_begin()) {
150 --I;
151 if (I->isDebugValue())
152 continue;
153 if (!I->isTerminator())
154 break;
155 // Remove the branch.
156 I->eraseFromParent();
157 I = MBB.instr_end();
158 ++Count;
159 }
160
161 return Count;
162}
163
Dan Gohman7a6b9822015-11-29 22:32:02 +0000164unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB,
165 MachineBasicBlock *TBB,
166 MachineBasicBlock *FBB,
167 ArrayRef<MachineOperand> Cond,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000168 const DebugLoc &DL) const {
Dan Gohman950a13c2015-09-16 16:51:30 +0000169 if (Cond.empty()) {
170 if (!TBB)
171 return 0;
172
173 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
174 return 1;
175 }
176
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000177 assert(Cond.size() == 2 && "Expected a flag and a successor block");
178
179 if (Cond[0].getImm()) {
Dan Gohman06b49582016-02-08 21:50:13 +0000180 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).addOperand(Cond[1]);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000181 } else {
182 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS))
Dan Gohman06b49582016-02-08 21:50:13 +0000183 .addMBB(TBB)
184 .addOperand(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
193bool WebAssemblyInstrInfo::ReverseBranchCondition(
194 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}