blob: e19e14c12b340976e0e3725789545326c72c6f6c [file] [log] [blame]
Andrew Lenharth72b16d82005-06-17 16:52:12 +00001#if 0
2//===- SparcV8ISelPattern.cpp - A pattern matching isel for SparcV8 -------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file was developed by the LLVM research group and is distributed under
7// the University of Illinois Open Source License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file defines a pattern matching instruction selector for SparcV8.
12//
13//===----------------------------------------------------------------------===//
14
15//Please note that this file is a work in progress, and not a high
16//priority for anyone.
17
18#include "SparcV8.h"
19#include "SparcV8RegisterInfo.h"
20#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/CommandLine.h"
35#include <set>
36#include <algorithm>
37using namespace llvm;
38
39//===----------------------------------------------------------------------===//
40// V8TargetLowering - SparcV8 Implementation of the TargetLowering interface
41namespace {
42 class V8TargetLowering : public TargetLowering {
43 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
44 public:
45 V8TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
46 // Set up the TargetLowering object.
47 //I am having problems with shr n ubyte 1
48 setShiftAmountType(MVT::i32);
49 setSetCCResultType(MVT::i32);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
51
52 //FIXME: get these right
53 addRegisterClass(MVT::i64, V8::GPRCRegisterClass);
54 addRegisterClass(MVT::f64, V8::FPRCRegisterClass);
55 addRegisterClass(MVT::f32, V8::FPRCRegisterClass);
56
57 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
58 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
59 setOperationAction(ISD::EXTLOAD, MVT::f32, Promote);
60
61 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Expand);
62 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
63
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
65 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
66 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
67 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
68
69 setOperationAction(ISD::UREM, MVT::i32, Expand);
70 setOperationAction(ISD::SREM, MVT::i32, Expand);
71
72 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
73 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
74 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
75
76 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
77 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
78 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
79
80 // We don't support sin/cos/sqrt
81 setOperationAction(ISD::FSIN , MVT::f64, Expand);
82 setOperationAction(ISD::FCOS , MVT::f64, Expand);
83 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
84 setOperationAction(ISD::FSIN , MVT::f32, Expand);
85 setOperationAction(ISD::FCOS , MVT::f32, Expand);
86 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
87
88 computeRegisterProperties();
89
90 addLegalFPImmediate(+0.0); //F31
91 addLegalFPImmediate(-0.0); //-F31
92 }
93
94 /// LowerArguments - This hook must be implemented to indicate how we should
95 /// lower the arguments for the specified function, into the specified DAG.
96 virtual std::vector<SDOperand>
97 LowerArguments(Function &F, SelectionDAG &DAG);
98
99 /// LowerCallTo - This hook lowers an abstract call to a function into an
100 /// actual call.
101 virtual std::pair<SDOperand, SDOperand>
102 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
103 bool isTailCall, SDOperand Callee, ArgListTy &Args,
104 SelectionDAG &DAG);
105 };
106}
107
108/// AddLiveIn - This helper function adds the specified physical register to the
109/// MachineFunction as a live in value. It also creates a corresponding virtual
110/// register for it.
111static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
112 TargetRegisterClass *RC) {
113 assert(RC->contains(PReg) && "Not the correct regclass!");
114 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
115 MF.addLiveIn(PReg, VReg);
116 return VReg;
117}
118
119std::vector<SDOperand>
120V8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
121{
Andrew Lenharthe014f892005-06-27 23:59:51 +0000122 static const unsigned IncomingArgRegs[] =
123 { V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5 };
Andrew Lenharth72b16d82005-06-17 16:52:12 +0000124 std::vector<SDOperand> ArgValues;
125
126 MachineFunction &MF = DAG.getMachineFunction();
127 MachineFrameInfo*MFI = MF.getFrameInfo();
128
129 MachineBasicBlock& BB = MF.front();
130
Andrew Lenharthe014f892005-06-27 23:59:51 +0000131 unsigned ArgNo = 0;
132 unsigned ArgOffset = 92;
133 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
134 I != E; ++I, ++ArgNo) {
135 MVT::ValueType VT = getValueType(I->getType());
136 SDOperand argt;
137 if (ArgNo < 6) {
138 switch(VT) {
139 default:
140 std::cerr << "Unknown Type " << VT << "\n";
141 abort();
142 case MVT::f64:
143 case MVT::i64:
144 //FIXME: figure out the build pair thing
145 assert(0 && "doubles and longs not supported yet");
146 case MVT::f32:
147 argt = DAG.getCopyFromReg(AddLiveIn(MF, IncomingArgRegs[ArgNo],
148 MVT::i32),
149 VT, DAG.getRoot());
150 //copy out of Int reg
151 argt = DAG.getNode(ISD::FP_TO_UINT, MVT::f32, argt);
152 break;
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
156 case MVT::i32:
157 argt = DAG.getCopyFromReg(AddLiveIn(MF, IncomingArgRegs[ArgNo],
158 getRegClassFor(MVT::i32)),
159 VT, DAG.getRoot());
160 if (VT != MVT::i32)
161 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
162 break;
163 }
164 DAG.setRoot(argt.getValue(1));
165 } else {
166 //stack passed
167 switch(VT) {
168 default:
169 std::cerr << "Unknown Type " << VT << "\n";
170 abort();
171 case MVT::f64:
172 case MVT::i64:
173 //FIXME: figure out the build pair thing
174 assert(0 && "doubles and longs not supported yet");
175 case MVT::f32:
176 case MVT::i1:
177 case MVT::i8:
178 case MVT::i16:
179 case MVT::i32:
180 // Create the frame index object for this incoming parameter...
181 int FI = MFI->CreateFixedObject(4, ArgOffset);
182 argt = DAG.getLoad(VT,
183 DAG.getEntryNode(),
184 DAG.getFramIndex(FI, MVT::i32),
185 DAG.getSrcValue(NULL));
186 ArgOffset += 4;
187 break;
188 }
189 ArgValues.push_back(argt);
190 }
191 }
192
Andrew Lenharth72b16d82005-06-17 16:52:12 +0000193 //return the arguments
194 return ArgValues;
195}
196
197std::pair<SDOperand, SDOperand>
198V8TargetLowering::LowerCallTo(SDOperand Chain,
199 const Type *RetTy, bool isVarArg,
200 unsigned CallingConv, bool isTailCall,
201 SDOperand Callee, ArgListTy &Args,
202 SelectionDAG &DAG) {
203 //FIXME
204 return std::make_pair(Chain, Chain);
205}
206
207namespace {
208
209//===--------------------------------------------------------------------===//
210/// ISel - V8 specific code to select V8 machine instructions for
211/// SelectionDAG operations.
212//===--------------------------------------------------------------------===//
213class ISel : public SelectionDAGISel {
214
215 /// V8Lowering - This object fully describes how to lower LLVM code to an
216 /// V8-specific SelectionDAG.
217 V8TargetLowering V8Lowering;
218
219 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
220 // for sdiv and udiv until it is put into the future
221 // dag combiner.
222
223 /// ExprMap - As shared expressions are codegen'd, we keep track of which
224 /// vreg the value is produced in, so we only emit one copy of each compiled
225 /// tree.
226 static const unsigned notIn = (unsigned)(-1);
227 std::map<SDOperand, unsigned> ExprMap;
228
229public:
230 ISel(TargetMachine &TM) : SelectionDAGISel(V8Lowering), V8Lowering(TM)
231 {}
232
233 /// InstructionSelectBasicBlock - This callback is invoked by
234 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
235 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
236 DEBUG(BB->dump());
237
238 // Codegen the basic block.
239 ISelDAG = &DAG;
240 max_depth = DAG.getRoot().getNodeDepth();
241 Select(DAG.getRoot());
242
243 // Clear state used for selection.
244 ExprMap.clear();
245 }
246
247 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
248
249 unsigned SelectExpr(SDOperand N);
250 void Select(SDOperand N);
251
252};
253}
254
255void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
256 // If this function has live-in values, emit the copies from pregs to vregs at
257 // the top of the function, before anything else.
258 MachineBasicBlock *BB = MF.begin();
259 if (MF.livein_begin() != MF.livein_end()) {
260 SSARegMap *RegMap = MF.getSSARegMap();
261 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
262 E = MF.livein_end(); LI != E; ++LI) {
263 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
264 if (RC == V8::GPRCRegisterClass) {
265 BuildMI(BB, V8::ORrr, 2, LI->second).addReg(LI->first).addReg(V8::G0);
266 } else if (RC == V8::FPRCRegisterClass) {
267 BuildMI(BB, V8::FMOVSrr, 2, LI->second).addReg(LI->first);
268 } else {
269 assert(0 && "Unknown regclass!");
270 }
271 }
272 }
273}
274
275//These describe LDAx
276static const int IMM_LOW = -32768;
277static const int IMM_HIGH = 32767;
278static const int IMM_MULT = 65536;
279
280static long getUpper16(long l)
281{
282 long y = l / IMM_MULT;
283 if (l % IMM_MULT > IMM_HIGH)
284 ++y;
285 return y;
286}
287
288static long getLower16(long l)
289{
290 long h = getUpper16(l);
291 return l - h * IMM_MULT;
292}
293
294unsigned ISel::SelectExpr(SDOperand N) {
295 unsigned Result;
296 unsigned Tmp1, Tmp2 = 0, Tmp3;
297 unsigned Opc = 0;
298 unsigned opcode = N.getOpcode();
299
300 SDNode *Node = N.Val;
301 MVT::ValueType DestType = N.getValueType();
302
303 unsigned &Reg = ExprMap[N];
304 if (Reg) return Reg;
305
306 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
307 Reg = Result = (N.getValueType() != MVT::Other) ?
308 MakeReg(N.getValueType()) : notIn;
309 else {
310 // If this is a call instruction, make sure to prepare ALL of the result
311 // values as well as the chain.
312 if (Node->getNumValues() == 1)
313 Reg = Result = notIn; // Void call, just a chain.
314 else {
315 Result = MakeReg(Node->getValueType(0));
316 ExprMap[N.getValue(0)] = Result;
317 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
318 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
319 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
320 }
321 }
322
323 switch (opcode) {
324 default:
325 Node->dump();
326 assert(0 && "Node not handled!\n");
327
328 case ISD::EXTLOAD:
329 case ISD::ZEXTLOAD:
330 case ISD::SEXTLOAD:
331 case ISD::LOAD:
332 {
333 // Make sure we generate both values.
334 if (Result != notIn)
335 ExprMap[N.getValue(1)] = notIn; // Generate the token
336 else
337 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
338
339 SDOperand Chain = N.getOperand(0);
340 SDOperand Address = N.getOperand(1);
341 Select(Chain);
342 unsigned Adr = SelectExpr(Address);
343 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
344 case MVT::i32: Opc = V8::LD;
345 case MVT::i16: Opc = opcode == ISD::ZEXTLOAD ? V8::LDUH : V8::LDSH; break;
346 case MVT::i8: Opc = opcode == ISD::ZEXTLOAD ? V8::LDUB : V8::LDSB; break;
347 case MVT::f64: Opc = V8::LDFSRrr;
348 case MVT::f32: Opc = V8::LDDFrr;
349 default:
350 Node->dump();
351 assert(0 && "Bad type!");
352 break;
353 }
354 BuildMI(BB, Opc, 1, Result).addReg(Adr);
355 return Result;
356 }
357
358 case ISD::TAILCALL:
359 case ISD::CALL:
360 {
361 //FIXME:
362 abort();
363 return Result;
364 }
365
366 case ISD::CopyFromReg:
367 {
368 // Make sure we generate both values.
369 if (Result != notIn)
370 ExprMap[N.getValue(1)] = notIn; // Generate the token
371 else
372 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
373
374 SDOperand Chain = N.getOperand(0);
375 Select(Chain);
376 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
377
378 BuildMI(BB, V8::ORrr, 2, Result).addReg(r).addReg(V8::G0);
379 return Result;
380 }
381
382 //Most of the plain arithmetic and logic share the same form, and the same
383 //constant immediate test
384 case ISD::XOR:
385 case ISD::AND:
386 case ISD::OR:
387 case ISD::SHL:
388 case ISD::SRL:
389 case ISD::SRA:
390 case ISD::ADD:
391 case ISD::SUB:
392 case ISD::SDIV:
393 case ISD::UDIV:
394 case ISD::SMUL:
395 case ISD::UMUL:
396 switch(opcode) {
397 case ISD::XOR: Opc = V8::XORrr; break;
398 case ISD::AND: Opc = V8::ANDrr; break;
399 case ISD::OR: Opc = V8::ORrr; break;
400 case ISD::SHL: Opc = V8::SLLrr; break;
401 case ISD::SRL: Opc = V8::SRLrr; break;
402 case ISD::SRA: Opc = V8::SRArr; break;
403 case ISD::ADD: Opc = V8::ADDrr; break;
404 case ISD::SUB: Opc = V8::SUBrr; break;
405 case ISD::SDIV: Opc = V8::SDIVrr; break;
406 case ISD::UDIV: Opc = V8::UDIVrr; break;
407 case ISD::SMUL: Opc = V8::SMULrr; break;
408 case ISD::UMUL: Opc = V8::UMULrr; break;
409 }
410 Tmp1 = SelectExpr(N.getOperand(0));
411 Tmp2 = SelectExpr(N.getOperand(1));
412 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
413 return Result;
414
415 }
416 return 0;
417}
418
419void ISel::Select(SDOperand N) {
420 unsigned Tmp1, Tmp2, Opc;
421 unsigned opcode = N.getOpcode();
422
423 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
424 return; // Already selected.
425
426 SDNode *Node = N.Val;
427
428 switch (opcode) {
429
430 default:
431 Node->dump(); std::cerr << "\n";
432 assert(0 && "Node not handled yet!");
433
434 case ISD::BRCOND: {
435 //FIXME
436 abort();
437 return;
438 }
439
440 case ISD::BR: {
441 MachineBasicBlock *Dest =
442 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
443
444 Select(N.getOperand(0));
445 BuildMI(BB, V8::BA, 1).addMBB(Dest);
446 return;
447 }
448
449 case ISD::ImplicitDef:
450 Select(N.getOperand(0));
451 BuildMI(BB, V8::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
452 return;
453
454 case ISD::EntryToken: return; // Noop
455
456 case ISD::TokenFactor:
457 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
458 Select(Node->getOperand(i));
459 return;
460
461 case ISD::CopyToReg:
462 Select(N.getOperand(0));
463 Tmp1 = SelectExpr(N.getOperand(1));
464 Tmp2 = cast<RegSDNode>(N)->getReg();
465
466 if (Tmp1 != Tmp2) {
467 if (N.getOperand(1).getValueType() == MVT::f64 ||
468 N.getOperand(1).getValueType() == MVT::f32)
469 BuildMI(BB, V8::FMOVS, 2, Tmp2).addReg(Tmp1);
470 else
471 BuildMI(BB, V8::ORrr, 2, Tmp2).addReg(Tmp1).addReg(V8::G0);
472 }
473 return;
474
475 case ISD::RET:
476 //FIXME:
477 abort();
478 return;
479
480 case ISD::TRUNCSTORE:
481 case ISD::STORE:
482 {
483 SDOperand Chain = N.getOperand(0);
484 SDOperand Value = N.getOperand(1);
485 SDOperand Address = N.getOperand(2);
486 Select(Chain);
487
488 Tmp1 = SelectExpr(Value);
489 Tmp2 = SelectExpr(Address);
490
491 unsigned VT = opcode == ISD::STORE ?
492 Value.getValueType() : cast<MVTSDNode>(Node)->getExtraValueType();
493 switch(VT) {
494 default: assert(0 && "unknown Type in store");
495 case MVT::f64: Opc = V8::STDFrr; break;
496 case MVT::f32: Opc = V8::STFrr; break;
497 case MVT::i1: //FIXME: DAG does not promote this load
498 case MVT::i8: Opc = V8::STBrr; break;
499 case MVT::i16: Opc = V8::STHrr; break;
500 case MVT::i32: Opc = V8::STLrr; break;
501 case MVT::i64: Opc = V8::STDrr; break;
502 }
503
504 BuildMI(BB,Opc,2).addReg(Tmp1).addReg(Tmp2);
505 return;
506 }
507
508 case ISD::EXTLOAD:
509 case ISD::SEXTLOAD:
510 case ISD::ZEXTLOAD:
511 case ISD::LOAD:
512 case ISD::CopyFromReg:
513 case ISD::TAILCALL:
514 case ISD::CALL:
515 case ISD::DYNAMIC_STACKALLOC:
516 ExprMap.erase(N);
517 SelectExpr(N);
518 return;
519
520 case ISD::CALLSEQ_START:
521 case ISD::CALLSEQ_END:
522 Select(N.getOperand(0));
523 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
524
525 Opc = N.getOpcode() == ISD::CALLSEQ_START ? V8::ADJUSTCALLSTACKDOWN :
526 V8::ADJUSTCALLSTACKUP;
527 BuildMI(BB, Opc, 1).addImm(Tmp1);
528 return;
529 }
530 assert(0 && "Should not be reached!");
531}
532
533
534/// createV8PatternInstructionSelector - This pass converts an LLVM function
535/// into a machine code representation using pattern matching and a machine
536/// description file.
537///
538FunctionPass *llvm::createV8PatternInstructionSelector(TargetMachine &TM) {
539 return new ISel(TM);
540}
541
542#endif