blob: c2eac6c3e0e88128fcc55923c4e9bcb08641fd55 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- C++ -*-===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the Alpha implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
15#include "AlphaInstrInfo.h"
16#include "AlphaGenInstrInfo.inc"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019using namespace llvm;
20
21AlphaInstrInfo::AlphaInstrInfo()
Owen Anderson718cb662007-09-07 04:06:50 +000022 : TargetInstrInfo(AlphaInsts, array_lengthof(AlphaInsts)),
Evan Cheng7ce45782006-11-13 23:36:35 +000023 RI(*this) { }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024
25
26bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
27 unsigned& sourceReg,
28 unsigned& destReg) const {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029 MachineOpCode oc = MI.getOpcode();
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +000030 if (oc == Alpha::BISr ||
Andrew Lenharthddc877c2006-03-09 18:18:51 +000031 oc == Alpha::CPYSS ||
32 oc == Alpha::CPYST ||
33 oc == Alpha::CPYSSt ||
34 oc == Alpha::CPYSTs) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000035 // or r1, r2, r2
36 // cpys(s|t) r1 r2 r2
Evan Cheng1e3417292007-04-25 07:12:14 +000037 assert(MI.getNumOperands() >= 3 &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +000038 MI.getOperand(0).isRegister() &&
39 MI.getOperand(1).isRegister() &&
40 MI.getOperand(2).isRegister() &&
41 "invalid Alpha BIS instruction!");
42 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
43 sourceReg = MI.getOperand(1).getReg();
44 destReg = MI.getOperand(0).getReg();
45 return true;
46 }
47 }
48 return false;
49}
Chris Lattner40839602006-02-02 20:12:32 +000050
51unsigned
52AlphaInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const {
53 switch (MI->getOpcode()) {
54 case Alpha::LDL:
55 case Alpha::LDQ:
56 case Alpha::LDBU:
57 case Alpha::LDWU:
58 case Alpha::LDS:
59 case Alpha::LDT:
60 if (MI->getOperand(1).isFrameIndex()) {
61 FrameIndex = MI->getOperand(1).getFrameIndex();
62 return MI->getOperand(0).getReg();
63 }
64 break;
65 }
66 return 0;
67}
68
Andrew Lenharth133d3102006-02-03 03:07:37 +000069unsigned
70AlphaInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
71 switch (MI->getOpcode()) {
72 case Alpha::STL:
73 case Alpha::STQ:
74 case Alpha::STB:
75 case Alpha::STW:
76 case Alpha::STS:
77 case Alpha::STT:
78 if (MI->getOperand(1).isFrameIndex()) {
79 FrameIndex = MI->getOperand(1).getFrameIndex();
80 return MI->getOperand(0).getReg();
81 }
82 break;
83 }
84 return 0;
85}
86
Andrew Lenharthf81173f2006-10-31 16:49:55 +000087static bool isAlphaIntCondCode(unsigned Opcode) {
88 switch (Opcode) {
89 case Alpha::BEQ:
90 case Alpha::BNE:
91 case Alpha::BGE:
92 case Alpha::BGT:
93 case Alpha::BLE:
94 case Alpha::BLT:
95 case Alpha::BLBC:
96 case Alpha::BLBS:
97 return true;
98 default:
99 return false;
100 }
101}
102
Evan Chengb5cdaa22007-05-18 00:05:48 +0000103unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
Chris Lattner0476b282006-10-24 16:41:36 +0000104 MachineBasicBlock *FBB,
105 const std::vector<MachineOperand> &Cond)const{
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000106 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
107 assert((Cond.size() == 2 || Cond.size() == 0) &&
108 "Alpha branch conditions have two components!");
109
110 // One-way branch.
111 if (FBB == 0) {
112 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000113 BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB);
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000114 else // Conditional branch
115 if (isAlphaIntCondCode(Cond[0].getImm()))
Evan Chengc0f64ff2006-11-27 23:37:22 +0000116 BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000117 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
118 else
Evan Chengc0f64ff2006-11-27 23:37:22 +0000119 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000120 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000121 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000122 }
123
124 // Two-way Conditional Branch.
125 if (isAlphaIntCondCode(Cond[0].getImm()))
Evan Chengc0f64ff2006-11-27 23:37:22 +0000126 BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000127 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128 else
Evan Chengc0f64ff2006-11-27 23:37:22 +0000129 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000130 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000131 BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000132 return 2;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000133}
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000134
135static unsigned AlphaRevCondCode(unsigned Opcode) {
136 switch (Opcode) {
137 case Alpha::BEQ: return Alpha::BNE;
138 case Alpha::BNE: return Alpha::BEQ;
139 case Alpha::BGE: return Alpha::BLT;
140 case Alpha::BGT: return Alpha::BLE;
141 case Alpha::BLE: return Alpha::BGT;
142 case Alpha::BLT: return Alpha::BGE;
143 case Alpha::BLBC: return Alpha::BLBS;
144 case Alpha::BLBS: return Alpha::BLBC;
145 case Alpha::FBEQ: return Alpha::FBNE;
146 case Alpha::FBNE: return Alpha::FBEQ;
147 case Alpha::FBGE: return Alpha::FBLT;
148 case Alpha::FBGT: return Alpha::FBLE;
149 case Alpha::FBLE: return Alpha::FBGT;
150 case Alpha::FBLT: return Alpha::FBGE;
151 default:
152 assert(0 && "Unknown opcode");
153 }
154}
155
156// Branch analysis.
157bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
158 MachineBasicBlock *&FBB,
159 std::vector<MachineOperand> &Cond) const {
160 // If the block has no terminators, it just falls into the block after it.
161 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000162 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000163 return false;
164
165 // Get the last instruction in the block.
166 MachineInstr *LastInst = I;
167
168 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000169 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000170 if (LastInst->getOpcode() == Alpha::BR) {
171 TBB = LastInst->getOperand(0).getMachineBasicBlock();
172 return false;
173 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
174 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
175 // Block ends with fall-through condbranch.
176 TBB = LastInst->getOperand(2).getMachineBasicBlock();
177 Cond.push_back(LastInst->getOperand(0));
178 Cond.push_back(LastInst->getOperand(1));
179 return false;
180 }
181 // Otherwise, don't know what this is.
182 return true;
183 }
184
185 // Get the instruction before it if it's a terminator.
186 MachineInstr *SecondLastInst = I;
187
188 // If there are three terminators, we don't know what sort of block this is.
189 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000190 isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000191 return true;
192
193 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
194 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
195 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
196 LastInst->getOpcode() == Alpha::BR) {
197 TBB = SecondLastInst->getOperand(2).getMachineBasicBlock();
198 Cond.push_back(SecondLastInst->getOperand(0));
199 Cond.push_back(SecondLastInst->getOperand(1));
200 FBB = LastInst->getOperand(0).getMachineBasicBlock();
201 return false;
202 }
203
Dale Johannesen13e8b512007-06-13 17:59:52 +0000204 // If the block ends with two Alpha::BRs, handle it. The second one is not
205 // executed, so remove it.
206 if (SecondLastInst->getOpcode() == Alpha::BR &&
207 LastInst->getOpcode() == Alpha::BR) {
208 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
209 I = LastInst;
210 I->eraseFromParent();
211 return false;
212 }
213
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000214 // Otherwise, can't handle this.
215 return true;
216}
217
Evan Chengb5cdaa22007-05-18 00:05:48 +0000218unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000219 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000220 if (I == MBB.begin()) return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000221 --I;
222 if (I->getOpcode() != Alpha::BR &&
223 I->getOpcode() != Alpha::COND_BRANCH_I &&
224 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000225 return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000226
227 // Remove the branch.
228 I->eraseFromParent();
229
230 I = MBB.end();
231
Evan Chengb5cdaa22007-05-18 00:05:48 +0000232 if (I == MBB.begin()) return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000233 --I;
234 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
235 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000236 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000237
238 // Remove the branch.
239 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000240 return 2;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000241}
242
243void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
244 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000245 BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31)
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000246 .addReg(Alpha::R31);
247}
248
249bool AlphaInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
250 if (MBB.empty()) return false;
251
252 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000253 case Alpha::RETDAG: // Return.
254 case Alpha::RETDAGp:
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000255 case Alpha::BR: // Uncond branch.
256 case Alpha::JMP: // Indirect branch.
257 return true;
258 default: return false;
259 }
260}
261bool AlphaInstrInfo::
262ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
263 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
264 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
265 return false;
266}
267