blob: a008c9618d594196c3e55d5fc449e5e4ed2a25b6 [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()) {
198 return NULL; // Already selected.
199 }
200 switch (Opc) {
201 default: break;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000202 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000203 unsigned RegClassID;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000204 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard8e5da412013-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 Kramerbda73ff2013-08-31 21:20:04 +0000243 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
Tom Stellard8e5da412013-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 Lejeune3b6f20e2013-03-05 15:04:49 +0000255 }
Tom Stellard0344cdf2013-08-01 15:23:42 +0000256
Tom Stellard8e5da412013-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 Stellard0344cdf2013-08-01 15:23:42 +0000263 }
Tom Stellard8e5da412013-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 Lejeune3b6f20e2013-03-05 15:04:49 +0000273 bool IsRegSeq = true;
274 for (unsigned i = 0; i < N->getNumOperands(); i++) {
Tom Stellard8e5da412013-08-14 23:24:32 +0000275 // XXX: Why is this here?
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000276 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
277 IsRegSeq = false;
278 break;
279 }
Tom Stellard8e5da412013-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 Lejeune3b6f20e2013-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 Stellard754f80f2013-04-05 23:31:51 +0000289 case ISD::BUILD_PAIR: {
290 SDValue RC, SubReg0, SubReg1;
291 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000292 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-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 Stellard2f7cdda2013-08-06 23:08:28 +0000300 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
Tom Stellard754f80f2013-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 Trickef9de2a2013-05-25 02:42:55 +0000309 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000310 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000311 }
Vincent Lejeune0167a312013-09-12 23:45:00 +0000312 return SelectCode(N);
Tom Stellard365366f2013-01-23 02:09:06 +0000313}
314
Tom Stellard75aadc22012-12-11 21:25:42 +0000315
316bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
317 if (!ptr) {
318 return false;
319 }
320 Type *ptrType = ptr->getType();
321 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
322}
323
Tom Stellard75aadc22012-12-11 21:25:42 +0000324bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
325 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
326}
327
328bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
329 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
330 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
331 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
332}
333
334bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
335 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
336}
337
338bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
339 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
340}
341
Tom Stellard1e803092013-07-23 01:48:18 +0000342bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
343 if (CbId == -1) {
344 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000345 }
Tom Stellard1e803092013-07-23 01:48:18 +0000346 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellard75aadc22012-12-11 21:25:42 +0000347}
348
Matt Arsenault2aabb062013-06-18 23:37:58 +0000349bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard8cb0e472013-07-23 23:54:56 +0000350 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
351 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
352 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
353 N->getMemoryVT().bitsLT(MVT::i32)) {
354 return true;
355 }
356 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000357 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
358}
359
Matt Arsenault2aabb062013-06-18 23:37:58 +0000360bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000361 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
362}
363
Matt Arsenault2aabb062013-06-18 23:37:58 +0000364bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000365 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
366}
367
Matt Arsenault2aabb062013-06-18 23:37:58 +0000368bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000369 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
370}
371
Matt Arsenault2aabb062013-06-18 23:37:58 +0000372bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000373 MachineMemOperand *MMO = N->getMemOperand();
374 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
375 if (MMO) {
376 const Value *V = MMO->getValue();
377 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
378 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
379 return true;
380 }
381 }
382 }
383 return false;
384}
385
Matt Arsenault2aabb062013-06-18 23:37:58 +0000386bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000387 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
388 // Check to make sure we are not a constant pool load or a constant load
389 // that is marked as a private load
390 if (isCPLoad(N) || isConstantLoad(N, -1)) {
391 return false;
392 }
393 }
394 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
395 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
396 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
397 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
398 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
399 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
400 return true;
401 }
402 return false;
403}
404
405const char *AMDGPUDAGToDAGISel::getPassName() const {
406 return "AMDGPU DAG->DAG Pattern Instruction Selection";
407}
408
409#ifdef DEBUGTMP
410#undef INT64_C
411#endif
412#undef DEBUGTMP
413
Tom Stellard41fc7852013-07-23 01:48:42 +0000414//===----------------------------------------------------------------------===//
415// Complex Patterns
416//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000417
Tom Stellard365366f2013-01-23 02:09:06 +0000418bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
419 SDValue& IntPtr) {
420 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
421 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
422 return true;
423 }
424 return false;
425}
426
427bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
428 SDValue& BaseReg, SDValue &Offset) {
429 if (!dyn_cast<ConstantSDNode>(Addr)) {
430 BaseReg = Addr;
431 Offset = CurDAG->getIntPtrConstant(0, true);
432 return true;
433 }
434 return false;
435}
436
Tom Stellard75aadc22012-12-11 21:25:42 +0000437bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
438 SDValue &Offset) {
439 ConstantSDNode * IMMOffset;
440
441 if (Addr.getOpcode() == ISD::ADD
442 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
443 && isInt<16>(IMMOffset->getZExtValue())) {
444
445 Base = Addr.getOperand(0);
446 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
447 return true;
448 // If the pointer address is constant, we can move it to the offset field.
449 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
450 && isInt<16>(IMMOffset->getZExtValue())) {
451 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000452 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000453 AMDGPU::ZERO, MVT::i32);
454 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
455 return true;
456 }
457
458 // Default case, no offset
459 Base = Addr;
460 Offset = CurDAG->getTargetConstant(0, MVT::i32);
461 return true;
462}
463
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000464bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
465 SDValue &Offset) {
466 ConstantSDNode *C;
467
468 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
469 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
470 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
471 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
472 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
473 Base = Addr.getOperand(0);
474 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
475 } else {
476 Base = Addr;
477 Offset = CurDAG->getTargetConstant(0, MVT::i32);
478 }
479
480 return true;
481}
Christian Konigd910b7d2013-02-26 17:52:16 +0000482
Tom Stellard41fc7852013-07-23 01:48:42 +0000483SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
484 APInt Demanded = APInt(32, 0x00FFFFFF);
485 APInt KnownZero, KnownOne;
486 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
487 const TargetLowering *TLI = getTargetLowering();
488 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
489 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
490 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
491 return SimplifyI24(TLO.New);
492 } else {
493 return Op;
494 }
495}
496
497bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
498
499 assert(Op.getValueType() == MVT::i32);
500
501 if (CurDAG->ComputeNumSignBits(Op) == 9) {
502 I24 = SimplifyI24(Op);
503 return true;
504 }
505 return false;
506}
507
508bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
509 APInt KnownZero;
510 APInt KnownOne;
511 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
512
513 assert (Op.getValueType() == MVT::i32);
514
515 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
516 // i32. These smaller types are legal to use with the i24 instructions.
517 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
518 Op.getOpcode() == ISD::ANY_EXTEND ||
519 ISD::isEXTLoad(Op.getNode())) {
520 U24 = SimplifyI24(Op);
521 return true;
522 }
523 return false;
524}
525
Christian Konigd910b7d2013-02-26 17:52:16 +0000526void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000527 const AMDGPUTargetLowering& Lowering =
528 (*(const AMDGPUTargetLowering*)getTargetLowering());
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000529 bool IsModified = false;
530 do {
531 IsModified = false;
532 // Go over all selected nodes and try to fold them a bit more
533 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
534 E = CurDAG->allnodes_end(); I != E; ++I) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000535
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000536 SDNode *Node = I;
Tom Stellard2183b702013-06-03 17:39:46 +0000537
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000538 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
539 if (!MachineNode)
540 continue;
Christian Konigd910b7d2013-02-26 17:52:16 +0000541
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000542 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
543 if (ResNode != Node) {
544 ReplaceUses(Node, ResNode);
545 IsModified = true;
546 }
Tom Stellard2183b702013-06-03 17:39:46 +0000547 }
Vincent Lejeuneab3baf82013-09-12 23:44:44 +0000548 CurDAG->RemoveDeadNodes();
549 } while (IsModified);
Christian Konigd910b7d2013-02-26 17:52:16 +0000550}