1 //===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //==-----------------------------------------------------------------------===//
11 /// \brief Defines an instruction selector for the AMDGPU target.
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"
23 #include "llvm/CodeGen/SelectionDAG.h"
29 //===----------------------------------------------------------------------===//
30 // Instruction Selector Implementation
31 //===----------------------------------------------------------------------===//
34 /// AMDGPU specific code to select AMDGPU machine instructions for
35 /// SelectionDAG operations.
36 class 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;
41 AMDGPUDAGToDAGISel(TargetMachine &TM);
42 virtual ~AMDGPUDAGToDAGISel();
44 SDNode *Select(SDNode *N);
45 virtual const char *getPassName() const;
48 inline SDValue getSmallIPtrImm(unsigned Imm);
49 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
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);
56 static bool checkType(const Value *ptr, unsigned int addrspace);
57 static const Value *getBasePointerValue(const Value *V);
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);
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);
72 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
73 bool SelectGlobalValueVariableOffset(SDValue Addr,
74 SDValue &BaseReg, SDValue& Offset);
75 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);
79 // Include the pieces autogenerated from the target description.
80 #include "AMDGPUGenDAGISel.inc"
82 } // end anonymous namespace
84 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
85 // DAG, ready for instruction scheduling.
86 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
88 return new AMDGPUDAGToDAGISel(TM);
91 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
93 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
96 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
99 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
100 return CurDAG->getTargetConstant(Imm, MVT::i32);
103 bool AMDGPUDAGToDAGISel::SelectADDRParam(
104 SDValue Addr, SDValue& R1, SDValue& R2) {
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);
112 R2 = CurDAG->getTargetConstant(0, MVT::i32);
114 } else if (Addr.getOpcode() == ISD::ADD) {
115 R1 = Addr.getOperand(0);
116 R2 = Addr.getOperand(1);
119 R2 = CurDAG->getTargetConstant(0, MVT::i32);
124 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
125 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
126 Addr.getOpcode() == ISD::TargetGlobalAddress) {
129 return SelectADDRParam(Addr, R1, R2);
133 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
134 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
135 Addr.getOpcode() == ISD::TargetGlobalAddress) {
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);
145 R2 = CurDAG->getTargetConstant(0, MVT::i64);
147 } else if (Addr.getOpcode() == ISD::ADD) {
148 R1 = Addr.getOperand(0);
149 R2 = Addr.getOperand(1);
152 R2 = CurDAG->getTargetConstant(0, MVT::i64);
157 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
158 unsigned int Opc = N->getOpcode();
159 if (N->isMachineOpcode()) {
160 return NULL; // Already selected.
164 case ISD::FrameIndex: {
165 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
166 unsigned int FI = FIN->getIndex();
167 EVT OpVT = N->getValueType(0);
168 unsigned int NewOpc = AMDGPU::COPY;
169 SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i32);
170 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, TFI);
174 case ISD::ConstantFP:
175 case ISD::Constant: {
176 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
177 // XXX: Custom immediate lowering not implemented yet. Instead we use
178 // pseudo instructions defined in SIInstructions.td
179 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
182 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
184 uint64_t ImmValue = 0;
185 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
187 if (N->getOpcode() == ISD::ConstantFP) {
188 // XXX: 64-bit Immediates not supported yet
189 assert(N->getValueType(0) != MVT::f64);
191 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
192 APFloat Value = C->getValueAPF();
193 float FloatValue = Value.convertToFloat();
194 if (FloatValue == 0.0) {
195 ImmReg = AMDGPU::ZERO;
196 } else if (FloatValue == 0.5) {
197 ImmReg = AMDGPU::HALF;
198 } else if (FloatValue == 1.0) {
199 ImmReg = AMDGPU::ONE;
201 ImmValue = Value.bitcastToAPInt().getZExtValue();
204 // XXX: 64-bit Immediates not supported yet
205 assert(N->getValueType(0) != MVT::i64);
207 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
208 if (C->getZExtValue() == 0) {
209 ImmReg = AMDGPU::ZERO;
210 } else if (C->getZExtValue() == 1) {
211 ImmReg = AMDGPU::ONE_INT;
213 ImmValue = C->getZExtValue();
217 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
218 Use != SDNode::use_end(); Use = Next) {
219 Next = llvm::next(Use);
220 std::vector<SDValue> Ops;
221 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
222 Ops.push_back(Use->getOperand(i));
225 if (!Use->isMachineOpcode()) {
226 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
227 // We can only use literal constants (e.g. AMDGPU::ZERO,
228 // AMDGPU::ONE, etc) in machine opcodes.
232 if (!TII->isALUInstr(Use->getMachineOpcode())) {
236 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
237 assert(ImmIdx != -1);
239 // subtract one from ImmIdx, because the DST operand is usually index
240 // 0 for MachineInstrs, but we have no DST in the Ops vector.
243 // Check that we aren't already using an immediate.
244 // XXX: It's possible for an instruction to have more than one
245 // immediate operand, but this is not supported yet.
246 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
247 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
250 if (C->getZExtValue() != 0) {
251 // This instruction is already using an immediate.
255 // Set the immediate value
256 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
259 // Set the immediate register
260 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
262 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
267 SDNode *Result = SelectCode(N);
269 // Fold operands of selected node
271 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
272 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
273 const R600InstrInfo *TII =
274 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
275 if (Result && Result->isMachineOpcode()
276 && TII->isALUInstr(Result->getMachineOpcode())) {
277 // Fold FNEG/FABS/CONST_ADDRESS
278 // TODO: Isel can generate multiple MachineInst, we need to recursively
280 bool IsModified = false;
282 std::vector<SDValue> Ops;
283 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
286 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
288 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
290 } while (IsModified);
292 // If node has a single use which is CLAMP_R600, folds it
293 if (Result->hasOneUse() && Result->isMachineOpcode()) {
294 SDNode *PotentialClamp = *Result->use_begin();
295 if (PotentialClamp->isMachineOpcode() &&
296 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
298 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
299 std::vector<SDValue> Ops;
300 unsigned NumOp = Result->getNumOperands();
301 for (unsigned i = 0; i < NumOp; ++i) {
302 Ops.push_back(Result->getOperand(i));
304 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
305 Result = CurDAG->SelectNodeTo(PotentialClamp,
306 Result->getMachineOpcode(), PotentialClamp->getVTList(),
316 bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
317 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
319 TII->getOperandIdx(Opcode, R600Operands::SRC0),
320 TII->getOperandIdx(Opcode, R600Operands::SRC1),
321 TII->getOperandIdx(Opcode, R600Operands::SRC2)
324 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
325 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
326 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
329 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
330 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
331 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
334 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
335 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
339 for (unsigned i = 0; i < 3; i++) {
340 if (OperandIdx[i] < 0)
342 SDValue Operand = Ops[OperandIdx[i] - 1];
343 switch (Operand.getOpcode()) {
344 case AMDGPUISD::CONST_ADDRESS: {
346 if (!Operand.getValueType().isVector() &&
347 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
348 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
349 Ops[SelIdx[i] - 1] = CstOffset;
357 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
358 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
363 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
364 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
367 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
376 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
380 Type *ptrType = ptr->getType();
381 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
384 const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
388 const Value *ret = NULL;
389 ValueMap<const Value *, bool> ValueBitMap;
390 std::queue<const Value *, std::list<const Value *> > ValueQueue;
392 while (!ValueQueue.empty()) {
393 V = ValueQueue.front();
394 if (ValueBitMap.find(V) == ValueBitMap.end()) {
395 ValueBitMap[V] = true;
396 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
399 } else if (dyn_cast<GlobalVariable>(V)) {
402 } else if (dyn_cast<Constant>(V)) {
403 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
405 ValueQueue.push(CE->getOperand(0));
407 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
410 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
411 uint32_t numOps = I->getNumOperands();
412 for (uint32_t x = 0; x < numOps; ++x) {
413 ValueQueue.push(I->getOperand(x));
416 assert(!"Found a Value that we didn't know how to handle!");
424 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
425 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
428 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
429 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
430 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
431 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
434 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
435 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
438 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
439 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
442 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
443 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
446 MachineMemOperand *MMO = N->getMemOperand();
447 const Value *V = MMO->getValue();
448 const Value *BV = getBasePointerValue(V);
451 && ((V && dyn_cast<GlobalValue>(V))
452 || (BV && dyn_cast<GlobalValue>(
453 getBasePointerValue(MMO->getValue()))))) {
454 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
460 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
461 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
464 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
465 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
468 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
469 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
472 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
473 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
476 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
477 MachineMemOperand *MMO = N->getMemOperand();
478 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
480 const Value *V = MMO->getValue();
481 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
482 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
490 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
491 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
492 // Check to make sure we are not a constant pool load or a constant load
493 // that is marked as a private load
494 if (isCPLoad(N) || isConstantLoad(N, -1)) {
498 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
499 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
500 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
501 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
502 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
503 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
509 const char *AMDGPUDAGToDAGISel::getPassName() const {
510 return "AMDGPU DAG->DAG Pattern Instruction Selection";
518 ///==== AMDGPU Functions ====///
520 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
522 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
523 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
529 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
530 SDValue& BaseReg, SDValue &Offset) {
531 if (!dyn_cast<ConstantSDNode>(Addr)) {
533 Offset = CurDAG->getIntPtrConstant(0, true);
539 bool AMDGPUDAGToDAGISel::SelectADDR8BitOffset(SDValue Addr, SDValue& Base,
541 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
542 Addr.getOpcode() == ISD::TargetGlobalAddress) {
547 if (Addr.getOpcode() == ISD::ADD) {
550 // Find the base ptr and the offset
551 for (unsigned i = 0; i < Addr.getNumOperands(); i++) {
552 SDValue Arg = Addr.getOperand(i);
553 ConstantSDNode * OffsetNode = dyn_cast<ConstantSDNode>(Arg);
554 // This arg isn't a constant so it must be the base PTR.
556 Base = Addr.getOperand(i);
559 // Check if the constant argument fits in 8-bits. The offset is in bytes
560 // so we need to convert it to dwords.
561 if (isUInt<8>(OffsetNode->getZExtValue() >> 2)) {
563 Offset = CurDAG->getTargetConstant(OffsetNode->getZExtValue() >> 2,
570 // Default case, no offset
572 Offset = CurDAG->getTargetConstant(0, MVT::i32);
576 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
578 ConstantSDNode * IMMOffset;
580 if (Addr.getOpcode() == ISD::ADD
581 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
582 && isInt<16>(IMMOffset->getZExtValue())) {
584 Base = Addr.getOperand(0);
585 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
587 // If the pointer address is constant, we can move it to the offset field.
588 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
589 && isInt<16>(IMMOffset->getZExtValue())) {
590 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
591 CurDAG->getEntryNode().getDebugLoc(),
592 AMDGPU::ZERO, MVT::i32);
593 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
597 // Default case, no offset
599 Offset = CurDAG->getTargetConstant(0, MVT::i32);
603 bool AMDGPUDAGToDAGISel::SelectADDRReg(SDValue Addr, SDValue& Base,
605 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
606 Addr.getOpcode() == ISD::TargetGlobalAddress ||
607 Addr.getOpcode() != ISD::ADD) {
611 Base = Addr.getOperand(0);
612 Offset = Addr.getOperand(1);