blob: 40f10057d576825a1952d7ae063e8c6f77d99876 [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 Lattner2a41a982006-06-28 22:00:36 +000031#include "llvm/Support/Visibility.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
79
Chris Lattner4416f1a2005-08-19 22:38:53 +000080 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
81 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000082 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000083
84 // Select - Convert the specified operand from a target-independent to a
85 // target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +000086 SDNode *Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000087
Nate Begeman02b88a42005-08-19 00:38:14 +000088 SDNode *SelectBitfieldInsert(SDNode *N);
89
Chris Lattner2fbb4572005-08-21 18:50:37 +000090 /// SelectCC - Select a comparison of the specified values with the
91 /// specified condition code, returning the CR# of the expression.
92 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
93
Nate Begeman7fd1edd2005-12-19 23:25:09 +000094 /// SelectAddrImm - Returns true if the address N can be represented by
95 /// a base register plus a signed 16-bit displacement [r+imm].
96 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
97
98 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
99 /// represented as an indexed [r+r] operation. Returns false if it can
100 /// be represented by [r+imm], which are preferred.
101 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000102
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000103 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
104 /// represented as an indexed [r+r] operation.
105 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +0000106
Chris Lattnere5ba5802006-03-22 05:26:03 +0000107 /// SelectAddrImmShift - Returns true if the address N can be represented by
108 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
109 /// for use by STD and friends.
110 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
111
Chris Lattnere5d88612006-02-24 02:13:12 +0000112 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
113 /// inline asm expressions.
114 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
115 char ConstraintCode,
116 std::vector<SDOperand> &OutOps,
117 SelectionDAG &DAG) {
118 SDOperand Op0, Op1;
119 switch (ConstraintCode) {
120 default: return true;
121 case 'm': // memory
122 if (!SelectAddrIdx(Op, Op0, Op1))
123 SelectAddrImm(Op, Op0, Op1);
124 break;
125 case 'o': // offsetable
126 if (!SelectAddrImm(Op, Op0, Op1)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000127 Op0 = Op;
128 AddToISelQueue(Op0); // r+0.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000129 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000130 }
131 break;
132 case 'v': // not offsetable
133 SelectAddrIdxOnly(Op, Op0, Op1);
134 break;
135 }
136
137 OutOps.push_back(Op0);
138 OutOps.push_back(Op1);
139 return false;
140 }
141
Chris Lattner047b9522005-08-25 22:04:30 +0000142 SDOperand BuildSDIVSequence(SDNode *N);
143 SDOperand BuildUDIVSequence(SDNode *N);
144
Chris Lattnera5a91b12005-08-17 19:33:03 +0000145 /// InstructionSelectBasicBlock - This callback is invoked by
146 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000147 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
148
Chris Lattner4bb18952006-03-16 18:25:23 +0000149 void InsertVRSaveCode(Function &Fn);
150
Chris Lattnera5a91b12005-08-17 19:33:03 +0000151 virtual const char *getPassName() const {
152 return "PowerPC DAG->DAG Pattern Instruction Selection";
153 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000154
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000155 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
156 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000157 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000158 // Should use subtarget info to pick the right hazard recognizer. For
159 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000160 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
161 assert(II && "No InstrInfo?");
162 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000163 }
Chris Lattneraf165382005-09-13 22:03:06 +0000164
165// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000166#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000167
168private:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000169 SDNode *SelectSETCC(SDOperand Op);
170 SDNode *MySelect_PPCbctrl(SDOperand N);
171 SDNode *MySelect_PPCcall(SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000172 };
173}
174
Chris Lattnerbd937b92005-10-06 18:45:51 +0000175/// InstructionSelectBasicBlock - This callback is invoked by
176/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000177void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000178 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000179
Chris Lattnerbd937b92005-10-06 18:45:51 +0000180 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000181 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000182 DAG.RemoveDeadNodes();
183
Chris Lattner1877ec92006-03-13 21:52:10 +0000184 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000185 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000186}
187
188/// InsertVRSaveCode - Once the entire function has been instruction selected,
189/// all virtual registers are created and all machine instructions are built,
190/// check to see if we need to save/restore VRSAVE. If so, do it.
191void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000192 // Check to see if this function uses vector registers, which means we have to
193 // save and restore the VRSAVE register and update it with the regs we use.
194 //
195 // In this case, there will be virtual registers of vector type type created
196 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000197 MachineFunction &Fn = MachineFunction::get(&F);
198 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000199 bool HasVectorVReg = false;
200 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000201 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000202 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
203 HasVectorVReg = true;
204 break;
205 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000206 if (!HasVectorVReg) return; // nothing to do.
207
Chris Lattner1877ec92006-03-13 21:52:10 +0000208 // If we have a vector register, we want to emit code into the entry and exit
209 // blocks to save and restore the VRSAVE register. We do this here (instead
210 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
211 //
212 // 1. This (trivially) reduces the load on the register allocator, by not
213 // having to represent the live range of the VRSAVE register.
214 // 2. This (more significantly) allows us to create a temporary virtual
215 // register to hold the saved VRSAVE value, allowing this temporary to be
216 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000217
218 // Create two vregs - one to hold the VRSAVE register that is live-in to the
219 // function and one for the value after having bits or'd into it.
220 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
221 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
222
223 MachineBasicBlock &EntryBB = *Fn.begin();
224 // Emit the following code into the entry block:
225 // InVRSAVE = MFVRSAVE
226 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
227 // MTVRSAVE UpdatedVRSAVE
228 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
229 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
230 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
231 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
232
233 // Find all return blocks, outputting a restore in each epilog.
234 const TargetInstrInfo &TII = *TM.getInstrInfo();
235 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
236 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
237 IP = BB->end(); --IP;
238
239 // Skip over all terminator instructions, which are part of the return
240 // sequence.
241 MachineBasicBlock::iterator I2 = IP;
242 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
243 IP = I2;
244
245 // Emit: MTVRSAVE InVRSave
246 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
247 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000248 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000249}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000250
Chris Lattner4bb18952006-03-16 18:25:23 +0000251
Chris Lattner4416f1a2005-08-19 22:38:53 +0000252/// getGlobalBaseReg - Output the instructions required to put the
253/// base address to use for accessing globals into a register.
254///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000255SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000256 if (!GlobalBaseReg) {
257 // Insert the set of GlobalBaseReg into the first MBB of the function
258 MachineBasicBlock &FirstMBB = BB->getParent()->front();
259 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
260 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000261
262 if (PPCLowering.getPointerTy() == MVT::i32)
263 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
264 else
265 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
266
Chris Lattner4416f1a2005-08-19 22:38:53 +0000267 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
268 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
269 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000270 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy());
271}
272
273/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
274/// or 64-bit immediate, and if the value can be accurately represented as a
275/// sign extension from a 16-bit value. If so, this returns true and the
276/// immediate.
277static bool isIntS16Immediate(SDNode *N, short &Imm) {
278 if (N->getOpcode() != ISD::Constant)
279 return false;
280
281 Imm = (short)cast<ConstantSDNode>(N)->getValue();
282 if (N->getValueType(0) == MVT::i32)
283 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
284 else
285 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
286}
287
288static bool isIntS16Immediate(SDOperand Op, short &Imm) {
289 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000290}
291
292
Chris Lattnerc08f9022006-06-27 00:04:13 +0000293/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
294/// operand. If so Imm will receive the 32-bit value.
295static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
296 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000297 Imm = cast<ConstantSDNode>(N)->getValue();
298 return true;
299 }
300 return false;
301}
302
Chris Lattnerc08f9022006-06-27 00:04:13 +0000303/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
304/// operand. If so Imm will receive the 64-bit value.
305static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
306 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
307 Imm = cast<ConstantSDNode>(N)->getValue();
308 return true;
309 }
310 return false;
311}
312
313// isInt32Immediate - This method tests to see if a constant operand.
314// If so Imm will receive the 32 bit value.
315static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
316 return isInt32Immediate(N.Val, Imm);
317}
318
319
320// isOpcWithIntImmediate - This method tests to see if the node is a specific
321// opcode and that it has a immediate integer right operand.
322// If so Imm will receive the 32 bit value.
323static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
324 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
325}
326
327
Nate Begemancffc32b2005-08-18 07:30:46 +0000328// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
329// any number of 0s on either side. The 1s are allowed to wrap from LSB to
330// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
331// not, since all 1s are not contiguous.
332static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
333 if (isShiftedMask_32(Val)) {
334 // look for the first non-zero bit
335 MB = CountLeadingZeros_32(Val);
336 // look for the first zero bit after the run of ones
337 ME = CountLeadingZeros_32((Val - 1) ^ Val);
338 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000339 } else {
340 Val = ~Val; // invert mask
341 if (isShiftedMask_32(Val)) {
342 // effectively look for the first zero bit
343 ME = CountLeadingZeros_32(Val) - 1;
344 // effectively look for the first one bit after the run of zeros
345 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
346 return true;
347 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000348 }
349 // no run present
350 return false;
351}
352
Chris Lattner65a419a2005-10-09 05:36:17 +0000353// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000354// and mask opcode and mask operation.
355static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
356 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000357 // Don't even go down this path for i64, since different logic will be
358 // necessary for rldicl/rldicr/rldimi.
359 if (N->getValueType(0) != MVT::i32)
360 return false;
361
Nate Begemancffc32b2005-08-18 07:30:46 +0000362 unsigned Shift = 32;
363 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
364 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000365 if (N->getNumOperands() != 2 ||
Chris Lattnerc08f9022006-06-27 00:04:13 +0000366 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000367 return false;
368
369 if (Opcode == ISD::SHL) {
370 // apply shift left to mask if it comes first
371 if (IsShiftMask) Mask = Mask << Shift;
372 // determine which bits are made indeterminant by shift
373 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000374 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000375 // apply shift right to mask if it comes first
376 if (IsShiftMask) Mask = Mask >> Shift;
377 // determine which bits are made indeterminant by shift
378 Indeterminant = ~(0xFFFFFFFFu >> Shift);
379 // adjust for the left rotate
380 Shift = 32 - Shift;
381 } else {
382 return false;
383 }
384
385 // if the mask doesn't intersect any Indeterminant bits
386 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000387 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000388 // make sure the mask is still a mask (wrap arounds may not be)
389 return isRunOfOnes(Mask, MB, ME);
390 }
391 return false;
392}
393
Nate Begeman02b88a42005-08-19 00:38:14 +0000394/// SelectBitfieldInsert - turn an or of two masked values into
395/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000396SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000397 SDOperand Op0 = N->getOperand(0);
398 SDOperand Op1 = N->getOperand(1);
399
Nate Begeman77f361f2006-05-07 00:23:38 +0000400 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000401 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
402 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000403
Nate Begeman4667f2c2006-05-08 17:38:32 +0000404 unsigned TargetMask = LKZ;
405 unsigned InsertMask = RKZ;
406
407 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
408 unsigned Op0Opc = Op0.getOpcode();
409 unsigned Op1Opc = Op1.getOpcode();
410 unsigned Value, SH = 0;
411 TargetMask = ~TargetMask;
412 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000413
Nate Begeman4667f2c2006-05-08 17:38:32 +0000414 // If the LHS has a foldable shift and the RHS does not, then swap it to the
415 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000416 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
417 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
418 Op0.getOperand(0).getOpcode() == ISD::SRL) {
419 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
420 Op1.getOperand(0).getOpcode() != ISD::SRL) {
421 std::swap(Op0, Op1);
422 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000423 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000424 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000425 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000426 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
427 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
428 Op1.getOperand(0).getOpcode() != ISD::SRL) {
429 std::swap(Op0, Op1);
430 std::swap(Op0Opc, Op1Opc);
431 std::swap(TargetMask, InsertMask);
432 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000433 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000434
435 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000436 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000437 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000438 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000439
440 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000441 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000442 Op1 = Op1.getOperand(0);
443 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
444 }
445 if (Op1Opc == ISD::AND) {
446 unsigned SHOpc = Op1.getOperand(0).getOpcode();
447 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000448 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000449 Op1 = Op1.getOperand(0).getOperand(0);
450 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
451 } else {
452 Op1 = Op1.getOperand(0);
453 }
454 }
455
456 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Evan Cheng6da2f322006-08-26 01:07:58 +0000457 AddToISelQueue(Tmp3);
458 AddToISelQueue(Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000459 SH &= 31;
Evan Cheng6da2f322006-08-26 01:07:58 +0000460 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp3, Op1,
Nate Begeman77f361f2006-05-07 00:23:38 +0000461 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000462 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000463 }
464 return 0;
465}
466
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000467/// SelectAddrImm - Returns true if the address N can be represented by
468/// a base register plus a signed 16-bit displacement [r+imm].
469bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
470 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000471 // If this can be more profitably realized as r+r, fail.
472 if (SelectAddrIdx(N, Disp, Base))
473 return false;
474
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000475 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000476 short imm = 0;
477 if (isIntS16Immediate(N.getOperand(1), imm)) {
478 Disp = getI32Imm((int)imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000479 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000480 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner9944b762005-08-21 22:31:09 +0000481 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000482 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000483 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000484 return true; // [r+i]
485 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000486 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000487 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000488 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000489 Disp = N.getOperand(1).getOperand(0); // The global address.
490 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000491 Disp.getOpcode() == ISD::TargetConstantPool ||
492 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000493 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000494 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000495 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000496 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000497 short imm = 0;
498 if (isIntS16Immediate(N.getOperand(1), imm)) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000499 // If this is an or of disjoint bitfields, we can codegen this as an add
500 // (for better address arithmetic) if the LHS and RHS of the OR are
501 // provably disjoint.
502 uint64_t LHSKnownZero, LHSKnownOne;
503 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
504 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000505 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000506 // If all of the bits are known zero on the LHS or RHS, the add won't
507 // carry.
508 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000509 Disp = getI32Imm((int)imm & 0xFFFF);
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000510 return true;
511 }
512 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000513 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
514 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000515
Chris Lattnerd9796442006-03-20 22:38:22 +0000516 // If this address fits entirely in a 16-bit sext immediate field, codegen
517 // this as "d, 0"
Chris Lattnerc08f9022006-06-27 00:04:13 +0000518 short Imm;
519 if (isIntS16Immediate(CN, Imm)) {
520 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
521 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
Chris Lattnerd9796442006-03-20 22:38:22 +0000522 return true;
523 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000524
525 // FIXME: Handle small sext constant offsets in PPC64 mode also!
526 if (CN->getValueType(0) == MVT::i32) {
527 int Addr = (int)CN->getValue();
Chris Lattnerd9796442006-03-20 22:38:22 +0000528
Chris Lattnerc08f9022006-06-27 00:04:13 +0000529 // Otherwise, break this down into an LIS + disp.
530 Disp = getI32Imm((short)Addr);
531 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
532 return true;
533 }
Chris Lattner9944b762005-08-21 22:31:09 +0000534 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000535
Chris Lattnerc08f9022006-06-27 00:04:13 +0000536 Disp = getSmallIPtrImm(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000537 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000538 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Nate Begeman28a6b022005-12-10 02:36:00 +0000539 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000540 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000541 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000542}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000543
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000544/// SelectAddrIdx - Given the specified addressed, check to see if it can be
545/// represented as an indexed [r+r] operation. Returns false if it can
546/// be represented by [r+imm], which are preferred.
547bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
548 SDOperand &Index) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000549 short imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000550 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000551 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000552 return false; // r+i
553 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
554 return false; // r+i
555
Evan Cheng7564e0b2006-02-05 08:45:01 +0000556 Base = N.getOperand(0);
557 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000558 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000559 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000560 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000561 return false; // r+i can fold it if we can.
562
563 // If this is an or of disjoint bitfields, we can codegen this as an add
564 // (for better address arithmetic) if the LHS and RHS of the OR are provably
565 // disjoint.
566 uint64_t LHSKnownZero, LHSKnownOne;
567 uint64_t RHSKnownZero, RHSKnownOne;
568 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
569 LHSKnownZero, LHSKnownOne);
570
571 if (LHSKnownZero) {
572 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
573 RHSKnownZero, RHSKnownOne);
574 // If all of the bits are known zero on the LHS or RHS, the add won't
575 // carry.
576 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
577 Base = N.getOperand(0);
578 Index = N.getOperand(1);
579 return true;
580 }
581 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000582 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000583
584 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000585}
586
587/// SelectAddrIdxOnly - Given the specified addressed, force it to be
588/// represented as an indexed [r+r] operation.
589bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
590 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000591 // Check to see if we can easily represent this as an [r+r] address. This
592 // will fail if it thinks that the address is more profitably represented as
593 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000594 if (SelectAddrIdx(N, Base, Index))
595 return true;
596
597 // If the operand is an addition, always emit this as [r+r], since this is
598 // better (for code size, and execution, as the memop does the add for free)
599 // than emitting an explicit add.
600 if (N.getOpcode() == ISD::ADD) {
601 Base = N.getOperand(0);
602 Index = N.getOperand(1);
603 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000604 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000605
606 // Otherwise, do it the hard way, using R0 as the base register.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000607 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
Chris Lattner54e869e2006-03-24 17:58:06 +0000608 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000609 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000610}
611
Chris Lattnere5ba5802006-03-22 05:26:03 +0000612/// SelectAddrImmShift - Returns true if the address N can be represented by
613/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
614/// for use by STD and friends.
615bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
616 SDOperand &Base) {
617 // If this can be more profitably realized as r+r, fail.
618 if (SelectAddrIdx(N, Disp, Base))
619 return false;
620
621 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000622 short imm = 0;
623 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
624 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000625 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000626 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000627 } else {
628 Base = N.getOperand(0);
629 }
630 return true; // [r+i]
631 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
632 // Match LOAD (ADD (X, Lo(G))).
633 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
634 && "Cannot handle constant offsets yet!");
635 Disp = N.getOperand(1).getOperand(0); // The global address.
636 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000637 Disp.getOpcode() == ISD::TargetConstantPool ||
638 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000639 Base = N.getOperand(0);
640 return true; // [&g+r]
641 }
642 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000643 short imm = 0;
644 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000645 // If this is an or of disjoint bitfields, we can codegen this as an add
646 // (for better address arithmetic) if the LHS and RHS of the OR are
647 // provably disjoint.
648 uint64_t LHSKnownZero, LHSKnownOne;
649 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
650 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000651 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000652 // If all of the bits are known zero on the LHS or RHS, the add won't
653 // carry.
654 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000655 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000656 return true;
657 }
658 }
659 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
660 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000661
662 // If this address fits entirely in a 14-bit sext immediate field, codegen
663 // this as "d, 0"
664 short Imm;
665 if (isIntS16Immediate(CN, Imm)) {
666 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
667 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
668 return true;
669 }
670
671 // FIXME: Handle small sext constant offsets in PPC64 mode also!
672 if (CN->getValueType(0) == MVT::i32) {
673 int Addr = (int)CN->getValue();
Chris Lattnere5ba5802006-03-22 05:26:03 +0000674
675 // Otherwise, break this down into an LIS + disp.
676 Disp = getI32Imm((short)Addr >> 2);
677 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
678 return true;
679 }
680 }
681
Chris Lattnerc08f9022006-06-27 00:04:13 +0000682 Disp = getSmallIPtrImm(0);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000683 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000684 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000685 else
686 Base = N;
687 return true; // [r+0]
688}
689
690
Chris Lattner2fbb4572005-08-21 18:50:37 +0000691/// SelectCC - Select a comparison of the specified values with the specified
692/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000693SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
694 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000695 // Always select the LHS.
Evan Cheng6da2f322006-08-26 01:07:58 +0000696 AddToISelQueue(LHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000697 unsigned Opc;
698
699 if (LHS.getValueType() == MVT::i32) {
Chris Lattner529c2332006-06-27 00:10:13 +0000700 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000701 if (ISD::isUnsignedIntSetCC(CC)) {
702 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
703 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
704 getI32Imm(Imm & 0xFFFF)), 0);
705 Opc = PPC::CMPLW;
706 } else {
707 short SImm;
708 if (isIntS16Immediate(RHS, SImm))
709 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
710 getI32Imm((int)SImm & 0xFFFF)),
711 0);
712 Opc = PPC::CMPW;
713 }
714 } else if (LHS.getValueType() == MVT::i64) {
715 uint64_t Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000716 if (ISD::isUnsignedIntSetCC(CC)) {
717 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
718 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
719 getI64Imm(Imm & 0xFFFF)), 0);
720 Opc = PPC::CMPLD;
721 } else {
722 short SImm;
723 if (isIntS16Immediate(RHS, SImm))
724 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
725 getI64Imm((int)SImm & 0xFFFF)),
726 0);
727 Opc = PPC::CMPD;
728 }
Chris Lattner919c0322005-10-01 01:35:02 +0000729 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000730 Opc = PPC::FCMPUS;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000731 } else {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000732 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
733 Opc = PPC::FCMPUD;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000734 }
Evan Cheng6da2f322006-08-26 01:07:58 +0000735 AddToISelQueue(RHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000736 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000737}
738
739/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
740/// to Condition.
741static unsigned getBCCForSetCC(ISD::CondCode CC) {
742 switch (CC) {
743 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000744 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000745 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000746 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000747 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000748 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000749 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000750 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000751 case ISD::SETULT:
752 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000753 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000754 case ISD::SETULE:
755 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000756 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000757 case ISD::SETUGT:
758 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000759 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000760 case ISD::SETUGE:
761 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000762
763 case ISD::SETO: return PPC::BUN;
764 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000765 }
766 return 0;
767}
768
Chris Lattner64906a02005-08-25 20:08:18 +0000769/// getCRIdxForSetCC - Return the index of the condition register field
770/// associated with the SetCC condition, and whether or not the field is
771/// treated as inverted. That is, lt = 0; ge = 0 inverted.
772static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
773 switch (CC) {
774 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000775 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000776 case ISD::SETULT:
777 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000778 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000779 case ISD::SETUGE:
780 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000781 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000782 case ISD::SETUGT:
783 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000784 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000785 case ISD::SETULE:
786 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000787 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000788 case ISD::SETUEQ:
Chris Lattner64906a02005-08-25 20:08:18 +0000789 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000790 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000791 case ISD::SETUNE:
Chris Lattner64906a02005-08-25 20:08:18 +0000792 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000793 case ISD::SETO: Inv = true; return 3;
794 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000795 }
796 return 0;
797}
Chris Lattner9944b762005-08-21 22:31:09 +0000798
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000799SDNode *PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000800 SDNode *N = Op.Val;
801 unsigned Imm;
802 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000803 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner222adac2005-10-06 19:03:35 +0000804 // We can codegen setcc op, imm very efficiently compared to a brcond.
805 // Check for those cases here.
806 // setcc op, 0
807 if (Imm == 0) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000808 SDOperand Op = N->getOperand(0);
809 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000810 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000811 default: break;
812 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000813 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000814 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000815 getI32Imm(5), getI32Imm(31)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000816 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000817 SDOperand AD =
818 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
819 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000820 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000821 AD.getValue(1)).Val;
Chris Lattner222adac2005-10-06 19:03:35 +0000822 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000823 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000824 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000825 getI32Imm(31), getI32Imm(31)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000826 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000827 SDOperand T =
828 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
829 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000830 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000831 getI32Imm(31), getI32Imm(31)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000832 }
833 }
Chris Lattner222adac2005-10-06 19:03:35 +0000834 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng6da2f322006-08-26 01:07:58 +0000835 SDOperand Op = N->getOperand(0);
836 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000837 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000838 default: break;
839 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000840 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
841 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000842 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000843 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
844 getI32Imm(0)), 0),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000845 Op.getValue(1)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000846 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000847 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
848 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
849 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000850 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000851 Op, SDOperand(AD, 1)).Val;
Chris Lattner222adac2005-10-06 19:03:35 +0000852 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000853 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000854 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
855 getI32Imm(1)), 0);
856 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
857 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000858 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000859 getI32Imm(31), getI32Imm(31)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000860 }
861 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000862 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
863 getI32Imm(1), getI32Imm(31),
864 getI32Imm(31)), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000865 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
866 getI32Imm(1)).Val;
Chris Lattnerdabb8292005-10-21 21:17:10 +0000867 }
Chris Lattner222adac2005-10-06 19:03:35 +0000868 }
869 }
870
871 bool Inv;
872 unsigned Idx = getCRIdxForSetCC(CC, Inv);
873 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
874 SDOperand IntCR;
875
876 // Force the ccreg into CR7.
877 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
878
Chris Lattner85961d52005-12-06 20:56:18 +0000879 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000880 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
881 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000882
883 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000884 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
885 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000886 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000887 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000888
889 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000890 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
891 getI32Imm((32-(3-Idx)) & 31),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000892 getI32Imm(31), getI32Imm(31)).Val;
Chris Lattner222adac2005-10-06 19:03:35 +0000893 } else {
894 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000895 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
896 getI32Imm((32-(3-Idx)) & 31),
897 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000898 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)).Val;
Chris Lattner222adac2005-10-06 19:03:35 +0000899 }
Chris Lattner222adac2005-10-06 19:03:35 +0000900}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000901
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000902
Chris Lattnera5a91b12005-08-17 19:33:03 +0000903// Select - Convert the specified operand from a target-independent to a
904// target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +0000905SDNode *PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000906 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000907 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000908 N->getOpcode() < PPCISD::FIRST_NUMBER) {
909 Result = Op;
Evan Cheng64a752f2006-08-11 09:08:15 +0000910 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000911 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000912
Chris Lattnera5a91b12005-08-17 19:33:03 +0000913 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000914 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000915 case ISD::SETCC:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000916 return SelectSETCC(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000917 case PPCISD::GlobalBaseReg:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000918 return getGlobalBaseReg().Val;
Chris Lattner860e8862005-11-17 07:30:41 +0000919
Chris Lattnere28e40a2005-08-25 00:45:43 +0000920 case ISD::FrameIndex: {
921 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000922 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
923 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000924 if (N->hasOneUse())
925 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
926 getSmallIPtrImm(0)).Val;
927 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
928 getSmallIPtrImm(0));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000929 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000930
931 case PPCISD::MFCR: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000932 SDOperand InFlag = N->getOperand(1);
933 AddToISelQueue(InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000934 // Use MFOCRF if supported.
935 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000936 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
937 N->getOperand(0), InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000938 else
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000939 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000940 }
941
Chris Lattner88add102005-09-28 22:50:24 +0000942 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000943 // FIXME: since this depends on the setting of the carry flag from the srawi
944 // we should really be making notes about that for the scheduler.
945 // FIXME: It sure would be nice if we could cheaply recognize the
946 // srl/add/sra pattern the dag combiner will generate for this as
947 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000948 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000949 if (isInt32Immediate(N->getOperand(1), Imm)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000950 SDOperand N0 = N->getOperand(0);
951 AddToISelQueue(N0);
Chris Lattner8784a232005-08-25 17:50:06 +0000952 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000953 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000954 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000955 N0, getI32Imm(Log2_32(Imm)));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000956 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
957 SDOperand(Op, 0), SDOperand(Op, 1)).Val;
Chris Lattner8784a232005-08-25 17:50:06 +0000958 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000959 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000960 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000961 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000962 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000963 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
964 SDOperand(Op, 0), SDOperand(Op, 1)),
965 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000966 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT).Val;
Chris Lattner8784a232005-08-25 17:50:06 +0000967 }
968 }
Chris Lattner047b9522005-08-25 22:04:30 +0000969
Chris Lattner237733e2005-09-29 23:33:31 +0000970 // Other cases are autogenerated.
971 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000972 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000973 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000974 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000975 // If this is an and of a value rotated between 0 and 31 bits and then and'd
976 // with a mask, emit rlwinm
Chris Lattnerc08f9022006-06-27 00:04:13 +0000977 if (isInt32Immediate(N->getOperand(1), Imm) &&
978 (isShiftedMask_32(Imm) || isShiftedMask_32(~Imm))) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000979 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000980 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000981 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000982 Val = N->getOperand(0).getOperand(0);
983 AddToISelQueue(Val);
Chris Lattner3393e802005-10-25 19:32:37 +0000984 } else if (Imm == 0) {
985 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng6da2f322006-08-26 01:07:58 +0000986 AddToISelQueue(N->getOperand(1));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000987 ReplaceUses(SDOperand(N, 0), N->getOperand(1));
Evan Cheng64a752f2006-08-11 09:08:15 +0000988 return NULL;
Chris Lattner3393e802005-10-25 19:32:37 +0000989 } else {
Evan Cheng6da2f322006-08-26 01:07:58 +0000990 Val = N->getOperand(0);
991 AddToISelQueue(Val);
Nate Begemancffc32b2005-08-18 07:30:46 +0000992 isRunOfOnes(Imm, MB, ME);
993 SH = 0;
994 }
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000995 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
996 getI32Imm(SH), getI32Imm(MB),
997 getI32Imm(ME)).Val;
Nate Begemancffc32b2005-08-18 07:30:46 +0000998 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000999 // ISD::OR doesn't get all the bitfield insertion fun.
1000 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattnerc08f9022006-06-27 00:04:13 +00001001 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman50fb3c42005-12-24 01:00:15 +00001002 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001003 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001004 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001005 Imm = ~(Imm^Imm2);
1006 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001007 AddToISelQueue(N->getOperand(0).getOperand(0));
1008 AddToISelQueue(N->getOperand(0).getOperand(1));
1009 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1010 N->getOperand(0).getOperand(0),
1011 N->getOperand(0).getOperand(1),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001012 getI32Imm(0), getI32Imm(MB),getI32Imm(ME));
Nate Begeman50fb3c42005-12-24 01:00:15 +00001013 }
1014 }
Chris Lattner237733e2005-09-29 23:33:31 +00001015
1016 // Other cases are autogenerated.
1017 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001018 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001019 case ISD::OR:
Chris Lattnercccef1c2006-06-27 21:08:52 +00001020 if (N->getValueType(0) == MVT::i32)
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001021 if (SDNode *I = SelectBitfieldInsert(N))
1022 return I;
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001023
Chris Lattner237733e2005-09-29 23:33:31 +00001024 // Other cases are autogenerated.
1025 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001026 case ISD::SHL: {
1027 unsigned Imm, SH, MB, ME;
1028 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001029 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001030 AddToISelQueue(N->getOperand(0).getOperand(0));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001031 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Evan Cheng6da2f322006-08-26 01:07:58 +00001032 N->getOperand(0).getOperand(0),
1033 getI32Imm(SH), getI32Imm(MB),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001034 getI32Imm(ME)).Val;
Nate Begeman8d948322005-10-19 01:12:32 +00001035 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001036
1037 // Other cases are autogenerated.
1038 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001039 }
1040 case ISD::SRL: {
1041 unsigned Imm, SH, MB, ME;
1042 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001043 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001044 AddToISelQueue(N->getOperand(0).getOperand(0));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001045 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Evan Cheng6da2f322006-08-26 01:07:58 +00001046 N->getOperand(0).getOperand(0),
1047 getI32Imm(SH), getI32Imm(MB),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001048 getI32Imm(ME)).Val;
Nate Begeman8d948322005-10-19 01:12:32 +00001049 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001050
1051 // Other cases are autogenerated.
1052 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001053 }
Chris Lattner13794f52005-08-26 18:46:49 +00001054 case ISD::SELECT_CC: {
1055 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1056
Chris Lattnerc08f9022006-06-27 00:04:13 +00001057 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +00001058 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1059 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1060 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1061 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001062 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1063 // FIXME: Implement this optzn for PPC64.
1064 N->getValueType(0) == MVT::i32) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001065 AddToISelQueue(N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001066 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001067 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng6da2f322006-08-26 01:07:58 +00001068 N->getOperand(0), getI32Imm(~0U));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001069 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Evan Cheng6da2f322006-08-26 01:07:58 +00001070 SDOperand(Tmp, 0), N->getOperand(0),
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001071 SDOperand(Tmp, 1)).Val;
Chris Lattner13794f52005-08-26 18:46:49 +00001072 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001073
Chris Lattner50ff55c2005-09-01 19:20:44 +00001074 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001075 unsigned BROpc = getBCCForSetCC(CC);
1076
1077 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001078 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001079 if (N->getValueType(0) == MVT::i32)
1080 SelectCCOp = PPC::SELECT_CC_I4;
1081 else if (N->getValueType(0) == MVT::i64)
1082 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +00001083 else if (N->getValueType(0) == MVT::f32)
1084 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001085 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001086 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001087 else
1088 SelectCCOp = PPC::SELECT_CC_VRRC;
1089
Evan Cheng6da2f322006-08-26 01:07:58 +00001090 AddToISelQueue(N->getOperand(2));
1091 AddToISelQueue(N->getOperand(3));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001092 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
Evan Cheng6da2f322006-08-26 01:07:58 +00001093 N->getOperand(2), N->getOperand(3),
1094 getI32Imm(BROpc)).Val;
Chris Lattner13794f52005-08-26 18:46:49 +00001095 }
Nate Begeman81e80972006-03-17 01:40:33 +00001096 case ISD::BR_CC: {
Evan Cheng6da2f322006-08-26 01:07:58 +00001097 AddToISelQueue(N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001098 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1099 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001100 return CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1101 CondCode, getI32Imm(getBCCForSetCC(CC)),
Evan Cheng6da2f322006-08-26 01:07:58 +00001102 N->getOperand(4), N->getOperand(0)).Val;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001103 }
Nate Begeman37efe672006-04-22 18:53:45 +00001104 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001105 // FIXME: Should custom lower this.
Evan Cheng6da2f322006-08-26 01:07:58 +00001106 SDOperand Chain = N->getOperand(0);
1107 SDOperand Target = N->getOperand(1);
1108 AddToISelQueue(Chain);
1109 AddToISelQueue(Target);
Chris Lattner6b76b962006-06-27 20:46:17 +00001110 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1111 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman37efe672006-04-22 18:53:45 +00001112 Chain), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001113 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain).Val;
Nate Begeman37efe672006-04-22 18:53:45 +00001114 }
Chris Lattnercf006312006-06-10 01:15:02 +00001115 // FIXME: These are manually selected because tblgen isn't handling varargs
1116 // nodes correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001117 case PPCISD::BCTRL: return MySelect_PPCbctrl(Op);
1118 case PPCISD::CALL: return MySelect_PPCcall(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001119 }
Chris Lattner25dae722005-09-03 00:53:47 +00001120
Evan Cheng64a752f2006-08-11 09:08:15 +00001121 return SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001122}
1123
1124
Chris Lattnercf006312006-06-10 01:15:02 +00001125// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1126// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001127SDNode *PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +00001128 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001129
1130 bool hasFlag =
1131 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1132
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001133 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001134 // Push varargs arguments, including optional flag.
1135 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001136 Chain = N.getOperand(i);
1137 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001138 Ops.push_back(Chain);
1139 }
1140
Evan Cheng6da2f322006-08-26 01:07:58 +00001141 Chain = N.getOperand(0);
1142 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001143 Ops.push_back(Chain);
1144
1145 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001146 Chain = N.getOperand(N.getNumOperands()-1);
1147 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001148 Ops.push_back(Chain);
1149 }
1150
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001151 return CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag,
1152 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001153}
1154
1155// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1156// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001157SDNode *PPCDAGToDAGISel::MySelect_PPCcall(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +00001158 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001159 SDOperand N1(0, 0);
1160 SDOperand Tmp0(0, 0);
1161 SDNode *ResNode;
1162 Chain = N.getOperand(0);
1163 N1 = N.getOperand(1);
1164
1165 // Pattern: (PPCcall:void (imm:i32):$func)
1166 // Emits: (BLA:void (imm:i32):$func)
1167 // Pattern complexity = 4 cost = 1
1168 if (N1.getOpcode() == ISD::Constant) {
1169 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1170
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001171 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001172 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1173
1174 bool hasFlag =
1175 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1176
1177 // Push varargs arguments, not including optional flag.
1178 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001179 Chain = N.getOperand(i);
1180 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001181 Ops.push_back(Chain);
1182 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001183 Chain = N.getOperand(0);
1184 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001185 Ops.push_back(Chain);
1186 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001187 Chain = N.getOperand(N.getNumOperands()-1);
1188 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001189 Ops.push_back(Chain);
1190 }
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001191 return CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag,
1192 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001193 }
1194
1195 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1196 // Emits: (BL:void (tglobaladdr:i32):$dst)
1197 // Pattern complexity = 4 cost = 1
1198 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001199 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001200 Ops.push_back(N1);
1201
1202 bool hasFlag =
1203 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1204
1205 // Push varargs arguments, not including optional flag.
1206 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001207 Chain = N.getOperand(i);
1208 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001209 Ops.push_back(Chain);
1210 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001211 Chain = N.getOperand(0);
1212 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001213 Ops.push_back(Chain);
1214 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001215 Chain = N.getOperand(N.getNumOperands()-1);
1216 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001217 Ops.push_back(Chain);
1218 }
1219
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001220 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1221 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001222 }
1223
1224 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1225 // Emits: (BL:void (texternalsym:i32):$dst)
1226 // Pattern complexity = 4 cost = 1
1227 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1228 std::vector<SDOperand> Ops;
1229 Ops.push_back(N1);
1230
1231 bool hasFlag =
1232 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1233
1234 // Push varargs arguments, not including optional flag.
1235 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001236 Chain = N.getOperand(i);
1237 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001238 Ops.push_back(Chain);
1239 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001240 Chain = N.getOperand(0);
1241 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001242 Ops.push_back(Chain);
1243 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001244 Chain = N.getOperand(N.getNumOperands()-1);
1245 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001246 Ops.push_back(Chain);
1247 }
1248
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001249 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1250 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001251 }
1252 std::cerr << "Cannot yet select: ";
1253 N.Val->dump(CurDAG);
1254 std::cerr << '\n';
1255 abort();
Evan Cheng64a752f2006-08-11 09:08:15 +00001256
1257 return NULL;
Chris Lattnercf006312006-06-10 01:15:02 +00001258}
1259
1260
Nate Begeman1d9d7422005-10-18 00:28:58 +00001261/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001262/// PowerPC-specific DAG, ready for instruction scheduling.
1263///
Evan Chengc4c62572006-03-13 23:20:37 +00001264FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001265 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001266}
1267