blob: 959d6621fba20023b95d9f96f36933d395b1c342 [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"
17#include "AMDILDevices.h"
18#include "R600InstrInfo.h"
Christian Konigd3b55092013-02-26 17:52:23 +000019#include "SIISelLowering.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000020#include "llvm/ADT/ValueMap.h"
Tom Stellard8a72c732013-06-03 17:39:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellardf98f2ce2012-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 Konigc018eca2013-02-26 17:52:16 +000048 virtual void PostprocessISelDAG();
Tom Stellardf98f2ce2012-12-11 21:25:42 +000049
50private:
51 inline SDValue getSmallIPtrImm(unsigned Imm);
Tom Stellard9f7818d2013-01-23 02:09:06 +000052 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000053
54 // Complex pattern selectors
55 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
56 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
57 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
58
59 static bool checkType(const Value *ptr, unsigned int addrspace);
60 static const Value *getBasePointerValue(const Value *V);
61
62 static bool isGlobalStore(const StoreSDNode *N);
63 static bool isPrivateStore(const StoreSDNode *N);
64 static bool isLocalStore(const StoreSDNode *N);
65 static bool isRegionStore(const StoreSDNode *N);
66
67 static bool isCPLoad(const LoadSDNode *N);
68 static bool isConstantLoad(const LoadSDNode *N, int cbID);
69 static bool isGlobalLoad(const LoadSDNode *N);
70 static bool isParamLoad(const LoadSDNode *N);
71 static bool isPrivateLoad(const LoadSDNode *N);
72 static bool isLocalLoad(const LoadSDNode *N);
73 static bool isRegionLoad(const LoadSDNode *N);
74
Tom Stellard9f7818d2013-01-23 02:09:06 +000075 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
76 bool SelectGlobalValueVariableOffset(SDValue Addr,
77 SDValue &BaseReg, SDValue& Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000078 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardc0b0c672013-02-06 17:32:29 +000079 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000080
81 // Include the pieces autogenerated from the target description.
82#include "AMDGPUGenDAGISel.inc"
83};
84} // end anonymous namespace
85
86/// \brief This pass converts a legalized DAG into a AMDGPU-specific
87// DAG, ready for instruction scheduling.
88FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
89 ) {
90 return new AMDGPUDAGToDAGISel(TM);
91}
92
93AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
94 )
95 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
96}
97
98AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
99}
100
101SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
102 return CurDAG->getTargetConstant(Imm, MVT::i32);
103}
104
105bool AMDGPUDAGToDAGISel::SelectADDRParam(
106 SDValue Addr, SDValue& R1, SDValue& R2) {
107
108 if (Addr.getOpcode() == ISD::FrameIndex) {
109 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
110 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
111 R2 = CurDAG->getTargetConstant(0, MVT::i32);
112 } else {
113 R1 = Addr;
114 R2 = CurDAG->getTargetConstant(0, MVT::i32);
115 }
116 } else if (Addr.getOpcode() == ISD::ADD) {
117 R1 = Addr.getOperand(0);
118 R2 = Addr.getOperand(1);
119 } else {
120 R1 = Addr;
121 R2 = CurDAG->getTargetConstant(0, MVT::i32);
122 }
123 return true;
124}
125
126bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
127 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
128 Addr.getOpcode() == ISD::TargetGlobalAddress) {
129 return false;
130 }
131 return SelectADDRParam(Addr, R1, R2);
132}
133
134
135bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
136 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
137 Addr.getOpcode() == ISD::TargetGlobalAddress) {
138 return false;
139 }
140
141 if (Addr.getOpcode() == ISD::FrameIndex) {
142 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
143 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
144 R2 = CurDAG->getTargetConstant(0, MVT::i64);
145 } else {
146 R1 = Addr;
147 R2 = CurDAG->getTargetConstant(0, MVT::i64);
148 }
149 } else if (Addr.getOpcode() == ISD::ADD) {
150 R1 = Addr.getOperand(0);
151 R2 = Addr.getOperand(1);
152 } else {
153 R1 = Addr;
154 R2 = CurDAG->getTargetConstant(0, MVT::i64);
155 }
156 return true;
157}
158
159SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
160 unsigned int Opc = N->getOpcode();
161 if (N->isMachineOpcode()) {
162 return NULL; // Already selected.
163 }
164 switch (Opc) {
165 default: break;
Vincent Lejeunecae68012013-03-05 15:04:49 +0000166 case ISD::BUILD_VECTOR: {
167 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
168 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
169 break;
170 }
171 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
172 // that adds a 128 bits reg copy when going through TwoAddressInstructions
173 // pass. We want to avoid 128 bits copies as much as possible because they
174 // can't be bundled by our scheduler.
175 SDValue RegSeqArgs[9] = {
176 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
177 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
178 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
179 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
180 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
181 };
182 bool IsRegSeq = true;
183 for (unsigned i = 0; i < N->getNumOperands(); i++) {
184 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
185 IsRegSeq = false;
186 break;
187 }
188 RegSeqArgs[2 * i + 1] = N->getOperand(i);
189 }
190 if (!IsRegSeq)
191 break;
192 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
193 RegSeqArgs, 2 * N->getNumOperands() + 1);
194 }
Tom Stellard17ea10c2013-04-05 23:31:51 +0000195 case ISD::BUILD_PAIR: {
196 SDValue RC, SubReg0, SubReg1;
197 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
198 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
199 break;
200 }
201 if (N->getValueType(0) == MVT::i128) {
202 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
203 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
204 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
205 } else if (N->getValueType(0) == MVT::i64) {
206 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
207 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
208 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
209 } else {
210 llvm_unreachable("Unhandled value type for BUILD_PAIR");
211 }
212 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
213 N->getOperand(1), SubReg1 };
214 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000215 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard17ea10c2013-04-05 23:31:51 +0000216 }
217
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000218 case ISD::ConstantFP:
219 case ISD::Constant: {
220 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
221 // XXX: Custom immediate lowering not implemented yet. Instead we use
222 // pseudo instructions defined in SIInstructions.td
223 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
224 break;
225 }
226 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
227
228 uint64_t ImmValue = 0;
229 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
230
231 if (N->getOpcode() == ISD::ConstantFP) {
232 // XXX: 64-bit Immediates not supported yet
233 assert(N->getValueType(0) != MVT::f64);
234
235 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
236 APFloat Value = C->getValueAPF();
237 float FloatValue = Value.convertToFloat();
238 if (FloatValue == 0.0) {
239 ImmReg = AMDGPU::ZERO;
240 } else if (FloatValue == 0.5) {
241 ImmReg = AMDGPU::HALF;
242 } else if (FloatValue == 1.0) {
243 ImmReg = AMDGPU::ONE;
244 } else {
245 ImmValue = Value.bitcastToAPInt().getZExtValue();
246 }
247 } else {
248 // XXX: 64-bit Immediates not supported yet
249 assert(N->getValueType(0) != MVT::i64);
250
251 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
252 if (C->getZExtValue() == 0) {
253 ImmReg = AMDGPU::ZERO;
254 } else if (C->getZExtValue() == 1) {
255 ImmReg = AMDGPU::ONE_INT;
256 } else {
257 ImmValue = C->getZExtValue();
258 }
259 }
260
261 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
262 Use != SDNode::use_end(); Use = Next) {
263 Next = llvm::next(Use);
264 std::vector<SDValue> Ops;
265 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
266 Ops.push_back(Use->getOperand(i));
267 }
268
269 if (!Use->isMachineOpcode()) {
270 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
271 // We can only use literal constants (e.g. AMDGPU::ZERO,
272 // AMDGPU::ONE, etc) in machine opcodes.
273 continue;
274 }
275 } else {
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000276 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
277 (TII->get(Use->getMachineOpcode()).TSFlags &
278 R600_InstFlag::VECTOR)) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000279 continue;
280 }
281
282 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
283 assert(ImmIdx != -1);
284
285 // subtract one from ImmIdx, because the DST operand is usually index
286 // 0 for MachineInstrs, but we have no DST in the Ops vector.
287 ImmIdx--;
288
289 // Check that we aren't already using an immediate.
290 // XXX: It's possible for an instruction to have more than one
291 // immediate operand, but this is not supported yet.
292 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
293 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
294 assert(C);
295
296 if (C->getZExtValue() != 0) {
297 // This instruction is already using an immediate.
298 continue;
299 }
300
301 // Set the immediate value
302 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
303 }
304 }
305 // Set the immediate register
306 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
307
308 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
309 }
310 break;
311 }
312 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000313 SDNode *Result = SelectCode(N);
314
315 // Fold operands of selected node
316
317 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
318 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
319 const R600InstrInfo *TII =
320 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000321 if (Result && Result->isMachineOpcode() &&
322 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellard4bdf9892013-01-31 22:11:54 +0000323 && TII->isALUInstr(Result->getMachineOpcode())) {
324 // Fold FNEG/FABS/CONST_ADDRESS
325 // TODO: Isel can generate multiple MachineInst, we need to recursively
326 // parse Result
Tom Stellard9f7818d2013-01-23 02:09:06 +0000327 bool IsModified = false;
328 do {
329 std::vector<SDValue> Ops;
330 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
331 I != E; ++I)
332 Ops.push_back(*I);
333 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
334 if (IsModified) {
Tom Stellard4bdf9892013-01-31 22:11:54 +0000335 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard9f7818d2013-01-23 02:09:06 +0000336 }
337 } while (IsModified);
Tom Stellard4bdf9892013-01-31 22:11:54 +0000338
339 // If node has a single use which is CLAMP_R600, folds it
340 if (Result->hasOneUse() && Result->isMachineOpcode()) {
341 SDNode *PotentialClamp = *Result->use_begin();
342 if (PotentialClamp->isMachineOpcode() &&
343 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
344 unsigned ClampIdx =
345 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
346 std::vector<SDValue> Ops;
347 unsigned NumOp = Result->getNumOperands();
348 for (unsigned i = 0; i < NumOp; ++i) {
349 Ops.push_back(Result->getOperand(i));
350 }
351 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
352 Result = CurDAG->SelectNodeTo(PotentialClamp,
353 Result->getMachineOpcode(), PotentialClamp->getVTList(),
354 Ops.data(), NumOp);
355 }
356 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000357 }
358 }
359
360 return Result;
361}
362
363bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
364 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
365 int OperandIdx[] = {
366 TII->getOperandIdx(Opcode, R600Operands::SRC0),
367 TII->getOperandIdx(Opcode, R600Operands::SRC1),
368 TII->getOperandIdx(Opcode, R600Operands::SRC2)
369 };
370 int SelIdx[] = {
371 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
372 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
373 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
374 };
Tom Stellard4bdf9892013-01-31 22:11:54 +0000375 int NegIdx[] = {
376 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
377 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
378 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
379 };
380 int AbsIdx[] = {
381 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
382 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
383 -1
384 };
385
Tom Stellard9f7818d2013-01-23 02:09:06 +0000386 for (unsigned i = 0; i < 3; i++) {
387 if (OperandIdx[i] < 0)
388 return false;
389 SDValue Operand = Ops[OperandIdx[i] - 1];
390 switch (Operand.getOpcode()) {
391 case AMDGPUISD::CONST_ADDRESS: {
392 SDValue CstOffset;
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000393 if (Operand.getValueType().isVector() ||
394 !SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset))
395 break;
396
397 // Gather others constants values
398 std::vector<unsigned> Consts;
399 for (unsigned j = 0; j < 3; j++) {
400 int SrcIdx = OperandIdx[j];
401 if (SrcIdx < 0)
402 break;
403 if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
404 if (Reg->getReg() == AMDGPU::ALU_CONST) {
405 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
406 Consts.push_back(Cst->getZExtValue());
407 }
408 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000409 }
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000410
411 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
412 Consts.push_back(Cst->getZExtValue());
413 if (!TII->fitsConstReadLimitations(Consts))
414 break;
415
416 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
417 Ops[SelIdx[i] - 1] = CstOffset;
418 return true;
Tom Stellard9f7818d2013-01-23 02:09:06 +0000419 }
Tom Stellard4bdf9892013-01-31 22:11:54 +0000420 case ISD::FNEG:
421 if (NegIdx[i] < 0)
422 break;
423 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
424 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
425 return true;
426 case ISD::FABS:
427 if (AbsIdx[i] < 0)
428 break;
429 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
430 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
431 return true;
Tom Stellardcacbcb02013-01-31 22:11:53 +0000432 case ISD::BITCAST:
433 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
434 return true;
Tom Stellard9f7818d2013-01-23 02:09:06 +0000435 default:
436 break;
437 }
438 }
439 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000440}
441
442bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
443 if (!ptr) {
444 return false;
445 }
446 Type *ptrType = ptr->getType();
447 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
448}
449
450const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
451 if (!V) {
452 return NULL;
453 }
454 const Value *ret = NULL;
455 ValueMap<const Value *, bool> ValueBitMap;
456 std::queue<const Value *, std::list<const Value *> > ValueQueue;
457 ValueQueue.push(V);
458 while (!ValueQueue.empty()) {
459 V = ValueQueue.front();
460 if (ValueBitMap.find(V) == ValueBitMap.end()) {
461 ValueBitMap[V] = true;
462 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
463 ret = V;
464 break;
465 } else if (dyn_cast<GlobalVariable>(V)) {
466 ret = V;
467 break;
468 } else if (dyn_cast<Constant>(V)) {
469 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
470 if (CE) {
471 ValueQueue.push(CE->getOperand(0));
472 }
473 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
474 ret = AI;
475 break;
476 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
477 uint32_t numOps = I->getNumOperands();
478 for (uint32_t x = 0; x < numOps; ++x) {
479 ValueQueue.push(I->getOperand(x));
480 }
481 } else {
482 assert(!"Found a Value that we didn't know how to handle!");
483 }
484 }
485 ValueQueue.pop();
486 }
487 return ret;
488}
489
490bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
491 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
492}
493
494bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
495 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
496 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
497 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
498}
499
500bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
501 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
502}
503
504bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
505 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
506}
507
508bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
509 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
510 return true;
511 }
512 MachineMemOperand *MMO = N->getMemOperand();
513 const Value *V = MMO->getValue();
514 const Value *BV = getBasePointerValue(V);
515 if (MMO
516 && MMO->getValue()
517 && ((V && dyn_cast<GlobalValue>(V))
518 || (BV && dyn_cast<GlobalValue>(
519 getBasePointerValue(MMO->getValue()))))) {
520 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
521 } else {
522 return false;
523 }
524}
525
526bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
527 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
528}
529
530bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
531 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
532}
533
534bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
535 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
536}
537
538bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
539 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
540}
541
542bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
543 MachineMemOperand *MMO = N->getMemOperand();
544 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
545 if (MMO) {
546 const Value *V = MMO->getValue();
547 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
548 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
549 return true;
550 }
551 }
552 }
553 return false;
554}
555
556bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
557 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
558 // Check to make sure we are not a constant pool load or a constant load
559 // that is marked as a private load
560 if (isCPLoad(N) || isConstantLoad(N, -1)) {
561 return false;
562 }
563 }
564 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
565 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
566 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
567 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
568 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
569 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
570 return true;
571 }
572 return false;
573}
574
575const char *AMDGPUDAGToDAGISel::getPassName() const {
576 return "AMDGPU DAG->DAG Pattern Instruction Selection";
577}
578
579#ifdef DEBUGTMP
580#undef INT64_C
581#endif
582#undef DEBUGTMP
583
584///==== AMDGPU Functions ====///
585
Tom Stellard9f7818d2013-01-23 02:09:06 +0000586bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
587 SDValue& IntPtr) {
588 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
589 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
590 return true;
591 }
592 return false;
593}
594
595bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
596 SDValue& BaseReg, SDValue &Offset) {
597 if (!dyn_cast<ConstantSDNode>(Addr)) {
598 BaseReg = Addr;
599 Offset = CurDAG->getIntPtrConstant(0, true);
600 return true;
601 }
602 return false;
603}
604
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000605bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
606 SDValue &Offset) {
607 ConstantSDNode * IMMOffset;
608
609 if (Addr.getOpcode() == ISD::ADD
610 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
611 && isInt<16>(IMMOffset->getZExtValue())) {
612
613 Base = Addr.getOperand(0);
614 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
615 return true;
616 // If the pointer address is constant, we can move it to the offset field.
617 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
618 && isInt<16>(IMMOffset->getZExtValue())) {
619 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +0000620 SDLoc(CurDAG->getEntryNode()),
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000621 AMDGPU::ZERO, MVT::i32);
622 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
623 return true;
624 }
625
626 // Default case, no offset
627 Base = Addr;
628 Offset = CurDAG->getTargetConstant(0, MVT::i32);
629 return true;
630}
631
Tom Stellardc0b0c672013-02-06 17:32:29 +0000632bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
633 SDValue &Offset) {
634 ConstantSDNode *C;
635
636 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
637 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
638 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
639 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
640 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
641 Base = Addr.getOperand(0);
642 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
643 } else {
644 Base = Addr;
645 Offset = CurDAG->getTargetConstant(0, MVT::i32);
646 }
647
648 return true;
649}
Christian Konigc018eca2013-02-26 17:52:16 +0000650
651void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
652
Tom Stellard8a72c732013-06-03 17:39:46 +0000653 if (Subtarget.device()->getGeneration() < AMDGPUDeviceInfo::HD7XXX) {
654 return;
655 }
656
Christian Konigc018eca2013-02-26 17:52:16 +0000657 // Go over all selected nodes and try to fold them a bit more
658 const AMDGPUTargetLowering& Lowering = ((const AMDGPUTargetLowering&)TLI);
659 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
660 E = CurDAG->allnodes_end(); I != E; ++I) {
661
Tom Stellard8a72c732013-06-03 17:39:46 +0000662 SDNode *Node = I;
663 switch (Node->getOpcode()) {
664 // Fix the register class in copy to CopyToReg nodes - ISel will always
665 // use SReg classes for 64-bit copies, but this is not always what we want.
666 case ISD::CopyToReg: {
667 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
668 SDValue Val = Node->getOperand(2);
669 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
670 if (RC != &AMDGPU::SReg_64RegClass) {
671 continue;
672 }
673
674 if (!Val.getNode()->isMachineOpcode()) {
675 continue;
676 }
677
678 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
679 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
680 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
681 continue;
682 }
683 }
684
685 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
686 if (!MachineNode)
Christian Konigc018eca2013-02-26 17:52:16 +0000687 continue;
688
Tom Stellard8a72c732013-06-03 17:39:46 +0000689 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
690 if (ResNode != Node) {
Christian Konigc018eca2013-02-26 17:52:16 +0000691 ReplaceUses(Node, ResNode);
Tom Stellard8a72c732013-06-03 17:39:46 +0000692 }
Christian Konigc018eca2013-02-26 17:52:16 +0000693 }
694}