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