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