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. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PowerPC.h" |
| 15 | #include "PowerPCInstrBuilder.h" |
| 16 | #include "PowerPCInstrInfo.h" |
| 17 | #include "PPC32RegisterInfo.h" |
| 18 | #include "llvm/Constants.h" // FIXME: REMOVE |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetLowering.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include <set> |
| 32 | #include <algorithm> |
| 33 | using namespace llvm; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface |
| 37 | namespace { |
| 38 | class PPC32TargetLowering : public TargetLowering { |
| 39 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
| 40 | int ReturnAddrIndex; // FrameIndex for return slot. |
| 41 | public: |
| 42 | PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
| 43 | // Set up the TargetLowering object. |
| 44 | |
| 45 | // Set up the register classes. |
| 46 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 47 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 48 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
| 49 | |
| 50 | computeRegisterProperties(); |
| 51 | } |
| 52 | |
| 53 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 54 | /// lower the arguments for the specified function, into the specified DAG. |
| 55 | virtual std::vector<SDOperand> |
| 56 | LowerArguments(Function &F, SelectionDAG &DAG); |
| 57 | |
| 58 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 59 | /// actual call. |
| 60 | virtual std::pair<SDOperand, SDOperand> |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 61 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, |
| 62 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 63 | |
| 64 | virtual std::pair<SDOperand, SDOperand> |
| 65 | LowerVAStart(SDOperand Chain, SelectionDAG &DAG); |
| 66 | |
| 67 | virtual std::pair<SDOperand,SDOperand> |
| 68 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 69 | const Type *ArgTy, SelectionDAG &DAG); |
| 70 | |
| 71 | virtual std::pair<SDOperand, SDOperand> |
| 72 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 73 | SelectionDAG &DAG); |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | std::vector<SDOperand> |
| 79 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 80 | // |
| 81 | // add beautiful description of PPC stack frame format, or at least some docs |
| 82 | // |
| 83 | MachineFunction &MF = DAG.getMachineFunction(); |
| 84 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 85 | MachineBasicBlock& BB = MF.front(); |
| 86 | std::vector<SDOperand> ArgValues; |
| 87 | |
| 88 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
| 89 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 90 | // handle tracking them for us. |
| 91 | std::vector<unsigned> argVR, argPR, argOp; |
| 92 | unsigned ArgOffset = 24; |
| 93 | unsigned GPR_remaining = 8; |
| 94 | unsigned FPR_remaining = 13; |
| 95 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 96 | static const unsigned GPR[] = { |
| 97 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 98 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 99 | }; |
| 100 | static const unsigned FPR[] = { |
| 101 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 102 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 103 | }; |
| 104 | |
| 105 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 106 | // the arguments start at offset 24, although they are likely to be passed |
| 107 | // in registers. |
| 108 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 109 | SDOperand newroot, argt; |
| 110 | unsigned ObjSize; |
| 111 | bool needsLoad = false; |
| 112 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 113 | |
| 114 | switch (ObjectVT) { |
| 115 | default: assert(0 && "Unhandled argument type!"); |
| 116 | case MVT::i1: |
| 117 | case MVT::i8: |
| 118 | case MVT::i16: |
| 119 | case MVT::i32: |
| 120 | ObjSize = 4; |
| 121 | if (GPR_remaining > 0) { |
| 122 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 123 | argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 124 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 125 | if (ObjectVT != MVT::i32) |
| 126 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 127 | } else { |
| 128 | needsLoad = true; |
| 129 | } |
| 130 | break; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 131 | case MVT::i64: ObjSize = 8; |
| 132 | // FIXME: can split 64b load between reg/mem if it is last arg in regs |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 133 | if (GPR_remaining > 1) { |
| 134 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 135 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 136 | // Copy the extracted halves into the virtual registers |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 137 | SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 138 | DAG.getRoot()); |
| 139 | SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 140 | // Build the outgoing arg thingy |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 141 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 142 | newroot = argLo; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 143 | } else { |
| 144 | needsLoad = true; |
| 145 | } |
| 146 | break; |
| 147 | case MVT::f32: ObjSize = 4; |
| 148 | case MVT::f64: ObjSize = 8; |
| 149 | if (FPR_remaining > 0) { |
| 150 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 151 | argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT, |
| 152 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 153 | --FPR_remaining; |
| 154 | ++FPR_idx; |
| 155 | } else { |
| 156 | needsLoad = true; |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | // We need to load the argument to a virtual register if we determined above |
| 162 | // that we ran out of physical registers of the appropriate type |
| 163 | if (needsLoad) { |
| 164 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 165 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 166 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN); |
| 167 | } |
| 168 | |
| 169 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 170 | // argument passing. |
| 171 | if (GPR_remaining > 0) { |
| 172 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 173 | GPR_remaining -= delta; |
| 174 | GPR_idx += delta; |
| 175 | } |
| 176 | ArgOffset += ObjSize; |
| 177 | |
| 178 | DAG.setRoot(newroot.getValue(1)); |
| 179 | ArgValues.push_back(argt); |
| 180 | } |
| 181 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 182 | // If the function takes variable number of arguments, make a frame index for |
| 183 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 184 | if (F.isVarArg()) |
| 185 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 186 | |
| 187 | return ArgValues; |
| 188 | } |
| 189 | |
| 190 | std::pair<SDOperand, SDOperand> |
| 191 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 192 | const Type *RetTy, bool isVarArg, |
| 193 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) { |
| 194 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 195 | // SelectExpr to use to put the arguments in the appropriate registers. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 196 | std::vector<SDOperand> args_to_use; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 197 | |
| 198 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 199 | // area, and parameter passing area. |
| 200 | unsigned NumBytes = 24; |
| 201 | |
| 202 | if (Args.empty()) { |
| 203 | NumBytes = 0; // Save zero bytes. |
| 204 | } else { |
| 205 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 206 | switch (getValueType(Args[i].second)) { |
| 207 | default: assert(0 && "Unknown value type!"); |
| 208 | case MVT::i1: |
| 209 | case MVT::i8: |
| 210 | case MVT::i16: |
| 211 | case MVT::i32: |
| 212 | case MVT::f32: |
| 213 | NumBytes += 4; |
| 214 | break; |
| 215 | case MVT::i64: |
| 216 | case MVT::f64: |
| 217 | NumBytes += 8; |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
| 222 | // plus 32 bytes of argument space in case any called code gets funky on us. |
| 223 | if (NumBytes < 56) NumBytes = 56; |
| 224 | |
| 225 | // Adjust the stack pointer for the new arguments... |
| 226 | // These operations are automatically eliminated by the prolog/epilog pass |
| 227 | Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain, |
| 228 | DAG.getConstant(NumBytes, getPointerTy())); |
| 229 | |
| 230 | // Set up a copy of the stack pointer for use loading and storing any |
| 231 | // arguments that may not fit in the registers available for argument |
| 232 | // passing. |
| 233 | SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32, |
| 234 | DAG.getEntryNode()); |
| 235 | |
| 236 | // Figure out which arguments are going to go in registers, and which in |
| 237 | // memory. Also, if this is a vararg function, floating point operations |
| 238 | // must be stored to our stack, and loaded into integer regs as well, if |
| 239 | // any integer regs are available for argument passing. |
| 240 | unsigned ArgOffset = 24; |
| 241 | unsigned GPR_remaining = 8; |
| 242 | unsigned FPR_remaining = 13; |
| 243 | std::vector<SDOperand> Stores; |
| 244 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 245 | // PtrOff will be used to store the current argument to the stack if a |
| 246 | // register cannot be found for it. |
| 247 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 248 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 249 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 250 | |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 251 | switch (ArgVT) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 252 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 253 | case MVT::i1: |
| 254 | case MVT::i8: |
| 255 | case MVT::i16: |
| 256 | // Promote the integer to 32 bits. If the input type is signed use a |
| 257 | // sign extend, otherwise use a zero extend. |
| 258 | if (Args[i].second->isSigned()) |
| 259 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 260 | else |
| 261 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 262 | // FALL THROUGH |
| 263 | case MVT::i32: |
| 264 | if (GPR_remaining > 0) { |
| 265 | args_to_use.push_back(Args[i].first); |
| 266 | --GPR_remaining; |
| 267 | } else { |
| 268 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 269 | Args[i].first, PtrOff)); |
| 270 | } |
| 271 | ArgOffset += 4; |
| 272 | break; |
| 273 | case MVT::i64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 274 | // If we have one free GPR left, we can place the upper half of the i64 |
| 275 | // in it, and store the other half to the stack. If we have two or more |
| 276 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 277 | if (GPR_remaining > 0) { |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 278 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 279 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 280 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 281 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 282 | args_to_use.push_back(Hi); |
| 283 | if (GPR_remaining > 1) { |
| 284 | args_to_use.push_back(Lo); |
| 285 | GPR_remaining -= 2; |
| 286 | } else { |
| 287 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 288 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
| 289 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 290 | Lo, PtrOff)); |
| 291 | --GPR_remaining; |
| 292 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 293 | } else { |
| 294 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 295 | Args[i].first, PtrOff)); |
| 296 | } |
| 297 | ArgOffset += 8; |
| 298 | break; |
| 299 | case MVT::f32: |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 300 | case MVT::f64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 301 | if (FPR_remaining > 0) { |
| 302 | if (isVarArg) { |
| 303 | // FIXME: Need FunctionType information so we can conditionally |
| 304 | // store only the non-fixed arguments in a vararg function. |
| 305 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 306 | Args[i].first, PtrOff)); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 307 | // FIXME: Need a way to communicate to the ISD::CALL select code |
| 308 | // that a particular argument is non-fixed so that we can load them |
| 309 | // into the correct GPR to shadow the FPR |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 310 | } |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 311 | args_to_use.push_back(Args[i].first); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 312 | --FPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 313 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 314 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 315 | // GPRs. |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 316 | if (GPR_remaining > 0) --GPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 317 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) --GPR_remaining; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 318 | } else { |
| 319 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 320 | Args[i].first, PtrOff)); |
| 321 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 322 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 323 | break; |
| 324 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 325 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 326 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | std::vector<MVT::ValueType> RetVals; |
| 330 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 331 | if (RetTyVT != MVT::isVoid) |
| 332 | RetVals.push_back(RetTyVT); |
| 333 | RetVals.push_back(MVT::Other); |
| 334 | |
| 335 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
| 336 | Chain, Callee, args_to_use), 0); |
| 337 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
| 338 | Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain, |
| 339 | DAG.getConstant(NumBytes, getPointerTy())); |
| 340 | return std::make_pair(TheCall, Chain); |
| 341 | } |
| 342 | |
| 343 | std::pair<SDOperand, SDOperand> |
| 344 | PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) { |
| 345 | //vastart just returns the address of the VarArgsFrameIndex slot. |
| 346 | return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain); |
| 347 | } |
| 348 | |
| 349 | std::pair<SDOperand,SDOperand> PPC32TargetLowering:: |
| 350 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 351 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 352 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 353 | SDOperand Result; |
| 354 | if (!isVANext) { |
| 355 | Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList); |
| 356 | } else { |
| 357 | unsigned Amt; |
| 358 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 359 | Amt = 4; |
| 360 | else { |
| 361 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 362 | "Other types should have been promoted for varargs!"); |
| 363 | Amt = 8; |
| 364 | } |
| 365 | Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 366 | DAG.getConstant(Amt, VAList.getValueType())); |
| 367 | } |
| 368 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 373 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 374 | SelectionDAG &DAG) { |
| 375 | abort(); |
| 376 | } |
| 377 | |
| 378 | namespace { |
| 379 | |
| 380 | //===--------------------------------------------------------------------===// |
| 381 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 382 | /// SelectionDAG operations. |
| 383 | //===--------------------------------------------------------------------===// |
| 384 | class ISel : public SelectionDAGISel { |
| 385 | |
| 386 | /// Comment Here. |
| 387 | PPC32TargetLowering PPC32Lowering; |
| 388 | |
| 389 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 390 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 391 | /// tree. |
| 392 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 393 | |
| 394 | unsigned GlobalBaseReg; |
| 395 | bool GlobalBaseInitialized; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 396 | |
| 397 | public: |
| 398 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) |
| 399 | {} |
| 400 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 401 | /// runOnFunction - Override this function in order to reset our per-function |
| 402 | /// variables. |
| 403 | virtual bool runOnFunction(Function &Fn) { |
| 404 | // Make sure we re-emit a set of the global base reg if necessary |
| 405 | GlobalBaseInitialized = false; |
| 406 | return SelectionDAGISel::runOnFunction(Fn); |
| 407 | } |
| 408 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 409 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 410 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 411 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 412 | DEBUG(BB->dump()); |
| 413 | // Codegen the basic block. |
| 414 | Select(DAG.getRoot()); |
| 415 | |
| 416 | // Clear state used for selection. |
| 417 | ExprMap.clear(); |
| 418 | } |
| 419 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 420 | unsigned ISel::getGlobalBaseReg(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 421 | unsigned SelectExpr(SDOperand N); |
| 422 | unsigned SelectExprFP(SDOperand N, unsigned Result); |
| 423 | void Select(SDOperand N); |
| 424 | |
| 425 | void SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
| 426 | void SelectBranchCC(SDOperand N); |
| 427 | }; |
| 428 | |
| 429 | /// canUseAsImmediateForOpcode - This method returns a value indicating whether |
| 430 | /// the ConstantSDNode N can be used as an immediate to Opcode. The return |
| 431 | /// values are either 0, 1 or 2. 0 indicates that either N is not a |
| 432 | /// ConstantSDNode, or is not suitable for use by that opcode. A return value |
| 433 | /// of 1 indicates that the constant may be used in normal immediate form. A |
| 434 | /// return value of 2 indicates that the constant may be used in shifted |
| 435 | /// immediate form. If the return value is nonzero, the constant value is |
| 436 | /// placed in Imm. |
| 437 | /// |
| 438 | static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode, |
| 439 | unsigned& Imm) { |
| 440 | if (N.getOpcode() != ISD::Constant) return 0; |
| 441 | |
| 442 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 443 | |
| 444 | switch(Opcode) { |
| 445 | default: return 0; |
| 446 | case ISD::ADD: |
| 447 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 448 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 449 | break; |
| 450 | case ISD::AND: |
| 451 | case ISD::XOR: |
| 452 | case ISD::OR: |
| 453 | if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; } |
| 454 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 455 | break; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 456 | case ISD::MUL: |
| 457 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 458 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | } |
| 463 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 464 | /// getGlobalBaseReg - Output the instructions required to put the |
| 465 | /// base address to use for accessing globals into a register. |
| 466 | /// |
| 467 | unsigned ISel::getGlobalBaseReg() { |
| 468 | if (!GlobalBaseInitialized) { |
| 469 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 470 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 471 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 472 | GlobalBaseReg = MakeReg(MVT::i32); |
| 473 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 474 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 475 | GlobalBaseInitialized = true; |
| 476 | } |
| 477 | return GlobalBaseReg; |
| 478 | } |
| 479 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 480 | //Check to see if the load is a constant offset from a base register |
| 481 | void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
| 482 | { |
| 483 | Reg = SelectExpr(N); |
| 484 | offset = 0; |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | void ISel::SelectBranchCC(SDOperand N) |
| 489 | { |
| 490 | assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???"); |
| 491 | MachineBasicBlock *Dest = |
| 492 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
| 493 | unsigned Opc; |
| 494 | |
| 495 | Select(N.getOperand(0)); //chain |
| 496 | SDOperand CC = N.getOperand(1); |
| 497 | |
| 498 | //Giveup and do the stupid thing |
| 499 | unsigned Tmp1 = SelectExpr(CC); |
| 500 | BuildMI(BB, PPC::BNE, 2).addReg(Tmp1).addMBB(Dest); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | unsigned ISel::SelectExprFP(SDOperand N, unsigned Result) |
| 505 | { |
| 506 | unsigned Tmp1, Tmp2, Tmp3; |
| 507 | unsigned Opc = 0; |
| 508 | SDNode *Node = N.Val; |
| 509 | MVT::ValueType DestType = N.getValueType(); |
| 510 | unsigned opcode = N.getOpcode(); |
| 511 | |
| 512 | switch (opcode) { |
| 513 | default: |
| 514 | Node->dump(); |
| 515 | assert(0 && "Node not handled!\n"); |
| 516 | |
| 517 | case ISD::SELECT: |
| 518 | abort(); |
| 519 | |
| 520 | case ISD::FP_ROUND: |
| 521 | assert (DestType == MVT::f32 && |
| 522 | N.getOperand(0).getValueType() == MVT::f64 && |
| 523 | "only f64 to f32 conversion supported here"); |
| 524 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 525 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 526 | return Result; |
| 527 | |
| 528 | case ISD::FP_EXTEND: |
| 529 | assert (DestType == MVT::f64 && |
| 530 | N.getOperand(0).getValueType() == MVT::f32 && |
| 531 | "only f32 to f64 conversion supported here"); |
| 532 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 533 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 534 | return Result; |
| 535 | |
| 536 | case ISD::CopyFromReg: |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 537 | if (Result == 1) |
| 538 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 539 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 540 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 541 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 542 | |
| 543 | case ISD::LOAD: |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 544 | case ISD::EXTLOAD: { |
| 545 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
| 546 | Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType(); |
| 547 | |
| 548 | // Make sure we generate both values. |
| 549 | if (Result != 1) |
| 550 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 551 | else |
| 552 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 553 | |
| 554 | SDOperand Chain = N.getOperand(0); |
| 555 | SDOperand Address = N.getOperand(1); |
| 556 | Select(Chain); |
| 557 | |
| 558 | switch (TypeBeingLoaded) { |
| 559 | default: assert(0 && "Cannot fp load this type!"); |
| 560 | case MVT::f32: Opc = PPC::LFS; break; |
| 561 | case MVT::f64: Opc = PPC::LFD; break; |
| 562 | } |
| 563 | |
| 564 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 565 | BuildMI(BB, Opc, 2, Result) |
| 566 | .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex()) |
| 567 | .addReg(PPC::R1); |
| 568 | } else { |
| 569 | int offset; |
| 570 | SelectAddr(Address, Tmp1, offset); |
| 571 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 572 | } |
| 573 | return Result; |
| 574 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 575 | |
| 576 | case ISD::ConstantFP: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 577 | assert(0 && "ISD::ConstantFP Unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 578 | abort(); |
| 579 | |
| 580 | case ISD::MUL: |
| 581 | case ISD::ADD: |
| 582 | case ISD::SUB: |
| 583 | case ISD::SDIV: |
| 584 | switch( opcode ) { |
| 585 | case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break; |
| 586 | case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break; |
| 587 | case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break; |
| 588 | case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break; |
| 589 | }; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 590 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 591 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 592 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 593 | return Result; |
| 594 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 595 | case ISD::UINT_TO_FP: |
| 596 | case ISD::SINT_TO_FP: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 597 | assert(0 && "ISD::U/SINT_TO_FP Unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 598 | abort(); |
| 599 | } |
| 600 | assert(0 && "should not get here"); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | unsigned ISel::SelectExpr(SDOperand N) { |
| 605 | unsigned Result; |
| 606 | unsigned Tmp1, Tmp2, Tmp3; |
| 607 | unsigned Opc = 0; |
| 608 | unsigned opcode = N.getOpcode(); |
| 609 | |
| 610 | SDNode *Node = N.Val; |
| 611 | MVT::ValueType DestType = N.getValueType(); |
| 612 | |
| 613 | unsigned &Reg = ExprMap[N]; |
| 614 | if (Reg) return Reg; |
| 615 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 616 | if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS && |
| 617 | N.getOpcode() != ISD::SUB_PARTS) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 618 | Reg = Result = (N.getValueType() != MVT::Other) ? |
| 619 | MakeReg(N.getValueType()) : 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 620 | else { |
| 621 | // If this is a call instruction, make sure to prepare ALL of the result |
| 622 | // values as well as the chain. |
| 623 | if (N.getOpcode() == ISD::CALL) { |
| 624 | if (Node->getNumValues() == 1) |
| 625 | Reg = Result = 1; // Void call, just a chain. |
| 626 | else { |
| 627 | Result = MakeReg(Node->getValueType(0)); |
| 628 | ExprMap[N.getValue(0)] = Result; |
| 629 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
| 630 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 631 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
| 632 | } |
| 633 | } else { |
| 634 | Result = MakeReg(Node->getValueType(0)); |
| 635 | ExprMap[N.getValue(0)] = Result; |
| 636 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 637 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (DestType == MVT::f64 || DestType == MVT::f32) |
| 642 | return SelectExprFP(N, Result); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 643 | |
| 644 | switch (opcode) { |
| 645 | default: |
| 646 | Node->dump(); |
| 647 | assert(0 && "Node not handled!\n"); |
| 648 | |
| 649 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 650 | // Generate both result values. FIXME: Need a better commment here? |
| 651 | if (Result != 1) |
| 652 | ExprMap[N.getValue(1)] = 1; |
| 653 | else |
| 654 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 655 | |
| 656 | // FIXME: We are currently ignoring the requested alignment for handling |
| 657 | // greater than the stack alignment. This will need to be revisited at some |
| 658 | // point. Align = N.getOperand(2); |
| 659 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 660 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 661 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 662 | << " the stack alignment yet!"; |
| 663 | abort(); |
| 664 | } |
| 665 | Select(N.getOperand(0)); |
| 666 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 667 | // Subtract size from stack pointer, thereby allocating some space. |
| 668 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 669 | // Put a pointer to the space into the result register by copying the SP |
| 670 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 671 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 672 | |
| 673 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 674 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 675 | Tmp2 = MakeReg(MVT::i32); |
| 676 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 677 | .addConstantPoolIndex(Tmp1); |
| 678 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 679 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 680 | |
| 681 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 682 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
| 683 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1); |
| 684 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 685 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 686 | case ISD::GlobalAddress: { |
| 687 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 688 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 689 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 690 | .addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 691 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 692 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 693 | } else { |
| 694 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 695 | } |
| 696 | return Result; |
| 697 | } |
| 698 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 699 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 700 | case ISD::EXTLOAD: |
| 701 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 702 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 703 | bool sext = (ISD::SEXTLOAD == opcode); |
| 704 | bool byte = (MVT::i8 == Node->getValueType(0)); |
| 705 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
| 706 | Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType(); |
| 707 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 708 | // Make sure we generate both values. |
| 709 | if (Result != 1) |
| 710 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 711 | else |
| 712 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 713 | |
| 714 | SDOperand Chain = N.getOperand(0); |
| 715 | SDOperand Address = N.getOperand(1); |
| 716 | Select(Chain); |
| 717 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 718 | switch (TypeBeingLoaded) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 719 | default: assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 720 | case MVT::i1: Opc = PPC::LBZ; break; |
| 721 | case MVT::i8: Opc = PPC::LBZ; break; |
| 722 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 723 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 726 | // Since there's no load byte & sign extend instruction we have to split |
| 727 | // byte SEXTLOADs into lbz + extsb. This requires we make a temp register. |
| 728 | if (sext && byte) { |
| 729 | Tmp3 = Result; |
| 730 | Result = MakeReg(MVT::i32); |
Chris Lattner | 848132d | 2005-03-29 15:13:27 +0000 | [diff] [blame] | 731 | } else { |
| 732 | Tmp3 = 0; // Silence GCC warning. |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 733 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 734 | if(Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 735 | BuildMI(BB, Opc, 2, Result) |
| 736 | .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex()) |
| 737 | .addReg(PPC::R1); |
| 738 | } else { |
| 739 | int offset; |
| 740 | SelectAddr(Address, Tmp1, offset); |
| 741 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 742 | } |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 743 | if (sext && byte) { |
| 744 | BuildMI(BB, PPC::EXTSB, 1, Tmp3).addReg(Result); |
| 745 | Result = Tmp3; |
| 746 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 747 | return Result; |
| 748 | } |
| 749 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 750 | case ISD::CALL: { |
| 751 | // Lower the chain for this call. |
| 752 | Select(N.getOperand(0)); |
| 753 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
| 754 | |
| 755 | // get the virtual reg for each argument |
| 756 | std::vector<unsigned> VRegs; |
| 757 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
| 758 | VRegs.push_back(SelectExpr(N.getOperand(i))); |
| 759 | |
| 760 | // The ABI specifies that the first 32 bytes of args may be passed in GPRs, |
| 761 | // and that 13 FPRs may also be used for passing any floating point args. |
| 762 | int GPR_remaining = 8, FPR_remaining = 13; |
| 763 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 764 | static const unsigned GPR[] = { |
| 765 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 766 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 767 | }; |
| 768 | static const unsigned FPR[] = { |
| 769 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, |
| 770 | PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, |
| 771 | PPC::F13 |
| 772 | }; |
| 773 | |
| 774 | // move the vregs into the appropriate architected register or stack slot |
| 775 | for(int i = 0, e = VRegs.size(); i < e; ++i) { |
| 776 | unsigned OperandType = N.getOperand(i+2).getValueType(); |
| 777 | switch(OperandType) { |
| 778 | default: |
| 779 | Node->dump(); |
| 780 | N.getOperand(i).Val->dump(); |
| 781 | std::cerr << "Type for " << i << " is: " << |
| 782 | N.getOperand(i+2).getValueType() << "\n"; |
| 783 | assert(0 && "Unknown value type for call"); |
| 784 | case MVT::i1: |
| 785 | case MVT::i8: |
| 786 | case MVT::i16: |
| 787 | case MVT::i32: |
| 788 | if (GPR_remaining > 0) |
| 789 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i]) |
| 790 | .addReg(VRegs[i]); |
| 791 | break; |
| 792 | case MVT::f32: |
| 793 | case MVT::f64: |
| 794 | if (FPR_remaining > 0) { |
| 795 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]); |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 796 | ++FPR_idx; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 797 | --FPR_remaining; |
| 798 | } |
| 799 | break; |
| 800 | } |
| 801 | // All arguments consume GPRs available for argument passing |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 802 | if (GPR_remaining > 0) { |
| 803 | ++GPR_idx; |
| 804 | --GPR_remaining; |
| 805 | } |
| 806 | if (MVT::f64 == OperandType && GPR_remaining > 0) { |
| 807 | ++GPR_idx; |
| 808 | --GPR_remaining; |
| 809 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Emit the correct call instruction based on the type of symbol called. |
| 813 | if (GlobalAddressSDNode *GASD = |
| 814 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 815 | BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true); |
| 816 | } else if (ExternalSymbolSDNode *ESSDN = |
| 817 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 818 | BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 819 | } else { |
| 820 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 821 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 822 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 823 | BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12); |
| 824 | } |
| 825 | |
| 826 | switch (Node->getValueType(0)) { |
| 827 | default: assert(0 && "Unknown value type for call result!"); |
| 828 | case MVT::Other: return 1; |
| 829 | case MVT::i1: |
| 830 | case MVT::i8: |
| 831 | case MVT::i16: |
| 832 | case MVT::i32: |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 833 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 834 | if (Node->getValueType(1) == MVT::i32) |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 835 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 836 | break; |
| 837 | case MVT::f32: |
| 838 | case MVT::f64: |
| 839 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 840 | break; |
| 841 | } |
| 842 | return Result+N.ResNo; |
| 843 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 844 | |
| 845 | case ISD::SIGN_EXTEND: |
| 846 | case ISD::SIGN_EXTEND_INREG: |
| 847 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 848 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 849 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
| 850 | case MVT::i16: |
| 851 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
| 852 | break; |
| 853 | case MVT::i8: |
| 854 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
| 855 | break; |
| 856 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 857 | return Result; |
| 858 | |
| 859 | case ISD::ZERO_EXTEND_INREG: |
| 860 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 861 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 862 | default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 863 | case MVT::i16: Tmp2 = 16; break; |
| 864 | case MVT::i8: Tmp2 = 24; break; |
| 865 | case MVT::i1: Tmp2 = 31; break; |
| 866 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame^] | 867 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2) |
| 868 | .addImm(31); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 869 | return Result; |
| 870 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 871 | case ISD::CopyFromReg: |
| 872 | if (Result == 1) |
| 873 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 874 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 875 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 876 | return Result; |
| 877 | |
| 878 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 879 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 880 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 881 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame^] | 882 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 883 | .addImm(31-Tmp2); |
| 884 | } else { |
| 885 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 886 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 887 | } |
| 888 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 889 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 890 | case ISD::SRL: |
| 891 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 892 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 893 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame^] | 894 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 895 | .addImm(Tmp2).addImm(31); |
| 896 | } else { |
| 897 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 898 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 899 | } |
| 900 | return Result; |
| 901 | |
| 902 | case ISD::SRA: |
| 903 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 904 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 905 | Tmp2 = CN->getValue() & 0x1F; |
| 906 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 907 | } else { |
| 908 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 909 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 910 | } |
| 911 | return Result; |
| 912 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 913 | case ISD::ADD: |
| 914 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 915 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 916 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 917 | default: assert(0 && "unhandled result code"); |
| 918 | case 0: // No immediate |
| 919 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 920 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 921 | break; |
| 922 | case 1: // Low immediate |
| 923 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 924 | break; |
| 925 | case 2: // Shifted immediate |
| 926 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 927 | break; |
| 928 | } |
| 929 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 930 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 931 | case ISD::AND: |
| 932 | case ISD::OR: |
| 933 | case ISD::XOR: |
| 934 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 935 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 936 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 937 | default: assert(0 && "unhandled result code"); |
| 938 | case 0: // No immediate |
| 939 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 940 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 941 | case ISD::AND: Opc = PPC::AND; break; |
| 942 | case ISD::OR: Opc = PPC::OR; break; |
| 943 | case ISD::XOR: Opc = PPC::XOR; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 944 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 945 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 946 | break; |
| 947 | case 1: // Low immediate |
| 948 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 949 | case ISD::AND: Opc = PPC::ANDIo; break; |
| 950 | case ISD::OR: Opc = PPC::ORI; break; |
| 951 | case ISD::XOR: Opc = PPC::XORI; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 952 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 953 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 954 | break; |
| 955 | case 2: // Shifted immediate |
| 956 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 957 | case ISD::AND: Opc = PPC::ANDISo; break; |
| 958 | case ISD::OR: Opc = PPC::ORIS; break; |
| 959 | case ISD::XOR: Opc = PPC::XORIS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 960 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 961 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 962 | break; |
| 963 | } |
| 964 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 965 | |
| 966 | case ISD::SUB: |
| 967 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 968 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 969 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 970 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 971 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 972 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 973 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 974 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 975 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 976 | if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) |
| 977 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 978 | else { |
| 979 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 980 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 981 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 982 | return Result; |
| 983 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 984 | case ISD::SDIV: |
| 985 | case ISD::UDIV: |
| 986 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 987 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 988 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 989 | Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 990 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 991 | return Result; |
| 992 | |
| 993 | case ISD::UREM: |
| 994 | case ISD::SREM: { |
| 995 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 996 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 997 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 998 | Tmp3 = MakeReg(MVT::i32); |
| 999 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1000 | Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW; |
| 1001 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 1002 | BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2); |
| 1003 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1); |
| 1004 | return Result; |
| 1005 | } |
| 1006 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1007 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1008 | case ISD::SUB_PARTS: { |
| 1009 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1010 | "Not an i64 add/sub!"); |
| 1011 | // Emit all of the operands. |
| 1012 | std::vector<unsigned> InVals; |
| 1013 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1014 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1015 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1016 | BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]); |
| 1017 | BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1018 | } else { |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 1019 | BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]); |
| 1020 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1021 | } |
| 1022 | return Result+N.ResNo; |
| 1023 | } |
| 1024 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1025 | case ISD::FP_TO_UINT: |
| 1026 | case ISD::FP_TO_SINT: |
| 1027 | abort(); |
| 1028 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1029 | case ISD::SETCC: |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame^] | 1030 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) { |
| 1031 | bool U = false; |
| 1032 | bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); |
| 1033 | |
| 1034 | switch (SetCC->getCondition()) { |
| 1035 | default: Node->dump(); assert(0 && "Unknown comparison!"); |
| 1036 | case ISD::SETEQ: Opc = PPC::BEQ; break; |
| 1037 | case ISD::SETNE: Opc = PPC::BNE; break; |
| 1038 | case ISD::SETULT: U = true; |
| 1039 | case ISD::SETLT: Opc = PPC::BLT; break; |
| 1040 | case ISD::SETULE: U = true; |
| 1041 | case ISD::SETLE: Opc = PPC::BLE; break; |
| 1042 | case ISD::SETUGT: U = true; |
| 1043 | case ISD::SETGT: Opc = PPC::BGT; break; |
| 1044 | case ISD::SETUGE: U = true; |
| 1045 | case ISD::SETGE: Opc = PPC::BGE; break; |
| 1046 | } |
| 1047 | |
| 1048 | // FIXME: Is there a situation in which we would ever need to emit fcmpo? |
| 1049 | static const unsigned CompareOpcodes[] = |
| 1050 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
| 1051 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
| 1052 | |
| 1053 | // Create an iterator with which to insert the MBB for copying the false |
| 1054 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1055 | MachineBasicBlock *thisMBB = BB; |
| 1056 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1057 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1058 | ++It; |
| 1059 | |
| 1060 | // thisMBB: |
| 1061 | // ... |
| 1062 | // cmpTY cr0, r1, r2 |
| 1063 | // %TrueValue = li 1 |
| 1064 | // bCC sinkMBB |
| 1065 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1066 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1067 | BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2); |
| 1068 | unsigned TrueValue = MakeReg(MVT::i32); |
| 1069 | BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1); |
| 1070 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1071 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1072 | BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB); |
| 1073 | MachineFunction *F = BB->getParent(); |
| 1074 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1075 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1076 | // Update machine-CFG edges |
| 1077 | BB->addSuccessor(copy0MBB); |
| 1078 | BB->addSuccessor(sinkMBB); |
| 1079 | |
| 1080 | // copy0MBB: |
| 1081 | // %FalseValue = li 0 |
| 1082 | // fallthrough |
| 1083 | BB = copy0MBB; |
| 1084 | unsigned FalseValue = MakeReg(MVT::i32); |
| 1085 | BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0); |
| 1086 | // Update machine-CFG edges |
| 1087 | BB->addSuccessor(sinkMBB); |
| 1088 | |
| 1089 | // sinkMBB: |
| 1090 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1091 | // ... |
| 1092 | BB = sinkMBB; |
| 1093 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1094 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
| 1095 | return Result; |
| 1096 | } |
| 1097 | assert(0 && "Is this legal?"); |
| 1098 | return 0; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1099 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1100 | case ISD::SELECT: |
| 1101 | abort(); |
| 1102 | |
| 1103 | case ISD::Constant: |
| 1104 | switch (N.getValueType()) { |
| 1105 | default: assert(0 && "Cannot use constants of this type!"); |
| 1106 | case MVT::i1: |
| 1107 | BuildMI(BB, PPC::LI, 1, Result) |
| 1108 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 1109 | break; |
| 1110 | case MVT::i32: |
| 1111 | { |
| 1112 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 1113 | if (v < 32768 && v >= -32768) { |
| 1114 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 1115 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1116 | Tmp1 = MakeReg(MVT::i32); |
| 1117 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 1118 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | return Result; |
| 1123 | } |
| 1124 | |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | void ISel::Select(SDOperand N) { |
| 1129 | unsigned Tmp1, Tmp2, Opc; |
| 1130 | unsigned opcode = N.getOpcode(); |
| 1131 | |
| 1132 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 1133 | return; // Already selected. |
| 1134 | |
| 1135 | SDNode *Node = N.Val; |
| 1136 | |
| 1137 | switch (Node->getOpcode()) { |
| 1138 | default: |
| 1139 | Node->dump(); std::cerr << "\n"; |
| 1140 | assert(0 && "Node not handled yet!"); |
| 1141 | case ISD::EntryToken: return; // Noop |
| 1142 | case ISD::TokenFactor: |
| 1143 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1144 | Select(Node->getOperand(i)); |
| 1145 | return; |
| 1146 | case ISD::ADJCALLSTACKDOWN: |
| 1147 | case ISD::ADJCALLSTACKUP: |
| 1148 | Select(N.getOperand(0)); |
| 1149 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 1150 | Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN : |
| 1151 | PPC::ADJCALLSTACKUP; |
| 1152 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 1153 | return; |
| 1154 | case ISD::BR: { |
| 1155 | MachineBasicBlock *Dest = |
| 1156 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1157 | Select(N.getOperand(0)); |
| 1158 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 1159 | return; |
| 1160 | } |
| 1161 | case ISD::BRCOND: |
| 1162 | SelectBranchCC(N); |
| 1163 | return; |
| 1164 | case ISD::CopyToReg: |
| 1165 | Select(N.getOperand(0)); |
| 1166 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1167 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
| 1168 | |
| 1169 | if (Tmp1 != Tmp2) { |
| 1170 | if (N.getOperand(1).getValueType() == MVT::f64 || |
| 1171 | N.getOperand(1).getValueType() == MVT::f32) |
| 1172 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 1173 | else |
| 1174 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1175 | } |
| 1176 | return; |
| 1177 | case ISD::ImplicitDef: |
| 1178 | Select(N.getOperand(0)); |
| 1179 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 1180 | return; |
| 1181 | case ISD::RET: |
| 1182 | switch (N.getNumOperands()) { |
| 1183 | default: |
| 1184 | assert(0 && "Unknown return instruction!"); |
| 1185 | case 3: |
| 1186 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1187 | N.getOperand(2).getValueType() == MVT::i32 && |
| 1188 | "Unknown two-register value!"); |
| 1189 | Select(N.getOperand(0)); |
| 1190 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1191 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 1192 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1193 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2); |
| 1194 | break; |
| 1195 | case 2: |
| 1196 | Select(N.getOperand(0)); |
| 1197 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1198 | switch (N.getOperand(1).getValueType()) { |
| 1199 | default: |
| 1200 | assert(0 && "Unknown return type!"); |
| 1201 | case MVT::f64: |
| 1202 | case MVT::f32: |
| 1203 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 1204 | break; |
| 1205 | case MVT::i32: |
| 1206 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1207 | break; |
| 1208 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1209 | case 1: |
| 1210 | Select(N.getOperand(0)); |
| 1211 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1212 | } |
| 1213 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 1214 | return; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1215 | case ISD::TRUNCSTORE: |
| 1216 | case ISD::STORE: |
| 1217 | { |
| 1218 | SDOperand Chain = N.getOperand(0); |
| 1219 | SDOperand Value = N.getOperand(1); |
| 1220 | SDOperand Address = N.getOperand(2); |
| 1221 | Select(Chain); |
| 1222 | |
| 1223 | Tmp1 = SelectExpr(Value); //value |
| 1224 | |
| 1225 | if (opcode == ISD::STORE) { |
| 1226 | switch(Value.getValueType()) { |
| 1227 | default: assert(0 && "unknown Type in store"); |
| 1228 | case MVT::i32: Opc = PPC::STW; break; |
| 1229 | case MVT::f64: Opc = PPC::STFD; break; |
| 1230 | case MVT::f32: Opc = PPC::STFS; break; |
| 1231 | } |
| 1232 | } else { //ISD::TRUNCSTORE |
| 1233 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 1234 | default: assert(0 && "unknown Type in store"); |
| 1235 | case MVT::i1: //FIXME: DAG does not promote this load |
| 1236 | case MVT::i8: Opc = PPC::STB; break; |
| 1237 | case MVT::i16: Opc = PPC::STH; break; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | if (Address.getOpcode() == ISD::GlobalAddress) |
| 1242 | { |
| 1243 | BuildMI(BB, Opc, 2).addReg(Tmp1) |
| 1244 | .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal()); |
| 1245 | } |
| 1246 | else if(Address.getOpcode() == ISD::FrameIndex) |
| 1247 | { |
| 1248 | BuildMI(BB, Opc, 2).addReg(Tmp1) |
| 1249 | .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex()); |
| 1250 | } |
| 1251 | else |
| 1252 | { |
| 1253 | int offset; |
| 1254 | SelectAddr(Address, Tmp2, offset); |
| 1255 | BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2); |
| 1256 | } |
| 1257 | return; |
| 1258 | } |
| 1259 | case ISD::EXTLOAD: |
| 1260 | case ISD::SEXTLOAD: |
| 1261 | case ISD::ZEXTLOAD: |
| 1262 | case ISD::LOAD: |
| 1263 | case ISD::CopyFromReg: |
| 1264 | case ISD::CALL: |
| 1265 | case ISD::DYNAMIC_STACKALLOC: |
| 1266 | ExprMap.erase(N); |
| 1267 | SelectExpr(N); |
| 1268 | return; |
| 1269 | } |
| 1270 | assert(0 && "Should not be reached!"); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1275 | /// into a machine code representation using pattern matching and a machine |
| 1276 | /// description file. |
| 1277 | /// |
| 1278 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
| 1279 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |