[WebAssembly] Implement eliminateCallFramePseudo
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyInstrInfo.cpp
1 //===-- 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"
23 using namespace llvm;
24
25 #define DEBUG_TYPE "wasm-instr-info"
26
27 #define GET_INSTRINFO_CTOR_DTOR
28 #include "WebAssemblyGenInstrInfo.inc"
29
30 WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
31     : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
32                               WebAssembly::ADJCALLSTACKUP),
33       RI(STI.getTargetTriple()) {}
34
35 void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
36                                        MachineBasicBlock::iterator I,
37                                        DebugLoc DL, unsigned DestReg,
38                                        unsigned SrcReg, bool KillSrc) const {
39   // 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);
45
46   unsigned CopyLocalOpcode;
47   if (RC == &WebAssembly::I32RegClass)
48     CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
49   else if (RC == &WebAssembly::I64RegClass)
50     CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
51   else if (RC == &WebAssembly::F32RegClass)
52     CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
53   else if (RC == &WebAssembly::F64RegClass)
54     CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
55   else
56     llvm_unreachable("Unexpected register class");
57
58   BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
59       .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
60 }
61
62 // Branch analysis.
63 bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
64                                          MachineBasicBlock *&TBB,
65                                          MachineBasicBlock *&FBB,
66                                          SmallVectorImpl<MachineOperand> &Cond,
67                                          bool /*AllowModify*/) const {
68   bool HaveCond = false;
69   for (MachineInstr &MI : iterator_range<MachineBasicBlock::instr_iterator>(
70            MBB.getFirstInstrTerminator(), MBB.instr_end())) {
71     switch (MI.getOpcode()) {
72     default:
73       // Unhandled instruction; bail out.
74       return true;
75     case WebAssembly::BR_IF:
76       if (HaveCond)
77         return true;
78       Cond.push_back(MachineOperand::CreateImm(true));
79       Cond.push_back(MI.getOperand(0));
80       TBB = MI.getOperand(1).getMBB();
81       HaveCond = true;
82       break;
83     case WebAssembly::BR_UNLESS:
84       if (HaveCond)
85         return true;
86       Cond.push_back(MachineOperand::CreateImm(false));
87       Cond.push_back(MI.getOperand(0));
88       TBB = MI.getOperand(1).getMBB();
89       HaveCond = true;
90       break;
91     case WebAssembly::BR:
92       if (!HaveCond)
93         TBB = MI.getOperand(0).getMBB();
94       else
95         FBB = MI.getOperand(0).getMBB();
96       break;
97     }
98     if (MI.isBarrier())
99       break;
100   }
101
102   return false;
103 }
104
105 unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
106   MachineBasicBlock::instr_iterator I = MBB.instr_end();
107   unsigned Count = 0;
108
109   while (I != MBB.instr_begin()) {
110     --I;
111     if (I->isDebugValue())
112       continue;
113     if (!I->isTerminator())
114       break;
115     // Remove the branch.
116     I->eraseFromParent();
117     I = MBB.instr_end();
118     ++Count;
119   }
120
121   return Count;
122 }
123
124 unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB,
125                                             MachineBasicBlock *TBB,
126                                             MachineBasicBlock *FBB,
127                                             ArrayRef<MachineOperand> Cond,
128                                             DebugLoc DL) const {
129   if (Cond.empty()) {
130     if (!TBB)
131       return 0;
132
133     BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
134     return 1;
135   }
136
137   assert(Cond.size() == 2 && "Expected a flag and a successor block");
138
139   if (Cond[0].getImm()) {
140     BuildMI(&MBB, DL, get(WebAssembly::BR_IF))
141         .addOperand(Cond[1])
142         .addMBB(TBB);
143   } else {
144     BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS))
145         .addOperand(Cond[1])
146         .addMBB(TBB);
147   }
148   if (!FBB)
149     return 1;
150
151   BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
152   return 2;
153 }
154
155 bool WebAssemblyInstrInfo::ReverseBranchCondition(
156     SmallVectorImpl<MachineOperand> &Cond) const {
157   assert(Cond.size() == 2 && "Expected a flag and a successor block");
158   Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
159   return false;
160 }