blob: 5c74a7ba6cd6690b8e20400cbb7e4527151b122e [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattnera5a91b12005-08-17 19:33:03 +00002//
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//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattnera5a91b12005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
17#include "PPCISelLowering.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "PPCHazardRecognizers.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000031#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000032using namespace llvm;
33
34namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000035 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
36
37 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000038 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000039 /// instructions for SelectionDAG operations.
40 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 class PPCDAGToDAGISel : public SelectionDAGISel {
Nate Begeman21e463b2005-10-16 05:39:50 +000042 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000043 unsigned GlobalBaseReg;
Chris Lattnerc6644182006-03-07 06:32:48 +000044 PPCHazardRecognizer970 PPC970HR;
Chris Lattnera5a91b12005-08-17 19:33:03 +000045 public:
Nate Begeman1d9d7422005-10-18 00:28:58 +000046 PPCDAGToDAGISel(TargetMachine &TM)
Nate Begeman21e463b2005-10-16 05:39:50 +000047 : SelectionDAGISel(PPCLowering), PPCLowering(TM) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000048
Chris Lattner4416f1a2005-08-19 22:38:53 +000049 virtual bool runOnFunction(Function &Fn) {
50 // Make sure we re-emit a set of the global base reg if necessary
51 GlobalBaseReg = 0;
52 return SelectionDAGISel::runOnFunction(Fn);
53 }
54
Chris Lattnera5a91b12005-08-17 19:33:03 +000055 /// getI32Imm - Return a target constant with the specified value, of type
56 /// i32.
57 inline SDOperand getI32Imm(unsigned Imm) {
58 return CurDAG->getTargetConstant(Imm, MVT::i32);
59 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000060
61 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
62 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000063 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000064
65 // Select - Convert the specified operand from a target-independent to a
66 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000067 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000068
Nate Begeman02b88a42005-08-19 00:38:14 +000069 SDNode *SelectBitfieldInsert(SDNode *N);
70
Chris Lattner2fbb4572005-08-21 18:50:37 +000071 /// SelectCC - Select a comparison of the specified values with the
72 /// specified condition code, returning the CR# of the expression.
73 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
74
Nate Begeman7fd1edd2005-12-19 23:25:09 +000075 /// SelectAddrImm - Returns true if the address N can be represented by
76 /// a base register plus a signed 16-bit displacement [r+imm].
77 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
78
79 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
80 /// represented as an indexed [r+r] operation. Returns false if it can
81 /// be represented by [r+imm], which are preferred.
82 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000083
Nate Begeman7fd1edd2005-12-19 23:25:09 +000084 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
85 /// represented as an indexed [r+r] operation.
86 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000087
Chris Lattnere5d88612006-02-24 02:13:12 +000088 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
89 /// inline asm expressions.
90 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
91 char ConstraintCode,
92 std::vector<SDOperand> &OutOps,
93 SelectionDAG &DAG) {
94 SDOperand Op0, Op1;
95 switch (ConstraintCode) {
96 default: return true;
97 case 'm': // memory
98 if (!SelectAddrIdx(Op, Op0, Op1))
99 SelectAddrImm(Op, Op0, Op1);
100 break;
101 case 'o': // offsetable
102 if (!SelectAddrImm(Op, Op0, Op1)) {
103 Select(Op0, Op); // r+0.
104 Op1 = getI32Imm(0);
105 }
106 break;
107 case 'v': // not offsetable
108 SelectAddrIdxOnly(Op, Op0, Op1);
109 break;
110 }
111
112 OutOps.push_back(Op0);
113 OutOps.push_back(Op1);
114 return false;
115 }
116
Chris Lattner047b9522005-08-25 22:04:30 +0000117 SDOperand BuildSDIVSequence(SDNode *N);
118 SDOperand BuildUDIVSequence(SDNode *N);
119
Chris Lattnera5a91b12005-08-17 19:33:03 +0000120 /// InstructionSelectBasicBlock - This callback is invoked by
121 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000122 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
123
Chris Lattnera5a91b12005-08-17 19:33:03 +0000124 virtual const char *getPassName() const {
125 return "PowerPC DAG->DAG Pattern Instruction Selection";
126 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000127
128 /// GetTargetHazardRecognizer - Return the hazard recognizer to use for this
129 /// target when scheduling the DAG.
130 virtual HazardRecognizer &GetTargetHazardRecognizer() {
131 // Should use subtarget info to pick the right hazard recognizer. For
132 // now, always return a PPC970 recognizer.
133 return PPC970HR;
134 }
Chris Lattneraf165382005-09-13 22:03:06 +0000135
136// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000137#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000138
139private:
Chris Lattner222adac2005-10-06 19:03:35 +0000140 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000141 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000142 };
143}
144
Chris Lattnerbd937b92005-10-06 18:45:51 +0000145/// InstructionSelectBasicBlock - This callback is invoked by
146/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000147void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000148 DEBUG(BB->dump());
149
150 // The selection process is inherently a bottom-up recursive process (users
151 // select their uses before themselves). Given infinite stack space, we
152 // could just start selecting on the root and traverse the whole graph. In
153 // practice however, this causes us to run out of stack space on large basic
154 // blocks. To avoid this problem, select the entry node, then all its uses,
155 // iteratively instead of recursively.
156 std::vector<SDOperand> Worklist;
157 Worklist.push_back(DAG.getEntryNode());
158
159 // Note that we can do this in the PPC target (scanning forward across token
160 // chain edges) because no nodes ever get folded across these edges. On a
161 // target like X86 which supports load/modify/store operations, this would
162 // have to be more careful.
163 while (!Worklist.empty()) {
164 SDOperand Node = Worklist.back();
165 Worklist.pop_back();
166
Chris Lattnercf01a702005-10-07 22:10:27 +0000167 // Chose from the least deep of the top two nodes.
168 if (!Worklist.empty() &&
169 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
170 std::swap(Worklist.back(), Node);
171
Chris Lattnerbd937b92005-10-06 18:45:51 +0000172 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
173 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
174 CodeGenMap.count(Node)) continue;
175
176 for (SDNode::use_iterator UI = Node.Val->use_begin(),
177 E = Node.Val->use_end(); UI != E; ++UI) {
178 // Scan the values. If this use has a value that is a token chain, add it
179 // to the worklist.
180 SDNode *User = *UI;
181 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
182 if (User->getValueType(i) == MVT::Other) {
183 Worklist.push_back(SDOperand(User, i));
184 break;
185 }
186 }
187
188 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000189 SDOperand Dummy;
190 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000191 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000192
Chris Lattnerbd937b92005-10-06 18:45:51 +0000193 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000194 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000195 CodeGenMap.clear();
196 DAG.RemoveDeadNodes();
197
198 // Emit machine code to BB.
199 ScheduleAndEmitDAG(DAG);
200}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000201
Chris Lattner4416f1a2005-08-19 22:38:53 +0000202/// getGlobalBaseReg - Output the instructions required to put the
203/// base address to use for accessing globals into a register.
204///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000205SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000206 if (!GlobalBaseReg) {
207 // Insert the set of GlobalBaseReg into the first MBB of the function
208 MachineBasicBlock &FirstMBB = BB->getParent()->front();
209 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
210 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000211 // FIXME: when we get to LP64, we will need to create the appropriate
212 // type of register here.
213 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000214 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
215 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
216 }
Chris Lattner9944b762005-08-21 22:31:09 +0000217 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000218}
219
220
Nate Begeman0f3257a2005-08-18 05:00:13 +0000221// isIntImmediate - This method tests to see if a constant operand.
222// If so Imm will receive the 32 bit value.
223static bool isIntImmediate(SDNode *N, unsigned& Imm) {
224 if (N->getOpcode() == ISD::Constant) {
225 Imm = cast<ConstantSDNode>(N)->getValue();
226 return true;
227 }
228 return false;
229}
230
Nate Begemancffc32b2005-08-18 07:30:46 +0000231// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
232// any number of 0s on either side. The 1s are allowed to wrap from LSB to
233// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
234// not, since all 1s are not contiguous.
235static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
236 if (isShiftedMask_32(Val)) {
237 // look for the first non-zero bit
238 MB = CountLeadingZeros_32(Val);
239 // look for the first zero bit after the run of ones
240 ME = CountLeadingZeros_32((Val - 1) ^ Val);
241 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000242 } else {
243 Val = ~Val; // invert mask
244 if (isShiftedMask_32(Val)) {
245 // effectively look for the first zero bit
246 ME = CountLeadingZeros_32(Val) - 1;
247 // effectively look for the first one bit after the run of zeros
248 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
249 return true;
250 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000251 }
252 // no run present
253 return false;
254}
255
Chris Lattner65a419a2005-10-09 05:36:17 +0000256// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000257// and mask opcode and mask operation.
258static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
259 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000260 // Don't even go down this path for i64, since different logic will be
261 // necessary for rldicl/rldicr/rldimi.
262 if (N->getValueType(0) != MVT::i32)
263 return false;
264
Nate Begemancffc32b2005-08-18 07:30:46 +0000265 unsigned Shift = 32;
266 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
267 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000268 if (N->getNumOperands() != 2 ||
269 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000270 return false;
271
272 if (Opcode == ISD::SHL) {
273 // apply shift left to mask if it comes first
274 if (IsShiftMask) Mask = Mask << Shift;
275 // determine which bits are made indeterminant by shift
276 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000277 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000278 // apply shift right to mask if it comes first
279 if (IsShiftMask) Mask = Mask >> Shift;
280 // determine which bits are made indeterminant by shift
281 Indeterminant = ~(0xFFFFFFFFu >> Shift);
282 // adjust for the left rotate
283 Shift = 32 - Shift;
284 } else {
285 return false;
286 }
287
288 // if the mask doesn't intersect any Indeterminant bits
289 if (Mask && !(Mask & Indeterminant)) {
290 SH = Shift;
291 // make sure the mask is still a mask (wrap arounds may not be)
292 return isRunOfOnes(Mask, MB, ME);
293 }
294 return false;
295}
296
Nate Begeman0f3257a2005-08-18 05:00:13 +0000297// isOpcWithIntImmediate - This method tests to see if the node is a specific
298// opcode and that it has a immediate integer right operand.
299// If so Imm will receive the 32 bit value.
300static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
301 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
302}
303
Chris Lattnera5a91b12005-08-17 19:33:03 +0000304// isIntImmediate - This method tests to see if a constant operand.
305// If so Imm will receive the 32 bit value.
306static bool isIntImmediate(SDOperand N, unsigned& Imm) {
307 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
308 Imm = (unsigned)CN->getSignExtended();
309 return true;
310 }
311 return false;
312}
313
Nate Begeman02b88a42005-08-19 00:38:14 +0000314/// SelectBitfieldInsert - turn an or of two masked values into
315/// the rotate left word immediate then mask insert (rlwimi) instruction.
316/// Returns true on success, false if the caller still needs to select OR.
317///
318/// Patterns matched:
319/// 1. or shl, and 5. or and, and
320/// 2. or and, shl 6. or shl, shr
321/// 3. or shr, and 7. or shr, shl
322/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000323SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000324 bool IsRotate = false;
325 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
326 unsigned Value;
327
328 SDOperand Op0 = N->getOperand(0);
329 SDOperand Op1 = N->getOperand(1);
330
331 unsigned Op0Opc = Op0.getOpcode();
332 unsigned Op1Opc = Op1.getOpcode();
333
334 // Verify that we have the correct opcodes
335 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
336 return false;
337 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
338 return false;
339
340 // Generate Mask value for Target
341 if (isIntImmediate(Op0.getOperand(1), Value)) {
342 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000343 case ISD::SHL: TgtMask <<= Value; break;
344 case ISD::SRL: TgtMask >>= Value; break;
345 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000346 }
347 } else {
348 return 0;
349 }
350
351 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000352 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000353 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000354
355 switch(Op1Opc) {
356 case ISD::SHL:
357 SH = Value;
358 InsMask <<= SH;
359 if (Op0Opc == ISD::SRL) IsRotate = true;
360 break;
361 case ISD::SRL:
362 SH = Value;
363 InsMask >>= SH;
364 SH = 32-SH;
365 if (Op0Opc == ISD::SHL) IsRotate = true;
366 break;
367 case ISD::AND:
368 InsMask &= Value;
369 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000370 }
371
372 // If both of the inputs are ANDs and one of them has a logical shift by
373 // constant as its input, make that AND the inserted value so that we can
374 // combine the shift into the rotate part of the rlwimi instruction
375 bool IsAndWithShiftOp = false;
376 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
377 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
378 Op1.getOperand(0).getOpcode() == ISD::SRL) {
379 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
380 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
381 IsAndWithShiftOp = true;
382 }
383 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
384 Op0.getOperand(0).getOpcode() == ISD::SRL) {
385 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
386 std::swap(Op0, Op1);
387 std::swap(TgtMask, InsMask);
388 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
389 IsAndWithShiftOp = true;
390 }
391 }
392 }
393
394 // Verify that the Target mask and Insert mask together form a full word mask
395 // and that the Insert mask is a run of set bits (which implies both are runs
396 // of set bits). Given that, Select the arguments and generate the rlwimi
397 // instruction.
398 unsigned MB, ME;
399 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
400 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
401 bool Op0IsAND = Op0Opc == ISD::AND;
402 // Check for rotlwi / rotrwi here, a special case of bitfield insert
403 // where both bitfield halves are sourced from the same value.
404 if (IsRotate && fullMask &&
405 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000406 SDOperand Tmp;
407 Select(Tmp, N->getOperand(0).getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000408 return CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Tmp,
409 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
Nate Begeman02b88a42005-08-19 00:38:14 +0000410 }
Evan Cheng34167212006-02-09 00:37:58 +0000411 SDOperand Tmp1, Tmp2;
412 Select(Tmp1, ((Op0IsAND && fullMask) ? Op0.getOperand(0) : Op0));
413 Select(Tmp2, (IsAndWithShiftOp ? Op1.getOperand(0).getOperand(0)
414 : Op1.getOperand(0)));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000415 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
416 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000417 }
418 return 0;
419}
420
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000421/// SelectAddrImm - Returns true if the address N can be represented by
422/// a base register plus a signed 16-bit displacement [r+imm].
423bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
424 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000425 // If this can be more profitably realized as r+r, fail.
426 if (SelectAddrIdx(N, Disp, Base))
427 return false;
428
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000429 if (N.getOpcode() == ISD::ADD) {
430 unsigned imm = 0;
431 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000432 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000433 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
434 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000435 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000436 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000437 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000438 return true; // [r+i]
439 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000440 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000441 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000442 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000443 Disp = N.getOperand(1).getOperand(0); // The global address.
444 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
445 Disp.getOpcode() == ISD::TargetConstantPool);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000446 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000447 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000448 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000449 } else if (N.getOpcode() == ISD::OR) {
450 unsigned imm = 0;
451 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
452 // If this is an or of disjoint bitfields, we can codegen this as an add
453 // (for better address arithmetic) if the LHS and RHS of the OR are
454 // provably disjoint.
455 uint64_t LHSKnownZero, LHSKnownOne;
456 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
457 LHSKnownZero, LHSKnownOne);
458 if ((LHSKnownZero|~imm) == ~0U) {
459 // If all of the bits are known zero on the LHS or RHS, the add won't
460 // carry.
461 Base = N.getOperand(0);
462 Disp = getI32Imm(imm & 0xFFFF);
463 return true;
464 }
465 }
Chris Lattner9944b762005-08-21 22:31:09 +0000466 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000467 Disp = getI32Imm(0);
468 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
469 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000470 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000471 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000472 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000473}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000474
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000475/// SelectAddrIdx - Given the specified addressed, check to see if it can be
476/// represented as an indexed [r+r] operation. Returns false if it can
477/// be represented by [r+imm], which are preferred.
478bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
479 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000480 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000481 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000482 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
483 return false; // r+i
484 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
485 return false; // r+i
486
Evan Cheng7564e0b2006-02-05 08:45:01 +0000487 Base = N.getOperand(0);
488 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000489 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000490 } else if (N.getOpcode() == ISD::OR) {
491 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
492 return false; // r+i can fold it if we can.
493
494 // If this is an or of disjoint bitfields, we can codegen this as an add
495 // (for better address arithmetic) if the LHS and RHS of the OR are provably
496 // disjoint.
497 uint64_t LHSKnownZero, LHSKnownOne;
498 uint64_t RHSKnownZero, RHSKnownOne;
499 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
500 LHSKnownZero, LHSKnownOne);
501
502 if (LHSKnownZero) {
503 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
504 RHSKnownZero, RHSKnownOne);
505 // If all of the bits are known zero on the LHS or RHS, the add won't
506 // carry.
507 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
508 Base = N.getOperand(0);
509 Index = N.getOperand(1);
510 return true;
511 }
512 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000513 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000514
515 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000516}
517
518/// SelectAddrIdxOnly - Given the specified addressed, force it to be
519/// represented as an indexed [r+r] operation.
520bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
521 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000522 // Check to see if we can easily represent this as an [r+r] address. This
523 // will fail if it thinks that the address is more profitably represented as
524 // reg+imm, e.g. where imm = 0.
525 if (!SelectAddrIdx(N, Base, Index)) {
526 // Nope, do it the hard way.
527 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
528 Index = N;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000529 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000530 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000531}
532
Chris Lattner2fbb4572005-08-21 18:50:37 +0000533/// SelectCC - Select a comparison of the specified values with the specified
534/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000535SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
536 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000537 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000538 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000539
540 // Use U to determine whether the SETCC immediate range is signed or not.
541 if (MVT::isInteger(LHS.getValueType())) {
542 bool U = ISD::isUnsignedIntSetCC(CC);
543 unsigned Imm;
544 if (isIntImmediate(RHS, Imm) &&
545 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000546 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
547 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000548 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000549 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
550 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000551 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000552 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000553 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000554 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000555 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000556 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000557 }
558}
559
560/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
561/// to Condition.
562static unsigned getBCCForSetCC(ISD::CondCode CC) {
563 switch (CC) {
564 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000565 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000566 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000567 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000568 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000569 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000570 case ISD::SETULT:
571 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000572 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000573 case ISD::SETULE:
574 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000575 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000576 case ISD::SETUGT:
577 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000578 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000579 case ISD::SETUGE:
580 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000581
582 case ISD::SETO: return PPC::BUN;
583 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000584 }
585 return 0;
586}
587
Chris Lattner64906a02005-08-25 20:08:18 +0000588/// getCRIdxForSetCC - Return the index of the condition register field
589/// associated with the SetCC condition, and whether or not the field is
590/// treated as inverted. That is, lt = 0; ge = 0 inverted.
591static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
592 switch (CC) {
593 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000594 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000595 case ISD::SETULT:
596 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000597 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000598 case ISD::SETUGE:
599 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000600 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000601 case ISD::SETUGT:
602 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000603 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000604 case ISD::SETULE:
605 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000606 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000607 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000608 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000609 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000610 case ISD::SETO: Inv = true; return 3;
611 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000612 }
613 return 0;
614}
Chris Lattner9944b762005-08-21 22:31:09 +0000615
Nate Begeman1d9d7422005-10-18 00:28:58 +0000616SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000617 SDNode *N = Op.Val;
618 unsigned Imm;
619 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
620 if (isIntImmediate(N->getOperand(1), Imm)) {
621 // We can codegen setcc op, imm very efficiently compared to a brcond.
622 // Check for those cases here.
623 // setcc op, 0
624 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000625 SDOperand Op;
626 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000627 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000628 default: break;
629 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000630 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000631 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
632 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000633 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000634 SDOperand AD =
635 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
636 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000637 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
638 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000639 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000640 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000641 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
642 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000643 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000644 SDOperand T =
645 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
646 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000647 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
648 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000649 }
650 }
Chris Lattner222adac2005-10-06 19:03:35 +0000651 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000652 SDOperand Op;
653 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000654 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000655 default: break;
656 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000657 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
658 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000659 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000660 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
661 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000662 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000663 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000664 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
665 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
666 Op, getI32Imm(~0U));
667 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
668 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000669 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000670 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000671 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
672 getI32Imm(1)), 0);
673 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
674 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000675 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
676 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000677 }
678 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000679 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
680 getI32Imm(1), getI32Imm(31),
681 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000682 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000683 }
Chris Lattner222adac2005-10-06 19:03:35 +0000684 }
685 }
686
687 bool Inv;
688 unsigned Idx = getCRIdxForSetCC(CC, Inv);
689 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
690 SDOperand IntCR;
691
692 // Force the ccreg into CR7.
693 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
694
Chris Lattner85961d52005-12-06 20:56:18 +0000695 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000696 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
697 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000698
699 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000700 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
701 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000702 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000703 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000704
705 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000706 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
707 getI32Imm((32-(3-Idx)) & 31),
708 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000709 } else {
710 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000711 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
712 getI32Imm((32-(3-Idx)) & 31),
713 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000714 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000715 }
Chris Lattner222adac2005-10-06 19:03:35 +0000716}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000717
Nate Begeman422b0ce2005-11-16 00:48:01 +0000718/// isCallCompatibleAddress - Return true if the specified 32-bit value is
719/// representable in the immediate field of a Bx instruction.
720static bool isCallCompatibleAddress(ConstantSDNode *C) {
721 int Addr = C->getValue();
722 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
723 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
724}
725
Nate Begeman1d9d7422005-10-18 00:28:58 +0000726SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000727 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000728 SDOperand Chain;
729 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000730
731 unsigned CallOpcode;
732 std::vector<SDOperand> CallOperands;
733
734 if (GlobalAddressSDNode *GASD =
735 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000736 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000737 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000738 } else if (ExternalSymbolSDNode *ESSDN =
739 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000740 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000741 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000742 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
743 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
744 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
745 CallOpcode = PPC::BLA;
746 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000747 } else {
748 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000749 SDOperand Callee;
750 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000751 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
752 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000753
754 // Copy the callee address into R12 on darwin.
755 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
756 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000757
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000758 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000759 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000760 }
761
762 unsigned GPR_idx = 0, FPR_idx = 0;
763 static const unsigned GPR[] = {
764 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
765 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
766 };
767 static const unsigned FPR[] = {
768 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
769 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
770 };
771
772 SDOperand InFlag; // Null incoming flag value.
773
774 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
775 unsigned DestReg = 0;
776 MVT::ValueType RegTy = N->getOperand(i).getValueType();
777 if (RegTy == MVT::i32) {
778 assert(GPR_idx < 8 && "Too many int args");
779 DestReg = GPR[GPR_idx++];
780 } else {
781 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
782 "Unpromoted integer arg?");
783 assert(FPR_idx < 13 && "Too many fp args");
784 DestReg = FPR[FPR_idx++];
785 }
786
787 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000788 SDOperand Val;
789 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000790 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
791 InFlag = Chain.getValue(1);
792 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
793 }
794 }
795
796 // Finally, once everything is in registers to pass to the call, emit the
797 // call itself.
798 if (InFlag.Val)
799 CallOperands.push_back(InFlag); // Strong dep on register copies.
800 else
801 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000802 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
803 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000804
805 std::vector<SDOperand> CallResults;
806
807 // If the call has results, copy the values out of the ret val registers.
808 switch (N->getValueType(0)) {
809 default: assert(0 && "Unexpected ret value!");
810 case MVT::Other: break;
811 case MVT::i32:
812 if (N->getValueType(1) == MVT::i32) {
813 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
814 Chain.getValue(1)).getValue(1);
815 CallResults.push_back(Chain.getValue(0));
816 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
817 Chain.getValue(2)).getValue(1);
818 CallResults.push_back(Chain.getValue(0));
819 } else {
820 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
821 Chain.getValue(1)).getValue(1);
822 CallResults.push_back(Chain.getValue(0));
823 }
824 break;
825 case MVT::f32:
826 case MVT::f64:
827 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
828 Chain.getValue(1)).getValue(1);
829 CallResults.push_back(Chain.getValue(0));
830 break;
831 }
832
833 CallResults.push_back(Chain);
834 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
835 CodeGenMap[Op.getValue(i)] = CallResults[i];
836 return CallResults[Op.ResNo];
837}
838
Chris Lattnera5a91b12005-08-17 19:33:03 +0000839// Select - Convert the specified operand from a target-independent to a
840// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000841void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000842 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000843 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000844 N->getOpcode() < PPCISD::FIRST_NUMBER) {
845 Result = Op;
846 return; // Already selected.
847 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000848
849 // If this has already been converted, use it.
850 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000851 if (CGMI != CodeGenMap.end()) {
852 Result = CGMI->second;
853 return;
854 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000855
856 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000857 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000858 case ISD::SETCC:
859 Result = SelectSETCC(Op);
860 return;
861 case PPCISD::CALL:
862 Result = SelectCALL(Op);
863 return;
864 case PPCISD::GlobalBaseReg:
865 Result = getGlobalBaseReg();
866 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000867
Chris Lattnere28e40a2005-08-25 00:45:43 +0000868 case ISD::FrameIndex: {
869 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000870 if (N->hasOneUse()) {
871 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
872 CurDAG->getTargetFrameIndex(FI, MVT::i32),
873 getI32Imm(0));
874 return;
875 }
876 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000877 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
878 CurDAG->getTargetFrameIndex(FI, MVT::i32),
879 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000880 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000881 }
Chris Lattner88add102005-09-28 22:50:24 +0000882 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000883 // FIXME: since this depends on the setting of the carry flag from the srawi
884 // we should really be making notes about that for the scheduler.
885 // FIXME: It sure would be nice if we could cheaply recognize the
886 // srl/add/sra pattern the dag combiner will generate for this as
887 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000888 unsigned Imm;
889 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000890 SDOperand N0;
891 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000892 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000893 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000894 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000895 N0, getI32Imm(Log2_32(Imm)));
896 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000897 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000898 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000899 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000900 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000901 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000902 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000903 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
904 SDOperand(Op, 0), SDOperand(Op, 1)),
905 0);
Evan Cheng34167212006-02-09 00:37:58 +0000906 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000907 }
Evan Cheng34167212006-02-09 00:37:58 +0000908 return;
Chris Lattner8784a232005-08-25 17:50:06 +0000909 }
Chris Lattner047b9522005-08-25 22:04:30 +0000910
Chris Lattner237733e2005-09-29 23:33:31 +0000911 // Other cases are autogenerated.
912 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000913 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000914 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000915 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000916 // If this is an and of a value rotated between 0 and 31 bits and then and'd
917 // with a mask, emit rlwinm
918 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
919 isShiftedMask_32(~Imm))) {
920 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000921 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000922 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000923 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000924 } else if (Imm == 0) {
925 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +0000926 Select(Result, N->getOperand(1));
927 return ;
Chris Lattner3393e802005-10-25 19:32:37 +0000928 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000929 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000930 isRunOfOnes(Imm, MB, ME);
931 SH = 0;
932 }
Evan Cheng34167212006-02-09 00:37:58 +0000933 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
934 getI32Imm(SH), getI32Imm(MB),
935 getI32Imm(ME));
936 return;
Nate Begemancffc32b2005-08-18 07:30:46 +0000937 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000938 // ISD::OR doesn't get all the bitfield insertion fun.
939 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
940 if (isIntImmediate(N->getOperand(1), Imm) &&
941 N->getOperand(0).getOpcode() == ISD::OR &&
942 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000943 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000944 Imm = ~(Imm^Imm2);
945 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000946 SDOperand Tmp1, Tmp2;
947 Select(Tmp1, N->getOperand(0).getOperand(0));
948 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000949 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
950 Tmp1, Tmp2,
951 getI32Imm(0), getI32Imm(MB),
952 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000953 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000954 }
955 }
Chris Lattner237733e2005-09-29 23:33:31 +0000956
957 // Other cases are autogenerated.
958 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000959 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000960 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +0000961 if (SDNode *I = SelectBitfieldInsert(N)) {
962 Result = CodeGenMap[Op] = SDOperand(I, 0);
963 return;
964 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000965
Chris Lattner237733e2005-09-29 23:33:31 +0000966 // Other cases are autogenerated.
967 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000968 case ISD::SHL: {
969 unsigned Imm, SH, MB, ME;
970 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000971 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000972 SDOperand Val;
973 Select(Val, N->getOperand(0).getOperand(0));
974 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
975 Val, getI32Imm(SH), getI32Imm(MB),
976 getI32Imm(ME));
977 return;
Nate Begeman8d948322005-10-19 01:12:32 +0000978 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000979
980 // Other cases are autogenerated.
981 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000982 }
983 case ISD::SRL: {
984 unsigned Imm, SH, MB, ME;
985 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000986 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000987 SDOperand Val;
988 Select(Val, N->getOperand(0).getOperand(0));
989 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
990 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
991 getI32Imm(ME));
992 return;
Nate Begeman8d948322005-10-19 01:12:32 +0000993 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000994
995 // Other cases are autogenerated.
996 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000997 }
Chris Lattner13794f52005-08-26 18:46:49 +0000998 case ISD::SELECT_CC: {
999 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1000
1001 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1002 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1003 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1004 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1005 if (N1C->isNullValue() && N3C->isNullValue() &&
1006 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001007 SDOperand LHS;
1008 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001009 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001010 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1011 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001012 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1013 SDOperand(Tmp, 0), LHS,
1014 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001015 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001016 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001017
Chris Lattner50ff55c2005-09-01 19:20:44 +00001018 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001019 unsigned BROpc = getBCCForSetCC(CC);
1020
1021 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001022 unsigned SelectCCOp;
1023 if (MVT::isInteger(N->getValueType(0)))
1024 SelectCCOp = PPC::SELECT_CC_Int;
1025 else if (N->getValueType(0) == MVT::f32)
1026 SelectCCOp = PPC::SELECT_CC_F4;
1027 else
1028 SelectCCOp = PPC::SELECT_CC_F8;
Evan Cheng34167212006-02-09 00:37:58 +00001029 SDOperand N2, N3;
1030 Select(N2, N->getOperand(2));
1031 Select(N3, N->getOperand(3));
1032 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1033 N2, N3, getI32Imm(BROpc));
1034 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001035 }
Chris Lattner2fbb4572005-08-21 18:50:37 +00001036 case ISD::BR_CC:
1037 case ISD::BRTWOWAY_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001038 SDOperand Chain;
1039 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001040 MachineBasicBlock *Dest =
1041 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
1042 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1043 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001044
1045 // If this is a two way branch, then grab the fallthrough basic block
1046 // argument and build a PowerPC branch pseudo-op, suitable for long branch
1047 // conversion if necessary by the branch selection pass. Otherwise, emit a
1048 // standard conditional branch.
1049 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
Chris Lattnerca0a4772005-10-01 23:06:26 +00001050 SDOperand CondTrueBlock = N->getOperand(4);
1051 SDOperand CondFalseBlock = N->getOperand(5);
Chris Lattnerca0a4772005-10-01 23:06:26 +00001052 unsigned Opc = getBCCForSetCC(CC);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001053 SDOperand CB =
1054 SDOperand(CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
1055 CondCode, getI32Imm(Opc),
1056 CondTrueBlock, CondFalseBlock,
1057 Chain), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001058 Result = CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, CondFalseBlock, CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001059 } else {
1060 // Iterate to the next basic block
1061 ilist<MachineBasicBlock>::iterator It = BB;
1062 ++It;
1063
1064 // If the fallthrough path is off the end of the function, which would be
1065 // undefined behavior, set it to be the same as the current block because
1066 // we have nothing better to set it to, and leaving it alone will cause
1067 // the PowerPC Branch Selection pass to crash.
1068 if (It == BB->getParent()->end()) It = Dest;
Evan Cheng34167212006-02-09 00:37:58 +00001069 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
1070 getI32Imm(getBCCForSetCC(CC)),
1071 N->getOperand(4), CurDAG->getBasicBlock(It),
1072 Chain);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001073 }
Evan Cheng34167212006-02-09 00:37:58 +00001074 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001075 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001076 }
Chris Lattner25dae722005-09-03 00:53:47 +00001077
Evan Cheng34167212006-02-09 00:37:58 +00001078 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001079}
1080
1081
Nate Begeman1d9d7422005-10-18 00:28:58 +00001082/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001083/// PowerPC-specific DAG, ready for instruction scheduling.
1084///
Nate Begeman1d9d7422005-10-18 00:28:58 +00001085FunctionPass *llvm::createPPCISelDag(TargetMachine &TM) {
1086 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001087}
1088