blob: e4fb07dea37f990680be7cc248bb28650d9fecc0 [file] [log] [blame]
Tom Stellardf98f2ce2012-12-11 21:25:42 +00001//===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//==-----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Defines an instruction selector for the AMDGPU target.
12//
13//===----------------------------------------------------------------------===//
14#include "AMDGPUInstrInfo.h"
15#include "AMDGPUISelLowering.h" // For AMDGPUISD
16#include "AMDGPURegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000017#include "R600InstrInfo.h"
Christian Konigd3b55092013-02-26 17:52:23 +000018#include "SIISelLowering.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000019#include "llvm/ADT/ValueMap.h"
Matt Arsenault70a3dc12013-06-18 23:37:58 +000020#include "llvm/Analysis/ValueTracking.h"
Tom Stellard8a72c732013-06-03 17:39:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Benjamin Kramer5c352902013-05-23 17:10:37 +000023#include "llvm/CodeGen/SelectionDAG.h"
Tom Stellardf98f2ce2012-12-11 21:25:42 +000024#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/Support/Compiler.h"
26#include <list>
27#include <queue>
28
29using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Instruction Selector Implementation
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// AMDGPU specific code to select AMDGPU machine instructions for
37/// SelectionDAG operations.
38class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40 // make the right decision when generating code for different targets.
41 const AMDGPUSubtarget &Subtarget;
42public:
43 AMDGPUDAGToDAGISel(TargetMachine &TM);
44 virtual ~AMDGPUDAGToDAGISel();
45
46 SDNode *Select(SDNode *N);
47 virtual const char *getPassName() const;
Christian Konigc018eca2013-02-26 17:52:16 +000048 virtual void PostprocessISelDAG();
Tom Stellardf98f2ce2012-12-11 21:25:42 +000049
50private:
51 inline SDValue getSmallIPtrImm(unsigned Imm);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000052 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
Tom Stellard58d33352013-07-23 01:48:24 +000053 const R600InstrInfo *TII);
Tom Stellard9f7818d2013-01-23 02:09:06 +000054 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Vincent Lejeunee67a4af2013-06-04 23:17:15 +000055 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000056
57 // Complex pattern selectors
58 bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
59 bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
60 bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
61
62 static bool checkType(const Value *ptr, unsigned int addrspace);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000063
64 static bool isGlobalStore(const StoreSDNode *N);
65 static bool isPrivateStore(const StoreSDNode *N);
66 static bool isLocalStore(const StoreSDNode *N);
67 static bool isRegionStore(const StoreSDNode *N);
68
Matt Arsenault70a3dc12013-06-18 23:37:58 +000069 bool isCPLoad(const LoadSDNode *N) const;
70 bool isConstantLoad(const LoadSDNode *N, int cbID) const;
71 bool isGlobalLoad(const LoadSDNode *N) const;
72 bool isParamLoad(const LoadSDNode *N) const;
73 bool isPrivateLoad(const LoadSDNode *N) const;
74 bool isLocalLoad(const LoadSDNode *N) const;
75 bool isRegionLoad(const LoadSDNode *N) const;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000076
Tom Stellard9f7818d2013-01-23 02:09:06 +000077 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
78 bool SelectGlobalValueVariableOffset(SDValue Addr,
79 SDValue &BaseReg, SDValue& Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000080 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardc0b0c672013-02-06 17:32:29 +000081 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000082
83 // Include the pieces autogenerated from the target description.
84#include "AMDGPUGenDAGISel.inc"
85};
86} // end anonymous namespace
87
88/// \brief This pass converts a legalized DAG into a AMDGPU-specific
89// DAG, ready for instruction scheduling.
90FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
91 ) {
92 return new AMDGPUDAGToDAGISel(TM);
93}
94
Bill Wendlingba54bca2013-06-19 21:36:55 +000095AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
Tom Stellardf98f2ce2012-12-11 21:25:42 +000096 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
97}
98
99AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
100}
101
102SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
103 return CurDAG->getTargetConstant(Imm, MVT::i32);
104}
105
106bool AMDGPUDAGToDAGISel::SelectADDRParam(
107 SDValue Addr, SDValue& R1, SDValue& R2) {
108
109 if (Addr.getOpcode() == ISD::FrameIndex) {
110 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
111 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
112 R2 = CurDAG->getTargetConstant(0, MVT::i32);
113 } else {
114 R1 = Addr;
115 R2 = CurDAG->getTargetConstant(0, MVT::i32);
116 }
117 } else if (Addr.getOpcode() == ISD::ADD) {
118 R1 = Addr.getOperand(0);
119 R2 = Addr.getOperand(1);
120 } else {
121 R1 = Addr;
122 R2 = CurDAG->getTargetConstant(0, MVT::i32);
123 }
124 return true;
125}
126
127bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
128 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
129 Addr.getOpcode() == ISD::TargetGlobalAddress) {
130 return false;
131 }
132 return SelectADDRParam(Addr, R1, R2);
133}
134
135
136bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
137 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
138 Addr.getOpcode() == ISD::TargetGlobalAddress) {
139 return false;
140 }
141
142 if (Addr.getOpcode() == ISD::FrameIndex) {
143 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
144 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
145 R2 = CurDAG->getTargetConstant(0, MVT::i64);
146 } else {
147 R1 = Addr;
148 R2 = CurDAG->getTargetConstant(0, MVT::i64);
149 }
150 } else if (Addr.getOpcode() == ISD::ADD) {
151 R1 = Addr.getOperand(0);
152 R2 = Addr.getOperand(1);
153 } else {
154 R1 = Addr;
155 R2 = CurDAG->getTargetConstant(0, MVT::i64);
156 }
157 return true;
158}
159
160SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard58d33352013-07-23 01:48:24 +0000161 const R600InstrInfo *TII =
162 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000163 unsigned int Opc = N->getOpcode();
164 if (N->isMachineOpcode()) {
165 return NULL; // Already selected.
166 }
167 switch (Opc) {
168 default: break;
Tom Stellard58d33352013-07-23 01:48:24 +0000169 case AMDGPUISD::CONST_ADDRESS: {
170 for (SDNode::use_iterator I = N->use_begin(), Next = llvm::next(I);
171 I != SDNode::use_end(); I = Next) {
172 Next = llvm::next(I);
173 if (!I->isMachineOpcode()) {
174 continue;
175 }
176 unsigned Opcode = I->getMachineOpcode();
177 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
178 int SrcIdx = I.getOperandNo();
179 int SelIdx;
180 // Unlike MachineInstrs, SDNodes do not have results in their operand
181 // list, so we need to increment the SrcIdx, since
182 // R600InstrInfo::getOperandIdx is based on the MachineInstr indices.
183 if (HasDst) {
184 SrcIdx++;
185 }
186
187 SelIdx = TII->getSelIdx(I->getMachineOpcode(), SrcIdx);
188 if (SelIdx < 0) {
189 continue;
190 }
191
192 SDValue CstOffset;
193 if (N->getValueType(0).isVector() ||
194 !SelectGlobalValueConstantOffset(N->getOperand(0), CstOffset))
195 continue;
196
197 // Gather constants values
198 int SrcIndices[] = {
199 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
200 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
201 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
202 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
203 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
204 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
205 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
206 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
207 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
208 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
209 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
210 };
211 std::vector<unsigned> Consts;
212 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
213 int OtherSrcIdx = SrcIndices[i];
214 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
215 if (OtherSrcIdx < 0 || OtherSelIdx < 0) {
216 continue;
217 }
218 if (HasDst) {
219 OtherSrcIdx--;
220 OtherSelIdx--;
221 }
222 if (RegisterSDNode *Reg =
223 dyn_cast<RegisterSDNode>(I->getOperand(OtherSrcIdx))) {
224 if (Reg->getReg() == AMDGPU::ALU_CONST) {
225 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(I->getOperand(OtherSelIdx));
226 Consts.push_back(Cst->getZExtValue());
227 }
228 }
229 }
230
231 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
232 Consts.push_back(Cst->getZExtValue());
233 if (!TII->fitsConstReadLimitations(Consts))
234 continue;
235
236 // Convert back to SDNode indices
237 if (HasDst) {
238 SrcIdx--;
239 SelIdx--;
240 }
241 std::vector<SDValue> Ops;
242 for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
243 if (i == SrcIdx) {
244 Ops.push_back(CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32));
245 } else if (i == SelIdx) {
246 Ops.push_back(CstOffset);
247 } else {
248 Ops.push_back(I->getOperand(i));
249 }
250 }
251 CurDAG->UpdateNodeOperands(*I, Ops.data(), Ops.size());
252 }
253 break;
254 }
Vincent Lejeunecae68012013-03-05 15:04:49 +0000255 case ISD::BUILD_VECTOR: {
256 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000257 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Vincent Lejeunecae68012013-03-05 15:04:49 +0000258 break;
259 }
260 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
261 // that adds a 128 bits reg copy when going through TwoAddressInstructions
262 // pass. We want to avoid 128 bits copies as much as possible because they
263 // can't be bundled by our scheduler.
264 SDValue RegSeqArgs[9] = {
265 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
266 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
267 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
268 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
269 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
270 };
271 bool IsRegSeq = true;
272 for (unsigned i = 0; i < N->getNumOperands(); i++) {
273 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
274 IsRegSeq = false;
275 break;
276 }
277 RegSeqArgs[2 * i + 1] = N->getOperand(i);
278 }
279 if (!IsRegSeq)
280 break;
281 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
282 RegSeqArgs, 2 * N->getNumOperands() + 1);
283 }
Tom Stellard17ea10c2013-04-05 23:31:51 +0000284 case ISD::BUILD_PAIR: {
285 SDValue RC, SubReg0, SubReg1;
286 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000287 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard17ea10c2013-04-05 23:31:51 +0000288 break;
289 }
290 if (N->getValueType(0) == MVT::i128) {
291 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
292 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
293 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
294 } else if (N->getValueType(0) == MVT::i64) {
295 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
296 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
297 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
298 } else {
299 llvm_unreachable("Unhandled value type for BUILD_PAIR");
300 }
301 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
302 N->getOperand(1), SubReg1 };
303 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000304 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard17ea10c2013-04-05 23:31:51 +0000305 }
306
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000307 case ISD::ConstantFP:
308 case ISD::Constant: {
309 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
310 // XXX: Custom immediate lowering not implemented yet. Instead we use
311 // pseudo instructions defined in SIInstructions.td
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000312 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000313 break;
314 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000315
316 uint64_t ImmValue = 0;
317 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
318
319 if (N->getOpcode() == ISD::ConstantFP) {
320 // XXX: 64-bit Immediates not supported yet
321 assert(N->getValueType(0) != MVT::f64);
322
323 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
324 APFloat Value = C->getValueAPF();
325 float FloatValue = Value.convertToFloat();
326 if (FloatValue == 0.0) {
327 ImmReg = AMDGPU::ZERO;
328 } else if (FloatValue == 0.5) {
329 ImmReg = AMDGPU::HALF;
330 } else if (FloatValue == 1.0) {
331 ImmReg = AMDGPU::ONE;
332 } else {
333 ImmValue = Value.bitcastToAPInt().getZExtValue();
334 }
335 } else {
336 // XXX: 64-bit Immediates not supported yet
337 assert(N->getValueType(0) != MVT::i64);
338
339 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
340 if (C->getZExtValue() == 0) {
341 ImmReg = AMDGPU::ZERO;
342 } else if (C->getZExtValue() == 1) {
343 ImmReg = AMDGPU::ONE_INT;
344 } else {
345 ImmValue = C->getZExtValue();
346 }
347 }
348
349 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
350 Use != SDNode::use_end(); Use = Next) {
351 Next = llvm::next(Use);
352 std::vector<SDValue> Ops;
353 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
354 Ops.push_back(Use->getOperand(i));
355 }
356
357 if (!Use->isMachineOpcode()) {
358 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
359 // We can only use literal constants (e.g. AMDGPU::ZERO,
360 // AMDGPU::ONE, etc) in machine opcodes.
361 continue;
362 }
363 } else {
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000364 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
365 (TII->get(Use->getMachineOpcode()).TSFlags &
366 R600_InstFlag::VECTOR)) {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000367 continue;
368 }
369
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000370 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(),
371 AMDGPU::OpName::literal);
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000372 if (ImmIdx == -1) {
373 continue;
374 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000375
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000376 if (TII->getOperandIdx(Use->getMachineOpcode(),
377 AMDGPU::OpName::dst) != -1) {
378 // subtract one from ImmIdx, because the DST operand is usually index
379 // 0 for MachineInstrs, but we have no DST in the Ops vector.
380 ImmIdx--;
381 }
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000382
383 // Check that we aren't already using an immediate.
384 // XXX: It's possible for an instruction to have more than one
385 // immediate operand, but this is not supported yet.
386 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
387 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
388 assert(C);
389
390 if (C->getZExtValue() != 0) {
391 // This instruction is already using an immediate.
392 continue;
393 }
394
395 // Set the immediate value
396 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
397 }
398 }
399 // Set the immediate register
400 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
401
402 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
403 }
404 break;
405 }
406 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000407 SDNode *Result = SelectCode(N);
408
409 // Fold operands of selected node
410
411 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000412 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard9f7818d2013-01-23 02:09:06 +0000413 const R600InstrInfo *TII =
414 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000415 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
416 bool IsModified = false;
417 do {
418 std::vector<SDValue> Ops;
419 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
420 I != E; ++I)
421 Ops.push_back(*I);
422 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
423 if (IsModified) {
424 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
425 }
426 } while (IsModified);
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000427
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000428 }
Vincent Lejeunedf65b0f2013-02-14 16:55:01 +0000429 if (Result && Result->isMachineOpcode() &&
430 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellarde3d4cbc2013-06-28 15:47:08 +0000431 && TII->hasInstrModifiers(Result->getMachineOpcode())) {
Tom Stellard58d33352013-07-23 01:48:24 +0000432 // Fold FNEG/FABS
Tom Stellard4bdf9892013-01-31 22:11:54 +0000433 // TODO: Isel can generate multiple MachineInst, we need to recursively
434 // parse Result
Tom Stellard9f7818d2013-01-23 02:09:06 +0000435 bool IsModified = false;
436 do {
437 std::vector<SDValue> Ops;
438 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
439 I != E; ++I)
440 Ops.push_back(*I);
441 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
442 if (IsModified) {
Tom Stellard4bdf9892013-01-31 22:11:54 +0000443 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard9f7818d2013-01-23 02:09:06 +0000444 }
445 } while (IsModified);
Tom Stellard4bdf9892013-01-31 22:11:54 +0000446
447 // If node has a single use which is CLAMP_R600, folds it
448 if (Result->hasOneUse() && Result->isMachineOpcode()) {
449 SDNode *PotentialClamp = *Result->use_begin();
450 if (PotentialClamp->isMachineOpcode() &&
451 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
452 unsigned ClampIdx =
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000453 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
Tom Stellard4bdf9892013-01-31 22:11:54 +0000454 std::vector<SDValue> Ops;
455 unsigned NumOp = Result->getNumOperands();
456 for (unsigned i = 0; i < NumOp; ++i) {
457 Ops.push_back(Result->getOperand(i));
458 }
459 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
460 Result = CurDAG->SelectNodeTo(PotentialClamp,
461 Result->getMachineOpcode(), PotentialClamp->getVTList(),
462 Ops.data(), NumOp);
463 }
464 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000465 }
466 }
467
468 return Result;
469}
470
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000471bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
Tom Stellard58d33352013-07-23 01:48:24 +0000472 SDValue &Abs, const R600InstrInfo *TII) {
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000473 switch (Src.getOpcode()) {
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000474 case ISD::FNEG:
475 Src = Src.getOperand(0);
476 Neg = CurDAG->getTargetConstant(1, MVT::i32);
477 return true;
478 case ISD::FABS:
479 if (!Abs.getNode())
480 return false;
481 Src = Src.getOperand(0);
482 Abs = CurDAG->getTargetConstant(1, MVT::i32);
483 return true;
484 case ISD::BITCAST:
485 Src = Src.getOperand(0);
486 return true;
487 default:
488 return false;
489 }
490}
491
Tom Stellard9f7818d2013-01-23 02:09:06 +0000492bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
493 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
494 int OperandIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000495 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
496 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
497 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
Tom Stellard9f7818d2013-01-23 02:09:06 +0000498 };
499 int SelIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000500 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
501 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
502 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
Tom Stellard9f7818d2013-01-23 02:09:06 +0000503 };
Tom Stellard4bdf9892013-01-31 22:11:54 +0000504 int NegIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000505 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
506 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
507 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
Tom Stellard4bdf9892013-01-31 22:11:54 +0000508 };
509 int AbsIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000510 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
511 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
Tom Stellard4bdf9892013-01-31 22:11:54 +0000512 -1
513 };
514
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000515
Tom Stellard9f7818d2013-01-23 02:09:06 +0000516 for (unsigned i = 0; i < 3; i++) {
517 if (OperandIdx[i] < 0)
518 return false;
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000519 SDValue &Src = Ops[OperandIdx[i] - 1];
520 SDValue &Sel = Ops[SelIdx[i] - 1];
521 SDValue &Neg = Ops[NegIdx[i] - 1];
522 SDValue FakeAbs;
523 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
Tom Stellard58d33352013-07-23 01:48:24 +0000524 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000525 return true;
526 }
527 return false;
528}
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000529
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000530bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
531 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
532 int OperandIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000533 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
534 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
535 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
536 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
537 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
538 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
539 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
540 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000541 };
542 int SelIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000543 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
544 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
545 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
546 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
547 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
548 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
549 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
550 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000551 };
552 int NegIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000553 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
554 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
555 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
556 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
557 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
558 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
559 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
560 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000561 };
562 int AbsIdx[] = {
Tom Stellard5e48a0e2013-06-25 21:22:18 +0000563 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
564 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
565 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
566 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
567 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
568 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
569 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
570 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000571 };
Vincent Lejeune3ab0ba32013-03-14 15:50:45 +0000572
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000573 for (unsigned i = 0; i < 8; i++) {
574 if (OperandIdx[i] < 0)
575 return false;
576 SDValue &Src = Ops[OperandIdx[i] - 1];
577 SDValue &Sel = Ops[SelIdx[i] - 1];
578 SDValue &Neg = Ops[NegIdx[i] - 1];
579 SDValue &Abs = Ops[AbsIdx[i] - 1];
Tom Stellard58d33352013-07-23 01:48:24 +0000580 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunee67a4af2013-06-04 23:17:15 +0000581 return true;
582 }
Tom Stellard9f7818d2013-01-23 02:09:06 +0000583 return false;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000584}
585
586bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
587 if (!ptr) {
588 return false;
589 }
590 Type *ptrType = ptr->getType();
591 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
592}
593
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000594bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
595 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
596}
597
598bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
599 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
600 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
601 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
602}
603
604bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
605 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
606}
607
608bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
609 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
610}
611
Tom Stellarda7eea052013-07-23 01:48:18 +0000612bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
613 if (CbId == -1) {
614 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000615 }
Tom Stellarda7eea052013-07-23 01:48:18 +0000616 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000617}
618
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000619bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000620 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
621}
622
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000623bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000624 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
625}
626
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000627bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000628 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
629}
630
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000631bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000632 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
633}
634
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000635bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000636 MachineMemOperand *MMO = N->getMemOperand();
637 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
638 if (MMO) {
639 const Value *V = MMO->getValue();
640 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
641 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
642 return true;
643 }
644 }
645 }
646 return false;
647}
648
Matt Arsenault70a3dc12013-06-18 23:37:58 +0000649bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000650 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
651 // Check to make sure we are not a constant pool load or a constant load
652 // that is marked as a private load
653 if (isCPLoad(N) || isConstantLoad(N, -1)) {
654 return false;
655 }
656 }
657 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
658 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
659 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
660 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
661 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
662 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
663 return true;
664 }
665 return false;
666}
667
668const char *AMDGPUDAGToDAGISel::getPassName() const {
669 return "AMDGPU DAG->DAG Pattern Instruction Selection";
670}
671
672#ifdef DEBUGTMP
673#undef INT64_C
674#endif
675#undef DEBUGTMP
676
677///==== AMDGPU Functions ====///
678
Tom Stellard9f7818d2013-01-23 02:09:06 +0000679bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
680 SDValue& IntPtr) {
681 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
682 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
683 return true;
684 }
685 return false;
686}
687
688bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
689 SDValue& BaseReg, SDValue &Offset) {
690 if (!dyn_cast<ConstantSDNode>(Addr)) {
691 BaseReg = Addr;
692 Offset = CurDAG->getIntPtrConstant(0, true);
693 return true;
694 }
695 return false;
696}
697
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000698bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
699 SDValue &Offset) {
700 ConstantSDNode * IMMOffset;
701
702 if (Addr.getOpcode() == ISD::ADD
703 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
704 && isInt<16>(IMMOffset->getZExtValue())) {
705
706 Base = Addr.getOperand(0);
707 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
708 return true;
709 // If the pointer address is constant, we can move it to the offset field.
710 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
711 && isInt<16>(IMMOffset->getZExtValue())) {
712 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +0000713 SDLoc(CurDAG->getEntryNode()),
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000714 AMDGPU::ZERO, MVT::i32);
715 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
716 return true;
717 }
718
719 // Default case, no offset
720 Base = Addr;
721 Offset = CurDAG->getTargetConstant(0, MVT::i32);
722 return true;
723}
724
Tom Stellardc0b0c672013-02-06 17:32:29 +0000725bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
726 SDValue &Offset) {
727 ConstantSDNode *C;
728
729 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
730 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
731 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
732 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
733 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
734 Base = Addr.getOperand(0);
735 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
736 } else {
737 Base = Addr;
738 Offset = CurDAG->getTargetConstant(0, MVT::i32);
739 }
740
741 return true;
742}
Christian Konigc018eca2013-02-26 17:52:16 +0000743
744void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
745
Tom Stellard3ff0abf2013-06-07 20:37:48 +0000746 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Tom Stellard8a72c732013-06-03 17:39:46 +0000747 return;
748 }
749
Christian Konigc018eca2013-02-26 17:52:16 +0000750 // Go over all selected nodes and try to fold them a bit more
Bill Wendlingba54bca2013-06-19 21:36:55 +0000751 const AMDGPUTargetLowering& Lowering =
752 (*(const AMDGPUTargetLowering*)getTargetLowering());
Christian Konigc018eca2013-02-26 17:52:16 +0000753 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
754 E = CurDAG->allnodes_end(); I != E; ++I) {
755
Tom Stellard8a72c732013-06-03 17:39:46 +0000756 SDNode *Node = I;
757 switch (Node->getOpcode()) {
758 // Fix the register class in copy to CopyToReg nodes - ISel will always
759 // use SReg classes for 64-bit copies, but this is not always what we want.
760 case ISD::CopyToReg: {
761 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
762 SDValue Val = Node->getOperand(2);
763 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
764 if (RC != &AMDGPU::SReg_64RegClass) {
765 continue;
766 }
767
Tom Stellard2948e692013-06-13 20:14:00 +0000768 if (!Val.getNode()->isMachineOpcode() ||
769 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
Tom Stellard8a72c732013-06-03 17:39:46 +0000770 continue;
771 }
772
773 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
774 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
775 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
776 continue;
777 }
778 }
779
780 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
781 if (!MachineNode)
Christian Konigc018eca2013-02-26 17:52:16 +0000782 continue;
783
Tom Stellard8a72c732013-06-03 17:39:46 +0000784 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
785 if (ResNode != Node) {
Christian Konigc018eca2013-02-26 17:52:16 +0000786 ReplaceUses(Node, ResNode);
Tom Stellard8a72c732013-06-03 17:39:46 +0000787 }
Christian Konigc018eca2013-02-26 17:52:16 +0000788 }
789}