blob: b125ba87ed13ddbd8db848a083934e587d8ec3ba [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"
17#include "AMDILDevices.h"
18#include "R600InstrInfo.h"
19#include "llvm/ADT/ValueMap.h"
20#include "llvm/CodeGen/PseudoSourceValue.h"
21#include "llvm/CodeGen/SelectionDAGISel.h"
22#include "llvm/Support/Compiler.h"
Tom Stellard365366f2013-01-23 02:09:06 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000024#include <list>
25#include <queue>
26
27using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30// Instruction Selector Implementation
31//===----------------------------------------------------------------------===//
32
33namespace {
34/// AMDGPU specific code to select AMDGPU machine instructions for
35/// SelectionDAG operations.
36class AMDGPUDAGToDAGISel : public SelectionDAGISel {
37 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
38 // make the right decision when generating code for different targets.
39 const AMDGPUSubtarget &Subtarget;
40public:
41 AMDGPUDAGToDAGISel(TargetMachine &TM);
42 virtual ~AMDGPUDAGToDAGISel();
43
44 SDNode *Select(SDNode *N);
45 virtual const char *getPassName() const;
46
47private:
48 inline SDValue getSmallIPtrImm(unsigned Imm);
Tom Stellard365366f2013-01-23 02:09:06 +000049 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellard75aadc22012-12-11 21:25:42 +000050
51 // Complex pattern selectors
52 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
53 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
54 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
55
56 static bool checkType(const Value *ptr, unsigned int addrspace);
57 static const Value *getBasePointerValue(const Value *V);
58
59 static bool isGlobalStore(const StoreSDNode *N);
60 static bool isPrivateStore(const StoreSDNode *N);
61 static bool isLocalStore(const StoreSDNode *N);
62 static bool isRegionStore(const StoreSDNode *N);
63
64 static bool isCPLoad(const LoadSDNode *N);
65 static bool isConstantLoad(const LoadSDNode *N, int cbID);
66 static bool isGlobalLoad(const LoadSDNode *N);
67 static bool isParamLoad(const LoadSDNode *N);
68 static bool isPrivateLoad(const LoadSDNode *N);
69 static bool isLocalLoad(const LoadSDNode *N);
70 static bool isRegionLoad(const LoadSDNode *N);
71
Tom Stellard365366f2013-01-23 02:09:06 +000072 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
73 bool SelectGlobalValueVariableOffset(SDValue Addr,
74 SDValue &BaseReg, SDValue& Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000075 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000076 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000077
78 // Include the pieces autogenerated from the target description.
79#include "AMDGPUGenDAGISel.inc"
80};
81} // end anonymous namespace
82
83/// \brief This pass converts a legalized DAG into a AMDGPU-specific
84// DAG, ready for instruction scheduling.
85FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
86 ) {
87 return new AMDGPUDAGToDAGISel(TM);
88}
89
90AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
91 )
92 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
93}
94
95AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
96}
97
98SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
99 return CurDAG->getTargetConstant(Imm, MVT::i32);
100}
101
102bool AMDGPUDAGToDAGISel::SelectADDRParam(
103 SDValue Addr, SDValue& R1, SDValue& R2) {
104
105 if (Addr.getOpcode() == ISD::FrameIndex) {
106 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
107 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
108 R2 = CurDAG->getTargetConstant(0, MVT::i32);
109 } else {
110 R1 = Addr;
111 R2 = CurDAG->getTargetConstant(0, MVT::i32);
112 }
113 } else if (Addr.getOpcode() == ISD::ADD) {
114 R1 = Addr.getOperand(0);
115 R2 = Addr.getOperand(1);
116 } else {
117 R1 = Addr;
118 R2 = CurDAG->getTargetConstant(0, MVT::i32);
119 }
120 return true;
121}
122
123bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
124 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
125 Addr.getOpcode() == ISD::TargetGlobalAddress) {
126 return false;
127 }
128 return SelectADDRParam(Addr, R1, R2);
129}
130
131
132bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
133 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
134 Addr.getOpcode() == ISD::TargetGlobalAddress) {
135 return false;
136 }
137
138 if (Addr.getOpcode() == ISD::FrameIndex) {
139 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
140 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
141 R2 = CurDAG->getTargetConstant(0, MVT::i64);
142 } else {
143 R1 = Addr;
144 R2 = CurDAG->getTargetConstant(0, MVT::i64);
145 }
146 } else if (Addr.getOpcode() == ISD::ADD) {
147 R1 = Addr.getOperand(0);
148 R2 = Addr.getOperand(1);
149 } else {
150 R1 = Addr;
151 R2 = CurDAG->getTargetConstant(0, MVT::i64);
152 }
153 return true;
154}
155
156SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
157 unsigned int Opc = N->getOpcode();
158 if (N->isMachineOpcode()) {
159 return NULL; // Already selected.
160 }
161 switch (Opc) {
162 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000163 case ISD::ConstantFP:
164 case ISD::Constant: {
165 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
166 // XXX: Custom immediate lowering not implemented yet. Instead we use
167 // pseudo instructions defined in SIInstructions.td
168 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
169 break;
170 }
171 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
172
173 uint64_t ImmValue = 0;
174 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
175
176 if (N->getOpcode() == ISD::ConstantFP) {
177 // XXX: 64-bit Immediates not supported yet
178 assert(N->getValueType(0) != MVT::f64);
179
180 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
181 APFloat Value = C->getValueAPF();
182 float FloatValue = Value.convertToFloat();
183 if (FloatValue == 0.0) {
184 ImmReg = AMDGPU::ZERO;
185 } else if (FloatValue == 0.5) {
186 ImmReg = AMDGPU::HALF;
187 } else if (FloatValue == 1.0) {
188 ImmReg = AMDGPU::ONE;
189 } else {
190 ImmValue = Value.bitcastToAPInt().getZExtValue();
191 }
192 } else {
193 // XXX: 64-bit Immediates not supported yet
194 assert(N->getValueType(0) != MVT::i64);
195
196 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
197 if (C->getZExtValue() == 0) {
198 ImmReg = AMDGPU::ZERO;
199 } else if (C->getZExtValue() == 1) {
200 ImmReg = AMDGPU::ONE_INT;
201 } else {
202 ImmValue = C->getZExtValue();
203 }
204 }
205
206 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
207 Use != SDNode::use_end(); Use = Next) {
208 Next = llvm::next(Use);
209 std::vector<SDValue> Ops;
210 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
211 Ops.push_back(Use->getOperand(i));
212 }
213
214 if (!Use->isMachineOpcode()) {
215 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
216 // We can only use literal constants (e.g. AMDGPU::ZERO,
217 // AMDGPU::ONE, etc) in machine opcodes.
218 continue;
219 }
220 } else {
Vincent Lejeunef694c102013-02-14 16:55:01 +0000221 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
222 (TII->get(Use->getMachineOpcode()).TSFlags &
223 R600_InstFlag::VECTOR)) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000224 continue;
225 }
226
227 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
228 assert(ImmIdx != -1);
229
230 // subtract one from ImmIdx, because the DST operand is usually index
231 // 0 for MachineInstrs, but we have no DST in the Ops vector.
232 ImmIdx--;
233
234 // Check that we aren't already using an immediate.
235 // XXX: It's possible for an instruction to have more than one
236 // immediate operand, but this is not supported yet.
237 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
238 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
239 assert(C);
240
241 if (C->getZExtValue() != 0) {
242 // This instruction is already using an immediate.
243 continue;
244 }
245
246 // Set the immediate value
247 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
248 }
249 }
250 // Set the immediate register
251 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
252
253 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
254 }
255 break;
256 }
257 }
Tom Stellard365366f2013-01-23 02:09:06 +0000258 SDNode *Result = SelectCode(N);
259
260 // Fold operands of selected node
261
262 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
263 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
264 const R600InstrInfo *TII =
265 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunef694c102013-02-14 16:55:01 +0000266 if (Result && Result->isMachineOpcode() &&
267 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellard49269212013-01-31 22:11:54 +0000268 && TII->isALUInstr(Result->getMachineOpcode())) {
269 // Fold FNEG/FABS/CONST_ADDRESS
270 // TODO: Isel can generate multiple MachineInst, we need to recursively
271 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000272 bool IsModified = false;
273 do {
274 std::vector<SDValue> Ops;
275 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
276 I != E; ++I)
277 Ops.push_back(*I);
278 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
279 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000280 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000281 }
282 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000283
284 // If node has a single use which is CLAMP_R600, folds it
285 if (Result->hasOneUse() && Result->isMachineOpcode()) {
286 SDNode *PotentialClamp = *Result->use_begin();
287 if (PotentialClamp->isMachineOpcode() &&
288 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
289 unsigned ClampIdx =
290 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
291 std::vector<SDValue> Ops;
292 unsigned NumOp = Result->getNumOperands();
293 for (unsigned i = 0; i < NumOp; ++i) {
294 Ops.push_back(Result->getOperand(i));
295 }
296 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
297 Result = CurDAG->SelectNodeTo(PotentialClamp,
298 Result->getMachineOpcode(), PotentialClamp->getVTList(),
299 Ops.data(), NumOp);
300 }
301 }
Tom Stellard365366f2013-01-23 02:09:06 +0000302 }
303 }
304
305 return Result;
306}
307
308bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
309 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
310 int OperandIdx[] = {
311 TII->getOperandIdx(Opcode, R600Operands::SRC0),
312 TII->getOperandIdx(Opcode, R600Operands::SRC1),
313 TII->getOperandIdx(Opcode, R600Operands::SRC2)
314 };
315 int SelIdx[] = {
316 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
317 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
318 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
319 };
Tom Stellard49269212013-01-31 22:11:54 +0000320 int NegIdx[] = {
321 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
322 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
323 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
324 };
325 int AbsIdx[] = {
326 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
327 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
328 -1
329 };
330
Tom Stellard365366f2013-01-23 02:09:06 +0000331 for (unsigned i = 0; i < 3; i++) {
332 if (OperandIdx[i] < 0)
333 return false;
334 SDValue Operand = Ops[OperandIdx[i] - 1];
335 switch (Operand.getOpcode()) {
336 case AMDGPUISD::CONST_ADDRESS: {
337 SDValue CstOffset;
338 if (!Operand.getValueType().isVector() &&
339 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
340 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
341 Ops[SelIdx[i] - 1] = CstOffset;
342 return true;
343 }
344 }
345 break;
Tom Stellard49269212013-01-31 22:11:54 +0000346 case ISD::FNEG:
347 if (NegIdx[i] < 0)
348 break;
349 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
350 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
351 return true;
352 case ISD::FABS:
353 if (AbsIdx[i] < 0)
354 break;
355 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
356 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
357 return true;
Tom Stellarddd04c832013-01-31 22:11:53 +0000358 case ISD::BITCAST:
359 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
360 return true;
Tom Stellard365366f2013-01-23 02:09:06 +0000361 default:
362 break;
363 }
364 }
365 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000366}
367
368bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
369 if (!ptr) {
370 return false;
371 }
372 Type *ptrType = ptr->getType();
373 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
374}
375
376const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
377 if (!V) {
378 return NULL;
379 }
380 const Value *ret = NULL;
381 ValueMap<const Value *, bool> ValueBitMap;
382 std::queue<const Value *, std::list<const Value *> > ValueQueue;
383 ValueQueue.push(V);
384 while (!ValueQueue.empty()) {
385 V = ValueQueue.front();
386 if (ValueBitMap.find(V) == ValueBitMap.end()) {
387 ValueBitMap[V] = true;
388 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
389 ret = V;
390 break;
391 } else if (dyn_cast<GlobalVariable>(V)) {
392 ret = V;
393 break;
394 } else if (dyn_cast<Constant>(V)) {
395 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
396 if (CE) {
397 ValueQueue.push(CE->getOperand(0));
398 }
399 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
400 ret = AI;
401 break;
402 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
403 uint32_t numOps = I->getNumOperands();
404 for (uint32_t x = 0; x < numOps; ++x) {
405 ValueQueue.push(I->getOperand(x));
406 }
407 } else {
408 assert(!"Found a Value that we didn't know how to handle!");
409 }
410 }
411 ValueQueue.pop();
412 }
413 return ret;
414}
415
416bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
417 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
418}
419
420bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
421 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
422 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
423 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
424}
425
426bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
427 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
428}
429
430bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
431 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
432}
433
434bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
435 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
436 return true;
437 }
438 MachineMemOperand *MMO = N->getMemOperand();
439 const Value *V = MMO->getValue();
440 const Value *BV = getBasePointerValue(V);
441 if (MMO
442 && MMO->getValue()
443 && ((V && dyn_cast<GlobalValue>(V))
444 || (BV && dyn_cast<GlobalValue>(
445 getBasePointerValue(MMO->getValue()))))) {
446 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
447 } else {
448 return false;
449 }
450}
451
452bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
453 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
454}
455
456bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
457 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
458}
459
460bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
461 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
462}
463
464bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
465 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
466}
467
468bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
469 MachineMemOperand *MMO = N->getMemOperand();
470 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
471 if (MMO) {
472 const Value *V = MMO->getValue();
473 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
474 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
475 return true;
476 }
477 }
478 }
479 return false;
480}
481
482bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
483 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
484 // Check to make sure we are not a constant pool load or a constant load
485 // that is marked as a private load
486 if (isCPLoad(N) || isConstantLoad(N, -1)) {
487 return false;
488 }
489 }
490 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
491 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
492 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
493 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
494 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
495 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
496 return true;
497 }
498 return false;
499}
500
501const char *AMDGPUDAGToDAGISel::getPassName() const {
502 return "AMDGPU DAG->DAG Pattern Instruction Selection";
503}
504
505#ifdef DEBUGTMP
506#undef INT64_C
507#endif
508#undef DEBUGTMP
509
510///==== AMDGPU Functions ====///
511
Tom Stellard365366f2013-01-23 02:09:06 +0000512bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
513 SDValue& IntPtr) {
514 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
515 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
516 return true;
517 }
518 return false;
519}
520
521bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
522 SDValue& BaseReg, SDValue &Offset) {
523 if (!dyn_cast<ConstantSDNode>(Addr)) {
524 BaseReg = Addr;
525 Offset = CurDAG->getIntPtrConstant(0, true);
526 return true;
527 }
528 return false;
529}
530
Tom Stellard75aadc22012-12-11 21:25:42 +0000531bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
532 SDValue &Offset) {
533 ConstantSDNode * IMMOffset;
534
535 if (Addr.getOpcode() == ISD::ADD
536 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
537 && isInt<16>(IMMOffset->getZExtValue())) {
538
539 Base = Addr.getOperand(0);
540 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
541 return true;
542 // If the pointer address is constant, we can move it to the offset field.
543 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
544 && isInt<16>(IMMOffset->getZExtValue())) {
545 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
546 CurDAG->getEntryNode().getDebugLoc(),
547 AMDGPU::ZERO, MVT::i32);
548 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
549 return true;
550 }
551
552 // Default case, no offset
553 Base = Addr;
554 Offset = CurDAG->getTargetConstant(0, MVT::i32);
555 return true;
556}
557
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000558bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
559 SDValue &Offset) {
560 ConstantSDNode *C;
561
562 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
563 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
564 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
565 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
566 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
567 Base = Addr.getOperand(0);
568 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
569 } else {
570 Base = Addr;
571 Offset = CurDAG->getTargetConstant(0, MVT::i32);
572 }
573
574 return true;
575}