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 |
| 734 | |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 735 | // Create an iterator with which to insert the MBB for copying the false |
| 736 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 737 | MachineBasicBlock *thisMBB = BB; |
| 738 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 739 | ilist<MachineBasicBlock>::iterator It = BB; |
| 740 | ++It; |
| 741 | |
| 742 | // thisMBB: |
| 743 | // ... |
| 744 | // TrueVal = ... |
| 745 | // cmpTY cr0, r1, r2 |
| 746 | // bCC copy1MBB |
| 747 | // fallthrough --> copy0MBB |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 748 | Tmp1 = SelectExpr(N.getOperand(0)); //Cond |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 749 | BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0); |
| 750 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 751 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 752 | BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
| 753 | MachineFunction *F = BB->getParent(); |
| 754 | F->getBasicBlockList().insert(It, copy0MBB); |
| 755 | F->getBasicBlockList().insert(It, sinkMBB); |
| 756 | // Update machine-CFG edges |
| 757 | BB->addSuccessor(copy0MBB); |
| 758 | BB->addSuccessor(sinkMBB); |
| 759 | |
| 760 | // copy0MBB: |
| 761 | // %FalseValue = ... |
| 762 | // # fallthrough to sinkMBB |
| 763 | BB = copy0MBB; |
Nate Begeman | 23afcfb | 2005-03-29 22:48:55 +0000 | [diff] [blame] | 764 | // Update machine-CFG edges |
| 765 | BB->addSuccessor(sinkMBB); |
| 766 | |
| 767 | // sinkMBB: |
| 768 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 769 | // ... |
| 770 | BB = sinkMBB; |
| 771 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 772 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 773 | return Result; |
| 774 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 775 | |
| 776 | case ISD::FP_ROUND: |
| 777 | assert (DestType == MVT::f32 && |
| 778 | N.getOperand(0).getValueType() == MVT::f64 && |
| 779 | "only f64 to f32 conversion supported here"); |
| 780 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 781 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 782 | return Result; |
| 783 | |
| 784 | case ISD::FP_EXTEND: |
| 785 | assert (DestType == MVT::f64 && |
| 786 | N.getOperand(0).getValueType() == MVT::f32 && |
| 787 | "only f32 to f64 conversion supported here"); |
| 788 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 789 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 790 | return Result; |
| 791 | |
| 792 | case ISD::CopyFromReg: |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 793 | if (Result == 1) |
| 794 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 795 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 796 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 797 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 798 | |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 799 | case ISD::ConstantFP: { |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 800 | ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 801 | Result = getConstDouble(CN->getValue(), Result); |
Nate Begeman | 6d369cc | 2005-04-01 01:08:07 +0000 | [diff] [blame] | 802 | return Result; |
| 803 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 804 | |
| 805 | case ISD::MUL: |
| 806 | case ISD::ADD: |
| 807 | case ISD::SUB: |
| 808 | case ISD::SDIV: |
| 809 | switch( opcode ) { |
| 810 | case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break; |
| 811 | case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break; |
| 812 | case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break; |
| 813 | case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break; |
| 814 | }; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 815 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 816 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 817 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 818 | return Result; |
| 819 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 820 | case ISD::UINT_TO_FP: |
Nate Begeman | fdcf341 | 2005-03-30 19:38:35 +0000 | [diff] [blame] | 821 | case ISD::SINT_TO_FP: { |
| 822 | assert (N.getOperand(0).getValueType() == MVT::i32 |
| 823 | && "int to float must operate on i32"); |
| 824 | bool IsUnsigned = (ISD::UINT_TO_FP == opcode); |
| 825 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 826 | Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into |
| 827 | Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant |
| 828 | unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant |
| 829 | |
| 830 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 831 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 832 | |
| 833 | // FIXME: pull this FP constant generation stuff out into something like |
| 834 | // the simple ISel's getReg. |
| 835 | if (IsUnsigned) { |
| 836 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52); |
| 837 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 838 | // Load constant fp value |
| 839 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 840 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg()) |
| 841 | .addConstantPoolIndex(CPI); |
| 842 | BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4); |
| 843 | // Store the hi & low halves of the fp value, currently in int regs |
| 844 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 845 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 846 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4); |
| 847 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 848 | // Generate the return value with a subtract |
| 849 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 850 | } else { |
| 851 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52); |
| 852 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 853 | // Load constant fp value |
| 854 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 855 | unsigned TmpL = MakeReg(MVT::i32); |
| 856 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg()) |
| 857 | .addConstantPoolIndex(CPI); |
| 858 | BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4); |
| 859 | // Store the hi & low halves of the fp value, currently in int regs |
| 860 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 861 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 862 | BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000); |
| 863 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4); |
| 864 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 865 | // Generate the return value with a subtract |
| 866 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 867 | } |
| 868 | return Result; |
| 869 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 870 | } |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 871 | assert(0 && "Should never get here"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | unsigned ISel::SelectExpr(SDOperand N) { |
| 876 | unsigned Result; |
| 877 | unsigned Tmp1, Tmp2, Tmp3; |
| 878 | unsigned Opc = 0; |
| 879 | unsigned opcode = N.getOpcode(); |
| 880 | |
| 881 | SDNode *Node = N.Val; |
| 882 | MVT::ValueType DestType = N.getValueType(); |
| 883 | |
| 884 | unsigned &Reg = ExprMap[N]; |
| 885 | if (Reg) return Reg; |
| 886 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 887 | if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS && |
| 888 | N.getOpcode() != ISD::SUB_PARTS) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 889 | Reg = Result = (N.getValueType() != MVT::Other) ? |
| 890 | MakeReg(N.getValueType()) : 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 891 | else { |
| 892 | // If this is a call instruction, make sure to prepare ALL of the result |
| 893 | // values as well as the chain. |
| 894 | if (N.getOpcode() == ISD::CALL) { |
| 895 | if (Node->getNumValues() == 1) |
| 896 | Reg = Result = 1; // Void call, just a chain. |
| 897 | else { |
| 898 | Result = MakeReg(Node->getValueType(0)); |
| 899 | ExprMap[N.getValue(0)] = Result; |
| 900 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
| 901 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 902 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
| 903 | } |
| 904 | } else { |
| 905 | Result = MakeReg(Node->getValueType(0)); |
| 906 | ExprMap[N.getValue(0)] = Result; |
| 907 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 908 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (DestType == MVT::f64 || DestType == MVT::f32) |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 913 | if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode) |
| 914 | return SelectExprFP(N, Result); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 915 | |
| 916 | switch (opcode) { |
| 917 | default: |
| 918 | Node->dump(); |
| 919 | assert(0 && "Node not handled!\n"); |
| 920 | |
| 921 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 922 | // Generate both result values. FIXME: Need a better commment here? |
| 923 | if (Result != 1) |
| 924 | ExprMap[N.getValue(1)] = 1; |
| 925 | else |
| 926 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 927 | |
| 928 | // FIXME: We are currently ignoring the requested alignment for handling |
| 929 | // greater than the stack alignment. This will need to be revisited at some |
| 930 | // point. Align = N.getOperand(2); |
| 931 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 932 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 933 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 934 | << " the stack alignment yet!"; |
| 935 | abort(); |
| 936 | } |
| 937 | Select(N.getOperand(0)); |
| 938 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 939 | // Subtract size from stack pointer, thereby allocating some space. |
| 940 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 941 | // Put a pointer to the space into the result register by copying the SP |
| 942 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 943 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 944 | |
| 945 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 946 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 947 | Tmp2 = MakeReg(MVT::i32); |
| 948 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 949 | .addConstantPoolIndex(Tmp1); |
| 950 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 951 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 952 | |
| 953 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 954 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 955 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 956 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 957 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 958 | case ISD::GlobalAddress: { |
| 959 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 960 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 961 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 962 | .addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 963 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 964 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 965 | } else { |
| 966 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 967 | } |
| 968 | return Result; |
| 969 | } |
| 970 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 971 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 972 | case ISD::EXTLOAD: |
| 973 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 974 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 975 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
| 976 | Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 977 | bool sext = (ISD::SEXTLOAD == opcode); |
| 978 | bool byte = (MVT::i8 == TypeBeingLoaded); |
| 979 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 980 | // Make sure we generate both values. |
| 981 | if (Result != 1) |
| 982 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 983 | else |
| 984 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 985 | |
| 986 | SDOperand Chain = N.getOperand(0); |
| 987 | SDOperand Address = N.getOperand(1); |
| 988 | Select(Chain); |
| 989 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 990 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 991 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 992 | case MVT::i1: Opc = PPC::LBZ; break; |
| 993 | case MVT::i8: Opc = PPC::LBZ; break; |
| 994 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 995 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 996 | case MVT::f32: Opc = PPC::LFS; break; |
| 997 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1000 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
| 1001 | Tmp1 = MakeReg(MVT::i32); |
| 1002 | int CPI = CP->getIndex(); |
| 1003 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1004 | .addConstantPoolIndex(CPI); |
| 1005 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1006 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1007 | else if(Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1008 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1009 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1010 | } else { |
| 1011 | int offset; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1012 | bool idx = SelectAddr(Address, Tmp1, offset); |
| 1013 | if (idx) { |
| 1014 | Opc = IndexedOpForOp(Opc); |
| 1015 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
| 1016 | } else { |
| 1017 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 1018 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1019 | } |
| 1020 | return Result; |
| 1021 | } |
| 1022 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1023 | case ISD::CALL: { |
| 1024 | // Lower the chain for this call. |
| 1025 | Select(N.getOperand(0)); |
| 1026 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1027 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1028 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1029 | Select(N.getOperand(i)); |
| 1030 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1031 | // Emit the correct call instruction based on the type of symbol called. |
| 1032 | if (GlobalAddressSDNode *GASD = |
| 1033 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 1034 | BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true); |
| 1035 | } else if (ExternalSymbolSDNode *ESSDN = |
| 1036 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 1037 | BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 1038 | } else { |
| 1039 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1040 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 1041 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 1042 | BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12); |
| 1043 | } |
| 1044 | |
| 1045 | switch (Node->getValueType(0)) { |
| 1046 | default: assert(0 && "Unknown value type for call result!"); |
| 1047 | case MVT::Other: return 1; |
| 1048 | case MVT::i1: |
| 1049 | case MVT::i8: |
| 1050 | case MVT::i16: |
| 1051 | case MVT::i32: |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 1052 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1053 | if (Node->getValueType(1) == MVT::i32) |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 1054 | 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] | 1055 | break; |
| 1056 | case MVT::f32: |
| 1057 | case MVT::f64: |
| 1058 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1059 | break; |
| 1060 | } |
| 1061 | return Result+N.ResNo; |
| 1062 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1063 | |
| 1064 | case ISD::SIGN_EXTEND: |
| 1065 | case ISD::SIGN_EXTEND_INREG: |
| 1066 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1067 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 1068 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
| 1069 | case MVT::i16: |
| 1070 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
| 1071 | break; |
| 1072 | case MVT::i8: |
| 1073 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
| 1074 | break; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1075 | case MVT::i1: |
| 1076 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0); |
| 1077 | break; |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1078 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1079 | return Result; |
| 1080 | |
| 1081 | case ISD::ZERO_EXTEND_INREG: |
| 1082 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1083 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1084 | default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1085 | case MVT::i16: Tmp2 = 16; break; |
| 1086 | case MVT::i8: Tmp2 = 24; break; |
| 1087 | case MVT::i1: Tmp2 = 31; break; |
| 1088 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1089 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2) |
| 1090 | .addImm(31); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1091 | return Result; |
| 1092 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1093 | case ISD::CopyFromReg: |
| 1094 | if (Result == 1) |
| 1095 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1096 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 1097 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1098 | return Result; |
| 1099 | |
| 1100 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1101 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1102 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1103 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1104 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1105 | .addImm(31-Tmp2); |
| 1106 | } else { |
| 1107 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1108 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1109 | } |
| 1110 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1111 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1112 | case ISD::SRL: |
| 1113 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1114 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1115 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1116 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1117 | .addImm(Tmp2).addImm(31); |
| 1118 | } else { |
| 1119 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1120 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1121 | } |
| 1122 | return Result; |
| 1123 | |
| 1124 | case ISD::SRA: |
| 1125 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1126 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1127 | Tmp2 = CN->getValue() & 0x1F; |
| 1128 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1129 | } else { |
| 1130 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1131 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1132 | } |
| 1133 | return Result; |
| 1134 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1135 | case ISD::ADD: |
| 1136 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1137 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1138 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1139 | default: assert(0 && "unhandled result code"); |
| 1140 | case 0: // No immediate |
| 1141 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1142 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1143 | break; |
| 1144 | case 1: // Low immediate |
| 1145 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1146 | break; |
| 1147 | case 2: // Shifted immediate |
| 1148 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1149 | break; |
| 1150 | } |
| 1151 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1152 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1153 | case ISD::AND: |
| 1154 | case ISD::OR: |
| 1155 | case ISD::XOR: |
| 1156 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1157 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1158 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1159 | default: assert(0 && "unhandled result code"); |
| 1160 | case 0: // No immediate |
| 1161 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1162 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1163 | case ISD::AND: Opc = PPC::AND; break; |
| 1164 | case ISD::OR: Opc = PPC::OR; break; |
| 1165 | case ISD::XOR: Opc = PPC::XOR; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1166 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1167 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1168 | break; |
| 1169 | case 1: // Low immediate |
| 1170 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1171 | case ISD::AND: Opc = PPC::ANDIo; break; |
| 1172 | case ISD::OR: Opc = PPC::ORI; break; |
| 1173 | case ISD::XOR: Opc = PPC::XORI; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1174 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1175 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | case 2: // Shifted immediate |
| 1178 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1179 | case ISD::AND: Opc = PPC::ANDISo; break; |
| 1180 | case ISD::OR: Opc = PPC::ORIS; break; |
| 1181 | case ISD::XOR: Opc = PPC::XORIS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1182 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1183 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1187 | |
| 1188 | case ISD::SUB: |
| 1189 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1190 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1191 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1192 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1193 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1194 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1195 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1196 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1197 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1198 | if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) |
| 1199 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1200 | else { |
| 1201 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1202 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1203 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1204 | return Result; |
| 1205 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1206 | case ISD::SDIV: |
| 1207 | case ISD::UDIV: |
| 1208 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1209 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1210 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1211 | Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 1212 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1213 | return Result; |
| 1214 | |
| 1215 | case ISD::UREM: |
| 1216 | case ISD::SREM: { |
| 1217 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 1218 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1219 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1220 | Tmp3 = MakeReg(MVT::i32); |
| 1221 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1222 | Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 1223 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 1224 | BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2); |
| 1225 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1); |
| 1226 | return Result; |
| 1227 | } |
| 1228 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1229 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1230 | case ISD::SUB_PARTS: { |
| 1231 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1232 | "Not an i64 add/sub!"); |
| 1233 | // Emit all of the operands. |
| 1234 | std::vector<unsigned> InVals; |
| 1235 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1236 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1237 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1238 | BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]); |
| 1239 | BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1240 | } else { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1241 | BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]); |
| 1242 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1243 | } |
| 1244 | return Result+N.ResNo; |
| 1245 | } |
| 1246 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1247 | case ISD::FP_TO_UINT: |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 1248 | case ISD::FP_TO_SINT: { |
| 1249 | bool U = (ISD::FP_TO_UINT == opcode); |
| 1250 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1251 | if (!U) { |
| 1252 | Tmp2 = MakeReg(MVT::f64); |
| 1253 | BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1); |
| 1254 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 1255 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx); |
| 1256 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4); |
| 1257 | return Result; |
| 1258 | } else { |
| 1259 | unsigned Zero = getConstDouble(0.0); |
| 1260 | unsigned MaxInt = getConstDouble((1LL << 32) - 1); |
| 1261 | unsigned Border = getConstDouble(1LL << 31); |
| 1262 | unsigned UseZero = MakeReg(MVT::f64); |
| 1263 | unsigned UseMaxInt = MakeReg(MVT::f64); |
| 1264 | unsigned UseChoice = MakeReg(MVT::f64); |
| 1265 | unsigned TmpReg = MakeReg(MVT::f64); |
| 1266 | unsigned TmpReg2 = MakeReg(MVT::f64); |
| 1267 | unsigned ConvReg = MakeReg(MVT::f64); |
| 1268 | unsigned IntTmp = MakeReg(MVT::i32); |
| 1269 | unsigned XorReg = MakeReg(MVT::i32); |
| 1270 | MachineFunction *F = BB->getParent(); |
| 1271 | int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8); |
| 1272 | // Update machine-CFG edges |
| 1273 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1274 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1275 | MachineBasicBlock *OldMBB = BB; |
| 1276 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1277 | F->getBasicBlockList().insert(It, XorMBB); |
| 1278 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1279 | BB->addSuccessor(XorMBB); |
| 1280 | BB->addSuccessor(PhiMBB); |
| 1281 | // Convert from floating point to unsigned 32-bit value |
| 1282 | // Use 0 if incoming value is < 0.0 |
| 1283 | BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero); |
| 1284 | // Use 2**32 - 1 if incoming value is >= 2**32 |
| 1285 | BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1); |
| 1286 | BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero) |
| 1287 | .addReg(MaxInt); |
| 1288 | // Subtract 2**31 |
| 1289 | BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
| 1290 | // Use difference if >= 2**31 |
| 1291 | BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border); |
| 1292 | BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
| 1293 | .addReg(UseChoice); |
| 1294 | // Convert to integer |
| 1295 | BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 1296 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx); |
| 1297 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4); |
| 1298 | BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 1299 | BuildMI(BB, PPC::B, 1).addMBB(XorMBB); |
| 1300 | |
| 1301 | // XorMBB: |
| 1302 | // add 2**31 if input was >= 2**31 |
| 1303 | BB = XorMBB; |
| 1304 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
| 1305 | XorMBB->addSuccessor(PhiMBB); |
| 1306 | |
| 1307 | // PhiMBB: |
| 1308 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 1309 | BB = PhiMBB; |
| 1310 | BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB) |
| 1311 | .addReg(XorReg).addMBB(XorMBB); |
| 1312 | return Result; |
| 1313 | } |
| 1314 | assert(0 && "Should never get here"); |
| 1315 | return 0; |
| 1316 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1317 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1318 | case ISD::SETCC: |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1319 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1320 | Opc = SelectSetCR0(N); |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1321 | |
Nate Begeman | 31318e4 | 2005-04-01 07:21:30 +0000 | [diff] [blame^] | 1322 | unsigned TrueValue = MakeReg(MVT::i32); |
| 1323 | BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1); |
| 1324 | unsigned FalseValue = MakeReg(MVT::i32); |
| 1325 | BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0); |
| 1326 | |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1327 | // Create an iterator with which to insert the MBB for copying the false |
| 1328 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1329 | MachineBasicBlock *thisMBB = BB; |
| 1330 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1331 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1332 | ++It; |
| 1333 | |
| 1334 | // thisMBB: |
| 1335 | // ... |
| 1336 | // cmpTY cr0, r1, r2 |
| 1337 | // %TrueValue = li 1 |
| 1338 | // bCC sinkMBB |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1339 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1340 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1341 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
| 1342 | MachineFunction *F = BB->getParent(); |
| 1343 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1344 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1345 | // Update machine-CFG edges |
| 1346 | BB->addSuccessor(copy0MBB); |
| 1347 | BB->addSuccessor(sinkMBB); |
| 1348 | |
| 1349 | // copy0MBB: |
| 1350 | // %FalseValue = li 0 |
| 1351 | // fallthrough |
| 1352 | BB = copy0MBB; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1353 | // Update machine-CFG edges |
| 1354 | BB->addSuccessor(sinkMBB); |
| 1355 | |
| 1356 | // sinkMBB: |
| 1357 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1358 | // ... |
| 1359 | BB = sinkMBB; |
| 1360 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1361 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 1362 | return Result; |
| 1363 | } |
| 1364 | assert(0 && "Is this legal?"); |
| 1365 | return 0; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1366 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1367 | case ISD::SELECT: { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1368 | Opc = SelectSetCR0(N.getOperand(0)); |
| 1369 | |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 1370 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
| 1371 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
| 1372 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1373 | // Create an iterator with which to insert the MBB for copying the false |
| 1374 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1375 | MachineBasicBlock *thisMBB = BB; |
| 1376 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1377 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1378 | ++It; |
| 1379 | |
| 1380 | // thisMBB: |
| 1381 | // ... |
| 1382 | // TrueVal = ... |
| 1383 | // cmpTY cr0, r1, r2 |
| 1384 | // bCC copy1MBB |
| 1385 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1386 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1387 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 1388 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1389 | MachineFunction *F = BB->getParent(); |
| 1390 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1391 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1392 | // Update machine-CFG edges |
| 1393 | BB->addSuccessor(copy0MBB); |
| 1394 | BB->addSuccessor(sinkMBB); |
| 1395 | |
| 1396 | // copy0MBB: |
| 1397 | // %FalseValue = ... |
| 1398 | // # fallthrough to sinkMBB |
| 1399 | BB = copy0MBB; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1400 | // Update machine-CFG edges |
| 1401 | BB->addSuccessor(sinkMBB); |
| 1402 | |
| 1403 | // sinkMBB: |
| 1404 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1405 | // ... |
| 1406 | BB = sinkMBB; |
| 1407 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1408 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 1409 | |
| 1410 | // FIXME: Select i64? |
| 1411 | return Result; |
| 1412 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1413 | |
| 1414 | case ISD::Constant: |
| 1415 | switch (N.getValueType()) { |
| 1416 | default: assert(0 && "Cannot use constants of this type!"); |
| 1417 | case MVT::i1: |
| 1418 | BuildMI(BB, PPC::LI, 1, Result) |
| 1419 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 1420 | break; |
| 1421 | case MVT::i32: |
| 1422 | { |
| 1423 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 1424 | if (v < 32768 && v >= -32768) { |
| 1425 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 1426 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1427 | Tmp1 = MakeReg(MVT::i32); |
| 1428 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 1429 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | return Result; |
| 1434 | } |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | void ISel::Select(SDOperand N) { |
| 1440 | unsigned Tmp1, Tmp2, Opc; |
| 1441 | unsigned opcode = N.getOpcode(); |
| 1442 | |
| 1443 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 1444 | return; // Already selected. |
| 1445 | |
| 1446 | SDNode *Node = N.Val; |
| 1447 | |
| 1448 | switch (Node->getOpcode()) { |
| 1449 | default: |
| 1450 | Node->dump(); std::cerr << "\n"; |
| 1451 | assert(0 && "Node not handled yet!"); |
| 1452 | case ISD::EntryToken: return; // Noop |
| 1453 | case ISD::TokenFactor: |
| 1454 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1455 | Select(Node->getOperand(i)); |
| 1456 | return; |
| 1457 | case ISD::ADJCALLSTACKDOWN: |
| 1458 | case ISD::ADJCALLSTACKUP: |
| 1459 | Select(N.getOperand(0)); |
| 1460 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 1461 | Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN : |
| 1462 | PPC::ADJCALLSTACKUP; |
| 1463 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 1464 | return; |
| 1465 | case ISD::BR: { |
| 1466 | MachineBasicBlock *Dest = |
| 1467 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1468 | Select(N.getOperand(0)); |
| 1469 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 1470 | return; |
| 1471 | } |
| 1472 | case ISD::BRCOND: |
| 1473 | SelectBranchCC(N); |
| 1474 | return; |
| 1475 | case ISD::CopyToReg: |
| 1476 | Select(N.getOperand(0)); |
| 1477 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1478 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
| 1479 | |
| 1480 | if (Tmp1 != Tmp2) { |
| 1481 | if (N.getOperand(1).getValueType() == MVT::f64 || |
| 1482 | N.getOperand(1).getValueType() == MVT::f32) |
| 1483 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 1484 | else |
| 1485 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1486 | } |
| 1487 | return; |
| 1488 | case ISD::ImplicitDef: |
| 1489 | Select(N.getOperand(0)); |
| 1490 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 1491 | return; |
| 1492 | case ISD::RET: |
| 1493 | switch (N.getNumOperands()) { |
| 1494 | default: |
| 1495 | assert(0 && "Unknown return instruction!"); |
| 1496 | case 3: |
| 1497 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1498 | N.getOperand(2).getValueType() == MVT::i32 && |
| 1499 | "Unknown two-register value!"); |
| 1500 | Select(N.getOperand(0)); |
| 1501 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1502 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 1503 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1504 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2); |
| 1505 | break; |
| 1506 | case 2: |
| 1507 | Select(N.getOperand(0)); |
| 1508 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1509 | switch (N.getOperand(1).getValueType()) { |
| 1510 | default: |
| 1511 | assert(0 && "Unknown return type!"); |
| 1512 | case MVT::f64: |
| 1513 | case MVT::f32: |
| 1514 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 1515 | break; |
| 1516 | case MVT::i32: |
| 1517 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1518 | break; |
| 1519 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1520 | case 1: |
| 1521 | Select(N.getOperand(0)); |
| 1522 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1523 | } |
| 1524 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 1525 | return; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1526 | case ISD::TRUNCSTORE: |
| 1527 | case ISD::STORE: |
| 1528 | { |
| 1529 | SDOperand Chain = N.getOperand(0); |
| 1530 | SDOperand Value = N.getOperand(1); |
| 1531 | SDOperand Address = N.getOperand(2); |
| 1532 | Select(Chain); |
| 1533 | |
| 1534 | Tmp1 = SelectExpr(Value); //value |
| 1535 | |
| 1536 | if (opcode == ISD::STORE) { |
| 1537 | switch(Value.getValueType()) { |
| 1538 | default: assert(0 && "unknown Type in store"); |
| 1539 | case MVT::i32: Opc = PPC::STW; break; |
| 1540 | case MVT::f64: Opc = PPC::STFD; break; |
| 1541 | case MVT::f32: Opc = PPC::STFS; break; |
| 1542 | } |
| 1543 | } else { //ISD::TRUNCSTORE |
| 1544 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 1545 | default: assert(0 && "unknown Type in store"); |
| 1546 | case MVT::i1: //FIXME: DAG does not promote this load |
| 1547 | case MVT::i8: Opc = PPC::STB; break; |
| 1548 | case MVT::i16: Opc = PPC::STH; break; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Nate Begeman | a7e11a4 | 2005-04-01 05:57:17 +0000 | [diff] [blame] | 1552 | if(Address.getOpcode() == ISD::FrameIndex) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1553 | { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1554 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1555 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1556 | } |
| 1557 | else |
| 1558 | { |
| 1559 | int offset; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1560 | bool idx = SelectAddr(Address, Tmp2, offset); |
| 1561 | if (idx) { |
| 1562 | Opc = IndexedOpForOp(Opc); |
| 1563 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
| 1564 | } else { |
| 1565 | BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2); |
| 1566 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1567 | } |
| 1568 | return; |
| 1569 | } |
| 1570 | case ISD::EXTLOAD: |
| 1571 | case ISD::SEXTLOAD: |
| 1572 | case ISD::ZEXTLOAD: |
| 1573 | case ISD::LOAD: |
| 1574 | case ISD::CopyFromReg: |
| 1575 | case ISD::CALL: |
| 1576 | case ISD::DYNAMIC_STACKALLOC: |
| 1577 | ExprMap.erase(N); |
| 1578 | SelectExpr(N); |
| 1579 | return; |
| 1580 | } |
| 1581 | assert(0 && "Should not be reached!"); |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1586 | /// into a machine code representation using pattern matching and a machine |
| 1587 | /// description file. |
| 1588 | /// |
| 1589 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
| 1590 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |