blob: ebf48cf01570d089e05a28fb5862bdde006b4451 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- R600InstrInfo.cpp - R600 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 R600 Implementation of TargetInstrInfo.
12//
13//===----------------------------------------------------------------------===//
14
15#include "R600InstrInfo.h"
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +000016#include "AMDGPU.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "AMDGPUSubtarget.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000018#include "AMDGPUTargetMachine.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "R600Defines.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000020#include "R600MachineFunctionInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "R600RegisterInfo.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025
26#define GET_INSTRINFO_CTOR
27#include "AMDGPUGenDFAPacketizer.inc"
28
29using namespace llvm;
30
31R600InstrInfo::R600InstrInfo(AMDGPUTargetMachine &tm)
32 : AMDGPUInstrInfo(tm),
Bill Wendling37e9adb2013-06-07 20:28:55 +000033 RI(tm),
Vincent Lejeunec2991642013-04-30 00:13:39 +000034 ST(tm.getSubtarget<AMDGPUSubtarget>())
Tom Stellard75aadc22012-12-11 21:25:42 +000035 { }
36
37const R600RegisterInfo &R600InstrInfo::getRegisterInfo() const {
38 return RI;
39}
40
41bool R600InstrInfo::isTrig(const MachineInstr &MI) const {
42 return get(MI.getOpcode()).TSFlags & R600_InstFlag::TRIG;
43}
44
45bool R600InstrInfo::isVector(const MachineInstr &MI) const {
46 return get(MI.getOpcode()).TSFlags & R600_InstFlag::VECTOR;
47}
48
49void
50R600InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator MI, DebugLoc DL,
52 unsigned DestReg, unsigned SrcReg,
53 bool KillSrc) const {
Tom Stellard0344cdf2013-08-01 15:23:42 +000054 unsigned VectorComponents = 0;
55 if (AMDGPU::R600_Reg128RegClass.contains(DestReg) &&
56 AMDGPU::R600_Reg128RegClass.contains(SrcReg)) {
57 VectorComponents = 4;
58 } else if(AMDGPU::R600_Reg64RegClass.contains(DestReg) &&
59 AMDGPU::R600_Reg64RegClass.contains(SrcReg)) {
60 VectorComponents = 2;
61 }
62
63 if (VectorComponents > 0) {
64 for (unsigned I = 0; I < VectorComponents; I++) {
Tom Stellard75aadc22012-12-11 21:25:42 +000065 unsigned SubRegIndex = RI.getSubRegFromChannel(I);
66 buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
67 RI.getSubReg(DestReg, SubRegIndex),
68 RI.getSubReg(SrcReg, SubRegIndex))
69 .addReg(DestReg,
70 RegState::Define | RegState::Implicit);
71 }
72 } else {
Tom Stellard75aadc22012-12-11 21:25:42 +000073 MachineInstr *NewMI = buildDefaultInstruction(MBB, MI, AMDGPU::MOV,
74 DestReg, SrcReg);
Tom Stellard02661d92013-06-25 21:22:18 +000075 NewMI->getOperand(getOperandIdx(*NewMI, AMDGPU::OpName::src0))
Tom Stellard75aadc22012-12-11 21:25:42 +000076 .setIsKill(KillSrc);
77 }
78}
79
80MachineInstr * R600InstrInfo::getMovImmInstr(MachineFunction *MF,
81 unsigned DstReg, int64_t Imm) const {
82 MachineInstr * MI = MF->CreateMachineInstr(get(AMDGPU::MOV), DebugLoc());
NAKAMURA Takumi2a0b40f2012-12-20 00:22:11 +000083 MachineInstrBuilder MIB(*MF, MI);
84 MIB.addReg(DstReg, RegState::Define);
85 MIB.addReg(AMDGPU::ALU_LITERAL_X);
86 MIB.addImm(Imm);
87 MIB.addReg(0); // PREDICATE_BIT
Tom Stellard75aadc22012-12-11 21:25:42 +000088
89 return MI;
90}
91
92unsigned R600InstrInfo::getIEQOpcode() const {
93 return AMDGPU::SETE_INT;
94}
95
96bool R600InstrInfo::isMov(unsigned Opcode) const {
97
98
99 switch(Opcode) {
100 default: return false;
101 case AMDGPU::MOV:
102 case AMDGPU::MOV_IMM_F32:
103 case AMDGPU::MOV_IMM_I32:
104 return true;
105 }
106}
107
108// Some instructions act as place holders to emulate operations that the GPU
109// hardware does automatically. This function can be used to check if
110// an opcode falls into this category.
111bool R600InstrInfo::isPlaceHolderOpcode(unsigned Opcode) const {
112 switch (Opcode) {
113 default: return false;
114 case AMDGPU::RETURN:
Tom Stellard75aadc22012-12-11 21:25:42 +0000115 return true;
116 }
117}
118
119bool R600InstrInfo::isReductionOp(unsigned Opcode) const {
Aaron Ballmanf04bbd82013-07-10 17:19:22 +0000120 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000121}
122
123bool R600InstrInfo::isCubeOp(unsigned Opcode) const {
124 switch(Opcode) {
125 default: return false;
126 case AMDGPU::CUBE_r600_pseudo:
127 case AMDGPU::CUBE_r600_real:
128 case AMDGPU::CUBE_eg_pseudo:
129 case AMDGPU::CUBE_eg_real:
130 return true;
131 }
132}
133
134bool R600InstrInfo::isALUInstr(unsigned Opcode) const {
135 unsigned TargetFlags = get(Opcode).TSFlags;
136
Tom Stellard5eb903d2013-06-28 15:46:53 +0000137 return (TargetFlags & R600_InstFlag::ALU_INST);
Tom Stellard75aadc22012-12-11 21:25:42 +0000138}
139
Tom Stellardc026e8b2013-06-28 15:47:08 +0000140bool R600InstrInfo::hasInstrModifiers(unsigned Opcode) const {
141 unsigned TargetFlags = get(Opcode).TSFlags;
142
143 return ((TargetFlags & R600_InstFlag::OP1) |
144 (TargetFlags & R600_InstFlag::OP2) |
145 (TargetFlags & R600_InstFlag::OP3));
146}
147
148bool R600InstrInfo::isLDSInstr(unsigned Opcode) const {
149 unsigned TargetFlags = get(Opcode).TSFlags;
150
151 return ((TargetFlags & R600_InstFlag::LDS_1A) |
Tom Stellardf3d166a2013-08-26 15:05:49 +0000152 (TargetFlags & R600_InstFlag::LDS_1A1D) |
153 (TargetFlags & R600_InstFlag::LDS_1A2D));
Tom Stellardc026e8b2013-06-28 15:47:08 +0000154}
155
Vincent Lejeunea4da6fb2013-10-01 19:32:58 +0000156bool R600InstrInfo::canBeConsideredALU(const MachineInstr *MI) const {
157 if (isALUInstr(MI->getOpcode()))
158 return true;
159 if (isVector(*MI) || isCubeOp(MI->getOpcode()))
160 return true;
161 switch (MI->getOpcode()) {
162 case AMDGPU::PRED_X:
163 case AMDGPU::INTERP_PAIR_XY:
164 case AMDGPU::INTERP_PAIR_ZW:
165 case AMDGPU::INTERP_VEC_LOAD:
166 case AMDGPU::COPY:
167 case AMDGPU::DOT_4:
168 return true;
169 default:
170 return false;
171 }
172}
173
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000174bool R600InstrInfo::isTransOnly(unsigned Opcode) const {
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000175 if (ST.hasCaymanISA())
176 return false;
177 return (get(Opcode).getSchedClass() == AMDGPU::Sched::TransALU);
Vincent Lejeune076c0b22013-04-30 00:14:17 +0000178}
179
180bool R600InstrInfo::isTransOnly(const MachineInstr *MI) const {
181 return isTransOnly(MI->getOpcode());
182}
183
Vincent Lejeune4d5c5e52013-09-04 19:53:30 +0000184bool R600InstrInfo::isVectorOnly(unsigned Opcode) const {
185 return (get(Opcode).getSchedClass() == AMDGPU::Sched::VecALU);
186}
187
188bool R600InstrInfo::isVectorOnly(const MachineInstr *MI) const {
189 return isVectorOnly(MI->getOpcode());
190}
191
Tom Stellard676c16d2013-08-16 01:11:51 +0000192bool R600InstrInfo::isExport(unsigned Opcode) const {
193 return (get(Opcode).TSFlags & R600_InstFlag::IS_EXPORT);
194}
195
Vincent Lejeunec2991642013-04-30 00:13:39 +0000196bool R600InstrInfo::usesVertexCache(unsigned Opcode) const {
Tom Stellardd93cede2013-05-06 17:50:57 +0000197 return ST.hasVertexCache() && IS_VTX(get(Opcode));
Vincent Lejeunec2991642013-04-30 00:13:39 +0000198}
199
200bool R600InstrInfo::usesVertexCache(const MachineInstr *MI) const {
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +0000201 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
202 return MFI->ShaderType != ShaderType::COMPUTE && usesVertexCache(MI->getOpcode());
Vincent Lejeunec2991642013-04-30 00:13:39 +0000203}
204
205bool R600InstrInfo::usesTextureCache(unsigned Opcode) const {
Tom Stellardd93cede2013-05-06 17:50:57 +0000206 return (!ST.hasVertexCache() && IS_VTX(get(Opcode))) || IS_TEX(get(Opcode));
Vincent Lejeunec2991642013-04-30 00:13:39 +0000207}
208
209bool R600InstrInfo::usesTextureCache(const MachineInstr *MI) const {
Vincent Lejeune3a8d78a2013-04-30 00:14:44 +0000210 const R600MachineFunctionInfo *MFI = MI->getParent()->getParent()->getInfo<R600MachineFunctionInfo>();
211 return (MFI->ShaderType == ShaderType::COMPUTE && usesVertexCache(MI->getOpcode())) ||
212 usesTextureCache(MI->getOpcode());
Vincent Lejeunec2991642013-04-30 00:13:39 +0000213}
214
Tom Stellardce540332013-06-28 15:46:59 +0000215bool R600InstrInfo::mustBeLastInClause(unsigned Opcode) const {
216 switch (Opcode) {
217 case AMDGPU::KILLGT:
218 case AMDGPU::GROUP_BARRIER:
219 return true;
220 default:
221 return false;
222 }
223}
224
Tom Stellard7f6fa4c2013-09-12 02:55:06 +0000225bool R600InstrInfo::readsLDSSrcReg(const MachineInstr *MI) const {
226 if (!isALUInstr(MI->getOpcode())) {
227 return false;
228 }
229 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
230 E = MI->operands_end(); I != E; ++I) {
231 if (!I->isReg() || !I->isUse() ||
232 TargetRegisterInfo::isVirtualRegister(I->getReg()))
233 continue;
234
235 if (AMDGPU::R600_LDS_SRC_REGRegClass.contains(I->getReg()))
236 return true;
237 }
238 return false;
239}
240
Tom Stellard84021442013-07-23 01:48:24 +0000241int R600InstrInfo::getSrcIdx(unsigned Opcode, unsigned SrcNum) const {
242 static const unsigned OpTable[] = {
243 AMDGPU::OpName::src0,
244 AMDGPU::OpName::src1,
245 AMDGPU::OpName::src2
246 };
247
248 assert (SrcNum < 3);
249 return getOperandIdx(Opcode, OpTable[SrcNum]);
250}
251
252#define SRC_SEL_ROWS 11
253int R600InstrInfo::getSelIdx(unsigned Opcode, unsigned SrcIdx) const {
254 static const unsigned SrcSelTable[SRC_SEL_ROWS][2] = {
255 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
256 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
257 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
258 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
259 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
260 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
261 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
262 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
263 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
264 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
265 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W}
266 };
267
268 for (unsigned i = 0; i < SRC_SEL_ROWS; ++i) {
269 if (getOperandIdx(Opcode, SrcSelTable[i][0]) == (int)SrcIdx) {
270 return getOperandIdx(Opcode, SrcSelTable[i][1]);
271 }
272 }
273 return -1;
274}
275#undef SRC_SEL_ROWS
276
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000277SmallVector<std::pair<MachineOperand *, int64_t>, 3>
278R600InstrInfo::getSrcs(MachineInstr *MI) const {
279 SmallVector<std::pair<MachineOperand *, int64_t>, 3> Result;
280
Vincent Lejeunec6896792013-06-04 23:17:15 +0000281 if (MI->getOpcode() == AMDGPU::DOT_4) {
Tom Stellard02661d92013-06-25 21:22:18 +0000282 static const unsigned OpTable[8][2] = {
283 {AMDGPU::OpName::src0_X, AMDGPU::OpName::src0_sel_X},
284 {AMDGPU::OpName::src0_Y, AMDGPU::OpName::src0_sel_Y},
285 {AMDGPU::OpName::src0_Z, AMDGPU::OpName::src0_sel_Z},
286 {AMDGPU::OpName::src0_W, AMDGPU::OpName::src0_sel_W},
287 {AMDGPU::OpName::src1_X, AMDGPU::OpName::src1_sel_X},
288 {AMDGPU::OpName::src1_Y, AMDGPU::OpName::src1_sel_Y},
289 {AMDGPU::OpName::src1_Z, AMDGPU::OpName::src1_sel_Z},
290 {AMDGPU::OpName::src1_W, AMDGPU::OpName::src1_sel_W},
Vincent Lejeunec6896792013-06-04 23:17:15 +0000291 };
292
293 for (unsigned j = 0; j < 8; j++) {
Tom Stellard02661d92013-06-25 21:22:18 +0000294 MachineOperand &MO = MI->getOperand(getOperandIdx(MI->getOpcode(),
295 OpTable[j][0]));
Vincent Lejeunec6896792013-06-04 23:17:15 +0000296 unsigned Reg = MO.getReg();
297 if (Reg == AMDGPU::ALU_CONST) {
Tom Stellard02661d92013-06-25 21:22:18 +0000298 unsigned Sel = MI->getOperand(getOperandIdx(MI->getOpcode(),
299 OpTable[j][1])).getImm();
Vincent Lejeunec6896792013-06-04 23:17:15 +0000300 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
301 continue;
302 }
303
304 }
305 return Result;
306 }
307
Tom Stellard02661d92013-06-25 21:22:18 +0000308 static const unsigned OpTable[3][2] = {
309 {AMDGPU::OpName::src0, AMDGPU::OpName::src0_sel},
310 {AMDGPU::OpName::src1, AMDGPU::OpName::src1_sel},
311 {AMDGPU::OpName::src2, AMDGPU::OpName::src2_sel},
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000312 };
313
314 for (unsigned j = 0; j < 3; j++) {
315 int SrcIdx = getOperandIdx(MI->getOpcode(), OpTable[j][0]);
316 if (SrcIdx < 0)
317 break;
318 MachineOperand &MO = MI->getOperand(SrcIdx);
319 unsigned Reg = MI->getOperand(SrcIdx).getReg();
320 if (Reg == AMDGPU::ALU_CONST) {
321 unsigned Sel = MI->getOperand(
322 getOperandIdx(MI->getOpcode(), OpTable[j][1])).getImm();
323 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Sel));
324 continue;
325 }
326 if (Reg == AMDGPU::ALU_LITERAL_X) {
327 unsigned Imm = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +0000328 getOperandIdx(MI->getOpcode(), AMDGPU::OpName::literal)).getImm();
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000329 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, Imm));
330 continue;
331 }
332 Result.push_back(std::pair<MachineOperand *, int64_t>(&MO, 0));
333 }
334 return Result;
335}
336
337std::vector<std::pair<int, unsigned> >
338R600InstrInfo::ExtractSrcs(MachineInstr *MI,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000339 const DenseMap<unsigned, unsigned> &PV,
340 unsigned &ConstCount) const {
341 ConstCount = 0;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000342 const SmallVector<std::pair<MachineOperand *, int64_t>, 3> Srcs = getSrcs(MI);
343 const std::pair<int, unsigned> DummyPair(-1, 0);
344 std::vector<std::pair<int, unsigned> > Result;
345 unsigned i = 0;
346 for (unsigned n = Srcs.size(); i < n; ++i) {
347 unsigned Reg = Srcs[i].first->getReg();
348 unsigned Index = RI.getEncodingValue(Reg) & 0xff;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000349 if (Reg == AMDGPU::OQAP) {
350 Result.push_back(std::pair<int, unsigned>(Index, 0));
351 }
Vincent Lejeune41d4cf22013-06-17 20:16:40 +0000352 if (PV.find(Reg) != PV.end()) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000353 // 255 is used to tells its a PS/PV reg
354 Result.push_back(std::pair<int, unsigned>(255, 0));
355 continue;
356 }
357 if (Index > 127) {
358 ConstCount++;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000359 Result.push_back(DummyPair);
360 continue;
361 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000362 unsigned Chan = RI.getHWRegChan(Reg);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000363 Result.push_back(std::pair<int, unsigned>(Index, Chan));
364 }
365 for (; i < 3; ++i)
366 Result.push_back(DummyPair);
367 return Result;
368}
369
370static std::vector<std::pair<int, unsigned> >
371Swizzle(std::vector<std::pair<int, unsigned> > Src,
372 R600InstrInfo::BankSwizzle Swz) {
Vincent Lejeune744efa42013-09-04 19:53:54 +0000373 if (Src[0] == Src[1])
374 Src[1].first = -1;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000375 switch (Swz) {
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000376 case R600InstrInfo::ALU_VEC_012_SCL_210:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000377 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000378 case R600InstrInfo::ALU_VEC_021_SCL_122:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000379 std::swap(Src[1], Src[2]);
380 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000381 case R600InstrInfo::ALU_VEC_102_SCL_221:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000382 std::swap(Src[0], Src[1]);
383 break;
Vincent Lejeunebb8a87212013-06-29 19:32:29 +0000384 case R600InstrInfo::ALU_VEC_120_SCL_212:
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000385 std::swap(Src[0], Src[1]);
386 std::swap(Src[0], Src[2]);
387 break;
388 case R600InstrInfo::ALU_VEC_201:
389 std::swap(Src[0], Src[2]);
390 std::swap(Src[0], Src[1]);
391 break;
392 case R600InstrInfo::ALU_VEC_210:
393 std::swap(Src[0], Src[2]);
394 break;
395 }
396 return Src;
397}
398
Vincent Lejeune77a83522013-06-29 19:32:43 +0000399static unsigned
400getTransSwizzle(R600InstrInfo::BankSwizzle Swz, unsigned Op) {
401 switch (Swz) {
402 case R600InstrInfo::ALU_VEC_012_SCL_210: {
403 unsigned Cycles[3] = { 2, 1, 0};
404 return Cycles[Op];
405 }
406 case R600InstrInfo::ALU_VEC_021_SCL_122: {
407 unsigned Cycles[3] = { 1, 2, 2};
408 return Cycles[Op];
409 }
410 case R600InstrInfo::ALU_VEC_120_SCL_212: {
411 unsigned Cycles[3] = { 2, 1, 2};
412 return Cycles[Op];
413 }
414 case R600InstrInfo::ALU_VEC_102_SCL_221: {
415 unsigned Cycles[3] = { 2, 2, 1};
416 return Cycles[Op];
417 }
418 default:
419 llvm_unreachable("Wrong Swizzle for Trans Slot");
420 return 0;
421 }
422}
423
424/// returns how many MIs (whose inputs are represented by IGSrcs) can be packed
425/// in the same Instruction Group while meeting read port limitations given a
426/// Swz swizzle sequence.
427unsigned R600InstrInfo::isLegalUpTo(
428 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
429 const std::vector<R600InstrInfo::BankSwizzle> &Swz,
430 const std::vector<std::pair<int, unsigned> > &TransSrcs,
431 R600InstrInfo::BankSwizzle TransSwz) const {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000432 int Vector[4][3];
433 memset(Vector, -1, sizeof(Vector));
Vincent Lejeune77a83522013-06-29 19:32:43 +0000434 for (unsigned i = 0, e = IGSrcs.size(); i < e; i++) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000435 const std::vector<std::pair<int, unsigned> > &Srcs =
436 Swizzle(IGSrcs[i], Swz[i]);
437 for (unsigned j = 0; j < 3; j++) {
438 const std::pair<int, unsigned> &Src = Srcs[j];
Vincent Lejeune77a83522013-06-29 19:32:43 +0000439 if (Src.first < 0 || Src.first == 255)
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000440 continue;
Tom Stellardc026e8b2013-06-28 15:47:08 +0000441 if (Src.first == GET_REG_INDEX(RI.getEncodingValue(AMDGPU::OQAP))) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000442 if (Swz[i] != R600InstrInfo::ALU_VEC_012_SCL_210 &&
443 Swz[i] != R600InstrInfo::ALU_VEC_021_SCL_122) {
Tom Stellardc026e8b2013-06-28 15:47:08 +0000444 // The value from output queue A (denoted by register OQAP) can
445 // only be fetched during the first cycle.
446 return false;
447 }
448 // OQAP does not count towards the normal read port restrictions
449 continue;
450 }
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000451 if (Vector[Src.second][j] < 0)
452 Vector[Src.second][j] = Src.first;
453 if (Vector[Src.second][j] != Src.first)
Vincent Lejeune77a83522013-06-29 19:32:43 +0000454 return i;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000455 }
456 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000457 // Now check Trans Alu
458 for (unsigned i = 0, e = TransSrcs.size(); i < e; ++i) {
459 const std::pair<int, unsigned> &Src = TransSrcs[i];
460 unsigned Cycle = getTransSwizzle(TransSwz, i);
461 if (Src.first < 0)
462 continue;
463 if (Src.first == 255)
464 continue;
465 if (Vector[Src.second][Cycle] < 0)
466 Vector[Src.second][Cycle] = Src.first;
467 if (Vector[Src.second][Cycle] != Src.first)
468 return IGSrcs.size() - 1;
469 }
470 return IGSrcs.size();
471}
472
473/// Given a swizzle sequence SwzCandidate and an index Idx, returns the next
474/// (in lexicographic term) swizzle sequence assuming that all swizzles after
475/// Idx can be skipped
476static bool
477NextPossibleSolution(
478 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
479 unsigned Idx) {
480 assert(Idx < SwzCandidate.size());
481 int ResetIdx = Idx;
482 while (ResetIdx > -1 && SwzCandidate[ResetIdx] == R600InstrInfo::ALU_VEC_210)
483 ResetIdx --;
484 for (unsigned i = ResetIdx + 1, e = SwzCandidate.size(); i < e; i++) {
485 SwzCandidate[i] = R600InstrInfo::ALU_VEC_012_SCL_210;
486 }
487 if (ResetIdx == -1)
488 return false;
Benjamin Kramer39690642013-06-29 20:04:19 +0000489 int NextSwizzle = SwzCandidate[ResetIdx] + 1;
490 SwzCandidate[ResetIdx] = (R600InstrInfo::BankSwizzle)NextSwizzle;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000491 return true;
492}
493
494/// Enumerate all possible Swizzle sequence to find one that can meet all
495/// read port requirements.
496bool R600InstrInfo::FindSwizzleForVectorSlot(
497 const std::vector<std::vector<std::pair<int, unsigned> > > &IGSrcs,
498 std::vector<R600InstrInfo::BankSwizzle> &SwzCandidate,
499 const std::vector<std::pair<int, unsigned> > &TransSrcs,
500 R600InstrInfo::BankSwizzle TransSwz) const {
501 unsigned ValidUpTo = 0;
502 do {
503 ValidUpTo = isLegalUpTo(IGSrcs, SwzCandidate, TransSrcs, TransSwz);
504 if (ValidUpTo == IGSrcs.size())
505 return true;
506 } while (NextPossibleSolution(SwzCandidate, ValidUpTo));
507 return false;
508}
509
510/// Instructions in Trans slot can't read gpr at cycle 0 if they also read
511/// a const, and can't read a gpr at cycle 1 if they read 2 const.
512static bool
513isConstCompatible(R600InstrInfo::BankSwizzle TransSwz,
514 const std::vector<std::pair<int, unsigned> > &TransOps,
515 unsigned ConstCount) {
Vincent Lejeune7e2c8322013-09-04 19:53:46 +0000516 // TransALU can't read 3 constants
517 if (ConstCount > 2)
518 return false;
Vincent Lejeune77a83522013-06-29 19:32:43 +0000519 for (unsigned i = 0, e = TransOps.size(); i < e; ++i) {
520 const std::pair<int, unsigned> &Src = TransOps[i];
521 unsigned Cycle = getTransSwizzle(TransSwz, i);
522 if (Src.first < 0)
523 continue;
524 if (ConstCount > 0 && Cycle == 0)
525 return false;
526 if (ConstCount > 1 && Cycle == 1)
527 return false;
528 }
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000529 return true;
530}
531
Tom Stellardc026e8b2013-06-28 15:47:08 +0000532bool
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000533R600InstrInfo::fitsReadPortLimitations(const std::vector<MachineInstr *> &IG,
Vincent Lejeune77a83522013-06-29 19:32:43 +0000534 const DenseMap<unsigned, unsigned> &PV,
535 std::vector<BankSwizzle> &ValidSwizzle,
536 bool isLastAluTrans)
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000537 const {
538 //Todo : support shared src0 - src1 operand
539
540 std::vector<std::vector<std::pair<int, unsigned> > > IGSrcs;
541 ValidSwizzle.clear();
Vincent Lejeune77a83522013-06-29 19:32:43 +0000542 unsigned ConstCount;
Vincent Lejeunea8a50242013-06-30 21:44:06 +0000543 BankSwizzle TransBS = ALU_VEC_012_SCL_210;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000544 for (unsigned i = 0, e = IG.size(); i < e; ++i) {
Vincent Lejeune77a83522013-06-29 19:32:43 +0000545 IGSrcs.push_back(ExtractSrcs(IG[i], PV, ConstCount));
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000546 unsigned Op = getOperandIdx(IG[i]->getOpcode(),
Tom Stellard02661d92013-06-25 21:22:18 +0000547 AMDGPU::OpName::bank_swizzle);
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000548 ValidSwizzle.push_back( (R600InstrInfo::BankSwizzle)
549 IG[i]->getOperand(Op).getImm());
550 }
Vincent Lejeune77a83522013-06-29 19:32:43 +0000551 std::vector<std::pair<int, unsigned> > TransOps;
552 if (!isLastAluTrans)
553 return FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps, TransBS);
554
555 TransOps = IGSrcs.back();
556 IGSrcs.pop_back();
557 ValidSwizzle.pop_back();
558
559 static const R600InstrInfo::BankSwizzle TransSwz[] = {
560 ALU_VEC_012_SCL_210,
561 ALU_VEC_021_SCL_122,
562 ALU_VEC_120_SCL_212,
563 ALU_VEC_102_SCL_221
564 };
565 for (unsigned i = 0; i < 4; i++) {
566 TransBS = TransSwz[i];
567 if (!isConstCompatible(TransBS, TransOps, ConstCount))
568 continue;
569 bool Result = FindSwizzleForVectorSlot(IGSrcs, ValidSwizzle, TransOps,
570 TransBS);
571 if (Result) {
572 ValidSwizzle.push_back(TransBS);
573 return true;
574 }
575 }
576
577 return false;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000578}
579
580
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000581bool
582R600InstrInfo::fitsConstReadLimitations(const std::vector<unsigned> &Consts)
583 const {
584 assert (Consts.size() <= 12 && "Too many operands in instructions group");
585 unsigned Pair1 = 0, Pair2 = 0;
586 for (unsigned i = 0, n = Consts.size(); i < n; ++i) {
587 unsigned ReadConstHalf = Consts[i] & 2;
588 unsigned ReadConstIndex = Consts[i] & (~3);
589 unsigned ReadHalfConst = ReadConstIndex | ReadConstHalf;
590 if (!Pair1) {
591 Pair1 = ReadHalfConst;
592 continue;
593 }
594 if (Pair1 == ReadHalfConst)
595 continue;
596 if (!Pair2) {
597 Pair2 = ReadHalfConst;
598 continue;
599 }
600 if (Pair2 != ReadHalfConst)
601 return false;
602 }
603 return true;
604}
605
606bool
Vincent Lejeune77a83522013-06-29 19:32:43 +0000607R600InstrInfo::fitsConstReadLimitations(const std::vector<MachineInstr *> &MIs)
608 const {
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000609 std::vector<unsigned> Consts;
Vincent Lejeunebb3f9312013-07-31 19:32:07 +0000610 SmallSet<int64_t, 4> Literals;
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000611 for (unsigned i = 0, n = MIs.size(); i < n; i++) {
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000612 MachineInstr *MI = MIs[i];
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000613 if (!isALUInstr(MI->getOpcode()))
614 continue;
615
Craig Topperb94011f2013-07-14 04:42:23 +0000616 const SmallVectorImpl<std::pair<MachineOperand *, int64_t> > &Srcs =
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000617 getSrcs(MI);
618
619 for (unsigned j = 0, e = Srcs.size(); j < e; j++) {
620 std::pair<MachineOperand *, unsigned> Src = Srcs[j];
Vincent Lejeunebb3f9312013-07-31 19:32:07 +0000621 if (Src.first->getReg() == AMDGPU::ALU_LITERAL_X)
622 Literals.insert(Src.second);
623 if (Literals.size() > 4)
624 return false;
Vincent Lejeune0fca91d2013-05-17 16:50:02 +0000625 if (Src.first->getReg() == AMDGPU::ALU_CONST)
626 Consts.push_back(Src.second);
627 if (AMDGPU::R600_KC0RegClass.contains(Src.first->getReg()) ||
628 AMDGPU::R600_KC1RegClass.contains(Src.first->getReg())) {
629 unsigned Index = RI.getEncodingValue(Src.first->getReg()) & 0xff;
630 unsigned Chan = RI.getHWRegChan(Src.first->getReg());
Vincent Lejeune147700b2013-04-30 00:14:27 +0000631 Consts.push_back((Index << 2) | Chan);
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000632 }
633 }
634 }
635 return fitsConstReadLimitations(Consts);
636}
637
Tom Stellard75aadc22012-12-11 21:25:42 +0000638DFAPacketizer *R600InstrInfo::CreateTargetScheduleState(const TargetMachine *TM,
639 const ScheduleDAG *DAG) const {
640 const InstrItineraryData *II = TM->getInstrItineraryData();
641 return TM->getSubtarget<AMDGPUSubtarget>().createDFAPacketizer(II);
642}
643
644static bool
645isPredicateSetter(unsigned Opcode) {
646 switch (Opcode) {
647 case AMDGPU::PRED_X:
648 return true;
649 default:
650 return false;
651 }
652}
653
654static MachineInstr *
655findFirstPredicateSetterFrom(MachineBasicBlock &MBB,
656 MachineBasicBlock::iterator I) {
657 while (I != MBB.begin()) {
658 --I;
659 MachineInstr *MI = I;
660 if (isPredicateSetter(MI->getOpcode()))
661 return MI;
662 }
663
664 return NULL;
665}
666
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000667static
668bool isJump(unsigned Opcode) {
669 return Opcode == AMDGPU::JUMP || Opcode == AMDGPU::JUMP_COND;
670}
671
Vincent Lejeune269708b2013-10-01 19:32:38 +0000672static bool isBranch(unsigned Opcode) {
673 return Opcode == AMDGPU::BRANCH || Opcode == AMDGPU::BRANCH_COND_i32 ||
674 Opcode == AMDGPU::BRANCH_COND_f32;
675}
676
Tom Stellard75aadc22012-12-11 21:25:42 +0000677bool
678R600InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
679 MachineBasicBlock *&TBB,
680 MachineBasicBlock *&FBB,
681 SmallVectorImpl<MachineOperand> &Cond,
682 bool AllowModify) const {
683 // Most of the following comes from the ARM implementation of AnalyzeBranch
684
685 // If the block has no terminators, it just falls into the block after it.
686 MachineBasicBlock::iterator I = MBB.end();
687 if (I == MBB.begin())
688 return false;
689 --I;
690 while (I->isDebugValue()) {
691 if (I == MBB.begin())
692 return false;
693 --I;
694 }
Vincent Lejeune269708b2013-10-01 19:32:38 +0000695 // AMDGPU::BRANCH* instructions are only available after isel and are not
696 // handled
697 if (isBranch(I->getOpcode()))
698 return true;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000699 if (!isJump(static_cast<MachineInstr *>(I)->getOpcode())) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000700 return false;
701 }
702
703 // Get the last instruction in the block.
704 MachineInstr *LastInst = I;
705
706 // If there is only one terminator instruction, process it.
707 unsigned LastOpc = LastInst->getOpcode();
708 if (I == MBB.begin() ||
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000709 !isJump(static_cast<MachineInstr *>(--I)->getOpcode())) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000710 if (LastOpc == AMDGPU::JUMP) {
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000711 TBB = LastInst->getOperand(0).getMBB();
712 return false;
713 } else if (LastOpc == AMDGPU::JUMP_COND) {
714 MachineInstr *predSet = I;
715 while (!isPredicateSetter(predSet->getOpcode())) {
716 predSet = --I;
Tom Stellard75aadc22012-12-11 21:25:42 +0000717 }
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000718 TBB = LastInst->getOperand(0).getMBB();
719 Cond.push_back(predSet->getOperand(1));
720 Cond.push_back(predSet->getOperand(2));
721 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
722 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000723 }
724 return true; // Can't handle indirect branch.
725 }
726
727 // Get the instruction before it if it is a terminator.
728 MachineInstr *SecondLastInst = I;
729 unsigned SecondLastOpc = SecondLastInst->getOpcode();
730
731 // If the block ends with a B and a Bcc, handle it.
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000732 if (SecondLastOpc == AMDGPU::JUMP_COND && LastOpc == AMDGPU::JUMP) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000733 MachineInstr *predSet = --I;
734 while (!isPredicateSetter(predSet->getOpcode())) {
735 predSet = --I;
736 }
737 TBB = SecondLastInst->getOperand(0).getMBB();
738 FBB = LastInst->getOperand(0).getMBB();
739 Cond.push_back(predSet->getOperand(1));
740 Cond.push_back(predSet->getOperand(2));
741 Cond.push_back(MachineOperand::CreateReg(AMDGPU::PRED_SEL_ONE, false));
742 return false;
743 }
744
745 // Otherwise, can't handle this.
746 return true;
747}
748
749int R600InstrInfo::getBranchInstr(const MachineOperand &op) const {
750 const MachineInstr *MI = op.getParent();
751
752 switch (MI->getDesc().OpInfo->RegClass) {
753 default: // FIXME: fallthrough??
754 case AMDGPU::GPRI32RegClassID: return AMDGPU::BRANCH_COND_i32;
755 case AMDGPU::GPRF32RegClassID: return AMDGPU::BRANCH_COND_f32;
756 };
757}
758
Vincent Lejeunece499742013-07-09 15:03:33 +0000759static
760MachineBasicBlock::iterator FindLastAluClause(MachineBasicBlock &MBB) {
761 for (MachineBasicBlock::reverse_iterator It = MBB.rbegin(), E = MBB.rend();
762 It != E; ++It) {
763 if (It->getOpcode() == AMDGPU::CF_ALU ||
764 It->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE)
765 return llvm::prior(It.base());
766 }
767 return MBB.end();
768}
769
Tom Stellard75aadc22012-12-11 21:25:42 +0000770unsigned
771R600InstrInfo::InsertBranch(MachineBasicBlock &MBB,
772 MachineBasicBlock *TBB,
773 MachineBasicBlock *FBB,
774 const SmallVectorImpl<MachineOperand> &Cond,
775 DebugLoc DL) const {
776 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
777
778 if (FBB == 0) {
779 if (Cond.empty()) {
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000780 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(TBB);
Tom Stellard75aadc22012-12-11 21:25:42 +0000781 return 1;
782 } else {
783 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
784 assert(PredSet && "No previous predicate !");
785 addFlag(PredSet, 0, MO_FLAG_PUSH);
786 PredSet->getOperand(2).setImm(Cond[1].getImm());
787
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000788 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellard75aadc22012-12-11 21:25:42 +0000789 .addMBB(TBB)
790 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunece499742013-07-09 15:03:33 +0000791 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
792 if (CfAlu == MBB.end())
793 return 1;
794 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
795 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellard75aadc22012-12-11 21:25:42 +0000796 return 1;
797 }
798 } else {
799 MachineInstr *PredSet = findFirstPredicateSetterFrom(MBB, MBB.end());
800 assert(PredSet && "No previous predicate !");
801 addFlag(PredSet, 0, MO_FLAG_PUSH);
802 PredSet->getOperand(2).setImm(Cond[1].getImm());
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000803 BuildMI(&MBB, DL, get(AMDGPU::JUMP_COND))
Tom Stellard75aadc22012-12-11 21:25:42 +0000804 .addMBB(TBB)
805 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000806 BuildMI(&MBB, DL, get(AMDGPU::JUMP)).addMBB(FBB);
Vincent Lejeunece499742013-07-09 15:03:33 +0000807 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
808 if (CfAlu == MBB.end())
809 return 2;
810 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU);
811 CfAlu->setDesc(get(AMDGPU::CF_ALU_PUSH_BEFORE));
Tom Stellard75aadc22012-12-11 21:25:42 +0000812 return 2;
813 }
814}
815
816unsigned
817R600InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
818
819 // Note : we leave PRED* instructions there.
820 // They may be needed when predicating instructions.
821
822 MachineBasicBlock::iterator I = MBB.end();
823
824 if (I == MBB.begin()) {
825 return 0;
826 }
827 --I;
828 switch (I->getOpcode()) {
829 default:
830 return 0;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000831 case AMDGPU::JUMP_COND: {
832 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
833 clearFlag(predSet, 0, MO_FLAG_PUSH);
834 I->eraseFromParent();
Vincent Lejeunece499742013-07-09 15:03:33 +0000835 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
836 if (CfAlu == MBB.end())
837 break;
838 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
839 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000840 break;
841 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000842 case AMDGPU::JUMP:
Tom Stellard75aadc22012-12-11 21:25:42 +0000843 I->eraseFromParent();
844 break;
845 }
846 I = MBB.end();
847
848 if (I == MBB.begin()) {
849 return 1;
850 }
851 --I;
852 switch (I->getOpcode()) {
853 // FIXME: only one case??
854 default:
855 return 1;
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000856 case AMDGPU::JUMP_COND: {
857 MachineInstr *predSet = findFirstPredicateSetterFrom(MBB, I);
858 clearFlag(predSet, 0, MO_FLAG_PUSH);
859 I->eraseFromParent();
Vincent Lejeunece499742013-07-09 15:03:33 +0000860 MachineBasicBlock::iterator CfAlu = FindLastAluClause(MBB);
861 if (CfAlu == MBB.end())
862 break;
863 assert (CfAlu->getOpcode() == AMDGPU::CF_ALU_PUSH_BEFORE);
864 CfAlu->setDesc(get(AMDGPU::CF_ALU));
Vincent Lejeunee5ecf102013-03-11 18:15:06 +0000865 break;
866 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000867 case AMDGPU::JUMP:
Tom Stellard75aadc22012-12-11 21:25:42 +0000868 I->eraseFromParent();
869 break;
870 }
871 return 2;
872}
873
874bool
875R600InstrInfo::isPredicated(const MachineInstr *MI) const {
876 int idx = MI->findFirstPredOperandIdx();
877 if (idx < 0)
878 return false;
879
880 unsigned Reg = MI->getOperand(idx).getReg();
881 switch (Reg) {
882 default: return false;
883 case AMDGPU::PRED_SEL_ONE:
884 case AMDGPU::PRED_SEL_ZERO:
885 case AMDGPU::PREDICATE_BIT:
886 return true;
887 }
888}
889
890bool
891R600InstrInfo::isPredicable(MachineInstr *MI) const {
892 // XXX: KILL* instructions can be predicated, but they must be the last
893 // instruction in a clause, so this means any instructions after them cannot
894 // be predicated. Until we have proper support for instruction clauses in the
895 // backend, we will mark KILL* instructions as unpredicable.
896
897 if (MI->getOpcode() == AMDGPU::KILLGT) {
898 return false;
Vincent Lejeunece499742013-07-09 15:03:33 +0000899 } else if (MI->getOpcode() == AMDGPU::CF_ALU) {
900 // If the clause start in the middle of MBB then the MBB has more
901 // than a single clause, unable to predicate several clauses.
902 if (MI->getParent()->begin() != MachineBasicBlock::iterator(MI))
903 return false;
904 // TODO: We don't support KC merging atm
905 if (MI->getOperand(3).getImm() != 0 || MI->getOperand(4).getImm() != 0)
906 return false;
907 return true;
Vincent Lejeunefe32bd82013-03-05 19:12:06 +0000908 } else if (isVector(*MI)) {
909 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000910 } else {
911 return AMDGPUInstrInfo::isPredicable(MI);
912 }
913}
914
915
916bool
917R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
918 unsigned NumCyles,
919 unsigned ExtraPredCycles,
920 const BranchProbability &Probability) const{
921 return true;
922}
923
924bool
925R600InstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
926 unsigned NumTCycles,
927 unsigned ExtraTCycles,
928 MachineBasicBlock &FMBB,
929 unsigned NumFCycles,
930 unsigned ExtraFCycles,
931 const BranchProbability &Probability) const {
932 return true;
933}
934
935bool
936R600InstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
937 unsigned NumCyles,
938 const BranchProbability &Probability)
939 const {
940 return true;
941}
942
943bool
944R600InstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
945 MachineBasicBlock &FMBB) const {
946 return false;
947}
948
949
950bool
951R600InstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
952 MachineOperand &MO = Cond[1];
953 switch (MO.getImm()) {
954 case OPCODE_IS_ZERO_INT:
955 MO.setImm(OPCODE_IS_NOT_ZERO_INT);
956 break;
957 case OPCODE_IS_NOT_ZERO_INT:
958 MO.setImm(OPCODE_IS_ZERO_INT);
959 break;
960 case OPCODE_IS_ZERO:
961 MO.setImm(OPCODE_IS_NOT_ZERO);
962 break;
963 case OPCODE_IS_NOT_ZERO:
964 MO.setImm(OPCODE_IS_ZERO);
965 break;
966 default:
967 return true;
968 }
969
970 MachineOperand &MO2 = Cond[2];
971 switch (MO2.getReg()) {
972 case AMDGPU::PRED_SEL_ZERO:
973 MO2.setReg(AMDGPU::PRED_SEL_ONE);
974 break;
975 case AMDGPU::PRED_SEL_ONE:
976 MO2.setReg(AMDGPU::PRED_SEL_ZERO);
977 break;
978 default:
979 return true;
980 }
981 return false;
982}
983
984bool
985R600InstrInfo::DefinesPredicate(MachineInstr *MI,
986 std::vector<MachineOperand> &Pred) const {
987 return isPredicateSetter(MI->getOpcode());
988}
989
990
991bool
992R600InstrInfo::SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
993 const SmallVectorImpl<MachineOperand> &Pred2) const {
994 return false;
995}
996
997
998bool
999R600InstrInfo::PredicateInstruction(MachineInstr *MI,
1000 const SmallVectorImpl<MachineOperand> &Pred) const {
1001 int PIdx = MI->findFirstPredOperandIdx();
1002
Vincent Lejeunece499742013-07-09 15:03:33 +00001003 if (MI->getOpcode() == AMDGPU::CF_ALU) {
1004 MI->getOperand(8).setImm(0);
1005 return true;
1006 }
1007
Tom Stellard75aadc22012-12-11 21:25:42 +00001008 if (PIdx != -1) {
1009 MachineOperand &PMO = MI->getOperand(PIdx);
1010 PMO.setReg(Pred[2].getReg());
NAKAMURA Takumi2a0b40f2012-12-20 00:22:11 +00001011 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
1012 MIB.addReg(AMDGPU::PREDICATE_BIT, RegState::Implicit);
Tom Stellard75aadc22012-12-11 21:25:42 +00001013 return true;
1014 }
1015
1016 return false;
1017}
1018
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00001019unsigned int R600InstrInfo::getPredicationCost(const MachineInstr *) const {
1020 return 2;
1021}
1022
Tom Stellard75aadc22012-12-11 21:25:42 +00001023unsigned int R600InstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1024 const MachineInstr *MI,
1025 unsigned *PredCost) const {
1026 if (PredCost)
1027 *PredCost = 2;
1028 return 2;
1029}
1030
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001031int R600InstrInfo::getIndirectIndexBegin(const MachineFunction &MF) const {
1032 const MachineRegisterInfo &MRI = MF.getRegInfo();
1033 const MachineFrameInfo *MFI = MF.getFrameInfo();
1034 int Offset = 0;
1035
1036 if (MFI->getNumObjects() == 0) {
1037 return -1;
1038 }
1039
1040 if (MRI.livein_empty()) {
1041 return 0;
1042 }
1043
1044 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1045 LE = MRI.livein_end();
1046 LI != LE; ++LI) {
1047 Offset = std::max(Offset,
1048 GET_REG_INDEX(RI.getEncodingValue(LI->first)));
1049 }
1050
1051 return Offset + 1;
1052}
1053
1054int R600InstrInfo::getIndirectIndexEnd(const MachineFunction &MF) const {
1055 int Offset = 0;
1056 const MachineFrameInfo *MFI = MF.getFrameInfo();
1057
1058 // Variable sized objects are not supported
1059 assert(!MFI->hasVarSizedObjects());
1060
1061 if (MFI->getNumObjects() == 0) {
1062 return -1;
1063 }
1064
1065 Offset = TM.getFrameLowering()->getFrameIndexOffset(MF, -1);
1066
1067 return getIndirectIndexBegin(MF) + Offset;
1068}
1069
1070std::vector<unsigned> R600InstrInfo::getIndirectReservedRegs(
1071 const MachineFunction &MF) const {
1072 const AMDGPUFrameLowering *TFL =
1073 static_cast<const AMDGPUFrameLowering*>(TM.getFrameLowering());
1074 std::vector<unsigned> Regs;
1075
1076 unsigned StackWidth = TFL->getStackWidth(MF);
1077 int End = getIndirectIndexEnd(MF);
1078
1079 if (End == -1) {
1080 return Regs;
1081 }
1082
1083 for (int Index = getIndirectIndexBegin(MF); Index <= End; ++Index) {
1084 unsigned SuperReg = AMDGPU::R600_Reg128RegClass.getRegister(Index);
1085 Regs.push_back(SuperReg);
1086 for (unsigned Chan = 0; Chan < StackWidth; ++Chan) {
1087 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister((4 * Index) + Chan);
1088 Regs.push_back(Reg);
1089 }
1090 }
1091 return Regs;
1092}
1093
1094unsigned R600InstrInfo::calculateIndirectAddress(unsigned RegIndex,
1095 unsigned Channel) const {
1096 // XXX: Remove when we support a stack width > 2
1097 assert(Channel == 0);
1098 return RegIndex;
1099}
1100
1101const TargetRegisterClass * R600InstrInfo::getIndirectAddrStoreRegClass(
1102 unsigned SourceReg) const {
1103 return &AMDGPU::R600_TReg32RegClass;
1104}
1105
1106const TargetRegisterClass *R600InstrInfo::getIndirectAddrLoadRegClass() const {
1107 return &AMDGPU::TRegMemRegClass;
1108}
1109
1110MachineInstrBuilder R600InstrInfo::buildIndirectWrite(MachineBasicBlock *MBB,
1111 MachineBasicBlock::iterator I,
1112 unsigned ValueReg, unsigned Address,
1113 unsigned OffsetReg) const {
1114 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1115 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1116 AMDGPU::AR_X, OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001117 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001118
1119 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1120 AddrReg, ValueReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001121 .addReg(AMDGPU::AR_X,
1122 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001123 setImmOperand(Mov, AMDGPU::OpName::dst_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001124 return Mov;
1125}
1126
1127MachineInstrBuilder R600InstrInfo::buildIndirectRead(MachineBasicBlock *MBB,
1128 MachineBasicBlock::iterator I,
1129 unsigned ValueReg, unsigned Address,
1130 unsigned OffsetReg) const {
1131 unsigned AddrReg = AMDGPU::R600_AddrRegClass.getRegister(Address);
1132 MachineInstr *MOVA = buildDefaultInstruction(*MBB, I, AMDGPU::MOVA_INT_eg,
1133 AMDGPU::AR_X,
1134 OffsetReg);
Tom Stellard02661d92013-06-25 21:22:18 +00001135 setImmOperand(MOVA, AMDGPU::OpName::write, 0);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001136 MachineInstrBuilder Mov = buildDefaultInstruction(*MBB, I, AMDGPU::MOV,
1137 ValueReg,
1138 AddrReg)
Tom Stellardaad53762013-06-05 03:43:06 +00001139 .addReg(AMDGPU::AR_X,
1140 RegState::Implicit | RegState::Kill);
Tom Stellard02661d92013-06-25 21:22:18 +00001141 setImmOperand(Mov, AMDGPU::OpName::src0_rel, 1);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001142
1143 return Mov;
1144}
1145
1146const TargetRegisterClass *R600InstrInfo::getSuperIndirectRegClass() const {
1147 return &AMDGPU::IndirectRegRegClass;
1148}
1149
Vincent Lejeune80031d9f2013-04-03 16:49:34 +00001150unsigned R600InstrInfo::getMaxAlusPerClause() const {
1151 return 115;
1152}
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00001153
Tom Stellard75aadc22012-12-11 21:25:42 +00001154MachineInstrBuilder R600InstrInfo::buildDefaultInstruction(MachineBasicBlock &MBB,
1155 MachineBasicBlock::iterator I,
1156 unsigned Opcode,
1157 unsigned DstReg,
1158 unsigned Src0Reg,
1159 unsigned Src1Reg) const {
1160 MachineInstrBuilder MIB = BuildMI(MBB, I, MBB.findDebugLoc(I), get(Opcode),
1161 DstReg); // $dst
1162
1163 if (Src1Reg) {
1164 MIB.addImm(0) // $update_exec_mask
1165 .addImm(0); // $update_predicate
1166 }
1167 MIB.addImm(1) // $write
1168 .addImm(0) // $omod
1169 .addImm(0) // $dst_rel
1170 .addImm(0) // $dst_clamp
1171 .addReg(Src0Reg) // $src0
1172 .addImm(0) // $src0_neg
1173 .addImm(0) // $src0_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001174 .addImm(0) // $src0_abs
1175 .addImm(-1); // $src0_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001176
1177 if (Src1Reg) {
1178 MIB.addReg(Src1Reg) // $src1
1179 .addImm(0) // $src1_neg
1180 .addImm(0) // $src1_rel
Tom Stellard365366f2013-01-23 02:09:06 +00001181 .addImm(0) // $src1_abs
1182 .addImm(-1); // $src1_sel
Tom Stellard75aadc22012-12-11 21:25:42 +00001183 }
1184
1185 //XXX: The r600g finalizer expects this to be 1, once we've moved the
1186 //scheduling to the backend, we can change the default to 0.
1187 MIB.addImm(1) // $last
1188 .addReg(AMDGPU::PRED_SEL_OFF) // $pred_sel
Vincent Lejeune22c42482013-04-30 00:14:08 +00001189 .addImm(0) // $literal
1190 .addImm(0); // $bank_swizzle
Tom Stellard75aadc22012-12-11 21:25:42 +00001191
1192 return MIB;
1193}
1194
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001195#define OPERAND_CASE(Label) \
1196 case Label: { \
Tom Stellard02661d92013-06-25 21:22:18 +00001197 static const unsigned Ops[] = \
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001198 { \
1199 Label##_X, \
1200 Label##_Y, \
1201 Label##_Z, \
1202 Label##_W \
1203 }; \
1204 return Ops[Slot]; \
1205 }
1206
Tom Stellard02661d92013-06-25 21:22:18 +00001207static unsigned getSlotedOps(unsigned Op, unsigned Slot) {
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001208 switch (Op) {
Tom Stellard02661d92013-06-25 21:22:18 +00001209 OPERAND_CASE(AMDGPU::OpName::update_exec_mask)
1210 OPERAND_CASE(AMDGPU::OpName::update_pred)
1211 OPERAND_CASE(AMDGPU::OpName::write)
1212 OPERAND_CASE(AMDGPU::OpName::omod)
1213 OPERAND_CASE(AMDGPU::OpName::dst_rel)
1214 OPERAND_CASE(AMDGPU::OpName::clamp)
1215 OPERAND_CASE(AMDGPU::OpName::src0)
1216 OPERAND_CASE(AMDGPU::OpName::src0_neg)
1217 OPERAND_CASE(AMDGPU::OpName::src0_rel)
1218 OPERAND_CASE(AMDGPU::OpName::src0_abs)
1219 OPERAND_CASE(AMDGPU::OpName::src0_sel)
1220 OPERAND_CASE(AMDGPU::OpName::src1)
1221 OPERAND_CASE(AMDGPU::OpName::src1_neg)
1222 OPERAND_CASE(AMDGPU::OpName::src1_rel)
1223 OPERAND_CASE(AMDGPU::OpName::src1_abs)
1224 OPERAND_CASE(AMDGPU::OpName::src1_sel)
1225 OPERAND_CASE(AMDGPU::OpName::pred_sel)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001226 default:
1227 llvm_unreachable("Wrong Operand");
1228 }
1229}
1230
1231#undef OPERAND_CASE
1232
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001233MachineInstr *R600InstrInfo::buildSlotOfVectorInstruction(
1234 MachineBasicBlock &MBB, MachineInstr *MI, unsigned Slot, unsigned DstReg)
1235 const {
1236 assert (MI->getOpcode() == AMDGPU::DOT_4 && "Not Implemented");
1237 unsigned Opcode;
1238 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +00001239 if (ST.getGeneration() <= AMDGPUSubtarget::R700)
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001240 Opcode = AMDGPU::DOT4_r600;
1241 else
1242 Opcode = AMDGPU::DOT4_eg;
1243 MachineBasicBlock::iterator I = MI;
1244 MachineOperand &Src0 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001245 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src0, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001246 MachineOperand &Src1 = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001247 getOperandIdx(MI->getOpcode(), getSlotedOps(AMDGPU::OpName::src1, Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001248 MachineInstr *MIB = buildDefaultInstruction(
1249 MBB, I, Opcode, DstReg, Src0.getReg(), Src1.getReg());
Tom Stellard02661d92013-06-25 21:22:18 +00001250 static const unsigned Operands[14] = {
1251 AMDGPU::OpName::update_exec_mask,
1252 AMDGPU::OpName::update_pred,
1253 AMDGPU::OpName::write,
1254 AMDGPU::OpName::omod,
1255 AMDGPU::OpName::dst_rel,
1256 AMDGPU::OpName::clamp,
1257 AMDGPU::OpName::src0_neg,
1258 AMDGPU::OpName::src0_rel,
1259 AMDGPU::OpName::src0_abs,
1260 AMDGPU::OpName::src0_sel,
1261 AMDGPU::OpName::src1_neg,
1262 AMDGPU::OpName::src1_rel,
1263 AMDGPU::OpName::src1_abs,
1264 AMDGPU::OpName::src1_sel,
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001265 };
1266
1267 for (unsigned i = 0; i < 14; i++) {
1268 MachineOperand &MO = MI->getOperand(
Tom Stellard02661d92013-06-25 21:22:18 +00001269 getOperandIdx(MI->getOpcode(), getSlotedOps(Operands[i], Slot)));
Vincent Lejeune519f21e2013-05-17 16:50:32 +00001270 assert (MO.isImm());
1271 setImmOperand(MIB, Operands[i], MO.getImm());
1272 }
1273 MIB->getOperand(20).setImm(0);
1274 return MIB;
1275}
1276
Tom Stellard75aadc22012-12-11 21:25:42 +00001277MachineInstr *R600InstrInfo::buildMovImm(MachineBasicBlock &BB,
1278 MachineBasicBlock::iterator I,
1279 unsigned DstReg,
1280 uint64_t Imm) const {
1281 MachineInstr *MovImm = buildDefaultInstruction(BB, I, AMDGPU::MOV, DstReg,
1282 AMDGPU::ALU_LITERAL_X);
Tom Stellard02661d92013-06-25 21:22:18 +00001283 setImmOperand(MovImm, AMDGPU::OpName::literal, Imm);
Tom Stellard75aadc22012-12-11 21:25:42 +00001284 return MovImm;
1285}
1286
Tom Stellard02661d92013-06-25 21:22:18 +00001287int R600InstrInfo::getOperandIdx(const MachineInstr &MI, unsigned Op) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001288 return getOperandIdx(MI.getOpcode(), Op);
1289}
1290
Tom Stellard02661d92013-06-25 21:22:18 +00001291int R600InstrInfo::getOperandIdx(unsigned Opcode, unsigned Op) const {
1292 return AMDGPU::getNamedOperandIdx(Opcode, Op);
Vincent Lejeunec6896792013-06-04 23:17:15 +00001293}
1294
Tom Stellard02661d92013-06-25 21:22:18 +00001295void R600InstrInfo::setImmOperand(MachineInstr *MI, unsigned Op,
Tom Stellard75aadc22012-12-11 21:25:42 +00001296 int64_t Imm) const {
1297 int Idx = getOperandIdx(*MI, Op);
1298 assert(Idx != -1 && "Operand not supported for this instruction.");
1299 assert(MI->getOperand(Idx).isImm());
1300 MI->getOperand(Idx).setImm(Imm);
1301}
1302
1303//===----------------------------------------------------------------------===//
1304// Instruction flag getters/setters
1305//===----------------------------------------------------------------------===//
1306
1307bool R600InstrInfo::hasFlagOperand(const MachineInstr &MI) const {
1308 return GET_FLAG_OPERAND_IDX(get(MI.getOpcode()).TSFlags) != 0;
1309}
1310
1311MachineOperand &R600InstrInfo::getFlagOp(MachineInstr *MI, unsigned SrcIdx,
1312 unsigned Flag) const {
1313 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1314 int FlagIndex = 0;
1315 if (Flag != 0) {
1316 // If we pass something other than the default value of Flag to this
1317 // function, it means we are want to set a flag on an instruction
1318 // that uses native encoding.
1319 assert(HAS_NATIVE_OPERANDS(TargetFlags));
1320 bool IsOP3 = (TargetFlags & R600_InstFlag::OP3) == R600_InstFlag::OP3;
1321 switch (Flag) {
1322 case MO_FLAG_CLAMP:
Tom Stellard02661d92013-06-25 21:22:18 +00001323 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::clamp);
Tom Stellard75aadc22012-12-11 21:25:42 +00001324 break;
1325 case MO_FLAG_MASK:
Tom Stellard02661d92013-06-25 21:22:18 +00001326 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::write);
Tom Stellard75aadc22012-12-11 21:25:42 +00001327 break;
1328 case MO_FLAG_NOT_LAST:
1329 case MO_FLAG_LAST:
Tom Stellard02661d92013-06-25 21:22:18 +00001330 FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::last);
Tom Stellard75aadc22012-12-11 21:25:42 +00001331 break;
1332 case MO_FLAG_NEG:
1333 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001334 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_neg); break;
1335 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_neg); break;
1336 case 2: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src2_neg); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001337 }
1338 break;
1339
1340 case MO_FLAG_ABS:
1341 assert(!IsOP3 && "Cannot set absolute value modifier for OP3 "
1342 "instructions.");
Tom Stellard6975d352012-12-13 19:38:52 +00001343 (void)IsOP3;
Tom Stellard75aadc22012-12-11 21:25:42 +00001344 switch (SrcIdx) {
Tom Stellard02661d92013-06-25 21:22:18 +00001345 case 0: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src0_abs); break;
1346 case 1: FlagIndex = getOperandIdx(*MI, AMDGPU::OpName::src1_abs); break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001347 }
1348 break;
1349
1350 default:
1351 FlagIndex = -1;
1352 break;
1353 }
1354 assert(FlagIndex != -1 && "Flag not supported for this instruction");
1355 } else {
1356 FlagIndex = GET_FLAG_OPERAND_IDX(TargetFlags);
1357 assert(FlagIndex != 0 &&
1358 "Instruction flags not supported for this instruction");
1359 }
1360
1361 MachineOperand &FlagOp = MI->getOperand(FlagIndex);
1362 assert(FlagOp.isImm());
1363 return FlagOp;
1364}
1365
1366void R600InstrInfo::addFlag(MachineInstr *MI, unsigned Operand,
1367 unsigned Flag) const {
1368 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1369 if (Flag == 0) {
1370 return;
1371 }
1372 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1373 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1374 if (Flag == MO_FLAG_NOT_LAST) {
1375 clearFlag(MI, Operand, MO_FLAG_LAST);
1376 } else if (Flag == MO_FLAG_MASK) {
1377 clearFlag(MI, Operand, Flag);
1378 } else {
1379 FlagOp.setImm(1);
1380 }
1381 } else {
1382 MachineOperand &FlagOp = getFlagOp(MI, Operand);
1383 FlagOp.setImm(FlagOp.getImm() | (Flag << (NUM_MO_FLAGS * Operand)));
1384 }
1385}
1386
1387void R600InstrInfo::clearFlag(MachineInstr *MI, unsigned Operand,
1388 unsigned Flag) const {
1389 unsigned TargetFlags = get(MI->getOpcode()).TSFlags;
1390 if (HAS_NATIVE_OPERANDS(TargetFlags)) {
1391 MachineOperand &FlagOp = getFlagOp(MI, Operand, Flag);
1392 FlagOp.setImm(0);
1393 } else {
1394 MachineOperand &FlagOp = getFlagOp(MI);
1395 unsigned InstFlags = FlagOp.getImm();
1396 InstFlags &= ~(Flag << (NUM_MO_FLAGS * Operand));
1397 FlagOp.setImm(InstFlags);
1398 }
1399}