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 unsigned 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 /// InstructionSelectBasicBlock - This callback is invoked by
73 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
74 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
76 // Select target instructions for the DAG.
77 Select(DAG.getRoot());
78 DAG.RemoveDeadNodes();
80 // Emit machine code to BB.
81 ScheduleAndEmitDAG(DAG);
84 virtual const char *getPassName() const {
85 return "PowerPC DAG->DAG Pattern Instruction Selection";
90 /// getGlobalBaseReg - Output the instructions required to put the
91 /// base address to use for accessing globals into a register.
93 unsigned PPC32DAGToDAGISel::getGlobalBaseReg() {
95 // Insert the set of GlobalBaseReg into the first MBB of the function
96 MachineBasicBlock &FirstMBB = BB->getParent()->front();
97 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
98 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
99 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
100 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
101 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
103 return GlobalBaseReg;
107 // isIntImmediate - This method tests to see if a constant operand.
108 // If so Imm will receive the 32 bit value.
109 static bool isIntImmediate(SDNode *N, unsigned& Imm) {
110 if (N->getOpcode() == ISD::Constant) {
111 Imm = cast<ConstantSDNode>(N)->getValue();
117 // isOprShiftImm - Returns true if the specified operand is a shift opcode with
118 // a immediate shift count less than 32.
119 static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
120 Opc = N->getOpcode();
121 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
122 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
125 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
126 // any number of 0s on either side. The 1s are allowed to wrap from LSB to
127 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
128 // not, since all 1s are not contiguous.
129 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
130 if (isShiftedMask_32(Val)) {
131 // look for the first non-zero bit
132 MB = CountLeadingZeros_32(Val);
133 // look for the first zero bit after the run of ones
134 ME = CountLeadingZeros_32((Val - 1) ^ Val);
136 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
137 // effectively look for the first zero bit
138 ME = CountLeadingZeros_32(Val) - 1;
139 // effectively look for the first one bit after the run of zeros
140 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
147 // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
148 // and mask opcode and mask operation.
149 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
150 unsigned &SH, unsigned &MB, unsigned &ME) {
152 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
153 unsigned Opcode = N->getOpcode();
154 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
157 if (Opcode == ISD::SHL) {
158 // apply shift left to mask if it comes first
159 if (IsShiftMask) Mask = Mask << Shift;
160 // determine which bits are made indeterminant by shift
161 Indeterminant = ~(0xFFFFFFFFu << Shift);
162 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
163 // apply shift right to mask if it comes first
164 if (IsShiftMask) Mask = Mask >> Shift;
165 // determine which bits are made indeterminant by shift
166 Indeterminant = ~(0xFFFFFFFFu >> Shift);
167 // adjust for the left rotate
173 // if the mask doesn't intersect any Indeterminant bits
174 if (Mask && !(Mask & Indeterminant)) {
176 // make sure the mask is still a mask (wrap arounds may not be)
177 return isRunOfOnes(Mask, MB, ME);
182 // isOpcWithIntImmediate - This method tests to see if the node is a specific
183 // opcode and that it has a immediate integer right operand.
184 // If so Imm will receive the 32 bit value.
185 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
186 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
189 // isOprNot - Returns true if the specified operand is an xor with immediate -1.
190 static bool isOprNot(SDNode *N) {
192 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
195 // Immediate constant composers.
196 // Lo16 - grabs the lo 16 bits from a 32 bit constant.
197 // Hi16 - grabs the hi 16 bits from a 32 bit constant.
198 // HA16 - computes the hi bits required if the lo bits are add/subtracted in
200 static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
201 static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
202 static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
204 // isIntImmediate - This method tests to see if a constant operand.
205 // If so Imm will receive the 32 bit value.
206 static bool isIntImmediate(SDOperand N, unsigned& Imm) {
207 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
208 Imm = (unsigned)CN->getSignExtended();
214 /// SelectBitfieldInsert - turn an or of two masked values into
215 /// the rotate left word immediate then mask insert (rlwimi) instruction.
216 /// Returns true on success, false if the caller still needs to select OR.
218 /// Patterns matched:
219 /// 1. or shl, and 5. or and, and
220 /// 2. or and, shl 6. or shl, shr
221 /// 3. or shr, and 7. or shr, shl
223 SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
224 bool IsRotate = false;
225 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
228 SDOperand Op0 = N->getOperand(0);
229 SDOperand Op1 = N->getOperand(1);
231 unsigned Op0Opc = Op0.getOpcode();
232 unsigned Op1Opc = Op1.getOpcode();
234 // Verify that we have the correct opcodes
235 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
237 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
240 // Generate Mask value for Target
241 if (isIntImmediate(Op0.getOperand(1), Value)) {
243 case ISD::SHL: TgtMask <<= Value; break;
244 case ISD::SRL: TgtMask >>= Value; break;
245 case ISD::AND: TgtMask &= Value; break;
251 // Generate Mask value for Insert
252 if (isIntImmediate(Op1.getOperand(1), Value)) {
257 if (Op0Opc == ISD::SRL) IsRotate = true;
263 if (Op0Opc == ISD::SHL) IsRotate = true;
273 // If both of the inputs are ANDs and one of them has a logical shift by
274 // constant as its input, make that AND the inserted value so that we can
275 // combine the shift into the rotate part of the rlwimi instruction
276 bool IsAndWithShiftOp = false;
277 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
278 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
279 Op1.getOperand(0).getOpcode() == ISD::SRL) {
280 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
281 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
282 IsAndWithShiftOp = true;
284 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
285 Op0.getOperand(0).getOpcode() == ISD::SRL) {
286 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
288 std::swap(TgtMask, InsMask);
289 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
290 IsAndWithShiftOp = true;
295 // Verify that the Target mask and Insert mask together form a full word mask
296 // and that the Insert mask is a run of set bits (which implies both are runs
297 // of set bits). Given that, Select the arguments and generate the rlwimi
300 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
301 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
302 bool Op0IsAND = Op0Opc == ISD::AND;
303 // Check for rotlwi / rotrwi here, a special case of bitfield insert
304 // where both bitfield halves are sourced from the same value.
305 if (IsRotate && fullMask &&
306 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
307 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
308 Select(N->getOperand(0).getOperand(0)),
309 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
312 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
314 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
315 : Select(Op1.getOperand(0));
316 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
317 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
323 // SelectIntImmediateExpr - Choose code for integer operations with an immediate
325 SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
326 unsigned OCHi, unsigned OCLo,
329 // Check to make sure this is a constant.
330 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
331 // Exit if not a constant.
333 // Extract immediate.
334 unsigned C = (unsigned)CN->getValue();
335 // Negate if required (ISD::SUB).
337 // Get the hi and lo portions of constant.
338 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
339 unsigned Lo = Lo16(C);
341 // If two instructions are needed and usage indicates it would be better to
342 // load immediate into a register, bail out.
343 if (Hi && Lo && CN->use_size() > 2) return false;
345 // Select the first operand.
346 SDOperand Opr0 = Select(LHS);
348 if (Lo) // Add in the lo-part.
349 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
350 if (Hi) // Add in the hi-part.
351 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
356 // Select - Convert the specified operand from a target-independent to a
357 // target-specific node if it hasn't already been changed.
358 SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
360 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
361 return Op; // Already selected.
363 switch (N->getOpcode()) {
365 std::cerr << "Cannot yet select: ";
369 case ISD::EntryToken: // These leaves remain the same.
372 case ISD::TokenFactor: {
374 if (N->getNumOperands() == 2) {
375 SDOperand Op0 = Select(N->getOperand(0));
376 SDOperand Op1 = Select(N->getOperand(1));
377 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
379 std::vector<SDOperand> Ops;
380 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
381 Ops.push_back(Select(N->getOperand(i)));
382 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
386 CurDAG->ReplaceAllUsesWith(N, New.Val);
391 case ISD::CopyFromReg: {
392 SDOperand Chain = Select(N->getOperand(0));
393 if (Chain == N->getOperand(0)) return Op; // No change
394 SDOperand New = CurDAG->getCopyFromReg(Chain,
395 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
396 return New.getValue(Op.ResNo);
398 case ISD::CopyToReg: {
399 SDOperand Chain = Select(N->getOperand(0));
400 SDOperand Reg = N->getOperand(1);
401 SDOperand Val = Select(N->getOperand(2));
402 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
403 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
405 CurDAG->ReplaceAllUsesWith(N, New.Val);
410 case ISD::Constant: {
411 assert(N->getValueType(0) == MVT::i32);
412 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
413 unsigned Hi = HA16(v);
414 unsigned Lo = Lo16(v);
416 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
418 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
420 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
422 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
426 case ISD::GlobalAddress: {
427 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
429 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
431 SDOperand PICBaseReg = CurDAG->getRegister(getGlobalBaseReg(), MVT::i32);
432 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, PICBaseReg, GA);
434 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
436 if (GV->hasWeakLinkage() || GV->isExternal())
437 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LWZ, GA, Tmp);
439 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LA, Tmp, GA);
442 case ISD::SIGN_EXTEND_INREG:
443 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
444 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
446 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
449 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
454 assert(N->getValueType(0) == MVT::i32);
455 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
458 MVT::ValueType Ty = N->getValueType(0);
459 if (Ty == MVT::i32) {
460 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
461 PPC::ADDIS, PPC::ADDI, true)) {
462 CurDAG->ReplaceAllUsesWith(N, I);
465 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
466 Select(N->getOperand(1)));
471 if (!NoExcessFPPrecision) { // Match FMA ops
472 if (N->getOperand(0).getOpcode() == ISD::MUL &&
473 N->getOperand(0).Val->hasOneUse()) {
474 ++FusedFP; // Statistic
475 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
476 Select(N->getOperand(0).getOperand(0)),
477 Select(N->getOperand(0).getOperand(1)),
478 Select(N->getOperand(1)));
480 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
481 N->getOperand(1).hasOneUse()) {
482 ++FusedFP; // Statistic
483 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
484 Select(N->getOperand(1).getOperand(0)),
485 Select(N->getOperand(1).getOperand(1)),
486 Select(N->getOperand(0)));
491 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
492 Select(N->getOperand(0)), Select(N->getOperand(1)));
496 MVT::ValueType Ty = N->getValueType(0);
497 if (Ty == MVT::i32) {
499 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
500 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
501 getI32Imm(Lo16(Imm)));
504 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
505 PPC::ADDIS, PPC::ADDI, true, true)) {
506 CurDAG->ReplaceAllUsesWith(N, I);
509 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
510 Select(N->getOperand(0)));
515 if (!NoExcessFPPrecision) { // Match FMA ops
516 if (N->getOperand(0).getOpcode() == ISD::MUL &&
517 N->getOperand(0).Val->hasOneUse()) {
518 ++FusedFP; // Statistic
519 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
520 Select(N->getOperand(0).getOperand(0)),
521 Select(N->getOperand(0).getOperand(1)),
522 Select(N->getOperand(1)));
524 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
525 N->getOperand(1).Val->hasOneUse()) {
526 ++FusedFP; // Statistic
527 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
528 Select(N->getOperand(1).getOperand(0)),
529 Select(N->getOperand(1).getOperand(1)),
530 Select(N->getOperand(0)));
534 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
535 Select(N->getOperand(0)),
536 Select(N->getOperand(1)));
541 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
542 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
543 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
546 switch (N->getValueType(0)) {
547 default: assert(0 && "Unhandled multiply type!");
548 case MVT::i32: Opc = PPC::MULLW; break;
549 case MVT::f32: Opc = PPC::FMULS; break;
550 case MVT::f64: Opc = PPC::FMUL; break;
552 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
553 Select(N->getOperand(1)));
557 assert(N->getValueType(0) == MVT::i32);
558 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
559 Select(N->getOperand(1)));
562 assert(N->getValueType(0) == MVT::i32);
563 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
564 Select(N->getOperand(1)));
568 // If this is an and of a value rotated between 0 and 31 bits and then and'd
569 // with a mask, emit rlwinm
570 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
571 isShiftedMask_32(~Imm))) {
574 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
575 Val = Select(N->getOperand(0).getOperand(0));
577 Val = Select(N->getOperand(0));
578 isRunOfOnes(Imm, MB, ME);
581 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
582 getI32Imm(MB), getI32Imm(ME));
585 // If this is an and with an immediate that isn't a mask, then codegen it as
586 // high and low 16 bit immediate ands.
587 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
589 PPC::ANDISo, PPC::ANDIo)) {
590 CurDAG->ReplaceAllUsesWith(N, I);
594 // Finally, check for the case where we are being asked to select
595 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
596 if (isOprNot(N->getOperand(0).Val))
597 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)),
598 Select(N->getOperand(0).getOperand(0)));
599 else if (isOprNot(N->getOperand(1).Val))
600 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)),
601 Select(N->getOperand(1).getOperand(0)));
603 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
604 Select(N->getOperand(1)));
608 if (SDNode *I = SelectBitfieldInsert(N)) {
609 CurDAG->ReplaceAllUsesWith(N, I);
613 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
615 PPC::ORIS, PPC::ORI)) {
616 CurDAG->ReplaceAllUsesWith(N, I);
620 // Finally, check for the case where we are being asked to select
621 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
622 if (isOprNot(N->getOperand(0).Val))
623 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(1)),
624 Select(N->getOperand(0).getOperand(0)));
625 else if (isOprNot(N->getOperand(1).Val))
626 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(0)),
627 Select(N->getOperand(1).getOperand(0)));
629 CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)),
630 Select(N->getOperand(1)));
633 // Check whether or not this node is a logical 'not'. This is represented
634 // by llvm as a xor with the constant value -1 (all bits set). If this is a
635 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
638 SDOperand Val = Select(N->getOperand(0));
639 switch (Val.getTargetOpcode()) {
640 default: Opc = 0; break;
641 case PPC::OR: Opc = PPC::NOR; break;
642 case PPC::AND: Opc = PPC::NAND; break;
643 case PPC::XOR: Opc = PPC::EQV; break;
646 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
649 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
652 // If this is a xor with an immediate other than -1, then codegen it as high
653 // and low 16 bit immediate xors.
654 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
656 PPC::XORIS, PPC::XORI)) {
657 CurDAG->ReplaceAllUsesWith(N, I);
661 // Finally, check for the case where we are being asked to select
662 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
663 if (isOprNot(N->getOperand(0).Val))
664 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV,
665 Select(N->getOperand(0).getOperand(0)),
666 Select(N->getOperand(1)));
668 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
669 Select(N->getOperand(1)));
672 unsigned Imm, SH, MB, ME;
673 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
674 isRotateAndMask(N, Imm, true, SH, MB, ME))
675 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
676 Select(N->getOperand(0).getOperand(0)),
677 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
678 else if (isIntImmediate(N->getOperand(1), Imm))
679 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
680 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
682 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)),
683 Select(N->getOperand(1)));
687 unsigned Imm, SH, MB, ME;
688 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
689 isRotateAndMask(N, Imm, true, SH, MB, ME))
690 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
691 Select(N->getOperand(0).getOperand(0)),
692 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
693 else if (isIntImmediate(N->getOperand(1), Imm))
694 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
695 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
697 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)),
698 Select(N->getOperand(1)));
702 unsigned Imm, SH, MB, ME;
703 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
704 isRotateAndMask(N, Imm, true, SH, MB, ME))
705 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
706 Select(N->getOperand(0).getOperand(0)),
707 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
708 else if (isIntImmediate(N->getOperand(1), Imm))
709 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAWI, Select(N->getOperand(0)),
712 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)),
713 Select(N->getOperand(1)));
717 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
718 Select(N->getOperand(0)));
721 assert(MVT::f64 == N->getValueType(0) &&
722 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
723 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
726 assert(MVT::f32 == N->getValueType(0) &&
727 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
728 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
731 SDOperand Val = Select(N->getOperand(0));
732 MVT::ValueType Ty = N->getValueType(0);
733 if (Val.Val->hasOneUse()) {
735 switch (Val.getTargetOpcode()) {
736 default: Opc = 0; break;
737 case PPC::FABS: Opc = PPC::FNABS; break;
738 case PPC::FMADD: Opc = PPC::FNMADD; break;
739 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
740 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
741 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
743 // If we inverted the opcode, then emit the new instruction with the
744 // inverted opcode and the original instruction's operands. Otherwise,
745 // fall through and generate a fneg instruction.
747 if (PPC::FNABS == Opc)
748 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
750 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
751 Val.getOperand(1), Val.getOperand(2));
755 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
759 MVT::ValueType Ty = N->getValueType(0);
760 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
761 Select(N->getOperand(0)));
765 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
767 if (N->getNumOperands() > 1) {
768 SDOperand Val = Select(N->getOperand(1));
769 switch (N->getOperand(1).getValueType()) {
770 default: assert(0 && "Unknown return type!");
773 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
776 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
780 if (N->getNumOperands() > 2) {
781 assert(N->getOperand(1).getValueType() == MVT::i32 &&
782 N->getOperand(2).getValueType() == MVT::i32 &&
783 N->getNumOperands() == 2 && "Unknown two-register ret value!");
784 Val = Select(N->getOperand(2));
785 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
789 // Finally, select this to a blr (return) instruction.
790 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
794 return SDOperand(N, 0);
798 /// createPPC32ISelDag - This pass converts a legalized DAG into a
799 /// PowerPC-specific DAG, ready for instruction scheduling.
801 FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
802 return new PPC32DAGToDAGISel(TM);