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