blob: 4e94717762dcecd4aeedffa37cf29901adc954b2 [file] [log] [blame]
Chris Lattnera5a91b12005-08-17 19:33:03 +00001//===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC,
11// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PowerPC.h"
16#include "PPC32TargetMachine.h"
17#include "PPC32ISelLowering.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/CodeGen/SelectionDAGISel.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/MathExtras.h"
24using namespace llvm;
25
26namespace {
27 Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
28 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
29 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
30
31 //===--------------------------------------------------------------------===//
32 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
33 /// instructions for SelectionDAG operations.
34 ///
35 class PPC32DAGToDAGISel : public SelectionDAGISel {
36 PPC32TargetLowering PPC32Lowering;
37
Chris Lattnera5a91b12005-08-17 19:33:03 +000038 public:
39 PPC32DAGToDAGISel(TargetMachine &TM)
40 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
41
Chris Lattnera5a91b12005-08-17 19:33:03 +000042 /// getI32Imm - Return a target constant with the specified value, of type
43 /// i32.
44 inline SDOperand getI32Imm(unsigned Imm) {
45 return CurDAG->getTargetConstant(Imm, MVT::i32);
46 }
47
48 // Select - Convert the specified operand from a target-independent to a
49 // target-specific node if it hasn't already been changed.
50 SDOperand Select(SDOperand Op);
51
52 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
53 unsigned OCHi, unsigned OCLo,
54 bool IsArithmetic = false,
55 bool Negate = false);
Nate Begeman02b88a42005-08-19 00:38:14 +000056 SDNode *SelectBitfieldInsert(SDNode *N);
57
Chris Lattnera5a91b12005-08-17 19:33:03 +000058 /// InstructionSelectBasicBlock - This callback is invoked by
59 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
60 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
61 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000062 // Select target instructions for the DAG.
Chris Lattnera5a91b12005-08-17 19:33:03 +000063 Select(DAG.getRoot());
64 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000065
Chris Lattnerd607c122005-08-18 18:46:06 +000066 // Emit machine code to BB.
67 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000068 }
69
70 virtual const char *getPassName() const {
71 return "PowerPC DAG->DAG Pattern Instruction Selection";
72 }
73 };
74}
75
Nate Begeman0f3257a2005-08-18 05:00:13 +000076// isIntImmediate - This method tests to see if a constant operand.
77// If so Imm will receive the 32 bit value.
78static bool isIntImmediate(SDNode *N, unsigned& Imm) {
79 if (N->getOpcode() == ISD::Constant) {
80 Imm = cast<ConstantSDNode>(N)->getValue();
81 return true;
82 }
83 return false;
84}
85
Nate Begemancffc32b2005-08-18 07:30:46 +000086// isOprShiftImm - Returns true if the specified operand is a shift opcode with
87// a immediate shift count less than 32.
88static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
89 Opc = N->getOpcode();
90 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
91 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
92}
93
94// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
95// any number of 0s on either side. The 1s are allowed to wrap from LSB to
96// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
97// not, since all 1s are not contiguous.
98static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
99 if (isShiftedMask_32(Val)) {
100 // look for the first non-zero bit
101 MB = CountLeadingZeros_32(Val);
102 // look for the first zero bit after the run of ones
103 ME = CountLeadingZeros_32((Val - 1) ^ Val);
104 return true;
105 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
106 // effectively look for the first zero bit
107 ME = CountLeadingZeros_32(Val) - 1;
108 // effectively look for the first one bit after the run of zeros
109 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
110 return true;
111 }
112 // no run present
113 return false;
114}
115
116// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
117// and mask opcode and mask operation.
118static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
119 unsigned &SH, unsigned &MB, unsigned &ME) {
120 unsigned Shift = 32;
121 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
122 unsigned Opcode = N->getOpcode();
123 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
124 return false;
125
126 if (Opcode == ISD::SHL) {
127 // apply shift left to mask if it comes first
128 if (IsShiftMask) Mask = Mask << Shift;
129 // determine which bits are made indeterminant by shift
130 Indeterminant = ~(0xFFFFFFFFu << Shift);
131 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
132 // apply shift right to mask if it comes first
133 if (IsShiftMask) Mask = Mask >> Shift;
134 // determine which bits are made indeterminant by shift
135 Indeterminant = ~(0xFFFFFFFFu >> Shift);
136 // adjust for the left rotate
137 Shift = 32 - Shift;
138 } else {
139 return false;
140 }
141
142 // if the mask doesn't intersect any Indeterminant bits
143 if (Mask && !(Mask & Indeterminant)) {
144 SH = Shift;
145 // make sure the mask is still a mask (wrap arounds may not be)
146 return isRunOfOnes(Mask, MB, ME);
147 }
148 return false;
149}
150
Nate Begeman0f3257a2005-08-18 05:00:13 +0000151// isOpcWithIntImmediate - This method tests to see if the node is a specific
152// opcode and that it has a immediate integer right operand.
153// If so Imm will receive the 32 bit value.
154static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
155 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
156}
157
158// isOprNot - Returns true if the specified operand is an xor with immediate -1.
159static bool isOprNot(SDNode *N) {
160 unsigned Imm;
161 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
162}
163
Chris Lattnera5a91b12005-08-17 19:33:03 +0000164// Immediate constant composers.
165// Lo16 - grabs the lo 16 bits from a 32 bit constant.
166// Hi16 - grabs the hi 16 bits from a 32 bit constant.
167// HA16 - computes the hi bits required if the lo bits are add/subtracted in
168// arithmethically.
169static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
170static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
171static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
172
173// isIntImmediate - This method tests to see if a constant operand.
174// If so Imm will receive the 32 bit value.
175static bool isIntImmediate(SDOperand N, unsigned& Imm) {
176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
177 Imm = (unsigned)CN->getSignExtended();
178 return true;
179 }
180 return false;
181}
182
Nate Begeman02b88a42005-08-19 00:38:14 +0000183/// SelectBitfieldInsert - turn an or of two masked values into
184/// the rotate left word immediate then mask insert (rlwimi) instruction.
185/// Returns true on success, false if the caller still needs to select OR.
186///
187/// Patterns matched:
188/// 1. or shl, and 5. or and, and
189/// 2. or and, shl 6. or shl, shr
190/// 3. or shr, and 7. or shr, shl
191/// 4. or and, shr
192SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
193 bool IsRotate = false;
194 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
195 unsigned Value;
196
197 SDOperand Op0 = N->getOperand(0);
198 SDOperand Op1 = N->getOperand(1);
199
200 unsigned Op0Opc = Op0.getOpcode();
201 unsigned Op1Opc = Op1.getOpcode();
202
203 // Verify that we have the correct opcodes
204 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
205 return false;
206 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
207 return false;
208
209 // Generate Mask value for Target
210 if (isIntImmediate(Op0.getOperand(1), Value)) {
211 switch(Op0Opc) {
212 case ISD::SHL: TgtMask <<= Value; break;
213 case ISD::SRL: TgtMask >>= Value; break;
214 case ISD::AND: TgtMask &= Value; break;
215 }
216 } else {
217 return 0;
218 }
219
220 // Generate Mask value for Insert
221 if (isIntImmediate(Op1.getOperand(1), Value)) {
222 switch(Op1Opc) {
223 case ISD::SHL:
224 SH = Value;
225 InsMask <<= SH;
226 if (Op0Opc == ISD::SRL) IsRotate = true;
227 break;
228 case ISD::SRL:
229 SH = Value;
230 InsMask >>= SH;
231 SH = 32-SH;
232 if (Op0Opc == ISD::SHL) IsRotate = true;
233 break;
234 case ISD::AND:
235 InsMask &= Value;
236 break;
237 }
238 } else {
239 return 0;
240 }
241
242 // If both of the inputs are ANDs and one of them has a logical shift by
243 // constant as its input, make that AND the inserted value so that we can
244 // combine the shift into the rotate part of the rlwimi instruction
245 bool IsAndWithShiftOp = false;
246 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
247 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
248 Op1.getOperand(0).getOpcode() == ISD::SRL) {
249 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
250 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
251 IsAndWithShiftOp = true;
252 }
253 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
254 Op0.getOperand(0).getOpcode() == ISD::SRL) {
255 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
256 std::swap(Op0, Op1);
257 std::swap(TgtMask, InsMask);
258 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
259 IsAndWithShiftOp = true;
260 }
261 }
262 }
263
264 // Verify that the Target mask and Insert mask together form a full word mask
265 // and that the Insert mask is a run of set bits (which implies both are runs
266 // of set bits). Given that, Select the arguments and generate the rlwimi
267 // instruction.
268 unsigned MB, ME;
269 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
270 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
271 bool Op0IsAND = Op0Opc == ISD::AND;
272 // Check for rotlwi / rotrwi here, a special case of bitfield insert
273 // where both bitfield halves are sourced from the same value.
274 if (IsRotate && fullMask &&
275 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
276 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
277 Select(N->getOperand(0).getOperand(0)),
278 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
279 return Op0.Val;
280 }
281 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
282 : Select(Op0);
283 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
284 : Select(Op1.getOperand(0));
285 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
286 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
287 return Op0.Val;
288 }
289 return 0;
290}
291
Chris Lattnera5a91b12005-08-17 19:33:03 +0000292// SelectIntImmediateExpr - Choose code for integer operations with an immediate
293// operand.
294SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
295 unsigned OCHi, unsigned OCLo,
296 bool IsArithmetic,
297 bool Negate) {
298 // Check to make sure this is a constant.
299 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
300 // Exit if not a constant.
301 if (!CN) return 0;
302 // Extract immediate.
303 unsigned C = (unsigned)CN->getValue();
304 // Negate if required (ISD::SUB).
305 if (Negate) C = -C;
306 // Get the hi and lo portions of constant.
307 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
308 unsigned Lo = Lo16(C);
309
310 // If two instructions are needed and usage indicates it would be better to
311 // load immediate into a register, bail out.
312 if (Hi && Lo && CN->use_size() > 2) return false;
313
314 // Select the first operand.
315 SDOperand Opr0 = Select(LHS);
316
317 if (Lo) // Add in the lo-part.
318 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
319 if (Hi) // Add in the hi-part.
320 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
321 return Opr0.Val;
322}
323
324
325// Select - Convert the specified operand from a target-independent to a
326// target-specific node if it hasn't already been changed.
327SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
328 SDNode *N = Op.Val;
329 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
330 return Op; // Already selected.
331
332 switch (N->getOpcode()) {
333 default:
334 std::cerr << "Cannot yet select: ";
335 N->dump();
336 std::cerr << "\n";
337 abort();
338 case ISD::EntryToken: // These leaves remain the same.
339 case ISD::UNDEF:
340 return Op;
341 case ISD::TokenFactor: {
342 SDOperand New;
343 if (N->getNumOperands() == 2) {
344 SDOperand Op0 = Select(N->getOperand(0));
345 SDOperand Op1 = Select(N->getOperand(1));
346 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
347 } else {
348 std::vector<SDOperand> Ops;
349 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
350 Ops.push_back(Select(N->getOperand(0)));
351 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
352 }
353
354 if (New.Val != N) {
355 CurDAG->ReplaceAllUsesWith(N, New.Val);
356 N = New.Val;
357 }
358 break;
359 }
360 case ISD::CopyFromReg: {
361 SDOperand Chain = Select(N->getOperand(0));
362 if (Chain == N->getOperand(0)) return Op; // No change
363 SDOperand New = CurDAG->getCopyFromReg(Chain,
364 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
365 return New.getValue(Op.ResNo);
366 }
367 case ISD::CopyToReg: {
368 SDOperand Chain = Select(N->getOperand(0));
369 SDOperand Reg = N->getOperand(1);
370 SDOperand Val = Select(N->getOperand(2));
371 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
372 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
373 Chain, Reg, Val);
374 CurDAG->ReplaceAllUsesWith(N, New.Val);
375 N = New.Val;
376 }
377 break;
378 }
379 case ISD::Constant: {
380 assert(N->getValueType(0) == MVT::i32);
381 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000382 unsigned Hi = HA16(v);
383 unsigned Lo = Lo16(v);
384 if (Hi && Lo) {
385 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
386 getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000387 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000388 } else if (Lo) {
389 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
390 } else {
391 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000392 }
Nate Begemana6940472005-08-18 18:01:39 +0000393 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000394 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000395 case ISD::SIGN_EXTEND_INREG:
396 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
397 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
398 case MVT::i16:
399 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
400 break;
401 case MVT::i8:
402 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
403 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000404 }
405 break;
406 case ISD::CTLZ:
407 assert(N->getValueType(0) == MVT::i32);
408 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
409 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000410 case ISD::ADD: {
411 MVT::ValueType Ty = N->getValueType(0);
412 if (Ty == MVT::i32) {
413 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
414 PPC::ADDIS, PPC::ADDI, true)) {
415 CurDAG->ReplaceAllUsesWith(N, I);
416 N = I;
417 } else {
418 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
419 Select(N->getOperand(1)));
420 }
421 break;
422 }
423
424 if (!NoExcessFPPrecision) { // Match FMA ops
425 if (N->getOperand(0).getOpcode() == ISD::MUL &&
426 N->getOperand(0).Val->hasOneUse()) {
427 ++FusedFP; // Statistic
428 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
429 Select(N->getOperand(0).getOperand(0)),
430 Select(N->getOperand(0).getOperand(1)),
431 Select(N->getOperand(1)));
432 break;
433 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
434 N->getOperand(1).hasOneUse()) {
435 ++FusedFP; // Statistic
436 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
437 Select(N->getOperand(1).getOperand(0)),
438 Select(N->getOperand(1).getOperand(1)),
439 Select(N->getOperand(0)));
440 break;
441 }
442 }
443
444 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
445 Select(N->getOperand(0)), Select(N->getOperand(1)));
446 break;
447 }
448 case ISD::SUB: {
449 MVT::ValueType Ty = N->getValueType(0);
450 if (Ty == MVT::i32) {
451 unsigned Imm;
452 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
453 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
454 getI32Imm(Lo16(Imm)));
455 break;
456 }
457 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
458 PPC::ADDIS, PPC::ADDI, true, true)) {
459 CurDAG->ReplaceAllUsesWith(N, I);
460 N = I;
461 } else {
462 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
463 Select(N->getOperand(0)));
464 }
465 break;
466 }
467
468 if (!NoExcessFPPrecision) { // Match FMA ops
469 if (N->getOperand(0).getOpcode() == ISD::MUL &&
470 N->getOperand(0).Val->hasOneUse()) {
471 ++FusedFP; // Statistic
472 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
473 Select(N->getOperand(0).getOperand(0)),
474 Select(N->getOperand(0).getOperand(1)),
475 Select(N->getOperand(1)));
476 break;
477 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
478 N->getOperand(1).Val->hasOneUse()) {
479 ++FusedFP; // Statistic
480 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
481 Select(N->getOperand(1).getOperand(0)),
482 Select(N->getOperand(1).getOperand(1)),
483 Select(N->getOperand(0)));
484 break;
485 }
486 }
487 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
488 Select(N->getOperand(0)),
489 Select(N->getOperand(1)));
490 break;
Nate Begeman26653502005-08-17 23:46:35 +0000491 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000492 case ISD::MUL: {
493 unsigned Imm, Opc;
494 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
495 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
496 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
497 break;
498 }
499 switch (N->getValueType(0)) {
500 default: assert(0 && "Unhandled multiply type!");
501 case MVT::i32: Opc = PPC::MULLW; break;
502 case MVT::f32: Opc = PPC::FMULS; break;
503 case MVT::f64: Opc = PPC::FMUL; break;
504 }
505 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
506 Select(N->getOperand(1)));
507 break;
508 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000509 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000510 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000511 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
512 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000513 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000514 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000515 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000516 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
517 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000518 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000519 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000520 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000521 // If this is an and of a value rotated between 0 and 31 bits and then and'd
522 // with a mask, emit rlwinm
523 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
524 isShiftedMask_32(~Imm))) {
525 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000526 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000527 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
528 Val = Select(N->getOperand(0).getOperand(0));
529 } else {
530 Val = Select(N->getOperand(0));
531 isRunOfOnes(Imm, MB, ME);
532 SH = 0;
533 }
534 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
535 getI32Imm(MB), getI32Imm(ME));
536 break;
537 }
538 // If this is an and with an immediate that isn't a mask, then codegen it as
539 // high and low 16 bit immediate ands.
540 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
541 N->getOperand(1),
542 PPC::ANDISo, PPC::ANDIo)) {
543 CurDAG->ReplaceAllUsesWith(N, I);
544 N = I;
545 break;
546 }
547 // Finally, check for the case where we are being asked to select
548 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
549 if (isOprNot(N->getOperand(0).Val))
550 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)),
551 Select(N->getOperand(0).getOperand(0)));
552 else if (isOprNot(N->getOperand(1).Val))
553 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)),
554 Select(N->getOperand(1).getOperand(0)));
555 else
556 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
557 Select(N->getOperand(1)));
558 break;
559 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000560 case ISD::OR:
561 if (SDNode *I = SelectBitfieldInsert(N)) {
562 CurDAG->ReplaceAllUsesWith(N, I);
563 N = I;
564 break;
565 }
566 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
567 N->getOperand(1),
568 PPC::ORIS, PPC::ORI)) {
569 CurDAG->ReplaceAllUsesWith(N, I);
570 N = I;
571 break;
572 }
573 // Finally, check for the case where we are being asked to select
574 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
575 if (isOprNot(N->getOperand(0).Val))
576 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(1)),
577 Select(N->getOperand(0).getOperand(0)));
578 else if (isOprNot(N->getOperand(1).Val))
579 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(0)),
580 Select(N->getOperand(1).getOperand(0)));
581 else
582 CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)),
583 Select(N->getOperand(1)));
584 break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000585 case ISD::XOR:
586 // Check whether or not this node is a logical 'not'. This is represented
587 // by llvm as a xor with the constant value -1 (all bits set). If this is a
588 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
589 if (isOprNot(N)) {
590 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +0000591 SDOperand Val = Select(N->getOperand(0));
592 switch (Val.getTargetOpcode()) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000593 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +0000594 case PPC::OR: Opc = PPC::NOR; break;
595 case PPC::AND: Opc = PPC::NAND; break;
596 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000597 }
598 if (Opc)
Nate Begeman131a8802005-08-18 05:44:50 +0000599 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
600 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +0000601 else
Nate Begeman131a8802005-08-18 05:44:50 +0000602 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +0000603 break;
604 }
605 // If this is a xor with an immediate other than -1, then codegen it as high
606 // and low 16 bit immediate xors.
607 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
608 N->getOperand(1),
609 PPC::XORIS, PPC::XORI)) {
610 CurDAG->ReplaceAllUsesWith(N, I);
611 N = I;
612 break;
613 }
614 // Finally, check for the case where we are being asked to select
615 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
616 if (isOprNot(N->getOperand(0).Val))
617 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV,
618 Select(N->getOperand(0).getOperand(0)),
619 Select(N->getOperand(1)));
620 else
621 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
622 Select(N->getOperand(1)));
623 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000624 case ISD::SHL: {
625 unsigned Imm, SH, MB, ME;
626 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
627 isRotateAndMask(N, Imm, true, SH, MB, ME))
628 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
629 Select(N->getOperand(0).getOperand(0)),
630 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
631 else if (isIntImmediate(N->getOperand(1), Imm))
632 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
633 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
634 else
635 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)),
636 Select(N->getOperand(1)));
637 break;
638 }
639 case ISD::SRL: {
640 unsigned Imm, SH, MB, ME;
641 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
642 isRotateAndMask(N, Imm, true, SH, MB, ME))
643 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
644 Select(N->getOperand(0).getOperand(0)),
645 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
646 else if (isIntImmediate(N->getOperand(1), Imm))
647 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
648 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
649 else
650 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)),
651 Select(N->getOperand(1)));
652 break;
653 }
654 case ISD::SRA: {
655 unsigned Imm, SH, MB, ME;
656 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
657 isRotateAndMask(N, Imm, true, SH, MB, ME))
658 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
659 Select(N->getOperand(0).getOperand(0)),
660 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
661 else if (isIntImmediate(N->getOperand(1), Imm))
662 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAWI, Select(N->getOperand(0)),
663 getI32Imm(Imm));
664 else
665 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)),
666 Select(N->getOperand(1)));
667 break;
668 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000669 case ISD::FABS:
Nate Begeman6a7d6112005-08-18 00:53:47 +0000670 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
671 Select(N->getOperand(0)));
672 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000673 case ISD::FP_EXTEND:
674 assert(MVT::f64 == N->getValueType(0) &&
675 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
676 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
677 break;
678 case ISD::FP_ROUND:
679 assert(MVT::f32 == N->getValueType(0) &&
680 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
681 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
682 break;
Nate Begeman26653502005-08-17 23:46:35 +0000683 case ISD::FNEG: {
684 SDOperand Val = Select(N->getOperand(0));
685 MVT::ValueType Ty = N->getValueType(0);
686 if (Val.Val->hasOneUse()) {
687 unsigned Opc;
688 switch (Val.getTargetOpcode()) {
689 default: Opc = 0; break;
690 case PPC::FABS: Opc = PPC::FNABS; break;
691 case PPC::FMADD: Opc = PPC::FNMADD; break;
692 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
693 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
694 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
695 }
696 // If we inverted the opcode, then emit the new instruction with the
697 // inverted opcode and the original instruction's operands. Otherwise,
698 // fall through and generate a fneg instruction.
699 if (Opc) {
700 if (PPC::FNABS == Opc)
701 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
702 else
703 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
704 Val.getOperand(1), Val.getOperand(2));
705 break;
706 }
707 }
708 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
709 break;
710 }
Nate Begeman6a7d6112005-08-18 00:53:47 +0000711 case ISD::FSQRT: {
712 MVT::ValueType Ty = N->getValueType(0);
713 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
714 Select(N->getOperand(0)));
715 break;
716 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000717 case ISD::RET: {
718 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
719
720 if (N->getNumOperands() > 1) {
721 SDOperand Val = Select(N->getOperand(1));
722 switch (N->getOperand(1).getValueType()) {
723 default: assert(0 && "Unknown return type!");
724 case MVT::f64:
725 case MVT::f32:
726 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
727 break;
728 case MVT::i32:
729 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
730 break;
731 }
732
733 if (N->getNumOperands() > 2) {
734 assert(N->getOperand(1).getValueType() == MVT::i32 &&
735 N->getOperand(2).getValueType() == MVT::i32 &&
736 N->getNumOperands() == 2 && "Unknown two-register ret value!");
737 Val = Select(N->getOperand(2));
738 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
739 }
740 }
741
742 // Finally, select this to a blr (return) instruction.
743 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
744 break;
745 }
746 }
747 return SDOperand(N, 0);
748}
749
750
751/// createPPC32ISelDag - This pass converts a legalized DAG into a
752/// PowerPC-specific DAG, ready for instruction scheduling.
753///
754FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
755 return new PPC32DAGToDAGISel(TM);
756}
757