blob: 46e50bc52cf2119f8b9aefca2e3835b454b70dba [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) {
Tom Stellard84021442013-07-23 01:48:24 +0000196 const R600InstrInfo *TII =
197 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +0000198 unsigned int Opc = N->getOpcode();
199 if (N->isMachineOpcode()) {
200 return NULL; // Already selected.
201 }
202 switch (Opc) {
203 default: break;
Tom Stellard84021442013-07-23 01:48:24 +0000204 case AMDGPUISD::CONST_ADDRESS: {
205 for (SDNode::use_iterator I = N->use_begin(), Next = llvm::next(I);
206 I != SDNode::use_end(); I = Next) {
207 Next = llvm::next(I);
208 if (!I->isMachineOpcode()) {
209 continue;
210 }
211 unsigned Opcode = I->getMachineOpcode();
212 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
213 int SrcIdx = I.getOperandNo();
214 int SelIdx;
215 // Unlike MachineInstrs, SDNodes do not have results in their operand
216 // list, so we need to increment the SrcIdx, since
217 // R600InstrInfo::getOperandIdx is based on the MachineInstr indices.
218 if (HasDst) {
219 SrcIdx++;
220 }
221
222 SelIdx = TII->getSelIdx(I->getMachineOpcode(), SrcIdx);
223 if (SelIdx < 0) {
224 continue;
225 }
226
227 SDValue CstOffset;
228 if (N->getValueType(0).isVector() ||
229 !SelectGlobalValueConstantOffset(N->getOperand(0), CstOffset))
230 continue;
231
232 // Gather constants values
233 int SrcIndices[] = {
234 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
235 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
236 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
237 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
238 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
239 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
240 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
241 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
242 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
243 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
244 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
245 };
246 std::vector<unsigned> Consts;
247 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
248 int OtherSrcIdx = SrcIndices[i];
249 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
250 if (OtherSrcIdx < 0 || OtherSelIdx < 0) {
251 continue;
252 }
253 if (HasDst) {
254 OtherSrcIdx--;
255 OtherSelIdx--;
256 }
257 if (RegisterSDNode *Reg =
258 dyn_cast<RegisterSDNode>(I->getOperand(OtherSrcIdx))) {
259 if (Reg->getReg() == AMDGPU::ALU_CONST) {
260 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(I->getOperand(OtherSelIdx));
261 Consts.push_back(Cst->getZExtValue());
262 }
263 }
264 }
265
266 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
267 Consts.push_back(Cst->getZExtValue());
268 if (!TII->fitsConstReadLimitations(Consts))
269 continue;
270
271 // Convert back to SDNode indices
272 if (HasDst) {
273 SrcIdx--;
274 SelIdx--;
275 }
276 std::vector<SDValue> Ops;
277 for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
278 if (i == SrcIdx) {
279 Ops.push_back(CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32));
280 } else if (i == SelIdx) {
281 Ops.push_back(CstOffset);
282 } else {
283 Ops.push_back(I->getOperand(i));
284 }
285 }
286 CurDAG->UpdateNodeOperands(*I, Ops.data(), Ops.size());
287 }
288 break;
289 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000290 case ISD::BUILD_VECTOR: {
Tom Stellard8e5da412013-08-14 23:24:32 +0000291 unsigned RegClassID;
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000292 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard8e5da412013-08-14 23:24:32 +0000293 const AMDGPURegisterInfo *TRI =
294 static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
295 const SIRegisterInfo *SIRI =
296 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
297 EVT VT = N->getValueType(0);
298 unsigned NumVectorElts = VT.getVectorNumElements();
299 assert(VT.getVectorElementType().bitsEq(MVT::i32));
300 if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
301 bool UseVReg = true;
302 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
303 U != E; ++U) {
304 if (!U->isMachineOpcode()) {
305 continue;
306 }
307 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
308 if (!RC) {
309 continue;
310 }
311 if (SIRI->isSGPRClass(RC)) {
312 UseVReg = false;
313 }
314 }
315 switch(NumVectorElts) {
316 case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
317 AMDGPU::SReg_32RegClassID;
318 break;
319 case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
320 AMDGPU::SReg_64RegClassID;
321 break;
322 case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
323 AMDGPU::SReg_128RegClassID;
324 break;
325 case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
326 AMDGPU::SReg_256RegClassID;
327 break;
328 case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
329 AMDGPU::SReg_512RegClassID;
330 break;
Benjamin Kramerbda73ff2013-08-31 21:20:04 +0000331 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
Tom Stellard8e5da412013-08-14 23:24:32 +0000332 }
333 } else {
334 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
335 // that adds a 128 bits reg copy when going through TwoAddressInstructions
336 // pass. We want to avoid 128 bits copies as much as possible because they
337 // can't be bundled by our scheduler.
338 switch(NumVectorElts) {
339 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
340 case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
341 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
342 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000343 }
Tom Stellard0344cdf2013-08-01 15:23:42 +0000344
Tom Stellard8e5da412013-08-14 23:24:32 +0000345 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
346
347 if (NumVectorElts == 1) {
348 return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
349 VT.getVectorElementType(),
350 N->getOperand(0), RegClass);
Tom Stellard0344cdf2013-08-01 15:23:42 +0000351 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000352
353 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
354 "supported yet");
355 // 16 = Max Num Vector Elements
356 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
357 // 1 = Vector Register Class
358 SDValue RegSeqArgs[16 * 2 + 1];
359
360 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000361 bool IsRegSeq = true;
362 for (unsigned i = 0; i < N->getNumOperands(); i++) {
Tom Stellard8e5da412013-08-14 23:24:32 +0000363 // XXX: Why is this here?
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000364 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
365 IsRegSeq = false;
366 break;
367 }
Tom Stellard8e5da412013-08-14 23:24:32 +0000368 RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
369 RegSeqArgs[1 + (2 * i) + 1] =
370 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000371 }
372 if (!IsRegSeq)
373 break;
374 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
375 RegSeqArgs, 2 * N->getNumOperands() + 1);
376 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000377 case ISD::BUILD_PAIR: {
378 SDValue RC, SubReg0, SubReg1;
379 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000380 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-04-05 23:31:51 +0000381 break;
382 }
383 if (N->getValueType(0) == MVT::i128) {
384 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
385 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
386 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
387 } else if (N->getValueType(0) == MVT::i64) {
Tom Stellard2f7cdda2013-08-06 23:08:28 +0000388 RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
Tom Stellard754f80f2013-04-05 23:31:51 +0000389 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
390 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
391 } else {
392 llvm_unreachable("Unhandled value type for BUILD_PAIR");
393 }
394 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
395 N->getOperand(1), SubReg1 };
396 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000397 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000398 }
399
Tom Stellard75aadc22012-12-11 21:25:42 +0000400 case ISD::ConstantFP:
401 case ISD::Constant: {
402 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
403 // XXX: Custom immediate lowering not implemented yet. Instead we use
404 // pseudo instructions defined in SIInstructions.td
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000405 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000406 break;
407 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000408
409 uint64_t ImmValue = 0;
410 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
411
412 if (N->getOpcode() == ISD::ConstantFP) {
413 // XXX: 64-bit Immediates not supported yet
414 assert(N->getValueType(0) != MVT::f64);
415
416 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
417 APFloat Value = C->getValueAPF();
418 float FloatValue = Value.convertToFloat();
419 if (FloatValue == 0.0) {
420 ImmReg = AMDGPU::ZERO;
421 } else if (FloatValue == 0.5) {
422 ImmReg = AMDGPU::HALF;
423 } else if (FloatValue == 1.0) {
424 ImmReg = AMDGPU::ONE;
425 } else {
426 ImmValue = Value.bitcastToAPInt().getZExtValue();
427 }
428 } else {
429 // XXX: 64-bit Immediates not supported yet
430 assert(N->getValueType(0) != MVT::i64);
431
432 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
433 if (C->getZExtValue() == 0) {
434 ImmReg = AMDGPU::ZERO;
435 } else if (C->getZExtValue() == 1) {
436 ImmReg = AMDGPU::ONE_INT;
437 } else {
438 ImmValue = C->getZExtValue();
439 }
440 }
441
442 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
443 Use != SDNode::use_end(); Use = Next) {
444 Next = llvm::next(Use);
445 std::vector<SDValue> Ops;
446 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
447 Ops.push_back(Use->getOperand(i));
448 }
449
450 if (!Use->isMachineOpcode()) {
451 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
452 // We can only use literal constants (e.g. AMDGPU::ZERO,
453 // AMDGPU::ONE, etc) in machine opcodes.
454 continue;
455 }
456 } else {
Tom Stellard16da74c2013-08-16 01:11:55 +0000457 switch(Use->getMachineOpcode()) {
458 case AMDGPU::REG_SEQUENCE: break;
459 default:
460 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
461 (TII->get(Use->getMachineOpcode()).TSFlags &
462 R600_InstFlag::VECTOR)) {
463 continue;
464 }
Tom Stellardc026e8b2013-06-28 15:47:08 +0000465 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000466
467 // Check that we aren't already using an immediate.
468 // XXX: It's possible for an instruction to have more than one
469 // immediate operand, but this is not supported yet.
470 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
Tom Stellard16da74c2013-08-16 01:11:55 +0000471 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(),
472 AMDGPU::OpName::literal);
473 if (ImmIdx == -1) {
474 continue;
475 }
476
477 if (TII->getOperandIdx(Use->getMachineOpcode(),
478 AMDGPU::OpName::dst) != -1) {
479 // subtract one from ImmIdx, because the DST operand is usually index
480 // 0 for MachineInstrs, but we have no DST in the Ops vector.
481 ImmIdx--;
482 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000483 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
484 assert(C);
485
486 if (C->getZExtValue() != 0) {
487 // This instruction is already using an immediate.
488 continue;
489 }
490
491 // Set the immediate value
492 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
493 }
494 }
495 // Set the immediate register
496 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
497
498 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
499 }
500 break;
501 }
502 }
Tom Stellard365366f2013-01-23 02:09:06 +0000503 SDNode *Result = SelectCode(N);
504
505 // Fold operands of selected node
506
507 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000508 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard365366f2013-01-23 02:09:06 +0000509 const R600InstrInfo *TII =
510 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunec6896792013-06-04 23:17:15 +0000511 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
512 bool IsModified = false;
513 do {
514 std::vector<SDValue> Ops;
515 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
516 I != E; ++I)
517 Ops.push_back(*I);
518 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
519 if (IsModified) {
520 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
521 }
522 } while (IsModified);
Matt Arsenault2aabb062013-06-18 23:37:58 +0000523
Vincent Lejeunec6896792013-06-04 23:17:15 +0000524 }
Vincent Lejeunef694c102013-02-14 16:55:01 +0000525 if (Result && Result->isMachineOpcode() &&
526 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellardc026e8b2013-06-28 15:47:08 +0000527 && TII->hasInstrModifiers(Result->getMachineOpcode())) {
Tom Stellard84021442013-07-23 01:48:24 +0000528 // Fold FNEG/FABS
Tom Stellard49269212013-01-31 22:11:54 +0000529 // TODO: Isel can generate multiple MachineInst, we need to recursively
530 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000531 bool IsModified = false;
532 do {
533 std::vector<SDValue> Ops;
534 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
535 I != E; ++I)
536 Ops.push_back(*I);
537 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
538 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000539 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000540 }
541 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000542
543 // If node has a single use which is CLAMP_R600, folds it
544 if (Result->hasOneUse() && Result->isMachineOpcode()) {
545 SDNode *PotentialClamp = *Result->use_begin();
546 if (PotentialClamp->isMachineOpcode() &&
547 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
548 unsigned ClampIdx =
Tom Stellard02661d92013-06-25 21:22:18 +0000549 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
Tom Stellard49269212013-01-31 22:11:54 +0000550 std::vector<SDValue> Ops;
551 unsigned NumOp = Result->getNumOperands();
552 for (unsigned i = 0; i < NumOp; ++i) {
553 Ops.push_back(Result->getOperand(i));
554 }
555 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
556 Result = CurDAG->SelectNodeTo(PotentialClamp,
557 Result->getMachineOpcode(), PotentialClamp->getVTList(),
558 Ops.data(), NumOp);
559 }
560 }
Tom Stellard365366f2013-01-23 02:09:06 +0000561 }
562 }
563
564 return Result;
565}
566
Vincent Lejeunec6896792013-06-04 23:17:15 +0000567bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
Tom Stellard84021442013-07-23 01:48:24 +0000568 SDValue &Abs, const R600InstrInfo *TII) {
Vincent Lejeunec6896792013-06-04 23:17:15 +0000569 switch (Src.getOpcode()) {
Vincent Lejeunec6896792013-06-04 23:17:15 +0000570 case ISD::FNEG:
571 Src = Src.getOperand(0);
572 Neg = CurDAG->getTargetConstant(1, MVT::i32);
573 return true;
574 case ISD::FABS:
575 if (!Abs.getNode())
576 return false;
577 Src = Src.getOperand(0);
578 Abs = CurDAG->getTargetConstant(1, MVT::i32);
579 return true;
580 case ISD::BITCAST:
581 Src = Src.getOperand(0);
582 return true;
583 default:
584 return false;
585 }
586}
587
Tom Stellard365366f2013-01-23 02:09:06 +0000588bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
589 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
590 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000591 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
592 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
593 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
Tom Stellard365366f2013-01-23 02:09:06 +0000594 };
595 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000596 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
597 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
598 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
Tom Stellard365366f2013-01-23 02:09:06 +0000599 };
Tom Stellard49269212013-01-31 22:11:54 +0000600 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000601 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
602 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
603 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
Tom Stellard49269212013-01-31 22:11:54 +0000604 };
605 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000606 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
607 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
Tom Stellard49269212013-01-31 22:11:54 +0000608 -1
609 };
610
Vincent Lejeunec6896792013-06-04 23:17:15 +0000611
Tom Stellard365366f2013-01-23 02:09:06 +0000612 for (unsigned i = 0; i < 3; i++) {
613 if (OperandIdx[i] < 0)
614 return false;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000615 SDValue &Src = Ops[OperandIdx[i] - 1];
616 SDValue &Sel = Ops[SelIdx[i] - 1];
617 SDValue &Neg = Ops[NegIdx[i] - 1];
618 SDValue FakeAbs;
619 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
Tom Stellard84021442013-07-23 01:48:24 +0000620 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunec6896792013-06-04 23:17:15 +0000621 return true;
622 }
623 return false;
624}
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000625
Vincent Lejeunec6896792013-06-04 23:17:15 +0000626bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
627 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
628 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000629 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
630 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
631 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
632 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
633 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
634 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
635 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
636 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000637 };
638 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000639 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
640 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
641 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
642 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
643 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
644 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
645 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
646 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000647 };
648 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000649 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
650 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
651 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
652 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
653 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
654 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
655 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
656 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000657 };
658 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000659 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
660 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
661 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
662 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
663 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
664 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
665 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
666 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000667 };
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000668
Vincent Lejeunec6896792013-06-04 23:17:15 +0000669 for (unsigned i = 0; i < 8; i++) {
670 if (OperandIdx[i] < 0)
671 return false;
672 SDValue &Src = Ops[OperandIdx[i] - 1];
673 SDValue &Sel = Ops[SelIdx[i] - 1];
674 SDValue &Neg = Ops[NegIdx[i] - 1];
675 SDValue &Abs = Ops[AbsIdx[i] - 1];
Tom Stellard84021442013-07-23 01:48:24 +0000676 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunec6896792013-06-04 23:17:15 +0000677 return true;
678 }
Tom Stellard365366f2013-01-23 02:09:06 +0000679 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000680}
681
682bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
683 if (!ptr) {
684 return false;
685 }
686 Type *ptrType = ptr->getType();
687 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
688}
689
Tom Stellard75aadc22012-12-11 21:25:42 +0000690bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
691 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
692}
693
694bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
695 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
696 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
697 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
698}
699
700bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
701 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
702}
703
704bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
705 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
706}
707
Tom Stellard1e803092013-07-23 01:48:18 +0000708bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
709 if (CbId == -1) {
710 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000711 }
Tom Stellard1e803092013-07-23 01:48:18 +0000712 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellard75aadc22012-12-11 21:25:42 +0000713}
714
Matt Arsenault2aabb062013-06-18 23:37:58 +0000715bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard8cb0e472013-07-23 23:54:56 +0000716 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
717 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
718 if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
719 N->getMemoryVT().bitsLT(MVT::i32)) {
720 return true;
721 }
722 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000723 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
724}
725
Matt Arsenault2aabb062013-06-18 23:37:58 +0000726bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000727 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
728}
729
Matt Arsenault2aabb062013-06-18 23:37:58 +0000730bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000731 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
732}
733
Matt Arsenault2aabb062013-06-18 23:37:58 +0000734bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000735 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
736}
737
Matt Arsenault2aabb062013-06-18 23:37:58 +0000738bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000739 MachineMemOperand *MMO = N->getMemOperand();
740 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
741 if (MMO) {
742 const Value *V = MMO->getValue();
743 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
744 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
745 return true;
746 }
747 }
748 }
749 return false;
750}
751
Matt Arsenault2aabb062013-06-18 23:37:58 +0000752bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000753 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
754 // Check to make sure we are not a constant pool load or a constant load
755 // that is marked as a private load
756 if (isCPLoad(N) || isConstantLoad(N, -1)) {
757 return false;
758 }
759 }
760 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
761 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
762 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
763 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
764 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
765 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
766 return true;
767 }
768 return false;
769}
770
771const char *AMDGPUDAGToDAGISel::getPassName() const {
772 return "AMDGPU DAG->DAG Pattern Instruction Selection";
773}
774
775#ifdef DEBUGTMP
776#undef INT64_C
777#endif
778#undef DEBUGTMP
779
Tom Stellard41fc7852013-07-23 01:48:42 +0000780//===----------------------------------------------------------------------===//
781// Complex Patterns
782//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000783
Tom Stellard365366f2013-01-23 02:09:06 +0000784bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
785 SDValue& IntPtr) {
786 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
787 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
788 return true;
789 }
790 return false;
791}
792
793bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
794 SDValue& BaseReg, SDValue &Offset) {
795 if (!dyn_cast<ConstantSDNode>(Addr)) {
796 BaseReg = Addr;
797 Offset = CurDAG->getIntPtrConstant(0, true);
798 return true;
799 }
800 return false;
801}
802
Tom Stellard75aadc22012-12-11 21:25:42 +0000803bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
804 SDValue &Offset) {
805 ConstantSDNode * IMMOffset;
806
807 if (Addr.getOpcode() == ISD::ADD
808 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
809 && isInt<16>(IMMOffset->getZExtValue())) {
810
811 Base = Addr.getOperand(0);
812 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
813 return true;
814 // If the pointer address is constant, we can move it to the offset field.
815 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
816 && isInt<16>(IMMOffset->getZExtValue())) {
817 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000818 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000819 AMDGPU::ZERO, MVT::i32);
820 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
821 return true;
822 }
823
824 // Default case, no offset
825 Base = Addr;
826 Offset = CurDAG->getTargetConstant(0, MVT::i32);
827 return true;
828}
829
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000830bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
831 SDValue &Offset) {
832 ConstantSDNode *C;
833
834 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
835 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
836 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
837 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
838 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
839 Base = Addr.getOperand(0);
840 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
841 } else {
842 Base = Addr;
843 Offset = CurDAG->getTargetConstant(0, MVT::i32);
844 }
845
846 return true;
847}
Christian Konigd910b7d2013-02-26 17:52:16 +0000848
Tom Stellard41fc7852013-07-23 01:48:42 +0000849SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
850 APInt Demanded = APInt(32, 0x00FFFFFF);
851 APInt KnownZero, KnownOne;
852 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
853 const TargetLowering *TLI = getTargetLowering();
854 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
855 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
856 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
857 return SimplifyI24(TLO.New);
858 } else {
859 return Op;
860 }
861}
862
863bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
864
865 assert(Op.getValueType() == MVT::i32);
866
867 if (CurDAG->ComputeNumSignBits(Op) == 9) {
868 I24 = SimplifyI24(Op);
869 return true;
870 }
871 return false;
872}
873
874bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
875 APInt KnownZero;
876 APInt KnownOne;
877 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
878
879 assert (Op.getValueType() == MVT::i32);
880
881 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
882 // i32. These smaller types are legal to use with the i24 instructions.
883 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
884 Op.getOpcode() == ISD::ANY_EXTEND ||
885 ISD::isEXTLoad(Op.getNode())) {
886 U24 = SimplifyI24(Op);
887 return true;
888 }
889 return false;
890}
891
Christian Konigd910b7d2013-02-26 17:52:16 +0000892void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
893
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000894 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Tom Stellard2183b702013-06-03 17:39:46 +0000895 return;
896 }
897
Christian Konigd910b7d2013-02-26 17:52:16 +0000898 // Go over all selected nodes and try to fold them a bit more
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000899 const AMDGPUTargetLowering& Lowering =
900 (*(const AMDGPUTargetLowering*)getTargetLowering());
Christian Konigd910b7d2013-02-26 17:52:16 +0000901 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
902 E = CurDAG->allnodes_end(); I != E; ++I) {
903
Tom Stellard2183b702013-06-03 17:39:46 +0000904 SDNode *Node = I;
Tom Stellard2183b702013-06-03 17:39:46 +0000905
906 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
907 if (!MachineNode)
Christian Konigd910b7d2013-02-26 17:52:16 +0000908 continue;
909
Tom Stellard2183b702013-06-03 17:39:46 +0000910 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
911 if (ResNode != Node) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000912 ReplaceUses(Node, ResNode);
Tom Stellard2183b702013-06-03 17:39:46 +0000913 }
Christian Konigd910b7d2013-02-26 17:52:16 +0000914 }
915}