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 "R600InstrInfo.h"
18 #include "SIISelLowering.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/IR/ValueMap.h"
25 #include "llvm/Support/Compiler.h"
31 //===----------------------------------------------------------------------===//
32 // Instruction Selector Implementation
33 //===----------------------------------------------------------------------===//
36 /// AMDGPU specific code to select AMDGPU machine instructions for
37 /// SelectionDAG operations.
38 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40 // make the right decision when generating code for different targets.
41 const AMDGPUSubtarget &Subtarget;
43 AMDGPUDAGToDAGISel(TargetMachine &TM);
44 virtual ~AMDGPUDAGToDAGISel();
46 SDNode *Select(SDNode *N);
47 virtual const char *getPassName() const;
48 virtual void PostprocessISelDAG();
51 bool isInlineImmediate(SDNode *N) const;
52 inline SDValue getSmallIPtrImm(unsigned Imm);
53 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
54 const R600InstrInfo *TII);
55 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
56 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
58 // Complex pattern selectors
59 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
60 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
61 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
62 SDValue SimplifyI24(SDValue &Op);
63 bool SelectI24(SDValue Addr, SDValue &Op);
64 bool SelectU24(SDValue Addr, SDValue &Op);
66 static bool checkType(const Value *ptr, unsigned int addrspace);
68 static bool isGlobalStore(const StoreSDNode *N);
69 static bool isPrivateStore(const StoreSDNode *N);
70 static bool isLocalStore(const StoreSDNode *N);
71 static bool isRegionStore(const StoreSDNode *N);
73 bool isCPLoad(const LoadSDNode *N) const;
74 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
75 bool isGlobalLoad(const LoadSDNode *N) const;
76 bool isParamLoad(const LoadSDNode *N) const;
77 bool isPrivateLoad(const LoadSDNode *N) const;
78 bool isLocalLoad(const LoadSDNode *N) const;
79 bool isRegionLoad(const LoadSDNode *N) const;
81 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
82 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
83 bool SelectGlobalValueVariableOffset(SDValue Addr,
84 SDValue &BaseReg, SDValue& Offset);
85 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
86 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
88 // Include the pieces autogenerated from the target description.
89 #include "AMDGPUGenDAGISel.inc"
91 } // end anonymous namespace
93 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
94 // DAG, ready for instruction scheduling.
95 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
97 return new AMDGPUDAGToDAGISel(TM);
100 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
101 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
104 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
107 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
108 const SITargetLowering *TL
109 = static_cast<const SITargetLowering *>(getTargetLowering());
110 return TL->analyzeImmediate(N) == 0;
113 /// \brief Determine the register class for \p OpNo
114 /// \returns The register class of the virtual register that will be used for
115 /// the given operand number \OpNo or NULL if the register class cannot be
117 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
118 unsigned OpNo) const {
119 if (!N->isMachineOpcode()) {
122 switch (N->getMachineOpcode()) {
124 const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
125 unsigned OpIdx = Desc.getNumDefs() + OpNo;
126 if (OpIdx >= Desc.getNumOperands())
128 int RegClass = Desc.OpInfo[OpIdx].RegClass;
129 if (RegClass == -1) {
132 return TM.getRegisterInfo()->getRegClass(RegClass);
134 case AMDGPU::REG_SEQUENCE: {
135 const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(
136 cast<ConstantSDNode>(N->getOperand(0))->getZExtValue());
138 dyn_cast<ConstantSDNode>(N->getOperand(OpNo + 1))->getZExtValue();
139 return TM.getRegisterInfo()->getSubClassWithSubReg(SuperRC, SubRegIdx);
144 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
145 return CurDAG->getTargetConstant(Imm, MVT::i32);
148 bool AMDGPUDAGToDAGISel::SelectADDRParam(
149 SDValue Addr, SDValue& R1, SDValue& R2) {
151 if (Addr.getOpcode() == ISD::FrameIndex) {
152 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
153 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
154 R2 = CurDAG->getTargetConstant(0, MVT::i32);
157 R2 = CurDAG->getTargetConstant(0, MVT::i32);
159 } else if (Addr.getOpcode() == ISD::ADD) {
160 R1 = Addr.getOperand(0);
161 R2 = Addr.getOperand(1);
164 R2 = CurDAG->getTargetConstant(0, MVT::i32);
169 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
170 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
171 Addr.getOpcode() == ISD::TargetGlobalAddress) {
174 return SelectADDRParam(Addr, R1, R2);
178 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
179 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
180 Addr.getOpcode() == ISD::TargetGlobalAddress) {
184 if (Addr.getOpcode() == ISD::FrameIndex) {
185 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
186 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
187 R2 = CurDAG->getTargetConstant(0, MVT::i64);
190 R2 = CurDAG->getTargetConstant(0, MVT::i64);
192 } else if (Addr.getOpcode() == ISD::ADD) {
193 R1 = Addr.getOperand(0);
194 R2 = Addr.getOperand(1);
197 R2 = CurDAG->getTargetConstant(0, MVT::i64);
202 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
203 unsigned int Opc = N->getOpcode();
204 if (N->isMachineOpcode()) {
206 return NULL; // Already selected.
210 // We are selecting i64 ADD here instead of custom lower it during
211 // DAG legalization, so we can fold some i64 ADDs used for address
212 // calculation into the LOAD and STORE instructions.
214 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
215 if (N->getValueType(0) != MVT::i64 ||
216 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
220 SDValue LHS = N->getOperand(0);
221 SDValue RHS = N->getOperand(1);
223 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
224 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
226 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
227 DL, MVT::i32, LHS, Sub0);
228 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
229 DL, MVT::i32, LHS, Sub1);
231 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
232 DL, MVT::i32, RHS, Sub0);
233 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
234 DL, MVT::i32, RHS, Sub1);
236 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
238 SmallVector<SDValue, 8> AddLoArgs;
239 AddLoArgs.push_back(SDValue(Lo0, 0));
240 AddLoArgs.push_back(SDValue(Lo1, 0));
242 SDNode *AddLo = CurDAG->getMachineNode(AMDGPU::S_ADD_I32, DL,
244 SDValue Carry = SDValue(AddLo, 1);
245 SDNode *AddHi = CurDAG->getMachineNode(AMDGPU::S_ADDC_U32, DL,
246 MVT::i32, SDValue(Hi0, 0),
247 SDValue(Hi1, 0), Carry);
250 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
256 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args, 5);
258 case ISD::BUILD_VECTOR: {
260 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
261 const AMDGPURegisterInfo *TRI =
262 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
263 const SIRegisterInfo *SIRI =
264 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
265 EVT VT = N->getValueType(0);
266 unsigned NumVectorElts = VT.getVectorNumElements();
267 assert(VT.getVectorElementType().bitsEq(MVT::i32));
268 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
270 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
272 if (!U->isMachineOpcode()) {
275 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
279 if (SIRI->isSGPRClass(RC)) {
283 switch(NumVectorElts) {
284 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
285 AMDGPU::SReg_32RegClassID;
287 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
288 AMDGPU::SReg_64RegClassID;
290 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
291 AMDGPU::SReg_128RegClassID;
293 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
294 AMDGPU::SReg_256RegClassID;
296 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
297 AMDGPU::SReg_512RegClassID;
299 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
302 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
303 // that adds a 128 bits reg copy when going through TwoAddressInstructions
304 // pass. We want to avoid 128 bits copies as much as possible because they
305 // can't be bundled by our scheduler.
306 switch(NumVectorElts) {
307 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
308 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
309 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
313 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
315 if (NumVectorElts == 1) {
316 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
317 VT.getVectorElementType(),
318 N->getOperand(0), RegClass);
321 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
323 // 16 = Max Num Vector Elements
324 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
325 // 1 = Vector Register Class
326 SDValue RegSeqArgs[16 * 2 + 1];
328 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
329 bool IsRegSeq = true;
330 for (unsigned i = 0; i < N->getNumOperands(); i++) {
331 // XXX: Why is this here?
332 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
336 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
337 RegSeqArgs[1 + (2 * i) + 1] =
338 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
342 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
343 RegSeqArgs, 2 * N->getNumOperands() + 1);
345 case ISD::BUILD_PAIR: {
346 SDValue RC, SubReg0, SubReg1;
347 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
348 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
351 if (N->getValueType(0) == MVT::i128) {
352 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
353 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
354 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
355 } else if (N->getValueType(0) == MVT::i64) {
356 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
357 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
358 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
360 llvm_unreachable("Unhandled value type for BUILD_PAIR");
362 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
363 N->getOperand(1), SubReg1 };
364 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
365 SDLoc(N), N->getValueType(0), Ops);
369 case ISD::ConstantFP: {
370 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
371 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
372 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
376 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
377 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
379 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
381 Imm = C->getZExtValue();
384 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
385 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
386 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
387 CurDAG->getConstant(Imm >> 32, MVT::i32));
388 const SDValue Ops[] = {
389 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
390 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
391 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
394 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
395 N->getValueType(0), Ops);
398 case AMDGPUISD::REGISTER_LOAD: {
399 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
400 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
402 SDValue Addr, Offset;
404 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
405 const SDValue Ops[] = {
408 CurDAG->getTargetConstant(0, MVT::i32),
411 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
412 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
415 case AMDGPUISD::REGISTER_STORE: {
416 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
417 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
419 SDValue Addr, Offset;
420 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
421 const SDValue Ops[] = {
425 CurDAG->getTargetConstant(0, MVT::i32),
428 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
429 CurDAG->getVTList(MVT::Other),
433 return SelectCode(N);
437 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
441 Type *ptrType = ptr->getType();
442 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
445 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
446 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
449 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
450 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
451 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
452 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
455 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
456 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
459 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
460 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
463 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
465 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
467 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
470 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
471 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
472 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
473 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
474 N->getMemoryVT().bitsLT(MVT::i32)) {
478 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
481 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
482 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
485 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
486 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
489 bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
490 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
493 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
494 MachineMemOperand *MMO = N->getMemOperand();
495 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
497 const Value *V = MMO->getValue();
498 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
499 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
507 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
508 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
509 // Check to make sure we are not a constant pool load or a constant load
510 // that is marked as a private load
511 if (isCPLoad(N) || isConstantLoad(N, -1)) {
515 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
516 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
517 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
518 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
519 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
520 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
526 const char *AMDGPUDAGToDAGISel::getPassName() const {
527 return "AMDGPU DAG->DAG Pattern Instruction Selection";
535 //===----------------------------------------------------------------------===//
537 //===----------------------------------------------------------------------===//
539 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
541 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
542 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
548 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
549 SDValue& BaseReg, SDValue &Offset) {
550 if (!dyn_cast<ConstantSDNode>(Addr)) {
552 Offset = CurDAG->getIntPtrConstant(0, true);
558 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
560 ConstantSDNode * IMMOffset;
562 if (Addr.getOpcode() == ISD::ADD
563 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
564 && isInt<16>(IMMOffset->getZExtValue())) {
566 Base = Addr.getOperand(0);
567 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
569 // If the pointer address is constant, we can move it to the offset field.
570 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
571 && isInt<16>(IMMOffset->getZExtValue())) {
572 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
573 SDLoc(CurDAG->getEntryNode()),
574 AMDGPU::ZERO, MVT::i32);
575 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
579 // Default case, no offset
581 Offset = CurDAG->getTargetConstant(0, MVT::i32);
585 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
589 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
590 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
591 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
592 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
593 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
594 Base = Addr.getOperand(0);
595 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
598 Offset = CurDAG->getTargetConstant(0, MVT::i32);
604 SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
605 APInt Demanded = APInt(32, 0x00FFFFFF);
606 APInt KnownZero, KnownOne;
607 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
608 const TargetLowering *TLI = getTargetLowering();
609 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
610 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
611 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
612 return SimplifyI24(TLO.New);
618 bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
620 assert(Op.getValueType() == MVT::i32);
622 if (CurDAG->ComputeNumSignBits(Op) == 9) {
623 I24 = SimplifyI24(Op);
629 bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
632 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
634 assert (Op.getValueType() == MVT::i32);
636 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
637 // i32. These smaller types are legal to use with the i24 instructions.
638 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
639 Op.getOpcode() == ISD::ANY_EXTEND ||
640 ISD::isEXTLoad(Op.getNode())) {
641 U24 = SimplifyI24(Op);
647 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
648 const AMDGPUTargetLowering& Lowering =
649 (*(const AMDGPUTargetLowering*)getTargetLowering());
650 bool IsModified = false;
653 // Go over all selected nodes and try to fold them a bit more
654 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
655 E = CurDAG->allnodes_end(); I != E; ++I) {
659 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
663 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
664 if (ResNode != Node) {
665 ReplaceUses(Node, ResNode);
669 CurDAG->RemoveDeadNodes();
670 } while (IsModified);