blob: dd3fe514df25444a66abf742924cb0f6c42e2546 [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 Lattner2fe76e52005-08-25 04:47:18 +000025#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000026#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
29using namespace llvm;
30
31namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000032 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.
Chris Lattner9944b762005-08-21 22:31:09 +000060 SDOperand 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 Lattner2fbb4572005-08-21 18:50:37 +000072 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
75
Chris Lattner9944b762005-08-21 22:31:09 +000076 /// SelectAddr - Given the specified address, return the two operands for a
77 /// load/store instruction, and return true if it should be an indexed [r+r]
78 /// operation.
79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
80
Chris Lattner047b9522005-08-25 22:04:30 +000081 SDOperand BuildSDIVSequence(SDNode *N);
82 SDOperand BuildUDIVSequence(SDNode *N);
83
Chris Lattnera5a91b12005-08-17 19:33:03 +000084 /// InstructionSelectBasicBlock - This callback is invoked by
85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
86 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
87 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000088 // Select target instructions for the DAG.
Chris Lattnerefa6abc2005-08-29 01:07:02 +000089 DAG.setRoot(Select(DAG.getRoot()));
Chris Lattnera5a91b12005-08-17 19:33:03 +000090 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000091
Chris Lattnerd607c122005-08-18 18:46:06 +000092 // Emit machine code to BB.
93 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000094 }
95
96 virtual const char *getPassName() const {
97 return "PowerPC DAG->DAG Pattern Instruction Selection";
98 }
99 };
100}
101
Chris Lattner6cd40d52005-09-03 01:17:22 +0000102#include "PPC32GenDAGISel.inc"
103
Chris Lattner4416f1a2005-08-19 22:38:53 +0000104/// getGlobalBaseReg - Output the instructions required to put the
105/// base address to use for accessing globals into a register.
106///
Chris Lattner9944b762005-08-21 22:31:09 +0000107SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000108 if (!GlobalBaseReg) {
109 // Insert the set of GlobalBaseReg into the first MBB of the function
110 MachineBasicBlock &FirstMBB = BB->getParent()->front();
111 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
112 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
113 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
114 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
115 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
116 }
Chris Lattner9944b762005-08-21 22:31:09 +0000117 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000118}
119
120
Nate Begeman0f3257a2005-08-18 05:00:13 +0000121// isIntImmediate - This method tests to see if a constant operand.
122// If so Imm will receive the 32 bit value.
123static bool isIntImmediate(SDNode *N, unsigned& Imm) {
124 if (N->getOpcode() == ISD::Constant) {
125 Imm = cast<ConstantSDNode>(N)->getValue();
126 return true;
127 }
128 return false;
129}
130
Nate Begemancffc32b2005-08-18 07:30:46 +0000131// isOprShiftImm - Returns true if the specified operand is a shift opcode with
132// a immediate shift count less than 32.
133static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
134 Opc = N->getOpcode();
135 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
136 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
137}
138
139// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
140// any number of 0s on either side. The 1s are allowed to wrap from LSB to
141// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
142// not, since all 1s are not contiguous.
143static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
144 if (isShiftedMask_32(Val)) {
145 // look for the first non-zero bit
146 MB = CountLeadingZeros_32(Val);
147 // look for the first zero bit after the run of ones
148 ME = CountLeadingZeros_32((Val - 1) ^ Val);
149 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000150 } else {
151 Val = ~Val; // invert mask
152 if (isShiftedMask_32(Val)) {
153 // effectively look for the first zero bit
154 ME = CountLeadingZeros_32(Val) - 1;
155 // effectively look for the first one bit after the run of zeros
156 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
157 return true;
158 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000159 }
160 // no run present
161 return false;
162}
163
164// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
165// and mask opcode and mask operation.
166static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
167 unsigned &SH, unsigned &MB, unsigned &ME) {
168 unsigned Shift = 32;
169 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
170 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000171 if (N->getNumOperands() != 2 ||
172 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000173 return false;
174
175 if (Opcode == ISD::SHL) {
176 // apply shift left to mask if it comes first
177 if (IsShiftMask) Mask = Mask << Shift;
178 // determine which bits are made indeterminant by shift
179 Indeterminant = ~(0xFFFFFFFFu << Shift);
180 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
181 // apply shift right to mask if it comes first
182 if (IsShiftMask) Mask = Mask >> Shift;
183 // determine which bits are made indeterminant by shift
184 Indeterminant = ~(0xFFFFFFFFu >> Shift);
185 // adjust for the left rotate
186 Shift = 32 - Shift;
187 } else {
188 return false;
189 }
190
191 // if the mask doesn't intersect any Indeterminant bits
192 if (Mask && !(Mask & Indeterminant)) {
193 SH = Shift;
194 // make sure the mask is still a mask (wrap arounds may not be)
195 return isRunOfOnes(Mask, MB, ME);
196 }
197 return false;
198}
199
Nate Begeman0f3257a2005-08-18 05:00:13 +0000200// isOpcWithIntImmediate - This method tests to see if the node is a specific
201// opcode and that it has a immediate integer right operand.
202// If so Imm will receive the 32 bit value.
203static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
204 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
205}
206
207// isOprNot - Returns true if the specified operand is an xor with immediate -1.
208static bool isOprNot(SDNode *N) {
209 unsigned Imm;
210 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
211}
212
Chris Lattnera5a91b12005-08-17 19:33:03 +0000213// Immediate constant composers.
214// Lo16 - grabs the lo 16 bits from a 32 bit constant.
215// Hi16 - grabs the hi 16 bits from a 32 bit constant.
216// HA16 - computes the hi bits required if the lo bits are add/subtracted in
217// arithmethically.
218static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
219static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
220static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
221
222// isIntImmediate - This method tests to see if a constant operand.
223// If so Imm will receive the 32 bit value.
224static bool isIntImmediate(SDOperand N, unsigned& Imm) {
225 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
226 Imm = (unsigned)CN->getSignExtended();
227 return true;
228 }
229 return false;
230}
231
Nate Begeman02b88a42005-08-19 00:38:14 +0000232/// SelectBitfieldInsert - turn an or of two masked values into
233/// the rotate left word immediate then mask insert (rlwimi) instruction.
234/// Returns true on success, false if the caller still needs to select OR.
235///
236/// Patterns matched:
237/// 1. or shl, and 5. or and, and
238/// 2. or and, shl 6. or shl, shr
239/// 3. or shr, and 7. or shr, shl
240/// 4. or and, shr
241SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
242 bool IsRotate = false;
243 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
244 unsigned Value;
245
246 SDOperand Op0 = N->getOperand(0);
247 SDOperand Op1 = N->getOperand(1);
248
249 unsigned Op0Opc = Op0.getOpcode();
250 unsigned Op1Opc = Op1.getOpcode();
251
252 // Verify that we have the correct opcodes
253 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
254 return false;
255 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
256 return false;
257
258 // Generate Mask value for Target
259 if (isIntImmediate(Op0.getOperand(1), Value)) {
260 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000261 case ISD::SHL: TgtMask <<= Value; break;
262 case ISD::SRL: TgtMask >>= Value; break;
263 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000264 }
265 } else {
266 return 0;
267 }
268
269 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000270 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000271 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000272
273 switch(Op1Opc) {
274 case ISD::SHL:
275 SH = Value;
276 InsMask <<= SH;
277 if (Op0Opc == ISD::SRL) IsRotate = true;
278 break;
279 case ISD::SRL:
280 SH = Value;
281 InsMask >>= SH;
282 SH = 32-SH;
283 if (Op0Opc == ISD::SHL) IsRotate = true;
284 break;
285 case ISD::AND:
286 InsMask &= Value;
287 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000288 }
289
290 // If both of the inputs are ANDs and one of them has a logical shift by
291 // constant as its input, make that AND the inserted value so that we can
292 // combine the shift into the rotate part of the rlwimi instruction
293 bool IsAndWithShiftOp = false;
294 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
295 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
296 Op1.getOperand(0).getOpcode() == ISD::SRL) {
297 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
298 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
299 IsAndWithShiftOp = true;
300 }
301 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
302 Op0.getOperand(0).getOpcode() == ISD::SRL) {
303 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
304 std::swap(Op0, Op1);
305 std::swap(TgtMask, InsMask);
306 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
307 IsAndWithShiftOp = true;
308 }
309 }
310 }
311
312 // Verify that the Target mask and Insert mask together form a full word mask
313 // and that the Insert mask is a run of set bits (which implies both are runs
314 // of set bits). Given that, Select the arguments and generate the rlwimi
315 // instruction.
316 unsigned MB, ME;
317 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
318 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
319 bool Op0IsAND = Op0Opc == ISD::AND;
320 // Check for rotlwi / rotrwi here, a special case of bitfield insert
321 // where both bitfield halves are sourced from the same value.
322 if (IsRotate && fullMask &&
323 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
324 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
325 Select(N->getOperand(0).getOperand(0)),
326 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
327 return Op0.Val;
328 }
329 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
330 : Select(Op0);
331 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
332 : Select(Op1.getOperand(0));
333 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
334 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
335 return Op0.Val;
336 }
337 return 0;
338}
339
Chris Lattnera5a91b12005-08-17 19:33:03 +0000340// SelectIntImmediateExpr - Choose code for integer operations with an immediate
341// operand.
342SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
343 unsigned OCHi, unsigned OCLo,
344 bool IsArithmetic,
345 bool Negate) {
346 // Check to make sure this is a constant.
347 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
348 // Exit if not a constant.
349 if (!CN) return 0;
350 // Extract immediate.
351 unsigned C = (unsigned)CN->getValue();
352 // Negate if required (ISD::SUB).
353 if (Negate) C = -C;
354 // Get the hi and lo portions of constant.
355 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
356 unsigned Lo = Lo16(C);
357
358 // If two instructions are needed and usage indicates it would be better to
359 // load immediate into a register, bail out.
360 if (Hi && Lo && CN->use_size() > 2) return false;
361
362 // Select the first operand.
363 SDOperand Opr0 = Select(LHS);
364
365 if (Lo) // Add in the lo-part.
366 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
367 if (Hi) // Add in the hi-part.
368 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
369 return Opr0.Val;
370}
371
Chris Lattner9944b762005-08-21 22:31:09 +0000372/// SelectAddr - Given the specified address, return the two operands for a
373/// load/store instruction, and return true if it should be an indexed [r+r]
374/// operation.
375bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
376 SDOperand &Op2) {
377 unsigned imm = 0;
378 if (Addr.getOpcode() == ISD::ADD) {
379 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
380 Op1 = getI32Imm(Lo16(imm));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000381 if (FrameIndexSDNode *FI =
382 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner9944b762005-08-21 22:31:09 +0000383 ++FrameOff;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000384 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000385 } else {
386 Op2 = Select(Addr.getOperand(0));
387 }
388 return false;
389 } else {
390 Op1 = Select(Addr.getOperand(0));
391 Op2 = Select(Addr.getOperand(1));
392 return true; // [r+r]
393 }
394 }
395
396 // Now check if we're dealing with a global, and whether or not we should emit
397 // an optimized load or store for statics.
398 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
399 GlobalValue *GV = GN->getGlobal();
400 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
401 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
402 if (PICEnabled)
403 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
404 Op1);
405 else
406 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
407 return false;
408 }
Chris Lattnere28e40a2005-08-25 00:45:43 +0000409 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) {
Chris Lattner9944b762005-08-21 22:31:09 +0000410 Op1 = getI32Imm(0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000411 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000412 return false;
413 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
414 Op1 = Addr;
415 if (PICEnabled)
416 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
417 else
418 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
419 return false;
420 }
421 Op1 = getI32Imm(0);
422 Op2 = Select(Addr);
423 return false;
424}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000425
Chris Lattner2fbb4572005-08-21 18:50:37 +0000426/// SelectCC - Select a comparison of the specified values with the specified
427/// condition code, returning the CR# of the expression.
428SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
429 ISD::CondCode CC) {
430 // Always select the LHS.
431 LHS = Select(LHS);
432
433 // Use U to determine whether the SETCC immediate range is signed or not.
434 if (MVT::isInteger(LHS.getValueType())) {
435 bool U = ISD::isUnsignedIntSetCC(CC);
436 unsigned Imm;
437 if (isIntImmediate(RHS, Imm) &&
438 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
439 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
440 LHS, getI32Imm(Lo16(Imm)));
441 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
442 LHS, Select(RHS));
443 } else {
444 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS));
445 }
446}
447
448/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
449/// to Condition.
450static unsigned getBCCForSetCC(ISD::CondCode CC) {
451 switch (CC) {
452 default: assert(0 && "Unknown condition!"); abort();
453 case ISD::SETEQ: return PPC::BEQ;
454 case ISD::SETNE: return PPC::BNE;
455 case ISD::SETULT:
456 case ISD::SETLT: return PPC::BLT;
457 case ISD::SETULE:
458 case ISD::SETLE: return PPC::BLE;
459 case ISD::SETUGT:
460 case ISD::SETGT: return PPC::BGT;
461 case ISD::SETUGE:
462 case ISD::SETGE: return PPC::BGE;
463 }
464 return 0;
465}
466
Chris Lattner64906a02005-08-25 20:08:18 +0000467/// getCRIdxForSetCC - Return the index of the condition register field
468/// associated with the SetCC condition, and whether or not the field is
469/// treated as inverted. That is, lt = 0; ge = 0 inverted.
470static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
471 switch (CC) {
472 default: assert(0 && "Unknown condition!"); abort();
473 case ISD::SETULT:
474 case ISD::SETLT: Inv = false; return 0;
475 case ISD::SETUGE:
476 case ISD::SETGE: Inv = true; return 0;
477 case ISD::SETUGT:
478 case ISD::SETGT: Inv = false; return 1;
479 case ISD::SETULE:
480 case ISD::SETLE: Inv = true; return 1;
481 case ISD::SETEQ: Inv = false; return 2;
482 case ISD::SETNE: Inv = true; return 2;
483 }
484 return 0;
485}
Chris Lattner9944b762005-08-21 22:31:09 +0000486
Chris Lattner047b9522005-08-25 22:04:30 +0000487// Structure used to return the necessary information to codegen an SDIV as
488// a multiply.
489struct ms {
490 int m; // magic number
491 int s; // shift amount
492};
493
494struct mu {
495 unsigned int m; // magic number
496 int a; // add indicator
497 int s; // shift amount
498};
499
500/// magic - calculate the magic numbers required to codegen an integer sdiv as
501/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
502/// or -1.
503static struct ms magic(int d) {
504 int p;
505 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
506 const unsigned int two31 = 0x80000000U;
507 struct ms mag;
508
509 ad = abs(d);
510 t = two31 + ((unsigned int)d >> 31);
511 anc = t - 1 - t%ad; // absolute value of nc
512 p = 31; // initialize p
513 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
514 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
515 q2 = two31/ad; // initialize q2 = 2p/abs(d)
516 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
517 do {
518 p = p + 1;
519 q1 = 2*q1; // update q1 = 2p/abs(nc)
520 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
521 if (r1 >= anc) { // must be unsigned comparison
522 q1 = q1 + 1;
523 r1 = r1 - anc;
524 }
525 q2 = 2*q2; // update q2 = 2p/abs(d)
526 r2 = 2*r2; // update r2 = rem(2p/abs(d))
527 if (r2 >= ad) { // must be unsigned comparison
528 q2 = q2 + 1;
529 r2 = r2 - ad;
530 }
531 delta = ad - r2;
532 } while (q1 < delta || (q1 == delta && r1 == 0));
533
534 mag.m = q2 + 1;
535 if (d < 0) mag.m = -mag.m; // resulting magic number
536 mag.s = p - 32; // resulting shift
537 return mag;
538}
539
540/// magicu - calculate the magic numbers required to codegen an integer udiv as
541/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
542static struct mu magicu(unsigned d)
543{
544 int p;
545 unsigned int nc, delta, q1, r1, q2, r2;
546 struct mu magu;
547 magu.a = 0; // initialize "add" indicator
548 nc = - 1 - (-d)%d;
549 p = 31; // initialize p
550 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
551 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
552 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
553 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
554 do {
555 p = p + 1;
556 if (r1 >= nc - r1 ) {
557 q1 = 2*q1 + 1; // update q1
558 r1 = 2*r1 - nc; // update r1
559 }
560 else {
561 q1 = 2*q1; // update q1
562 r1 = 2*r1; // update r1
563 }
564 if (r2 + 1 >= d - r2) {
565 if (q2 >= 0x7FFFFFFF) magu.a = 1;
566 q2 = 2*q2 + 1; // update q2
567 r2 = 2*r2 + 1 - d; // update r2
568 }
569 else {
570 if (q2 >= 0x80000000) magu.a = 1;
571 q2 = 2*q2; // update q2
572 r2 = 2*r2 + 1; // update r2
573 }
574 delta = d - 1 - r2;
575 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
576 magu.m = q2 + 1; // resulting magic number
577 magu.s = p - 32; // resulting shift
578 return magu;
579}
580
581/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
582/// return a DAG expression to select that will generate the same value by
583/// multiplying by a magic number. See:
584/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
585SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) {
586 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue();
587 ms magics = magic(d);
588 // Multiply the numerator (operand 0) by the magic value
589 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0),
590 CurDAG->getConstant(magics.m, MVT::i32));
591 // If d > 0 and m < 0, add the numerator
592 if (d > 0 && magics.m < 0)
593 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0));
594 // If d < 0 and m > 0, subtract the numerator.
595 if (d < 0 && magics.m > 0)
596 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0));
597 // Shift right algebraic if shift value is nonzero
598 if (magics.s > 0)
599 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q,
600 CurDAG->getConstant(magics.s, MVT::i32));
601 // Extract the sign bit and add it to the quotient
602 SDOperand T =
603 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32));
604 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T);
605}
606
607/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
608/// return a DAG expression to select that will generate the same value by
609/// multiplying by a magic number. See:
610/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
611SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) {
612 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue();
613 mu magics = magicu(d);
614 // Multiply the numerator (operand 0) by the magic value
615 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0),
616 CurDAG->getConstant(magics.m, MVT::i32));
617 if (magics.a == 0) {
618 return CurDAG->getNode(ISD::SRL, MVT::i32, Q,
619 CurDAG->getConstant(magics.s, MVT::i32));
620 } else {
621 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q);
622 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
623 CurDAG->getConstant(1, MVT::i32));
624 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
625 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
626 CurDAG->getConstant(magics.s-1, MVT::i32));
627 }
628}
629
Chris Lattnera5a91b12005-08-17 19:33:03 +0000630// Select - Convert the specified operand from a target-independent to a
631// target-specific node if it hasn't already been changed.
632SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
633 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000634 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
635 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattnera5a91b12005-08-17 19:33:03 +0000636 return Op; // Already selected.
637
638 switch (N->getOpcode()) {
639 default:
640 std::cerr << "Cannot yet select: ";
641 N->dump();
642 std::cerr << "\n";
643 abort();
644 case ISD::EntryToken: // These leaves remain the same.
Chris Lattnera5a91b12005-08-17 19:33:03 +0000645 return Op;
Chris Lattner99296ff2005-08-31 18:08:46 +0000646 case ISD::AssertSext:
647 case ISD::AssertZext:
648 return Select(N->getOperand(0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000649 case ISD::TokenFactor: {
650 SDOperand New;
651 if (N->getNumOperands() == 2) {
652 SDOperand Op0 = Select(N->getOperand(0));
653 SDOperand Op1 = Select(N->getOperand(1));
654 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
655 } else {
656 std::vector<SDOperand> Ops;
657 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000658 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000659 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
660 }
661
662 if (New.Val != N) {
Chris Lattner52987f42005-08-26 18:37:23 +0000663 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000664 N = New.Val;
665 }
Chris Lattner25dae722005-09-03 00:53:47 +0000666 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000667 }
668 case ISD::CopyFromReg: {
669 SDOperand Chain = Select(N->getOperand(0));
670 if (Chain == N->getOperand(0)) return Op; // No change
671 SDOperand New = CurDAG->getCopyFromReg(Chain,
672 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
673 return New.getValue(Op.ResNo);
674 }
675 case ISD::CopyToReg: {
676 SDOperand Chain = Select(N->getOperand(0));
677 SDOperand Reg = N->getOperand(1);
678 SDOperand Val = Select(N->getOperand(2));
679 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
680 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
681 Chain, Reg, Val);
Chris Lattner52987f42005-08-26 18:37:23 +0000682 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000683 N = New.Val;
684 }
Chris Lattner25dae722005-09-03 00:53:47 +0000685 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000686 }
687 case ISD::Constant: {
688 assert(N->getValueType(0) == MVT::i32);
689 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Chris Lattner2fef8092005-08-29 01:01:01 +0000690
691 // NOTE: This doesn't use SelectNodeTo, because doing that will prevent
692 // folding shared immediates into other the second instruction that
693 // uses it.
Chris Lattner393ecd62005-09-01 19:38:28 +0000694 if (isInt16(v))
Chris Lattner2fef8092005-08-29 01:01:01 +0000695 return CurDAG->getTargetNode(PPC::LI, MVT::i32, getI32Imm(v));
Chris Lattner393ecd62005-09-01 19:38:28 +0000696
697 unsigned Hi = Hi16(v);
698 unsigned Lo = Lo16(v);
699
700 if (!Lo)
701 return CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi));
702
703 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi));
704 return CurDAG->getTargetNode(PPC::ORI, MVT::i32, Top, getI32Imm(Lo));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000705 }
Chris Lattner2b544002005-08-24 23:08:16 +0000706 case ISD::UNDEF:
707 if (N->getValueType(0) == MVT::i32)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000708 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattner2b544002005-08-24 23:08:16 +0000709 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000710 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0));
Chris Lattner25dae722005-09-03 00:53:47 +0000711 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000712 case ISD::FrameIndex: {
713 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000714 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere28e40a2005-08-25 00:45:43 +0000715 CurDAG->getTargetFrameIndex(FI, MVT::i32),
716 getI32Imm(0));
Chris Lattner25dae722005-09-03 00:53:47 +0000717 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000718 }
Chris Lattner34e17052005-08-25 05:04:11 +0000719 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000720 Constant *C = cast<ConstantPoolSDNode>(N)->get();
721 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner34e17052005-08-25 05:04:11 +0000722 if (PICEnabled)
723 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
724 else
725 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000726 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner25dae722005-09-03 00:53:47 +0000727 return SDOperand(N, 0);
Chris Lattner34e17052005-08-25 05:04:11 +0000728 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000729 case ISD::GlobalAddress: {
730 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
731 SDOperand Tmp;
732 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000733 if (PICEnabled)
734 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
735 else
Chris Lattner4416f1a2005-08-19 22:38:53 +0000736 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +0000737
Chris Lattner4416f1a2005-08-19 22:38:53 +0000738 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000739 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000740 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000741 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner25dae722005-09-03 00:53:47 +0000742 return SDOperand(N, 0);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000743 }
Chris Lattner9c2dece2005-08-29 23:30:11 +0000744 case ISD::DYNAMIC_STACKALLOC: {
745 // FIXME: We are currently ignoring the requested alignment for handling
746 // greater than the stack alignment. This will need to be revisited at some
747 // point. Align = N.getOperand(2);
748 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
749 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
750 std::cerr << "Cannot allocate stack object with greater alignment than"
751 << " the stack alignment yet!";
752 abort();
753 }
754 SDOperand Chain = Select(N->getOperand(0));
755 SDOperand Amt = Select(N->getOperand(1));
756
757 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
758
Chris Lattner75592e42005-09-01 21:31:30 +0000759 SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
760 Chain = R1Val.getValue(1);
761
Chris Lattner9c2dece2005-08-29 23:30:11 +0000762 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
763 // from the stack pointer, giving us the result pointer.
Chris Lattner75592e42005-09-01 21:31:30 +0000764 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val);
Chris Lattner9c2dece2005-08-29 23:30:11 +0000765
766 // Copy this result back into R1.
767 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
768
769 // Copy this result back out of R1 to make sure we're not using the stack
770 // space without decrementing the stack pointer.
771 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
772
773 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
774 CurDAG->ReplaceAllUsesWith(N, Result.Val);
Chris Lattner25dae722005-09-03 00:53:47 +0000775 return SDOperand(Result.Val, Op.ResNo);
Chris Lattner9c2dece2005-08-29 23:30:11 +0000776 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000777 case ISD::SIGN_EXTEND_INREG:
778 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
779 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
780 case MVT::i16:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000781 CurDAG->SelectNodeTo(N, PPC::EXTSH, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000782 break;
783 case MVT::i8:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000784 CurDAG->SelectNodeTo(N, PPC::EXTSB, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000785 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000786 }
Chris Lattner25dae722005-09-03 00:53:47 +0000787 return SDOperand(N, 0);
Nate Begeman305a1c72005-08-18 03:04:18 +0000788 case ISD::CTLZ:
789 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000790 CurDAG->SelectNodeTo(N, PPC::CNTLZW, MVT::i32, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000791 return SDOperand(N, 0);
Chris Lattner0bbea952005-08-26 20:25:03 +0000792 case PPCISD::FSEL:
793 CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0),
794 Select(N->getOperand(0)),
795 Select(N->getOperand(1)),
796 Select(N->getOperand(2)));
Chris Lattner25dae722005-09-03 00:53:47 +0000797 return SDOperand(N, 0);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000798 case PPCISD::FCFID:
799 CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0),
800 Select(N->getOperand(0)));
801 return SDOperand(N, 0);
802 case PPCISD::FCTIDZ:
803 CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0),
804 Select(N->getOperand(0)));
805 return SDOperand(N, 0);
Chris Lattnerf7605322005-08-31 21:09:52 +0000806 case PPCISD::FCTIWZ:
807 CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0),
808 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000809 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000810 case ISD::ADD: {
811 MVT::ValueType Ty = N->getValueType(0);
812 if (Ty == MVT::i32) {
813 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
814 PPC::ADDIS, PPC::ADDI, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000815 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000816 N = I;
817 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000818 CurDAG->SelectNodeTo(N, PPC::ADD, MVT::i32, Select(N->getOperand(0)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000819 Select(N->getOperand(1)));
820 }
Chris Lattner25dae722005-09-03 00:53:47 +0000821 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000822 }
823
824 if (!NoExcessFPPrecision) { // Match FMA ops
825 if (N->getOperand(0).getOpcode() == ISD::MUL &&
826 N->getOperand(0).Val->hasOneUse()) {
827 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000828 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000829 Select(N->getOperand(0).getOperand(0)),
830 Select(N->getOperand(0).getOperand(1)),
831 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000832 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000833 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
834 N->getOperand(1).hasOneUse()) {
835 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000836 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000837 Select(N->getOperand(1).getOperand(0)),
838 Select(N->getOperand(1).getOperand(1)),
839 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000840 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000841 }
842 }
843
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000844 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000845 Select(N->getOperand(0)), Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000846 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000847 }
848 case ISD::SUB: {
849 MVT::ValueType Ty = N->getValueType(0);
850 if (Ty == MVT::i32) {
851 unsigned Imm;
852 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
Nate Begemanc6b07172005-08-24 05:03:20 +0000853 if (0 == Imm)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000854 CurDAG->SelectNodeTo(N, PPC::NEG, Ty, Select(N->getOperand(1)));
Nate Begemanc6b07172005-08-24 05:03:20 +0000855 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000856 CurDAG->SelectNodeTo(N, PPC::SUBFIC, Ty, Select(N->getOperand(1)),
Nate Begemanc6b07172005-08-24 05:03:20 +0000857 getI32Imm(Lo16(Imm)));
Chris Lattner25dae722005-09-03 00:53:47 +0000858 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000859 }
860 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
861 PPC::ADDIS, PPC::ADDI, true, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000862 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000863 N = I;
864 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000865 CurDAG->SelectNodeTo(N, PPC::SUBF, Ty, Select(N->getOperand(1)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000866 Select(N->getOperand(0)));
867 }
Chris Lattner25dae722005-09-03 00:53:47 +0000868 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000869 }
870
871 if (!NoExcessFPPrecision) { // Match FMA ops
872 if (N->getOperand(0).getOpcode() == ISD::MUL &&
873 N->getOperand(0).Val->hasOneUse()) {
874 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000875 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000876 Select(N->getOperand(0).getOperand(0)),
877 Select(N->getOperand(0).getOperand(1)),
878 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000879 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000880 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
881 N->getOperand(1).Val->hasOneUse()) {
882 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000883 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000884 Select(N->getOperand(1).getOperand(0)),
885 Select(N->getOperand(1).getOperand(1)),
886 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000887 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000888 }
889 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000890 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000891 Select(N->getOperand(0)),
892 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000893 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +0000894 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000895 case ISD::MUL: {
896 unsigned Imm, Opc;
897 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000898 CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32,
Nate Begemanb5a06682005-08-18 00:21:41 +0000899 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
Chris Lattner25dae722005-09-03 00:53:47 +0000900 return SDOperand(N, 0);
Nate Begemanb5a06682005-08-18 00:21:41 +0000901 }
902 switch (N->getValueType(0)) {
903 default: assert(0 && "Unhandled multiply type!");
904 case MVT::i32: Opc = PPC::MULLW; break;
905 case MVT::f32: Opc = PPC::FMULS; break;
906 case MVT::f64: Opc = PPC::FMUL; break;
907 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000908 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Nate Begemanb5a06682005-08-18 00:21:41 +0000909 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000910 return SDOperand(N, 0);
Nate Begemanb5a06682005-08-18 00:21:41 +0000911 }
Chris Lattner8784a232005-08-25 17:50:06 +0000912 case ISD::SDIV: {
913 unsigned Imm;
914 if (isIntImmediate(N->getOperand(1), Imm)) {
915 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
916 SDOperand Op =
917 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
918 Select(N->getOperand(0)),
919 getI32Imm(Log2_32(Imm)));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000920 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner8784a232005-08-25 17:50:06 +0000921 Op.getValue(0), Op.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +0000922 return SDOperand(N, 0);
Chris Lattner8784a232005-08-25 17:50:06 +0000923 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
924 SDOperand Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000925 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Chris Lattner8784a232005-08-25 17:50:06 +0000926 Select(N->getOperand(0)),
927 getI32Imm(Log2_32(-Imm)));
928 SDOperand PT =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000929 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0),
930 Op.getValue(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000931 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner25dae722005-09-03 00:53:47 +0000932 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +0000933 } else if (Imm) {
934 SDOperand Result = Select(BuildSDIVSequence(N));
935 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000936 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000937 N = Result.Val;
Chris Lattner25dae722005-09-03 00:53:47 +0000938 return SDOperand(N, 0);
Chris Lattner8784a232005-08-25 17:50:06 +0000939 }
940 }
Chris Lattner047b9522005-08-25 22:04:30 +0000941
942 unsigned Opc;
943 switch (N->getValueType(0)) {
Chris Lattner95e06822005-08-26 16:38:51 +0000944 default: assert(0 && "Unknown type to ISD::SDIV");
Chris Lattner047b9522005-08-25 22:04:30 +0000945 case MVT::i32: Opc = PPC::DIVW; break;
946 case MVT::f32: Opc = PPC::FDIVS; break;
947 case MVT::f64: Opc = PPC::FDIV; break;
948 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000949 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000950 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000951 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +0000952 }
953 case ISD::UDIV: {
954 // If this is a divide by constant, we can emit code using some magic
955 // constants to implement it as a multiply instead.
956 unsigned Imm;
Chris Lattnera9317ed2005-08-25 23:21:06 +0000957 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner047b9522005-08-25 22:04:30 +0000958 SDOperand Result = Select(BuildUDIVSequence(N));
959 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000960 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000961 N = Result.Val;
Chris Lattner25dae722005-09-03 00:53:47 +0000962 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +0000963 }
964
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000965 CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000966 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000967 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +0000968 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000969 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000970 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000971 CurDAG->SelectNodeTo(N, PPC::MULHW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000972 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000973 return SDOperand(N, 0);
Nate Begeman305a1c72005-08-18 03:04:18 +0000974 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000975 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000976 CurDAG->SelectNodeTo(N, PPC::MULHWU, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000977 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000978 return SDOperand(N, 0);
Nate Begemancffc32b2005-08-18 07:30:46 +0000979 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000980 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000981 // If this is an and of a value rotated between 0 and 31 bits and then and'd
982 // with a mask, emit rlwinm
983 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
984 isShiftedMask_32(~Imm))) {
985 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000986 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000987 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
988 Val = Select(N->getOperand(0).getOperand(0));
989 } else {
990 Val = Select(N->getOperand(0));
991 isRunOfOnes(Imm, MB, ME);
992 SH = 0;
993 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000994 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemancffc32b2005-08-18 07:30:46 +0000995 getI32Imm(MB), getI32Imm(ME));
Chris Lattner25dae722005-09-03 00:53:47 +0000996 return SDOperand(N, 0);
Nate Begemancffc32b2005-08-18 07:30:46 +0000997 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000998 // Finally, check for the case where we are being asked to select
999 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
1000 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001001 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)),
Nate Begemancffc32b2005-08-18 07:30:46 +00001002 Select(N->getOperand(0).getOperand(0)));
1003 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001004 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +00001005 Select(N->getOperand(1).getOperand(0)));
1006 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001007 CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +00001008 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001009 return SDOperand(N, 0);
Nate Begemancffc32b2005-08-18 07:30:46 +00001010 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001011 case ISD::OR:
1012 if (SDNode *I = SelectBitfieldInsert(N)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001013 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001014 N = I;
Chris Lattner25dae722005-09-03 00:53:47 +00001015 return SDOperand(N, 0);
Nate Begeman02b88a42005-08-19 00:38:14 +00001016 }
1017 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1018 N->getOperand(1),
1019 PPC::ORIS, PPC::ORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001020 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001021 N = I;
Chris Lattner25dae722005-09-03 00:53:47 +00001022 return SDOperand(N, 0);
Nate Begeman02b88a42005-08-19 00:38:14 +00001023 }
1024 // Finally, check for the case where we are being asked to select
1025 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
1026 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001027 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001028 Select(N->getOperand(0).getOperand(0)));
1029 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001030 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001031 Select(N->getOperand(1).getOperand(0)));
1032 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001033 CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001034 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001035 return SDOperand(N, 0);
Nate Begeman0f3257a2005-08-18 05:00:13 +00001036 case ISD::XOR:
1037 // Check whether or not this node is a logical 'not'. This is represented
1038 // by llvm as a xor with the constant value -1 (all bits set). If this is a
1039 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
1040 if (isOprNot(N)) {
1041 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +00001042 SDOperand Val = Select(N->getOperand(0));
Chris Lattner528f58e2005-08-28 23:39:22 +00001043 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman0f3257a2005-08-18 05:00:13 +00001044 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +00001045 case PPC::OR: Opc = PPC::NOR; break;
1046 case PPC::AND: Opc = PPC::NAND; break;
1047 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +00001048 }
1049 if (Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001050 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Val.getOperand(0),
Nate Begeman131a8802005-08-18 05:44:50 +00001051 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001052 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001053 CurDAG->SelectNodeTo(N, PPC::NOR, MVT::i32, Val, Val);
Chris Lattner25dae722005-09-03 00:53:47 +00001054 return SDOperand(N, 0);
Nate Begeman0f3257a2005-08-18 05:00:13 +00001055 }
1056 // If this is a xor with an immediate other than -1, then codegen it as high
1057 // and low 16 bit immediate xors.
1058 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1059 N->getOperand(1),
1060 PPC::XORIS, PPC::XORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001061 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001062 N = I;
Chris Lattner25dae722005-09-03 00:53:47 +00001063 return SDOperand(N, 0);
Nate Begeman0f3257a2005-08-18 05:00:13 +00001064 }
1065 // Finally, check for the case where we are being asked to select
1066 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
1067 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001068 CurDAG->SelectNodeTo(N, PPC::EQV, MVT::i32,
Nate Begeman0f3257a2005-08-18 05:00:13 +00001069 Select(N->getOperand(0).getOperand(0)),
1070 Select(N->getOperand(1)));
1071 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001072 CurDAG->SelectNodeTo(N, PPC::XOR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman0f3257a2005-08-18 05:00:13 +00001073 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001074 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001075 case ISD::SHL: {
1076 unsigned Imm, SH, MB, ME;
1077 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1078 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001079 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001080 Select(N->getOperand(0).getOperand(0)),
1081 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1082 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001083 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001084 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
1085 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001086 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001087 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001088 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001089 }
1090 case ISD::SRL: {
1091 unsigned Imm, SH, MB, ME;
1092 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1093 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001094 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001095 Select(N->getOperand(0).getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001096 getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME));
Nate Begemanc15ed442005-08-18 23:38:00 +00001097 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001098 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001099 getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm),
1100 getI32Imm(31));
Nate Begemanc15ed442005-08-18 23:38:00 +00001101 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001102 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001103 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001104 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001105 }
1106 case ISD::SRA: {
1107 unsigned Imm, SH, MB, ME;
1108 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1109 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001110 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001111 Select(N->getOperand(0).getOperand(0)),
1112 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1113 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001114 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001115 getI32Imm(Imm));
1116 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001117 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001118 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001119 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001120 }
Nate Begeman305a1c72005-08-18 03:04:18 +00001121 case ISD::FABS:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001122 CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0),
Nate Begeman6a7d6112005-08-18 00:53:47 +00001123 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001124 return SDOperand(N, 0);
Chris Lattner8f838722005-08-30 00:30:43 +00001125 case ISD::FP_EXTEND:
Nate Begeman305a1c72005-08-18 03:04:18 +00001126 assert(MVT::f64 == N->getValueType(0) &&
1127 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner8f838722005-08-30 00:30:43 +00001128 // We need to emit an FMR to make sure that the result has the right value
1129 // type.
1130 CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001131 return SDOperand(N, 0);
Nate Begeman305a1c72005-08-18 03:04:18 +00001132 case ISD::FP_ROUND:
1133 assert(MVT::f32 == N->getValueType(0) &&
1134 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001135 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001136 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001137 case ISD::FNEG: {
1138 SDOperand Val = Select(N->getOperand(0));
1139 MVT::ValueType Ty = N->getValueType(0);
1140 if (Val.Val->hasOneUse()) {
1141 unsigned Opc;
Chris Lattner528f58e2005-08-28 23:39:22 +00001142 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman26653502005-08-17 23:46:35 +00001143 default: Opc = 0; break;
1144 case PPC::FABS: Opc = PPC::FNABS; break;
1145 case PPC::FMADD: Opc = PPC::FNMADD; break;
1146 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1147 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1148 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1149 }
1150 // If we inverted the opcode, then emit the new instruction with the
1151 // inverted opcode and the original instruction's operands. Otherwise,
1152 // fall through and generate a fneg instruction.
1153 if (Opc) {
1154 if (PPC::FNABS == Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001155 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman26653502005-08-17 23:46:35 +00001156 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001157 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman26653502005-08-17 23:46:35 +00001158 Val.getOperand(1), Val.getOperand(2));
Chris Lattner25dae722005-09-03 00:53:47 +00001159 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001160 }
1161 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001162 CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val);
Chris Lattner25dae722005-09-03 00:53:47 +00001163 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001164 }
Nate Begeman6a7d6112005-08-18 00:53:47 +00001165 case ISD::FSQRT: {
1166 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001167 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman6a7d6112005-08-18 00:53:47 +00001168 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001169 return SDOperand(N, 0);
Nate Begeman6a7d6112005-08-18 00:53:47 +00001170 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001171
1172 case ISD::ADD_PARTS: {
1173 SDOperand LHSL = Select(N->getOperand(0));
1174 SDOperand LHSH = Select(N->getOperand(1));
1175
1176 unsigned Imm;
Chris Lattner95e06822005-08-26 16:38:51 +00001177 bool ME = false, ZE = false;
Chris Lattnera9317ed2005-08-25 23:21:06 +00001178 if (isIntImmediate(N->getOperand(3), Imm)) {
1179 ME = (signed)Imm == -1;
1180 ZE = Imm == 0;
1181 }
1182
1183 std::vector<SDOperand> Result;
1184 SDOperand CarryFromLo;
1185 if (isIntImmediate(N->getOperand(2), Imm) &&
1186 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
1187 // Codegen the low 32 bits of the add. Interestingly, there is no
1188 // shifted form of add immediate carrying.
1189 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1190 LHSL, getI32Imm(Imm));
1191 } else {
1192 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
1193 LHSL, Select(N->getOperand(2)));
1194 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001195 CarryFromLo = CarryFromLo.getValue(1);
1196
1197 // Codegen the high 32 bits, adding zero, minus one, or the full value
1198 // along with the carry flag produced by addc/addic.
1199 SDOperand ResultHi;
1200 if (ZE)
1201 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
1202 else if (ME)
1203 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
1204 else
1205 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
1206 Select(N->getOperand(3)), CarryFromLo);
Chris Lattnerb20c3182005-08-25 23:36:49 +00001207 Result.push_back(CarryFromLo.getValue(0));
Chris Lattner14b86c72005-08-30 17:40:13 +00001208 Result.push_back(ResultHi);
Chris Lattnera9317ed2005-08-25 23:21:06 +00001209 CurDAG->ReplaceAllUsesWith(N, Result);
1210 return Result[Op.ResNo];
1211 }
1212 case ISD::SUB_PARTS: {
1213 SDOperand LHSL = Select(N->getOperand(0));
1214 SDOperand LHSH = Select(N->getOperand(1));
1215 SDOperand RHSL = Select(N->getOperand(2));
1216 SDOperand RHSH = Select(N->getOperand(3));
1217
1218 std::vector<SDOperand> Result;
1219 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
1220 RHSL, LHSL));
1221 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
1222 Result[0].getValue(1)));
1223 CurDAG->ReplaceAllUsesWith(N, Result);
1224 return Result[Op.ResNo];
1225 }
1226
Chris Lattner9944b762005-08-21 22:31:09 +00001227 case ISD::LOAD:
1228 case ISD::EXTLOAD:
1229 case ISD::ZEXTLOAD:
1230 case ISD::SEXTLOAD: {
1231 SDOperand Op1, Op2;
1232 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1233
1234 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1235 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1236 unsigned Opc;
1237 switch (TypeBeingLoaded) {
1238 default: N->dump(); assert(0 && "Cannot load this type!");
1239 case MVT::i1:
1240 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1241 case MVT::i16:
1242 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1243 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1244 } else {
1245 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1246 }
1247 break;
1248 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1249 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1250 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1251 }
1252
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001253 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattner9944b762005-08-21 22:31:09 +00001254 Op1, Op2, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001255 return SDOperand(N, Op.ResNo);
Chris Lattner9944b762005-08-21 22:31:09 +00001256 }
1257
Chris Lattnerf7f22552005-08-22 01:27:59 +00001258 case ISD::TRUNCSTORE:
1259 case ISD::STORE: {
1260 SDOperand AddrOp1, AddrOp2;
1261 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1262
1263 unsigned Opc;
1264 if (N->getOpcode() == ISD::STORE) {
1265 switch (N->getOperand(1).getValueType()) {
1266 default: assert(0 && "unknown Type in store");
1267 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1268 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1269 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1270 }
1271 } else { //ISD::TRUNCSTORE
1272 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1273 default: assert(0 && "unknown Type in store");
1274 case MVT::i1:
1275 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1276 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1277 }
1278 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001279
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001280 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerf7f22552005-08-22 01:27:59 +00001281 AddrOp1, AddrOp2, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001282 return SDOperand(N, 0);
Chris Lattnerf7f22552005-08-22 01:27:59 +00001283 }
Chris Lattner64906a02005-08-25 20:08:18 +00001284
1285 case ISD::SETCC: {
1286 unsigned Imm;
1287 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1288 if (isIntImmediate(N->getOperand(1), Imm)) {
1289 // We can codegen setcc op, imm very efficiently compared to a brcond.
1290 // Check for those cases here.
1291 // setcc op, 0
1292 if (Imm == 0) {
1293 SDOperand Op = Select(N->getOperand(0));
1294 switch (CC) {
1295 default: assert(0 && "Unhandled SetCC condition"); abort();
1296 case ISD::SETEQ:
1297 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001298 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattner64906a02005-08-25 20:08:18 +00001299 getI32Imm(5), getI32Imm(31));
1300 break;
1301 case ISD::SETNE: {
1302 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1303 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001304 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001305 break;
1306 }
1307 case ISD::SETLT:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001308 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001309 getI32Imm(31), getI32Imm(31));
1310 break;
1311 case ISD::SETGT: {
1312 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
1313 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001314 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001315 getI32Imm(31), getI32Imm(31));
1316 break;
1317 }
1318 }
Chris Lattner25dae722005-09-03 00:53:47 +00001319 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001320 } else if (Imm == ~0U) { // setcc op, -1
1321 SDOperand Op = Select(N->getOperand(0));
1322 switch (CC) {
1323 default: assert(0 && "Unhandled SetCC condition"); abort();
1324 case ISD::SETEQ:
1325 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1326 Op, getI32Imm(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001327 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner64906a02005-08-25 20:08:18 +00001328 CurDAG->getTargetNode(PPC::LI, MVT::i32,
1329 getI32Imm(0)),
1330 Op.getValue(1));
1331 break;
1332 case ISD::SETNE: {
1333 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
Chris Lattner8bbcc202005-08-29 23:49:25 +00001334 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1335 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001336 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001337 break;
1338 }
1339 case ISD::SETLT: {
1340 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
1341 getI32Imm(1));
1342 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001343 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001344 getI32Imm(31), getI32Imm(31));
1345 break;
1346 }
1347 case ISD::SETGT:
1348 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
1349 getI32Imm(31), getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001350 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001351 break;
1352 }
Chris Lattner25dae722005-09-03 00:53:47 +00001353 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001354 }
1355 }
1356
1357 bool Inv;
1358 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Chris Lattner50ff55c2005-09-01 19:20:44 +00001359 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner64906a02005-08-25 20:08:18 +00001360 SDOperand IntCR;
Chris Lattner957fcfb2005-08-25 21:39:42 +00001361
1362 // Force the ccreg into CR7.
1363 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
1364
1365 std::vector<MVT::ValueType> VTs;
1366 VTs.push_back(MVT::Other);
1367 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
1368 std::vector<SDOperand> Ops;
1369 Ops.push_back(CurDAG->getEntryNode());
1370 Ops.push_back(CR7Reg);
1371 Ops.push_back(CCReg);
1372 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
1373
1374 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1375 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
1376 else
1377 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
Chris Lattner64906a02005-08-25 20:08:18 +00001378
1379 if (!Inv) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001380 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner64906a02005-08-25 20:08:18 +00001381 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
1382 } else {
1383 SDOperand Tmp =
1384 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
1385 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001386 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001387 }
1388
Chris Lattner25dae722005-09-03 00:53:47 +00001389 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001390 }
Chris Lattnera2590c52005-08-24 00:47:15 +00001391
Chris Lattner13794f52005-08-26 18:46:49 +00001392 case ISD::SELECT_CC: {
1393 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1394
1395 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1396 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1397 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1398 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1399 if (N1C->isNullValue() && N3C->isNullValue() &&
1400 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1401 SDOperand LHS = Select(N->getOperand(0));
1402 SDOperand Tmp =
1403 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1404 LHS, getI32Imm(~0U));
1405 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1406 Tmp.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +00001407 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001408 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001409
Chris Lattner50ff55c2005-09-01 19:20:44 +00001410 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001411 unsigned BROpc = getBCCForSetCC(CC);
1412
1413 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
1414 unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int;
1415 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1416 Select(N->getOperand(2)), Select(N->getOperand(3)),
1417 getI32Imm(BROpc));
Chris Lattner25dae722005-09-03 00:53:47 +00001418 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001419 }
1420
Chris Lattnera2590c52005-08-24 00:47:15 +00001421 case ISD::CALLSEQ_START:
1422 case ISD::CALLSEQ_END: {
1423 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1424 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1425 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001426 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001427 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001428 return SDOperand(N, 0);
Chris Lattnera2590c52005-08-24 00:47:15 +00001429 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001430 case ISD::CALL:
1431 case ISD::TAILCALL: {
1432 SDOperand Chain = Select(N->getOperand(0));
1433
1434 unsigned CallOpcode;
1435 std::vector<SDOperand> CallOperands;
1436
1437 if (GlobalAddressSDNode *GASD =
1438 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
1439 CallOpcode = PPC::CALLpcrel;
1440 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
1441 MVT::i32));
1442 } else if (ExternalSymbolSDNode *ESSDN =
1443 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
1444 CallOpcode = PPC::CALLpcrel;
1445 CallOperands.push_back(N->getOperand(1));
1446 } else {
1447 // Copy the callee address into the CTR register.
1448 SDOperand Callee = Select(N->getOperand(1));
1449 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
1450
1451 // Copy the callee address into R12 on darwin.
1452 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
Chris Lattner2a06a5e2005-08-29 00:26:57 +00001453 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001454
1455 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
1456 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
1457 CallOperands.push_back(R12);
1458 CallOpcode = PPC::CALLindirect;
1459 }
1460
1461 unsigned GPR_idx = 0, FPR_idx = 0;
1462 static const unsigned GPR[] = {
1463 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1464 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1465 };
1466 static const unsigned FPR[] = {
1467 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1468 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1469 };
1470
Chris Lattner31ce12f2005-08-30 01:57:02 +00001471 SDOperand InFlag; // Null incoming flag value.
1472
Chris Lattner7107c102005-08-29 22:22:57 +00001473 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1474 unsigned DestReg = 0;
Chris Lattnereb80fe82005-08-30 22:59:48 +00001475 MVT::ValueType RegTy = N->getOperand(i).getValueType();
1476 if (RegTy == MVT::i32) {
Chris Lattner7107c102005-08-29 22:22:57 +00001477 assert(GPR_idx < 8 && "Too many int args");
1478 DestReg = GPR[GPR_idx++];
Chris Lattner7107c102005-08-29 22:22:57 +00001479 } else {
1480 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
1481 "Unpromoted integer arg?");
1482 assert(FPR_idx < 13 && "Too many fp args");
1483 DestReg = FPR[FPR_idx++];
Chris Lattner7107c102005-08-29 22:22:57 +00001484 }
1485
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001486 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Chris Lattner2ea0c662005-08-30 21:28:19 +00001487 SDOperand Val = Select(N->getOperand(i));
Chris Lattner2ea0c662005-08-30 21:28:19 +00001488 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
Chris Lattner31ce12f2005-08-30 01:57:02 +00001489 InFlag = Chain.getValue(1);
1490 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001491 }
Chris Lattner7107c102005-08-29 22:22:57 +00001492 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001493
1494 // Finally, once everything is in registers to pass to the call, emit the
1495 // call itself.
Chris Lattner31ce12f2005-08-30 01:57:02 +00001496 if (InFlag.Val)
1497 CallOperands.push_back(InFlag); // Strong dep on register copies.
1498 else
1499 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
1500 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
1501 CallOperands);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001502
1503 std::vector<SDOperand> CallResults;
1504
1505 // If the call has results, copy the values out of the ret val registers.
1506 switch (N->getValueType(0)) {
1507 default: assert(0 && "Unexpected ret value!");
1508 case MVT::Other: break;
1509 case MVT::i32:
1510 if (N->getValueType(1) == MVT::i32) {
Chris Lattner31ce12f2005-08-30 01:57:02 +00001511 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
1512 Chain.getValue(1)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001513 CallResults.push_back(Chain.getValue(0));
Chris Lattner31ce12f2005-08-30 01:57:02 +00001514 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1515 Chain.getValue(1)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001516 CallResults.push_back(Chain.getValue(0));
1517 } else {
Chris Lattner31ce12f2005-08-30 01:57:02 +00001518 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1519 Chain.getValue(1)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001520 CallResults.push_back(Chain.getValue(0));
1521 }
1522 break;
1523 case MVT::f32:
1524 case MVT::f64:
Chris Lattnereb80fe82005-08-30 22:59:48 +00001525 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
Chris Lattner31ce12f2005-08-30 01:57:02 +00001526 Chain.getValue(1)).getValue(1);
Chris Lattnereb80fe82005-08-30 22:59:48 +00001527 CallResults.push_back(Chain.getValue(0));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001528 break;
1529 }
1530
1531 CallResults.push_back(Chain);
1532 CurDAG->ReplaceAllUsesWith(N, CallResults);
1533 return CallResults[Op.ResNo];
1534 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001535 case ISD::RET: {
1536 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1537
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001538 if (N->getNumOperands() == 2) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001539 SDOperand Val = Select(N->getOperand(1));
Chris Lattnereb80fe82005-08-30 22:59:48 +00001540 if (N->getOperand(1).getValueType() == MVT::i32) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001541 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
Chris Lattnereb80fe82005-08-30 22:59:48 +00001542 } else {
1543 assert(MVT::isFloatingPoint(N->getOperand(1).getValueType()));
1544 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001545 }
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001546 } else if (N->getNumOperands() > 1) {
1547 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1548 N->getOperand(2).getValueType() == MVT::i32 &&
1549 N->getNumOperands() == 3 && "Unknown two-register ret value!");
1550 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1)));
1551 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2)));
Chris Lattnera5a91b12005-08-17 19:33:03 +00001552 }
1553
1554 // Finally, select this to a blr (return) instruction.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001555 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattner25dae722005-09-03 00:53:47 +00001556 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001557 }
Chris Lattner89532c72005-08-25 00:29:58 +00001558 case ISD::BR:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001559 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner89532c72005-08-25 00:29:58 +00001560 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001561 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001562 case ISD::BR_CC:
1563 case ISD::BRTWOWAY_CC: {
1564 SDOperand Chain = Select(N->getOperand(0));
1565 MachineBasicBlock *Dest =
1566 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1567 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1568 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1569 unsigned Opc = getBCCForSetCC(CC);
1570
1571 // If this is a two way branch, then grab the fallthrough basic block
1572 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1573 // conversion if necessary by the branch selection pass. Otherwise, emit a
1574 // standard conditional branch.
1575 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
1576 MachineBasicBlock *Fallthrough =
1577 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
1578 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1579 CondCode, getI32Imm(Opc),
1580 N->getOperand(4), N->getOperand(5),
1581 Chain);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001582 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001583 } else {
1584 // Iterate to the next basic block
1585 ilist<MachineBasicBlock>::iterator It = BB;
1586 ++It;
1587
1588 // If the fallthrough path is off the end of the function, which would be
1589 // undefined behavior, set it to be the same as the current block because
1590 // we have nothing better to set it to, and leaving it alone will cause
1591 // the PowerPC Branch Selection pass to crash.
1592 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001593 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001594 getI32Imm(Opc), N->getOperand(4),
1595 CurDAG->getBasicBlock(It), Chain);
1596 }
Chris Lattner25dae722005-09-03 00:53:47 +00001597 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001598 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001599 }
Chris Lattner25dae722005-09-03 00:53:47 +00001600
1601 assert(0 && "Unreachable!");
1602 abort();
Chris Lattnerddf3e7d2005-08-22 00:59:14 +00001603 return SDOperand(N, Op.ResNo);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001604}
1605
1606
1607/// createPPC32ISelDag - This pass converts a legalized DAG into a
1608/// PowerPC-specific DAG, ready for instruction scheduling.
1609///
1610FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1611 return new PPC32DAGToDAGISel(TM);
1612}
1613