blob: 85ca15c67de00c3f9a8cea10c3837bf9b1aa659e [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 Lattner420736d2006-03-25 06:47:10 +000028#include "llvm/Intrinsics.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000033#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000034#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000035using namespace llvm;
36
37namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000038 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
39
40 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000042 /// instructions for SelectionDAG operations.
43 ///
Chris Lattner2a41a982006-06-28 22:00:36 +000044 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000045 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000046 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000047 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000048 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000049 PPCDAGToDAGISel(PPCTargetMachine &tm)
50 : SelectionDAGISel(PPCLowering), TM(tm),
51 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000052
Chris Lattner4416f1a2005-08-19 22:38:53 +000053 virtual bool runOnFunction(Function &Fn) {
54 // Make sure we re-emit a set of the global base reg if necessary
55 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000056 SelectionDAGISel::runOnFunction(Fn);
57
58 InsertVRSaveCode(Fn);
59 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000060 }
61
Chris Lattnera5a91b12005-08-17 19:33:03 +000062 /// getI32Imm - Return a target constant with the specified value, of type
63 /// i32.
64 inline SDOperand getI32Imm(unsigned Imm) {
65 return CurDAG->getTargetConstant(Imm, MVT::i32);
66 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000067
Chris Lattnerc08f9022006-06-27 00:04:13 +000068 /// getI64Imm - Return a target constant with the specified value, of type
69 /// i64.
70 inline SDOperand getI64Imm(uint64_t Imm) {
71 return CurDAG->getTargetConstant(Imm, MVT::i64);
72 }
73
74 /// getSmallIPtrImm - Return a target constant of pointer type.
75 inline SDOperand getSmallIPtrImm(unsigned Imm) {
76 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
77 }
78
Nate Begemanf42f1332006-09-22 05:01:56 +000079 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
80 /// with any number of 0s on either side. The 1s are allowed to wrap from
81 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
82 /// 0x0F0F0000 is not, since all 1s are not contiguous.
83 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
84
85
86 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
87 /// rotate and mask opcode and mask operation.
88 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
89 unsigned &SH, unsigned &MB, unsigned &ME);
Chris Lattnerc08f9022006-06-27 00:04:13 +000090
Chris Lattner4416f1a2005-08-19 22:38:53 +000091 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
92 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +000093 SDNode *getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000094
95 // Select - Convert the specified operand from a target-independent to a
96 // target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +000097 SDNode *Select(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000098
Nate Begeman02b88a42005-08-19 00:38:14 +000099 SDNode *SelectBitfieldInsert(SDNode *N);
100
Chris Lattner2fbb4572005-08-21 18:50:37 +0000101 /// SelectCC - Select a comparison of the specified values with the
102 /// specified condition code, returning the CR# of the expression.
103 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
104
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000105 /// SelectAddrImm - Returns true if the address N can be represented by
106 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000107 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base) {
108 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
109 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000110
111 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
112 /// represented as an indexed [r+r] operation. Returns false if it can
113 /// be represented by [r+imm], which are preferred.
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000114 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index) {
115 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
116 }
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000117
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000118 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
119 /// represented as an indexed [r+r] operation.
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000120 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index) {
121 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
122 }
Chris Lattner9944b762005-08-21 22:31:09 +0000123
Chris Lattnere5ba5802006-03-22 05:26:03 +0000124 /// SelectAddrImmShift - Returns true if the address N can be represented by
125 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
126 /// for use by STD and friends.
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000127 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base) {
128 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
129 }
130
Chris Lattnere5d88612006-02-24 02:13:12 +0000131 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
132 /// inline asm expressions.
133 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
134 char ConstraintCode,
135 std::vector<SDOperand> &OutOps,
136 SelectionDAG &DAG) {
137 SDOperand Op0, Op1;
138 switch (ConstraintCode) {
139 default: return true;
140 case 'm': // memory
141 if (!SelectAddrIdx(Op, Op0, Op1))
142 SelectAddrImm(Op, Op0, Op1);
143 break;
144 case 'o': // offsetable
145 if (!SelectAddrImm(Op, Op0, Op1)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000146 Op0 = Op;
147 AddToISelQueue(Op0); // r+0.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000148 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000149 }
150 break;
151 case 'v': // not offsetable
152 SelectAddrIdxOnly(Op, Op0, Op1);
153 break;
154 }
155
156 OutOps.push_back(Op0);
157 OutOps.push_back(Op1);
158 return false;
159 }
160
Chris Lattner047b9522005-08-25 22:04:30 +0000161 SDOperand BuildSDIVSequence(SDNode *N);
162 SDOperand BuildUDIVSequence(SDNode *N);
163
Chris Lattnera5a91b12005-08-17 19:33:03 +0000164 /// InstructionSelectBasicBlock - This callback is invoked by
165 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000166 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
167
Chris Lattner4bb18952006-03-16 18:25:23 +0000168 void InsertVRSaveCode(Function &Fn);
169
Chris Lattnera5a91b12005-08-17 19:33:03 +0000170 virtual const char *getPassName() const {
171 return "PowerPC DAG->DAG Pattern Instruction Selection";
172 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000173
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000174 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
175 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000176 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000177 // Should use subtarget info to pick the right hazard recognizer. For
178 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000179 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
180 assert(II && "No InstrInfo?");
181 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000182 }
Chris Lattneraf165382005-09-13 22:03:06 +0000183
184// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000185#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000186
187private:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000188 SDNode *SelectSETCC(SDOperand Op);
189 SDNode *MySelect_PPCbctrl(SDOperand N);
190 SDNode *MySelect_PPCcall(SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000191 };
192}
193
Chris Lattnerbd937b92005-10-06 18:45:51 +0000194/// InstructionSelectBasicBlock - This callback is invoked by
195/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000196void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000197 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000198
Chris Lattnerbd937b92005-10-06 18:45:51 +0000199 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000200 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000201 DAG.RemoveDeadNodes();
202
Chris Lattner1877ec92006-03-13 21:52:10 +0000203 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000204 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000205}
206
207/// InsertVRSaveCode - Once the entire function has been instruction selected,
208/// all virtual registers are created and all machine instructions are built,
209/// check to see if we need to save/restore VRSAVE. If so, do it.
210void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000211 // Check to see if this function uses vector registers, which means we have to
212 // save and restore the VRSAVE register and update it with the regs we use.
213 //
214 // In this case, there will be virtual registers of vector type type created
215 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000216 MachineFunction &Fn = MachineFunction::get(&F);
217 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000218 bool HasVectorVReg = false;
219 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000220 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000221 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
222 HasVectorVReg = true;
223 break;
224 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000225 if (!HasVectorVReg) return; // nothing to do.
226
Chris Lattner1877ec92006-03-13 21:52:10 +0000227 // If we have a vector register, we want to emit code into the entry and exit
228 // blocks to save and restore the VRSAVE register. We do this here (instead
229 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
230 //
231 // 1. This (trivially) reduces the load on the register allocator, by not
232 // having to represent the live range of the VRSAVE register.
233 // 2. This (more significantly) allows us to create a temporary virtual
234 // register to hold the saved VRSAVE value, allowing this temporary to be
235 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000236
237 // Create two vregs - one to hold the VRSAVE register that is live-in to the
238 // function and one for the value after having bits or'd into it.
239 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
240 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
241
242 MachineBasicBlock &EntryBB = *Fn.begin();
243 // Emit the following code into the entry block:
244 // InVRSAVE = MFVRSAVE
245 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
246 // MTVRSAVE UpdatedVRSAVE
247 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
248 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
249 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
250 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
251
252 // Find all return blocks, outputting a restore in each epilog.
253 const TargetInstrInfo &TII = *TM.getInstrInfo();
254 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
255 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
256 IP = BB->end(); --IP;
257
258 // Skip over all terminator instructions, which are part of the return
259 // sequence.
260 MachineBasicBlock::iterator I2 = IP;
261 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
262 IP = I2;
263
264 // Emit: MTVRSAVE InVRSave
265 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
266 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000267 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000268}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000269
Chris Lattner4bb18952006-03-16 18:25:23 +0000270
Chris Lattner4416f1a2005-08-19 22:38:53 +0000271/// getGlobalBaseReg - Output the instructions required to put the
272/// base address to use for accessing globals into a register.
273///
Evan Cheng9ade2182006-08-26 05:34:46 +0000274SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000275 if (!GlobalBaseReg) {
276 // Insert the set of GlobalBaseReg into the first MBB of the function
277 MachineBasicBlock &FirstMBB = BB->getParent()->front();
278 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
279 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000280
281 if (PPCLowering.getPointerTy() == MVT::i32)
282 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
283 else
284 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
285
Chris Lattner4416f1a2005-08-19 22:38:53 +0000286 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
287 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
288 }
Evan Cheng9ade2182006-08-26 05:34:46 +0000289 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy()).Val;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000290}
291
292/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
293/// or 64-bit immediate, and if the value can be accurately represented as a
294/// sign extension from a 16-bit value. If so, this returns true and the
295/// immediate.
296static bool isIntS16Immediate(SDNode *N, short &Imm) {
297 if (N->getOpcode() != ISD::Constant)
298 return false;
299
300 Imm = (short)cast<ConstantSDNode>(N)->getValue();
301 if (N->getValueType(0) == MVT::i32)
302 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
303 else
304 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
305}
306
307static bool isIntS16Immediate(SDOperand Op, short &Imm) {
308 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000309}
310
311
Chris Lattnerc08f9022006-06-27 00:04:13 +0000312/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
313/// operand. If so Imm will receive the 32-bit value.
314static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
315 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000316 Imm = cast<ConstantSDNode>(N)->getValue();
317 return true;
318 }
319 return false;
320}
321
Chris Lattnerc08f9022006-06-27 00:04:13 +0000322/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
323/// operand. If so Imm will receive the 64-bit value.
324static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Chris Lattner71176242006-09-20 04:33:27 +0000325 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000326 Imm = cast<ConstantSDNode>(N)->getValue();
327 return true;
328 }
329 return false;
330}
331
332// isInt32Immediate - This method tests to see if a constant operand.
333// If so Imm will receive the 32 bit value.
334static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
335 return isInt32Immediate(N.Val, Imm);
336}
337
338
339// isOpcWithIntImmediate - This method tests to see if the node is a specific
340// opcode and that it has a immediate integer right operand.
341// If so Imm will receive the 32 bit value.
342static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
343 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
344}
345
Nate Begemanf42f1332006-09-22 05:01:56 +0000346bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000347 if (isShiftedMask_32(Val)) {
348 // look for the first non-zero bit
349 MB = CountLeadingZeros_32(Val);
350 // look for the first zero bit after the run of ones
351 ME = CountLeadingZeros_32((Val - 1) ^ Val);
352 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000353 } else {
354 Val = ~Val; // invert mask
355 if (isShiftedMask_32(Val)) {
356 // effectively look for the first zero bit
357 ME = CountLeadingZeros_32(Val) - 1;
358 // effectively look for the first one bit after the run of zeros
359 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
360 return true;
361 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000362 }
363 // no run present
364 return false;
365}
366
Nate Begemanf42f1332006-09-22 05:01:56 +0000367bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
368 bool IsShiftMask, unsigned &SH,
369 unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000370 // Don't even go down this path for i64, since different logic will be
371 // necessary for rldicl/rldicr/rldimi.
372 if (N->getValueType(0) != MVT::i32)
373 return false;
374
Nate Begemancffc32b2005-08-18 07:30:46 +0000375 unsigned Shift = 32;
376 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
377 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000378 if (N->getNumOperands() != 2 ||
Chris Lattnerc08f9022006-06-27 00:04:13 +0000379 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000380 return false;
381
382 if (Opcode == ISD::SHL) {
383 // apply shift left to mask if it comes first
384 if (IsShiftMask) Mask = Mask << Shift;
385 // determine which bits are made indeterminant by shift
386 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000387 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000388 // apply shift right to mask if it comes first
389 if (IsShiftMask) Mask = Mask >> Shift;
390 // determine which bits are made indeterminant by shift
391 Indeterminant = ~(0xFFFFFFFFu >> Shift);
392 // adjust for the left rotate
393 Shift = 32 - Shift;
Nate Begemanf42f1332006-09-22 05:01:56 +0000394 } else if (Opcode == ISD::ROTL) {
395 Indeterminant = 0;
Nate Begemancffc32b2005-08-18 07:30:46 +0000396 } else {
397 return false;
398 }
399
400 // if the mask doesn't intersect any Indeterminant bits
401 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000402 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000403 // make sure the mask is still a mask (wrap arounds may not be)
404 return isRunOfOnes(Mask, MB, ME);
405 }
406 return false;
407}
408
Nate Begeman02b88a42005-08-19 00:38:14 +0000409/// SelectBitfieldInsert - turn an or of two masked values into
410/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000411SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000412 SDOperand Op0 = N->getOperand(0);
413 SDOperand Op1 = N->getOperand(1);
414
Nate Begeman77f361f2006-05-07 00:23:38 +0000415 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000416 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
417 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000418
Nate Begeman4667f2c2006-05-08 17:38:32 +0000419 unsigned TargetMask = LKZ;
420 unsigned InsertMask = RKZ;
421
422 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
423 unsigned Op0Opc = Op0.getOpcode();
424 unsigned Op1Opc = Op1.getOpcode();
425 unsigned Value, SH = 0;
426 TargetMask = ~TargetMask;
427 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000428
Nate Begeman4667f2c2006-05-08 17:38:32 +0000429 // If the LHS has a foldable shift and the RHS does not, then swap it to the
430 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000431 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
432 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
433 Op0.getOperand(0).getOpcode() == ISD::SRL) {
434 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
435 Op1.getOperand(0).getOpcode() != ISD::SRL) {
436 std::swap(Op0, Op1);
437 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000438 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000439 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000440 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000441 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
442 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
443 Op1.getOperand(0).getOpcode() != ISD::SRL) {
444 std::swap(Op0, Op1);
445 std::swap(Op0Opc, Op1Opc);
446 std::swap(TargetMask, InsertMask);
447 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000448 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000449
450 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000451 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000452 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000453 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000454
455 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000456 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000457 Op1 = Op1.getOperand(0);
458 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
459 }
460 if (Op1Opc == ISD::AND) {
461 unsigned SHOpc = Op1.getOperand(0).getOpcode();
462 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000463 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000464 Op1 = Op1.getOperand(0).getOperand(0);
465 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
466 } else {
467 Op1 = Op1.getOperand(0);
468 }
469 }
470
471 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Evan Cheng6da2f322006-08-26 01:07:58 +0000472 AddToISelQueue(Tmp3);
473 AddToISelQueue(Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000474 SH &= 31;
Evan Cheng0b828e02006-08-27 08:14:06 +0000475 SDOperand Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
476 getI32Imm(ME) };
477 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman02b88a42005-08-19 00:38:14 +0000478 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000479 }
480 return 0;
481}
482
Chris Lattnere5ba5802006-03-22 05:26:03 +0000483
Chris Lattner2fbb4572005-08-21 18:50:37 +0000484/// SelectCC - Select a comparison of the specified values with the specified
485/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000486SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
487 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000488 // Always select the LHS.
Evan Cheng6da2f322006-08-26 01:07:58 +0000489 AddToISelQueue(LHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000490 unsigned Opc;
491
492 if (LHS.getValueType() == MVT::i32) {
Chris Lattner529c2332006-06-27 00:10:13 +0000493 unsigned Imm;
Chris Lattner3836dbd2006-09-20 04:25:47 +0000494 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
495 if (isInt32Immediate(RHS, Imm)) {
496 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
497 if (isUInt16(Imm))
498 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
499 getI32Imm(Imm & 0xFFFF)), 0);
500 // If this is a 16-bit signed immediate, fold it.
501 if (isInt16(Imm))
502 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
503 getI32Imm(Imm & 0xFFFF)), 0);
504
505 // For non-equality comparisons, the default code would materialize the
506 // constant, then compare against it, like this:
507 // lis r2, 4660
508 // ori r2, r2, 22136
509 // cmpw cr0, r3, r2
510 // Since we are just comparing for equality, we can emit this instead:
511 // xoris r0,r3,0x1234
512 // cmplwi cr0,r0,0x5678
513 // beq cr0,L6
514 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS, MVT::i32, LHS,
515 getI32Imm(Imm >> 16)), 0);
516 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, Xor,
517 getI32Imm(Imm & 0xFFFF)), 0);
518 }
519 Opc = PPC::CMPLW;
520 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000521 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
522 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
523 getI32Imm(Imm & 0xFFFF)), 0);
524 Opc = PPC::CMPLW;
525 } else {
526 short SImm;
527 if (isIntS16Immediate(RHS, SImm))
528 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
529 getI32Imm((int)SImm & 0xFFFF)),
530 0);
531 Opc = PPC::CMPW;
532 }
533 } else if (LHS.getValueType() == MVT::i64) {
534 uint64_t Imm;
Chris Lattner71176242006-09-20 04:33:27 +0000535 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
536 if (isInt64Immediate(RHS.Val, Imm)) {
537 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
538 if (isUInt16(Imm))
539 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
540 getI32Imm(Imm & 0xFFFF)), 0);
541 // If this is a 16-bit signed immediate, fold it.
542 if (isInt16(Imm))
543 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
544 getI32Imm(Imm & 0xFFFF)), 0);
545
546 // For non-equality comparisons, the default code would materialize the
547 // constant, then compare against it, like this:
548 // lis r2, 4660
549 // ori r2, r2, 22136
550 // cmpd cr0, r3, r2
551 // Since we are just comparing for equality, we can emit this instead:
552 // xoris r0,r3,0x1234
553 // cmpldi cr0,r0,0x5678
554 // beq cr0,L6
555 if (isUInt32(Imm)) {
556 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS8, MVT::i64, LHS,
557 getI64Imm(Imm >> 16)), 0);
558 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, Xor,
559 getI64Imm(Imm & 0xFFFF)), 0);
560 }
561 }
562 Opc = PPC::CMPLD;
563 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000564 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
565 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
566 getI64Imm(Imm & 0xFFFF)), 0);
567 Opc = PPC::CMPLD;
568 } else {
569 short SImm;
570 if (isIntS16Immediate(RHS, SImm))
571 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000572 getI64Imm(SImm & 0xFFFF)),
Chris Lattnerc08f9022006-06-27 00:04:13 +0000573 0);
574 Opc = PPC::CMPD;
575 }
Chris Lattner919c0322005-10-01 01:35:02 +0000576 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000577 Opc = PPC::FCMPUS;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000578 } else {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000579 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
580 Opc = PPC::FCMPUD;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000581 }
Evan Cheng6da2f322006-08-26 01:07:58 +0000582 AddToISelQueue(RHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000583 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000584}
585
586/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
587/// to Condition.
588static unsigned getBCCForSetCC(ISD::CondCode CC) {
589 switch (CC) {
590 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000591 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000592 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000593 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000594 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000595 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000596 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000597 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000598 case ISD::SETULT:
599 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000600 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000601 case ISD::SETULE:
602 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000603 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000604 case ISD::SETUGT:
605 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000606 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000607 case ISD::SETUGE:
608 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000609
Chris Lattner1d754002006-10-30 23:02:25 +0000610 case ISD::SETO: return PPC::BNU;
611 case ISD::SETUO: return PPC::BUN;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000612 }
613 return 0;
614}
615
Chris Lattner64906a02005-08-25 20:08:18 +0000616/// getCRIdxForSetCC - Return the index of the condition register field
617/// associated with the SetCC condition, and whether or not the field is
618/// treated as inverted. That is, lt = 0; ge = 0 inverted.
619static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
620 switch (CC) {
621 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000622 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000623 case ISD::SETULT:
624 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000625 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000626 case ISD::SETUGE:
627 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000628 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000629 case ISD::SETUGT:
630 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000631 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000632 case ISD::SETULE:
633 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000634 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000635 case ISD::SETUEQ:
Chris Lattner64906a02005-08-25 20:08:18 +0000636 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000637 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000638 case ISD::SETUNE:
Chris Lattner64906a02005-08-25 20:08:18 +0000639 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000640 case ISD::SETO: Inv = true; return 3;
641 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000642 }
643 return 0;
644}
Chris Lattner9944b762005-08-21 22:31:09 +0000645
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000646SDNode *PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000647 SDNode *N = Op.Val;
648 unsigned Imm;
649 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000650 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner222adac2005-10-06 19:03:35 +0000651 // We can codegen setcc op, imm very efficiently compared to a brcond.
652 // Check for those cases here.
653 // setcc op, 0
654 if (Imm == 0) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000655 SDOperand Op = N->getOperand(0);
656 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000657 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000658 default: break;
Evan Cheng0b828e02006-08-27 08:14:06 +0000659 case ISD::SETEQ: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000660 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000661 SDOperand Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
662 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
663 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000664 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000665 SDOperand AD =
666 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
667 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000668 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000669 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000670 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000671 case ISD::SETLT: {
672 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
673 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
674 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000675 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000676 SDOperand T =
677 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
678 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000679 SDOperand Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
680 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000681 }
682 }
Chris Lattner222adac2005-10-06 19:03:35 +0000683 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng6da2f322006-08-26 01:07:58 +0000684 SDOperand Op = N->getOperand(0);
685 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000686 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000687 default: break;
688 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000689 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
690 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000691 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000692 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
693 getI32Imm(0)), 0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000694 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000695 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000696 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
697 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
698 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000699 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000700 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000701 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000702 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000703 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
704 getI32Imm(1)), 0);
705 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
706 Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000707 SDOperand Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
708 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000709 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000710 case ISD::SETGT: {
711 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
712 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000713 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000714 getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000715 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000716 }
Chris Lattner222adac2005-10-06 19:03:35 +0000717 }
718 }
719
720 bool Inv;
721 unsigned Idx = getCRIdxForSetCC(CC, Inv);
722 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
723 SDOperand IntCR;
724
725 // Force the ccreg into CR7.
726 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
727
Chris Lattner85961d52005-12-06 20:56:18 +0000728 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000729 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
730 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000731
732 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000733 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
734 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000735 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000736 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000737
Evan Cheng0b828e02006-08-27 08:14:06 +0000738 SDOperand Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
739 getI32Imm(31), getI32Imm(31) };
Chris Lattner222adac2005-10-06 19:03:35 +0000740 if (!Inv) {
Evan Cheng0b828e02006-08-27 08:14:06 +0000741 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner222adac2005-10-06 19:03:35 +0000742 } else {
743 SDOperand Tmp =
Evan Cheng0b828e02006-08-27 08:14:06 +0000744 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000745 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000746 }
Chris Lattner222adac2005-10-06 19:03:35 +0000747}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000748
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000749
Chris Lattnera5a91b12005-08-17 19:33:03 +0000750// Select - Convert the specified operand from a target-independent to a
751// target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +0000752SDNode *PPCDAGToDAGISel::Select(SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000753 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000754 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng9ade2182006-08-26 05:34:46 +0000755 N->getOpcode() < PPCISD::FIRST_NUMBER)
Evan Cheng64a752f2006-08-11 09:08:15 +0000756 return NULL; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000757
Chris Lattnera5a91b12005-08-17 19:33:03 +0000758 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000759 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000760 case ISD::SETCC:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000761 return SelectSETCC(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000762 case PPCISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000763 return getGlobalBaseReg();
Chris Lattner860e8862005-11-17 07:30:41 +0000764
Chris Lattnere28e40a2005-08-25 00:45:43 +0000765 case ISD::FrameIndex: {
766 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000767 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
768 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000769 if (N->hasOneUse())
770 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
Evan Cheng95514ba2006-08-26 08:00:10 +0000771 getSmallIPtrImm(0));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000772 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
773 getSmallIPtrImm(0));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000774 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000775
776 case PPCISD::MFCR: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000777 SDOperand InFlag = N->getOperand(1);
778 AddToISelQueue(InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000779 // Use MFOCRF if supported.
780 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000781 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
782 N->getOperand(0), InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000783 else
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000784 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000785 }
786
Chris Lattner88add102005-09-28 22:50:24 +0000787 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000788 // FIXME: since this depends on the setting of the carry flag from the srawi
789 // we should really be making notes about that for the scheduler.
790 // FIXME: It sure would be nice if we could cheaply recognize the
791 // srl/add/sra pattern the dag combiner will generate for this as
792 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000793 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000794 if (isInt32Immediate(N->getOperand(1), Imm)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000795 SDOperand N0 = N->getOperand(0);
796 AddToISelQueue(N0);
Chris Lattner8784a232005-08-25 17:50:06 +0000797 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000798 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000799 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000800 N0, getI32Imm(Log2_32(Imm)));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000801 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng95514ba2006-08-26 08:00:10 +0000802 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000803 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000804 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000805 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000806 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000807 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000808 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
809 SDOperand(Op, 0), SDOperand(Op, 1)),
810 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000811 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000812 }
813 }
Chris Lattner047b9522005-08-25 22:04:30 +0000814
Chris Lattner237733e2005-09-29 23:33:31 +0000815 // Other cases are autogenerated.
816 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000817 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000818 case ISD::AND: {
Nate Begemanf42f1332006-09-22 05:01:56 +0000819 unsigned Imm, Imm2, SH, MB, ME;
820
Nate Begemancffc32b2005-08-18 07:30:46 +0000821 // If this is an and of a value rotated between 0 and 31 bits and then and'd
822 // with a mask, emit rlwinm
Chris Lattnerc08f9022006-06-27 00:04:13 +0000823 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begemanf42f1332006-09-22 05:01:56 +0000824 isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
825 SDOperand Val = N->getOperand(0).getOperand(0);
826 AddToISelQueue(Val);
Evan Cheng0b828e02006-08-27 08:14:06 +0000827 SDOperand Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
828 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemancffc32b2005-08-18 07:30:46 +0000829 }
Nate Begemanf42f1332006-09-22 05:01:56 +0000830 // If this is just a masked value where the input is not handled above, and
831 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
832 if (isInt32Immediate(N->getOperand(1), Imm) &&
833 isRunOfOnes(Imm, MB, ME) &&
834 N->getOperand(0).getOpcode() != ISD::ROTL) {
835 SDOperand Val = N->getOperand(0);
836 AddToISelQueue(Val);
837 SDOperand Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
838 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
839 }
840 // AND X, 0 -> 0, not "rlwinm 32".
841 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
842 AddToISelQueue(N->getOperand(1));
843 ReplaceUses(SDOperand(N, 0), N->getOperand(1));
844 return NULL;
845 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000846 // ISD::OR doesn't get all the bitfield insertion fun.
847 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattnerc08f9022006-06-27 00:04:13 +0000848 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman50fb3c42005-12-24 01:00:15 +0000849 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000850 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000851 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000852 Imm = ~(Imm^Imm2);
853 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000854 AddToISelQueue(N->getOperand(0).getOperand(0));
855 AddToISelQueue(N->getOperand(0).getOperand(1));
Evan Cheng0b828e02006-08-27 08:14:06 +0000856 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
857 N->getOperand(0).getOperand(1),
858 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
859 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman50fb3c42005-12-24 01:00:15 +0000860 }
861 }
Chris Lattner237733e2005-09-29 23:33:31 +0000862
863 // Other cases are autogenerated.
864 break;
Nate Begemancffc32b2005-08-18 07:30:46 +0000865 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000866 case ISD::OR:
Chris Lattnercccef1c2006-06-27 21:08:52 +0000867 if (N->getValueType(0) == MVT::i32)
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000868 if (SDNode *I = SelectBitfieldInsert(N))
869 return I;
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000870
Chris Lattner237733e2005-09-29 23:33:31 +0000871 // Other cases are autogenerated.
872 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000873 case ISD::SHL: {
874 unsigned Imm, SH, MB, ME;
875 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000876 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000877 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Cheng0b828e02006-08-27 08:14:06 +0000878 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
879 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
880 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +0000881 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000882
883 // Other cases are autogenerated.
884 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000885 }
886 case ISD::SRL: {
887 unsigned Imm, SH, MB, ME;
888 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +0000889 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000890 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Cheng0b828e02006-08-27 08:14:06 +0000891 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
892 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
893 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +0000894 }
Nate Begeman2d5aff72005-10-19 18:42:01 +0000895
896 // Other cases are autogenerated.
897 break;
Nate Begemanc15ed442005-08-18 23:38:00 +0000898 }
Chris Lattner13794f52005-08-26 18:46:49 +0000899 case ISD::SELECT_CC: {
900 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
901
Chris Lattnerc08f9022006-06-27 00:04:13 +0000902 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +0000903 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
904 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
905 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
906 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000907 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
908 // FIXME: Implement this optzn for PPC64.
909 N->getValueType(0) == MVT::i32) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000910 AddToISelQueue(N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000911 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +0000912 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng6da2f322006-08-26 01:07:58 +0000913 N->getOperand(0), getI32Imm(~0U));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000914 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Evan Cheng6da2f322006-08-26 01:07:58 +0000915 SDOperand(Tmp, 0), N->getOperand(0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000916 SDOperand(Tmp, 1));
Chris Lattner13794f52005-08-26 18:46:49 +0000917 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000918
Chris Lattner50ff55c2005-09-01 19:20:44 +0000919 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000920 unsigned BROpc = getBCCForSetCC(CC);
921
Chris Lattner919c0322005-10-01 01:35:02 +0000922 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000923 if (N->getValueType(0) == MVT::i32)
924 SelectCCOp = PPC::SELECT_CC_I4;
925 else if (N->getValueType(0) == MVT::i64)
926 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +0000927 else if (N->getValueType(0) == MVT::f32)
928 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +0000929 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +0000930 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +0000931 else
932 SelectCCOp = PPC::SELECT_CC_VRRC;
933
Evan Cheng6da2f322006-08-26 01:07:58 +0000934 AddToISelQueue(N->getOperand(2));
935 AddToISelQueue(N->getOperand(3));
Evan Cheng0b828e02006-08-27 08:14:06 +0000936 SDOperand Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
937 getI32Imm(BROpc) };
938 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattner13794f52005-08-26 18:46:49 +0000939 }
Nate Begeman81e80972006-03-17 01:40:33 +0000940 case ISD::BR_CC: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000941 AddToISelQueue(N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +0000942 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
943 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Evan Cheng0b828e02006-08-27 08:14:06 +0000944 SDOperand Ops[] = { CondCode, getI32Imm(getBCCForSetCC(CC)),
945 N->getOperand(4), N->getOperand(0) };
946 return CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, Ops, 4);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000947 }
Nate Begeman37efe672006-04-22 18:53:45 +0000948 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +0000949 // FIXME: Should custom lower this.
Evan Cheng6da2f322006-08-26 01:07:58 +0000950 SDOperand Chain = N->getOperand(0);
951 SDOperand Target = N->getOperand(1);
952 AddToISelQueue(Chain);
953 AddToISelQueue(Target);
Chris Lattner6b76b962006-06-27 20:46:17 +0000954 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
955 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman37efe672006-04-22 18:53:45 +0000956 Chain), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000957 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
Nate Begeman37efe672006-04-22 18:53:45 +0000958 }
Chris Lattnercf006312006-06-10 01:15:02 +0000959 // FIXME: These are manually selected because tblgen isn't handling varargs
960 // nodes correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000961 case PPCISD::BCTRL: return MySelect_PPCbctrl(Op);
962 case PPCISD::CALL: return MySelect_PPCcall(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000963 }
Chris Lattner25dae722005-09-03 00:53:47 +0000964
Evan Cheng9ade2182006-08-26 05:34:46 +0000965 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000966}
967
968
Chris Lattnercf006312006-06-10 01:15:02 +0000969// FIXME: This is manually selected because tblgen isn't handling varargs nodes
970// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000971SDNode *PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +0000972 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +0000973
974 bool hasFlag =
975 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
976
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000977 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +0000978 // Push varargs arguments, including optional flag.
979 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000980 Chain = N.getOperand(i);
981 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +0000982 Ops.push_back(Chain);
983 }
984
Evan Cheng6da2f322006-08-26 01:07:58 +0000985 Chain = N.getOperand(0);
986 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +0000987 Ops.push_back(Chain);
988
989 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000990 Chain = N.getOperand(N.getNumOperands()-1);
991 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +0000992 Ops.push_back(Chain);
993 }
994
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000995 return CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag,
996 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +0000997}
998
999// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1000// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001001SDNode *PPCDAGToDAGISel::MySelect_PPCcall(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +00001002 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001003 SDOperand N1(0, 0);
1004 SDOperand Tmp0(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001005 Chain = N.getOperand(0);
1006 N1 = N.getOperand(1);
1007
1008 // Pattern: (PPCcall:void (imm:i32):$func)
1009 // Emits: (BLA:void (imm:i32):$func)
1010 // Pattern complexity = 4 cost = 1
1011 if (N1.getOpcode() == ISD::Constant) {
1012 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1013
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001014 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001015 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1016
1017 bool hasFlag =
1018 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1019
1020 // Push varargs arguments, not including optional flag.
1021 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001022 Chain = N.getOperand(i);
1023 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001024 Ops.push_back(Chain);
1025 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001026 Chain = N.getOperand(0);
1027 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001028 Ops.push_back(Chain);
1029 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001030 Chain = N.getOperand(N.getNumOperands()-1);
1031 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001032 Ops.push_back(Chain);
1033 }
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001034 return CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag,
1035 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001036 }
1037
1038 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1039 // Emits: (BL:void (tglobaladdr:i32):$dst)
1040 // Pattern complexity = 4 cost = 1
1041 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001042 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001043 Ops.push_back(N1);
1044
1045 bool hasFlag =
1046 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1047
1048 // Push varargs arguments, not including optional flag.
1049 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001050 Chain = N.getOperand(i);
1051 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001052 Ops.push_back(Chain);
1053 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001054 Chain = N.getOperand(0);
1055 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001056 Ops.push_back(Chain);
1057 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001058 Chain = N.getOperand(N.getNumOperands()-1);
1059 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001060 Ops.push_back(Chain);
1061 }
1062
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001063 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1064 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001065 }
1066
1067 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1068 // Emits: (BL:void (texternalsym:i32):$dst)
1069 // Pattern complexity = 4 cost = 1
1070 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1071 std::vector<SDOperand> Ops;
1072 Ops.push_back(N1);
1073
1074 bool hasFlag =
1075 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1076
1077 // Push varargs arguments, not including optional flag.
1078 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001079 Chain = N.getOperand(i);
1080 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001081 Ops.push_back(Chain);
1082 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001083 Chain = N.getOperand(0);
1084 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001085 Ops.push_back(Chain);
1086 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001087 Chain = N.getOperand(N.getNumOperands()-1);
1088 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001089 Ops.push_back(Chain);
1090 }
1091
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001092 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1093 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001094 }
1095 std::cerr << "Cannot yet select: ";
1096 N.Val->dump(CurDAG);
1097 std::cerr << '\n';
1098 abort();
Evan Cheng64a752f2006-08-11 09:08:15 +00001099
1100 return NULL;
Chris Lattnercf006312006-06-10 01:15:02 +00001101}
1102
1103
Nate Begeman1d9d7422005-10-18 00:28:58 +00001104/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001105/// PowerPC-specific DAG, ready for instruction scheduling.
1106///
Evan Chengc4c62572006-03-13 23:20:37 +00001107FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001108 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001109}
1110