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