Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelLowering.cpp - PPC32 DAG Lowering Implementation ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PPC32ISelLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PPC32ISelLowering.h" |
| 15 | #include "PPC32TargetMachine.h" |
| 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM) |
| 24 | : TargetLowering(TM) { |
| 25 | |
| 26 | // Fold away setcc operations if possible. |
| 27 | setSetCCIsExpensive(); |
| 28 | |
| 29 | // Set up the register classes. |
| 30 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
| 31 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
| 32 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
| 33 | |
| 34 | // PowerPC has no intrinsics for these particular operations |
| 35 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 36 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 37 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 38 | |
| 39 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 40 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 41 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
| 42 | |
| 43 | // PowerPC has no SREM/UREM instructions |
| 44 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 45 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 46 | |
| 47 | // We don't support sin/cos/sqrt/fmod |
| 48 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 49 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 50 | setOperationAction(ISD::SREM , MVT::f64, Expand); |
| 51 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 52 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 53 | setOperationAction(ISD::SREM , MVT::f32, Expand); |
| 54 | |
| 55 | // If we're enabling GP optimizations, use hardware square root |
| 56 | if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) { |
| 57 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 58 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 59 | } |
| 60 | |
| 61 | // PowerPC does not have CTPOP or CTTZ |
| 62 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 63 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 64 | |
| 65 | // PowerPC does not have Select |
| 66 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
| 67 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 68 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 70 | // PowerPC wants to turn select_cc of FP into fsel when possible. |
| 71 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 72 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 73 | |
| 74 | // PowerPC does not have BRCOND* which requires SetCC |
| 75 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| 76 | setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 77 | |
| 78 | // PowerPC does not have FP_TO_UINT |
| 79 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
| 80 | |
Jim Laskey | ad23c9d | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 81 | // PowerPC does not have [U|S]INT_TO_FP |
| 82 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); |
| 83 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 84 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 85 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 86 | |
| 87 | computeRegisterProperties(); |
| 88 | } |
| 89 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 90 | /// isFloatingPointZero - Return true if this is 0.0 or -0.0. |
| 91 | static bool isFloatingPointZero(SDOperand Op) { |
| 92 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
| 93 | return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0); |
| 94 | else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) { |
| 95 | // Maybe this has already been legalized into the constant pool? |
| 96 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) |
| 97 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get())) |
| 98 | return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0); |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 103 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 104 | /// |
| 105 | SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 106 | switch (Op.getOpcode()) { |
| 107 | default: assert(0 && "Wasn't expecting to be able to lower this!"); |
| 108 | case ISD::SELECT_CC: |
| 109 | // Turn FP only select_cc's into fsel instructions. |
| 110 | if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) && |
| 111 | MVT::isFloatingPoint(Op.getOperand(2).getValueType())) { |
| 112 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
| 113 | MVT::ValueType ResVT = Op.getValueType(); |
| 114 | MVT::ValueType CmpVT = Op.getOperand(0).getValueType(); |
| 115 | SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| 116 | SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3); |
| 117 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 118 | // If the RHS of the comparison is a 0.0, we don't need to do the |
| 119 | // subtraction at all. |
| 120 | if (isFloatingPointZero(RHS)) |
| 121 | switch (CC) { |
| 122 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 123 | case ISD::SETULT: |
| 124 | case ISD::SETLT: |
| 125 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 126 | case ISD::SETUGE: |
| 127 | case ISD::SETGE: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 128 | return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV); |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 129 | case ISD::SETUGT: |
| 130 | case ISD::SETGT: |
| 131 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 132 | case ISD::SETULE: |
| 133 | case ISD::SETLE: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 134 | return DAG.getNode(PPCISD::FSEL, ResVT, |
| 135 | DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV); |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 138 | switch (CC) { |
| 139 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 140 | case ISD::SETULT: |
| 141 | case ISD::SETLT: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 142 | return DAG.getNode(PPCISD::FSEL, ResVT, |
| 143 | DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 144 | case ISD::SETUGE: |
| 145 | case ISD::SETGE: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 146 | return DAG.getNode(PPCISD::FSEL, ResVT, |
| 147 | DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 148 | case ISD::SETUGT: |
| 149 | case ISD::SETGT: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 150 | return DAG.getNode(PPCISD::FSEL, ResVT, |
| 151 | DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 152 | case ISD::SETULE: |
| 153 | case ISD::SETLE: |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 154 | return DAG.getNode(PPCISD::FSEL, ResVT, |
| 155 | DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | return SDOperand(); |
| 161 | } |
| 162 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 163 | std::vector<SDOperand> |
| 164 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 165 | // |
| 166 | // add beautiful description of PPC stack frame format, or at least some docs |
| 167 | // |
| 168 | MachineFunction &MF = DAG.getMachineFunction(); |
| 169 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 170 | MachineBasicBlock& BB = MF.front(); |
| 171 | std::vector<SDOperand> ArgValues; |
| 172 | |
| 173 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
| 174 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 175 | // handle tracking them for us. |
| 176 | std::vector<unsigned> argVR, argPR, argOp; |
| 177 | unsigned ArgOffset = 24; |
| 178 | unsigned GPR_remaining = 8; |
| 179 | unsigned FPR_remaining = 13; |
| 180 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 181 | static const unsigned GPR[] = { |
| 182 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 183 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 184 | }; |
| 185 | static const unsigned FPR[] = { |
| 186 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 187 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 188 | }; |
| 189 | |
| 190 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 191 | // the arguments start at offset 24, although they are likely to be passed |
| 192 | // in registers. |
| 193 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 194 | SDOperand newroot, argt; |
| 195 | unsigned ObjSize; |
| 196 | bool needsLoad = false; |
| 197 | bool ArgLive = !I->use_empty(); |
| 198 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 199 | |
| 200 | switch (ObjectVT) { |
| 201 | default: assert(0 && "Unhandled argument type!"); |
| 202 | case MVT::i1: |
| 203 | case MVT::i8: |
| 204 | case MVT::i16: |
| 205 | case MVT::i32: |
| 206 | ObjSize = 4; |
| 207 | if (!ArgLive) break; |
| 208 | if (GPR_remaining > 0) { |
| 209 | MF.addLiveIn(GPR[GPR_idx]); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 210 | argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), |
| 211 | GPR[GPR_idx], MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 212 | if (ObjectVT != MVT::i32) |
| 213 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
| 214 | } else { |
| 215 | needsLoad = true; |
| 216 | } |
| 217 | break; |
| 218 | case MVT::i64: ObjSize = 8; |
| 219 | if (!ArgLive) break; |
| 220 | if (GPR_remaining > 0) { |
| 221 | SDOperand argHi, argLo; |
| 222 | MF.addLiveIn(GPR[GPR_idx]); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 223 | argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 224 | // If we have two or more remaining argument registers, then both halves |
| 225 | // of the i64 can be sourced from there. Otherwise, the lower half will |
| 226 | // have to come off the stack. This can happen when an i64 is preceded |
| 227 | // by 28 bytes of arguments. |
| 228 | if (GPR_remaining > 1) { |
| 229 | MF.addLiveIn(GPR[GPR_idx+1]); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 230 | argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 231 | } else { |
| 232 | int FI = MFI->CreateFixedObject(4, ArgOffset+4); |
| 233 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 234 | argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 235 | DAG.getSrcValue(NULL)); |
| 236 | } |
| 237 | // Build the outgoing arg thingy |
| 238 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 239 | newroot = argLo; |
| 240 | } else { |
| 241 | needsLoad = true; |
| 242 | } |
| 243 | break; |
| 244 | case MVT::f32: |
| 245 | case MVT::f64: |
| 246 | ObjSize = (ObjectVT == MVT::f64) ? 8 : 4; |
| 247 | if (!ArgLive) break; |
| 248 | if (FPR_remaining > 0) { |
| 249 | MF.addLiveIn(FPR[FPR_idx]); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 250 | argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), |
| 251 | FPR[FPR_idx], ObjectVT); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 252 | --FPR_remaining; |
| 253 | ++FPR_idx; |
| 254 | } else { |
| 255 | needsLoad = true; |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | // We need to load the argument to a virtual register if we determined above |
| 261 | // that we ran out of physical registers of the appropriate type |
| 262 | if (needsLoad) { |
| 263 | unsigned SubregOffset = 0; |
| 264 | if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3; |
| 265 | if (ObjectVT == MVT::i16) SubregOffset = 2; |
| 266 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 267 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 268 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, |
| 269 | DAG.getConstant(SubregOffset, MVT::i32)); |
| 270 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 271 | DAG.getSrcValue(NULL)); |
| 272 | } |
| 273 | |
| 274 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 275 | // argument passing. |
| 276 | if (GPR_remaining > 0) { |
| 277 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 278 | GPR_remaining -= delta; |
| 279 | GPR_idx += delta; |
| 280 | } |
| 281 | ArgOffset += ObjSize; |
| 282 | if (newroot.Val) |
| 283 | DAG.setRoot(newroot.getValue(1)); |
| 284 | |
| 285 | ArgValues.push_back(argt); |
| 286 | } |
| 287 | |
| 288 | // If the function takes variable number of arguments, make a frame index for |
| 289 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 290 | if (F.isVarArg()) { |
| 291 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 292 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 293 | // If this function is vararg, store any remaining integer argument regs |
| 294 | // to their spots on the stack so that they may be loaded by deferencing the |
| 295 | // result of va_next. |
| 296 | std::vector<SDOperand> MemOps; |
| 297 | for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) { |
| 298 | MF.addLiveIn(GPR[GPR_idx]); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 299 | SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 300 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
| 301 | Val, FIN, DAG.getSrcValue(NULL)); |
| 302 | MemOps.push_back(Store); |
| 303 | // Increment the address by four for the next argument to store |
| 304 | SDOperand PtrOff = DAG.getConstant(4, getPointerTy()); |
| 305 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff); |
| 306 | } |
| 307 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps)); |
| 308 | } |
| 309 | |
| 310 | // Finally, inform the code generator which regs we return values in. |
| 311 | switch (getValueType(F.getReturnType())) { |
| 312 | default: assert(0 && "Unknown type!"); |
| 313 | case MVT::isVoid: break; |
| 314 | case MVT::i1: |
| 315 | case MVT::i8: |
| 316 | case MVT::i16: |
| 317 | case MVT::i32: |
| 318 | MF.addLiveOut(PPC::R3); |
| 319 | break; |
| 320 | case MVT::i64: |
| 321 | MF.addLiveOut(PPC::R3); |
| 322 | MF.addLiveOut(PPC::R4); |
| 323 | break; |
| 324 | case MVT::f32: |
| 325 | case MVT::f64: |
| 326 | MF.addLiveOut(PPC::F1); |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | return ArgValues; |
| 331 | } |
| 332 | |
| 333 | std::pair<SDOperand, SDOperand> |
| 334 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
| 335 | const Type *RetTy, bool isVarArg, |
| 336 | unsigned CallingConv, bool isTailCall, |
| 337 | SDOperand Callee, ArgListTy &Args, |
| 338 | SelectionDAG &DAG) { |
| 339 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 340 | // SelectExpr to use to put the arguments in the appropriate registers. |
| 341 | std::vector<SDOperand> args_to_use; |
| 342 | |
| 343 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 344 | // area, and parameter passing area. |
| 345 | unsigned NumBytes = 24; |
| 346 | |
| 347 | if (Args.empty()) { |
| 348 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
| 349 | DAG.getConstant(NumBytes, getPointerTy())); |
| 350 | } else { |
| 351 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 352 | switch (getValueType(Args[i].second)) { |
| 353 | default: assert(0 && "Unknown value type!"); |
| 354 | case MVT::i1: |
| 355 | case MVT::i8: |
| 356 | case MVT::i16: |
| 357 | case MVT::i32: |
| 358 | case MVT::f32: |
| 359 | NumBytes += 4; |
| 360 | break; |
| 361 | case MVT::i64: |
| 362 | case MVT::f64: |
| 363 | NumBytes += 8; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
| 368 | // plus 32 bytes of argument space in case any called code gets funky on us. |
| 369 | // (Required by ABI to support var arg) |
| 370 | if (NumBytes < 56) NumBytes = 56; |
| 371 | |
| 372 | // Adjust the stack pointer for the new arguments... |
| 373 | // These operations are automatically eliminated by the prolog/epilog pass |
| 374 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
| 375 | DAG.getConstant(NumBytes, getPointerTy())); |
| 376 | |
| 377 | // Set up a copy of the stack pointer for use loading and storing any |
| 378 | // arguments that may not fit in the registers available for argument |
| 379 | // passing. |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 380 | SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(), |
| 381 | PPC::R1, MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 382 | |
| 383 | // Figure out which arguments are going to go in registers, and which in |
| 384 | // memory. Also, if this is a vararg function, floating point operations |
| 385 | // must be stored to our stack, and loaded into integer regs as well, if |
| 386 | // any integer regs are available for argument passing. |
| 387 | unsigned ArgOffset = 24; |
| 388 | unsigned GPR_remaining = 8; |
| 389 | unsigned FPR_remaining = 13; |
| 390 | |
| 391 | std::vector<SDOperand> MemOps; |
| 392 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 393 | // PtrOff will be used to store the current argument to the stack if a |
| 394 | // register cannot be found for it. |
| 395 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 396 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 397 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
| 398 | |
| 399 | switch (ArgVT) { |
| 400 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 401 | case MVT::i1: |
| 402 | case MVT::i8: |
| 403 | case MVT::i16: |
| 404 | // Promote the integer to 32 bits. If the input type is signed use a |
| 405 | // sign extend, otherwise use a zero extend. |
| 406 | if (Args[i].second->isSigned()) |
| 407 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 408 | else |
| 409 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 410 | // FALL THROUGH |
| 411 | case MVT::i32: |
| 412 | if (GPR_remaining > 0) { |
| 413 | args_to_use.push_back(Args[i].first); |
| 414 | --GPR_remaining; |
| 415 | } else { |
| 416 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 417 | Args[i].first, PtrOff, |
| 418 | DAG.getSrcValue(NULL))); |
| 419 | } |
| 420 | ArgOffset += 4; |
| 421 | break; |
| 422 | case MVT::i64: |
| 423 | // If we have one free GPR left, we can place the upper half of the i64 |
| 424 | // in it, and store the other half to the stack. If we have two or more |
| 425 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 426 | if (GPR_remaining > 0) { |
| 427 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 428 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 429 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 430 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 431 | args_to_use.push_back(Hi); |
| 432 | --GPR_remaining; |
| 433 | if (GPR_remaining > 0) { |
| 434 | args_to_use.push_back(Lo); |
| 435 | --GPR_remaining; |
| 436 | } else { |
| 437 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 438 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
| 439 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 440 | Lo, PtrOff, DAG.getSrcValue(NULL))); |
| 441 | } |
| 442 | } else { |
| 443 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 444 | Args[i].first, PtrOff, |
| 445 | DAG.getSrcValue(NULL))); |
| 446 | } |
| 447 | ArgOffset += 8; |
| 448 | break; |
| 449 | case MVT::f32: |
| 450 | case MVT::f64: |
| 451 | if (FPR_remaining > 0) { |
| 452 | args_to_use.push_back(Args[i].first); |
| 453 | --FPR_remaining; |
| 454 | if (isVarArg) { |
| 455 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 456 | Args[i].first, PtrOff, |
| 457 | DAG.getSrcValue(NULL)); |
| 458 | MemOps.push_back(Store); |
| 459 | // Float varargs are always shadowed in available integer registers |
| 460 | if (GPR_remaining > 0) { |
| 461 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 462 | DAG.getSrcValue(NULL)); |
| 463 | MemOps.push_back(Load); |
| 464 | args_to_use.push_back(Load); |
| 465 | --GPR_remaining; |
| 466 | } |
| 467 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 468 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 469 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
| 470 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 471 | DAG.getSrcValue(NULL)); |
| 472 | MemOps.push_back(Load); |
| 473 | args_to_use.push_back(Load); |
| 474 | --GPR_remaining; |
| 475 | } |
| 476 | } else { |
| 477 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 478 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 479 | // GPRs. |
| 480 | if (GPR_remaining > 0) { |
| 481 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 482 | --GPR_remaining; |
| 483 | } |
| 484 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 485 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 486 | --GPR_remaining; |
| 487 | } |
| 488 | } |
| 489 | } else { |
| 490 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 491 | Args[i].first, PtrOff, |
| 492 | DAG.getSrcValue(NULL))); |
| 493 | } |
| 494 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | if (!MemOps.empty()) |
| 499 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
| 500 | } |
| 501 | |
| 502 | std::vector<MVT::ValueType> RetVals; |
| 503 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 504 | if (RetTyVT != MVT::isVoid) |
| 505 | RetVals.push_back(RetTyVT); |
| 506 | RetVals.push_back(MVT::Other); |
| 507 | |
| 508 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
| 509 | Chain, Callee, args_to_use), 0); |
| 510 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
| 511 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
| 512 | DAG.getConstant(NumBytes, getPointerTy())); |
| 513 | return std::make_pair(TheCall, Chain); |
| 514 | } |
| 515 | |
| 516 | SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 517 | Value *VAListV, SelectionDAG &DAG) { |
| 518 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 519 | // memory location argument. |
| 520 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 521 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 522 | DAG.getSrcValue(VAListV)); |
| 523 | } |
| 524 | |
| 525 | std::pair<SDOperand,SDOperand> |
| 526 | PPC32TargetLowering::LowerVAArg(SDOperand Chain, |
| 527 | SDOperand VAListP, Value *VAListV, |
| 528 | const Type *ArgTy, SelectionDAG &DAG) { |
| 529 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 530 | |
| 531 | SDOperand VAList = |
| 532 | DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV)); |
| 533 | SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL)); |
| 534 | unsigned Amt; |
| 535 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 536 | Amt = 4; |
| 537 | else { |
| 538 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 539 | "Other types should have been promoted for varargs!"); |
| 540 | Amt = 8; |
| 541 | } |
| 542 | VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 543 | DAG.getConstant(Amt, VAList.getValueType())); |
| 544 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 545 | VAList, VAListP, DAG.getSrcValue(VAListV)); |
| 546 | return std::make_pair(Result, Chain); |
| 547 | } |
| 548 | |
| 549 | |
| 550 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 551 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 552 | SelectionDAG &DAG) { |
| 553 | assert(0 && "LowerFrameReturnAddress unimplemented"); |
| 554 | abort(); |
| 555 | } |