blob: 26994090643e94ff9afd6abcc1855926666b98a8 [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 SelectADDR8BitOffset(SDValue Addr, SDValue& Base, SDValue& Offset);
76 bool SelectADDRReg(SDValue Addr, SDValue& Base, SDValue& Offset);
77 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000078 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000079
80 // Include the pieces autogenerated from the target description.
81#include "AMDGPUGenDAGISel.inc"
82};
83} // end anonymous namespace
84
85/// \brief This pass converts a legalized DAG into a AMDGPU-specific
86// DAG, ready for instruction scheduling.
87FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
88 ) {
89 return new AMDGPUDAGToDAGISel(TM);
90}
91
92AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
93 )
94 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
95}
96
97AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
98}
99
100SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
101 return CurDAG->getTargetConstant(Imm, MVT::i32);
102}
103
104bool AMDGPUDAGToDAGISel::SelectADDRParam(
105 SDValue Addr, SDValue& R1, SDValue& R2) {
106
107 if (Addr.getOpcode() == ISD::FrameIndex) {
108 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
109 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
110 R2 = CurDAG->getTargetConstant(0, MVT::i32);
111 } else {
112 R1 = Addr;
113 R2 = CurDAG->getTargetConstant(0, MVT::i32);
114 }
115 } else if (Addr.getOpcode() == ISD::ADD) {
116 R1 = Addr.getOperand(0);
117 R2 = Addr.getOperand(1);
118 } else {
119 R1 = Addr;
120 R2 = CurDAG->getTargetConstant(0, MVT::i32);
121 }
122 return true;
123}
124
125bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
126 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
127 Addr.getOpcode() == ISD::TargetGlobalAddress) {
128 return false;
129 }
130 return SelectADDRParam(Addr, R1, R2);
131}
132
133
134bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
135 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
136 Addr.getOpcode() == ISD::TargetGlobalAddress) {
137 return false;
138 }
139
140 if (Addr.getOpcode() == ISD::FrameIndex) {
141 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
142 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
143 R2 = CurDAG->getTargetConstant(0, MVT::i64);
144 } else {
145 R1 = Addr;
146 R2 = CurDAG->getTargetConstant(0, MVT::i64);
147 }
148 } else if (Addr.getOpcode() == ISD::ADD) {
149 R1 = Addr.getOperand(0);
150 R2 = Addr.getOperand(1);
151 } else {
152 R1 = Addr;
153 R2 = CurDAG->getTargetConstant(0, MVT::i64);
154 }
155 return true;
156}
157
158SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
159 unsigned int Opc = N->getOpcode();
160 if (N->isMachineOpcode()) {
161 return NULL; // Already selected.
162 }
163 switch (Opc) {
164 default: break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000165 case ISD::ConstantFP:
166 case ISD::Constant: {
167 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
168 // XXX: Custom immediate lowering not implemented yet. Instead we use
169 // pseudo instructions defined in SIInstructions.td
170 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
171 break;
172 }
173 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
174
175 uint64_t ImmValue = 0;
176 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
177
178 if (N->getOpcode() == ISD::ConstantFP) {
179 // XXX: 64-bit Immediates not supported yet
180 assert(N->getValueType(0) != MVT::f64);
181
182 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
183 APFloat Value = C->getValueAPF();
184 float FloatValue = Value.convertToFloat();
185 if (FloatValue == 0.0) {
186 ImmReg = AMDGPU::ZERO;
187 } else if (FloatValue == 0.5) {
188 ImmReg = AMDGPU::HALF;
189 } else if (FloatValue == 1.0) {
190 ImmReg = AMDGPU::ONE;
191 } else {
192 ImmValue = Value.bitcastToAPInt().getZExtValue();
193 }
194 } else {
195 // XXX: 64-bit Immediates not supported yet
196 assert(N->getValueType(0) != MVT::i64);
197
198 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
199 if (C->getZExtValue() == 0) {
200 ImmReg = AMDGPU::ZERO;
201 } else if (C->getZExtValue() == 1) {
202 ImmReg = AMDGPU::ONE_INT;
203 } else {
204 ImmValue = C->getZExtValue();
205 }
206 }
207
208 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
209 Use != SDNode::use_end(); Use = Next) {
210 Next = llvm::next(Use);
211 std::vector<SDValue> Ops;
212 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
213 Ops.push_back(Use->getOperand(i));
214 }
215
216 if (!Use->isMachineOpcode()) {
217 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
218 // We can only use literal constants (e.g. AMDGPU::ZERO,
219 // AMDGPU::ONE, etc) in machine opcodes.
220 continue;
221 }
222 } else {
223 if (!TII->isALUInstr(Use->getMachineOpcode())) {
224 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());
Tom Stellard49269212013-01-31 22:11:54 +0000266 if (Result && Result->isMachineOpcode()
267 && TII->isALUInstr(Result->getMachineOpcode())) {
268 // Fold FNEG/FABS/CONST_ADDRESS
269 // TODO: Isel can generate multiple MachineInst, we need to recursively
270 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000271 bool IsModified = false;
272 do {
273 std::vector<SDValue> Ops;
274 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
275 I != E; ++I)
276 Ops.push_back(*I);
277 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
278 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000279 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000280 }
281 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000282
283 // If node has a single use which is CLAMP_R600, folds it
284 if (Result->hasOneUse() && Result->isMachineOpcode()) {
285 SDNode *PotentialClamp = *Result->use_begin();
286 if (PotentialClamp->isMachineOpcode() &&
287 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
288 unsigned ClampIdx =
289 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
290 std::vector<SDValue> Ops;
291 unsigned NumOp = Result->getNumOperands();
292 for (unsigned i = 0; i < NumOp; ++i) {
293 Ops.push_back(Result->getOperand(i));
294 }
295 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
296 Result = CurDAG->SelectNodeTo(PotentialClamp,
297 Result->getMachineOpcode(), PotentialClamp->getVTList(),
298 Ops.data(), NumOp);
299 }
300 }
Tom Stellard365366f2013-01-23 02:09:06 +0000301 }
302 }
303
304 return Result;
305}
306
307bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
308 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
309 int OperandIdx[] = {
310 TII->getOperandIdx(Opcode, R600Operands::SRC0),
311 TII->getOperandIdx(Opcode, R600Operands::SRC1),
312 TII->getOperandIdx(Opcode, R600Operands::SRC2)
313 };
314 int SelIdx[] = {
315 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
316 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
317 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
318 };
Tom Stellard49269212013-01-31 22:11:54 +0000319 int NegIdx[] = {
320 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
321 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
322 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
323 };
324 int AbsIdx[] = {
325 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
326 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
327 -1
328 };
329
Tom Stellard365366f2013-01-23 02:09:06 +0000330 for (unsigned i = 0; i < 3; i++) {
331 if (OperandIdx[i] < 0)
332 return false;
333 SDValue Operand = Ops[OperandIdx[i] - 1];
334 switch (Operand.getOpcode()) {
335 case AMDGPUISD::CONST_ADDRESS: {
336 SDValue CstOffset;
337 if (!Operand.getValueType().isVector() &&
338 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
339 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
340 Ops[SelIdx[i] - 1] = CstOffset;
341 return true;
342 }
343 }
344 break;
Tom Stellard49269212013-01-31 22:11:54 +0000345 case ISD::FNEG:
346 if (NegIdx[i] < 0)
347 break;
348 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
349 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
350 return true;
351 case ISD::FABS:
352 if (AbsIdx[i] < 0)
353 break;
354 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
355 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
356 return true;
Tom Stellarddd04c832013-01-31 22:11:53 +0000357 case ISD::BITCAST:
358 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
359 return true;
Tom Stellard365366f2013-01-23 02:09:06 +0000360 default:
361 break;
362 }
363 }
364 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000365}
366
367bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
368 if (!ptr) {
369 return false;
370 }
371 Type *ptrType = ptr->getType();
372 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
373}
374
375const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
376 if (!V) {
377 return NULL;
378 }
379 const Value *ret = NULL;
380 ValueMap<const Value *, bool> ValueBitMap;
381 std::queue<const Value *, std::list<const Value *> > ValueQueue;
382 ValueQueue.push(V);
383 while (!ValueQueue.empty()) {
384 V = ValueQueue.front();
385 if (ValueBitMap.find(V) == ValueBitMap.end()) {
386 ValueBitMap[V] = true;
387 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
388 ret = V;
389 break;
390 } else if (dyn_cast<GlobalVariable>(V)) {
391 ret = V;
392 break;
393 } else if (dyn_cast<Constant>(V)) {
394 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
395 if (CE) {
396 ValueQueue.push(CE->getOperand(0));
397 }
398 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
399 ret = AI;
400 break;
401 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
402 uint32_t numOps = I->getNumOperands();
403 for (uint32_t x = 0; x < numOps; ++x) {
404 ValueQueue.push(I->getOperand(x));
405 }
406 } else {
407 assert(!"Found a Value that we didn't know how to handle!");
408 }
409 }
410 ValueQueue.pop();
411 }
412 return ret;
413}
414
415bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
416 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
417}
418
419bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
420 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
421 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
422 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
423}
424
425bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
426 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
427}
428
429bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
430 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
431}
432
433bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
434 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
435 return true;
436 }
437 MachineMemOperand *MMO = N->getMemOperand();
438 const Value *V = MMO->getValue();
439 const Value *BV = getBasePointerValue(V);
440 if (MMO
441 && MMO->getValue()
442 && ((V && dyn_cast<GlobalValue>(V))
443 || (BV && dyn_cast<GlobalValue>(
444 getBasePointerValue(MMO->getValue()))))) {
445 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
446 } else {
447 return false;
448 }
449}
450
451bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
452 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
453}
454
455bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
456 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
457}
458
459bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
460 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
461}
462
463bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
464 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
465}
466
467bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
468 MachineMemOperand *MMO = N->getMemOperand();
469 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
470 if (MMO) {
471 const Value *V = MMO->getValue();
472 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
473 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
474 return true;
475 }
476 }
477 }
478 return false;
479}
480
481bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
482 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
483 // Check to make sure we are not a constant pool load or a constant load
484 // that is marked as a private load
485 if (isCPLoad(N) || isConstantLoad(N, -1)) {
486 return false;
487 }
488 }
489 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
490 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
491 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
492 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
493 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
494 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
495 return true;
496 }
497 return false;
498}
499
500const char *AMDGPUDAGToDAGISel::getPassName() const {
501 return "AMDGPU DAG->DAG Pattern Instruction Selection";
502}
503
504#ifdef DEBUGTMP
505#undef INT64_C
506#endif
507#undef DEBUGTMP
508
509///==== AMDGPU Functions ====///
510
Tom Stellard365366f2013-01-23 02:09:06 +0000511bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
512 SDValue& IntPtr) {
513 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
514 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
515 return true;
516 }
517 return false;
518}
519
520bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
521 SDValue& BaseReg, SDValue &Offset) {
522 if (!dyn_cast<ConstantSDNode>(Addr)) {
523 BaseReg = Addr;
524 Offset = CurDAG->getIntPtrConstant(0, true);
525 return true;
526 }
527 return false;
528}
529
Tom Stellard75aadc22012-12-11 21:25:42 +0000530bool AMDGPUDAGToDAGISel::SelectADDR8BitOffset(SDValue Addr, SDValue& Base,
531 SDValue& Offset) {
532 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
533 Addr.getOpcode() == ISD::TargetGlobalAddress) {
534 return false;
535 }
536
537
538 if (Addr.getOpcode() == ISD::ADD) {
539 bool Match = false;
540
541 // Find the base ptr and the offset
542 for (unsigned i = 0; i < Addr.getNumOperands(); i++) {
543 SDValue Arg = Addr.getOperand(i);
544 ConstantSDNode * OffsetNode = dyn_cast<ConstantSDNode>(Arg);
545 // This arg isn't a constant so it must be the base PTR.
546 if (!OffsetNode) {
547 Base = Addr.getOperand(i);
548 continue;
549 }
550 // Check if the constant argument fits in 8-bits. The offset is in bytes
551 // so we need to convert it to dwords.
552 if (isUInt<8>(OffsetNode->getZExtValue() >> 2)) {
553 Match = true;
554 Offset = CurDAG->getTargetConstant(OffsetNode->getZExtValue() >> 2,
555 MVT::i32);
556 }
557 }
558 return Match;
559 }
560
561 // Default case, no offset
562 Base = Addr;
563 Offset = CurDAG->getTargetConstant(0, MVT::i32);
564 return true;
565}
566
567bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
568 SDValue &Offset) {
569 ConstantSDNode * IMMOffset;
570
571 if (Addr.getOpcode() == ISD::ADD
572 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
573 && isInt<16>(IMMOffset->getZExtValue())) {
574
575 Base = Addr.getOperand(0);
576 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
577 return true;
578 // If the pointer address is constant, we can move it to the offset field.
579 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
580 && isInt<16>(IMMOffset->getZExtValue())) {
581 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
582 CurDAG->getEntryNode().getDebugLoc(),
583 AMDGPU::ZERO, MVT::i32);
584 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
585 return true;
586 }
587
588 // Default case, no offset
589 Base = Addr;
590 Offset = CurDAG->getTargetConstant(0, MVT::i32);
591 return true;
592}
593
594bool AMDGPUDAGToDAGISel::SelectADDRReg(SDValue Addr, SDValue& Base,
595 SDValue& Offset) {
596 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
597 Addr.getOpcode() == ISD::TargetGlobalAddress ||
598 Addr.getOpcode() != ISD::ADD) {
599 return false;
600 }
601
602 Base = Addr.getOperand(0);
603 Offset = Addr.getOperand(1);
604
605 return true;
606}
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000607
608bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
609 SDValue &Offset) {
610 ConstantSDNode *C;
611
612 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
613 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
614 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
615 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
616 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
617 Base = Addr.getOperand(0);
618 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
619 } else {
620 Base = Addr;
621 Offset = CurDAG->getTargetConstant(0, MVT::i32);
622 }
623
624 return true;
625}