blob: fea875c3e75565727c82e5aee928bda2a709e9c5 [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"
Tom Stellard75aadc22012-12-11 21:25:42 +000017#include "R600InstrInfo.h"
Christian Konigf82901a2013-02-26 17:52:23 +000018#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000019#include "llvm/ADT/ValueMap.h"
Matt Arsenault2aabb062013-06-18 23:37:58 +000020#include "llvm/Analysis/ValueTracking.h"
Tom Stellard2183b702013-06-03 17:39:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellard75aadc22012-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 Konigd910b7d2013-02-26 17:52:16 +000048 virtual void PostprocessISelDAG();
Tom Stellard75aadc22012-12-11 21:25:42 +000049
50private:
51 inline SDValue getSmallIPtrImm(unsigned Imm);
Vincent Lejeunec6896792013-06-04 23:17:15 +000052 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
Tom Stellard84021442013-07-23 01:48:24 +000053 const R600InstrInfo *TII);
Tom Stellard365366f2013-01-23 02:09:06 +000054 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunec6896792013-06-04 23:17:15 +000055 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellard75aadc22012-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 Stellard41fc7852013-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 Stellard75aadc22012-12-11 21:25:42 +000064
65 static bool checkType(const Value *ptr, unsigned int addrspace);
Tom Stellard75aadc22012-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 Arsenault2aabb062013-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 Stellard75aadc22012-12-11 21:25:42 +000079
Tom Stellarddf94dc32013-08-14 23:24:24 +000080 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
Tom Stellard365366f2013-01-23 02:09:06 +000081 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
82 bool SelectGlobalValueVariableOffset(SDValue Addr,
83 SDValue &BaseReg, SDValue& Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000084 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000085 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard75aadc22012-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 Wendlinga3cd3502013-06-19 21:36:55 +000099AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
Tom Stellard75aadc22012-12-11 21:25:42 +0000100 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
101}
102
103AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
104}
105
Tom Stellarddf94dc32013-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 Samsonov3186eb32013-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 Stellarddf94dc32013-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 Stellard75aadc22012-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()) {
Tim Northover31d093c2013-09-22 08:21:56 +0000198 N->setNodeId(-1);
Tom Stellard75aadc22012-12-11 21:25:42 +0000199 return NULL; // Already selected.
200 }
201 switch (Opc) {
202 default: break;
Tom Stellard1f15bff2014-02-25 21:36:18 +0000203 // We are selecting i64 ADD here instead of custom lower it during
204 // DAG legalization, so we can fold some i64 ADDs used for address
205 // calculation into the LOAD and STORE instructions.
206 case ISD::ADD: {
207 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
208 if (N->getValueType(0) != MVT::i64 ||
209 ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
210 break;
211
212 SDLoc DL(N);
213 SDValue LHS = N->getOperand(0);
214 SDValue RHS = N->getOperand(1);
215
216 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
217 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
218
219 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
220 DL, MVT::i32, LHS, Sub0);
221 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
222 DL, MVT::i32, LHS, Sub1);
223
224 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
225 DL, MVT::i32, RHS, Sub0);
226 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
227 DL, MVT::i32, RHS, Sub1);
228
229 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
230
231 SmallVector<SDValue, 8> AddLoArgs;
232 AddLoArgs.push_back(SDValue(Lo0, 0));
233 AddLoArgs.push_back(SDValue(Lo1, 0));
234
235 SDNode *AddLo = CurDAG->getMachineNode(AMDGPU::S_ADD_I32, DL,
236 VTList, AddLoArgs);
237 SDValue Carry = SDValue(AddLo, 1);
238 SDNode *AddHi = CurDAG->getMachineNode(AMDGPU::S_ADDC_U32, DL,
239 MVT::i32, SDValue(Hi0, 0),
240 SDValue(Hi1, 0), Carry);
241
242 SDValue Args[5] = {
243 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
244 SDValue(AddLo,0),
245 Sub0,
246 SDValue(AddHi,0),
247 Sub1,
248 };
249 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args, 5);
250 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000251 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000252 unsigned RegClassID;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000253 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard8e5da412013-08-14 23:24:32 +0000254 const AMDGPURegisterInfo *TRI =
255 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
256 const SIRegisterInfo *SIRI =
257 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
258 EVT VT = N->getValueType(0);
259 unsigned NumVectorElts = VT.getVectorNumElements();
260 assert(VT.getVectorElementType().bitsEq(MVT::i32));
261 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
262 bool UseVReg = true;
263 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
264 U != E; ++U) {
265 if (!U->isMachineOpcode()) {
266 continue;
267 }
268 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
269 if (!RC) {
270 continue;
271 }
272 if (SIRI->isSGPRClass(RC)) {
273 UseVReg = false;
274 }
275 }
276 switch(NumVectorElts) {
277 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
278 AMDGPU::SReg_32RegClassID;
279 break;
280 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
281 AMDGPU::SReg_64RegClassID;
282 break;
283 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
284 AMDGPU::SReg_128RegClassID;
285 break;
286 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
287 AMDGPU::SReg_256RegClassID;
288 break;
289 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
290 AMDGPU::SReg_512RegClassID;
291 break;
Benjamin Kramerbda73ff2013-08-31 21:20:04 +0000292 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
Tom Stellard8e5da412013-08-14 23:24:32 +0000293 }
294 } else {
295 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
296 // that adds a 128 bits reg copy when going through TwoAddressInstructions
297 // pass. We want to avoid 128 bits copies as much as possible because they
298 // can't be bundled by our scheduler.
299 switch(NumVectorElts) {
300 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
301 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
302 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
303 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000304 }
Tom Stellard0344cdf2013-08-01 15:23:42 +0000305
Tom Stellard8e5da412013-08-14 23:24:32 +0000306 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
307
308 if (NumVectorElts == 1) {
309 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
310 VT.getVectorElementType(),
311 N->getOperand(0), RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000312 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000313
314 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
315 "supported yet");
316 // 16 = Max Num Vector Elements
317 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
318 // 1 = Vector Register Class
319 SDValue RegSeqArgs[16 * 2 + 1];
320
321 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000322 bool IsRegSeq = true;
323 for (unsigned i = 0; i < N->getNumOperands(); i++) {
Tom Stellard8e5da412013-08-14 23:24:32 +0000324 // XXX: Why is this here?
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000325 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
326 IsRegSeq = false;
327 break;
328 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000329 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
330 RegSeqArgs[1 + (2 * i) + 1] =
331 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000332 }
333 if (!IsRegSeq)
334 break;
335 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
336 RegSeqArgs, 2 * N->getNumOperands() + 1);
337 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000338 case ISD::BUILD_PAIR: {
339 SDValue RC, SubReg0, SubReg1;
340 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000341 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-04-05 23:31:51 +0000342 break;
343 }
344 if (N->getValueType(0) == MVT::i128) {
345 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
346 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
347 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
348 } else if (N->getValueType(0) == MVT::i64) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000349 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000350 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
351 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
352 } else {
353 llvm_unreachable("Unhandled value type for BUILD_PAIR");
354 }
355 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
356 N->getOperand(1), SubReg1 };
357 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000358 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000359 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000360 case AMDGPUISD::REGISTER_LOAD: {
361 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
362 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
363 break;
364 SDValue Addr, Offset;
365
366 SelectADDRIndirect(N->getOperand(1), Addr, Offset);
367 const SDValue Ops[] = {
368 Addr,
369 Offset,
370 CurDAG->getTargetConstant(0, MVT::i32),
371 N->getOperand(0),
372 };
373 return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
374 CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
375 Ops);
376 }
377 case AMDGPUISD::REGISTER_STORE: {
378 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
379 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
380 break;
381 SDValue Addr, Offset;
382 SelectADDRIndirect(N->getOperand(2), Addr, Offset);
383 const SDValue Ops[] = {
384 N->getOperand(1),
385 Addr,
386 Offset,
387 CurDAG->getTargetConstant(0, MVT::i32),
388 N->getOperand(0),
389 };
390 return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
391 CurDAG->getVTList(MVT::Other),
392 Ops);
393 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000394 }
Vincent Lejeune0167a312013-09-12 23:45:00 +0000395 return SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000396}
397
Tom Stellard75aadc22012-12-11 21:25:42 +0000398
399bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
400 if (!ptr) {
401 return false;
402 }
403 Type *ptrType = ptr->getType();
404 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
405}
406
Tom Stellard75aadc22012-12-11 21:25:42 +0000407bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
408 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
409}
410
411bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
412 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
413 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
414 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
415}
416
417bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
418 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
419}
420
421bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
422 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
423}
424
Tom Stellard1e803092013-07-23 01:48:18 +0000425bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
426 if (CbId == -1) {
427 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000428 }
Tom Stellard1e803092013-07-23 01:48:18 +0000429 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellard75aadc22012-12-11 21:25:42 +0000430}
431
Matt Arsenault2aabb062013-06-18 23:37:58 +0000432bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard8cb0e472013-07-23 23:54:56 +0000433 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
434 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
435 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
436 N->getMemoryVT().bitsLT(MVT::i32)) {
437 return true;
438 }
439 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000440 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
441}
442
Matt Arsenault2aabb062013-06-18 23:37:58 +0000443bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000444 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
445}
446
Matt Arsenault2aabb062013-06-18 23:37:58 +0000447bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000448 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
449}
450
Matt Arsenault2aabb062013-06-18 23:37:58 +0000451bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000452 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
453}
454
Matt Arsenault2aabb062013-06-18 23:37:58 +0000455bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000456 MachineMemOperand *MMO = N->getMemOperand();
457 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
458 if (MMO) {
459 const Value *V = MMO->getValue();
460 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
461 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
462 return true;
463 }
464 }
465 }
466 return false;
467}
468
Matt Arsenault2aabb062013-06-18 23:37:58 +0000469bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000470 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
471 // Check to make sure we are not a constant pool load or a constant load
472 // that is marked as a private load
473 if (isCPLoad(N) || isConstantLoad(N, -1)) {
474 return false;
475 }
476 }
477 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
478 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
479 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
480 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
481 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
482 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
483 return true;
484 }
485 return false;
486}
487
488const char *AMDGPUDAGToDAGISel::getPassName() const {
489 return "AMDGPU DAG->DAG Pattern Instruction Selection";
490}
491
492#ifdef DEBUGTMP
493#undef INT64_C
494#endif
495#undef DEBUGTMP
496
Tom Stellard41fc7852013-07-23 01:48:42 +0000497//===----------------------------------------------------------------------===//
498// Complex Patterns
499//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000500
Tom Stellard365366f2013-01-23 02:09:06 +0000501bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
502 SDValue& IntPtr) {
503 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
504 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
505 return true;
506 }
507 return false;
508}
509
510bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
511 SDValue& BaseReg, SDValue &Offset) {
512 if (!dyn_cast<ConstantSDNode>(Addr)) {
513 BaseReg = Addr;
514 Offset = CurDAG->getIntPtrConstant(0, true);
515 return true;
516 }
517 return false;
518}
519
Tom Stellard75aadc22012-12-11 21:25:42 +0000520bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
521 SDValue &Offset) {
522 ConstantSDNode * IMMOffset;
523
524 if (Addr.getOpcode() == ISD::ADD
525 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
526 && isInt<16>(IMMOffset->getZExtValue())) {
527
528 Base = Addr.getOperand(0);
529 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
530 return true;
531 // If the pointer address is constant, we can move it to the offset field.
532 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
533 && isInt<16>(IMMOffset->getZExtValue())) {
534 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000535 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000536 AMDGPU::ZERO, MVT::i32);
537 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
538 return true;
539 }
540
541 // Default case, no offset
542 Base = Addr;
543 Offset = CurDAG->getTargetConstant(0, MVT::i32);
544 return true;
545}
546
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000547bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
548 SDValue &Offset) {
549 ConstantSDNode *C;
550
551 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
552 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
553 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
554 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
555 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
556 Base = Addr.getOperand(0);
557 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
558 } else {
559 Base = Addr;
560 Offset = CurDAG->getTargetConstant(0, MVT::i32);
561 }
562
563 return true;
564}
Christian Konigd910b7d2013-02-26 17:52:16 +0000565
Tom Stellard41fc7852013-07-23 01:48:42 +0000566SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
567 APInt Demanded = APInt(32, 0x00FFFFFF);
568 APInt KnownZero, KnownOne;
569 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
570 const TargetLowering *TLI = getTargetLowering();
571 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
572 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
573 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
574 return SimplifyI24(TLO.New);
575 } else {
576 return Op;
577 }
578}
579
580bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
581
582 assert(Op.getValueType() == MVT::i32);
583
584 if (CurDAG->ComputeNumSignBits(Op) == 9) {
585 I24 = SimplifyI24(Op);
586 return true;
587 }
588 return false;
589}
590
591bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
592 APInt KnownZero;
593 APInt KnownOne;
594 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
595
596 assert (Op.getValueType() == MVT::i32);
597
598 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
599 // i32. These smaller types are legal to use with the i24 instructions.
600 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
601 Op.getOpcode() == ISD::ANY_EXTEND ||
602 ISD::isEXTLoad(Op.getNode())) {
603 U24 = SimplifyI24(Op);
604 return true;
605 }
606 return false;
607}
608
Christian Konigd910b7d2013-02-26 17:52:16 +0000609void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000610 const AMDGPUTargetLowering& Lowering =
611 (*(const AMDGPUTargetLowering*)getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000612 bool IsModified = false;
613 do {
614 IsModified = false;
615 // Go over all selected nodes and try to fold them a bit more
616 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
617 E = CurDAG->allnodes_end(); I != E; ++I) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000618
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000619 SDNode *Node = I;
Tom Stellard2183b702013-06-03 17:39:46 +0000620
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000621 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
622 if (!MachineNode)
623 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +0000624
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000625 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
626 if (ResNode != Node) {
627 ReplaceUses(Node, ResNode);
628 IsModified = true;
629 }
Tom Stellard2183b702013-06-03 17:39:46 +0000630 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000631 CurDAG->RemoveDeadNodes();
632 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +0000633}