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