blob: 696910911f9e07cb1eb0465d0675f1e8479b4432 [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 Stellard365366f2013-01-23 02:09:06 +000080 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
81 bool SelectGlobalValueVariableOffset(SDValue Addr,
82 SDValue &BaseReg, SDValue& Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000083 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellardf3b2a1e2013-02-06 17:32:29 +000084 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
Tom Stellard75aadc22012-12-11 21:25:42 +000085
86 // Include the pieces autogenerated from the target description.
87#include "AMDGPUGenDAGISel.inc"
88};
89} // end anonymous namespace
90
91/// \brief This pass converts a legalized DAG into a AMDGPU-specific
92// DAG, ready for instruction scheduling.
93FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
94 ) {
95 return new AMDGPUDAGToDAGISel(TM);
96}
97
Bill Wendlinga3cd3502013-06-19 21:36:55 +000098AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
Tom Stellard75aadc22012-12-11 21:25:42 +000099 : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
100}
101
102AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
103}
104
105SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
106 return CurDAG->getTargetConstant(Imm, MVT::i32);
107}
108
109bool AMDGPUDAGToDAGISel::SelectADDRParam(
110 SDValue Addr, SDValue& R1, SDValue& R2) {
111
112 if (Addr.getOpcode() == ISD::FrameIndex) {
113 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
114 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
115 R2 = CurDAG->getTargetConstant(0, MVT::i32);
116 } else {
117 R1 = Addr;
118 R2 = CurDAG->getTargetConstant(0, MVT::i32);
119 }
120 } else if (Addr.getOpcode() == ISD::ADD) {
121 R1 = Addr.getOperand(0);
122 R2 = Addr.getOperand(1);
123 } else {
124 R1 = Addr;
125 R2 = CurDAG->getTargetConstant(0, MVT::i32);
126 }
127 return true;
128}
129
130bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
131 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
132 Addr.getOpcode() == ISD::TargetGlobalAddress) {
133 return false;
134 }
135 return SelectADDRParam(Addr, R1, R2);
136}
137
138
139bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
140 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
141 Addr.getOpcode() == ISD::TargetGlobalAddress) {
142 return false;
143 }
144
145 if (Addr.getOpcode() == ISD::FrameIndex) {
146 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
147 R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
148 R2 = CurDAG->getTargetConstant(0, MVT::i64);
149 } else {
150 R1 = Addr;
151 R2 = CurDAG->getTargetConstant(0, MVT::i64);
152 }
153 } else if (Addr.getOpcode() == ISD::ADD) {
154 R1 = Addr.getOperand(0);
155 R2 = Addr.getOperand(1);
156 } else {
157 R1 = Addr;
158 R2 = CurDAG->getTargetConstant(0, MVT::i64);
159 }
160 return true;
161}
162
163SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
Tom Stellard84021442013-07-23 01:48:24 +0000164 const R600InstrInfo *TII =
165 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +0000166 unsigned int Opc = N->getOpcode();
167 if (N->isMachineOpcode()) {
168 return NULL; // Already selected.
169 }
170 switch (Opc) {
171 default: break;
Tom Stellard84021442013-07-23 01:48:24 +0000172 case AMDGPUISD::CONST_ADDRESS: {
173 for (SDNode::use_iterator I = N->use_begin(), Next = llvm::next(I);
174 I != SDNode::use_end(); I = Next) {
175 Next = llvm::next(I);
176 if (!I->isMachineOpcode()) {
177 continue;
178 }
179 unsigned Opcode = I->getMachineOpcode();
180 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
181 int SrcIdx = I.getOperandNo();
182 int SelIdx;
183 // Unlike MachineInstrs, SDNodes do not have results in their operand
184 // list, so we need to increment the SrcIdx, since
185 // R600InstrInfo::getOperandIdx is based on the MachineInstr indices.
186 if (HasDst) {
187 SrcIdx++;
188 }
189
190 SelIdx = TII->getSelIdx(I->getMachineOpcode(), SrcIdx);
191 if (SelIdx < 0) {
192 continue;
193 }
194
195 SDValue CstOffset;
196 if (N->getValueType(0).isVector() ||
197 !SelectGlobalValueConstantOffset(N->getOperand(0), CstOffset))
198 continue;
199
200 // Gather constants values
201 int SrcIndices[] = {
202 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
203 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
204 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
205 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
206 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
207 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
208 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
209 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
210 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
211 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
212 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
213 };
214 std::vector<unsigned> Consts;
215 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
216 int OtherSrcIdx = SrcIndices[i];
217 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
218 if (OtherSrcIdx < 0 || OtherSelIdx < 0) {
219 continue;
220 }
221 if (HasDst) {
222 OtherSrcIdx--;
223 OtherSelIdx--;
224 }
225 if (RegisterSDNode *Reg =
226 dyn_cast<RegisterSDNode>(I->getOperand(OtherSrcIdx))) {
227 if (Reg->getReg() == AMDGPU::ALU_CONST) {
228 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(I->getOperand(OtherSelIdx));
229 Consts.push_back(Cst->getZExtValue());
230 }
231 }
232 }
233
234 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
235 Consts.push_back(Cst->getZExtValue());
236 if (!TII->fitsConstReadLimitations(Consts))
237 continue;
238
239 // Convert back to SDNode indices
240 if (HasDst) {
241 SrcIdx--;
242 SelIdx--;
243 }
244 std::vector<SDValue> Ops;
245 for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
246 if (i == SrcIdx) {
247 Ops.push_back(CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32));
248 } else if (i == SelIdx) {
249 Ops.push_back(CstOffset);
250 } else {
251 Ops.push_back(I->getOperand(i));
252 }
253 }
254 CurDAG->UpdateNodeOperands(*I, Ops.data(), Ops.size());
255 }
256 break;
257 }
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000258 case ISD::BUILD_VECTOR: {
259 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000260 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Vincent Lejeune3b6f20e2013-03-05 15:04:49 +0000261 break;
262 }
263 // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
264 // that adds a 128 bits reg copy when going through TwoAddressInstructions
265 // pass. We want to avoid 128 bits copies as much as possible because they
266 // can't be bundled by our scheduler.
267 SDValue RegSeqArgs[9] = {
268 CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
269 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
270 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
271 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
272 SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
273 };
274 bool IsRegSeq = true;
275 for (unsigned i = 0; i < N->getNumOperands(); i++) {
276 if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
277 IsRegSeq = false;
278 break;
279 }
280 RegSeqArgs[2 * i + 1] = N->getOperand(i);
281 }
282 if (!IsRegSeq)
283 break;
284 return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
285 RegSeqArgs, 2 * N->getNumOperands() + 1);
286 }
Tom Stellard754f80f2013-04-05 23:31:51 +0000287 case ISD::BUILD_PAIR: {
288 SDValue RC, SubReg0, SubReg1;
289 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000290 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard754f80f2013-04-05 23:31:51 +0000291 break;
292 }
293 if (N->getValueType(0) == MVT::i128) {
294 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
295 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
296 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
297 } else if (N->getValueType(0) == MVT::i64) {
298 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
299 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
300 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
301 } else {
302 llvm_unreachable("Unhandled value type for BUILD_PAIR");
303 }
304 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
305 N->getOperand(1), SubReg1 };
306 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000307 SDLoc(N), N->getValueType(0), Ops);
Tom Stellard754f80f2013-04-05 23:31:51 +0000308 }
309
Tom Stellard75aadc22012-12-11 21:25:42 +0000310 case ISD::ConstantFP:
311 case ISD::Constant: {
312 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
313 // XXX: Custom immediate lowering not implemented yet. Instead we use
314 // pseudo instructions defined in SIInstructions.td
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000315 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000316 break;
317 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000318
319 uint64_t ImmValue = 0;
320 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
321
322 if (N->getOpcode() == ISD::ConstantFP) {
323 // XXX: 64-bit Immediates not supported yet
324 assert(N->getValueType(0) != MVT::f64);
325
326 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
327 APFloat Value = C->getValueAPF();
328 float FloatValue = Value.convertToFloat();
329 if (FloatValue == 0.0) {
330 ImmReg = AMDGPU::ZERO;
331 } else if (FloatValue == 0.5) {
332 ImmReg = AMDGPU::HALF;
333 } else if (FloatValue == 1.0) {
334 ImmReg = AMDGPU::ONE;
335 } else {
336 ImmValue = Value.bitcastToAPInt().getZExtValue();
337 }
338 } else {
339 // XXX: 64-bit Immediates not supported yet
340 assert(N->getValueType(0) != MVT::i64);
341
342 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
343 if (C->getZExtValue() == 0) {
344 ImmReg = AMDGPU::ZERO;
345 } else if (C->getZExtValue() == 1) {
346 ImmReg = AMDGPU::ONE_INT;
347 } else {
348 ImmValue = C->getZExtValue();
349 }
350 }
351
352 for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
353 Use != SDNode::use_end(); Use = Next) {
354 Next = llvm::next(Use);
355 std::vector<SDValue> Ops;
356 for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
357 Ops.push_back(Use->getOperand(i));
358 }
359
360 if (!Use->isMachineOpcode()) {
361 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
362 // We can only use literal constants (e.g. AMDGPU::ZERO,
363 // AMDGPU::ONE, etc) in machine opcodes.
364 continue;
365 }
366 } else {
Vincent Lejeunef694c102013-02-14 16:55:01 +0000367 if (!TII->isALUInstr(Use->getMachineOpcode()) ||
368 (TII->get(Use->getMachineOpcode()).TSFlags &
369 R600_InstFlag::VECTOR)) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000370 continue;
371 }
372
Tom Stellard02661d92013-06-25 21:22:18 +0000373 int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(),
374 AMDGPU::OpName::literal);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000375 if (ImmIdx == -1) {
376 continue;
377 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000378
Tom Stellardc026e8b2013-06-28 15:47:08 +0000379 if (TII->getOperandIdx(Use->getMachineOpcode(),
380 AMDGPU::OpName::dst) != -1) {
381 // subtract one from ImmIdx, because the DST operand is usually index
382 // 0 for MachineInstrs, but we have no DST in the Ops vector.
383 ImmIdx--;
384 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000385
386 // Check that we aren't already using an immediate.
387 // XXX: It's possible for an instruction to have more than one
388 // immediate operand, but this is not supported yet.
389 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
390 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
391 assert(C);
392
393 if (C->getZExtValue() != 0) {
394 // This instruction is already using an immediate.
395 continue;
396 }
397
398 // Set the immediate value
399 Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
400 }
401 }
402 // Set the immediate register
403 Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
404
405 CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
406 }
407 break;
408 }
409 }
Tom Stellard365366f2013-01-23 02:09:06 +0000410 SDNode *Result = SelectCode(N);
411
412 // Fold operands of selected node
413
414 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000415 if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
Tom Stellard365366f2013-01-23 02:09:06 +0000416 const R600InstrInfo *TII =
417 static_cast<const R600InstrInfo*>(TM.getInstrInfo());
Vincent Lejeunec6896792013-06-04 23:17:15 +0000418 if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
419 bool IsModified = false;
420 do {
421 std::vector<SDValue> Ops;
422 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
423 I != E; ++I)
424 Ops.push_back(*I);
425 IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
426 if (IsModified) {
427 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
428 }
429 } while (IsModified);
Matt Arsenault2aabb062013-06-18 23:37:58 +0000430
Vincent Lejeunec6896792013-06-04 23:17:15 +0000431 }
Vincent Lejeunef694c102013-02-14 16:55:01 +0000432 if (Result && Result->isMachineOpcode() &&
433 !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
Tom Stellardc026e8b2013-06-28 15:47:08 +0000434 && TII->hasInstrModifiers(Result->getMachineOpcode())) {
Tom Stellard84021442013-07-23 01:48:24 +0000435 // Fold FNEG/FABS
Tom Stellard49269212013-01-31 22:11:54 +0000436 // TODO: Isel can generate multiple MachineInst, we need to recursively
437 // parse Result
Tom Stellard365366f2013-01-23 02:09:06 +0000438 bool IsModified = false;
439 do {
440 std::vector<SDValue> Ops;
441 for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
442 I != E; ++I)
443 Ops.push_back(*I);
444 IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
445 if (IsModified) {
Tom Stellard49269212013-01-31 22:11:54 +0000446 Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
Tom Stellard365366f2013-01-23 02:09:06 +0000447 }
448 } while (IsModified);
Tom Stellard49269212013-01-31 22:11:54 +0000449
450 // If node has a single use which is CLAMP_R600, folds it
451 if (Result->hasOneUse() && Result->isMachineOpcode()) {
452 SDNode *PotentialClamp = *Result->use_begin();
453 if (PotentialClamp->isMachineOpcode() &&
454 PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
455 unsigned ClampIdx =
Tom Stellard02661d92013-06-25 21:22:18 +0000456 TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
Tom Stellard49269212013-01-31 22:11:54 +0000457 std::vector<SDValue> Ops;
458 unsigned NumOp = Result->getNumOperands();
459 for (unsigned i = 0; i < NumOp; ++i) {
460 Ops.push_back(Result->getOperand(i));
461 }
462 Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
463 Result = CurDAG->SelectNodeTo(PotentialClamp,
464 Result->getMachineOpcode(), PotentialClamp->getVTList(),
465 Ops.data(), NumOp);
466 }
467 }
Tom Stellard365366f2013-01-23 02:09:06 +0000468 }
469 }
470
471 return Result;
472}
473
Vincent Lejeunec6896792013-06-04 23:17:15 +0000474bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
Tom Stellard84021442013-07-23 01:48:24 +0000475 SDValue &Abs, const R600InstrInfo *TII) {
Vincent Lejeunec6896792013-06-04 23:17:15 +0000476 switch (Src.getOpcode()) {
Vincent Lejeunec6896792013-06-04 23:17:15 +0000477 case ISD::FNEG:
478 Src = Src.getOperand(0);
479 Neg = CurDAG->getTargetConstant(1, MVT::i32);
480 return true;
481 case ISD::FABS:
482 if (!Abs.getNode())
483 return false;
484 Src = Src.getOperand(0);
485 Abs = CurDAG->getTargetConstant(1, MVT::i32);
486 return true;
487 case ISD::BITCAST:
488 Src = Src.getOperand(0);
489 return true;
490 default:
491 return false;
492 }
493}
494
Tom Stellard365366f2013-01-23 02:09:06 +0000495bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
496 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
497 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000498 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
499 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
500 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
Tom Stellard365366f2013-01-23 02:09:06 +0000501 };
502 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000503 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
504 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
505 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
Tom Stellard365366f2013-01-23 02:09:06 +0000506 };
Tom Stellard49269212013-01-31 22:11:54 +0000507 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000508 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
509 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
510 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
Tom Stellard49269212013-01-31 22:11:54 +0000511 };
512 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000513 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
514 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
Tom Stellard49269212013-01-31 22:11:54 +0000515 -1
516 };
517
Vincent Lejeunec6896792013-06-04 23:17:15 +0000518
Tom Stellard365366f2013-01-23 02:09:06 +0000519 for (unsigned i = 0; i < 3; i++) {
520 if (OperandIdx[i] < 0)
521 return false;
Vincent Lejeunec6896792013-06-04 23:17:15 +0000522 SDValue &Src = Ops[OperandIdx[i] - 1];
523 SDValue &Sel = Ops[SelIdx[i] - 1];
524 SDValue &Neg = Ops[NegIdx[i] - 1];
525 SDValue FakeAbs;
526 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
Tom Stellard84021442013-07-23 01:48:24 +0000527 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunec6896792013-06-04 23:17:15 +0000528 return true;
529 }
530 return false;
531}
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000532
Vincent Lejeunec6896792013-06-04 23:17:15 +0000533bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
534 const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
535 int OperandIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000536 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
537 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
538 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
539 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
540 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
541 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
542 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
543 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000544 };
545 int SelIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000546 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
547 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
548 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
549 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
550 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
551 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
552 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
553 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000554 };
555 int NegIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000556 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
557 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
558 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
559 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
560 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
561 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
562 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
563 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000564 };
565 int AbsIdx[] = {
Tom Stellard02661d92013-06-25 21:22:18 +0000566 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
567 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
568 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
569 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
570 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
571 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
572 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
573 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
Vincent Lejeunec6896792013-06-04 23:17:15 +0000574 };
Vincent Lejeune0a22bc42013-03-14 15:50:45 +0000575
Vincent Lejeunec6896792013-06-04 23:17:15 +0000576 for (unsigned i = 0; i < 8; i++) {
577 if (OperandIdx[i] < 0)
578 return false;
579 SDValue &Src = Ops[OperandIdx[i] - 1];
580 SDValue &Sel = Ops[SelIdx[i] - 1];
581 SDValue &Neg = Ops[NegIdx[i] - 1];
582 SDValue &Abs = Ops[AbsIdx[i] - 1];
Tom Stellard84021442013-07-23 01:48:24 +0000583 if (FoldOperand(Src, Sel, Neg, Abs, TII))
Vincent Lejeunec6896792013-06-04 23:17:15 +0000584 return true;
585 }
Tom Stellard365366f2013-01-23 02:09:06 +0000586 return false;
Tom Stellard75aadc22012-12-11 21:25:42 +0000587}
588
589bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
590 if (!ptr) {
591 return false;
592 }
593 Type *ptrType = ptr->getType();
594 return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
595}
596
Tom Stellard75aadc22012-12-11 21:25:42 +0000597bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
598 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
599}
600
601bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
602 return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
603 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
604 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
605}
606
607bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
608 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
609}
610
611bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
612 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
613}
614
Tom Stellard1e803092013-07-23 01:48:18 +0000615bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
616 if (CbId == -1) {
617 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000618 }
Tom Stellard1e803092013-07-23 01:48:18 +0000619 return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
Tom Stellard75aadc22012-12-11 21:25:42 +0000620}
621
Matt Arsenault2aabb062013-06-18 23:37:58 +0000622bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000623 return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
624}
625
Matt Arsenault2aabb062013-06-18 23:37:58 +0000626bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000627 return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
628}
629
Matt Arsenault2aabb062013-06-18 23:37:58 +0000630bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000631 return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
632}
633
Matt Arsenault2aabb062013-06-18 23:37:58 +0000634bool AMDGPUDAGToDAGISel::isRegionLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000635 return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
636}
637
Matt Arsenault2aabb062013-06-18 23:37:58 +0000638bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000639 MachineMemOperand *MMO = N->getMemOperand();
640 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
641 if (MMO) {
642 const Value *V = MMO->getValue();
643 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
644 if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
645 return true;
646 }
647 }
648 }
649 return false;
650}
651
Matt Arsenault2aabb062013-06-18 23:37:58 +0000652bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000653 if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
654 // Check to make sure we are not a constant pool load or a constant load
655 // that is marked as a private load
656 if (isCPLoad(N) || isConstantLoad(N, -1)) {
657 return false;
658 }
659 }
660 if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
661 && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
662 && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
663 && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
664 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
665 && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
666 return true;
667 }
668 return false;
669}
670
671const char *AMDGPUDAGToDAGISel::getPassName() const {
672 return "AMDGPU DAG->DAG Pattern Instruction Selection";
673}
674
675#ifdef DEBUGTMP
676#undef INT64_C
677#endif
678#undef DEBUGTMP
679
Tom Stellard41fc7852013-07-23 01:48:42 +0000680//===----------------------------------------------------------------------===//
681// Complex Patterns
682//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000683
Tom Stellard365366f2013-01-23 02:09:06 +0000684bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
685 SDValue& IntPtr) {
686 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
687 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
688 return true;
689 }
690 return false;
691}
692
693bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
694 SDValue& BaseReg, SDValue &Offset) {
695 if (!dyn_cast<ConstantSDNode>(Addr)) {
696 BaseReg = Addr;
697 Offset = CurDAG->getIntPtrConstant(0, true);
698 return true;
699 }
700 return false;
701}
702
Tom Stellard75aadc22012-12-11 21:25:42 +0000703bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
704 SDValue &Offset) {
705 ConstantSDNode * IMMOffset;
706
707 if (Addr.getOpcode() == ISD::ADD
708 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
709 && isInt<16>(IMMOffset->getZExtValue())) {
710
711 Base = Addr.getOperand(0);
712 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
713 return true;
714 // If the pointer address is constant, we can move it to the offset field.
715 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
716 && isInt<16>(IMMOffset->getZExtValue())) {
717 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +0000718 SDLoc(CurDAG->getEntryNode()),
Tom Stellard75aadc22012-12-11 21:25:42 +0000719 AMDGPU::ZERO, MVT::i32);
720 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
721 return true;
722 }
723
724 // Default case, no offset
725 Base = Addr;
726 Offset = CurDAG->getTargetConstant(0, MVT::i32);
727 return true;
728}
729
Tom Stellardf3b2a1e2013-02-06 17:32:29 +0000730bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
731 SDValue &Offset) {
732 ConstantSDNode *C;
733
734 if ((C = dyn_cast<ConstantSDNode>(Addr))) {
735 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
736 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
737 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
738 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
739 Base = Addr.getOperand(0);
740 Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
741 } else {
742 Base = Addr;
743 Offset = CurDAG->getTargetConstant(0, MVT::i32);
744 }
745
746 return true;
747}
Christian Konigd910b7d2013-02-26 17:52:16 +0000748
Tom Stellard41fc7852013-07-23 01:48:42 +0000749SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
750 APInt Demanded = APInt(32, 0x00FFFFFF);
751 APInt KnownZero, KnownOne;
752 TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
753 const TargetLowering *TLI = getTargetLowering();
754 if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
755 CurDAG->ReplaceAllUsesWith(Op, TLO.New);
756 CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
757 return SimplifyI24(TLO.New);
758 } else {
759 return Op;
760 }
761}
762
763bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
764
765 assert(Op.getValueType() == MVT::i32);
766
767 if (CurDAG->ComputeNumSignBits(Op) == 9) {
768 I24 = SimplifyI24(Op);
769 return true;
770 }
771 return false;
772}
773
774bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
775 APInt KnownZero;
776 APInt KnownOne;
777 CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
778
779 assert (Op.getValueType() == MVT::i32);
780
781 // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
782 // i32. These smaller types are legal to use with the i24 instructions.
783 if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
784 Op.getOpcode() == ISD::ANY_EXTEND ||
785 ISD::isEXTLoad(Op.getNode())) {
786 U24 = SimplifyI24(Op);
787 return true;
788 }
789 return false;
790}
791
Christian Konigd910b7d2013-02-26 17:52:16 +0000792void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
793
Tom Stellarda6c6e1b2013-06-07 20:37:48 +0000794 if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Tom Stellard2183b702013-06-03 17:39:46 +0000795 return;
796 }
797
Christian Konigd910b7d2013-02-26 17:52:16 +0000798 // Go over all selected nodes and try to fold them a bit more
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000799 const AMDGPUTargetLowering& Lowering =
800 (*(const AMDGPUTargetLowering*)getTargetLowering());
Christian Konigd910b7d2013-02-26 17:52:16 +0000801 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
802 E = CurDAG->allnodes_end(); I != E; ++I) {
803
Tom Stellard2183b702013-06-03 17:39:46 +0000804 SDNode *Node = I;
805 switch (Node->getOpcode()) {
806 // Fix the register class in copy to CopyToReg nodes - ISel will always
807 // use SReg classes for 64-bit copies, but this is not always what we want.
808 case ISD::CopyToReg: {
809 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
810 SDValue Val = Node->getOperand(2);
811 const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
812 if (RC != &AMDGPU::SReg_64RegClass) {
813 continue;
814 }
815
Tom Stellardadba0832013-06-13 20:14:00 +0000816 if (!Val.getNode()->isMachineOpcode() ||
817 Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
Tom Stellard2183b702013-06-03 17:39:46 +0000818 continue;
819 }
820
821 const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
822 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
823 RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
824 continue;
825 }
826 }
827
828 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
829 if (!MachineNode)
Christian Konigd910b7d2013-02-26 17:52:16 +0000830 continue;
831
Tom Stellard2183b702013-06-03 17:39:46 +0000832 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
833 if (ResNode != Node) {
Christian Konigd910b7d2013-02-26 17:52:16 +0000834 ReplaceUses(Node, ResNode);
Tom Stellard2183b702013-06-03 17:39:46 +0000835 }
Christian Konigd910b7d2013-02-26 17:52:16 +0000836 }
837}