blob: 95037ba6a9e6db76a0e68c03ba8eb17b9b0323f3 [file] [log] [blame]
Tom Stellardf98f2ce2012-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"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000017#include "R600InstrInfo.h"
Christian Konigd3b55092013-02-26 17:52:23 +000018#include "SIISelLowering.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000019#include "llvm/ADT/ValueMap.h"
Matt Arsenault70a3dc12013-06-18 23:37:58 +000020#include "llvm/Analysis/ValueTracking.h"
Tom Stellard8a72c732013-06-03 17:39:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000024#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Support/Compiler.h"
26#include <list>
27#include <queue>
28
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Instruction Selector Implementation
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// AMDGPU specific code to select AMDGPU machine instructions for
37/// SelectionDAG operations.
38class 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;
42public:
43 AMDGPUDAGToDAGISel(TargetMachine &TM);
44 virtual ~AMDGPUDAGToDAGISel();
45
46 SDNode *Select(SDNode *N);
47 virtual const char *getPassName() const;
Christian Konigc018eca2013-02-26 17:52:16 +000048 virtual void PostprocessISelDAG();
Tom Stellardf98f2ce2012-12-11 21:25:42 +000049
50private:
51 inline SDValue getSmallIPtrImm(unsigned Imm);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000052 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
Tom Stellard58d33352013-07-23 01:48:24 +000053 const R600InstrInfo *TII);
Tom Stellard9f7818d2013-01-23 02:09:06 +000054 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000055 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000056
57 // Complex pattern selectors
58 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
59 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
60 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
Tom Stellard3f5d63b2013-07-23 01:48:42 +000061 SDValue SimplifyI24(SDValue &Op);
62 bool SelectI24(SDValue Addr, SDValue &Op);
63 bool SelectU24(SDValue Addr, SDValue &Op);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000064
65 static bool checkType(const Value *ptr, unsigned int addrspace);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000066
67 static bool isGlobalStore(const StoreSDNode *N);
68 static bool isPrivateStore(const StoreSDNode *N);
69 static bool isLocalStore(const StoreSDNode *N);
70 static bool isRegionStore(const StoreSDNode *N);
71
Matt Arsenault70a3dc12013-06-18 23:37:58 +000072 bool isCPLoad(const LoadSDNode *N) const;
73 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
74 bool isGlobalLoad(const LoadSDNode *N) const;
75 bool isParamLoad(const LoadSDNode *N) const;
76 bool isPrivateLoad(const LoadSDNode *N) const;
77 bool isLocalLoad(const LoadSDNode *N) const;
78 bool isRegionLoad(const LoadSDNode *N) const;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000079
Tom Stellard636298b2013-08-14 23:24:24 +000080 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard9f7818d2013-01-23 02:09:06 +000081 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
82 bool SelectGlobalValueVariableOffset(SDValue Addr,
83 SDValue &BaseReg, SDValue& Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000084 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardc0b0c672013-02-06 17:32:29 +000085 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000086
87 // Include the pieces autogenerated from the target description.
88#include "AMDGPUGenDAGISel.inc"
89};
90} // end anonymous namespace
91
92/// \brief This pass converts a legalized DAG into a AMDGPU-specific
93// DAG, ready for instruction scheduling.
94FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
95 ) {
96 return new AMDGPUDAGToDAGISel(TM);
97}
98
Bill Wendlingba54bca2013-06-19 21:36:55 +000099AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000100 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
101}
102
103AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
104}
105
Tom Stellard636298b2013-08-14 23:24:24 +0000106/// \brief Determine the register class for \p OpNo
107/// \returns The register class of the virtual register that will be used for
108/// the given operand number \OpNo or NULL if the register class cannot be
109/// determined.
110const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
111 unsigned OpNo) const {
112 if (!N->isMachineOpcode()) {
113 return NULL;
114 }
115 switch (N->getMachineOpcode()) {
116 default: {
117 const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
Alexey Samsonov84e51512013-08-15 07:11:34 +0000118 unsigned OpIdx = Desc.getNumDefs() + OpNo;
119 if (OpIdx >= Desc.getNumOperands())
120 return NULL;
121 int RegClass = Desc.OpInfo[OpIdx].RegClass;
Tom Stellard636298b2013-08-14 23:24:24 +0000122 if (RegClass == -1) {
123 return NULL;
124 }
125 return TM.getRegisterInfo()->getRegClass(RegClass);
126 }
127 case AMDGPU::REG_SEQUENCE: {
128 const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(
129 cast<ConstantSDNode>(N->getOperand(0))->getZExtValue());
130 unsigned SubRegIdx =
131 dyn_cast<ConstantSDNode>(N->getOperand(OpNo + 1))->getZExtValue();
132 return TM.getRegisterInfo()->getSubClassWithSubReg(SuperRC, SubRegIdx);
133 }
134 }
135}
136
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000137SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
138 return CurDAG->getTargetConstant(Imm, MVT::i32);
139}
140
141bool AMDGPUDAGToDAGISel::SelectADDRParam(
142 SDValue Addr, SDValue& R1, SDValue& R2) {
143
144 if (Addr.getOpcode() == ISD::FrameIndex) {
145 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
146 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
147 R2 = CurDAG->getTargetConstant(0, MVT::i32);
148 } else {
149 R1 = Addr;
150 R2 = CurDAG->getTargetConstant(0, MVT::i32);
151 }
152 } else if (Addr.getOpcode() == ISD::ADD) {
153 R1 = Addr.getOperand(0);
154 R2 = Addr.getOperand(1);
155 } else {
156 R1 = Addr;
157 R2 = CurDAG->getTargetConstant(0, MVT::i32);
158 }
159 return true;
160}
161
162bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
163 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
164 Addr.getOpcode() == ISD::TargetGlobalAddress) {
165 return false;
166 }
167 return SelectADDRParam(Addr, R1, R2);
168}
169
170
171bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
172 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
173 Addr.getOpcode() == ISD::TargetGlobalAddress) {
174 return false;
175 }
176
177 if (Addr.getOpcode() == ISD::FrameIndex) {
178 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
179 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
180 R2 = CurDAG->getTargetConstant(0, MVT::i64);
181 } else {
182 R1 = Addr;
183 R2 = CurDAG->getTargetConstant(0, MVT::i64);
184 }
185 } else if (Addr.getOpcode() == ISD::ADD) {
186 R1 = Addr.getOperand(0);
187 R2 = Addr.getOperand(1);
188 } else {
189 R1 = Addr;
190 R2 = CurDAG->getTargetConstant(0, MVT::i64);
191 }
192 return true;
193}
194
195SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
196 unsigned int Opc = N->getOpcode();
197 if (N->isMachineOpcode()) {
198 return NULL; // Already selected.
199 }
200 switch (Opc) {
201 default: break;
Vincent Lejeunecae68012013-03-05 15:04:49 +0000202 case ISD::BUILD_VECTOR: {
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000203 unsigned RegClassID;
Vincent Lejeunecae68012013-03-05 15:04:49 +0000204 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000205 const AMDGPURegisterInfo *TRI =
206 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
207 const SIRegisterInfo *SIRI =
208 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
209 EVT VT = N->getValueType(0);
210 unsigned NumVectorElts = VT.getVectorNumElements();
211 assert(VT.getVectorElementType().bitsEq(MVT::i32));
212 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
213 bool UseVReg = true;
214 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
215 U != E; ++U) {
216 if (!U->isMachineOpcode()) {
217 continue;
218 }
219 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
220 if (!RC) {
221 continue;
222 }
223 if (SIRI->isSGPRClass(RC)) {
224 UseVReg = false;
225 }
226 }
227 switch(NumVectorElts) {
228 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
229 AMDGPU::SReg_32RegClassID;
230 break;
231 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
232 AMDGPU::SReg_64RegClassID;
233 break;
234 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
235 AMDGPU::SReg_128RegClassID;
236 break;
237 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
238 AMDGPU::SReg_256RegClassID;
239 break;
240 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
241 AMDGPU::SReg_512RegClassID;
242 break;
Benjamin Kramerf53e9b42013-08-31 21:20:04 +0000243 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000244 }
245 } else {
246 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
247 // that adds a 128 bits reg copy when going through TwoAddressInstructions
248 // pass. We want to avoid 128 bits copies as much as possible because they
249 // can't be bundled by our scheduler.
250 switch(NumVectorElts) {
251 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
252 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
253 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
254 }
Vincent Lejeunecae68012013-03-05 15:04:49 +0000255 }
Tom Stellard692ee102013-08-01 15:23:42 +0000256
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000257 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
258
259 if (NumVectorElts == 1) {
260 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
261 VT.getVectorElementType(),
262 N->getOperand(0), RegClass);
Tom Stellard692ee102013-08-01 15:23:42 +0000263 }
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000264
265 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
266 "supported yet");
267 // 16 = Max Num Vector Elements
268 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
269 // 1 = Vector Register Class
270 SDValue RegSeqArgs[16 * 2 + 1];
271
272 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
Vincent Lejeunecae68012013-03-05 15:04:49 +0000273 bool IsRegSeq = true;
274 for (unsigned i = 0; i < N->getNumOperands(); i++) {
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000275 // XXX: Why is this here?
Vincent Lejeunecae68012013-03-05 15:04:49 +0000276 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
277 IsRegSeq = false;
278 break;
279 }
Tom Stellard38d5e1c2013-08-14 23:24:32 +0000280 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
281 RegSeqArgs[1 + (2 * i) + 1] =
282 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
Vincent Lejeunecae68012013-03-05 15:04:49 +0000283 }
284 if (!IsRegSeq)
285 break;
286 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
287 RegSeqArgs, 2 * N->getNumOperands() + 1);
288 }
Tom Stellard17ea10c2013-04-05 23:31:51 +0000289 case ISD::BUILD_PAIR: {
290 SDValue RC, SubReg0, SubReg1;
291 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000292 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard17ea10c2013-04-05 23:31:51 +0000293 break;
294 }
295 if (N->getValueType(0) == MVT::i128) {
296 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
297 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
298 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
299 } else if (N->getValueType(0) == MVT::i64) {
Tom Stellard3492eef2013-08-06 23:08:28 +0000300 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
Tom Stellard17ea10c2013-04-05 23:31:51 +0000301 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
302 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
303 } else {
304 llvm_unreachable("Unhandled value type for BUILD_PAIR");
305 }
306 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
307 N->getOperand(1), SubReg1 };
308 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000309 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard17ea10c2013-04-05 23:31:51 +0000310 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000311 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000312 SDNode *Result = SelectCode(N);
313
314 // Fold operands of selected node
315
316 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000317 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard9f7818d2013-01-23 02:09:06 +0000318 const R600InstrInfo *TII =
319 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000320 if (Result && Result->isMachineOpcode() &&
321 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000322 && TII->hasInstrModifiers(Result->getMachineOpcode())) {
Tom Stellard4bdf9892013-01-31 22:11:54 +0000323 // If node has a single use which is CLAMP_R600, folds it
324 if (Result->hasOneUse() && Result->isMachineOpcode()) {
325 SDNode *PotentialClamp = *Result->use_begin();
326 if (PotentialClamp->isMachineOpcode() &&
327 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
328 unsigned ClampIdx =
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000329 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
Tom Stellard4bdf9892013-01-31 22:11:54 +0000330 std::vector<SDValue> Ops;
331 unsigned NumOp = Result->getNumOperands();
332 for (unsigned i = 0; i < NumOp; ++i) {
333 Ops.push_back(Result->getOperand(i));
334 }
335 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
336 Result = CurDAG->SelectNodeTo(PotentialClamp,
337 Result->getMachineOpcode(), PotentialClamp->getVTList(),
338 Ops.data(), NumOp);
339 }
340 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000341 }
342 }
343
344 return Result;
345}
346
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000347
348bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
349 if (!ptr) {
350 return false;
351 }
352 Type *ptrType = ptr->getType();
353 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
354}
355
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000356bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
357 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
358}
359
360bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
361 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
362 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
363 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
364}
365
366bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
367 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
368}
369
370bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
371 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
372}
373
Tom Stellarda7eea052013-07-23 01:48:18 +0000374bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
375 if (CbId == -1) {
376 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000377 }
Tom Stellarda7eea052013-07-23 01:48:18 +0000378 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000379}
380
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000381bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard8ea83d42013-07-23 23:54:56 +0000382 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
383 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
384 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
385 N->getMemoryVT().bitsLT(MVT::i32)) {
386 return true;
387 }
388 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000389 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
390}
391
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000392bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000393 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
394}
395
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000396bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000397 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
398}
399
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000400bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000401 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
402}
403
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000404bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000405 MachineMemOperand *MMO = N->getMemOperand();
406 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
407 if (MMO) {
408 const Value *V = MMO->getValue();
409 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
410 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
411 return true;
412 }
413 }
414 }
415 return false;
416}
417
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000418bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000419 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
420 // Check to make sure we are not a constant pool load or a constant load
421 // that is marked as a private load
422 if (isCPLoad(N) || isConstantLoad(N, -1)) {
423 return false;
424 }
425 }
426 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
427 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
428 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
429 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
430 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
431 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
432 return true;
433 }
434 return false;
435}
436
437const char *AMDGPUDAGToDAGISel::getPassName() const {
438 return "AMDGPU DAG->DAG Pattern Instruction Selection";
439}
440
441#ifdef DEBUGTMP
442#undef INT64_C
443#endif
444#undef DEBUGTMP
445
Tom Stellard3f5d63b2013-07-23 01:48:42 +0000446//===----------------------------------------------------------------------===//
447// Complex Patterns
448//===----------------------------------------------------------------------===//
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000449
Tom Stellard9f7818d2013-01-23 02:09:06 +0000450bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
451 SDValue& IntPtr) {
452 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
453 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
454 return true;
455 }
456 return false;
457}
458
459bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
460 SDValue& BaseReg, SDValue &Offset) {
461 if (!dyn_cast<ConstantSDNode>(Addr)) {
462 BaseReg = Addr;
463 Offset = CurDAG->getIntPtrConstant(0, true);
464 return true;
465 }
466 return false;
467}
468
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000469bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
470 SDValue &Offset) {
471 ConstantSDNode * IMMOffset;
472
473 if (Addr.getOpcode() == ISD::ADD
474 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
475 && isInt<16>(IMMOffset->getZExtValue())) {
476
477 Base = Addr.getOperand(0);
478 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
479 return true;
480 // If the pointer address is constant, we can move it to the offset field.
481 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
482 && isInt<16>(IMMOffset->getZExtValue())) {
483 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +0000484 SDLoc(CurDAG->getEntryNode()),
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000485 AMDGPU::ZERO, MVT::i32);
486 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
487 return true;
488 }
489
490 // Default case, no offset
491 Base = Addr;
492 Offset = CurDAG->getTargetConstant(0, MVT::i32);
493 return true;
494}
495
Tom Stellardc0b0c672013-02-06 17:32:29 +0000496bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
497 SDValue &Offset) {
498 ConstantSDNode *C;
499
500 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
501 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
502 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
503 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
504 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
505 Base = Addr.getOperand(0);
506 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
507 } else {
508 Base = Addr;
509 Offset = CurDAG->getTargetConstant(0, MVT::i32);
510 }
511
512 return true;
513}
Christian Konigc018eca2013-02-26 17:52:16 +0000514
Tom Stellard3f5d63b2013-07-23 01:48:42 +0000515SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
516 APInt Demanded = APInt(32, 0x00FFFFFF);
517 APInt KnownZero, KnownOne;
518 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
519 const TargetLowering *TLI = getTargetLowering();
520 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
521 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
522 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
523 return SimplifyI24(TLO.New);
524 } else {
525 return Op;
526 }
527}
528
529bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
530
531 assert(Op.getValueType() == MVT::i32);
532
533 if (CurDAG->ComputeNumSignBits(Op) == 9) {
534 I24 = SimplifyI24(Op);
535 return true;
536 }
537 return false;
538}
539
540bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
541 APInt KnownZero;
542 APInt KnownOne;
543 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
544
545 assert (Op.getValueType() == MVT::i32);
546
547 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
548 // i32. These smaller types are legal to use with the i24 instructions.
549 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
550 Op.getOpcode() == ISD::ANY_EXTEND ||
551 ISD::isEXTLoad(Op.getNode())) {
552 U24 = SimplifyI24(Op);
553 return true;
554 }
555 return false;
556}
557
Christian Konigc018eca2013-02-26 17:52:16 +0000558void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlingba54bca2013-06-19 21:36:55 +0000559 const AMDGPUTargetLowering& Lowering =
560 (*(const AMDGPUTargetLowering*)getTargetLowering());
Vincent Lejeunefe783182013-09-12 23:44:44 +0000561 bool IsModified = false;
562 do {
563 IsModified = false;
564 // Go over all selected nodes and try to fold them a bit more
565 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
566 E = CurDAG->allnodes_end(); I != E; ++I) {
Christian Konigc018eca2013-02-26 17:52:16 +0000567
Vincent Lejeunefe783182013-09-12 23:44:44 +0000568 SDNode *Node = I;
Tom Stellard8a72c732013-06-03 17:39:46 +0000569
Vincent Lejeunefe783182013-09-12 23:44:44 +0000570 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
571 if (!MachineNode)
572 continue;
Christian Konigc018eca2013-02-26 17:52:16 +0000573
Vincent Lejeunefe783182013-09-12 23:44:44 +0000574 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
575 if (ResNode != Node) {
576 ReplaceUses(Node, ResNode);
577 IsModified = true;
578 }
Tom Stellard8a72c732013-06-03 17:39:46 +0000579 }
Vincent Lejeunefe783182013-09-12 23:44:44 +0000580 CurDAG->RemoveDeadNodes();
581 } while (IsModified);
Christian Konigc018eca2013-02-26 17:52:16 +0000582}