blob: f2d65697da7a30b3b1ec32f1f6ceef9fba20cd86 [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"
Chris Lattnerc8a89a12005-08-28 23:59:09 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000021#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30using namespace llvm;
31
32namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000033 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
34 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
35
36 //===--------------------------------------------------------------------===//
37 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
38 /// instructions for SelectionDAG operations.
39 ///
40 class PPC32DAGToDAGISel : public SelectionDAGISel {
41 PPC32TargetLowering PPC32Lowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000042 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000043 public:
44 PPC32DAGToDAGISel(TargetMachine &TM)
45 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
46
Chris Lattner4416f1a2005-08-19 22:38:53 +000047 virtual bool runOnFunction(Function &Fn) {
48 // Make sure we re-emit a set of the global base reg if necessary
49 GlobalBaseReg = 0;
50 return SelectionDAGISel::runOnFunction(Fn);
51 }
52
Chris Lattnera5a91b12005-08-17 19:33:03 +000053 /// getI32Imm - Return a target constant with the specified value, of type
54 /// i32.
55 inline SDOperand getI32Imm(unsigned Imm) {
56 return CurDAG->getTargetConstant(Imm, MVT::i32);
57 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000058
59 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
60 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000061 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000062
63 // Select - Convert the specified operand from a target-independent to a
64 // target-specific node if it hasn't already been changed.
65 SDOperand Select(SDOperand Op);
66
67 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
68 unsigned OCHi, unsigned OCLo,
69 bool IsArithmetic = false,
70 bool Negate = false);
Nate Begeman02b88a42005-08-19 00:38:14 +000071 SDNode *SelectBitfieldInsert(SDNode *N);
72
Chris Lattner2fbb4572005-08-21 18:50:37 +000073 /// SelectCC - Select a comparison of the specified values with the
74 /// specified condition code, returning the CR# of the expression.
75 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
76
Chris Lattner9944b762005-08-21 22:31:09 +000077 /// SelectAddr - Given the specified address, return the two operands for a
78 /// load/store instruction, and return true if it should be an indexed [r+r]
79 /// operation.
80 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
81
Chris Lattner047b9522005-08-25 22:04:30 +000082 SDOperand BuildSDIVSequence(SDNode *N);
83 SDOperand BuildUDIVSequence(SDNode *N);
84
Chris Lattnera5a91b12005-08-17 19:33:03 +000085 /// InstructionSelectBasicBlock - This callback is invoked by
86 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
87 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
88 DEBUG(BB->dump());
Chris Lattnerd607c122005-08-18 18:46:06 +000089 // Select target instructions for the DAG.
Chris Lattnerefa6abc2005-08-29 01:07:02 +000090 DAG.setRoot(Select(DAG.getRoot()));
Chris Lattnera5a91b12005-08-17 19:33:03 +000091 DAG.RemoveDeadNodes();
Chris Lattnerd607c122005-08-18 18:46:06 +000092
Chris Lattnerd607c122005-08-18 18:46:06 +000093 // Emit machine code to BB.
94 ScheduleAndEmitDAG(DAG);
Chris Lattnera5a91b12005-08-17 19:33:03 +000095 }
96
97 virtual const char *getPassName() const {
98 return "PowerPC DAG->DAG Pattern Instruction Selection";
99 }
100 };
101}
102
Chris Lattner4416f1a2005-08-19 22:38:53 +0000103/// getGlobalBaseReg - Output the instructions required to put the
104/// base address to use for accessing globals into a register.
105///
Chris Lattner9944b762005-08-21 22:31:09 +0000106SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000107 if (!GlobalBaseReg) {
108 // Insert the set of GlobalBaseReg into the first MBB of the function
109 MachineBasicBlock &FirstMBB = BB->getParent()->front();
110 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
111 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
112 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
113 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
114 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
115 }
Chris Lattner9944b762005-08-21 22:31:09 +0000116 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000117}
118
119
Nate Begeman0f3257a2005-08-18 05:00:13 +0000120// isIntImmediate - This method tests to see if a constant operand.
121// If so Imm will receive the 32 bit value.
122static bool isIntImmediate(SDNode *N, unsigned& Imm) {
123 if (N->getOpcode() == ISD::Constant) {
124 Imm = cast<ConstantSDNode>(N)->getValue();
125 return true;
126 }
127 return false;
128}
129
Nate Begemancffc32b2005-08-18 07:30:46 +0000130// isOprShiftImm - Returns true if the specified operand is a shift opcode with
131// a immediate shift count less than 32.
132static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
133 Opc = N->getOpcode();
134 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
135 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
136}
137
138// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
139// any number of 0s on either side. The 1s are allowed to wrap from LSB to
140// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
141// not, since all 1s are not contiguous.
142static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
143 if (isShiftedMask_32(Val)) {
144 // look for the first non-zero bit
145 MB = CountLeadingZeros_32(Val);
146 // look for the first zero bit after the run of ones
147 ME = CountLeadingZeros_32((Val - 1) ^ Val);
148 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000149 } else {
150 Val = ~Val; // invert mask
151 if (isShiftedMask_32(Val)) {
152 // effectively look for the first zero bit
153 ME = CountLeadingZeros_32(Val) - 1;
154 // effectively look for the first one bit after the run of zeros
155 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
156 return true;
157 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000158 }
159 // no run present
160 return false;
161}
162
163// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
164// and mask opcode and mask operation.
165static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
166 unsigned &SH, unsigned &MB, unsigned &ME) {
167 unsigned Shift = 32;
168 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
169 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000170 if (N->getNumOperands() != 2 ||
171 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000172 return false;
173
174 if (Opcode == ISD::SHL) {
175 // apply shift left to mask if it comes first
176 if (IsShiftMask) Mask = Mask << Shift;
177 // determine which bits are made indeterminant by shift
178 Indeterminant = ~(0xFFFFFFFFu << Shift);
179 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
180 // apply shift right to mask if it comes first
181 if (IsShiftMask) Mask = Mask >> Shift;
182 // determine which bits are made indeterminant by shift
183 Indeterminant = ~(0xFFFFFFFFu >> Shift);
184 // adjust for the left rotate
185 Shift = 32 - Shift;
186 } else {
187 return false;
188 }
189
190 // if the mask doesn't intersect any Indeterminant bits
191 if (Mask && !(Mask & Indeterminant)) {
192 SH = Shift;
193 // make sure the mask is still a mask (wrap arounds may not be)
194 return isRunOfOnes(Mask, MB, ME);
195 }
196 return false;
197}
198
Nate Begeman0f3257a2005-08-18 05:00:13 +0000199// isOpcWithIntImmediate - This method tests to see if the node is a specific
200// opcode and that it has a immediate integer right operand.
201// If so Imm will receive the 32 bit value.
202static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
203 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
204}
205
206// isOprNot - Returns true if the specified operand is an xor with immediate -1.
207static bool isOprNot(SDNode *N) {
208 unsigned Imm;
209 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
210}
211
Chris Lattnera5a91b12005-08-17 19:33:03 +0000212// Immediate constant composers.
213// Lo16 - grabs the lo 16 bits from a 32 bit constant.
214// Hi16 - grabs the hi 16 bits from a 32 bit constant.
215// HA16 - computes the hi bits required if the lo bits are add/subtracted in
216// arithmethically.
217static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
218static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
219static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
220
221// isIntImmediate - This method tests to see if a constant operand.
222// If so Imm will receive the 32 bit value.
223static bool isIntImmediate(SDOperand N, unsigned& Imm) {
224 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
225 Imm = (unsigned)CN->getSignExtended();
226 return true;
227 }
228 return false;
229}
230
Nate Begeman02b88a42005-08-19 00:38:14 +0000231/// SelectBitfieldInsert - turn an or of two masked values into
232/// the rotate left word immediate then mask insert (rlwimi) instruction.
233/// Returns true on success, false if the caller still needs to select OR.
234///
235/// Patterns matched:
236/// 1. or shl, and 5. or and, and
237/// 2. or and, shl 6. or shl, shr
238/// 3. or shr, and 7. or shr, shl
239/// 4. or and, shr
240SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
241 bool IsRotate = false;
242 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
243 unsigned Value;
244
245 SDOperand Op0 = N->getOperand(0);
246 SDOperand Op1 = N->getOperand(1);
247
248 unsigned Op0Opc = Op0.getOpcode();
249 unsigned Op1Opc = Op1.getOpcode();
250
251 // Verify that we have the correct opcodes
252 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
253 return false;
254 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
255 return false;
256
257 // Generate Mask value for Target
258 if (isIntImmediate(Op0.getOperand(1), Value)) {
259 switch(Op0Opc) {
260 case ISD::SHL: TgtMask <<= Value; break;
261 case ISD::SRL: TgtMask >>= Value; break;
262 case ISD::AND: TgtMask &= Value; break;
263 }
264 } else {
265 return 0;
266 }
267
268 // Generate Mask value for Insert
269 if (isIntImmediate(Op1.getOperand(1), Value)) {
270 switch(Op1Opc) {
271 case ISD::SHL:
272 SH = Value;
273 InsMask <<= SH;
274 if (Op0Opc == ISD::SRL) IsRotate = true;
275 break;
276 case ISD::SRL:
277 SH = Value;
278 InsMask >>= SH;
279 SH = 32-SH;
280 if (Op0Opc == ISD::SHL) IsRotate = true;
281 break;
282 case ISD::AND:
283 InsMask &= Value;
284 break;
285 }
286 } else {
287 return 0;
288 }
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;
646 case ISD::TokenFactor: {
647 SDOperand New;
648 if (N->getNumOperands() == 2) {
649 SDOperand Op0 = Select(N->getOperand(0));
650 SDOperand Op1 = Select(N->getOperand(1));
651 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
652 } else {
653 std::vector<SDOperand> Ops;
654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000655 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000656 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
657 }
658
659 if (New.Val != N) {
Chris Lattner52987f42005-08-26 18:37:23 +0000660 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000661 N = New.Val;
662 }
663 break;
664 }
665 case ISD::CopyFromReg: {
666 SDOperand Chain = Select(N->getOperand(0));
667 if (Chain == N->getOperand(0)) return Op; // No change
668 SDOperand New = CurDAG->getCopyFromReg(Chain,
669 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
670 return New.getValue(Op.ResNo);
671 }
672 case ISD::CopyToReg: {
673 SDOperand Chain = Select(N->getOperand(0));
674 SDOperand Reg = N->getOperand(1);
675 SDOperand Val = Select(N->getOperand(2));
676 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
677 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
678 Chain, Reg, Val);
Chris Lattner52987f42005-08-26 18:37:23 +0000679 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000680 N = New.Val;
681 }
682 break;
683 }
684 case ISD::Constant: {
685 assert(N->getValueType(0) == MVT::i32);
686 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000687 unsigned Hi = HA16(v);
688 unsigned Lo = Lo16(v);
Chris Lattner2fef8092005-08-29 01:01:01 +0000689
690 // NOTE: This doesn't use SelectNodeTo, because doing that will prevent
691 // folding shared immediates into other the second instruction that
692 // uses it.
Nate Begemana6940472005-08-18 18:01:39 +0000693 if (Hi && Lo) {
694 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
695 getI32Imm(v >> 16));
Chris Lattner2fef8092005-08-29 01:01:01 +0000696 return CurDAG->getTargetNode(PPC::ORI, MVT::i32, Top,
697 getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000698 } else if (Lo) {
Chris Lattner2fef8092005-08-29 01:01:01 +0000699 return CurDAG->getTargetNode(PPC::LI, MVT::i32, getI32Imm(v));
Nate Begemana6940472005-08-18 18:01:39 +0000700 } else {
Chris Lattner2fef8092005-08-29 01:01:01 +0000701 return CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000702 }
703 }
Chris Lattner2b544002005-08-24 23:08:16 +0000704 case ISD::UNDEF:
705 if (N->getValueType(0) == MVT::i32)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000706 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattner2b544002005-08-24 23:08:16 +0000707 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000708 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0));
Chris Lattner2b544002005-08-24 23:08:16 +0000709 break;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000710 case ISD::FrameIndex: {
711 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000712 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere28e40a2005-08-25 00:45:43 +0000713 CurDAG->getTargetFrameIndex(FI, MVT::i32),
714 getI32Imm(0));
715 break;
716 }
Chris Lattner34e17052005-08-25 05:04:11 +0000717 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000718 Constant *C = cast<ConstantPoolSDNode>(N)->get();
719 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner34e17052005-08-25 05:04:11 +0000720 if (PICEnabled)
721 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
722 else
723 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000724 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner34e17052005-08-25 05:04:11 +0000725 break;
726 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000727 case ISD::GlobalAddress: {
728 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
729 SDOperand Tmp;
730 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000731 if (PICEnabled)
732 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
733 else
Chris Lattner4416f1a2005-08-19 22:38:53 +0000734 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +0000735
Chris Lattner4416f1a2005-08-19 22:38:53 +0000736 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000737 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000738 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000739 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000740 break;
741 }
Chris Lattner9c2dece2005-08-29 23:30:11 +0000742 case ISD::DYNAMIC_STACKALLOC: {
743 // FIXME: We are currently ignoring the requested alignment for handling
744 // greater than the stack alignment. This will need to be revisited at some
745 // point. Align = N.getOperand(2);
746 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
747 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
748 std::cerr << "Cannot allocate stack object with greater alignment than"
749 << " the stack alignment yet!";
750 abort();
751 }
752 SDOperand Chain = Select(N->getOperand(0));
753 SDOperand Amt = Select(N->getOperand(1));
754
755 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
756
757 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
758 // from the stack pointer, giving us the result pointer.
759 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Reg);
760
761 // Copy this result back into R1.
762 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
763
764 // Copy this result back out of R1 to make sure we're not using the stack
765 // space without decrementing the stack pointer.
766 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
767
768 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
769 CurDAG->ReplaceAllUsesWith(N, Result.Val);
770 N = Result.Val;
771 break;
772 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000773 case ISD::SIGN_EXTEND_INREG:
774 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
775 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
776 case MVT::i16:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000777 CurDAG->SelectNodeTo(N, PPC::EXTSH, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000778 break;
779 case MVT::i8:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000780 CurDAG->SelectNodeTo(N, PPC::EXTSB, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000781 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000782 }
783 break;
784 case ISD::CTLZ:
785 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000786 CurDAG->SelectNodeTo(N, PPC::CNTLZW, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000787 break;
Chris Lattner0bbea952005-08-26 20:25:03 +0000788 case PPCISD::FSEL:
789 CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0),
790 Select(N->getOperand(0)),
791 Select(N->getOperand(1)),
792 Select(N->getOperand(2)));
793 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000794 case ISD::ADD: {
795 MVT::ValueType Ty = N->getValueType(0);
796 if (Ty == MVT::i32) {
797 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
798 PPC::ADDIS, PPC::ADDI, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000799 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000800 N = I;
801 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000802 CurDAG->SelectNodeTo(N, PPC::ADD, MVT::i32, Select(N->getOperand(0)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000803 Select(N->getOperand(1)));
804 }
805 break;
806 }
807
808 if (!NoExcessFPPrecision) { // Match FMA ops
809 if (N->getOperand(0).getOpcode() == ISD::MUL &&
810 N->getOperand(0).Val->hasOneUse()) {
811 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000812 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000813 Select(N->getOperand(0).getOperand(0)),
814 Select(N->getOperand(0).getOperand(1)),
815 Select(N->getOperand(1)));
816 break;
817 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
818 N->getOperand(1).hasOneUse()) {
819 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000820 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000821 Select(N->getOperand(1).getOperand(0)),
822 Select(N->getOperand(1).getOperand(1)),
823 Select(N->getOperand(0)));
824 break;
825 }
826 }
827
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000828 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000829 Select(N->getOperand(0)), Select(N->getOperand(1)));
830 break;
831 }
832 case ISD::SUB: {
833 MVT::ValueType Ty = N->getValueType(0);
834 if (Ty == MVT::i32) {
835 unsigned Imm;
836 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
Nate Begemanc6b07172005-08-24 05:03:20 +0000837 if (0 == Imm)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000838 CurDAG->SelectNodeTo(N, PPC::NEG, Ty, Select(N->getOperand(1)));
Nate Begemanc6b07172005-08-24 05:03:20 +0000839 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000840 CurDAG->SelectNodeTo(N, PPC::SUBFIC, Ty, Select(N->getOperand(1)),
Nate Begemanc6b07172005-08-24 05:03:20 +0000841 getI32Imm(Lo16(Imm)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000842 break;
843 }
844 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
845 PPC::ADDIS, PPC::ADDI, true, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000846 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000847 N = I;
848 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000849 CurDAG->SelectNodeTo(N, PPC::SUBF, Ty, Select(N->getOperand(1)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000850 Select(N->getOperand(0)));
851 }
852 break;
853 }
854
855 if (!NoExcessFPPrecision) { // Match FMA ops
856 if (N->getOperand(0).getOpcode() == ISD::MUL &&
857 N->getOperand(0).Val->hasOneUse()) {
858 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000859 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000860 Select(N->getOperand(0).getOperand(0)),
861 Select(N->getOperand(0).getOperand(1)),
862 Select(N->getOperand(1)));
863 break;
864 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
865 N->getOperand(1).Val->hasOneUse()) {
866 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000867 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000868 Select(N->getOperand(1).getOperand(0)),
869 Select(N->getOperand(1).getOperand(1)),
870 Select(N->getOperand(0)));
871 break;
872 }
873 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000874 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000875 Select(N->getOperand(0)),
876 Select(N->getOperand(1)));
877 break;
Nate Begeman26653502005-08-17 23:46:35 +0000878 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000879 case ISD::MUL: {
880 unsigned Imm, Opc;
881 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000882 CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32,
Nate Begemanb5a06682005-08-18 00:21:41 +0000883 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
884 break;
885 }
886 switch (N->getValueType(0)) {
887 default: assert(0 && "Unhandled multiply type!");
888 case MVT::i32: Opc = PPC::MULLW; break;
889 case MVT::f32: Opc = PPC::FMULS; break;
890 case MVT::f64: Opc = PPC::FMUL; break;
891 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000892 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Nate Begemanb5a06682005-08-18 00:21:41 +0000893 Select(N->getOperand(1)));
894 break;
895 }
Chris Lattner8784a232005-08-25 17:50:06 +0000896 case ISD::SDIV: {
897 unsigned Imm;
898 if (isIntImmediate(N->getOperand(1), Imm)) {
899 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
900 SDOperand Op =
901 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
902 Select(N->getOperand(0)),
903 getI32Imm(Log2_32(Imm)));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000904 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner8784a232005-08-25 17:50:06 +0000905 Op.getValue(0), Op.getValue(1));
906 break;
907 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
908 SDOperand Op =
909 CurDAG->getTargetNode(PPC::SRAWI, MVT::Flag, MVT::i32,
910 Select(N->getOperand(0)),
911 getI32Imm(Log2_32(-Imm)));
912 SDOperand PT =
913 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(1),
914 Op.getValue(0));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000915 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000916 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000917 } else if (Imm) {
918 SDOperand Result = Select(BuildSDIVSequence(N));
919 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000920 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000921 N = Result.Val;
922 break;
Chris Lattner8784a232005-08-25 17:50:06 +0000923 }
924 }
Chris Lattner047b9522005-08-25 22:04:30 +0000925
926 unsigned Opc;
927 switch (N->getValueType(0)) {
Chris Lattner95e06822005-08-26 16:38:51 +0000928 default: assert(0 && "Unknown type to ISD::SDIV");
Chris Lattner047b9522005-08-25 22:04:30 +0000929 case MVT::i32: Opc = PPC::DIVW; break;
930 case MVT::f32: Opc = PPC::FDIVS; break;
931 case MVT::f64: Opc = PPC::FDIV; break;
932 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000933 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000934 Select(N->getOperand(1)));
935 break;
936 }
937 case ISD::UDIV: {
938 // If this is a divide by constant, we can emit code using some magic
939 // constants to implement it as a multiply instead.
940 unsigned Imm;
Chris Lattnera9317ed2005-08-25 23:21:06 +0000941 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner047b9522005-08-25 22:04:30 +0000942 SDOperand Result = Select(BuildUDIVSequence(N));
943 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000944 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000945 N = Result.Val;
946 break;
947 }
948
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000949 CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000950 Select(N->getOperand(1)));
951 break;
952 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000953 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000954 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000955 CurDAG->SelectNodeTo(N, PPC::MULHW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000956 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000957 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000958 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000959 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000960 CurDAG->SelectNodeTo(N, PPC::MULHWU, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000961 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000962 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000963 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000964 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000965 // If this is an and of a value rotated between 0 and 31 bits and then and'd
966 // with a mask, emit rlwinm
967 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
968 isShiftedMask_32(~Imm))) {
969 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000970 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000971 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
972 Val = Select(N->getOperand(0).getOperand(0));
973 } else {
974 Val = Select(N->getOperand(0));
975 isRunOfOnes(Imm, MB, ME);
976 SH = 0;
977 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000978 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemancffc32b2005-08-18 07:30:46 +0000979 getI32Imm(MB), getI32Imm(ME));
980 break;
981 }
982 // If this is an and with an immediate that isn't a mask, then codegen it as
983 // high and low 16 bit immediate ands.
984 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
985 N->getOperand(1),
986 PPC::ANDISo, PPC::ANDIo)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000987 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000988 N = I;
989 break;
990 }
991 // Finally, check for the case where we are being asked to select
992 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
993 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000994 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)),
Nate Begemancffc32b2005-08-18 07:30:46 +0000995 Select(N->getOperand(0).getOperand(0)));
996 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000997 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +0000998 Select(N->getOperand(1).getOperand(0)));
999 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001000 CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +00001001 Select(N->getOperand(1)));
1002 break;
1003 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001004 case ISD::OR:
1005 if (SDNode *I = SelectBitfieldInsert(N)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001006 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001007 N = I;
1008 break;
1009 }
1010 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1011 N->getOperand(1),
1012 PPC::ORIS, PPC::ORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001013 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001014 N = I;
1015 break;
1016 }
1017 // Finally, check for the case where we are being asked to select
1018 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
1019 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001020 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001021 Select(N->getOperand(0).getOperand(0)));
1022 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001023 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001024 Select(N->getOperand(1).getOperand(0)));
1025 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001026 CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001027 Select(N->getOperand(1)));
1028 break;
Nate Begeman0f3257a2005-08-18 05:00:13 +00001029 case ISD::XOR:
1030 // Check whether or not this node is a logical 'not'. This is represented
1031 // by llvm as a xor with the constant value -1 (all bits set). If this is a
1032 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
1033 if (isOprNot(N)) {
1034 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +00001035 SDOperand Val = Select(N->getOperand(0));
Chris Lattner528f58e2005-08-28 23:39:22 +00001036 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman0f3257a2005-08-18 05:00:13 +00001037 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +00001038 case PPC::OR: Opc = PPC::NOR; break;
1039 case PPC::AND: Opc = PPC::NAND; break;
1040 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +00001041 }
1042 if (Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001043 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Val.getOperand(0),
Nate Begeman131a8802005-08-18 05:44:50 +00001044 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001045 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001046 CurDAG->SelectNodeTo(N, PPC::NOR, MVT::i32, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +00001047 break;
1048 }
1049 // If this is a xor with an immediate other than -1, then codegen it as high
1050 // and low 16 bit immediate xors.
1051 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1052 N->getOperand(1),
1053 PPC::XORIS, PPC::XORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001054 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001055 N = I;
1056 break;
1057 }
1058 // Finally, check for the case where we are being asked to select
1059 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
1060 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001061 CurDAG->SelectNodeTo(N, PPC::EQV, MVT::i32,
Nate Begeman0f3257a2005-08-18 05:00:13 +00001062 Select(N->getOperand(0).getOperand(0)),
1063 Select(N->getOperand(1)));
1064 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001065 CurDAG->SelectNodeTo(N, PPC::XOR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman0f3257a2005-08-18 05:00:13 +00001066 Select(N->getOperand(1)));
1067 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001068 case ISD::SHL: {
1069 unsigned Imm, SH, MB, ME;
1070 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1071 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001072 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001073 Select(N->getOperand(0).getOperand(0)),
1074 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1075 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001076 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001077 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
1078 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001079 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001080 Select(N->getOperand(1)));
1081 break;
1082 }
1083 case ISD::SRL: {
1084 unsigned Imm, SH, MB, ME;
1085 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1086 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001087 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001088 Select(N->getOperand(0).getOperand(0)),
1089 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1090 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001091 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001092 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
1093 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001094 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001095 Select(N->getOperand(1)));
1096 break;
1097 }
1098 case ISD::SRA: {
1099 unsigned Imm, SH, MB, ME;
1100 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1101 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001102 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001103 Select(N->getOperand(0).getOperand(0)),
1104 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1105 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001106 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001107 getI32Imm(Imm));
1108 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001109 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001110 Select(N->getOperand(1)));
1111 break;
1112 }
Nate Begeman305a1c72005-08-18 03:04:18 +00001113 case ISD::FABS:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001114 CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0),
Nate Begeman6a7d6112005-08-18 00:53:47 +00001115 Select(N->getOperand(0)));
1116 break;
Chris Lattner8f838722005-08-30 00:30:43 +00001117 case ISD::FP_EXTEND:
Nate Begeman305a1c72005-08-18 03:04:18 +00001118 assert(MVT::f64 == N->getValueType(0) &&
1119 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner8f838722005-08-30 00:30:43 +00001120 // We need to emit an FMR to make sure that the result has the right value
1121 // type.
1122 CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0)));
1123 break;
Nate Begeman305a1c72005-08-18 03:04:18 +00001124 case ISD::FP_ROUND:
1125 assert(MVT::f32 == N->getValueType(0) &&
1126 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001127 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +00001128 break;
Chris Lattnerc8a89a12005-08-28 23:59:09 +00001129 case ISD::FP_TO_SINT: {
1130 SDOperand In = Select(N->getOperand(0));
1131 In = CurDAG->getTargetNode(PPC::FCTIWZ, MVT::f64, In);
1132
1133 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1134 SDOperand FI = CurDAG->getTargetFrameIndex(FrameIdx, MVT::f64);
Chris Lattner9c2dece2005-08-29 23:30:11 +00001135 SDOperand ST = CurDAG->getTargetNode(PPC::STFD, MVT::Other, In,
1136 getI32Imm(0), FI);
1137 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, MVT::Other,
1138 getI32Imm(4), FI, ST);
Chris Lattnerc8a89a12005-08-28 23:59:09 +00001139 break;
1140 }
Nate Begeman26653502005-08-17 23:46:35 +00001141 case ISD::FNEG: {
1142 SDOperand Val = Select(N->getOperand(0));
1143 MVT::ValueType Ty = N->getValueType(0);
1144 if (Val.Val->hasOneUse()) {
1145 unsigned Opc;
Chris Lattner528f58e2005-08-28 23:39:22 +00001146 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman26653502005-08-17 23:46:35 +00001147 default: Opc = 0; break;
1148 case PPC::FABS: Opc = PPC::FNABS; break;
1149 case PPC::FMADD: Opc = PPC::FNMADD; break;
1150 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1151 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1152 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1153 }
1154 // If we inverted the opcode, then emit the new instruction with the
1155 // inverted opcode and the original instruction's operands. Otherwise,
1156 // fall through and generate a fneg instruction.
1157 if (Opc) {
1158 if (PPC::FNABS == Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001159 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman26653502005-08-17 23:46:35 +00001160 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001161 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman26653502005-08-17 23:46:35 +00001162 Val.getOperand(1), Val.getOperand(2));
1163 break;
1164 }
1165 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001166 CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val);
Nate Begeman26653502005-08-17 23:46:35 +00001167 break;
1168 }
Nate Begeman6a7d6112005-08-18 00:53:47 +00001169 case ISD::FSQRT: {
1170 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001171 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman6a7d6112005-08-18 00:53:47 +00001172 Select(N->getOperand(0)));
1173 break;
1174 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001175
1176 case ISD::ADD_PARTS: {
1177 SDOperand LHSL = Select(N->getOperand(0));
1178 SDOperand LHSH = Select(N->getOperand(1));
1179
1180 unsigned Imm;
Chris Lattner95e06822005-08-26 16:38:51 +00001181 bool ME = false, ZE = false;
Chris Lattnera9317ed2005-08-25 23:21:06 +00001182 if (isIntImmediate(N->getOperand(3), Imm)) {
1183 ME = (signed)Imm == -1;
1184 ZE = Imm == 0;
1185 }
1186
1187 std::vector<SDOperand> Result;
1188 SDOperand CarryFromLo;
1189 if (isIntImmediate(N->getOperand(2), Imm) &&
1190 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
1191 // Codegen the low 32 bits of the add. Interestingly, there is no
1192 // shifted form of add immediate carrying.
1193 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1194 LHSL, getI32Imm(Imm));
1195 } else {
1196 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
1197 LHSL, Select(N->getOperand(2)));
1198 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001199 CarryFromLo = CarryFromLo.getValue(1);
1200
1201 // Codegen the high 32 bits, adding zero, minus one, or the full value
1202 // along with the carry flag produced by addc/addic.
1203 SDOperand ResultHi;
1204 if (ZE)
1205 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
1206 else if (ME)
1207 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
1208 else
1209 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
1210 Select(N->getOperand(3)), CarryFromLo);
1211 Result.push_back(ResultHi);
Chris Lattnerb20c3182005-08-25 23:36:49 +00001212 Result.push_back(CarryFromLo.getValue(0));
Chris Lattnera9317ed2005-08-25 23:21:06 +00001213 CurDAG->ReplaceAllUsesWith(N, Result);
1214 return Result[Op.ResNo];
1215 }
1216 case ISD::SUB_PARTS: {
1217 SDOperand LHSL = Select(N->getOperand(0));
1218 SDOperand LHSH = Select(N->getOperand(1));
1219 SDOperand RHSL = Select(N->getOperand(2));
1220 SDOperand RHSH = Select(N->getOperand(3));
1221
1222 std::vector<SDOperand> Result;
1223 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
1224 RHSL, LHSL));
1225 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
1226 Result[0].getValue(1)));
1227 CurDAG->ReplaceAllUsesWith(N, Result);
1228 return Result[Op.ResNo];
1229 }
Nate Begeman6660cd62005-08-26 00:28:00 +00001230 case ISD::SHL_PARTS: {
1231 SDOperand HI = Select(N->getOperand(0));
1232 SDOperand LO = Select(N->getOperand(1));
1233 SDOperand SH = Select(N->getOperand(2));
Nate Begemanbb22df32005-08-26 00:34:06 +00001234 SDOperand SH_LO_R = CurDAG->getTargetNode(PPC::SUBFIC, MVT::i32, MVT::Flag,
1235 SH, getI32Imm(32));
Nate Begeman6660cd62005-08-26 00:28:00 +00001236 SDOperand SH_LO_L = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, SH,
1237 getI32Imm((unsigned)-32));
1238 SDOperand HI_SHL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, HI, SH);
1239 SDOperand HI_LOR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, LO, SH_LO_R);
1240 SDOperand HI_LOL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, LO, SH_LO_L);
1241 SDOperand HI_OR = CurDAG->getTargetNode(PPC::OR, MVT::i32, HI_SHL, HI_LOR);
1242
1243 std::vector<SDOperand> Result;
1244 Result.push_back(CurDAG->getTargetNode(PPC::SLW, MVT::i32, LO, SH));
1245 Result.push_back(CurDAG->getTargetNode(PPC::OR, MVT::i32, HI_OR, HI_LOL));
1246 CurDAG->ReplaceAllUsesWith(N, Result);
1247 return Result[Op.ResNo];
1248 }
1249 case ISD::SRL_PARTS: {
1250 SDOperand HI = Select(N->getOperand(0));
1251 SDOperand LO = Select(N->getOperand(1));
1252 SDOperand SH = Select(N->getOperand(2));
Nate Begemanbb22df32005-08-26 00:34:06 +00001253 SDOperand SH_HI_L = CurDAG->getTargetNode(PPC::SUBFIC, MVT::i32, MVT::Flag,
1254 SH, getI32Imm(32));
Nate Begeman6660cd62005-08-26 00:28:00 +00001255 SDOperand SH_HI_R = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, SH,
1256 getI32Imm((unsigned)-32));
1257 SDOperand LO_SHR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, LO, SH);
1258 SDOperand LO_HIL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, HI, SH_HI_L);
1259 SDOperand LO_HIR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, HI, SH_HI_R);
1260 SDOperand LO_OR = CurDAG->getTargetNode(PPC::OR, MVT::i32, LO_SHR, LO_HIL);
1261
1262 std::vector<SDOperand> Result;
1263 Result.push_back(CurDAG->getTargetNode(PPC::OR, MVT::i32, LO_OR, LO_HIR));
1264 Result.push_back(CurDAG->getTargetNode(PPC::SRW, MVT::i32, HI, SH));
1265 CurDAG->ReplaceAllUsesWith(N, Result);
1266 return Result[Op.ResNo];
1267 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001268
Chris Lattner9944b762005-08-21 22:31:09 +00001269 case ISD::LOAD:
1270 case ISD::EXTLOAD:
1271 case ISD::ZEXTLOAD:
1272 case ISD::SEXTLOAD: {
1273 SDOperand Op1, Op2;
1274 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1275
1276 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1277 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1278 unsigned Opc;
1279 switch (TypeBeingLoaded) {
1280 default: N->dump(); assert(0 && "Cannot load this type!");
1281 case MVT::i1:
1282 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1283 case MVT::i16:
1284 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1285 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1286 } else {
1287 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1288 }
1289 break;
1290 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1291 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1292 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1293 }
1294
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001295 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattner9944b762005-08-21 22:31:09 +00001296 Op1, Op2, Select(N->getOperand(0)));
1297 break;
1298 }
1299
Chris Lattnerf7f22552005-08-22 01:27:59 +00001300 case ISD::TRUNCSTORE:
1301 case ISD::STORE: {
1302 SDOperand AddrOp1, AddrOp2;
1303 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1304
1305 unsigned Opc;
1306 if (N->getOpcode() == ISD::STORE) {
1307 switch (N->getOperand(1).getValueType()) {
1308 default: assert(0 && "unknown Type in store");
1309 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1310 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1311 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1312 }
1313 } else { //ISD::TRUNCSTORE
1314 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1315 default: assert(0 && "unknown Type in store");
1316 case MVT::i1:
1317 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1318 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1319 }
1320 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001321
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001322 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerf7f22552005-08-22 01:27:59 +00001323 AddrOp1, AddrOp2, Select(N->getOperand(0)));
1324 break;
1325 }
Chris Lattner64906a02005-08-25 20:08:18 +00001326
1327 case ISD::SETCC: {
1328 unsigned Imm;
1329 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1330 if (isIntImmediate(N->getOperand(1), Imm)) {
1331 // We can codegen setcc op, imm very efficiently compared to a brcond.
1332 // Check for those cases here.
1333 // setcc op, 0
1334 if (Imm == 0) {
1335 SDOperand Op = Select(N->getOperand(0));
1336 switch (CC) {
1337 default: assert(0 && "Unhandled SetCC condition"); abort();
1338 case ISD::SETEQ:
1339 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001340 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattner64906a02005-08-25 20:08:18 +00001341 getI32Imm(5), getI32Imm(31));
1342 break;
1343 case ISD::SETNE: {
1344 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1345 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001346 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001347 break;
1348 }
1349 case ISD::SETLT:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001350 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001351 getI32Imm(31), getI32Imm(31));
1352 break;
1353 case ISD::SETGT: {
1354 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
1355 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001356 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001357 getI32Imm(31), getI32Imm(31));
1358 break;
1359 }
1360 }
1361 break;
1362 } else if (Imm == ~0U) { // setcc op, -1
1363 SDOperand Op = Select(N->getOperand(0));
1364 switch (CC) {
1365 default: assert(0 && "Unhandled SetCC condition"); abort();
1366 case ISD::SETEQ:
1367 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1368 Op, getI32Imm(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001369 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner64906a02005-08-25 20:08:18 +00001370 CurDAG->getTargetNode(PPC::LI, MVT::i32,
1371 getI32Imm(0)),
1372 Op.getValue(1));
1373 break;
1374 case ISD::SETNE: {
1375 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
Chris Lattner8bbcc202005-08-29 23:49:25 +00001376 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1377 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001378 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001379 break;
1380 }
1381 case ISD::SETLT: {
1382 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
1383 getI32Imm(1));
1384 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001385 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001386 getI32Imm(31), getI32Imm(31));
1387 break;
1388 }
1389 case ISD::SETGT:
1390 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
1391 getI32Imm(31), getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001392 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001393 break;
1394 }
1395 break;
1396 }
1397 }
1398
1399 bool Inv;
1400 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1401 SDOperand CCReg =
1402 SelectCC(Select(N->getOperand(0)), Select(N->getOperand(1)), CC);
1403 SDOperand IntCR;
Chris Lattner957fcfb2005-08-25 21:39:42 +00001404
1405 // Force the ccreg into CR7.
1406 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
1407
1408 std::vector<MVT::ValueType> VTs;
1409 VTs.push_back(MVT::Other);
1410 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
1411 std::vector<SDOperand> Ops;
1412 Ops.push_back(CurDAG->getEntryNode());
1413 Ops.push_back(CR7Reg);
1414 Ops.push_back(CCReg);
1415 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
1416
1417 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1418 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
1419 else
1420 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
Chris Lattner64906a02005-08-25 20:08:18 +00001421
1422 if (!Inv) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001423 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner64906a02005-08-25 20:08:18 +00001424 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
1425 } else {
1426 SDOperand Tmp =
1427 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
1428 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001429 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001430 }
1431
1432 break;
1433 }
Chris Lattnera2590c52005-08-24 00:47:15 +00001434
Chris Lattner13794f52005-08-26 18:46:49 +00001435 case ISD::SELECT_CC: {
1436 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1437
1438 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1439 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1440 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1441 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1442 if (N1C->isNullValue() && N3C->isNullValue() &&
1443 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1444 SDOperand LHS = Select(N->getOperand(0));
1445 SDOperand Tmp =
1446 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1447 LHS, getI32Imm(~0U));
1448 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1449 Tmp.getValue(1));
1450 break;
1451 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001452
1453 SDOperand CCReg = SelectCC(Select(N->getOperand(0)),
1454 Select(N->getOperand(1)), CC);
1455 unsigned BROpc = getBCCForSetCC(CC);
1456
1457 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
1458 unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int;
1459 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1460 Select(N->getOperand(2)), Select(N->getOperand(3)),
1461 getI32Imm(BROpc));
1462 break;
Chris Lattner13794f52005-08-26 18:46:49 +00001463 }
1464
Chris Lattnera2590c52005-08-24 00:47:15 +00001465 case ISD::CALLSEQ_START:
1466 case ISD::CALLSEQ_END: {
1467 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1468 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1469 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001470 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001471 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattnera2590c52005-08-24 00:47:15 +00001472 break;
1473 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001474 case ISD::CALL:
1475 case ISD::TAILCALL: {
1476 SDOperand Chain = Select(N->getOperand(0));
1477
1478 unsigned CallOpcode;
1479 std::vector<SDOperand> CallOperands;
1480
1481 if (GlobalAddressSDNode *GASD =
1482 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
1483 CallOpcode = PPC::CALLpcrel;
1484 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
1485 MVT::i32));
1486 } else if (ExternalSymbolSDNode *ESSDN =
1487 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
1488 CallOpcode = PPC::CALLpcrel;
1489 CallOperands.push_back(N->getOperand(1));
1490 } else {
1491 // Copy the callee address into the CTR register.
1492 SDOperand Callee = Select(N->getOperand(1));
1493 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
1494
1495 // Copy the callee address into R12 on darwin.
1496 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
Chris Lattner2a06a5e2005-08-29 00:26:57 +00001497 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001498
1499 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
1500 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
1501 CallOperands.push_back(R12);
1502 CallOpcode = PPC::CALLindirect;
1503 }
1504
1505 unsigned GPR_idx = 0, FPR_idx = 0;
1506 static const unsigned GPR[] = {
1507 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1508 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1509 };
1510 static const unsigned FPR[] = {
1511 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1512 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1513 };
1514
Chris Lattner7107c102005-08-29 22:22:57 +00001515 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1516 unsigned DestReg = 0;
1517 MVT::ValueType RegTy;
1518 if (N->getOperand(i).getValueType() == MVT::i32) {
1519 assert(GPR_idx < 8 && "Too many int args");
1520 DestReg = GPR[GPR_idx++];
1521 RegTy = MVT::i32;
1522 } else {
1523 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
1524 "Unpromoted integer arg?");
1525 assert(FPR_idx < 13 && "Too many fp args");
1526 DestReg = FPR[FPR_idx++];
1527 RegTy = MVT::f64; // Even if this is really f32!
1528 }
1529
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001530 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001531 SDOperand Reg = CurDAG->getRegister(DestReg, RegTy);
1532 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg,
1533 Select(N->getOperand(i)));
1534 CallOperands.push_back(Reg);
1535 }
Chris Lattner7107c102005-08-29 22:22:57 +00001536 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001537
1538 // Finally, once everything is in registers to pass to the call, emit the
1539 // call itself.
1540 CallOperands.push_back(Chain);
1541 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, CallOperands);
1542
1543 std::vector<SDOperand> CallResults;
1544
1545 // If the call has results, copy the values out of the ret val registers.
1546 switch (N->getValueType(0)) {
1547 default: assert(0 && "Unexpected ret value!");
1548 case MVT::Other: break;
1549 case MVT::i32:
1550 if (N->getValueType(1) == MVT::i32) {
1551 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32).getValue(1);
1552 CallResults.push_back(Chain.getValue(0));
1553 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1554 CallResults.push_back(Chain.getValue(0));
1555 } else {
1556 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1557 CallResults.push_back(Chain.getValue(0));
1558 }
1559 break;
1560 case MVT::f32:
1561 case MVT::f64:
1562 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, MVT::f64).getValue(1);
Chris Lattner8f838722005-08-30 00:30:43 +00001563 if (N->getValueType(0) == MVT::f64)
1564 CallResults.push_back(Chain.getValue(0));
1565 else
1566 // Insert an FMR to convert the result to f32 from f64.
1567 CallResults.push_back(CurDAG->getTargetNode(PPC::FMR, MVT::f32,
1568 Chain.getValue(0)));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001569 break;
1570 }
1571
1572 CallResults.push_back(Chain);
1573 CurDAG->ReplaceAllUsesWith(N, CallResults);
1574 return CallResults[Op.ResNo];
1575 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001576 case ISD::RET: {
1577 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1578
1579 if (N->getNumOperands() > 1) {
1580 SDOperand Val = Select(N->getOperand(1));
1581 switch (N->getOperand(1).getValueType()) {
1582 default: assert(0 && "Unknown return type!");
Chris Lattnera5a91b12005-08-17 19:33:03 +00001583 case MVT::f32:
Chris Lattner8f838722005-08-30 00:30:43 +00001584 // Insert a copy to get the type right.
1585 Val = CurDAG->getTargetNode(PPC::FMR, MVT::f64, Val);
1586 // FALL THROUGH
1587 case MVT::f64:
Chris Lattnera5a91b12005-08-17 19:33:03 +00001588 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
1589 break;
1590 case MVT::i32:
1591 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
1592 break;
1593 }
1594
1595 if (N->getNumOperands() > 2) {
1596 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1597 N->getOperand(2).getValueType() == MVT::i32 &&
Chris Lattnera9317ed2005-08-25 23:21:06 +00001598 N->getNumOperands() == 3 && "Unknown two-register ret value!");
Chris Lattnera5a91b12005-08-17 19:33:03 +00001599 Val = Select(N->getOperand(2));
1600 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
1601 }
1602 }
1603
1604 // Finally, select this to a blr (return) instruction.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001605 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001606 break;
1607 }
Chris Lattner89532c72005-08-25 00:29:58 +00001608 case ISD::BR:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001609 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner89532c72005-08-25 00:29:58 +00001610 Select(N->getOperand(0)));
1611 break;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001612 case ISD::BR_CC:
1613 case ISD::BRTWOWAY_CC: {
1614 SDOperand Chain = Select(N->getOperand(0));
1615 MachineBasicBlock *Dest =
1616 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1617 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1618 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1619 unsigned Opc = getBCCForSetCC(CC);
1620
1621 // If this is a two way branch, then grab the fallthrough basic block
1622 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1623 // conversion if necessary by the branch selection pass. Otherwise, emit a
1624 // standard conditional branch.
1625 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
1626 MachineBasicBlock *Fallthrough =
1627 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
1628 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1629 CondCode, getI32Imm(Opc),
1630 N->getOperand(4), N->getOperand(5),
1631 Chain);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001632 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001633 } else {
1634 // Iterate to the next basic block
1635 ilist<MachineBasicBlock>::iterator It = BB;
1636 ++It;
1637
1638 // If the fallthrough path is off the end of the function, which would be
1639 // undefined behavior, set it to be the same as the current block because
1640 // we have nothing better to set it to, and leaving it alone will cause
1641 // the PowerPC Branch Selection pass to crash.
1642 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001643 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001644 getI32Imm(Opc), N->getOperand(4),
1645 CurDAG->getBasicBlock(It), Chain);
1646 }
1647 break;
1648 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001649 }
Chris Lattnerddf3e7d2005-08-22 00:59:14 +00001650 return SDOperand(N, Op.ResNo);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001651}
1652
1653
1654/// createPPC32ISelDag - This pass converts a legalized DAG into a
1655/// PowerPC-specific DAG, ready for instruction scheduling.
1656///
1657FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1658 return new PPC32DAGToDAGISel(TM);
1659}
1660