Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PowerPC.h" |
| 15 | #include "PowerPCInstrBuilder.h" |
| 16 | #include "PowerPCInstrInfo.h" |
| 17 | #include "PPC32RegisterInfo.h" |
| 18 | #include "llvm/Constants.h" // FIXME: REMOVE |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetLowering.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include <set> |
| 32 | #include <algorithm> |
| 33 | using namespace llvm; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface |
| 37 | namespace { |
| 38 | class PPC32TargetLowering : public TargetLowering { |
| 39 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
| 40 | int ReturnAddrIndex; // FrameIndex for return slot. |
| 41 | public: |
| 42 | PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 43 | // Set up the register classes. |
| 44 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 45 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 46 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
| 47 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 48 | // PowerPC has no intrinsics for these particular operations |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 49 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 50 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 51 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 52 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 53 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 54 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 55 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
| 56 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 57 | addLegalFPImmediate(+0.0); // Necessary for FSEL |
| 58 | addLegalFPImmediate(-0.0); // |
| 59 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 60 | computeRegisterProperties(); |
| 61 | } |
| 62 | |
| 63 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 64 | /// lower the arguments for the specified function, into the specified DAG. |
| 65 | virtual std::vector<SDOperand> |
| 66 | LowerArguments(Function &F, SelectionDAG &DAG); |
| 67 | |
| 68 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 69 | /// actual call. |
| 70 | virtual std::pair<SDOperand, SDOperand> |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 71 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, |
| 72 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 73 | |
| 74 | virtual std::pair<SDOperand, SDOperand> |
| 75 | LowerVAStart(SDOperand Chain, SelectionDAG &DAG); |
| 76 | |
| 77 | virtual std::pair<SDOperand,SDOperand> |
| 78 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 79 | const Type *ArgTy, SelectionDAG &DAG); |
| 80 | |
| 81 | virtual std::pair<SDOperand, SDOperand> |
| 82 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 83 | SelectionDAG &DAG); |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | std::vector<SDOperand> |
| 89 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 90 | // |
| 91 | // add beautiful description of PPC stack frame format, or at least some docs |
| 92 | // |
| 93 | MachineFunction &MF = DAG.getMachineFunction(); |
| 94 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 95 | MachineBasicBlock& BB = MF.front(); |
| 96 | std::vector<SDOperand> ArgValues; |
| 97 | |
| 98 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
| 99 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 100 | // handle tracking them for us. |
| 101 | std::vector<unsigned> argVR, argPR, argOp; |
| 102 | unsigned ArgOffset = 24; |
| 103 | unsigned GPR_remaining = 8; |
| 104 | unsigned FPR_remaining = 13; |
| 105 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 106 | static const unsigned GPR[] = { |
| 107 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 108 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 109 | }; |
| 110 | static const unsigned FPR[] = { |
| 111 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 112 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 113 | }; |
| 114 | |
| 115 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 116 | // the arguments start at offset 24, although they are likely to be passed |
| 117 | // in registers. |
| 118 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 119 | SDOperand newroot, argt; |
| 120 | unsigned ObjSize; |
| 121 | bool needsLoad = false; |
| 122 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 123 | |
| 124 | switch (ObjectVT) { |
| 125 | default: assert(0 && "Unhandled argument type!"); |
| 126 | case MVT::i1: |
| 127 | case MVT::i8: |
| 128 | case MVT::i16: |
| 129 | case MVT::i32: |
| 130 | ObjSize = 4; |
| 131 | if (GPR_remaining > 0) { |
| 132 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 133 | argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 134 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 135 | if (ObjectVT != MVT::i32) |
| 136 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 137 | } else { |
| 138 | needsLoad = true; |
| 139 | } |
| 140 | break; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 141 | case MVT::i64: ObjSize = 8; |
| 142 | // FIXME: can split 64b load between reg/mem if it is last arg in regs |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 143 | if (GPR_remaining > 1) { |
| 144 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 145 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 146 | // Copy the extracted halves into the virtual registers |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 147 | SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 148 | DAG.getRoot()); |
| 149 | SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 150 | // Build the outgoing arg thingy |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 151 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 152 | newroot = argLo; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 153 | } else { |
| 154 | needsLoad = true; |
| 155 | } |
| 156 | break; |
| 157 | case MVT::f32: ObjSize = 4; |
| 158 | case MVT::f64: ObjSize = 8; |
| 159 | if (FPR_remaining > 0) { |
| 160 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 161 | argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT, |
| 162 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 163 | --FPR_remaining; |
| 164 | ++FPR_idx; |
| 165 | } else { |
| 166 | needsLoad = true; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | // We need to load the argument to a virtual register if we determined above |
| 172 | // that we ran out of physical registers of the appropriate type |
| 173 | if (needsLoad) { |
| 174 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 175 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 176 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN); |
| 177 | } |
| 178 | |
| 179 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 180 | // argument passing. |
| 181 | if (GPR_remaining > 0) { |
| 182 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 183 | GPR_remaining -= delta; |
| 184 | GPR_idx += delta; |
| 185 | } |
| 186 | ArgOffset += ObjSize; |
| 187 | |
| 188 | DAG.setRoot(newroot.getValue(1)); |
| 189 | ArgValues.push_back(argt); |
| 190 | } |
| 191 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 192 | // If the function takes variable number of arguments, make a frame index for |
| 193 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 194 | if (F.isVarArg()) |
| 195 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 196 | |
| 197 | return ArgValues; |
| 198 | } |
| 199 | |
| 200 | std::pair<SDOperand, SDOperand> |
| 201 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 202 | const Type *RetTy, bool isVarArg, |
| 203 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) { |
| 204 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 205 | // SelectExpr to use to put the arguments in the appropriate registers. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 206 | std::vector<SDOperand> args_to_use; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 207 | |
| 208 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 209 | // area, and parameter passing area. |
| 210 | unsigned NumBytes = 24; |
| 211 | |
| 212 | if (Args.empty()) { |
| 213 | NumBytes = 0; // Save zero bytes. |
| 214 | } else { |
| 215 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 216 | switch (getValueType(Args[i].second)) { |
| 217 | default: assert(0 && "Unknown value type!"); |
| 218 | case MVT::i1: |
| 219 | case MVT::i8: |
| 220 | case MVT::i16: |
| 221 | case MVT::i32: |
| 222 | case MVT::f32: |
| 223 | NumBytes += 4; |
| 224 | break; |
| 225 | case MVT::i64: |
| 226 | case MVT::f64: |
| 227 | NumBytes += 8; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
| 232 | // plus 32 bytes of argument space in case any called code gets funky on us. |
| 233 | if (NumBytes < 56) NumBytes = 56; |
| 234 | |
| 235 | // Adjust the stack pointer for the new arguments... |
| 236 | // These operations are automatically eliminated by the prolog/epilog pass |
| 237 | Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain, |
| 238 | DAG.getConstant(NumBytes, getPointerTy())); |
| 239 | |
| 240 | // Set up a copy of the stack pointer for use loading and storing any |
| 241 | // arguments that may not fit in the registers available for argument |
| 242 | // passing. |
| 243 | SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32, |
| 244 | DAG.getEntryNode()); |
| 245 | |
| 246 | // Figure out which arguments are going to go in registers, and which in |
| 247 | // memory. Also, if this is a vararg function, floating point operations |
| 248 | // must be stored to our stack, and loaded into integer regs as well, if |
| 249 | // any integer regs are available for argument passing. |
| 250 | unsigned ArgOffset = 24; |
| 251 | unsigned GPR_remaining = 8; |
| 252 | unsigned FPR_remaining = 13; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 253 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 254 | static const unsigned GPR[] = { |
| 255 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 256 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 257 | }; |
| 258 | static const unsigned FPR[] = { |
| 259 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 260 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 261 | }; |
| 262 | |
| 263 | std::vector<SDOperand> MemOps; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 264 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 265 | // PtrOff will be used to store the current argument to the stack if a |
| 266 | // register cannot be found for it. |
| 267 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 268 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 269 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 270 | |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 271 | switch (ArgVT) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 272 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 273 | case MVT::i1: |
| 274 | case MVT::i8: |
| 275 | case MVT::i16: |
| 276 | // Promote the integer to 32 bits. If the input type is signed use a |
| 277 | // sign extend, otherwise use a zero extend. |
| 278 | if (Args[i].second->isSigned()) |
| 279 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 280 | else |
| 281 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 282 | // FALL THROUGH |
| 283 | case MVT::i32: |
| 284 | if (GPR_remaining > 0) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 285 | args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first, |
| 286 | GPR[GPR_idx])); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 287 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 288 | ++GPR_idx; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 289 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 290 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 291 | Args[i].first, PtrOff)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 292 | } |
| 293 | ArgOffset += 4; |
| 294 | break; |
| 295 | case MVT::i64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 296 | // If we have one free GPR left, we can place the upper half of the i64 |
| 297 | // in it, and store the other half to the stack. If we have two or more |
| 298 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 299 | if (GPR_remaining > 0) { |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 300 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 301 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 302 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 303 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 304 | args_to_use.push_back(DAG.getCopyToReg(Chain, Hi, GPR[GPR_idx])); |
| 305 | --GPR_remaining; |
| 306 | ++GPR_idx; |
| 307 | if (GPR_remaining > 0) { |
| 308 | args_to_use.push_back(DAG.getCopyToReg(Chain, Lo, GPR[GPR_idx])); |
| 309 | --GPR_remaining; |
| 310 | ++GPR_idx; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 311 | } else { |
| 312 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 313 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 314 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 315 | Lo, PtrOff)); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 316 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 317 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 318 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 319 | Args[i].first, PtrOff)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 320 | } |
| 321 | ArgOffset += 8; |
| 322 | break; |
| 323 | case MVT::f32: |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 324 | case MVT::f64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 325 | if (FPR_remaining > 0) { |
| 326 | if (isVarArg) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 327 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 328 | Args[i].first, PtrOff); |
| 329 | MemOps.push_back(Store); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 330 | // Float varargs are always shadowed in available integer registers |
| 331 | if (GPR_remaining > 0) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 332 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 333 | MemOps.push_back(Load); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 334 | args_to_use.push_back(DAG.getCopyToReg(Load, Load, |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 335 | GPR[GPR_idx])); |
| 336 | } |
| 337 | if (GPR_remaining > 1 && MVT::f64 == ArgVT) { |
| 338 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 339 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 340 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 341 | MemOps.push_back(Load); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 342 | args_to_use.push_back(DAG.getCopyToReg(Load, Load, |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 343 | GPR[GPR_idx+1])); |
| 344 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 345 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 346 | args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first, |
| 347 | FPR[FPR_idx])); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 348 | --FPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 349 | ++FPR_idx; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 350 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 351 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 352 | // GPRs. |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 353 | if (GPR_remaining > 0) { |
| 354 | --GPR_remaining; |
| 355 | ++GPR_idx; |
| 356 | } |
| 357 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 358 | --GPR_remaining; |
| 359 | ++GPR_idx; |
| 360 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 361 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 362 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 363 | Args[i].first, PtrOff)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 364 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 365 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 366 | break; |
| 367 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 368 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 369 | if (!MemOps.empty()) |
| 370 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | std::vector<MVT::ValueType> RetVals; |
| 374 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 375 | if (RetTyVT != MVT::isVoid) |
| 376 | RetVals.push_back(RetTyVT); |
| 377 | RetVals.push_back(MVT::Other); |
| 378 | |
| 379 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
| 380 | Chain, Callee, args_to_use), 0); |
| 381 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
| 382 | Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain, |
| 383 | DAG.getConstant(NumBytes, getPointerTy())); |
| 384 | return std::make_pair(TheCall, Chain); |
| 385 | } |
| 386 | |
| 387 | std::pair<SDOperand, SDOperand> |
| 388 | PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) { |
| 389 | //vastart just returns the address of the VarArgsFrameIndex slot. |
| 390 | return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain); |
| 391 | } |
| 392 | |
| 393 | std::pair<SDOperand,SDOperand> PPC32TargetLowering:: |
| 394 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 395 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 396 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 397 | SDOperand Result; |
| 398 | if (!isVANext) { |
| 399 | Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList); |
| 400 | } else { |
| 401 | unsigned Amt; |
| 402 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 403 | Amt = 4; |
| 404 | else { |
| 405 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 406 | "Other types should have been promoted for varargs!"); |
| 407 | Amt = 8; |
| 408 | } |
| 409 | Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 410 | DAG.getConstant(Amt, VAList.getValueType())); |
| 411 | } |
| 412 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
| 416 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 417 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 418 | SelectionDAG &DAG) { |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 419 | assert(0 && "LowerFrameReturnAddress unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 420 | abort(); |
| 421 | } |
| 422 | |
| 423 | namespace { |
| 424 | |
| 425 | //===--------------------------------------------------------------------===// |
| 426 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 427 | /// SelectionDAG operations. |
| 428 | //===--------------------------------------------------------------------===// |
| 429 | class ISel : public SelectionDAGISel { |
| 430 | |
| 431 | /// Comment Here. |
| 432 | PPC32TargetLowering PPC32Lowering; |
| 433 | |
| 434 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 435 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 436 | /// tree. |
| 437 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 438 | |
| 439 | unsigned GlobalBaseReg; |
| 440 | bool GlobalBaseInitialized; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 441 | |
| 442 | public: |
| 443 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) |
| 444 | {} |
| 445 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 446 | /// runOnFunction - Override this function in order to reset our per-function |
| 447 | /// variables. |
| 448 | virtual bool runOnFunction(Function &Fn) { |
| 449 | // Make sure we re-emit a set of the global base reg if necessary |
| 450 | GlobalBaseInitialized = false; |
| 451 | return SelectionDAGISel::runOnFunction(Fn); |
| 452 | } |
| 453 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 454 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 455 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 456 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 457 | DEBUG(BB->dump()); |
| 458 | // Codegen the basic block. |
| 459 | Select(DAG.getRoot()); |
| 460 | |
| 461 | // Clear state used for selection. |
| 462 | ExprMap.clear(); |
| 463 | } |
| 464 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 465 | unsigned getGlobalBaseReg(); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 466 | unsigned getConstDouble(double floatVal, unsigned Result); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 467 | unsigned SelectSetCR0(SDOperand CC); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 468 | unsigned SelectExpr(SDOperand N); |
| 469 | unsigned SelectExprFP(SDOperand N, unsigned Result); |
| 470 | void Select(SDOperand N); |
| 471 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 472 | bool SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 473 | void SelectBranchCC(SDOperand N); |
| 474 | }; |
| 475 | |
| 476 | /// canUseAsImmediateForOpcode - This method returns a value indicating whether |
| 477 | /// the ConstantSDNode N can be used as an immediate to Opcode. The return |
| 478 | /// values are either 0, 1 or 2. 0 indicates that either N is not a |
| 479 | /// ConstantSDNode, or is not suitable for use by that opcode. A return value |
| 480 | /// of 1 indicates that the constant may be used in normal immediate form. A |
| 481 | /// return value of 2 indicates that the constant may be used in shifted |
| 482 | /// immediate form. If the return value is nonzero, the constant value is |
| 483 | /// placed in Imm. |
| 484 | /// |
| 485 | static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode, |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 486 | unsigned& Imm, bool U = false) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 487 | if (N.getOpcode() != ISD::Constant) return 0; |
| 488 | |
| 489 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 490 | |
| 491 | switch(Opcode) { |
| 492 | default: return 0; |
| 493 | case ISD::ADD: |
| 494 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 495 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 496 | break; |
| 497 | case ISD::AND: |
| 498 | case ISD::XOR: |
| 499 | case ISD::OR: |
| 500 | if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; } |
| 501 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 502 | break; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 503 | case ISD::MUL: |
| 504 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 505 | break; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 506 | case ISD::SETCC: |
| 507 | if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; } |
| 508 | if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; } |
| 509 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 510 | } |
| 511 | return 0; |
| 512 | } |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 513 | |
| 514 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 515 | /// to Condition. If the Condition is unordered or unsigned, the bool argument |
| 516 | /// U is set to true, otherwise it is set to false. |
| 517 | static unsigned getBCCForSetCC(unsigned Condition, bool& U) { |
| 518 | U = false; |
| 519 | switch (Condition) { |
| 520 | default: assert(0 && "Unknown condition!"); abort(); |
| 521 | case ISD::SETEQ: return PPC::BEQ; |
| 522 | case ISD::SETNE: return PPC::BNE; |
| 523 | case ISD::SETULT: U = true; |
| 524 | case ISD::SETLT: return PPC::BLT; |
| 525 | case ISD::SETULE: U = true; |
| 526 | case ISD::SETLE: return PPC::BLE; |
| 527 | case ISD::SETUGT: U = true; |
| 528 | case ISD::SETGT: return PPC::BGT; |
| 529 | case ISD::SETUGE: U = true; |
| 530 | case ISD::SETGE: return PPC::BGE; |
| 531 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 536 | /// and store immediate instructions. |
| 537 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 538 | switch(Opcode) { |
| 539 | default: assert(0 && "Unknown opcode!"); abort(); |
| 540 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 541 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 542 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 543 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 544 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 545 | case PPC::LFD: return PPC::LFDX; |
| 546 | } |
| 547 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 548 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 551 | /// getGlobalBaseReg - Output the instructions required to put the |
| 552 | /// base address to use for accessing globals into a register. |
| 553 | /// |
| 554 | unsigned ISel::getGlobalBaseReg() { |
| 555 | if (!GlobalBaseInitialized) { |
| 556 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 557 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 558 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 559 | GlobalBaseReg = MakeReg(MVT::i32); |
| 560 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 561 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 562 | GlobalBaseInitialized = true; |
| 563 | } |
| 564 | return GlobalBaseReg; |
| 565 | } |
| 566 | |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 567 | /// getConstDouble - Loads a floating point value into a register, via the |
| 568 | /// Constant Pool. Optionally takes a register in which to load the value. |
| 569 | unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) { |
| 570 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 571 | if (0 == Result) Result = MakeReg(MVT::f64); |
| 572 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 573 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal); |
| 574 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 575 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 576 | .addConstantPoolIndex(CPI); |
| 577 | BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
| 578 | return Result; |
| 579 | } |
| 580 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 581 | unsigned ISel::SelectSetCR0(SDOperand CC) { |
| 582 | unsigned Opc, Tmp1, Tmp2; |
| 583 | static const unsigned CompareOpcodes[] = |
| 584 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
| 585 | |
| 586 | // If the first operand to the select is a SETCC node, then we can fold it |
| 587 | // into the branch that selects which value to return. |
| 588 | SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val); |
| 589 | if (SetCC && CC.getOpcode() == ISD::SETCC) { |
| 590 | bool U; |
| 591 | Opc = getBCCForSetCC(SetCC->getCondition(), U); |
| 592 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
| 593 | |
| 594 | // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC, |
| 595 | // so that it knows whether the SETCC immediate range is signed or not. |
| 596 | if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC, |
| 597 | Tmp2, U)) { |
| 598 | if (U) |
| 599 | BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2); |
| 600 | else |
| 601 | BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2); |
| 602 | } else { |
| 603 | bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); |
| 604 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
| 605 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
| 606 | BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2); |
| 607 | } |
| 608 | } else { |
| 609 | Tmp1 = SelectExpr(CC); |
| 610 | BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0); |
| 611 | Opc = PPC::BNE; |
| 612 | } |
| 613 | return Opc; |
| 614 | } |
| 615 | |
| 616 | /// Check to see if the load is a constant offset from a base register |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 617 | bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 618 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 619 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 620 | if (N.getOpcode() == ISD::ADD) { |
| 621 | Reg = SelectExpr(N.getOperand(0)); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 622 | if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 623 | offset = imm; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 624 | return false; |
| 625 | } |
| 626 | offset = SelectExpr(N.getOperand(1)); |
| 627 | return true; |
| 628 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 629 | Reg = SelectExpr(N); |
| 630 | offset = 0; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 631 | return false; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void ISel::SelectBranchCC(SDOperand N) |
| 635 | { |
| 636 | assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???"); |
| 637 | MachineBasicBlock *Dest = |
| 638 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 639 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 640 | Select(N.getOperand(0)); //chain |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 641 | unsigned Opc = SelectSetCR0(N.getOperand(1)); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 642 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
| 646 | unsigned ISel::SelectExprFP(SDOperand N, unsigned Result) |
| 647 | { |
| 648 | unsigned Tmp1, Tmp2, Tmp3; |
| 649 | unsigned Opc = 0; |
| 650 | SDNode *Node = N.Val; |
| 651 | MVT::ValueType DestType = N.getValueType(); |
| 652 | unsigned opcode = N.getOpcode(); |
| 653 | |
| 654 | switch (opcode) { |
| 655 | default: |
| 656 | Node->dump(); |
| 657 | assert(0 && "Node not handled!\n"); |
| 658 | |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 659 | case ISD::SELECT: { |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 660 | // Attempt to generate FSEL. We can do this whenever we have an FP result, |
| 661 | // and an FP comparison in the SetCC node. |
| 662 | SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val); |
| 663 | if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC && |
| 664 | !MVT::isInteger(SetCC->getOperand(0).getValueType()) && |
| 665 | SetCC->getCondition() != ISD::SETEQ && |
| 666 | SetCC->getCondition() != ISD::SETNE) { |
| 667 | MVT::ValueType VT = SetCC->getOperand(0).getValueType(); |
| 668 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 669 | unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE |
| 670 | unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE |
| 671 | |
| 672 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)); |
| 673 | if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) { |
| 674 | switch(SetCC->getCondition()) { |
| 675 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 676 | case ISD::SETULT: |
| 677 | case ISD::SETLT: |
| 678 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV); |
| 679 | return Result; |
| 680 | case ISD::SETUGE: |
| 681 | case ISD::SETGE: |
| 682 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV); |
| 683 | return Result; |
| 684 | case ISD::SETUGT: |
| 685 | case ISD::SETGT: { |
| 686 | Tmp2 = MakeReg(VT); |
| 687 | BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1); |
| 688 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV); |
| 689 | return Result; |
| 690 | } |
| 691 | case ISD::SETULE: |
| 692 | case ISD::SETLE: { |
| 693 | Tmp2 = MakeReg(VT); |
| 694 | BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1); |
| 695 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV); |
| 696 | return Result; |
| 697 | } |
| 698 | } |
| 699 | } else { |
| 700 | Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS; |
| 701 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
| 702 | Tmp3 = MakeReg(VT); |
| 703 | switch(SetCC->getCondition()) { |
| 704 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 705 | case ISD::SETULT: |
| 706 | case ISD::SETLT: |
| 707 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 708 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 709 | return Result; |
| 710 | case ISD::SETUGE: |
| 711 | case ISD::SETGE: |
| 712 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 713 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 714 | return Result; |
| 715 | case ISD::SETUGT: |
| 716 | case ISD::SETGT: |
| 717 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 718 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 719 | return Result; |
| 720 | case ISD::SETULE: |
| 721 | case ISD::SETLE: |
| 722 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 723 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 724 | return Result; |
| 725 | } |
| 726 | } |
| 727 | assert(0 && "Should never get here"); |
| 728 | return 0; |
| 729 | } |
| 730 | |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 731 | // Create an iterator with which to insert the MBB for copying the false |
| 732 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 733 | MachineBasicBlock *thisMBB = BB; |
| 734 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 735 | ilist<MachineBasicBlock>::iterator It = BB; |
| 736 | ++It; |
| 737 | |
| 738 | // thisMBB: |
| 739 | // ... |
| 740 | // TrueVal = ... |
| 741 | // cmpTY cr0, r1, r2 |
| 742 | // bCC copy1MBB |
| 743 | // fallthrough --> copy0MBB |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 744 | Tmp1 = SelectExpr(N.getOperand(0)); //Cond |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 745 | BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0); |
| 746 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 747 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 748 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
| 749 | BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
| 750 | MachineFunction *F = BB->getParent(); |
| 751 | F->getBasicBlockList().insert(It, copy0MBB); |
| 752 | F->getBasicBlockList().insert(It, sinkMBB); |
| 753 | // Update machine-CFG edges |
| 754 | BB->addSuccessor(copy0MBB); |
| 755 | BB->addSuccessor(sinkMBB); |
| 756 | |
| 757 | // copy0MBB: |
| 758 | // %FalseValue = ... |
| 759 | // # fallthrough to sinkMBB |
| 760 | BB = copy0MBB; |
| 761 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
| 762 | // Update machine-CFG edges |
| 763 | BB->addSuccessor(sinkMBB); |
| 764 | |
| 765 | // sinkMBB: |
| 766 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 767 | // ... |
| 768 | BB = sinkMBB; |
| 769 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 770 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 771 | return Result; |
| 772 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 773 | |
| 774 | case ISD::FP_ROUND: |
| 775 | assert (DestType == MVT::f32 && |
| 776 | N.getOperand(0).getValueType() == MVT::f64 && |
| 777 | "only f64 to f32 conversion supported here"); |
| 778 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 779 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 780 | return Result; |
| 781 | |
| 782 | case ISD::FP_EXTEND: |
| 783 | assert (DestType == MVT::f64 && |
| 784 | N.getOperand(0).getValueType() == MVT::f32 && |
| 785 | "only f32 to f64 conversion supported here"); |
| 786 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 787 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 788 | return Result; |
| 789 | |
| 790 | case ISD::CopyFromReg: |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 791 | if (Result == 1) |
| 792 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 793 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 794 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 795 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 796 | |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 797 | case ISD::ConstantFP: { |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 798 | ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 799 | Result = getConstDouble(CN->getValue(), Result); |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 800 | return Result; |
| 801 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 802 | |
| 803 | case ISD::MUL: |
| 804 | case ISD::ADD: |
| 805 | case ISD::SUB: |
| 806 | case ISD::SDIV: |
| 807 | switch( opcode ) { |
| 808 | case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break; |
| 809 | case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break; |
| 810 | case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break; |
| 811 | case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break; |
| 812 | }; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 813 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 814 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 815 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 816 | return Result; |
| 817 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 818 | case ISD::UINT_TO_FP: |
Nate Begeman | fdcf341 | 2005-03-30 19:38:35 +0000 | [diff] [blame] | 819 | case ISD::SINT_TO_FP: { |
| 820 | assert (N.getOperand(0).getValueType() == MVT::i32 |
| 821 | && "int to float must operate on i32"); |
| 822 | bool IsUnsigned = (ISD::UINT_TO_FP == opcode); |
| 823 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 824 | Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into |
| 825 | Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant |
| 826 | unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant |
| 827 | |
| 828 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 829 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 830 | |
| 831 | // FIXME: pull this FP constant generation stuff out into something like |
| 832 | // the simple ISel's getReg. |
| 833 | if (IsUnsigned) { |
| 834 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52); |
| 835 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 836 | // Load constant fp value |
| 837 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 838 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg()) |
| 839 | .addConstantPoolIndex(CPI); |
| 840 | BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4); |
| 841 | // Store the hi & low halves of the fp value, currently in int regs |
| 842 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 843 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 844 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4); |
| 845 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 846 | // Generate the return value with a subtract |
| 847 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 848 | } else { |
| 849 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52); |
| 850 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 851 | // Load constant fp value |
| 852 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 853 | unsigned TmpL = MakeReg(MVT::i32); |
| 854 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg()) |
| 855 | .addConstantPoolIndex(CPI); |
| 856 | BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4); |
| 857 | // Store the hi & low halves of the fp value, currently in int regs |
| 858 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 859 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 860 | BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000); |
| 861 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4); |
| 862 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 863 | // Generate the return value with a subtract |
| 864 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 865 | } |
| 866 | return Result; |
| 867 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 868 | } |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 869 | assert(0 && "Should never get here"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | unsigned ISel::SelectExpr(SDOperand N) { |
| 874 | unsigned Result; |
| 875 | unsigned Tmp1, Tmp2, Tmp3; |
| 876 | unsigned Opc = 0; |
| 877 | unsigned opcode = N.getOpcode(); |
| 878 | |
| 879 | SDNode *Node = N.Val; |
| 880 | MVT::ValueType DestType = N.getValueType(); |
| 881 | |
| 882 | unsigned &Reg = ExprMap[N]; |
| 883 | if (Reg) return Reg; |
| 884 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 885 | if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS && |
| 886 | N.getOpcode() != ISD::SUB_PARTS) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 887 | Reg = Result = (N.getValueType() != MVT::Other) ? |
| 888 | MakeReg(N.getValueType()) : 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 889 | else { |
| 890 | // If this is a call instruction, make sure to prepare ALL of the result |
| 891 | // values as well as the chain. |
| 892 | if (N.getOpcode() == ISD::CALL) { |
| 893 | if (Node->getNumValues() == 1) |
| 894 | Reg = Result = 1; // Void call, just a chain. |
| 895 | else { |
| 896 | Result = MakeReg(Node->getValueType(0)); |
| 897 | ExprMap[N.getValue(0)] = Result; |
| 898 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
| 899 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 900 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
| 901 | } |
| 902 | } else { |
| 903 | Result = MakeReg(Node->getValueType(0)); |
| 904 | ExprMap[N.getValue(0)] = Result; |
| 905 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 906 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (DestType == MVT::f64 || DestType == MVT::f32) |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 911 | if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode) |
| 912 | return SelectExprFP(N, Result); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 913 | |
| 914 | switch (opcode) { |
| 915 | default: |
| 916 | Node->dump(); |
| 917 | assert(0 && "Node not handled!\n"); |
| 918 | |
| 919 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 920 | // Generate both result values. FIXME: Need a better commment here? |
| 921 | if (Result != 1) |
| 922 | ExprMap[N.getValue(1)] = 1; |
| 923 | else |
| 924 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 925 | |
| 926 | // FIXME: We are currently ignoring the requested alignment for handling |
| 927 | // greater than the stack alignment. This will need to be revisited at some |
| 928 | // point. Align = N.getOperand(2); |
| 929 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 930 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 931 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 932 | << " the stack alignment yet!"; |
| 933 | abort(); |
| 934 | } |
| 935 | Select(N.getOperand(0)); |
| 936 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 937 | // Subtract size from stack pointer, thereby allocating some space. |
| 938 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 939 | // Put a pointer to the space into the result register by copying the SP |
| 940 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 941 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 942 | |
| 943 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 944 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 945 | Tmp2 = MakeReg(MVT::i32); |
| 946 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 947 | .addConstantPoolIndex(Tmp1); |
| 948 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 949 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 950 | |
| 951 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 952 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 953 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 954 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 955 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 956 | case ISD::GlobalAddress: { |
| 957 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 958 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 959 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 960 | .addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 961 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 962 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 963 | } else { |
| 964 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 965 | } |
| 966 | return Result; |
| 967 | } |
| 968 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 969 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 970 | case ISD::EXTLOAD: |
| 971 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 972 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 973 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
| 974 | Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 975 | bool sext = (ISD::SEXTLOAD == opcode); |
| 976 | bool byte = (MVT::i8 == TypeBeingLoaded); |
| 977 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 978 | // Make sure we generate both values. |
| 979 | if (Result != 1) |
| 980 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 981 | else |
| 982 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 983 | |
| 984 | SDOperand Chain = N.getOperand(0); |
| 985 | SDOperand Address = N.getOperand(1); |
| 986 | Select(Chain); |
| 987 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 988 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 989 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 990 | case MVT::i1: Opc = PPC::LBZ; break; |
| 991 | case MVT::i8: Opc = PPC::LBZ; break; |
| 992 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 993 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 994 | case MVT::f32: Opc = PPC::LFS; break; |
| 995 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 998 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
| 999 | Tmp1 = MakeReg(MVT::i32); |
| 1000 | int CPI = CP->getIndex(); |
| 1001 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1002 | .addConstantPoolIndex(CPI); |
| 1003 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1004 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1005 | else if(Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1006 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1007 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1008 | } else { |
| 1009 | int offset; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 1010 | bool idx = SelectAddr(Address, Tmp1, offset); |
| 1011 | if (idx) { |
| 1012 | Opc = IndexedOpForOp(Opc); |
| 1013 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
| 1014 | } else { |
| 1015 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 1016 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1017 | } |
| 1018 | return Result; |
| 1019 | } |
| 1020 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1021 | case ISD::CALL: { |
| 1022 | // Lower the chain for this call. |
| 1023 | Select(N.getOperand(0)); |
| 1024 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1025 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1026 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1027 | Select(N.getOperand(i)); |
| 1028 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1029 | // Emit the correct call instruction based on the type of symbol called. |
| 1030 | if (GlobalAddressSDNode *GASD = |
| 1031 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 1032 | BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true); |
| 1033 | } else if (ExternalSymbolSDNode *ESSDN = |
| 1034 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 1035 | BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 1036 | } else { |
| 1037 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1038 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 1039 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 1040 | BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12); |
| 1041 | } |
| 1042 | |
| 1043 | switch (Node->getValueType(0)) { |
| 1044 | default: assert(0 && "Unknown value type for call result!"); |
| 1045 | case MVT::Other: return 1; |
| 1046 | case MVT::i1: |
| 1047 | case MVT::i8: |
| 1048 | case MVT::i16: |
| 1049 | case MVT::i32: |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 1050 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1051 | if (Node->getValueType(1) == MVT::i32) |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 1052 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | case MVT::f32: |
| 1055 | case MVT::f64: |
| 1056 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1057 | break; |
| 1058 | } |
| 1059 | return Result+N.ResNo; |
| 1060 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1061 | |
| 1062 | case ISD::SIGN_EXTEND: |
| 1063 | case ISD::SIGN_EXTEND_INREG: |
| 1064 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1065 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 1066 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
| 1067 | case MVT::i16: |
| 1068 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
| 1069 | break; |
| 1070 | case MVT::i8: |
| 1071 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
| 1072 | break; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1073 | case MVT::i1: |
| 1074 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0); |
| 1075 | break; |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1076 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1077 | return Result; |
| 1078 | |
| 1079 | case ISD::ZERO_EXTEND_INREG: |
| 1080 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1081 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1082 | default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1083 | case MVT::i16: Tmp2 = 16; break; |
| 1084 | case MVT::i8: Tmp2 = 24; break; |
| 1085 | case MVT::i1: Tmp2 = 31; break; |
| 1086 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1087 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2) |
| 1088 | .addImm(31); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1089 | return Result; |
| 1090 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1091 | case ISD::CopyFromReg: |
| 1092 | if (Result == 1) |
| 1093 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1094 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 1095 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1096 | return Result; |
| 1097 | |
| 1098 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1099 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1100 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1101 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1102 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1103 | .addImm(31-Tmp2); |
| 1104 | } else { |
| 1105 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1106 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1107 | } |
| 1108 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1109 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1110 | case ISD::SRL: |
| 1111 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1112 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1113 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1114 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1115 | .addImm(Tmp2).addImm(31); |
| 1116 | } else { |
| 1117 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1118 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1119 | } |
| 1120 | return Result; |
| 1121 | |
| 1122 | case ISD::SRA: |
| 1123 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1124 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1125 | Tmp2 = CN->getValue() & 0x1F; |
| 1126 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1127 | } else { |
| 1128 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1129 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1130 | } |
| 1131 | return Result; |
| 1132 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1133 | case ISD::ADD: |
| 1134 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1135 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1136 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1137 | default: assert(0 && "unhandled result code"); |
| 1138 | case 0: // No immediate |
| 1139 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1140 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1141 | break; |
| 1142 | case 1: // Low immediate |
| 1143 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1144 | break; |
| 1145 | case 2: // Shifted immediate |
| 1146 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1147 | break; |
| 1148 | } |
| 1149 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1150 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1151 | case ISD::AND: |
| 1152 | case ISD::OR: |
| 1153 | case ISD::XOR: |
| 1154 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1155 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1156 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1157 | default: assert(0 && "unhandled result code"); |
| 1158 | case 0: // No immediate |
| 1159 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1160 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1161 | case ISD::AND: Opc = PPC::AND; break; |
| 1162 | case ISD::OR: Opc = PPC::OR; break; |
| 1163 | case ISD::XOR: Opc = PPC::XOR; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1164 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1165 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | case 1: // Low immediate |
| 1168 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1169 | case ISD::AND: Opc = PPC::ANDIo; break; |
| 1170 | case ISD::OR: Opc = PPC::ORI; break; |
| 1171 | case ISD::XOR: Opc = PPC::XORI; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1172 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1173 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | case 2: // Shifted immediate |
| 1176 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1177 | case ISD::AND: Opc = PPC::ANDISo; break; |
| 1178 | case ISD::OR: Opc = PPC::ORIS; break; |
| 1179 | case ISD::XOR: Opc = PPC::XORIS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1180 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1181 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1182 | break; |
| 1183 | } |
| 1184 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1185 | |
| 1186 | case ISD::SUB: |
| 1187 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1188 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1189 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1190 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1191 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1192 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1193 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1194 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1195 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1196 | if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) |
| 1197 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1198 | else { |
| 1199 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1200 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1201 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1202 | return Result; |
| 1203 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1204 | case ISD::SDIV: |
| 1205 | case ISD::UDIV: |
| 1206 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1207 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1208 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1209 | Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 1210 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1211 | return Result; |
| 1212 | |
| 1213 | case ISD::UREM: |
| 1214 | case ISD::SREM: { |
| 1215 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1216 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1217 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1218 | Tmp3 = MakeReg(MVT::i32); |
| 1219 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1220 | Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 1221 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 1222 | BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2); |
| 1223 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1); |
| 1224 | return Result; |
| 1225 | } |
| 1226 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1227 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1228 | case ISD::SUB_PARTS: { |
| 1229 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1230 | "Not an i64 add/sub!"); |
| 1231 | // Emit all of the operands. |
| 1232 | std::vector<unsigned> InVals; |
| 1233 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1234 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1235 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1236 | BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]); |
| 1237 | BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1238 | } else { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1239 | BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]); |
| 1240 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1241 | } |
| 1242 | return Result+N.ResNo; |
| 1243 | } |
| 1244 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1245 | case ISD::FP_TO_UINT: |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 1246 | case ISD::FP_TO_SINT: { |
| 1247 | bool U = (ISD::FP_TO_UINT == opcode); |
| 1248 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1249 | if (!U) { |
| 1250 | Tmp2 = MakeReg(MVT::f64); |
| 1251 | BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1); |
| 1252 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 1253 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx); |
| 1254 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4); |
| 1255 | return Result; |
| 1256 | } else { |
| 1257 | unsigned Zero = getConstDouble(0.0); |
| 1258 | unsigned MaxInt = getConstDouble((1LL << 32) - 1); |
| 1259 | unsigned Border = getConstDouble(1LL << 31); |
| 1260 | unsigned UseZero = MakeReg(MVT::f64); |
| 1261 | unsigned UseMaxInt = MakeReg(MVT::f64); |
| 1262 | unsigned UseChoice = MakeReg(MVT::f64); |
| 1263 | unsigned TmpReg = MakeReg(MVT::f64); |
| 1264 | unsigned TmpReg2 = MakeReg(MVT::f64); |
| 1265 | unsigned ConvReg = MakeReg(MVT::f64); |
| 1266 | unsigned IntTmp = MakeReg(MVT::i32); |
| 1267 | unsigned XorReg = MakeReg(MVT::i32); |
| 1268 | MachineFunction *F = BB->getParent(); |
| 1269 | int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8); |
| 1270 | // Update machine-CFG edges |
| 1271 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1272 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1273 | MachineBasicBlock *OldMBB = BB; |
| 1274 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1275 | F->getBasicBlockList().insert(It, XorMBB); |
| 1276 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1277 | BB->addSuccessor(XorMBB); |
| 1278 | BB->addSuccessor(PhiMBB); |
| 1279 | // Convert from floating point to unsigned 32-bit value |
| 1280 | // Use 0 if incoming value is < 0.0 |
| 1281 | BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero); |
| 1282 | // Use 2**32 - 1 if incoming value is >= 2**32 |
| 1283 | BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1); |
| 1284 | BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero) |
| 1285 | .addReg(MaxInt); |
| 1286 | // Subtract 2**31 |
| 1287 | BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
| 1288 | // Use difference if >= 2**31 |
| 1289 | BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border); |
| 1290 | BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
| 1291 | .addReg(UseChoice); |
| 1292 | // Convert to integer |
| 1293 | BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 1294 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx); |
| 1295 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4); |
| 1296 | BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 1297 | BuildMI(BB, PPC::B, 1).addMBB(XorMBB); |
| 1298 | |
| 1299 | // XorMBB: |
| 1300 | // add 2**31 if input was >= 2**31 |
| 1301 | BB = XorMBB; |
| 1302 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
| 1303 | XorMBB->addSuccessor(PhiMBB); |
| 1304 | |
| 1305 | // PhiMBB: |
| 1306 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 1307 | BB = PhiMBB; |
| 1308 | BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB) |
| 1309 | .addReg(XorReg).addMBB(XorMBB); |
| 1310 | return Result; |
| 1311 | } |
| 1312 | assert(0 && "Should never get here"); |
| 1313 | return 0; |
| 1314 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1315 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1316 | case ISD::SETCC: |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1317 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1318 | Opc = SelectSetCR0(N); |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Create an iterator with which to insert the MBB for copying the false |
| 1321 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1322 | MachineBasicBlock *thisMBB = BB; |
| 1323 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1324 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1325 | ++It; |
| 1326 | |
| 1327 | // thisMBB: |
| 1328 | // ... |
| 1329 | // cmpTY cr0, r1, r2 |
| 1330 | // %TrueValue = li 1 |
| 1331 | // bCC sinkMBB |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1332 | unsigned TrueValue = MakeReg(MVT::i32); |
| 1333 | BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1); |
| 1334 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1335 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1336 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
| 1337 | MachineFunction *F = BB->getParent(); |
| 1338 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1339 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1340 | // Update machine-CFG edges |
| 1341 | BB->addSuccessor(copy0MBB); |
| 1342 | BB->addSuccessor(sinkMBB); |
| 1343 | |
| 1344 | // copy0MBB: |
| 1345 | // %FalseValue = li 0 |
| 1346 | // fallthrough |
| 1347 | BB = copy0MBB; |
| 1348 | unsigned FalseValue = MakeReg(MVT::i32); |
| 1349 | BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0); |
| 1350 | // Update machine-CFG edges |
| 1351 | BB->addSuccessor(sinkMBB); |
| 1352 | |
| 1353 | // sinkMBB: |
| 1354 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1355 | // ... |
| 1356 | BB = sinkMBB; |
| 1357 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1358 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 1359 | return Result; |
| 1360 | } |
| 1361 | assert(0 && "Is this legal?"); |
| 1362 | return 0; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1363 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1364 | case ISD::SELECT: { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1365 | Opc = SelectSetCR0(N.getOperand(0)); |
| 1366 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1367 | // Create an iterator with which to insert the MBB for copying the false |
| 1368 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1369 | MachineBasicBlock *thisMBB = BB; |
| 1370 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1371 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1372 | ++It; |
| 1373 | |
| 1374 | // thisMBB: |
| 1375 | // ... |
| 1376 | // TrueVal = ... |
| 1377 | // cmpTY cr0, r1, r2 |
| 1378 | // bCC copy1MBB |
| 1379 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1380 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1381 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1382 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 1383 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1384 | MachineFunction *F = BB->getParent(); |
| 1385 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1386 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1387 | // Update machine-CFG edges |
| 1388 | BB->addSuccessor(copy0MBB); |
| 1389 | BB->addSuccessor(sinkMBB); |
| 1390 | |
| 1391 | // copy0MBB: |
| 1392 | // %FalseValue = ... |
| 1393 | // # fallthrough to sinkMBB |
| 1394 | BB = copy0MBB; |
| 1395 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
| 1396 | // Update machine-CFG edges |
| 1397 | BB->addSuccessor(sinkMBB); |
| 1398 | |
| 1399 | // sinkMBB: |
| 1400 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1401 | // ... |
| 1402 | BB = sinkMBB; |
| 1403 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1404 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 1405 | |
| 1406 | // FIXME: Select i64? |
| 1407 | return Result; |
| 1408 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1409 | |
| 1410 | case ISD::Constant: |
| 1411 | switch (N.getValueType()) { |
| 1412 | default: assert(0 && "Cannot use constants of this type!"); |
| 1413 | case MVT::i1: |
| 1414 | BuildMI(BB, PPC::LI, 1, Result) |
| 1415 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 1416 | break; |
| 1417 | case MVT::i32: |
| 1418 | { |
| 1419 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 1420 | if (v < 32768 && v >= -32768) { |
| 1421 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 1422 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1423 | Tmp1 = MakeReg(MVT::i32); |
| 1424 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 1425 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | return Result; |
| 1430 | } |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | void ISel::Select(SDOperand N) { |
| 1436 | unsigned Tmp1, Tmp2, Opc; |
| 1437 | unsigned opcode = N.getOpcode(); |
| 1438 | |
| 1439 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 1440 | return; // Already selected. |
| 1441 | |
| 1442 | SDNode *Node = N.Val; |
| 1443 | |
| 1444 | switch (Node->getOpcode()) { |
| 1445 | default: |
| 1446 | Node->dump(); std::cerr << "\n"; |
| 1447 | assert(0 && "Node not handled yet!"); |
| 1448 | case ISD::EntryToken: return; // Noop |
| 1449 | case ISD::TokenFactor: |
| 1450 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1451 | Select(Node->getOperand(i)); |
| 1452 | return; |
| 1453 | case ISD::ADJCALLSTACKDOWN: |
| 1454 | case ISD::ADJCALLSTACKUP: |
| 1455 | Select(N.getOperand(0)); |
| 1456 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 1457 | Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN : |
| 1458 | PPC::ADJCALLSTACKUP; |
| 1459 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 1460 | return; |
| 1461 | case ISD::BR: { |
| 1462 | MachineBasicBlock *Dest = |
| 1463 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1464 | Select(N.getOperand(0)); |
| 1465 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 1466 | return; |
| 1467 | } |
| 1468 | case ISD::BRCOND: |
| 1469 | SelectBranchCC(N); |
| 1470 | return; |
| 1471 | case ISD::CopyToReg: |
| 1472 | Select(N.getOperand(0)); |
| 1473 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1474 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
| 1475 | |
| 1476 | if (Tmp1 != Tmp2) { |
| 1477 | if (N.getOperand(1).getValueType() == MVT::f64 || |
| 1478 | N.getOperand(1).getValueType() == MVT::f32) |
| 1479 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 1480 | else |
| 1481 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1482 | } |
| 1483 | return; |
| 1484 | case ISD::ImplicitDef: |
| 1485 | Select(N.getOperand(0)); |
| 1486 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 1487 | return; |
| 1488 | case ISD::RET: |
| 1489 | switch (N.getNumOperands()) { |
| 1490 | default: |
| 1491 | assert(0 && "Unknown return instruction!"); |
| 1492 | case 3: |
| 1493 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1494 | N.getOperand(2).getValueType() == MVT::i32 && |
| 1495 | "Unknown two-register value!"); |
| 1496 | Select(N.getOperand(0)); |
| 1497 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1498 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 1499 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1500 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2); |
| 1501 | break; |
| 1502 | case 2: |
| 1503 | Select(N.getOperand(0)); |
| 1504 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1505 | switch (N.getOperand(1).getValueType()) { |
| 1506 | default: |
| 1507 | assert(0 && "Unknown return type!"); |
| 1508 | case MVT::f64: |
| 1509 | case MVT::f32: |
| 1510 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 1511 | break; |
| 1512 | case MVT::i32: |
| 1513 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1514 | break; |
| 1515 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1516 | case 1: |
| 1517 | Select(N.getOperand(0)); |
| 1518 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1519 | } |
| 1520 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 1521 | return; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1522 | case ISD::TRUNCSTORE: |
| 1523 | case ISD::STORE: |
| 1524 | { |
| 1525 | SDOperand Chain = N.getOperand(0); |
| 1526 | SDOperand Value = N.getOperand(1); |
| 1527 | SDOperand Address = N.getOperand(2); |
| 1528 | Select(Chain); |
| 1529 | |
| 1530 | Tmp1 = SelectExpr(Value); //value |
| 1531 | |
| 1532 | if (opcode == ISD::STORE) { |
| 1533 | switch(Value.getValueType()) { |
| 1534 | default: assert(0 && "unknown Type in store"); |
| 1535 | case MVT::i32: Opc = PPC::STW; break; |
| 1536 | case MVT::f64: Opc = PPC::STFD; break; |
| 1537 | case MVT::f32: Opc = PPC::STFS; break; |
| 1538 | } |
| 1539 | } else { //ISD::TRUNCSTORE |
| 1540 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 1541 | default: assert(0 && "unknown Type in store"); |
| 1542 | case MVT::i1: //FIXME: DAG does not promote this load |
| 1543 | case MVT::i8: Opc = PPC::STB; break; |
| 1544 | case MVT::i16: Opc = PPC::STH; break; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | if (Address.getOpcode() == ISD::GlobalAddress) |
| 1549 | { |
| 1550 | BuildMI(BB, Opc, 2).addReg(Tmp1) |
| 1551 | .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal()); |
| 1552 | } |
| 1553 | else if(Address.getOpcode() == ISD::FrameIndex) |
| 1554 | { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1555 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1556 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1557 | } |
| 1558 | else |
| 1559 | { |
| 1560 | int offset; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame^] | 1561 | bool idx = SelectAddr(Address, Tmp2, offset); |
| 1562 | if (idx) { |
| 1563 | Opc = IndexedOpForOp(Opc); |
| 1564 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
| 1565 | } else { |
| 1566 | BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2); |
| 1567 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1568 | } |
| 1569 | return; |
| 1570 | } |
| 1571 | case ISD::EXTLOAD: |
| 1572 | case ISD::SEXTLOAD: |
| 1573 | case ISD::ZEXTLOAD: |
| 1574 | case ISD::LOAD: |
| 1575 | case ISD::CopyFromReg: |
| 1576 | case ISD::CALL: |
| 1577 | case ISD::DYNAMIC_STACKALLOC: |
| 1578 | ExprMap.erase(N); |
| 1579 | SelectExpr(N); |
| 1580 | return; |
| 1581 | } |
| 1582 | assert(0 && "Should not be reached!"); |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1587 | /// into a machine code representation using pattern matching and a machine |
| 1588 | /// description file. |
| 1589 | /// |
| 1590 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
| 1591 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |