blob: 9a5e9e6147af56d179faec53f2503b5adbc916db [file] [log] [blame]
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001//===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
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 Defines an instruction selector for the AMDGPU target.
12//
13//===----------------------------------------------------------------------===//
14#include "AMDGPUInstrInfo.h"
15#include "AMDGPUISelLowering.h" // For AMDGPUISD
16#include "AMDGPURegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000017#include "R600InstrInfo.h"
Christian Konigd3b55092013-02-26 17:52:23 +000018#include "SIISelLowering.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000019#include "llvm/ADT/ValueMap.h"
Tom Stellard8a72c732013-06-03 17:39:46 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000022#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000023#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Support/Compiler.h"
25#include <list>
26#include <queue>
27
28using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Instruction Selector Implementation
32//===----------------------------------------------------------------------===//
33
34namespace {
35/// AMDGPU specific code to select AMDGPU machine instructions for
36/// SelectionDAG operations.
37class AMDGPUDAGToDAGISel : public SelectionDAGISel {
38 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
39 // make the right decision when generating code for different targets.
40 const AMDGPUSubtarget &Subtarget;
41public:
42 AMDGPUDAGToDAGISel(TargetMachine &TM);
43 virtual ~AMDGPUDAGToDAGISel();
44
45 SDNode *Select(SDNode *N);
46 virtual const char *getPassName() const;
Christian Konigc018eca2013-02-26 17:52:16 +000047 virtual void PostprocessISelDAG();
Tom Stellardf98f2ce2012-12-11 21:25:42 +000048
49private:
50 inline SDValue getSmallIPtrImm(unsigned Imm);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000051 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
52 const R600InstrInfo *TII, std::vector<unsigned> Cst);
Tom Stellard9f7818d2013-01-23 02:09:06 +000053 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000054 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000055
56 // Complex pattern selectors
57 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
58 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
59 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
60
61 static bool checkType(const Value *ptr, unsigned int addrspace);
62 static const Value *getBasePointerValue(const Value *V);
63
64 static bool isGlobalStore(const StoreSDNode *N);
65 static bool isPrivateStore(const StoreSDNode *N);
66 static bool isLocalStore(const StoreSDNode *N);
67 static bool isRegionStore(const StoreSDNode *N);
68
69 static bool isCPLoad(const LoadSDNode *N);
70 static bool isConstantLoad(const LoadSDNode *N, int cbID);
71 static bool isGlobalLoad(const LoadSDNode *N);
72 static bool isParamLoad(const LoadSDNode *N);
73 static bool isPrivateLoad(const LoadSDNode *N);
74 static bool isLocalLoad(const LoadSDNode *N);
75 static bool isRegionLoad(const LoadSDNode *N);
76
Tom Stellard9f7818d2013-01-23 02:09:06 +000077 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
78 bool SelectGlobalValueVariableOffset(SDValue Addr,
79 SDValue &BaseReg, SDValue& Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000080 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardc0b0c672013-02-06 17:32:29 +000081 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000082
83 // Include the pieces autogenerated from the target description.
84#include "AMDGPUGenDAGISel.inc"
85};
86} // end anonymous namespace
87
88/// \brief This pass converts a legalized DAG into a AMDGPU-specific
89// DAG, ready for instruction scheduling.
90FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
91 ) {
92 return new AMDGPUDAGToDAGISel(TM);
93}
94
95AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
96 )
97 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
98}
99
100AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
101}
102
103SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
104 return CurDAG->getTargetConstant(Imm, MVT::i32);
105}
106
107bool AMDGPUDAGToDAGISel::SelectADDRParam(
108 SDValue Addr, SDValue& R1, SDValue& R2) {
109
110 if (Addr.getOpcode() == ISD::FrameIndex) {
111 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
112 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
113 R2 = CurDAG->getTargetConstant(0, MVT::i32);
114 } else {
115 R1 = Addr;
116 R2 = CurDAG->getTargetConstant(0, MVT::i32);
117 }
118 } else if (Addr.getOpcode() == ISD::ADD) {
119 R1 = Addr.getOperand(0);
120 R2 = Addr.getOperand(1);
121 } else {
122 R1 = Addr;
123 R2 = CurDAG->getTargetConstant(0, MVT::i32);
124 }
125 return true;
126}
127
128bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
129 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
130 Addr.getOpcode() == ISD::TargetGlobalAddress) {
131 return false;
132 }
133 return SelectADDRParam(Addr, R1, R2);
134}
135
136
137bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
138 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
139 Addr.getOpcode() == ISD::TargetGlobalAddress) {
140 return false;
141 }
142
143 if (Addr.getOpcode() == ISD::FrameIndex) {
144 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
145 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
146 R2 = CurDAG->getTargetConstant(0, MVT::i64);
147 } else {
148 R1 = Addr;
149 R2 = CurDAG->getTargetConstant(0, MVT::i64);
150 }
151 } else if (Addr.getOpcode() == ISD::ADD) {
152 R1 = Addr.getOperand(0);
153 R2 = Addr.getOperand(1);
154 } else {
155 R1 = Addr;
156 R2 = CurDAG->getTargetConstant(0, MVT::i64);
157 }
158 return true;
159}
160
161SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
162 unsigned int Opc = N->getOpcode();
163 if (N->isMachineOpcode()) {
164 return NULL; // Already selected.
165 }
166 switch (Opc) {
167 default: break;
Vincent Lejeunecae68012013-03-05 15:04:49 +0000168 case ISD::BUILD_VECTOR: {
169 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000170 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Vincent Lejeunecae68012013-03-05 15:04:49 +0000171 break;
172 }
173 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
174 // that adds a 128 bits reg copy when going through TwoAddressInstructions
175 // pass. We want to avoid 128 bits copies as much as possible because they
176 // can't be bundled by our scheduler.
177 SDValue RegSeqArgs[9] = {
178 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
179 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
180 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
181 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
182 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
183 };
184 bool IsRegSeq = true;
185 for (unsigned i = 0; i < N->getNumOperands(); i++) {
186 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
187 IsRegSeq = false;
188 break;
189 }
190 RegSeqArgs[2 * i + 1] = N->getOperand(i);
191 }
192 if (!IsRegSeq)
193 break;
194 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
195 RegSeqArgs, 2 * N->getNumOperands() + 1);
196 }
Tom Stellard17ea10c2013-04-05 23:31:51 +0000197 case ISD::BUILD_PAIR: {
198 SDValue RC, SubReg0, SubReg1;
199 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000200 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard17ea10c2013-04-05 23:31:51 +0000201 break;
202 }
203 if (N->getValueType(0) == MVT::i128) {
204 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
205 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
206 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
207 } else if (N->getValueType(0) == MVT::i64) {
208 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
209 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
210 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
211 } else {
212 llvm_unreachable("Unhandled value type for BUILD_PAIR");
213 }
214 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
215 N->getOperand(1), SubReg1 };
216 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000217 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard17ea10c2013-04-05 23:31:51 +0000218 }
219
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000220 case ISD::ConstantFP:
221 case ISD::Constant: {
222 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
223 // XXX: Custom immediate lowering not implemented yet. Instead we use
224 // pseudo instructions defined in SIInstructions.td
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000225 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000226 break;
227 }
228 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
229
230 uint64_t ImmValue = 0;
231 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
232
233 if (N->getOpcode() == ISD::ConstantFP) {
234 // XXX: 64-bit Immediates not supported yet
235 assert(N->getValueType(0) != MVT::f64);
236
237 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
238 APFloat Value = C->getValueAPF();
239 float FloatValue = Value.convertToFloat();
240 if (FloatValue == 0.0) {
241 ImmReg = AMDGPU::ZERO;
242 } else if (FloatValue == 0.5) {
243 ImmReg = AMDGPU::HALF;
244 } else if (FloatValue == 1.0) {
245 ImmReg = AMDGPU::ONE;
246 } else {
247 ImmValue = Value.bitcastToAPInt().getZExtValue();
248 }
249 } else {
250 // XXX: 64-bit Immediates not supported yet
251 assert(N->getValueType(0) != MVT::i64);
252
253 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
254 if (C->getZExtValue() == 0) {
255 ImmReg = AMDGPU::ZERO;
256 } else if (C->getZExtValue() == 1) {
257 ImmReg = AMDGPU::ONE_INT;
258 } else {
259 ImmValue = C->getZExtValue();
260 }
261 }
262
263 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
264 Use != SDNode::use_end(); Use = Next) {
265 Next = llvm::next(Use);
266 std::vector<SDValue> Ops;
267 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
268 Ops.push_back(Use->getOperand(i));
269 }
270
271 if (!Use->isMachineOpcode()) {
272 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
273 // We can only use literal constants (e.g. AMDGPU::ZERO,
274 // AMDGPU::ONE, etc) in machine opcodes.
275 continue;
276 }
277 } else {
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000278 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
279 (TII->get(Use->getMachineOpcode()).TSFlags &
280 R600_InstFlag::VECTOR)) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000281 continue;
282 }
283
284 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
285 assert(ImmIdx != -1);
286
287 // subtract one from ImmIdx, because the DST operand is usually index
288 // 0 for MachineInstrs, but we have no DST in the Ops vector.
289 ImmIdx--;
290
291 // Check that we aren't already using an immediate.
292 // XXX: It's possible for an instruction to have more than one
293 // immediate operand, but this is not supported yet.
294 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
295 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
296 assert(C);
297
298 if (C->getZExtValue() != 0) {
299 // This instruction is already using an immediate.
300 continue;
301 }
302
303 // Set the immediate value
304 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
305 }
306 }
307 // Set the immediate register
308 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
309
310 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
311 }
312 break;
313 }
314 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000315 SDNode *Result = SelectCode(N);
316
317 // Fold operands of selected node
318
319 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000320 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard9f7818d2013-01-23 02:09:06 +0000321 const R600InstrInfo *TII =
322 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000323 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
324 bool IsModified = false;
325 do {
326 std::vector<SDValue> Ops;
327 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
328 I != E; ++I)
329 Ops.push_back(*I);
330 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
331 if (IsModified) {
332 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
333 }
334 } while (IsModified);
335
336 }
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000337 if (Result && Result->isMachineOpcode() &&
338 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellard4bdf9892013-01-31 22:11:54 +0000339 && TII->isALUInstr(Result->getMachineOpcode())) {
340 // Fold FNEG/FABS/CONST_ADDRESS
341 // TODO: Isel can generate multiple MachineInst, we need to recursively
342 // parse Result
Tom Stellard9f7818d2013-01-23 02:09:06 +0000343 bool IsModified = false;
344 do {
345 std::vector<SDValue> Ops;
346 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
347 I != E; ++I)
348 Ops.push_back(*I);
349 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
350 if (IsModified) {
Tom Stellard4bdf9892013-01-31 22:11:54 +0000351 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard9f7818d2013-01-23 02:09:06 +0000352 }
353 } while (IsModified);
Tom Stellard4bdf9892013-01-31 22:11:54 +0000354
355 // If node has a single use which is CLAMP_R600, folds it
356 if (Result->hasOneUse() && Result->isMachineOpcode()) {
357 SDNode *PotentialClamp = *Result->use_begin();
358 if (PotentialClamp->isMachineOpcode() &&
359 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
360 unsigned ClampIdx =
361 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
362 std::vector<SDValue> Ops;
363 unsigned NumOp = Result->getNumOperands();
364 for (unsigned i = 0; i < NumOp; ++i) {
365 Ops.push_back(Result->getOperand(i));
366 }
367 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
368 Result = CurDAG->SelectNodeTo(PotentialClamp,
369 Result->getMachineOpcode(), PotentialClamp->getVTList(),
370 Ops.data(), NumOp);
371 }
372 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000373 }
374 }
375
376 return Result;
377}
378
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000379bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
380 SDValue &Abs, const R600InstrInfo *TII,
381 std::vector<unsigned> Consts) {
382 switch (Src.getOpcode()) {
383 case AMDGPUISD::CONST_ADDRESS: {
384 SDValue CstOffset;
385 if (Src.getValueType().isVector() ||
386 !SelectGlobalValueConstantOffset(Src.getOperand(0), CstOffset))
387 return false;
388
389 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
390 Consts.push_back(Cst->getZExtValue());
391 if (!TII->fitsConstReadLimitations(Consts))
392 return false;
393
394 Src = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
395 Sel = CstOffset;
396 return true;
397 }
398 case ISD::FNEG:
399 Src = Src.getOperand(0);
400 Neg = CurDAG->getTargetConstant(1, MVT::i32);
401 return true;
402 case ISD::FABS:
403 if (!Abs.getNode())
404 return false;
405 Src = Src.getOperand(0);
406 Abs = CurDAG->getTargetConstant(1, MVT::i32);
407 return true;
408 case ISD::BITCAST:
409 Src = Src.getOperand(0);
410 return true;
411 default:
412 return false;
413 }
414}
415
Tom Stellard9f7818d2013-01-23 02:09:06 +0000416bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
417 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
418 int OperandIdx[] = {
419 TII->getOperandIdx(Opcode, R600Operands::SRC0),
420 TII->getOperandIdx(Opcode, R600Operands::SRC1),
421 TII->getOperandIdx(Opcode, R600Operands::SRC2)
422 };
423 int SelIdx[] = {
424 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
425 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
426 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
427 };
Tom Stellard4bdf9892013-01-31 22:11:54 +0000428 int NegIdx[] = {
429 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
430 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
431 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
432 };
433 int AbsIdx[] = {
434 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
435 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
436 -1
437 };
438
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000439 // Gather constants values
440 std::vector<unsigned> Consts;
441 for (unsigned j = 0; j < 3; j++) {
442 int SrcIdx = OperandIdx[j];
443 if (SrcIdx < 0)
444 break;
445 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
446 if (Reg->getReg() == AMDGPU::ALU_CONST) {
447 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
448 Consts.push_back(Cst->getZExtValue());
449 }
450 }
451 }
452
Tom Stellard9f7818d2013-01-23 02:09:06 +0000453 for (unsigned i = 0; i < 3; i++) {
454 if (OperandIdx[i] < 0)
455 return false;
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000456 SDValue &Src = Ops[OperandIdx[i] - 1];
457 SDValue &Sel = Ops[SelIdx[i] - 1];
458 SDValue &Neg = Ops[NegIdx[i] - 1];
459 SDValue FakeAbs;
460 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
461 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
462 return true;
463 }
464 return false;
465}
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000466
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000467bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
468 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
469 int OperandIdx[] = {
470 TII->getOperandIdx(Opcode, R600Operands::SRC0_X),
471 TII->getOperandIdx(Opcode, R600Operands::SRC0_Y),
472 TII->getOperandIdx(Opcode, R600Operands::SRC0_Z),
473 TII->getOperandIdx(Opcode, R600Operands::SRC0_W),
474 TII->getOperandIdx(Opcode, R600Operands::SRC1_X),
475 TII->getOperandIdx(Opcode, R600Operands::SRC1_Y),
476 TII->getOperandIdx(Opcode, R600Operands::SRC1_Z),
477 TII->getOperandIdx(Opcode, R600Operands::SRC1_W)
478 };
479 int SelIdx[] = {
480 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_X),
481 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_Y),
482 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_Z),
483 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL_W),
484 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_X),
485 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_Y),
486 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_Z),
487 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL_W)
488 };
489 int NegIdx[] = {
490 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_X),
491 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_Y),
492 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_Z),
493 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG_W),
494 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_X),
495 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_Y),
496 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_Z),
497 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG_W)
498 };
499 int AbsIdx[] = {
500 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_X),
501 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_Y),
502 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_Z),
503 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS_W),
504 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_X),
505 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_Y),
506 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_Z),
507 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS_W)
508 };
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000509
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000510 // Gather constants values
511 std::vector<unsigned> Consts;
512 for (unsigned j = 0; j < 8; j++) {
513 int SrcIdx = OperandIdx[j];
514 if (SrcIdx < 0)
Tom Stellard9f7818d2013-01-23 02:09:06 +0000515 break;
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000516 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
517 if (Reg->getReg() == AMDGPU::ALU_CONST) {
518 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
519 Consts.push_back(Cst->getZExtValue());
520 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000521 }
522 }
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000523
524 for (unsigned i = 0; i < 8; i++) {
525 if (OperandIdx[i] < 0)
526 return false;
527 SDValue &Src = Ops[OperandIdx[i] - 1];
528 SDValue &Sel = Ops[SelIdx[i] - 1];
529 SDValue &Neg = Ops[NegIdx[i] - 1];
530 SDValue &Abs = Ops[AbsIdx[i] - 1];
531 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
532 return true;
533 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000534 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000535}
536
537bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
538 if (!ptr) {
539 return false;
540 }
541 Type *ptrType = ptr->getType();
542 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
543}
544
545const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
546 if (!V) {
547 return NULL;
548 }
549 const Value *ret = NULL;
550 ValueMap<const Value *, bool> ValueBitMap;
551 std::queue<const Value *, std::list<const Value *> > ValueQueue;
552 ValueQueue.push(V);
553 while (!ValueQueue.empty()) {
554 V = ValueQueue.front();
555 if (ValueBitMap.find(V) == ValueBitMap.end()) {
556 ValueBitMap[V] = true;
557 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
558 ret = V;
559 break;
560 } else if (dyn_cast<GlobalVariable>(V)) {
561 ret = V;
562 break;
563 } else if (dyn_cast<Constant>(V)) {
564 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
565 if (CE) {
566 ValueQueue.push(CE->getOperand(0));
567 }
568 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
569 ret = AI;
570 break;
571 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
572 uint32_t numOps = I->getNumOperands();
573 for (uint32_t x = 0; x < numOps; ++x) {
574 ValueQueue.push(I->getOperand(x));
575 }
576 } else {
577 assert(!"Found a Value that we didn't know how to handle!");
578 }
579 }
580 ValueQueue.pop();
581 }
582 return ret;
583}
584
585bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
586 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
587}
588
589bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
590 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
591 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
592 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
593}
594
595bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
596 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
597}
598
599bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
600 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
601}
602
603bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
604 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
605 return true;
606 }
607 MachineMemOperand *MMO = N->getMemOperand();
608 const Value *V = MMO->getValue();
609 const Value *BV = getBasePointerValue(V);
610 if (MMO
611 && MMO->getValue()
612 && ((V && dyn_cast<GlobalValue>(V))
613 || (BV && dyn_cast<GlobalValue>(
614 getBasePointerValue(MMO->getValue()))))) {
615 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
616 } else {
617 return false;
618 }
619}
620
621bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
622 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
623}
624
625bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
626 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
627}
628
629bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
630 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
631}
632
633bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
634 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
635}
636
637bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
638 MachineMemOperand *MMO = N->getMemOperand();
639 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
640 if (MMO) {
641 const Value *V = MMO->getValue();
642 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
643 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
644 return true;
645 }
646 }
647 }
648 return false;
649}
650
651bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
652 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
653 // Check to make sure we are not a constant pool load or a constant load
654 // that is marked as a private load
655 if (isCPLoad(N) || isConstantLoad(N, -1)) {
656 return false;
657 }
658 }
659 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
660 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
661 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
662 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
663 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
664 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
665 return true;
666 }
667 return false;
668}
669
670const char *AMDGPUDAGToDAGISel::getPassName() const {
671 return "AMDGPU DAG->DAG Pattern Instruction Selection";
672}
673
674#ifdef DEBUGTMP
675#undef INT64_C
676#endif
677#undef DEBUGTMP
678
679///==== AMDGPU Functions ====///
680
Tom Stellard9f7818d2013-01-23 02:09:06 +0000681bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
682 SDValue& IntPtr) {
683 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
684 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
685 return true;
686 }
687 return false;
688}
689
690bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
691 SDValue& BaseReg, SDValue &Offset) {
692 if (!dyn_cast<ConstantSDNode>(Addr)) {
693 BaseReg = Addr;
694 Offset = CurDAG->getIntPtrConstant(0, true);
695 return true;
696 }
697 return false;
698}
699
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000700bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
701 SDValue &Offset) {
702 ConstantSDNode * IMMOffset;
703
704 if (Addr.getOpcode() == ISD::ADD
705 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
706 && isInt<16>(IMMOffset->getZExtValue())) {
707
708 Base = Addr.getOperand(0);
709 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
710 return true;
711 // If the pointer address is constant, we can move it to the offset field.
712 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
713 && isInt<16>(IMMOffset->getZExtValue())) {
714 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +0000715 SDLoc(CurDAG->getEntryNode()),
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000716 AMDGPU::ZERO, MVT::i32);
717 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
718 return true;
719 }
720
721 // Default case, no offset
722 Base = Addr;
723 Offset = CurDAG->getTargetConstant(0, MVT::i32);
724 return true;
725}
726
Tom Stellardc0b0c672013-02-06 17:32:29 +0000727bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
728 SDValue &Offset) {
729 ConstantSDNode *C;
730
731 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
732 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
733 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
734 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
735 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
736 Base = Addr.getOperand(0);
737 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
738 } else {
739 Base = Addr;
740 Offset = CurDAG->getTargetConstant(0, MVT::i32);
741 }
742
743 return true;
744}
Christian Konigc018eca2013-02-26 17:52:16 +0000745
746void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
747
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000748 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Tom Stellard8a72c732013-06-03 17:39:46 +0000749 return;
750 }
751
Christian Konigc018eca2013-02-26 17:52:16 +0000752 // Go over all selected nodes and try to fold them a bit more
Bill Wendlingb88cef52013-06-06 01:04:21 +0000753 const AMDGPUTargetLowering& Lowering = (*(const AMDGPUTargetLowering*)TLI);
Christian Konigc018eca2013-02-26 17:52:16 +0000754 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
755 E = CurDAG->allnodes_end(); I != E; ++I) {
756
Tom Stellard8a72c732013-06-03 17:39:46 +0000757 SDNode *Node = I;
758 switch (Node->getOpcode()) {
759 // Fix the register class in copy to CopyToReg nodes - ISel will always
760 // use SReg classes for 64-bit copies, but this is not always what we want.
761 case ISD::CopyToReg: {
762 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
763 SDValue Val = Node->getOperand(2);
764 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
765 if (RC != &AMDGPU::SReg_64RegClass) {
766 continue;
767 }
768
Tom Stellard2948e692013-06-13 20:14:00 +0000769 if (!Val.getNode()->isMachineOpcode() ||
770 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
Tom Stellard8a72c732013-06-03 17:39:46 +0000771 continue;
772 }
773
774 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
775 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
776 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
777 continue;
778 }
779 }
780
781 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
782 if (!MachineNode)
Christian Konigc018eca2013-02-26 17:52:16 +0000783 continue;
784
Tom Stellard8a72c732013-06-03 17:39:46 +0000785 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
786 if (ResNode != Node) {
Christian Konigc018eca2013-02-26 17:52:16 +0000787 ReplaceUses(Node, ResNode);
Tom Stellard8a72c732013-06-03 17:39:46 +0000788 }
Christian Konigc018eca2013-02-26 17:52:16 +0000789 }
790}