1 //===-- PPC64ISelPattern.cpp - A pattern matching inst selector for PPC64 -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Begeman and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for 64 bit PowerPC.
12 //===----------------------------------------------------------------------===//
15 #include "PowerPCInstrBuilder.h"
16 #include "PowerPCInstrInfo.h"
17 #include "PPC64RegisterInfo.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/Target/TargetOptions.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/ADT/Statistic.h"
36 //===----------------------------------------------------------------------===//
37 // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
39 class PPC64TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
41 int ReturnAddrIndex; // FrameIndex for return slot.
43 PPC64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Fold away setcc operations if possible.
45 setSetCCIsExpensive();
47 // Set up the register classes.
48 addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
49 addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
50 addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
52 // PowerPC has no intrinsics for these particular operations
53 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
54 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
55 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
56 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
58 // We don't support sin/cos/sqrt/fmod
59 setOperationAction(ISD::FSIN , MVT::f64, Expand);
60 setOperationAction(ISD::FCOS , MVT::f64, Expand);
61 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
62 setOperationAction(ISD::SREM , MVT::f64, Expand);
63 setOperationAction(ISD::FSIN , MVT::f32, Expand);
64 setOperationAction(ISD::FCOS , MVT::f32, Expand);
65 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
66 setOperationAction(ISD::SREM , MVT::f32, Expand);
68 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
69 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
70 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
72 // PowerPC has no SREM/UREM instructions
73 setOperationAction(ISD::SREM, MVT::i64, Expand);
74 setOperationAction(ISD::UREM, MVT::i64, Expand);
76 // PowerPC has these, but they are not implemented
77 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
78 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
79 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
81 setShiftAmountFlavor(Extend); // shl X, 32 == 0
82 addLegalFPImmediate(+0.0); // Necessary for FSEL
83 addLegalFPImmediate(-0.0); //
85 computeRegisterProperties();
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
93 /// LowerCallTo - This hook lowers an abstract call to a function into an
95 virtual std::pair<SDOperand, SDOperand>
96 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
97 bool isTailCall, SDOperand Callee, ArgListTy &Args,
100 virtual std::pair<SDOperand, SDOperand>
101 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
103 virtual std::pair<SDOperand,SDOperand>
104 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
105 const Type *ArgTy, SelectionDAG &DAG);
107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
114 std::vector<SDOperand>
115 PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
117 // add beautiful description of PPC stack frame format, or at least some docs
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
128 unsigned ArgOffset = 48;
129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
132 static const unsigned GPR[] = {
133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
142 // the arguments start at offset 48, although they are likely to be passed
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
146 bool needsLoad = false;
147 MVT::ValueType ObjectVT = getValueType(I->getType());
150 default: assert(0 && "Unhandled argument type!");
156 if (GPR_remaining > 0) {
157 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
158 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
160 if (ObjectVT != MVT::i64)
161 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
168 if (FPR_remaining > 0) {
169 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
170 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
180 // We need to load the argument to a virtual register if we determined above
181 // that we ran out of physical registers of the appropriate type
183 unsigned SubregOffset = 0;
185 default: assert(0 && "Unhandled argument type!");
187 case MVT::i8: SubregOffset = 7; break;
188 case MVT::i16: SubregOffset = 6; break;
190 case MVT::f32: SubregOffset = 4; break;
192 case MVT::f64: SubregOffset = 0; break;
194 int FI = MFI->CreateFixedObject(8, ArgOffset);
195 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
196 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
197 DAG.getConstant(SubregOffset, MVT::i64));
198 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
201 // Every 4 bytes of argument space consumes one of the GPRs available for
203 if (GPR_remaining > 0) {
209 DAG.setRoot(newroot.getValue(1));
210 ArgValues.push_back(argt);
213 // If the function takes variable number of arguments, make a frame index for
214 // the start of the first vararg value... for expansion of llvm.va_start.
216 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
217 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
218 // If this function is vararg, store any remaining integer argument regs
219 // to their spots on the stack so that they may be loaded by deferencing the
220 // result of va_next.
221 std::vector<SDOperand> MemOps;
222 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
223 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
224 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
225 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
226 Val, FIN, DAG.getSrcValue(NULL));
227 MemOps.push_back(Store);
228 // Increment the address by eight for the next argument to store
229 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
230 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
232 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
238 std::pair<SDOperand, SDOperand>
239 PPC64TargetLowering::LowerCallTo(SDOperand Chain,
240 const Type *RetTy, bool isVarArg,
241 unsigned CallingConv, bool isTailCall,
242 SDOperand Callee, ArgListTy &Args,
244 // args_to_use will accumulate outgoing args for the ISD::CALL case in
245 // SelectExpr to use to put the arguments in the appropriate registers.
246 std::vector<SDOperand> args_to_use;
248 // Count how many bytes are to be pushed on the stack, including the linkage
249 // area, and parameter passing area.
250 unsigned NumBytes = 48;
253 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
254 DAG.getConstant(NumBytes, getPointerTy()));
256 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
258 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
259 // plus 64 bytes of argument space in case any called code gets funky on us.
260 if (NumBytes < 112) NumBytes = 112;
262 // Adjust the stack pointer for the new arguments...
263 // These operations are automatically eliminated by the prolog/epilog pass
264 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
265 DAG.getConstant(NumBytes, getPointerTy()));
267 // Set up a copy of the stack pointer for use loading and storing any
268 // arguments that may not fit in the registers available for argument
270 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
273 // Figure out which arguments are going to go in registers, and which in
274 // memory. Also, if this is a vararg function, floating point operations
275 // must be stored to our stack, and loaded into integer regs as well, if
276 // any integer regs are available for argument passing.
277 unsigned ArgOffset = 48;
278 unsigned GPR_remaining = 8;
279 unsigned FPR_remaining = 13;
281 std::vector<SDOperand> MemOps;
282 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
283 // PtrOff will be used to store the current argument to the stack if a
284 // register cannot be found for it.
285 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
286 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
287 MVT::ValueType ArgVT = getValueType(Args[i].second);
290 default: assert(0 && "Unexpected ValueType for argument!");
295 // Promote the integer to 64 bits. If the input type is signed use a
296 // sign extend, otherwise use a zero extend.
297 if (Args[i].second->isSigned())
298 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
300 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
303 if (GPR_remaining > 0) {
304 args_to_use.push_back(Args[i].first);
307 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
308 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
314 if (FPR_remaining > 0) {
315 args_to_use.push_back(Args[i].first);
318 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
319 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
320 MemOps.push_back(Store);
321 // Float varargs are always shadowed in available integer registers
322 if (GPR_remaining > 0) {
323 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
324 MemOps.push_back(Load);
325 args_to_use.push_back(Load);
329 // If we have any FPRs remaining, we may also have GPRs remaining.
330 // Args passed in FPRs also consume an available GPR.
331 if (GPR_remaining > 0) {
332 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
337 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
338 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
345 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
348 std::vector<MVT::ValueType> RetVals;
349 MVT::ValueType RetTyVT = getValueType(RetTy);
350 if (RetTyVT != MVT::isVoid)
351 RetVals.push_back(RetTyVT);
352 RetVals.push_back(MVT::Other);
354 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
355 Chain, Callee, args_to_use), 0);
356 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
357 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
358 DAG.getConstant(NumBytes, getPointerTy()));
359 return std::make_pair(TheCall, Chain);
362 std::pair<SDOperand, SDOperand>
363 PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
364 //vastart just returns the address of the VarArgsFrameIndex slot.
365 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
368 std::pair<SDOperand,SDOperand> PPC64TargetLowering::
369 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
370 const Type *ArgTy, SelectionDAG &DAG) {
371 MVT::ValueType ArgVT = getValueType(ArgTy);
374 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
376 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
377 DAG.getConstant(8, VAList.getValueType()));
379 return std::make_pair(Result, Chain);
383 std::pair<SDOperand, SDOperand> PPC64TargetLowering::
384 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
386 assert(0 && "LowerFrameReturnAddress unimplemented");
391 Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
392 Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
393 //===--------------------------------------------------------------------===//
394 /// ISel - PPC32 specific code to select PPC32 machine instructions for
395 /// SelectionDAG operations.
396 //===--------------------------------------------------------------------===//
397 class ISel : public SelectionDAGISel {
400 PPC64TargetLowering PPC64Lowering;
402 /// ExprMap - As shared expressions are codegen'd, we keep track of which
403 /// vreg the value is produced in, so we only emit one copy of each compiled
405 std::map<SDOperand, unsigned> ExprMap;
407 unsigned GlobalBaseReg;
408 bool GlobalBaseInitialized;
411 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
414 /// runOnFunction - Override this function in order to reset our per-function
416 virtual bool runOnFunction(Function &Fn) {
417 // Make sure we re-emit a set of the global base reg if necessary
418 GlobalBaseInitialized = false;
419 return SelectionDAGISel::runOnFunction(Fn);
422 /// InstructionSelectBasicBlock - This callback is invoked by
423 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
424 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
426 // Codegen the basic block.
427 Select(DAG.getRoot());
429 // Clear state used for selection.
433 unsigned getGlobalBaseReg();
434 unsigned getConstDouble(double floatVal, unsigned Result);
435 unsigned SelectSetCR0(SDOperand CC);
436 unsigned SelectExpr(SDOperand N);
437 unsigned SelectExprFP(SDOperand N, unsigned Result);
438 void Select(SDOperand N);
440 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
441 void SelectBranchCC(SDOperand N);
444 /// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
445 /// returns zero when the input is not exactly a power of two.
446 static unsigned ExactLog2(unsigned Val) {
447 if (Val == 0 || (Val & (Val-1))) return 0;
456 /// getImmediateForOpcode - This method returns a value indicating whether
457 /// the ConstantSDNode N can be used as an immediate to Opcode. The return
458 /// values are either 0, 1 or 2. 0 indicates that either N is not a
459 /// ConstantSDNode, or is not suitable for use by that opcode. A return value
460 /// of 1 indicates that the constant may be used in normal immediate form. A
461 /// return value of 2 indicates that the constant may be used in shifted
462 /// immediate form. A return value of 3 indicates that log base 2 of the
463 /// constant may be used.
465 static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
466 unsigned& Imm, bool U = false) {
467 if (N.getOpcode() != ISD::Constant) return 0;
469 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
474 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
475 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
480 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
481 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
485 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
488 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
489 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
492 if ((Imm = ExactLog2(v))) { return 3; }
498 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
499 /// to Condition. If the Condition is unordered or unsigned, the bool argument
500 /// U is set to true, otherwise it is set to false.
501 static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
504 default: assert(0 && "Unknown condition!"); abort();
505 case ISD::SETEQ: return PPC::BEQ;
506 case ISD::SETNE: return PPC::BNE;
507 case ISD::SETULT: U = true;
508 case ISD::SETLT: return PPC::BLT;
509 case ISD::SETULE: U = true;
510 case ISD::SETLE: return PPC::BLE;
511 case ISD::SETUGT: U = true;
512 case ISD::SETGT: return PPC::BGT;
513 case ISD::SETUGE: U = true;
514 case ISD::SETGE: return PPC::BGE;
519 /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
520 /// and store immediate instructions.
521 static unsigned IndexedOpForOp(unsigned Opcode) {
523 default: assert(0 && "Unknown opcode!"); abort();
524 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
525 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
526 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
527 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
528 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
529 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
530 case PPC::LFD: return PPC::LFDX;
536 /// getGlobalBaseReg - Output the instructions required to put the
537 /// base address to use for accessing globals into a register.
539 unsigned ISel::getGlobalBaseReg() {
540 if (!GlobalBaseInitialized) {
541 // Insert the set of GlobalBaseReg into the first MBB of the function
542 MachineBasicBlock &FirstMBB = BB->getParent()->front();
543 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
544 GlobalBaseReg = MakeReg(MVT::i64);
545 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
546 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
547 GlobalBaseInitialized = true;
549 return GlobalBaseReg;
552 /// getConstDouble - Loads a floating point value into a register, via the
553 /// Constant Pool. Optionally takes a register in which to load the value.
554 unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
555 unsigned Tmp1 = MakeReg(MVT::i64);
556 if (0 == Result) Result = MakeReg(MVT::f64);
557 MachineConstantPool *CP = BB->getParent()->getConstantPool();
558 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
559 unsigned CPI = CP->getConstantPoolIndex(CFP);
560 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
561 .addConstantPoolIndex(CPI);
562 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
566 unsigned ISel::SelectSetCR0(SDOperand CC) {
567 unsigned Opc, Tmp1, Tmp2;
568 static const unsigned CompareOpcodes[] =
569 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
571 // If the first operand to the select is a SETCC node, then we can fold it
572 // into the branch that selects which value to return.
573 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
574 if (SetCC && CC.getOpcode() == ISD::SETCC) {
576 Opc = getBCCForSetCC(SetCC->getCondition(), U);
577 Tmp1 = SelectExpr(SetCC->getOperand(0));
579 // Pass the optional argument U to getImmediateForOpcode for SETCC,
580 // so that it knows whether the SETCC immediate range is signed or not.
581 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
584 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
586 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
588 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
589 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
590 Tmp2 = SelectExpr(SetCC->getOperand(1));
591 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
594 Tmp1 = SelectExpr(CC);
595 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
601 /// Check to see if the load is a constant offset from a base register
602 bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
604 unsigned imm = 0, opcode = N.getOpcode();
605 if (N.getOpcode() == ISD::ADD) {
606 Reg = SelectExpr(N.getOperand(0));
607 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
611 offset = SelectExpr(N.getOperand(1));
619 void ISel::SelectBranchCC(SDOperand N)
621 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
622 MachineBasicBlock *Dest =
623 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
625 // Get the MBB we will fall through to so that we can hand it off to the
626 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
627 //ilist<MachineBasicBlock>::iterator It = BB;
628 //MachineBasicBlock *Fallthrough = ++It;
630 Select(N.getOperand(0)); //chain
631 unsigned Opc = SelectSetCR0(N.getOperand(1));
632 // FIXME: Use this once we have something approximating two-way branches
633 // We cannot currently use this in case the ISel hands us something like
636 // since the fallthrough basic block for the conditional branch does not start
637 // with the unconditional branch (it is skipped over).
638 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
639 // .addMBB(Dest).addMBB(Fallthrough);
640 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
644 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
646 unsigned Tmp1, Tmp2, Tmp3;
648 SDNode *Node = N.Val;
649 MVT::ValueType DestType = N.getValueType();
650 unsigned opcode = N.getOpcode();
655 assert(0 && "Node not handled!\n");
658 // Attempt to generate FSEL. We can do this whenever we have an FP result,
659 // and an FP comparison in the SetCC node.
660 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
661 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
662 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
663 SetCC->getCondition() != ISD::SETEQ &&
664 SetCC->getCondition() != ISD::SETNE) {
665 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
666 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
667 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
668 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
670 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
671 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
672 switch(SetCC->getCondition()) {
673 default: assert(0 && "Invalid FSEL condition"); abort();
676 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
680 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
685 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
686 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
692 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
693 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
698 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
699 Tmp2 = SelectExpr(SetCC->getOperand(1));
701 switch(SetCC->getCondition()) {
702 default: assert(0 && "Invalid FSEL condition"); abort();
705 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
706 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
710 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
711 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
715 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
716 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
720 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
721 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
725 assert(0 && "Should never get here");
729 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
730 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
731 Opc = SelectSetCR0(N.getOperand(0));
733 // Create an iterator with which to insert the MBB for copying the false
734 // value and the MBB to hold the PHI instruction for this SetCC.
735 MachineBasicBlock *thisMBB = BB;
736 const BasicBlock *LLVM_BB = BB->getBasicBlock();
737 ilist<MachineBasicBlock>::iterator It = BB;
745 // fallthrough --> copy0MBB
746 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
747 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
748 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
749 MachineFunction *F = BB->getParent();
750 F->getBasicBlockList().insert(It, copy0MBB);
751 F->getBasicBlockList().insert(It, sinkMBB);
752 // Update machine-CFG edges
753 BB->addSuccessor(copy0MBB);
754 BB->addSuccessor(sinkMBB);
758 // # fallthrough to sinkMBB
760 // Update machine-CFG edges
761 BB->addSuccessor(sinkMBB);
764 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
767 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
768 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
773 if (!NoExcessFPPrecision &&
774 ISD::ADD == N.getOperand(0).getOpcode() &&
775 N.getOperand(0).Val->hasOneUse() &&
776 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
777 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
778 ++FusedFP; // Statistic
779 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
780 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
781 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
782 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
783 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
784 } else if (!NoExcessFPPrecision &&
785 ISD::SUB == N.getOperand(0).getOpcode() &&
786 N.getOperand(0).Val->hasOneUse() &&
787 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
788 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
789 ++FusedFP; // Statistic
790 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
791 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
792 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
793 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
794 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
795 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
796 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
797 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
799 Tmp1 = SelectExpr(N.getOperand(0));
800 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
805 Tmp1 = SelectExpr(N.getOperand(0));
806 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
810 assert (DestType == MVT::f32 &&
811 N.getOperand(0).getValueType() == MVT::f64 &&
812 "only f64 to f32 conversion supported here");
813 Tmp1 = SelectExpr(N.getOperand(0));
814 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
818 assert (DestType == MVT::f64 &&
819 N.getOperand(0).getValueType() == MVT::f32 &&
820 "only f32 to f64 conversion supported here");
821 Tmp1 = SelectExpr(N.getOperand(0));
822 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
825 case ISD::CopyFromReg:
827 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
828 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
829 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
832 case ISD::ConstantFP: {
833 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
834 Result = getConstDouble(CN->getValue(), Result);
839 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
840 N.getOperand(0).Val->hasOneUse()) {
841 ++FusedFP; // Statistic
842 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
843 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
844 Tmp3 = SelectExpr(N.getOperand(1));
845 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
846 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
849 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
850 Tmp1 = SelectExpr(N.getOperand(0));
851 Tmp2 = SelectExpr(N.getOperand(1));
852 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
856 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
857 N.getOperand(0).Val->hasOneUse()) {
858 ++FusedFP; // Statistic
859 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
860 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
861 Tmp3 = SelectExpr(N.getOperand(1));
862 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
863 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
866 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
867 Tmp1 = SelectExpr(N.getOperand(0));
868 Tmp2 = SelectExpr(N.getOperand(1));
869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
875 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
876 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
878 Tmp1 = SelectExpr(N.getOperand(0));
879 Tmp2 = SelectExpr(N.getOperand(1));
880 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
883 case ISD::UINT_TO_FP:
884 case ISD::SINT_TO_FP: {
885 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
886 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
887 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
888 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
889 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
891 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
892 MachineConstantPool *CP = BB->getParent()->getConstantPool();
894 // FIXME: pull this FP constant generation stuff out into something like
895 // the simple ISel's getReg.
897 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
898 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
899 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
901 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
902 unsigned CPI = CP->getConstantPoolIndex(CFP);
903 // Load constant fp value
904 unsigned Tmp4 = MakeReg(MVT::i32);
905 unsigned TmpL = MakeReg(MVT::i32);
906 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
907 .addConstantPoolIndex(CPI);
908 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
909 // Store the hi & low halves of the fp value, currently in int regs
910 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
911 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
912 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
913 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
914 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
915 // Generate the return value with a subtract
916 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
921 assert(0 && "Should never get here");
925 unsigned ISel::SelectExpr(SDOperand N) {
927 unsigned Tmp1, Tmp2, Tmp3;
929 unsigned opcode = N.getOpcode();
931 SDNode *Node = N.Val;
932 MVT::ValueType DestType = N.getValueType();
934 unsigned &Reg = ExprMap[N];
937 switch (N.getOpcode()) {
939 Reg = Result = (N.getValueType() != MVT::Other) ?
940 MakeReg(N.getValueType()) : 1;
944 // If this is a call instruction, make sure to prepare ALL of the result
945 // values as well as the chain.
946 if (Node->getNumValues() == 1)
947 Reg = Result = 1; // Void call, just a chain.
949 Result = MakeReg(Node->getValueType(0));
950 ExprMap[N.getValue(0)] = Result;
951 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
952 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
953 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
958 if (ISD::CopyFromReg == opcode)
959 DestType = N.getValue(0).getValueType();
961 if (DestType == MVT::f64 || DestType == MVT::f32)
962 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
963 return SelectExprFP(N, Result);
968 assert(0 && "Node not handled!\n");
970 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
972 case ISD::DYNAMIC_STACKALLOC:
973 // Generate both result values. FIXME: Need a better commment here?
975 ExprMap[N.getValue(1)] = 1;
977 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
979 // FIXME: We are currently ignoring the requested alignment for handling
980 // greater than the stack alignment. This will need to be revisited at some
981 // point. Align = N.getOperand(2);
982 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
983 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
984 std::cerr << "Cannot allocate stack object with greater alignment than"
985 << " the stack alignment yet!";
988 Select(N.getOperand(0));
989 Tmp1 = SelectExpr(N.getOperand(1));
990 // Subtract size from stack pointer, thereby allocating some space.
991 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
992 // Put a pointer to the space into the result register by copying the SP
993 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
996 case ISD::ConstantPool:
997 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
998 Tmp2 = MakeReg(MVT::i64);
999 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1000 .addConstantPoolIndex(Tmp1);
1001 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1004 case ISD::FrameIndex:
1005 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1006 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1009 case ISD::GlobalAddress: {
1010 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1011 Tmp1 = MakeReg(MVT::i64);
1012 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1013 .addGlobalAddress(GV);
1014 if (GV->hasWeakLinkage() || GV->isExternal()) {
1015 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1017 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1025 case ISD::SEXTLOAD: {
1026 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1027 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1028 bool sext = (ISD::SEXTLOAD == opcode);
1030 // Make sure we generate both values.
1032 ExprMap[N.getValue(1)] = 1; // Generate the token
1034 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1036 SDOperand Chain = N.getOperand(0);
1037 SDOperand Address = N.getOperand(1);
1040 switch (TypeBeingLoaded) {
1041 default: Node->dump(); assert(0 && "Cannot load this type!");
1042 case MVT::i1: Opc = PPC::LBZ; break;
1043 case MVT::i8: Opc = PPC::LBZ; break;
1044 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1045 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1046 case MVT::i64: Opc = PPC::LD; break;
1047 case MVT::f32: Opc = PPC::LFS; break;
1048 case MVT::f64: Opc = PPC::LFD; break;
1051 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1052 Tmp1 = MakeReg(MVT::i64);
1053 int CPI = CP->getIndex();
1054 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1055 .addConstantPoolIndex(CPI);
1056 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1058 else if(Address.getOpcode() == ISD::FrameIndex) {
1059 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1060 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1063 bool idx = SelectAddr(Address, Tmp1, offset);
1065 Opc = IndexedOpForOp(Opc);
1066 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1068 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1076 unsigned GPR_idx = 0, FPR_idx = 0;
1077 static const unsigned GPR[] = {
1078 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1079 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1081 static const unsigned FPR[] = {
1082 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1083 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1086 // Lower the chain for this call.
1087 Select(N.getOperand(0));
1088 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1090 MachineInstr *CallMI;
1091 // Emit the correct call instruction based on the type of symbol called.
1092 if (GlobalAddressSDNode *GASD =
1093 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1094 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1096 } else if (ExternalSymbolSDNode *ESSDN =
1097 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1098 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1101 Tmp1 = SelectExpr(N.getOperand(1));
1102 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1103 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1104 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1108 // Load the register args to virtual regs
1109 std::vector<unsigned> ArgVR;
1110 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1111 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1113 // Copy the virtual registers into the appropriate argument register
1114 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1115 switch(N.getOperand(i+2).getValueType()) {
1116 default: Node->dump(); assert(0 && "Unknown value type for call");
1122 assert(GPR_idx < 8 && "Too many int args");
1123 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1124 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1125 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1131 assert(FPR_idx < 13 && "Too many fp args");
1132 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1133 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1139 // Put the call instruction in the correct place in the MachineBasicBlock
1140 BB->push_back(CallMI);
1142 switch (Node->getValueType(0)) {
1143 default: assert(0 && "Unknown value type for call result!");
1144 case MVT::Other: return 1;
1150 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1154 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1157 return Result+N.ResNo;
1160 case ISD::SIGN_EXTEND:
1161 case ISD::SIGN_EXTEND_INREG:
1162 Tmp1 = SelectExpr(N.getOperand(0));
1163 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1164 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1166 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
1169 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1172 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1175 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1180 case ISD::CopyFromReg:
1182 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1183 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1184 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1190 Tmp2 = CN->getValue() & 0x3F;
1191 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1194 Tmp2 = SelectExpr(N.getOperand(1));
1195 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1200 Tmp1 = SelectExpr(N.getOperand(0));
1201 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1202 Tmp2 = CN->getValue() & 0x3F;
1203 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1206 Tmp2 = SelectExpr(N.getOperand(1));
1207 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1212 Tmp1 = SelectExpr(N.getOperand(0));
1213 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1214 Tmp2 = CN->getValue() & 0x3F;
1215 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1217 Tmp2 = SelectExpr(N.getOperand(1));
1218 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1223 Tmp1 = SelectExpr(N.getOperand(0));
1224 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1225 default: assert(0 && "unhandled result code");
1226 case 0: // No immediate
1227 Tmp2 = SelectExpr(N.getOperand(1));
1228 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1230 case 1: // Low immediate
1231 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1233 case 2: // Shifted immediate
1234 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1241 Tmp1 = SelectExpr(N.getOperand(0));
1242 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1243 default: assert(0 && "unhandled result code");
1244 case 0: // No immediate
1245 Tmp2 = SelectExpr(N.getOperand(1));
1247 case ISD::AND: Opc = PPC::AND; break;
1248 case ISD::OR: Opc = PPC::OR; break;
1250 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1252 case 1: // Low immediate
1254 case ISD::AND: Opc = PPC::ANDIo; break;
1255 case ISD::OR: Opc = PPC::ORI; break;
1257 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1259 case 2: // Shifted immediate
1261 case ISD::AND: Opc = PPC::ANDISo; break;
1262 case ISD::OR: Opc = PPC::ORIS; break;
1264 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1270 // Check for EQV: xor, (xor a, -1), b
1271 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1272 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1273 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1275 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1280 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1281 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1282 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1284 switch(N.getOperand(0).getOpcode()) {
1286 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1288 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1291 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1292 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1293 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1296 Tmp1 = SelectExpr(N.getOperand(0));
1297 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1302 Tmp1 = SelectExpr(N.getOperand(0));
1303 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1304 default: assert(0 && "unhandled result code");
1305 case 0: // No immediate
1306 Tmp2 = SelectExpr(N.getOperand(1));
1307 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 case 1: // Low immediate
1310 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1312 case 2: // Shifted immediate
1313 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1320 Tmp2 = SelectExpr(N.getOperand(1));
1321 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1322 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1324 Tmp1 = SelectExpr(N.getOperand(0));
1325 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1330 Tmp1 = SelectExpr(N.getOperand(0));
1331 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1332 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1334 Tmp2 = SelectExpr(N.getOperand(1));
1335 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1341 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1342 Tmp1 = MakeReg(MVT::i64);
1343 Tmp2 = SelectExpr(N.getOperand(0));
1344 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1345 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1348 Tmp1 = SelectExpr(N.getOperand(0));
1349 Tmp2 = SelectExpr(N.getOperand(1));
1350 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1351 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1354 case ISD::FP_TO_UINT:
1355 case ISD::FP_TO_SINT: {
1356 Tmp1 = SelectExpr(N.getOperand(0));
1357 Tmp2 = MakeReg(MVT::f64);
1358 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1359 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1360 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1361 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1366 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1367 Opc = SelectSetCR0(N);
1369 unsigned TrueValue = MakeReg(MVT::i32);
1370 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1371 unsigned FalseValue = MakeReg(MVT::i32);
1372 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1374 // Create an iterator with which to insert the MBB for copying the false
1375 // value and the MBB to hold the PHI instruction for this SetCC.
1376 MachineBasicBlock *thisMBB = BB;
1377 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1378 ilist<MachineBasicBlock>::iterator It = BB;
1383 // cmpTY cr0, r1, r2
1384 // %TrueValue = li 1
1386 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1387 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1388 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1389 MachineFunction *F = BB->getParent();
1390 F->getBasicBlockList().insert(It, copy0MBB);
1391 F->getBasicBlockList().insert(It, sinkMBB);
1392 // Update machine-CFG edges
1393 BB->addSuccessor(copy0MBB);
1394 BB->addSuccessor(sinkMBB);
1397 // %FalseValue = li 0
1400 // Update machine-CFG edges
1401 BB->addSuccessor(sinkMBB);
1404 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1407 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1408 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1411 assert(0 && "Is this legal?");
1415 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1416 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1417 Opc = SelectSetCR0(N.getOperand(0));
1419 // Create an iterator with which to insert the MBB for copying the false
1420 // value and the MBB to hold the PHI instruction for this SetCC.
1421 MachineBasicBlock *thisMBB = BB;
1422 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1423 ilist<MachineBasicBlock>::iterator It = BB;
1429 // cmpTY cr0, r1, r2
1431 // fallthrough --> copy0MBB
1432 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1433 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1434 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1435 MachineFunction *F = BB->getParent();
1436 F->getBasicBlockList().insert(It, copy0MBB);
1437 F->getBasicBlockList().insert(It, sinkMBB);
1438 // Update machine-CFG edges
1439 BB->addSuccessor(copy0MBB);
1440 BB->addSuccessor(sinkMBB);
1443 // %FalseValue = ...
1444 // # fallthrough to sinkMBB
1446 // Update machine-CFG edges
1447 BB->addSuccessor(sinkMBB);
1450 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1453 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1454 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1456 // FIXME: Select i64?
1461 switch (N.getValueType()) {
1462 default: assert(0 && "Cannot use constants of this type!");
1464 BuildMI(BB, PPC::LI, 1, Result)
1465 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1469 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1470 if (v < 32768 && v >= -32768) {
1471 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1473 Tmp1 = MakeReg(MVT::i32);
1474 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1475 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1485 void ISel::Select(SDOperand N) {
1486 unsigned Tmp1, Tmp2, Opc;
1487 unsigned opcode = N.getOpcode();
1489 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1490 return; // Already selected.
1492 SDNode *Node = N.Val;
1494 switch (Node->getOpcode()) {
1496 Node->dump(); std::cerr << "\n";
1497 assert(0 && "Node not handled yet!");
1498 case ISD::EntryToken: return; // Noop
1499 case ISD::TokenFactor:
1500 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1501 Select(Node->getOperand(i));
1503 case ISD::CALLSEQ_START:
1504 case ISD::CALLSEQ_END:
1505 Select(N.getOperand(0));
1506 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1507 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
1508 PPC::ADJCALLSTACKUP;
1509 BuildMI(BB, Opc, 1).addImm(Tmp1);
1512 MachineBasicBlock *Dest =
1513 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1514 Select(N.getOperand(0));
1515 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1521 case ISD::CopyToReg:
1522 Select(N.getOperand(0));
1523 Tmp1 = SelectExpr(N.getOperand(1));
1524 Tmp2 = cast<RegSDNode>(N)->getReg();
1527 if (N.getOperand(1).getValueType() == MVT::f64 ||
1528 N.getOperand(1).getValueType() == MVT::f32)
1529 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1531 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1534 case ISD::ImplicitDef:
1535 Select(N.getOperand(0));
1536 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1539 switch (N.getNumOperands()) {
1541 assert(0 && "Unknown return instruction!");
1543 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1544 N.getOperand(2).getValueType() == MVT::i32 &&
1545 "Unknown two-register value!");
1546 Select(N.getOperand(0));
1547 Tmp1 = SelectExpr(N.getOperand(1));
1548 Tmp2 = SelectExpr(N.getOperand(2));
1549 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1550 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1553 Select(N.getOperand(0));
1554 Tmp1 = SelectExpr(N.getOperand(1));
1555 switch (N.getOperand(1).getValueType()) {
1557 assert(0 && "Unknown return type!");
1560 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1563 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1567 Select(N.getOperand(0));
1570 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1572 case ISD::TRUNCSTORE:
1575 SDOperand Chain = N.getOperand(0);
1576 SDOperand Value = N.getOperand(1);
1577 SDOperand Address = N.getOperand(2);
1580 Tmp1 = SelectExpr(Value); //value
1582 if (opcode == ISD::STORE) {
1583 switch(Value.getValueType()) {
1584 default: assert(0 && "unknown Type in store");
1585 case MVT::i64: Opc = PPC::STD; break;
1586 case MVT::f64: Opc = PPC::STFD; break;
1587 case MVT::f32: Opc = PPC::STFS; break;
1589 } else { //ISD::TRUNCSTORE
1590 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1591 default: assert(0 && "unknown Type in store");
1592 case MVT::i1: //FIXME: DAG does not promote this load
1593 case MVT::i8: Opc= PPC::STB; break;
1594 case MVT::i16: Opc = PPC::STH; break;
1595 case MVT::i32: Opc = PPC::STW; break;
1599 if(Address.getOpcode() == ISD::FrameIndex)
1601 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1602 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1607 bool idx = SelectAddr(Address, Tmp2, offset);
1609 Opc = IndexedOpForOp(Opc);
1610 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1612 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1621 case ISD::CopyFromReg:
1624 case ISD::DYNAMIC_STACKALLOC:
1629 assert(0 && "Should not be reached!");
1633 /// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1634 /// into a machine code representation using pattern matching and a machine
1635 /// description file.
1637 FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
1638 return new ISel(TM);