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