summaryrefslogtreecommitdiff
path: root/lib/Target/TGSI/TGSIISelLowering.cpp
blob: 77187bff8e208dc43969a48888be33515fe01b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//===-- TGSIISelLowering.cpp - TGSI DAG Lowering Implementation ---------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the interfaces that TGSI uses to lower LLVM code into a
// selection DAG.
//
//===----------------------------------------------------------------------===//

#include "TGSIISelLowering.h"
#include "TGSIRegisterInfo.h"
#include "TGSITargetMachine.h"
#include "TGSITargetObjectFile.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/CodeGen/CallingConvLower.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/ManagedStatic.h"
using namespace llvm;


//===----------------------------------------------------------------------===//
// Calling Convention Implementation
//===----------------------------------------------------------------------===//

#include "TGSIGenCallingConv.inc"

SDValue
TGSITargetLowering::LowerReturn(SDValue Chain,
                                CallingConv::ID CallConv, bool isVarArg,
                                const SmallVectorImpl<ISD::OutputArg> &Outs,
                                const SmallVectorImpl<SDValue> &OutVals,
                                const SDLoc &dl, SelectionDAG &DAG) const {
   SDValue Flag;

   // CCValAssign - represent the assignment of the return value to locations.
   SmallVector<CCValAssign, 16> RVLocs;

   // CCState - Info about the registers and stack slot.
   CCState ccinfo(CallConv, isVarArg, DAG.getMachineFunction(),
                  RVLocs, *DAG.getContext());

   ccinfo.AnalyzeReturn(Outs, RetCC_TGSI);

   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
      CCValAssign& VA = RVLocs[i];

      assert(VA.isRegLoc() && "CCValAssign must be RegLoc");

      unsigned Reg = VA.getLocReg();

      Chain = DAG.getCopyToReg(Chain, dl, Reg, OutVals[i], Flag);

      // Guarantee that all emitted copies are stuck together.
      Flag = Chain.getValue(1);
   }

   if (Flag.getNode())
      return DAG.getNode(TGSIISD::RET, dl, MVT::Other, Chain, Flag);
   else
      return DAG.getNode(TGSIISD::RET, dl, MVT::Other, Chain);
}

SDValue
TGSITargetLowering::LowerFormalArguments(SDValue Chain,
                                         CallingConv::ID CallConv, bool isVarArg,
                                         const SmallVectorImpl<ISD::InputArg> &Ins,
                                         const SDLoc &dl, SelectionDAG &DAG,
                                         SmallVectorImpl<SDValue> &InVals) const {
   MachineFunction &mf = DAG.getMachineFunction();
   MachineRegisterInfo &reginfo = mf.getRegInfo();

   // Assign locations to all of the incoming arguments.
   SmallVector<CCValAssign, 16> ArgLocs;
   CCState ccinfo(CallConv, isVarArg, DAG.getMachineFunction(),
                  ArgLocs, *DAG.getContext());
   CCAssignFn *ccfn = KernCC_TGSI;
   // (isKernelFunction(mf.getFunction()) ? KernCC_TGSI : FunCC_TGSI);

   ccinfo.AnalyzeFormalArguments(Ins, ccfn);

   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
      CCValAssign &va = ArgLocs[i];
      MVT vt = va.getValVT();
      SDValue arg;

      if (va.isRegLoc()) {
         unsigned vreg = reginfo.createVirtualRegister(getRegClassFor(vt));

         reginfo.addLiveIn(va.getLocReg(), vreg);
         arg = DAG.getCopyFromReg(Chain, dl, vreg, vt);

      } else {
         SDValue ptr = DAG.getConstant(va.getLocMemOffset(), dl, MVT::i32);

         arg = DAG.getNode(TGSIISD::LOAD_INPUT, dl, vt, Chain, ptr);
      }

      InVals.push_back(arg);
   }

   return Chain;
}

SDValue
TGSITargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
                              SmallVectorImpl<SDValue> &InVals) const {
   SelectionDAG &DAG = CLI.DAG;
   SDLoc dl = CLI.DL;
   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
   SDValue Chain = CLI.Chain;
   SDValue Callee = CLI.Callee;
   bool &isTailCall = CLI.IsTailCall;
   CallingConv::ID CallConv = CLI.CallConv;
   bool isVarArg = CLI.IsVarArg;

   SDValue InFlag;
   // Analyze operands of the call, assigning locations to each operand.
   SmallVector<CCValAssign, 16> ArgLocs;
   CCState ccinfo(CallConv, isVarArg, DAG.getMachineFunction(),
                  ArgLocs, *DAG.getContext());
   ccinfo.AnalyzeCallOperands(Outs, FunCC_TGSI);

   // Walk the register assignments, inserting copies.
   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
      CCValAssign &VA = ArgLocs[i];
      SDValue Arg = OutVals[i];

      if (VA.getLocVT() == MVT::f32)
         Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);

      Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, InFlag);
      InFlag = Chain.getValue(1);
   }

   // Returns a chain & a flag for retval copy to use
   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
   SmallVector<SDValue, 8> Ops;
   Ops.push_back(Chain);
   Ops.push_back(Callee);
   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
      unsigned Reg = ArgLocs[i].getLocReg();

      Ops.push_back(DAG.getRegister(Reg, ArgLocs[i].getLocVT()));
   }
   if (InFlag.getNode())
      Ops.push_back(InFlag);

   Chain = DAG.getNode(TGSIISD::CALL, dl, NodeTys, Ops);
   InFlag = Chain.getValue(1);

   // Assign locations to each value returned by this call.
   SmallVector<CCValAssign, 16> RVLocs;
   CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
                  RVLocs, *DAG.getContext());

   RVInfo.AnalyzeCallResult(Ins, RetCC_TGSI);

   // Copy all of the result registers out of their specified physreg.
   for (unsigned i = 0; i != RVLocs.size(); ++i) {
      unsigned Reg = RVLocs[i].getLocReg();

      Chain = DAG.getCopyFromReg(Chain, dl, Reg,
                                 RVLocs[i].getValVT(), InFlag).getValue(1);
      InFlag = Chain.getValue(2);
      InVals.push_back(Chain.getValue(0));
   }

   isTailCall = false;
   return Chain;
}

//===----------------------------------------------------------------------===//
// TargetLowering Implementation
//===----------------------------------------------------------------------===//

TGSITargetLowering::TGSITargetLowering(TargetMachine &TM,
                                       const TGSISubtarget &STI)
   : TargetLowering(TM), Subtarget(&STI) {

   // Set up the register classes.
   addRegisterClass(MVT::i32, &TGSI::IRegsRegClass);
   addRegisterClass(MVT::v4i32, &TGSI::IVRegsRegClass);
   addRegisterClass(MVT::f32, &TGSI::FRegsRegClass);
   addRegisterClass(MVT::v4f32, &TGSI::FVRegsRegClass);

   setStackPointerRegisterToSaveRestore(TGSI::TEMP0);
   computeRegisterProperties(Subtarget->getRegisterInfo());

   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
   setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
   setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
   setOperationAction(ISD::BR_CC, MVT::f64, Expand);
   setOperationAction(ISD::BR_CC, MVT::Other, Expand);
   setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);

   setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
   setOperationAction(ISD::FGETSIGN, MVT::f32, Legal);
   setOperationAction(ISD::FLOG2, MVT::f32, Legal);
   setOperationAction(ISD::FEXP2, MVT::f32, Legal);

   setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
   setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Expand);
   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Expand);

   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
}

const char *TGSITargetLowering::getTargetNodeName(unsigned Opcode) const {
   switch (Opcode) {
      case TGSIISD::LOAD_INPUT:
         return "TGSIISD::LOAD_INPUT";
      case TGSIISD::CALL:
         return "TGSIISD::CALL";
      case TGSIISD::RET:
         return "TGSIISD::RET";
      default:
         llvm_unreachable("Invalid opcode");
   }
}

SDValue TGSITargetLowering::
CreateLiveInRegister(SelectionDAG &DAG, const TargetRegisterClass *RC,
                     unsigned Reg, EVT VT) const {
   MachineFunction &MF = DAG.getMachineFunction();
   MachineRegisterInfo &MRI = MF.getRegInfo();
   unsigned VirtualRegister;
   if (!MRI.isLiveIn(Reg)) {
      VirtualRegister = MRI.createVirtualRegister(RC);
      MRI.addLiveIn(Reg, VirtualRegister);
   } else {
      VirtualRegister = MRI.getLiveInVirtReg(Reg);
   }
   return DAG.getRegister(VirtualRegister, VT);
}

SDValue TGSITargetLowering::
LowerOperation(SDValue op, SelectionDAG &dag) const {
   switch (op.getOpcode()) {
   case ISD::INTRINSIC_WO_CHAIN: {
      unsigned IntrinsicID =
                         cast<ConstantSDNode>(op.getOperand(0))->getZExtValue();
      EVT VT = op.getValueType();
      switch(IntrinsicID) {
      case Intrinsic::tgsi_read_blockid_x:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_ID(x), VT);
      case Intrinsic::tgsi_read_blockid_y:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_ID(y), VT);
      case Intrinsic::tgsi_read_blockid_z:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_ID(z), VT);
      case Intrinsic::tgsi_read_blocksize_x:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_SIZE(x), VT);
      case Intrinsic::tgsi_read_blocksize_y:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_SIZE(y), VT);
      case Intrinsic::tgsi_read_blocksize_z:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_BLOCK_SIZE(z), VT);
      case Intrinsic::tgsi_read_gridsize_x:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_GRID_SIZE(x), VT);
      case Intrinsic::tgsi_read_gridsize_y:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_GRID_SIZE(y), VT);
      case Intrinsic::tgsi_read_gridsize_z:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_GRID_SIZE(z), VT);
      case Intrinsic::tgsi_read_threadid_x:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_THREAD_ID(x), VT);
      case Intrinsic::tgsi_read_threadid_y:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_THREAD_ID(y), VT);
      case Intrinsic::tgsi_read_threadid_z:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_THREAD_ID(z), VT);
      case Intrinsic::tgsi_read_workdim:
         return CreateLiveInRegister(dag, &TGSI::IRegsRegClass,
                                     TGSI_WORK_DIM, VT);
      default:
         llvm_unreachable("Unknown TGSI Intrinsic");
      }
      break; /* Never reached */
      }
   default:
      llvm_unreachable("Should not custom lower this!");
   }
}


// Pin TGSISection's and TGSITargetObjectFile's vtables to this file.
void TGSISection::anchor() {}

TGSITargetObjectFile::~TGSITargetObjectFile() {
  delete static_cast<TGSISection *>(TextSection);
  delete static_cast<TGSISection *>(DataSection);
  delete static_cast<TGSISection *>(BSSSection);
  delete static_cast<TGSISection *>(ReadOnlySection);

  delete static_cast<TGSISection *>(StaticCtorSection);
  delete static_cast<TGSISection *>(StaticDtorSection);
  delete static_cast<TGSISection *>(LSDASection);
  delete static_cast<TGSISection *>(EHFrameSection);
  delete static_cast<TGSISection *>(DwarfAbbrevSection);
  delete static_cast<TGSISection *>(DwarfInfoSection);
  delete static_cast<TGSISection *>(DwarfLineSection);
  delete static_cast<TGSISection *>(DwarfFrameSection);
  delete static_cast<TGSISection *>(DwarfPubTypesSection);
  delete static_cast<const TGSISection *>(DwarfDebugInlineSection);
  delete static_cast<TGSISection *>(DwarfStrSection);
  delete static_cast<TGSISection *>(DwarfLocSection);
  delete static_cast<TGSISection *>(DwarfARangesSection);
  delete static_cast<TGSISection *>(DwarfRangesSection);
}

MCSection *
TGSITargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
                                              SectionKind Kind, Mangler &Mang,
                                              const TargetMachine &TM) const {
  return getDataSection();
}