blob: a0af76570a9bd8d8146397b70121414cc1b4edbf [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();
170 if (!isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
171 return false;
172
173 if (Opcode == ISD::SHL) {
174 // apply shift left to mask if it comes first
175 if (IsShiftMask) Mask = Mask << Shift;
176 // determine which bits are made indeterminant by shift
177 Indeterminant = ~(0xFFFFFFFFu << Shift);
178 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
179 // apply shift right to mask if it comes first
180 if (IsShiftMask) Mask = Mask >> Shift;
181 // determine which bits are made indeterminant by shift
182 Indeterminant = ~(0xFFFFFFFFu >> Shift);
183 // adjust for the left rotate
184 Shift = 32 - Shift;
185 } else {
186 return false;
187 }
188
189 // if the mask doesn't intersect any Indeterminant bits
190 if (Mask && !(Mask & Indeterminant)) {
191 SH = Shift;
192 // make sure the mask is still a mask (wrap arounds may not be)
193 return isRunOfOnes(Mask, MB, ME);
194 }
195 return false;
196}
197
Nate Begeman0f3257a2005-08-18 05:00:13 +0000198// isOpcWithIntImmediate - This method tests to see if the node is a specific
199// opcode and that it has a immediate integer right operand.
200// If so Imm will receive the 32 bit value.
201static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
202 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
203}
204
205// isOprNot - Returns true if the specified operand is an xor with immediate -1.
206static bool isOprNot(SDNode *N) {
207 unsigned Imm;
208 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
209}
210
Chris Lattnera5a91b12005-08-17 19:33:03 +0000211// Immediate constant composers.
212// Lo16 - grabs the lo 16 bits from a 32 bit constant.
213// Hi16 - grabs the hi 16 bits from a 32 bit constant.
214// HA16 - computes the hi bits required if the lo bits are add/subtracted in
215// arithmethically.
216static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
217static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
218static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
219
220// isIntImmediate - This method tests to see if a constant operand.
221// If so Imm will receive the 32 bit value.
222static bool isIntImmediate(SDOperand N, unsigned& Imm) {
223 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
224 Imm = (unsigned)CN->getSignExtended();
225 return true;
226 }
227 return false;
228}
229
Nate Begeman02b88a42005-08-19 00:38:14 +0000230/// SelectBitfieldInsert - turn an or of two masked values into
231/// the rotate left word immediate then mask insert (rlwimi) instruction.
232/// Returns true on success, false if the caller still needs to select OR.
233///
234/// Patterns matched:
235/// 1. or shl, and 5. or and, and
236/// 2. or and, shl 6. or shl, shr
237/// 3. or shr, and 7. or shr, shl
238/// 4. or and, shr
239SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
240 bool IsRotate = false;
241 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
242 unsigned Value;
243
244 SDOperand Op0 = N->getOperand(0);
245 SDOperand Op1 = N->getOperand(1);
246
247 unsigned Op0Opc = Op0.getOpcode();
248 unsigned Op1Opc = Op1.getOpcode();
249
250 // Verify that we have the correct opcodes
251 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
252 return false;
253 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
254 return false;
255
256 // Generate Mask value for Target
257 if (isIntImmediate(Op0.getOperand(1), Value)) {
258 switch(Op0Opc) {
259 case ISD::SHL: TgtMask <<= Value; break;
260 case ISD::SRL: TgtMask >>= Value; break;
261 case ISD::AND: TgtMask &= Value; break;
262 }
263 } else {
264 return 0;
265 }
266
267 // Generate Mask value for Insert
268 if (isIntImmediate(Op1.getOperand(1), Value)) {
269 switch(Op1Opc) {
270 case ISD::SHL:
271 SH = Value;
272 InsMask <<= SH;
273 if (Op0Opc == ISD::SRL) IsRotate = true;
274 break;
275 case ISD::SRL:
276 SH = Value;
277 InsMask >>= SH;
278 SH = 32-SH;
279 if (Op0Opc == ISD::SHL) IsRotate = true;
280 break;
281 case ISD::AND:
282 InsMask &= Value;
283 break;
284 }
285 } else {
286 return 0;
287 }
288
289 // If both of the inputs are ANDs and one of them has a logical shift by
290 // constant as its input, make that AND the inserted value so that we can
291 // combine the shift into the rotate part of the rlwimi instruction
292 bool IsAndWithShiftOp = false;
293 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
294 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
295 Op1.getOperand(0).getOpcode() == ISD::SRL) {
296 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
297 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
298 IsAndWithShiftOp = true;
299 }
300 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
301 Op0.getOperand(0).getOpcode() == ISD::SRL) {
302 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
303 std::swap(Op0, Op1);
304 std::swap(TgtMask, InsMask);
305 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
306 IsAndWithShiftOp = true;
307 }
308 }
309 }
310
311 // Verify that the Target mask and Insert mask together form a full word mask
312 // and that the Insert mask is a run of set bits (which implies both are runs
313 // of set bits). Given that, Select the arguments and generate the rlwimi
314 // instruction.
315 unsigned MB, ME;
316 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
317 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
318 bool Op0IsAND = Op0Opc == ISD::AND;
319 // Check for rotlwi / rotrwi here, a special case of bitfield insert
320 // where both bitfield halves are sourced from the same value.
321 if (IsRotate && fullMask &&
322 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
323 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
324 Select(N->getOperand(0).getOperand(0)),
325 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
326 return Op0.Val;
327 }
328 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
329 : Select(Op0);
330 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
331 : Select(Op1.getOperand(0));
332 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
333 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
334 return Op0.Val;
335 }
336 return 0;
337}
338
Chris Lattnera5a91b12005-08-17 19:33:03 +0000339// SelectIntImmediateExpr - Choose code for integer operations with an immediate
340// operand.
341SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
342 unsigned OCHi, unsigned OCLo,
343 bool IsArithmetic,
344 bool Negate) {
345 // Check to make sure this is a constant.
346 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
347 // Exit if not a constant.
348 if (!CN) return 0;
349 // Extract immediate.
350 unsigned C = (unsigned)CN->getValue();
351 // Negate if required (ISD::SUB).
352 if (Negate) C = -C;
353 // Get the hi and lo portions of constant.
354 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
355 unsigned Lo = Lo16(C);
356
357 // If two instructions are needed and usage indicates it would be better to
358 // load immediate into a register, bail out.
359 if (Hi && Lo && CN->use_size() > 2) return false;
360
361 // Select the first operand.
362 SDOperand Opr0 = Select(LHS);
363
364 if (Lo) // Add in the lo-part.
365 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
366 if (Hi) // Add in the hi-part.
367 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
368 return Opr0.Val;
369}
370
Chris Lattner9944b762005-08-21 22:31:09 +0000371/// SelectAddr - Given the specified address, return the two operands for a
372/// load/store instruction, and return true if it should be an indexed [r+r]
373/// operation.
374bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
375 SDOperand &Op2) {
376 unsigned imm = 0;
377 if (Addr.getOpcode() == ISD::ADD) {
378 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
379 Op1 = getI32Imm(Lo16(imm));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000380 if (FrameIndexSDNode *FI =
381 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner9944b762005-08-21 22:31:09 +0000382 ++FrameOff;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000383 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000384 } else {
385 Op2 = Select(Addr.getOperand(0));
386 }
387 return false;
388 } else {
389 Op1 = Select(Addr.getOperand(0));
390 Op2 = Select(Addr.getOperand(1));
391 return true; // [r+r]
392 }
393 }
394
395 // Now check if we're dealing with a global, and whether or not we should emit
396 // an optimized load or store for statics.
397 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
398 GlobalValue *GV = GN->getGlobal();
399 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
400 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
401 if (PICEnabled)
402 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
403 Op1);
404 else
405 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
406 return false;
407 }
Chris Lattnere28e40a2005-08-25 00:45:43 +0000408 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) {
Chris Lattner9944b762005-08-21 22:31:09 +0000409 Op1 = getI32Imm(0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000410 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000411 return false;
412 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
413 Op1 = Addr;
414 if (PICEnabled)
415 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
416 else
417 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
418 return false;
419 }
420 Op1 = getI32Imm(0);
421 Op2 = Select(Addr);
422 return false;
423}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000424
Chris Lattner2fbb4572005-08-21 18:50:37 +0000425/// SelectCC - Select a comparison of the specified values with the specified
426/// condition code, returning the CR# of the expression.
427SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
428 ISD::CondCode CC) {
429 // Always select the LHS.
430 LHS = Select(LHS);
431
432 // Use U to determine whether the SETCC immediate range is signed or not.
433 if (MVT::isInteger(LHS.getValueType())) {
434 bool U = ISD::isUnsignedIntSetCC(CC);
435 unsigned Imm;
436 if (isIntImmediate(RHS, Imm) &&
437 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
438 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
439 LHS, getI32Imm(Lo16(Imm)));
440 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
441 LHS, Select(RHS));
442 } else {
443 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS));
444 }
445}
446
447/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
448/// to Condition.
449static unsigned getBCCForSetCC(ISD::CondCode CC) {
450 switch (CC) {
451 default: assert(0 && "Unknown condition!"); abort();
452 case ISD::SETEQ: return PPC::BEQ;
453 case ISD::SETNE: return PPC::BNE;
454 case ISD::SETULT:
455 case ISD::SETLT: return PPC::BLT;
456 case ISD::SETULE:
457 case ISD::SETLE: return PPC::BLE;
458 case ISD::SETUGT:
459 case ISD::SETGT: return PPC::BGT;
460 case ISD::SETUGE:
461 case ISD::SETGE: return PPC::BGE;
462 }
463 return 0;
464}
465
Chris Lattner64906a02005-08-25 20:08:18 +0000466/// getCRIdxForSetCC - Return the index of the condition register field
467/// associated with the SetCC condition, and whether or not the field is
468/// treated as inverted. That is, lt = 0; ge = 0 inverted.
469static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
470 switch (CC) {
471 default: assert(0 && "Unknown condition!"); abort();
472 case ISD::SETULT:
473 case ISD::SETLT: Inv = false; return 0;
474 case ISD::SETUGE:
475 case ISD::SETGE: Inv = true; return 0;
476 case ISD::SETUGT:
477 case ISD::SETGT: Inv = false; return 1;
478 case ISD::SETULE:
479 case ISD::SETLE: Inv = true; return 1;
480 case ISD::SETEQ: Inv = false; return 2;
481 case ISD::SETNE: Inv = true; return 2;
482 }
483 return 0;
484}
Chris Lattner9944b762005-08-21 22:31:09 +0000485
Chris Lattner047b9522005-08-25 22:04:30 +0000486// Structure used to return the necessary information to codegen an SDIV as
487// a multiply.
488struct ms {
489 int m; // magic number
490 int s; // shift amount
491};
492
493struct mu {
494 unsigned int m; // magic number
495 int a; // add indicator
496 int s; // shift amount
497};
498
499/// magic - calculate the magic numbers required to codegen an integer sdiv as
500/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
501/// or -1.
502static struct ms magic(int d) {
503 int p;
504 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
505 const unsigned int two31 = 0x80000000U;
506 struct ms mag;
507
508 ad = abs(d);
509 t = two31 + ((unsigned int)d >> 31);
510 anc = t - 1 - t%ad; // absolute value of nc
511 p = 31; // initialize p
512 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
513 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
514 q2 = two31/ad; // initialize q2 = 2p/abs(d)
515 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
516 do {
517 p = p + 1;
518 q1 = 2*q1; // update q1 = 2p/abs(nc)
519 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
520 if (r1 >= anc) { // must be unsigned comparison
521 q1 = q1 + 1;
522 r1 = r1 - anc;
523 }
524 q2 = 2*q2; // update q2 = 2p/abs(d)
525 r2 = 2*r2; // update r2 = rem(2p/abs(d))
526 if (r2 >= ad) { // must be unsigned comparison
527 q2 = q2 + 1;
528 r2 = r2 - ad;
529 }
530 delta = ad - r2;
531 } while (q1 < delta || (q1 == delta && r1 == 0));
532
533 mag.m = q2 + 1;
534 if (d < 0) mag.m = -mag.m; // resulting magic number
535 mag.s = p - 32; // resulting shift
536 return mag;
537}
538
539/// magicu - calculate the magic numbers required to codegen an integer udiv as
540/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
541static struct mu magicu(unsigned d)
542{
543 int p;
544 unsigned int nc, delta, q1, r1, q2, r2;
545 struct mu magu;
546 magu.a = 0; // initialize "add" indicator
547 nc = - 1 - (-d)%d;
548 p = 31; // initialize p
549 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
550 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
551 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
552 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
553 do {
554 p = p + 1;
555 if (r1 >= nc - r1 ) {
556 q1 = 2*q1 + 1; // update q1
557 r1 = 2*r1 - nc; // update r1
558 }
559 else {
560 q1 = 2*q1; // update q1
561 r1 = 2*r1; // update r1
562 }
563 if (r2 + 1 >= d - r2) {
564 if (q2 >= 0x7FFFFFFF) magu.a = 1;
565 q2 = 2*q2 + 1; // update q2
566 r2 = 2*r2 + 1 - d; // update r2
567 }
568 else {
569 if (q2 >= 0x80000000) magu.a = 1;
570 q2 = 2*q2; // update q2
571 r2 = 2*r2 + 1; // update r2
572 }
573 delta = d - 1 - r2;
574 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
575 magu.m = q2 + 1; // resulting magic number
576 magu.s = p - 32; // resulting shift
577 return magu;
578}
579
580/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
581/// return a DAG expression to select that will generate the same value by
582/// multiplying by a magic number. See:
583/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
584SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) {
585 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue();
586 ms magics = magic(d);
587 // Multiply the numerator (operand 0) by the magic value
588 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0),
589 CurDAG->getConstant(magics.m, MVT::i32));
590 // If d > 0 and m < 0, add the numerator
591 if (d > 0 && magics.m < 0)
592 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0));
593 // If d < 0 and m > 0, subtract the numerator.
594 if (d < 0 && magics.m > 0)
595 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0));
596 // Shift right algebraic if shift value is nonzero
597 if (magics.s > 0)
598 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q,
599 CurDAG->getConstant(magics.s, MVT::i32));
600 // Extract the sign bit and add it to the quotient
601 SDOperand T =
602 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32));
603 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T);
604}
605
606/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
607/// return a DAG expression to select that will generate the same value by
608/// multiplying by a magic number. See:
609/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
610SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) {
611 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue();
612 mu magics = magicu(d);
613 // Multiply the numerator (operand 0) by the magic value
614 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0),
615 CurDAG->getConstant(magics.m, MVT::i32));
616 if (magics.a == 0) {
617 return CurDAG->getNode(ISD::SRL, MVT::i32, Q,
618 CurDAG->getConstant(magics.s, MVT::i32));
619 } else {
620 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q);
621 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
622 CurDAG->getConstant(1, MVT::i32));
623 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
624 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
625 CurDAG->getConstant(magics.s-1, MVT::i32));
626 }
627}
628
Chris Lattnera5a91b12005-08-17 19:33:03 +0000629// Select - Convert the specified operand from a target-independent to a
630// target-specific node if it hasn't already been changed.
631SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
632 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000633 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
634 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattnera5a91b12005-08-17 19:33:03 +0000635 return Op; // Already selected.
636
637 switch (N->getOpcode()) {
638 default:
639 std::cerr << "Cannot yet select: ";
640 N->dump();
641 std::cerr << "\n";
642 abort();
643 case ISD::EntryToken: // These leaves remain the same.
Chris Lattnera5a91b12005-08-17 19:33:03 +0000644 return Op;
645 case ISD::TokenFactor: {
646 SDOperand New;
647 if (N->getNumOperands() == 2) {
648 SDOperand Op0 = Select(N->getOperand(0));
649 SDOperand Op1 = Select(N->getOperand(1));
650 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
651 } else {
652 std::vector<SDOperand> Ops;
653 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000654 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000655 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
656 }
657
658 if (New.Val != N) {
Chris Lattner52987f42005-08-26 18:37:23 +0000659 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000660 N = New.Val;
661 }
662 break;
663 }
664 case ISD::CopyFromReg: {
665 SDOperand Chain = Select(N->getOperand(0));
666 if (Chain == N->getOperand(0)) return Op; // No change
667 SDOperand New = CurDAG->getCopyFromReg(Chain,
668 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
669 return New.getValue(Op.ResNo);
670 }
671 case ISD::CopyToReg: {
672 SDOperand Chain = Select(N->getOperand(0));
673 SDOperand Reg = N->getOperand(1);
674 SDOperand Val = Select(N->getOperand(2));
675 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
676 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
677 Chain, Reg, Val);
Chris Lattner52987f42005-08-26 18:37:23 +0000678 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000679 N = New.Val;
680 }
681 break;
682 }
683 case ISD::Constant: {
684 assert(N->getValueType(0) == MVT::i32);
685 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +0000686 unsigned Hi = HA16(v);
687 unsigned Lo = Lo16(v);
Chris Lattner2fef8092005-08-29 01:01:01 +0000688
689 // NOTE: This doesn't use SelectNodeTo, because doing that will prevent
690 // folding shared immediates into other the second instruction that
691 // uses it.
Nate Begemana6940472005-08-18 18:01:39 +0000692 if (Hi && Lo) {
693 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
694 getI32Imm(v >> 16));
Chris Lattner2fef8092005-08-29 01:01:01 +0000695 return CurDAG->getTargetNode(PPC::ORI, MVT::i32, Top,
696 getI32Imm(v & 0xFFFF));
Nate Begemana6940472005-08-18 18:01:39 +0000697 } else if (Lo) {
Chris Lattner2fef8092005-08-29 01:01:01 +0000698 return CurDAG->getTargetNode(PPC::LI, MVT::i32, getI32Imm(v));
Nate Begemana6940472005-08-18 18:01:39 +0000699 } else {
Chris Lattner2fef8092005-08-29 01:01:01 +0000700 return CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(v >> 16));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000701 }
702 }
Chris Lattner2b544002005-08-24 23:08:16 +0000703 case ISD::UNDEF:
704 if (N->getValueType(0) == MVT::i32)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000705 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattner2b544002005-08-24 23:08:16 +0000706 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000707 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0));
Chris Lattner2b544002005-08-24 23:08:16 +0000708 break;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000709 case ISD::FrameIndex: {
710 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000711 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere28e40a2005-08-25 00:45:43 +0000712 CurDAG->getTargetFrameIndex(FI, MVT::i32),
713 getI32Imm(0));
714 break;
715 }
Chris Lattner34e17052005-08-25 05:04:11 +0000716 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000717 Constant *C = cast<ConstantPoolSDNode>(N)->get();
718 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner34e17052005-08-25 05:04:11 +0000719 if (PICEnabled)
720 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
721 else
722 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000723 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner34e17052005-08-25 05:04:11 +0000724 break;
725 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000726 case ISD::GlobalAddress: {
727 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
728 SDOperand Tmp;
729 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000730 if (PICEnabled)
731 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
732 else
Chris Lattner4416f1a2005-08-19 22:38:53 +0000733 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +0000734
Chris Lattner4416f1a2005-08-19 22:38:53 +0000735 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000736 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000737 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000738 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000739 break;
740 }
Chris Lattner9c2dece2005-08-29 23:30:11 +0000741 case ISD::DYNAMIC_STACKALLOC: {
742 // FIXME: We are currently ignoring the requested alignment for handling
743 // greater than the stack alignment. This will need to be revisited at some
744 // point. Align = N.getOperand(2);
745 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
746 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
747 std::cerr << "Cannot allocate stack object with greater alignment than"
748 << " the stack alignment yet!";
749 abort();
750 }
751 SDOperand Chain = Select(N->getOperand(0));
752 SDOperand Amt = Select(N->getOperand(1));
753
754 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
755
756 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
757 // from the stack pointer, giving us the result pointer.
758 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Reg);
759
760 // Copy this result back into R1.
761 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
762
763 // Copy this result back out of R1 to make sure we're not using the stack
764 // space without decrementing the stack pointer.
765 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
766
767 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
768 CurDAG->ReplaceAllUsesWith(N, Result.Val);
769 N = Result.Val;
770 break;
771 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000772 case ISD::SIGN_EXTEND_INREG:
773 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
774 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
775 case MVT::i16:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000776 CurDAG->SelectNodeTo(N, PPC::EXTSH, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000777 break;
778 case MVT::i8:
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000779 CurDAG->SelectNodeTo(N, PPC::EXTSB, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000780 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000781 }
782 break;
783 case ISD::CTLZ:
784 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000785 CurDAG->SelectNodeTo(N, PPC::CNTLZW, MVT::i32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +0000786 break;
Chris Lattner0bbea952005-08-26 20:25:03 +0000787 case PPCISD::FSEL:
788 CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0),
789 Select(N->getOperand(0)),
790 Select(N->getOperand(1)),
791 Select(N->getOperand(2)));
792 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000793 case ISD::ADD: {
794 MVT::ValueType Ty = N->getValueType(0);
795 if (Ty == MVT::i32) {
796 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
797 PPC::ADDIS, PPC::ADDI, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000798 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000799 N = I;
800 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000801 CurDAG->SelectNodeTo(N, PPC::ADD, MVT::i32, Select(N->getOperand(0)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000802 Select(N->getOperand(1)));
803 }
804 break;
805 }
806
807 if (!NoExcessFPPrecision) { // Match FMA ops
808 if (N->getOperand(0).getOpcode() == ISD::MUL &&
809 N->getOperand(0).Val->hasOneUse()) {
810 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000811 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000812 Select(N->getOperand(0).getOperand(0)),
813 Select(N->getOperand(0).getOperand(1)),
814 Select(N->getOperand(1)));
815 break;
816 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
817 N->getOperand(1).hasOneUse()) {
818 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000819 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000820 Select(N->getOperand(1).getOperand(0)),
821 Select(N->getOperand(1).getOperand(1)),
822 Select(N->getOperand(0)));
823 break;
824 }
825 }
826
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000827 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000828 Select(N->getOperand(0)), Select(N->getOperand(1)));
829 break;
830 }
831 case ISD::SUB: {
832 MVT::ValueType Ty = N->getValueType(0);
833 if (Ty == MVT::i32) {
834 unsigned Imm;
835 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
Nate Begemanc6b07172005-08-24 05:03:20 +0000836 if (0 == Imm)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000837 CurDAG->SelectNodeTo(N, PPC::NEG, Ty, Select(N->getOperand(1)));
Nate Begemanc6b07172005-08-24 05:03:20 +0000838 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000839 CurDAG->SelectNodeTo(N, PPC::SUBFIC, Ty, Select(N->getOperand(1)),
Nate Begemanc6b07172005-08-24 05:03:20 +0000840 getI32Imm(Lo16(Imm)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000841 break;
842 }
843 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
844 PPC::ADDIS, PPC::ADDI, true, true)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000845 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000846 N = I;
847 } else {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000848 CurDAG->SelectNodeTo(N, PPC::SUBF, Ty, Select(N->getOperand(1)),
Chris Lattnera5a91b12005-08-17 19:33:03 +0000849 Select(N->getOperand(0)));
850 }
851 break;
852 }
853
854 if (!NoExcessFPPrecision) { // Match FMA ops
855 if (N->getOperand(0).getOpcode() == ISD::MUL &&
856 N->getOperand(0).Val->hasOneUse()) {
857 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000858 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000859 Select(N->getOperand(0).getOperand(0)),
860 Select(N->getOperand(0).getOperand(1)),
861 Select(N->getOperand(1)));
862 break;
863 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
864 N->getOperand(1).Val->hasOneUse()) {
865 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000866 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000867 Select(N->getOperand(1).getOperand(0)),
868 Select(N->getOperand(1).getOperand(1)),
869 Select(N->getOperand(0)));
870 break;
871 }
872 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000873 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000874 Select(N->getOperand(0)),
875 Select(N->getOperand(1)));
876 break;
Nate Begeman26653502005-08-17 23:46:35 +0000877 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000878 case ISD::MUL: {
879 unsigned Imm, Opc;
880 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000881 CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32,
Nate Begemanb5a06682005-08-18 00:21:41 +0000882 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
883 break;
884 }
885 switch (N->getValueType(0)) {
886 default: assert(0 && "Unhandled multiply type!");
887 case MVT::i32: Opc = PPC::MULLW; break;
888 case MVT::f32: Opc = PPC::FMULS; break;
889 case MVT::f64: Opc = PPC::FMUL; break;
890 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000891 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Nate Begemanb5a06682005-08-18 00:21:41 +0000892 Select(N->getOperand(1)));
893 break;
894 }
Chris Lattner8784a232005-08-25 17:50:06 +0000895 case ISD::SDIV: {
896 unsigned Imm;
897 if (isIntImmediate(N->getOperand(1), Imm)) {
898 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
899 SDOperand Op =
900 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
901 Select(N->getOperand(0)),
902 getI32Imm(Log2_32(Imm)));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000903 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner8784a232005-08-25 17:50:06 +0000904 Op.getValue(0), Op.getValue(1));
905 break;
906 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
907 SDOperand Op =
908 CurDAG->getTargetNode(PPC::SRAWI, MVT::Flag, MVT::i32,
909 Select(N->getOperand(0)),
910 getI32Imm(Log2_32(-Imm)));
911 SDOperand PT =
912 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(1),
913 Op.getValue(0));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000914 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000915 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000916 } else if (Imm) {
917 SDOperand Result = Select(BuildSDIVSequence(N));
918 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000919 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000920 N = Result.Val;
921 break;
Chris Lattner8784a232005-08-25 17:50:06 +0000922 }
923 }
Chris Lattner047b9522005-08-25 22:04:30 +0000924
925 unsigned Opc;
926 switch (N->getValueType(0)) {
Chris Lattner95e06822005-08-26 16:38:51 +0000927 default: assert(0 && "Unknown type to ISD::SDIV");
Chris Lattner047b9522005-08-25 22:04:30 +0000928 case MVT::i32: Opc = PPC::DIVW; break;
929 case MVT::f32: Opc = PPC::FDIVS; break;
930 case MVT::f64: Opc = PPC::FDIV; break;
931 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000932 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000933 Select(N->getOperand(1)));
934 break;
935 }
936 case ISD::UDIV: {
937 // If this is a divide by constant, we can emit code using some magic
938 // constants to implement it as a multiply instead.
939 unsigned Imm;
Chris Lattnera9317ed2005-08-25 23:21:06 +0000940 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner047b9522005-08-25 22:04:30 +0000941 SDOperand Result = Select(BuildUDIVSequence(N));
942 assert(Result.ResNo == 0);
Chris Lattner52987f42005-08-26 18:37:23 +0000943 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner047b9522005-08-25 22:04:30 +0000944 N = Result.Val;
945 break;
946 }
947
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000948 CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)),
Chris Lattner047b9522005-08-25 22:04:30 +0000949 Select(N->getOperand(1)));
950 break;
951 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000952 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000953 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000954 CurDAG->SelectNodeTo(N, PPC::MULHW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000955 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000956 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000957 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000958 assert(N->getValueType(0) == MVT::i32);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000959 CurDAG->SelectNodeTo(N, PPC::MULHWU, MVT::i32, Select(N->getOperand(0)),
Nate Begeman305a1c72005-08-18 03:04:18 +0000960 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000961 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000962 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000963 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000964 // If this is an and of a value rotated between 0 and 31 bits and then and'd
965 // with a mask, emit rlwinm
966 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
967 isShiftedMask_32(~Imm))) {
968 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000969 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000970 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
971 Val = Select(N->getOperand(0).getOperand(0));
972 } else {
973 Val = Select(N->getOperand(0));
974 isRunOfOnes(Imm, MB, ME);
975 SH = 0;
976 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000977 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemancffc32b2005-08-18 07:30:46 +0000978 getI32Imm(MB), getI32Imm(ME));
979 break;
980 }
981 // If this is an and with an immediate that isn't a mask, then codegen it as
982 // high and low 16 bit immediate ands.
983 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
984 N->getOperand(1),
985 PPC::ANDISo, PPC::ANDIo)) {
Chris Lattner52987f42005-08-26 18:37:23 +0000986 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000987 N = I;
988 break;
989 }
990 // Finally, check for the case where we are being asked to select
991 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
992 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000993 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)),
Nate Begemancffc32b2005-08-18 07:30:46 +0000994 Select(N->getOperand(0).getOperand(0)));
995 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000996 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +0000997 Select(N->getOperand(1).getOperand(0)));
998 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000999 CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)),
Nate Begemancffc32b2005-08-18 07:30:46 +00001000 Select(N->getOperand(1)));
1001 break;
1002 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001003 case ISD::OR:
1004 if (SDNode *I = SelectBitfieldInsert(N)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001005 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001006 N = I;
1007 break;
1008 }
1009 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1010 N->getOperand(1),
1011 PPC::ORIS, PPC::ORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001012 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman02b88a42005-08-19 00:38:14 +00001013 N = I;
1014 break;
1015 }
1016 // Finally, check for the case where we are being asked to select
1017 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
1018 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001019 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001020 Select(N->getOperand(0).getOperand(0)));
1021 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001022 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001023 Select(N->getOperand(1).getOperand(0)));
1024 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001025 CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman02b88a42005-08-19 00:38:14 +00001026 Select(N->getOperand(1)));
1027 break;
Nate Begeman0f3257a2005-08-18 05:00:13 +00001028 case ISD::XOR:
1029 // Check whether or not this node is a logical 'not'. This is represented
1030 // by llvm as a xor with the constant value -1 (all bits set). If this is a
1031 // 'not', then fold 'or' into 'nor', and so forth for the supported ops.
1032 if (isOprNot(N)) {
1033 unsigned Opc;
Nate Begeman131a8802005-08-18 05:44:50 +00001034 SDOperand Val = Select(N->getOperand(0));
Chris Lattner528f58e2005-08-28 23:39:22 +00001035 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman0f3257a2005-08-18 05:00:13 +00001036 default: Opc = 0; break;
Nate Begeman131a8802005-08-18 05:44:50 +00001037 case PPC::OR: Opc = PPC::NOR; break;
1038 case PPC::AND: Opc = PPC::NAND; break;
1039 case PPC::XOR: Opc = PPC::EQV; break;
Nate Begeman0f3257a2005-08-18 05:00:13 +00001040 }
1041 if (Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001042 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Val.getOperand(0),
Nate Begeman131a8802005-08-18 05:44:50 +00001043 Val.getOperand(1));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001044 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001045 CurDAG->SelectNodeTo(N, PPC::NOR, MVT::i32, Val, Val);
Nate Begeman0f3257a2005-08-18 05:00:13 +00001046 break;
1047 }
1048 // If this is a xor with an immediate other than -1, then codegen it as high
1049 // and low 16 bit immediate xors.
1050 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1051 N->getOperand(1),
1052 PPC::XORIS, PPC::XORI)) {
Chris Lattner52987f42005-08-26 18:37:23 +00001053 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman0f3257a2005-08-18 05:00:13 +00001054 N = I;
1055 break;
1056 }
1057 // Finally, check for the case where we are being asked to select
1058 // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv
1059 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001060 CurDAG->SelectNodeTo(N, PPC::EQV, MVT::i32,
Nate Begeman0f3257a2005-08-18 05:00:13 +00001061 Select(N->getOperand(0).getOperand(0)),
1062 Select(N->getOperand(1)));
1063 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001064 CurDAG->SelectNodeTo(N, PPC::XOR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman0f3257a2005-08-18 05:00:13 +00001065 Select(N->getOperand(1)));
1066 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001067 case ISD::SHL: {
1068 unsigned Imm, SH, MB, ME;
1069 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1070 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001071 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001072 Select(N->getOperand(0).getOperand(0)),
1073 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1074 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001075 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001076 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
1077 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001078 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001079 Select(N->getOperand(1)));
1080 break;
1081 }
1082 case ISD::SRL: {
1083 unsigned Imm, SH, MB, ME;
1084 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1085 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001086 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001087 Select(N->getOperand(0).getOperand(0)),
1088 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1089 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001090 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001091 getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31));
1092 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001093 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001094 Select(N->getOperand(1)));
1095 break;
1096 }
1097 case ISD::SRA: {
1098 unsigned Imm, SH, MB, ME;
1099 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1100 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001101 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001102 Select(N->getOperand(0).getOperand(0)),
1103 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1104 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001105 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001106 getI32Imm(Imm));
1107 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001108 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001109 Select(N->getOperand(1)));
1110 break;
1111 }
Nate Begeman305a1c72005-08-18 03:04:18 +00001112 case ISD::FABS:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001113 CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0),
Nate Begeman6a7d6112005-08-18 00:53:47 +00001114 Select(N->getOperand(0)));
1115 break;
Chris Lattner8f838722005-08-30 00:30:43 +00001116 case ISD::FP_EXTEND:
Nate Begeman305a1c72005-08-18 03:04:18 +00001117 assert(MVT::f64 == N->getValueType(0) &&
1118 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner8f838722005-08-30 00:30:43 +00001119 // We need to emit an FMR to make sure that the result has the right value
1120 // type.
1121 CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0)));
1122 break;
Nate Begeman305a1c72005-08-18 03:04:18 +00001123 case ISD::FP_ROUND:
1124 assert(MVT::f32 == N->getValueType(0) &&
1125 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001126 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Nate Begeman305a1c72005-08-18 03:04:18 +00001127 break;
Chris Lattnerc8a89a12005-08-28 23:59:09 +00001128 case ISD::FP_TO_SINT: {
1129 SDOperand In = Select(N->getOperand(0));
1130 In = CurDAG->getTargetNode(PPC::FCTIWZ, MVT::f64, In);
1131
1132 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1133 SDOperand FI = CurDAG->getTargetFrameIndex(FrameIdx, MVT::f64);
Chris Lattner9c2dece2005-08-29 23:30:11 +00001134 SDOperand ST = CurDAG->getTargetNode(PPC::STFD, MVT::Other, In,
1135 getI32Imm(0), FI);
1136 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, MVT::Other,
1137 getI32Imm(4), FI, ST);
Chris Lattnerc8a89a12005-08-28 23:59:09 +00001138 break;
1139 }
Nate Begeman26653502005-08-17 23:46:35 +00001140 case ISD::FNEG: {
1141 SDOperand Val = Select(N->getOperand(0));
1142 MVT::ValueType Ty = N->getValueType(0);
1143 if (Val.Val->hasOneUse()) {
1144 unsigned Opc;
Chris Lattner528f58e2005-08-28 23:39:22 +00001145 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman26653502005-08-17 23:46:35 +00001146 default: Opc = 0; break;
1147 case PPC::FABS: Opc = PPC::FNABS; break;
1148 case PPC::FMADD: Opc = PPC::FNMADD; break;
1149 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1150 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1151 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1152 }
1153 // If we inverted the opcode, then emit the new instruction with the
1154 // inverted opcode and the original instruction's operands. Otherwise,
1155 // fall through and generate a fneg instruction.
1156 if (Opc) {
1157 if (PPC::FNABS == Opc)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001158 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman26653502005-08-17 23:46:35 +00001159 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001160 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman26653502005-08-17 23:46:35 +00001161 Val.getOperand(1), Val.getOperand(2));
1162 break;
1163 }
1164 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001165 CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val);
Nate Begeman26653502005-08-17 23:46:35 +00001166 break;
1167 }
Nate Begeman6a7d6112005-08-18 00:53:47 +00001168 case ISD::FSQRT: {
1169 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001170 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman6a7d6112005-08-18 00:53:47 +00001171 Select(N->getOperand(0)));
1172 break;
1173 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001174
1175 case ISD::ADD_PARTS: {
1176 SDOperand LHSL = Select(N->getOperand(0));
1177 SDOperand LHSH = Select(N->getOperand(1));
1178
1179 unsigned Imm;
Chris Lattner95e06822005-08-26 16:38:51 +00001180 bool ME = false, ZE = false;
Chris Lattnera9317ed2005-08-25 23:21:06 +00001181 if (isIntImmediate(N->getOperand(3), Imm)) {
1182 ME = (signed)Imm == -1;
1183 ZE = Imm == 0;
1184 }
1185
1186 std::vector<SDOperand> Result;
1187 SDOperand CarryFromLo;
1188 if (isIntImmediate(N->getOperand(2), Imm) &&
1189 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
1190 // Codegen the low 32 bits of the add. Interestingly, there is no
1191 // shifted form of add immediate carrying.
1192 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1193 LHSL, getI32Imm(Imm));
1194 } else {
1195 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
1196 LHSL, Select(N->getOperand(2)));
1197 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001198 CarryFromLo = CarryFromLo.getValue(1);
1199
1200 // Codegen the high 32 bits, adding zero, minus one, or the full value
1201 // along with the carry flag produced by addc/addic.
1202 SDOperand ResultHi;
1203 if (ZE)
1204 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
1205 else if (ME)
1206 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
1207 else
1208 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
1209 Select(N->getOperand(3)), CarryFromLo);
1210 Result.push_back(ResultHi);
Chris Lattnerb20c3182005-08-25 23:36:49 +00001211 Result.push_back(CarryFromLo.getValue(0));
Chris Lattnera9317ed2005-08-25 23:21:06 +00001212 CurDAG->ReplaceAllUsesWith(N, Result);
1213 return Result[Op.ResNo];
1214 }
1215 case ISD::SUB_PARTS: {
1216 SDOperand LHSL = Select(N->getOperand(0));
1217 SDOperand LHSH = Select(N->getOperand(1));
1218 SDOperand RHSL = Select(N->getOperand(2));
1219 SDOperand RHSH = Select(N->getOperand(3));
1220
1221 std::vector<SDOperand> Result;
1222 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
1223 RHSL, LHSL));
1224 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
1225 Result[0].getValue(1)));
1226 CurDAG->ReplaceAllUsesWith(N, Result);
1227 return Result[Op.ResNo];
1228 }
Nate Begeman6660cd62005-08-26 00:28:00 +00001229 case ISD::SHL_PARTS: {
1230 SDOperand HI = Select(N->getOperand(0));
1231 SDOperand LO = Select(N->getOperand(1));
1232 SDOperand SH = Select(N->getOperand(2));
Nate Begemanbb22df32005-08-26 00:34:06 +00001233 SDOperand SH_LO_R = CurDAG->getTargetNode(PPC::SUBFIC, MVT::i32, MVT::Flag,
1234 SH, getI32Imm(32));
Nate Begeman6660cd62005-08-26 00:28:00 +00001235 SDOperand SH_LO_L = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, SH,
1236 getI32Imm((unsigned)-32));
1237 SDOperand HI_SHL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, HI, SH);
1238 SDOperand HI_LOR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, LO, SH_LO_R);
1239 SDOperand HI_LOL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, LO, SH_LO_L);
1240 SDOperand HI_OR = CurDAG->getTargetNode(PPC::OR, MVT::i32, HI_SHL, HI_LOR);
1241
1242 std::vector<SDOperand> Result;
1243 Result.push_back(CurDAG->getTargetNode(PPC::SLW, MVT::i32, LO, SH));
1244 Result.push_back(CurDAG->getTargetNode(PPC::OR, MVT::i32, HI_OR, HI_LOL));
1245 CurDAG->ReplaceAllUsesWith(N, Result);
1246 return Result[Op.ResNo];
1247 }
1248 case ISD::SRL_PARTS: {
1249 SDOperand HI = Select(N->getOperand(0));
1250 SDOperand LO = Select(N->getOperand(1));
1251 SDOperand SH = Select(N->getOperand(2));
Nate Begemanbb22df32005-08-26 00:34:06 +00001252 SDOperand SH_HI_L = CurDAG->getTargetNode(PPC::SUBFIC, MVT::i32, MVT::Flag,
1253 SH, getI32Imm(32));
Nate Begeman6660cd62005-08-26 00:28:00 +00001254 SDOperand SH_HI_R = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, SH,
1255 getI32Imm((unsigned)-32));
1256 SDOperand LO_SHR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, LO, SH);
1257 SDOperand LO_HIL = CurDAG->getTargetNode(PPC::SLW, MVT::i32, HI, SH_HI_L);
1258 SDOperand LO_HIR = CurDAG->getTargetNode(PPC::SRW, MVT::i32, HI, SH_HI_R);
1259 SDOperand LO_OR = CurDAG->getTargetNode(PPC::OR, MVT::i32, LO_SHR, LO_HIL);
1260
1261 std::vector<SDOperand> Result;
1262 Result.push_back(CurDAG->getTargetNode(PPC::OR, MVT::i32, LO_OR, LO_HIR));
1263 Result.push_back(CurDAG->getTargetNode(PPC::SRW, MVT::i32, HI, SH));
1264 CurDAG->ReplaceAllUsesWith(N, Result);
1265 return Result[Op.ResNo];
1266 }
Chris Lattnera9317ed2005-08-25 23:21:06 +00001267
Chris Lattner9944b762005-08-21 22:31:09 +00001268 case ISD::LOAD:
1269 case ISD::EXTLOAD:
1270 case ISD::ZEXTLOAD:
1271 case ISD::SEXTLOAD: {
1272 SDOperand Op1, Op2;
1273 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1274
1275 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1276 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1277 unsigned Opc;
1278 switch (TypeBeingLoaded) {
1279 default: N->dump(); assert(0 && "Cannot load this type!");
1280 case MVT::i1:
1281 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1282 case MVT::i16:
1283 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1284 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1285 } else {
1286 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1287 }
1288 break;
1289 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1290 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1291 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1292 }
1293
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001294 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattner9944b762005-08-21 22:31:09 +00001295 Op1, Op2, Select(N->getOperand(0)));
1296 break;
1297 }
1298
Chris Lattnerf7f22552005-08-22 01:27:59 +00001299 case ISD::TRUNCSTORE:
1300 case ISD::STORE: {
1301 SDOperand AddrOp1, AddrOp2;
1302 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1303
1304 unsigned Opc;
1305 if (N->getOpcode() == ISD::STORE) {
1306 switch (N->getOperand(1).getValueType()) {
1307 default: assert(0 && "unknown Type in store");
1308 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1309 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1310 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1311 }
1312 } else { //ISD::TRUNCSTORE
1313 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1314 default: assert(0 && "unknown Type in store");
1315 case MVT::i1:
1316 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1317 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1318 }
1319 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001320
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001321 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerf7f22552005-08-22 01:27:59 +00001322 AddrOp1, AddrOp2, Select(N->getOperand(0)));
1323 break;
1324 }
Chris Lattner64906a02005-08-25 20:08:18 +00001325
1326 case ISD::SETCC: {
1327 unsigned Imm;
1328 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1329 if (isIntImmediate(N->getOperand(1), Imm)) {
1330 // We can codegen setcc op, imm very efficiently compared to a brcond.
1331 // Check for those cases here.
1332 // setcc op, 0
1333 if (Imm == 0) {
1334 SDOperand Op = Select(N->getOperand(0));
1335 switch (CC) {
1336 default: assert(0 && "Unhandled SetCC condition"); abort();
1337 case ISD::SETEQ:
1338 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001339 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattner64906a02005-08-25 20:08:18 +00001340 getI32Imm(5), getI32Imm(31));
1341 break;
1342 case ISD::SETNE: {
1343 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1344 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001345 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001346 break;
1347 }
1348 case ISD::SETLT:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001349 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001350 getI32Imm(31), getI32Imm(31));
1351 break;
1352 case ISD::SETGT: {
1353 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
1354 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001355 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001356 getI32Imm(31), getI32Imm(31));
1357 break;
1358 }
1359 }
1360 break;
1361 } else if (Imm == ~0U) { // setcc op, -1
1362 SDOperand Op = Select(N->getOperand(0));
1363 switch (CC) {
1364 default: assert(0 && "Unhandled SetCC condition"); abort();
1365 case ISD::SETEQ:
1366 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1367 Op, getI32Imm(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001368 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner64906a02005-08-25 20:08:18 +00001369 CurDAG->getTargetNode(PPC::LI, MVT::i32,
1370 getI32Imm(0)),
1371 Op.getValue(1));
1372 break;
1373 case ISD::SETNE: {
1374 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
Chris Lattner8bbcc202005-08-29 23:49:25 +00001375 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1376 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001377 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001378 break;
1379 }
1380 case ISD::SETLT: {
1381 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
1382 getI32Imm(1));
1383 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001384 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001385 getI32Imm(31), getI32Imm(31));
1386 break;
1387 }
1388 case ISD::SETGT:
1389 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
1390 getI32Imm(31), getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001391 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001392 break;
1393 }
1394 break;
1395 }
1396 }
1397
1398 bool Inv;
1399 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1400 SDOperand CCReg =
1401 SelectCC(Select(N->getOperand(0)), Select(N->getOperand(1)), CC);
1402 SDOperand IntCR;
Chris Lattner957fcfb2005-08-25 21:39:42 +00001403
1404 // Force the ccreg into CR7.
1405 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
1406
1407 std::vector<MVT::ValueType> VTs;
1408 VTs.push_back(MVT::Other);
1409 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
1410 std::vector<SDOperand> Ops;
1411 Ops.push_back(CurDAG->getEntryNode());
1412 Ops.push_back(CR7Reg);
1413 Ops.push_back(CCReg);
1414 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
1415
1416 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1417 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
1418 else
1419 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
Chris Lattner64906a02005-08-25 20:08:18 +00001420
1421 if (!Inv) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001422 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner64906a02005-08-25 20:08:18 +00001423 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
1424 } else {
1425 SDOperand Tmp =
1426 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
1427 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001428 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001429 }
1430
1431 break;
1432 }
Chris Lattnera2590c52005-08-24 00:47:15 +00001433
Chris Lattner13794f52005-08-26 18:46:49 +00001434 case ISD::SELECT_CC: {
1435 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1436
1437 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1438 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1439 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1440 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1441 if (N1C->isNullValue() && N3C->isNullValue() &&
1442 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1443 SDOperand LHS = Select(N->getOperand(0));
1444 SDOperand Tmp =
1445 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1446 LHS, getI32Imm(~0U));
1447 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1448 Tmp.getValue(1));
1449 break;
1450 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001451
1452 SDOperand CCReg = SelectCC(Select(N->getOperand(0)),
1453 Select(N->getOperand(1)), CC);
1454 unsigned BROpc = getBCCForSetCC(CC);
1455
1456 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
1457 unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int;
1458 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1459 Select(N->getOperand(2)), Select(N->getOperand(3)),
1460 getI32Imm(BROpc));
1461 break;
Chris Lattner13794f52005-08-26 18:46:49 +00001462 }
1463
Chris Lattnera2590c52005-08-24 00:47:15 +00001464 case ISD::CALLSEQ_START:
1465 case ISD::CALLSEQ_END: {
1466 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1467 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1468 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001469 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001470 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattnera2590c52005-08-24 00:47:15 +00001471 break;
1472 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001473 case ISD::CALL:
1474 case ISD::TAILCALL: {
1475 SDOperand Chain = Select(N->getOperand(0));
1476
1477 unsigned CallOpcode;
1478 std::vector<SDOperand> CallOperands;
1479
1480 if (GlobalAddressSDNode *GASD =
1481 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
1482 CallOpcode = PPC::CALLpcrel;
1483 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
1484 MVT::i32));
1485 } else if (ExternalSymbolSDNode *ESSDN =
1486 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
1487 CallOpcode = PPC::CALLpcrel;
1488 CallOperands.push_back(N->getOperand(1));
1489 } else {
1490 // Copy the callee address into the CTR register.
1491 SDOperand Callee = Select(N->getOperand(1));
1492 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
1493
1494 // Copy the callee address into R12 on darwin.
1495 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
Chris Lattner2a06a5e2005-08-29 00:26:57 +00001496 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001497
1498 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
1499 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
1500 CallOperands.push_back(R12);
1501 CallOpcode = PPC::CALLindirect;
1502 }
1503
1504 unsigned GPR_idx = 0, FPR_idx = 0;
1505 static const unsigned GPR[] = {
1506 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1507 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1508 };
1509 static const unsigned FPR[] = {
1510 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1511 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1512 };
1513
Chris Lattner7107c102005-08-29 22:22:57 +00001514 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1515 unsigned DestReg = 0;
1516 MVT::ValueType RegTy;
1517 if (N->getOperand(i).getValueType() == MVT::i32) {
1518 assert(GPR_idx < 8 && "Too many int args");
1519 DestReg = GPR[GPR_idx++];
1520 RegTy = MVT::i32;
1521 } else {
1522 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
1523 "Unpromoted integer arg?");
1524 assert(FPR_idx < 13 && "Too many fp args");
1525 DestReg = FPR[FPR_idx++];
1526 RegTy = MVT::f64; // Even if this is really f32!
1527 }
1528
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001529 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001530 SDOperand Reg = CurDAG->getRegister(DestReg, RegTy);
1531 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg,
1532 Select(N->getOperand(i)));
1533 CallOperands.push_back(Reg);
1534 }
Chris Lattner7107c102005-08-29 22:22:57 +00001535 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001536
1537 // Finally, once everything is in registers to pass to the call, emit the
1538 // call itself.
1539 CallOperands.push_back(Chain);
1540 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, CallOperands);
1541
1542 std::vector<SDOperand> CallResults;
1543
1544 // If the call has results, copy the values out of the ret val registers.
1545 switch (N->getValueType(0)) {
1546 default: assert(0 && "Unexpected ret value!");
1547 case MVT::Other: break;
1548 case MVT::i32:
1549 if (N->getValueType(1) == MVT::i32) {
1550 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32).getValue(1);
1551 CallResults.push_back(Chain.getValue(0));
1552 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1553 CallResults.push_back(Chain.getValue(0));
1554 } else {
1555 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32).getValue(1);
1556 CallResults.push_back(Chain.getValue(0));
1557 }
1558 break;
1559 case MVT::f32:
1560 case MVT::f64:
1561 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, MVT::f64).getValue(1);
Chris Lattner8f838722005-08-30 00:30:43 +00001562 if (N->getValueType(0) == MVT::f64)
1563 CallResults.push_back(Chain.getValue(0));
1564 else
1565 // Insert an FMR to convert the result to f32 from f64.
1566 CallResults.push_back(CurDAG->getTargetNode(PPC::FMR, MVT::f32,
1567 Chain.getValue(0)));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001568 break;
1569 }
1570
1571 CallResults.push_back(Chain);
1572 CurDAG->ReplaceAllUsesWith(N, CallResults);
1573 return CallResults[Op.ResNo];
1574 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001575 case ISD::RET: {
1576 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1577
1578 if (N->getNumOperands() > 1) {
1579 SDOperand Val = Select(N->getOperand(1));
1580 switch (N->getOperand(1).getValueType()) {
1581 default: assert(0 && "Unknown return type!");
Chris Lattnera5a91b12005-08-17 19:33:03 +00001582 case MVT::f32:
Chris Lattner8f838722005-08-30 00:30:43 +00001583 // Insert a copy to get the type right.
1584 Val = CurDAG->getTargetNode(PPC::FMR, MVT::f64, Val);
1585 // FALL THROUGH
1586 case MVT::f64:
Chris Lattnera5a91b12005-08-17 19:33:03 +00001587 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
1588 break;
1589 case MVT::i32:
1590 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
1591 break;
1592 }
1593
1594 if (N->getNumOperands() > 2) {
1595 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1596 N->getOperand(2).getValueType() == MVT::i32 &&
Chris Lattnera9317ed2005-08-25 23:21:06 +00001597 N->getNumOperands() == 3 && "Unknown two-register ret value!");
Chris Lattnera5a91b12005-08-17 19:33:03 +00001598 Val = Select(N->getOperand(2));
1599 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
1600 }
1601 }
1602
1603 // Finally, select this to a blr (return) instruction.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001604 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001605 break;
1606 }
Chris Lattner89532c72005-08-25 00:29:58 +00001607 case ISD::BR:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001608 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner89532c72005-08-25 00:29:58 +00001609 Select(N->getOperand(0)));
1610 break;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001611 case ISD::BR_CC:
1612 case ISD::BRTWOWAY_CC: {
1613 SDOperand Chain = Select(N->getOperand(0));
1614 MachineBasicBlock *Dest =
1615 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1616 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1617 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1618 unsigned Opc = getBCCForSetCC(CC);
1619
1620 // If this is a two way branch, then grab the fallthrough basic block
1621 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1622 // conversion if necessary by the branch selection pass. Otherwise, emit a
1623 // standard conditional branch.
1624 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
1625 MachineBasicBlock *Fallthrough =
1626 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
1627 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1628 CondCode, getI32Imm(Opc),
1629 N->getOperand(4), N->getOperand(5),
1630 Chain);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001631 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001632 } else {
1633 // Iterate to the next basic block
1634 ilist<MachineBasicBlock>::iterator It = BB;
1635 ++It;
1636
1637 // If the fallthrough path is off the end of the function, which would be
1638 // undefined behavior, set it to be the same as the current block because
1639 // we have nothing better to set it to, and leaving it alone will cause
1640 // the PowerPC Branch Selection pass to crash.
1641 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001642 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001643 getI32Imm(Opc), N->getOperand(4),
1644 CurDAG->getBasicBlock(It), Chain);
1645 }
1646 break;
1647 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001648 }
Chris Lattnerddf3e7d2005-08-22 00:59:14 +00001649 return SDOperand(N, Op.ResNo);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001650}
1651
1652
1653/// createPPC32ISelDag - This pass converts a legalized DAG into a
1654/// PowerPC-specific DAG, ready for instruction scheduling.
1655///
1656FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1657 return new PPC32DAGToDAGISel(TM);
1658}
1659