blob: e79ab3c0db9361e7b6082d8acb677f7f3e9940da [file] [log] [blame]
Tom Stellard75aadc22012-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 Stellard75aadc22012-12-11 21:25:42 +000017#include "R600InstrInfo.h"
Christian Konigf82901a2013-02-26 17:52:23 +000018#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "llvm/ADT/ValueMap.h"
Matt Arsenault2aabb062013-06-18 23:37:58 +000020#include "llvm/Analysis/ValueTracking.h"
Tom Stellard2183b702013-06-03 17:39:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000024#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Support/Compiler.h"
26#include <list>
27#include <queue>
28
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Instruction Selector Implementation
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// AMDGPU specific code to select AMDGPU machine instructions for
37/// SelectionDAG operations.
38class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40 // make the right decision when generating code for different targets.
41 const AMDGPUSubtarget &Subtarget;
42public:
43 AMDGPUDAGToDAGISel(TargetMachine &TM);
44 virtual ~AMDGPUDAGToDAGISel();
45
46 SDNode *Select(SDNode *N);
47 virtual const char *getPassName() const;
Christian Konigd910b7d2013-02-26 17:52:16 +000048 virtual void PostprocessISelDAG();
Tom Stellard75aadc22012-12-11 21:25:42 +000049
50private:
51 inline SDValue getSmallIPtrImm(unsigned Imm);
Vincent Lejeunec6896792013-06-04 23:17:15 +000052 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
53 const R600InstrInfo *TII, std::vector<unsigned> Cst);
Tom Stellard365366f2013-01-23 02:09:06 +000054 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunec6896792013-06-04 23:17:15 +000055 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellard75aadc22012-12-11 21:25:42 +000056
57 // Complex pattern selectors
58 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
59 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
60 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
61
62 static bool checkType(const Value *ptr, unsigned int addrspace);
Tom Stellard75aadc22012-12-11 21:25:42 +000063
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
Matt Arsenault2aabb062013-06-18 23:37:58 +000069 bool isCPLoad(const LoadSDNode *N) const;
70 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
71 bool isGlobalLoad(const LoadSDNode *N) const;
72 bool isParamLoad(const LoadSDNode *N) const;
73 bool isPrivateLoad(const LoadSDNode *N) const;
74 bool isLocalLoad(const LoadSDNode *N) const;
75 bool isRegionLoad(const LoadSDNode *N) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000076
Tom Stellard365366f2013-01-23 02:09:06 +000077 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
78 bool SelectGlobalValueVariableOffset(SDValue Addr,
79 SDValue &BaseReg, SDValue& Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000080 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000081 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard75aadc22012-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
Bill Wendlinga3cd3502013-06-19 21:36:55 +000095AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
Tom Stellard75aadc22012-12-11 21:25:42 +000096 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
97}
98
99AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
100}
101
102SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
103 return CurDAG->getTargetConstant(Imm, MVT::i32);
104}
105
106bool AMDGPUDAGToDAGISel::SelectADDRParam(
107 SDValue Addr, SDValue& R1, SDValue& R2) {
108
109 if (Addr.getOpcode() == ISD::FrameIndex) {
110 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
111 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
112 R2 = CurDAG->getTargetConstant(0, MVT::i32);
113 } else {
114 R1 = Addr;
115 R2 = CurDAG->getTargetConstant(0, MVT::i32);
116 }
117 } else if (Addr.getOpcode() == ISD::ADD) {
118 R1 = Addr.getOperand(0);
119 R2 = Addr.getOperand(1);
120 } else {
121 R1 = Addr;
122 R2 = CurDAG->getTargetConstant(0, MVT::i32);
123 }
124 return true;
125}
126
127bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
128 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
129 Addr.getOpcode() == ISD::TargetGlobalAddress) {
130 return false;
131 }
132 return SelectADDRParam(Addr, R1, R2);
133}
134
135
136bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
137 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
138 Addr.getOpcode() == ISD::TargetGlobalAddress) {
139 return false;
140 }
141
142 if (Addr.getOpcode() == ISD::FrameIndex) {
143 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
144 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
145 R2 = CurDAG->getTargetConstant(0, MVT::i64);
146 } else {
147 R1 = Addr;
148 R2 = CurDAG->getTargetConstant(0, MVT::i64);
149 }
150 } else if (Addr.getOpcode() == ISD::ADD) {
151 R1 = Addr.getOperand(0);
152 R2 = Addr.getOperand(1);
153 } else {
154 R1 = Addr;
155 R2 = CurDAG->getTargetConstant(0, MVT::i64);
156 }
157 return true;
158}
159
160SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
161 unsigned int Opc = N->getOpcode();
162 if (N->isMachineOpcode()) {
163 return NULL; // Already selected.
164 }
165 switch (Opc) {
166 default: break;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000167 case ISD::BUILD_VECTOR: {
168 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000169 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000170 break;
171 }
172 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
173 // that adds a 128 bits reg copy when going through TwoAddressInstructions
174 // pass. We want to avoid 128 bits copies as much as possible because they
175 // can't be bundled by our scheduler.
176 SDValue RegSeqArgs[9] = {
177 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
178 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
179 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
180 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
181 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
182 };
183 bool IsRegSeq = true;
184 for (unsigned i = 0; i < N->getNumOperands(); i++) {
185 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
186 IsRegSeq = false;
187 break;
188 }
189 RegSeqArgs[2 * i + 1] = N->getOperand(i);
190 }
191 if (!IsRegSeq)
192 break;
193 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
194 RegSeqArgs, 2 * N->getNumOperands() + 1);
195 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000196 case ISD::BUILD_PAIR: {
197 SDValue RC, SubReg0, SubReg1;
198 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000199 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-04-05 23:31:51 +0000200 break;
201 }
202 if (N->getValueType(0) == MVT::i128) {
203 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
204 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
205 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
206 } else if (N->getValueType(0) == MVT::i64) {
207 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
208 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
209 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
210 } else {
211 llvm_unreachable("Unhandled value type for BUILD_PAIR");
212 }
213 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
214 N->getOperand(1), SubReg1 };
215 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000216 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000217 }
218
Tom Stellard75aadc22012-12-11 21:25:42 +0000219 case ISD::ConstantFP:
220 case ISD::Constant: {
221 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
222 // XXX: Custom immediate lowering not implemented yet. Instead we use
223 // pseudo instructions defined in SIInstructions.td
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000224 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000225 break;
226 }
227 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
228
229 uint64_t ImmValue = 0;
230 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
231
232 if (N->getOpcode() == ISD::ConstantFP) {
233 // XXX: 64-bit Immediates not supported yet
234 assert(N->getValueType(0) != MVT::f64);
235
236 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
237 APFloat Value = C->getValueAPF();
238 float FloatValue = Value.convertToFloat();
239 if (FloatValue == 0.0) {
240 ImmReg = AMDGPU::ZERO;
241 } else if (FloatValue == 0.5) {
242 ImmReg = AMDGPU::HALF;
243 } else if (FloatValue == 1.0) {
244 ImmReg = AMDGPU::ONE;
245 } else {
246 ImmValue = Value.bitcastToAPInt().getZExtValue();
247 }
248 } else {
249 // XXX: 64-bit Immediates not supported yet
250 assert(N->getValueType(0) != MVT::i64);
251
252 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
253 if (C->getZExtValue() == 0) {
254 ImmReg = AMDGPU::ZERO;
255 } else if (C->getZExtValue() == 1) {
256 ImmReg = AMDGPU::ONE_INT;
257 } else {
258 ImmValue = C->getZExtValue();
259 }
260 }
261
262 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
263 Use != SDNode::use_end(); Use = Next) {
264 Next = llvm::next(Use);
265 std::vector<SDValue> Ops;
266 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
267 Ops.push_back(Use->getOperand(i));
268 }
269
270 if (!Use->isMachineOpcode()) {
271 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
272 // We can only use literal constants (e.g. AMDGPU::ZERO,
273 // AMDGPU::ONE, etc) in machine opcodes.
274 continue;
275 }
276 } else {
Vincent Lejeunef694c102013-02-14 16:55:01 +0000277 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
278 (TII->get(Use->getMachineOpcode()).TSFlags &
279 R600_InstFlag::VECTOR)) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000280 continue;
281 }
282
Tom Stellard02661d92013-06-25 21:22:18 +0000283 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(),
284 AMDGPU::OpName::literal);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000285 if (ImmIdx == -1) {
286 continue;
287 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000288
Tom Stellardc026e8b2013-06-28 15:47:08 +0000289 if (TII->getOperandIdx(Use->getMachineOpcode(),
290 AMDGPU::OpName::dst) != -1) {
291 // subtract one from ImmIdx, because the DST operand is usually index
292 // 0 for MachineInstrs, but we have no DST in the Ops vector.
293 ImmIdx--;
294 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000295
296 // Check that we aren't already using an immediate.
297 // XXX: It's possible for an instruction to have more than one
298 // immediate operand, but this is not supported yet.
299 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
300 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
301 assert(C);
302
303 if (C->getZExtValue() != 0) {
304 // This instruction is already using an immediate.
305 continue;
306 }
307
308 // Set the immediate value
309 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
310 }
311 }
312 // Set the immediate register
313 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
314
315 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
316 }
317 break;
318 }
319 }
Tom Stellard365366f2013-01-23 02:09:06 +0000320 SDNode *Result = SelectCode(N);
321
322 // Fold operands of selected node
323
324 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000325 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard365366f2013-01-23 02:09:06 +0000326 const R600InstrInfo *TII =
327 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunec6896792013-06-04 23:17:15 +0000328 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
329 bool IsModified = false;
330 do {
331 std::vector<SDValue> Ops;
332 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
333 I != E; ++I)
334 Ops.push_back(*I);
335 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
336 if (IsModified) {
337 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
338 }
339 } while (IsModified);
Matt Arsenault2aabb062013-06-18 23:37:58 +0000340
Vincent Lejeunec6896792013-06-04 23:17:15 +0000341 }
Vincent Lejeunef694c102013-02-14 16:55:01 +0000342 if (Result && Result->isMachineOpcode() &&
343 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellardc026e8b2013-06-28 15:47:08 +0000344 && TII->hasInstrModifiers(Result->getMachineOpcode())) {
Tom Stellard49269212013-01-31 22:11:54 +0000345 // Fold FNEG/FABS/CONST_ADDRESS
346 // TODO: Isel can generate multiple MachineInst, we need to recursively
347 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000348 bool IsModified = false;
349 do {
350 std::vector<SDValue> Ops;
351 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
352 I != E; ++I)
353 Ops.push_back(*I);
354 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
355 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000356 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000357 }
358 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000359
360 // If node has a single use which is CLAMP_R600, folds it
361 if (Result->hasOneUse() && Result->isMachineOpcode()) {
362 SDNode *PotentialClamp = *Result->use_begin();
363 if (PotentialClamp->isMachineOpcode() &&
364 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
365 unsigned ClampIdx =
Tom Stellard02661d92013-06-25 21:22:18 +0000366 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
Tom Stellard49269212013-01-31 22:11:54 +0000367 std::vector<SDValue> Ops;
368 unsigned NumOp = Result->getNumOperands();
369 for (unsigned i = 0; i < NumOp; ++i) {
370 Ops.push_back(Result->getOperand(i));
371 }
372 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
373 Result = CurDAG->SelectNodeTo(PotentialClamp,
374 Result->getMachineOpcode(), PotentialClamp->getVTList(),
375 Ops.data(), NumOp);
376 }
377 }
Tom Stellard365366f2013-01-23 02:09:06 +0000378 }
379 }
380
381 return Result;
382}
383
Vincent Lejeunec6896792013-06-04 23:17:15 +0000384bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
385 SDValue &Abs, const R600InstrInfo *TII,
386 std::vector<unsigned> Consts) {
387 switch (Src.getOpcode()) {
388 case AMDGPUISD::CONST_ADDRESS: {
389 SDValue CstOffset;
390 if (Src.getValueType().isVector() ||
391 !SelectGlobalValueConstantOffset(Src.getOperand(0), CstOffset))
392 return false;
393
394 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
395 Consts.push_back(Cst->getZExtValue());
396 if (!TII->fitsConstReadLimitations(Consts))
397 return false;
398
399 Src = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
400 Sel = CstOffset;
401 return true;
402 }
403 case ISD::FNEG:
404 Src = Src.getOperand(0);
405 Neg = CurDAG->getTargetConstant(1, MVT::i32);
406 return true;
407 case ISD::FABS:
408 if (!Abs.getNode())
409 return false;
410 Src = Src.getOperand(0);
411 Abs = CurDAG->getTargetConstant(1, MVT::i32);
412 return true;
413 case ISD::BITCAST:
414 Src = Src.getOperand(0);
415 return true;
416 default:
417 return false;
418 }
419}
420
Tom Stellard365366f2013-01-23 02:09:06 +0000421bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
422 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
423 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000424 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
425 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
426 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
Tom Stellard365366f2013-01-23 02:09:06 +0000427 };
428 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000429 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
430 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
431 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
Tom Stellard365366f2013-01-23 02:09:06 +0000432 };
Tom Stellard49269212013-01-31 22:11:54 +0000433 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000434 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
435 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
436 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
Tom Stellard49269212013-01-31 22:11:54 +0000437 };
438 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000439 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
440 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
Tom Stellard49269212013-01-31 22:11:54 +0000441 -1
442 };
443
Vincent Lejeunec6896792013-06-04 23:17:15 +0000444 // Gather constants values
445 std::vector<unsigned> Consts;
446 for (unsigned j = 0; j < 3; j++) {
447 int SrcIdx = OperandIdx[j];
448 if (SrcIdx < 0)
449 break;
450 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
451 if (Reg->getReg() == AMDGPU::ALU_CONST) {
452 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
453 Consts.push_back(Cst->getZExtValue());
454 }
455 }
456 }
457
Tom Stellard365366f2013-01-23 02:09:06 +0000458 for (unsigned i = 0; i < 3; i++) {
459 if (OperandIdx[i] < 0)
460 return false;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000461 SDValue &Src = Ops[OperandIdx[i] - 1];
462 SDValue &Sel = Ops[SelIdx[i] - 1];
463 SDValue &Neg = Ops[NegIdx[i] - 1];
464 SDValue FakeAbs;
465 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
466 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
467 return true;
468 }
469 return false;
470}
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000471
Vincent Lejeunec6896792013-06-04 23:17:15 +0000472bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
473 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
474 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000475 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
476 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
477 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
478 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
479 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
480 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
481 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
482 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000483 };
484 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000485 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
486 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
487 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
488 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
489 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
490 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
491 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
492 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000493 };
494 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000495 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
496 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
497 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
498 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
499 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
500 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
501 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
502 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000503 };
504 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000505 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
506 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
507 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
508 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
509 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
510 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
511 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
512 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000513 };
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000514
Vincent Lejeunec6896792013-06-04 23:17:15 +0000515 // Gather constants values
516 std::vector<unsigned> Consts;
517 for (unsigned j = 0; j < 8; j++) {
518 int SrcIdx = OperandIdx[j];
519 if (SrcIdx < 0)
Tom Stellard365366f2013-01-23 02:09:06 +0000520 break;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000521 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
522 if (Reg->getReg() == AMDGPU::ALU_CONST) {
523 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
524 Consts.push_back(Cst->getZExtValue());
525 }
Tom Stellard365366f2013-01-23 02:09:06 +0000526 }
527 }
Vincent Lejeunec6896792013-06-04 23:17:15 +0000528
529 for (unsigned i = 0; i < 8; i++) {
530 if (OperandIdx[i] < 0)
531 return false;
532 SDValue &Src = Ops[OperandIdx[i] - 1];
533 SDValue &Sel = Ops[SelIdx[i] - 1];
534 SDValue &Neg = Ops[NegIdx[i] - 1];
535 SDValue &Abs = Ops[AbsIdx[i] - 1];
536 if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
537 return true;
538 }
Tom Stellard365366f2013-01-23 02:09:06 +0000539 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000540}
541
542bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
543 if (!ptr) {
544 return false;
545 }
546 Type *ptrType = ptr->getType();
547 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
548}
549
Tom Stellard75aadc22012-12-11 21:25:42 +0000550bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
551 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
552}
553
554bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
555 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
556 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
557 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
558}
559
560bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
561 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
562}
563
564bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
565 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
566}
567
Matt Arsenault2aabb062013-06-18 23:37:58 +0000568bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000569 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
570 return true;
571 }
Matt Arsenault2aabb062013-06-18 23:37:58 +0000572
573 const DataLayout *DL = TM.getDataLayout();
Tom Stellard75aadc22012-12-11 21:25:42 +0000574 MachineMemOperand *MMO = N->getMemOperand();
575 const Value *V = MMO->getValue();
Matt Arsenault2aabb062013-06-18 23:37:58 +0000576 const Value *BV = GetUnderlyingObject(V, DL, 0);
Tom Stellard75aadc22012-12-11 21:25:42 +0000577 if (MMO
578 && MMO->getValue()
579 && ((V && dyn_cast<GlobalValue>(V))
580 || (BV && dyn_cast<GlobalValue>(
Matt Arsenault2aabb062013-06-18 23:37:58 +0000581 GetUnderlyingObject(MMO->getValue(), DL, 0))))) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000582 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
583 } else {
584 return false;
585 }
586}
587
Matt Arsenault2aabb062013-06-18 23:37:58 +0000588bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000589 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
590}
591
Matt Arsenault2aabb062013-06-18 23:37:58 +0000592bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000593 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
594}
595
Matt Arsenault2aabb062013-06-18 23:37:58 +0000596bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000597 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
598}
599
Matt Arsenault2aabb062013-06-18 23:37:58 +0000600bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000601 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
602}
603
Matt Arsenault2aabb062013-06-18 23:37:58 +0000604bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000605 MachineMemOperand *MMO = N->getMemOperand();
606 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
607 if (MMO) {
608 const Value *V = MMO->getValue();
609 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
610 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
611 return true;
612 }
613 }
614 }
615 return false;
616}
617
Matt Arsenault2aabb062013-06-18 23:37:58 +0000618bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000619 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
620 // Check to make sure we are not a constant pool load or a constant load
621 // that is marked as a private load
622 if (isCPLoad(N) || isConstantLoad(N, -1)) {
623 return false;
624 }
625 }
626 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
627 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
628 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
629 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
630 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
631 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
632 return true;
633 }
634 return false;
635}
636
637const char *AMDGPUDAGToDAGISel::getPassName() const {
638 return "AMDGPU DAG->DAG Pattern Instruction Selection";
639}
640
641#ifdef DEBUGTMP
642#undef INT64_C
643#endif
644#undef DEBUGTMP
645
646///==== AMDGPU Functions ====///
647
Tom Stellard365366f2013-01-23 02:09:06 +0000648bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
649 SDValue& IntPtr) {
650 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
651 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
652 return true;
653 }
654 return false;
655}
656
657bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
658 SDValue& BaseReg, SDValue &Offset) {
659 if (!dyn_cast<ConstantSDNode>(Addr)) {
660 BaseReg = Addr;
661 Offset = CurDAG->getIntPtrConstant(0, true);
662 return true;
663 }
664 return false;
665}
666
Tom Stellard75aadc22012-12-11 21:25:42 +0000667bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
668 SDValue &Offset) {
669 ConstantSDNode * IMMOffset;
670
671 if (Addr.getOpcode() == ISD::ADD
672 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
673 && isInt<16>(IMMOffset->getZExtValue())) {
674
675 Base = Addr.getOperand(0);
676 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
677 return true;
678 // If the pointer address is constant, we can move it to the offset field.
679 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
680 && isInt<16>(IMMOffset->getZExtValue())) {
681 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000682 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000683 AMDGPU::ZERO, MVT::i32);
684 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
685 return true;
686 }
687
688 // Default case, no offset
689 Base = Addr;
690 Offset = CurDAG->getTargetConstant(0, MVT::i32);
691 return true;
692}
693
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000694bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
695 SDValue &Offset) {
696 ConstantSDNode *C;
697
698 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
699 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
700 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
701 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
702 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
703 Base = Addr.getOperand(0);
704 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
705 } else {
706 Base = Addr;
707 Offset = CurDAG->getTargetConstant(0, MVT::i32);
708 }
709
710 return true;
711}
Christian Konigd910b7d2013-02-26 17:52:16 +0000712
713void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
714
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000715 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Tom Stellard2183b702013-06-03 17:39:46 +0000716 return;
717 }
718
Christian Konigd910b7d2013-02-26 17:52:16 +0000719 // Go over all selected nodes and try to fold them a bit more
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000720 const AMDGPUTargetLowering& Lowering =
721 (*(const AMDGPUTargetLowering*)getTargetLowering());
Christian Konigd910b7d2013-02-26 17:52:16 +0000722 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
723 E = CurDAG->allnodes_end(); I != E; ++I) {
724
Tom Stellard2183b702013-06-03 17:39:46 +0000725 SDNode *Node = I;
726 switch (Node->getOpcode()) {
727 // Fix the register class in copy to CopyToReg nodes - ISel will always
728 // use SReg classes for 64-bit copies, but this is not always what we want.
729 case ISD::CopyToReg: {
730 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
731 SDValue Val = Node->getOperand(2);
732 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
733 if (RC != &AMDGPU::SReg_64RegClass) {
734 continue;
735 }
736
Tom Stellardadba0832013-06-13 20:14:00 +0000737 if (!Val.getNode()->isMachineOpcode() ||
738 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
Tom Stellard2183b702013-06-03 17:39:46 +0000739 continue;
740 }
741
742 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
743 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
744 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
745 continue;
746 }
747 }
748
749 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
750 if (!MachineNode)
Christian Konigd910b7d2013-02-26 17:52:16 +0000751 continue;
752
Tom Stellard2183b702013-06-03 17:39:46 +0000753 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
754 if (ResNode != Node) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000755 ReplaceUses(Node, ResNode);
Tom Stellard2183b702013-06-03 17:39:46 +0000756 }
Christian Konigd910b7d2013-02-26 17:52:16 +0000757 }
758}