blob: d77ce571895fa143bb6778e8e19f1b54a28c3b89 [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 ///
44 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
45 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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 return CurDAG->getTargetConstant(Imm, MVT::i32);
68 }
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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 return CurDAG->getTargetConstant(Imm, MVT::i64);
74 }
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
146 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000147 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000149 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000150 SDValue Op0, Op1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 switch (ConstraintCode) {
152 default: return true;
153 case 'm': // memory
154 if (!SelectAddrIdx(Op, Op, Op0, Op1))
155 SelectAddrImm(Op, Op, Op0, Op1);
156 break;
157 case 'o': // offsetable
158 if (!SelectAddrImm(Op, Op, Op0, Op1)) {
159 Op0 = Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 Op1 = getSmallIPtrImm(0);
161 }
162 break;
163 case 'v': // not offsetable
164 SelectAddrIdxOnly(Op, Op, Op0, Op1);
165 break;
166 }
167
168 OutOps.push_back(Op0);
169 OutOps.push_back(Op1);
170 return false;
171 }
172
Dan Gohman8181bd12008-07-27 21:46:04 +0000173 SDValue BuildSDIVSequence(SDNode *N);
174 SDValue BuildUDIVSequence(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
Evan Cheng34fd4f32008-06-30 20:45:06 +0000176 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000178 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000180 void InsertVRSaveCode(MachineFunction &MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 virtual const char *getPassName() const {
183 return "PowerPC DAG->DAG Pattern Instruction Selection";
184 }
185
186 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
187 /// this target when scheduling the DAG.
Dan Gohmandd6547d2009-01-15 22:18:12 +0000188 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 // Should use subtarget info to pick the right hazard recognizer. For
190 // now, always return a PPC970 recognizer.
Dan Gohman404e8542008-09-04 15:39:15 +0000191 const TargetInstrInfo *II = TM.getInstrInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 assert(II && "No InstrInfo?");
193 return new PPCHazardRecognizer970(*II);
194 }
195
196// Include the pieces autogenerated from the target description.
197#include "PPCGenDAGISel.inc"
198
199private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000200 SDNode *SelectSETCC(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 };
202}
203
Evan Cheng34fd4f32008-06-30 20:45:06 +0000204/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000206void PPCDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 DEBUG(BB->dump());
208
209 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000210 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +0000211 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212}
213
214/// InsertVRSaveCode - Once the entire function has been instruction selected,
215/// all virtual registers are created and all machine instructions are built,
216/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohmanfdf9ee22009-07-31 18:16:33 +0000217void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 // Check to see if this function uses vector registers, which means we have to
219 // save and restore the VRSAVE register and update it with the regs we use.
220 //
221 // In this case, there will be virtual registers of vector type type created
222 // by the scheduler. Detect them now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 bool HasVectorVReg = false;
Dan Gohman1e57df32008-02-10 18:45:23 +0000224 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +0000225 e = RegInfo->getLastVirtReg()+1; i != e; ++i)
226 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 HasVectorVReg = true;
228 break;
229 }
230 if (!HasVectorVReg) return; // nothing to do.
231
232 // If we have a vector register, we want to emit code into the entry and exit
233 // blocks to save and restore the VRSAVE register. We do this here (instead
234 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
235 //
236 // 1. This (trivially) reduces the load on the register allocator, by not
237 // having to represent the live range of the VRSAVE register.
238 // 2. This (more significantly) allows us to create a temporary virtual
239 // register to hold the saved VRSAVE value, allowing this temporary to be
240 // register allocated, instead of forcing it to be spilled to the stack.
241
242 // Create two vregs - one to hold the VRSAVE register that is live-in to the
243 // function and one for the value after having bits or'd into it.
Chris Lattner1b989192007-12-31 04:13:23 +0000244 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
245 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
247 const TargetInstrInfo &TII = *TM.getInstrInfo();
248 MachineBasicBlock &EntryBB = *Fn.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000249 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 // Emit the following code into the entry block:
251 // InVRSAVE = MFVRSAVE
252 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
253 // MTVRSAVE UpdatedVRSAVE
254 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000255 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
256 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattner62327602008-01-07 01:56:04 +0000257 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000258 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
260 // Find all return blocks, outputting a restore in each epilog.
261 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Chris Lattner5b930372008-01-07 07:27:27 +0000262 if (!BB->empty() && BB->back().getDesc().isReturn()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 IP = BB->end(); --IP;
264
265 // Skip over all terminator instructions, which are part of the return
266 // sequence.
267 MachineBasicBlock::iterator I2 = IP;
Chris Lattner5b930372008-01-07 07:27:27 +0000268 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 IP = I2;
270
271 // Emit: MTVRSAVE InVRSave
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000272 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 }
274 }
275}
276
277
278/// getGlobalBaseReg - Output the instructions required to put the
279/// base address to use for accessing globals into a register.
280///
281SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
282 if (!GlobalBaseReg) {
283 const TargetInstrInfo &TII = *TM.getInstrInfo();
284 // Insert the set of GlobalBaseReg into the first MBB of the function
285 MachineBasicBlock &FirstMBB = BB->getParent()->front();
286 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000287 DebugLoc dl = DebugLoc::getUnknownLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288
289 if (PPCLowering.getPointerTy() == MVT::i32) {
Chris Lattner1b989192007-12-31 04:13:23 +0000290 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000291 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR);
292 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000294 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
Dale Johannesenf1cac0f2009-02-13 02:27:39 +0000295 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8);
296 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 }
298 }
Gabor Greife9f7f582008-08-31 15:37:04 +0000299 return CurDAG->getRegister(GlobalBaseReg,
300 PPCLowering.getPointerTy()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301}
302
303/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
304/// or 64-bit immediate, and if the value can be accurately represented as a
305/// sign extension from a 16-bit value. If so, this returns true and the
306/// immediate.
307static bool isIntS16Immediate(SDNode *N, short &Imm) {
308 if (N->getOpcode() != ISD::Constant)
309 return false;
310
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000311 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 if (N->getValueType(0) == MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000313 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 else
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000315 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316}
317
Dan Gohman8181bd12008-07-27 21:46:04 +0000318static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000319 return isIntS16Immediate(Op.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320}
321
322
323/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
324/// operand. If so Imm will receive the 32-bit value.
325static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
326 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000327 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 return true;
329 }
330 return false;
331}
332
333/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
334/// operand. If so Imm will receive the 64-bit value.
335static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
336 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000337 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 return true;
339 }
340 return false;
341}
342
343// isInt32Immediate - This method tests to see if a constant operand.
344// If so Imm will receive the 32 bit value.
Dan Gohman8181bd12008-07-27 21:46:04 +0000345static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000346 return isInt32Immediate(N.getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347}
348
349
350// isOpcWithIntImmediate - This method tests to see if the node is a specific
351// opcode and that it has a immediate integer right operand.
352// If so Imm will receive the 32 bit value.
353static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greife9f7f582008-08-31 15:37:04 +0000354 return N->getOpcode() == Opc
355 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356}
357
358bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
359 if (isShiftedMask_32(Val)) {
360 // look for the first non-zero bit
361 MB = CountLeadingZeros_32(Val);
362 // look for the first zero bit after the run of ones
363 ME = CountLeadingZeros_32((Val - 1) ^ Val);
364 return true;
365 } else {
366 Val = ~Val; // invert mask
367 if (isShiftedMask_32(Val)) {
368 // effectively look for the first zero bit
369 ME = CountLeadingZeros_32(Val) - 1;
370 // effectively look for the first one bit after the run of zeros
371 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
372 return true;
373 }
374 }
375 // no run present
376 return false;
377}
378
379bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
380 bool IsShiftMask, unsigned &SH,
381 unsigned &MB, unsigned &ME) {
382 // Don't even go down this path for i64, since different logic will be
383 // necessary for rldicl/rldicr/rldimi.
384 if (N->getValueType(0) != MVT::i32)
385 return false;
386
387 unsigned Shift = 32;
388 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
389 unsigned Opcode = N->getOpcode();
390 if (N->getNumOperands() != 2 ||
Gabor Greif1c80d112008-08-28 21:40:38 +0000391 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 return false;
393
394 if (Opcode == ISD::SHL) {
395 // apply shift left to mask if it comes first
396 if (IsShiftMask) Mask = Mask << Shift;
397 // determine which bits are made indeterminant by shift
398 Indeterminant = ~(0xFFFFFFFFu << Shift);
399 } else if (Opcode == ISD::SRL) {
400 // apply shift right to mask if it comes first
401 if (IsShiftMask) Mask = Mask >> Shift;
402 // determine which bits are made indeterminant by shift
403 Indeterminant = ~(0xFFFFFFFFu >> Shift);
404 // adjust for the left rotate
405 Shift = 32 - Shift;
406 } else if (Opcode == ISD::ROTL) {
407 Indeterminant = 0;
408 } else {
409 return false;
410 }
411
412 // if the mask doesn't intersect any Indeterminant bits
413 if (Mask && !(Mask & Indeterminant)) {
414 SH = Shift & 31;
415 // make sure the mask is still a mask (wrap arounds may not be)
416 return isRunOfOnes(Mask, MB, ME);
417 }
418 return false;
419}
420
421/// SelectBitfieldInsert - turn an or of two masked values into
422/// the rotate left word immediate then mask insert (rlwimi) instruction.
423SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000424 SDValue Op0 = N->getOperand(0);
425 SDValue Op1 = N->getOperand(1);
Dale Johannesen913ba762009-02-06 01:31:28 +0000426 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427
Dan Gohman63f4e462008-02-27 01:23:58 +0000428 APInt LKZ, LKO, RKZ, RKO;
429 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
430 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
Dan Gohman63f4e462008-02-27 01:23:58 +0000432 unsigned TargetMask = LKZ.getZExtValue();
433 unsigned InsertMask = RKZ.getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
435 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
436 unsigned Op0Opc = Op0.getOpcode();
437 unsigned Op1Opc = Op1.getOpcode();
438 unsigned Value, SH = 0;
439 TargetMask = ~TargetMask;
440 InsertMask = ~InsertMask;
441
442 // If the LHS has a foldable shift and the RHS does not, then swap it to the
443 // RHS so that we can fold the shift into the insert.
444 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
445 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
446 Op0.getOperand(0).getOpcode() == ISD::SRL) {
447 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
448 Op1.getOperand(0).getOpcode() != ISD::SRL) {
449 std::swap(Op0, Op1);
450 std::swap(Op0Opc, Op1Opc);
451 std::swap(TargetMask, InsertMask);
452 }
453 }
454 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
455 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
456 Op1.getOperand(0).getOpcode() != ISD::SRL) {
457 std::swap(Op0, Op1);
458 std::swap(Op0Opc, Op1Opc);
459 std::swap(TargetMask, InsertMask);
460 }
461 }
462
463 unsigned MB, ME;
464 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000465 SDValue Tmp1, Tmp2, Tmp3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
467
468 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
469 isInt32Immediate(Op1.getOperand(1), Value)) {
470 Op1 = Op1.getOperand(0);
471 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
472 }
473 if (Op1Opc == ISD::AND) {
474 unsigned SHOpc = Op1.getOperand(0).getOpcode();
475 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
476 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
477 Op1 = Op1.getOperand(0).getOperand(0);
478 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
479 } else {
480 Op1 = Op1.getOperand(0);
481 }
482 }
483
484 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 SH &= 31;
Dan Gohman8181bd12008-07-27 21:46:04 +0000486 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 getI32Imm(ME) };
Dale Johannesen913ba762009-02-06 01:31:28 +0000488 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 }
490 }
491 return 0;
492}
493
494/// SelectCC - Select a comparison of the specified values with the specified
495/// condition code, returning the CR# of the expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000496SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Dale Johannesen5d398a32009-02-06 19:16:40 +0000497 ISD::CondCode CC, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 // Always select the LHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 unsigned Opc;
500
501 if (LHS.getValueType() == MVT::i32) {
502 unsigned Imm;
503 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
504 if (isInt32Immediate(RHS, Imm)) {
505 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
506 if (isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000507 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 getI32Imm(Imm & 0xFFFF)), 0);
509 // If this is a 16-bit signed immediate, fold it.
510 if (isInt16((int)Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000511 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 getI32Imm(Imm & 0xFFFF)), 0);
513
514 // For non-equality comparisons, the default code would materialize the
515 // constant, then compare against it, like this:
516 // lis r2, 4660
517 // ori r2, r2, 22136
518 // cmpw cr0, r3, r2
519 // Since we are just comparing for equality, we can emit this instead:
520 // xoris r0,r3,0x1234
521 // cmplwi cr0,r0,0x5678
522 // beq cr0,L6
Dale Johannesen5d398a32009-02-06 19:16:40 +0000523 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 getI32Imm(Imm >> 16)), 0);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000525 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 getI32Imm(Imm & 0xFFFF)), 0);
527 }
528 Opc = PPC::CMPLW;
529 } else if (ISD::isUnsignedIntSetCC(CC)) {
530 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000531 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 getI32Imm(Imm & 0xFFFF)), 0);
533 Opc = PPC::CMPLW;
534 } else {
535 short SImm;
536 if (isIntS16Immediate(RHS, SImm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000537 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 getI32Imm((int)SImm & 0xFFFF)),
539 0);
540 Opc = PPC::CMPW;
541 }
542 } else if (LHS.getValueType() == MVT::i64) {
543 uint64_t Imm;
544 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000545 if (isInt64Immediate(RHS.getNode(), Imm)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
547 if (isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000548 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 getI32Imm(Imm & 0xFFFF)), 0);
550 // If this is a 16-bit signed immediate, fold it.
551 if (isInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000552 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 getI32Imm(Imm & 0xFFFF)), 0);
554
555 // For non-equality comparisons, the default code would materialize the
556 // constant, then compare against it, like this:
557 // lis r2, 4660
558 // ori r2, r2, 22136
559 // cmpd cr0, r3, r2
560 // Since we are just comparing for equality, we can emit this instead:
561 // xoris r0,r3,0x1234
562 // cmpldi cr0,r0,0x5678
563 // beq cr0,L6
564 if (isUInt32(Imm)) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000565 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 getI64Imm(Imm >> 16)), 0);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000567 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 getI64Imm(Imm & 0xFFFF)), 0);
569 }
570 }
571 Opc = PPC::CMPLD;
572 } else if (ISD::isUnsignedIntSetCC(CC)) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000573 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000574 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 getI64Imm(Imm & 0xFFFF)), 0);
576 Opc = PPC::CMPLD;
577 } else {
578 short SImm;
579 if (isIntS16Immediate(RHS, SImm))
Dale Johannesen5d398a32009-02-06 19:16:40 +0000580 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 getI64Imm(SImm & 0xFFFF)),
582 0);
583 Opc = PPC::CMPD;
584 }
585 } else if (LHS.getValueType() == MVT::f32) {
586 Opc = PPC::FCMPUS;
587 } else {
588 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
589 Opc = PPC::FCMPUD;
590 }
Dale Johannesen5d398a32009-02-06 19:16:40 +0000591 return SDValue(CurDAG->getTargetNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592}
593
594static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
595 switch (CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 case ISD::SETUEQ:
Dale Johannesen32100b22008-11-07 22:54:33 +0000597 case ISD::SETONE:
598 case ISD::SETOLE:
599 case ISD::SETOGE:
Edwin Törökbd448e32009-07-14 16:55:14 +0000600 llvm_unreachable("Should be lowered by legalize!");
601 default: llvm_unreachable("Unknown condition!");
Dale Johannesen32100b22008-11-07 22:54:33 +0000602 case ISD::SETOEQ:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 case ISD::SETEQ: return PPC::PRED_EQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 case ISD::SETUNE:
605 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000606 case ISD::SETOLT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 case ISD::SETLT: return PPC::PRED_LT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608 case ISD::SETULE:
609 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen32100b22008-11-07 22:54:33 +0000610 case ISD::SETOGT:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 case ISD::SETGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 case ISD::SETUGE:
613 case ISD::SETGE: return PPC::PRED_GE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 case ISD::SETO: return PPC::PRED_NU;
615 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen32100b22008-11-07 22:54:33 +0000616 // These two are invalid for floating point. Assume we have int.
617 case ISD::SETULT: return PPC::PRED_LT;
618 case ISD::SETUGT: return PPC::PRED_GT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 }
620}
621
622/// getCRIdxForSetCC - Return the index of the condition register field
623/// associated with the SetCC condition, and whether or not the field is
624/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Chris Lattner6c36fb52008-01-08 06:46:30 +0000625///
626/// If this returns with Other != -1, then the returned comparison is an or of
627/// two simpler comparisons. In this case, Invert is guaranteed to be false.
628static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
629 Invert = false;
630 Other = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000632 default: llvm_unreachable("Unknown condition!");
Chris Lattner6c36fb52008-01-08 06:46:30 +0000633 case ISD::SETOLT:
634 case ISD::SETLT: return 0; // Bit #0 = SETOLT
635 case ISD::SETOGT:
636 case ISD::SETGT: return 1; // Bit #1 = SETOGT
637 case ISD::SETOEQ:
638 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
639 case ISD::SETUO: return 3; // Bit #3 = SETUO
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 case ISD::SETUGE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000641 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 case ISD::SETULE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000643 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 case ISD::SETUNE:
Chris Lattner6c36fb52008-01-08 06:46:30 +0000645 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
646 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Dale Johannesen32100b22008-11-07 22:54:33 +0000647 case ISD::SETUEQ:
648 case ISD::SETOGE:
649 case ISD::SETOLE:
650 case ISD::SETONE:
Edwin Törökbd448e32009-07-14 16:55:14 +0000651 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen32100b22008-11-07 22:54:33 +0000652 // These are invalid for floating point. Assume integer.
653 case ISD::SETULT: return 0;
654 case ISD::SETUGT: return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 }
656 return 0;
657}
658
Dan Gohman8181bd12008-07-27 21:46:04 +0000659SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000660 SDNode *N = Op.getNode();
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000661 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 unsigned Imm;
663 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
664 if (isInt32Immediate(N->getOperand(1), Imm)) {
665 // We can codegen setcc op, imm very efficiently compared to a brcond.
666 // Check for those cases here.
667 // setcc op, 0
668 if (Imm == 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000669 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 switch (CC) {
671 default: break;
672 case ISD::SETEQ: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000673 Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000674 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
676 }
677 case ISD::SETNE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000678 SDValue AD =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000679 SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 Op, getI32Imm(~0U)), 0);
681 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
682 AD.getValue(1));
683 }
684 case ISD::SETLT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000685 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
687 }
688 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000689 SDValue T =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000690 SDValue(CurDAG->getTargetNode(PPC::NEG, dl, MVT::i32, Op), 0);
691 T = SDValue(CurDAG->getTargetNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000692 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
694 }
695 }
696 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman8181bd12008-07-27 21:46:04 +0000697 SDValue Op = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 switch (CC) {
699 default: break;
700 case ISD::SETEQ:
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000701 Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 Op, getI32Imm(1)), 0);
703 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000704 SDValue(CurDAG->getTargetNode(PPC::LI, dl,
705 MVT::i32,
706 getI32Imm(0)), 0),
707 Op.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 case ISD::SETNE: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000709 Op = SDValue(CurDAG->getTargetNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
710 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 Op, getI32Imm(~0U));
Dan Gohman8181bd12008-07-27 21:46:04 +0000712 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
713 Op, SDValue(AD, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 }
715 case ISD::SETLT: {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000716 SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, dl, MVT::i32, Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 getI32Imm(1)), 0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000718 SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, dl, MVT::i32, AD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 Op), 0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000720 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
722 }
723 case ISD::SETGT: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000724 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000725 Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4),
726 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
728 getI32Imm(1));
729 }
730 }
731 }
732 }
733
734 bool Inv;
Chris Lattner6c36fb52008-01-08 06:46:30 +0000735 int OtherCondIdx;
736 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
Dale Johannesen5d398a32009-02-06 19:16:40 +0000737 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +0000738 SDValue IntCR;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739
740 // Force the ccreg into CR7.
Dan Gohman8181bd12008-07-27 21:46:04 +0000741 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742
Dan Gohman8181bd12008-07-27 21:46:04 +0000743 SDValue InFlag(0, 0); // Null incoming flag value.
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000744 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 InFlag).getValue(1);
746
Chris Lattner6c36fb52008-01-08 06:46:30 +0000747 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000748 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 CCReg), 0);
750 else
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000751 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, CCReg), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
Dan Gohman8181bd12008-07-27 21:46:04 +0000753 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 getI32Imm(31), getI32Imm(31) };
Chris Lattner6c36fb52008-01-08 06:46:30 +0000755 if (OtherCondIdx == -1 && !Inv)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000757
758 // Get the specified bit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000759 SDValue Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000760 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000761 if (Inv) {
762 assert(OtherCondIdx == -1 && "Can't have split plus negation");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
764 }
Chris Lattner6c36fb52008-01-08 06:46:30 +0000765
766 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
767 // We already got the bit for the first part of the comparison (e.g. SETULE).
768
769 // Get the other bit of the comparison.
770 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
Dan Gohman8181bd12008-07-27 21:46:04 +0000771 SDValue OtherCond =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000772 SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
Chris Lattner6c36fb52008-01-08 06:46:30 +0000773
774 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775}
776
777
778// Select - Convert the specified operand from a target-independent to a
779// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000780SDNode *PPCDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000781 SDNode *N = Op.getNode();
Dale Johannesen913ba762009-02-06 01:31:28 +0000782 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000783 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 return NULL; // Already selected.
785
786 switch (N->getOpcode()) {
787 default: break;
788
789 case ISD::Constant: {
790 if (N->getValueType(0) == MVT::i64) {
791 // Get 64 bit value.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000792 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 // Assume no remaining bits.
794 unsigned Remainder = 0;
795 // Assume no shift required.
796 unsigned Shift = 0;
797
798 // If it can't be represented as a 32 bit value.
799 if (!isInt32(Imm)) {
800 Shift = CountTrailingZeros_64(Imm);
801 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
802
803 // If the shifted value fits 32 bits.
804 if (isInt32(ImmSh)) {
805 // Go with the shifted value.
806 Imm = ImmSh;
807 } else {
808 // Still stuck with a 64 bit value.
809 Remainder = Imm;
810 Shift = 32;
811 Imm >>= 32;
812 }
813 }
814
815 // Intermediate operand.
816 SDNode *Result;
817
818 // Handle first 32 bits.
819 unsigned Lo = Imm & 0xFFFF;
820 unsigned Hi = (Imm >> 16) & 0xFFFF;
821
822 // Simple value.
823 if (isInt16(Imm)) {
824 // Just the Lo bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000825 Result = CurDAG->getTargetNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 } else if (Lo) {
827 // Handle the Hi bits.
828 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dale Johannesen913ba762009-02-06 01:31:28 +0000829 Result = CurDAG->getTargetNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 // And Lo bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000831 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000832 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 } else {
834 // Just the Hi bits.
Dale Johannesen913ba762009-02-06 01:31:28 +0000835 Result = CurDAG->getTargetNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 }
837
838 // If no shift, we're done.
839 if (!Shift) return Result;
840
841 // Shift for next step if the upper 32-bits were not zero.
842 if (Imm) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000843 Result = CurDAG->getTargetNode(PPC::RLDICR, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000844 SDValue(Result, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 getI32Imm(Shift), getI32Imm(63 - Shift));
846 }
847
848 // Add in the last bits as required.
849 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000850 Result = CurDAG->getTargetNode(PPC::ORIS8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000851 SDValue(Result, 0), getI32Imm(Hi));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 }
853 if ((Lo = Remainder & 0xFFFF)) {
Dale Johannesen913ba762009-02-06 01:31:28 +0000854 Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue(Result, 0), getI32Imm(Lo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 }
857
858 return Result;
859 }
860 break;
861 }
862
863 case ISD::SETCC:
864 return SelectSETCC(Op);
865 case PPCISD::GlobalBaseReg:
866 return getGlobalBaseReg();
867
868 case ISD::FrameIndex: {
869 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman8181bd12008-07-27 21:46:04 +0000870 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
872 if (N->hasOneUse())
873 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
874 getSmallIPtrImm(0));
Dale Johannesen913ba762009-02-06 01:31:28 +0000875 return CurDAG->getTargetNode(Opc, dl, Op.getValueType(), TFI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 getSmallIPtrImm(0));
877 }
878
879 case PPCISD::MFCR: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000880 SDValue InFlag = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 // Use MFOCRF if supported.
Evan Cheng9d99c5e2007-10-23 06:42:42 +0000882 if (PPCSubTarget.isGigaProcessor())
Dale Johannesen913ba762009-02-06 01:31:28 +0000883 return CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 N->getOperand(0), InFlag);
885 else
Dale Johannesen913ba762009-02-06 01:31:28 +0000886 return CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 }
888
889 case ISD::SDIV: {
890 // FIXME: since this depends on the setting of the carry flag from the srawi
891 // we should really be making notes about that for the scheduler.
892 // FIXME: It sure would be nice if we could cheaply recognize the
893 // srl/add/sra pattern the dag combiner will generate for this as
894 // sra/addze rather than having to handle sdiv ourselves. oh well.
895 unsigned Imm;
896 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000897 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
899 SDNode *Op =
Dale Johannesen913ba762009-02-06 01:31:28 +0000900 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 N0, getI32Imm(Log2_32(Imm)));
902 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +0000903 SDValue(Op, 0), SDValue(Op, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
905 SDNode *Op =
Dale Johannesen913ba762009-02-06 01:31:28 +0000906 CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman8181bd12008-07-27 21:46:04 +0000908 SDValue PT =
Dale Johannesen913ba762009-02-06 01:31:28 +0000909 SDValue(CurDAG->getTargetNode(PPC::ADDZE, dl, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +0000910 SDValue(Op, 0), SDValue(Op, 1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 0);
912 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
913 }
914 }
915
916 // Other cases are autogenerated.
917 break;
918 }
919
920 case ISD::LOAD: {
921 // Handle preincrement loads.
922 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands92c43912008-06-06 12:08:01 +0000923 MVT LoadedVT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924
925 // Normal loads are handled by code generated from the .td file.
926 if (LD->getAddressingMode() != ISD::PRE_INC)
927 break;
928
Dan Gohman8181bd12008-07-27 21:46:04 +0000929 SDValue Offset = LD->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 if (isa<ConstantSDNode>(Offset) ||
931 Offset.getOpcode() == ISD::TargetGlobalAddress) {
932
933 unsigned Opcode;
934 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
935 if (LD->getValueType(0) != MVT::i64) {
936 // Handle PPC32 integer and normal FP loads.
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000937 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands92c43912008-06-06 12:08:01 +0000938 switch (LoadedVT.getSimpleVT()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000939 default: llvm_unreachable("Invalid PPC load type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 case MVT::f64: Opcode = PPC::LFDU; break;
941 case MVT::f32: Opcode = PPC::LFSU; break;
942 case MVT::i32: Opcode = PPC::LWZU; break;
943 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
944 case MVT::i1:
945 case MVT::i8: Opcode = PPC::LBZU; break;
946 }
947 } else {
948 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000949 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands92c43912008-06-06 12:08:01 +0000950 switch (LoadedVT.getSimpleVT()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000951 default: llvm_unreachable("Invalid PPC load type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 case MVT::i64: Opcode = PPC::LDU; break;
953 case MVT::i32: Opcode = PPC::LWZU8; break;
954 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
955 case MVT::i1:
956 case MVT::i8: Opcode = PPC::LBZU8; break;
957 }
958 }
959
Dan Gohman8181bd12008-07-27 21:46:04 +0000960 SDValue Chain = LD->getChain();
961 SDValue Base = LD->getBasePtr();
Dan Gohman8181bd12008-07-27 21:46:04 +0000962 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 // FIXME: PPC64
Dale Johannesen913ba762009-02-06 01:31:28 +0000964 return CurDAG->getTargetNode(Opcode, dl, LD->getValueType(0),
Dan Gohmanbd68c792008-07-17 19:10:17 +0000965 PPCLowering.getPointerTy(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 MVT::Other, Ops, 3);
967 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000968 llvm_unreachable("R+R preindex loads not supported yet!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 }
970 }
971
972 case ISD::AND: {
973 unsigned Imm, Imm2, SH, MB, ME;
974
975 // If this is an and of a value rotated between 0 and 31 bits and then and'd
976 // with a mask, emit rlwinm
977 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000978 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000979 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000980 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
982 }
983 // If this is just a masked value where the input is not handled above, and
984 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
985 if (isInt32Immediate(N->getOperand(1), Imm) &&
986 isRunOfOnes(Imm, MB, ME) &&
987 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000988 SDValue Val = N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +0000989 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
991 }
992 // AND X, 0 -> 0, not "rlwinm 32".
993 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000994 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 return NULL;
996 }
997 // ISD::OR doesn't get all the bitfield insertion fun.
998 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
999 if (isInt32Immediate(N->getOperand(1), Imm) &&
1000 N->getOperand(0).getOpcode() == ISD::OR &&
1001 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1002 unsigned MB, ME;
1003 Imm = ~(Imm^Imm2);
1004 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001005 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 N->getOperand(0).getOperand(1),
1007 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Dale Johannesen913ba762009-02-06 01:31:28 +00001008 return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 }
1010 }
1011
1012 // Other cases are autogenerated.
1013 break;
1014 }
1015 case ISD::OR:
1016 if (N->getValueType(0) == MVT::i32)
1017 if (SDNode *I = SelectBitfieldInsert(N))
1018 return I;
1019
1020 // Other cases are autogenerated.
1021 break;
1022 case ISD::SHL: {
1023 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001024 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001026 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1028 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1029 }
1030
1031 // Other cases are autogenerated.
1032 break;
1033 }
1034 case ISD::SRL: {
1035 unsigned Imm, SH, MB, ME;
Gabor Greif1c80d112008-08-28 21:40:38 +00001036 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001038 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1040 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1041 }
1042
1043 // Other cases are autogenerated.
1044 break;
1045 }
1046 case ISD::SELECT_CC: {
1047 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1048
1049 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1050 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1051 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1052 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1053 if (N1C->isNullValue() && N3C->isNullValue() &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001054 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 // FIXME: Implement this optzn for PPC64.
1056 N->getValueType(0) == MVT::i32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 SDNode *Tmp =
Dale Johannesen913ba762009-02-06 01:31:28 +00001058 CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 N->getOperand(0), getI32Imm(~0U));
1060 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Dan Gohman8181bd12008-07-27 21:46:04 +00001061 SDValue(Tmp, 0), N->getOperand(0),
1062 SDValue(Tmp, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 }
1064
Dale Johannesen5d398a32009-02-06 19:16:40 +00001065 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 unsigned BROpc = getPredicateForSetCC(CC);
1067
1068 unsigned SelectCCOp;
1069 if (N->getValueType(0) == MVT::i32)
1070 SelectCCOp = PPC::SELECT_CC_I4;
1071 else if (N->getValueType(0) == MVT::i64)
1072 SelectCCOp = PPC::SELECT_CC_I8;
1073 else if (N->getValueType(0) == MVT::f32)
1074 SelectCCOp = PPC::SELECT_CC_F4;
1075 else if (N->getValueType(0) == MVT::f64)
1076 SelectCCOp = PPC::SELECT_CC_F8;
1077 else
1078 SelectCCOp = PPC::SELECT_CC_VRRC;
1079
Dan Gohman8181bd12008-07-27 21:46:04 +00001080 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081 getI32Imm(BROpc) };
1082 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1083 }
1084 case PPCISD::COND_BRANCH: {
Dan Gohmana1fb67a2008-11-05 17:16:24 +00001085 // Op #0 is the Chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 // Op #1 is the PPC::PRED_* number.
1087 // Op #2 is the CR#
1088 // Op #3 is the Dest MBB
Dan Gohmancc3df852008-11-05 04:14:16 +00001089 // Op #4 is the Flag.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman8181bd12008-07-27 21:46:04 +00001091 SDValue Pred =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001092 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00001093 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 N->getOperand(0), N->getOperand(4) };
1095 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1096 }
1097 case ISD::BR_CC: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesen5d398a32009-02-06 19:16:40 +00001099 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Dan Gohman8181bd12008-07-27 21:46:04 +00001100 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001101 N->getOperand(4), N->getOperand(0) };
1102 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1103 }
1104 case ISD::BRIND: {
1105 // FIXME: Should custom lower this.
Dan Gohman8181bd12008-07-27 21:46:04 +00001106 SDValue Chain = N->getOperand(0);
1107 SDValue Target = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Dale Johannesen913ba762009-02-06 01:31:28 +00001109 Chain = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Target,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 Chain), 0);
1111 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1112 }
Evan Chengb6facc42009-01-16 22:57:32 +00001113 case ISD::DECLARE: {
1114 SDValue Chain = N->getOperand(0);
1115 SDValue N1 = N->getOperand(1);
1116 SDValue N2 = N->getOperand(2);
1117 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattnerebf925e2009-02-12 17:37:15 +00001118
1119 // FIXME: We need to handle this for VLAs.
1120 if (!FINode) {
1121 ReplaceUses(Op.getValue(0), Chain);
1122 return NULL;
1123 }
1124
Evan Cheng27cec742009-01-19 18:31:51 +00001125 if (N2.getOpcode() == ISD::ADD) {
1126 if (N2.getOperand(0).getOpcode() == ISD::ADD &&
1127 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
1128 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Hi &&
1129 N2.getOperand(1).getOpcode() == PPCISD::Lo)
1130 N2 = N2.getOperand(0).getOperand(1).getOperand(0);
1131 else if (N2.getOperand(0).getOpcode() == ISD::ADD &&
Evan Chenga7482db2009-01-19 18:57:29 +00001132 N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
1133 N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Lo &&
Evan Cheng27cec742009-01-19 18:31:51 +00001134 N2.getOperand(1).getOpcode() == PPCISD::Hi)
1135 N2 = N2.getOperand(0).getOperand(1).getOperand(0);
1136 else if (N2.getOperand(0).getOpcode() == PPCISD::Hi &&
1137 N2.getOperand(1).getOpcode() == PPCISD::Lo)
1138 N2 = N2.getOperand(0).getOperand(0);
1139 }
Chris Lattnerebf925e2009-02-12 17:37:15 +00001140
1141 // If we don't have a global address here, the debug info is mangled, just
1142 // drop it.
1143 if (!isa<GlobalAddressSDNode>(N2)) {
1144 ReplaceUses(Op.getValue(0), Chain);
1145 return NULL;
1146 }
Evan Chengb6facc42009-01-16 22:57:32 +00001147 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1148 GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();
1149 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1150 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
1151 return CurDAG->SelectNodeTo(N, TargetInstrInfo::DECLARE,
1152 MVT::Other, Tmp1, Tmp2, Chain);
1153 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 }
1155
1156 return SelectCode(Op);
1157}
1158
1159
1160
1161/// createPPCISelDag - This pass converts a legalized DAG into a
1162/// PowerPC-specific DAG, ready for instruction scheduling.
1163///
1164FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1165 return new PPCDAGToDAGISel(TM);
1166}
1167