blob: aaf2bba2ad4313affaf638f9cdc4dc5ad44003c5 [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 {
Chris Lattnera5a91b12005-08-17 19:33:03 +000031 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
32 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
33
34 //===--------------------------------------------------------------------===//
35 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
36 /// instructions for SelectionDAG operations.
37 ///
38 class PPC32DAGToDAGISel : public SelectionDAGISel {
39 PPC32TargetLowering PPC32Lowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000040 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000041 public:
42 PPC32DAGToDAGISel(TargetMachine &TM)
43 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
44
Chris Lattner4416f1a2005-08-19 22:38:53 +000045 virtual bool runOnFunction(Function &Fn) {
46 // Make sure we re-emit a set of the global base reg if necessary
47 GlobalBaseReg = 0;
48 return SelectionDAGISel::runOnFunction(Fn);
49 }
50
Chris Lattnera5a91b12005-08-17 19:33:03 +000051 /// getI32Imm - Return a target constant with the specified value, of type
52 /// i32.
53 inline SDOperand getI32Imm(unsigned Imm) {
54 return CurDAG->getTargetConstant(Imm, MVT::i32);
55 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000056
57 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
58 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000059 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000060
61 // Select - Convert the specified operand from a target-independent to a
62 // target-specific node if it hasn't already been changed.
63 SDOperand Select(SDOperand Op);
64
65 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
66 unsigned OCHi, unsigned OCLo,
67 bool IsArithmetic = false,
68 bool Negate = false);
Nate Begeman02b88a42005-08-19 00:38:14 +000069 SDNode *SelectBitfieldInsert(SDNode *N);
70
Chris Lattner2fbb4572005-08-21 18:50:37 +000071 /// SelectCC - Select a comparison of the specified values with the
72 /// specified condition code, returning the CR# of the expression.
73 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
74
Chris Lattner9944b762005-08-21 22:31:09 +000075 /// SelectAddr - Given the specified address, return the two operands for a
76 /// load/store instruction, and return true if it should be an indexed [r+r]
77 /// operation.
78 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
79
Chris Lattnera5a91b12005-08-17 19:33:03 +000080 /// InstructionSelectBasicBlock - This callback is invoked by
81 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
82 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
83 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000084 // Select target instructions for the DAG.
Chris Lattnera5a91b12005-08-17 19:33:03 +000085 Select(DAG.getRoot());
86 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000087
Chris Lattnerd607c122005-08-18 18:46:06 +000088 // Emit machine code to BB.
89 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000090 }
91
92 virtual const char *getPassName() const {
93 return "PowerPC DAG->DAG Pattern Instruction Selection";
94 }
95 };
96}
97
Chris Lattner4416f1a2005-08-19 22:38:53 +000098/// getGlobalBaseReg - Output the instructions required to put the
99/// base address to use for accessing globals into a register.
100///
Chris Lattner9944b762005-08-21 22:31:09 +0000101SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000102 if (!GlobalBaseReg) {
103 // Insert the set of GlobalBaseReg into the first MBB of the function
104 MachineBasicBlock &FirstMBB = BB->getParent()->front();
105 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
106 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
107 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
108 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
109 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
110 }
Chris Lattner9944b762005-08-21 22:31:09 +0000111 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000112}
113
114
Nate Begeman0f3257a2005-08-18 05:00:13 +0000115// isIntImmediate - This method tests to see if a constant operand.
116// If so Imm will receive the 32 bit value.
117static bool isIntImmediate(SDNode *N, unsigned& Imm) {
118 if (N->getOpcode() == ISD::Constant) {
119 Imm = cast<ConstantSDNode>(N)->getValue();
120 return true;
121 }
122 return false;
123}
124
Nate Begemancffc32b2005-08-18 07:30:46 +0000125// isOprShiftImm - Returns true if the specified operand is a shift opcode with
126// a immediate shift count less than 32.
127static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
128 Opc = N->getOpcode();
129 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
130 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
131}
132
133// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
134// any number of 0s on either side. The 1s are allowed to wrap from LSB to
135// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
136// not, since all 1s are not contiguous.
137static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
138 if (isShiftedMask_32(Val)) {
139 // look for the first non-zero bit
140 MB = CountLeadingZeros_32(Val);
141 // look for the first zero bit after the run of ones
142 ME = CountLeadingZeros_32((Val - 1) ^ Val);
143 return true;
144 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
145 // effectively look for the first zero bit
146 ME = CountLeadingZeros_32(Val) - 1;
147 // effectively look for the first one bit after the run of zeros
148 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
149 return true;
150 }
151 // no run present
152 return false;
153}
154
155// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
156// and mask opcode and mask operation.
157static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
158 unsigned &SH, unsigned &MB, unsigned &ME) {
159 unsigned Shift = 32;
160 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
161 unsigned Opcode = N->getOpcode();
162 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
163 return false;
164
165 if (Opcode == ISD::SHL) {
166 // apply shift left to mask if it comes first
167 if (IsShiftMask) Mask = Mask << Shift;
168 // determine which bits are made indeterminant by shift
169 Indeterminant = ~(0xFFFFFFFFu << Shift);
170 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
171 // apply shift right to mask if it comes first
172 if (IsShiftMask) Mask = Mask >> Shift;
173 // determine which bits are made indeterminant by shift
174 Indeterminant = ~(0xFFFFFFFFu >> Shift);
175 // adjust for the left rotate
176 Shift = 32 - Shift;
177 } else {
178 return false;
179 }
180
181 // if the mask doesn't intersect any Indeterminant bits
182 if (Mask && !(Mask & Indeterminant)) {
183 SH = Shift;
184 // make sure the mask is still a mask (wrap arounds may not be)
185 return isRunOfOnes(Mask, MB, ME);
186 }
187 return false;
188}
189
Nate Begeman0f3257a2005-08-18 05:00:13 +0000190// isOpcWithIntImmediate - This method tests to see if the node is a specific
191// opcode and that it has a immediate integer right operand.
192// If so Imm will receive the 32 bit value.
193static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
194 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
195}
196
197// isOprNot - Returns true if the specified operand is an xor with immediate -1.
198static bool isOprNot(SDNode *N) {
199 unsigned Imm;
200 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
201}
202
Chris Lattnera5a91b12005-08-17 19:33:03 +0000203// Immediate constant composers.
204// Lo16 - grabs the lo 16 bits from a 32 bit constant.
205// Hi16 - grabs the hi 16 bits from a 32 bit constant.
206// HA16 - computes the hi bits required if the lo bits are add/subtracted in
207// arithmethically.
208static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
209static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
210static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
211
212// isIntImmediate - This method tests to see if a constant operand.
213// If so Imm will receive the 32 bit value.
214static bool isIntImmediate(SDOperand N, unsigned& Imm) {
215 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
216 Imm = (unsigned)CN->getSignExtended();
217 return true;
218 }
219 return false;
220}
221
Nate Begeman02b88a42005-08-19 00:38:14 +0000222/// SelectBitfieldInsert - turn an or of two masked values into
223/// the rotate left word immediate then mask insert (rlwimi) instruction.
224/// Returns true on success, false if the caller still needs to select OR.
225///
226/// Patterns matched:
227/// 1. or shl, and 5. or and, and
228/// 2. or and, shl 6. or shl, shr
229/// 3. or shr, and 7. or shr, shl
230/// 4. or and, shr
231SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
232 bool IsRotate = false;
233 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
234 unsigned Value;
235
236 SDOperand Op0 = N->getOperand(0);
237 SDOperand Op1 = N->getOperand(1);
238
239 unsigned Op0Opc = Op0.getOpcode();
240 unsigned Op1Opc = Op1.getOpcode();
241
242 // Verify that we have the correct opcodes
243 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
244 return false;
245 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
246 return false;
247
248 // Generate Mask value for Target
249 if (isIntImmediate(Op0.getOperand(1), Value)) {
250 switch(Op0Opc) {
251 case ISD::SHL: TgtMask <<= Value; break;
252 case ISD::SRL: TgtMask >>= Value; break;
253 case ISD::AND: TgtMask &= Value; break;
254 }
255 } else {
256 return 0;
257 }
258
259 // Generate Mask value for Insert
260 if (isIntImmediate(Op1.getOperand(1), Value)) {
261 switch(Op1Opc) {
262 case ISD::SHL:
263 SH = Value;
264 InsMask <<= SH;
265 if (Op0Opc == ISD::SRL) IsRotate = true;
266 break;
267 case ISD::SRL:
268 SH = Value;
269 InsMask >>= SH;
270 SH = 32-SH;
271 if (Op0Opc == ISD::SHL) IsRotate = true;
272 break;
273 case ISD::AND:
274 InsMask &= Value;
275 break;
276 }
277 } else {
278 return 0;
279 }
280
281 // If both of the inputs are ANDs and one of them has a logical shift by
282 // constant as its input, make that AND the inserted value so that we can
283 // combine the shift into the rotate part of the rlwimi instruction
284 bool IsAndWithShiftOp = false;
285 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
286 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
287 Op1.getOperand(0).getOpcode() == ISD::SRL) {
288 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
289 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
290 IsAndWithShiftOp = true;
291 }
292 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
293 Op0.getOperand(0).getOpcode() == ISD::SRL) {
294 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
295 std::swap(Op0, Op1);
296 std::swap(TgtMask, InsMask);
297 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
298 IsAndWithShiftOp = true;
299 }
300 }
301 }
302
303 // Verify that the Target mask and Insert mask together form a full word mask
304 // and that the Insert mask is a run of set bits (which implies both are runs
305 // of set bits). Given that, Select the arguments and generate the rlwimi
306 // instruction.
307 unsigned MB, ME;
308 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
309 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
310 bool Op0IsAND = Op0Opc == ISD::AND;
311 // Check for rotlwi / rotrwi here, a special case of bitfield insert
312 // where both bitfield halves are sourced from the same value.
313 if (IsRotate && fullMask &&
314 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
315 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
316 Select(N->getOperand(0).getOperand(0)),
317 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
318 return Op0.Val;
319 }
320 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
321 : Select(Op0);
322 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
323 : Select(Op1.getOperand(0));
324 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
325 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
326 return Op0.Val;
327 }
328 return 0;
329}
330
Chris Lattnera5a91b12005-08-17 19:33:03 +0000331// SelectIntImmediateExpr - Choose code for integer operations with an immediate
332// operand.
333SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
334 unsigned OCHi, unsigned OCLo,
335 bool IsArithmetic,
336 bool Negate) {
337 // Check to make sure this is a constant.
338 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
339 // Exit if not a constant.
340 if (!CN) return 0;
341 // Extract immediate.
342 unsigned C = (unsigned)CN->getValue();
343 // Negate if required (ISD::SUB).
344 if (Negate) C = -C;
345 // Get the hi and lo portions of constant.
346 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
347 unsigned Lo = Lo16(C);
348
349 // If two instructions are needed and usage indicates it would be better to
350 // load immediate into a register, bail out.
351 if (Hi && Lo && CN->use_size() > 2) return false;
352
353 // Select the first operand.
354 SDOperand Opr0 = Select(LHS);
355
356 if (Lo) // Add in the lo-part.
357 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
358 if (Hi) // Add in the hi-part.
359 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
360 return Opr0.Val;
361}
362
Chris Lattner9944b762005-08-21 22:31:09 +0000363/// SelectAddr - Given the specified address, return the two operands for a
364/// load/store instruction, and return true if it should be an indexed [r+r]
365/// operation.
366bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
367 SDOperand &Op2) {
368 unsigned imm = 0;
369 if (Addr.getOpcode() == ISD::ADD) {
370 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
371 Op1 = getI32Imm(Lo16(imm));
372 if (isa<FrameIndexSDNode>(Addr.getOperand(0))) {
373 ++FrameOff;
374 Op2 = Addr.getOperand(0);
375 } else {
376 Op2 = Select(Addr.getOperand(0));
377 }
378 return false;
379 } else {
380 Op1 = Select(Addr.getOperand(0));
381 Op2 = Select(Addr.getOperand(1));
382 return true; // [r+r]
383 }
384 }
385
386 // Now check if we're dealing with a global, and whether or not we should emit
387 // an optimized load or store for statics.
388 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
389 GlobalValue *GV = GN->getGlobal();
390 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
391 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
392 if (PICEnabled)
393 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
394 Op1);
395 else
396 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
397 return false;
398 }
399 } else if (isa<FrameIndexSDNode>(Addr)) {
400 Op1 = getI32Imm(0);
401 Op2 = Addr;
402 return false;
403 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
404 Op1 = Addr;
405 if (PICEnabled)
406 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
407 else
408 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
409 return false;
410 }
411 Op1 = getI32Imm(0);
412 Op2 = Select(Addr);
413 return false;
414}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000415
Chris Lattner2fbb4572005-08-21 18:50:37 +0000416/// SelectCC - Select a comparison of the specified values with the specified
417/// condition code, returning the CR# of the expression.
418SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
419 ISD::CondCode CC) {
420 // Always select the LHS.
421 LHS = Select(LHS);
422
423 // Use U to determine whether the SETCC immediate range is signed or not.
424 if (MVT::isInteger(LHS.getValueType())) {
425 bool U = ISD::isUnsignedIntSetCC(CC);
426 unsigned Imm;
427 if (isIntImmediate(RHS, Imm) &&
428 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
429 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
430 LHS, getI32Imm(Lo16(Imm)));
431 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
432 LHS, Select(RHS));
433 } else {
434 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS));
435 }
436}
437
438/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
439/// to Condition.
440static unsigned getBCCForSetCC(ISD::CondCode CC) {
441 switch (CC) {
442 default: assert(0 && "Unknown condition!"); abort();
443 case ISD::SETEQ: return PPC::BEQ;
444 case ISD::SETNE: return PPC::BNE;
445 case ISD::SETULT:
446 case ISD::SETLT: return PPC::BLT;
447 case ISD::SETULE:
448 case ISD::SETLE: return PPC::BLE;
449 case ISD::SETUGT:
450 case ISD::SETGT: return PPC::BGT;
451 case ISD::SETUGE:
452 case ISD::SETGE: return PPC::BGE;
453 }
454 return 0;
455}
456
Chris Lattner9944b762005-08-21 22:31:09 +0000457
Chris Lattnera5a91b12005-08-17 19:33:03 +0000458// Select - Convert the specified operand from a target-independent to a
459// target-specific node if it hasn't already been changed.
460SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
461 SDNode *N = Op.Val;
462 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
463 return Op; // Already selected.
464
465 switch (N->getOpcode()) {
466 default:
467 std::cerr << "Cannot yet select: ";
468 N->dump();
469 std::cerr << "\n";
470 abort();
471 case ISD::EntryToken: // These leaves remain the same.
Chris Lattnera5a91b12005-08-17 19:33:03 +0000472 return Op;
473 case ISD::TokenFactor: {
474 SDOperand New;
475 if (N->getNumOperands() == 2) {
476 SDOperand Op0 = Select(N->getOperand(0));
477 SDOperand Op1 = Select(N->getOperand(1));
478 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
479 } else {
480 std::vector<SDOperand> Ops;
481 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000482 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000483 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
484 }
485
486 if (New.Val != N) {
487 CurDAG->ReplaceAllUsesWith(N, New.Val);
488 N = New.Val;
489 }
490 break;
491 }
492 case ISD::CopyFromReg: {
493 SDOperand Chain = Select(N->getOperand(0));
494 if (Chain == N->getOperand(0)) return Op; // No change
495 SDOperand New = CurDAG->getCopyFromReg(Chain,
496 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
497 return New.getValue(Op.ResNo);
498 }
499 case ISD::CopyToReg: {
500 SDOperand Chain = Select(N->getOperand(0));
501 SDOperand Reg = N->getOperand(1);
502 SDOperand Val = Select(N->getOperand(2));
503 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
504 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
505 Chain, Reg, Val);
506 CurDAG->ReplaceAllUsesWith(N, New.Val);
507 N = New.Val;
508 }
509 break;
510 }
511 case ISD::Constant: {
512 assert(N->getValueType(0) == MVT::i32);
513 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000514 unsigned Hi = HA16(v);
515 unsigned Lo = Lo16(v);
516 if (Hi && Lo) {
517 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
518 getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000519 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000520 } else if (Lo) {
521 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
522 } else {
523 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LIS, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000524 }
Nate Begemana6940472005-08-18 18:01:39 +0000525 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000526 }
Chris Lattner2b544002005-08-24 23:08:16 +0000527 case ISD::UNDEF:
528 if (N->getValueType(0) == MVT::i32)
529 CurDAG->SelectNodeTo(N, MVT::i32, PPC::IMPLICIT_DEF_GPR);
530 else
531 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::IMPLICIT_DEF_FP);
532 break;
Chris Lattner4416f1a2005-08-19 22:38:53 +0000533 case ISD::GlobalAddress: {
534 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
535 SDOperand Tmp;
536 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000537 if (PICEnabled)
538 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
539 else
Chris Lattner4416f1a2005-08-19 22:38:53 +0000540 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +0000541
Chris Lattner4416f1a2005-08-19 22:38:53 +0000542 if (GV->hasWeakLinkage() || GV->isExternal())
543 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LWZ, GA, Tmp);
544 else
545 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LA, Tmp, GA);
546 break;
547 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000548 case ISD::SIGN_EXTEND_INREG:
549 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
550 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
551 case MVT::i16:
552 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
553 break;
554 case MVT::i8:
555 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
556 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000557 }
558 break;
559 case ISD::CTLZ:
560 assert(N->getValueType(0) == MVT::i32);
561 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
562 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000563 case ISD::ADD: {
564 MVT::ValueType Ty = N->getValueType(0);
565 if (Ty == MVT::i32) {
566 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
567 PPC::ADDIS, PPC::ADDI, true)) {
568 CurDAG->ReplaceAllUsesWith(N, I);
569 N = I;
570 } else {
571 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
572 Select(N->getOperand(1)));
573 }
574 break;
575 }
576
577 if (!NoExcessFPPrecision) { // Match FMA ops
578 if (N->getOperand(0).getOpcode() == ISD::MUL &&
579 N->getOperand(0).Val->hasOneUse()) {
580 ++FusedFP; // Statistic
581 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
582 Select(N->getOperand(0).getOperand(0)),
583 Select(N->getOperand(0).getOperand(1)),
584 Select(N->getOperand(1)));
585 break;
586 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
587 N->getOperand(1).hasOneUse()) {
588 ++FusedFP; // Statistic
589 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
590 Select(N->getOperand(1).getOperand(0)),
591 Select(N->getOperand(1).getOperand(1)),
592 Select(N->getOperand(0)));
593 break;
594 }
595 }
596
597 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
598 Select(N->getOperand(0)), Select(N->getOperand(1)));
599 break;
600 }
601 case ISD::SUB: {
602 MVT::ValueType Ty = N->getValueType(0);
603 if (Ty == MVT::i32) {
604 unsigned Imm;
605 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
Nate Begemanc6b07172005-08-24 05:03:20 +0000606 if (0 == Imm)
607 CurDAG->SelectNodeTo(N, Ty, PPC::NEG, Select(N->getOperand(1)));
608 else
609 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
610 getI32Imm(Lo16(Imm)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000611 break;
612 }
613 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
614 PPC::ADDIS, PPC::ADDI, true, true)) {
615 CurDAG->ReplaceAllUsesWith(N, I);
616 N = I;
617 } else {
618 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
619 Select(N->getOperand(0)));
620 }
621 break;
622 }
623
624 if (!NoExcessFPPrecision) { // Match FMA ops
625 if (N->getOperand(0).getOpcode() == ISD::MUL &&
626 N->getOperand(0).Val->hasOneUse()) {
627 ++FusedFP; // Statistic
628 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
629 Select(N->getOperand(0).getOperand(0)),
630 Select(N->getOperand(0).getOperand(1)),
631 Select(N->getOperand(1)));
632 break;
633 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
634 N->getOperand(1).Val->hasOneUse()) {
635 ++FusedFP; // Statistic
636 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
637 Select(N->getOperand(1).getOperand(0)),
638 Select(N->getOperand(1).getOperand(1)),
639 Select(N->getOperand(0)));
640 break;
641 }
642 }
643 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
644 Select(N->getOperand(0)),
645 Select(N->getOperand(1)));
646 break;
Nate Begeman26653502005-08-17 23:46:35 +0000647 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000648 case ISD::MUL: {
649 unsigned Imm, Opc;
650 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
651 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
652 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
653 break;
654 }
655 switch (N->getValueType(0)) {
656 default: assert(0 && "Unhandled multiply type!");
657 case MVT::i32: Opc = PPC::MULLW; break;
658 case MVT::f32: Opc = PPC::FMULS; break;
659 case MVT::f64: Opc = PPC::FMUL; break;
660 }
661 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
662 Select(N->getOperand(1)));
663 break;
664 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000665 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000666 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000667 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
668 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000669 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000670 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000671 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000672 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
673 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000674 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000675 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000676 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000677 // If this is an and of a value rotated between 0 and 31 bits and then and'd
678 // with a mask, emit rlwinm
679 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
680 isShiftedMask_32(~Imm))) {
681 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000682 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000683 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
684 Val = Select(N->getOperand(0).getOperand(0));
685 } else {
686 Val = Select(N->getOperand(0));
687 isRunOfOnes(Imm, MB, ME);
688 SH = 0;
689 }
690 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Val, getI32Imm(SH),
691 getI32Imm(MB), getI32Imm(ME));
692 break;
693 }
694 // If this is an and with an immediate that isn't a mask, then codegen it as
695 // high and low 16 bit immediate ands.
696 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
697 N->getOperand(1),
698 PPC::ANDISo, PPC::ANDIo)) {
699 CurDAG->ReplaceAllUsesWith(N, I);
700 N = I;
701 break;
702 }
703 // Finally, check for the case where we are being asked to select
704 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
705 if (isOprNot(N->getOperand(0).Val))
706 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(1)),
707 Select(N->getOperand(0).getOperand(0)));
708 else if (isOprNot(N->getOperand(1).Val))
709 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ANDC, Select(N->getOperand(0)),
710 Select(N->getOperand(1).getOperand(0)));
711 else
712 CurDAG->SelectNodeTo(N, MVT::i32, PPC::AND, Select(N->getOperand(0)),
713 Select(N->getOperand(1)));
714 break;
715 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000716 case ISD::OR:
717 if (SDNode *I = SelectBitfieldInsert(N)) {
718 CurDAG->ReplaceAllUsesWith(N, I);
719 N = I;
720 break;
721 }
722 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
723 N->getOperand(1),
724 PPC::ORIS, PPC::ORI)) {
725 CurDAG->ReplaceAllUsesWith(N, I);
726 N = I;
727 break;
728 }
729 // Finally, check for the case where we are being asked to select
730 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
731 if (isOprNot(N->getOperand(0).Val))
732 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(1)),
733 Select(N->getOperand(0).getOperand(0)));
734 else if (isOprNot(N->getOperand(1).Val))
735 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORC, Select(N->getOperand(0)),
736 Select(N->getOperand(1).getOperand(0)));
737 else
738 CurDAG->SelectNodeTo(N, MVT::i32, PPC::OR, Select(N->getOperand(0)),
739 Select(N->getOperand(1)));
740 break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000741 case ISD::XOR:
742 // Check whether or not this node is a logical 'not'. This is represented
743 // by llvm as a xor with the constant value -1 (all bits set). If this is a
744 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
745 if (isOprNot(N)) {
746 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +0000747 SDOperand Val = Select(N->getOperand(0));
748 switch (Val.getTargetOpcode()) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000749 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +0000750 case PPC::OR: Opc = PPC::NOR; break;
751 case PPC::AND: Opc = PPC::NAND; break;
752 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +0000753 }
754 if (Opc)
Nate Begeman131a8802005-08-18 05:44:50 +0000755 CurDAG->SelectNodeTo(N, MVT::i32, Opc, Val.getOperand(0),
756 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +0000757 else
Nate Begeman131a8802005-08-18 05:44:50 +0000758 CurDAG->SelectNodeTo(N, MVT::i32, PPC::NOR, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +0000759 break;
760 }
761 // If this is a xor with an immediate other than -1, then codegen it as high
762 // and low 16 bit immediate xors.
763 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
764 N->getOperand(1),
765 PPC::XORIS, PPC::XORI)) {
766 CurDAG->ReplaceAllUsesWith(N, I);
767 N = I;
768 break;
769 }
770 // Finally, check for the case where we are being asked to select
771 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
772 if (isOprNot(N->getOperand(0).Val))
773 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EQV,
774 Select(N->getOperand(0).getOperand(0)),
775 Select(N->getOperand(1)));
776 else
777 CurDAG->SelectNodeTo(N, MVT::i32, PPC::XOR, Select(N->getOperand(0)),
778 Select(N->getOperand(1)));
779 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000780 case ISD::SHL: {
781 unsigned Imm, SH, MB, ME;
782 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
783 isRotateAndMask(N, Imm, true, SH, MB, ME))
784 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
785 Select(N->getOperand(0).getOperand(0)),
786 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
787 else if (isIntImmediate(N->getOperand(1), Imm))
788 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
789 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
790 else
791 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SLW, Select(N->getOperand(0)),
792 Select(N->getOperand(1)));
793 break;
794 }
795 case ISD::SRL: {
796 unsigned Imm, SH, MB, ME;
797 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
798 isRotateAndMask(N, Imm, true, SH, MB, ME))
799 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
800 Select(N->getOperand(0).getOperand(0)),
801 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
802 else if (isIntImmediate(N->getOperand(1), Imm))
803 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM, Select(N->getOperand(0)),
804 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
805 else
806 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRW, Select(N->getOperand(0)),
807 Select(N->getOperand(1)));
808 break;
809 }
810 case ISD::SRA: {
811 unsigned Imm, SH, MB, ME;
812 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
813 isRotateAndMask(N, Imm, true, SH, MB, ME))
814 CurDAG->SelectNodeTo(N, MVT::i32, PPC::RLWINM,
815 Select(N->getOperand(0).getOperand(0)),
816 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
817 else if (isIntImmediate(N->getOperand(1), Imm))
818 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAWI, Select(N->getOperand(0)),
819 getI32Imm(Imm));
820 else
821 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SRAW, Select(N->getOperand(0)),
822 Select(N->getOperand(1)));
823 break;
824 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000825 case ISD::FABS:
Nate Begeman6a7d6112005-08-18 00:53:47 +0000826 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
827 Select(N->getOperand(0)));
828 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000829 case ISD::FP_EXTEND:
830 assert(MVT::f64 == N->getValueType(0) &&
831 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
832 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
833 break;
834 case ISD::FP_ROUND:
835 assert(MVT::f32 == N->getValueType(0) &&
836 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
837 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
838 break;
Nate Begeman26653502005-08-17 23:46:35 +0000839 case ISD::FNEG: {
840 SDOperand Val = Select(N->getOperand(0));
841 MVT::ValueType Ty = N->getValueType(0);
842 if (Val.Val->hasOneUse()) {
843 unsigned Opc;
844 switch (Val.getTargetOpcode()) {
845 default: Opc = 0; break;
846 case PPC::FABS: Opc = PPC::FNABS; break;
847 case PPC::FMADD: Opc = PPC::FNMADD; break;
848 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
849 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
850 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
851 }
852 // If we inverted the opcode, then emit the new instruction with the
853 // inverted opcode and the original instruction's operands. Otherwise,
854 // fall through and generate a fneg instruction.
855 if (Opc) {
856 if (PPC::FNABS == Opc)
857 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
858 else
859 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
860 Val.getOperand(1), Val.getOperand(2));
861 break;
862 }
863 }
864 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
865 break;
866 }
Nate Begeman6a7d6112005-08-18 00:53:47 +0000867 case ISD::FSQRT: {
868 MVT::ValueType Ty = N->getValueType(0);
869 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
870 Select(N->getOperand(0)));
871 break;
872 }
Chris Lattner9944b762005-08-21 22:31:09 +0000873 case ISD::LOAD:
874 case ISD::EXTLOAD:
875 case ISD::ZEXTLOAD:
876 case ISD::SEXTLOAD: {
877 SDOperand Op1, Op2;
878 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
879
880 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
881 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
882 unsigned Opc;
883 switch (TypeBeingLoaded) {
884 default: N->dump(); assert(0 && "Cannot load this type!");
885 case MVT::i1:
886 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
887 case MVT::i16:
888 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
889 Opc = isIdx ? PPC::LHAX : PPC::LHA;
890 } else {
891 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
892 }
893 break;
894 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
895 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
896 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
897 }
898
899 CurDAG->SelectNodeTo(N, N->getValueType(0), MVT::Other, Opc,
900 Op1, Op2, Select(N->getOperand(0)));
901 break;
902 }
903
Chris Lattnerf7f22552005-08-22 01:27:59 +0000904 case ISD::TRUNCSTORE:
905 case ISD::STORE: {
906 SDOperand AddrOp1, AddrOp2;
907 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
908
909 unsigned Opc;
910 if (N->getOpcode() == ISD::STORE) {
911 switch (N->getOperand(1).getValueType()) {
912 default: assert(0 && "unknown Type in store");
913 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
914 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
915 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
916 }
917 } else { //ISD::TRUNCSTORE
918 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
919 default: assert(0 && "unknown Type in store");
920 case MVT::i1:
921 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
922 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
923 }
924 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +0000925
Chris Lattnerf7f22552005-08-22 01:27:59 +0000926 CurDAG->SelectNodeTo(N, MVT::Other, Opc, Select(N->getOperand(1)),
927 AddrOp1, AddrOp2, Select(N->getOperand(0)));
928 break;
929 }
Chris Lattnera2590c52005-08-24 00:47:15 +0000930
931 case ISD::CALLSEQ_START:
932 case ISD::CALLSEQ_END: {
933 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
934 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
935 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattnerfb0c9642005-08-24 22:45:17 +0000936 CurDAG->SelectNodeTo(N, MVT::Other, Opc,
937 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattnera2590c52005-08-24 00:47:15 +0000938 break;
939 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +0000940 case ISD::CALL:
941 case ISD::TAILCALL: {
942 SDOperand Chain = Select(N->getOperand(0));
943
944 unsigned CallOpcode;
945 std::vector<SDOperand> CallOperands;
946
947 if (GlobalAddressSDNode *GASD =
948 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
949 CallOpcode = PPC::CALLpcrel;
950 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
951 MVT::i32));
952 } else if (ExternalSymbolSDNode *ESSDN =
953 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
954 CallOpcode = PPC::CALLpcrel;
955 CallOperands.push_back(N->getOperand(1));
956 } else {
957 // Copy the callee address into the CTR register.
958 SDOperand Callee = Select(N->getOperand(1));
959 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
960
961 // Copy the callee address into R12 on darwin.
962 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
963 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, R12, Callee, Chain);
964
965 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
966 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
967 CallOperands.push_back(R12);
968 CallOpcode = PPC::CALLindirect;
969 }
970
971 unsigned GPR_idx = 0, FPR_idx = 0;
972 static const unsigned GPR[] = {
973 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
974 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
975 };
976 static const unsigned FPR[] = {
977 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
978 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
979 };
980
981 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i)
982 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
983 unsigned DestReg = 0;
984 MVT::ValueType RegTy;
985 if (N->getOperand(i).getValueType() == MVT::i32) {
986 assert(GPR_idx < 8 && "Too many int args");
987 DestReg = GPR[GPR_idx++];
988 RegTy = MVT::i32;
989 } else {
Chris Lattnered7956b2005-08-25 00:19:12 +0000990 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
Chris Lattnerfb0c9642005-08-24 22:45:17 +0000991 "Unpromoted integer arg?");
992 assert(FPR_idx < 13 && "Too many fp args");
993 DestReg = FPR[FPR_idx++];
994 RegTy = MVT::f64; // Even if this is really f32!
995 }
996
997 SDOperand Reg = CurDAG->getRegister(DestReg, RegTy);
998 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg,
999 Select(N->getOperand(i)));
1000 CallOperands.push_back(Reg);
1001 }
1002
1003 // Finally, once everything is in registers to pass to the call, emit the
1004 // call itself.
1005 CallOperands.push_back(Chain);
1006 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, CallOperands);
1007
1008 std::vector<SDOperand> CallResults;
1009
1010 // If the call has results, copy the values out of the ret val registers.
1011 switch (N->getValueType(0)) {
1012 default: assert(0 && "Unexpected ret value!");
1013 case MVT::Other: break;
1014 case MVT::i32:
1015 if (N->getValueType(1) == MVT::i32) {
1016 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32).getValue(1);
1017 CallResults.push_back(Chain.getValue(0));
1018 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1019 CallResults.push_back(Chain.getValue(0));
1020 } else {
1021 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1022 CallResults.push_back(Chain.getValue(0));
1023 }
1024 break;
1025 case MVT::f32:
1026 case MVT::f64:
1027 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, MVT::f64).getValue(1);
1028 CallResults.push_back(Chain.getValue(0));
1029 break;
1030 }
1031
1032 CallResults.push_back(Chain);
1033 CurDAG->ReplaceAllUsesWith(N, CallResults);
1034 return CallResults[Op.ResNo];
1035 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001036 case ISD::RET: {
1037 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1038
1039 if (N->getNumOperands() > 1) {
1040 SDOperand Val = Select(N->getOperand(1));
1041 switch (N->getOperand(1).getValueType()) {
1042 default: assert(0 && "Unknown return type!");
1043 case MVT::f64:
1044 case MVT::f32:
1045 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
1046 break;
1047 case MVT::i32:
1048 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
1049 break;
1050 }
1051
1052 if (N->getNumOperands() > 2) {
1053 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1054 N->getOperand(2).getValueType() == MVT::i32 &&
1055 N->getNumOperands() == 2 && "Unknown two-register ret value!");
1056 Val = Select(N->getOperand(2));
1057 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
1058 }
1059 }
1060
1061 // Finally, select this to a blr (return) instruction.
1062 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
1063 break;
1064 }
Chris Lattner89532c72005-08-25 00:29:58 +00001065 case ISD::BR:
1066 CurDAG->SelectNodeTo(N, MVT::Other, PPC::B, N->getOperand(1),
1067 Select(N->getOperand(0)));
1068 break;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001069 case ISD::BR_CC:
1070 case ISD::BRTWOWAY_CC: {
1071 SDOperand Chain = Select(N->getOperand(0));
1072 MachineBasicBlock *Dest =
1073 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1074 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1075 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1076 unsigned Opc = getBCCForSetCC(CC);
1077
1078 // If this is a two way branch, then grab the fallthrough basic block
1079 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1080 // conversion if necessary by the branch selection pass. Otherwise, emit a
1081 // standard conditional branch.
1082 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
1083 MachineBasicBlock *Fallthrough =
1084 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
1085 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1086 CondCode, getI32Imm(Opc),
1087 N->getOperand(4), N->getOperand(5),
1088 Chain);
1089 CurDAG->SelectNodeTo(N, MVT::Other, PPC::B, N->getOperand(5), CB);
1090 } else {
1091 // Iterate to the next basic block
1092 ilist<MachineBasicBlock>::iterator It = BB;
1093 ++It;
1094
1095 // If the fallthrough path is off the end of the function, which would be
1096 // undefined behavior, set it to be the same as the current block because
1097 // we have nothing better to set it to, and leaving it alone will cause
1098 // the PowerPC Branch Selection pass to crash.
1099 if (It == BB->getParent()->end()) It = Dest;
1100 CurDAG->SelectNodeTo(N, MVT::Other, PPC::COND_BRANCH, CondCode,
1101 getI32Imm(Opc), N->getOperand(4),
1102 CurDAG->getBasicBlock(It), Chain);
1103 }
1104 break;
1105 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001106 }
Chris Lattnerddf3e7d2005-08-22 00:59:14 +00001107 return SDOperand(N, Op.ResNo);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001108}
1109
1110
1111/// createPPC32ISelDag - This pass converts a legalized DAG into a
1112/// PowerPC-specific DAG, ready for instruction scheduling.
1113///
1114FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1115 return new PPC32DAGToDAGISel(TM);
1116}
1117