blob: 03028822d8ed507539349c850ca7f6c0f9098ac9 [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"
Chris Lattner4416f1a2005-08-19 22:38:53 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000025#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/MathExtras.h"
28using namespace llvm;
29
30namespace {
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");
34
35 //===--------------------------------------------------------------------===//
36 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class PPC32DAGToDAGISel : public SelectionDAGISel {
40 PPC32TargetLowering PPC32Lowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000041 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000042 public:
43 PPC32DAGToDAGISel(TargetMachine &TM)
44 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
45
Chris Lattner4416f1a2005-08-19 22:38:53 +000046 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
48 GlobalBaseReg = 0;
49 return SelectionDAGISel::runOnFunction(Fn);
50 }
51
Chris Lattnera5a91b12005-08-17 19:33:03 +000052 /// getI32Imm - Return a target constant with the specified value, of type
53 /// i32.
54 inline SDOperand getI32Imm(unsigned Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i32);
56 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000057
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();
Chris Lattnera5a91b12005-08-17 19:33:03 +000061
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);
65
66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
69 bool Negate = false);
Nate Begeman02b88a42005-08-19 00:38:14 +000070 SDNode *SelectBitfieldInsert(SDNode *N);
71
Chris Lattnera5a91b12005-08-17 19:33:03 +000072 /// InstructionSelectBasicBlock - This callback is invoked by
73 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
74 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
75 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000076 // Select target instructions for the DAG.
Chris Lattnera5a91b12005-08-17 19:33:03 +000077 Select(DAG.getRoot());
78 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000079
Chris Lattnerd607c122005-08-18 18:46:06 +000080 // Emit machine code to BB.
81 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000082 }
83
84 virtual const char *getPassName() const {
85 return "PowerPC DAG->DAG Pattern Instruction Selection";
86 }
87 };
88}
89
Chris Lattner4416f1a2005-08-19 22:38:53 +000090/// getGlobalBaseReg - Output the instructions required to put the
91/// base address to use for accessing globals into a register.
92///
93unsigned PPC32DAGToDAGISel::getGlobalBaseReg() {
94 if (!GlobalBaseReg) {
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);
102 }
103 return GlobalBaseReg;
104}
105
106
Nate Begeman0f3257a2005-08-18 05:00:13 +0000107// isIntImmediate - This method tests to see if a constant operand.
108// If so Imm will receive the 32 bit value.
109static bool isIntImmediate(SDNode *N, unsigned& Imm) {
110 if (N->getOpcode() == ISD::Constant) {
111 Imm = cast<ConstantSDNode>(N)->getValue();
112 return true;
113 }
114 return false;
115}
116
Nate Begemancffc32b2005-08-18 07:30:46 +0000117// isOprShiftImm - Returns true if the specified operand is a shift opcode with
118// a immediate shift count less than 32.
119static 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;
123}
124
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.
129static 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);
135 return true;
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;
141 return true;
142 }
143 // no run present
144 return false;
145}
146
147// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
148// and mask opcode and mask operation.
149static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
150 unsigned &SH, unsigned &MB, unsigned &ME) {
151 unsigned Shift = 32;
152 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
153 unsigned Opcode = N->getOpcode();
154 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
155 return false;
156
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
168 Shift = 32 - Shift;
169 } else {
170 return false;
171 }
172
173 // if the mask doesn't intersect any Indeterminant bits
174 if (Mask && !(Mask & Indeterminant)) {
175 SH = Shift;
176 // make sure the mask is still a mask (wrap arounds may not be)
177 return isRunOfOnes(Mask, MB, ME);
178 }
179 return false;
180}
181
Nate Begeman0f3257a2005-08-18 05:00:13 +0000182// 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.
185static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
186 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
187}
188
189// isOprNot - Returns true if the specified operand is an xor with immediate -1.
190static bool isOprNot(SDNode *N) {
191 unsigned Imm;
192 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
193}
194
Chris Lattnera5a91b12005-08-17 19:33:03 +0000195// 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
199// arithmethically.
200static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
201static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
202static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
203
204// isIntImmediate - This method tests to see if a constant operand.
205// If so Imm will receive the 32 bit value.
206static bool isIntImmediate(SDOperand N, unsigned& Imm) {
207 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
208 Imm = (unsigned)CN->getSignExtended();
209 return true;
210 }
211 return false;
212}
213
Nate Begeman02b88a42005-08-19 00:38:14 +0000214/// 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.
217///
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
222/// 4. or and, shr
223SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
224 bool IsRotate = false;
225 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
226 unsigned Value;
227
228 SDOperand Op0 = N->getOperand(0);
229 SDOperand Op1 = N->getOperand(1);
230
231 unsigned Op0Opc = Op0.getOpcode();
232 unsigned Op1Opc = Op1.getOpcode();
233
234 // Verify that we have the correct opcodes
235 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
236 return false;
237 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
238 return false;
239
240 // Generate Mask value for Target
241 if (isIntImmediate(Op0.getOperand(1), Value)) {
242 switch(Op0Opc) {
243 case ISD::SHL: TgtMask <<= Value; break;
244 case ISD::SRL: TgtMask >>= Value; break;
245 case ISD::AND: TgtMask &= Value; break;
246 }
247 } else {
248 return 0;
249 }
250
251 // Generate Mask value for Insert
252 if (isIntImmediate(Op1.getOperand(1), Value)) {
253 switch(Op1Opc) {
254 case ISD::SHL:
255 SH = Value;
256 InsMask <<= SH;
257 if (Op0Opc == ISD::SRL) IsRotate = true;
258 break;
259 case ISD::SRL:
260 SH = Value;
261 InsMask >>= SH;
262 SH = 32-SH;
263 if (Op0Opc == ISD::SHL) IsRotate = true;
264 break;
265 case ISD::AND:
266 InsMask &= Value;
267 break;
268 }
269 } else {
270 return 0;
271 }
272
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;
283 }
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)) {
287 std::swap(Op0, Op1);
288 std::swap(TgtMask, InsMask);
289 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
290 IsAndWithShiftOp = true;
291 }
292 }
293 }
294
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
298 // instruction.
299 unsigned MB, ME;
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));
310 return Op0.Val;
311 }
312 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
313 : Select(Op0);
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));
318 return Op0.Val;
319 }
320 return 0;
321}
322
Chris Lattnera5a91b12005-08-17 19:33:03 +0000323// SelectIntImmediateExpr - Choose code for integer operations with an immediate
324// operand.
325SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
326 unsigned OCHi, unsigned OCLo,
327 bool IsArithmetic,
328 bool Negate) {
329 // Check to make sure this is a constant.
330 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
331 // Exit if not a constant.
332 if (!CN) return 0;
333 // Extract immediate.
334 unsigned C = (unsigned)CN->getValue();
335 // Negate if required (ISD::SUB).
336 if (Negate) C = -C;
337 // Get the hi and lo portions of constant.
338 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
339 unsigned Lo = Lo16(C);
340
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;
344
345 // Select the first operand.
346 SDOperand Opr0 = Select(LHS);
347
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));
352 return Opr0.Val;
353}
354
355
356// Select - Convert the specified operand from a target-independent to a
357// target-specific node if it hasn't already been changed.
358SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
359 SDNode *N = Op.Val;
360 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
361 return Op; // Already selected.
362
363 switch (N->getOpcode()) {
364 default:
365 std::cerr << "Cannot yet select: ";
366 N->dump();
367 std::cerr << "\n";
368 abort();
369 case ISD::EntryToken: // These leaves remain the same.
370 case ISD::UNDEF:
371 return Op;
372 case ISD::TokenFactor: {
373 SDOperand New;
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);
378 } else {
379 std::vector<SDOperand> Ops;
380 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000381 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000382 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
383 }
384
385 if (New.Val != N) {
386 CurDAG->ReplaceAllUsesWith(N, New.Val);
387 N = New.Val;
388 }
389 break;
390 }
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);
397 }
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,
404 Chain, Reg, Val);
405 CurDAG->ReplaceAllUsesWith(N, New.Val);
406 N = New.Val;
407 }
408 break;
409 }
410 case ISD::Constant: {
411 assert(N->getValueType(0) == MVT::i32);
412 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000413 unsigned Hi = HA16(v);
414 unsigned Lo = Lo16(v);
415 if (Hi && Lo) {
416 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
417 getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000418 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000419 } else if (Lo) {
420 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
421 } else {
422 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000423 }
Nate Begemana6940472005-08-18 18:01:39 +0000424 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000425 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000426 case ISD::GlobalAddress: {
427 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
428 SDOperand Tmp;
429 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
430 if (PICEnabled) {
431 SDOperand PICBaseReg = CurDAG->getRegister(getGlobalBaseReg(), MVT::i32);
432 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, PICBaseReg, GA);
433 } else {
434 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
435 }
436 if (GV->hasWeakLinkage() || GV->isExternal())
437 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LWZ, GA, Tmp);
438 else
439 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LA, Tmp, GA);
440 break;
441 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000442 case ISD::SIGN_EXTEND_INREG:
443 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
444 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
445 case MVT::i16:
446 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
447 break;
448 case MVT::i8:
449 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
450 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000451 }
452 break;
453 case ISD::CTLZ:
454 assert(N->getValueType(0) == MVT::i32);
455 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
456 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000457 case ISD::ADD: {
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);
463 N = I;
464 } else {
465 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
466 Select(N->getOperand(1)));
467 }
468 break;
469 }
470
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)));
479 break;
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)));
487 break;
488 }
489 }
490
491 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
492 Select(N->getOperand(0)), Select(N->getOperand(1)));
493 break;
494 }
495 case ISD::SUB: {
496 MVT::ValueType Ty = N->getValueType(0);
497 if (Ty == MVT::i32) {
498 unsigned Imm;
499 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
500 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
501 getI32Imm(Lo16(Imm)));
502 break;
503 }
504 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
505 PPC::ADDIS, PPC::ADDI, true, true)) {
506 CurDAG->ReplaceAllUsesWith(N, I);
507 N = I;
508 } else {
509 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
510 Select(N->getOperand(0)));
511 }
512 break;
513 }
514
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)));
523 break;
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)));
531 break;
532 }
533 }
534 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
535 Select(N->getOperand(0)),
536 Select(N->getOperand(1)));
537 break;
Nate Begeman26653502005-08-17 23:46:35 +0000538 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000539 case ISD::MUL: {
540 unsigned Imm, Opc;
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)));
544 break;
545 }
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;
551 }
552 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
553 Select(N->getOperand(1)));
554 break;
555 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000556 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000557 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000558 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
559 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000560 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000561 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000562 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000563 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
564 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000565 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000566 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000567 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000568 // 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))) {
572 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000573 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000574 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
575 Val = Select(N->getOperand(0).getOperand(0));
576 } else {
577 Val = Select(N->getOperand(0));
578 isRunOfOnes(Imm, MB, ME);
579 SH = 0;
580 }
581 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
582 getI32Imm(MB), getI32Imm(ME));
583 break;
584 }
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),
588 N->getOperand(1),
589 PPC::ANDISo, PPC::ANDIo)) {
590 CurDAG->ReplaceAllUsesWith(N, I);
591 N = I;
592 break;
593 }
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)));
602 else
603 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
604 Select(N->getOperand(1)));
605 break;
606 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000607 case ISD::OR:
608 if (SDNode *I = SelectBitfieldInsert(N)) {
609 CurDAG->ReplaceAllUsesWith(N, I);
610 N = I;
611 break;
612 }
613 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
614 N->getOperand(1),
615 PPC::ORIS, PPC::ORI)) {
616 CurDAG->ReplaceAllUsesWith(N, I);
617 N = I;
618 break;
619 }
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)));
628 else
629 CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)),
630 Select(N->getOperand(1)));
631 break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000632 case ISD::XOR:
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.
636 if (isOprNot(N)) {
637 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +0000638 SDOperand Val = Select(N->getOperand(0));
639 switch (Val.getTargetOpcode()) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000640 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +0000641 case PPC::OR: Opc = PPC::NOR; break;
642 case PPC::AND: Opc = PPC::NAND; break;
643 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000644 }
645 if (Opc)
Nate Begeman131a8802005-08-18 05:44:50 +0000646 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
647 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +0000648 else
Nate Begeman131a8802005-08-18 05:44:50 +0000649 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +0000650 break;
651 }
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),
655 N->getOperand(1),
656 PPC::XORIS, PPC::XORI)) {
657 CurDAG->ReplaceAllUsesWith(N, I);
658 N = I;
659 break;
660 }
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)));
667 else
668 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
669 Select(N->getOperand(1)));
670 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000671 case ISD::SHL: {
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));
681 else
682 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)),
683 Select(N->getOperand(1)));
684 break;
685 }
686 case ISD::SRL: {
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));
696 else
697 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)),
698 Select(N->getOperand(1)));
699 break;
700 }
701 case ISD::SRA: {
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)),
710 getI32Imm(Imm));
711 else
712 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)),
713 Select(N->getOperand(1)));
714 break;
715 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000716 case ISD::FABS:
Nate Begeman6a7d6112005-08-18 00:53:47 +0000717 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
718 Select(N->getOperand(0)));
719 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000720 case ISD::FP_EXTEND:
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)));
724 break;
725 case ISD::FP_ROUND:
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)));
729 break;
Nate Begeman26653502005-08-17 23:46:35 +0000730 case ISD::FNEG: {
731 SDOperand Val = Select(N->getOperand(0));
732 MVT::ValueType Ty = N->getValueType(0);
733 if (Val.Val->hasOneUse()) {
734 unsigned Opc;
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;
742 }
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.
746 if (Opc) {
747 if (PPC::FNABS == Opc)
748 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
749 else
750 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
751 Val.getOperand(1), Val.getOperand(2));
752 break;
753 }
754 }
755 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
756 break;
757 }
Nate Begeman6a7d6112005-08-18 00:53:47 +0000758 case ISD::FSQRT: {
759 MVT::ValueType Ty = N->getValueType(0);
760 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
761 Select(N->getOperand(0)));
762 break;
763 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000764 case ISD::RET: {
765 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
766
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!");
771 case MVT::f64:
772 case MVT::f32:
773 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
774 break;
775 case MVT::i32:
776 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
777 break;
778 }
779
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);
786 }
787 }
788
789 // Finally, select this to a blr (return) instruction.
790 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
791 break;
792 }
793 }
794 return SDOperand(N, 0);
795}
796
797
798/// createPPC32ISelDag - This pass converts a legalized DAG into a
799/// PowerPC-specific DAG, ready for instruction scheduling.
800///
801FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
802 return new PPC32DAGToDAGISel(TM);
803}
804