1 //===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner 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 32 bit PowerPC,
11 // converting from a legalized dag to a PPC dag.
13 //===----------------------------------------------------------------------===//
16 #include "PPC32TargetMachine.h"
17 #include "PPC32ISelLowering.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/GlobalValue.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
31 Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
32 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
33 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
35 //===--------------------------------------------------------------------===//
36 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
37 /// instructions for SelectionDAG operations.
39 class PPC32DAGToDAGISel : public SelectionDAGISel {
40 PPC32TargetLowering PPC32Lowering;
41 unsigned GlobalBaseReg;
43 PPC32DAGToDAGISel(TargetMachine &TM)
44 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
46 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
49 return SelectionDAGISel::runOnFunction(Fn);
52 /// getI32Imm - Return a target constant with the specified value, of type
54 inline SDOperand getI32Imm(unsigned Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i32);
58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
59 /// base register. Return the virtual register that holds this value.
60 SDOperand getGlobalBaseReg();
62 // Select - Convert the specified operand from a target-independent to a
63 // target-specific node if it hasn't already been changed.
64 SDOperand Select(SDOperand Op);
66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
70 SDNode *SelectBitfieldInsert(SDNode *N);
72 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
76 /// SelectAddr - Given the specified address, return the two operands for a
77 /// load/store instruction, and return true if it should be an indexed [r+r]
79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
81 /// InstructionSelectBasicBlock - This callback is invoked by
82 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
83 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
85 // Select target instructions for the DAG.
86 Select(DAG.getRoot());
87 DAG.RemoveDeadNodes();
89 // Emit machine code to BB.
90 ScheduleAndEmitDAG(DAG);
93 virtual const char *getPassName() const {
94 return "PowerPC DAG->DAG Pattern Instruction Selection";
99 /// getGlobalBaseReg - Output the instructions required to put the
100 /// base address to use for accessing globals into a register.
102 SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
103 if (!GlobalBaseReg) {
104 // Insert the set of GlobalBaseReg into the first MBB of the function
105 MachineBasicBlock &FirstMBB = BB->getParent()->front();
106 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
107 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
108 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
109 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
110 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
112 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
116 // isIntImmediate - This method tests to see if a constant operand.
117 // If so Imm will receive the 32 bit value.
118 static bool isIntImmediate(SDNode *N, unsigned& Imm) {
119 if (N->getOpcode() == ISD::Constant) {
120 Imm = cast<ConstantSDNode>(N)->getValue();
126 // isOprShiftImm - Returns true if the specified operand is a shift opcode with
127 // a immediate shift count less than 32.
128 static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
129 Opc = N->getOpcode();
130 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
131 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
134 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
135 // any number of 0s on either side. The 1s are allowed to wrap from LSB to
136 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
137 // not, since all 1s are not contiguous.
138 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
139 if (isShiftedMask_32(Val)) {
140 // look for the first non-zero bit
141 MB = CountLeadingZeros_32(Val);
142 // look for the first zero bit after the run of ones
143 ME = CountLeadingZeros_32((Val - 1) ^ Val);
145 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
146 // effectively look for the first zero bit
147 ME = CountLeadingZeros_32(Val) - 1;
148 // effectively look for the first one bit after the run of zeros
149 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
156 // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
157 // and mask opcode and mask operation.
158 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
159 unsigned &SH, unsigned &MB, unsigned &ME) {
161 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
162 unsigned Opcode = N->getOpcode();
163 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
166 if (Opcode == ISD::SHL) {
167 // apply shift left to mask if it comes first
168 if (IsShiftMask) Mask = Mask << Shift;
169 // determine which bits are made indeterminant by shift
170 Indeterminant = ~(0xFFFFFFFFu << Shift);
171 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
172 // apply shift right to mask if it comes first
173 if (IsShiftMask) Mask = Mask >> Shift;
174 // determine which bits are made indeterminant by shift
175 Indeterminant = ~(0xFFFFFFFFu >> Shift);
176 // adjust for the left rotate
182 // if the mask doesn't intersect any Indeterminant bits
183 if (Mask && !(Mask & Indeterminant)) {
185 // make sure the mask is still a mask (wrap arounds may not be)
186 return isRunOfOnes(Mask, MB, ME);
191 // isOpcWithIntImmediate - This method tests to see if the node is a specific
192 // opcode and that it has a immediate integer right operand.
193 // If so Imm will receive the 32 bit value.
194 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
195 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
198 // isOprNot - Returns true if the specified operand is an xor with immediate -1.
199 static bool isOprNot(SDNode *N) {
201 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
204 // Immediate constant composers.
205 // Lo16 - grabs the lo 16 bits from a 32 bit constant.
206 // Hi16 - grabs the hi 16 bits from a 32 bit constant.
207 // HA16 - computes the hi bits required if the lo bits are add/subtracted in
209 static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
210 static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
211 static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
213 // isIntImmediate - This method tests to see if a constant operand.
214 // If so Imm will receive the 32 bit value.
215 static bool isIntImmediate(SDOperand N, unsigned& Imm) {
216 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
217 Imm = (unsigned)CN->getSignExtended();
223 /// SelectBitfieldInsert - turn an or of two masked values into
224 /// the rotate left word immediate then mask insert (rlwimi) instruction.
225 /// Returns true on success, false if the caller still needs to select OR.
227 /// Patterns matched:
228 /// 1. or shl, and 5. or and, and
229 /// 2. or and, shl 6. or shl, shr
230 /// 3. or shr, and 7. or shr, shl
232 SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
233 bool IsRotate = false;
234 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
237 SDOperand Op0 = N->getOperand(0);
238 SDOperand Op1 = N->getOperand(1);
240 unsigned Op0Opc = Op0.getOpcode();
241 unsigned Op1Opc = Op1.getOpcode();
243 // Verify that we have the correct opcodes
244 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
246 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
249 // Generate Mask value for Target
250 if (isIntImmediate(Op0.getOperand(1), Value)) {
252 case ISD::SHL: TgtMask <<= Value; break;
253 case ISD::SRL: TgtMask >>= Value; break;
254 case ISD::AND: TgtMask &= Value; break;
260 // Generate Mask value for Insert
261 if (isIntImmediate(Op1.getOperand(1), Value)) {
266 if (Op0Opc == ISD::SRL) IsRotate = true;
272 if (Op0Opc == ISD::SHL) IsRotate = true;
282 // If both of the inputs are ANDs and one of them has a logical shift by
283 // constant as its input, make that AND the inserted value so that we can
284 // combine the shift into the rotate part of the rlwimi instruction
285 bool IsAndWithShiftOp = false;
286 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
287 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
288 Op1.getOperand(0).getOpcode() == ISD::SRL) {
289 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
290 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
291 IsAndWithShiftOp = true;
293 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
294 Op0.getOperand(0).getOpcode() == ISD::SRL) {
295 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
297 std::swap(TgtMask, InsMask);
298 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
299 IsAndWithShiftOp = true;
304 // Verify that the Target mask and Insert mask together form a full word mask
305 // and that the Insert mask is a run of set bits (which implies both are runs
306 // of set bits). Given that, Select the arguments and generate the rlwimi
309 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
310 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
311 bool Op0IsAND = Op0Opc == ISD::AND;
312 // Check for rotlwi / rotrwi here, a special case of bitfield insert
313 // where both bitfield halves are sourced from the same value.
314 if (IsRotate && fullMask &&
315 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
316 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
317 Select(N->getOperand(0).getOperand(0)),
318 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
321 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
323 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
324 : Select(Op1.getOperand(0));
325 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
326 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
332 // SelectIntImmediateExpr - Choose code for integer operations with an immediate
334 SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
335 unsigned OCHi, unsigned OCLo,
338 // Check to make sure this is a constant.
339 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
340 // Exit if not a constant.
342 // Extract immediate.
343 unsigned C = (unsigned)CN->getValue();
344 // Negate if required (ISD::SUB).
346 // Get the hi and lo portions of constant.
347 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
348 unsigned Lo = Lo16(C);
350 // If two instructions are needed and usage indicates it would be better to
351 // load immediate into a register, bail out.
352 if (Hi && Lo && CN->use_size() > 2) return false;
354 // Select the first operand.
355 SDOperand Opr0 = Select(LHS);
357 if (Lo) // Add in the lo-part.
358 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
359 if (Hi) // Add in the hi-part.
360 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
364 /// SelectAddr - Given the specified address, return the two operands for a
365 /// load/store instruction, and return true if it should be an indexed [r+r]
367 bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
370 if (Addr.getOpcode() == ISD::ADD) {
371 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
372 Op1 = getI32Imm(Lo16(imm));
373 if (isa<FrameIndexSDNode>(Addr.getOperand(0))) {
375 Op2 = Addr.getOperand(0);
377 Op2 = Select(Addr.getOperand(0));
381 Op1 = Select(Addr.getOperand(0));
382 Op2 = Select(Addr.getOperand(1));
383 return true; // [r+r]
387 // Now check if we're dealing with a global, and whether or not we should emit
388 // an optimized load or store for statics.
389 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
390 GlobalValue *GV = GN->getGlobal();
391 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
392 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
394 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
397 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
400 } else if (isa<FrameIndexSDNode>(Addr)) {
404 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
407 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
409 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
417 /// SelectCC - Select a comparison of the specified values with the specified
418 /// condition code, returning the CR# of the expression.
419 SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
421 // Always select the LHS.
424 // Use U to determine whether the SETCC immediate range is signed or not.
425 if (MVT::isInteger(LHS.getValueType())) {
426 bool U = ISD::isUnsignedIntSetCC(CC);
428 if (isIntImmediate(RHS, Imm) &&
429 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
430 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
431 LHS, getI32Imm(Lo16(Imm)));
432 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
435 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS));
439 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
441 static unsigned getBCCForSetCC(ISD::CondCode CC) {
443 default: assert(0 && "Unknown condition!"); abort();
444 case ISD::SETEQ: return PPC::BEQ;
445 case ISD::SETNE: return PPC::BNE;
447 case ISD::SETLT: return PPC::BLT;
449 case ISD::SETLE: return PPC::BLE;
451 case ISD::SETGT: return PPC::BGT;
453 case ISD::SETGE: return PPC::BGE;
459 // Select - Convert the specified operand from a target-independent to a
460 // target-specific node if it hasn't already been changed.
461 SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
463 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
464 return Op; // Already selected.
466 switch (N->getOpcode()) {
468 std::cerr << "Cannot yet select: ";
472 case ISD::EntryToken: // These leaves remain the same.
475 case ISD::TokenFactor: {
477 if (N->getNumOperands() == 2) {
478 SDOperand Op0 = Select(N->getOperand(0));
479 SDOperand Op1 = Select(N->getOperand(1));
480 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
482 std::vector<SDOperand> Ops;
483 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
484 Ops.push_back(Select(N->getOperand(i)));
485 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
489 CurDAG->ReplaceAllUsesWith(N, New.Val);
494 case ISD::CopyFromReg: {
495 SDOperand Chain = Select(N->getOperand(0));
496 if (Chain == N->getOperand(0)) return Op; // No change
497 SDOperand New = CurDAG->getCopyFromReg(Chain,
498 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
499 return New.getValue(Op.ResNo);
501 case ISD::CopyToReg: {
502 SDOperand Chain = Select(N->getOperand(0));
503 SDOperand Reg = N->getOperand(1);
504 SDOperand Val = Select(N->getOperand(2));
505 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
506 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
508 CurDAG->ReplaceAllUsesWith(N, New.Val);
513 case ISD::Constant: {
514 assert(N->getValueType(0) == MVT::i32);
515 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
516 unsigned Hi = HA16(v);
517 unsigned Lo = Lo16(v);
519 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
521 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
523 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
525 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
529 case ISD::GlobalAddress: {
530 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
532 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
534 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
536 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
538 if (GV->hasWeakLinkage() || GV->isExternal())
539 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LWZ, GA, Tmp);
541 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LA, Tmp, GA);
544 case ISD::SIGN_EXTEND_INREG:
545 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
546 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
548 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
551 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
556 assert(N->getValueType(0) == MVT::i32);
557 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
560 MVT::ValueType Ty = N->getValueType(0);
561 if (Ty == MVT::i32) {
562 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
563 PPC::ADDIS, PPC::ADDI, true)) {
564 CurDAG->ReplaceAllUsesWith(N, I);
567 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
568 Select(N->getOperand(1)));
573 if (!NoExcessFPPrecision) { // Match FMA ops
574 if (N->getOperand(0).getOpcode() == ISD::MUL &&
575 N->getOperand(0).Val->hasOneUse()) {
576 ++FusedFP; // Statistic
577 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
578 Select(N->getOperand(0).getOperand(0)),
579 Select(N->getOperand(0).getOperand(1)),
580 Select(N->getOperand(1)));
582 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
583 N->getOperand(1).hasOneUse()) {
584 ++FusedFP; // Statistic
585 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
586 Select(N->getOperand(1).getOperand(0)),
587 Select(N->getOperand(1).getOperand(1)),
588 Select(N->getOperand(0)));
593 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
594 Select(N->getOperand(0)), Select(N->getOperand(1)));
598 MVT::ValueType Ty = N->getValueType(0);
599 if (Ty == MVT::i32) {
601 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
602 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
603 getI32Imm(Lo16(Imm)));
606 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
607 PPC::ADDIS, PPC::ADDI, true, true)) {
608 CurDAG->ReplaceAllUsesWith(N, I);
611 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
612 Select(N->getOperand(0)));
617 if (!NoExcessFPPrecision) { // Match FMA ops
618 if (N->getOperand(0).getOpcode() == ISD::MUL &&
619 N->getOperand(0).Val->hasOneUse()) {
620 ++FusedFP; // Statistic
621 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
622 Select(N->getOperand(0).getOperand(0)),
623 Select(N->getOperand(0).getOperand(1)),
624 Select(N->getOperand(1)));
626 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
627 N->getOperand(1).Val->hasOneUse()) {
628 ++FusedFP; // Statistic
629 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
630 Select(N->getOperand(1).getOperand(0)),
631 Select(N->getOperand(1).getOperand(1)),
632 Select(N->getOperand(0)));
636 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
637 Select(N->getOperand(0)),
638 Select(N->getOperand(1)));
643 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
644 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
645 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
648 switch (N->getValueType(0)) {
649 default: assert(0 && "Unhandled multiply type!");
650 case MVT::i32: Opc = PPC::MULLW; break;
651 case MVT::f32: Opc = PPC::FMULS; break;
652 case MVT::f64: Opc = PPC::FMUL; break;
654 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
655 Select(N->getOperand(1)));
659 assert(N->getValueType(0) == MVT::i32);
660 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
661 Select(N->getOperand(1)));
664 assert(N->getValueType(0) == MVT::i32);
665 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
666 Select(N->getOperand(1)));
670 // If this is an and of a value rotated between 0 and 31 bits and then and'd
671 // with a mask, emit rlwinm
672 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
673 isShiftedMask_32(~Imm))) {
676 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
677 Val = Select(N->getOperand(0).getOperand(0));
679 Val = Select(N->getOperand(0));
680 isRunOfOnes(Imm, MB, ME);
683 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
684 getI32Imm(MB), getI32Imm(ME));
687 // If this is an and with an immediate that isn't a mask, then codegen it as
688 // high and low 16 bit immediate ands.
689 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
691 PPC::ANDISo, PPC::ANDIo)) {
692 CurDAG->ReplaceAllUsesWith(N, I);
696 // Finally, check for the case where we are being asked to select
697 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
698 if (isOprNot(N->getOperand(0).Val))
699 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)),
700 Select(N->getOperand(0).getOperand(0)));
701 else if (isOprNot(N->getOperand(1).Val))
702 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)),
703 Select(N->getOperand(1).getOperand(0)));
705 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
706 Select(N->getOperand(1)));
710 if (SDNode *I = SelectBitfieldInsert(N)) {
711 CurDAG->ReplaceAllUsesWith(N, I);
715 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
717 PPC::ORIS, PPC::ORI)) {
718 CurDAG->ReplaceAllUsesWith(N, I);
722 // Finally, check for the case where we are being asked to select
723 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
724 if (isOprNot(N->getOperand(0).Val))
725 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(1)),
726 Select(N->getOperand(0).getOperand(0)));
727 else if (isOprNot(N->getOperand(1).Val))
728 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(0)),
729 Select(N->getOperand(1).getOperand(0)));
731 CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)),
732 Select(N->getOperand(1)));
735 // Check whether or not this node is a logical 'not'. This is represented
736 // by llvm as a xor with the constant value -1 (all bits set). If this is a
737 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
740 SDOperand Val = Select(N->getOperand(0));
741 switch (Val.getTargetOpcode()) {
742 default: Opc = 0; break;
743 case PPC::OR: Opc = PPC::NOR; break;
744 case PPC::AND: Opc = PPC::NAND; break;
745 case PPC::XOR: Opc = PPC::EQV; break;
748 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
751 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
754 // If this is a xor with an immediate other than -1, then codegen it as high
755 // and low 16 bit immediate xors.
756 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
758 PPC::XORIS, PPC::XORI)) {
759 CurDAG->ReplaceAllUsesWith(N, I);
763 // Finally, check for the case where we are being asked to select
764 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
765 if (isOprNot(N->getOperand(0).Val))
766 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV,
767 Select(N->getOperand(0).getOperand(0)),
768 Select(N->getOperand(1)));
770 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
771 Select(N->getOperand(1)));
774 unsigned Imm, SH, MB, ME;
775 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
776 isRotateAndMask(N, Imm, true, SH, MB, ME))
777 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
778 Select(N->getOperand(0).getOperand(0)),
779 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
780 else if (isIntImmediate(N->getOperand(1), Imm))
781 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
782 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
784 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)),
785 Select(N->getOperand(1)));
789 unsigned Imm, SH, MB, ME;
790 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
791 isRotateAndMask(N, Imm, true, SH, MB, ME))
792 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
793 Select(N->getOperand(0).getOperand(0)),
794 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
795 else if (isIntImmediate(N->getOperand(1), Imm))
796 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
797 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
799 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)),
800 Select(N->getOperand(1)));
804 unsigned Imm, SH, MB, ME;
805 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
806 isRotateAndMask(N, Imm, true, SH, MB, ME))
807 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
808 Select(N->getOperand(0).getOperand(0)),
809 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
810 else if (isIntImmediate(N->getOperand(1), Imm))
811 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAWI, Select(N->getOperand(0)),
814 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)),
815 Select(N->getOperand(1)));
819 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
820 Select(N->getOperand(0)));
823 assert(MVT::f64 == N->getValueType(0) &&
824 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
825 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
828 assert(MVT::f32 == N->getValueType(0) &&
829 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
830 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
833 SDOperand Val = Select(N->getOperand(0));
834 MVT::ValueType Ty = N->getValueType(0);
835 if (Val.Val->hasOneUse()) {
837 switch (Val.getTargetOpcode()) {
838 default: Opc = 0; break;
839 case PPC::FABS: Opc = PPC::FNABS; break;
840 case PPC::FMADD: Opc = PPC::FNMADD; break;
841 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
842 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
843 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
845 // If we inverted the opcode, then emit the new instruction with the
846 // inverted opcode and the original instruction's operands. Otherwise,
847 // fall through and generate a fneg instruction.
849 if (PPC::FNABS == Opc)
850 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
852 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
853 Val.getOperand(1), Val.getOperand(2));
857 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
861 MVT::ValueType Ty = N->getValueType(0);
862 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
863 Select(N->getOperand(0)));
869 case ISD::SEXTLOAD: {
871 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
873 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
874 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
876 switch (TypeBeingLoaded) {
877 default: N->dump(); assert(0 && "Cannot load this type!");
879 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
881 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
882 Opc = isIdx ? PPC::LHAX : PPC::LHA;
884 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
887 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
888 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
889 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
892 CurDAG->SelectNodeTo(N, N->getValueType(0), MVT::Other, Opc,
893 Op1, Op2, Select(N->getOperand(0)));
898 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
900 if (N->getNumOperands() > 1) {
901 SDOperand Val = Select(N->getOperand(1));
902 switch (N->getOperand(1).getValueType()) {
903 default: assert(0 && "Unknown return type!");
906 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
909 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
913 if (N->getNumOperands() > 2) {
914 assert(N->getOperand(1).getValueType() == MVT::i32 &&
915 N->getOperand(2).getValueType() == MVT::i32 &&
916 N->getNumOperands() == 2 && "Unknown two-register ret value!");
917 Val = Select(N->getOperand(2));
918 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
922 // Finally, select this to a blr (return) instruction.
923 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
928 case ISD::BRTWOWAY_CC: {
929 SDOperand Chain = Select(N->getOperand(0));
930 MachineBasicBlock *Dest =
931 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
932 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
933 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
934 unsigned Opc = getBCCForSetCC(CC);
936 // If this is a two way branch, then grab the fallthrough basic block
937 // argument and build a PowerPC branch pseudo-op, suitable for long branch
938 // conversion if necessary by the branch selection pass. Otherwise, emit a
939 // standard conditional branch.
940 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
941 MachineBasicBlock *Fallthrough =
942 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
943 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
944 CondCode, getI32Imm(Opc),
945 N->getOperand(4), N->getOperand(5),
947 CurDAG->SelectNodeTo(N, MVT::Other, PPC::B, N->getOperand(5), CB);
949 // Iterate to the next basic block
950 ilist<MachineBasicBlock>::iterator It = BB;
953 // If the fallthrough path is off the end of the function, which would be
954 // undefined behavior, set it to be the same as the current block because
955 // we have nothing better to set it to, and leaving it alone will cause
956 // the PowerPC Branch Selection pass to crash.
957 if (It == BB->getParent()->end()) It = Dest;
958 CurDAG->SelectNodeTo(N, MVT::Other, PPC::COND_BRANCH, CondCode,
959 getI32Imm(Opc), N->getOperand(4),
960 CurDAG->getBasicBlock(It), Chain);
965 return SDOperand(N, Op.ResNo);
969 /// createPPC32ISelDag - This pass converts a legalized DAG into a
970 /// PowerPC-specific DAG, ready for instruction scheduling.
972 FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
973 return new PPC32DAGToDAGISel(TM);