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 // Use the default implementation.
114 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
115 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
118 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
119 // They also have instructions for converting between i64 and fp.
120 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
121 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
122 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
123 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
125 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
126 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
129 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
130 // 64 bit PowerPC implementations can support i64 types directly
131 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
132 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
133 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
135 // 32 bit PowerPC wants to expand i64 shifts itself.
136 setOperationAction(ISD::SHL, MVT::i64, Custom);
137 setOperationAction(ISD::SRL, MVT::i64, Custom);
138 setOperationAction(ISD::SRA, MVT::i64, Custom);
141 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
142 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
143 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
146 setSetCCResultContents(ZeroOrOneSetCCResult);
147 setSchedulingPreference(SchedulingForLatency);
148 setStackPointerRegisterToSaveRestore(PPC::R1);
150 computeRegisterProperties();
153 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
156 case PPCISD::FSEL: return "PPCISD::FSEL";
157 case PPCISD::FCFID: return "PPCISD::FCFID";
158 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
159 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
160 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
161 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
162 case PPCISD::Hi: return "PPCISD::Hi";
163 case PPCISD::Lo: return "PPCISD::Lo";
164 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
165 case PPCISD::SRL: return "PPCISD::SRL";
166 case PPCISD::SRA: return "PPCISD::SRA";
167 case PPCISD::SHL: return "PPCISD::SHL";
168 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
172 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
173 static bool isFloatingPointZero(SDOperand Op) {
174 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
175 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
176 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
177 // Maybe this has already been legalized into the constant pool?
178 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
179 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
180 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
185 /// LowerOperation - Provide custom lowering hooks for some operations.
187 SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
188 switch (Op.getOpcode()) {
189 default: assert(0 && "Wasn't expecting to be able to lower this!");
190 case ISD::FP_TO_SINT: {
191 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
192 SDOperand Src = Op.getOperand(0);
193 if (Src.getValueType() == MVT::f32)
194 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
197 switch (Op.getValueType()) {
198 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
200 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
203 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
207 // Convert the FP value to an int value through memory.
208 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
209 if (Op.getValueType() == MVT::i32)
210 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
213 case ISD::SINT_TO_FP: {
214 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
215 "Unhandled SINT_TO_FP type in custom expander!");
216 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
217 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
218 if (MVT::f32 == Op.getValueType())
219 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
222 case ISD::SELECT_CC: {
223 // Turn FP only select_cc's into fsel instructions.
224 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
225 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
228 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
230 // Cannot handle SETEQ/SETNE.
231 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
233 MVT::ValueType ResVT = Op.getValueType();
234 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
235 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
236 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
238 // If the RHS of the comparison is a 0.0, we don't need to do the
239 // subtraction at all.
240 if (isFloatingPointZero(RHS))
242 default: break; // SETUO etc aren't handled by fsel.
245 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
248 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
249 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
250 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
253 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
256 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
257 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
258 return DAG.getNode(PPCISD::FSEL, ResVT,
259 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
264 default: break; // SETUO etc aren't handled by fsel.
267 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
268 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
269 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
270 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
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, TV, FV);
279 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
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, FV, TV);
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, TV, FV);
293 assert(Op.getValueType() == MVT::i64 &&
294 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
295 // The generic code does a fine job expanding shift by a constant.
296 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
298 // Otherwise, expand into a bunch of logical ops. Note that these ops
299 // depend on the PPC behavior for oversized shift amounts.
300 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
301 DAG.getConstant(0, MVT::i32));
302 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
303 DAG.getConstant(1, MVT::i32));
304 SDOperand Amt = Op.getOperand(1);
306 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
307 DAG.getConstant(32, MVT::i32), Amt);
308 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
309 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
310 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
311 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
312 DAG.getConstant(-32U, MVT::i32));
313 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
314 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
315 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
316 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
319 assert(Op.getValueType() == MVT::i64 &&
320 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
321 // The generic code does a fine job expanding shift by a constant.
322 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
324 // Otherwise, expand into a bunch of logical ops. Note that these ops
325 // depend on the PPC behavior for oversized shift amounts.
326 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
327 DAG.getConstant(0, MVT::i32));
328 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
329 DAG.getConstant(1, MVT::i32));
330 SDOperand Amt = Op.getOperand(1);
332 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
333 DAG.getConstant(32, MVT::i32), Amt);
334 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
335 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
336 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
337 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
338 DAG.getConstant(-32U, MVT::i32));
339 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
340 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
341 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
342 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
345 assert(Op.getValueType() == MVT::i64 &&
346 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
347 // The generic code does a fine job expanding shift by a constant.
348 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
350 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
351 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
352 DAG.getConstant(0, MVT::i32));
353 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
354 DAG.getConstant(1, MVT::i32));
355 SDOperand Amt = Op.getOperand(1);
357 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
358 DAG.getConstant(32, MVT::i32), Amt);
359 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
360 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
361 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
362 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
363 DAG.getConstant(-32U, MVT::i32));
364 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
365 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
366 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
367 Tmp4, Tmp6, ISD::SETLE);
368 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
370 case ISD::ConstantPool: {
371 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
372 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
373 SDOperand Zero = DAG.getConstant(0, MVT::i32);
375 if (PPCGenerateStaticCode) {
376 // Generate non-pic code that has direct accesses to the constant pool.
377 // The address of the global is just (hi(&g)+lo(&g)).
378 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
379 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
380 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
383 // Only lower ConstantPool on Darwin.
384 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
385 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
387 // With PIC, the first instruction is actually "GR+hi(&G)".
388 Hi = DAG.getNode(ISD::ADD, MVT::i32,
389 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
392 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
393 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
396 case ISD::GlobalAddress: {
397 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
398 GlobalValue *GV = GSDN->getGlobal();
399 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
400 SDOperand Zero = DAG.getConstant(0, MVT::i32);
402 if (PPCGenerateStaticCode) {
403 // Generate non-pic code that has direct accesses to globals.
404 // The address of the global is just (hi(&g)+lo(&g)).
405 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
406 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
407 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
410 // Only lower GlobalAddress on Darwin.
411 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
413 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
415 // With PIC, the first instruction is actually "GR+hi(&G)".
416 Hi = DAG.getNode(ISD::ADD, MVT::i32,
417 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
420 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
421 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
423 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
426 // If the global is weak or external, we have to go through the lazy
428 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
434 std::vector<SDOperand>
435 PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
437 // add beautiful description of PPC stack frame format, or at least some docs
439 MachineFunction &MF = DAG.getMachineFunction();
440 MachineFrameInfo *MFI = MF.getFrameInfo();
441 MachineBasicBlock& BB = MF.front();
442 SSARegMap *RegMap = MF.getSSARegMap();
443 std::vector<SDOperand> ArgValues;
445 unsigned ArgOffset = 24;
446 unsigned GPR_remaining = 8;
447 unsigned FPR_remaining = 13;
448 unsigned GPR_idx = 0, FPR_idx = 0;
449 static const unsigned GPR[] = {
450 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
451 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
453 static const unsigned FPR[] = {
454 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
455 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
458 // Add DAG nodes to load the arguments... On entry to a function on PPC,
459 // the arguments start at offset 24, although they are likely to be passed
461 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
462 SDOperand newroot, argt;
464 bool needsLoad = false;
465 bool ArgLive = !I->use_empty();
466 MVT::ValueType ObjectVT = getValueType(I->getType());
469 default: assert(0 && "Unhandled argument type!");
476 if (GPR_remaining > 0) {
477 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
478 MF.addLiveIn(GPR[GPR_idx], VReg);
479 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
480 if (ObjectVT != MVT::i32) {
481 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
483 argt = DAG.getNode(AssertOp, MVT::i32, argt,
484 DAG.getValueType(ObjectVT));
485 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
494 if (GPR_remaining > 0) {
495 SDOperand argHi, argLo;
496 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
497 MF.addLiveIn(GPR[GPR_idx], VReg);
498 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
499 // If we have two or more remaining argument registers, then both halves
500 // of the i64 can be sourced from there. Otherwise, the lower half will
501 // have to come off the stack. This can happen when an i64 is preceded
502 // by 28 bytes of arguments.
503 if (GPR_remaining > 1) {
504 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
505 MF.addLiveIn(GPR[GPR_idx+1], VReg);
506 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
508 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
509 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
510 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
511 DAG.getSrcValue(NULL));
513 // Build the outgoing arg thingy
514 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
522 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
524 if (FPR_remaining > 0) {
530 if (FPR_remaining > 0) {
532 if (ObjectVT == MVT::f32)
533 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
535 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
536 MF.addLiveIn(FPR[FPR_idx], VReg);
537 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
546 // We need to load the argument to a virtual register if we determined above
547 // that we ran out of physical registers of the appropriate type
549 unsigned SubregOffset = 0;
550 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
551 if (ObjectVT == MVT::i16) SubregOffset = 2;
552 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
553 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
554 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
555 DAG.getConstant(SubregOffset, MVT::i32));
556 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
557 DAG.getSrcValue(NULL));
560 // Every 4 bytes of argument space consumes one of the GPRs available for
562 if (GPR_remaining > 0) {
563 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
564 GPR_remaining -= delta;
567 ArgOffset += ObjSize;
569 DAG.setRoot(newroot.getValue(1));
571 ArgValues.push_back(argt);
574 // If the function takes variable number of arguments, make a frame index for
575 // the start of the first vararg value... for expansion of llvm.va_start.
577 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
578 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
579 // If this function is vararg, store any remaining integer argument regs
580 // to their spots on the stack so that they may be loaded by deferencing the
581 // result of va_next.
582 std::vector<SDOperand> MemOps;
583 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
584 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
585 MF.addLiveIn(GPR[GPR_idx], VReg);
586 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
587 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
588 Val, FIN, DAG.getSrcValue(NULL));
589 MemOps.push_back(Store);
590 // Increment the address by four for the next argument to store
591 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
592 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
594 if (!MemOps.empty()) {
595 MemOps.push_back(DAG.getRoot());
596 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
600 // Finally, inform the code generator which regs we return values in.
601 switch (getValueType(F.getReturnType())) {
602 default: assert(0 && "Unknown type!");
603 case MVT::isVoid: break;
608 MF.addLiveOut(PPC::R3);
611 MF.addLiveOut(PPC::R3);
612 MF.addLiveOut(PPC::R4);
616 MF.addLiveOut(PPC::F1);
623 std::pair<SDOperand, SDOperand>
624 PPCTargetLowering::LowerCallTo(SDOperand Chain,
625 const Type *RetTy, bool isVarArg,
626 unsigned CallingConv, bool isTailCall,
627 SDOperand Callee, ArgListTy &Args,
629 // args_to_use will accumulate outgoing args for the ISD::CALL case in
630 // SelectExpr to use to put the arguments in the appropriate registers.
631 std::vector<SDOperand> args_to_use;
633 // Count how many bytes are to be pushed on the stack, including the linkage
634 // area, and parameter passing area.
635 unsigned NumBytes = 24;
638 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
639 DAG.getConstant(NumBytes, getPointerTy()));
641 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
642 switch (getValueType(Args[i].second)) {
643 default: assert(0 && "Unknown value type!");
658 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
659 // plus 32 bytes of argument space in case any called code gets funky on us.
660 // (Required by ABI to support var arg)
661 if (NumBytes < 56) NumBytes = 56;
663 // Adjust the stack pointer for the new arguments...
664 // These operations are automatically eliminated by the prolog/epilog pass
665 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
666 DAG.getConstant(NumBytes, getPointerTy()));
668 // Set up a copy of the stack pointer for use loading and storing any
669 // arguments that may not fit in the registers available for argument
671 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
673 // Figure out which arguments are going to go in registers, and which in
674 // memory. Also, if this is a vararg function, floating point operations
675 // must be stored to our stack, and loaded into integer regs as well, if
676 // any integer regs are available for argument passing.
677 unsigned ArgOffset = 24;
678 unsigned GPR_remaining = 8;
679 unsigned FPR_remaining = 13;
681 std::vector<SDOperand> MemOps;
682 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
683 // PtrOff will be used to store the current argument to the stack if a
684 // register cannot be found for it.
685 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
686 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
687 MVT::ValueType ArgVT = getValueType(Args[i].second);
690 default: assert(0 && "Unexpected ValueType for argument!");
694 // Promote the integer to 32 bits. If the input type is signed use a
695 // sign extend, otherwise use a zero extend.
696 if (Args[i].second->isSigned())
697 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
699 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
702 if (GPR_remaining > 0) {
703 args_to_use.push_back(Args[i].first);
706 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
707 Args[i].first, PtrOff,
708 DAG.getSrcValue(NULL)));
713 // If we have one free GPR left, we can place the upper half of the i64
714 // in it, and store the other half to the stack. If we have two or more
715 // free GPRs, then we can pass both halves of the i64 in registers.
716 if (GPR_remaining > 0) {
717 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
718 Args[i].first, DAG.getConstant(1, MVT::i32));
719 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
720 Args[i].first, DAG.getConstant(0, MVT::i32));
721 args_to_use.push_back(Hi);
723 if (GPR_remaining > 0) {
724 args_to_use.push_back(Lo);
727 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
728 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
729 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
730 Lo, PtrOff, DAG.getSrcValue(NULL)));
733 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
734 Args[i].first, PtrOff,
735 DAG.getSrcValue(NULL)));
741 if (FPR_remaining > 0) {
742 args_to_use.push_back(Args[i].first);
745 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
746 Args[i].first, PtrOff,
747 DAG.getSrcValue(NULL));
748 MemOps.push_back(Store);
749 // Float varargs are always shadowed in available integer registers
750 if (GPR_remaining > 0) {
751 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
752 DAG.getSrcValue(NULL));
753 MemOps.push_back(Load.getValue(1));
754 args_to_use.push_back(Load);
757 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
758 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
759 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
760 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
761 DAG.getSrcValue(NULL));
762 MemOps.push_back(Load.getValue(1));
763 args_to_use.push_back(Load);
767 // If we have any FPRs remaining, we may also have GPRs remaining.
768 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
770 if (GPR_remaining > 0) {
771 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
774 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
775 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
780 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
781 Args[i].first, PtrOff,
782 DAG.getSrcValue(NULL)));
784 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
789 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
792 std::vector<MVT::ValueType> RetVals;
793 MVT::ValueType RetTyVT = getValueType(RetTy);
794 MVT::ValueType ActualRetTyVT = RetTyVT;
795 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
796 ActualRetTyVT = MVT::i32; // Promote result to i32.
798 if (RetTyVT != MVT::isVoid)
799 RetVals.push_back(ActualRetTyVT);
800 RetVals.push_back(MVT::Other);
802 // If the callee is a GlobalAddress node (quite common, every direct call is)
803 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
804 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
805 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
807 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
808 Chain, Callee, args_to_use), 0);
809 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
810 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
811 DAG.getConstant(NumBytes, getPointerTy()));
812 SDOperand RetVal = TheCall;
814 // If the result is a small value, add a note so that we keep track of the
815 // information about whether it is sign or zero extended.
816 if (RetTyVT != ActualRetTyVT) {
817 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
818 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
819 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
822 return std::make_pair(RetVal, Chain);
825 SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
828 switch (Op.getValueType()) {
829 default: assert(0 && "Unknown type to return!");
831 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
835 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
838 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
839 DAG.getConstant(1, MVT::i32));
840 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
841 DAG.getConstant(0, MVT::i32));
842 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
843 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
846 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
849 SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
850 Value *VAListV, SelectionDAG &DAG) {
851 // vastart just stores the address of the VarArgsFrameIndex slot into the
852 // memory location argument.
853 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
854 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
855 DAG.getSrcValue(VAListV));
858 std::pair<SDOperand,SDOperand>
859 PPCTargetLowering::LowerVAArg(SDOperand Chain,
860 SDOperand VAListP, Value *VAListV,
861 const Type *ArgTy, SelectionDAG &DAG) {
862 MVT::ValueType ArgVT = getValueType(ArgTy);
865 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
866 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
868 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
871 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
872 "Other types should have been promoted for varargs!");
875 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
876 DAG.getConstant(Amt, VAList.getValueType()));
877 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
878 VAList, VAListP, DAG.getSrcValue(VAListV));
879 return std::make_pair(Result, Chain);
883 std::pair<SDOperand, SDOperand> PPCTargetLowering::
884 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
886 assert(0 && "LowerFrameReturnAddress unimplemented");
891 PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
892 MachineBasicBlock *BB) {
893 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
894 MI->getOpcode() == PPC::SELECT_CC_F4 ||
895 MI->getOpcode() == PPC::SELECT_CC_F8) &&
896 "Unexpected instr type to insert");
898 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
899 // control-flow pattern. The incoming instruction knows the destination vreg
900 // to set, the condition code register to branch on, the true/false values to
901 // select between, and a branch opcode to use.
902 const BasicBlock *LLVM_BB = BB->getBasicBlock();
903 ilist<MachineBasicBlock>::iterator It = BB;
911 // fallthrough --> copy0MBB
912 MachineBasicBlock *thisMBB = BB;
913 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
914 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
915 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
916 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
917 MachineFunction *F = BB->getParent();
918 F->getBasicBlockList().insert(It, copy0MBB);
919 F->getBasicBlockList().insert(It, sinkMBB);
920 // Update machine-CFG edges
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, PPC::PHI, 4, MI->getOperand(0).getReg())
937 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
938 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
940 delete MI; // The pseudo instruction is gone now.