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 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
99 virtual std::pair<SDOperand, SDOperand>
100 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
102 virtual std::pair<SDOperand,SDOperand>
103 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
104 const Type *ArgTy, SelectionDAG &DAG);
106 virtual std::pair<SDOperand, SDOperand>
107 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
113 std::vector<SDOperand>
114 PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 // add beautiful description of PPC stack frame format, or at least some docs
118 MachineFunction &MF = DAG.getMachineFunction();
119 MachineFrameInfo *MFI = MF.getFrameInfo();
120 MachineBasicBlock& BB = MF.front();
121 std::vector<SDOperand> ArgValues;
123 // Due to the rather complicated nature of the PowerPC ABI, rather than a
124 // fixed size array of physical args, for the sake of simplicity let the STL
125 // handle tracking them for us.
126 std::vector<unsigned> argVR, argPR, argOp;
127 unsigned ArgOffset = 48;
128 unsigned GPR_remaining = 8;
129 unsigned FPR_remaining = 13;
130 unsigned GPR_idx = 0, FPR_idx = 0;
131 static const unsigned GPR[] = {
132 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
133 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 static const unsigned FPR[] = {
136 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
137 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
140 // Add DAG nodes to load the arguments... On entry to a function on PPC,
141 // the arguments start at offset 48, although they are likely to be passed
143 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
144 SDOperand newroot, argt;
145 bool needsLoad = false;
146 MVT::ValueType ObjectVT = getValueType(I->getType());
149 default: assert(0 && "Unhandled argument type!");
155 if (GPR_remaining > 0) {
156 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
157 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
159 if (ObjectVT != MVT::i64)
160 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
167 if (FPR_remaining > 0) {
168 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
169 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
179 // We need to load the argument to a virtual register if we determined above
180 // that we ran out of physical registers of the appropriate type
182 unsigned SubregOffset = 0;
184 default: assert(0 && "Unhandled argument type!");
186 case MVT::i8: SubregOffset = 7; break;
187 case MVT::i16: SubregOffset = 6; break;
189 case MVT::f32: SubregOffset = 4; break;
191 case MVT::f64: SubregOffset = 0; break;
193 int FI = MFI->CreateFixedObject(8, ArgOffset);
194 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
195 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
196 DAG.getConstant(SubregOffset, MVT::i64));
197 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
200 // Every 4 bytes of argument space consumes one of the GPRs available for
202 if (GPR_remaining > 0) {
208 DAG.setRoot(newroot.getValue(1));
209 ArgValues.push_back(argt);
212 // If the function takes variable number of arguments, make a frame index for
213 // the start of the first vararg value... for expansion of llvm.va_start.
215 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
216 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
217 // If this function is vararg, store any remaining integer argument regs
218 // to their spots on the stack so that they may be loaded by deferencing the
219 // result of va_next.
220 std::vector<SDOperand> MemOps;
221 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
222 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
223 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
224 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
225 Val, FIN, DAG.getSrcValue(NULL));
226 MemOps.push_back(Store);
227 // Increment the address by eight for the next argument to store
228 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
229 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
231 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
237 std::pair<SDOperand, SDOperand>
238 PPC64TargetLowering::LowerCallTo(SDOperand Chain,
239 const Type *RetTy, bool isVarArg,
240 unsigned CallingConv,
241 SDOperand Callee, ArgListTy &Args,
243 // args_to_use will accumulate outgoing args for the ISD::CALL case in
244 // SelectExpr to use to put the arguments in the appropriate registers.
245 std::vector<SDOperand> args_to_use;
247 // Count how many bytes are to be pushed on the stack, including the linkage
248 // area, and parameter passing area.
249 unsigned NumBytes = 48;
252 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
253 DAG.getConstant(NumBytes, getPointerTy()));
255 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
257 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
258 // plus 64 bytes of argument space in case any called code gets funky on us.
259 if (NumBytes < 112) NumBytes = 112;
261 // Adjust the stack pointer for the new arguments...
262 // These operations are automatically eliminated by the prolog/epilog pass
263 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
264 DAG.getConstant(NumBytes, getPointerTy()));
266 // Set up a copy of the stack pointer for use loading and storing any
267 // arguments that may not fit in the registers available for argument
269 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
272 // Figure out which arguments are going to go in registers, and which in
273 // memory. Also, if this is a vararg function, floating point operations
274 // must be stored to our stack, and loaded into integer regs as well, if
275 // any integer regs are available for argument passing.
276 unsigned ArgOffset = 48;
277 unsigned GPR_remaining = 8;
278 unsigned FPR_remaining = 13;
280 std::vector<SDOperand> MemOps;
281 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
282 // PtrOff will be used to store the current argument to the stack if a
283 // register cannot be found for it.
284 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
285 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
286 MVT::ValueType ArgVT = getValueType(Args[i].second);
289 default: assert(0 && "Unexpected ValueType for argument!");
294 // Promote the integer to 64 bits. If the input type is signed use a
295 // sign extend, otherwise use a zero extend.
296 if (Args[i].second->isSigned())
297 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
299 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
302 if (GPR_remaining > 0) {
303 args_to_use.push_back(Args[i].first);
306 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
307 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
313 if (FPR_remaining > 0) {
314 args_to_use.push_back(Args[i].first);
317 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
318 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
319 MemOps.push_back(Store);
320 // Float varargs are always shadowed in available integer registers
321 if (GPR_remaining > 0) {
322 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
323 MemOps.push_back(Load);
324 args_to_use.push_back(Load);
328 // If we have any FPRs remaining, we may also have GPRs remaining.
329 // Args passed in FPRs also consume an available GPR.
330 if (GPR_remaining > 0) {
331 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
336 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
337 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
344 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
347 std::vector<MVT::ValueType> RetVals;
348 MVT::ValueType RetTyVT = getValueType(RetTy);
349 if (RetTyVT != MVT::isVoid)
350 RetVals.push_back(RetTyVT);
351 RetVals.push_back(MVT::Other);
353 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
354 Chain, Callee, args_to_use), 0);
355 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
356 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
357 DAG.getConstant(NumBytes, getPointerTy()));
358 return std::make_pair(TheCall, Chain);
361 std::pair<SDOperand, SDOperand>
362 PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
363 //vastart just returns the address of the VarArgsFrameIndex slot.
364 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
367 std::pair<SDOperand,SDOperand> PPC64TargetLowering::
368 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
369 const Type *ArgTy, SelectionDAG &DAG) {
370 MVT::ValueType ArgVT = getValueType(ArgTy);
373 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
375 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
376 DAG.getConstant(8, VAList.getValueType()));
378 return std::make_pair(Result, Chain);
382 std::pair<SDOperand, SDOperand> PPC64TargetLowering::
383 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
385 assert(0 && "LowerFrameReturnAddress unimplemented");
390 Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
391 Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
392 //===--------------------------------------------------------------------===//
393 /// ISel - PPC32 specific code to select PPC32 machine instructions for
394 /// SelectionDAG operations.
395 //===--------------------------------------------------------------------===//
396 class ISel : public SelectionDAGISel {
399 PPC64TargetLowering PPC64Lowering;
401 /// ExprMap - As shared expressions are codegen'd, we keep track of which
402 /// vreg the value is produced in, so we only emit one copy of each compiled
404 std::map<SDOperand, unsigned> ExprMap;
406 unsigned GlobalBaseReg;
407 bool GlobalBaseInitialized;
410 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
413 /// runOnFunction - Override this function in order to reset our per-function
415 virtual bool runOnFunction(Function &Fn) {
416 // Make sure we re-emit a set of the global base reg if necessary
417 GlobalBaseInitialized = false;
418 return SelectionDAGISel::runOnFunction(Fn);
421 /// InstructionSelectBasicBlock - This callback is invoked by
422 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
423 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
425 // Codegen the basic block.
426 Select(DAG.getRoot());
428 // Clear state used for selection.
432 unsigned getGlobalBaseReg();
433 unsigned getConstDouble(double floatVal, unsigned Result);
434 unsigned SelectSetCR0(SDOperand CC);
435 unsigned SelectExpr(SDOperand N);
436 unsigned SelectExprFP(SDOperand N, unsigned Result);
437 void Select(SDOperand N);
439 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
440 void SelectBranchCC(SDOperand N);
443 /// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
444 /// returns zero when the input is not exactly a power of two.
445 static unsigned ExactLog2(unsigned Val) {
446 if (Val == 0 || (Val & (Val-1))) return 0;
455 /// getImmediateForOpcode - This method returns a value indicating whether
456 /// the ConstantSDNode N can be used as an immediate to Opcode. The return
457 /// values are either 0, 1 or 2. 0 indicates that either N is not a
458 /// ConstantSDNode, or is not suitable for use by that opcode. A return value
459 /// of 1 indicates that the constant may be used in normal immediate form. A
460 /// return value of 2 indicates that the constant may be used in shifted
461 /// immediate form. A return value of 3 indicates that log base 2 of the
462 /// constant may be used.
464 static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
465 unsigned& Imm, bool U = false) {
466 if (N.getOpcode() != ISD::Constant) return 0;
468 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
473 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
474 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
479 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
480 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
484 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
487 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
488 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
491 if ((Imm = ExactLog2(v))) { return 3; }
497 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
498 /// to Condition. If the Condition is unordered or unsigned, the bool argument
499 /// U is set to true, otherwise it is set to false.
500 static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
503 default: assert(0 && "Unknown condition!"); abort();
504 case ISD::SETEQ: return PPC::BEQ;
505 case ISD::SETNE: return PPC::BNE;
506 case ISD::SETULT: U = true;
507 case ISD::SETLT: return PPC::BLT;
508 case ISD::SETULE: U = true;
509 case ISD::SETLE: return PPC::BLE;
510 case ISD::SETUGT: U = true;
511 case ISD::SETGT: return PPC::BGT;
512 case ISD::SETUGE: U = true;
513 case ISD::SETGE: return PPC::BGE;
518 /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
519 /// and store immediate instructions.
520 static unsigned IndexedOpForOp(unsigned Opcode) {
522 default: assert(0 && "Unknown opcode!"); abort();
523 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
524 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
525 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
526 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
527 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
528 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
529 case PPC::LFD: return PPC::LFDX;
535 /// getGlobalBaseReg - Output the instructions required to put the
536 /// base address to use for accessing globals into a register.
538 unsigned ISel::getGlobalBaseReg() {
539 if (!GlobalBaseInitialized) {
540 // Insert the set of GlobalBaseReg into the first MBB of the function
541 MachineBasicBlock &FirstMBB = BB->getParent()->front();
542 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
543 GlobalBaseReg = MakeReg(MVT::i64);
544 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
545 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
546 GlobalBaseInitialized = true;
548 return GlobalBaseReg;
551 /// getConstDouble - Loads a floating point value into a register, via the
552 /// Constant Pool. Optionally takes a register in which to load the value.
553 unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
554 unsigned Tmp1 = MakeReg(MVT::i64);
555 if (0 == Result) Result = MakeReg(MVT::f64);
556 MachineConstantPool *CP = BB->getParent()->getConstantPool();
557 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
558 unsigned CPI = CP->getConstantPoolIndex(CFP);
559 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
560 .addConstantPoolIndex(CPI);
561 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
565 unsigned ISel::SelectSetCR0(SDOperand CC) {
566 unsigned Opc, Tmp1, Tmp2;
567 static const unsigned CompareOpcodes[] =
568 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
570 // If the first operand to the select is a SETCC node, then we can fold it
571 // into the branch that selects which value to return.
572 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
573 if (SetCC && CC.getOpcode() == ISD::SETCC) {
575 Opc = getBCCForSetCC(SetCC->getCondition(), U);
576 Tmp1 = SelectExpr(SetCC->getOperand(0));
578 // Pass the optional argument U to getImmediateForOpcode for SETCC,
579 // so that it knows whether the SETCC immediate range is signed or not.
580 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
583 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
585 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
587 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
588 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
589 Tmp2 = SelectExpr(SetCC->getOperand(1));
590 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
593 Tmp1 = SelectExpr(CC);
594 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
600 /// Check to see if the load is a constant offset from a base register
601 bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
603 unsigned imm = 0, opcode = N.getOpcode();
604 if (N.getOpcode() == ISD::ADD) {
605 Reg = SelectExpr(N.getOperand(0));
606 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
610 offset = SelectExpr(N.getOperand(1));
618 void ISel::SelectBranchCC(SDOperand N)
620 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
621 MachineBasicBlock *Dest =
622 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
624 // Get the MBB we will fall through to so that we can hand it off to the
625 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
626 //ilist<MachineBasicBlock>::iterator It = BB;
627 //MachineBasicBlock *Fallthrough = ++It;
629 Select(N.getOperand(0)); //chain
630 unsigned Opc = SelectSetCR0(N.getOperand(1));
631 // FIXME: Use this once we have something approximating two-way branches
632 // We cannot currently use this in case the ISel hands us something like
635 // since the fallthrough basic block for the conditional branch does not start
636 // with the unconditional branch (it is skipped over).
637 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
638 // .addMBB(Dest).addMBB(Fallthrough);
639 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
643 unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
645 unsigned Tmp1, Tmp2, Tmp3;
647 SDNode *Node = N.Val;
648 MVT::ValueType DestType = N.getValueType();
649 unsigned opcode = N.getOpcode();
654 assert(0 && "Node not handled!\n");
657 // Attempt to generate FSEL. We can do this whenever we have an FP result,
658 // and an FP comparison in the SetCC node.
659 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
660 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
661 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
662 SetCC->getCondition() != ISD::SETEQ &&
663 SetCC->getCondition() != ISD::SETNE) {
664 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
665 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
666 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
667 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
669 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
670 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
671 switch(SetCC->getCondition()) {
672 default: assert(0 && "Invalid FSEL condition"); abort();
675 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
679 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
684 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
685 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
691 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
692 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
697 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
698 Tmp2 = SelectExpr(SetCC->getOperand(1));
700 switch(SetCC->getCondition()) {
701 default: assert(0 && "Invalid FSEL condition"); abort();
704 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
705 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
709 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
710 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
714 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
715 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
719 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
720 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
724 assert(0 && "Should never get here");
728 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
729 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
730 Opc = SelectSetCR0(N.getOperand(0));
732 // Create an iterator with which to insert the MBB for copying the false
733 // value and the MBB to hold the PHI instruction for this SetCC.
734 MachineBasicBlock *thisMBB = BB;
735 const BasicBlock *LLVM_BB = BB->getBasicBlock();
736 ilist<MachineBasicBlock>::iterator It = BB;
744 // fallthrough --> copy0MBB
745 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
746 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
747 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
748 MachineFunction *F = BB->getParent();
749 F->getBasicBlockList().insert(It, copy0MBB);
750 F->getBasicBlockList().insert(It, sinkMBB);
751 // Update machine-CFG edges
752 BB->addSuccessor(copy0MBB);
753 BB->addSuccessor(sinkMBB);
757 // # fallthrough to sinkMBB
759 // Update machine-CFG edges
760 BB->addSuccessor(sinkMBB);
763 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
766 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
767 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
772 if (!NoExcessFPPrecision &&
773 ISD::ADD == N.getOperand(0).getOpcode() &&
774 N.getOperand(0).Val->hasOneUse() &&
775 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
776 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
777 ++FusedFP; // Statistic
778 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
779 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
780 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
781 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
782 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
783 } else if (!NoExcessFPPrecision &&
784 ISD::SUB == N.getOperand(0).getOpcode() &&
785 N.getOperand(0).Val->hasOneUse() &&
786 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
787 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
788 ++FusedFP; // Statistic
789 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
790 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
791 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
792 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
793 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
794 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
795 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
796 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
798 Tmp1 = SelectExpr(N.getOperand(0));
799 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
804 Tmp1 = SelectExpr(N.getOperand(0));
805 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
809 assert (DestType == MVT::f32 &&
810 N.getOperand(0).getValueType() == MVT::f64 &&
811 "only f64 to f32 conversion supported here");
812 Tmp1 = SelectExpr(N.getOperand(0));
813 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
817 assert (DestType == MVT::f64 &&
818 N.getOperand(0).getValueType() == MVT::f32 &&
819 "only f32 to f64 conversion supported here");
820 Tmp1 = SelectExpr(N.getOperand(0));
821 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
824 case ISD::CopyFromReg:
826 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
827 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
828 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
831 case ISD::ConstantFP: {
832 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
833 Result = getConstDouble(CN->getValue(), Result);
838 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
839 N.getOperand(0).Val->hasOneUse()) {
840 ++FusedFP; // Statistic
841 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
842 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
843 Tmp3 = SelectExpr(N.getOperand(1));
844 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
845 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
848 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
849 Tmp1 = SelectExpr(N.getOperand(0));
850 Tmp2 = SelectExpr(N.getOperand(1));
851 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
855 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
856 N.getOperand(0).Val->hasOneUse()) {
857 ++FusedFP; // Statistic
858 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
859 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
860 Tmp3 = SelectExpr(N.getOperand(1));
861 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
862 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
865 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
866 Tmp1 = SelectExpr(N.getOperand(0));
867 Tmp2 = SelectExpr(N.getOperand(1));
868 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
874 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
875 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
877 Tmp1 = SelectExpr(N.getOperand(0));
878 Tmp2 = SelectExpr(N.getOperand(1));
879 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
882 case ISD::UINT_TO_FP:
883 case ISD::SINT_TO_FP: {
884 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
885 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
886 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
887 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
888 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
890 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
891 MachineConstantPool *CP = BB->getParent()->getConstantPool();
893 // FIXME: pull this FP constant generation stuff out into something like
894 // the simple ISel's getReg.
896 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
897 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
898 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
900 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
901 unsigned CPI = CP->getConstantPoolIndex(CFP);
902 // Load constant fp value
903 unsigned Tmp4 = MakeReg(MVT::i32);
904 unsigned TmpL = MakeReg(MVT::i32);
905 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
906 .addConstantPoolIndex(CPI);
907 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
908 // Store the hi & low halves of the fp value, currently in int regs
909 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
910 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
911 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
912 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
913 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
914 // Generate the return value with a subtract
915 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
920 assert(0 && "Should never get here");
924 unsigned ISel::SelectExpr(SDOperand N) {
926 unsigned Tmp1, Tmp2, Tmp3;
928 unsigned opcode = N.getOpcode();
930 SDNode *Node = N.Val;
931 MVT::ValueType DestType = N.getValueType();
933 unsigned &Reg = ExprMap[N];
936 switch (N.getOpcode()) {
938 Reg = Result = (N.getValueType() != MVT::Other) ?
939 MakeReg(N.getValueType()) : 1;
942 // If this is a call instruction, make sure to prepare ALL of the result
943 // values as well as the chain.
944 if (Node->getNumValues() == 1)
945 Reg = Result = 1; // Void call, just a chain.
947 Result = MakeReg(Node->getValueType(0));
948 ExprMap[N.getValue(0)] = Result;
949 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
950 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
951 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
956 if (ISD::CopyFromReg == opcode)
957 DestType = N.getValue(0).getValueType();
959 if (DestType == MVT::f64 || DestType == MVT::f32)
960 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
961 return SelectExprFP(N, Result);
966 assert(0 && "Node not handled!\n");
968 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
970 case ISD::DYNAMIC_STACKALLOC:
971 // Generate both result values. FIXME: Need a better commment here?
973 ExprMap[N.getValue(1)] = 1;
975 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
977 // FIXME: We are currently ignoring the requested alignment for handling
978 // greater than the stack alignment. This will need to be revisited at some
979 // point. Align = N.getOperand(2);
980 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
981 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
982 std::cerr << "Cannot allocate stack object with greater alignment than"
983 << " the stack alignment yet!";
986 Select(N.getOperand(0));
987 Tmp1 = SelectExpr(N.getOperand(1));
988 // Subtract size from stack pointer, thereby allocating some space.
989 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
990 // Put a pointer to the space into the result register by copying the SP
991 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
994 case ISD::ConstantPool:
995 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
996 Tmp2 = MakeReg(MVT::i64);
997 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
998 .addConstantPoolIndex(Tmp1);
999 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1002 case ISD::FrameIndex:
1003 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1004 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1007 case ISD::GlobalAddress: {
1008 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1009 Tmp1 = MakeReg(MVT::i64);
1010 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1011 .addGlobalAddress(GV);
1012 if (GV->hasWeakLinkage() || GV->isExternal()) {
1013 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1015 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1023 case ISD::SEXTLOAD: {
1024 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1025 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1026 bool sext = (ISD::SEXTLOAD == opcode);
1028 // Make sure we generate both values.
1030 ExprMap[N.getValue(1)] = 1; // Generate the token
1032 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1034 SDOperand Chain = N.getOperand(0);
1035 SDOperand Address = N.getOperand(1);
1038 switch (TypeBeingLoaded) {
1039 default: Node->dump(); assert(0 && "Cannot load this type!");
1040 case MVT::i1: Opc = PPC::LBZ; break;
1041 case MVT::i8: Opc = PPC::LBZ; break;
1042 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1043 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1044 case MVT::i64: Opc = PPC::LD; break;
1045 case MVT::f32: Opc = PPC::LFS; break;
1046 case MVT::f64: Opc = PPC::LFD; break;
1049 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1050 Tmp1 = MakeReg(MVT::i64);
1051 int CPI = CP->getIndex();
1052 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1053 .addConstantPoolIndex(CPI);
1054 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1056 else if(Address.getOpcode() == ISD::FrameIndex) {
1057 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1058 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1061 bool idx = SelectAddr(Address, Tmp1, offset);
1063 Opc = IndexedOpForOp(Opc);
1064 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1066 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1073 unsigned GPR_idx = 0, FPR_idx = 0;
1074 static const unsigned GPR[] = {
1075 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1076 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1078 static const unsigned FPR[] = {
1079 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1080 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1083 // Lower the chain for this call.
1084 Select(N.getOperand(0));
1085 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1087 MachineInstr *CallMI;
1088 // Emit the correct call instruction based on the type of symbol called.
1089 if (GlobalAddressSDNode *GASD =
1090 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1091 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1093 } else if (ExternalSymbolSDNode *ESSDN =
1094 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1095 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1098 Tmp1 = SelectExpr(N.getOperand(1));
1099 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1100 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1101 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1105 // Load the register args to virtual regs
1106 std::vector<unsigned> ArgVR;
1107 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1108 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1110 // Copy the virtual registers into the appropriate argument register
1111 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1112 switch(N.getOperand(i+2).getValueType()) {
1113 default: Node->dump(); assert(0 && "Unknown value type for call");
1119 assert(GPR_idx < 8 && "Too many int args");
1120 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1121 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1122 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1128 assert(FPR_idx < 13 && "Too many fp args");
1129 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1130 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1136 // Put the call instruction in the correct place in the MachineBasicBlock
1137 BB->push_back(CallMI);
1139 switch (Node->getValueType(0)) {
1140 default: assert(0 && "Unknown value type for call result!");
1141 case MVT::Other: return 1;
1147 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1151 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1154 return Result+N.ResNo;
1157 case ISD::SIGN_EXTEND:
1158 case ISD::SIGN_EXTEND_INREG:
1159 Tmp1 = SelectExpr(N.getOperand(0));
1160 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1161 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1163 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
1166 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1169 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1172 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1177 case ISD::CopyFromReg:
1179 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1180 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1181 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1185 Tmp1 = SelectExpr(N.getOperand(0));
1186 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1187 Tmp2 = CN->getValue() & 0x3F;
1188 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1191 Tmp2 = SelectExpr(N.getOperand(1));
1192 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1197 Tmp1 = SelectExpr(N.getOperand(0));
1198 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1199 Tmp2 = CN->getValue() & 0x3F;
1200 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1203 Tmp2 = SelectExpr(N.getOperand(1));
1204 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1211 Tmp2 = CN->getValue() & 0x3F;
1212 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1214 Tmp2 = SelectExpr(N.getOperand(1));
1215 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1220 Tmp1 = SelectExpr(N.getOperand(0));
1221 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1222 default: assert(0 && "unhandled result code");
1223 case 0: // No immediate
1224 Tmp2 = SelectExpr(N.getOperand(1));
1225 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1227 case 1: // Low immediate
1228 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1230 case 2: // Shifted immediate
1231 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1238 Tmp1 = SelectExpr(N.getOperand(0));
1239 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1240 default: assert(0 && "unhandled result code");
1241 case 0: // No immediate
1242 Tmp2 = SelectExpr(N.getOperand(1));
1244 case ISD::AND: Opc = PPC::AND; break;
1245 case ISD::OR: Opc = PPC::OR; break;
1247 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1249 case 1: // Low immediate
1251 case ISD::AND: Opc = PPC::ANDIo; break;
1252 case ISD::OR: Opc = PPC::ORI; break;
1254 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1256 case 2: // Shifted immediate
1258 case ISD::AND: Opc = PPC::ANDISo; break;
1259 case ISD::OR: Opc = PPC::ORIS; break;
1261 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1267 // Check for EQV: xor, (xor a, -1), b
1268 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1269 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1270 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1272 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1273 Tmp2 = SelectExpr(N.getOperand(1));
1274 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1277 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1278 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1279 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1281 switch(N.getOperand(0).getOpcode()) {
1283 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1284 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1285 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1288 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1289 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1290 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1293 Tmp1 = SelectExpr(N.getOperand(0));
1294 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1299 Tmp1 = SelectExpr(N.getOperand(0));
1300 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1301 default: assert(0 && "unhandled result code");
1302 case 0: // No immediate
1303 Tmp2 = SelectExpr(N.getOperand(1));
1304 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1306 case 1: // Low immediate
1307 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1309 case 2: // Shifted immediate
1310 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1317 Tmp2 = SelectExpr(N.getOperand(1));
1318 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1319 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1321 Tmp1 = SelectExpr(N.getOperand(0));
1322 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1327 Tmp1 = SelectExpr(N.getOperand(0));
1328 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1329 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1331 Tmp2 = SelectExpr(N.getOperand(1));
1332 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1338 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1339 Tmp1 = MakeReg(MVT::i64);
1340 Tmp2 = SelectExpr(N.getOperand(0));
1341 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1342 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1345 Tmp1 = SelectExpr(N.getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(1));
1347 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1348 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1351 case ISD::FP_TO_UINT:
1352 case ISD::FP_TO_SINT: {
1353 Tmp1 = SelectExpr(N.getOperand(0));
1354 Tmp2 = MakeReg(MVT::f64);
1355 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1356 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1357 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1358 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1363 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1364 Opc = SelectSetCR0(N);
1366 unsigned TrueValue = MakeReg(MVT::i32);
1367 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1368 unsigned FalseValue = MakeReg(MVT::i32);
1369 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1371 // Create an iterator with which to insert the MBB for copying the false
1372 // value and the MBB to hold the PHI instruction for this SetCC.
1373 MachineBasicBlock *thisMBB = BB;
1374 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1375 ilist<MachineBasicBlock>::iterator It = BB;
1380 // cmpTY cr0, r1, r2
1381 // %TrueValue = li 1
1383 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1384 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1385 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1386 MachineFunction *F = BB->getParent();
1387 F->getBasicBlockList().insert(It, copy0MBB);
1388 F->getBasicBlockList().insert(It, sinkMBB);
1389 // Update machine-CFG edges
1390 BB->addSuccessor(copy0MBB);
1391 BB->addSuccessor(sinkMBB);
1394 // %FalseValue = li 0
1397 // Update machine-CFG edges
1398 BB->addSuccessor(sinkMBB);
1401 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1404 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1405 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1408 assert(0 && "Is this legal?");
1412 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1413 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1414 Opc = SelectSetCR0(N.getOperand(0));
1416 // Create an iterator with which to insert the MBB for copying the false
1417 // value and the MBB to hold the PHI instruction for this SetCC.
1418 MachineBasicBlock *thisMBB = BB;
1419 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1420 ilist<MachineBasicBlock>::iterator It = BB;
1426 // cmpTY cr0, r1, r2
1428 // fallthrough --> copy0MBB
1429 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1430 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1431 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1432 MachineFunction *F = BB->getParent();
1433 F->getBasicBlockList().insert(It, copy0MBB);
1434 F->getBasicBlockList().insert(It, sinkMBB);
1435 // Update machine-CFG edges
1436 BB->addSuccessor(copy0MBB);
1437 BB->addSuccessor(sinkMBB);
1440 // %FalseValue = ...
1441 // # fallthrough to sinkMBB
1443 // Update machine-CFG edges
1444 BB->addSuccessor(sinkMBB);
1447 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1450 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1451 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1453 // FIXME: Select i64?
1458 switch (N.getValueType()) {
1459 default: assert(0 && "Cannot use constants of this type!");
1461 BuildMI(BB, PPC::LI, 1, Result)
1462 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1466 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1467 if (v < 32768 && v >= -32768) {
1468 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1470 Tmp1 = MakeReg(MVT::i32);
1471 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1472 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1482 void ISel::Select(SDOperand N) {
1483 unsigned Tmp1, Tmp2, Opc;
1484 unsigned opcode = N.getOpcode();
1486 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1487 return; // Already selected.
1489 SDNode *Node = N.Val;
1491 switch (Node->getOpcode()) {
1493 Node->dump(); std::cerr << "\n";
1494 assert(0 && "Node not handled yet!");
1495 case ISD::EntryToken: return; // Noop
1496 case ISD::TokenFactor:
1497 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1498 Select(Node->getOperand(i));
1500 case ISD::CALLSEQ_START:
1501 case ISD::CALLSEQ_END:
1502 Select(N.getOperand(0));
1503 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1504 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
1505 PPC::ADJCALLSTACKUP;
1506 BuildMI(BB, Opc, 1).addImm(Tmp1);
1509 MachineBasicBlock *Dest =
1510 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1511 Select(N.getOperand(0));
1512 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1518 case ISD::CopyToReg:
1519 Select(N.getOperand(0));
1520 Tmp1 = SelectExpr(N.getOperand(1));
1521 Tmp2 = cast<RegSDNode>(N)->getReg();
1524 if (N.getOperand(1).getValueType() == MVT::f64 ||
1525 N.getOperand(1).getValueType() == MVT::f32)
1526 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1528 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1531 case ISD::ImplicitDef:
1532 Select(N.getOperand(0));
1533 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1536 switch (N.getNumOperands()) {
1538 assert(0 && "Unknown return instruction!");
1540 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1541 N.getOperand(2).getValueType() == MVT::i32 &&
1542 "Unknown two-register value!");
1543 Select(N.getOperand(0));
1544 Tmp1 = SelectExpr(N.getOperand(1));
1545 Tmp2 = SelectExpr(N.getOperand(2));
1546 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1547 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1550 Select(N.getOperand(0));
1551 Tmp1 = SelectExpr(N.getOperand(1));
1552 switch (N.getOperand(1).getValueType()) {
1554 assert(0 && "Unknown return type!");
1557 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1560 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1564 Select(N.getOperand(0));
1567 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1569 case ISD::TRUNCSTORE:
1572 SDOperand Chain = N.getOperand(0);
1573 SDOperand Value = N.getOperand(1);
1574 SDOperand Address = N.getOperand(2);
1577 Tmp1 = SelectExpr(Value); //value
1579 if (opcode == ISD::STORE) {
1580 switch(Value.getValueType()) {
1581 default: assert(0 && "unknown Type in store");
1582 case MVT::i64: Opc = PPC::STD; break;
1583 case MVT::f64: Opc = PPC::STFD; break;
1584 case MVT::f32: Opc = PPC::STFS; break;
1586 } else { //ISD::TRUNCSTORE
1587 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1588 default: assert(0 && "unknown Type in store");
1589 case MVT::i1: //FIXME: DAG does not promote this load
1590 case MVT::i8: Opc= PPC::STB; break;
1591 case MVT::i16: Opc = PPC::STH; break;
1592 case MVT::i32: Opc = PPC::STW; break;
1596 if(Address.getOpcode() == ISD::FrameIndex)
1598 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1599 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1604 bool idx = SelectAddr(Address, Tmp2, offset);
1606 Opc = IndexedOpForOp(Opc);
1607 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1609 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1618 case ISD::CopyFromReg:
1620 case ISD::DYNAMIC_STACKALLOC:
1625 assert(0 && "Should not be reached!");
1629 /// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1630 /// into a machine code representation using pattern matching and a machine
1631 /// description file.
1633 FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
1634 return new ISel(TM);