blob: a88e8c7fc64df50169458f1779c201dec1ee9bed [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 {
221 if (!TII->isALUInstr(Use->getMachineOpcode())) {
222 continue;
223 }
224
225 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
226 assert(ImmIdx != -1);
227
228 // subtract one from ImmIdx, because the DST operand is usually index
229 // 0 for MachineInstrs, but we have no DST in the Ops vector.
230 ImmIdx--;
231
232 // Check that we aren't already using an immediate.
233 // XXX: It's possible for an instruction to have more than one
234 // immediate operand, but this is not supported yet.
235 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
236 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
237 assert(C);
238
239 if (C->getZExtValue() != 0) {
240 // This instruction is already using an immediate.
241 continue;
242 }
243
244 // Set the immediate value
245 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
246 }
247 }
248 // Set the immediate register
249 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
250
251 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
252 }
253 break;
254 }
255 }
Tom Stellard365366f2013-01-23 02:09:06 +0000256 SDNode *Result = SelectCode(N);
257
258 // Fold operands of selected node
259
260 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
261 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
262 const R600InstrInfo *TII =
263 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Tom Stellard49269212013-01-31 22:11:54 +0000264 if (Result && Result->isMachineOpcode()
265 && TII->isALUInstr(Result->getMachineOpcode())) {
266 // Fold FNEG/FABS/CONST_ADDRESS
267 // TODO: Isel can generate multiple MachineInst, we need to recursively
268 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000269 bool IsModified = false;
270 do {
271 std::vector<SDValue> Ops;
272 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
273 I != E; ++I)
274 Ops.push_back(*I);
275 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
276 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000277 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000278 }
279 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000280
281 // If node has a single use which is CLAMP_R600, folds it
282 if (Result->hasOneUse() && Result->isMachineOpcode()) {
283 SDNode *PotentialClamp = *Result->use_begin();
284 if (PotentialClamp->isMachineOpcode() &&
285 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
286 unsigned ClampIdx =
287 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
288 std::vector<SDValue> Ops;
289 unsigned NumOp = Result->getNumOperands();
290 for (unsigned i = 0; i < NumOp; ++i) {
291 Ops.push_back(Result->getOperand(i));
292 }
293 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
294 Result = CurDAG->SelectNodeTo(PotentialClamp,
295 Result->getMachineOpcode(), PotentialClamp->getVTList(),
296 Ops.data(), NumOp);
297 }
298 }
Tom Stellard365366f2013-01-23 02:09:06 +0000299 }
300 }
301
302 return Result;
303}
304
305bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
306 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
307 int OperandIdx[] = {
308 TII->getOperandIdx(Opcode, R600Operands::SRC0),
309 TII->getOperandIdx(Opcode, R600Operands::SRC1),
310 TII->getOperandIdx(Opcode, R600Operands::SRC2)
311 };
312 int SelIdx[] = {
313 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
314 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
315 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
316 };
Tom Stellard49269212013-01-31 22:11:54 +0000317 int NegIdx[] = {
318 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
319 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
320 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
321 };
322 int AbsIdx[] = {
323 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
324 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
325 -1
326 };
327
Tom Stellard365366f2013-01-23 02:09:06 +0000328 for (unsigned i = 0; i < 3; i++) {
329 if (OperandIdx[i] < 0)
330 return false;
331 SDValue Operand = Ops[OperandIdx[i] - 1];
332 switch (Operand.getOpcode()) {
333 case AMDGPUISD::CONST_ADDRESS: {
334 SDValue CstOffset;
335 if (!Operand.getValueType().isVector() &&
336 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
337 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
338 Ops[SelIdx[i] - 1] = CstOffset;
339 return true;
340 }
341 }
342 break;
Tom Stellard49269212013-01-31 22:11:54 +0000343 case ISD::FNEG:
344 if (NegIdx[i] < 0)
345 break;
346 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
347 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
348 return true;
349 case ISD::FABS:
350 if (AbsIdx[i] < 0)
351 break;
352 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
353 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
354 return true;
Tom Stellarddd04c832013-01-31 22:11:53 +0000355 case ISD::BITCAST:
356 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
357 return true;
Tom Stellard365366f2013-01-23 02:09:06 +0000358 default:
359 break;
360 }
361 }
362 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000363}
364
365bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
366 if (!ptr) {
367 return false;
368 }
369 Type *ptrType = ptr->getType();
370 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
371}
372
373const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
374 if (!V) {
375 return NULL;
376 }
377 const Value *ret = NULL;
378 ValueMap<const Value *, bool> ValueBitMap;
379 std::queue<const Value *, std::list<const Value *> > ValueQueue;
380 ValueQueue.push(V);
381 while (!ValueQueue.empty()) {
382 V = ValueQueue.front();
383 if (ValueBitMap.find(V) == ValueBitMap.end()) {
384 ValueBitMap[V] = true;
385 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
386 ret = V;
387 break;
388 } else if (dyn_cast<GlobalVariable>(V)) {
389 ret = V;
390 break;
391 } else if (dyn_cast<Constant>(V)) {
392 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
393 if (CE) {
394 ValueQueue.push(CE->getOperand(0));
395 }
396 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
397 ret = AI;
398 break;
399 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
400 uint32_t numOps = I->getNumOperands();
401 for (uint32_t x = 0; x < numOps; ++x) {
402 ValueQueue.push(I->getOperand(x));
403 }
404 } else {
405 assert(!"Found a Value that we didn't know how to handle!");
406 }
407 }
408 ValueQueue.pop();
409 }
410 return ret;
411}
412
413bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
414 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
415}
416
417bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
418 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
419 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
420 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
421}
422
423bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
424 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
425}
426
427bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
428 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
429}
430
431bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
432 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
433 return true;
434 }
435 MachineMemOperand *MMO = N->getMemOperand();
436 const Value *V = MMO->getValue();
437 const Value *BV = getBasePointerValue(V);
438 if (MMO
439 && MMO->getValue()
440 && ((V && dyn_cast<GlobalValue>(V))
441 || (BV && dyn_cast<GlobalValue>(
442 getBasePointerValue(MMO->getValue()))))) {
443 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
444 } else {
445 return false;
446 }
447}
448
449bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
450 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
451}
452
453bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
454 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
455}
456
457bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
458 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
459}
460
461bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
462 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
463}
464
465bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
466 MachineMemOperand *MMO = N->getMemOperand();
467 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
468 if (MMO) {
469 const Value *V = MMO->getValue();
470 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
471 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
472 return true;
473 }
474 }
475 }
476 return false;
477}
478
479bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
480 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
481 // Check to make sure we are not a constant pool load or a constant load
482 // that is marked as a private load
483 if (isCPLoad(N) || isConstantLoad(N, -1)) {
484 return false;
485 }
486 }
487 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
488 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
489 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
490 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
491 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
492 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
493 return true;
494 }
495 return false;
496}
497
498const char *AMDGPUDAGToDAGISel::getPassName() const {
499 return "AMDGPU DAG->DAG Pattern Instruction Selection";
500}
501
502#ifdef DEBUGTMP
503#undef INT64_C
504#endif
505#undef DEBUGTMP
506
507///==== AMDGPU Functions ====///
508
Tom Stellard365366f2013-01-23 02:09:06 +0000509bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
510 SDValue& IntPtr) {
511 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
512 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
513 return true;
514 }
515 return false;
516}
517
518bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
519 SDValue& BaseReg, SDValue &Offset) {
520 if (!dyn_cast<ConstantSDNode>(Addr)) {
521 BaseReg = Addr;
522 Offset = CurDAG->getIntPtrConstant(0, true);
523 return true;
524 }
525 return false;
526}
527
Tom Stellard75aadc22012-12-11 21:25:42 +0000528bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
529 SDValue &Offset) {
530 ConstantSDNode * IMMOffset;
531
532 if (Addr.getOpcode() == ISD::ADD
533 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
534 && isInt<16>(IMMOffset->getZExtValue())) {
535
536 Base = Addr.getOperand(0);
537 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
538 return true;
539 // If the pointer address is constant, we can move it to the offset field.
540 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
541 && isInt<16>(IMMOffset->getZExtValue())) {
542 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
543 CurDAG->getEntryNode().getDebugLoc(),
544 AMDGPU::ZERO, MVT::i32);
545 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
546 return true;
547 }
548
549 // Default case, no offset
550 Base = Addr;
551 Offset = CurDAG->getTargetConstant(0, MVT::i32);
552 return true;
553}
554
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000555bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
556 SDValue &Offset) {
557 ConstantSDNode *C;
558
559 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
560 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
561 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
562 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
563 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
564 Base = Addr.getOperand(0);
565 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
566 } else {
567 Base = Addr;
568 Offset = CurDAG->getTargetConstant(0, MVT::i32);
569 }
570
571 return true;
572}