blob: 2e726e949db86a8c1d97a339137f1f9150894920 [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: {
Vincent Lejeunef940fd02013-02-14 16:57:19 +0000337 if (i == 2)
338 break;
Tom Stellard365366f2013-01-23 02:09:06 +0000339 SDValue CstOffset;
340 if (!Operand.getValueType().isVector() &&
341 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
342 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
343 Ops[SelIdx[i] - 1] = CstOffset;
344 return true;
345 }
346 }
347 break;
Tom Stellard49269212013-01-31 22:11:54 +0000348 case ISD::FNEG:
349 if (NegIdx[i] < 0)
350 break;
351 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
352 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
353 return true;
354 case ISD::FABS:
355 if (AbsIdx[i] < 0)
356 break;
357 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
358 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
359 return true;
Tom Stellarddd04c832013-01-31 22:11:53 +0000360 case ISD::BITCAST:
361 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
362 return true;
Tom Stellard365366f2013-01-23 02:09:06 +0000363 default:
364 break;
365 }
366 }
367 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000368}
369
370bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
371 if (!ptr) {
372 return false;
373 }
374 Type *ptrType = ptr->getType();
375 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
376}
377
378const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
379 if (!V) {
380 return NULL;
381 }
382 const Value *ret = NULL;
383 ValueMap<const Value *, bool> ValueBitMap;
384 std::queue<const Value *, std::list<const Value *> > ValueQueue;
385 ValueQueue.push(V);
386 while (!ValueQueue.empty()) {
387 V = ValueQueue.front();
388 if (ValueBitMap.find(V) == ValueBitMap.end()) {
389 ValueBitMap[V] = true;
390 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
391 ret = V;
392 break;
393 } else if (dyn_cast<GlobalVariable>(V)) {
394 ret = V;
395 break;
396 } else if (dyn_cast<Constant>(V)) {
397 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
398 if (CE) {
399 ValueQueue.push(CE->getOperand(0));
400 }
401 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
402 ret = AI;
403 break;
404 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
405 uint32_t numOps = I->getNumOperands();
406 for (uint32_t x = 0; x < numOps; ++x) {
407 ValueQueue.push(I->getOperand(x));
408 }
409 } else {
410 assert(!"Found a Value that we didn't know how to handle!");
411 }
412 }
413 ValueQueue.pop();
414 }
415 return ret;
416}
417
418bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
419 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
420}
421
422bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
423 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
424 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
425 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
426}
427
428bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
429 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
430}
431
432bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
433 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
434}
435
436bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
437 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
438 return true;
439 }
440 MachineMemOperand *MMO = N->getMemOperand();
441 const Value *V = MMO->getValue();
442 const Value *BV = getBasePointerValue(V);
443 if (MMO
444 && MMO->getValue()
445 && ((V && dyn_cast<GlobalValue>(V))
446 || (BV && dyn_cast<GlobalValue>(
447 getBasePointerValue(MMO->getValue()))))) {
448 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
449 } else {
450 return false;
451 }
452}
453
454bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
455 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
456}
457
458bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
459 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
460}
461
462bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
463 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
464}
465
466bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
467 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
468}
469
470bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
471 MachineMemOperand *MMO = N->getMemOperand();
472 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
473 if (MMO) {
474 const Value *V = MMO->getValue();
475 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
476 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
477 return true;
478 }
479 }
480 }
481 return false;
482}
483
484bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
485 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
486 // Check to make sure we are not a constant pool load or a constant load
487 // that is marked as a private load
488 if (isCPLoad(N) || isConstantLoad(N, -1)) {
489 return false;
490 }
491 }
492 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
493 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
494 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
495 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
496 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
497 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
498 return true;
499 }
500 return false;
501}
502
503const char *AMDGPUDAGToDAGISel::getPassName() const {
504 return "AMDGPU DAG->DAG Pattern Instruction Selection";
505}
506
507#ifdef DEBUGTMP
508#undef INT64_C
509#endif
510#undef DEBUGTMP
511
512///==== AMDGPU Functions ====///
513
Tom Stellard365366f2013-01-23 02:09:06 +0000514bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
515 SDValue& IntPtr) {
516 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
517 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
518 return true;
519 }
520 return false;
521}
522
523bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
524 SDValue& BaseReg, SDValue &Offset) {
525 if (!dyn_cast<ConstantSDNode>(Addr)) {
526 BaseReg = Addr;
527 Offset = CurDAG->getIntPtrConstant(0, true);
528 return true;
529 }
530 return false;
531}
532
Tom Stellard75aadc22012-12-11 21:25:42 +0000533bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
534 SDValue &Offset) {
535 ConstantSDNode * IMMOffset;
536
537 if (Addr.getOpcode() == ISD::ADD
538 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
539 && isInt<16>(IMMOffset->getZExtValue())) {
540
541 Base = Addr.getOperand(0);
542 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
543 return true;
544 // If the pointer address is constant, we can move it to the offset field.
545 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
546 && isInt<16>(IMMOffset->getZExtValue())) {
547 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
548 CurDAG->getEntryNode().getDebugLoc(),
549 AMDGPU::ZERO, MVT::i32);
550 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
551 return true;
552 }
553
554 // Default case, no offset
555 Base = Addr;
556 Offset = CurDAG->getTargetConstant(0, MVT::i32);
557 return true;
558}
559
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000560bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
561 SDValue &Offset) {
562 ConstantSDNode *C;
563
564 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
565 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
566 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
567 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
568 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
569 Base = Addr.getOperand(0);
570 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
571 } else {
572 Base = Addr;
573 Offset = CurDAG->getTargetConstant(0, MVT::i32);
574 }
575
576 return true;
577}