blob: 687b119272710cbbd80e007078cbb018cd7fdc99 [file] [log] [blame]
Chris Lattnera5a91b12005-08-17 19:33:03 +00001//===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC,
11// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PowerPC.h"
16#include "PPC32TargetMachine.h"
17#include "PPC32ISelLowering.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000025#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000026#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
29using namespace llvm;
30
31namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000032 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
33 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
34
35 //===--------------------------------------------------------------------===//
36 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class PPC32DAGToDAGISel : public SelectionDAGISel {
40 PPC32TargetLowering PPC32Lowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000041 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000042 public:
43 PPC32DAGToDAGISel(TargetMachine &TM)
44 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
45
Chris Lattner4416f1a2005-08-19 22:38:53 +000046 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
48 GlobalBaseReg = 0;
49 return SelectionDAGISel::runOnFunction(Fn);
50 }
51
Chris Lattnera5a91b12005-08-17 19:33:03 +000052 /// getI32Imm - Return a target constant with the specified value, of type
53 /// i32.
54 inline SDOperand getI32Imm(unsigned Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i32);
56 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000057
58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
59 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000060 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000061
62 // Select - Convert the specified operand from a target-independent to a
63 // target-specific node if it hasn't already been changed.
64 SDOperand Select(SDOperand Op);
65
66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
69 bool Negate = false);
Nate Begeman02b88a42005-08-19 00:38:14 +000070 SDNode *SelectBitfieldInsert(SDNode *N);
71
Chris Lattner2fbb4572005-08-21 18:50:37 +000072 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
75
Chris Lattner9944b762005-08-21 22:31:09 +000076 /// SelectAddr - Given the specified address, return the two operands for a
77 /// load/store instruction, and return true if it should be an indexed [r+r]
78 /// operation.
79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
80
Chris Lattner047b9522005-08-25 22:04:30 +000081 SDOperand BuildSDIVSequence(SDNode *N);
82 SDOperand BuildUDIVSequence(SDNode *N);
83
Chris Lattnera5a91b12005-08-17 19:33:03 +000084 /// InstructionSelectBasicBlock - This callback is invoked by
85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +000086 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
87
Chris Lattnera5a91b12005-08-17 19:33:03 +000088 virtual const char *getPassName() const {
89 return "PowerPC DAG->DAG Pattern Instruction Selection";
90 }
Chris Lattneraf165382005-09-13 22:03:06 +000091
92// Include the pieces autogenerated from the target description.
93#include "PPC32GenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +000094
95private:
96 SDOperand SelectDYNAMIC_STACKALLOC(SDOperand N);
Chris Lattner2b63e4c2005-10-06 18:56:10 +000097 SDOperand SelectADD_PARTS(SDOperand N);
98 SDOperand SelectSUB_PARTS(SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +000099 };
100}
101
Chris Lattnerbd937b92005-10-06 18:45:51 +0000102/// InstructionSelectBasicBlock - This callback is invoked by
103/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
104void PPC32DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
105 DEBUG(BB->dump());
106
107 // The selection process is inherently a bottom-up recursive process (users
108 // select their uses before themselves). Given infinite stack space, we
109 // could just start selecting on the root and traverse the whole graph. In
110 // practice however, this causes us to run out of stack space on large basic
111 // blocks. To avoid this problem, select the entry node, then all its uses,
112 // iteratively instead of recursively.
113 std::vector<SDOperand> Worklist;
114 Worklist.push_back(DAG.getEntryNode());
115
116 // Note that we can do this in the PPC target (scanning forward across token
117 // chain edges) because no nodes ever get folded across these edges. On a
118 // target like X86 which supports load/modify/store operations, this would
119 // have to be more careful.
120 while (!Worklist.empty()) {
121 SDOperand Node = Worklist.back();
122 Worklist.pop_back();
123
124 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
125 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
126 CodeGenMap.count(Node)) continue;
127
128 for (SDNode::use_iterator UI = Node.Val->use_begin(),
129 E = Node.Val->use_end(); UI != E; ++UI) {
130 // Scan the values. If this use has a value that is a token chain, add it
131 // to the worklist.
132 SDNode *User = *UI;
133 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
134 if (User->getValueType(i) == MVT::Other) {
135 Worklist.push_back(SDOperand(User, i));
136 break;
137 }
138 }
139
140 // Finally, legalize this node.
141 Select(Node);
142 }
143
144 // Select target instructions for the DAG.
145 DAG.setRoot(Select(DAG.getRoot()));
146 CodeGenMap.clear();
147 DAG.RemoveDeadNodes();
148
149 // Emit machine code to BB.
150 ScheduleAndEmitDAG(DAG);
151}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000152
Chris Lattner4416f1a2005-08-19 22:38:53 +0000153/// getGlobalBaseReg - Output the instructions required to put the
154/// base address to use for accessing globals into a register.
155///
Chris Lattner9944b762005-08-21 22:31:09 +0000156SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000157 if (!GlobalBaseReg) {
158 // Insert the set of GlobalBaseReg into the first MBB of the function
159 MachineBasicBlock &FirstMBB = BB->getParent()->front();
160 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
161 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
162 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
163 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
164 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
165 }
Chris Lattner9944b762005-08-21 22:31:09 +0000166 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000167}
168
169
Nate Begeman0f3257a2005-08-18 05:00:13 +0000170// isIntImmediate - This method tests to see if a constant operand.
171// If so Imm will receive the 32 bit value.
172static bool isIntImmediate(SDNode *N, unsigned& Imm) {
173 if (N->getOpcode() == ISD::Constant) {
174 Imm = cast<ConstantSDNode>(N)->getValue();
175 return true;
176 }
177 return false;
178}
179
Nate Begemancffc32b2005-08-18 07:30:46 +0000180// isOprShiftImm - Returns true if the specified operand is a shift opcode with
181// a immediate shift count less than 32.
182static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
183 Opc = N->getOpcode();
184 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
185 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
186}
187
188// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
189// any number of 0s on either side. The 1s are allowed to wrap from LSB to
190// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
191// not, since all 1s are not contiguous.
192static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
193 if (isShiftedMask_32(Val)) {
194 // look for the first non-zero bit
195 MB = CountLeadingZeros_32(Val);
196 // look for the first zero bit after the run of ones
197 ME = CountLeadingZeros_32((Val - 1) ^ Val);
198 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000199 } else {
200 Val = ~Val; // invert mask
201 if (isShiftedMask_32(Val)) {
202 // effectively look for the first zero bit
203 ME = CountLeadingZeros_32(Val) - 1;
204 // effectively look for the first one bit after the run of zeros
205 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
206 return true;
207 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000208 }
209 // no run present
210 return false;
211}
212
213// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
214// and mask opcode and mask operation.
215static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
216 unsigned &SH, unsigned &MB, unsigned &ME) {
217 unsigned Shift = 32;
218 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
219 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000220 if (N->getNumOperands() != 2 ||
221 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000222 return false;
223
224 if (Opcode == ISD::SHL) {
225 // apply shift left to mask if it comes first
226 if (IsShiftMask) Mask = Mask << Shift;
227 // determine which bits are made indeterminant by shift
228 Indeterminant = ~(0xFFFFFFFFu << Shift);
229 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
230 // apply shift right to mask if it comes first
231 if (IsShiftMask) Mask = Mask >> Shift;
232 // determine which bits are made indeterminant by shift
233 Indeterminant = ~(0xFFFFFFFFu >> Shift);
234 // adjust for the left rotate
235 Shift = 32 - Shift;
236 } else {
237 return false;
238 }
239
240 // if the mask doesn't intersect any Indeterminant bits
241 if (Mask && !(Mask & Indeterminant)) {
242 SH = Shift;
243 // make sure the mask is still a mask (wrap arounds may not be)
244 return isRunOfOnes(Mask, MB, ME);
245 }
246 return false;
247}
248
Nate Begeman0f3257a2005-08-18 05:00:13 +0000249// isOpcWithIntImmediate - This method tests to see if the node is a specific
250// opcode and that it has a immediate integer right operand.
251// If so Imm will receive the 32 bit value.
252static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
253 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
254}
255
256// isOprNot - Returns true if the specified operand is an xor with immediate -1.
257static bool isOprNot(SDNode *N) {
258 unsigned Imm;
259 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
260}
261
Chris Lattnera5a91b12005-08-17 19:33:03 +0000262// Immediate constant composers.
263// Lo16 - grabs the lo 16 bits from a 32 bit constant.
264// Hi16 - grabs the hi 16 bits from a 32 bit constant.
265// HA16 - computes the hi bits required if the lo bits are add/subtracted in
266// arithmethically.
267static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
268static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
269static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
270
271// isIntImmediate - This method tests to see if a constant operand.
272// If so Imm will receive the 32 bit value.
273static bool isIntImmediate(SDOperand N, unsigned& Imm) {
274 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
275 Imm = (unsigned)CN->getSignExtended();
276 return true;
277 }
278 return false;
279}
280
Nate Begeman02b88a42005-08-19 00:38:14 +0000281/// SelectBitfieldInsert - turn an or of two masked values into
282/// the rotate left word immediate then mask insert (rlwimi) instruction.
283/// Returns true on success, false if the caller still needs to select OR.
284///
285/// Patterns matched:
286/// 1. or shl, and 5. or and, and
287/// 2. or and, shl 6. or shl, shr
288/// 3. or shr, and 7. or shr, shl
289/// 4. or and, shr
290SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
291 bool IsRotate = false;
292 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
293 unsigned Value;
294
295 SDOperand Op0 = N->getOperand(0);
296 SDOperand Op1 = N->getOperand(1);
297
298 unsigned Op0Opc = Op0.getOpcode();
299 unsigned Op1Opc = Op1.getOpcode();
300
301 // Verify that we have the correct opcodes
302 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
303 return false;
304 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
305 return false;
306
307 // Generate Mask value for Target
308 if (isIntImmediate(Op0.getOperand(1), Value)) {
309 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000310 case ISD::SHL: TgtMask <<= Value; break;
311 case ISD::SRL: TgtMask >>= Value; break;
312 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000313 }
314 } else {
315 return 0;
316 }
317
318 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000319 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000320 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000321
322 switch(Op1Opc) {
323 case ISD::SHL:
324 SH = Value;
325 InsMask <<= SH;
326 if (Op0Opc == ISD::SRL) IsRotate = true;
327 break;
328 case ISD::SRL:
329 SH = Value;
330 InsMask >>= SH;
331 SH = 32-SH;
332 if (Op0Opc == ISD::SHL) IsRotate = true;
333 break;
334 case ISD::AND:
335 InsMask &= Value;
336 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000337 }
338
339 // If both of the inputs are ANDs and one of them has a logical shift by
340 // constant as its input, make that AND the inserted value so that we can
341 // combine the shift into the rotate part of the rlwimi instruction
342 bool IsAndWithShiftOp = false;
343 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
344 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
345 Op1.getOperand(0).getOpcode() == ISD::SRL) {
346 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
347 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
348 IsAndWithShiftOp = true;
349 }
350 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
351 Op0.getOperand(0).getOpcode() == ISD::SRL) {
352 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
353 std::swap(Op0, Op1);
354 std::swap(TgtMask, InsMask);
355 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
356 IsAndWithShiftOp = true;
357 }
358 }
359 }
360
361 // Verify that the Target mask and Insert mask together form a full word mask
362 // and that the Insert mask is a run of set bits (which implies both are runs
363 // of set bits). Given that, Select the arguments and generate the rlwimi
364 // instruction.
365 unsigned MB, ME;
366 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
367 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
368 bool Op0IsAND = Op0Opc == ISD::AND;
369 // Check for rotlwi / rotrwi here, a special case of bitfield insert
370 // where both bitfield halves are sourced from the same value.
371 if (IsRotate && fullMask &&
372 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
373 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
374 Select(N->getOperand(0).getOperand(0)),
375 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
376 return Op0.Val;
377 }
378 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
379 : Select(Op0);
380 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
381 : Select(Op1.getOperand(0));
382 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
383 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
384 return Op0.Val;
385 }
386 return 0;
387}
388
Chris Lattnera5a91b12005-08-17 19:33:03 +0000389// SelectIntImmediateExpr - Choose code for integer operations with an immediate
390// operand.
391SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
392 unsigned OCHi, unsigned OCLo,
393 bool IsArithmetic,
394 bool Negate) {
395 // Check to make sure this is a constant.
396 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
397 // Exit if not a constant.
398 if (!CN) return 0;
399 // Extract immediate.
400 unsigned C = (unsigned)CN->getValue();
401 // Negate if required (ISD::SUB).
402 if (Negate) C = -C;
403 // Get the hi and lo portions of constant.
404 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
405 unsigned Lo = Lo16(C);
406
407 // If two instructions are needed and usage indicates it would be better to
408 // load immediate into a register, bail out.
409 if (Hi && Lo && CN->use_size() > 2) return false;
410
411 // Select the first operand.
412 SDOperand Opr0 = Select(LHS);
413
414 if (Lo) // Add in the lo-part.
415 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
416 if (Hi) // Add in the hi-part.
417 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
418 return Opr0.Val;
419}
420
Chris Lattner9944b762005-08-21 22:31:09 +0000421/// SelectAddr - Given the specified address, return the two operands for a
422/// load/store instruction, and return true if it should be an indexed [r+r]
423/// operation.
424bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
425 SDOperand &Op2) {
426 unsigned imm = 0;
427 if (Addr.getOpcode() == ISD::ADD) {
428 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
429 Op1 = getI32Imm(Lo16(imm));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000430 if (FrameIndexSDNode *FI =
431 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner9944b762005-08-21 22:31:09 +0000432 ++FrameOff;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000433 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000434 } else {
435 Op2 = Select(Addr.getOperand(0));
436 }
437 return false;
438 } else {
439 Op1 = Select(Addr.getOperand(0));
440 Op2 = Select(Addr.getOperand(1));
441 return true; // [r+r]
442 }
443 }
444
445 // Now check if we're dealing with a global, and whether or not we should emit
446 // an optimized load or store for statics.
447 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
448 GlobalValue *GV = GN->getGlobal();
449 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
450 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
451 if (PICEnabled)
452 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
453 Op1);
454 else
455 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
456 return false;
457 }
Chris Lattnere28e40a2005-08-25 00:45:43 +0000458 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) {
Chris Lattner9944b762005-08-21 22:31:09 +0000459 Op1 = getI32Imm(0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000460 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000461 return false;
462 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
463 Op1 = Addr;
464 if (PICEnabled)
465 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
466 else
467 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
468 return false;
469 }
470 Op1 = getI32Imm(0);
471 Op2 = Select(Addr);
472 return false;
473}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000474
Chris Lattner2fbb4572005-08-21 18:50:37 +0000475/// SelectCC - Select a comparison of the specified values with the specified
476/// condition code, returning the CR# of the expression.
477SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
478 ISD::CondCode CC) {
479 // Always select the LHS.
480 LHS = Select(LHS);
481
482 // Use U to determine whether the SETCC immediate range is signed or not.
483 if (MVT::isInteger(LHS.getValueType())) {
484 bool U = ISD::isUnsignedIntSetCC(CC);
485 unsigned Imm;
486 if (isIntImmediate(RHS, Imm) &&
487 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
488 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
489 LHS, getI32Imm(Lo16(Imm)));
490 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
491 LHS, Select(RHS));
Chris Lattner919c0322005-10-01 01:35:02 +0000492 } else if (LHS.getValueType() == MVT::f32) {
493 return CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000494 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000495 return CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000496 }
497}
498
499/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
500/// to Condition.
501static unsigned getBCCForSetCC(ISD::CondCode CC) {
502 switch (CC) {
503 default: assert(0 && "Unknown condition!"); abort();
504 case ISD::SETEQ: return PPC::BEQ;
505 case ISD::SETNE: return PPC::BNE;
506 case ISD::SETULT:
507 case ISD::SETLT: return PPC::BLT;
508 case ISD::SETULE:
509 case ISD::SETLE: return PPC::BLE;
510 case ISD::SETUGT:
511 case ISD::SETGT: return PPC::BGT;
512 case ISD::SETUGE:
513 case ISD::SETGE: return PPC::BGE;
514 }
515 return 0;
516}
517
Chris Lattner64906a02005-08-25 20:08:18 +0000518/// getCRIdxForSetCC - Return the index of the condition register field
519/// associated with the SetCC condition, and whether or not the field is
520/// treated as inverted. That is, lt = 0; ge = 0 inverted.
521static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
522 switch (CC) {
523 default: assert(0 && "Unknown condition!"); abort();
524 case ISD::SETULT:
525 case ISD::SETLT: Inv = false; return 0;
526 case ISD::SETUGE:
527 case ISD::SETGE: Inv = true; return 0;
528 case ISD::SETUGT:
529 case ISD::SETGT: Inv = false; return 1;
530 case ISD::SETULE:
531 case ISD::SETLE: Inv = true; return 1;
532 case ISD::SETEQ: Inv = false; return 2;
533 case ISD::SETNE: Inv = true; return 2;
534 }
535 return 0;
536}
Chris Lattner9944b762005-08-21 22:31:09 +0000537
Chris Lattner047b9522005-08-25 22:04:30 +0000538// Structure used to return the necessary information to codegen an SDIV as
539// a multiply.
540struct ms {
541 int m; // magic number
542 int s; // shift amount
543};
544
545struct mu {
546 unsigned int m; // magic number
547 int a; // add indicator
548 int s; // shift amount
549};
550
551/// magic - calculate the magic numbers required to codegen an integer sdiv as
552/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
553/// or -1.
554static struct ms magic(int d) {
555 int p;
556 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
557 const unsigned int two31 = 0x80000000U;
558 struct ms mag;
559
560 ad = abs(d);
561 t = two31 + ((unsigned int)d >> 31);
562 anc = t - 1 - t%ad; // absolute value of nc
563 p = 31; // initialize p
564 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
565 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
566 q2 = two31/ad; // initialize q2 = 2p/abs(d)
567 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
568 do {
569 p = p + 1;
570 q1 = 2*q1; // update q1 = 2p/abs(nc)
571 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
572 if (r1 >= anc) { // must be unsigned comparison
573 q1 = q1 + 1;
574 r1 = r1 - anc;
575 }
576 q2 = 2*q2; // update q2 = 2p/abs(d)
577 r2 = 2*r2; // update r2 = rem(2p/abs(d))
578 if (r2 >= ad) { // must be unsigned comparison
579 q2 = q2 + 1;
580 r2 = r2 - ad;
581 }
582 delta = ad - r2;
583 } while (q1 < delta || (q1 == delta && r1 == 0));
584
585 mag.m = q2 + 1;
586 if (d < 0) mag.m = -mag.m; // resulting magic number
587 mag.s = p - 32; // resulting shift
588 return mag;
589}
590
591/// magicu - calculate the magic numbers required to codegen an integer udiv as
592/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
593static struct mu magicu(unsigned d)
594{
595 int p;
596 unsigned int nc, delta, q1, r1, q2, r2;
597 struct mu magu;
598 magu.a = 0; // initialize "add" indicator
599 nc = - 1 - (-d)%d;
600 p = 31; // initialize p
601 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
602 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
603 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
604 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
605 do {
606 p = p + 1;
607 if (r1 >= nc - r1 ) {
608 q1 = 2*q1 + 1; // update q1
609 r1 = 2*r1 - nc; // update r1
610 }
611 else {
612 q1 = 2*q1; // update q1
613 r1 = 2*r1; // update r1
614 }
615 if (r2 + 1 >= d - r2) {
616 if (q2 >= 0x7FFFFFFF) magu.a = 1;
617 q2 = 2*q2 + 1; // update q2
618 r2 = 2*r2 + 1 - d; // update r2
619 }
620 else {
621 if (q2 >= 0x80000000) magu.a = 1;
622 q2 = 2*q2; // update q2
623 r2 = 2*r2 + 1; // update r2
624 }
625 delta = d - 1 - r2;
626 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
627 magu.m = q2 + 1; // resulting magic number
628 magu.s = p - 32; // resulting shift
629 return magu;
630}
631
632/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
633/// return a DAG expression to select that will generate the same value by
634/// multiplying by a magic number. See:
635/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
636SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) {
637 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue();
638 ms magics = magic(d);
639 // Multiply the numerator (operand 0) by the magic value
640 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0),
641 CurDAG->getConstant(magics.m, MVT::i32));
642 // If d > 0 and m < 0, add the numerator
643 if (d > 0 && magics.m < 0)
644 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0));
645 // If d < 0 and m > 0, subtract the numerator.
646 if (d < 0 && magics.m > 0)
647 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0));
648 // Shift right algebraic if shift value is nonzero
649 if (magics.s > 0)
650 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q,
651 CurDAG->getConstant(magics.s, MVT::i32));
652 // Extract the sign bit and add it to the quotient
653 SDOperand T =
654 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32));
655 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T);
656}
657
658/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
659/// return a DAG expression to select that will generate the same value by
660/// multiplying by a magic number. See:
661/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
662SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) {
663 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue();
664 mu magics = magicu(d);
665 // Multiply the numerator (operand 0) by the magic value
666 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0),
667 CurDAG->getConstant(magics.m, MVT::i32));
668 if (magics.a == 0) {
669 return CurDAG->getNode(ISD::SRL, MVT::i32, Q,
670 CurDAG->getConstant(magics.s, MVT::i32));
671 } else {
672 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q);
673 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
674 CurDAG->getConstant(1, MVT::i32));
675 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
676 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
677 CurDAG->getConstant(magics.s-1, MVT::i32));
678 }
679}
680
Chris Lattnerbd937b92005-10-06 18:45:51 +0000681SDOperand PPC32DAGToDAGISel::SelectDYNAMIC_STACKALLOC(SDOperand Op) {
682 SDNode *N = Op.Val;
683
684 // FIXME: We are currently ignoring the requested alignment for handling
685 // greater than the stack alignment. This will need to be revisited at some
686 // point. Align = N.getOperand(2);
687 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
688 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
689 std::cerr << "Cannot allocate stack object with greater alignment than"
690 << " the stack alignment yet!";
691 abort();
692 }
693 SDOperand Chain = Select(N->getOperand(0));
694 SDOperand Amt = Select(N->getOperand(1));
695
696 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
697
698 SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
699 Chain = R1Val.getValue(1);
700
701 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
702 // from the stack pointer, giving us the result pointer.
703 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val);
704
705 // Copy this result back into R1.
706 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
707
708 // Copy this result back out of R1 to make sure we're not using the stack
709 // space without decrementing the stack pointer.
710 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
711
712 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
713 CodeGenMap[Op.getValue(0)] = Result;
714 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
715 return SDOperand(Result.Val, Op.ResNo);
716}
717
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000718SDOperand PPC32DAGToDAGISel::SelectADD_PARTS(SDOperand Op) {
719 SDNode *N = Op.Val;
720 SDOperand LHSL = Select(N->getOperand(0));
721 SDOperand LHSH = Select(N->getOperand(1));
722
723 unsigned Imm;
724 bool ME = false, ZE = false;
725 if (isIntImmediate(N->getOperand(3), Imm)) {
726 ME = (signed)Imm == -1;
727 ZE = Imm == 0;
728 }
729
730 std::vector<SDOperand> Result;
731 SDOperand CarryFromLo;
732 if (isIntImmediate(N->getOperand(2), Imm) &&
733 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
734 // Codegen the low 32 bits of the add. Interestingly, there is no
735 // shifted form of add immediate carrying.
736 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
737 LHSL, getI32Imm(Imm));
738 } else {
739 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
740 LHSL, Select(N->getOperand(2)));
741 }
742 CarryFromLo = CarryFromLo.getValue(1);
743
744 // Codegen the high 32 bits, adding zero, minus one, or the full value
745 // along with the carry flag produced by addc/addic.
746 SDOperand ResultHi;
747 if (ZE)
748 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
749 else if (ME)
750 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
751 else
752 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
753 Select(N->getOperand(3)), CarryFromLo);
754 Result.push_back(CarryFromLo.getValue(0));
755 Result.push_back(ResultHi);
756
757 CodeGenMap[Op.getValue(0)] = Result[0];
758 CodeGenMap[Op.getValue(1)] = Result[1];
759 return Result[Op.ResNo];
760}
761SDOperand PPC32DAGToDAGISel::SelectSUB_PARTS(SDOperand Op) {
762 SDNode *N = Op.Val;
763 SDOperand LHSL = Select(N->getOperand(0));
764 SDOperand LHSH = Select(N->getOperand(1));
765 SDOperand RHSL = Select(N->getOperand(2));
766 SDOperand RHSH = Select(N->getOperand(3));
767
768 std::vector<SDOperand> Result;
769 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
770 RHSL, LHSL));
771 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
772 Result[0].getValue(1)));
773 CodeGenMap[Op.getValue(0)] = Result[0];
774 CodeGenMap[Op.getValue(1)] = Result[1];
775 return Result[Op.ResNo];
776}
777
778
Chris Lattnera5a91b12005-08-17 19:33:03 +0000779// Select - Convert the specified operand from a target-independent to a
780// target-specific node if it hasn't already been changed.
781SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
782 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000783 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
784 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattnera5a91b12005-08-17 19:33:03 +0000785 return Op; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000786
787 // If this has already been converted, use it.
788 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
789 if (CGMI != CodeGenMap.end()) return CGMI->second;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000790
791 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000792 default: break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000793 case ISD::TokenFactor: {
794 SDOperand New;
795 if (N->getNumOperands() == 2) {
796 SDOperand Op0 = Select(N->getOperand(0));
797 SDOperand Op1 = Select(N->getOperand(1));
798 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
799 } else {
800 std::vector<SDOperand> Ops;
801 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +0000802 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +0000803 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
804 }
805
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000806 if (!N->hasOneUse()) CodeGenMap[Op] = New;
807 return New;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000808 }
809 case ISD::CopyFromReg: {
810 SDOperand Chain = Select(N->getOperand(0));
811 if (Chain == N->getOperand(0)) return Op; // No change
812 SDOperand New = CurDAG->getCopyFromReg(Chain,
813 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
814 return New.getValue(Op.ResNo);
815 }
816 case ISD::CopyToReg: {
817 SDOperand Chain = Select(N->getOperand(0));
818 SDOperand Reg = N->getOperand(1);
819 SDOperand Val = Select(N->getOperand(2));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000820 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
821 Chain, Reg, Val);
822 if (!N->hasOneUse()) CodeGenMap[Op] = New;
823 return New;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000824 }
Chris Lattner2b544002005-08-24 23:08:16 +0000825 case ISD::UNDEF:
826 if (N->getValueType(0) == MVT::i32)
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000827 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattner919c0322005-10-01 01:35:02 +0000828 else if (N->getValueType(0) == MVT::f32)
829 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F4, MVT::f32);
830 else
831 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F8, MVT::f64);
Chris Lattner25dae722005-09-03 00:53:47 +0000832 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000833 case ISD::FrameIndex: {
834 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000835 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere28e40a2005-08-25 00:45:43 +0000836 CurDAG->getTargetFrameIndex(FI, MVT::i32),
837 getI32Imm(0));
Chris Lattner25dae722005-09-03 00:53:47 +0000838 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000839 }
Chris Lattner34e17052005-08-25 05:04:11 +0000840 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +0000841 Constant *C = cast<ConstantPoolSDNode>(N)->get();
842 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner34e17052005-08-25 05:04:11 +0000843 if (PICEnabled)
844 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
845 else
846 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000847 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner25dae722005-09-03 00:53:47 +0000848 return SDOperand(N, 0);
Chris Lattner34e17052005-08-25 05:04:11 +0000849 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000850 case ISD::GlobalAddress: {
851 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
852 SDOperand Tmp;
853 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000854 if (PICEnabled)
855 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
856 else
Chris Lattner4416f1a2005-08-19 22:38:53 +0000857 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +0000858
Chris Lattner4416f1a2005-08-19 22:38:53 +0000859 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000860 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000861 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000862 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner25dae722005-09-03 00:53:47 +0000863 return SDOperand(N, 0);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000864 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000865 case ISD::DYNAMIC_STACKALLOC:
866 return SelectDYNAMIC_STACKALLOC(Op);
Chris Lattner867940d2005-10-02 06:58:23 +0000867 case PPCISD::FSEL: {
Chris Lattner43f07a42005-10-02 07:07:49 +0000868 SDOperand Comparison = Select(N->getOperand(0));
869 // Extend the comparison to 64-bits.
870 if (Comparison.getValueType() == MVT::f32)
871 Comparison = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Comparison);
872
873 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FSELS : PPC::FSELD;
874 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Comparison,
875 Select(N->getOperand(1)), Select(N->getOperand(2)));
Chris Lattner25dae722005-09-03 00:53:47 +0000876 return SDOperand(N, 0);
Chris Lattner867940d2005-10-02 06:58:23 +0000877 }
Nate Begemanc09eeec2005-09-06 22:03:27 +0000878 case PPCISD::FCFID:
879 CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0),
880 Select(N->getOperand(0)));
881 return SDOperand(N, 0);
882 case PPCISD::FCTIDZ:
883 CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0),
884 Select(N->getOperand(0)));
885 return SDOperand(N, 0);
Chris Lattnerf7605322005-08-31 21:09:52 +0000886 case PPCISD::FCTIWZ:
887 CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0),
888 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000889 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +0000890 case ISD::FADD: {
891 MVT::ValueType Ty = N->getValueType(0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000892 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattner615c2d02005-09-28 22:29:58 +0000893 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +0000894 N->getOperand(0).Val->hasOneUse()) {
895 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000896 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000897 Select(N->getOperand(0).getOperand(0)),
898 Select(N->getOperand(0).getOperand(1)),
899 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000900 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +0000901 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +0000902 N->getOperand(1).hasOneUse()) {
903 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000904 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000905 Select(N->getOperand(1).getOperand(0)),
906 Select(N->getOperand(1).getOperand(1)),
907 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000908 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000909 }
910 }
911
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000912 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000913 Select(N->getOperand(0)), Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000914 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000915 }
Chris Lattner615c2d02005-09-28 22:29:58 +0000916 case ISD::FSUB: {
917 MVT::ValueType Ty = N->getValueType(0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000918
919 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattner615c2d02005-09-28 22:29:58 +0000920 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +0000921 N->getOperand(0).Val->hasOneUse()) {
922 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000923 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000924 Select(N->getOperand(0).getOperand(0)),
925 Select(N->getOperand(0).getOperand(1)),
926 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000927 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +0000928 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +0000929 N->getOperand(1).Val->hasOneUse()) {
930 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000931 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000932 Select(N->getOperand(1).getOperand(0)),
933 Select(N->getOperand(1).getOperand(1)),
934 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +0000935 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000936 }
937 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000938 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +0000939 Select(N->getOperand(0)),
940 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +0000941 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +0000942 }
Chris Lattner88add102005-09-28 22:50:24 +0000943 case ISD::SDIV: {
Chris Lattner8784a232005-08-25 17:50:06 +0000944 unsigned Imm;
945 if (isIntImmediate(N->getOperand(1), Imm)) {
946 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
947 SDOperand Op =
948 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
949 Select(N->getOperand(0)),
950 getI32Imm(Log2_32(Imm)));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000951 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner8784a232005-08-25 17:50:06 +0000952 Op.getValue(0), Op.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +0000953 return SDOperand(N, 0);
Chris Lattner8784a232005-08-25 17:50:06 +0000954 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
955 SDOperand Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000956 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Chris Lattner8784a232005-08-25 17:50:06 +0000957 Select(N->getOperand(0)),
958 getI32Imm(Log2_32(-Imm)));
959 SDOperand PT =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000960 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0),
961 Op.getValue(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +0000962 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner25dae722005-09-03 00:53:47 +0000963 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +0000964 } else if (Imm) {
965 SDOperand Result = Select(BuildSDIVSequence(N));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000966 CodeGenMap[Op] = Result;
967 return Result;
Chris Lattner8784a232005-08-25 17:50:06 +0000968 }
969 }
Chris Lattner047b9522005-08-25 22:04:30 +0000970
Chris Lattner237733e2005-09-29 23:33:31 +0000971 // Other cases are autogenerated.
972 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000973 }
974 case ISD::UDIV: {
975 // If this is a divide by constant, we can emit code using some magic
976 // constants to implement it as a multiply instead.
977 unsigned Imm;
Chris Lattnera9317ed2005-08-25 23:21:06 +0000978 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner047b9522005-08-25 22:04:30 +0000979 SDOperand Result = Select(BuildUDIVSequence(N));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000980 CodeGenMap[Op] = Result;
981 return Result;
Chris Lattner047b9522005-08-25 22:04:30 +0000982 }
983
Chris Lattner237733e2005-09-29 23:33:31 +0000984 // Other cases are autogenerated.
985 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000986 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000987 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +0000988 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +0000989 // If this is an and of a value rotated between 0 and 31 bits and then and'd
990 // with a mask, emit rlwinm
991 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
992 isShiftedMask_32(~Imm))) {
993 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000994 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000995 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
996 Val = Select(N->getOperand(0).getOperand(0));
997 } else {
998 Val = Select(N->getOperand(0));
999 isRunOfOnes(Imm, MB, ME);
1000 SH = 0;
1001 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001002 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemancffc32b2005-08-18 07:30:46 +00001003 getI32Imm(MB), getI32Imm(ME));
Chris Lattner25dae722005-09-03 00:53:47 +00001004 return SDOperand(N, 0);
Nate Begemancffc32b2005-08-18 07:30:46 +00001005 }
Chris Lattner237733e2005-09-29 23:33:31 +00001006
1007 // Other cases are autogenerated.
1008 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001009 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001010 case ISD::OR:
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001011 if (SDNode *I = SelectBitfieldInsert(N))
1012 return CodeGenMap[Op] = SDOperand(I, 0);
1013
Nate Begeman02b88a42005-08-19 00:38:14 +00001014 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1015 N->getOperand(1),
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001016 PPC::ORIS, PPC::ORI))
1017 return CodeGenMap[Op] = SDOperand(I, 0);
1018
Chris Lattner237733e2005-09-29 23:33:31 +00001019 // Other cases are autogenerated.
1020 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001021 case ISD::SHL: {
1022 unsigned Imm, SH, MB, ME;
1023 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1024 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001025 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001026 Select(N->getOperand(0).getOperand(0)),
1027 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1028 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001029 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001030 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
1031 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001032 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001033 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001034 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001035 }
1036 case ISD::SRL: {
1037 unsigned Imm, SH, MB, ME;
1038 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1039 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001040 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001041 Select(N->getOperand(0).getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001042 getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME));
Nate Begemanc15ed442005-08-18 23:38:00 +00001043 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001044 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001045 getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm),
1046 getI32Imm(31));
Nate Begemanc15ed442005-08-18 23:38:00 +00001047 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001048 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001049 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001050 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001051 }
1052 case ISD::SRA: {
1053 unsigned Imm, SH, MB, ME;
1054 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1055 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001056 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001057 Select(N->getOperand(0).getOperand(0)),
1058 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1059 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001060 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001061 getI32Imm(Imm));
1062 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001063 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001064 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001065 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001066 }
Chris Lattnerd8ead9e2005-09-28 22:53:16 +00001067 case ISD::FMUL: {
1068 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FMULS : PPC::FMUL;
1069 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
1070 Select(N->getOperand(1)));
1071 return SDOperand(N, 0);
1072 }
1073 case ISD::FDIV: {
1074 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FDIVS : PPC::FDIV;
1075 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
1076 Select(N->getOperand(1)));
1077 return SDOperand(N, 0);
1078 }
Nate Begeman305a1c72005-08-18 03:04:18 +00001079 case ISD::FABS:
Chris Lattner919c0322005-10-01 01:35:02 +00001080 if (N->getValueType(0) == MVT::f32)
1081 CurDAG->SelectNodeTo(N, PPC::FABSS, MVT::f32, Select(N->getOperand(0)));
1082 else
1083 CurDAG->SelectNodeTo(N, PPC::FABSD, MVT::f64, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001084 return SDOperand(N, 0);
Chris Lattner8f838722005-08-30 00:30:43 +00001085 case ISD::FP_EXTEND:
Nate Begeman305a1c72005-08-18 03:04:18 +00001086 assert(MVT::f64 == N->getValueType(0) &&
1087 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner8f838722005-08-30 00:30:43 +00001088 // We need to emit an FMR to make sure that the result has the right value
1089 // type.
Chris Lattner919c0322005-10-01 01:35:02 +00001090 CurDAG->SelectNodeTo(N, PPC::FMRSD, MVT::f64, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001091 return SDOperand(N, 0);
Nate Begeman305a1c72005-08-18 03:04:18 +00001092 case ISD::FP_ROUND:
1093 assert(MVT::f32 == N->getValueType(0) &&
1094 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001095 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001096 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001097 case ISD::FNEG: {
1098 SDOperand Val = Select(N->getOperand(0));
1099 MVT::ValueType Ty = N->getValueType(0);
1100 if (Val.Val->hasOneUse()) {
1101 unsigned Opc;
Chris Lattner528f58e2005-08-28 23:39:22 +00001102 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman26653502005-08-17 23:46:35 +00001103 default: Opc = 0; break;
Chris Lattner919c0322005-10-01 01:35:02 +00001104 case PPC::FABSS: Opc = PPC::FNABSS; break;
1105 case PPC::FABSD: Opc = PPC::FNABSD; break;
Nate Begeman26653502005-08-17 23:46:35 +00001106 case PPC::FMADD: Opc = PPC::FNMADD; break;
1107 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1108 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1109 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1110 }
1111 // If we inverted the opcode, then emit the new instruction with the
1112 // inverted opcode and the original instruction's operands. Otherwise,
1113 // fall through and generate a fneg instruction.
1114 if (Opc) {
Chris Lattner919c0322005-10-01 01:35:02 +00001115 if (Opc == PPC::FNABSS || Opc == PPC::FNABSD)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001116 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman26653502005-08-17 23:46:35 +00001117 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001118 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman26653502005-08-17 23:46:35 +00001119 Val.getOperand(1), Val.getOperand(2));
Chris Lattner25dae722005-09-03 00:53:47 +00001120 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001121 }
1122 }
Chris Lattner919c0322005-10-01 01:35:02 +00001123 if (Ty == MVT::f32)
1124 CurDAG->SelectNodeTo(N, PPC::FNEGS, MVT::f32, Val);
1125 else
Chris Lattner2c1760f2005-10-01 02:51:36 +00001126 CurDAG->SelectNodeTo(N, PPC::FNEGD, MVT::f64, Val);
Chris Lattner25dae722005-09-03 00:53:47 +00001127 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001128 }
Nate Begeman6a7d6112005-08-18 00:53:47 +00001129 case ISD::FSQRT: {
1130 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001131 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman6a7d6112005-08-18 00:53:47 +00001132 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001133 return SDOperand(N, 0);
Nate Begeman6a7d6112005-08-18 00:53:47 +00001134 }
Chris Lattner2b63e4c2005-10-06 18:56:10 +00001135 case ISD::ADD_PARTS: return SelectADD_PARTS(Op);
1136 case ISD::SUB_PARTS: return SelectSUB_PARTS(Op);
Chris Lattnera9317ed2005-08-25 23:21:06 +00001137
Chris Lattner9944b762005-08-21 22:31:09 +00001138 case ISD::LOAD:
1139 case ISD::EXTLOAD:
1140 case ISD::ZEXTLOAD:
1141 case ISD::SEXTLOAD: {
1142 SDOperand Op1, Op2;
1143 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1144
1145 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1146 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1147 unsigned Opc;
1148 switch (TypeBeingLoaded) {
1149 default: N->dump(); assert(0 && "Cannot load this type!");
1150 case MVT::i1:
1151 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1152 case MVT::i16:
1153 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1154 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1155 } else {
1156 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1157 }
1158 break;
1159 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1160 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1161 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1162 }
1163
Chris Lattner919c0322005-10-01 01:35:02 +00001164 // If this is an f32 -> f64 load, emit the f32 load, then use an 'extending
1165 // copy'.
1166 if (TypeBeingLoaded != MVT::f32 || N->getOpcode() == ISD::LOAD) {
1167 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
1168 Op1, Op2, Select(N->getOperand(0)));
1169 return SDOperand(N, Op.ResNo);
1170 } else {
1171 std::vector<SDOperand> Ops;
1172 Ops.push_back(Op1);
1173 Ops.push_back(Op2);
1174 Ops.push_back(Select(N->getOperand(0)));
1175 SDOperand Res = CurDAG->getTargetNode(Opc, MVT::f32, MVT::Other, Ops);
1176 SDOperand Ext = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Res);
1177 CodeGenMap[Op.getValue(0)] = Ext;
1178 CodeGenMap[Op.getValue(1)] = Res.getValue(1);
1179 if (Op.ResNo)
1180 return Res.getValue(1);
1181 else
1182 return Ext;
1183 }
Chris Lattner9944b762005-08-21 22:31:09 +00001184 }
1185
Chris Lattnerf7f22552005-08-22 01:27:59 +00001186 case ISD::TRUNCSTORE:
1187 case ISD::STORE: {
1188 SDOperand AddrOp1, AddrOp2;
1189 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1190
1191 unsigned Opc;
1192 if (N->getOpcode() == ISD::STORE) {
1193 switch (N->getOperand(1).getValueType()) {
1194 default: assert(0 && "unknown Type in store");
1195 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1196 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1197 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1198 }
1199 } else { //ISD::TRUNCSTORE
1200 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1201 default: assert(0 && "unknown Type in store");
Chris Lattnerf7f22552005-08-22 01:27:59 +00001202 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1203 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1204 }
1205 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001206
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001207 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerf7f22552005-08-22 01:27:59 +00001208 AddrOp1, AddrOp2, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001209 return SDOperand(N, 0);
Chris Lattnerf7f22552005-08-22 01:27:59 +00001210 }
Chris Lattner64906a02005-08-25 20:08:18 +00001211
1212 case ISD::SETCC: {
1213 unsigned Imm;
1214 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1215 if (isIntImmediate(N->getOperand(1), Imm)) {
1216 // We can codegen setcc op, imm very efficiently compared to a brcond.
1217 // Check for those cases here.
1218 // setcc op, 0
1219 if (Imm == 0) {
1220 SDOperand Op = Select(N->getOperand(0));
1221 switch (CC) {
1222 default: assert(0 && "Unhandled SetCC condition"); abort();
1223 case ISD::SETEQ:
1224 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001225 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattner64906a02005-08-25 20:08:18 +00001226 getI32Imm(5), getI32Imm(31));
1227 break;
1228 case ISD::SETNE: {
1229 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1230 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001231 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001232 break;
1233 }
1234 case ISD::SETLT:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001235 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001236 getI32Imm(31), getI32Imm(31));
1237 break;
1238 case ISD::SETGT: {
1239 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
1240 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001241 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001242 getI32Imm(31), getI32Imm(31));
1243 break;
1244 }
1245 }
Chris Lattner25dae722005-09-03 00:53:47 +00001246 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001247 } else if (Imm == ~0U) { // setcc op, -1
1248 SDOperand Op = Select(N->getOperand(0));
1249 switch (CC) {
1250 default: assert(0 && "Unhandled SetCC condition"); abort();
1251 case ISD::SETEQ:
1252 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1253 Op, getI32Imm(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001254 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner64906a02005-08-25 20:08:18 +00001255 CurDAG->getTargetNode(PPC::LI, MVT::i32,
1256 getI32Imm(0)),
1257 Op.getValue(1));
1258 break;
1259 case ISD::SETNE: {
1260 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
Chris Lattner8bbcc202005-08-29 23:49:25 +00001261 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1262 Op, getI32Imm(~0U));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001263 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001264 break;
1265 }
1266 case ISD::SETLT: {
1267 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
1268 getI32Imm(1));
1269 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001270 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattner64906a02005-08-25 20:08:18 +00001271 getI32Imm(31), getI32Imm(31));
1272 break;
1273 }
1274 case ISD::SETGT:
1275 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
1276 getI32Imm(31), getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001277 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001278 break;
1279 }
Chris Lattner25dae722005-09-03 00:53:47 +00001280 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001281 }
1282 }
1283
1284 bool Inv;
1285 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Chris Lattner50ff55c2005-09-01 19:20:44 +00001286 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner64906a02005-08-25 20:08:18 +00001287 SDOperand IntCR;
Chris Lattner957fcfb2005-08-25 21:39:42 +00001288
1289 // Force the ccreg into CR7.
1290 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
1291
1292 std::vector<MVT::ValueType> VTs;
1293 VTs.push_back(MVT::Other);
1294 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
1295 std::vector<SDOperand> Ops;
1296 Ops.push_back(CurDAG->getEntryNode());
1297 Ops.push_back(CR7Reg);
1298 Ops.push_back(CCReg);
1299 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
1300
1301 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1302 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
1303 else
1304 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
Chris Lattner64906a02005-08-25 20:08:18 +00001305
1306 if (!Inv) {
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001307 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner64906a02005-08-25 20:08:18 +00001308 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
1309 } else {
1310 SDOperand Tmp =
1311 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
1312 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001313 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner64906a02005-08-25 20:08:18 +00001314 }
1315
Chris Lattner25dae722005-09-03 00:53:47 +00001316 return SDOperand(N, 0);
Chris Lattner64906a02005-08-25 20:08:18 +00001317 }
Chris Lattnera2590c52005-08-24 00:47:15 +00001318
Chris Lattner13794f52005-08-26 18:46:49 +00001319 case ISD::SELECT_CC: {
1320 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1321
1322 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1323 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1324 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1325 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1326 if (N1C->isNullValue() && N3C->isNullValue() &&
1327 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1328 SDOperand LHS = Select(N->getOperand(0));
1329 SDOperand Tmp =
1330 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1331 LHS, getI32Imm(~0U));
1332 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1333 Tmp.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +00001334 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001335 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001336
Chris Lattner50ff55c2005-09-01 19:20:44 +00001337 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001338 unsigned BROpc = getBCCForSetCC(CC);
1339
1340 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001341 unsigned SelectCCOp;
1342 if (MVT::isInteger(N->getValueType(0)))
1343 SelectCCOp = PPC::SELECT_CC_Int;
1344 else if (N->getValueType(0) == MVT::f32)
1345 SelectCCOp = PPC::SELECT_CC_F4;
1346 else
1347 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001348 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1349 Select(N->getOperand(2)), Select(N->getOperand(3)),
1350 getI32Imm(BROpc));
Chris Lattner25dae722005-09-03 00:53:47 +00001351 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001352 }
1353
Chris Lattnera2590c52005-08-24 00:47:15 +00001354 case ISD::CALLSEQ_START:
1355 case ISD::CALLSEQ_END: {
1356 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1357 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1358 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001359 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001360 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001361 return SDOperand(N, 0);
Chris Lattnera2590c52005-08-24 00:47:15 +00001362 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001363 case ISD::CALL:
1364 case ISD::TAILCALL: {
1365 SDOperand Chain = Select(N->getOperand(0));
1366
1367 unsigned CallOpcode;
1368 std::vector<SDOperand> CallOperands;
1369
1370 if (GlobalAddressSDNode *GASD =
1371 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
1372 CallOpcode = PPC::CALLpcrel;
1373 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
1374 MVT::i32));
1375 } else if (ExternalSymbolSDNode *ESSDN =
1376 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
1377 CallOpcode = PPC::CALLpcrel;
1378 CallOperands.push_back(N->getOperand(1));
1379 } else {
1380 // Copy the callee address into the CTR register.
1381 SDOperand Callee = Select(N->getOperand(1));
1382 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
1383
1384 // Copy the callee address into R12 on darwin.
1385 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
Chris Lattner2a06a5e2005-08-29 00:26:57 +00001386 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001387
1388 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
1389 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
1390 CallOperands.push_back(R12);
1391 CallOpcode = PPC::CALLindirect;
1392 }
1393
1394 unsigned GPR_idx = 0, FPR_idx = 0;
1395 static const unsigned GPR[] = {
1396 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1397 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1398 };
1399 static const unsigned FPR[] = {
1400 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1401 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1402 };
1403
Chris Lattner31ce12f2005-08-30 01:57:02 +00001404 SDOperand InFlag; // Null incoming flag value.
1405
Chris Lattner7107c102005-08-29 22:22:57 +00001406 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
1407 unsigned DestReg = 0;
Chris Lattnereb80fe82005-08-30 22:59:48 +00001408 MVT::ValueType RegTy = N->getOperand(i).getValueType();
1409 if (RegTy == MVT::i32) {
Chris Lattner7107c102005-08-29 22:22:57 +00001410 assert(GPR_idx < 8 && "Too many int args");
1411 DestReg = GPR[GPR_idx++];
Chris Lattner7107c102005-08-29 22:22:57 +00001412 } else {
1413 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
1414 "Unpromoted integer arg?");
1415 assert(FPR_idx < 13 && "Too many fp args");
1416 DestReg = FPR[FPR_idx++];
Chris Lattner7107c102005-08-29 22:22:57 +00001417 }
1418
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001419 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Chris Lattner2ea0c662005-08-30 21:28:19 +00001420 SDOperand Val = Select(N->getOperand(i));
Chris Lattner2ea0c662005-08-30 21:28:19 +00001421 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
Chris Lattner31ce12f2005-08-30 01:57:02 +00001422 InFlag = Chain.getValue(1);
1423 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001424 }
Chris Lattner7107c102005-08-29 22:22:57 +00001425 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001426
1427 // Finally, once everything is in registers to pass to the call, emit the
1428 // call itself.
Chris Lattner31ce12f2005-08-30 01:57:02 +00001429 if (InFlag.Val)
1430 CallOperands.push_back(InFlag); // Strong dep on register copies.
1431 else
1432 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
1433 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
1434 CallOperands);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001435
1436 std::vector<SDOperand> CallResults;
1437
1438 // If the call has results, copy the values out of the ret val registers.
1439 switch (N->getValueType(0)) {
1440 default: assert(0 && "Unexpected ret value!");
1441 case MVT::Other: break;
1442 case MVT::i32:
1443 if (N->getValueType(1) == MVT::i32) {
Chris Lattner31ce12f2005-08-30 01:57:02 +00001444 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
1445 Chain.getValue(1)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001446 CallResults.push_back(Chain.getValue(0));
Chris Lattner31ce12f2005-08-30 01:57:02 +00001447 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
Jim Laskey242f2552005-09-30 23:43:37 +00001448 Chain.getValue(2)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001449 CallResults.push_back(Chain.getValue(0));
1450 } else {
Chris Lattner31ce12f2005-08-30 01:57:02 +00001451 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1452 Chain.getValue(1)).getValue(1);
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001453 CallResults.push_back(Chain.getValue(0));
1454 }
1455 break;
1456 case MVT::f32:
1457 case MVT::f64:
Chris Lattnereb80fe82005-08-30 22:59:48 +00001458 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
Chris Lattner31ce12f2005-08-30 01:57:02 +00001459 Chain.getValue(1)).getValue(1);
Chris Lattnereb80fe82005-08-30 22:59:48 +00001460 CallResults.push_back(Chain.getValue(0));
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001461 break;
1462 }
1463
1464 CallResults.push_back(Chain);
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001465 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
1466 CodeGenMap[Op.getValue(i)] = CallResults[i];
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001467 return CallResults[Op.ResNo];
1468 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001469 case ISD::RET: {
1470 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1471
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001472 if (N->getNumOperands() == 2) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001473 SDOperand Val = Select(N->getOperand(1));
Chris Lattnereb80fe82005-08-30 22:59:48 +00001474 if (N->getOperand(1).getValueType() == MVT::i32) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001475 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
Chris Lattnereb80fe82005-08-30 22:59:48 +00001476 } else {
1477 assert(MVT::isFloatingPoint(N->getOperand(1).getValueType()));
1478 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001479 }
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001480 } else if (N->getNumOperands() > 1) {
1481 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1482 N->getOperand(2).getValueType() == MVT::i32 &&
1483 N->getNumOperands() == 3 && "Unknown two-register ret value!");
1484 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1)));
1485 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2)));
Chris Lattnera5a91b12005-08-17 19:33:03 +00001486 }
1487
1488 // Finally, select this to a blr (return) instruction.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001489 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattner25dae722005-09-03 00:53:47 +00001490 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001491 }
Chris Lattner89532c72005-08-25 00:29:58 +00001492 case ISD::BR:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001493 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner89532c72005-08-25 00:29:58 +00001494 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001495 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001496 case ISD::BR_CC:
1497 case ISD::BRTWOWAY_CC: {
1498 SDOperand Chain = Select(N->getOperand(0));
1499 MachineBasicBlock *Dest =
1500 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1501 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1502 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001503
1504 // If this is a two way branch, then grab the fallthrough basic block
1505 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1506 // conversion if necessary by the branch selection pass. Otherwise, emit a
1507 // standard conditional branch.
1508 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
Chris Lattnerca0a4772005-10-01 23:06:26 +00001509 SDOperand CondTrueBlock = N->getOperand(4);
1510 SDOperand CondFalseBlock = N->getOperand(5);
1511
1512 // If the false case is the current basic block, then this is a self loop.
1513 // We do not want to emit "Loop: ... brcond Out; br Loop", as it adds an
1514 // extra dispatch group to the loop. Instead, invert the condition and
1515 // emit "Loop: ... br!cond Loop; br Out
1516 if (cast<BasicBlockSDNode>(CondFalseBlock)->getBasicBlock() == BB) {
1517 std::swap(CondTrueBlock, CondFalseBlock);
1518 CC = getSetCCInverse(CC,
1519 MVT::isInteger(N->getOperand(2).getValueType()));
1520 }
1521
1522 unsigned Opc = getBCCForSetCC(CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001523 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1524 CondCode, getI32Imm(Opc),
Chris Lattnerca0a4772005-10-01 23:06:26 +00001525 CondTrueBlock, CondFalseBlock,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001526 Chain);
Chris Lattnerca0a4772005-10-01 23:06:26 +00001527 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, CondFalseBlock, CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001528 } else {
1529 // Iterate to the next basic block
1530 ilist<MachineBasicBlock>::iterator It = BB;
1531 ++It;
1532
1533 // If the fallthrough path is off the end of the function, which would be
1534 // undefined behavior, set it to be the same as the current block because
1535 // we have nothing better to set it to, and leaving it alone will cause
1536 // the PowerPC Branch Selection pass to crash.
1537 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001538 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattnerca0a4772005-10-01 23:06:26 +00001539 getI32Imm(getBCCForSetCC(CC)), N->getOperand(4),
Chris Lattner2fbb4572005-08-21 18:50:37 +00001540 CurDAG->getBasicBlock(It), Chain);
1541 }
Chris Lattner25dae722005-09-03 00:53:47 +00001542 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001543 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001544 }
Chris Lattner25dae722005-09-03 00:53:47 +00001545
Chris Lattner19c09072005-09-07 23:45:15 +00001546 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001547}
1548
1549
1550/// createPPC32ISelDag - This pass converts a legalized DAG into a
1551/// PowerPC-specific DAG, ready for instruction scheduling.
1552///
1553FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1554 return new PPC32DAGToDAGISel(TM);
1555}
1556