blob: 3265b0a96012782a569ebf6a0eda0446e9ea5259 [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);
56
57 /// InstructionSelectBasicBlock - This callback is invoked by
58 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
59 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
60 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000061 // Select target instructions for the DAG.
Chris Lattnera5a91b12005-08-17 19:33:03 +000062 Select(DAG.getRoot());
63 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000064
Chris Lattnerd607c122005-08-18 18:46:06 +000065 // Emit machine code to BB.
66 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000067 }
68
69 virtual const char *getPassName() const {
70 return "PowerPC DAG->DAG Pattern Instruction Selection";
71 }
72 };
73}
74
Nate Begeman0f3257a2005-08-18 05:00:13 +000075// isIntImmediate - This method tests to see if a constant operand.
76// If so Imm will receive the 32 bit value.
77static bool isIntImmediate(SDNode *N, unsigned& Imm) {
78 if (N->getOpcode() == ISD::Constant) {
79 Imm = cast<ConstantSDNode>(N)->getValue();
80 return true;
81 }
82 return false;
83}
84
Nate Begemancffc32b2005-08-18 07:30:46 +000085// isOprShiftImm - Returns true if the specified operand is a shift opcode with
86// a immediate shift count less than 32.
87static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
88 Opc = N->getOpcode();
89 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
90 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
91}
92
93// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
94// any number of 0s on either side. The 1s are allowed to wrap from LSB to
95// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
96// not, since all 1s are not contiguous.
97static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
98 if (isShiftedMask_32(Val)) {
99 // look for the first non-zero bit
100 MB = CountLeadingZeros_32(Val);
101 // look for the first zero bit after the run of ones
102 ME = CountLeadingZeros_32((Val - 1) ^ Val);
103 return true;
104 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
105 // effectively look for the first zero bit
106 ME = CountLeadingZeros_32(Val) - 1;
107 // effectively look for the first one bit after the run of zeros
108 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
109 return true;
110 }
111 // no run present
112 return false;
113}
114
115// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
116// and mask opcode and mask operation.
117static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
118 unsigned &SH, unsigned &MB, unsigned &ME) {
119 unsigned Shift = 32;
120 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
121 unsigned Opcode = N->getOpcode();
122 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
123 return false;
124
125 if (Opcode == ISD::SHL) {
126 // apply shift left to mask if it comes first
127 if (IsShiftMask) Mask = Mask << Shift;
128 // determine which bits are made indeterminant by shift
129 Indeterminant = ~(0xFFFFFFFFu << Shift);
130 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
131 // apply shift right to mask if it comes first
132 if (IsShiftMask) Mask = Mask >> Shift;
133 // determine which bits are made indeterminant by shift
134 Indeterminant = ~(0xFFFFFFFFu >> Shift);
135 // adjust for the left rotate
136 Shift = 32 - Shift;
137 } else {
138 return false;
139 }
140
141 // if the mask doesn't intersect any Indeterminant bits
142 if (Mask && !(Mask & Indeterminant)) {
143 SH = Shift;
144 // make sure the mask is still a mask (wrap arounds may not be)
145 return isRunOfOnes(Mask, MB, ME);
146 }
147 return false;
148}
149
Nate Begeman0f3257a2005-08-18 05:00:13 +0000150// isOpcWithIntImmediate - This method tests to see if the node is a specific
151// opcode and that it has a immediate integer right operand.
152// If so Imm will receive the 32 bit value.
153static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
154 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
155}
156
157// isOprNot - Returns true if the specified operand is an xor with immediate -1.
158static bool isOprNot(SDNode *N) {
159 unsigned Imm;
160 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
161}
162
Chris Lattnera5a91b12005-08-17 19:33:03 +0000163// Immediate constant composers.
164// Lo16 - grabs the lo 16 bits from a 32 bit constant.
165// Hi16 - grabs the hi 16 bits from a 32 bit constant.
166// HA16 - computes the hi bits required if the lo bits are add/subtracted in
167// arithmethically.
168static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
169static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
170static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
171
172// isIntImmediate - This method tests to see if a constant operand.
173// If so Imm will receive the 32 bit value.
174static bool isIntImmediate(SDOperand N, unsigned& Imm) {
175 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
176 Imm = (unsigned)CN->getSignExtended();
177 return true;
178 }
179 return false;
180}
181
182// SelectIntImmediateExpr - Choose code for integer operations with an immediate
183// operand.
184SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
185 unsigned OCHi, unsigned OCLo,
186 bool IsArithmetic,
187 bool Negate) {
188 // Check to make sure this is a constant.
189 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
190 // Exit if not a constant.
191 if (!CN) return 0;
192 // Extract immediate.
193 unsigned C = (unsigned)CN->getValue();
194 // Negate if required (ISD::SUB).
195 if (Negate) C = -C;
196 // Get the hi and lo portions of constant.
197 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
198 unsigned Lo = Lo16(C);
199
200 // If two instructions are needed and usage indicates it would be better to
201 // load immediate into a register, bail out.
202 if (Hi && Lo && CN->use_size() > 2) return false;
203
204 // Select the first operand.
205 SDOperand Opr0 = Select(LHS);
206
207 if (Lo) // Add in the lo-part.
208 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
209 if (Hi) // Add in the hi-part.
210 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
211 return Opr0.Val;
212}
213
214
215// Select - Convert the specified operand from a target-independent to a
216// target-specific node if it hasn't already been changed.
217SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
218 SDNode *N = Op.Val;
219 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
220 return Op; // Already selected.
221
222 switch (N->getOpcode()) {
223 default:
224 std::cerr << "Cannot yet select: ";
225 N->dump();
226 std::cerr << "\n";
227 abort();
228 case ISD::EntryToken: // These leaves remain the same.
229 case ISD::UNDEF:
230 return Op;
231 case ISD::TokenFactor: {
232 SDOperand New;
233 if (N->getNumOperands() == 2) {
234 SDOperand Op0 = Select(N->getOperand(0));
235 SDOperand Op1 = Select(N->getOperand(1));
236 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
237 } else {
238 std::vector<SDOperand> Ops;
239 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
240 Ops.push_back(Select(N->getOperand(0)));
241 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
242 }
243
244 if (New.Val != N) {
245 CurDAG->ReplaceAllUsesWith(N, New.Val);
246 N = New.Val;
247 }
248 break;
249 }
250 case ISD::CopyFromReg: {
251 SDOperand Chain = Select(N->getOperand(0));
252 if (Chain == N->getOperand(0)) return Op; // No change
253 SDOperand New = CurDAG->getCopyFromReg(Chain,
254 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
255 return New.getValue(Op.ResNo);
256 }
257 case ISD::CopyToReg: {
258 SDOperand Chain = Select(N->getOperand(0));
259 SDOperand Reg = N->getOperand(1);
260 SDOperand Val = Select(N->getOperand(2));
261 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
262 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
263 Chain, Reg, Val);
264 CurDAG->ReplaceAllUsesWith(N, New.Val);
265 N = New.Val;
266 }
267 break;
268 }
269 case ISD::Constant: {
270 assert(N->getValueType(0) == MVT::i32);
271 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000272 unsigned Hi = HA16(v);
273 unsigned Lo = Lo16(v);
274 if (Hi && Lo) {
275 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
276 getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000277 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000278 } else if (Lo) {
279 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
280 } else {
281 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000282 }
Nate Begemana6940472005-08-18 18:01:39 +0000283 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000284 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000285 case ISD::SIGN_EXTEND_INREG:
286 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
287 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
288 case MVT::i16:
289 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
290 break;
291 case MVT::i8:
292 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
293 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000294 }
295 break;
296 case ISD::CTLZ:
297 assert(N->getValueType(0) == MVT::i32);
298 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
299 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000300 case ISD::ADD: {
301 MVT::ValueType Ty = N->getValueType(0);
302 if (Ty == MVT::i32) {
303 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
304 PPC::ADDIS, PPC::ADDI, true)) {
305 CurDAG->ReplaceAllUsesWith(N, I);
306 N = I;
307 } else {
308 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
309 Select(N->getOperand(1)));
310 }
311 break;
312 }
313
314 if (!NoExcessFPPrecision) { // Match FMA ops
315 if (N->getOperand(0).getOpcode() == ISD::MUL &&
316 N->getOperand(0).Val->hasOneUse()) {
317 ++FusedFP; // Statistic
318 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
319 Select(N->getOperand(0).getOperand(0)),
320 Select(N->getOperand(0).getOperand(1)),
321 Select(N->getOperand(1)));
322 break;
323 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
324 N->getOperand(1).hasOneUse()) {
325 ++FusedFP; // Statistic
326 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
327 Select(N->getOperand(1).getOperand(0)),
328 Select(N->getOperand(1).getOperand(1)),
329 Select(N->getOperand(0)));
330 break;
331 }
332 }
333
334 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
335 Select(N->getOperand(0)), Select(N->getOperand(1)));
336 break;
337 }
338 case ISD::SUB: {
339 MVT::ValueType Ty = N->getValueType(0);
340 if (Ty == MVT::i32) {
341 unsigned Imm;
342 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
343 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
344 getI32Imm(Lo16(Imm)));
345 break;
346 }
347 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
348 PPC::ADDIS, PPC::ADDI, true, true)) {
349 CurDAG->ReplaceAllUsesWith(N, I);
350 N = I;
351 } else {
352 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
353 Select(N->getOperand(0)));
354 }
355 break;
356 }
357
358 if (!NoExcessFPPrecision) { // Match FMA ops
359 if (N->getOperand(0).getOpcode() == ISD::MUL &&
360 N->getOperand(0).Val->hasOneUse()) {
361 ++FusedFP; // Statistic
362 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
363 Select(N->getOperand(0).getOperand(0)),
364 Select(N->getOperand(0).getOperand(1)),
365 Select(N->getOperand(1)));
366 break;
367 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
368 N->getOperand(1).Val->hasOneUse()) {
369 ++FusedFP; // Statistic
370 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
371 Select(N->getOperand(1).getOperand(0)),
372 Select(N->getOperand(1).getOperand(1)),
373 Select(N->getOperand(0)));
374 break;
375 }
376 }
377 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
378 Select(N->getOperand(0)),
379 Select(N->getOperand(1)));
380 break;
Nate Begeman26653502005-08-17 23:46:35 +0000381 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000382 case ISD::MUL: {
383 unsigned Imm, Opc;
384 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
385 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
386 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
387 break;
388 }
389 switch (N->getValueType(0)) {
390 default: assert(0 && "Unhandled multiply type!");
391 case MVT::i32: Opc = PPC::MULLW; break;
392 case MVT::f32: Opc = PPC::FMULS; break;
393 case MVT::f64: Opc = PPC::FMUL; break;
394 }
395 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
396 Select(N->getOperand(1)));
397 break;
398 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000399 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000400 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000401 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
402 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000403 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000404 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000405 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000406 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
407 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000408 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000409 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000410 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000411 // If this is an and of a value rotated between 0 and 31 bits and then and'd
412 // with a mask, emit rlwinm
413 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
414 isShiftedMask_32(~Imm))) {
415 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000416 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000417 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
418 Val = Select(N->getOperand(0).getOperand(0));
419 } else {
420 Val = Select(N->getOperand(0));
421 isRunOfOnes(Imm, MB, ME);
422 SH = 0;
423 }
424 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
425 getI32Imm(MB), getI32Imm(ME));
426 break;
427 }
428 // If this is an and with an immediate that isn't a mask, then codegen it as
429 // high and low 16 bit immediate ands.
430 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
431 N->getOperand(1),
432 PPC::ANDISo, PPC::ANDIo)) {
433 CurDAG->ReplaceAllUsesWith(N, I);
434 N = I;
435 break;
436 }
437 // Finally, check for the case where we are being asked to select
438 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
439 if (isOprNot(N->getOperand(0).Val))
440 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)),
441 Select(N->getOperand(0).getOperand(0)));
442 else if (isOprNot(N->getOperand(1).Val))
443 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)),
444 Select(N->getOperand(1).getOperand(0)));
445 else
446 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
447 Select(N->getOperand(1)));
448 break;
449 }
Nate Begeman0f3257a2005-08-18 05:00:13 +0000450 case ISD::XOR:
451 // Check whether or not this node is a logical 'not'. This is represented
452 // by llvm as a xor with the constant value -1 (all bits set). If this is a
453 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
454 if (isOprNot(N)) {
455 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +0000456 SDOperand Val = Select(N->getOperand(0));
457 switch (Val.getTargetOpcode()) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000458 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +0000459 case PPC::OR: Opc = PPC::NOR; break;
460 case PPC::AND: Opc = PPC::NAND; break;
461 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000462 }
463 if (Opc)
Nate Begeman131a8802005-08-18 05:44:50 +0000464 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
465 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +0000466 else
Nate Begeman131a8802005-08-18 05:44:50 +0000467 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +0000468 break;
469 }
470 // If this is a xor with an immediate other than -1, then codegen it as high
471 // and low 16 bit immediate xors.
472 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
473 N->getOperand(1),
474 PPC::XORIS, PPC::XORI)) {
475 CurDAG->ReplaceAllUsesWith(N, I);
476 N = I;
477 break;
478 }
479 // Finally, check for the case where we are being asked to select
480 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
481 if (isOprNot(N->getOperand(0).Val))
482 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV,
483 Select(N->getOperand(0).getOperand(0)),
484 Select(N->getOperand(1)));
485 else
486 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
487 Select(N->getOperand(1)));
488 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000489 case ISD::FABS:
Nate Begeman6a7d6112005-08-18 00:53:47 +0000490 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
491 Select(N->getOperand(0)));
492 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000493 case ISD::FP_EXTEND:
494 assert(MVT::f64 == N->getValueType(0) &&
495 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
496 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
497 break;
498 case ISD::FP_ROUND:
499 assert(MVT::f32 == N->getValueType(0) &&
500 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
501 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
502 break;
Nate Begeman26653502005-08-17 23:46:35 +0000503 case ISD::FNEG: {
504 SDOperand Val = Select(N->getOperand(0));
505 MVT::ValueType Ty = N->getValueType(0);
506 if (Val.Val->hasOneUse()) {
507 unsigned Opc;
508 switch (Val.getTargetOpcode()) {
509 default: Opc = 0; break;
510 case PPC::FABS: Opc = PPC::FNABS; break;
511 case PPC::FMADD: Opc = PPC::FNMADD; break;
512 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
513 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
514 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
515 }
516 // If we inverted the opcode, then emit the new instruction with the
517 // inverted opcode and the original instruction's operands. Otherwise,
518 // fall through and generate a fneg instruction.
519 if (Opc) {
520 if (PPC::FNABS == Opc)
521 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
522 else
523 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
524 Val.getOperand(1), Val.getOperand(2));
525 break;
526 }
527 }
528 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
529 break;
530 }
Nate Begeman6a7d6112005-08-18 00:53:47 +0000531 case ISD::FSQRT: {
532 MVT::ValueType Ty = N->getValueType(0);
533 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
534 Select(N->getOperand(0)));
535 break;
536 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000537 case ISD::RET: {
538 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
539
540 if (N->getNumOperands() > 1) {
541 SDOperand Val = Select(N->getOperand(1));
542 switch (N->getOperand(1).getValueType()) {
543 default: assert(0 && "Unknown return type!");
544 case MVT::f64:
545 case MVT::f32:
546 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
547 break;
548 case MVT::i32:
549 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
550 break;
551 }
552
553 if (N->getNumOperands() > 2) {
554 assert(N->getOperand(1).getValueType() == MVT::i32 &&
555 N->getOperand(2).getValueType() == MVT::i32 &&
556 N->getNumOperands() == 2 && "Unknown two-register ret value!");
557 Val = Select(N->getOperand(2));
558 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
559 }
560 }
561
562 // Finally, select this to a blr (return) instruction.
563 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
564 break;
565 }
566 }
567 return SDOperand(N, 0);
568}
569
570
571/// createPPC32ISelDag - This pass converts a legalized DAG into a
572/// PowerPC-specific DAG, ready for instruction scheduling.
573///
574FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
575 return new PPC32DAGToDAGISel(TM);
576}
577