blob: 5e7663cdb5063f7190a4e966e71bfce47b04da38 [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
35void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
36 MachineBasicBlock::iterator I,
37 DebugLoc DL, unsigned DestReg,
38 unsigned SrcReg, bool KillSrc) const {
Derek Schuff8bb5f292015-12-16 23:21:30 +000039 // This method is called by post-RA expansion, which expects only pregs to
40 // exist. However we need to handle both here.
41 auto &MRI = MBB.getParent()->getRegInfo();
42 const TargetRegisterClass *RC = TargetRegisterInfo::isVirtualRegister(DestReg) ?
43 MRI.getRegClass(DestReg) :
44 MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(SrcReg);
Dan Gohman4ba48162015-11-18 16:12:01 +000045
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000046 unsigned CopyLocalOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000047 if (RC == &WebAssembly::I32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000048 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000049 else if (RC == &WebAssembly::I64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000050 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000051 else if (RC == &WebAssembly::F32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000052 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000053 else if (RC == &WebAssembly::F64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000054 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000055 else
56 llvm_unreachable("Unexpected register class");
57
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000058 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000059 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
60}
Dan Gohman950a13c2015-09-16 16:51:30 +000061
62// Branch analysis.
63bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
64 MachineBasicBlock *&TBB,
65 MachineBasicBlock *&FBB,
66 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +000067 bool /*AllowModify*/) const {
Dan Gohman950a13c2015-09-16 16:51:30 +000068 bool HaveCond = false;
Dan Gohmand544e0c2015-12-21 17:22:02 +000069 for (MachineInstr &MI : MBB.terminators()) {
Dan Gohman950a13c2015-09-16 16:51:30 +000070 switch (MI.getOpcode()) {
71 default:
72 // Unhandled instruction; bail out.
73 return true;
Dan Gohman231244c2015-11-13 00:46:31 +000074 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +000075 if (HaveCond)
76 return true;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000077 Cond.push_back(MachineOperand::CreateImm(true));
78 Cond.push_back(MI.getOperand(0));
79 TBB = MI.getOperand(1).getMBB();
80 HaveCond = true;
81 break;
82 case WebAssembly::BR_UNLESS:
83 if (HaveCond)
84 return true;
85 Cond.push_back(MachineOperand::CreateImm(false));
Derek Schuff4ed47782015-11-16 21:04:51 +000086 Cond.push_back(MI.getOperand(0));
87 TBB = MI.getOperand(1).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +000088 HaveCond = true;
89 break;
90 case WebAssembly::BR:
91 if (!HaveCond)
92 TBB = MI.getOperand(0).getMBB();
93 else
94 FBB = MI.getOperand(0).getMBB();
95 break;
96 }
97 if (MI.isBarrier())
98 break;
99 }
100
101 return false;
102}
103
104unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
105 MachineBasicBlock::instr_iterator I = MBB.instr_end();
106 unsigned Count = 0;
107
108 while (I != MBB.instr_begin()) {
109 --I;
110 if (I->isDebugValue())
111 continue;
112 if (!I->isTerminator())
113 break;
114 // Remove the branch.
115 I->eraseFromParent();
116 I = MBB.instr_end();
117 ++Count;
118 }
119
120 return Count;
121}
122
Dan Gohman7a6b9822015-11-29 22:32:02 +0000123unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB,
124 MachineBasicBlock *TBB,
125 MachineBasicBlock *FBB,
126 ArrayRef<MachineOperand> Cond,
127 DebugLoc DL) const {
Dan Gohman950a13c2015-09-16 16:51:30 +0000128 if (Cond.empty()) {
129 if (!TBB)
130 return 0;
131
132 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
133 return 1;
134 }
135
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000136 assert(Cond.size() == 2 && "Expected a flag and a successor block");
137
138 if (Cond[0].getImm()) {
139 BuildMI(&MBB, DL, get(WebAssembly::BR_IF))
140 .addOperand(Cond[1])
141 .addMBB(TBB);
142 } else {
143 BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS))
144 .addOperand(Cond[1])
145 .addMBB(TBB);
146 }
Dan Gohman950a13c2015-09-16 16:51:30 +0000147 if (!FBB)
148 return 1;
149
150 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
151 return 2;
152}
153
154bool WebAssemblyInstrInfo::ReverseBranchCondition(
155 SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000156 assert(Cond.size() == 2 && "Expected a flag and a successor block");
157 Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
158 return false;
Dan Gohman950a13c2015-09-16 16:51:30 +0000159}