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 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
141 // V8 has no intrinsics for these particular operations.
142 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
143 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
144 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
146 setOperationAction(ISD::FSIN , MVT::f64, Expand);
147 setOperationAction(ISD::FCOS , MVT::f64, Expand);
148 setOperationAction(ISD::FSIN , MVT::f32, Expand);
149 setOperationAction(ISD::FCOS , MVT::f32, Expand);
150 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
151 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
152 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
153 setOperationAction(ISD::ROTL , MVT::i32, Expand);
154 setOperationAction(ISD::ROTR , MVT::i32, Expand);
155 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
157 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
158 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
159 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
161 // We don't have line number support yet.
162 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
163 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
164 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
166 // Not implemented yet.
167 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
168 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
170 computeRegisterProperties();
173 const char *SparcV8TargetLowering::getTargetNodeName(unsigned Opcode) const {
176 case V8ISD::CMPICC: return "V8ISD::CMPICC";
177 case V8ISD::CMPFCC: return "V8ISD::CMPFCC";
178 case V8ISD::BRICC: return "V8ISD::BRICC";
179 case V8ISD::BRFCC: return "V8ISD::BRFCC";
180 case V8ISD::Hi: return "V8ISD::Hi";
181 case V8ISD::Lo: return "V8ISD::Lo";
182 case V8ISD::FTOI: return "V8ISD::FTOI";
183 case V8ISD::ITOF: return "V8ISD::ITOF";
184 case V8ISD::SELECT_ICC: return "V8ISD::SELECT_ICC";
185 case V8ISD::SELECT_FCC: return "V8ISD::SELECT_FCC";
186 case V8ISD::RET_FLAG: return "V8ISD::RET_FLAG";
190 /// LowerArguments - V8 uses a very simple ABI, where all values are passed in
191 /// either one or two GPRs, including FP values. TODO: we should pass FP values
192 /// in FP registers for fastcc functions.
193 std::vector<SDOperand>
194 SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
195 MachineFunction &MF = DAG.getMachineFunction();
196 SSARegMap *RegMap = MF.getSSARegMap();
197 std::vector<SDOperand> ArgValues;
199 static const unsigned ArgRegs[] = {
200 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
203 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
204 unsigned ArgOffset = 68;
206 SDOperand Root = DAG.getRoot();
207 std::vector<SDOperand> OutChains;
209 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
210 MVT::ValueType ObjectVT = getValueType(I->getType());
213 default: assert(0 && "Unhandled argument type!");
218 if (I->use_empty()) { // Argument is dead.
219 if (CurArgReg < ArgRegEnd) ++CurArgReg;
220 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
221 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
222 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
223 MF.addLiveIn(*CurArgReg++, VReg);
224 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
225 if (ObjectVT != MVT::i32) {
226 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
228 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
229 DAG.getValueType(ObjectVT));
230 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
232 ArgValues.push_back(Arg);
234 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
235 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
237 if (ObjectVT == MVT::i32) {
238 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
241 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
243 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
244 DAG.getSrcValue(0), ObjectVT);
246 ArgValues.push_back(Load);
252 if (I->use_empty()) { // Argument is dead.
253 if (CurArgReg < ArgRegEnd) ++CurArgReg;
254 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
255 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
256 // FP value is passed in an integer register.
257 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
258 MF.addLiveIn(*CurArgReg++, VReg);
259 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
261 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
262 ArgValues.push_back(Arg);
269 if (I->use_empty()) { // Argument is dead.
270 if (CurArgReg < ArgRegEnd) ++CurArgReg;
271 if (CurArgReg < ArgRegEnd) ++CurArgReg;
272 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
273 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
274 ((CurArgReg-ArgRegs) & 1) == 0) {
275 // If this is a double argument and the whole thing lives on the stack,
276 // and the argument is aligned, load the double straight from the stack.
277 // We can't do a load in cases like void foo([6ints], int,double),
278 // because the double wouldn't be aligned!
279 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
280 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
281 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
282 DAG.getSrcValue(0)));
285 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
286 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
287 MF.addLiveIn(*CurArgReg++, VRegHi);
288 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
290 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
291 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
292 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
296 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
297 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
298 MF.addLiveIn(*CurArgReg++, VRegLo);
299 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
301 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
302 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
303 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
306 // Compose the two halves together into an i64 unit.
307 SDOperand WholeValue =
308 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
310 // If we want a double, do a bit convert.
311 if (ObjectVT == MVT::f64)
312 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
314 ArgValues.push_back(WholeValue);
321 // Store remaining ArgRegs to the stack if this is a varargs function.
322 if (F.getFunctionType()->isVarArg()) {
323 // Remember the vararg offset for the va_start implementation.
324 VarArgsFrameOffset = ArgOffset;
326 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
327 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
328 MF.addLiveIn(*CurArgReg, VReg);
329 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
331 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
332 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
334 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
335 Arg, FIPtr, DAG.getSrcValue(0)));
340 if (!OutChains.empty())
341 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
343 // Finally, inform the code generator which regs we return values in.
344 switch (getValueType(F.getReturnType())) {
345 default: assert(0 && "Unknown type!");
346 case MVT::isVoid: break;
351 MF.addLiveOut(V8::I0);
354 MF.addLiveOut(V8::I0);
355 MF.addLiveOut(V8::I1);
358 MF.addLiveOut(V8::F0);
361 MF.addLiveOut(V8::D0);
368 std::pair<SDOperand, SDOperand>
369 SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
370 bool isVarArg, unsigned CC,
371 bool isTailCall, SDOperand Callee,
372 ArgListTy &Args, SelectionDAG &DAG) {
373 MachineFunction &MF = DAG.getMachineFunction();
374 // Count the size of the outgoing arguments.
375 unsigned ArgsSize = 0;
376 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
377 switch (getValueType(Args[i].second)) {
378 default: assert(0 && "Unknown value type!");
393 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
397 // Keep stack frames 8-byte aligned.
398 ArgsSize = (ArgsSize+7) & ~7;
400 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
401 DAG.getConstant(ArgsSize, getPointerTy()));
403 SDOperand StackPtr, NullSV;
404 std::vector<SDOperand> Stores;
405 std::vector<SDOperand> RegValuesToPass;
406 unsigned ArgOffset = 68;
407 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
408 SDOperand Val = Args[i].first;
409 MVT::ValueType ObjectVT = Val.getValueType();
410 SDOperand ValToStore(0, 0);
413 default: assert(0 && "Unhandled argument type!");
417 // Promote the integer to 32-bits. If the input type is signed, use a
418 // sign extend, otherwise use a zero extend.
419 if (Args[i].second->isSigned())
420 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
422 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
427 if (RegValuesToPass.size() >= 6) {
430 RegValuesToPass.push_back(Val);
435 if (RegValuesToPass.size() >= 6) {
438 // Convert this to a FP value in an int reg.
439 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
440 RegValuesToPass.push_back(Val);
445 // If we can store this directly into the outgoing slot, do so. We can
446 // do this when all ArgRegs are used and if the outgoing slot is aligned.
447 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
452 // Otherwise, convert this to a FP value in int regs.
453 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
457 if (RegValuesToPass.size() >= 6) {
458 ValToStore = Val; // Whole thing is passed in memory.
462 // Split the value into top and bottom part. Top part goes in a reg.
463 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
464 DAG.getConstant(1, MVT::i32));
465 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
466 DAG.getConstant(0, MVT::i32));
467 RegValuesToPass.push_back(Hi);
469 if (RegValuesToPass.size() >= 6) {
474 RegValuesToPass.push_back(Lo);
479 if (ValToStore.Val) {
481 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
482 NullSV = DAG.getSrcValue(NULL);
484 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
485 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
486 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
487 ValToStore, PtrOff, NullSV));
489 ArgOffset += ObjSize;
492 // Emit all stores, make sure the occur before any copies into physregs.
494 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
496 static const unsigned ArgRegs[] = {
497 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
500 // Build a sequence of copy-to-reg nodes chained together with token chain
501 // and flag operands which copy the outgoing args into O[0-5].
503 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
504 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
505 InFlag = Chain.getValue(1);
508 // If the callee is a GlobalAddress node (quite common, every direct call is)
509 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
510 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
511 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
513 std::vector<MVT::ValueType> NodeTys;
514 NodeTys.push_back(MVT::Other); // Returns a chain
515 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
517 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
519 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
520 InFlag = Chain.getValue(1);
522 MVT::ValueType RetTyVT = getValueType(RetTy);
524 if (RetTyVT != MVT::isVoid) {
526 default: assert(0 && "Unknown value type to return!");
530 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
531 Chain = RetVal.getValue(1);
533 // Add a note to keep track of whether it is sign or zero extended.
534 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
535 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
536 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
539 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
540 Chain = RetVal.getValue(1);
543 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
544 Chain = RetVal.getValue(1);
547 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
548 Chain = RetVal.getValue(1);
551 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
552 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
554 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
555 Chain = Hi.getValue(1);
560 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
561 DAG.getConstant(ArgsSize, getPointerTy()));
563 return std::make_pair(RetVal, Chain);
566 SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
569 switch (Op.getValueType()) {
570 default: assert(0 && "Unknown type to return!");
572 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
575 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
578 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
581 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
582 DAG.getConstant(1, MVT::i32));
583 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
584 DAG.getConstant(0, MVT::i32));
585 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
586 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
589 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
592 SDOperand SparcV8TargetLowering::
593 LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
596 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
597 DAG.getRegister(V8::I6, MVT::i32),
598 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
599 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
600 VAListP, DAG.getSrcValue(VAListV));
603 std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
604 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
605 const Type *ArgTy, SelectionDAG &DAG) {
606 // Load the pointer out of the valist.
607 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
608 VAListP, DAG.getSrcValue(VAListV));
609 MVT::ValueType ArgVT = getValueType(ArgTy);
610 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
611 Ptr, DAG.getSrcValue(NULL));
612 // Increment the pointer.
613 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
614 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
615 // Store it back to the valist.
616 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
617 VAListP, DAG.getSrcValue(VAListV));
618 return std::make_pair(Val, Chain);
621 std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
622 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
624 assert(0 && "Unimp");
628 SDOperand SparcV8TargetLowering::
629 LowerOperation(SDOperand Op, SelectionDAG &DAG) {
630 switch (Op.getOpcode()) {
631 default: assert(0 && "Should not custom lower this!");
632 case ISD::GlobalAddress: {
633 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
634 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
635 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
636 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
637 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
639 case ISD::ConstantPool: {
640 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
641 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
642 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
643 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
644 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
646 case ISD::FP_TO_SINT:
647 // Convert the fp value to integer in an FP register.
648 assert(Op.getValueType() == MVT::i32);
649 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
650 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
651 case ISD::SINT_TO_FP: {
652 assert(Op.getOperand(0).getValueType() == MVT::i32);
653 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
654 // Convert the int value to FP in an FP register.
655 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
658 SDOperand Chain = Op.getOperand(0);
659 SDOperand CC = Op.getOperand(1);
660 SDOperand LHS = Op.getOperand(2);
661 SDOperand RHS = Op.getOperand(3);
662 SDOperand Dest = Op.getOperand(4);
664 // Get the condition flag.
665 if (LHS.getValueType() == MVT::i32) {
666 std::vector<MVT::ValueType> VTs;
667 VTs.push_back(MVT::i32);
668 VTs.push_back(MVT::Flag);
669 std::vector<SDOperand> Ops;
672 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
673 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
675 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
676 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
679 case ISD::SELECT_CC: {
680 SDOperand LHS = Op.getOperand(0);
681 SDOperand RHS = Op.getOperand(1);
682 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
683 SDOperand TrueVal = Op.getOperand(2);
684 SDOperand FalseVal = Op.getOperand(3);
686 SDOperand CompareFlag;
688 if (LHS.getValueType() == MVT::i32) {
689 std::vector<MVT::ValueType> VTs;
690 VTs.push_back(LHS.getValueType()); // subcc returns a value
691 VTs.push_back(MVT::Flag);
692 std::vector<SDOperand> Ops;
695 CompareFlag = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
696 Opc = V8ISD::SELECT_ICC;
698 CompareFlag = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
699 Opc = V8ISD::SELECT_FCC;
701 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
702 DAG.getConstant(CC, MVT::i32), CompareFlag);
704 case ISD::DYNAMIC_STACKALLOC: {
705 SDOperand Chain = Op.getOperand(0);
706 SDOperand Size = Op.getOperand(1);
708 SDOperand SP = DAG.getCopyFromReg(Chain, V8::O6, MVT::i32);
709 Chain = SP.getValue(1);
710 SDOperand Res = DAG.getNode(ISD::SUB, MVT::i32, SP, Size);
711 Chain = DAG.getCopyToReg(Chain, V8::O6, Res);
713 std::vector<MVT::ValueType> VTs(Op.Val->value_begin(), Op.Val->value_end());
714 std::vector<SDOperand> Ops;
716 Ops.push_back(Chain);
717 return DAG.getNode(ISD::MERGE_VALUES, VTs, Ops);
723 SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
724 MachineBasicBlock *BB) {
726 // Figure out the conditional branch opcode to use for this select_cc.
727 switch (MI->getOpcode()) {
728 default: assert(0 && "Unknown SELECT_CC!");
729 case V8::SELECT_CC_Int_ICC:
730 case V8::SELECT_CC_FP_ICC:
731 case V8::SELECT_CC_DFP_ICC:
733 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
734 default: assert(0 && "Unknown integer condition code!");
735 case ISD::SETEQ: BROpcode = V8::BE; break;
736 case ISD::SETNE: BROpcode = V8::BNE; break;
737 case ISD::SETLT: BROpcode = V8::BL; break;
738 case ISD::SETGT: BROpcode = V8::BG; break;
739 case ISD::SETLE: BROpcode = V8::BLE; break;
740 case ISD::SETGE: BROpcode = V8::BGE; break;
741 case ISD::SETULT: BROpcode = V8::BCS; break;
742 case ISD::SETULE: BROpcode = V8::BLEU; break;
743 case ISD::SETUGT: BROpcode = V8::BGU; break;
744 case ISD::SETUGE: BROpcode = V8::BCC; break;
747 case V8::SELECT_CC_Int_FCC:
748 case V8::SELECT_CC_FP_FCC:
749 case V8::SELECT_CC_DFP_FCC:
751 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
752 default: assert(0 && "Unknown fp condition code!");
753 case ISD::SETEQ: BROpcode = V8::FBE; break;
754 case ISD::SETNE: BROpcode = V8::FBNE; break;
755 case ISD::SETLT: BROpcode = V8::FBL; break;
756 case ISD::SETGT: BROpcode = V8::FBG; break;
757 case ISD::SETLE: BROpcode = V8::FBLE; break;
758 case ISD::SETGE: BROpcode = V8::FBGE; break;
759 case ISD::SETULT: BROpcode = V8::FBUL; break;
760 case ISD::SETULE: BROpcode = V8::FBULE; break;
761 case ISD::SETUGT: BROpcode = V8::FBUG; break;
762 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
763 case ISD::SETUO: BROpcode = V8::FBU; break;
764 case ISD::SETO: BROpcode = V8::FBO; break;
765 case ISD::SETONE: BROpcode = V8::FBLG; break;
766 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
771 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
772 // control-flow pattern. The incoming instruction knows the destination vreg
773 // to set, the condition code register to branch on, the true/false values to
774 // select between, and a branch opcode to use.
775 const BasicBlock *LLVM_BB = BB->getBasicBlock();
776 ilist<MachineBasicBlock>::iterator It = BB;
783 // fallthrough --> copy0MBB
784 MachineBasicBlock *thisMBB = BB;
785 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
786 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
787 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
788 MachineFunction *F = BB->getParent();
789 F->getBasicBlockList().insert(It, copy0MBB);
790 F->getBasicBlockList().insert(It, sinkMBB);
791 // Update machine-CFG edges
792 BB->addSuccessor(copy0MBB);
793 BB->addSuccessor(sinkMBB);
797 // # fallthrough to sinkMBB
800 // Update machine-CFG edges
801 BB->addSuccessor(sinkMBB);
804 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
807 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
808 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
809 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
811 delete MI; // The pseudo instruction is gone now.
815 //===----------------------------------------------------------------------===//
816 // Instruction Selector Implementation
817 //===----------------------------------------------------------------------===//
819 //===--------------------------------------------------------------------===//
820 /// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
821 /// instructions for SelectionDAG operations.
824 class SparcV8DAGToDAGISel : public SelectionDAGISel {
825 SparcV8TargetLowering V8Lowering;
827 SparcV8DAGToDAGISel(TargetMachine &TM)
828 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
830 SDOperand Select(SDOperand Op);
832 // Complex Pattern Selectors.
833 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
834 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
836 /// InstructionSelectBasicBlock - This callback is invoked by
837 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
838 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
840 virtual const char *getPassName() const {
841 return "PowerPC DAG->DAG Pattern Instruction Selection";
844 // Include the pieces autogenerated from the target description.
845 #include "SparcV8GenDAGISel.inc"
847 } // end anonymous namespace
849 /// InstructionSelectBasicBlock - This callback is invoked by
850 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
851 void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
854 // Select target instructions for the DAG.
855 DAG.setRoot(Select(DAG.getRoot()));
857 DAG.RemoveDeadNodes();
859 // Emit machine code to BB.
860 ScheduleAndEmitDAG(DAG);
863 bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
865 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
866 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
867 Offset = CurDAG->getTargetConstant(0, MVT::i32);
871 if (Addr.getOpcode() == ISD::ADD) {
872 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
873 if (Predicate_simm13(CN)) {
874 if (FrameIndexSDNode *FIN =
875 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
876 // Constant offset from frame ref.
877 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
879 Base = Select(Addr.getOperand(0));
881 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
885 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
886 Base = Select(Addr.getOperand(1));
887 Offset = Addr.getOperand(0).getOperand(0);
890 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
891 Base = Select(Addr.getOperand(0));
892 Offset = Addr.getOperand(1).getOperand(0);
897 Offset = CurDAG->getTargetConstant(0, MVT::i32);
901 bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
903 if (Addr.getOpcode() == ISD::FrameIndex) return false;
904 if (Addr.getOpcode() == ISD::ADD) {
905 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
906 Predicate_simm13(Addr.getOperand(1).Val))
907 return false; // Let the reg+imm pattern catch this!
908 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
909 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
910 return false; // Let the reg+imm pattern catch this!
911 R1 = Select(Addr.getOperand(0));
912 R2 = Select(Addr.getOperand(1));
917 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
921 SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
923 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
924 N->getOpcode() < V8ISD::FIRST_NUMBER)
925 return Op; // Already selected.
926 // If this has already been converted, use it.
927 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
928 if (CGMI != CodeGenMap.end()) return CGMI->second;
930 switch (N->getOpcode()) {
932 case ISD::FrameIndex: {
933 int FI = cast<FrameIndexSDNode>(N)->getIndex();
935 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
936 CurDAG->getTargetFrameIndex(FI, MVT::i32),
937 CurDAG->getTargetConstant(0, MVT::i32));
938 return CodeGenMap[Op] =
939 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
940 CurDAG->getTargetFrameIndex(FI, MVT::i32),
941 CurDAG->getTargetConstant(0, MVT::i32));
943 case ISD::ADD_PARTS: {
944 SDOperand LHSL = Select(N->getOperand(0));
945 SDOperand LHSH = Select(N->getOperand(1));
946 SDOperand RHSL = Select(N->getOperand(2));
947 SDOperand RHSH = Select(N->getOperand(3));
948 // FIXME, handle immediate RHS.
949 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
951 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
953 CodeGenMap[SDOperand(N, 0)] = Low;
954 CodeGenMap[SDOperand(N, 1)] = Hi;
955 return Op.ResNo ? Hi : Low;
957 case ISD::SUB_PARTS: {
958 SDOperand LHSL = Select(N->getOperand(0));
959 SDOperand LHSH = Select(N->getOperand(1));
960 SDOperand RHSL = Select(N->getOperand(2));
961 SDOperand RHSH = Select(N->getOperand(3));
962 // FIXME, handle immediate RHS.
963 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
965 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
967 CodeGenMap[SDOperand(N, 0)] = Low;
968 CodeGenMap[SDOperand(N, 1)] = Hi;
969 return Op.ResNo ? Hi : Low;
973 // FIXME: should use a custom expander to expose the SRA to the dag.
974 SDOperand DivLHS = Select(N->getOperand(0));
975 SDOperand DivRHS = Select(N->getOperand(1));
977 // Set the Y register to the high-part.
979 if (N->getOpcode() == ISD::SDIV) {
980 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
981 CurDAG->getTargetConstant(31, MVT::i32));
983 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
985 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
986 CurDAG->getRegister(V8::G0, MVT::i32));
988 // FIXME: Handle div by immediate.
989 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
990 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
994 // FIXME: Handle mul by immediate.
995 SDOperand MulLHS = Select(N->getOperand(0));
996 SDOperand MulRHS = Select(N->getOperand(1));
997 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
998 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
1000 // The high part is in the Y register.
1001 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
1004 // FIXME: This is a workaround for a bug in tblgen.
1005 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
1006 // Emits: (CALL:void (tglobaladdr:i32):$dst)
1007 // Pattern complexity = 2 cost = 1
1008 SDOperand N1 = N->getOperand(1);
1009 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
1010 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
1011 SDOperand InFlag = SDOperand(0, 0);
1012 SDOperand Chain = N->getOperand(0);
1013 SDOperand Tmp0 = N1;
1014 Chain = Select(Chain);
1016 if (N->getNumOperands() == 3) {
1017 InFlag = Select(N->getOperand(2));
1018 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1021 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1024 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
1025 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
1026 return Result.getValue(Op.ResNo);
1032 return SelectCode(Op);
1036 /// createPPCISelDag - This pass converts a legalized DAG into a
1037 /// PowerPC-specific DAG, ready for instruction scheduling.
1039 FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1040 return new SparcV8DAGToDAGISel(TM);