blob: 88b375b02187cfcb1cf73308d066af7c879da489 [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;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000203 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000204 unsigned RegClassID;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000205 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard8e5da412013-08-14 23:24:32 +0000206 const AMDGPURegisterInfo *TRI =
207 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
208 const SIRegisterInfo *SIRI =
209 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
210 EVT VT = N->getValueType(0);
211 unsigned NumVectorElts = VT.getVectorNumElements();
212 assert(VT.getVectorElementType().bitsEq(MVT::i32));
213 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
214 bool UseVReg = true;
215 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
216 U != E; ++U) {
217 if (!U->isMachineOpcode()) {
218 continue;
219 }
220 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
221 if (!RC) {
222 continue;
223 }
224 if (SIRI->isSGPRClass(RC)) {
225 UseVReg = false;
226 }
227 }
228 switch(NumVectorElts) {
229 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
230 AMDGPU::SReg_32RegClassID;
231 break;
232 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
233 AMDGPU::SReg_64RegClassID;
234 break;
235 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
236 AMDGPU::SReg_128RegClassID;
237 break;
238 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
239 AMDGPU::SReg_256RegClassID;
240 break;
241 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
242 AMDGPU::SReg_512RegClassID;
243 break;
Benjamin Kramerbda73ff2013-08-31 21:20:04 +0000244 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
Tom Stellard8e5da412013-08-14 23:24:32 +0000245 }
246 } else {
247 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
248 // that adds a 128 bits reg copy when going through TwoAddressInstructions
249 // pass. We want to avoid 128 bits copies as much as possible because they
250 // can't be bundled by our scheduler.
251 switch(NumVectorElts) {
252 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
253 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
254 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
255 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000256 }
Tom Stellard0344cdf2013-08-01 15:23:42 +0000257
Tom Stellard8e5da412013-08-14 23:24:32 +0000258 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
259
260 if (NumVectorElts == 1) {
261 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
262 VT.getVectorElementType(),
263 N->getOperand(0), RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000264 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000265
266 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
267 "supported yet");
268 // 16 = Max Num Vector Elements
269 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
270 // 1 = Vector Register Class
271 SDValue RegSeqArgs[16 * 2 + 1];
272
273 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000274 bool IsRegSeq = true;
275 for (unsigned i = 0; i < N->getNumOperands(); i++) {
Tom Stellard8e5da412013-08-14 23:24:32 +0000276 // XXX: Why is this here?
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000277 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
278 IsRegSeq = false;
279 break;
280 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000281 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
282 RegSeqArgs[1 + (2 * i) + 1] =
283 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000284 }
285 if (!IsRegSeq)
286 break;
287 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
288 RegSeqArgs, 2 * N->getNumOperands() + 1);
289 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000290 case ISD::BUILD_PAIR: {
291 SDValue RC, SubReg0, SubReg1;
292 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000293 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-04-05 23:31:51 +0000294 break;
295 }
296 if (N->getValueType(0) == MVT::i128) {
297 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
298 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
299 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
300 } else if (N->getValueType(0) == MVT::i64) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000301 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000302 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
303 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
304 } else {
305 llvm_unreachable("Unhandled value type for BUILD_PAIR");
306 }
307 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
308 N->getOperand(1), SubReg1 };
309 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000310 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000311 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000312 }
Vincent Lejeune0167a312013-09-12 23:45:00 +0000313 return SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000314}
315
Tom Stellard75aadc22012-12-11 21:25:42 +0000316
317bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
318 if (!ptr) {
319 return false;
320 }
321 Type *ptrType = ptr->getType();
322 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
323}
324
Tom Stellard75aadc22012-12-11 21:25:42 +0000325bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
326 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
327}
328
329bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
330 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
331 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
332 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
333}
334
335bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
336 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
337}
338
339bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
340 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
341}
342
Tom Stellard1e803092013-07-23 01:48:18 +0000343bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
344 if (CbId == -1) {
345 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000346 }
Tom Stellard1e803092013-07-23 01:48:18 +0000347 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellard75aadc22012-12-11 21:25:42 +0000348}
349
Matt Arsenault2aabb062013-06-18 23:37:58 +0000350bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard8cb0e472013-07-23 23:54:56 +0000351 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
352 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
353 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
354 N->getMemoryVT().bitsLT(MVT::i32)) {
355 return true;
356 }
357 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000358 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
359}
360
Matt Arsenault2aabb062013-06-18 23:37:58 +0000361bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000362 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
363}
364
Matt Arsenault2aabb062013-06-18 23:37:58 +0000365bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000366 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
367}
368
Matt Arsenault2aabb062013-06-18 23:37:58 +0000369bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000370 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
371}
372
Matt Arsenault2aabb062013-06-18 23:37:58 +0000373bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000374 MachineMemOperand *MMO = N->getMemOperand();
375 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
376 if (MMO) {
377 const Value *V = MMO->getValue();
378 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
379 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
380 return true;
381 }
382 }
383 }
384 return false;
385}
386
Matt Arsenault2aabb062013-06-18 23:37:58 +0000387bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000388 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
389 // Check to make sure we are not a constant pool load or a constant load
390 // that is marked as a private load
391 if (isCPLoad(N) || isConstantLoad(N, -1)) {
392 return false;
393 }
394 }
395 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
396 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
397 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
398 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
399 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
400 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
401 return true;
402 }
403 return false;
404}
405
406const char *AMDGPUDAGToDAGISel::getPassName() const {
407 return "AMDGPU DAG->DAG Pattern Instruction Selection";
408}
409
410#ifdef DEBUGTMP
411#undef INT64_C
412#endif
413#undef DEBUGTMP
414
Tom Stellard41fc7852013-07-23 01:48:42 +0000415//===----------------------------------------------------------------------===//
416// Complex Patterns
417//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000418
Tom Stellard365366f2013-01-23 02:09:06 +0000419bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
420 SDValue& IntPtr) {
421 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
422 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
423 return true;
424 }
425 return false;
426}
427
428bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
429 SDValue& BaseReg, SDValue &Offset) {
430 if (!dyn_cast<ConstantSDNode>(Addr)) {
431 BaseReg = Addr;
432 Offset = CurDAG->getIntPtrConstant(0, true);
433 return true;
434 }
435 return false;
436}
437
Tom Stellard75aadc22012-12-11 21:25:42 +0000438bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
439 SDValue &Offset) {
440 ConstantSDNode * IMMOffset;
441
442 if (Addr.getOpcode() == ISD::ADD
443 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
444 && isInt<16>(IMMOffset->getZExtValue())) {
445
446 Base = Addr.getOperand(0);
447 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
448 return true;
449 // If the pointer address is constant, we can move it to the offset field.
450 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
451 && isInt<16>(IMMOffset->getZExtValue())) {
452 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000453 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000454 AMDGPU::ZERO, MVT::i32);
455 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
456 return true;
457 }
458
459 // Default case, no offset
460 Base = Addr;
461 Offset = CurDAG->getTargetConstant(0, MVT::i32);
462 return true;
463}
464
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000465bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
466 SDValue &Offset) {
467 ConstantSDNode *C;
468
469 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
470 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
471 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
472 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
473 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
474 Base = Addr.getOperand(0);
475 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
476 } else {
477 Base = Addr;
478 Offset = CurDAG->getTargetConstant(0, MVT::i32);
479 }
480
481 return true;
482}
Christian Konigd910b7d2013-02-26 17:52:16 +0000483
Tom Stellard41fc7852013-07-23 01:48:42 +0000484SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
485 APInt Demanded = APInt(32, 0x00FFFFFF);
486 APInt KnownZero, KnownOne;
487 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
488 const TargetLowering *TLI = getTargetLowering();
489 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
490 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
491 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
492 return SimplifyI24(TLO.New);
493 } else {
494 return Op;
495 }
496}
497
498bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
499
500 assert(Op.getValueType() == MVT::i32);
501
502 if (CurDAG->ComputeNumSignBits(Op) == 9) {
503 I24 = SimplifyI24(Op);
504 return true;
505 }
506 return false;
507}
508
509bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
510 APInt KnownZero;
511 APInt KnownOne;
512 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
513
514 assert (Op.getValueType() == MVT::i32);
515
516 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
517 // i32. These smaller types are legal to use with the i24 instructions.
518 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
519 Op.getOpcode() == ISD::ANY_EXTEND ||
520 ISD::isEXTLoad(Op.getNode())) {
521 U24 = SimplifyI24(Op);
522 return true;
523 }
524 return false;
525}
526
Christian Konigd910b7d2013-02-26 17:52:16 +0000527void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000528 const AMDGPUTargetLowering& Lowering =
529 (*(const AMDGPUTargetLowering*)getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000530 bool IsModified = false;
531 do {
532 IsModified = false;
533 // Go over all selected nodes and try to fold them a bit more
534 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
535 E = CurDAG->allnodes_end(); I != E; ++I) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000536
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000537 SDNode *Node = I;
Tom Stellard2183b702013-06-03 17:39:46 +0000538
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000539 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
540 if (!MachineNode)
541 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +0000542
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000543 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
544 if (ResNode != Node) {
545 ReplaceUses(Node, ResNode);
546 IsModified = true;
547 }
Tom Stellard2183b702013-06-03 17:39:46 +0000548 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000549 CurDAG->RemoveDeadNodes();
550 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +0000551}