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