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