1 //===-- AMDILISelLowering.cpp - AMDIL DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //==-----------------------------------------------------------------------===//
11 /// \brief TargetLowering functions borrowed from AMDIL.
13 //===----------------------------------------------------------------------===//
15 #include "AMDGPUISelLowering.h"
16 #include "AMDGPURegisterInfo.h"
17 #include "AMDGPUSubtarget.h"
18 #include "AMDILIntrinsicInfo.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetInstrInfo.h"
31 #include "llvm/Target/TargetOptions.h"
34 //===----------------------------------------------------------------------===//
35 // TargetLowering Implementation Help Functions End
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
39 // TargetLowering Class Implementation Begins
40 //===----------------------------------------------------------------------===//
41 void AMDGPUTargetLowering::InitAMDILLowering() {
42 static const MVT::SimpleValueType types[] = {
53 static const MVT::SimpleValueType FloatTypes[] = {
58 static const MVT::SimpleValueType VectorTypes[] = {
65 const AMDGPUSubtarget &STM = getTargetMachine().getSubtarget<AMDGPUSubtarget>();
67 for (MVT VT : types) {
68 setOperationAction(ISD::SUBE, VT, Expand);
69 setOperationAction(ISD::SUBC, VT, Expand);
70 setOperationAction(ISD::ADDE, VT, Expand);
71 setOperationAction(ISD::ADDC, VT, Expand);
72 setOperationAction(ISD::BRCOND, VT, Custom);
73 setOperationAction(ISD::BR_JT, VT, Expand);
74 setOperationAction(ISD::BRIND, VT, Expand);
75 // TODO: Implement custom UREM/SREM routines
76 setOperationAction(ISD::SREM, VT, Expand);
77 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
78 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
80 setOperationAction(ISD::SDIV, VT, Custom);
83 for (MVT VT : FloatTypes) {
84 setOperationAction(ISD::FP_ROUND_INREG, VT, Expand);
87 for (MVT VT : VectorTypes) {
88 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
89 setOperationAction(ISD::SELECT_CC, VT, Expand);
92 setOperationAction(ISD::MULHU, MVT::i64, Expand);
93 setOperationAction(ISD::MULHS, MVT::i64, Expand);
94 if (STM.hasHWFP64()) {
95 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
96 setOperationAction(ISD::FABS, MVT::f64, Expand);
99 setOperationAction(ISD::SUBC, MVT::Other, Expand);
100 setOperationAction(ISD::ADDE, MVT::Other, Expand);
101 setOperationAction(ISD::ADDC, MVT::Other, Expand);
102 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
103 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
104 setOperationAction(ISD::BRIND, MVT::Other, Expand);
106 setOperationAction(ISD::Constant, MVT::i32, Legal);
107 setOperationAction(ISD::Constant, MVT::i64, Legal);
108 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
110 setPow2DivIsCheap(false);
111 setSelectIsExpensive(true); // FIXME: This makes no sense at all
115 AMDGPUTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
116 const CallInst &I, unsigned Intrinsic) const {
120 // The backend supports 32 and 64 bit floating point immediates
122 AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
123 if (VT.getScalarType().getSimpleVT().SimpleTy == MVT::f32
124 || VT.getScalarType().getSimpleVT().SimpleTy == MVT::f64) {
132 AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
133 if (VT.getScalarType().getSimpleVT().SimpleTy == MVT::f32
134 || VT.getScalarType().getSimpleVT().SimpleTy == MVT::f64) {
142 // isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
143 // be zero. Op is expected to be a target specific node. Used by DAG
146 //===----------------------------------------------------------------------===//
147 // Other Lowering Hooks
148 //===----------------------------------------------------------------------===//
151 AMDGPUTargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
152 EVT OVT = Op.getValueType();
154 if (OVT.getScalarType() == MVT::i64) {
155 DST = LowerSDIV64(Op, DAG);
156 } else if (OVT.getScalarType() == MVT::i32) {
157 DST = LowerSDIV32(Op, DAG);
158 } else if (OVT.getScalarType() == MVT::i16
159 || OVT.getScalarType() == MVT::i8) {
160 DST = LowerSDIV24(Op, DAG);
162 DST = SDValue(Op.getNode(), 0);
168 AMDGPUTargetLowering::LowerSREM(SDValue Op, SelectionDAG &DAG) const {
169 EVT OVT = Op.getValueType();
171 if (OVT.getScalarType() == MVT::i64)
172 return LowerSREM64(Op, DAG);
174 if (OVT.getScalarType() == MVT::i32)
175 return LowerSREM32(Op, DAG);
177 return SDValue(Op.getNode(), 0);
181 AMDGPUTargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
182 SDValue Chain = Op.getOperand(0);
183 SDValue Cond = Op.getOperand(1);
184 SDValue Jump = Op.getOperand(2);
186 Result = DAG.getNode(
187 AMDGPUISD::BRANCH_COND,
195 AMDGPUTargetLowering::LowerSDIV24(SDValue Op, SelectionDAG &DAG) const {
197 EVT OVT = Op.getValueType();
198 SDValue LHS = Op.getOperand(0);
199 SDValue RHS = Op.getOperand(1);
202 if (!OVT.isVector()) {
205 } else if (OVT.getVectorNumElements() == 2) {
208 } else if (OVT.getVectorNumElements() == 4) {
212 unsigned bitsize = OVT.getScalarType().getSizeInBits();
213 // char|short jq = ia ^ ib;
214 SDValue jq = DAG.getNode(ISD::XOR, DL, OVT, LHS, RHS);
216 // jq = jq >> (bitsize - 2)
217 jq = DAG.getNode(ISD::SRA, DL, OVT, jq, DAG.getConstant(bitsize - 2, OVT));
220 jq = DAG.getNode(ISD::OR, DL, OVT, jq, DAG.getConstant(1, OVT));
223 jq = DAG.getSExtOrTrunc(jq, DL, INTTY);
225 // int ia = (int)LHS;
226 SDValue ia = DAG.getSExtOrTrunc(LHS, DL, INTTY);
229 SDValue ib = DAG.getSExtOrTrunc(RHS, DL, INTTY);
231 // float fa = (float)ia;
232 SDValue fa = DAG.getNode(ISD::SINT_TO_FP, DL, FLTTY, ia);
234 // float fb = (float)ib;
235 SDValue fb = DAG.getNode(ISD::SINT_TO_FP, DL, FLTTY, ib);
237 // float fq = native_divide(fa, fb);
238 SDValue fq = DAG.getNode(AMDGPUISD::DIV_INF, DL, FLTTY, fa, fb);
241 fq = DAG.getNode(ISD::FTRUNC, DL, FLTTY, fq);
243 // float fqneg = -fq;
244 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FLTTY, fq);
246 // float fr = mad(fqneg, fb, fa);
247 SDValue fr = DAG.getNode(ISD::FADD, DL, FLTTY,
248 DAG.getNode(ISD::MUL, DL, FLTTY, fqneg, fb), fa);
251 SDValue iq = DAG.getNode(ISD::FP_TO_SINT, DL, INTTY, fq);
254 fr = DAG.getNode(ISD::FABS, DL, FLTTY, fr);
257 fb = DAG.getNode(ISD::FABS, DL, FLTTY, fb);
259 // int cv = fr >= fb;
261 if (INTTY == MVT::i32) {
262 cv = DAG.getSetCC(DL, INTTY, fr, fb, ISD::SETOGE);
264 cv = DAG.getSetCC(DL, INTTY, fr, fb, ISD::SETOGE);
266 // jq = (cv ? jq : 0);
267 jq = DAG.getNode(ISD::SELECT, DL, OVT, cv, jq,
268 DAG.getConstant(0, OVT));
270 iq = DAG.getSExtOrTrunc(iq, DL, OVT);
271 iq = DAG.getNode(ISD::ADD, DL, OVT, iq, jq);
276 AMDGPUTargetLowering::LowerSDIV32(SDValue Op, SelectionDAG &DAG) const {
278 EVT OVT = Op.getValueType();
279 SDValue LHS = Op.getOperand(0);
280 SDValue RHS = Op.getOperand(1);
281 // The LowerSDIV32 function generates equivalent to the following IL.
291 // ixor r10, r10, r11
302 SDValue r10 = DAG.getSelectCC(DL,
303 r0, DAG.getConstant(0, OVT),
304 DAG.getConstant(-1, MVT::i32),
305 DAG.getConstant(0, MVT::i32),
309 SDValue r11 = DAG.getSelectCC(DL,
310 r1, DAG.getConstant(0, OVT),
311 DAG.getConstant(-1, MVT::i32),
312 DAG.getConstant(0, MVT::i32),
316 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
319 r1 = DAG.getNode(ISD::ADD, DL, OVT, r1, r11);
322 r0 = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
325 r1 = DAG.getNode(ISD::XOR, DL, OVT, r1, r11);
328 r0 = DAG.getNode(ISD::UDIV, DL, OVT, r0, r1);
330 // ixor r10, r10, r11
331 r10 = DAG.getNode(ISD::XOR, DL, OVT, r10, r11);
334 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
337 SDValue DST = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
342 AMDGPUTargetLowering::LowerSDIV64(SDValue Op, SelectionDAG &DAG) const {
343 return SDValue(Op.getNode(), 0);
347 AMDGPUTargetLowering::LowerSREM32(SDValue Op, SelectionDAG &DAG) const {
349 EVT OVT = Op.getValueType();
350 SDValue LHS = Op.getOperand(0);
351 SDValue RHS = Op.getOperand(1);
352 // The LowerSREM32 function generates equivalent to the following IL.
374 SDValue r10 = DAG.getSetCC(DL, OVT, r0, DAG.getConstant(0, OVT), ISD::SETLT);
377 SDValue r11 = DAG.getSetCC(DL, OVT, r1, DAG.getConstant(0, OVT), ISD::SETLT);
380 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
383 r1 = DAG.getNode(ISD::ADD, DL, OVT, r1, r11);
386 r0 = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
389 r1 = DAG.getNode(ISD::XOR, DL, OVT, r1, r11);
392 SDValue r20 = DAG.getNode(ISD::UREM, DL, OVT, r0, r1);
395 r20 = DAG.getNode(AMDGPUISD::UMUL, DL, OVT, r20, r1);
398 r0 = DAG.getNode(ISD::SUB, DL, OVT, r0, r20);
401 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
404 SDValue DST = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
409 AMDGPUTargetLowering::LowerSREM64(SDValue Op, SelectionDAG &DAG) const {
410 return SDValue(Op.getNode(), 0);