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