blob: 10c9b2bba94a1990e26748861901f24a9a7f30e0 [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:
Chris Lattner222adac2005-10-06 19:03:35 +000096 SDOperand SelectDYNAMIC_STACKALLOC(SDOperand Op);
97 SDOperand SelectADD_PARTS(SDOperand Op);
98 SDOperand SelectSUB_PARTS(SDOperand Op);
99 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000100 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000101 };
102}
103
Chris Lattnerbd937b92005-10-06 18:45:51 +0000104/// InstructionSelectBasicBlock - This callback is invoked by
105/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
106void PPC32DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
107 DEBUG(BB->dump());
108
109 // The selection process is inherently a bottom-up recursive process (users
110 // select their uses before themselves). Given infinite stack space, we
111 // could just start selecting on the root and traverse the whole graph. In
112 // practice however, this causes us to run out of stack space on large basic
113 // blocks. To avoid this problem, select the entry node, then all its uses,
114 // iteratively instead of recursively.
115 std::vector<SDOperand> Worklist;
116 Worklist.push_back(DAG.getEntryNode());
117
118 // Note that we can do this in the PPC target (scanning forward across token
119 // chain edges) because no nodes ever get folded across these edges. On a
120 // target like X86 which supports load/modify/store operations, this would
121 // have to be more careful.
122 while (!Worklist.empty()) {
123 SDOperand Node = Worklist.back();
124 Worklist.pop_back();
125
126 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
127 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
128 CodeGenMap.count(Node)) continue;
129
130 for (SDNode::use_iterator UI = Node.Val->use_begin(),
131 E = Node.Val->use_end(); UI != E; ++UI) {
132 // Scan the values. If this use has a value that is a token chain, add it
133 // to the worklist.
134 SDNode *User = *UI;
135 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
136 if (User->getValueType(i) == MVT::Other) {
137 Worklist.push_back(SDOperand(User, i));
138 break;
139 }
140 }
141
142 // Finally, legalize this node.
143 Select(Node);
144 }
145
146 // Select target instructions for the DAG.
147 DAG.setRoot(Select(DAG.getRoot()));
148 CodeGenMap.clear();
149 DAG.RemoveDeadNodes();
150
151 // Emit machine code to BB.
152 ScheduleAndEmitDAG(DAG);
153}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000154
Chris Lattner4416f1a2005-08-19 22:38:53 +0000155/// getGlobalBaseReg - Output the instructions required to put the
156/// base address to use for accessing globals into a register.
157///
Chris Lattner9944b762005-08-21 22:31:09 +0000158SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000159 if (!GlobalBaseReg) {
160 // Insert the set of GlobalBaseReg into the first MBB of the function
161 MachineBasicBlock &FirstMBB = BB->getParent()->front();
162 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
163 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
164 GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
165 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
166 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
167 }
Chris Lattner9944b762005-08-21 22:31:09 +0000168 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000169}
170
171
Nate Begeman0f3257a2005-08-18 05:00:13 +0000172// isIntImmediate - This method tests to see if a constant operand.
173// If so Imm will receive the 32 bit value.
174static bool isIntImmediate(SDNode *N, unsigned& Imm) {
175 if (N->getOpcode() == ISD::Constant) {
176 Imm = cast<ConstantSDNode>(N)->getValue();
177 return true;
178 }
179 return false;
180}
181
Nate Begemancffc32b2005-08-18 07:30:46 +0000182// isOprShiftImm - Returns true if the specified operand is a shift opcode with
183// a immediate shift count less than 32.
184static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) {
185 Opc = N->getOpcode();
186 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
187 isIntImmediate(N->getOperand(1).Val, SH) && SH < 32;
188}
189
190// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
191// any number of 0s on either side. The 1s are allowed to wrap from LSB to
192// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
193// not, since all 1s are not contiguous.
194static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
195 if (isShiftedMask_32(Val)) {
196 // look for the first non-zero bit
197 MB = CountLeadingZeros_32(Val);
198 // look for the first zero bit after the run of ones
199 ME = CountLeadingZeros_32((Val - 1) ^ Val);
200 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000201 } else {
202 Val = ~Val; // invert mask
203 if (isShiftedMask_32(Val)) {
204 // effectively look for the first zero bit
205 ME = CountLeadingZeros_32(Val) - 1;
206 // effectively look for the first one bit after the run of zeros
207 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
208 return true;
209 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000210 }
211 // no run present
212 return false;
213}
214
215// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
216// and mask opcode and mask operation.
217static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
218 unsigned &SH, unsigned &MB, unsigned &ME) {
219 unsigned Shift = 32;
220 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
221 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000222 if (N->getNumOperands() != 2 ||
223 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000224 return false;
225
226 if (Opcode == ISD::SHL) {
227 // apply shift left to mask if it comes first
228 if (IsShiftMask) Mask = Mask << Shift;
229 // determine which bits are made indeterminant by shift
230 Indeterminant = ~(0xFFFFFFFFu << Shift);
231 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) {
232 // apply shift right to mask if it comes first
233 if (IsShiftMask) Mask = Mask >> Shift;
234 // determine which bits are made indeterminant by shift
235 Indeterminant = ~(0xFFFFFFFFu >> Shift);
236 // adjust for the left rotate
237 Shift = 32 - Shift;
238 } else {
239 return false;
240 }
241
242 // if the mask doesn't intersect any Indeterminant bits
243 if (Mask && !(Mask & Indeterminant)) {
244 SH = Shift;
245 // make sure the mask is still a mask (wrap arounds may not be)
246 return isRunOfOnes(Mask, MB, ME);
247 }
248 return false;
249}
250
Nate Begeman0f3257a2005-08-18 05:00:13 +0000251// isOpcWithIntImmediate - This method tests to see if the node is a specific
252// opcode and that it has a immediate integer right operand.
253// If so Imm will receive the 32 bit value.
254static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
255 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
256}
257
258// isOprNot - Returns true if the specified operand is an xor with immediate -1.
259static bool isOprNot(SDNode *N) {
260 unsigned Imm;
261 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
262}
263
Chris Lattnera5a91b12005-08-17 19:33:03 +0000264// Immediate constant composers.
265// Lo16 - grabs the lo 16 bits from a 32 bit constant.
266// Hi16 - grabs the hi 16 bits from a 32 bit constant.
267// HA16 - computes the hi bits required if the lo bits are add/subtracted in
268// arithmethically.
269static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
270static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
271static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
272
273// isIntImmediate - This method tests to see if a constant operand.
274// If so Imm will receive the 32 bit value.
275static bool isIntImmediate(SDOperand N, unsigned& Imm) {
276 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
277 Imm = (unsigned)CN->getSignExtended();
278 return true;
279 }
280 return false;
281}
282
Nate Begeman02b88a42005-08-19 00:38:14 +0000283/// SelectBitfieldInsert - turn an or of two masked values into
284/// the rotate left word immediate then mask insert (rlwimi) instruction.
285/// Returns true on success, false if the caller still needs to select OR.
286///
287/// Patterns matched:
288/// 1. or shl, and 5. or and, and
289/// 2. or and, shl 6. or shl, shr
290/// 3. or shr, and 7. or shr, shl
291/// 4. or and, shr
292SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
293 bool IsRotate = false;
294 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
295 unsigned Value;
296
297 SDOperand Op0 = N->getOperand(0);
298 SDOperand Op1 = N->getOperand(1);
299
300 unsigned Op0Opc = Op0.getOpcode();
301 unsigned Op1Opc = Op1.getOpcode();
302
303 // Verify that we have the correct opcodes
304 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
305 return false;
306 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
307 return false;
308
309 // Generate Mask value for Target
310 if (isIntImmediate(Op0.getOperand(1), Value)) {
311 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000312 case ISD::SHL: TgtMask <<= Value; break;
313 case ISD::SRL: TgtMask >>= Value; break;
314 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000315 }
316 } else {
317 return 0;
318 }
319
320 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000321 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000322 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000323
324 switch(Op1Opc) {
325 case ISD::SHL:
326 SH = Value;
327 InsMask <<= SH;
328 if (Op0Opc == ISD::SRL) IsRotate = true;
329 break;
330 case ISD::SRL:
331 SH = Value;
332 InsMask >>= SH;
333 SH = 32-SH;
334 if (Op0Opc == ISD::SHL) IsRotate = true;
335 break;
336 case ISD::AND:
337 InsMask &= Value;
338 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000339 }
340
341 // If both of the inputs are ANDs and one of them has a logical shift by
342 // constant as its input, make that AND the inserted value so that we can
343 // combine the shift into the rotate part of the rlwimi instruction
344 bool IsAndWithShiftOp = false;
345 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
346 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
347 Op1.getOperand(0).getOpcode() == ISD::SRL) {
348 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
349 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
350 IsAndWithShiftOp = true;
351 }
352 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
353 Op0.getOperand(0).getOpcode() == ISD::SRL) {
354 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
355 std::swap(Op0, Op1);
356 std::swap(TgtMask, InsMask);
357 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
358 IsAndWithShiftOp = true;
359 }
360 }
361 }
362
363 // Verify that the Target mask and Insert mask together form a full word mask
364 // and that the Insert mask is a run of set bits (which implies both are runs
365 // of set bits). Given that, Select the arguments and generate the rlwimi
366 // instruction.
367 unsigned MB, ME;
368 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
369 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
370 bool Op0IsAND = Op0Opc == ISD::AND;
371 // Check for rotlwi / rotrwi here, a special case of bitfield insert
372 // where both bitfield halves are sourced from the same value.
373 if (IsRotate && fullMask &&
374 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
375 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
376 Select(N->getOperand(0).getOperand(0)),
377 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
378 return Op0.Val;
379 }
380 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
381 : Select(Op0);
382 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
383 : Select(Op1.getOperand(0));
384 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
385 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
386 return Op0.Val;
387 }
388 return 0;
389}
390
Chris Lattnera5a91b12005-08-17 19:33:03 +0000391// SelectIntImmediateExpr - Choose code for integer operations with an immediate
392// operand.
393SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
394 unsigned OCHi, unsigned OCLo,
395 bool IsArithmetic,
396 bool Negate) {
397 // Check to make sure this is a constant.
398 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
399 // Exit if not a constant.
400 if (!CN) return 0;
401 // Extract immediate.
402 unsigned C = (unsigned)CN->getValue();
403 // Negate if required (ISD::SUB).
404 if (Negate) C = -C;
405 // Get the hi and lo portions of constant.
406 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
407 unsigned Lo = Lo16(C);
408
409 // If two instructions are needed and usage indicates it would be better to
410 // load immediate into a register, bail out.
411 if (Hi && Lo && CN->use_size() > 2) return false;
412
413 // Select the first operand.
414 SDOperand Opr0 = Select(LHS);
415
416 if (Lo) // Add in the lo-part.
417 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
418 if (Hi) // Add in the hi-part.
419 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
420 return Opr0.Val;
421}
422
Chris Lattner9944b762005-08-21 22:31:09 +0000423/// SelectAddr - Given the specified address, return the two operands for a
424/// load/store instruction, and return true if it should be an indexed [r+r]
425/// operation.
426bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1,
427 SDOperand &Op2) {
428 unsigned imm = 0;
429 if (Addr.getOpcode() == ISD::ADD) {
430 if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) {
431 Op1 = getI32Imm(Lo16(imm));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000432 if (FrameIndexSDNode *FI =
433 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner9944b762005-08-21 22:31:09 +0000434 ++FrameOff;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000435 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000436 } else {
437 Op2 = Select(Addr.getOperand(0));
438 }
439 return false;
440 } else {
441 Op1 = Select(Addr.getOperand(0));
442 Op2 = Select(Addr.getOperand(1));
443 return true; // [r+r]
444 }
445 }
446
447 // Now check if we're dealing with a global, and whether or not we should emit
448 // an optimized load or store for statics.
449 if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) {
450 GlobalValue *GV = GN->getGlobal();
451 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
452 Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
453 if (PICEnabled)
454 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),
455 Op1);
456 else
457 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
458 return false;
459 }
Chris Lattnere28e40a2005-08-25 00:45:43 +0000460 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) {
Chris Lattner9944b762005-08-21 22:31:09 +0000461 Op1 = getI32Imm(0);
Chris Lattnere28e40a2005-08-25 00:45:43 +0000462 Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000463 return false;
464 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) {
465 Op1 = Addr;
466 if (PICEnabled)
467 Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1);
468 else
469 Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1);
470 return false;
471 }
472 Op1 = getI32Imm(0);
473 Op2 = Select(Addr);
474 return false;
475}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000476
Chris Lattner2fbb4572005-08-21 18:50:37 +0000477/// SelectCC - Select a comparison of the specified values with the specified
478/// condition code, returning the CR# of the expression.
479SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
480 ISD::CondCode CC) {
481 // Always select the LHS.
482 LHS = Select(LHS);
483
484 // Use U to determine whether the SETCC immediate range is signed or not.
485 if (MVT::isInteger(LHS.getValueType())) {
486 bool U = ISD::isUnsignedIntSetCC(CC);
487 unsigned Imm;
488 if (isIntImmediate(RHS, Imm) &&
489 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
490 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
491 LHS, getI32Imm(Lo16(Imm)));
492 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
493 LHS, Select(RHS));
Chris Lattner919c0322005-10-01 01:35:02 +0000494 } else if (LHS.getValueType() == MVT::f32) {
495 return CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000496 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000497 return CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000498 }
499}
500
501/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
502/// to Condition.
503static unsigned getBCCForSetCC(ISD::CondCode CC) {
504 switch (CC) {
505 default: assert(0 && "Unknown condition!"); abort();
506 case ISD::SETEQ: return PPC::BEQ;
507 case ISD::SETNE: return PPC::BNE;
508 case ISD::SETULT:
509 case ISD::SETLT: return PPC::BLT;
510 case ISD::SETULE:
511 case ISD::SETLE: return PPC::BLE;
512 case ISD::SETUGT:
513 case ISD::SETGT: return PPC::BGT;
514 case ISD::SETUGE:
515 case ISD::SETGE: return PPC::BGE;
516 }
517 return 0;
518}
519
Chris Lattner64906a02005-08-25 20:08:18 +0000520/// getCRIdxForSetCC - Return the index of the condition register field
521/// associated with the SetCC condition, and whether or not the field is
522/// treated as inverted. That is, lt = 0; ge = 0 inverted.
523static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
524 switch (CC) {
525 default: assert(0 && "Unknown condition!"); abort();
526 case ISD::SETULT:
527 case ISD::SETLT: Inv = false; return 0;
528 case ISD::SETUGE:
529 case ISD::SETGE: Inv = true; return 0;
530 case ISD::SETUGT:
531 case ISD::SETGT: Inv = false; return 1;
532 case ISD::SETULE:
533 case ISD::SETLE: Inv = true; return 1;
534 case ISD::SETEQ: Inv = false; return 2;
535 case ISD::SETNE: Inv = true; return 2;
536 }
537 return 0;
538}
Chris Lattner9944b762005-08-21 22:31:09 +0000539
Chris Lattner047b9522005-08-25 22:04:30 +0000540// Structure used to return the necessary information to codegen an SDIV as
541// a multiply.
542struct ms {
543 int m; // magic number
544 int s; // shift amount
545};
546
547struct mu {
548 unsigned int m; // magic number
549 int a; // add indicator
550 int s; // shift amount
551};
552
553/// magic - calculate the magic numbers required to codegen an integer sdiv as
554/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
555/// or -1.
556static struct ms magic(int d) {
557 int p;
558 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
559 const unsigned int two31 = 0x80000000U;
560 struct ms mag;
561
562 ad = abs(d);
563 t = two31 + ((unsigned int)d >> 31);
564 anc = t - 1 - t%ad; // absolute value of nc
565 p = 31; // initialize p
566 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
567 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
568 q2 = two31/ad; // initialize q2 = 2p/abs(d)
569 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
570 do {
571 p = p + 1;
572 q1 = 2*q1; // update q1 = 2p/abs(nc)
573 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
574 if (r1 >= anc) { // must be unsigned comparison
575 q1 = q1 + 1;
576 r1 = r1 - anc;
577 }
578 q2 = 2*q2; // update q2 = 2p/abs(d)
579 r2 = 2*r2; // update r2 = rem(2p/abs(d))
580 if (r2 >= ad) { // must be unsigned comparison
581 q2 = q2 + 1;
582 r2 = r2 - ad;
583 }
584 delta = ad - r2;
585 } while (q1 < delta || (q1 == delta && r1 == 0));
586
587 mag.m = q2 + 1;
588 if (d < 0) mag.m = -mag.m; // resulting magic number
589 mag.s = p - 32; // resulting shift
590 return mag;
591}
592
593/// magicu - calculate the magic numbers required to codegen an integer udiv as
594/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
595static struct mu magicu(unsigned d)
596{
597 int p;
598 unsigned int nc, delta, q1, r1, q2, r2;
599 struct mu magu;
600 magu.a = 0; // initialize "add" indicator
601 nc = - 1 - (-d)%d;
602 p = 31; // initialize p
603 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
604 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
605 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
606 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
607 do {
608 p = p + 1;
609 if (r1 >= nc - r1 ) {
610 q1 = 2*q1 + 1; // update q1
611 r1 = 2*r1 - nc; // update r1
612 }
613 else {
614 q1 = 2*q1; // update q1
615 r1 = 2*r1; // update r1
616 }
617 if (r2 + 1 >= d - r2) {
618 if (q2 >= 0x7FFFFFFF) magu.a = 1;
619 q2 = 2*q2 + 1; // update q2
620 r2 = 2*r2 + 1 - d; // update r2
621 }
622 else {
623 if (q2 >= 0x80000000) magu.a = 1;
624 q2 = 2*q2; // update q2
625 r2 = 2*r2 + 1; // update r2
626 }
627 delta = d - 1 - r2;
628 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
629 magu.m = q2 + 1; // resulting magic number
630 magu.s = p - 32; // resulting shift
631 return magu;
632}
633
634/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
635/// return a DAG expression to select that will generate the same value by
636/// multiplying by a magic number. See:
637/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
638SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) {
639 int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue();
640 ms magics = magic(d);
641 // Multiply the numerator (operand 0) by the magic value
642 SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0),
643 CurDAG->getConstant(magics.m, MVT::i32));
644 // If d > 0 and m < 0, add the numerator
645 if (d > 0 && magics.m < 0)
646 Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0));
647 // If d < 0 and m > 0, subtract the numerator.
648 if (d < 0 && magics.m > 0)
649 Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0));
650 // Shift right algebraic if shift value is nonzero
651 if (magics.s > 0)
652 Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q,
653 CurDAG->getConstant(magics.s, MVT::i32));
654 // Extract the sign bit and add it to the quotient
655 SDOperand T =
656 CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32));
657 return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T);
658}
659
660/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
661/// return a DAG expression to select that will generate the same value by
662/// multiplying by a magic number. See:
663/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
664SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) {
665 unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue();
666 mu magics = magicu(d);
667 // Multiply the numerator (operand 0) by the magic value
668 SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0),
669 CurDAG->getConstant(magics.m, MVT::i32));
670 if (magics.a == 0) {
671 return CurDAG->getNode(ISD::SRL, MVT::i32, Q,
672 CurDAG->getConstant(magics.s, MVT::i32));
673 } else {
674 SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q);
675 NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
676 CurDAG->getConstant(1, MVT::i32));
677 NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
678 return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ,
679 CurDAG->getConstant(magics.s-1, MVT::i32));
680 }
681}
682
Chris Lattnerbd937b92005-10-06 18:45:51 +0000683SDOperand PPC32DAGToDAGISel::SelectDYNAMIC_STACKALLOC(SDOperand Op) {
684 SDNode *N = Op.Val;
685
686 // FIXME: We are currently ignoring the requested alignment for handling
687 // greater than the stack alignment. This will need to be revisited at some
688 // point. Align = N.getOperand(2);
689 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
690 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
691 std::cerr << "Cannot allocate stack object with greater alignment than"
692 << " the stack alignment yet!";
693 abort();
694 }
695 SDOperand Chain = Select(N->getOperand(0));
696 SDOperand Amt = Select(N->getOperand(1));
697
698 SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32);
699
700 SDOperand R1Val = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
701 Chain = R1Val.getValue(1);
702
703 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
704 // from the stack pointer, giving us the result pointer.
705 SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Val);
706
707 // Copy this result back into R1.
708 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result);
709
710 // Copy this result back out of R1 to make sure we're not using the stack
711 // space without decrementing the stack pointer.
712 Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32);
713
714 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
715 CodeGenMap[Op.getValue(0)] = Result;
716 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
717 return SDOperand(Result.Val, Op.ResNo);
718}
719
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000720SDOperand PPC32DAGToDAGISel::SelectADD_PARTS(SDOperand Op) {
721 SDNode *N = Op.Val;
722 SDOperand LHSL = Select(N->getOperand(0));
723 SDOperand LHSH = Select(N->getOperand(1));
724
725 unsigned Imm;
726 bool ME = false, ZE = false;
727 if (isIntImmediate(N->getOperand(3), Imm)) {
728 ME = (signed)Imm == -1;
729 ZE = Imm == 0;
730 }
731
732 std::vector<SDOperand> Result;
733 SDOperand CarryFromLo;
734 if (isIntImmediate(N->getOperand(2), Imm) &&
735 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
736 // Codegen the low 32 bits of the add. Interestingly, there is no
737 // shifted form of add immediate carrying.
738 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
739 LHSL, getI32Imm(Imm));
740 } else {
741 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
742 LHSL, Select(N->getOperand(2)));
743 }
744 CarryFromLo = CarryFromLo.getValue(1);
745
746 // Codegen the high 32 bits, adding zero, minus one, or the full value
747 // along with the carry flag produced by addc/addic.
748 SDOperand ResultHi;
749 if (ZE)
750 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
751 else if (ME)
752 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
753 else
754 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
755 Select(N->getOperand(3)), CarryFromLo);
756 Result.push_back(CarryFromLo.getValue(0));
757 Result.push_back(ResultHi);
758
759 CodeGenMap[Op.getValue(0)] = Result[0];
760 CodeGenMap[Op.getValue(1)] = Result[1];
761 return Result[Op.ResNo];
762}
763SDOperand PPC32DAGToDAGISel::SelectSUB_PARTS(SDOperand Op) {
764 SDNode *N = Op.Val;
765 SDOperand LHSL = Select(N->getOperand(0));
766 SDOperand LHSH = Select(N->getOperand(1));
767 SDOperand RHSL = Select(N->getOperand(2));
768 SDOperand RHSH = Select(N->getOperand(3));
769
770 std::vector<SDOperand> Result;
771 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
772 RHSL, LHSL));
773 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
774 Result[0].getValue(1)));
775 CodeGenMap[Op.getValue(0)] = Result[0];
776 CodeGenMap[Op.getValue(1)] = Result[1];
777 return Result[Op.ResNo];
778}
779
Chris Lattner222adac2005-10-06 19:03:35 +0000780SDOperand PPC32DAGToDAGISel::SelectSETCC(SDOperand Op) {
781 SDNode *N = Op.Val;
782 unsigned Imm;
783 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
784 if (isIntImmediate(N->getOperand(1), Imm)) {
785 // We can codegen setcc op, imm very efficiently compared to a brcond.
786 // Check for those cases here.
787 // setcc op, 0
788 if (Imm == 0) {
789 SDOperand Op = Select(N->getOperand(0));
790 switch (CC) {
791 default: assert(0 && "Unhandled SetCC condition"); abort();
792 case ISD::SETEQ:
793 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
794 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
795 getI32Imm(5), getI32Imm(31));
796 break;
797 case ISD::SETNE: {
798 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
799 Op, getI32Imm(~0U));
800 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
801 break;
802 }
803 case ISD::SETLT:
804 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
805 getI32Imm(31), getI32Imm(31));
806 break;
807 case ISD::SETGT: {
808 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
809 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
810 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
811 getI32Imm(31), getI32Imm(31));
812 break;
813 }
814 }
815 return SDOperand(N, 0);
816 } else if (Imm == ~0U) { // setcc op, -1
817 SDOperand Op = Select(N->getOperand(0));
818 switch (CC) {
819 default: assert(0 && "Unhandled SetCC condition"); abort();
820 case ISD::SETEQ:
821 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
822 Op, getI32Imm(1));
823 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
824 CurDAG->getTargetNode(PPC::LI, MVT::i32,
825 getI32Imm(0)),
826 Op.getValue(1));
827 break;
828 case ISD::SETNE: {
829 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
830 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
831 Op, getI32Imm(~0U));
832 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
833 break;
834 }
835 case ISD::SETLT: {
836 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
837 getI32Imm(1));
838 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
839 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
840 getI32Imm(31), getI32Imm(31));
841 break;
842 }
843 case ISD::SETGT:
844 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
845 getI32Imm(31), getI32Imm(31));
846 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
847 break;
848 }
849 return SDOperand(N, 0);
850 }
851 }
852
853 bool Inv;
854 unsigned Idx = getCRIdxForSetCC(CC, Inv);
855 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
856 SDOperand IntCR;
857
858 // Force the ccreg into CR7.
859 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
860
861 std::vector<MVT::ValueType> VTs;
862 VTs.push_back(MVT::Other);
863 VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag
864 std::vector<SDOperand> Ops;
865 Ops.push_back(CurDAG->getEntryNode());
866 Ops.push_back(CR7Reg);
867 Ops.push_back(CCReg);
868 CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1);
869
870 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
871 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
872 else
873 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
874
875 if (!Inv) {
876 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
877 getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31));
878 } else {
879 SDOperand Tmp =
880 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
881 getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31));
882 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
883 }
884
885 return SDOperand(N, 0);
886}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000887
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000888SDOperand PPC32DAGToDAGISel::SelectCALL(SDOperand Op) {
889 SDNode *N = Op.Val;
890 SDOperand Chain = Select(N->getOperand(0));
891
892 unsigned CallOpcode;
893 std::vector<SDOperand> CallOperands;
894
895 if (GlobalAddressSDNode *GASD =
896 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
897 CallOpcode = PPC::CALLpcrel;
898 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
899 MVT::i32));
900 } else if (ExternalSymbolSDNode *ESSDN =
901 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
902 CallOpcode = PPC::CALLpcrel;
903 CallOperands.push_back(N->getOperand(1));
904 } else {
905 // Copy the callee address into the CTR register.
906 SDOperand Callee = Select(N->getOperand(1));
907 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
908
909 // Copy the callee address into R12 on darwin.
910 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
911 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
912
913 CallOperands.push_back(getI32Imm(20)); // Information to encode indcall
914 CallOperands.push_back(getI32Imm(0)); // Information to encode indcall
915 CallOperands.push_back(R12);
916 CallOpcode = PPC::CALLindirect;
917 }
918
919 unsigned GPR_idx = 0, FPR_idx = 0;
920 static const unsigned GPR[] = {
921 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
922 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
923 };
924 static const unsigned FPR[] = {
925 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
926 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
927 };
928
929 SDOperand InFlag; // Null incoming flag value.
930
931 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
932 unsigned DestReg = 0;
933 MVT::ValueType RegTy = N->getOperand(i).getValueType();
934 if (RegTy == MVT::i32) {
935 assert(GPR_idx < 8 && "Too many int args");
936 DestReg = GPR[GPR_idx++];
937 } else {
938 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
939 "Unpromoted integer arg?");
940 assert(FPR_idx < 13 && "Too many fp args");
941 DestReg = FPR[FPR_idx++];
942 }
943
944 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
945 SDOperand Val = Select(N->getOperand(i));
946 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
947 InFlag = Chain.getValue(1);
948 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
949 }
950 }
951
952 // Finally, once everything is in registers to pass to the call, emit the
953 // call itself.
954 if (InFlag.Val)
955 CallOperands.push_back(InFlag); // Strong dep on register copies.
956 else
957 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
958 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
959 CallOperands);
960
961 std::vector<SDOperand> CallResults;
962
963 // If the call has results, copy the values out of the ret val registers.
964 switch (N->getValueType(0)) {
965 default: assert(0 && "Unexpected ret value!");
966 case MVT::Other: break;
967 case MVT::i32:
968 if (N->getValueType(1) == MVT::i32) {
969 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
970 Chain.getValue(1)).getValue(1);
971 CallResults.push_back(Chain.getValue(0));
972 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
973 Chain.getValue(2)).getValue(1);
974 CallResults.push_back(Chain.getValue(0));
975 } else {
976 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
977 Chain.getValue(1)).getValue(1);
978 CallResults.push_back(Chain.getValue(0));
979 }
980 break;
981 case MVT::f32:
982 case MVT::f64:
983 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
984 Chain.getValue(1)).getValue(1);
985 CallResults.push_back(Chain.getValue(0));
986 break;
987 }
988
989 CallResults.push_back(Chain);
990 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
991 CodeGenMap[Op.getValue(i)] = CallResults[i];
992 return CallResults[Op.ResNo];
993}
994
Chris Lattnera5a91b12005-08-17 19:33:03 +0000995// Select - Convert the specified operand from a target-independent to a
996// target-specific node if it hasn't already been changed.
997SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
998 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000999 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
1000 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattnera5a91b12005-08-17 19:33:03 +00001001 return Op; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001002
1003 // If this has already been converted, use it.
1004 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
1005 if (CGMI != CodeGenMap.end()) return CGMI->second;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001006
1007 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +00001008 default: break;
Chris Lattner222adac2005-10-06 19:03:35 +00001009 case ISD::DYNAMIC_STACKALLOC: return SelectDYNAMIC_STACKALLOC(Op);
1010 case ISD::ADD_PARTS: return SelectADD_PARTS(Op);
1011 case ISD::SUB_PARTS: return SelectSUB_PARTS(Op);
1012 case ISD::SETCC: return SelectSETCC(Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +00001013 case ISD::CALL: return SelectCALL(Op);
1014 case ISD::TAILCALL: return SelectCALL(Op);
1015
Chris Lattnera5a91b12005-08-17 19:33:03 +00001016 case ISD::TokenFactor: {
1017 SDOperand New;
1018 if (N->getNumOperands() == 2) {
1019 SDOperand Op0 = Select(N->getOperand(0));
1020 SDOperand Op1 = Select(N->getOperand(1));
1021 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
1022 } else {
1023 std::vector<SDOperand> Ops;
1024 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Chris Lattner7e659972005-08-19 21:33:02 +00001025 Ops.push_back(Select(N->getOperand(i)));
Chris Lattnera5a91b12005-08-17 19:33:03 +00001026 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
1027 }
1028
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001029 if (!N->hasOneUse()) CodeGenMap[Op] = New;
1030 return New;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001031 }
1032 case ISD::CopyFromReg: {
1033 SDOperand Chain = Select(N->getOperand(0));
1034 if (Chain == N->getOperand(0)) return Op; // No change
1035 SDOperand New = CurDAG->getCopyFromReg(Chain,
1036 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
1037 return New.getValue(Op.ResNo);
1038 }
1039 case ISD::CopyToReg: {
1040 SDOperand Chain = Select(N->getOperand(0));
1041 SDOperand Reg = N->getOperand(1);
1042 SDOperand Val = Select(N->getOperand(2));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001043 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
1044 Chain, Reg, Val);
1045 if (!N->hasOneUse()) CodeGenMap[Op] = New;
1046 return New;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001047 }
Chris Lattner2b544002005-08-24 23:08:16 +00001048 case ISD::UNDEF:
1049 if (N->getValueType(0) == MVT::i32)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001050 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32);
Chris Lattner919c0322005-10-01 01:35:02 +00001051 else if (N->getValueType(0) == MVT::f32)
1052 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F4, MVT::f32);
1053 else
1054 CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F8, MVT::f64);
Chris Lattner25dae722005-09-03 00:53:47 +00001055 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +00001056 case ISD::FrameIndex: {
1057 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001058 CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
Chris Lattnere28e40a2005-08-25 00:45:43 +00001059 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1060 getI32Imm(0));
Chris Lattner25dae722005-09-03 00:53:47 +00001061 return SDOperand(N, 0);
Chris Lattnere28e40a2005-08-25 00:45:43 +00001062 }
Chris Lattner34e17052005-08-25 05:04:11 +00001063 case ISD::ConstantPool: {
Chris Lattner5839bf22005-08-26 17:15:30 +00001064 Constant *C = cast<ConstantPoolSDNode>(N)->get();
1065 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
Chris Lattner34e17052005-08-25 05:04:11 +00001066 if (PICEnabled)
1067 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
1068 else
1069 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001070 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
Chris Lattner25dae722005-09-03 00:53:47 +00001071 return SDOperand(N, 0);
Chris Lattner34e17052005-08-25 05:04:11 +00001072 }
Chris Lattner4416f1a2005-08-19 22:38:53 +00001073 case ISD::GlobalAddress: {
1074 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1075 SDOperand Tmp;
1076 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +00001077 if (PICEnabled)
1078 Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA);
1079 else
Chris Lattner4416f1a2005-08-19 22:38:53 +00001080 Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA);
Chris Lattner9944b762005-08-21 22:31:09 +00001081
Chris Lattner4416f1a2005-08-19 22:38:53 +00001082 if (GV->hasWeakLinkage() || GV->isExternal())
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001083 CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp);
Chris Lattner4416f1a2005-08-19 22:38:53 +00001084 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001085 CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA);
Chris Lattner25dae722005-09-03 00:53:47 +00001086 return SDOperand(N, 0);
Chris Lattner4416f1a2005-08-19 22:38:53 +00001087 }
Chris Lattner222adac2005-10-06 19:03:35 +00001088
Chris Lattner867940d2005-10-02 06:58:23 +00001089 case PPCISD::FSEL: {
Chris Lattner43f07a42005-10-02 07:07:49 +00001090 SDOperand Comparison = Select(N->getOperand(0));
1091 // Extend the comparison to 64-bits.
1092 if (Comparison.getValueType() == MVT::f32)
1093 Comparison = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Comparison);
1094
1095 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FSELS : PPC::FSELD;
1096 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Comparison,
1097 Select(N->getOperand(1)), Select(N->getOperand(2)));
Chris Lattner25dae722005-09-03 00:53:47 +00001098 return SDOperand(N, 0);
Chris Lattner867940d2005-10-02 06:58:23 +00001099 }
Nate Begemanc09eeec2005-09-06 22:03:27 +00001100 case PPCISD::FCFID:
1101 CurDAG->SelectNodeTo(N, PPC::FCFID, N->getValueType(0),
1102 Select(N->getOperand(0)));
1103 return SDOperand(N, 0);
1104 case PPCISD::FCTIDZ:
1105 CurDAG->SelectNodeTo(N, PPC::FCTIDZ, N->getValueType(0),
1106 Select(N->getOperand(0)));
1107 return SDOperand(N, 0);
Chris Lattnerf7605322005-08-31 21:09:52 +00001108 case PPCISD::FCTIWZ:
1109 CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0),
1110 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001111 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +00001112 case ISD::FADD: {
1113 MVT::ValueType Ty = N->getValueType(0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001114 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattner615c2d02005-09-28 22:29:58 +00001115 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +00001116 N->getOperand(0).Val->hasOneUse()) {
1117 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001118 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001119 Select(N->getOperand(0).getOperand(0)),
1120 Select(N->getOperand(0).getOperand(1)),
1121 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001122 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +00001123 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +00001124 N->getOperand(1).hasOneUse()) {
1125 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001126 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001127 Select(N->getOperand(1).getOperand(0)),
1128 Select(N->getOperand(1).getOperand(1)),
1129 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001130 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001131 }
1132 }
1133
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001134 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001135 Select(N->getOperand(0)), Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001136 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001137 }
Chris Lattner615c2d02005-09-28 22:29:58 +00001138 case ISD::FSUB: {
1139 MVT::ValueType Ty = N->getValueType(0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001140
1141 if (!NoExcessFPPrecision) { // Match FMA ops
Chris Lattner615c2d02005-09-28 22:29:58 +00001142 if (N->getOperand(0).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +00001143 N->getOperand(0).Val->hasOneUse()) {
1144 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001145 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001146 Select(N->getOperand(0).getOperand(0)),
1147 Select(N->getOperand(0).getOperand(1)),
1148 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001149 return SDOperand(N, 0);
Chris Lattner615c2d02005-09-28 22:29:58 +00001150 } else if (N->getOperand(1).getOpcode() == ISD::FMUL &&
Chris Lattnera5a91b12005-08-17 19:33:03 +00001151 N->getOperand(1).Val->hasOneUse()) {
1152 ++FusedFP; // Statistic
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001153 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001154 Select(N->getOperand(1).getOperand(0)),
1155 Select(N->getOperand(1).getOperand(1)),
1156 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001157 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001158 }
1159 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001160 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty,
Chris Lattnera5a91b12005-08-17 19:33:03 +00001161 Select(N->getOperand(0)),
1162 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001163 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001164 }
Chris Lattner88add102005-09-28 22:50:24 +00001165 case ISD::SDIV: {
Chris Lattner8784a232005-08-25 17:50:06 +00001166 unsigned Imm;
1167 if (isIntImmediate(N->getOperand(1), Imm)) {
1168 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
1169 SDOperand Op =
1170 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
1171 Select(N->getOperand(0)),
1172 getI32Imm(Log2_32(Imm)));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001173 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Chris Lattner8784a232005-08-25 17:50:06 +00001174 Op.getValue(0), Op.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +00001175 return SDOperand(N, 0);
Chris Lattner8784a232005-08-25 17:50:06 +00001176 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1177 SDOperand Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001178 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Chris Lattner8784a232005-08-25 17:50:06 +00001179 Select(N->getOperand(0)),
1180 getI32Imm(Log2_32(-Imm)));
1181 SDOperand PT =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001182 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0),
1183 Op.getValue(1));
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001184 CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner25dae722005-09-03 00:53:47 +00001185 return SDOperand(N, 0);
Chris Lattner047b9522005-08-25 22:04:30 +00001186 } else if (Imm) {
1187 SDOperand Result = Select(BuildSDIVSequence(N));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001188 CodeGenMap[Op] = Result;
1189 return Result;
Chris Lattner8784a232005-08-25 17:50:06 +00001190 }
1191 }
Chris Lattner047b9522005-08-25 22:04:30 +00001192
Chris Lattner237733e2005-09-29 23:33:31 +00001193 // Other cases are autogenerated.
1194 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001195 }
1196 case ISD::UDIV: {
1197 // If this is a divide by constant, we can emit code using some magic
1198 // constants to implement it as a multiply instead.
1199 unsigned Imm;
Chris Lattnera9317ed2005-08-25 23:21:06 +00001200 if (isIntImmediate(N->getOperand(1), Imm) && Imm) {
Chris Lattner047b9522005-08-25 22:04:30 +00001201 SDOperand Result = Select(BuildUDIVSequence(N));
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001202 CodeGenMap[Op] = Result;
1203 return Result;
Chris Lattner047b9522005-08-25 22:04:30 +00001204 }
1205
Chris Lattner237733e2005-09-29 23:33:31 +00001206 // Other cases are autogenerated.
1207 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001208 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001209 case ISD::AND: {
Nate Begemana6940472005-08-18 18:01:39 +00001210 unsigned Imm;
Nate Begemancffc32b2005-08-18 07:30:46 +00001211 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1212 // with a mask, emit rlwinm
1213 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
1214 isShiftedMask_32(~Imm))) {
1215 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001216 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001217 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
1218 Val = Select(N->getOperand(0).getOperand(0));
1219 } else {
1220 Val = Select(N->getOperand(0));
1221 isRunOfOnes(Imm, MB, ME);
1222 SH = 0;
1223 }
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001224 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
Nate Begemancffc32b2005-08-18 07:30:46 +00001225 getI32Imm(MB), getI32Imm(ME));
Chris Lattner25dae722005-09-03 00:53:47 +00001226 return SDOperand(N, 0);
Nate Begemancffc32b2005-08-18 07:30:46 +00001227 }
Chris Lattner237733e2005-09-29 23:33:31 +00001228
1229 // Other cases are autogenerated.
1230 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001231 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001232 case ISD::OR:
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001233 if (SDNode *I = SelectBitfieldInsert(N))
1234 return CodeGenMap[Op] = SDOperand(I, 0);
1235
Nate Begeman02b88a42005-08-19 00:38:14 +00001236 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0),
1237 N->getOperand(1),
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001238 PPC::ORIS, PPC::ORI))
1239 return CodeGenMap[Op] = SDOperand(I, 0);
1240
Chris Lattner237733e2005-09-29 23:33:31 +00001241 // Other cases are autogenerated.
1242 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001243 case ISD::SHL: {
1244 unsigned Imm, SH, MB, ME;
1245 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1246 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001247 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001248 Select(N->getOperand(0).getOperand(0)),
1249 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1250 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001251 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001252 getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm));
1253 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001254 CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001255 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001256 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001257 }
1258 case ISD::SRL: {
1259 unsigned Imm, SH, MB, ME;
1260 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1261 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001262 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001263 Select(N->getOperand(0).getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001264 getI32Imm(SH & 0x1F), getI32Imm(MB), getI32Imm(ME));
Nate Begemanc15ed442005-08-18 23:38:00 +00001265 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001266 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc09eeec2005-09-06 22:03:27 +00001267 getI32Imm((32-Imm) & 0x1F), getI32Imm(Imm),
1268 getI32Imm(31));
Nate Begemanc15ed442005-08-18 23:38:00 +00001269 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001270 CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001271 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001272 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001273 }
1274 case ISD::SRA: {
1275 unsigned Imm, SH, MB, ME;
1276 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1277 isRotateAndMask(N, Imm, true, SH, MB, ME))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001278 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Nate Begemanc15ed442005-08-18 23:38:00 +00001279 Select(N->getOperand(0).getOperand(0)),
1280 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
1281 else if (isIntImmediate(N->getOperand(1), Imm))
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001282 CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001283 getI32Imm(Imm));
1284 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001285 CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)),
Nate Begemanc15ed442005-08-18 23:38:00 +00001286 Select(N->getOperand(1)));
Chris Lattner25dae722005-09-03 00:53:47 +00001287 return SDOperand(N, 0);
Nate Begemanc15ed442005-08-18 23:38:00 +00001288 }
Chris Lattnerd8ead9e2005-09-28 22:53:16 +00001289 case ISD::FMUL: {
1290 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FMULS : PPC::FMUL;
1291 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
1292 Select(N->getOperand(1)));
1293 return SDOperand(N, 0);
1294 }
1295 case ISD::FDIV: {
1296 unsigned Opc = N->getValueType(0) == MVT::f32 ? PPC::FDIVS : PPC::FDIV;
1297 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)),
1298 Select(N->getOperand(1)));
1299 return SDOperand(N, 0);
1300 }
Nate Begeman305a1c72005-08-18 03:04:18 +00001301 case ISD::FABS:
Chris Lattner919c0322005-10-01 01:35:02 +00001302 if (N->getValueType(0) == MVT::f32)
1303 CurDAG->SelectNodeTo(N, PPC::FABSS, MVT::f32, Select(N->getOperand(0)));
1304 else
1305 CurDAG->SelectNodeTo(N, PPC::FABSD, MVT::f64, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001306 return SDOperand(N, 0);
Chris Lattner8f838722005-08-30 00:30:43 +00001307 case ISD::FP_EXTEND:
Nate Begeman305a1c72005-08-18 03:04:18 +00001308 assert(MVT::f64 == N->getValueType(0) &&
1309 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
Chris Lattner8f838722005-08-30 00:30:43 +00001310 // We need to emit an FMR to make sure that the result has the right value
1311 // type.
Chris Lattner919c0322005-10-01 01:35:02 +00001312 CurDAG->SelectNodeTo(N, PPC::FMRSD, MVT::f64, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001313 return SDOperand(N, 0);
Nate Begeman305a1c72005-08-18 03:04:18 +00001314 case ISD::FP_ROUND:
1315 assert(MVT::f32 == N->getValueType(0) &&
1316 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001317 CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001318 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001319 case ISD::FNEG: {
1320 SDOperand Val = Select(N->getOperand(0));
1321 MVT::ValueType Ty = N->getValueType(0);
1322 if (Val.Val->hasOneUse()) {
1323 unsigned Opc;
Chris Lattner528f58e2005-08-28 23:39:22 +00001324 switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) {
Nate Begeman26653502005-08-17 23:46:35 +00001325 default: Opc = 0; break;
Chris Lattner919c0322005-10-01 01:35:02 +00001326 case PPC::FABSS: Opc = PPC::FNABSS; break;
1327 case PPC::FABSD: Opc = PPC::FNABSD; break;
Nate Begeman26653502005-08-17 23:46:35 +00001328 case PPC::FMADD: Opc = PPC::FNMADD; break;
1329 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
1330 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
1331 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
1332 }
1333 // If we inverted the opcode, then emit the new instruction with the
1334 // inverted opcode and the original instruction's operands. Otherwise,
1335 // fall through and generate a fneg instruction.
1336 if (Opc) {
Chris Lattner919c0322005-10-01 01:35:02 +00001337 if (Opc == PPC::FNABSS || Opc == PPC::FNABSD)
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001338 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0));
Nate Begeman26653502005-08-17 23:46:35 +00001339 else
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001340 CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0),
Nate Begeman26653502005-08-17 23:46:35 +00001341 Val.getOperand(1), Val.getOperand(2));
Chris Lattner25dae722005-09-03 00:53:47 +00001342 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001343 }
1344 }
Chris Lattner919c0322005-10-01 01:35:02 +00001345 if (Ty == MVT::f32)
1346 CurDAG->SelectNodeTo(N, PPC::FNEGS, MVT::f32, Val);
1347 else
Chris Lattner2c1760f2005-10-01 02:51:36 +00001348 CurDAG->SelectNodeTo(N, PPC::FNEGD, MVT::f64, Val);
Chris Lattner25dae722005-09-03 00:53:47 +00001349 return SDOperand(N, 0);
Nate Begeman26653502005-08-17 23:46:35 +00001350 }
Nate Begeman6a7d6112005-08-18 00:53:47 +00001351 case ISD::FSQRT: {
1352 MVT::ValueType Ty = N->getValueType(0);
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001353 CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty,
Nate Begeman6a7d6112005-08-18 00:53:47 +00001354 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001355 return SDOperand(N, 0);
Nate Begeman6a7d6112005-08-18 00:53:47 +00001356 }
Chris Lattner222adac2005-10-06 19:03:35 +00001357
Chris Lattner9944b762005-08-21 22:31:09 +00001358 case ISD::LOAD:
1359 case ISD::EXTLOAD:
1360 case ISD::ZEXTLOAD:
1361 case ISD::SEXTLOAD: {
1362 SDOperand Op1, Op2;
1363 bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2);
1364
1365 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
1366 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
1367 unsigned Opc;
1368 switch (TypeBeingLoaded) {
1369 default: N->dump(); assert(0 && "Cannot load this type!");
1370 case MVT::i1:
1371 case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break;
1372 case MVT::i16:
1373 if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load?
1374 Opc = isIdx ? PPC::LHAX : PPC::LHA;
1375 } else {
1376 Opc = isIdx ? PPC::LHZX : PPC::LHZ;
1377 }
1378 break;
1379 case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break;
1380 case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break;
1381 case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break;
1382 }
1383
Chris Lattner919c0322005-10-01 01:35:02 +00001384 // If this is an f32 -> f64 load, emit the f32 load, then use an 'extending
1385 // copy'.
1386 if (TypeBeingLoaded != MVT::f32 || N->getOpcode() == ISD::LOAD) {
1387 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
1388 Op1, Op2, Select(N->getOperand(0)));
1389 return SDOperand(N, Op.ResNo);
1390 } else {
1391 std::vector<SDOperand> Ops;
1392 Ops.push_back(Op1);
1393 Ops.push_back(Op2);
1394 Ops.push_back(Select(N->getOperand(0)));
1395 SDOperand Res = CurDAG->getTargetNode(Opc, MVT::f32, MVT::Other, Ops);
1396 SDOperand Ext = CurDAG->getTargetNode(PPC::FMRSD, MVT::f64, Res);
1397 CodeGenMap[Op.getValue(0)] = Ext;
1398 CodeGenMap[Op.getValue(1)] = Res.getValue(1);
1399 if (Op.ResNo)
1400 return Res.getValue(1);
1401 else
1402 return Ext;
1403 }
Chris Lattner9944b762005-08-21 22:31:09 +00001404 }
1405
Chris Lattnerf7f22552005-08-22 01:27:59 +00001406 case ISD::TRUNCSTORE:
1407 case ISD::STORE: {
1408 SDOperand AddrOp1, AddrOp2;
1409 bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2);
1410
1411 unsigned Opc;
1412 if (N->getOpcode() == ISD::STORE) {
1413 switch (N->getOperand(1).getValueType()) {
1414 default: assert(0 && "unknown Type in store");
1415 case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break;
1416 case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break;
1417 case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break;
1418 }
1419 } else { //ISD::TRUNCSTORE
1420 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
1421 default: assert(0 && "unknown Type in store");
Chris Lattnerf7f22552005-08-22 01:27:59 +00001422 case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break;
1423 case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break;
1424 }
1425 }
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001426
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001427 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)),
Chris Lattnerf7f22552005-08-22 01:27:59 +00001428 AddrOp1, AddrOp2, Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001429 return SDOperand(N, 0);
Chris Lattnerf7f22552005-08-22 01:27:59 +00001430 }
Chris Lattner64906a02005-08-25 20:08:18 +00001431
Chris Lattner13794f52005-08-26 18:46:49 +00001432 case ISD::SELECT_CC: {
1433 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1434
1435 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1436 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1437 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1438 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1439 if (N1C->isNullValue() && N3C->isNullValue() &&
1440 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
1441 SDOperand LHS = Select(N->getOperand(0));
1442 SDOperand Tmp =
1443 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1444 LHS, getI32Imm(~0U));
1445 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
1446 Tmp.getValue(1));
Chris Lattner25dae722005-09-03 00:53:47 +00001447 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001448 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001449
Chris Lattner50ff55c2005-09-01 19:20:44 +00001450 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001451 unsigned BROpc = getBCCForSetCC(CC);
1452
1453 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001454 unsigned SelectCCOp;
1455 if (MVT::isInteger(N->getValueType(0)))
1456 SelectCCOp = PPC::SELECT_CC_Int;
1457 else if (N->getValueType(0) == MVT::f32)
1458 SelectCCOp = PPC::SELECT_CC_F4;
1459 else
1460 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001461 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1462 Select(N->getOperand(2)), Select(N->getOperand(3)),
1463 getI32Imm(BROpc));
Chris Lattner25dae722005-09-03 00:53:47 +00001464 return SDOperand(N, 0);
Chris Lattner13794f52005-08-26 18:46:49 +00001465 }
1466
Chris Lattnera2590c52005-08-24 00:47:15 +00001467 case ISD::CALLSEQ_START:
1468 case ISD::CALLSEQ_END: {
1469 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
1470 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
1471 PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001472 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
Chris Lattnerfb0c9642005-08-24 22:45:17 +00001473 getI32Imm(Amt), Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001474 return SDOperand(N, 0);
Chris Lattnera2590c52005-08-24 00:47:15 +00001475 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001476 case ISD::RET: {
1477 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
1478
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001479 if (N->getNumOperands() == 2) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001480 SDOperand Val = Select(N->getOperand(1));
Chris Lattnereb80fe82005-08-30 22:59:48 +00001481 if (N->getOperand(1).getValueType() == MVT::i32) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001482 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
Chris Lattnereb80fe82005-08-30 22:59:48 +00001483 } else {
1484 assert(MVT::isFloatingPoint(N->getOperand(1).getValueType()));
1485 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001486 }
Chris Lattner7a49fdc2005-08-31 01:34:29 +00001487 } else if (N->getNumOperands() > 1) {
1488 assert(N->getOperand(1).getValueType() == MVT::i32 &&
1489 N->getOperand(2).getValueType() == MVT::i32 &&
1490 N->getNumOperands() == 3 && "Unknown two-register ret value!");
1491 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1)));
1492 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2)));
Chris Lattnera5a91b12005-08-17 19:33:03 +00001493 }
1494
1495 // Finally, select this to a blr (return) instruction.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001496 CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain);
Chris Lattner25dae722005-09-03 00:53:47 +00001497 return SDOperand(N, 0);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001498 }
Chris Lattner89532c72005-08-25 00:29:58 +00001499 case ISD::BR:
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001500 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1),
Chris Lattner89532c72005-08-25 00:29:58 +00001501 Select(N->getOperand(0)));
Chris Lattner25dae722005-09-03 00:53:47 +00001502 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001503 case ISD::BR_CC:
1504 case ISD::BRTWOWAY_CC: {
1505 SDOperand Chain = Select(N->getOperand(0));
1506 MachineBasicBlock *Dest =
1507 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1508 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1509 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001510
1511 // If this is a two way branch, then grab the fallthrough basic block
1512 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1513 // conversion if necessary by the branch selection pass. Otherwise, emit a
1514 // standard conditional branch.
1515 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
Chris Lattnerca0a4772005-10-01 23:06:26 +00001516 SDOperand CondTrueBlock = N->getOperand(4);
1517 SDOperand CondFalseBlock = N->getOperand(5);
1518
1519 // If the false case is the current basic block, then this is a self loop.
1520 // We do not want to emit "Loop: ... brcond Out; br Loop", as it adds an
1521 // extra dispatch group to the loop. Instead, invert the condition and
1522 // emit "Loop: ... br!cond Loop; br Out
1523 if (cast<BasicBlockSDNode>(CondFalseBlock)->getBasicBlock() == BB) {
1524 std::swap(CondTrueBlock, CondFalseBlock);
1525 CC = getSetCCInverse(CC,
1526 MVT::isInteger(N->getOperand(2).getValueType()));
1527 }
1528
1529 unsigned Opc = getBCCForSetCC(CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001530 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1531 CondCode, getI32Imm(Opc),
Chris Lattnerca0a4772005-10-01 23:06:26 +00001532 CondTrueBlock, CondFalseBlock,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001533 Chain);
Chris Lattnerca0a4772005-10-01 23:06:26 +00001534 CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, CondFalseBlock, CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001535 } else {
1536 // Iterate to the next basic block
1537 ilist<MachineBasicBlock>::iterator It = BB;
1538 ++It;
1539
1540 // If the fallthrough path is off the end of the function, which would be
1541 // undefined behavior, set it to be the same as the current block because
1542 // we have nothing better to set it to, and leaving it alone will cause
1543 // the PowerPC Branch Selection pass to crash.
1544 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001545 CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
Chris Lattnerca0a4772005-10-01 23:06:26 +00001546 getI32Imm(getBCCForSetCC(CC)), N->getOperand(4),
Chris Lattner2fbb4572005-08-21 18:50:37 +00001547 CurDAG->getBasicBlock(It), Chain);
1548 }
Chris Lattner25dae722005-09-03 00:53:47 +00001549 return SDOperand(N, 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001550 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001551 }
Chris Lattner25dae722005-09-03 00:53:47 +00001552
Chris Lattner19c09072005-09-07 23:45:15 +00001553 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001554}
1555
1556
1557/// createPPC32ISelDag - This pass converts a legalized DAG into a
1558/// PowerPC-specific DAG, ready for instruction scheduling.
1559///
1560FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
1561 return new PPC32DAGToDAGISel(TM);
1562}
1563