blob: 37dd0c4026a276f30147cc51c54733f69e50b954 [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"
18#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,
32 WebAssembly::ADJCALLSTACKUP),
33 RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000034
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000035bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable(
36 const MachineInstr *MI, AliasAnalysis *AA) const {
37 switch (MI->getOpcode()) {
38 case WebAssembly::CONST_I32:
39 case WebAssembly::CONST_I64:
40 case WebAssembly::CONST_F32:
41 case WebAssembly::CONST_F64:
42 // isReallyTriviallyReMaterializableGeneric misses these because of the
43 // ARGUMENTS implicit def, so we manualy override it here.
44 return true;
45 default:
46 return false;
47 }
48}
49
Dan Gohman4f52e002015-09-09 00:52:47 +000050void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator I,
52 DebugLoc DL, unsigned DestReg,
53 unsigned SrcReg, bool KillSrc) const {
Derek Schuff8bb5f292015-12-16 23:21:30 +000054 // This method is called by post-RA expansion, which expects only pregs to
55 // exist. However we need to handle both here.
56 auto &MRI = MBB.getParent()->getRegInfo();
Dan Gohmanb6fd39a2016-01-19 16:59:23 +000057 const TargetRegisterClass *RC =
58 TargetRegisterInfo::isVirtualRegister(DestReg)
59 ? MRI.getRegClass(DestReg)
60 : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(SrcReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000061
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000062 unsigned CopyLocalOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000063 if (RC == &WebAssembly::I32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000064 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000065 else if (RC == &WebAssembly::I64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000066 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000067 else if (RC == &WebAssembly::F32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000068 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000069 else if (RC == &WebAssembly::F64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000070 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000071 else
72 llvm_unreachable("Unexpected register class");
73
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000074 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000075 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
76}
Dan Gohman950a13c2015-09-16 16:51:30 +000077
78// Branch analysis.
79bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
80 MachineBasicBlock *&TBB,
81 MachineBasicBlock *&FBB,
82 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +000083 bool /*AllowModify*/) const {
Dan Gohman950a13c2015-09-16 16:51:30 +000084 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +000085 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +000086 switch (MI.getOpcode()) {
87 default:
88 // Unhandled instruction; bail out.
89 return true;
Dan Gohman231244c2015-11-13 00:46:31 +000090 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +000091 if (HaveCond)
92 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +000093 // If we're running after CFGStackify, we can't optimize further.
94 if (!MI.getOperand(1).isMBB())
95 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000096 Cond.push_back(MachineOperand::CreateImm(true));
97 Cond.push_back(MI.getOperand(0));
98 TBB = MI.getOperand(1).getMBB();
99 HaveCond = true;
100 break;
101 case WebAssembly::BR_UNLESS:
102 if (HaveCond)
103 return true;
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000104 // If we're running after CFGStackify, we can't optimize further.
105 if (!MI.getOperand(1).isMBB())
106 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000107 Cond.push_back(MachineOperand::CreateImm(false));
Derek Schuff4ed47782015-11-16 21:04:51 +0000108 Cond.push_back(MI.getOperand(0));
109 TBB = MI.getOperand(1).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +0000110 HaveCond = true;
111 break;
112 case WebAssembly::BR:
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000113 // If we're running after CFGStackify, we can't optimize further.
114 if (!MI.getOperand(0).isMBB())
115 return true;
Dan Gohman950a13c2015-09-16 16:51:30 +0000116 if (!HaveCond)
117 TBB = MI.getOperand(0).getMBB();
118 else
119 FBB = MI.getOperand(0).getMBB();
120 break;
121 }
122 if (MI.isBarrier())
123 break;
124 }
125
126 return false;
127}
128
129unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
130 MachineBasicBlock::instr_iterator I = MBB.instr_end();
131 unsigned Count = 0;
132
133 while (I != MBB.instr_begin()) {
134 --I;
135 if (I->isDebugValue())
136 continue;
137 if (!I->isTerminator())
138 break;
139 // Remove the branch.
140 I->eraseFromParent();
141 I = MBB.instr_end();
142 ++Count;
143 }
144
145 return Count;
146}
147
Dan Gohman7a6b9822015-11-29 22:32:02 +0000148unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB,
149 MachineBasicBlock *TBB,
150 MachineBasicBlock *FBB,
151 ArrayRef<MachineOperand> Cond,
152 DebugLoc DL) const {
Dan Gohman950a13c2015-09-16 16:51:30 +0000153 if (Cond.empty()) {
154 if (!TBB)
155 return 0;
156
157 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
158 return 1;
159 }
160
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000161 assert(Cond.size() == 2 && "Expected a flag and a successor block");
162
163 if (Cond[0].getImm()) {
Dan Gohmanb6fd39a2016-01-19 16:59:23 +0000164 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addOperand(Cond[1]).addMBB(TBB);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000165 } else {
166 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS))
167 .addOperand(Cond[1])
168 .addMBB(TBB);
169 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000170 if (!FBB)
171 return 1;
172
173 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
174 return 2;
175}
176
177bool WebAssemblyInstrInfo::ReverseBranchCondition(
178 SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000179 assert(Cond.size() == 2 && "Expected a flag and a successor block");
180 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
181 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000182}