blob: 92512cc7a6a8236485fd7ee3e0b6e660986fe57b [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 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<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
33
34 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000035 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000036 /// instructions for SelectionDAG operations.
37 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000038 class PPCDAGToDAGISel : public SelectionDAGISel {
Nate Begeman21e463b2005-10-16 05:39:50 +000039 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000040 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000041 public:
Nate Begeman1d9d7422005-10-18 00:28:58 +000042 PPCDAGToDAGISel(TargetMachine &TM)
Nate Begeman21e463b2005-10-16 05:39:50 +000043 : SelectionDAGISel(PPCLowering), PPCLowering(TM) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000044
Chris Lattner4416f1a2005-08-19 22:38:53 +000045 virtual bool runOnFunction(Function &Fn) {
46 // Make sure we re-emit a set of the global base reg if necessary
47 GlobalBaseReg = 0;
48 return SelectionDAGISel::runOnFunction(Fn);
49 }
50
Chris Lattnera5a91b12005-08-17 19:33:03 +000051 /// getI32Imm - Return a target constant with the specified value, of type
52 /// i32.
53 inline SDOperand getI32Imm(unsigned Imm) {
54 return CurDAG->getTargetConstant(Imm, MVT::i32);
55 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000056
57 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
58 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000059 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000060
61 // Select - Convert the specified operand from a target-independent to a
62 // target-specific node if it hasn't already been changed.
63 SDOperand Select(SDOperand Op);
64
Nate Begeman02b88a42005-08-19 00:38:14 +000065 SDNode *SelectBitfieldInsert(SDNode *N);
66
Chris Lattner2fbb4572005-08-21 18:50:37 +000067 /// SelectCC - Select a comparison of the specified values with the
68 /// specified condition code, returning the CR# of the expression.
69 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
70
Nate Begeman7fd1edd2005-12-19 23:25:09 +000071 /// SelectAddrImm - Returns true if the address N can be represented by
72 /// a base register plus a signed 16-bit displacement [r+imm].
73 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
74
75 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
76 /// represented as an indexed [r+r] operation. Returns false if it can
77 /// be represented by [r+imm], which are preferred.
78 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000079
Nate Begeman7fd1edd2005-12-19 23:25:09 +000080 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
81 /// represented as an indexed [r+r] operation.
82 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000083
Chris Lattner047b9522005-08-25 22:04:30 +000084 SDOperand BuildSDIVSequence(SDNode *N);
85 SDOperand BuildUDIVSequence(SDNode *N);
86
Chris Lattnera5a91b12005-08-17 19:33:03 +000087 /// InstructionSelectBasicBlock - This callback is invoked by
88 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +000089 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
90
Chris Lattnera5a91b12005-08-17 19:33:03 +000091 virtual const char *getPassName() const {
92 return "PowerPC DAG->DAG Pattern Instruction Selection";
93 }
Chris Lattneraf165382005-09-13 22:03:06 +000094
95// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +000096#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +000097
98private:
Chris Lattner222adac2005-10-06 19:03:35 +000099 SDOperand SelectADD_PARTS(SDOperand Op);
100 SDOperand SelectSUB_PARTS(SDOperand Op);
101 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000102 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000103 };
104}
105
Chris Lattnerbd937b92005-10-06 18:45:51 +0000106/// InstructionSelectBasicBlock - This callback is invoked by
107/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000108void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000109 DEBUG(BB->dump());
110
111 // The selection process is inherently a bottom-up recursive process (users
112 // select their uses before themselves). Given infinite stack space, we
113 // could just start selecting on the root and traverse the whole graph. In
114 // practice however, this causes us to run out of stack space on large basic
115 // blocks. To avoid this problem, select the entry node, then all its uses,
116 // iteratively instead of recursively.
117 std::vector<SDOperand> Worklist;
118 Worklist.push_back(DAG.getEntryNode());
119
120 // Note that we can do this in the PPC target (scanning forward across token
121 // chain edges) because no nodes ever get folded across these edges. On a
122 // target like X86 which supports load/modify/store operations, this would
123 // have to be more careful.
124 while (!Worklist.empty()) {
125 SDOperand Node = Worklist.back();
126 Worklist.pop_back();
127
Chris Lattnercf01a702005-10-07 22:10:27 +0000128 // Chose from the least deep of the top two nodes.
129 if (!Worklist.empty() &&
130 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
131 std::swap(Worklist.back(), Node);
132
Chris Lattnerbd937b92005-10-06 18:45:51 +0000133 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
134 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
135 CodeGenMap.count(Node)) continue;
136
137 for (SDNode::use_iterator UI = Node.Val->use_begin(),
138 E = Node.Val->use_end(); UI != E; ++UI) {
139 // Scan the values. If this use has a value that is a token chain, add it
140 // to the worklist.
141 SDNode *User = *UI;
142 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
143 if (User->getValueType(i) == MVT::Other) {
144 Worklist.push_back(SDOperand(User, i));
145 break;
146 }
147 }
148
149 // Finally, legalize this node.
150 Select(Node);
151 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000152
Chris Lattnerbd937b92005-10-06 18:45:51 +0000153 // Select target instructions for the DAG.
154 DAG.setRoot(Select(DAG.getRoot()));
155 CodeGenMap.clear();
156 DAG.RemoveDeadNodes();
157
158 // Emit machine code to BB.
159 ScheduleAndEmitDAG(DAG);
160}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000161
Chris Lattner4416f1a2005-08-19 22:38:53 +0000162/// getGlobalBaseReg - Output the instructions required to put the
163/// base address to use for accessing globals into a register.
164///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000165SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000166 if (!GlobalBaseReg) {
167 // Insert the set of GlobalBaseReg into the first MBB of the function
168 MachineBasicBlock &FirstMBB = BB->getParent()->front();
169 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
170 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000171 // FIXME: when we get to LP64, we will need to create the appropriate
172 // type of register here.
173 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000174 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
175 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
176 }
Chris Lattner9944b762005-08-21 22:31:09 +0000177 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000178}
179
180
Nate Begeman0f3257a2005-08-18 05:00:13 +0000181// isIntImmediate - This method tests to see if a constant operand.
182// If so Imm will receive the 32 bit value.
183static bool isIntImmediate(SDNode *N, unsigned& Imm) {
184 if (N->getOpcode() == ISD::Constant) {
185 Imm = cast<ConstantSDNode>(N)->getValue();
186 return true;
187 }
188 return false;
189}
190
Nate Begemancffc32b2005-08-18 07:30:46 +0000191// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
192// any number of 0s on either side. The 1s are allowed to wrap from LSB to
193// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
194// not, since all 1s are not contiguous.
195static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
196 if (isShiftedMask_32(Val)) {
197 // look for the first non-zero bit
198 MB = CountLeadingZeros_32(Val);
199 // look for the first zero bit after the run of ones
200 ME = CountLeadingZeros_32((Val - 1) ^ Val);
201 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000202 } else {
203 Val = ~Val; // invert mask
204 if (isShiftedMask_32(Val)) {
205 // effectively look for the first zero bit
206 ME = CountLeadingZeros_32(Val) - 1;
207 // effectively look for the first one bit after the run of zeros
208 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
209 return true;
210 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000211 }
212 // no run present
213 return false;
214}
215
Chris Lattner65a419a2005-10-09 05:36:17 +0000216// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000217// and mask opcode and mask operation.
218static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
219 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000220 // Don't even go down this path for i64, since different logic will be
221 // necessary for rldicl/rldicr/rldimi.
222 if (N->getValueType(0) != MVT::i32)
223 return false;
224
Nate Begemancffc32b2005-08-18 07:30:46 +0000225 unsigned Shift = 32;
226 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
227 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000228 if (N->getNumOperands() != 2 ||
229 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000230 return false;
231
232 if (Opcode == ISD::SHL) {
233 // apply shift left to mask if it comes first
234 if (IsShiftMask) Mask = Mask << Shift;
235 // determine which bits are made indeterminant by shift
236 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000237 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000238 // apply shift right to mask if it comes first
239 if (IsShiftMask) Mask = Mask >> Shift;
240 // determine which bits are made indeterminant by shift
241 Indeterminant = ~(0xFFFFFFFFu >> Shift);
242 // adjust for the left rotate
243 Shift = 32 - Shift;
244 } else {
245 return false;
246 }
247
248 // if the mask doesn't intersect any Indeterminant bits
249 if (Mask && !(Mask & Indeterminant)) {
250 SH = Shift;
251 // make sure the mask is still a mask (wrap arounds may not be)
252 return isRunOfOnes(Mask, MB, ME);
253 }
254 return false;
255}
256
Nate Begeman0f3257a2005-08-18 05:00:13 +0000257// isOpcWithIntImmediate - This method tests to see if the node is a specific
258// opcode and that it has a immediate integer right operand.
259// If so Imm will receive the 32 bit value.
260static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
261 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
262}
263
Chris Lattnera5a91b12005-08-17 19:33:03 +0000264// isIntImmediate - This method tests to see if a constant operand.
265// If so Imm will receive the 32 bit value.
266static bool isIntImmediate(SDOperand N, unsigned& Imm) {
267 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
268 Imm = (unsigned)CN->getSignExtended();
269 return true;
270 }
271 return false;
272}
273
Nate Begeman02b88a42005-08-19 00:38:14 +0000274/// SelectBitfieldInsert - turn an or of two masked values into
275/// the rotate left word immediate then mask insert (rlwimi) instruction.
276/// Returns true on success, false if the caller still needs to select OR.
277///
278/// Patterns matched:
279/// 1. or shl, and 5. or and, and
280/// 2. or and, shl 6. or shl, shr
281/// 3. or shr, and 7. or shr, shl
282/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000283SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000284 bool IsRotate = false;
285 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
286 unsigned Value;
287
288 SDOperand Op0 = N->getOperand(0);
289 SDOperand Op1 = N->getOperand(1);
290
291 unsigned Op0Opc = Op0.getOpcode();
292 unsigned Op1Opc = Op1.getOpcode();
293
294 // Verify that we have the correct opcodes
295 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
296 return false;
297 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
298 return false;
299
300 // Generate Mask value for Target
301 if (isIntImmediate(Op0.getOperand(1), Value)) {
302 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000303 case ISD::SHL: TgtMask <<= Value; break;
304 case ISD::SRL: TgtMask >>= Value; break;
305 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000306 }
307 } else {
308 return 0;
309 }
310
311 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000312 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000313 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000314
315 switch(Op1Opc) {
316 case ISD::SHL:
317 SH = Value;
318 InsMask <<= SH;
319 if (Op0Opc == ISD::SRL) IsRotate = true;
320 break;
321 case ISD::SRL:
322 SH = Value;
323 InsMask >>= SH;
324 SH = 32-SH;
325 if (Op0Opc == ISD::SHL) IsRotate = true;
326 break;
327 case ISD::AND:
328 InsMask &= Value;
329 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000330 }
331
332 // If both of the inputs are ANDs and one of them has a logical shift by
333 // constant as its input, make that AND the inserted value so that we can
334 // combine the shift into the rotate part of the rlwimi instruction
335 bool IsAndWithShiftOp = false;
336 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
337 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
338 Op1.getOperand(0).getOpcode() == ISD::SRL) {
339 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
340 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
341 IsAndWithShiftOp = true;
342 }
343 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
344 Op0.getOperand(0).getOpcode() == ISD::SRL) {
345 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
346 std::swap(Op0, Op1);
347 std::swap(TgtMask, InsMask);
348 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
349 IsAndWithShiftOp = true;
350 }
351 }
352 }
353
354 // Verify that the Target mask and Insert mask together form a full word mask
355 // and that the Insert mask is a run of set bits (which implies both are runs
356 // of set bits). Given that, Select the arguments and generate the rlwimi
357 // instruction.
358 unsigned MB, ME;
359 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
360 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
361 bool Op0IsAND = Op0Opc == ISD::AND;
362 // Check for rotlwi / rotrwi here, a special case of bitfield insert
363 // where both bitfield halves are sourced from the same value.
364 if (IsRotate && fullMask &&
365 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
366 Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32,
367 Select(N->getOperand(0).getOperand(0)),
368 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
369 return Op0.Val;
370 }
371 SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0))
372 : Select(Op0);
373 SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0))
374 : Select(Op1.getOperand(0));
375 Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
376 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
377 return Op0.Val;
378 }
379 return 0;
380}
381
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000382/// SelectAddrImm - Returns true if the address N can be represented by
383/// a base register plus a signed 16-bit displacement [r+imm].
384bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
385 SDOperand &Base) {
386 if (N.getOpcode() == ISD::ADD) {
387 unsigned imm = 0;
388 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000389 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000390 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
391 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000392 } else {
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000393 Base = Select(N.getOperand(0));
Chris Lattner9944b762005-08-21 22:31:09 +0000394 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000395 return true; // [r+i]
396 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000397 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000398 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000399 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000400 Disp = N.getOperand(1).getOperand(0); // The global address.
401 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
402 Disp.getOpcode() == ISD::TargetConstantPool);
403 Base = Select(N.getOperand(0));
404 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000405 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000406 return false; // [r+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000407 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000408 Disp = getI32Imm(0);
409 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
410 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000411 else
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000412 Base = Select(N);
413 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000414}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000415
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000416/// SelectAddrIdx - Given the specified addressed, check to see if it can be
417/// represented as an indexed [r+r] operation. Returns false if it can
418/// be represented by [r+imm], which are preferred.
419bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
420 SDOperand &Index) {
421 // Check to see if we can represent this as an [r+imm] address instead,
422 // which will fail if the address is more profitably represented as an
423 // [r+r] address.
424 if (SelectAddrImm(N, Base, Index))
425 return false;
426
427 if (N.getOpcode() == ISD::ADD) {
428 Base = Select(N.getOperand(0));
429 Index = Select(N.getOperand(1));
430 return true;
431 }
432
Nate Begeman88276b82005-12-19 23:40:42 +0000433 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000434 Index = Select(N);
435 return true;
436}
437
438/// SelectAddrIdxOnly - Given the specified addressed, force it to be
439/// represented as an indexed [r+r] operation.
440bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
441 SDOperand &Index) {
442 if (N.getOpcode() == ISD::ADD) {
443 Base = Select(N.getOperand(0));
444 Index = Select(N.getOperand(1));
445 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000446 }
447
Nate Begeman88276b82005-12-19 23:40:42 +0000448 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000449 Index = Select(N);
450 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000451}
452
Chris Lattner2fbb4572005-08-21 18:50:37 +0000453/// SelectCC - Select a comparison of the specified values with the specified
454/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000455SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
456 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000457 // Always select the LHS.
458 LHS = Select(LHS);
459
460 // Use U to determine whether the SETCC immediate range is signed or not.
461 if (MVT::isInteger(LHS.getValueType())) {
462 bool U = ISD::isUnsignedIntSetCC(CC);
463 unsigned Imm;
464 if (isIntImmediate(RHS, Imm) &&
465 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
466 return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32,
Chris Lattner17e82d22006-01-12 01:54:15 +0000467 LHS, getI32Imm(Imm & 0xFFFF));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000468 return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
469 LHS, Select(RHS));
Chris Lattner919c0322005-10-01 01:35:02 +0000470 } else if (LHS.getValueType() == MVT::f32) {
471 return CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000472 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000473 return CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, Select(RHS));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000474 }
475}
476
477/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
478/// to Condition.
479static unsigned getBCCForSetCC(ISD::CondCode CC) {
480 switch (CC) {
481 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000482 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000483 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000484 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000485 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000486 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000487 case ISD::SETULT:
488 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000489 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000490 case ISD::SETULE:
491 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000492 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000493 case ISD::SETUGT:
494 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000495 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000496 case ISD::SETUGE:
497 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000498
499 case ISD::SETO: return PPC::BUN;
500 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000501 }
502 return 0;
503}
504
Chris Lattner64906a02005-08-25 20:08:18 +0000505/// getCRIdxForSetCC - Return the index of the condition register field
506/// associated with the SetCC condition, and whether or not the field is
507/// treated as inverted. That is, lt = 0; ge = 0 inverted.
508static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
509 switch (CC) {
510 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000511 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000512 case ISD::SETULT:
513 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000514 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000515 case ISD::SETUGE:
516 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000517 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000518 case ISD::SETUGT:
519 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000520 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000521 case ISD::SETULE:
522 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000523 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000524 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000525 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000526 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000527 case ISD::SETO: Inv = true; return 3;
528 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000529 }
530 return 0;
531}
Chris Lattner9944b762005-08-21 22:31:09 +0000532
Chris Lattnerbd937b92005-10-06 18:45:51 +0000533
Nate Begeman1d9d7422005-10-18 00:28:58 +0000534SDOperand PPCDAGToDAGISel::SelectADD_PARTS(SDOperand Op) {
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000535 SDNode *N = Op.Val;
536 SDOperand LHSL = Select(N->getOperand(0));
537 SDOperand LHSH = Select(N->getOperand(1));
538
539 unsigned Imm;
540 bool ME = false, ZE = false;
541 if (isIntImmediate(N->getOperand(3), Imm)) {
542 ME = (signed)Imm == -1;
543 ZE = Imm == 0;
544 }
545
546 std::vector<SDOperand> Result;
547 SDOperand CarryFromLo;
548 if (isIntImmediate(N->getOperand(2), Imm) &&
549 ((signed)Imm >= -32768 || (signed)Imm < 32768)) {
550 // Codegen the low 32 bits of the add. Interestingly, there is no
551 // shifted form of add immediate carrying.
552 CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
553 LHSL, getI32Imm(Imm));
554 } else {
555 CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag,
556 LHSL, Select(N->getOperand(2)));
557 }
558 CarryFromLo = CarryFromLo.getValue(1);
559
560 // Codegen the high 32 bits, adding zero, minus one, or the full value
561 // along with the carry flag produced by addc/addic.
562 SDOperand ResultHi;
563 if (ZE)
564 ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo);
565 else if (ME)
566 ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo);
567 else
568 ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH,
569 Select(N->getOperand(3)), CarryFromLo);
570 Result.push_back(CarryFromLo.getValue(0));
571 Result.push_back(ResultHi);
572
573 CodeGenMap[Op.getValue(0)] = Result[0];
574 CodeGenMap[Op.getValue(1)] = Result[1];
575 return Result[Op.ResNo];
576}
Nate Begeman1d9d7422005-10-18 00:28:58 +0000577SDOperand PPCDAGToDAGISel::SelectSUB_PARTS(SDOperand Op) {
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000578 SDNode *N = Op.Val;
579 SDOperand LHSL = Select(N->getOperand(0));
580 SDOperand LHSH = Select(N->getOperand(1));
581 SDOperand RHSL = Select(N->getOperand(2));
582 SDOperand RHSH = Select(N->getOperand(3));
583
584 std::vector<SDOperand> Result;
585 Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag,
586 RHSL, LHSL));
587 Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH,
588 Result[0].getValue(1)));
589 CodeGenMap[Op.getValue(0)] = Result[0];
590 CodeGenMap[Op.getValue(1)] = Result[1];
591 return Result[Op.ResNo];
592}
593
Nate Begeman1d9d7422005-10-18 00:28:58 +0000594SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000595 SDNode *N = Op.Val;
596 unsigned Imm;
597 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
598 if (isIntImmediate(N->getOperand(1), Imm)) {
599 // We can codegen setcc op, imm very efficiently compared to a brcond.
600 // Check for those cases here.
601 // setcc op, 0
602 if (Imm == 0) {
603 SDOperand Op = Select(N->getOperand(0));
604 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000605 default: break;
606 case ISD::SETEQ:
607 Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op);
Chris Lattner71d3d502005-11-30 22:53:06 +0000608 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
609 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000610 case ISD::SETNE: {
611 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
612 Op, getI32Imm(~0U));
Chris Lattner71d3d502005-11-30 22:53:06 +0000613 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
614 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000615 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000616 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000617 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
618 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000619 case ISD::SETGT: {
620 SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op);
621 T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);;
Chris Lattner71d3d502005-11-30 22:53:06 +0000622 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
623 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000624 }
625 }
Chris Lattner222adac2005-10-06 19:03:35 +0000626 } else if (Imm == ~0U) { // setcc op, -1
627 SDOperand Op = Select(N->getOperand(0));
628 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000629 default: break;
630 case ISD::SETEQ:
631 Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
632 Op, getI32Imm(1));
Chris Lattner71d3d502005-11-30 22:53:06 +0000633 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
634 CurDAG->getTargetNode(PPC::LI, MVT::i32,
635 getI32Imm(0)),
636 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000637 case ISD::SETNE: {
638 Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op);
639 SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
640 Op, getI32Imm(~0U));
Chris Lattner71d3d502005-11-30 22:53:06 +0000641 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
642 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000643 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000644 case ISD::SETLT: {
645 SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
646 getI32Imm(1));
647 SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op);
Chris Lattner71d3d502005-11-30 22:53:06 +0000648 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
649 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000650 }
651 case ISD::SETGT:
652 Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
653 getI32Imm(31), getI32Imm(31));
Chris Lattner71d3d502005-11-30 22:53:06 +0000654 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000655 }
Chris Lattner222adac2005-10-06 19:03:35 +0000656 }
657 }
658
659 bool Inv;
660 unsigned Idx = getCRIdxForSetCC(CC, Inv);
661 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
662 SDOperand IntCR;
663
664 // Force the ccreg into CR7.
665 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
666
Chris Lattner85961d52005-12-06 20:56:18 +0000667 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000668 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
669 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000670
671 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
672 IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg);
673 else
674 IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg);
675
676 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000677 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
678 getI32Imm((32-(3-Idx)) & 31),
679 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000680 } else {
681 SDOperand Tmp =
682 CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
Chris Lattner7d7b9672005-10-28 22:58:07 +0000683 getI32Imm((32-(3-Idx)) & 31),
684 getI32Imm(31),getI32Imm(31));
Chris Lattner71d3d502005-11-30 22:53:06 +0000685 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000686 }
Chris Lattner222adac2005-10-06 19:03:35 +0000687}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000688
Nate Begeman422b0ce2005-11-16 00:48:01 +0000689/// isCallCompatibleAddress - Return true if the specified 32-bit value is
690/// representable in the immediate field of a Bx instruction.
691static bool isCallCompatibleAddress(ConstantSDNode *C) {
692 int Addr = C->getValue();
693 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
694 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
695}
696
Nate Begeman1d9d7422005-10-18 00:28:58 +0000697SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000698 SDNode *N = Op.Val;
699 SDOperand Chain = Select(N->getOperand(0));
700
701 unsigned CallOpcode;
702 std::vector<SDOperand> CallOperands;
703
704 if (GlobalAddressSDNode *GASD =
705 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000706 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000707 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000708 } else if (ExternalSymbolSDNode *ESSDN =
709 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000710 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000711 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000712 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
713 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
714 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
715 CallOpcode = PPC::BLA;
716 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000717 } else {
718 // Copy the callee address into the CTR register.
719 SDOperand Callee = Select(N->getOperand(1));
720 Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain);
721
722 // Copy the callee address into R12 on darwin.
723 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
724 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000725
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000726 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000727 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000728 }
729
730 unsigned GPR_idx = 0, FPR_idx = 0;
731 static const unsigned GPR[] = {
732 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
733 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
734 };
735 static const unsigned FPR[] = {
736 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
737 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
738 };
739
740 SDOperand InFlag; // Null incoming flag value.
741
742 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
743 unsigned DestReg = 0;
744 MVT::ValueType RegTy = N->getOperand(i).getValueType();
745 if (RegTy == MVT::i32) {
746 assert(GPR_idx < 8 && "Too many int args");
747 DestReg = GPR[GPR_idx++];
748 } else {
749 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
750 "Unpromoted integer arg?");
751 assert(FPR_idx < 13 && "Too many fp args");
752 DestReg = FPR[FPR_idx++];
753 }
754
755 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
756 SDOperand Val = Select(N->getOperand(i));
757 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
758 InFlag = Chain.getValue(1);
759 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
760 }
761 }
762
763 // Finally, once everything is in registers to pass to the call, emit the
764 // call itself.
765 if (InFlag.Val)
766 CallOperands.push_back(InFlag); // Strong dep on register copies.
767 else
768 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
769 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
770 CallOperands);
771
772 std::vector<SDOperand> CallResults;
773
774 // If the call has results, copy the values out of the ret val registers.
775 switch (N->getValueType(0)) {
776 default: assert(0 && "Unexpected ret value!");
777 case MVT::Other: break;
778 case MVT::i32:
779 if (N->getValueType(1) == MVT::i32) {
780 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
781 Chain.getValue(1)).getValue(1);
782 CallResults.push_back(Chain.getValue(0));
783 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
784 Chain.getValue(2)).getValue(1);
785 CallResults.push_back(Chain.getValue(0));
786 } else {
787 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
788 Chain.getValue(1)).getValue(1);
789 CallResults.push_back(Chain.getValue(0));
790 }
791 break;
792 case MVT::f32:
793 case MVT::f64:
794 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
795 Chain.getValue(1)).getValue(1);
796 CallResults.push_back(Chain.getValue(0));
797 break;
798 }
799
800 CallResults.push_back(Chain);
801 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
802 CodeGenMap[Op.getValue(i)] = CallResults[i];
803 return CallResults[Op.ResNo];
804}
805
Chris Lattnera5a91b12005-08-17 19:33:03 +0000806// Select - Convert the specified operand from a target-independent to a
807// target-specific node if it hasn't already been changed.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000808SDOperand PPCDAGToDAGISel::Select(SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000809 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000810 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
811 N->getOpcode() < PPCISD::FIRST_NUMBER)
Chris Lattnera5a91b12005-08-17 19:33:03 +0000812 return Op; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000813
814 // If this has already been converted, use it.
815 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
816 if (CGMI != CodeGenMap.end()) return CGMI->second;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000817
818 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000819 default: break;
Chris Lattner222adac2005-10-06 19:03:35 +0000820 case ISD::ADD_PARTS: return SelectADD_PARTS(Op);
821 case ISD::SUB_PARTS: return SelectSUB_PARTS(Op);
822 case ISD::SETCC: return SelectSETCC(Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000823 case ISD::CALL: return SelectCALL(Op);
824 case ISD::TAILCALL: return SelectCALL(Op);
Chris Lattner860e8862005-11-17 07:30:41 +0000825 case PPCISD::GlobalBaseReg: return getGlobalBaseReg();
826
Chris Lattnere28e40a2005-08-25 00:45:43 +0000827 case ISD::FrameIndex: {
828 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner71d3d502005-11-30 22:53:06 +0000829 if (N->hasOneUse())
830 return CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
831 CurDAG->getTargetFrameIndex(FI, MVT::i32),
832 getI32Imm(0));
Chris Lattner05f56a52005-12-01 18:09:22 +0000833 return CodeGenMap[Op] =
834 CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
835 CurDAG->getTargetFrameIndex(FI, MVT::i32),
836 getI32Imm(0));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000837 }
Chris Lattner88add102005-09-28 22:50:24 +0000838 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000839 // FIXME: since this depends on the setting of the carry flag from the srawi
840 // we should really be making notes about that for the scheduler.
841 // FIXME: It sure would be nice if we could cheaply recognize the
842 // srl/add/sra pattern the dag combiner will generate for this as
843 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000844 unsigned Imm;
845 if (isIntImmediate(N->getOperand(1), Imm)) {
846 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
847 SDOperand Op =
848 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
849 Select(N->getOperand(0)),
850 getI32Imm(Log2_32(Imm)));
Chris Lattner71d3d502005-11-30 22:53:06 +0000851 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
852 Op.getValue(0), Op.getValue(1));
Chris Lattner8784a232005-08-25 17:50:06 +0000853 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
854 SDOperand Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000855 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Chris Lattner8784a232005-08-25 17:50:06 +0000856 Select(N->getOperand(0)),
857 getI32Imm(Log2_32(-Imm)));
858 SDOperand PT =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000859 CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0),
860 Op.getValue(1));
Chris Lattner71d3d502005-11-30 22:53:06 +0000861 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000862 }
863 }
Chris Lattner047b9522005-08-25 22:04:30 +0000864
Chris Lattner237733e2005-09-29 23:33:31 +0000865 // Other cases are autogenerated.
866 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000867 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000868 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000869 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000870 // If this is an and of a value rotated between 0 and 31 bits and then and'd
871 // with a mask, emit rlwinm
872 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
873 isShiftedMask_32(~Imm))) {
874 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000875 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000876 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
877 Val = Select(N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000878 } else if (Imm == 0) {
879 // AND X, 0 -> 0, not "rlwinm 32".
880 return Select(N->getOperand(1));
881 } else {
Nate Begemancffc32b2005-08-18 07:30:46 +0000882 Val = Select(N->getOperand(0));
883 isRunOfOnes(Imm, MB, ME);
884 SH = 0;
885 }
Chris Lattner71d3d502005-11-30 22:53:06 +0000886 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH),
887 getI32Imm(MB), getI32Imm(ME));
Nate Begemancffc32b2005-08-18 07:30:46 +0000888 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000889 // ISD::OR doesn't get all the bitfield insertion fun.
890 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
891 if (isIntImmediate(N->getOperand(1), Imm) &&
892 N->getOperand(0).getOpcode() == ISD::OR &&
893 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000894 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000895 Imm = ~(Imm^Imm2);
896 if (isRunOfOnes(Imm, MB, ME)) {
897 SDOperand Tmp1 = Select(N->getOperand(0).getOperand(0));
898 SDOperand Tmp2 = Select(N->getOperand(0).getOperand(1));
899 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000900 getI32Imm(0), getI32Imm(MB), getI32Imm(ME));
Nate Begeman50fb3c42005-12-24 01:00:15 +0000901 }
902 }
Chris Lattner237733e2005-09-29 23:33:31 +0000903
904 // Other cases are autogenerated.
905 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000906 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000907 case ISD::OR:
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000908 if (SDNode *I = SelectBitfieldInsert(N))
909 return CodeGenMap[Op] = SDOperand(I, 0);
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000910
Chris Lattner237733e2005-09-29 23:33:31 +0000911 // Other cases are autogenerated.
912 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000913 case ISD::SHL: {
914 unsigned Imm, SH, MB, ME;
915 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000916 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000917 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
918 Select(N->getOperand(0).getOperand(0)),
919 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman8d948322005-10-19 01:12:32 +0000920 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000921
922 // Other cases are autogenerated.
923 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000924 }
925 case ISD::SRL: {
926 unsigned Imm, SH, MB, ME;
927 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000928 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000929 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
930 Select(N->getOperand(0).getOperand(0)),
931 getI32Imm(SH & 0x1F), getI32Imm(MB),
932 getI32Imm(ME));
Nate Begeman8d948322005-10-19 01:12:32 +0000933 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000934
935 // Other cases are autogenerated.
936 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000937 }
Chris Lattner13794f52005-08-26 18:46:49 +0000938 case ISD::SELECT_CC: {
939 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
940
941 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
942 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
943 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
944 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
945 if (N1C->isNullValue() && N3C->isNullValue() &&
946 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
947 SDOperand LHS = Select(N->getOperand(0));
948 SDOperand Tmp =
949 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
950 LHS, getI32Imm(~0U));
Chris Lattner71d3d502005-11-30 22:53:06 +0000951 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS,
952 Tmp.getValue(1));
Chris Lattner13794f52005-08-26 18:46:49 +0000953 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000954
Chris Lattner50ff55c2005-09-01 19:20:44 +0000955 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000956 unsigned BROpc = getBCCForSetCC(CC);
957
958 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +0000959 unsigned SelectCCOp;
960 if (MVT::isInteger(N->getValueType(0)))
961 SelectCCOp = PPC::SELECT_CC_Int;
962 else if (N->getValueType(0) == MVT::f32)
963 SelectCCOp = PPC::SELECT_CC_F4;
964 else
965 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner71d3d502005-11-30 22:53:06 +0000966 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
967 Select(N->getOperand(2)),
968 Select(N->getOperand(3)),
969 getI32Imm(BROpc));
Chris Lattner13794f52005-08-26 18:46:49 +0000970 }
Chris Lattner2fbb4572005-08-21 18:50:37 +0000971 case ISD::BR_CC:
972 case ISD::BRTWOWAY_CC: {
973 SDOperand Chain = Select(N->getOperand(0));
974 MachineBasicBlock *Dest =
975 cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock();
976 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
977 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000978
979 // If this is a two way branch, then grab the fallthrough basic block
980 // argument and build a PowerPC branch pseudo-op, suitable for long branch
981 // conversion if necessary by the branch selection pass. Otherwise, emit a
982 // standard conditional branch.
983 if (N->getOpcode() == ISD::BRTWOWAY_CC) {
Chris Lattnerca0a4772005-10-01 23:06:26 +0000984 SDOperand CondTrueBlock = N->getOperand(4);
985 SDOperand CondFalseBlock = N->getOperand(5);
986
987 // If the false case is the current basic block, then this is a self loop.
988 // We do not want to emit "Loop: ... brcond Out; br Loop", as it adds an
989 // extra dispatch group to the loop. Instead, invert the condition and
990 // emit "Loop: ... br!cond Loop; br Out
991 if (cast<BasicBlockSDNode>(CondFalseBlock)->getBasicBlock() == BB) {
992 std::swap(CondTrueBlock, CondFalseBlock);
993 CC = getSetCCInverse(CC,
994 MVT::isInteger(N->getOperand(2).getValueType()));
995 }
996
997 unsigned Opc = getBCCForSetCC(CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000998 SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other,
999 CondCode, getI32Imm(Opc),
Chris Lattnerca0a4772005-10-01 23:06:26 +00001000 CondTrueBlock, CondFalseBlock,
Chris Lattner2fbb4572005-08-21 18:50:37 +00001001 Chain);
Chris Lattner71d3d502005-11-30 22:53:06 +00001002 return CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, CondFalseBlock, CB);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001003 } else {
1004 // Iterate to the next basic block
1005 ilist<MachineBasicBlock>::iterator It = BB;
1006 ++It;
1007
1008 // If the fallthrough path is off the end of the function, which would be
1009 // undefined behavior, set it to be the same as the current block because
1010 // we have nothing better to set it to, and leaving it alone will cause
1011 // the PowerPC Branch Selection pass to crash.
1012 if (It == BB->getParent()->end()) It = Dest;
Chris Lattner71d3d502005-11-30 22:53:06 +00001013 return CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode,
1014 getI32Imm(getBCCForSetCC(CC)),
1015 N->getOperand(4), CurDAG->getBasicBlock(It),
1016 Chain);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001017 }
Chris Lattner2fbb4572005-08-21 18:50:37 +00001018 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001019 }
Chris Lattner25dae722005-09-03 00:53:47 +00001020
Chris Lattner19c09072005-09-07 23:45:15 +00001021 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001022}
1023
1024
Nate Begeman1d9d7422005-10-18 00:28:58 +00001025/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001026/// PowerPC-specific DAG, ready for instruction scheduling.
1027///
Nate Begeman1d9d7422005-10-18 00:28:58 +00001028FunctionPass *llvm::createPPCISelDag(TargetMachine &TM) {
1029 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001030}
1031