1 //===-- SparcV8ISelDAGToDAG.cpp - A dag to dag inst selector for SparcV8 --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the V8 target
12 //===----------------------------------------------------------------------===//
15 #include "SparcV8TargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/CodeGen/SSARegMap.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Support/Debug.h"
29 //===----------------------------------------------------------------------===//
30 // TargetLowering Implementation
31 //===----------------------------------------------------------------------===//
35 FIRST_NUMBER = ISD::BUILTIN_OP_END+V8::INSTRUCTION_LIST_END,
36 CMPICC, // Compare two GPR operands, set icc.
37 CMPFCC, // Compare two FP operands, set fcc.
38 BRICC, // Branch to dest on icc condition
39 BRFCC, // Branch to dest on fcc condition
41 Hi, Lo, // Hi/Lo operations, typically on a global address.
43 FTOI, // FP to Int within a FP register.
44 ITOF, // Int to FP within a FP register.
46 SELECT_ICC, // Select between two values using the current ICC flags.
47 SELECT_FCC, // Select between two values using the current FCC flags.
49 RET_FLAG, // Return with a flag operand.
54 class SparcV8TargetLowering : public TargetLowering {
55 int VarArgsFrameOffset; // Frame offset to start of varargs area.
57 SparcV8TargetLowering(TargetMachine &TM);
58 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
59 virtual std::vector<SDOperand>
60 LowerArguments(Function &F, SelectionDAG &DAG);
61 virtual std::pair<SDOperand, SDOperand>
62 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
64 bool isTailCall, SDOperand Callee, ArgListTy &Args,
67 virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
69 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
70 Value *VAListV, SelectionDAG &DAG);
71 virtual std::pair<SDOperand,SDOperand>
72 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
73 const Type *ArgTy, SelectionDAG &DAG);
74 virtual std::pair<SDOperand, SDOperand>
75 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
77 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
78 MachineBasicBlock *MBB);
80 virtual const char *getTargetNodeName(unsigned Opcode) const;
84 SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
85 : TargetLowering(TM) {
87 // Set up the register classes.
88 addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
89 addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
90 addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
92 // Custom legalize GlobalAddress nodes into LO/HI parts.
93 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
94 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
96 // Sparc doesn't have sext_inreg, replace them with shl/sra
97 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
98 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
99 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
101 // Sparc has no REM operation.
102 setOperationAction(ISD::UREM, MVT::i32, Expand);
103 setOperationAction(ISD::SREM, MVT::i32, Expand);
105 // Custom expand fp<->sint
106 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
107 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
110 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
111 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
113 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
114 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
116 // Turn FP extload into load/fextend
117 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
119 // Sparc has no select or setcc: expand to SELECT_CC.
120 setOperationAction(ISD::SELECT, MVT::i32, Expand);
121 setOperationAction(ISD::SELECT, MVT::f32, Expand);
122 setOperationAction(ISD::SELECT, MVT::f64, Expand);
123 setOperationAction(ISD::SETCC, MVT::i32, Expand);
124 setOperationAction(ISD::SETCC, MVT::f32, Expand);
125 setOperationAction(ISD::SETCC, MVT::f64, Expand);
127 // Sparc doesn't have BRCOND either, it has BR_CC.
128 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
129 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
130 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
131 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
132 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
133 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
135 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
136 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
137 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
139 // V8 has no intrinsics for these particular operations.
140 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
141 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
142 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
144 setOperationAction(ISD::FSIN , MVT::f64, Expand);
145 setOperationAction(ISD::FCOS , MVT::f64, Expand);
146 setOperationAction(ISD::FSIN , MVT::f32, Expand);
147 setOperationAction(ISD::FCOS , MVT::f32, Expand);
148 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
151 setOperationAction(ISD::ROTL , MVT::i32, Expand);
152 setOperationAction(ISD::ROTR , MVT::i32, Expand);
154 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
155 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
156 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
158 // We don't have line number support yet.
159 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
160 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
161 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
163 computeRegisterProperties();
166 const char *SparcV8TargetLowering::getTargetNodeName(unsigned Opcode) const {
168 case V8ISD::CMPICC: return "V8ISD::CMPICC";
169 case V8ISD::CMPFCC: return "V8ISD::CMPFCC";
170 case V8ISD::BRICC: return "V8ISD::BRICC";
171 case V8ISD::BRFCC: return "V8ISD::BRFCC";
172 case V8ISD::Hi: return "V8ISD::Hi";
173 case V8ISD::Lo: return "V8ISD::Lo";
174 case V8ISD::FTOI: return "V8ISD::FTOI";
175 case V8ISD::ITOF: return "V8ISD::ITOF";
176 case V8ISD::SELECT_ICC: return "V8ISD::SELECT_ICC";
177 case V8ISD::SELECT_FCC: return "V8ISD::SELECT_FCC";
178 case V8ISD::RET_FLAG: return "V8ISD::RET_FLAG";
182 /// LowerArguments - V8 uses a very simple ABI, where all values are passed in
183 /// either one or two GPRs, including FP values. TODO: we should pass FP values
184 /// in FP registers for fastcc functions.
185 std::vector<SDOperand>
186 SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
187 MachineFunction &MF = DAG.getMachineFunction();
188 SSARegMap *RegMap = MF.getSSARegMap();
189 std::vector<SDOperand> ArgValues;
191 static const unsigned ArgRegs[] = {
192 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
195 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
196 unsigned ArgOffset = 68;
198 SDOperand Root = DAG.getRoot();
199 std::vector<SDOperand> OutChains;
201 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
202 MVT::ValueType ObjectVT = getValueType(I->getType());
205 default: assert(0 && "Unhandled argument type!");
210 if (I->use_empty()) { // Argument is dead.
211 if (CurArgReg < ArgRegEnd) ++CurArgReg;
212 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
213 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
214 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
215 MF.addLiveIn(*CurArgReg++, VReg);
216 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
217 if (ObjectVT != MVT::i32) {
218 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
220 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
221 DAG.getValueType(ObjectVT));
222 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
224 ArgValues.push_back(Arg);
226 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
227 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
229 if (ObjectVT == MVT::i32) {
230 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
233 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
235 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
236 DAG.getSrcValue(0), ObjectVT);
238 ArgValues.push_back(Load);
244 if (I->use_empty()) { // Argument is dead.
245 if (CurArgReg < ArgRegEnd) ++CurArgReg;
246 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
247 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
248 // FP value is passed in an integer register.
249 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
250 MF.addLiveIn(*CurArgReg++, VReg);
251 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
253 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
254 ArgValues.push_back(Arg);
261 if (I->use_empty()) { // Argument is dead.
262 if (CurArgReg < ArgRegEnd) ++CurArgReg;
263 if (CurArgReg < ArgRegEnd) ++CurArgReg;
264 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
265 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
266 ((CurArgReg-ArgRegs) & 1) == 0) {
267 // If this is a double argument and the whole thing lives on the stack,
268 // and the argument is aligned, load the double straight from the stack.
269 // We can't do a load in cases like void foo([6ints], int,double),
270 // because the double wouldn't be aligned!
271 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
272 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
273 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
274 DAG.getSrcValue(0)));
277 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
278 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
279 MF.addLiveIn(*CurArgReg++, VRegHi);
280 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
282 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
283 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
284 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
288 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
289 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
290 MF.addLiveIn(*CurArgReg++, VRegLo);
291 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
293 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
294 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
295 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
298 // Compose the two halves together into an i64 unit.
299 SDOperand WholeValue =
300 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
302 // If we want a double, do a bit convert.
303 if (ObjectVT == MVT::f64)
304 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
306 ArgValues.push_back(WholeValue);
313 // Store remaining ArgRegs to the stack if this is a varargs function.
314 if (F.getFunctionType()->isVarArg()) {
315 // Remember the vararg offset for the va_start implementation.
316 VarArgsFrameOffset = ArgOffset;
318 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
319 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
320 MF.addLiveIn(*CurArgReg, VReg);
321 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
323 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
324 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
326 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
327 Arg, FIPtr, DAG.getSrcValue(0)));
332 if (!OutChains.empty())
333 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
335 // Finally, inform the code generator which regs we return values in.
336 switch (getValueType(F.getReturnType())) {
337 default: assert(0 && "Unknown type!");
338 case MVT::isVoid: break;
343 MF.addLiveOut(V8::I0);
346 MF.addLiveOut(V8::I0);
347 MF.addLiveOut(V8::I1);
350 MF.addLiveOut(V8::F0);
353 MF.addLiveOut(V8::D0);
360 std::pair<SDOperand, SDOperand>
361 SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
362 bool isVarArg, unsigned CC,
363 bool isTailCall, SDOperand Callee,
364 ArgListTy &Args, SelectionDAG &DAG) {
365 MachineFunction &MF = DAG.getMachineFunction();
366 // Count the size of the outgoing arguments.
367 unsigned ArgsSize = 0;
368 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
369 switch (getValueType(Args[i].second)) {
370 default: assert(0 && "Unknown value type!");
385 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
389 // Keep stack frames 8-byte aligned.
390 ArgsSize = (ArgsSize+7) & ~7;
392 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
393 DAG.getConstant(ArgsSize, getPointerTy()));
395 SDOperand StackPtr, NullSV;
396 std::vector<SDOperand> Stores;
397 std::vector<SDOperand> RegValuesToPass;
398 unsigned ArgOffset = 68;
399 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
400 SDOperand Val = Args[i].first;
401 MVT::ValueType ObjectVT = Val.getValueType();
402 SDOperand ValToStore(0, 0);
405 default: assert(0 && "Unhandled argument type!");
409 // Promote the integer to 32-bits. If the input type is signed, use a
410 // sign extend, otherwise use a zero extend.
411 if (Args[i].second->isSigned())
412 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
414 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
419 if (RegValuesToPass.size() >= 6) {
422 RegValuesToPass.push_back(Val);
427 if (RegValuesToPass.size() >= 6) {
430 // Convert this to a FP value in an int reg.
431 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
432 RegValuesToPass.push_back(Val);
437 // If we can store this directly into the outgoing slot, do so. We can
438 // do this when all ArgRegs are used and if the outgoing slot is aligned.
439 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
444 // Otherwise, convert this to a FP value in int regs.
445 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
449 if (RegValuesToPass.size() >= 6) {
450 ValToStore = Val; // Whole thing is passed in memory.
454 // Split the value into top and bottom part. Top part goes in a reg.
455 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
456 DAG.getConstant(1, MVT::i32));
457 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
458 DAG.getConstant(0, MVT::i32));
459 RegValuesToPass.push_back(Hi);
461 if (RegValuesToPass.size() >= 6) {
466 RegValuesToPass.push_back(Lo);
471 if (ValToStore.Val) {
473 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
474 NullSV = DAG.getSrcValue(NULL);
476 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
477 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
478 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
479 ValToStore, PtrOff, NullSV));
481 ArgOffset += ObjSize;
484 // Emit all stores, make sure the occur before any copies into physregs.
486 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
488 static const unsigned ArgRegs[] = {
489 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
492 // Build a sequence of copy-to-reg nodes chained together with token chain
493 // and flag operands which copy the outgoing args into O[0-5].
495 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
496 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
497 InFlag = Chain.getValue(1);
500 // If the callee is a GlobalAddress node (quite common, every direct call is)
501 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
503 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
505 std::vector<MVT::ValueType> NodeTys;
506 NodeTys.push_back(MVT::Other); // Returns a chain
507 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
509 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
511 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
512 InFlag = Chain.getValue(1);
514 MVT::ValueType RetTyVT = getValueType(RetTy);
516 if (RetTyVT != MVT::isVoid) {
518 default: assert(0 && "Unknown value type to return!");
522 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
523 Chain = RetVal.getValue(1);
525 // Add a note to keep track of whether it is sign or zero extended.
526 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
527 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
528 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
531 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
532 Chain = RetVal.getValue(1);
535 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
536 Chain = RetVal.getValue(1);
539 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
540 Chain = RetVal.getValue(1);
543 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
544 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
546 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
547 Chain = Hi.getValue(1);
552 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
553 DAG.getConstant(ArgsSize, getPointerTy()));
555 return std::make_pair(RetVal, Chain);
558 SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
561 switch (Op.getValueType()) {
562 default: assert(0 && "Unknown type to return!");
564 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
567 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
570 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
573 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
574 DAG.getConstant(1, MVT::i32));
575 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
576 DAG.getConstant(0, MVT::i32));
577 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
578 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
581 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
584 SDOperand SparcV8TargetLowering::
585 LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
588 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
589 DAG.getRegister(V8::I6, MVT::i32),
590 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
591 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
592 VAListP, DAG.getSrcValue(VAListV));
595 std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
596 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
597 const Type *ArgTy, SelectionDAG &DAG) {
598 // Load the pointer out of the valist.
599 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
600 VAListP, DAG.getSrcValue(VAListV));
601 MVT::ValueType ArgVT = getValueType(ArgTy);
602 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
603 Ptr, DAG.getSrcValue(NULL));
604 // Increment the pointer.
605 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
606 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
607 // Store it back to the valist.
608 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
609 VAListP, DAG.getSrcValue(VAListV));
610 return std::make_pair(Val, Chain);
613 std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
614 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
616 assert(0 && "Unimp");
620 SDOperand SparcV8TargetLowering::
621 LowerOperation(SDOperand Op, SelectionDAG &DAG) {
622 switch (Op.getOpcode()) {
623 default: assert(0 && "Should not custom lower this!");
624 case ISD::GlobalAddress: {
625 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
626 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
627 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
628 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
629 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
631 case ISD::ConstantPool: {
632 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
633 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
634 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
635 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
636 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
638 case ISD::FP_TO_SINT:
639 // Convert the fp value to integer in an FP register.
640 assert(Op.getValueType() == MVT::i32);
641 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
642 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
643 case ISD::SINT_TO_FP: {
644 assert(Op.getOperand(0).getValueType() == MVT::i32);
645 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
646 // Convert the int value to FP in an FP register.
647 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
650 SDOperand Chain = Op.getOperand(0);
651 SDOperand CC = Op.getOperand(1);
652 SDOperand LHS = Op.getOperand(2);
653 SDOperand RHS = Op.getOperand(3);
654 SDOperand Dest = Op.getOperand(4);
656 // Get the condition flag.
657 if (LHS.getValueType() == MVT::i32) {
658 std::vector<MVT::ValueType> VTs;
659 VTs.push_back(MVT::i32);
660 VTs.push_back(MVT::Flag);
661 std::vector<SDOperand> Ops;
664 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops);
665 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
667 std::vector<MVT::ValueType> VTs;
668 VTs.push_back(MVT::i32);
669 VTs.push_back(MVT::Flag);
670 std::vector<SDOperand> Ops;
673 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, VTs, Ops);
674 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
677 case ISD::SELECT_CC: {
678 SDOperand LHS = Op.getOperand(0);
679 SDOperand RHS = Op.getOperand(1);
680 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
681 SDOperand TrueVal = Op.getOperand(2);
682 SDOperand FalseVal = Op.getOperand(3);
685 Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
686 std::vector<MVT::ValueType> VTs;
687 VTs.push_back(LHS.getValueType());
688 VTs.push_back(MVT::Flag);
689 std::vector<SDOperand> Ops;
692 SDOperand CompareFlag = DAG.getNode(Opc, VTs, Ops).getValue(1);
694 Opc = LHS.getValueType() == MVT::i32 ?
695 V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
696 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
697 DAG.getConstant(CC, MVT::i32), CompareFlag);
703 SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
704 MachineBasicBlock *BB) {
706 // Figure out the conditional branch opcode to use for this select_cc.
707 switch (MI->getOpcode()) {
708 default: assert(0 && "Unknown SELECT_CC!");
709 case V8::SELECT_CC_Int_ICC:
710 case V8::SELECT_CC_FP_ICC:
711 case V8::SELECT_CC_DFP_ICC:
713 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
714 default: assert(0 && "Unknown integer condition code!");
715 case ISD::SETEQ: BROpcode = V8::BE; break;
716 case ISD::SETNE: BROpcode = V8::BNE; break;
717 case ISD::SETLT: BROpcode = V8::BL; break;
718 case ISD::SETGT: BROpcode = V8::BG; break;
719 case ISD::SETLE: BROpcode = V8::BLE; break;
720 case ISD::SETGE: BROpcode = V8::BGE; break;
721 case ISD::SETULT: BROpcode = V8::BCS; break;
722 case ISD::SETULE: BROpcode = V8::BLEU; break;
723 case ISD::SETUGT: BROpcode = V8::BGU; break;
724 case ISD::SETUGE: BROpcode = V8::BCC; break;
727 case V8::SELECT_CC_Int_FCC:
728 case V8::SELECT_CC_FP_FCC:
729 case V8::SELECT_CC_DFP_FCC:
731 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
732 default: assert(0 && "Unknown fp condition code!");
733 case ISD::SETEQ: BROpcode = V8::FBE; break;
734 case ISD::SETNE: BROpcode = V8::FBNE; break;
735 case ISD::SETLT: BROpcode = V8::FBL; break;
736 case ISD::SETGT: BROpcode = V8::FBG; break;
737 case ISD::SETLE: BROpcode = V8::FBLE; break;
738 case ISD::SETGE: BROpcode = V8::FBGE; break;
739 case ISD::SETULT: BROpcode = V8::FBUL; break;
740 case ISD::SETULE: BROpcode = V8::FBULE; break;
741 case ISD::SETUGT: BROpcode = V8::FBUG; break;
742 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
743 case ISD::SETUO: BROpcode = V8::FBU; break;
744 case ISD::SETO: BROpcode = V8::FBO; break;
745 case ISD::SETONE: BROpcode = V8::FBLG; break;
746 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
751 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
752 // control-flow pattern. The incoming instruction knows the destination vreg
753 // to set, the condition code register to branch on, the true/false values to
754 // select between, and a branch opcode to use.
755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
756 ilist<MachineBasicBlock>::iterator It = BB;
763 // fallthrough --> copy0MBB
764 MachineBasicBlock *thisMBB = BB;
765 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
766 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
767 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
768 MachineFunction *F = BB->getParent();
769 F->getBasicBlockList().insert(It, copy0MBB);
770 F->getBasicBlockList().insert(It, sinkMBB);
771 // Update machine-CFG edges
772 BB->addSuccessor(copy0MBB);
773 BB->addSuccessor(sinkMBB);
777 // # fallthrough to sinkMBB
780 // Update machine-CFG edges
781 BB->addSuccessor(sinkMBB);
784 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
787 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
788 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
789 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
791 delete MI; // The pseudo instruction is gone now.
795 //===----------------------------------------------------------------------===//
796 // Instruction Selector Implementation
797 //===----------------------------------------------------------------------===//
799 //===--------------------------------------------------------------------===//
800 /// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
801 /// instructions for SelectionDAG operations.
804 class SparcV8DAGToDAGISel : public SelectionDAGISel {
805 SparcV8TargetLowering V8Lowering;
807 SparcV8DAGToDAGISel(TargetMachine &TM)
808 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
810 SDOperand Select(SDOperand Op);
812 // Complex Pattern Selectors.
813 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
814 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
816 /// InstructionSelectBasicBlock - This callback is invoked by
817 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
818 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
820 virtual const char *getPassName() const {
821 return "PowerPC DAG->DAG Pattern Instruction Selection";
824 // Include the pieces autogenerated from the target description.
825 #include "SparcV8GenDAGISel.inc"
827 } // end anonymous namespace
829 /// InstructionSelectBasicBlock - This callback is invoked by
830 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
831 void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
834 // Select target instructions for the DAG.
835 DAG.setRoot(Select(DAG.getRoot()));
837 DAG.RemoveDeadNodes();
839 // Emit machine code to BB.
840 ScheduleAndEmitDAG(DAG);
843 bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
845 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
846 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
847 Offset = CurDAG->getTargetConstant(0, MVT::i32);
851 if (Addr.getOpcode() == ISD::ADD) {
852 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
853 if (Predicate_simm13(CN)) {
854 if (FrameIndexSDNode *FIN =
855 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
856 // Constant offset from frame ref.
857 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
859 Base = Select(Addr.getOperand(0));
861 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
865 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
866 Base = Select(Addr.getOperand(1));
867 Offset = Addr.getOperand(0).getOperand(0);
870 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
871 Base = Select(Addr.getOperand(0));
872 Offset = Addr.getOperand(1).getOperand(0);
877 Offset = CurDAG->getTargetConstant(0, MVT::i32);
881 bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
883 if (Addr.getOpcode() == ISD::FrameIndex) return false;
884 if (Addr.getOpcode() == ISD::ADD) {
885 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
886 Predicate_simm13(Addr.getOperand(1).Val))
887 return false; // Let the reg+imm pattern catch this!
888 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
889 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
890 return false; // Let the reg+imm pattern catch this!
891 R1 = Select(Addr.getOperand(0));
892 R2 = Select(Addr.getOperand(1));
897 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
901 SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
903 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
904 N->getOpcode() < V8ISD::FIRST_NUMBER)
905 return Op; // Already selected.
906 // If this has already been converted, use it.
907 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
908 if (CGMI != CodeGenMap.end()) return CGMI->second;
910 switch (N->getOpcode()) {
912 case ISD::FrameIndex: {
913 int FI = cast<FrameIndexSDNode>(N)->getIndex();
915 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
916 CurDAG->getTargetFrameIndex(FI, MVT::i32),
917 CurDAG->getTargetConstant(0, MVT::i32));
918 return CodeGenMap[Op] =
919 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
920 CurDAG->getTargetFrameIndex(FI, MVT::i32),
921 CurDAG->getTargetConstant(0, MVT::i32));
923 case ISD::ADD_PARTS: {
924 SDOperand LHSL = Select(N->getOperand(0));
925 SDOperand LHSH = Select(N->getOperand(1));
926 SDOperand RHSL = Select(N->getOperand(2));
927 SDOperand RHSH = Select(N->getOperand(3));
928 // FIXME, handle immediate RHS.
929 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
931 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
933 CodeGenMap[SDOperand(N, 0)] = Low;
934 CodeGenMap[SDOperand(N, 1)] = Hi;
935 return Op.ResNo ? Hi : Low;
937 case ISD::SUB_PARTS: {
938 SDOperand LHSL = Select(N->getOperand(0));
939 SDOperand LHSH = Select(N->getOperand(1));
940 SDOperand RHSL = Select(N->getOperand(2));
941 SDOperand RHSH = Select(N->getOperand(3));
942 // FIXME, handle immediate RHS.
943 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
945 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
947 CodeGenMap[SDOperand(N, 0)] = Low;
948 CodeGenMap[SDOperand(N, 1)] = Hi;
949 return Op.ResNo ? Hi : Low;
953 // FIXME: should use a custom expander to expose the SRA to the dag.
954 SDOperand DivLHS = Select(N->getOperand(0));
955 SDOperand DivRHS = Select(N->getOperand(1));
957 // Set the Y register to the high-part.
959 if (N->getOpcode() == ISD::SDIV) {
960 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
961 CurDAG->getTargetConstant(31, MVT::i32));
963 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
965 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
966 CurDAG->getRegister(V8::G0, MVT::i32));
968 // FIXME: Handle div by immediate.
969 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
970 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
974 // FIXME: Handle mul by immediate.
975 SDOperand MulLHS = Select(N->getOperand(0));
976 SDOperand MulRHS = Select(N->getOperand(1));
977 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
978 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
980 // The high part is in the Y register.
981 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
984 // FIXME: This is a workaround for a bug in tblgen.
985 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
986 // Emits: (CALL:void (tglobaladdr:i32):$dst)
987 // Pattern complexity = 2 cost = 1
988 SDOperand N1 = N->getOperand(1);
989 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
990 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
991 SDOperand InFlag = SDOperand(0, 0);
992 SDOperand Chain = N->getOperand(0);
994 Chain = Select(Chain);
996 if (N->getNumOperands() == 3) {
997 InFlag = Select(N->getOperand(2));
998 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1001 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1004 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
1005 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
1006 return Result.getValue(Op.ResNo);
1012 return SelectCode(Op);
1016 /// createPPCISelDag - This pass converts a legalized DAG into a
1017 /// PowerPC-specific DAG, ready for instruction scheduling.
1019 FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1020 return new SparcV8DAGToDAGISel(TM);