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