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 "SIISelLowering.h"
20 #include "llvm/ADT/ValueMap.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
30 //===----------------------------------------------------------------------===//
31 // Instruction Selector Implementation
32 //===----------------------------------------------------------------------===//
35 /// AMDGPU specific code to select AMDGPU machine instructions for
36 /// SelectionDAG operations.
37 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
38 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
39 // make the right decision when generating code for different targets.
40 const AMDGPUSubtarget &Subtarget;
42 AMDGPUDAGToDAGISel(TargetMachine &TM);
43 virtual ~AMDGPUDAGToDAGISel();
45 SDNode *Select(SDNode *N);
46 virtual const char *getPassName() const;
47 virtual void PostprocessISelDAG();
50 inline SDValue getSmallIPtrImm(unsigned Imm);
51 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
53 // Complex pattern selectors
54 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
55 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
56 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
58 static bool checkType(const Value *ptr, unsigned int addrspace);
59 static const Value *getBasePointerValue(const Value *V);
61 static bool isGlobalStore(const StoreSDNode *N);
62 static bool isPrivateStore(const StoreSDNode *N);
63 static bool isLocalStore(const StoreSDNode *N);
64 static bool isRegionStore(const StoreSDNode *N);
66 static bool isCPLoad(const LoadSDNode *N);
67 static bool isConstantLoad(const LoadSDNode *N, int cbID);
68 static bool isGlobalLoad(const LoadSDNode *N);
69 static bool isParamLoad(const LoadSDNode *N);
70 static bool isPrivateLoad(const LoadSDNode *N);
71 static bool isLocalLoad(const LoadSDNode *N);
72 static bool isRegionLoad(const LoadSDNode *N);
74 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
75 bool SelectGlobalValueVariableOffset(SDValue Addr,
76 SDValue &BaseReg, SDValue& Offset);
77 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
78 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
80 // Include the pieces autogenerated from the target description.
81 #include "AMDGPUGenDAGISel.inc"
83 } // end anonymous namespace
85 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
86 // DAG, ready for instruction scheduling.
87 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
89 return new AMDGPUDAGToDAGISel(TM);
92 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
94 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
97 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
100 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
101 return CurDAG->getTargetConstant(Imm, MVT::i32);
104 bool AMDGPUDAGToDAGISel::SelectADDRParam(
105 SDValue Addr, SDValue& R1, SDValue& R2) {
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);
113 R2 = CurDAG->getTargetConstant(0, MVT::i32);
115 } else if (Addr.getOpcode() == ISD::ADD) {
116 R1 = Addr.getOperand(0);
117 R2 = Addr.getOperand(1);
120 R2 = CurDAG->getTargetConstant(0, MVT::i32);
125 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
126 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
127 Addr.getOpcode() == ISD::TargetGlobalAddress) {
130 return SelectADDRParam(Addr, R1, R2);
134 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
135 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
136 Addr.getOpcode() == ISD::TargetGlobalAddress) {
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);
146 R2 = CurDAG->getTargetConstant(0, MVT::i64);
148 } else if (Addr.getOpcode() == ISD::ADD) {
149 R1 = Addr.getOperand(0);
150 R2 = Addr.getOperand(1);
153 R2 = CurDAG->getTargetConstant(0, MVT::i64);
158 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
159 unsigned int Opc = N->getOpcode();
160 if (N->isMachineOpcode()) {
161 return NULL; // Already selected.
165 case ISD::BUILD_VECTOR: {
166 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
167 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
170 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
171 // that adds a 128 bits reg copy when going through TwoAddressInstructions
172 // pass. We want to avoid 128 bits copies as much as possible because they
173 // can't be bundled by our scheduler.
174 SDValue RegSeqArgs[9] = {
175 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
176 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
177 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
178 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
179 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
181 bool IsRegSeq = true;
182 for (unsigned i = 0; i < N->getNumOperands(); i++) {
183 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
187 RegSeqArgs[2 * i + 1] = N->getOperand(i);
191 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
192 RegSeqArgs, 2 * N->getNumOperands() + 1);
194 case ISD::ConstantFP:
195 case ISD::Constant: {
196 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
197 // XXX: Custom immediate lowering not implemented yet. Instead we use
198 // pseudo instructions defined in SIInstructions.td
199 if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
202 const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
204 uint64_t ImmValue = 0;
205 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
207 if (N->getOpcode() == ISD::ConstantFP) {
208 // XXX: 64-bit Immediates not supported yet
209 assert(N->getValueType(0) != MVT::f64);
211 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
212 APFloat Value = C->getValueAPF();
213 float FloatValue = Value.convertToFloat();
214 if (FloatValue == 0.0) {
215 ImmReg = AMDGPU::ZERO;
216 } else if (FloatValue == 0.5) {
217 ImmReg = AMDGPU::HALF;
218 } else if (FloatValue == 1.0) {
219 ImmReg = AMDGPU::ONE;
221 ImmValue = Value.bitcastToAPInt().getZExtValue();
224 // XXX: 64-bit Immediates not supported yet
225 assert(N->getValueType(0) != MVT::i64);
227 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
228 if (C->getZExtValue() == 0) {
229 ImmReg = AMDGPU::ZERO;
230 } else if (C->getZExtValue() == 1) {
231 ImmReg = AMDGPU::ONE_INT;
233 ImmValue = C->getZExtValue();
237 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
238 Use != SDNode::use_end(); Use = Next) {
239 Next = llvm::next(Use);
240 std::vector<SDValue> Ops;
241 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
242 Ops.push_back(Use->getOperand(i));
245 if (!Use->isMachineOpcode()) {
246 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
247 // We can only use literal constants (e.g. AMDGPU::ZERO,
248 // AMDGPU::ONE, etc) in machine opcodes.
252 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
253 (TII->get(Use->getMachineOpcode()).TSFlags &
254 R600_InstFlag::VECTOR)) {
258 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
259 assert(ImmIdx != -1);
261 // subtract one from ImmIdx, because the DST operand is usually index
262 // 0 for MachineInstrs, but we have no DST in the Ops vector.
265 // Check that we aren't already using an immediate.
266 // XXX: It's possible for an instruction to have more than one
267 // immediate operand, but this is not supported yet.
268 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
269 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
272 if (C->getZExtValue() != 0) {
273 // This instruction is already using an immediate.
277 // Set the immediate value
278 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
281 // Set the immediate register
282 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
284 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
289 SDNode *Result = SelectCode(N);
291 // Fold operands of selected node
293 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
294 if (ST.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX) {
295 const R600InstrInfo *TII =
296 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
297 if (Result && Result->isMachineOpcode() &&
298 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
299 && TII->isALUInstr(Result->getMachineOpcode())) {
300 // Fold FNEG/FABS/CONST_ADDRESS
301 // TODO: Isel can generate multiple MachineInst, we need to recursively
303 bool IsModified = false;
305 std::vector<SDValue> Ops;
306 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
309 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
311 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
313 } while (IsModified);
315 // If node has a single use which is CLAMP_R600, folds it
316 if (Result->hasOneUse() && Result->isMachineOpcode()) {
317 SDNode *PotentialClamp = *Result->use_begin();
318 if (PotentialClamp->isMachineOpcode() &&
319 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
321 TII->getOperandIdx(Result->getMachineOpcode(), R600Operands::CLAMP);
322 std::vector<SDValue> Ops;
323 unsigned NumOp = Result->getNumOperands();
324 for (unsigned i = 0; i < NumOp; ++i) {
325 Ops.push_back(Result->getOperand(i));
327 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
328 Result = CurDAG->SelectNodeTo(PotentialClamp,
329 Result->getMachineOpcode(), PotentialClamp->getVTList(),
339 bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
340 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
342 TII->getOperandIdx(Opcode, R600Operands::SRC0),
343 TII->getOperandIdx(Opcode, R600Operands::SRC1),
344 TII->getOperandIdx(Opcode, R600Operands::SRC2)
347 TII->getOperandIdx(Opcode, R600Operands::SRC0_SEL),
348 TII->getOperandIdx(Opcode, R600Operands::SRC1_SEL),
349 TII->getOperandIdx(Opcode, R600Operands::SRC2_SEL)
352 TII->getOperandIdx(Opcode, R600Operands::SRC0_NEG),
353 TII->getOperandIdx(Opcode, R600Operands::SRC1_NEG),
354 TII->getOperandIdx(Opcode, R600Operands::SRC2_NEG)
357 TII->getOperandIdx(Opcode, R600Operands::SRC0_ABS),
358 TII->getOperandIdx(Opcode, R600Operands::SRC1_ABS),
362 for (unsigned i = 0; i < 3; i++) {
363 if (OperandIdx[i] < 0)
365 SDValue Operand = Ops[OperandIdx[i] - 1];
366 switch (Operand.getOpcode()) {
367 case AMDGPUISD::CONST_ADDRESS: {
371 if (!Operand.getValueType().isVector() &&
372 SelectGlobalValueConstantOffset(Operand.getOperand(0), CstOffset)) {
373 Ops[OperandIdx[i] - 1] = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
374 Ops[SelIdx[i] - 1] = CstOffset;
382 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
383 Ops[NegIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
388 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
389 Ops[AbsIdx[i] - 1] = CurDAG->getTargetConstant(1, MVT::i32);
392 Ops[OperandIdx[i] - 1] = Operand.getOperand(0);
401 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
405 Type *ptrType = ptr->getType();
406 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
409 const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
413 const Value *ret = NULL;
414 ValueMap<const Value *, bool> ValueBitMap;
415 std::queue<const Value *, std::list<const Value *> > ValueQueue;
417 while (!ValueQueue.empty()) {
418 V = ValueQueue.front();
419 if (ValueBitMap.find(V) == ValueBitMap.end()) {
420 ValueBitMap[V] = true;
421 if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
424 } else if (dyn_cast<GlobalVariable>(V)) {
427 } else if (dyn_cast<Constant>(V)) {
428 const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
430 ValueQueue.push(CE->getOperand(0));
432 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
435 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
436 uint32_t numOps = I->getNumOperands();
437 for (uint32_t x = 0; x < numOps; ++x) {
438 ValueQueue.push(I->getOperand(x));
441 assert(!"Found a Value that we didn't know how to handle!");
449 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
450 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
453 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
454 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
455 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
456 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
459 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
460 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
463 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
464 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
467 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
468 if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
471 MachineMemOperand *MMO = N->getMemOperand();
472 const Value *V = MMO->getValue();
473 const Value *BV = getBasePointerValue(V);
476 && ((V && dyn_cast<GlobalValue>(V))
477 || (BV && dyn_cast<GlobalValue>(
478 getBasePointerValue(MMO->getValue()))))) {
479 return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
485 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
486 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
489 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
490 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
493 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) {
494 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
497 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) {
498 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
501 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
502 MachineMemOperand *MMO = N->getMemOperand();
503 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
505 const Value *V = MMO->getValue();
506 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
507 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
515 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
516 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
517 // Check to make sure we are not a constant pool load or a constant load
518 // that is marked as a private load
519 if (isCPLoad(N) || isConstantLoad(N, -1)) {
523 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
524 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
525 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
526 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
527 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
528 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
534 const char *AMDGPUDAGToDAGISel::getPassName() const {
535 return "AMDGPU DAG->DAG Pattern Instruction Selection";
543 ///==== AMDGPU Functions ====///
545 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
547 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
548 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
554 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
555 SDValue& BaseReg, SDValue &Offset) {
556 if (!dyn_cast<ConstantSDNode>(Addr)) {
558 Offset = CurDAG->getIntPtrConstant(0, true);
564 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
566 ConstantSDNode * IMMOffset;
568 if (Addr.getOpcode() == ISD::ADD
569 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
570 && isInt<16>(IMMOffset->getZExtValue())) {
572 Base = Addr.getOperand(0);
573 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
575 // If the pointer address is constant, we can move it to the offset field.
576 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
577 && isInt<16>(IMMOffset->getZExtValue())) {
578 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
579 CurDAG->getEntryNode().getDebugLoc(),
580 AMDGPU::ZERO, MVT::i32);
581 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
585 // Default case, no offset
587 Offset = CurDAG->getTargetConstant(0, MVT::i32);
591 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
595 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
596 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
597 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
598 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
599 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
600 Base = Addr.getOperand(0);
601 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
604 Offset = CurDAG->getTargetConstant(0, MVT::i32);
610 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
612 // Go over all selected nodes and try to fold them a bit more
613 const AMDGPUTargetLowering& Lowering = ((const AMDGPUTargetLowering&)TLI);
614 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
615 E = CurDAG->allnodes_end(); I != E; ++I) {
617 MachineSDNode *Node = dyn_cast<MachineSDNode>(I);
621 SDNode *ResNode = Lowering.PostISelFolding(Node, *CurDAG);
623 ReplaceUses(Node, ResNode);