blob: a2e185009536d0e8609db5656ea53eb64c8ee780 [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"
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000023#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Chris Lattner1b989192007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/Target/TargetOptions.h"
28#include "llvm/Constants.h"
Chris Lattner3ed055f2009-04-17 00:26:12 +000029#include "llvm/Function.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/GlobalValue.h"
31#include "llvm/Intrinsics.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/MathExtras.h"
34#include "llvm/Support/Compiler.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
39namespace {
40 //===--------------------------------------------------------------------===//
41 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
42 /// instructions for SelectionDAG operations.
43 ///
Nick Lewycky492d06e2009-10-25 06:33:48 +000044 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 PPCTargetMachine &TM;
Dan Gohmanf2b29572008-10-03 16:55:19 +000046 PPCTargetLowering &PPCLowering;
Evan Cheng9d99c5e2007-10-23 06:42:42 +000047 const PPCSubtarget &PPCSubTarget;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 unsigned GlobalBaseReg;
49 public:
Dan Gohmane887fdf2008-07-07 18:00:37 +000050 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman96eb47a2009-01-15 19:20:50 +000051 : SelectionDAGISel(tm), TM(tm),
Evan Cheng9d99c5e2007-10-23 06:42:42 +000052 PPCLowering(*TM.getTargetLowering()),
53 PPCSubTarget(*TM.getSubtargetImpl()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000055 virtual bool runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 // Make sure we re-emit a set of the global base reg if necessary
57 GlobalBaseReg = 0;
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000058 SelectionDAGISel::runOnMachineFunction(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000060 InsertVRSaveCode(MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 return true;
62 }
63
64 /// getI32Imm - Return a target constant with the specified value, of type
65 /// i32.
Dan Gohman8181bd12008-07-27 21:46:04 +000066 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000067 return CurDAG->getTargetConstant(Imm, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 }
69
70 /// getI64Imm - Return a target constant with the specified value, of type
71 /// i64.
Dan Gohman8181bd12008-07-27 21:46:04 +000072 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000073 return CurDAG->getTargetConstant(Imm, MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 }
75
76 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman8181bd12008-07-27 21:46:04 +000077 inline SDValue getSmallIPtrImm(unsigned Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
79 }
80
81 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
82 /// with any number of 0s on either side. The 1s are allowed to wrap from
83 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
84 /// 0x0F0F0000 is not, since all 1s are not contiguous.
85 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
86
87
88 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
89 /// rotate and mask opcode and mask operation.
90 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
91 unsigned &SH, unsigned &MB, unsigned &ME);
92
93 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
94 /// base register. Return the virtual register that holds this value.
95 SDNode *getGlobalBaseReg();
96
97 // Select - Convert the specified operand from a target-independent to a
98 // target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +000099 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 SDNode *SelectBitfieldInsert(SDNode *N);
102
103 /// SelectCC - Select a comparison of the specified values with the
104 /// specified condition code, returning the CR# of the expression.
Dale Johannesen5d398a32009-02-06 19:16:40 +0000105 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 /// SelectAddrImm - Returns true if the address N can be represented by
108 /// a base register plus a signed 16-bit displacement [r+imm].
Dan Gohman8181bd12008-07-27 21:46:04 +0000109 bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp,
110 SDValue &Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
112 }
113
114 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
115 /// immediate field. Because preinc imms have already been validated, just
116 /// accept it.
Dan Gohman8181bd12008-07-27 21:46:04 +0000117 bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 Out = N;
119 return true;
120 }
121
122 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
123 /// represented as an indexed [r+r] operation. Returns false if it can
124 /// be represented by [r+imm], which are preferred.
Dan Gohman8181bd12008-07-27 21:46:04 +0000125 bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base,
126 SDValue &Index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
128 }
129
130 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
131 /// represented as an indexed [r+r] operation.
Dan Gohman8181bd12008-07-27 21:46:04 +0000132 bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base,
133 SDValue &Index) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
135 }
136
137 /// SelectAddrImmShift - Returns true if the address N can be represented by
138 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
139 /// for use by STD and friends.
Dan Gohman8181bd12008-07-27 21:46:04 +0000140 bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp,
141 SDValue &Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
143 }
144
145 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000146 /// inline asm expressions. It is always correct to compute the value into
147 /// a register. The case of adding a (possibly relocatable) constant to a
148 /// register can be improved, but it is wrong to substitute Reg+Reg for
149 /// Reg in an asm, because the load or store opcode would have to change.
150 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000152 std::vector<SDValue> &OutOps) {
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000153 OutOps.push_back(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 return false;
155 }
156
Dan Gohman8181bd12008-07-27 21:46:04 +0000157 SDValue BuildSDIVSequence(SDNode *N);
158 SDValue BuildUDIVSequence(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Evan Cheng34fd4f32008-06-30 20:45:06 +0000160 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000162 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000164 void InsertVRSaveCode(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166 virtual const char *getPassName() const {
167 return "PowerPC DAG->DAG Pattern Instruction Selection";
168 }
169
170 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
171 /// this target when scheduling the DAG.
Dan Gohmandd6547d2009-01-15 22:18:12 +0000172 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // Should use subtarget info to pick the right hazard recognizer. For
174 // now, always return a PPC970 recognizer.
Dan Gohman404e8542008-09-04 15:39:15 +0000175 const TargetInstrInfo *II = TM.getInstrInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 assert(II && "No InstrInfo?");
177 return new PPCHazardRecognizer970(*II);
178 }
179
180// Include the pieces autogenerated from the target description.
181#include "PPCGenDAGISel.inc"
182
183private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 SDNode *SelectSETCC(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 };
186}
187
Evan Cheng34fd4f32008-06-30 20:45:06 +0000188/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000190void PPCDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 DEBUG(BB->dump());
192
193 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000194 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +0000195 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196}
197
198/// InsertVRSaveCode - Once the entire function has been instruction selected,
199/// all virtual registers are created and all machine instructions are built,
200/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000201void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 // Check to see if this function uses vector registers, which means we have to
203 // save and restore the VRSAVE register and update it with the regs we use.
204 //
205 // In this case, there will be virtual registers of vector type type created
206 // by the scheduler. Detect them now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 bool HasVectorVReg = false;
Dan Gohman1e57df32008-02-10 18:45:23 +0000208 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +0000209 e = RegInfo->getLastVirtReg()+1; i != e; ++i)
210 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 HasVectorVReg = true;
212 break;
213 }
214 if (!HasVectorVReg) return; // nothing to do.
215
216 // If we have a vector register, we want to emit code into the entry and exit
217 // blocks to save and restore the VRSAVE register. We do this here (instead
218 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
219 //
220 // 1. This (trivially) reduces the load on the register allocator, by not
221 // having to represent the live range of the VRSAVE register.
222 // 2. This (more significantly) allows us to create a temporary virtual
223 // register to hold the saved VRSAVE value, allowing this temporary to be
224 // register allocated, instead of forcing it to be spilled to the stack.
225
226 // Create two vregs - one to hold the VRSAVE register that is live-in to the
227 // function and one for the value after having bits or'd into it.
Chris Lattner1b989192007-12-31 04:13:23 +0000228 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
229 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230
231 const TargetInstrInfo &TII = *TM.getInstrInfo();
232 MachineBasicBlock &EntryBB = *Fn.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000233 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 // Emit the following code into the entry block:
235 // InVRSAVE = MFVRSAVE
236 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
237 // MTVRSAVE UpdatedVRSAVE
238 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000239 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
240 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattner62327602008-01-07 01:56:04 +0000241 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000242 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
244 // Find all return blocks, outputting a restore in each epilog.
245 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Chris Lattner5b930372008-01-07 07:27:27 +0000246 if (!BB->empty() && BB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 IP = BB->end(); --IP;
248
249 // Skip over all terminator instructions, which are part of the return
250 // sequence.
251 MachineBasicBlock::iterator I2 = IP;
Chris Lattner5b930372008-01-07 07:27:27 +0000252 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 IP = I2;
254
255 // Emit: MTVRSAVE InVRSave
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000256 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 }
258 }
259}
260
261
262/// getGlobalBaseReg - Output the instructions required to put the
263/// base address to use for accessing globals into a register.
264///
265SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
266 if (!GlobalBaseReg) {
267 const TargetInstrInfo &TII = *TM.getInstrInfo();
268 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohman40660072009-08-15 02:07:36 +0000269 MachineBasicBlock &FirstMBB = MF->front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000271 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000273 if (PPCLowering.getPointerTy() == MVT::i32) {
Chris Lattner1b989192007-12-31 04:13:23 +0000274 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000275 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR);
276 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000278 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000279 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8);
280 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 }
282 }
Gabor Greife9f7f582008-08-31 15:37:04 +0000283 return CurDAG->getRegister(GlobalBaseReg,
284 PPCLowering.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285}
286
287/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
288/// or 64-bit immediate, and if the value can be accurately represented as a
289/// sign extension from a 16-bit value. If so, this returns true and the
290/// immediate.
291static bool isIntS16Immediate(SDNode *N, short &Imm) {
292 if (N->getOpcode() != ISD::Constant)
293 return false;
294
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000295 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000296 if (N->getValueType(0) == MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000297 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000299 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300}
301
Dan Gohman8181bd12008-07-27 21:46:04 +0000302static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000303 return isIntS16Immediate(Op.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304}
305
306
307/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
308/// operand. If so Imm will receive the 32-bit value.
309static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000310 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000311 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 return true;
313 }
314 return false;
315}
316
317/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
318/// operand. If so Imm will receive the 64-bit value.
319static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000320 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000321 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 return true;
323 }
324 return false;
325}
326
327// isInt32Immediate - This method tests to see if a constant operand.
328// If so Imm will receive the 32 bit value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000329static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000330 return isInt32Immediate(N.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331}
332
333
334// isOpcWithIntImmediate - This method tests to see if the node is a specific
335// opcode and that it has a immediate integer right operand.
336// If so Imm will receive the 32 bit value.
337static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000338 return N->getOpcode() == Opc
339 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340}
341
342bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
343 if (isShiftedMask_32(Val)) {
344 // look for the first non-zero bit
345 MB = CountLeadingZeros_32(Val);
346 // look for the first zero bit after the run of ones
347 ME = CountLeadingZeros_32((Val - 1) ^ Val);
348 return true;
349 } else {
350 Val = ~Val; // invert mask
351 if (isShiftedMask_32(Val)) {
352 // effectively look for the first zero bit
353 ME = CountLeadingZeros_32(Val) - 1;
354 // effectively look for the first one bit after the run of zeros
355 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
356 return true;
357 }
358 }
359 // no run present
360 return false;
361}
362
363bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
364 bool IsShiftMask, unsigned &SH,
365 unsigned &MB, unsigned &ME) {
366 // Don't even go down this path for i64, since different logic will be
367 // necessary for rldicl/rldicr/rldimi.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000368 if (N->getValueType(0) != MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 return false;
370
371 unsigned Shift = 32;
372 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
373 unsigned Opcode = N->getOpcode();
374 if (N->getNumOperands() != 2 ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000375 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 return false;
377
378 if (Opcode == ISD::SHL) {
379 // apply shift left to mask if it comes first
380 if (IsShiftMask) Mask = Mask << Shift;
381 // determine which bits are made indeterminant by shift
382 Indeterminant = ~(0xFFFFFFFFu << Shift);
383 } else if (Opcode == ISD::SRL) {
384 // apply shift right to mask if it comes first
385 if (IsShiftMask) Mask = Mask >> Shift;
386 // determine which bits are made indeterminant by shift
387 Indeterminant = ~(0xFFFFFFFFu >> Shift);
388 // adjust for the left rotate
389 Shift = 32 - Shift;
390 } else if (Opcode == ISD::ROTL) {
391 Indeterminant = 0;
392 } else {
393 return false;
394 }
395
396 // if the mask doesn't intersect any Indeterminant bits
397 if (Mask && !(Mask & Indeterminant)) {
398 SH = Shift & 31;
399 // make sure the mask is still a mask (wrap arounds may not be)
400 return isRunOfOnes(Mask, MB, ME);
401 }
402 return false;
403}
404
405/// SelectBitfieldInsert - turn an or of two masked values into
406/// the rotate left word immediate then mask insert (rlwimi) instruction.
407SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000408 SDValue Op0 = N->getOperand(0);
409 SDValue Op1 = N->getOperand(1);
Dale Johannesen913ba762009-02-06 01:31:28 +0000410 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411
Dan Gohman63f4e462008-02-27 01:23:58 +0000412 APInt LKZ, LKO, RKZ, RKO;
413 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
414 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415
Dan Gohman63f4e462008-02-27 01:23:58 +0000416 unsigned TargetMask = LKZ.getZExtValue();
417 unsigned InsertMask = RKZ.getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418
419 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
420 unsigned Op0Opc = Op0.getOpcode();
421 unsigned Op1Opc = Op1.getOpcode();
422 unsigned Value, SH = 0;
423 TargetMask = ~TargetMask;
424 InsertMask = ~InsertMask;
425
426 // If the LHS has a foldable shift and the RHS does not, then swap it to the
427 // RHS so that we can fold the shift into the insert.
428 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
429 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
430 Op0.getOperand(0).getOpcode() == ISD::SRL) {
431 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
432 Op1.getOperand(0).getOpcode() != ISD::SRL) {
433 std::swap(Op0, Op1);
434 std::swap(Op0Opc, Op1Opc);
435 std::swap(TargetMask, InsertMask);
436 }
437 }
438 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
439 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
440 Op1.getOperand(0).getOpcode() != ISD::SRL) {
441 std::swap(Op0, Op1);
442 std::swap(Op0Opc, Op1Opc);
443 std::swap(TargetMask, InsertMask);
444 }
445 }
446
447 unsigned MB, ME;
448 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000449 SDValue Tmp1, Tmp2, Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
451
452 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
453 isInt32Immediate(Op1.getOperand(1), Value)) {
454 Op1 = Op1.getOperand(0);
455 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
456 }
457 if (Op1Opc == ISD::AND) {
458 unsigned SHOpc = Op1.getOperand(0).getOpcode();
459 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
460 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
461 Op1 = Op1.getOperand(0).getOperand(0);
462 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
463 } else {
464 Op1 = Op1.getOperand(0);
465 }
466 }
467
468 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 SH &= 31;
Dan Gohman8181bd12008-07-27 21:46:04 +0000470 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 getI32Imm(ME) };
Dan Gohman61fda0d2009-09-25 18:54:59 +0000472 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 }
474 }
475 return 0;
476}
477
478/// SelectCC - Select a comparison of the specified values with the specified
479/// condition code, returning the CR# of the expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000480SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Dale Johannesen5d398a32009-02-06 19:16:40 +0000481 ISD::CondCode CC, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 // Always select the LHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 unsigned Opc;
484
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000485 if (LHS.getValueType() == MVT::i32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 unsigned Imm;
487 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
488 if (isInt32Immediate(RHS, Imm)) {
489 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
490 if (isUInt16(Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000491 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
492 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 // If this is a 16-bit signed immediate, fold it.
494 if (isInt16((int)Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000495 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
496 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497
498 // For non-equality comparisons, the default code would materialize the
499 // constant, then compare against it, like this:
500 // lis r2, 4660
501 // ori r2, r2, 22136
502 // cmpw cr0, r3, r2
503 // Since we are just comparing for equality, we can emit this instead:
504 // xoris r0,r3,0x1234
505 // cmplwi cr0,r0,0x5678
506 // beq cr0,L6
Dan Gohman61fda0d2009-09-25 18:54:59 +0000507 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
508 getI32Imm(Imm >> 16)), 0);
509 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
510 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 }
512 Opc = PPC::CMPLW;
513 } else if (ISD::isUnsignedIntSetCC(CC)) {
514 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000515 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
516 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 Opc = PPC::CMPLW;
518 } else {
519 short SImm;
520 if (isIntS16Immediate(RHS, SImm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000521 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
522 getI32Imm((int)SImm & 0xFFFF)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 0);
524 Opc = PPC::CMPW;
525 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000526 } else if (LHS.getValueType() == MVT::i64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 uint64_t Imm;
528 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000529 if (isInt64Immediate(RHS.getNode(), Imm)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
531 if (isUInt16(Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000532 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
533 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 // If this is a 16-bit signed immediate, fold it.
535 if (isInt16(Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000536 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
537 getI32Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
539 // For non-equality comparisons, the default code would materialize the
540 // constant, then compare against it, like this:
541 // lis r2, 4660
542 // ori r2, r2, 22136
543 // cmpd cr0, r3, r2
544 // Since we are just comparing for equality, we can emit this instead:
545 // xoris r0,r3,0x1234
546 // cmpldi cr0,r0,0x5678
547 // beq cr0,L6
548 if (isUInt32(Imm)) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000549 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
550 getI64Imm(Imm >> 16)), 0);
551 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
552 getI64Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 }
554 }
555 Opc = PPC::CMPLD;
556 } else if (ISD::isUnsignedIntSetCC(CC)) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000557 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000558 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
559 getI64Imm(Imm & 0xFFFF)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 Opc = PPC::CMPLD;
561 } else {
562 short SImm;
563 if (isIntS16Immediate(RHS, SImm))
Dan Gohman61fda0d2009-09-25 18:54:59 +0000564 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
565 getI64Imm(SImm & 0xFFFF)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 0);
567 Opc = PPC::CMPD;
568 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000569 } else if (LHS.getValueType() == MVT::f32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 Opc = PPC::FCMPUS;
571 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000572 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 Opc = PPC::FCMPUD;
574 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000575 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576}
577
578static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
579 switch (CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 case ISD::SETUEQ:
Dale Johannesen32100b22008-11-07 22:54:33 +0000581 case ISD::SETONE:
582 case ISD::SETOLE:
583 case ISD::SETOGE:
Edwin Törökbd448e32009-07-14 16:55:14 +0000584 llvm_unreachable("Should be lowered by legalize!");
585 default: llvm_unreachable("Unknown condition!");
Dale Johannesen32100b22008-11-07 22:54:33 +0000586 case ISD::SETOEQ:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 case ISD::SETEQ: return PPC::PRED_EQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 case ISD::SETUNE:
589 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000590 case ISD::SETOLT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 case ISD::SETLT: return PPC::PRED_LT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 case ISD::SETULE:
593 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000594 case ISD::SETOGT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 case ISD::SETGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 case ISD::SETUGE:
597 case ISD::SETGE: return PPC::PRED_GE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 case ISD::SETO: return PPC::PRED_NU;
599 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen32100b22008-11-07 22:54:33 +0000600 // These two are invalid for floating point. Assume we have int.
601 case ISD::SETULT: return PPC::PRED_LT;
602 case ISD::SETUGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 }
604}
605
606/// getCRIdxForSetCC - Return the index of the condition register field
607/// associated with the SetCC condition, and whether or not the field is
608/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Chris Lattner6c36fb52008-01-08 06:46:30 +0000609///
610/// If this returns with Other != -1, then the returned comparison is an or of
611/// two simpler comparisons. In this case, Invert is guaranteed to be false.
612static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
613 Invert = false;
614 Other = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000616 default: llvm_unreachable("Unknown condition!");
Chris Lattner6c36fb52008-01-08 06:46:30 +0000617 case ISD::SETOLT:
618 case ISD::SETLT: return 0; // Bit #0 = SETOLT
619 case ISD::SETOGT:
620 case ISD::SETGT: return 1; // Bit #1 = SETOGT
621 case ISD::SETOEQ:
622 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
623 case ISD::SETUO: return 3; // Bit #3 = SETUO
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 case ISD::SETUGE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000625 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 case ISD::SETULE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000627 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 case ISD::SETUNE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000629 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
630 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Dale Johannesen32100b22008-11-07 22:54:33 +0000631 case ISD::SETUEQ:
632 case ISD::SETOGE:
633 case ISD::SETOLE:
634 case ISD::SETONE:
Edwin Törökbd448e32009-07-14 16:55:14 +0000635 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen32100b22008-11-07 22:54:33 +0000636 // These are invalid for floating point. Assume integer.
637 case ISD::SETULT: return 0;
638 case ISD::SETUGT: return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 }
640 return 0;
641}
642
Dan Gohman8181bd12008-07-27 21:46:04 +0000643SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000644 SDNode *N = Op.getNode();
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000645 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 unsigned Imm;
647 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
648 if (isInt32Immediate(N->getOperand(1), Imm)) {
649 // We can codegen setcc op, imm very efficiently compared to a brcond.
650 // Check for those cases here.
651 // setcc op, 0
652 if (Imm == 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000653 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 switch (CC) {
655 default: break;
656 case ISD::SETEQ: {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000657 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000658 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000659 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 }
661 case ISD::SETNE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000662 SDValue AD =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000663 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
664 Op, getI32Imm(~0U)), 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000665 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 AD.getValue(1));
667 }
668 case ISD::SETLT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000669 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000670 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 }
672 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000673 SDValue T =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000674 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
675 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000676 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000677 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 }
679 }
680 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman8181bd12008-07-27 21:46:04 +0000681 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 switch (CC) {
683 default: break;
684 case ISD::SETEQ:
Dan Gohman61fda0d2009-09-25 18:54:59 +0000685 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
686 Op, getI32Imm(1)), 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000687 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman61fda0d2009-09-25 18:54:59 +0000688 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
689 MVT::i32,
690 getI32Imm(0)), 0),
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000691 Op.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 case ISD::SETNE: {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000693 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
694 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
695 Op, getI32Imm(~0U));
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000696 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman8181bd12008-07-27 21:46:04 +0000697 Op, SDValue(AD, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
699 case ISD::SETLT: {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000700 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
701 getI32Imm(1)), 0);
702 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
703 Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000704 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000705 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
707 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000708 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohman61fda0d2009-09-25 18:54:59 +0000709 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4),
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000710 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000711 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 getI32Imm(1));
713 }
714 }
715 }
716 }
717
718 bool Inv;
Chris Lattner6c36fb52008-01-08 06:46:30 +0000719 int OtherCondIdx;
720 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000721 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000722 SDValue IntCR;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723
724 // Force the ccreg into CR7.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000725 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726
Dan Gohman8181bd12008-07-27 21:46:04 +0000727 SDValue InFlag(0, 0); // Null incoming flag value.
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000728 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 InFlag).getValue(1);
730
Chris Lattner6c36fb52008-01-08 06:46:30 +0000731 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
Dan Gohman61fda0d2009-09-25 18:54:59 +0000732 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
733 CCReg), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 else
Dan Gohman61fda0d2009-09-25 18:54:59 +0000735 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFCR, dl, MVT::i32, CCReg), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736
Dan Gohman8181bd12008-07-27 21:46:04 +0000737 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 getI32Imm(31), getI32Imm(31) };
Chris Lattner6c36fb52008-01-08 06:46:30 +0000739 if (OtherCondIdx == -1 && !Inv)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000740 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000741
742 // Get the specified bit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000743 SDValue Tmp =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000744 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000745 if (Inv) {
746 assert(OtherCondIdx == -1 && "Can't have split plus negation");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000747 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 }
Chris Lattner6c36fb52008-01-08 06:46:30 +0000749
750 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
751 // We already got the bit for the first part of the comparison (e.g. SETULE).
752
753 // Get the other bit of the comparison.
754 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
Dan Gohman8181bd12008-07-27 21:46:04 +0000755 SDValue OtherCond =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000756 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000757
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000758 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759}
760
761
762// Select - Convert the specified operand from a target-independent to a
763// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000764SDNode *PPCDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000765 SDNode *N = Op.getNode();
Dale Johannesen913ba762009-02-06 01:31:28 +0000766 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000767 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 return NULL; // Already selected.
769
770 switch (N->getOpcode()) {
771 default: break;
772
773 case ISD::Constant: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000774 if (N->getValueType(0) == MVT::i64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 // Get 64 bit value.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000776 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 // Assume no remaining bits.
778 unsigned Remainder = 0;
779 // Assume no shift required.
780 unsigned Shift = 0;
781
782 // If it can't be represented as a 32 bit value.
783 if (!isInt32(Imm)) {
784 Shift = CountTrailingZeros_64(Imm);
785 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
786
787 // If the shifted value fits 32 bits.
788 if (isInt32(ImmSh)) {
789 // Go with the shifted value.
790 Imm = ImmSh;
791 } else {
792 // Still stuck with a 64 bit value.
793 Remainder = Imm;
794 Shift = 32;
795 Imm >>= 32;
796 }
797 }
798
799 // Intermediate operand.
800 SDNode *Result;
801
802 // Handle first 32 bits.
803 unsigned Lo = Imm & 0xFFFF;
804 unsigned Hi = (Imm >> 16) & 0xFFFF;
805
806 // Simple value.
807 if (isInt16(Imm)) {
808 // Just the Lo bits.
Dan Gohman61fda0d2009-09-25 18:54:59 +0000809 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 } else if (Lo) {
811 // Handle the Hi bits.
812 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman61fda0d2009-09-25 18:54:59 +0000813 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 // And Lo bits.
Dan Gohman61fda0d2009-09-25 18:54:59 +0000815 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
816 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 } else {
818 // Just the Hi bits.
Dan Gohman61fda0d2009-09-25 18:54:59 +0000819 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 }
821
822 // If no shift, we're done.
823 if (!Shift) return Result;
824
825 // Shift for next step if the upper 32-bits were not zero.
826 if (Imm) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000827 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
828 SDValue(Result, 0),
829 getI32Imm(Shift),
830 getI32Imm(63 - Shift));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 }
832
833 // Add in the last bits as required.
834 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000835 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
836 SDValue(Result, 0), getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 }
838 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000839 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
840 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 }
842
843 return Result;
844 }
845 break;
846 }
847
848 case ISD::SETCC:
849 return SelectSETCC(Op);
850 case PPCISD::GlobalBaseReg:
851 return getGlobalBaseReg();
852
853 case ISD::FrameIndex: {
854 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000856 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 if (N->hasOneUse())
858 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
859 getSmallIPtrImm(0));
Dan Gohman61fda0d2009-09-25 18:54:59 +0000860 return CurDAG->getMachineNode(Opc, dl, Op.getValueType(), TFI,
861 getSmallIPtrImm(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 }
863
864 case PPCISD::MFCR: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000865 SDValue InFlag = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 // Use MFOCRF if supported.
Evan Cheng9d99c5e2007-10-23 06:42:42 +0000867 if (PPCSubTarget.isGigaProcessor())
Dan Gohman61fda0d2009-09-25 18:54:59 +0000868 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
869 N->getOperand(0), InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 else
Dan Gohman61fda0d2009-09-25 18:54:59 +0000871 return CurDAG->getMachineNode(PPC::MFCR, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 }
873
874 case ISD::SDIV: {
875 // FIXME: since this depends on the setting of the carry flag from the srawi
876 // we should really be making notes about that for the scheduler.
877 // FIXME: It sure would be nice if we could cheaply recognize the
878 // srl/add/sra pattern the dag combiner will generate for this as
879 // sra/addze rather than having to handle sdiv ourselves. oh well.
880 unsigned Imm;
881 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000882 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
884 SDNode *Op =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000885 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
886 N0, getI32Imm(Log2_32(Imm)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000887 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +0000888 SDValue(Op, 0), SDValue(Op, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
890 SDNode *Op =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000891 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
892 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman8181bd12008-07-27 21:46:04 +0000893 SDValue PT =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000894 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
895 SDValue(Op, 0), SDValue(Op, 1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000897 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 }
899 }
900
901 // Other cases are autogenerated.
902 break;
903 }
904
905 case ISD::LOAD: {
906 // Handle preincrement loads.
907 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersonac9de032009-08-10 22:56:29 +0000908 EVT LoadedVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909
910 // Normal loads are handled by code generated from the .td file.
911 if (LD->getAddressingMode() != ISD::PRE_INC)
912 break;
913
Dan Gohman8181bd12008-07-27 21:46:04 +0000914 SDValue Offset = LD->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 if (isa<ConstantSDNode>(Offset) ||
916 Offset.getOpcode() == ISD::TargetGlobalAddress) {
917
918 unsigned Opcode;
919 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000920 if (LD->getValueType(0) != MVT::i64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 // Handle PPC32 integer and normal FP loads.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000922 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
923 switch (LoadedVT.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000924 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000925 case MVT::f64: Opcode = PPC::LFDU; break;
926 case MVT::f32: Opcode = PPC::LFSU; break;
927 case MVT::i32: Opcode = PPC::LWZU; break;
928 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
929 case MVT::i1:
930 case MVT::i8: Opcode = PPC::LBZU; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 }
932 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000933 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
934 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
935 switch (LoadedVT.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000936 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000937 case MVT::i64: Opcode = PPC::LDU; break;
938 case MVT::i32: Opcode = PPC::LWZU8; break;
939 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
940 case MVT::i1:
941 case MVT::i8: Opcode = PPC::LBZU8; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 }
943 }
944
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SDValue Chain = LD->getChain();
946 SDValue Base = LD->getBasePtr();
Dan Gohman8181bd12008-07-27 21:46:04 +0000947 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 // FIXME: PPC64
Dan Gohman61fda0d2009-09-25 18:54:59 +0000949 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
950 PPCLowering.getPointerTy(),
951 MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000953 llvm_unreachable("R+R preindex loads not supported yet!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 }
955 }
956
957 case ISD::AND: {
958 unsigned Imm, Imm2, SH, MB, ME;
959
960 // If this is an and of a value rotated between 0 and 31 bits and then and'd
961 // with a mask, emit rlwinm
962 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000963 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000964 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000965 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000966 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 }
968 // If this is just a masked value where the input is not handled above, and
969 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
970 if (isInt32Immediate(N->getOperand(1), Imm) &&
971 isRunOfOnes(Imm, MB, ME) &&
972 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000973 SDValue Val = N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000974 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000975 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 }
977 // AND X, 0 -> 0, not "rlwinm 32".
978 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000979 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 return NULL;
981 }
982 // ISD::OR doesn't get all the bitfield insertion fun.
983 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
984 if (isInt32Immediate(N->getOperand(1), Imm) &&
985 N->getOperand(0).getOpcode() == ISD::OR &&
986 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
987 unsigned MB, ME;
988 Imm = ~(Imm^Imm2);
989 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000990 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 N->getOperand(0).getOperand(1),
992 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Dan Gohman61fda0d2009-09-25 18:54:59 +0000993 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 }
995 }
996
997 // Other cases are autogenerated.
998 break;
999 }
1000 case ISD::OR:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001001 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 if (SDNode *I = SelectBitfieldInsert(N))
1003 return I;
1004
1005 // Other cases are autogenerated.
1006 break;
1007 case ISD::SHL: {
1008 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001009 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001011 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001013 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 }
1015
1016 // Other cases are autogenerated.
1017 break;
1018 }
1019 case ISD::SRL: {
1020 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001021 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001023 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001025 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 }
1027
1028 // Other cases are autogenerated.
1029 break;
1030 }
1031 case ISD::SELECT_CC: {
1032 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1033
1034 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1035 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1036 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1037 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1038 if (N1C->isNullValue() && N3C->isNullValue() &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001039 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 // FIXME: Implement this optzn for PPC64.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001041 N->getValueType(0) == MVT::i32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 SDNode *Tmp =
Dan Gohman61fda0d2009-09-25 18:54:59 +00001043 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
1044 N->getOperand(0), getI32Imm(~0U));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001045 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +00001046 SDValue(Tmp, 0), N->getOperand(0),
1047 SDValue(Tmp, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 }
1049
Dale Johannesen5d398a32009-02-06 19:16:40 +00001050 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 unsigned BROpc = getPredicateForSetCC(CC);
1052
1053 unsigned SelectCCOp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001054 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001056 else if (N->getValueType(0) == MVT::i64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001058 else if (N->getValueType(0) == MVT::f32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001060 else if (N->getValueType(0) == MVT::f64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 SelectCCOp = PPC::SELECT_CC_F8;
1062 else
1063 SelectCCOp = PPC::SELECT_CC_VRRC;
1064
Dan Gohman8181bd12008-07-27 21:46:04 +00001065 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 getI32Imm(BROpc) };
1067 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1068 }
1069 case PPCISD::COND_BRANCH: {
Dan Gohmana1fb67a2008-11-05 17:16:24 +00001070 // Op #0 is the Chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 // Op #1 is the PPC::PRED_* number.
1072 // Op #2 is the CR#
1073 // Op #3 is the Dest MBB
Dan Gohmancc3df852008-11-05 04:14:16 +00001074 // Op #4 is the Flag.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman8181bd12008-07-27 21:46:04 +00001076 SDValue Pred =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001077 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00001078 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 N->getOperand(0), N->getOperand(4) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001080 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 }
1082 case ISD::BR_CC: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesen5d398a32009-02-06 19:16:40 +00001084 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +00001085 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 N->getOperand(4), N->getOperand(0) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001087 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 }
1089 case ISD::BRIND: {
1090 // FIXME: Should custom lower this.
Dan Gohman8181bd12008-07-27 21:46:04 +00001091 SDValue Chain = N->getOperand(0);
1092 SDValue Target = N->getOperand(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001093 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Dan Gohman61fda0d2009-09-25 18:54:59 +00001094 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Target,
1095 Chain), 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001096 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 }
1099
1100 return SelectCode(Op);
1101}
1102
1103
1104
1105/// createPPCISelDag - This pass converts a legalized DAG into a
1106/// PowerPC-specific DAG, ready for instruction scheduling.
1107///
1108FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1109 return new PPCDAGToDAGISel(TM);
1110}
1111