1 //===-- SparcISelLowering.cpp - Sparc 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 //===----------------------------------------------------------------------===//
10 // This file implements the interfaces that Sparc uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #include "SparcISelLowering.h"
16 #include "SparcTargetMachine.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
27 //===----------------------------------------------------------------------===//
28 // Calling Convention Implementation
29 //===----------------------------------------------------------------------===//
31 #include "SparcGenCallingConv.inc"
33 static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
34 // CCValAssign - represent the assignment of the return value to locations.
35 SmallVector<CCValAssign, 16> RVLocs;
36 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
37 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
39 // CCState - Info about the registers and stack slot.
40 CCState CCInfo(CC, isVarArg, DAG.getTarget(), RVLocs);
42 // Analize return values of ISD::RET
43 CCInfo.AnalyzeReturn(Op.Val, RetCC_Sparc32);
45 // If this is the first return lowered for this function, add the regs to the
46 // liveout set for the function.
47 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
48 for (unsigned i = 0; i != RVLocs.size(); ++i)
49 if (RVLocs[i].isRegLoc())
50 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
53 SDOperand Chain = Op.getOperand(0);
56 // Copy the result values into the output registers.
57 for (unsigned i = 0; i != RVLocs.size(); ++i) {
58 CCValAssign &VA = RVLocs[i];
59 assert(VA.isRegLoc() && "Can only return in registers!");
61 // ISD::RET => ret chain, (regnum1,val1), ...
62 // So i*2+1 index only the regnums.
63 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
65 // Guarantee that all emitted copies are stuck together with flags.
66 Flag = Chain.getValue(1);
70 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain, Flag);
71 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain);
74 /// LowerArguments - V8 uses a very simple ABI, where all values are passed in
75 /// either one or two GPRs, including FP values. TODO: we should pass FP values
76 /// in FP registers for fastcc functions.
78 SparcTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
79 SmallVectorImpl<SDOperand> &ArgValues) {
80 MachineFunction &MF = DAG.getMachineFunction();
81 MachineRegisterInfo &RegInfo = MF.getRegInfo();
83 static const unsigned ArgRegs[] = {
84 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
87 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
88 unsigned ArgOffset = 68;
90 SDOperand Root = DAG.getRoot();
91 std::vector<SDOperand> OutChains;
93 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
94 MVT ObjectVT = getValueType(I->getType());
96 switch (ObjectVT.getSimpleVT()) {
97 default: assert(0 && "Unhandled argument type!");
102 if (I->use_empty()) { // Argument is dead.
103 if (CurArgReg < ArgRegEnd) ++CurArgReg;
104 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
105 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
106 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
107 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
108 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
109 if (ObjectVT != MVT::i32) {
110 unsigned AssertOp = ISD::AssertSext;
111 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
112 DAG.getValueType(ObjectVT));
113 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
115 ArgValues.push_back(Arg);
117 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
118 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
120 if (ObjectVT == MVT::i32) {
121 Load = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
123 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
125 // Sparc is big endian, so add an offset based on the ObjectVT.
126 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
127 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
128 DAG.getConstant(Offset, MVT::i32));
129 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
131 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
133 ArgValues.push_back(Load);
139 if (I->use_empty()) { // Argument is dead.
140 if (CurArgReg < ArgRegEnd) ++CurArgReg;
141 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
142 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
143 // FP value is passed in an integer register.
144 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
145 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
146 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
148 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
149 ArgValues.push_back(Arg);
151 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
152 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
153 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, NULL, 0);
154 ArgValues.push_back(Load);
161 if (I->use_empty()) { // Argument is dead.
162 if (CurArgReg < ArgRegEnd) ++CurArgReg;
163 if (CurArgReg < ArgRegEnd) ++CurArgReg;
164 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
167 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
168 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
169 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
170 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
172 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
173 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
174 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
178 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
179 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
180 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
181 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
183 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
185 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
188 // Compose the two halves together into an i64 unit.
189 SDOperand WholeValue =
190 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
192 // If we want a double, do a bit convert.
193 if (ObjectVT == MVT::f64)
194 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
196 ArgValues.push_back(WholeValue);
203 // Store remaining ArgRegs to the stack if this is a varargs function.
205 // Remember the vararg offset for the va_start implementation.
206 VarArgsFrameOffset = ArgOffset;
208 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
209 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
210 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
211 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
213 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
214 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
216 OutChains.push_back(DAG.getStore(DAG.getRoot(), Arg, FIPtr, NULL, 0));
221 if (!OutChains.empty())
222 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other,
223 &OutChains[0], OutChains.size()));
226 static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
227 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
228 SDOperand Chain = Op.getOperand(0);
229 SDOperand Callee = Op.getOperand(4);
230 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
233 // Analyze operands of the call, assigning locations to each operand.
234 SmallVector<CCValAssign, 16> ArgLocs;
235 CCState CCInfo(CallingConv, isVarArg, DAG.getTarget(), ArgLocs);
236 CCInfo.AnalyzeCallOperands(Op.Val, CC_Sparc32);
238 // Get the size of the outgoing arguments stack space requirement.
239 unsigned ArgsSize = CCInfo.getNextStackOffset();
240 // FIXME: We can't use this until f64 is known to take two GPRs.
244 // Count the size of the outgoing arguments.
245 unsigned ArgsSize = 0;
246 for (unsigned i = 5, e = Op.getNumOperands(); i != e; i += 2) {
247 switch (Op.getOperand(i).getValueType().getSimpleVT()) {
248 default: assert(0 && "Unknown value type!");
263 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
268 // Keep stack frames 8-byte aligned.
269 ArgsSize = (ArgsSize+7) & ~7;
271 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize));
273 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
274 SmallVector<SDOperand, 8> MemOpChains;
277 // Walk the register/memloc assignments, inserting copies/loads.
278 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
279 CCValAssign &VA = ArgLocs[i];
281 // Arguments start after the 5 first operands of ISD::CALL
282 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
284 // Promote the value if needed.
285 switch (VA.getLocInfo()) {
286 default: assert(0 && "Unknown loc info!");
287 case CCValAssign::Full: break;
288 case CCValAssign::SExt:
289 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
291 case CCValAssign::ZExt:
292 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
294 case CCValAssign::AExt:
295 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
299 // Arguments that can be passed on register must be kept at
302 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
306 assert(VA.isMemLoc());
308 // Create a store off the stack pointer for this argument.
309 SDOperand StackPtr = DAG.getRegister(SP::O6, MVT::i32);
310 // FIXME: VERIFY THAT 68 IS RIGHT.
311 SDOperand PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
312 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
313 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
317 static const unsigned ArgRegs[] = {
318 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
320 unsigned ArgOffset = 68;
322 for (unsigned i = 5, e = Op.getNumOperands(); i != e; i += 2) {
323 SDOperand Val = Op.getOperand(i);
324 MVT ObjectVT = Val.getValueType();
325 SDOperand ValToStore(0, 0);
327 switch (ObjectVT.getSimpleVT()) {
328 default: assert(0 && "Unhandled argument type!");
332 if (RegsToPass.size() >= 6) {
335 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
340 if (RegsToPass.size() >= 6) {
343 // Convert this to a FP value in an int reg.
344 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
345 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
350 // Otherwise, convert this to a FP value in int regs.
351 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
355 if (RegsToPass.size() >= 6) {
356 ValToStore = Val; // Whole thing is passed in memory.
360 // Split the value into top and bottom part. Top part goes in a reg.
361 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
362 DAG.getConstant(1, MVT::i32));
363 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
364 DAG.getConstant(0, MVT::i32));
365 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
367 if (RegsToPass.size() >= 6) {
372 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
377 if (ValToStore.Val) {
378 SDOperand StackPtr = DAG.getRegister(SP::O6, MVT::i32);
379 SDOperand PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
380 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
381 MemOpChains.push_back(DAG.getStore(Chain, ValToStore, PtrOff, NULL, 0));
383 ArgOffset += ObjSize;
387 // Emit all stores, make sure the occur before any copies into physregs.
388 if (!MemOpChains.empty())
389 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
390 &MemOpChains[0], MemOpChains.size());
392 // Build a sequence of copy-to-reg nodes chained together with token
393 // chain and flag operands which copy the outgoing args into registers.
394 // The InFlag in necessary since all emited instructions must be
397 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
398 unsigned Reg = RegsToPass[i].first;
399 // Remap I0->I7 -> O0->O7.
400 if (Reg >= SP::I0 && Reg <= SP::I7)
401 Reg = Reg-SP::I0+SP::O0;
403 Chain = DAG.getCopyToReg(Chain, Reg, RegsToPass[i].second, InFlag);
404 InFlag = Chain.getValue(1);
407 // If the callee is a GlobalAddress node (quite common, every direct call is)
408 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
409 // Likewise ExternalSymbol -> TargetExternalSymbol.
410 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
411 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
412 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
413 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
415 std::vector<MVT> NodeTys;
416 NodeTys.push_back(MVT::Other); // Returns a chain
417 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
418 SDOperand Ops[] = { Chain, Callee, InFlag };
419 Chain = DAG.getNode(SPISD::CALL, NodeTys, Ops, InFlag.Val ? 3 : 2);
420 InFlag = Chain.getValue(1);
422 Chain = DAG.getCALLSEQ_END(Chain,
423 DAG.getConstant(ArgsSize, MVT::i32),
424 DAG.getConstant(0, MVT::i32), InFlag);
425 InFlag = Chain.getValue(1);
427 // Assign locations to each value returned by this call.
428 SmallVector<CCValAssign, 16> RVLocs;
429 CCState RVInfo(CallingConv, isVarArg, DAG.getTarget(), RVLocs);
431 RVInfo.AnalyzeCallResult(Op.Val, RetCC_Sparc32);
432 SmallVector<SDOperand, 8> ResultVals;
434 // Copy all of the result registers out of their specified physreg.
435 for (unsigned i = 0; i != RVLocs.size(); ++i) {
436 unsigned Reg = RVLocs[i].getLocReg();
438 // Remap I0->I7 -> O0->O7.
439 if (Reg >= SP::I0 && Reg <= SP::I7)
440 Reg = Reg-SP::I0+SP::O0;
442 Chain = DAG.getCopyFromReg(Chain, Reg,
443 RVLocs[i].getValVT(), InFlag).getValue(1);
444 InFlag = Chain.getValue(2);
445 ResultVals.push_back(Chain.getValue(0));
448 ResultVals.push_back(Chain);
450 // Merge everything together with a MERGE_VALUES node.
451 return DAG.getMergeValues(Op.Val->getVTList(), &ResultVals[0],
457 //===----------------------------------------------------------------------===//
458 // TargetLowering Implementation
459 //===----------------------------------------------------------------------===//
461 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
463 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
465 default: assert(0 && "Unknown integer condition code!");
466 case ISD::SETEQ: return SPCC::ICC_E;
467 case ISD::SETNE: return SPCC::ICC_NE;
468 case ISD::SETLT: return SPCC::ICC_L;
469 case ISD::SETGT: return SPCC::ICC_G;
470 case ISD::SETLE: return SPCC::ICC_LE;
471 case ISD::SETGE: return SPCC::ICC_GE;
472 case ISD::SETULT: return SPCC::ICC_CS;
473 case ISD::SETULE: return SPCC::ICC_LEU;
474 case ISD::SETUGT: return SPCC::ICC_GU;
475 case ISD::SETUGE: return SPCC::ICC_CC;
479 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
481 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
483 default: assert(0 && "Unknown fp condition code!");
485 case ISD::SETOEQ: return SPCC::FCC_E;
487 case ISD::SETUNE: return SPCC::FCC_NE;
489 case ISD::SETOLT: return SPCC::FCC_L;
491 case ISD::SETOGT: return SPCC::FCC_G;
493 case ISD::SETOLE: return SPCC::FCC_LE;
495 case ISD::SETOGE: return SPCC::FCC_GE;
496 case ISD::SETULT: return SPCC::FCC_UL;
497 case ISD::SETULE: return SPCC::FCC_ULE;
498 case ISD::SETUGT: return SPCC::FCC_UG;
499 case ISD::SETUGE: return SPCC::FCC_UGE;
500 case ISD::SETUO: return SPCC::FCC_U;
501 case ISD::SETO: return SPCC::FCC_O;
502 case ISD::SETONE: return SPCC::FCC_LG;
503 case ISD::SETUEQ: return SPCC::FCC_UE;
508 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
509 : TargetLowering(TM) {
511 // Set up the register classes.
512 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
513 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
514 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
516 // Turn FP extload into load/fextend
517 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
518 // Sparc doesn't have i1 sign extending load
519 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
520 // Turn FP truncstore into trunc + store.
521 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
523 // Custom legalize GlobalAddress nodes into LO/HI parts.
524 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
525 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
526 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
528 // Sparc doesn't have sext_inreg, replace them with shl/sra
529 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
530 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
531 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
533 // Sparc has no REM or DIVREM operations.
534 setOperationAction(ISD::UREM, MVT::i32, Expand);
535 setOperationAction(ISD::SREM, MVT::i32, Expand);
536 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
537 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
539 // Custom expand fp<->sint
540 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
541 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
544 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
545 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
547 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
548 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
550 // Sparc has no select or setcc: expand to SELECT_CC.
551 setOperationAction(ISD::SELECT, MVT::i32, Expand);
552 setOperationAction(ISD::SELECT, MVT::f32, Expand);
553 setOperationAction(ISD::SELECT, MVT::f64, Expand);
554 setOperationAction(ISD::SETCC, MVT::i32, Expand);
555 setOperationAction(ISD::SETCC, MVT::f32, Expand);
556 setOperationAction(ISD::SETCC, MVT::f64, Expand);
558 // Sparc doesn't have BRCOND either, it has BR_CC.
559 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
560 setOperationAction(ISD::BRIND, MVT::Other, Expand);
561 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
562 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
563 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
564 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
566 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
567 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
568 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
570 // SPARC has no intrinsics for these particular operations.
571 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
573 setOperationAction(ISD::FSIN , MVT::f64, Expand);
574 setOperationAction(ISD::FCOS , MVT::f64, Expand);
575 setOperationAction(ISD::FREM , MVT::f64, Expand);
576 setOperationAction(ISD::FSIN , MVT::f32, Expand);
577 setOperationAction(ISD::FCOS , MVT::f32, Expand);
578 setOperationAction(ISD::FREM , MVT::f32, Expand);
579 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
580 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
581 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
582 setOperationAction(ISD::ROTL , MVT::i32, Expand);
583 setOperationAction(ISD::ROTR , MVT::i32, Expand);
584 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
585 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
586 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
587 setOperationAction(ISD::FPOW , MVT::f64, Expand);
588 setOperationAction(ISD::FPOW , MVT::f32, Expand);
590 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
591 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
592 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
594 // FIXME: Sparc provides these multiplies, but we don't have them yet.
595 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
597 // We don't have line number support yet.
598 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
599 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
600 setOperationAction(ISD::LABEL, MVT::Other, Expand);
602 // RET must be custom lowered, to meet ABI requirements
603 setOperationAction(ISD::RET , MVT::Other, Custom);
605 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
606 setOperationAction(ISD::VASTART , MVT::Other, Custom);
607 // VAARG needs to be lowered to not do unaligned accesses for doubles.
608 setOperationAction(ISD::VAARG , MVT::Other, Custom);
610 // Use the default implementation.
611 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
612 setOperationAction(ISD::VAEND , MVT::Other, Expand);
613 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
614 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
615 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
617 // No debug info support yet.
618 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
619 setOperationAction(ISD::LABEL, MVT::Other, Expand);
620 setOperationAction(ISD::DECLARE, MVT::Other, Expand);
622 setStackPointerRegisterToSaveRestore(SP::O6);
624 if (TM.getSubtarget<SparcSubtarget>().isV9())
625 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
627 computeRegisterProperties();
630 const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
633 case SPISD::CMPICC: return "SPISD::CMPICC";
634 case SPISD::CMPFCC: return "SPISD::CMPFCC";
635 case SPISD::BRICC: return "SPISD::BRICC";
636 case SPISD::BRFCC: return "SPISD::BRFCC";
637 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
638 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
639 case SPISD::Hi: return "SPISD::Hi";
640 case SPISD::Lo: return "SPISD::Lo";
641 case SPISD::FTOI: return "SPISD::FTOI";
642 case SPISD::ITOF: return "SPISD::ITOF";
643 case SPISD::CALL: return "SPISD::CALL";
644 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
648 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
649 /// be zero. Op is expected to be a target specific node. Used by DAG
651 void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
655 const SelectionDAG &DAG,
656 unsigned Depth) const {
657 APInt KnownZero2, KnownOne2;
658 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
660 switch (Op.getOpcode()) {
662 case SPISD::SELECT_ICC:
663 case SPISD::SELECT_FCC:
664 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
666 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
668 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
669 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
671 // Only known if known in both the LHS and RHS.
672 KnownOne &= KnownOne2;
673 KnownZero &= KnownZero2;
678 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
679 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
680 static void LookThroughSetCC(SDOperand &LHS, SDOperand &RHS,
681 ISD::CondCode CC, unsigned &SPCC) {
682 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0 &&
684 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
685 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
686 (LHS.getOpcode() == SPISD::SELECT_FCC &&
687 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
688 isa<ConstantSDNode>(LHS.getOperand(0)) &&
689 isa<ConstantSDNode>(LHS.getOperand(1)) &&
690 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
691 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
692 SDOperand CMPCC = LHS.getOperand(3);
693 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
694 LHS = CMPCC.getOperand(0);
695 RHS = CMPCC.getOperand(1);
699 static SDOperand LowerGLOBALADDRESS(SDOperand Op, SelectionDAG &DAG) {
700 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
701 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
702 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, GA);
703 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, GA);
704 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
707 static SDOperand LowerCONSTANTPOOL(SDOperand Op, SelectionDAG &DAG) {
708 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
709 Constant *C = N->getConstVal();
710 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
711 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, CP);
712 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, CP);
713 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
716 static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
717 // Convert the fp value to integer in an FP register.
718 assert(Op.getValueType() == MVT::i32);
719 Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
720 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
723 static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
724 assert(Op.getOperand(0).getValueType() == MVT::i32);
725 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
726 // Convert the int value to FP in an FP register.
727 return DAG.getNode(SPISD::ITOF, Op.getValueType(), Tmp);
730 static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
731 SDOperand Chain = Op.getOperand(0);
732 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
733 SDOperand LHS = Op.getOperand(2);
734 SDOperand RHS = Op.getOperand(3);
735 SDOperand Dest = Op.getOperand(4);
736 unsigned Opc, SPCC = ~0U;
738 // If this is a br_cc of a "setcc", and if the setcc got lowered into
739 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
740 LookThroughSetCC(LHS, RHS, CC, SPCC);
742 // Get the condition flag.
743 SDOperand CompareFlag;
744 if (LHS.getValueType() == MVT::i32) {
745 std::vector<MVT> VTs;
746 VTs.push_back(MVT::i32);
747 VTs.push_back(MVT::Flag);
748 SDOperand Ops[2] = { LHS, RHS };
749 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
750 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
753 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
754 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
757 return DAG.getNode(Opc, MVT::Other, Chain, Dest,
758 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
761 static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
762 SDOperand LHS = Op.getOperand(0);
763 SDOperand RHS = Op.getOperand(1);
764 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
765 SDOperand TrueVal = Op.getOperand(2);
766 SDOperand FalseVal = Op.getOperand(3);
767 unsigned Opc, SPCC = ~0U;
769 // If this is a select_cc of a "setcc", and if the setcc got lowered into
770 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
771 LookThroughSetCC(LHS, RHS, CC, SPCC);
773 SDOperand CompareFlag;
774 if (LHS.getValueType() == MVT::i32) {
775 std::vector<MVT> VTs;
776 VTs.push_back(LHS.getValueType()); // subcc returns a value
777 VTs.push_back(MVT::Flag);
778 SDOperand Ops[2] = { LHS, RHS };
779 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
780 Opc = SPISD::SELECT_ICC;
781 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
783 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
784 Opc = SPISD::SELECT_FCC;
785 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
787 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
788 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
791 static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
792 SparcTargetLowering &TLI) {
793 // vastart just stores the address of the VarArgsFrameIndex slot into the
794 // memory location argument.
795 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
796 DAG.getRegister(SP::I6, MVT::i32),
797 DAG.getConstant(TLI.getVarArgsFrameOffset(),
799 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
800 return DAG.getStore(Op.getOperand(0), Offset, Op.getOperand(1), SV, 0);
803 static SDOperand LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
804 SDNode *Node = Op.Val;
805 MVT VT = Node->getValueType(0);
806 SDOperand InChain = Node->getOperand(0);
807 SDOperand VAListPtr = Node->getOperand(1);
808 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
809 SDOperand VAList = DAG.getLoad(MVT::i32, InChain, VAListPtr, SV, 0);
810 // Increment the pointer, VAList, to the next vaarg
811 SDOperand NextPtr = DAG.getNode(ISD::ADD, MVT::i32, VAList,
812 DAG.getConstant(VT.getSizeInBits()/8,
814 // Store the incremented VAList to the legalized pointer
815 InChain = DAG.getStore(VAList.getValue(1), NextPtr,
817 // Load the actual argument out of the pointer VAList, unless this is an
820 return DAG.getLoad(VT, InChain, VAList, NULL, 0);
822 // Otherwise, load it as i64, then do a bitconvert.
823 SDOperand V = DAG.getLoad(MVT::i64, InChain, VAList, NULL, 0);
825 // Bit-Convert the value to f64.
827 DAG.getNode(ISD::BIT_CONVERT, MVT::f64, V),
830 return DAG.getMergeValues(DAG.getVTList(MVT::f64, MVT::Other), Ops, 2);
833 static SDOperand LowerDYNAMIC_STACKALLOC(SDOperand Op, SelectionDAG &DAG) {
834 SDOperand Chain = Op.getOperand(0); // Legalize the chain.
835 SDOperand Size = Op.getOperand(1); // Legalize the size.
837 unsigned SPReg = SP::O6;
838 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, MVT::i32);
839 SDOperand NewSP = DAG.getNode(ISD::SUB, MVT::i32, SP, Size); // Value
840 Chain = DAG.getCopyToReg(SP.getValue(1), SPReg, NewSP); // Output chain
842 // The resultant pointer is actually 16 words from the bottom of the stack,
843 // to provide a register spill area.
844 SDOperand NewVal = DAG.getNode(ISD::ADD, MVT::i32, NewSP,
845 DAG.getConstant(96, MVT::i32));
846 SDOperand Ops[2] = { NewVal, Chain };
847 return DAG.getMergeValues(DAG.getVTList(MVT::i32, MVT::Other), Ops, 2);
851 SDOperand SparcTargetLowering::
852 LowerOperation(SDOperand Op, SelectionDAG &DAG) {
853 switch (Op.getOpcode()) {
854 default: assert(0 && "Should not custom lower this!");
855 // Frame & Return address. Currently unimplemented
856 case ISD::RETURNADDR: return SDOperand();
857 case ISD::FRAMEADDR: return SDOperand();
858 case ISD::GlobalTLSAddress:
859 assert(0 && "TLS not implemented for Sparc.");
860 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
861 case ISD::ConstantPool: return LowerCONSTANTPOOL(Op, DAG);
862 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
863 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
864 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
865 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
866 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
867 case ISD::VAARG: return LowerVAARG(Op, DAG);
868 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
869 case ISD::CALL: return LowerCALL(Op, DAG);
870 case ISD::RET: return LowerRET(Op, DAG);
875 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
876 MachineBasicBlock *BB) {
877 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
880 // Figure out the conditional branch opcode to use for this select_cc.
881 switch (MI->getOpcode()) {
882 default: assert(0 && "Unknown SELECT_CC!");
883 case SP::SELECT_CC_Int_ICC:
884 case SP::SELECT_CC_FP_ICC:
885 case SP::SELECT_CC_DFP_ICC:
886 BROpcode = SP::BCOND;
888 case SP::SELECT_CC_Int_FCC:
889 case SP::SELECT_CC_FP_FCC:
890 case SP::SELECT_CC_DFP_FCC:
891 BROpcode = SP::FBCOND;
895 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
897 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
898 // control-flow pattern. The incoming instruction knows the destination vreg
899 // to set, the condition code register to branch on, the true/false values to
900 // select between, and a branch opcode to use.
901 const BasicBlock *LLVM_BB = BB->getBasicBlock();
902 ilist<MachineBasicBlock>::iterator It = BB;
909 // fallthrough --> copy0MBB
910 MachineBasicBlock *thisMBB = BB;
911 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
912 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
913 BuildMI(BB, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
914 MachineFunction *F = BB->getParent();
915 F->getBasicBlockList().insert(It, copy0MBB);
916 F->getBasicBlockList().insert(It, sinkMBB);
917 // Update machine-CFG edges by transferring all successors of the current
918 // block to the new block which will contain the Phi node for the select.
919 sinkMBB->transferSuccessors(BB);
920 // Next, add the true and fallthrough blocks as its successors.
921 BB->addSuccessor(copy0MBB);
922 BB->addSuccessor(sinkMBB);
926 // # fallthrough to sinkMBB
929 // Update machine-CFG edges
930 BB->addSuccessor(sinkMBB);
933 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
936 BuildMI(BB, TII.get(SP::PHI), MI->getOperand(0).getReg())
937 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
938 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
940 delete MI; // The pseudo instruction is gone now.