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