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