blob: 823e3162191ea1629de8ad89af40fe46eeb976a9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for PowerPC,
11// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "ppc-codegen"
16#include "PPC.h"
17#include "PPCPredicates.h"
18#include "PPCTargetMachine.h"
19#include "PPCISelLowering.h"
20#include "PPCHazardRecognizers.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/Target/TargetOptions.h"
27#include "llvm/Constants.h"
Chris Lattner3ed055f2009-04-17 00:26:12 +000028#include "llvm/Function.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/GlobalValue.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
36namespace {
37 //===--------------------------------------------------------------------===//
38 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
39 /// instructions for SelectionDAG operations.
40 ///
41 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
42 PPCTargetMachine &TM;
Dan Gohmanf2b29572008-10-03 16:55:19 +000043 PPCTargetLowering &PPCLowering;
Evan Cheng9d99c5e2007-10-23 06:42:42 +000044 const PPCSubtarget &PPCSubTarget;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 unsigned GlobalBaseReg;
46 public:
Dan Gohmane887fdf2008-07-07 18:00:37 +000047 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman96eb47a2009-01-15 19:20:50 +000048 : SelectionDAGISel(tm), TM(tm),
Evan Cheng9d99c5e2007-10-23 06:42:42 +000049 PPCLowering(*TM.getTargetLowering()),
50 PPCSubTarget(*TM.getSubtargetImpl()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 virtual bool runOnFunction(Function &Fn) {
Chris Lattner3ed055f2009-04-17 00:26:12 +000053 // Do not codegen any 'available_externally' functions at all, they have
54 // definitions outside the translation unit.
55 if (Fn.hasAvailableExternallyLinkage())
56 return false;
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 // Make sure we re-emit a set of the global base reg if necessary
59 GlobalBaseReg = 0;
60 SelectionDAGISel::runOnFunction(Fn);
61
62 InsertVRSaveCode(Fn);
63 return true;
64 }
65
66 /// getI32Imm - Return a target constant with the specified value, of type
67 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +000068 inline SDValue getI32Imm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 return CurDAG->getTargetConstant(Imm, MVT::i32);
70 }
71
72 /// getI64Imm - Return a target constant with the specified value, of type
73 /// i64.
Dan Gohman8181bd12008-07-27 21:46:04 +000074 inline SDValue getI64Imm(uint64_t Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return CurDAG->getTargetConstant(Imm, MVT::i64);
76 }
77
78 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman8181bd12008-07-27 21:46:04 +000079 inline SDValue getSmallIPtrImm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
81 }
82
83 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
84 /// with any number of 0s on either side. The 1s are allowed to wrap from
85 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
86 /// 0x0F0F0000 is not, since all 1s are not contiguous.
87 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
88
89
90 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
91 /// rotate and mask opcode and mask operation.
92 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
93 unsigned &SH, unsigned &MB, unsigned &ME);
94
95 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
96 /// base register. Return the virtual register that holds this value.
97 SDNode *getGlobalBaseReg();
98
99 // Select - Convert the specified operand from a target-independent to a
100 // target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000101 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 SDNode *SelectBitfieldInsert(SDNode *N);
104
105 /// SelectCC - Select a comparison of the specified values with the
106 /// specified condition code, returning the CR# of the expression.
Dale Johannesen5d398a32009-02-06 19:16:40 +0000107 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109 /// SelectAddrImm - Returns true if the address N can be represented by
110 /// a base register plus a signed 16-bit displacement [r+imm].
Dan Gohman8181bd12008-07-27 21:46:04 +0000111 bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp,
112 SDValue &Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
114 }
115
116 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
117 /// immediate field. Because preinc imms have already been validated, just
118 /// accept it.
Dan Gohman8181bd12008-07-27 21:46:04 +0000119 bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 Out = N;
121 return true;
122 }
123
124 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
125 /// represented as an indexed [r+r] operation. Returns false if it can
126 /// be represented by [r+imm], which are preferred.
Dan Gohman8181bd12008-07-27 21:46:04 +0000127 bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base,
128 SDValue &Index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
130 }
131
132 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
133 /// represented as an indexed [r+r] operation.
Dan Gohman8181bd12008-07-27 21:46:04 +0000134 bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base,
135 SDValue &Index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
137 }
138
139 /// SelectAddrImmShift - Returns true if the address N can be represented by
140 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
141 /// for use by STD and friends.
Dan Gohman8181bd12008-07-27 21:46:04 +0000142 bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp,
143 SDValue &Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
145 }
146
147 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
148 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000149 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000151 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000152 SDValue Op0, Op1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 switch (ConstraintCode) {
154 default: return true;
155 case 'm': // memory
156 if (!SelectAddrIdx(Op, Op, Op0, Op1))
157 SelectAddrImm(Op, Op, Op0, Op1);
158 break;
159 case 'o': // offsetable
160 if (!SelectAddrImm(Op, Op, Op0, Op1)) {
161 Op0 = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 Op1 = getSmallIPtrImm(0);
163 }
164 break;
165 case 'v': // not offsetable
166 SelectAddrIdxOnly(Op, Op, Op0, Op1);
167 break;
168 }
169
170 OutOps.push_back(Op0);
171 OutOps.push_back(Op1);
172 return false;
173 }
174
Dan Gohman8181bd12008-07-27 21:46:04 +0000175 SDValue BuildSDIVSequence(SDNode *N);
176 SDValue BuildUDIVSequence(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
Evan Cheng34fd4f32008-06-30 20:45:06 +0000178 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000180 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 void InsertVRSaveCode(Function &Fn);
183
184 virtual const char *getPassName() const {
185 return "PowerPC DAG->DAG Pattern Instruction Selection";
186 }
187
188 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
189 /// this target when scheduling the DAG.
Dan Gohmandd6547d2009-01-15 22:18:12 +0000190 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 // Should use subtarget info to pick the right hazard recognizer. For
192 // now, always return a PPC970 recognizer.
Dan Gohman404e8542008-09-04 15:39:15 +0000193 const TargetInstrInfo *II = TM.getInstrInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 assert(II && "No InstrInfo?");
195 return new PPCHazardRecognizer970(*II);
196 }
197
198// Include the pieces autogenerated from the target description.
199#include "PPCGenDAGISel.inc"
200
201private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000202 SDNode *SelectSETCC(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 };
204}
205
Evan Cheng34fd4f32008-06-30 20:45:06 +0000206/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000208void PPCDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 DEBUG(BB->dump());
210
211 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000212 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +0000213 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214}
215
216/// InsertVRSaveCode - Once the entire function has been instruction selected,
217/// all virtual registers are created and all machine instructions are built,
218/// check to see if we need to save/restore VRSAVE. If so, do it.
219void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
220 // Check to see if this function uses vector registers, which means we have to
221 // save and restore the VRSAVE register and update it with the regs we use.
222 //
223 // In this case, there will be virtual registers of vector type type created
224 // by the scheduler. Detect them now.
225 MachineFunction &Fn = MachineFunction::get(&F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 bool HasVectorVReg = false;
Dan Gohman1e57df32008-02-10 18:45:23 +0000227 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +0000228 e = RegInfo->getLastVirtReg()+1; i != e; ++i)
229 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 HasVectorVReg = true;
231 break;
232 }
233 if (!HasVectorVReg) return; // nothing to do.
234
235 // If we have a vector register, we want to emit code into the entry and exit
236 // blocks to save and restore the VRSAVE register. We do this here (instead
237 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
238 //
239 // 1. This (trivially) reduces the load on the register allocator, by not
240 // having to represent the live range of the VRSAVE register.
241 // 2. This (more significantly) allows us to create a temporary virtual
242 // register to hold the saved VRSAVE value, allowing this temporary to be
243 // register allocated, instead of forcing it to be spilled to the stack.
244
245 // Create two vregs - one to hold the VRSAVE register that is live-in to the
246 // function and one for the value after having bits or'd into it.
Chris Lattner1b989192007-12-31 04:13:23 +0000247 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
248 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
250 const TargetInstrInfo &TII = *TM.getInstrInfo();
251 MachineBasicBlock &EntryBB = *Fn.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000252 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 // Emit the following code into the entry block:
254 // InVRSAVE = MFVRSAVE
255 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
256 // MTVRSAVE UpdatedVRSAVE
257 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000258 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
259 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattner62327602008-01-07 01:56:04 +0000260 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000261 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262
263 // Find all return blocks, outputting a restore in each epilog.
264 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Chris Lattner5b930372008-01-07 07:27:27 +0000265 if (!BB->empty() && BB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 IP = BB->end(); --IP;
267
268 // Skip over all terminator instructions, which are part of the return
269 // sequence.
270 MachineBasicBlock::iterator I2 = IP;
Chris Lattner5b930372008-01-07 07:27:27 +0000271 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 IP = I2;
273
274 // Emit: MTVRSAVE InVRSave
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000275 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 }
277 }
278}
279
280
281/// getGlobalBaseReg - Output the instructions required to put the
282/// base address to use for accessing globals into a register.
283///
284SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
285 if (!GlobalBaseReg) {
286 const TargetInstrInfo &TII = *TM.getInstrInfo();
287 // Insert the set of GlobalBaseReg into the first MBB of the function
288 MachineBasicBlock &FirstMBB = BB->getParent()->front();
289 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000290 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291
292 if (PPCLowering.getPointerTy() == MVT::i32) {
Chris Lattner1b989192007-12-31 04:13:23 +0000293 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000294 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR);
295 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000297 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000298 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8);
299 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 }
301 }
Gabor Greife9f7f582008-08-31 15:37:04 +0000302 return CurDAG->getRegister(GlobalBaseReg,
303 PPCLowering.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304}
305
306/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
307/// or 64-bit immediate, and if the value can be accurately represented as a
308/// sign extension from a 16-bit value. If so, this returns true and the
309/// immediate.
310static bool isIntS16Immediate(SDNode *N, short &Imm) {
311 if (N->getOpcode() != ISD::Constant)
312 return false;
313
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000314 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 if (N->getValueType(0) == MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000316 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000318 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319}
320
Dan Gohman8181bd12008-07-27 21:46:04 +0000321static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000322 return isIntS16Immediate(Op.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323}
324
325
326/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
327/// operand. If so Imm will receive the 32-bit value.
328static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
329 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000330 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 return true;
332 }
333 return false;
334}
335
336/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
337/// operand. If so Imm will receive the 64-bit value.
338static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
339 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000340 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 return true;
342 }
343 return false;
344}
345
346// isInt32Immediate - This method tests to see if a constant operand.
347// If so Imm will receive the 32 bit value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000348static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000349 return isInt32Immediate(N.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350}
351
352
353// isOpcWithIntImmediate - This method tests to see if the node is a specific
354// opcode and that it has a immediate integer right operand.
355// If so Imm will receive the 32 bit value.
356static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000357 return N->getOpcode() == Opc
358 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359}
360
361bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
362 if (isShiftedMask_32(Val)) {
363 // look for the first non-zero bit
364 MB = CountLeadingZeros_32(Val);
365 // look for the first zero bit after the run of ones
366 ME = CountLeadingZeros_32((Val - 1) ^ Val);
367 return true;
368 } else {
369 Val = ~Val; // invert mask
370 if (isShiftedMask_32(Val)) {
371 // effectively look for the first zero bit
372 ME = CountLeadingZeros_32(Val) - 1;
373 // effectively look for the first one bit after the run of zeros
374 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
375 return true;
376 }
377 }
378 // no run present
379 return false;
380}
381
382bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
383 bool IsShiftMask, unsigned &SH,
384 unsigned &MB, unsigned &ME) {
385 // Don't even go down this path for i64, since different logic will be
386 // necessary for rldicl/rldicr/rldimi.
387 if (N->getValueType(0) != MVT::i32)
388 return false;
389
390 unsigned Shift = 32;
391 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
392 unsigned Opcode = N->getOpcode();
393 if (N->getNumOperands() != 2 ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000394 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 return false;
396
397 if (Opcode == ISD::SHL) {
398 // apply shift left to mask if it comes first
399 if (IsShiftMask) Mask = Mask << Shift;
400 // determine which bits are made indeterminant by shift
401 Indeterminant = ~(0xFFFFFFFFu << Shift);
402 } else if (Opcode == ISD::SRL) {
403 // apply shift right to mask if it comes first
404 if (IsShiftMask) Mask = Mask >> Shift;
405 // determine which bits are made indeterminant by shift
406 Indeterminant = ~(0xFFFFFFFFu >> Shift);
407 // adjust for the left rotate
408 Shift = 32 - Shift;
409 } else if (Opcode == ISD::ROTL) {
410 Indeterminant = 0;
411 } else {
412 return false;
413 }
414
415 // if the mask doesn't intersect any Indeterminant bits
416 if (Mask && !(Mask & Indeterminant)) {
417 SH = Shift & 31;
418 // make sure the mask is still a mask (wrap arounds may not be)
419 return isRunOfOnes(Mask, MB, ME);
420 }
421 return false;
422}
423
424/// SelectBitfieldInsert - turn an or of two masked values into
425/// the rotate left word immediate then mask insert (rlwimi) instruction.
426SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000427 SDValue Op0 = N->getOperand(0);
428 SDValue Op1 = N->getOperand(1);
Dale Johannesen913ba762009-02-06 01:31:28 +0000429 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
Dan Gohman63f4e462008-02-27 01:23:58 +0000431 APInt LKZ, LKO, RKZ, RKO;
432 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
433 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
Dan Gohman63f4e462008-02-27 01:23:58 +0000435 unsigned TargetMask = LKZ.getZExtValue();
436 unsigned InsertMask = RKZ.getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
438 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
439 unsigned Op0Opc = Op0.getOpcode();
440 unsigned Op1Opc = Op1.getOpcode();
441 unsigned Value, SH = 0;
442 TargetMask = ~TargetMask;
443 InsertMask = ~InsertMask;
444
445 // If the LHS has a foldable shift and the RHS does not, then swap it to the
446 // RHS so that we can fold the shift into the insert.
447 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
448 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
449 Op0.getOperand(0).getOpcode() == ISD::SRL) {
450 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
451 Op1.getOperand(0).getOpcode() != ISD::SRL) {
452 std::swap(Op0, Op1);
453 std::swap(Op0Opc, Op1Opc);
454 std::swap(TargetMask, InsertMask);
455 }
456 }
457 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
458 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
459 Op1.getOperand(0).getOpcode() != ISD::SRL) {
460 std::swap(Op0, Op1);
461 std::swap(Op0Opc, Op1Opc);
462 std::swap(TargetMask, InsertMask);
463 }
464 }
465
466 unsigned MB, ME;
467 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000468 SDValue Tmp1, Tmp2, Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
470
471 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
472 isInt32Immediate(Op1.getOperand(1), Value)) {
473 Op1 = Op1.getOperand(0);
474 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
475 }
476 if (Op1Opc == ISD::AND) {
477 unsigned SHOpc = Op1.getOperand(0).getOpcode();
478 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
479 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
480 Op1 = Op1.getOperand(0).getOperand(0);
481 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
482 } else {
483 Op1 = Op1.getOperand(0);
484 }
485 }
486
487 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 SH &= 31;
Dan Gohman8181bd12008-07-27 21:46:04 +0000489 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 getI32Imm(ME) };
Dale Johannesen913ba762009-02-06 01:31:28 +0000491 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493 }
494 return 0;
495}
496
497/// SelectCC - Select a comparison of the specified values with the specified
498/// condition code, returning the CR# of the expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000499SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Dale Johannesen5d398a32009-02-06 19:16:40 +0000500 ISD::CondCode CC, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 // Always select the LHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 unsigned Opc;
503
504 if (LHS.getValueType() == MVT::i32) {
505 unsigned Imm;
506 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
507 if (isInt32Immediate(RHS, Imm)) {
508 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
509 if (isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000510 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 getI32Imm(Imm & 0xFFFF)), 0);
512 // If this is a 16-bit signed immediate, fold it.
513 if (isInt16((int)Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000514 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 getI32Imm(Imm & 0xFFFF)), 0);
516
517 // For non-equality comparisons, the default code would materialize the
518 // constant, then compare against it, like this:
519 // lis r2, 4660
520 // ori r2, r2, 22136
521 // cmpw cr0, r3, r2
522 // Since we are just comparing for equality, we can emit this instead:
523 // xoris r0,r3,0x1234
524 // cmplwi cr0,r0,0x5678
525 // beq cr0,L6
Dale Johannesen5d398a32009-02-06 19:16:40 +0000526 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 getI32Imm(Imm >> 16)), 0);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000528 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 getI32Imm(Imm & 0xFFFF)), 0);
530 }
531 Opc = PPC::CMPLW;
532 } else if (ISD::isUnsignedIntSetCC(CC)) {
533 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000534 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 getI32Imm(Imm & 0xFFFF)), 0);
536 Opc = PPC::CMPLW;
537 } else {
538 short SImm;
539 if (isIntS16Immediate(RHS, SImm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000540 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 getI32Imm((int)SImm & 0xFFFF)),
542 0);
543 Opc = PPC::CMPW;
544 }
545 } else if (LHS.getValueType() == MVT::i64) {
546 uint64_t Imm;
547 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000548 if (isInt64Immediate(RHS.getNode(), Imm)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
550 if (isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000551 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 getI32Imm(Imm & 0xFFFF)), 0);
553 // If this is a 16-bit signed immediate, fold it.
554 if (isInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000555 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 getI32Imm(Imm & 0xFFFF)), 0);
557
558 // For non-equality comparisons, the default code would materialize the
559 // constant, then compare against it, like this:
560 // lis r2, 4660
561 // ori r2, r2, 22136
562 // cmpd cr0, r3, r2
563 // Since we are just comparing for equality, we can emit this instead:
564 // xoris r0,r3,0x1234
565 // cmpldi cr0,r0,0x5678
566 // beq cr0,L6
567 if (isUInt32(Imm)) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000568 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 getI64Imm(Imm >> 16)), 0);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000570 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 getI64Imm(Imm & 0xFFFF)), 0);
572 }
573 }
574 Opc = PPC::CMPLD;
575 } else if (ISD::isUnsignedIntSetCC(CC)) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000576 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000577 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 getI64Imm(Imm & 0xFFFF)), 0);
579 Opc = PPC::CMPLD;
580 } else {
581 short SImm;
582 if (isIntS16Immediate(RHS, SImm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000583 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 getI64Imm(SImm & 0xFFFF)),
585 0);
586 Opc = PPC::CMPD;
587 }
588 } else if (LHS.getValueType() == MVT::f32) {
589 Opc = PPC::FCMPUS;
590 } else {
591 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
592 Opc = PPC::FCMPUD;
593 }
Dale Johannesen5d398a32009-02-06 19:16:40 +0000594 return SDValue(CurDAG->getTargetNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595}
596
597static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
598 switch (CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 case ISD::SETUEQ:
Dale Johannesen32100b22008-11-07 22:54:33 +0000600 case ISD::SETONE:
601 case ISD::SETOLE:
602 case ISD::SETOGE:
603 assert(0 && "Should be lowered by legalize!");
604 default: assert(0 && "Unknown condition!"); abort();
605 case ISD::SETOEQ:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 case ISD::SETEQ: return PPC::PRED_EQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 case ISD::SETUNE:
608 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000609 case ISD::SETOLT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 case ISD::SETLT: return PPC::PRED_LT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 case ISD::SETULE:
612 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000613 case ISD::SETOGT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 case ISD::SETGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 case ISD::SETUGE:
616 case ISD::SETGE: return PPC::PRED_GE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 case ISD::SETO: return PPC::PRED_NU;
618 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen32100b22008-11-07 22:54:33 +0000619 // These two are invalid for floating point. Assume we have int.
620 case ISD::SETULT: return PPC::PRED_LT;
621 case ISD::SETUGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 }
623}
624
625/// getCRIdxForSetCC - Return the index of the condition register field
626/// associated with the SetCC condition, and whether or not the field is
627/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Chris Lattner6c36fb52008-01-08 06:46:30 +0000628///
629/// If this returns with Other != -1, then the returned comparison is an or of
630/// two simpler comparisons. In this case, Invert is guaranteed to be false.
631static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
632 Invert = false;
633 Other = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 switch (CC) {
635 default: assert(0 && "Unknown condition!"); abort();
Chris Lattner6c36fb52008-01-08 06:46:30 +0000636 case ISD::SETOLT:
637 case ISD::SETLT: return 0; // Bit #0 = SETOLT
638 case ISD::SETOGT:
639 case ISD::SETGT: return 1; // Bit #1 = SETOGT
640 case ISD::SETOEQ:
641 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
642 case ISD::SETUO: return 3; // Bit #3 = SETUO
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 case ISD::SETUGE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000644 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 case ISD::SETULE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000646 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 case ISD::SETUNE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000648 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
649 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Dale Johannesen32100b22008-11-07 22:54:33 +0000650 case ISD::SETUEQ:
651 case ISD::SETOGE:
652 case ISD::SETOLE:
653 case ISD::SETONE:
654 assert(0 && "Invalid branch code: should be expanded by legalize");
655 // These are invalid for floating point. Assume integer.
656 case ISD::SETULT: return 0;
657 case ISD::SETUGT: return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 }
659 return 0;
660}
661
Dan Gohman8181bd12008-07-27 21:46:04 +0000662SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000663 SDNode *N = Op.getNode();
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000664 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 unsigned Imm;
666 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
667 if (isInt32Immediate(N->getOperand(1), Imm)) {
668 // We can codegen setcc op, imm very efficiently compared to a brcond.
669 // Check for those cases here.
670 // setcc op, 0
671 if (Imm == 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000672 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 switch (CC) {
674 default: break;
675 case ISD::SETEQ: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000676 Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000677 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
679 }
680 case ISD::SETNE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000681 SDValue AD =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000682 SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 Op, getI32Imm(~0U)), 0);
684 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
685 AD.getValue(1));
686 }
687 case ISD::SETLT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000688 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
690 }
691 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000692 SDValue T =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000693 SDValue(CurDAG->getTargetNode(PPC::NEG, dl, MVT::i32, Op), 0);
694 T = SDValue(CurDAG->getTargetNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000695 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
697 }
698 }
699 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman8181bd12008-07-27 21:46:04 +0000700 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 switch (CC) {
702 default: break;
703 case ISD::SETEQ:
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000704 Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 Op, getI32Imm(1)), 0);
706 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000707 SDValue(CurDAG->getTargetNode(PPC::LI, dl,
708 MVT::i32,
709 getI32Imm(0)), 0),
710 Op.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 case ISD::SETNE: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000712 Op = SDValue(CurDAG->getTargetNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
713 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 Op, getI32Imm(~0U));
Dan Gohman8181bd12008-07-27 21:46:04 +0000715 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
716 Op, SDValue(AD, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 }
718 case ISD::SETLT: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000719 SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, dl, MVT::i32, Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 getI32Imm(1)), 0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000721 SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, dl, MVT::i32, AD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000723 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
725 }
726 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000727 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000728 Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4),
729 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
731 getI32Imm(1));
732 }
733 }
734 }
735 }
736
737 bool Inv;
Chris Lattner6c36fb52008-01-08 06:46:30 +0000738 int OtherCondIdx;
739 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000740 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000741 SDValue IntCR;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742
743 // Force the ccreg into CR7.
Dan Gohman8181bd12008-07-27 21:46:04 +0000744 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745
Dan Gohman8181bd12008-07-27 21:46:04 +0000746 SDValue InFlag(0, 0); // Null incoming flag value.
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000747 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 InFlag).getValue(1);
749
Chris Lattner6c36fb52008-01-08 06:46:30 +0000750 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000751 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 CCReg), 0);
753 else
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000754 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, CCReg), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755
Dan Gohman8181bd12008-07-27 21:46:04 +0000756 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 getI32Imm(31), getI32Imm(31) };
Chris Lattner6c36fb52008-01-08 06:46:30 +0000758 if (OtherCondIdx == -1 && !Inv)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000760
761 // Get the specified bit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000762 SDValue Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000763 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000764 if (Inv) {
765 assert(OtherCondIdx == -1 && "Can't have split plus negation");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
767 }
Chris Lattner6c36fb52008-01-08 06:46:30 +0000768
769 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
770 // We already got the bit for the first part of the comparison (e.g. SETULE).
771
772 // Get the other bit of the comparison.
773 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
Dan Gohman8181bd12008-07-27 21:46:04 +0000774 SDValue OtherCond =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000775 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000776
777 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778}
779
780
781// Select - Convert the specified operand from a target-independent to a
782// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000783SDNode *PPCDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000784 SDNode *N = Op.getNode();
Dale Johannesen913ba762009-02-06 01:31:28 +0000785 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000786 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 return NULL; // Already selected.
788
789 switch (N->getOpcode()) {
790 default: break;
791
792 case ISD::Constant: {
793 if (N->getValueType(0) == MVT::i64) {
794 // Get 64 bit value.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000795 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 // Assume no remaining bits.
797 unsigned Remainder = 0;
798 // Assume no shift required.
799 unsigned Shift = 0;
800
801 // If it can't be represented as a 32 bit value.
802 if (!isInt32(Imm)) {
803 Shift = CountTrailingZeros_64(Imm);
804 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
805
806 // If the shifted value fits 32 bits.
807 if (isInt32(ImmSh)) {
808 // Go with the shifted value.
809 Imm = ImmSh;
810 } else {
811 // Still stuck with a 64 bit value.
812 Remainder = Imm;
813 Shift = 32;
814 Imm >>= 32;
815 }
816 }
817
818 // Intermediate operand.
819 SDNode *Result;
820
821 // Handle first 32 bits.
822 unsigned Lo = Imm & 0xFFFF;
823 unsigned Hi = (Imm >> 16) & 0xFFFF;
824
825 // Simple value.
826 if (isInt16(Imm)) {
827 // Just the Lo bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000828 Result = CurDAG->getTargetNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 } else if (Lo) {
830 // Handle the Hi bits.
831 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dale Johannesen913ba762009-02-06 01:31:28 +0000832 Result = CurDAG->getTargetNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 // And Lo bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000834 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000835 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 } else {
837 // Just the Hi bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000838 Result = CurDAG->getTargetNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 }
840
841 // If no shift, we're done.
842 if (!Shift) return Result;
843
844 // Shift for next step if the upper 32-bits were not zero.
845 if (Imm) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000846 Result = CurDAG->getTargetNode(PPC::RLDICR, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000847 SDValue(Result, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 getI32Imm(Shift), getI32Imm(63 - Shift));
849 }
850
851 // Add in the last bits as required.
852 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000853 Result = CurDAG->getTargetNode(PPC::ORIS8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000854 SDValue(Result, 0), getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 }
856 if ((Lo = Remainder & 0xFFFF)) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000857 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 }
860
861 return Result;
862 }
863 break;
864 }
865
866 case ISD::SETCC:
867 return SelectSETCC(Op);
868 case PPCISD::GlobalBaseReg:
869 return getGlobalBaseReg();
870
871 case ISD::FrameIndex: {
872 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman8181bd12008-07-27 21:46:04 +0000873 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
875 if (N->hasOneUse())
876 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
877 getSmallIPtrImm(0));
Dale Johannesen913ba762009-02-06 01:31:28 +0000878 return CurDAG->getTargetNode(Opc, dl, Op.getValueType(), TFI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 getSmallIPtrImm(0));
880 }
881
882 case PPCISD::MFCR: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000883 SDValue InFlag = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 // Use MFOCRF if supported.
Evan Cheng9d99c5e2007-10-23 06:42:42 +0000885 if (PPCSubTarget.isGigaProcessor())
Dale Johannesen913ba762009-02-06 01:31:28 +0000886 return CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 N->getOperand(0), InFlag);
888 else
Dale Johannesen913ba762009-02-06 01:31:28 +0000889 return CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 }
891
892 case ISD::SDIV: {
893 // FIXME: since this depends on the setting of the carry flag from the srawi
894 // we should really be making notes about that for the scheduler.
895 // FIXME: It sure would be nice if we could cheaply recognize the
896 // srl/add/sra pattern the dag combiner will generate for this as
897 // sra/addze rather than having to handle sdiv ourselves. oh well.
898 unsigned Imm;
899 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000900 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
902 SDNode *Op =
Dale Johannesen913ba762009-02-06 01:31:28 +0000903 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 N0, getI32Imm(Log2_32(Imm)));
905 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +0000906 SDValue(Op, 0), SDValue(Op, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
908 SDNode *Op =
Dale Johannesen913ba762009-02-06 01:31:28 +0000909 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman8181bd12008-07-27 21:46:04 +0000911 SDValue PT =
Dale Johannesen913ba762009-02-06 01:31:28 +0000912 SDValue(CurDAG->getTargetNode(PPC::ADDZE, dl, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +0000913 SDValue(Op, 0), SDValue(Op, 1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 0);
915 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
916 }
917 }
918
919 // Other cases are autogenerated.
920 break;
921 }
922
923 case ISD::LOAD: {
924 // Handle preincrement loads.
925 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands92c43912008-06-06 12:08:01 +0000926 MVT LoadedVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927
928 // Normal loads are handled by code generated from the .td file.
929 if (LD->getAddressingMode() != ISD::PRE_INC)
930 break;
931
Dan Gohman8181bd12008-07-27 21:46:04 +0000932 SDValue Offset = LD->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 if (isa<ConstantSDNode>(Offset) ||
934 Offset.getOpcode() == ISD::TargetGlobalAddress) {
935
936 unsigned Opcode;
937 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
938 if (LD->getValueType(0) != MVT::i64) {
939 // Handle PPC32 integer and normal FP loads.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000940 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands92c43912008-06-06 12:08:01 +0000941 switch (LoadedVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 default: assert(0 && "Invalid PPC load type!");
943 case MVT::f64: Opcode = PPC::LFDU; break;
944 case MVT::f32: Opcode = PPC::LFSU; break;
945 case MVT::i32: Opcode = PPC::LWZU; break;
946 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
947 case MVT::i1:
948 case MVT::i8: Opcode = PPC::LBZU; break;
949 }
950 } else {
951 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000952 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands92c43912008-06-06 12:08:01 +0000953 switch (LoadedVT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 default: assert(0 && "Invalid PPC load type!");
955 case MVT::i64: Opcode = PPC::LDU; break;
956 case MVT::i32: Opcode = PPC::LWZU8; break;
957 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
958 case MVT::i1:
959 case MVT::i8: Opcode = PPC::LBZU8; break;
960 }
961 }
962
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SDValue Chain = LD->getChain();
964 SDValue Base = LD->getBasePtr();
Dan Gohman8181bd12008-07-27 21:46:04 +0000965 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 // FIXME: PPC64
Dale Johannesen913ba762009-02-06 01:31:28 +0000967 return CurDAG->getTargetNode(Opcode, dl, LD->getValueType(0),
Dan Gohmanbd68c792008-07-17 19:10:17 +0000968 PPCLowering.getPointerTy(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 MVT::Other, Ops, 3);
970 } else {
971 assert(0 && "R+R preindex loads not supported yet!");
972 }
973 }
974
975 case ISD::AND: {
976 unsigned Imm, Imm2, SH, MB, ME;
977
978 // If this is an and of a value rotated between 0 and 31 bits and then and'd
979 // with a mask, emit rlwinm
980 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000981 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000982 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000983 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
985 }
986 // If this is just a masked value where the input is not handled above, and
987 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
988 if (isInt32Immediate(N->getOperand(1), Imm) &&
989 isRunOfOnes(Imm, MB, ME) &&
990 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000991 SDValue Val = N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000992 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
994 }
995 // AND X, 0 -> 0, not "rlwinm 32".
996 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000997 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 return NULL;
999 }
1000 // ISD::OR doesn't get all the bitfield insertion fun.
1001 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1002 if (isInt32Immediate(N->getOperand(1), Imm) &&
1003 N->getOperand(0).getOpcode() == ISD::OR &&
1004 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1005 unsigned MB, ME;
1006 Imm = ~(Imm^Imm2);
1007 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 N->getOperand(0).getOperand(1),
1010 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Dale Johannesen913ba762009-02-06 01:31:28 +00001011 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 }
1013 }
1014
1015 // Other cases are autogenerated.
1016 break;
1017 }
1018 case ISD::OR:
1019 if (N->getValueType(0) == MVT::i32)
1020 if (SDNode *I = SelectBitfieldInsert(N))
1021 return I;
1022
1023 // Other cases are autogenerated.
1024 break;
1025 case ISD::SHL: {
1026 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001027 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001029 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1031 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1032 }
1033
1034 // Other cases are autogenerated.
1035 break;
1036 }
1037 case ISD::SRL: {
1038 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001039 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001041 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1043 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1044 }
1045
1046 // Other cases are autogenerated.
1047 break;
1048 }
1049 case ISD::SELECT_CC: {
1050 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1051
1052 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1053 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1054 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1055 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1056 if (N1C->isNullValue() && N3C->isNullValue() &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001057 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 // FIXME: Implement this optzn for PPC64.
1059 N->getValueType(0) == MVT::i32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 SDNode *Tmp =
Dale Johannesen913ba762009-02-06 01:31:28 +00001061 CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 N->getOperand(0), getI32Imm(~0U));
1063 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +00001064 SDValue(Tmp, 0), N->getOperand(0),
1065 SDValue(Tmp, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 }
1067
Dale Johannesen5d398a32009-02-06 19:16:40 +00001068 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 unsigned BROpc = getPredicateForSetCC(CC);
1070
1071 unsigned SelectCCOp;
1072 if (N->getValueType(0) == MVT::i32)
1073 SelectCCOp = PPC::SELECT_CC_I4;
1074 else if (N->getValueType(0) == MVT::i64)
1075 SelectCCOp = PPC::SELECT_CC_I8;
1076 else if (N->getValueType(0) == MVT::f32)
1077 SelectCCOp = PPC::SELECT_CC_F4;
1078 else if (N->getValueType(0) == MVT::f64)
1079 SelectCCOp = PPC::SELECT_CC_F8;
1080 else
1081 SelectCCOp = PPC::SELECT_CC_VRRC;
1082
Dan Gohman8181bd12008-07-27 21:46:04 +00001083 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 getI32Imm(BROpc) };
1085 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1086 }
1087 case PPCISD::COND_BRANCH: {
Dan Gohmana1fb67a2008-11-05 17:16:24 +00001088 // Op #0 is the Chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 // Op #1 is the PPC::PRED_* number.
1090 // Op #2 is the CR#
1091 // Op #3 is the Dest MBB
Dan Gohmancc3df852008-11-05 04:14:16 +00001092 // Op #4 is the Flag.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman8181bd12008-07-27 21:46:04 +00001094 SDValue Pred =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001095 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00001096 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 N->getOperand(0), N->getOperand(4) };
1098 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1099 }
1100 case ISD::BR_CC: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesen5d398a32009-02-06 19:16:40 +00001102 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +00001103 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104 N->getOperand(4), N->getOperand(0) };
1105 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1106 }
1107 case ISD::BRIND: {
1108 // FIXME: Should custom lower this.
Dan Gohman8181bd12008-07-27 21:46:04 +00001109 SDValue Chain = N->getOperand(0);
1110 SDValue Target = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Dale Johannesen913ba762009-02-06 01:31:28 +00001112 Chain = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Target,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001113 Chain), 0);
1114 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1115 }
Evan Chengb6facc42009-01-16 22:57:32 +00001116 case ISD::DECLARE: {
1117 SDValue Chain = N->getOperand(0);
1118 SDValue N1 = N->getOperand(1);
1119 SDValue N2 = N->getOperand(2);
1120 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattnerebf925e2009-02-12 17:37:15 +00001121
1122 // FIXME: We need to handle this for VLAs.
1123 if (!FINode) {
1124 ReplaceUses(Op.getValue(0), Chain);
1125 return NULL;
1126 }
1127
Evan Cheng27cec742009-01-19 18:31:51 +00001128 if (N2.getOpcode() == ISD::ADD) {
1129 if (N2.getOperand(0).getOpcode() == ISD::ADD &&
1130 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
1131 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Hi &&
1132 N2.getOperand(1).getOpcode() == PPCISD::Lo)
1133 N2 = N2.getOperand(0).getOperand(1).getOperand(0);
1134 else if (N2.getOperand(0).getOpcode() == ISD::ADD &&
Evan Chenga7482db2009-01-19 18:57:29 +00001135 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
1136 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Lo &&
Evan Cheng27cec742009-01-19 18:31:51 +00001137 N2.getOperand(1).getOpcode() == PPCISD::Hi)
1138 N2 = N2.getOperand(0).getOperand(1).getOperand(0);
1139 else if (N2.getOperand(0).getOpcode() == PPCISD::Hi &&
1140 N2.getOperand(1).getOpcode() == PPCISD::Lo)
1141 N2 = N2.getOperand(0).getOperand(0);
1142 }
Chris Lattnerebf925e2009-02-12 17:37:15 +00001143
1144 // If we don't have a global address here, the debug info is mangled, just
1145 // drop it.
1146 if (!isa<GlobalAddressSDNode>(N2)) {
1147 ReplaceUses(Op.getValue(0), Chain);
1148 return NULL;
1149 }
Evan Chengb6facc42009-01-16 22:57:32 +00001150 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1151 GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();
1152 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1153 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
1154 return CurDAG->SelectNodeTo(N, TargetInstrInfo::DECLARE,
1155 MVT::Other, Tmp1, Tmp2, Chain);
1156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 }
1158
1159 return SelectCode(Op);
1160}
1161
1162
1163
1164/// createPPCISelDag - This pass converts a legalized DAG into a
1165/// PowerPC-specific DAG, ready for instruction scheduling.
1166///
1167FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1168 return new PPCDAGToDAGISel(TM);
1169}
1170