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); |
| 47 | addRegisterClass(MVT::f32, PPC32::GPRCRegisterClass); |
| 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> |
| 61 | LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee, |
| 62 | ArgListTy &Args, SelectionDAG &DAG); |
| 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]); |
| 123 | unsigned virtReg = |
| 124 | MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32)); |
| 125 | argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot()); |
| 126 | if (ObjectVT != MVT::i32) |
| 127 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
| 128 | argVR.push_back(virtReg); |
| 129 | argPR.push_back(GPR[GPR_idx]); |
| 130 | argOp.push_back(PPC::OR); |
| 131 | } else { |
| 132 | needsLoad = true; |
| 133 | } |
| 134 | break; |
| 135 | case MVT::i64: ObjSize = 8; |
| 136 | if (GPR_remaining > 1) { |
| 137 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 138 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]); |
| 139 | MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32)); |
| 140 | unsigned virtReg = |
| 141 | MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i32))-1; |
| 142 | // FIXME: is this correct? |
| 143 | argt = newroot = DAG.getCopyFromReg(virtReg, MVT::i32, DAG.getRoot()); |
| 144 | argt = DAG.getCopyFromReg(virtReg+1, MVT::i32, newroot); |
| 145 | // Push the arguments for emitting into BB later |
| 146 | argVR.push_back(virtReg); argVR.push_back(virtReg+1); |
| 147 | argPR.push_back(GPR[GPR_idx]); argPR.push_back(GPR[GPR_idx+1]); |
| 148 | argOp.push_back(PPC::OR); argOp.push_back(PPC::OR); |
| 149 | } else { |
| 150 | needsLoad = true; |
| 151 | } |
| 152 | break; |
| 153 | case MVT::f32: ObjSize = 4; |
| 154 | case MVT::f64: ObjSize = 8; |
| 155 | if (FPR_remaining > 0) { |
| 156 | BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]); |
| 157 | unsigned virtReg = |
| 158 | MF.getSSARegMap()->createVirtualRegister(getRegClassFor(ObjectVT)); |
| 159 | argt = newroot = DAG.getCopyFromReg(virtReg, ObjectVT, DAG.getRoot()); |
| 160 | argVR.push_back(virtReg); |
| 161 | argPR.push_back(FPR[FPR_idx]); |
| 162 | argOp.push_back(PPC::FMR); |
| 163 | --FPR_remaining; |
| 164 | ++FPR_idx; |
| 165 | } else { |
| 166 | needsLoad = true; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | // We need to load the argument to a virtual register if we determined above |
| 172 | // that we ran out of physical registers of the appropriate type |
| 173 | if (needsLoad) { |
| 174 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 175 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 176 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN); |
| 177 | } |
| 178 | |
| 179 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 180 | // argument passing. |
| 181 | if (GPR_remaining > 0) { |
| 182 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 183 | GPR_remaining -= delta; |
| 184 | GPR_idx += delta; |
| 185 | } |
| 186 | ArgOffset += ObjSize; |
| 187 | |
| 188 | DAG.setRoot(newroot.getValue(1)); |
| 189 | ArgValues.push_back(argt); |
| 190 | } |
| 191 | |
| 192 | for (int i = 0, count = argVR.size(); i < count; ++i) { |
| 193 | if (argOp[i] == PPC::FMR) |
| 194 | BuildMI(&BB, argOp[i], 1, argVR[i]).addReg(argPR[i]); |
| 195 | else |
| 196 | BuildMI(&BB, argOp[i], 2, argVR[i]).addReg(argPR[i]).addReg(argPR[i]); |
| 197 | } |
| 198 | |
| 199 | // If the function takes variable number of arguments, make a frame index for |
| 200 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 201 | if (F.isVarArg()) |
| 202 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 203 | |
| 204 | return ArgValues; |
| 205 | } |
| 206 | |
| 207 | std::pair<SDOperand, SDOperand> |
| 208 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
| 209 | const Type *RetTy, SDOperand Callee, |
| 210 | ArgListTy &Args, SelectionDAG &DAG) { |
| 211 | // FIXME |
| 212 | int NumBytes = 56; |
| 213 | |
| 214 | Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain, |
| 215 | DAG.getConstant(NumBytes, getPointerTy())); |
| 216 | std::vector<SDOperand> args_to_use; |
| 217 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 218 | { |
| 219 | switch (getValueType(Args[i].second)) { |
| 220 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 221 | case MVT::i1: |
| 222 | case MVT::i8: |
| 223 | case MVT::i16: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 224 | // Promote the integer to 32 bits. If the input type is signed use a |
| 225 | // sign extend, otherwise use a zero extend. |
| 226 | if (Args[i].second->isSigned()) |
| 227 | Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 228 | else |
| 229 | Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 230 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 231 | case MVT::i32: |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 232 | case MVT::i64: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 233 | case MVT::f32: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 234 | case MVT::f64: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | args_to_use.push_back(Args[i].first); |
| 238 | } |
| 239 | |
| 240 | std::vector<MVT::ValueType> RetVals; |
| 241 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 242 | if (RetTyVT != MVT::isVoid) |
| 243 | RetVals.push_back(RetTyVT); |
| 244 | RetVals.push_back(MVT::Other); |
| 245 | |
| 246 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
| 247 | Chain, Callee, args_to_use), 0); |
| 248 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
| 249 | Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain, |
| 250 | DAG.getConstant(NumBytes, getPointerTy())); |
| 251 | return std::make_pair(TheCall, Chain); |
| 252 | } |
| 253 | |
| 254 | std::pair<SDOperand, SDOperand> |
| 255 | PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) { |
| 256 | //vastart just returns the address of the VarArgsFrameIndex slot. |
| 257 | return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain); |
| 258 | } |
| 259 | |
| 260 | std::pair<SDOperand,SDOperand> PPC32TargetLowering:: |
| 261 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 262 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 263 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 264 | SDOperand Result; |
| 265 | if (!isVANext) { |
| 266 | Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList); |
| 267 | } else { |
| 268 | unsigned Amt; |
| 269 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 270 | Amt = 4; |
| 271 | else { |
| 272 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 273 | "Other types should have been promoted for varargs!"); |
| 274 | Amt = 8; |
| 275 | } |
| 276 | Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 277 | DAG.getConstant(Amt, VAList.getValueType())); |
| 278 | } |
| 279 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 284 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 285 | SelectionDAG &DAG) { |
| 286 | abort(); |
| 287 | } |
| 288 | |
| 289 | namespace { |
| 290 | |
| 291 | //===--------------------------------------------------------------------===// |
| 292 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 293 | /// SelectionDAG operations. |
| 294 | //===--------------------------------------------------------------------===// |
| 295 | class ISel : public SelectionDAGISel { |
| 296 | |
| 297 | /// Comment Here. |
| 298 | PPC32TargetLowering PPC32Lowering; |
| 299 | |
| 300 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 301 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 302 | /// tree. |
| 303 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 304 | |
| 305 | unsigned GlobalBaseReg; |
| 306 | bool GlobalBaseInitialized; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 307 | |
| 308 | public: |
| 309 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) |
| 310 | {} |
| 311 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 312 | /// runOnFunction - Override this function in order to reset our per-function |
| 313 | /// variables. |
| 314 | virtual bool runOnFunction(Function &Fn) { |
| 315 | // Make sure we re-emit a set of the global base reg if necessary |
| 316 | GlobalBaseInitialized = false; |
| 317 | return SelectionDAGISel::runOnFunction(Fn); |
| 318 | } |
| 319 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 320 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 321 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 322 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 323 | DEBUG(BB->dump()); |
| 324 | // Codegen the basic block. |
| 325 | Select(DAG.getRoot()); |
| 326 | |
| 327 | // Clear state used for selection. |
| 328 | ExprMap.clear(); |
| 329 | } |
| 330 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 331 | unsigned ISel::getGlobalBaseReg(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 332 | unsigned SelectExpr(SDOperand N); |
| 333 | unsigned SelectExprFP(SDOperand N, unsigned Result); |
| 334 | void Select(SDOperand N); |
| 335 | |
| 336 | void SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
| 337 | void SelectBranchCC(SDOperand N); |
| 338 | }; |
| 339 | |
| 340 | /// canUseAsImmediateForOpcode - This method returns a value indicating whether |
| 341 | /// the ConstantSDNode N can be used as an immediate to Opcode. The return |
| 342 | /// values are either 0, 1 or 2. 0 indicates that either N is not a |
| 343 | /// ConstantSDNode, or is not suitable for use by that opcode. A return value |
| 344 | /// of 1 indicates that the constant may be used in normal immediate form. A |
| 345 | /// return value of 2 indicates that the constant may be used in shifted |
| 346 | /// immediate form. If the return value is nonzero, the constant value is |
| 347 | /// placed in Imm. |
| 348 | /// |
| 349 | static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode, |
| 350 | unsigned& Imm) { |
| 351 | if (N.getOpcode() != ISD::Constant) return 0; |
| 352 | |
| 353 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 354 | |
| 355 | switch(Opcode) { |
| 356 | default: return 0; |
| 357 | case ISD::ADD: |
| 358 | if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; } |
| 359 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 360 | break; |
| 361 | case ISD::AND: |
| 362 | case ISD::XOR: |
| 363 | case ISD::OR: |
| 364 | if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; } |
| 365 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 366 | break; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | } |
| 371 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 372 | /// getGlobalBaseReg - Output the instructions required to put the |
| 373 | /// base address to use for accessing globals into a register. |
| 374 | /// |
| 375 | unsigned ISel::getGlobalBaseReg() { |
| 376 | if (!GlobalBaseInitialized) { |
| 377 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 378 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 379 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 380 | GlobalBaseReg = MakeReg(MVT::i32); |
| 381 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 382 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 383 | GlobalBaseInitialized = true; |
| 384 | } |
| 385 | return GlobalBaseReg; |
| 386 | } |
| 387 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 388 | //Check to see if the load is a constant offset from a base register |
| 389 | void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
| 390 | { |
| 391 | Reg = SelectExpr(N); |
| 392 | offset = 0; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | void ISel::SelectBranchCC(SDOperand N) |
| 397 | { |
| 398 | assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???"); |
| 399 | MachineBasicBlock *Dest = |
| 400 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
| 401 | unsigned Opc; |
| 402 | |
| 403 | Select(N.getOperand(0)); //chain |
| 404 | SDOperand CC = N.getOperand(1); |
| 405 | |
| 406 | //Giveup and do the stupid thing |
| 407 | unsigned Tmp1 = SelectExpr(CC); |
| 408 | BuildMI(BB, PPC::BNE, 2).addReg(Tmp1).addMBB(Dest); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | unsigned ISel::SelectExprFP(SDOperand N, unsigned Result) |
| 413 | { |
| 414 | unsigned Tmp1, Tmp2, Tmp3; |
| 415 | unsigned Opc = 0; |
| 416 | SDNode *Node = N.Val; |
| 417 | MVT::ValueType DestType = N.getValueType(); |
| 418 | unsigned opcode = N.getOpcode(); |
| 419 | |
| 420 | switch (opcode) { |
| 421 | default: |
| 422 | Node->dump(); |
| 423 | assert(0 && "Node not handled!\n"); |
| 424 | |
| 425 | case ISD::SELECT: |
| 426 | abort(); |
| 427 | |
| 428 | case ISD::FP_ROUND: |
| 429 | assert (DestType == MVT::f32 && |
| 430 | N.getOperand(0).getValueType() == MVT::f64 && |
| 431 | "only f64 to f32 conversion supported here"); |
| 432 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 433 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 434 | return Result; |
| 435 | |
| 436 | case ISD::FP_EXTEND: |
| 437 | assert (DestType == MVT::f64 && |
| 438 | N.getOperand(0).getValueType() == MVT::f32 && |
| 439 | "only f32 to f64 conversion supported here"); |
| 440 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 441 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 442 | return Result; |
| 443 | |
| 444 | case ISD::CopyFromReg: |
| 445 | // FIXME: Handle copy from physregs! |
| 446 | // Just use the specified register as our input. |
| 447 | return dyn_cast<RegSDNode>(Node)->getReg(); |
| 448 | |
| 449 | case ISD::LOAD: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 450 | case ISD::EXTLOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 451 | abort(); |
| 452 | |
| 453 | case ISD::ConstantFP: |
| 454 | abort(); |
| 455 | |
| 456 | case ISD::MUL: |
| 457 | case ISD::ADD: |
| 458 | case ISD::SUB: |
| 459 | case ISD::SDIV: |
| 460 | switch( opcode ) { |
| 461 | case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break; |
| 462 | case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break; |
| 463 | case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break; |
| 464 | case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break; |
| 465 | }; |
| 466 | |
| 467 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 468 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 469 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 470 | return Result; |
| 471 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 472 | case ISD::UINT_TO_FP: |
| 473 | case ISD::SINT_TO_FP: |
| 474 | abort(); |
| 475 | } |
| 476 | assert(0 && "should not get here"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | unsigned ISel::SelectExpr(SDOperand N) { |
| 481 | unsigned Result; |
| 482 | unsigned Tmp1, Tmp2, Tmp3; |
| 483 | unsigned Opc = 0; |
| 484 | unsigned opcode = N.getOpcode(); |
| 485 | |
| 486 | SDNode *Node = N.Val; |
| 487 | MVT::ValueType DestType = N.getValueType(); |
| 488 | |
| 489 | unsigned &Reg = ExprMap[N]; |
| 490 | if (Reg) return Reg; |
| 491 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 492 | if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS && |
| 493 | N.getOpcode() != ISD::SUB_PARTS) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 494 | Reg = Result = (N.getValueType() != MVT::Other) ? |
| 495 | MakeReg(N.getValueType()) : 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 496 | else { |
| 497 | // If this is a call instruction, make sure to prepare ALL of the result |
| 498 | // values as well as the chain. |
| 499 | if (N.getOpcode() == ISD::CALL) { |
| 500 | if (Node->getNumValues() == 1) |
| 501 | Reg = Result = 1; // Void call, just a chain. |
| 502 | else { |
| 503 | Result = MakeReg(Node->getValueType(0)); |
| 504 | ExprMap[N.getValue(0)] = Result; |
| 505 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
| 506 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 507 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
| 508 | } |
| 509 | } else { |
| 510 | Result = MakeReg(Node->getValueType(0)); |
| 511 | ExprMap[N.getValue(0)] = Result; |
| 512 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 513 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (DestType == MVT::f64 || DestType == MVT::f32) |
| 518 | return SelectExprFP(N, Result); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 519 | |
| 520 | switch (opcode) { |
| 521 | default: |
| 522 | Node->dump(); |
| 523 | assert(0 && "Node not handled!\n"); |
| 524 | |
| 525 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 526 | // Generate both result values. FIXME: Need a better commment here? |
| 527 | if (Result != 1) |
| 528 | ExprMap[N.getValue(1)] = 1; |
| 529 | else |
| 530 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 531 | |
| 532 | // FIXME: We are currently ignoring the requested alignment for handling |
| 533 | // greater than the stack alignment. This will need to be revisited at some |
| 534 | // point. Align = N.getOperand(2); |
| 535 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 536 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 537 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 538 | << " the stack alignment yet!"; |
| 539 | abort(); |
| 540 | } |
| 541 | Select(N.getOperand(0)); |
| 542 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 543 | // Subtract size from stack pointer, thereby allocating some space. |
| 544 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 545 | // Put a pointer to the space into the result register by copying the SP |
| 546 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 547 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 548 | |
| 549 | case ISD::ConstantPool: |
| 550 | abort(); |
| 551 | |
| 552 | case ISD::FrameIndex: |
| 553 | abort(); |
| 554 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 555 | case ISD::GlobalAddress: { |
| 556 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 557 | unsigned Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 558 | BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 559 | .addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 560 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 561 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 562 | } else { |
| 563 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 564 | } |
| 565 | return Result; |
| 566 | } |
| 567 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 568 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 569 | case ISD::EXTLOAD: |
| 570 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 571 | case ISD::SEXTLOAD: { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 572 | // Make sure we generate both values. |
| 573 | if (Result != 1) |
| 574 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 575 | else |
| 576 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 577 | |
| 578 | SDOperand Chain = N.getOperand(0); |
| 579 | SDOperand Address = N.getOperand(1); |
| 580 | Select(Chain); |
| 581 | |
| 582 | switch (Node->getValueType(0)) { |
| 583 | default: assert(0 && "Cannot load this type!"); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 584 | case MVT::i1: Opc = PPC::LBZ; Tmp3 = 0; break; |
| 585 | case MVT::i8: Opc = PPC::LBZ; Tmp3 = 1; break; |
| 586 | case MVT::i16: Opc = PPC::LHZ; Tmp3 = 0; break; |
| 587 | case MVT::i32: Opc = PPC::LWZ; Tmp3 = 0; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 590 | if(Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 591 | BuildMI(BB, Opc, 2, Result) |
| 592 | .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex()) |
| 593 | .addReg(PPC::R1); |
| 594 | } else { |
| 595 | int offset; |
| 596 | SelectAddr(Address, Tmp1, offset); |
| 597 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 598 | } |
| 599 | return Result; |
| 600 | } |
| 601 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 602 | case ISD::CALL: { |
| 603 | // Lower the chain for this call. |
| 604 | Select(N.getOperand(0)); |
| 605 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
| 606 | |
| 607 | // get the virtual reg for each argument |
| 608 | std::vector<unsigned> VRegs; |
| 609 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
| 610 | VRegs.push_back(SelectExpr(N.getOperand(i))); |
| 611 | |
| 612 | // The ABI specifies that the first 32 bytes of args may be passed in GPRs, |
| 613 | // and that 13 FPRs may also be used for passing any floating point args. |
| 614 | int GPR_remaining = 8, FPR_remaining = 13; |
| 615 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 616 | static const unsigned GPR[] = { |
| 617 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 618 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 619 | }; |
| 620 | static const unsigned FPR[] = { |
| 621 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, |
| 622 | PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, |
| 623 | PPC::F13 |
| 624 | }; |
| 625 | |
| 626 | // move the vregs into the appropriate architected register or stack slot |
| 627 | for(int i = 0, e = VRegs.size(); i < e; ++i) { |
| 628 | unsigned OperandType = N.getOperand(i+2).getValueType(); |
| 629 | switch(OperandType) { |
| 630 | default: |
| 631 | Node->dump(); |
| 632 | N.getOperand(i).Val->dump(); |
| 633 | std::cerr << "Type for " << i << " is: " << |
| 634 | N.getOperand(i+2).getValueType() << "\n"; |
| 635 | assert(0 && "Unknown value type for call"); |
| 636 | case MVT::i1: |
| 637 | case MVT::i8: |
| 638 | case MVT::i16: |
| 639 | case MVT::i32: |
| 640 | if (GPR_remaining > 0) |
| 641 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(VRegs[i]) |
| 642 | .addReg(VRegs[i]); |
| 643 | break; |
| 644 | case MVT::f32: |
| 645 | case MVT::f64: |
| 646 | if (FPR_remaining > 0) { |
| 647 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(VRegs[i]); |
| 648 | --FPR_remaining; |
| 649 | } |
| 650 | break; |
| 651 | } |
| 652 | // All arguments consume GPRs available for argument passing |
| 653 | if (GPR_remaining > 0) --GPR_remaining; |
| 654 | if (MVT::f64 == OperandType && GPR_remaining > 0) --GPR_remaining; |
| 655 | } |
| 656 | |
| 657 | // Emit the correct call instruction based on the type of symbol called. |
| 658 | if (GlobalAddressSDNode *GASD = |
| 659 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 660 | BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true); |
| 661 | } else if (ExternalSymbolSDNode *ESSDN = |
| 662 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 663 | BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 664 | } else { |
| 665 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 666 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 667 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 668 | BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12); |
| 669 | } |
| 670 | |
| 671 | switch (Node->getValueType(0)) { |
| 672 | default: assert(0 && "Unknown value type for call result!"); |
| 673 | case MVT::Other: return 1; |
| 674 | case MVT::i1: |
| 675 | case MVT::i8: |
| 676 | case MVT::i16: |
| 677 | case MVT::i32: |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 678 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 679 | if (Node->getValueType(1) == MVT::i32) |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame^] | 680 | 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] | 681 | break; |
| 682 | case MVT::f32: |
| 683 | case MVT::f64: |
| 684 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 685 | break; |
| 686 | } |
| 687 | return Result+N.ResNo; |
| 688 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 689 | |
| 690 | case ISD::SIGN_EXTEND: |
| 691 | case ISD::SIGN_EXTEND_INREG: |
| 692 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 693 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
| 694 | return Result; |
| 695 | |
| 696 | case ISD::ZERO_EXTEND_INREG: |
| 697 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 698 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 699 | default: |
| 700 | Node->dump(); |
| 701 | assert(0 && "Zero Extend InReg not there yet"); |
| 702 | break; |
| 703 | case MVT::i16: Tmp2 = 16; break; |
| 704 | case MVT::i8: Tmp2 = 24; break; |
| 705 | case MVT::i1: Tmp2 = 31; break; |
| 706 | } |
| 707 | BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(0).addImm(0) |
| 708 | .addImm(Tmp2).addImm(31); |
| 709 | return Result; |
| 710 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 711 | case ISD::CopyFromReg: |
| 712 | if (Result == 1) |
| 713 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 714 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
| 715 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 716 | return Result; |
| 717 | |
| 718 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 719 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 720 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 721 | Tmp2 = CN->getValue() & 0x1F; |
| 722 | BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
| 723 | .addImm(31-Tmp2); |
| 724 | } else { |
| 725 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 726 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 727 | } |
| 728 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 729 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 730 | case ISD::SRL: |
| 731 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 732 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 733 | Tmp2 = CN->getValue() & 0x1F; |
| 734 | BuildMI(BB, PPC::RLWINM, 5, Result).addReg(Tmp1).addImm(32-Tmp2) |
| 735 | .addImm(Tmp2).addImm(31); |
| 736 | } else { |
| 737 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 738 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 739 | } |
| 740 | return Result; |
| 741 | |
| 742 | case ISD::SRA: |
| 743 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 744 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 745 | Tmp2 = CN->getValue() & 0x1F; |
| 746 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 747 | } else { |
| 748 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 749 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 750 | } |
| 751 | return Result; |
| 752 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 753 | case ISD::ADD: |
| 754 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 755 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 756 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 757 | default: assert(0 && "unhandled result code"); |
| 758 | case 0: // No immediate |
| 759 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 760 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 761 | break; |
| 762 | case 1: // Low immediate |
| 763 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 764 | break; |
| 765 | case 2: // Shifted immediate |
| 766 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 767 | break; |
| 768 | } |
| 769 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 770 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 771 | case ISD::AND: |
| 772 | case ISD::OR: |
| 773 | case ISD::XOR: |
| 774 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 775 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 776 | switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 777 | default: assert(0 && "unhandled result code"); |
| 778 | case 0: // No immediate |
| 779 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 780 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 781 | case ISD::AND: Opc = PPC::AND; break; |
| 782 | case ISD::OR: Opc = PPC::OR; break; |
| 783 | case ISD::XOR: Opc = PPC::XOR; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 784 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 785 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 786 | break; |
| 787 | case 1: // Low immediate |
| 788 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 789 | case ISD::AND: Opc = PPC::ANDIo; break; |
| 790 | case ISD::OR: Opc = PPC::ORI; break; |
| 791 | case ISD::XOR: Opc = PPC::XORI; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 792 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 793 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 794 | break; |
| 795 | case 2: // Shifted immediate |
| 796 | switch (opcode) { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 797 | case ISD::AND: Opc = PPC::ANDISo; break; |
| 798 | case ISD::OR: Opc = PPC::ORIS; break; |
| 799 | case ISD::XOR: Opc = PPC::XORIS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 800 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 801 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
| 804 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 805 | |
| 806 | case ISD::SUB: |
| 807 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 808 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 809 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 810 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 811 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 812 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 813 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 814 | assert (DestType == MVT::i32 && "Only do arithmetic on i32s!"); |
| 815 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 816 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 817 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 818 | return Result; |
| 819 | |
| 820 | case ISD::ADD_PARTS: |
| 821 | case ISD::SUB_PARTS: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 822 | case ISD::UREM: |
| 823 | case ISD::SREM: |
| 824 | case ISD::SDIV: |
| 825 | case ISD::UDIV: |
| 826 | abort(); |
| 827 | |
| 828 | case ISD::FP_TO_UINT: |
| 829 | case ISD::FP_TO_SINT: |
| 830 | abort(); |
| 831 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 832 | case ISD::SETCC: |
| 833 | abort(); |
| 834 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 835 | case ISD::SELECT: |
| 836 | abort(); |
| 837 | |
| 838 | case ISD::Constant: |
| 839 | switch (N.getValueType()) { |
| 840 | default: assert(0 && "Cannot use constants of this type!"); |
| 841 | case MVT::i1: |
| 842 | BuildMI(BB, PPC::LI, 1, Result) |
| 843 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 844 | break; |
| 845 | case MVT::i32: |
| 846 | { |
| 847 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 848 | if (v < 32768 && v >= -32768) { |
| 849 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 850 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 851 | Tmp1 = MakeReg(MVT::i32); |
| 852 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 853 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | } |
| 857 | return Result; |
| 858 | } |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | void ISel::Select(SDOperand N) { |
| 864 | unsigned Tmp1, Tmp2, Opc; |
| 865 | unsigned opcode = N.getOpcode(); |
| 866 | |
| 867 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 868 | return; // Already selected. |
| 869 | |
| 870 | SDNode *Node = N.Val; |
| 871 | |
| 872 | switch (Node->getOpcode()) { |
| 873 | default: |
| 874 | Node->dump(); std::cerr << "\n"; |
| 875 | assert(0 && "Node not handled yet!"); |
| 876 | case ISD::EntryToken: return; // Noop |
| 877 | case ISD::TokenFactor: |
| 878 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 879 | Select(Node->getOperand(i)); |
| 880 | return; |
| 881 | case ISD::ADJCALLSTACKDOWN: |
| 882 | case ISD::ADJCALLSTACKUP: |
| 883 | Select(N.getOperand(0)); |
| 884 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 885 | Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN : |
| 886 | PPC::ADJCALLSTACKUP; |
| 887 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 888 | return; |
| 889 | case ISD::BR: { |
| 890 | MachineBasicBlock *Dest = |
| 891 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 892 | Select(N.getOperand(0)); |
| 893 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 894 | return; |
| 895 | } |
| 896 | case ISD::BRCOND: |
| 897 | SelectBranchCC(N); |
| 898 | return; |
| 899 | case ISD::CopyToReg: |
| 900 | Select(N.getOperand(0)); |
| 901 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 902 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
| 903 | |
| 904 | if (Tmp1 != Tmp2) { |
| 905 | if (N.getOperand(1).getValueType() == MVT::f64 || |
| 906 | N.getOperand(1).getValueType() == MVT::f32) |
| 907 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 908 | else |
| 909 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 910 | } |
| 911 | return; |
| 912 | case ISD::ImplicitDef: |
| 913 | Select(N.getOperand(0)); |
| 914 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 915 | return; |
| 916 | case ISD::RET: |
| 917 | switch (N.getNumOperands()) { |
| 918 | default: |
| 919 | assert(0 && "Unknown return instruction!"); |
| 920 | case 3: |
| 921 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 922 | N.getOperand(2).getValueType() == MVT::i32 && |
| 923 | "Unknown two-register value!"); |
| 924 | Select(N.getOperand(0)); |
| 925 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 926 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 927 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 928 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2); |
| 929 | break; |
| 930 | case 2: |
| 931 | Select(N.getOperand(0)); |
| 932 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 933 | switch (N.getOperand(1).getValueType()) { |
| 934 | default: |
| 935 | assert(0 && "Unknown return type!"); |
| 936 | case MVT::f64: |
| 937 | case MVT::f32: |
| 938 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 939 | break; |
| 940 | case MVT::i32: |
| 941 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 942 | break; |
| 943 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 944 | case 1: |
| 945 | Select(N.getOperand(0)); |
| 946 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 947 | } |
| 948 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 949 | return; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 950 | case ISD::TRUNCSTORE: |
| 951 | case ISD::STORE: |
| 952 | { |
| 953 | SDOperand Chain = N.getOperand(0); |
| 954 | SDOperand Value = N.getOperand(1); |
| 955 | SDOperand Address = N.getOperand(2); |
| 956 | Select(Chain); |
| 957 | |
| 958 | Tmp1 = SelectExpr(Value); //value |
| 959 | |
| 960 | if (opcode == ISD::STORE) { |
| 961 | switch(Value.getValueType()) { |
| 962 | default: assert(0 && "unknown Type in store"); |
| 963 | case MVT::i32: Opc = PPC::STW; break; |
| 964 | case MVT::f64: Opc = PPC::STFD; break; |
| 965 | case MVT::f32: Opc = PPC::STFS; break; |
| 966 | } |
| 967 | } else { //ISD::TRUNCSTORE |
| 968 | switch(cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 969 | default: assert(0 && "unknown Type in store"); |
| 970 | case MVT::i1: //FIXME: DAG does not promote this load |
| 971 | case MVT::i8: Opc = PPC::STB; break; |
| 972 | case MVT::i16: Opc = PPC::STH; break; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if (Address.getOpcode() == ISD::GlobalAddress) |
| 977 | { |
| 978 | BuildMI(BB, Opc, 2).addReg(Tmp1) |
| 979 | .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal()); |
| 980 | } |
| 981 | else if(Address.getOpcode() == ISD::FrameIndex) |
| 982 | { |
| 983 | BuildMI(BB, Opc, 2).addReg(Tmp1) |
| 984 | .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex()); |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | int offset; |
| 989 | SelectAddr(Address, Tmp2, offset); |
| 990 | BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2); |
| 991 | } |
| 992 | return; |
| 993 | } |
| 994 | case ISD::EXTLOAD: |
| 995 | case ISD::SEXTLOAD: |
| 996 | case ISD::ZEXTLOAD: |
| 997 | case ISD::LOAD: |
| 998 | case ISD::CopyFromReg: |
| 999 | case ISD::CALL: |
| 1000 | case ISD::DYNAMIC_STACKALLOC: |
| 1001 | ExprMap.erase(N); |
| 1002 | SelectExpr(N); |
| 1003 | return; |
| 1004 | } |
| 1005 | assert(0 && "Should not be reached!"); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1010 | /// into a machine code representation using pattern matching and a machine |
| 1011 | /// description file. |
| 1012 | /// |
| 1013 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
| 1014 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |