blob: 0fd4e027f588f748cecfe4be1d57f7ad1870348d [file] [log] [blame]
Chris Lattner43ff01e2005-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 Lattner45640392005-08-19 22:38:53 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner666512c2005-08-25 04:47:18 +000025#include "llvm/Constants.h"
Chris Lattner45640392005-08-19 22:38:53 +000026#include "llvm/GlobalValue.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
29using namespace llvm;
30
31namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000032 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
33 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
34
35 //===--------------------------------------------------------------------===//
36 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class PPC32DAGToDAGISel : public SelectionDAGISel {
40 PPC32TargetLowering PPC32Lowering;
Chris Lattner45640392005-08-19 22:38:53 +000041 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000042 public:
43 PPC32DAGToDAGISel(TargetMachine &TM)
44 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
45
Chris Lattner45640392005-08-19 22:38:53 +000046 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
48 GlobalBaseReg = 0;
49 return SelectionDAGISel::runOnFunction(Fn);
50 }
51
Chris Lattner43ff01e2005-08-17 19:33:03 +000052 /// getI32Imm - Return a target constant with the specified value, of type
53 /// i32.
54 inline SDOperand getI32Imm(unsigned Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i32);
56 }
Chris Lattner45640392005-08-19 22:38:53 +000057
58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
59 /// base register. Return the virtual register that holds this value.
Chris Lattnerc5292ec2005-08-21 22:31:09 +000060 SDOperand getGlobalBaseReg();
Chris Lattner43ff01e2005-08-17 19:33:03 +000061
62 // Select - Convert the specified operand from a target-independent to a
63 // target-specific node if it hasn't already been changed.
64 SDOperand Select(SDOperand Op);
65
66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
69 bool Negate = false);
Nate Begeman93c4bc62005-08-19 00:38:14 +000070 SDNode *SelectBitfieldInsert(SDNode *N);
71
Chris Lattner2a1823d2005-08-21 18:50:37 +000072 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
75
Chris Lattnerc5292ec2005-08-21 22:31:09 +000076 /// SelectAddr - Given the specified address, return the two operands for a
77 /// load/store instruction, and return true if it should be an indexed [r+r]
78 /// operation.
79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
80
Chris Lattner6e184f22005-08-25 22:04:30 +000081 SDOperand BuildSDIVSequence(SDNode *N);
82 SDOperand BuildUDIVSequence(SDNode *N);
83
Chris Lattner43ff01e2005-08-17 19:33:03 +000084 /// InstructionSelectBasicBlock - This callback is invoked by
85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
86 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
87 DEBUG(BB->dump());
Chris Lattner015d7392005-08-18 18:46:06 +000088 // Select target instructions for the DAG.
Chris Lattnerc429ab22005-08-29 01:07:02 +000089 DAG.setRoot(Select(DAG.getRoot()));
Chris Lattnerc628f002005-09-27 17:45:33 +000090 CodeGenMap.clear();
Chris Lattner43ff01e2005-08-17 19:33:03 +000091 DAG.RemoveDeadNodes();
Chris Lattner015d7392005-08-18 18:46:06 +000092
Chris Lattner015d7392005-08-18 18:46:06 +000093 // Emit machine code to BB.
94 ScheduleAndEmitDAG(DAG);
Chris Lattner43ff01e2005-08-17 19:33:03 +000095 }
96
97 virtual const char *getPassName() const {
98 return "PowerPC DAG->DAG Pattern Instruction Selection";
99 }
Chris Lattner03e08ee2005-09-13 22:03:06 +0000100
101// Include the pieces autogenerated from the target description.
102#include "PPC32GenDAGISel.inc"
Chris Lattner43ff01e2005-08-17 19:33:03 +0000103 };
104}
105
Chris Lattner8ae95252005-09-03 01:17:22 +0000106
Chris Lattner45640392005-08-19 22:38:53 +0000107/// getGlobalBaseReg - Output the instructions required to put the
108/// base address to use for accessing globals into a register.
109///
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000110SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000111 if (!GlobalBaseReg) {
112 // Insert the set of GlobalBaseReg into the first MBB of the function
113 MachineBasicBlock &FirstMBB = BB->getParent()->front();
114 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
115 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
116 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
117 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
118 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
119 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000120 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner45640392005-08-19 22:38:53 +0000121}
122
123
Nate Begeman72d6f882005-08-18 05:00:13 +0000124// isIntImmediate - This method tests to see if a constant operand.
125// If so Imm will receive the 32 bit value.
126static bool isIntImmediate(SDNode *N, unsigned& Imm) {
127 if (N->getOpcode() == ISD::Constant) {
128 Imm = cast<ConstantSDNode>(N)->getValue();
129 return true;
130 }
131 return false;
132}
133
Nate Begemanb3821a32005-08-18 07:30:46 +0000134// isOprShiftImm - Returns true if the specified operand is a shift opcode with
135// a immediate shift count less than 32.
136static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
137 Opc = N->getOpcode();
138 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
139 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
140}
141
142// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
143// any number of 0s on either side. The 1s are allowed to wrap from LSB to
144// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
145// not, since all 1s are not contiguous.
146static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
147 if (isShiftedMask_32(Val)) {
148 // look for the first non-zero bit
149 MB = CountLeadingZeros_32(Val);
150 // look for the first zero bit after the run of ones
151 ME = CountLeadingZeros_32((Val - 1) ^ Val);
152 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000153 } else {
154 Val = ~Val; // invert mask
155 if (isShiftedMask_32(Val)) {
156 // effectively look for the first zero bit
157 ME = CountLeadingZeros_32(Val) - 1;
158 // effectively look for the first one bit after the run of zeros
159 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
160 return true;
161 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000162 }
163 // no run present
164 return false;
165}
166
167// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
168// and mask opcode and mask operation.
169static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
170 unsigned &SH, unsigned &MB, unsigned &ME) {
171 unsigned Shift = 32;
172 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
173 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000174 if (N->getNumOperands() != 2 ||
175 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000176 return false;
177
178 if (Opcode == ISD::SHL) {
179 // apply shift left 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 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
184 // apply shift right to mask if it comes first
185 if (IsShiftMask) Mask = Mask >> Shift;
186 // determine which bits are made indeterminant by shift
187 Indeterminant = ~(0xFFFFFFFFu >> Shift);
188 // adjust for the left rotate
189 Shift = 32 - Shift;
190 } else {
191 return false;
192 }
193
194 // if the mask doesn't intersect any Indeterminant bits
195 if (Mask && !(Mask & Indeterminant)) {
196 SH = Shift;
197 // make sure the mask is still a mask (wrap arounds may not be)
198 return isRunOfOnes(Mask, MB, ME);
199 }
200 return false;
201}
202
Nate Begeman72d6f882005-08-18 05:00:13 +0000203// isOpcWithIntImmediate - This method tests to see if the node is a specific
204// opcode and that it has a immediate integer right operand.
205// If so Imm will receive the 32 bit value.
206static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
207 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
208}
209
210// isOprNot - Returns true if the specified operand is an xor with immediate -1.
211static bool isOprNot(SDNode *N) {
212 unsigned Imm;
213 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
214}
215
Chris Lattner43ff01e2005-08-17 19:33:03 +0000216// Immediate constant composers.
217// Lo16 - grabs the lo 16 bits from a 32 bit constant.
218// Hi16 - grabs the hi 16 bits from a 32 bit constant.
219// HA16 - computes the hi bits required if the lo bits are add/subtracted in
220// arithmethically.
221static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
222static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
223static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
224
225// isIntImmediate - This method tests to see if a constant operand.
226// If so Imm will receive the 32 bit value.
227static bool isIntImmediate(SDOperand N, unsigned& Imm) {
228 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
229 Imm = (unsigned)CN->getSignExtended();
230 return true;
231 }
232 return false;
233}
234
Nate Begeman93c4bc62005-08-19 00:38:14 +0000235/// SelectBitfieldInsert - turn an or of two masked values into
236/// the rotate left word immediate then mask insert (rlwimi) instruction.
237/// Returns true on success, false if the caller still needs to select OR.
238///
239/// Patterns matched:
240/// 1. or shl, and 5. or and, and
241/// 2. or and, shl 6. or shl, shr
242/// 3. or shr, and 7. or shr, shl
243/// 4. or and, shr
244SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
245 bool IsRotate = false;
246 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
247 unsigned Value;
248
249 SDOperand Op0 = N->getOperand(0);
250 SDOperand Op1 = N->getOperand(1);
251
252 unsigned Op0Opc = Op0.getOpcode();
253 unsigned Op1Opc = Op1.getOpcode();
254
255 // Verify that we have the correct opcodes
256 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
257 return false;
258 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
259 return false;
260
261 // Generate Mask value for Target
262 if (isIntImmediate(Op0.getOperand(1), Value)) {
263 switch(Op0Opc) {
Chris Lattner9f23ae22005-08-30 18:37:48 +0000264 case ISD::SHL: TgtMask <<= Value; break;
265 case ISD::SRL: TgtMask >>= Value; break;
266 case ISD::AND: TgtMask &= Value; break;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000267 }
268 } else {
269 return 0;
270 }
271
272 // Generate Mask value for Insert
Chris Lattner9f23ae22005-08-30 18:37:48 +0000273 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman93c4bc62005-08-19 00:38:14 +0000274 return 0;
Chris Lattner9f23ae22005-08-30 18:37:48 +0000275
276 switch(Op1Opc) {
277 case ISD::SHL:
278 SH = Value;
279 InsMask <<= SH;
280 if (Op0Opc == ISD::SRL) IsRotate = true;
281 break;
282 case ISD::SRL:
283 SH = Value;
284 InsMask >>= SH;
285 SH = 32-SH;
286 if (Op0Opc == ISD::SHL) IsRotate = true;
287 break;
288 case ISD::AND:
289 InsMask &= Value;
290 break;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000291 }
292
293 // If both of the inputs are ANDs and one of them has a logical shift by
294 // constant as its input, make that AND the inserted value so that we can
295 // combine the shift into the rotate part of the rlwimi instruction
296 bool IsAndWithShiftOp = false;
297 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
298 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
299 Op1.getOperand(0).getOpcode() == ISD::SRL) {
300 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
301 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
302 IsAndWithShiftOp = true;
303 }
304 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
305 Op0.getOperand(0).getOpcode() == ISD::SRL) {
306 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
307 std::swap(Op0, Op1);
308 std::swap(TgtMask, InsMask);
309 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
310 IsAndWithShiftOp = true;
311 }
312 }
313 }
314
315 // Verify that the Target mask and Insert mask together form a full word mask
316 // and that the Insert mask is a run of set bits (which implies both are runs
317 // of set bits). Given that, Select the arguments and generate the rlwimi
318 // instruction.
319 unsigned MB, ME;
320 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
321 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
322 bool Op0IsAND = Op0Opc == ISD::AND;
323 // Check for rotlwi / rotrwi here, a special case of bitfield insert
324 // where both bitfield halves are sourced from the same value.
325 if (IsRotate && fullMask &&
326 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
327 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
328 Select(N->getOperand(0).getOperand(0)),
329 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
330 return Op0.Val;
331 }
332 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
333 : Select(Op0);
334 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
335 : Select(Op1.getOperand(0));
336 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
337 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
338 return Op0.Val;
339 }
340 return 0;
341}
342
Chris Lattner43ff01e2005-08-17 19:33:03 +0000343// SelectIntImmediateExpr - Choose code for integer operations with an immediate
344// operand.
345SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
346 unsigned OCHi, unsigned OCLo,
347 bool IsArithmetic,
348 bool Negate) {
349 // Check to make sure this is a constant.
350 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
351 // Exit if not a constant.
352 if (!CN) return 0;
353 // Extract immediate.
354 unsigned C = (unsigned)CN->getValue();
355 // Negate if required (ISD::SUB).
356 if (Negate) C = -C;
357 // Get the hi and lo portions of constant.
358 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
359 unsigned Lo = Lo16(C);
360
361 // If two instructions are needed and usage indicates it would be better to
362 // load immediate into a register, bail out.
363 if (Hi && Lo && CN->use_size() > 2) return false;
364
365 // Select the first operand.
366 SDOperand Opr0 = Select(LHS);
367
368 if (Lo) // Add in the lo-part.
369 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
370 if (Hi) // Add in the hi-part.
371 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
372 return Opr0.Val;
373}
374
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000375/// SelectAddr - Given the specified address, return the two operands for a
376/// load/store instruction, and return true if it should be an indexed [r+r]
377/// operation.
378bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
379 SDOperand &Op2) {
380 unsigned imm = 0;
381 if (Addr.getOpcode() == ISD::ADD) {
382 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
383 Op1 = getI32Imm(Lo16(imm));
Chris Lattnere4c338d2005-08-25 00:45:43 +0000384 if (FrameIndexSDNode *FI =
385 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000386 ++FrameOff;
Chris Lattnere4c338d2005-08-25 00:45:43 +0000387 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000388 } else {
389 Op2 = Select(Addr.getOperand(0));
390 }
391 return false;
392 } else {
393 Op1 = Select(Addr.getOperand(0));
394 Op2 = Select(Addr.getOperand(1));
395 return true; // [r+r]
396 }
397 }
398
399 // Now check if we're dealing with a global, and whether or not we should emit
400 // an optimized load or store for statics.
401 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
402 GlobalValue *GV = GN->getGlobal();
403 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
404 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
405 if (PICEnabled)
406 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
407 Op1);
408 else
409 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
410 return false;
411 }
Chris Lattnere4c338d2005-08-25 00:45:43 +0000412 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) {
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000413 Op1 = getI32Imm(0);
Chris Lattnere4c338d2005-08-25 00:45:43 +0000414 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000415 return false;
416 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
417 Op1 = Addr;
418 if (PICEnabled)
419 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
420 else
421 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
422 return false;
423 }
424 Op1 = getI32Imm(0);
425 Op2 = Select(Addr);
426 return false;
427}
Chris Lattner43ff01e2005-08-17 19:33:03 +0000428
Chris Lattner2a1823d2005-08-21 18:50:37 +0000429/// SelectCC - Select a comparison of the specified values with the specified
430/// condition code, returning the CR# of the expression.
431SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
432 ISD::CondCode CC) {
433 // Always select the LHS.
434 LHS = Select(LHS);
435
436 // Use U to determine whether the SETCC immediate range is signed or not.
437 if (MVT::isInteger(LHS.getValueType())) {
438 bool U = ISD::isUnsignedIntSetCC(CC);
439 unsigned Imm;
440 if (isIntImmediate(RHS, Imm) &&
441 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
442 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
443 LHS, getI32Imm(Lo16(Imm)));
444 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
445 LHS, Select(RHS));
446 } else {
447 return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS));
448 }
449}
450
451/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
452/// to Condition.
453static unsigned getBCCForSetCC(ISD::CondCode CC) {
454 switch (CC) {
455 default: assert(0 && "Unknown condition!"); abort();
456 case ISD::SETEQ: return PPC::BEQ;
457 case ISD::SETNE: return PPC::BNE;
458 case ISD::SETULT:
459 case ISD::SETLT: return PPC::BLT;
460 case ISD::SETULE:
461 case ISD::SETLE: return PPC::BLE;
462 case ISD::SETUGT:
463 case ISD::SETGT: return PPC::BGT;
464 case ISD::SETUGE:
465 case ISD::SETGE: return PPC::BGE;
466 }
467 return 0;
468}
469
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000470/// getCRIdxForSetCC - Return the index of the condition register field
471/// associated with the SetCC condition, and whether or not the field is
472/// treated as inverted. That is, lt = 0; ge = 0 inverted.
473static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
474 switch (CC) {
475 default: assert(0 && "Unknown condition!"); abort();
476 case ISD::SETULT:
477 case ISD::SETLT: Inv = false; return 0;
478 case ISD::SETUGE:
479 case ISD::SETGE: Inv = true; return 0;
480 case ISD::SETUGT:
481 case ISD::SETGT: Inv = false; return 1;
482 case ISD::SETULE:
483 case ISD::SETLE: Inv = true; return 1;
484 case ISD::SETEQ: Inv = false; return 2;
485 case ISD::SETNE: Inv = true; return 2;
486 }
487 return 0;
488}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000489
Chris Lattner6e184f22005-08-25 22:04:30 +0000490// Structure used to return the necessary information to codegen an SDIV as
491// a multiply.
492struct ms {
493 int m; // magic number
494 int s; // shift amount
495};
496
497struct mu {
498 unsigned int m; // magic number
499 int a; // add indicator
500 int s; // shift amount
501};
502
503/// magic - calculate the magic numbers required to codegen an integer sdiv as
504/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
505/// or -1.
506static struct ms magic(int d) {
507 int p;
508 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
509 const unsigned int two31 = 0x80000000U;
510 struct ms mag;
511
512 ad = abs(d);
513 t = two31 + ((unsigned int)d >> 31);
514 anc = t - 1 - t%ad; // absolute value of nc
515 p = 31; // initialize p
516 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
517 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
518 q2 = two31/ad; // initialize q2 = 2p/abs(d)
519 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
520 do {
521 p = p + 1;
522 q1 = 2*q1; // update q1 = 2p/abs(nc)
523 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
524 if (r1 >= anc) { // must be unsigned comparison
525 q1 = q1 + 1;
526 r1 = r1 - anc;
527 }
528 q2 = 2*q2; // update q2 = 2p/abs(d)
529 r2 = 2*r2; // update r2 = rem(2p/abs(d))
530 if (r2 >= ad) { // must be unsigned comparison
531 q2 = q2 + 1;
532 r2 = r2 - ad;
533 }
534 delta = ad - r2;
535 } while (q1 < delta || (q1 == delta && r1 == 0));
536
537 mag.m = q2 + 1;
538 if (d < 0) mag.m = -mag.m; // resulting magic number
539 mag.s = p - 32; // resulting shift
540 return mag;
541}
542
543/// magicu - calculate the magic numbers required to codegen an integer udiv as
544/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
545static struct mu magicu(unsigned d)
546{
547 int p;
548 unsigned int nc, delta, q1, r1, q2, r2;
549 struct mu magu;
550 magu.a = 0; // initialize "add" indicator
551 nc = - 1 - (-d)%d;
552 p = 31; // initialize p
553 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
554 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
555 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
556 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
557 do {
558 p = p + 1;
559 if (r1 >= nc - r1 ) {
560 q1 = 2*q1 + 1; // update q1
561 r1 = 2*r1 - nc; // update r1
562 }
563 else {
564 q1 = 2*q1; // update q1
565 r1 = 2*r1; // update r1
566 }
567 if (r2 + 1 >= d - r2) {
568 if (q2 >= 0x7FFFFFFF) magu.a = 1;
569 q2 = 2*q2 + 1; // update q2
570 r2 = 2*r2 + 1 - d; // update r2
571 }
572 else {
573 if (q2 >= 0x80000000) magu.a = 1;
574 q2 = 2*q2; // update q2
575 r2 = 2*r2 + 1; // update r2
576 }
577 delta = d - 1 - r2;
578 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
579 magu.m = q2 + 1; // resulting magic number
580 magu.s = p - 32; // resulting shift
581 return magu;
582}
583
584/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
585/// return a DAG expression to select that will generate the same value by
586/// multiplying by a magic number. See:
587/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
588SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) {
589 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue();
590 ms magics = magic(d);
591 // Multiply the numerator (operand 0) by the magic value
592 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0),
593 CurDAG->getConstant(magics.m, MVT::i32));
594 // If d > 0 and m < 0, add the numerator
595 if (d > 0 && magics.m < 0)
596 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0));
597 // If d < 0 and m > 0, subtract the numerator.
598 if (d < 0 && magics.m > 0)
599 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0));
600 // Shift right algebraic if shift value is nonzero
601 if (magics.s > 0)
602 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q,
603 CurDAG->getConstant(magics.s, MVT::i32));
604 // Extract the sign bit and add it to the quotient
605 SDOperand T =
606 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32));
607 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T);
608}
609
610/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
611/// return a DAG expression to select that will generate the same value by
612/// multiplying by a magic number. See:
613/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
614SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) {
615 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue();
616 mu magics = magicu(d);
617 // Multiply the numerator (operand 0) by the magic value
618 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0),
619 CurDAG->getConstant(magics.m, MVT::i32));
620 if (magics.a == 0) {
621 return CurDAG->getNode(ISD::SRL, MVT::i32, Q,
622 CurDAG->getConstant(magics.s, MVT::i32));
623 } else {
624 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q);
625 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
626 CurDAG->getConstant(1, MVT::i32));
627 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
628 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
629 CurDAG->getConstant(magics.s-1, MVT::i32));
630 }
631}
632
Chris Lattner43ff01e2005-08-17 19:33:03 +0000633// Select - Convert the specified operand from a target-independent to a
634// target-specific node if it hasn't already been changed.
635SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
636 SDNode *N = Op.Val;
Chris Lattnerb2854fa2005-08-26 20:25:03 +0000637 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
638 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattner43ff01e2005-08-17 19:33:03 +0000639 return Op; // Already selected.
640
641 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000642 default: break;
Chris Lattner43ff01e2005-08-17 19:33:03 +0000643 case ISD::TokenFactor: {
644 SDOperand New;
645 if (N->getNumOperands() == 2) {
646 SDOperand Op0 = Select(N->getOperand(0));
647 SDOperand Op1 = Select(N->getOperand(1));
648 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
649 } else {
650 std::vector<SDOperand> Ops;
651 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner65d66792005-08-19 21:33:02 +0000652 Ops.push_back(Select(N->getOperand(i)));
Chris Lattner43ff01e2005-08-17 19:33:03 +0000653 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
654 }
655
656 if (New.Val != N) {
Chris Lattnera9e6a822005-08-26 18:37:23 +0000657 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000658 N = New.Val;
659 }
Chris Lattner5f12cf12005-09-03 00:53:47 +0000660 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000661 }
662 case ISD::CopyFromReg: {
663 SDOperand Chain = Select(N->getOperand(0));
664 if (Chain == N->getOperand(0)) return Op; // No change
665 SDOperand New = CurDAG->getCopyFromReg(Chain,
666 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
667 return New.getValue(Op.ResNo);
668 }
669 case ISD::CopyToReg: {
670 SDOperand Chain = Select(N->getOperand(0));
671 SDOperand Reg = N->getOperand(1);
672 SDOperand Val = Select(N->getOperand(2));
673 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
674 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
675 Chain, Reg, Val);
Chris Lattnera9e6a822005-08-26 18:37:23 +0000676 CurDAG->ReplaceAllUsesWith(Op, New);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000677 N = New.Val;
678 }
Chris Lattner5f12cf12005-09-03 00:53:47 +0000679 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000680 }
Chris Lattnera3fbdae2005-08-24 23:08:16 +0000681 case ISD::UNDEF:
682 if (N->getValueType(0) == MVT::i32)
Chris Lattner2091a362005-08-26 16:36:26 +0000683 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattnera3fbdae2005-08-24 23:08:16 +0000684 else
Chris Lattner2091a362005-08-26 16:36:26 +0000685 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000686 return SDOperand(N, 0);
Chris Lattnere4c338d2005-08-25 00:45:43 +0000687 case ISD::FrameIndex: {
688 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2091a362005-08-26 16:36:26 +0000689 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere4c338d2005-08-25 00:45:43 +0000690 CurDAG->getTargetFrameIndex(FI, MVT::i32),
691 getI32Imm(0));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000692 return SDOperand(N, 0);
Chris Lattnere4c338d2005-08-25 00:45:43 +0000693 }
Chris Lattner25db6992005-08-25 05:04:11 +0000694 case ISD::ConstantPool: {
Chris Lattnerc30405e2005-08-26 17:15:30 +0000695 Constant *C = cast<ConstantPoolSDNode>(N)->get();
696 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner25db6992005-08-25 05:04:11 +0000697 if (PICEnabled)
698 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
699 else
700 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2091a362005-08-26 16:36:26 +0000701 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner5f12cf12005-09-03 00:53:47 +0000702 return SDOperand(N, 0);
Chris Lattner25db6992005-08-25 05:04:11 +0000703 }
Chris Lattner45640392005-08-19 22:38:53 +0000704 case ISD::GlobalAddress: {
705 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
706 SDOperand Tmp;
707 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000708 if (PICEnabled)
709 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
710 else
Chris Lattner45640392005-08-19 22:38:53 +0000711 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000712
Chris Lattner45640392005-08-19 22:38:53 +0000713 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2091a362005-08-26 16:36:26 +0000714 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner45640392005-08-19 22:38:53 +0000715 else
Chris Lattner2091a362005-08-26 16:36:26 +0000716 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner5f12cf12005-09-03 00:53:47 +0000717 return SDOperand(N, 0);
Chris Lattner45640392005-08-19 22:38:53 +0000718 }
Chris Lattner1cbbe102005-08-29 23:30:11 +0000719 case ISD::DYNAMIC_STACKALLOC: {
720 // FIXME: We are currently ignoring the requested alignment for handling
721 // greater than the stack alignment. This will need to be revisited at some
722 // point. Align = N.getOperand(2);
723 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
724 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
725 std::cerr << "Cannot allocate stack object with greater alignment than"
726 << " the stack alignment yet!";
727 abort();
728 }
729 SDOperand Chain = Select(N->getOperand(0));
730 SDOperand Amt = Select(N->getOperand(1));
731
732 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
733
Chris Lattnera305d282005-09-01 21:31:30 +0000734 SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
735 Chain = R1Val.getValue(1);
736
Chris Lattner1cbbe102005-08-29 23:30:11 +0000737 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
738 // from the stack pointer, giving us the result pointer.
Chris Lattnera305d282005-09-01 21:31:30 +0000739 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val);
Chris Lattner1cbbe102005-08-29 23:30:11 +0000740
741 // Copy this result back into R1.
742 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
743
744 // Copy this result back out of R1 to make sure we're not using the stack
745 // space without decrementing the stack pointer.
746 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
747
748 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
749 CurDAG->ReplaceAllUsesWith(N, Result.Val);
Chris Lattner5f12cf12005-09-03 00:53:47 +0000750 return SDOperand(Result.Val, Op.ResNo);
Chris Lattner1cbbe102005-08-29 23:30:11 +0000751 }
Chris Lattnerb2854fa2005-08-26 20:25:03 +0000752 case PPCISD::FSEL:
753 CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0),
754 Select(N->getOperand(0)),
755 Select(N->getOperand(1)),
756 Select(N->getOperand(2)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000757 return SDOperand(N, 0);
Nate Begeman60952142005-09-06 22:03:27 +0000758 case PPCISD::FCFID:
759 CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0),
760 Select(N->getOperand(0)));
761 return SDOperand(N, 0);
762 case PPCISD::FCTIDZ:
763 CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0),
764 Select(N->getOperand(0)));
765 return SDOperand(N, 0);
Chris Lattnerda2e04c2005-08-31 21:09:52 +0000766 case PPCISD::FCTIWZ:
767 CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0),
768 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000769 return SDOperand(N, 0);
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000770 case ISD::FADD: {
771 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000772 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000773 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattner43ff01e2005-08-17 19:33:03 +0000774 N->getOperand(0).Val->hasOneUse()) {
775 ++FusedFP; // Statistic
Chris Lattner2091a362005-08-26 16:36:26 +0000776 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000777 Select(N->getOperand(0).getOperand(0)),
778 Select(N->getOperand(0).getOperand(1)),
779 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000780 return SDOperand(N, 0);
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000781 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattner43ff01e2005-08-17 19:33:03 +0000782 N->getOperand(1).hasOneUse()) {
783 ++FusedFP; // Statistic
Chris Lattner2091a362005-08-26 16:36:26 +0000784 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000785 Select(N->getOperand(1).getOperand(0)),
786 Select(N->getOperand(1).getOperand(1)),
787 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000788 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000789 }
790 }
791
Chris Lattner2091a362005-08-26 16:36:26 +0000792 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000793 Select(N->getOperand(0)), Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000794 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000795 }
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000796 case ISD::FSUB: {
797 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000798
799 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000800 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattner43ff01e2005-08-17 19:33:03 +0000801 N->getOperand(0).Val->hasOneUse()) {
802 ++FusedFP; // Statistic
Chris Lattner2091a362005-08-26 16:36:26 +0000803 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000804 Select(N->getOperand(0).getOperand(0)),
805 Select(N->getOperand(0).getOperand(1)),
806 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000807 return SDOperand(N, 0);
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000808 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattner43ff01e2005-08-17 19:33:03 +0000809 N->getOperand(1).Val->hasOneUse()) {
810 ++FusedFP; // Statistic
Chris Lattner2091a362005-08-26 16:36:26 +0000811 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000812 Select(N->getOperand(1).getOperand(0)),
813 Select(N->getOperand(1).getOperand(1)),
814 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000815 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000816 }
817 }
Chris Lattner2091a362005-08-26 16:36:26 +0000818 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattner43ff01e2005-08-17 19:33:03 +0000819 Select(N->getOperand(0)),
820 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000821 return SDOperand(N, 0);
Nate Begeman3fcf47d2005-08-17 23:46:35 +0000822 }
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000823 case ISD::FMUL:
Nate Begeman74d55292005-08-18 00:21:41 +0000824 case ISD::MUL: {
825 unsigned Imm, Opc;
826 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
Chris Lattner2091a362005-08-26 16:36:26 +0000827 CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32,
Nate Begeman74d55292005-08-18 00:21:41 +0000828 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000829 return SDOperand(N, 0);
Nate Begeman74d55292005-08-18 00:21:41 +0000830 }
831 switch (N->getValueType(0)) {
832 default: assert(0 && "Unhandled multiply type!");
833 case MVT::i32: Opc = PPC::MULLW; break;
834 case MVT::f32: Opc = PPC::FMULS; break;
835 case MVT::f64: Opc = PPC::FMUL; break;
836 }
Chris Lattner2091a362005-08-26 16:36:26 +0000837 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Nate Begeman74d55292005-08-18 00:21:41 +0000838 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000839 return SDOperand(N, 0);
Nate Begeman74d55292005-08-18 00:21:41 +0000840 }
Chris Lattnerd3ea19b2005-09-28 22:29:58 +0000841 case ISD::SDIV:
842 case ISD::FDIV: {
Chris Lattnerdc664572005-08-25 17:50:06 +0000843 unsigned Imm;
844 if (isIntImmediate(N->getOperand(1), Imm)) {
845 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
846 SDOperand Op =
847 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
848 Select(N->getOperand(0)),
849 getI32Imm(Log2_32(Imm)));
Chris Lattner2091a362005-08-26 16:36:26 +0000850 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattnerdc664572005-08-25 17:50:06 +0000851 Op.getValue(0), Op.getValue(1));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000852 return SDOperand(N, 0);
Chris Lattnerdc664572005-08-25 17:50:06 +0000853 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
854 SDOperand Op =
Chris Lattner45706e92005-08-30 17:13:58 +0000855 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Chris Lattnerdc664572005-08-25 17:50:06 +0000856 Select(N->getOperand(0)),
857 getI32Imm(Log2_32(-Imm)));
858 SDOperand PT =
Chris Lattner45706e92005-08-30 17:13:58 +0000859 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0),
860 Op.getValue(1));
Chris Lattner2091a362005-08-26 16:36:26 +0000861 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner5f12cf12005-09-03 00:53:47 +0000862 return SDOperand(N, 0);
Chris Lattner6e184f22005-08-25 22:04:30 +0000863 } else if (Imm) {
864 SDOperand Result = Select(BuildSDIVSequence(N));
865 assert(Result.ResNo == 0);
Chris Lattnera9e6a822005-08-26 18:37:23 +0000866 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner6e184f22005-08-25 22:04:30 +0000867 N = Result.Val;
Chris Lattner5f12cf12005-09-03 00:53:47 +0000868 return SDOperand(N, 0);
Chris Lattnerdc664572005-08-25 17:50:06 +0000869 }
870 }
Chris Lattner6e184f22005-08-25 22:04:30 +0000871
872 unsigned Opc;
873 switch (N->getValueType(0)) {
Chris Lattner7bbdae52005-08-26 16:38:51 +0000874 default: assert(0 && "Unknown type to ISD::SDIV");
Chris Lattner6e184f22005-08-25 22:04:30 +0000875 case MVT::i32: Opc = PPC::DIVW; break;
876 case MVT::f32: Opc = PPC::FDIVS; break;
877 case MVT::f64: Opc = PPC::FDIV; break;
878 }
Chris Lattner2091a362005-08-26 16:36:26 +0000879 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
Chris Lattner6e184f22005-08-25 22:04:30 +0000880 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000881 return SDOperand(N, 0);
Chris Lattner6e184f22005-08-25 22:04:30 +0000882 }
883 case ISD::UDIV: {
884 // If this is a divide by constant, we can emit code using some magic
885 // constants to implement it as a multiply instead.
886 unsigned Imm;
Chris Lattner02884fe2005-08-25 23:21:06 +0000887 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner6e184f22005-08-25 22:04:30 +0000888 SDOperand Result = Select(BuildUDIVSequence(N));
889 assert(Result.ResNo == 0);
Chris Lattnera9e6a822005-08-26 18:37:23 +0000890 CurDAG->ReplaceAllUsesWith(Op, Result);
Chris Lattner6e184f22005-08-25 22:04:30 +0000891 N = Result.Val;
Chris Lattner5f12cf12005-09-03 00:53:47 +0000892 return SDOperand(N, 0);
Chris Lattner6e184f22005-08-25 22:04:30 +0000893 }
894
Chris Lattner2091a362005-08-26 16:36:26 +0000895 CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)),
Chris Lattner6e184f22005-08-25 22:04:30 +0000896 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000897 return SDOperand(N, 0);
Chris Lattner6e184f22005-08-25 22:04:30 +0000898 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000899 case ISD::AND: {
Nate Begemand3263872005-08-18 18:01:39 +0000900 unsigned Imm;
Nate Begemanb3821a32005-08-18 07:30:46 +0000901 // If this is an and of a value rotated between 0 and 31 bits and then and'd
902 // with a mask, emit rlwinm
903 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
904 isShiftedMask_32(~Imm))) {
905 SDOperand Val;
Nate Begemand3263872005-08-18 18:01:39 +0000906 unsigned SH, MB, ME;
Nate Begemanb3821a32005-08-18 07:30:46 +0000907 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
908 Val = Select(N->getOperand(0).getOperand(0));
909 } else {
910 Val = Select(N->getOperand(0));
911 isRunOfOnes(Imm, MB, ME);
912 SH = 0;
913 }
Chris Lattner2091a362005-08-26 16:36:26 +0000914 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemanb3821a32005-08-18 07:30:46 +0000915 getI32Imm(MB), getI32Imm(ME));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000916 return SDOperand(N, 0);
Nate Begemanb3821a32005-08-18 07:30:46 +0000917 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000918 // Finally, check for the case where we are being asked to select
919 // and (not(a), b) or and (a, not(b)) which can be selected as andc.
920 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2091a362005-08-26 16:36:26 +0000921 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)),
Nate Begemanb3821a32005-08-18 07:30:46 +0000922 Select(N->getOperand(0).getOperand(0)));
923 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2091a362005-08-26 16:36:26 +0000924 CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)),
Nate Begemanb3821a32005-08-18 07:30:46 +0000925 Select(N->getOperand(1).getOperand(0)));
926 else
Chris Lattner2091a362005-08-26 16:36:26 +0000927 CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)),
Nate Begemanb3821a32005-08-18 07:30:46 +0000928 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000929 return SDOperand(N, 0);
Nate Begemanb3821a32005-08-18 07:30:46 +0000930 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000931 case ISD::OR:
932 if (SDNode *I = SelectBitfieldInsert(N)) {
Chris Lattnera9e6a822005-08-26 18:37:23 +0000933 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman93c4bc62005-08-19 00:38:14 +0000934 N = I;
Chris Lattner5f12cf12005-09-03 00:53:47 +0000935 return SDOperand(N, 0);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000936 }
937 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
938 N->getOperand(1),
939 PPC::ORIS, PPC::ORI)) {
Chris Lattnera9e6a822005-08-26 18:37:23 +0000940 CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0));
Nate Begeman93c4bc62005-08-19 00:38:14 +0000941 N = I;
Chris Lattner5f12cf12005-09-03 00:53:47 +0000942 return SDOperand(N, 0);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000943 }
944 // Finally, check for the case where we are being asked to select
945 // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc.
946 if (isOprNot(N->getOperand(0).Val))
Chris Lattner2091a362005-08-26 16:36:26 +0000947 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)),
Nate Begeman93c4bc62005-08-19 00:38:14 +0000948 Select(N->getOperand(0).getOperand(0)));
949 else if (isOprNot(N->getOperand(1).Val))
Chris Lattner2091a362005-08-26 16:36:26 +0000950 CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)),
Nate Begeman93c4bc62005-08-19 00:38:14 +0000951 Select(N->getOperand(1).getOperand(0)));
952 else
Chris Lattner2091a362005-08-26 16:36:26 +0000953 CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)),
Nate Begeman93c4bc62005-08-19 00:38:14 +0000954 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000955 return SDOperand(N, 0);
Nate Begeman33acb2c2005-08-18 23:38:00 +0000956 case ISD::SHL: {
957 unsigned Imm, SH, MB, ME;
958 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
959 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2091a362005-08-26 16:36:26 +0000960 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begeman33acb2c2005-08-18 23:38:00 +0000961 Select(N->getOperand(0).getOperand(0)),
962 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
963 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2091a362005-08-26 16:36:26 +0000964 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begeman33acb2c2005-08-18 23:38:00 +0000965 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
966 else
Chris Lattner2091a362005-08-26 16:36:26 +0000967 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman33acb2c2005-08-18 23:38:00 +0000968 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000969 return SDOperand(N, 0);
Nate Begeman33acb2c2005-08-18 23:38:00 +0000970 }
971 case ISD::SRL: {
972 unsigned Imm, SH, MB, ME;
973 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
974 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2091a362005-08-26 16:36:26 +0000975 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begeman33acb2c2005-08-18 23:38:00 +0000976 Select(N->getOperand(0).getOperand(0)),
Nate Begeman60952142005-09-06 22:03:27 +0000977 getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME));
Nate Begeman33acb2c2005-08-18 23:38:00 +0000978 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2091a362005-08-26 16:36:26 +0000979 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begeman60952142005-09-06 22:03:27 +0000980 getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm),
981 getI32Imm(31));
Nate Begeman33acb2c2005-08-18 23:38:00 +0000982 else
Chris Lattner2091a362005-08-26 16:36:26 +0000983 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman33acb2c2005-08-18 23:38:00 +0000984 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +0000985 return SDOperand(N, 0);
Nate Begeman33acb2c2005-08-18 23:38:00 +0000986 }
987 case ISD::SRA: {
988 unsigned Imm, SH, MB, ME;
989 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
990 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2091a362005-08-26 16:36:26 +0000991 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begeman33acb2c2005-08-18 23:38:00 +0000992 Select(N->getOperand(0).getOperand(0)),
993 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
994 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2091a362005-08-26 16:36:26 +0000995 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begeman33acb2c2005-08-18 23:38:00 +0000996 getI32Imm(Imm));
997 else
Chris Lattner2091a362005-08-26 16:36:26 +0000998 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begeman33acb2c2005-08-18 23:38:00 +0000999 Select(N->getOperand(1)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001000 return SDOperand(N, 0);
Nate Begeman33acb2c2005-08-18 23:38:00 +00001001 }
Nate Begeman4bfb4a22005-08-18 03:04:18 +00001002 case ISD::FABS:
Chris Lattner2091a362005-08-26 16:36:26 +00001003 CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0),
Nate Begeman457367f2005-08-18 00:53:47 +00001004 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001005 return SDOperand(N, 0);
Chris Lattner61f7c3e2005-08-30 00:30:43 +00001006 case ISD::FP_EXTEND:
Nate Begeman4bfb4a22005-08-18 03:04:18 +00001007 assert(MVT::f64 == N->getValueType(0) &&
1008 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner61f7c3e2005-08-30 00:30:43 +00001009 // We need to emit an FMR to make sure that the result has the right value
1010 // type.
1011 CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001012 return SDOperand(N, 0);
Nate Begeman4bfb4a22005-08-18 03:04:18 +00001013 case ISD::FP_ROUND:
1014 assert(MVT::f32 == N->getValueType(0) &&
1015 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2091a362005-08-26 16:36:26 +00001016 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001017 return SDOperand(N, 0);
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001018 case ISD::FNEG: {
1019 SDOperand Val = Select(N->getOperand(0));
1020 MVT::ValueType Ty = N->getValueType(0);
1021 if (Val.Val->hasOneUse()) {
1022 unsigned Opc;
Chris Lattner38660c62005-08-28 23:39:22 +00001023 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001024 default: Opc = 0; break;
1025 case PPC::FABS: Opc = PPC::FNABS; break;
1026 case PPC::FMADD: Opc = PPC::FNMADD; break;
1027 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1028 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1029 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1030 }
1031 // If we inverted the opcode, then emit the new instruction with the
1032 // inverted opcode and the original instruction's operands. Otherwise,
1033 // fall through and generate a fneg instruction.
1034 if (Opc) {
1035 if (PPC::FNABS == Opc)
Chris Lattner2091a362005-08-26 16:36:26 +00001036 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001037 else
Chris Lattner2091a362005-08-26 16:36:26 +00001038 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001039 Val.getOperand(1), Val.getOperand(2));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001040 return SDOperand(N, 0);
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001041 }
1042 }
Chris Lattner2091a362005-08-26 16:36:26 +00001043 CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val);
Chris Lattner5f12cf12005-09-03 00:53:47 +00001044 return SDOperand(N, 0);
Nate Begeman3fcf47d2005-08-17 23:46:35 +00001045 }
Nate Begeman457367f2005-08-18 00:53:47 +00001046 case ISD::FSQRT: {
1047 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2091a362005-08-26 16:36:26 +00001048 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman457367f2005-08-18 00:53:47 +00001049 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001050 return SDOperand(N, 0);
Nate Begeman457367f2005-08-18 00:53:47 +00001051 }
Chris Lattner02884fe2005-08-25 23:21:06 +00001052
1053 case ISD::ADD_PARTS: {
1054 SDOperand LHSL = Select(N->getOperand(0));
1055 SDOperand LHSH = Select(N->getOperand(1));
1056
1057 unsigned Imm;
Chris Lattner7bbdae52005-08-26 16:38:51 +00001058 bool ME = false, ZE = false;
Chris Lattner02884fe2005-08-25 23:21:06 +00001059 if (isIntImmediate(N->getOperand(3), Imm)) {
1060 ME = (signed)Imm == -1;
1061 ZE = Imm == 0;
1062 }
1063
1064 std::vector<SDOperand> Result;
1065 SDOperand CarryFromLo;
1066 if (isIntImmediate(N->getOperand(2), Imm) &&
1067 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
1068 // Codegen the low 32 bits of the add. Interestingly, there is no
1069 // shifted form of add immediate carrying.
1070 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1071 LHSL, getI32Imm(Imm));
1072 } else {
1073 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
1074 LHSL, Select(N->getOperand(2)));
1075 }
Chris Lattner02884fe2005-08-25 23:21:06 +00001076 CarryFromLo = CarryFromLo.getValue(1);
1077
1078 // Codegen the high 32 bits, adding zero, minus one, or the full value
1079 // along with the carry flag produced by addc/addic.
1080 SDOperand ResultHi;
1081 if (ZE)
1082 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
1083 else if (ME)
1084 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
1085 else
1086 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
1087 Select(N->getOperand(3)), CarryFromLo);
Chris Lattnerb81431b2005-08-25 23:36:49 +00001088 Result.push_back(CarryFromLo.getValue(0));
Chris Lattnerbdf3d3d2005-08-30 17:40:13 +00001089 Result.push_back(ResultHi);
Chris Lattner02884fe2005-08-25 23:21:06 +00001090 CurDAG->ReplaceAllUsesWith(N, Result);
1091 return Result[Op.ResNo];
1092 }
1093 case ISD::SUB_PARTS: {
1094 SDOperand LHSL = Select(N->getOperand(0));
1095 SDOperand LHSH = Select(N->getOperand(1));
1096 SDOperand RHSL = Select(N->getOperand(2));
1097 SDOperand RHSH = Select(N->getOperand(3));
1098
1099 std::vector<SDOperand> Result;
1100 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
1101 RHSL, LHSL));
1102 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
1103 Result[0].getValue(1)));
1104 CurDAG->ReplaceAllUsesWith(N, Result);
1105 return Result[Op.ResNo];
1106 }
1107
Chris Lattnerc5292ec2005-08-21 22:31:09 +00001108 case ISD::LOAD:
1109 case ISD::EXTLOAD:
1110 case ISD::ZEXTLOAD:
1111 case ISD::SEXTLOAD: {
1112 SDOperand Op1, Op2;
1113 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1114
1115 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1116 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1117 unsigned Opc;
1118 switch (TypeBeingLoaded) {
1119 default: N->dump(); assert(0 && "Cannot load this type!");
1120 case MVT::i1:
1121 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1122 case MVT::i16:
1123 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1124 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1125 } else {
1126 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1127 }
1128 break;
1129 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1130 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1131 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1132 }
1133
Chris Lattner2091a362005-08-26 16:36:26 +00001134 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattnerc5292ec2005-08-21 22:31:09 +00001135 Op1, Op2, Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001136 return SDOperand(N, Op.ResNo);
Chris Lattnerc5292ec2005-08-21 22:31:09 +00001137 }
1138
Chris Lattnerca0c0d72005-08-22 01:27:59 +00001139 case ISD::TRUNCSTORE:
1140 case ISD::STORE: {
1141 SDOperand AddrOp1, AddrOp2;
1142 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1143
1144 unsigned Opc;
1145 if (N->getOpcode() == ISD::STORE) {
1146 switch (N->getOperand(1).getValueType()) {
1147 default: assert(0 && "unknown Type in store");
1148 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1149 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1150 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1151 }
1152 } else { //ISD::TRUNCSTORE
1153 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1154 default: assert(0 && "unknown Type in store");
Chris Lattnerca0c0d72005-08-22 01:27:59 +00001155 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1156 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1157 }
1158 }
Chris Lattnerd83cd352005-08-24 22:45:17 +00001159
Chris Lattner2091a362005-08-26 16:36:26 +00001160 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerca0c0d72005-08-22 01:27:59 +00001161 AddrOp1, AddrOp2, Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001162 return SDOperand(N, 0);
Chris Lattnerca0c0d72005-08-22 01:27:59 +00001163 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001164
1165 case ISD::SETCC: {
1166 unsigned Imm;
1167 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1168 if (isIntImmediate(N->getOperand(1), Imm)) {
1169 // We can codegen setcc op, imm very efficiently compared to a brcond.
1170 // Check for those cases here.
1171 // setcc op, 0
1172 if (Imm == 0) {
1173 SDOperand Op = Select(N->getOperand(0));
1174 switch (CC) {
1175 default: assert(0 && "Unhandled SetCC condition"); abort();
1176 case ISD::SETEQ:
1177 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner2091a362005-08-26 16:36:26 +00001178 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001179 getI32Imm(5), getI32Imm(31));
1180 break;
1181 case ISD::SETNE: {
1182 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1183 Op, getI32Imm(~0U));
Chris Lattner2091a362005-08-26 16:36:26 +00001184 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001185 break;
1186 }
1187 case ISD::SETLT:
Chris Lattner2091a362005-08-26 16:36:26 +00001188 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001189 getI32Imm(31), getI32Imm(31));
1190 break;
1191 case ISD::SETGT: {
1192 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
1193 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner2091a362005-08-26 16:36:26 +00001194 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001195 getI32Imm(31), getI32Imm(31));
1196 break;
1197 }
1198 }
Chris Lattner5f12cf12005-09-03 00:53:47 +00001199 return SDOperand(N, 0);
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001200 } else if (Imm == ~0U) { // setcc op, -1
1201 SDOperand Op = Select(N->getOperand(0));
1202 switch (CC) {
1203 default: assert(0 && "Unhandled SetCC condition"); abort();
1204 case ISD::SETEQ:
1205 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1206 Op, getI32Imm(1));
Chris Lattner2091a362005-08-26 16:36:26 +00001207 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001208 CurDAG->getTargetNode(PPC::LI, MVT::i32,
1209 getI32Imm(0)),
1210 Op.getValue(1));
1211 break;
1212 case ISD::SETNE: {
1213 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
Chris Lattner12357282005-08-29 23:49:25 +00001214 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1215 Op, getI32Imm(~0U));
Chris Lattner2091a362005-08-26 16:36:26 +00001216 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001217 break;
1218 }
1219 case ISD::SETLT: {
1220 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
1221 getI32Imm(1));
1222 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner2091a362005-08-26 16:36:26 +00001223 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001224 getI32Imm(31), getI32Imm(31));
1225 break;
1226 }
1227 case ISD::SETGT:
1228 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
1229 getI32Imm(31), getI32Imm(31));
Chris Lattner2091a362005-08-26 16:36:26 +00001230 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001231 break;
1232 }
Chris Lattner5f12cf12005-09-03 00:53:47 +00001233 return SDOperand(N, 0);
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001234 }
1235 }
1236
1237 bool Inv;
1238 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Chris Lattner34182af2005-09-01 19:20:44 +00001239 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001240 SDOperand IntCR;
Chris Lattnerb746dd12005-08-25 21:39:42 +00001241
1242 // Force the ccreg into CR7.
1243 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
1244
1245 std::vector<MVT::ValueType> VTs;
1246 VTs.push_back(MVT::Other);
1247 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
1248 std::vector<SDOperand> Ops;
1249 Ops.push_back(CurDAG->getEntryNode());
1250 Ops.push_back(CR7Reg);
1251 Ops.push_back(CCReg);
1252 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
1253
1254 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1255 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
1256 else
1257 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001258
1259 if (!Inv) {
Chris Lattner2091a362005-08-26 16:36:26 +00001260 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001261 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
1262 } else {
1263 SDOperand Tmp =
1264 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
1265 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
Chris Lattner2091a362005-08-26 16:36:26 +00001266 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001267 }
1268
Chris Lattner5f12cf12005-09-03 00:53:47 +00001269 return SDOperand(N, 0);
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001270 }
Chris Lattnerb6d034a2005-08-24 00:47:15 +00001271
Chris Lattnerbec817c2005-08-26 18:46:49 +00001272 case ISD::SELECT_CC: {
1273 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1274
1275 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1276 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1277 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1278 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1279 if (N1C->isNullValue() && N3C->isNullValue() &&
1280 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1281 SDOperand LHS = Select(N->getOperand(0));
1282 SDOperand Tmp =
1283 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1284 LHS, getI32Imm(~0U));
1285 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1286 Tmp.getValue(1));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001287 return SDOperand(N, 0);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001288 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001289
Chris Lattner34182af2005-09-01 19:20:44 +00001290 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001291 unsigned BROpc = getBCCForSetCC(CC);
1292
1293 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
1294 unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int;
1295 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1296 Select(N->getOperand(2)), Select(N->getOperand(3)),
1297 getI32Imm(BROpc));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001298 return SDOperand(N, 0);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001299 }
1300
Chris Lattnerb6d034a2005-08-24 00:47:15 +00001301 case ISD::CALLSEQ_START:
1302 case ISD::CALLSEQ_END: {
1303 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1304 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1305 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2091a362005-08-26 16:36:26 +00001306 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerd83cd352005-08-24 22:45:17 +00001307 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001308 return SDOperand(N, 0);
Chris Lattnerb6d034a2005-08-24 00:47:15 +00001309 }
Chris Lattnerd83cd352005-08-24 22:45:17 +00001310 case ISD::CALL:
1311 case ISD::TAILCALL: {
1312 SDOperand Chain = Select(N->getOperand(0));
1313
1314 unsigned CallOpcode;
1315 std::vector<SDOperand> CallOperands;
1316
1317 if (GlobalAddressSDNode *GASD =
1318 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
1319 CallOpcode = PPC::CALLpcrel;
1320 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
1321 MVT::i32));
1322 } else if (ExternalSymbolSDNode *ESSDN =
1323 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
1324 CallOpcode = PPC::CALLpcrel;
1325 CallOperands.push_back(N->getOperand(1));
1326 } else {
1327 // Copy the callee address into the CTR register.
1328 SDOperand Callee = Select(N->getOperand(1));
1329 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
1330
1331 // Copy the callee address into R12 on darwin.
1332 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
Chris Lattner3ccad3f2005-08-29 00:26:57 +00001333 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Chris Lattnerd83cd352005-08-24 22:45:17 +00001334
1335 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
1336 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
1337 CallOperands.push_back(R12);
1338 CallOpcode = PPC::CALLindirect;
1339 }
1340
1341 unsigned GPR_idx = 0, FPR_idx = 0;
1342 static const unsigned GPR[] = {
1343 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1344 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1345 };
1346 static const unsigned FPR[] = {
1347 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1348 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1349 };
1350
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001351 SDOperand InFlag; // Null incoming flag value.
1352
Chris Lattnerb2b41852005-08-29 22:22:57 +00001353 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1354 unsigned DestReg = 0;
Chris Lattner69e9a9a2005-08-30 22:59:48 +00001355 MVT::ValueType RegTy = N->getOperand(i).getValueType();
1356 if (RegTy == MVT::i32) {
Chris Lattnerb2b41852005-08-29 22:22:57 +00001357 assert(GPR_idx < 8 && "Too many int args");
1358 DestReg = GPR[GPR_idx++];
Chris Lattnerb2b41852005-08-29 22:22:57 +00001359 } else {
1360 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
1361 "Unpromoted integer arg?");
1362 assert(FPR_idx < 13 && "Too many fp args");
1363 DestReg = FPR[FPR_idx++];
Chris Lattnerb2b41852005-08-29 22:22:57 +00001364 }
1365
Chris Lattnerd83cd352005-08-24 22:45:17 +00001366 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Chris Lattner8f8d5392005-08-30 21:28:19 +00001367 SDOperand Val = Select(N->getOperand(i));
Chris Lattner8f8d5392005-08-30 21:28:19 +00001368 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001369 InFlag = Chain.getValue(1);
1370 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
Chris Lattnerd83cd352005-08-24 22:45:17 +00001371 }
Chris Lattnerb2b41852005-08-29 22:22:57 +00001372 }
Chris Lattnerd83cd352005-08-24 22:45:17 +00001373
1374 // Finally, once everything is in registers to pass to the call, emit the
1375 // call itself.
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001376 if (InFlag.Val)
1377 CallOperands.push_back(InFlag); // Strong dep on register copies.
1378 else
1379 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
1380 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
1381 CallOperands);
Chris Lattnerd83cd352005-08-24 22:45:17 +00001382
1383 std::vector<SDOperand> CallResults;
1384
1385 // If the call has results, copy the values out of the ret val registers.
1386 switch (N->getValueType(0)) {
1387 default: assert(0 && "Unexpected ret value!");
1388 case MVT::Other: break;
1389 case MVT::i32:
1390 if (N->getValueType(1) == MVT::i32) {
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001391 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
1392 Chain.getValue(1)).getValue(1);
Chris Lattnerd83cd352005-08-24 22:45:17 +00001393 CallResults.push_back(Chain.getValue(0));
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001394 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1395 Chain.getValue(1)).getValue(1);
Chris Lattnerd83cd352005-08-24 22:45:17 +00001396 CallResults.push_back(Chain.getValue(0));
1397 } else {
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001398 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1399 Chain.getValue(1)).getValue(1);
Chris Lattnerd83cd352005-08-24 22:45:17 +00001400 CallResults.push_back(Chain.getValue(0));
1401 }
1402 break;
1403 case MVT::f32:
1404 case MVT::f64:
Chris Lattner69e9a9a2005-08-30 22:59:48 +00001405 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
Chris Lattner7a59b1c2005-08-30 01:57:02 +00001406 Chain.getValue(1)).getValue(1);
Chris Lattner69e9a9a2005-08-30 22:59:48 +00001407 CallResults.push_back(Chain.getValue(0));
Chris Lattnerd83cd352005-08-24 22:45:17 +00001408 break;
1409 }
1410
1411 CallResults.push_back(Chain);
1412 CurDAG->ReplaceAllUsesWith(N, CallResults);
1413 return CallResults[Op.ResNo];
1414 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001415 case ISD::RET: {
1416 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1417
Chris Lattnerf4d59432005-08-31 01:34:29 +00001418 if (N->getNumOperands() == 2) {
Chris Lattner43ff01e2005-08-17 19:33:03 +00001419 SDOperand Val = Select(N->getOperand(1));
Chris Lattner69e9a9a2005-08-30 22:59:48 +00001420 if (N->getOperand(1).getValueType() == MVT::i32) {
Chris Lattner43ff01e2005-08-17 19:33:03 +00001421 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
Chris Lattner69e9a9a2005-08-30 22:59:48 +00001422 } else {
1423 assert(MVT::isFloatingPoint(N->getOperand(1).getValueType()));
1424 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001425 }
Chris Lattnerf4d59432005-08-31 01:34:29 +00001426 } else if (N->getNumOperands() > 1) {
1427 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1428 N->getOperand(2).getValueType() == MVT::i32 &&
1429 N->getNumOperands() == 3 && "Unknown two-register ret value!");
1430 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1)));
1431 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2)));
Chris Lattner43ff01e2005-08-17 19:33:03 +00001432 }
1433
1434 // Finally, select this to a blr (return) instruction.
Chris Lattner2091a362005-08-26 16:36:26 +00001435 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattner5f12cf12005-09-03 00:53:47 +00001436 return SDOperand(N, 0);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001437 }
Chris Lattner66a6a132005-08-25 00:29:58 +00001438 case ISD::BR:
Chris Lattner2091a362005-08-26 16:36:26 +00001439 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner66a6a132005-08-25 00:29:58 +00001440 Select(N->getOperand(0)));
Chris Lattner5f12cf12005-09-03 00:53:47 +00001441 return SDOperand(N, 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001442 case ISD::BR_CC:
1443 case ISD::BRTWOWAY_CC: {
1444 SDOperand Chain = Select(N->getOperand(0));
1445 MachineBasicBlock *Dest =
1446 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1447 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1448 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1449 unsigned Opc = getBCCForSetCC(CC);
1450
1451 // If this is a two way branch, then grab the fallthrough basic block
1452 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1453 // conversion if necessary by the branch selection pass. Otherwise, emit a
1454 // standard conditional branch.
1455 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
1456 MachineBasicBlock *Fallthrough =
1457 cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock();
1458 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1459 CondCode, getI32Imm(Opc),
1460 N->getOperand(4), N->getOperand(5),
1461 Chain);
Chris Lattner2091a362005-08-26 16:36:26 +00001462 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001463 } else {
1464 // Iterate to the next basic block
1465 ilist<MachineBasicBlock>::iterator It = BB;
1466 ++It;
1467
1468 // If the fallthrough path is off the end of the function, which would be
1469 // undefined behavior, set it to be the same as the current block because
1470 // we have nothing better to set it to, and leaving it alone will cause
1471 // the PowerPC Branch Selection pass to crash.
1472 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2091a362005-08-26 16:36:26 +00001473 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattner2a1823d2005-08-21 18:50:37 +00001474 getI32Imm(Opc), N->getOperand(4),
1475 CurDAG->getBasicBlock(It), Chain);
1476 }
Chris Lattner5f12cf12005-09-03 00:53:47 +00001477 return SDOperand(N, 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001478 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001479 }
Chris Lattner5f12cf12005-09-03 00:53:47 +00001480
Chris Lattner498915d2005-09-07 23:45:15 +00001481 return SelectCode(Op);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001482}
1483
1484
1485/// createPPC32ISelDag - This pass converts a legalized DAG into a
1486/// PowerPC-specific DAG, ready for instruction scheduling.
1487///
1488FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1489 return new PPC32DAGToDAGISel(TM);
1490}
1491