1 //===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
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 implements the PPCISelLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "PPCISelLowering.h"
15 #include "PPCTargetMachine.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Function.h"
25 PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
26 : TargetLowering(TM) {
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
32 // Use _setjmp/_longjmp instead of setjmp/longjmp.
33 setUseUnderscoreSetJmpLongJmp(true);
35 // Set up the register classes.
36 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
40 // PowerPC has no intrinsics for these particular operations
41 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
45 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
49 // PowerPC has no SREM/UREM instructions
50 setOperationAction(ISD::SREM, MVT::i32, Expand);
51 setOperationAction(ISD::UREM, MVT::i32, Expand);
53 // We don't support sin/cos/sqrt/fmod
54 setOperationAction(ISD::FSIN , MVT::f64, Expand);
55 setOperationAction(ISD::FCOS , MVT::f64, Expand);
56 setOperationAction(ISD::FREM , MVT::f64, Expand);
57 setOperationAction(ISD::FSIN , MVT::f32, Expand);
58 setOperationAction(ISD::FCOS , MVT::f32, Expand);
59 setOperationAction(ISD::FREM , MVT::f32, Expand);
61 // If we're enabling GP optimizations, use hardware square root
62 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
63 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
67 // PowerPC does not have BSWAP, CTPOP or CTTZ
68 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
69 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
72 // PowerPC does not have ROTR
73 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
75 // PowerPC does not have Select
76 setOperationAction(ISD::SELECT, MVT::i32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f32, Expand);
78 setOperationAction(ISD::SELECT, MVT::f64, Expand);
80 // PowerPC wants to turn select_cc of FP into fsel when possible.
81 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
82 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
84 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
88 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
89 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
91 // PowerPC does not have [U|S]INT_TO_FP
92 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
93 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
95 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
96 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
98 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
101 // Support label based line numbers.
102 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
103 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
104 // FIXME - use subtarget debug flags
105 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
106 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
108 // We want to legalize GlobalAddress and ConstantPool nodes into the
109 // appropriate instructions to materialize the address.
110 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
111 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
113 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
114 setOperationAction(ISD::VASTART , MVT::Other, Custom);
116 // Use the default implementation.
117 setOperationAction(ISD::VAARG , MVT::Other, Expand);
118 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
119 setOperationAction(ISD::VAEND , MVT::Other, Expand);
120 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
121 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
122 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
124 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
125 // They also have instructions for converting between i64 and fp.
126 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
127 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
128 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
129 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
131 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
132 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
135 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
136 // 64 bit PowerPC implementations can support i64 types directly
137 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
138 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
139 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
141 // 32 bit PowerPC wants to expand i64 shifts itself.
142 setOperationAction(ISD::SHL, MVT::i64, Custom);
143 setOperationAction(ISD::SRL, MVT::i64, Custom);
144 setOperationAction(ISD::SRA, MVT::i64, Custom);
147 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
148 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
149 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
152 setSetCCResultContents(ZeroOrOneSetCCResult);
153 setSchedulingPreference(SchedulingForLatency);
154 setStackPointerRegisterToSaveRestore(PPC::R1);
156 computeRegisterProperties();
159 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
162 case PPCISD::FSEL: return "PPCISD::FSEL";
163 case PPCISD::FCFID: return "PPCISD::FCFID";
164 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
165 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
166 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
167 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
168 case PPCISD::Hi: return "PPCISD::Hi";
169 case PPCISD::Lo: return "PPCISD::Lo";
170 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
171 case PPCISD::SRL: return "PPCISD::SRL";
172 case PPCISD::SRA: return "PPCISD::SRA";
173 case PPCISD::SHL: return "PPCISD::SHL";
174 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
178 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
179 static bool isFloatingPointZero(SDOperand Op) {
180 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
181 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
182 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
183 // Maybe this has already been legalized into the constant pool?
184 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
185 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
186 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
191 /// LowerOperation - Provide custom lowering hooks for some operations.
193 SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
194 switch (Op.getOpcode()) {
195 default: assert(0 && "Wasn't expecting to be able to lower this!");
196 case ISD::FP_TO_SINT: {
197 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
198 SDOperand Src = Op.getOperand(0);
199 if (Src.getValueType() == MVT::f32)
200 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
203 switch (Op.getValueType()) {
204 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
206 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
209 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
213 // Convert the FP value to an int value through memory.
214 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
215 if (Op.getValueType() == MVT::i32)
216 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
219 case ISD::SINT_TO_FP: {
220 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
221 "Unhandled SINT_TO_FP type in custom expander!");
222 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
223 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
224 if (MVT::f32 == Op.getValueType())
225 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
228 case ISD::SELECT_CC: {
229 // Turn FP only select_cc's into fsel instructions.
230 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
231 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
234 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
236 // Cannot handle SETEQ/SETNE.
237 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
239 MVT::ValueType ResVT = Op.getValueType();
240 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
241 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
242 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
244 // If the RHS of the comparison is a 0.0, we don't need to do the
245 // subtraction at all.
246 if (isFloatingPointZero(RHS))
248 default: break; // SETUO etc aren't handled by fsel.
251 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
254 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
255 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
256 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
259 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
262 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
263 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
264 return DAG.getNode(PPCISD::FSEL, ResVT,
265 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
270 default: break; // SETUO etc aren't handled by fsel.
273 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
274 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
275 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
276 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
279 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
280 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
281 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
282 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
285 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
286 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
287 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
288 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
291 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
292 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
293 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
294 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
299 assert(Op.getValueType() == MVT::i64 &&
300 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
301 // The generic code does a fine job expanding shift by a constant.
302 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
304 // Otherwise, expand into a bunch of logical ops. Note that these ops
305 // depend on the PPC behavior for oversized shift amounts.
306 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
307 DAG.getConstant(0, MVT::i32));
308 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
309 DAG.getConstant(1, MVT::i32));
310 SDOperand Amt = Op.getOperand(1);
312 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
313 DAG.getConstant(32, MVT::i32), Amt);
314 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
315 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
316 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
317 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
318 DAG.getConstant(-32U, MVT::i32));
319 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
320 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
321 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
322 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
325 assert(Op.getValueType() == MVT::i64 &&
326 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
327 // The generic code does a fine job expanding shift by a constant.
328 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
330 // Otherwise, expand into a bunch of logical ops. Note that these ops
331 // depend on the PPC behavior for oversized shift amounts.
332 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
333 DAG.getConstant(0, MVT::i32));
334 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
335 DAG.getConstant(1, MVT::i32));
336 SDOperand Amt = Op.getOperand(1);
338 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
339 DAG.getConstant(32, MVT::i32), Amt);
340 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
341 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
342 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
343 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
344 DAG.getConstant(-32U, MVT::i32));
345 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
346 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
347 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
348 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
351 assert(Op.getValueType() == MVT::i64 &&
352 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
353 // The generic code does a fine job expanding shift by a constant.
354 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
356 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
358 DAG.getConstant(0, MVT::i32));
359 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
360 DAG.getConstant(1, MVT::i32));
361 SDOperand Amt = Op.getOperand(1);
363 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
364 DAG.getConstant(32, MVT::i32), Amt);
365 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
366 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
367 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
368 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
369 DAG.getConstant(-32U, MVT::i32));
370 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
371 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
372 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
373 Tmp4, Tmp6, ISD::SETLE);
374 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
376 case ISD::ConstantPool: {
377 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
378 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
379 SDOperand Zero = DAG.getConstant(0, MVT::i32);
381 if (PPCGenerateStaticCode) {
382 // Generate non-pic code that has direct accesses to the constant pool.
383 // The address of the global is just (hi(&g)+lo(&g)).
384 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
385 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
386 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
389 // Only lower ConstantPool on Darwin.
390 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
391 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
393 // With PIC, the first instruction is actually "GR+hi(&G)".
394 Hi = DAG.getNode(ISD::ADD, MVT::i32,
395 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
398 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
399 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
402 case ISD::GlobalAddress: {
403 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
404 GlobalValue *GV = GSDN->getGlobal();
405 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
406 SDOperand Zero = DAG.getConstant(0, MVT::i32);
408 if (PPCGenerateStaticCode) {
409 // Generate non-pic code that has direct accesses to globals.
410 // The address of the global is just (hi(&g)+lo(&g)).
411 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
412 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
413 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
416 // Only lower GlobalAddress on Darwin.
417 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
419 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
421 // With PIC, the first instruction is actually "GR+hi(&G)".
422 Hi = DAG.getNode(ISD::ADD, MVT::i32,
423 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
426 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
427 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
429 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
432 // If the global is weak or external, we have to go through the lazy
434 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
437 // vastart just stores the address of the VarArgsFrameIndex slot into the
438 // memory location argument.
439 // FIXME: Replace MVT::i32 with PointerTy
440 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
441 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
442 Op.getOperand(1), Op.getOperand(2));
448 std::vector<SDOperand>
449 PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
451 // add beautiful description of PPC stack frame format, or at least some docs
453 MachineFunction &MF = DAG.getMachineFunction();
454 MachineFrameInfo *MFI = MF.getFrameInfo();
455 MachineBasicBlock& BB = MF.front();
456 SSARegMap *RegMap = MF.getSSARegMap();
457 std::vector<SDOperand> ArgValues;
459 unsigned ArgOffset = 24;
460 unsigned GPR_remaining = 8;
461 unsigned FPR_remaining = 13;
462 unsigned GPR_idx = 0, FPR_idx = 0;
463 static const unsigned GPR[] = {
464 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
465 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
467 static const unsigned FPR[] = {
468 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
469 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
472 // Add DAG nodes to load the arguments... On entry to a function on PPC,
473 // the arguments start at offset 24, although they are likely to be passed
475 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
476 SDOperand newroot, argt;
478 bool needsLoad = false;
479 bool ArgLive = !I->use_empty();
480 MVT::ValueType ObjectVT = getValueType(I->getType());
483 default: assert(0 && "Unhandled argument type!");
490 if (GPR_remaining > 0) {
491 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
492 MF.addLiveIn(GPR[GPR_idx], VReg);
493 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
494 if (ObjectVT != MVT::i32) {
495 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
497 argt = DAG.getNode(AssertOp, MVT::i32, argt,
498 DAG.getValueType(ObjectVT));
499 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
508 if (GPR_remaining > 0) {
509 SDOperand argHi, argLo;
510 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
511 MF.addLiveIn(GPR[GPR_idx], VReg);
512 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
513 // If we have two or more remaining argument registers, then both halves
514 // of the i64 can be sourced from there. Otherwise, the lower half will
515 // have to come off the stack. This can happen when an i64 is preceded
516 // by 28 bytes of arguments.
517 if (GPR_remaining > 1) {
518 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
519 MF.addLiveIn(GPR[GPR_idx+1], VReg);
520 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
522 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
523 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
524 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
525 DAG.getSrcValue(NULL));
527 // Build the outgoing arg thingy
528 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
536 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
538 if (FPR_remaining > 0) {
544 if (FPR_remaining > 0) {
546 if (ObjectVT == MVT::f32)
547 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
549 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
550 MF.addLiveIn(FPR[FPR_idx], VReg);
551 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
560 // We need to load the argument to a virtual register if we determined above
561 // that we ran out of physical registers of the appropriate type
563 unsigned SubregOffset = 0;
564 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
565 if (ObjectVT == MVT::i16) SubregOffset = 2;
566 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
567 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
568 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
569 DAG.getConstant(SubregOffset, MVT::i32));
570 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
571 DAG.getSrcValue(NULL));
574 // Every 4 bytes of argument space consumes one of the GPRs available for
576 if (GPR_remaining > 0) {
577 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
578 GPR_remaining -= delta;
581 ArgOffset += ObjSize;
583 DAG.setRoot(newroot.getValue(1));
585 ArgValues.push_back(argt);
588 // If the function takes variable number of arguments, make a frame index for
589 // the start of the first vararg value... for expansion of llvm.va_start.
591 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
592 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
593 // If this function is vararg, store any remaining integer argument regs
594 // to their spots on the stack so that they may be loaded by deferencing the
595 // result of va_next.
596 std::vector<SDOperand> MemOps;
597 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
598 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
599 MF.addLiveIn(GPR[GPR_idx], VReg);
600 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
601 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
602 Val, FIN, DAG.getSrcValue(NULL));
603 MemOps.push_back(Store);
604 // Increment the address by four for the next argument to store
605 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
606 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
608 if (!MemOps.empty()) {
609 MemOps.push_back(DAG.getRoot());
610 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
614 // Finally, inform the code generator which regs we return values in.
615 switch (getValueType(F.getReturnType())) {
616 default: assert(0 && "Unknown type!");
617 case MVT::isVoid: break;
622 MF.addLiveOut(PPC::R3);
625 MF.addLiveOut(PPC::R3);
626 MF.addLiveOut(PPC::R4);
630 MF.addLiveOut(PPC::F1);
637 std::pair<SDOperand, SDOperand>
638 PPCTargetLowering::LowerCallTo(SDOperand Chain,
639 const Type *RetTy, bool isVarArg,
640 unsigned CallingConv, bool isTailCall,
641 SDOperand Callee, ArgListTy &Args,
643 // args_to_use will accumulate outgoing args for the ISD::CALL case in
644 // SelectExpr to use to put the arguments in the appropriate registers.
645 std::vector<SDOperand> args_to_use;
647 // Count how many bytes are to be pushed on the stack, including the linkage
648 // area, and parameter passing area.
649 unsigned NumBytes = 24;
652 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
653 DAG.getConstant(NumBytes, getPointerTy()));
655 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
656 switch (getValueType(Args[i].second)) {
657 default: assert(0 && "Unknown value type!");
672 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
673 // plus 32 bytes of argument space in case any called code gets funky on us.
674 // (Required by ABI to support var arg)
675 if (NumBytes < 56) NumBytes = 56;
677 // Adjust the stack pointer for the new arguments...
678 // These operations are automatically eliminated by the prolog/epilog pass
679 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
680 DAG.getConstant(NumBytes, getPointerTy()));
682 // Set up a copy of the stack pointer for use loading and storing any
683 // arguments that may not fit in the registers available for argument
685 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
687 // Figure out which arguments are going to go in registers, and which in
688 // memory. Also, if this is a vararg function, floating point operations
689 // must be stored to our stack, and loaded into integer regs as well, if
690 // any integer regs are available for argument passing.
691 unsigned ArgOffset = 24;
692 unsigned GPR_remaining = 8;
693 unsigned FPR_remaining = 13;
695 std::vector<SDOperand> MemOps;
696 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
697 // PtrOff will be used to store the current argument to the stack if a
698 // register cannot be found for it.
699 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
700 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
701 MVT::ValueType ArgVT = getValueType(Args[i].second);
704 default: assert(0 && "Unexpected ValueType for argument!");
708 // Promote the integer to 32 bits. If the input type is signed use a
709 // sign extend, otherwise use a zero extend.
710 if (Args[i].second->isSigned())
711 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
713 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
716 if (GPR_remaining > 0) {
717 args_to_use.push_back(Args[i].first);
720 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
721 Args[i].first, PtrOff,
722 DAG.getSrcValue(NULL)));
727 // If we have one free GPR left, we can place the upper half of the i64
728 // in it, and store the other half to the stack. If we have two or more
729 // free GPRs, then we can pass both halves of the i64 in registers.
730 if (GPR_remaining > 0) {
731 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
732 Args[i].first, DAG.getConstant(1, MVT::i32));
733 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
734 Args[i].first, DAG.getConstant(0, MVT::i32));
735 args_to_use.push_back(Hi);
737 if (GPR_remaining > 0) {
738 args_to_use.push_back(Lo);
741 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
742 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
743 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
744 Lo, PtrOff, DAG.getSrcValue(NULL)));
747 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
748 Args[i].first, PtrOff,
749 DAG.getSrcValue(NULL)));
755 if (FPR_remaining > 0) {
756 args_to_use.push_back(Args[i].first);
759 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
760 Args[i].first, PtrOff,
761 DAG.getSrcValue(NULL));
762 MemOps.push_back(Store);
763 // Float varargs are always shadowed in available integer registers
764 if (GPR_remaining > 0) {
765 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
766 DAG.getSrcValue(NULL));
767 MemOps.push_back(Load.getValue(1));
768 args_to_use.push_back(Load);
771 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
772 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
773 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
774 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
775 DAG.getSrcValue(NULL));
776 MemOps.push_back(Load.getValue(1));
777 args_to_use.push_back(Load);
781 // If we have any FPRs remaining, we may also have GPRs remaining.
782 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
784 if (GPR_remaining > 0) {
785 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
788 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
789 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
794 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
795 Args[i].first, PtrOff,
796 DAG.getSrcValue(NULL)));
798 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
803 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
806 std::vector<MVT::ValueType> RetVals;
807 MVT::ValueType RetTyVT = getValueType(RetTy);
808 MVT::ValueType ActualRetTyVT = RetTyVT;
809 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
810 ActualRetTyVT = MVT::i32; // Promote result to i32.
812 if (RetTyVT != MVT::isVoid)
813 RetVals.push_back(ActualRetTyVT);
814 RetVals.push_back(MVT::Other);
816 // If the callee is a GlobalAddress node (quite common, every direct call is)
817 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
818 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
819 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
821 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
822 Chain, Callee, args_to_use), 0);
823 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
824 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
825 DAG.getConstant(NumBytes, getPointerTy()));
826 SDOperand RetVal = TheCall;
828 // If the result is a small value, add a note so that we keep track of the
829 // information about whether it is sign or zero extended.
830 if (RetTyVT != ActualRetTyVT) {
831 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
832 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
833 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
836 return std::make_pair(RetVal, Chain);
839 SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
842 switch (Op.getValueType()) {
843 default: assert(0 && "Unknown type to return!");
845 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
849 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
852 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
853 DAG.getConstant(1, MVT::i32));
854 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
855 DAG.getConstant(0, MVT::i32));
856 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
857 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
860 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
863 std::pair<SDOperand, SDOperand> PPCTargetLowering::
864 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
866 assert(0 && "LowerFrameReturnAddress unimplemented");
871 PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
872 MachineBasicBlock *BB) {
873 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
874 MI->getOpcode() == PPC::SELECT_CC_F4 ||
875 MI->getOpcode() == PPC::SELECT_CC_F8) &&
876 "Unexpected instr type to insert");
878 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
879 // control-flow pattern. The incoming instruction knows the destination vreg
880 // to set, the condition code register to branch on, the true/false values to
881 // select between, and a branch opcode to use.
882 const BasicBlock *LLVM_BB = BB->getBasicBlock();
883 ilist<MachineBasicBlock>::iterator It = BB;
891 // fallthrough --> copy0MBB
892 MachineBasicBlock *thisMBB = BB;
893 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
894 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
895 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
896 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
897 MachineFunction *F = BB->getParent();
898 F->getBasicBlockList().insert(It, copy0MBB);
899 F->getBasicBlockList().insert(It, sinkMBB);
900 // Update machine-CFG edges
901 BB->addSuccessor(copy0MBB);
902 BB->addSuccessor(sinkMBB);
906 // # fallthrough to sinkMBB
909 // Update machine-CFG edges
910 BB->addSuccessor(sinkMBB);
913 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
916 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
917 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
918 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
920 delete MI; // The pseudo instruction is gone now.