blob: e9a7ac9c7e05cc0e57540d7db509c13ad8873fed [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner6f3b9542005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
17#include "PPCISelLowering.h"
Chris Lattner2cab1352006-03-07 06:32:48 +000018#include "PPCHazardRecognizers.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner666512c2005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner45640392005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000028#include "llvm/Intrinsics.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000032#include <iostream>
Evan Chengb9d34bd2006-08-07 22:28:20 +000033#include <queue>
Evan Cheng54cb1832006-02-05 06:46:41 +000034#include <set>
Chris Lattner43ff01e2005-08-17 19:33:03 +000035using namespace llvm;
36
37namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000038 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
39
40 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000041 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000042 /// instructions for SelectionDAG operations.
43 ///
Chris Lattner2f8c2d82006-06-28 22:00:36 +000044 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner1678a6c2006-03-16 18:25:23 +000045 PPCTargetMachine &TM;
Nate Begeman6cca84e2005-10-16 05:39:50 +000046 PPCTargetLowering PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +000047 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000048 public:
Chris Lattner1678a6c2006-03-16 18:25:23 +000049 PPCDAGToDAGISel(PPCTargetMachine &tm)
50 : SelectionDAGISel(PPCLowering), TM(tm),
51 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattner43ff01e2005-08-17 19:33:03 +000052
Chris Lattner45640392005-08-19 22:38:53 +000053 virtual bool runOnFunction(Function &Fn) {
54 // Make sure we re-emit a set of the global base reg if necessary
55 GlobalBaseReg = 0;
Chris Lattner1678a6c2006-03-16 18:25:23 +000056 SelectionDAGISel::runOnFunction(Fn);
57
58 InsertVRSaveCode(Fn);
59 return true;
Chris Lattner45640392005-08-19 22:38:53 +000060 }
61
Chris Lattner43ff01e2005-08-17 19:33:03 +000062 /// getI32Imm - Return a target constant with the specified value, of type
63 /// i32.
64 inline SDOperand getI32Imm(unsigned Imm) {
65 return CurDAG->getTargetConstant(Imm, MVT::i32);
66 }
Chris Lattner45640392005-08-19 22:38:53 +000067
Chris Lattner97b3da12006-06-27 00:04:13 +000068 /// getI64Imm - Return a target constant with the specified value, of type
69 /// i64.
70 inline SDOperand getI64Imm(uint64_t Imm) {
71 return CurDAG->getTargetConstant(Imm, MVT::i64);
72 }
73
74 /// getSmallIPtrImm - Return a target constant of pointer type.
75 inline SDOperand getSmallIPtrImm(unsigned Imm) {
76 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
77 }
78
79
Chris Lattner45640392005-08-19 22:38:53 +000080 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
81 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +000082 SDNode *getGlobalBaseReg();
Chris Lattner43ff01e2005-08-17 19:33:03 +000083
84 // Select - Convert the specified operand from a target-independent to a
85 // target-specific node if it hasn't already been changed.
Evan Cheng61413a32006-08-26 05:34:46 +000086 SDNode *Select(SDOperand Op);
Chris Lattner43ff01e2005-08-17 19:33:03 +000087
Nate Begeman93c4bc62005-08-19 00:38:14 +000088 SDNode *SelectBitfieldInsert(SDNode *N);
89
Chris Lattner2a1823d2005-08-21 18:50:37 +000090 /// SelectCC - Select a comparison of the specified values with the
91 /// specified condition code, returning the CR# of the expression.
92 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
93
Nate Begeman8e6a8af2005-12-19 23:25:09 +000094 /// SelectAddrImm - Returns true if the address N can be represented by
95 /// a base register plus a signed 16-bit displacement [r+imm].
96 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
97
98 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
99 /// represented as an indexed [r+r] operation. Returns false if it can
100 /// be represented by [r+imm], which are preferred.
101 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begeman1064d6e2005-11-30 08:22:07 +0000102
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000103 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
104 /// represented as an indexed [r+r] operation.
105 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000106
Chris Lattner77373d12006-03-22 05:26:03 +0000107 /// SelectAddrImmShift - Returns true if the address N can be represented by
108 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
109 /// for use by STD and friends.
110 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
111
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000112 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
113 /// inline asm expressions.
114 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
115 char ConstraintCode,
116 std::vector<SDOperand> &OutOps,
117 SelectionDAG &DAG) {
118 SDOperand Op0, Op1;
119 switch (ConstraintCode) {
120 default: return true;
121 case 'm': // memory
122 if (!SelectAddrIdx(Op, Op0, Op1))
123 SelectAddrImm(Op, Op0, Op1);
124 break;
125 case 'o': // offsetable
126 if (!SelectAddrImm(Op, Op0, Op1)) {
Evan Chengab8297f2006-08-26 01:07:58 +0000127 Op0 = Op;
128 AddToISelQueue(Op0); // r+0.
Chris Lattner97b3da12006-06-27 00:04:13 +0000129 Op1 = getSmallIPtrImm(0);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000130 }
131 break;
132 case 'v': // not offsetable
133 SelectAddrIdxOnly(Op, Op0, Op1);
134 break;
135 }
136
137 OutOps.push_back(Op0);
138 OutOps.push_back(Op1);
139 return false;
140 }
141
Chris Lattner6e184f22005-08-25 22:04:30 +0000142 SDOperand BuildSDIVSequence(SDNode *N);
143 SDOperand BuildUDIVSequence(SDNode *N);
144
Chris Lattner43ff01e2005-08-17 19:33:03 +0000145 /// InstructionSelectBasicBlock - This callback is invoked by
146 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner259e6c72005-10-06 18:45:51 +0000147 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
148
Chris Lattner1678a6c2006-03-16 18:25:23 +0000149 void InsertVRSaveCode(Function &Fn);
150
Chris Lattner43ff01e2005-08-17 19:33:03 +0000151 virtual const char *getPassName() const {
152 return "PowerPC DAG->DAG Pattern Instruction Selection";
153 }
Chris Lattner2cab1352006-03-07 06:32:48 +0000154
Chris Lattnerf058f5a2006-05-16 23:54:25 +0000155 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
156 /// this target when scheduling the DAG.
Chris Lattner543832d2006-03-08 04:25:59 +0000157 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattner2cab1352006-03-07 06:32:48 +0000158 // Should use subtarget info to pick the right hazard recognizer. For
159 // now, always return a PPC970 recognizer.
Chris Lattner51348c52006-03-12 09:13:49 +0000160 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
161 assert(II && "No InstrInfo?");
162 return new PPCHazardRecognizer970(*II);
Chris Lattner2cab1352006-03-07 06:32:48 +0000163 }
Chris Lattner03e08ee2005-09-13 22:03:06 +0000164
165// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000166#include "PPCGenDAGISel.inc"
Chris Lattner259e6c72005-10-06 18:45:51 +0000167
168private:
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000169 SDNode *SelectSETCC(SDOperand Op);
170 SDNode *MySelect_PPCbctrl(SDOperand N);
171 SDNode *MySelect_PPCcall(SDOperand N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000172 };
173}
174
Chris Lattner259e6c72005-10-06 18:45:51 +0000175/// InstructionSelectBasicBlock - This callback is invoked by
176/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman0b71e002005-10-18 00:28:58 +0000177void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattner259e6c72005-10-06 18:45:51 +0000178 DEBUG(BB->dump());
Evan Chengf3008962006-07-27 06:40:15 +0000179
Chris Lattner259e6c72005-10-06 18:45:51 +0000180 // Select target instructions for the DAG.
Evan Cheng54cb1832006-02-05 06:46:41 +0000181 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattner259e6c72005-10-06 18:45:51 +0000182 DAG.RemoveDeadNodes();
183
Chris Lattner02e2c182006-03-13 21:52:10 +0000184 // Emit machine code to BB.
Chris Lattner259e6c72005-10-06 18:45:51 +0000185 ScheduleAndEmitDAG(DAG);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000186}
187
188/// InsertVRSaveCode - Once the entire function has been instruction selected,
189/// all virtual registers are created and all machine instructions are built,
190/// check to see if we need to save/restore VRSAVE. If so, do it.
191void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000192 // Check to see if this function uses vector registers, which means we have to
193 // save and restore the VRSAVE register and update it with the regs we use.
194 //
195 // In this case, there will be virtual registers of vector type type created
196 // by the scheduler. Detect them now.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000197 MachineFunction &Fn = MachineFunction::get(&F);
198 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner02e2c182006-03-13 21:52:10 +0000199 bool HasVectorVReg = false;
200 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnerab1ed2a2006-03-14 17:56:49 +0000201 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner02e2c182006-03-13 21:52:10 +0000202 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
203 HasVectorVReg = true;
204 break;
205 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000206 if (!HasVectorVReg) return; // nothing to do.
207
Chris Lattner02e2c182006-03-13 21:52:10 +0000208 // If we have a vector register, we want to emit code into the entry and exit
209 // blocks to save and restore the VRSAVE register. We do this here (instead
210 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
211 //
212 // 1. This (trivially) reduces the load on the register allocator, by not
213 // having to represent the live range of the VRSAVE register.
214 // 2. This (more significantly) allows us to create a temporary virtual
215 // register to hold the saved VRSAVE value, allowing this temporary to be
216 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000217
218 // Create two vregs - one to hold the VRSAVE register that is live-in to the
219 // function and one for the value after having bits or'd into it.
220 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
221 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
222
223 MachineBasicBlock &EntryBB = *Fn.begin();
224 // Emit the following code into the entry block:
225 // InVRSAVE = MFVRSAVE
226 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
227 // MTVRSAVE UpdatedVRSAVE
228 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
229 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
230 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
231 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
232
233 // Find all return blocks, outputting a restore in each epilog.
234 const TargetInstrInfo &TII = *TM.getInstrInfo();
235 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
236 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
237 IP = BB->end(); --IP;
238
239 // Skip over all terminator instructions, which are part of the return
240 // sequence.
241 MachineBasicBlock::iterator I2 = IP;
242 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
243 IP = I2;
244
245 // Emit: MTVRSAVE InVRSave
246 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
247 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000248 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000249}
Chris Lattner8ae95252005-09-03 01:17:22 +0000250
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251
Chris Lattner45640392005-08-19 22:38:53 +0000252/// getGlobalBaseReg - Output the instructions required to put the
253/// base address to use for accessing globals into a register.
254///
Evan Cheng61413a32006-08-26 05:34:46 +0000255SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000256 if (!GlobalBaseReg) {
257 // Insert the set of GlobalBaseReg into the first MBB of the function
258 MachineBasicBlock &FirstMBB = BB->getParent()->front();
259 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
260 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattner97b3da12006-06-27 00:04:13 +0000261
262 if (PPCLowering.getPointerTy() == MVT::i32)
263 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
264 else
265 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
266
Chris Lattner45640392005-08-19 22:38:53 +0000267 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
268 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
269 }
Evan Cheng61413a32006-08-26 05:34:46 +0000270 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy()).Val;
Chris Lattner97b3da12006-06-27 00:04:13 +0000271}
272
273/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
274/// or 64-bit immediate, and if the value can be accurately represented as a
275/// sign extension from a 16-bit value. If so, this returns true and the
276/// immediate.
277static bool isIntS16Immediate(SDNode *N, short &Imm) {
278 if (N->getOpcode() != ISD::Constant)
279 return false;
280
281 Imm = (short)cast<ConstantSDNode>(N)->getValue();
282 if (N->getValueType(0) == MVT::i32)
283 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
284 else
285 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
286}
287
288static bool isIntS16Immediate(SDOperand Op, short &Imm) {
289 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000290}
291
292
Chris Lattner97b3da12006-06-27 00:04:13 +0000293/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
294/// operand. If so Imm will receive the 32-bit value.
295static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
296 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman72d6f882005-08-18 05:00:13 +0000297 Imm = cast<ConstantSDNode>(N)->getValue();
298 return true;
299 }
300 return false;
301}
302
Chris Lattner97b3da12006-06-27 00:04:13 +0000303/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
304/// operand. If so Imm will receive the 64-bit value.
305static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000306 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000307 Imm = cast<ConstantSDNode>(N)->getValue();
308 return true;
309 }
310 return false;
311}
312
313// isInt32Immediate - This method tests to see if a constant operand.
314// If so Imm will receive the 32 bit value.
315static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
316 return isInt32Immediate(N.Val, Imm);
317}
318
319
320// isOpcWithIntImmediate - This method tests to see if the node is a specific
321// opcode and that it has a immediate integer right operand.
322// If so Imm will receive the 32 bit value.
323static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
324 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
325}
326
327
Nate Begemanb3821a32005-08-18 07:30:46 +0000328// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
329// any number of 0s on either side. The 1s are allowed to wrap from LSB to
330// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
331// not, since all 1s are not contiguous.
332static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
333 if (isShiftedMask_32(Val)) {
334 // look for the first non-zero bit
335 MB = CountLeadingZeros_32(Val);
336 // look for the first zero bit after the run of ones
337 ME = CountLeadingZeros_32((Val - 1) ^ Val);
338 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000339 } else {
340 Val = ~Val; // invert mask
341 if (isShiftedMask_32(Val)) {
342 // effectively look for the first zero bit
343 ME = CountLeadingZeros_32(Val) - 1;
344 // effectively look for the first one bit after the run of zeros
345 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
346 return true;
347 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000348 }
349 // no run present
350 return false;
351}
352
Chris Lattner89c7fa22005-10-09 05:36:17 +0000353// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemanb3821a32005-08-18 07:30:46 +0000354// and mask opcode and mask operation.
355static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
356 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000357 // Don't even go down this path for i64, since different logic will be
358 // necessary for rldicl/rldicr/rldimi.
359 if (N->getValueType(0) != MVT::i32)
360 return false;
361
Nate Begemanb3821a32005-08-18 07:30:46 +0000362 unsigned Shift = 32;
363 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
364 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000365 if (N->getNumOperands() != 2 ||
Chris Lattner97b3da12006-06-27 00:04:13 +0000366 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000367 return false;
368
369 if (Opcode == ISD::SHL) {
370 // apply shift left to mask if it comes first
371 if (IsShiftMask) Mask = Mask << Shift;
372 // determine which bits are made indeterminant by shift
373 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattnerefa38262005-10-15 21:40:12 +0000374 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000375 // apply shift right to mask if it comes first
376 if (IsShiftMask) Mask = Mask >> Shift;
377 // determine which bits are made indeterminant by shift
378 Indeterminant = ~(0xFFFFFFFFu >> Shift);
379 // adjust for the left rotate
380 Shift = 32 - Shift;
381 } else {
382 return false;
383 }
384
385 // if the mask doesn't intersect any Indeterminant bits
386 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000387 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000388 // make sure the mask is still a mask (wrap arounds may not be)
389 return isRunOfOnes(Mask, MB, ME);
390 }
391 return false;
392}
393
Nate Begeman93c4bc62005-08-19 00:38:14 +0000394/// SelectBitfieldInsert - turn an or of two masked values into
395/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000396SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman93c4bc62005-08-19 00:38:14 +0000397 SDOperand Op0 = N->getOperand(0);
398 SDOperand Op1 = N->getOperand(1);
399
Nate Begeman1333cea2006-05-07 00:23:38 +0000400 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000401 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
402 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000403
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000404 unsigned TargetMask = LKZ;
405 unsigned InsertMask = RKZ;
406
407 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
408 unsigned Op0Opc = Op0.getOpcode();
409 unsigned Op1Opc = Op1.getOpcode();
410 unsigned Value, SH = 0;
411 TargetMask = ~TargetMask;
412 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000413
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000414 // If the LHS has a foldable shift and the RHS does not, then swap it to the
415 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000416 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
417 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
418 Op0.getOperand(0).getOpcode() == ISD::SRL) {
419 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
420 Op1.getOperand(0).getOpcode() != ISD::SRL) {
421 std::swap(Op0, Op1);
422 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000423 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000424 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000425 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000426 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
427 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
428 Op1.getOperand(0).getOpcode() != ISD::SRL) {
429 std::swap(Op0, Op1);
430 std::swap(Op0Opc, Op1Opc);
431 std::swap(TargetMask, InsertMask);
432 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000433 }
Nate Begeman1333cea2006-05-07 00:23:38 +0000434
435 unsigned MB, ME;
Chris Lattnera2963392006-05-12 16:29:37 +0000436 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000437 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000438 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman1333cea2006-05-07 00:23:38 +0000439
440 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000441 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000442 Op1 = Op1.getOperand(0);
443 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
444 }
445 if (Op1Opc == ISD::AND) {
446 unsigned SHOpc = Op1.getOperand(0).getOpcode();
447 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000448 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000449 Op1 = Op1.getOperand(0).getOperand(0);
450 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
451 } else {
452 Op1 = Op1.getOperand(0);
453 }
454 }
455
456 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Evan Chengab8297f2006-08-26 01:07:58 +0000457 AddToISelQueue(Tmp3);
458 AddToISelQueue(Op1);
Chris Lattnera2963392006-05-12 16:29:37 +0000459 SH &= 31;
Evan Chengc3acfc02006-08-27 08:14:06 +0000460 SDOperand Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
461 getI32Imm(ME) };
462 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000463 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000464 }
465 return 0;
466}
467
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000468/// SelectAddrImm - Returns true if the address N can be represented by
469/// a base register plus a signed 16-bit displacement [r+imm].
470bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
471 SDOperand &Base) {
Chris Lattner60a60f42006-03-01 07:14:48 +0000472 // If this can be more profitably realized as r+r, fail.
473 if (SelectAddrIdx(N, Disp, Base))
474 return false;
475
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000476 if (N.getOpcode() == ISD::ADD) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000477 short imm = 0;
478 if (isIntS16Immediate(N.getOperand(1), imm)) {
479 Disp = getI32Imm((int)imm & 0xFFFF);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000480 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000481 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000482 } else {
Evan Chengbfa4b7c2006-02-05 08:45:01 +0000483 Base = N.getOperand(0);
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000484 }
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000485 return true; // [r+i]
486 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner0fe88e32005-11-17 18:02:16 +0000487 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000488 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner0fe88e32005-11-17 18:02:16 +0000489 && "Cannot handle constant offsets yet!");
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000490 Disp = N.getOperand(1).getOperand(0); // The global address.
491 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000492 Disp.getOpcode() == ISD::TargetConstantPool ||
493 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Chengbfa4b7c2006-02-05 08:45:01 +0000494 Base = N.getOperand(0);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000495 return true; // [&g+r]
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000496 }
Chris Lattner60a60f42006-03-01 07:14:48 +0000497 } else if (N.getOpcode() == ISD::OR) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000498 short imm = 0;
499 if (isIntS16Immediate(N.getOperand(1), imm)) {
Chris Lattner60a60f42006-03-01 07:14:48 +0000500 // If this is an or of disjoint bitfields, we can codegen this as an add
501 // (for better address arithmetic) if the LHS and RHS of the OR are
502 // provably disjoint.
503 uint64_t LHSKnownZero, LHSKnownOne;
504 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
505 LHSKnownZero, LHSKnownOne);
Chris Lattner97b3da12006-06-27 00:04:13 +0000506 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner60a60f42006-03-01 07:14:48 +0000507 // If all of the bits are known zero on the LHS or RHS, the add won't
508 // carry.
509 Base = N.getOperand(0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000510 Disp = getI32Imm((int)imm & 0xFFFF);
Chris Lattner60a60f42006-03-01 07:14:48 +0000511 return true;
512 }
513 }
Chris Lattnerc8b16d02006-03-20 22:38:22 +0000514 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
515 // Loading from a constant address.
Chris Lattner97b3da12006-06-27 00:04:13 +0000516
Chris Lattnerc8b16d02006-03-20 22:38:22 +0000517 // If this address fits entirely in a 16-bit sext immediate field, codegen
518 // this as "d, 0"
Chris Lattner97b3da12006-06-27 00:04:13 +0000519 short Imm;
520 if (isIntS16Immediate(CN, Imm)) {
521 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
522 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
Chris Lattnerc8b16d02006-03-20 22:38:22 +0000523 return true;
524 }
Chris Lattner97b3da12006-06-27 00:04:13 +0000525
526 // FIXME: Handle small sext constant offsets in PPC64 mode also!
527 if (CN->getValueType(0) == MVT::i32) {
528 int Addr = (int)CN->getValue();
Chris Lattnerc8b16d02006-03-20 22:38:22 +0000529
Chris Lattner97b3da12006-06-27 00:04:13 +0000530 // Otherwise, break this down into an LIS + disp.
531 Disp = getI32Imm((short)Addr);
532 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
533 return true;
534 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000535 }
Chris Lattnerc8b16d02006-03-20 22:38:22 +0000536
Chris Lattner97b3da12006-06-27 00:04:13 +0000537 Disp = getSmallIPtrImm(0);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000538 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattner97b3da12006-06-27 00:04:13 +0000539 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Nate Begeman4e56db62005-12-10 02:36:00 +0000540 else
Evan Chengbfa4b7c2006-02-05 08:45:01 +0000541 Base = N;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000542 return true; // [r+0]
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000543}
Chris Lattner43ff01e2005-08-17 19:33:03 +0000544
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000545/// SelectAddrIdx - Given the specified addressed, check to see if it can be
546/// represented as an indexed [r+r] operation. Returns false if it can
547/// be represented by [r+imm], which are preferred.
548bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
549 SDOperand &Index) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000550 short imm = 0;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000551 if (N.getOpcode() == ISD::ADD) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000552 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner60a60f42006-03-01 07:14:48 +0000553 return false; // r+i
554 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
555 return false; // r+i
556
Evan Chengbfa4b7c2006-02-05 08:45:01 +0000557 Base = N.getOperand(0);
558 Index = N.getOperand(1);
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000559 return true;
Chris Lattner60a60f42006-03-01 07:14:48 +0000560 } else if (N.getOpcode() == ISD::OR) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000561 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner60a60f42006-03-01 07:14:48 +0000562 return false; // r+i can fold it if we can.
563
564 // If this is an or of disjoint bitfields, we can codegen this as an add
565 // (for better address arithmetic) if the LHS and RHS of the OR are provably
566 // disjoint.
567 uint64_t LHSKnownZero, LHSKnownOne;
568 uint64_t RHSKnownZero, RHSKnownOne;
569 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
570 LHSKnownZero, LHSKnownOne);
571
572 if (LHSKnownZero) {
573 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
574 RHSKnownZero, RHSKnownOne);
575 // If all of the bits are known zero on the LHS or RHS, the add won't
576 // carry.
577 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
578 Base = N.getOperand(0);
579 Index = N.getOperand(1);
580 return true;
581 }
582 }
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000583 }
Chris Lattner60a60f42006-03-01 07:14:48 +0000584
585 return false;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000586}
587
588/// SelectAddrIdxOnly - Given the specified addressed, force it to be
589/// represented as an indexed [r+r] operation.
590bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
591 SDOperand &Index) {
Chris Lattner60a60f42006-03-01 07:14:48 +0000592 // Check to see if we can easily represent this as an [r+r] address. This
593 // will fail if it thinks that the address is more profitably represented as
594 // reg+imm, e.g. where imm = 0.
Chris Lattnerf2286d52006-03-24 17:58:06 +0000595 if (SelectAddrIdx(N, Base, Index))
596 return true;
597
598 // If the operand is an addition, always emit this as [r+r], since this is
599 // better (for code size, and execution, as the memop does the add for free)
600 // than emitting an explicit add.
601 if (N.getOpcode() == ISD::ADD) {
602 Base = N.getOperand(0);
603 Index = N.getOperand(1);
604 return true;
Nate Begeman1064d6e2005-11-30 08:22:07 +0000605 }
Chris Lattnerf2286d52006-03-24 17:58:06 +0000606
607 // Otherwise, do it the hard way, using R0 as the base register.
Chris Lattner97b3da12006-06-27 00:04:13 +0000608 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
Chris Lattnerf2286d52006-03-24 17:58:06 +0000609 Index = N;
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000610 return true;
Nate Begeman1064d6e2005-11-30 08:22:07 +0000611}
612
Chris Lattner77373d12006-03-22 05:26:03 +0000613/// SelectAddrImmShift - Returns true if the address N can be represented by
614/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
615/// for use by STD and friends.
616bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
617 SDOperand &Base) {
618 // If this can be more profitably realized as r+r, fail.
619 if (SelectAddrIdx(N, Disp, Base))
620 return false;
621
622 if (N.getOpcode() == ISD::ADD) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000623 short imm = 0;
624 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
625 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattner77373d12006-03-22 05:26:03 +0000626 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000627 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner77373d12006-03-22 05:26:03 +0000628 } else {
629 Base = N.getOperand(0);
630 }
631 return true; // [r+i]
632 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
633 // Match LOAD (ADD (X, Lo(G))).
634 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
635 && "Cannot handle constant offsets yet!");
636 Disp = N.getOperand(1).getOperand(0); // The global address.
637 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000638 Disp.getOpcode() == ISD::TargetConstantPool ||
639 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattner77373d12006-03-22 05:26:03 +0000640 Base = N.getOperand(0);
641 return true; // [&g+r]
642 }
643 } else if (N.getOpcode() == ISD::OR) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000644 short imm = 0;
645 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Chris Lattner77373d12006-03-22 05:26:03 +0000646 // If this is an or of disjoint bitfields, we can codegen this as an add
647 // (for better address arithmetic) if the LHS and RHS of the OR are
648 // provably disjoint.
649 uint64_t LHSKnownZero, LHSKnownOne;
650 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
651 LHSKnownZero, LHSKnownOne);
Chris Lattner97b3da12006-06-27 00:04:13 +0000652 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner77373d12006-03-22 05:26:03 +0000653 // If all of the bits are known zero on the LHS or RHS, the add won't
654 // carry.
655 Base = N.getOperand(0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000656 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattner77373d12006-03-22 05:26:03 +0000657 return true;
658 }
659 }
660 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
661 // Loading from a constant address.
Chris Lattner97b3da12006-06-27 00:04:13 +0000662
663 // If this address fits entirely in a 14-bit sext immediate field, codegen
664 // this as "d, 0"
665 short Imm;
666 if (isIntS16Immediate(CN, Imm)) {
667 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
668 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
669 return true;
670 }
671
672 // FIXME: Handle small sext constant offsets in PPC64 mode also!
673 if (CN->getValueType(0) == MVT::i32) {
674 int Addr = (int)CN->getValue();
Chris Lattner77373d12006-03-22 05:26:03 +0000675
676 // Otherwise, break this down into an LIS + disp.
677 Disp = getI32Imm((short)Addr >> 2);
678 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
679 return true;
680 }
681 }
682
Chris Lattner97b3da12006-06-27 00:04:13 +0000683 Disp = getSmallIPtrImm(0);
Chris Lattner77373d12006-03-22 05:26:03 +0000684 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattner97b3da12006-06-27 00:04:13 +0000685 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner77373d12006-03-22 05:26:03 +0000686 else
687 Base = N;
688 return true; // [r+0]
689}
690
691
Chris Lattner2a1823d2005-08-21 18:50:37 +0000692/// SelectCC - Select a comparison of the specified values with the specified
693/// condition code, returning the CR# of the expression.
Nate Begeman0b71e002005-10-18 00:28:58 +0000694SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
695 ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000696 // Always select the LHS.
Evan Chengab8297f2006-08-26 01:07:58 +0000697 AddToISelQueue(LHS);
Chris Lattner97b3da12006-06-27 00:04:13 +0000698 unsigned Opc;
699
700 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000701 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000702 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
703 if (isInt32Immediate(RHS, Imm)) {
704 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
705 if (isUInt16(Imm))
706 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
707 getI32Imm(Imm & 0xFFFF)), 0);
708 // If this is a 16-bit signed immediate, fold it.
709 if (isInt16(Imm))
710 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
711 getI32Imm(Imm & 0xFFFF)), 0);
712
713 // For non-equality comparisons, the default code would materialize the
714 // constant, then compare against it, like this:
715 // lis r2, 4660
716 // ori r2, r2, 22136
717 // cmpw cr0, r3, r2
718 // Since we are just comparing for equality, we can emit this instead:
719 // xoris r0,r3,0x1234
720 // cmplwi cr0,r0,0x5678
721 // beq cr0,L6
722 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS, MVT::i32, LHS,
723 getI32Imm(Imm >> 16)), 0);
724 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, Xor,
725 getI32Imm(Imm & 0xFFFF)), 0);
726 }
727 Opc = PPC::CMPLW;
728 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000729 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
730 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
731 getI32Imm(Imm & 0xFFFF)), 0);
732 Opc = PPC::CMPLW;
733 } else {
734 short SImm;
735 if (isIntS16Immediate(RHS, SImm))
736 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
737 getI32Imm((int)SImm & 0xFFFF)),
738 0);
739 Opc = PPC::CMPW;
740 }
741 } else if (LHS.getValueType() == MVT::i64) {
742 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000743 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
744 if (isInt64Immediate(RHS.Val, Imm)) {
745 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
746 if (isUInt16(Imm))
747 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
748 getI32Imm(Imm & 0xFFFF)), 0);
749 // If this is a 16-bit signed immediate, fold it.
750 if (isInt16(Imm))
751 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
752 getI32Imm(Imm & 0xFFFF)), 0);
753
754 // For non-equality comparisons, the default code would materialize the
755 // constant, then compare against it, like this:
756 // lis r2, 4660
757 // ori r2, r2, 22136
758 // cmpd cr0, r3, r2
759 // Since we are just comparing for equality, we can emit this instead:
760 // xoris r0,r3,0x1234
761 // cmpldi cr0,r0,0x5678
762 // beq cr0,L6
763 if (isUInt32(Imm)) {
764 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS8, MVT::i64, LHS,
765 getI64Imm(Imm >> 16)), 0);
766 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, Xor,
767 getI64Imm(Imm & 0xFFFF)), 0);
768 }
769 }
770 Opc = PPC::CMPLD;
771 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000772 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
773 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
774 getI64Imm(Imm & 0xFFFF)), 0);
775 Opc = PPC::CMPLD;
776 } else {
777 short SImm;
778 if (isIntS16Immediate(RHS, SImm))
779 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000780 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000781 0);
782 Opc = PPC::CMPD;
783 }
Chris Lattnerd3eee1a2005-10-01 01:35:02 +0000784 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000785 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000786 } else {
Chris Lattner97b3da12006-06-27 00:04:13 +0000787 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
788 Opc = PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000789 }
Evan Chengab8297f2006-08-26 01:07:58 +0000790 AddToISelQueue(RHS);
Chris Lattner97b3da12006-06-27 00:04:13 +0000791 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000792}
793
794/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
795/// to Condition.
796static unsigned getBCCForSetCC(ISD::CondCode CC) {
797 switch (CC) {
798 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnerf8899a62005-10-28 20:49:47 +0000799 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner630bbce2006-05-25 16:54:16 +0000800 case ISD::SETUEQ:
Chris Lattner2a1823d2005-08-21 18:50:37 +0000801 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000802 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner630bbce2006-05-25 16:54:16 +0000803 case ISD::SETUNE:
Chris Lattner2a1823d2005-08-21 18:50:37 +0000804 case ISD::SETNE: return PPC::BNE;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000805 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2a1823d2005-08-21 18:50:37 +0000806 case ISD::SETULT:
807 case ISD::SETLT: return PPC::BLT;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000808 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2a1823d2005-08-21 18:50:37 +0000809 case ISD::SETULE:
810 case ISD::SETLE: return PPC::BLE;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000811 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2a1823d2005-08-21 18:50:37 +0000812 case ISD::SETUGT:
813 case ISD::SETGT: return PPC::BGT;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000814 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2a1823d2005-08-21 18:50:37 +0000815 case ISD::SETUGE:
816 case ISD::SETGE: return PPC::BGE;
Chris Lattner5d6cb602005-10-28 20:32:44 +0000817
818 case ISD::SETO: return PPC::BUN;
819 case ISD::SETUO: return PPC::BNU;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000820 }
821 return 0;
822}
823
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000824/// getCRIdxForSetCC - Return the index of the condition register field
825/// associated with the SetCC condition, and whether or not the field is
826/// treated as inverted. That is, lt = 0; ge = 0 inverted.
827static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
828 switch (CC) {
829 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnerf8899a62005-10-28 20:49:47 +0000830 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000831 case ISD::SETULT:
832 case ISD::SETLT: Inv = false; return 0;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000833 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000834 case ISD::SETUGE:
835 case ISD::SETGE: Inv = true; return 0;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000836 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000837 case ISD::SETUGT:
838 case ISD::SETGT: Inv = false; return 1;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000839 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000840 case ISD::SETULE:
841 case ISD::SETLE: Inv = true; return 1;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000842 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000843 case ISD::SETUEQ:
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000844 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnerf8899a62005-10-28 20:49:47 +0000845 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000846 case ISD::SETUNE:
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000847 case ISD::SETNE: Inv = true; return 2;
Chris Lattner5d6cb602005-10-28 20:32:44 +0000848 case ISD::SETO: Inv = true; return 3;
849 case ISD::SETUO: Inv = false; return 3;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000850 }
851 return 0;
852}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000853
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000854SDNode *PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner491b8292005-10-06 19:03:35 +0000855 SDNode *N = Op.Val;
856 unsigned Imm;
857 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattner97b3da12006-06-27 00:04:13 +0000858 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000859 // We can codegen setcc op, imm very efficiently compared to a brcond.
860 // Check for those cases here.
861 // setcc op, 0
862 if (Imm == 0) {
Evan Chengab8297f2006-08-26 01:07:58 +0000863 SDOperand Op = N->getOperand(0);
864 AddToISelQueue(Op);
Chris Lattner491b8292005-10-06 19:03:35 +0000865 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000866 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000867 case ISD::SETEQ: {
Evan Chengd1b82d82006-02-09 07:17:49 +0000868 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Evan Chengc3acfc02006-08-27 08:14:06 +0000869 SDOperand Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
870 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
871 }
Chris Lattnere2969492005-10-21 21:17:10 +0000872 case ISD::SETNE: {
Evan Chengd1b82d82006-02-09 07:17:49 +0000873 SDOperand AD =
874 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
875 Op, getI32Imm(~0U)), 0);
Chris Lattnere3189772005-11-30 22:53:06 +0000876 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000877 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000878 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000879 case ISD::SETLT: {
880 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
881 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
882 }
Chris Lattnere2969492005-10-21 21:17:10 +0000883 case ISD::SETGT: {
Evan Chengd1b82d82006-02-09 07:17:49 +0000884 SDOperand T =
885 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
886 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Evan Chengc3acfc02006-08-27 08:14:06 +0000887 SDOperand Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
888 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000889 }
890 }
Chris Lattner491b8292005-10-06 19:03:35 +0000891 } else if (Imm == ~0U) { // setcc op, -1
Evan Chengab8297f2006-08-26 01:07:58 +0000892 SDOperand Op = N->getOperand(0);
893 AddToISelQueue(Op);
Chris Lattner491b8292005-10-06 19:03:35 +0000894 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000895 default: break;
896 case ISD::SETEQ:
Evan Chengd1b82d82006-02-09 07:17:49 +0000897 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
898 Op, getI32Imm(1)), 0);
Chris Lattnere3189772005-11-30 22:53:06 +0000899 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Chengd1b82d82006-02-09 07:17:49 +0000900 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
901 getI32Imm(0)), 0),
Evan Cheng34b70ee2006-08-26 08:00:10 +0000902 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000903 case ISD::SETNE: {
Evan Chengd1b82d82006-02-09 07:17:49 +0000904 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
905 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
906 Op, getI32Imm(~0U));
Chris Lattnerf058f5a2006-05-16 23:54:25 +0000907 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
Evan Cheng34b70ee2006-08-26 08:00:10 +0000908 Op, SDOperand(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000909 }
Chris Lattnere2969492005-10-21 21:17:10 +0000910 case ISD::SETLT: {
Evan Chengd1b82d82006-02-09 07:17:49 +0000911 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
912 getI32Imm(1)), 0);
913 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
914 Op), 0);
Evan Chengc3acfc02006-08-27 08:14:06 +0000915 SDOperand Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
916 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000917 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000918 case ISD::SETGT: {
919 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
920 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000921 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000922 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000923 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000924 }
Chris Lattner491b8292005-10-06 19:03:35 +0000925 }
926 }
927
928 bool Inv;
929 unsigned Idx = getCRIdxForSetCC(CC, Inv);
930 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
931 SDOperand IntCR;
932
933 // Force the ccreg into CR7.
934 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
935
Chris Lattnerde085f02005-12-06 20:56:18 +0000936 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerbd099102005-12-01 03:50:19 +0000937 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
938 InFlag).getValue(1);
Chris Lattner491b8292005-10-06 19:03:35 +0000939
940 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Chengd1b82d82006-02-09 07:17:49 +0000941 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
942 CCReg), 0);
Chris Lattner491b8292005-10-06 19:03:35 +0000943 else
Evan Chengd1b82d82006-02-09 07:17:49 +0000944 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner491b8292005-10-06 19:03:35 +0000945
Evan Chengc3acfc02006-08-27 08:14:06 +0000946 SDOperand Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
947 getI32Imm(31), getI32Imm(31) };
Chris Lattner491b8292005-10-06 19:03:35 +0000948 if (!Inv) {
Evan Chengc3acfc02006-08-27 08:14:06 +0000949 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner491b8292005-10-06 19:03:35 +0000950 } else {
951 SDOperand Tmp =
Evan Chengc3acfc02006-08-27 08:14:06 +0000952 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Evan Cheng34b70ee2006-08-26 08:00:10 +0000953 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000954 }
Chris Lattner491b8292005-10-06 19:03:35 +0000955}
Chris Lattner502a3692005-10-06 18:56:10 +0000956
Chris Lattner318622f2005-10-06 19:07:45 +0000957
Chris Lattner43ff01e2005-08-17 19:33:03 +0000958// Select - Convert the specified operand from a target-independent to a
959// target-specific node if it hasn't already been changed.
Evan Cheng61413a32006-08-26 05:34:46 +0000960SDNode *PPCDAGToDAGISel::Select(SDOperand Op) {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000961 SDNode *N = Op.Val;
Chris Lattnerb2854fa2005-08-26 20:25:03 +0000962 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng61413a32006-08-26 05:34:46 +0000963 N->getOpcode() < PPCISD::FIRST_NUMBER)
Evan Chengbd1c5a82006-08-11 09:08:15 +0000964 return NULL; // Already selected.
Chris Lattner08c319f2005-09-29 00:59:32 +0000965
Chris Lattner43ff01e2005-08-17 19:33:03 +0000966 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000967 default: break;
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000968 case ISD::SETCC:
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000969 return SelectSETCC(Op);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000970 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000971 return getGlobalBaseReg();
Chris Lattner595088a2005-11-17 07:30:41 +0000972
Chris Lattnere4c338d2005-08-25 00:45:43 +0000973 case ISD::FrameIndex: {
974 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner97b3da12006-06-27 00:04:13 +0000975 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
976 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000977 if (N->hasOneUse())
978 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000979 getSmallIPtrImm(0));
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000980 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
981 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +0000982 }
Chris Lattner6961fc72006-03-26 10:06:40 +0000983
984 case PPCISD::MFCR: {
Evan Chengab8297f2006-08-26 01:07:58 +0000985 SDOperand InFlag = N->getOperand(1);
986 AddToISelQueue(InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +0000987 // Use MFOCRF if supported.
988 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000989 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
990 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +0000991 else
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000992 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +0000993 }
994
Chris Lattner57693112005-09-28 22:50:24 +0000995 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +0000996 // FIXME: since this depends on the setting of the carry flag from the srawi
997 // we should really be making notes about that for the scheduler.
998 // FIXME: It sure would be nice if we could cheaply recognize the
999 // srl/add/sra pattern the dag combiner will generate for this as
1000 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001001 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001002 if (isInt32Immediate(N->getOperand(1), Imm)) {
Evan Chengab8297f2006-08-26 01:07:58 +00001003 SDOperand N0 = N->getOperand(0);
1004 AddToISelQueue(N0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001005 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001006 SDNode *Op =
Chris Lattnerdc664572005-08-25 17:50:06 +00001007 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001008 N0, getI32Imm(Log2_32(Imm)));
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001009 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001010 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001011 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001012 SDNode *Op =
Chris Lattner45706e92005-08-30 17:13:58 +00001013 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001014 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattnerdc664572005-08-25 17:50:06 +00001015 SDOperand PT =
Evan Chengd1b82d82006-02-09 07:17:49 +00001016 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1017 SDOperand(Op, 0), SDOperand(Op, 1)),
1018 0);
Evan Cheng34b70ee2006-08-26 08:00:10 +00001019 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001020 }
1021 }
Chris Lattner6e184f22005-08-25 22:04:30 +00001022
Chris Lattner1de57062005-09-29 23:33:31 +00001023 // Other cases are autogenerated.
1024 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001025 }
Nate Begemanb3821a32005-08-18 07:30:46 +00001026 case ISD::AND: {
Nate Begeman9aea6e42005-12-24 01:00:15 +00001027 unsigned Imm, Imm2;
Nate Begemanb3821a32005-08-18 07:30:46 +00001028 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1029 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001030 if (isInt32Immediate(N->getOperand(1), Imm) &&
1031 (isShiftedMask_32(Imm) || isShiftedMask_32(~Imm))) {
Nate Begemanb3821a32005-08-18 07:30:46 +00001032 SDOperand Val;
Nate Begemand3263872005-08-18 18:01:39 +00001033 unsigned SH, MB, ME;
Nate Begemanb3821a32005-08-18 07:30:46 +00001034 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Chengab8297f2006-08-26 01:07:58 +00001035 Val = N->getOperand(0).getOperand(0);
1036 AddToISelQueue(Val);
Chris Lattnere1fd05e2005-10-25 19:32:37 +00001037 } else if (Imm == 0) {
1038 // AND X, 0 -> 0, not "rlwinm 32".
Evan Chengab8297f2006-08-26 01:07:58 +00001039 AddToISelQueue(N->getOperand(1));
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001040 ReplaceUses(SDOperand(N, 0), N->getOperand(1));
Evan Chengbd1c5a82006-08-11 09:08:15 +00001041 return NULL;
Chris Lattnere1fd05e2005-10-25 19:32:37 +00001042 } else {
Evan Chengab8297f2006-08-26 01:07:58 +00001043 Val = N->getOperand(0);
1044 AddToISelQueue(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +00001045 isRunOfOnes(Imm, MB, ME);
1046 SH = 0;
1047 }
Evan Chengc3acfc02006-08-27 08:14:06 +00001048 SDOperand Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1049 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemanb3821a32005-08-18 07:30:46 +00001050 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001051 // ISD::OR doesn't get all the bitfield insertion fun.
1052 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattner97b3da12006-06-27 00:04:13 +00001053 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001054 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001055 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001056 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001057 Imm = ~(Imm^Imm2);
1058 if (isRunOfOnes(Imm, MB, ME)) {
Evan Chengab8297f2006-08-26 01:07:58 +00001059 AddToISelQueue(N->getOperand(0).getOperand(0));
1060 AddToISelQueue(N->getOperand(0).getOperand(1));
Evan Chengc3acfc02006-08-27 08:14:06 +00001061 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1062 N->getOperand(0).getOperand(1),
1063 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1064 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001065 }
1066 }
Chris Lattner1de57062005-09-29 23:33:31 +00001067
1068 // Other cases are autogenerated.
1069 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001070 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001071 case ISD::OR:
Chris Lattnerca9c4882006-06-27 21:08:52 +00001072 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001073 if (SDNode *I = SelectBitfieldInsert(N))
1074 return I;
Chris Lattner08c319f2005-09-29 00:59:32 +00001075
Chris Lattner1de57062005-09-29 23:33:31 +00001076 // Other cases are autogenerated.
1077 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001078 case ISD::SHL: {
1079 unsigned Imm, SH, MB, ME;
1080 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001081 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Chengab8297f2006-08-26 01:07:58 +00001082 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Chengc3acfc02006-08-27 08:14:06 +00001083 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1084 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1085 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001086 }
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001087
1088 // Other cases are autogenerated.
1089 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001090 }
1091 case ISD::SRL: {
1092 unsigned Imm, SH, MB, ME;
1093 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001094 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Chengab8297f2006-08-26 01:07:58 +00001095 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Chengc3acfc02006-08-27 08:14:06 +00001096 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1097 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1098 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001099 }
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001100
1101 // Other cases are autogenerated.
1102 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001103 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001104 case ISD::SELECT_CC: {
1105 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1106
Chris Lattner97b3da12006-06-27 00:04:13 +00001107 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattnerbec817c2005-08-26 18:46:49 +00001108 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1109 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1110 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1111 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001112 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1113 // FIXME: Implement this optzn for PPC64.
1114 N->getValueType(0) == MVT::i32) {
Evan Chengab8297f2006-08-26 01:07:58 +00001115 AddToISelQueue(N->getOperand(0));
Evan Chengd1b82d82006-02-09 07:17:49 +00001116 SDNode *Tmp =
Chris Lattnerbec817c2005-08-26 18:46:49 +00001117 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Chengab8297f2006-08-26 01:07:58 +00001118 N->getOperand(0), getI32Imm(~0U));
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001119 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Evan Chengab8297f2006-08-26 01:07:58 +00001120 SDOperand(Tmp, 0), N->getOperand(0),
Evan Cheng34b70ee2006-08-26 08:00:10 +00001121 SDOperand(Tmp, 1));
Chris Lattnerbec817c2005-08-26 18:46:49 +00001122 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001123
Chris Lattner34182af2005-09-01 19:20:44 +00001124 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001125 unsigned BROpc = getBCCForSetCC(CC);
1126
1127 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001128 unsigned SelectCCOp;
Chris Lattner97b3da12006-06-27 00:04:13 +00001129 if (N->getValueType(0) == MVT::i32)
1130 SelectCCOp = PPC::SELECT_CC_I4;
1131 else if (N->getValueType(0) == MVT::i64)
1132 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001133 else if (N->getValueType(0) == MVT::f32)
1134 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001135 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001136 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001137 else
1138 SelectCCOp = PPC::SELECT_CC_VRRC;
1139
Evan Chengab8297f2006-08-26 01:07:58 +00001140 AddToISelQueue(N->getOperand(2));
1141 AddToISelQueue(N->getOperand(3));
Evan Chengc3acfc02006-08-27 08:14:06 +00001142 SDOperand Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1143 getI32Imm(BROpc) };
1144 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001145 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001146 case ISD::BR_CC: {
Evan Chengab8297f2006-08-26 01:07:58 +00001147 AddToISelQueue(N->getOperand(0));
Chris Lattner2a1823d2005-08-21 18:50:37 +00001148 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1149 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Evan Chengc3acfc02006-08-27 08:14:06 +00001150 SDOperand Ops[] = { CondCode, getI32Imm(getBCCForSetCC(CC)),
1151 N->getOperand(4), N->getOperand(0) };
1152 return CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, Ops, 4);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001153 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001154 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001155 // FIXME: Should custom lower this.
Evan Chengab8297f2006-08-26 01:07:58 +00001156 SDOperand Chain = N->getOperand(0);
1157 SDOperand Target = N->getOperand(1);
1158 AddToISelQueue(Chain);
1159 AddToISelQueue(Target);
Chris Lattnerf882c542006-06-27 20:46:17 +00001160 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1161 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001162 Chain), 0);
Evan Cheng34b70ee2006-08-26 08:00:10 +00001163 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001164 }
Chris Lattnerb055c872006-06-10 01:15:02 +00001165 // FIXME: These are manually selected because tblgen isn't handling varargs
1166 // nodes correctly.
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001167 case PPCISD::BCTRL: return MySelect_PPCbctrl(Op);
1168 case PPCISD::CALL: return MySelect_PPCcall(Op);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001169 }
Chris Lattner5f12cf12005-09-03 00:53:47 +00001170
Evan Cheng61413a32006-08-26 05:34:46 +00001171 return SelectCode(Op);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001172}
1173
1174
Chris Lattnerb055c872006-06-10 01:15:02 +00001175// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1176// correctly.
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001177SDNode *PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand N) {
Chris Lattnerb055c872006-06-10 01:15:02 +00001178 SDOperand Chain(0, 0);
Chris Lattnerb055c872006-06-10 01:15:02 +00001179
1180 bool hasFlag =
1181 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1182
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001183 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb055c872006-06-10 01:15:02 +00001184 // Push varargs arguments, including optional flag.
1185 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Chengab8297f2006-08-26 01:07:58 +00001186 Chain = N.getOperand(i);
1187 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001188 Ops.push_back(Chain);
1189 }
1190
Evan Chengab8297f2006-08-26 01:07:58 +00001191 Chain = N.getOperand(0);
1192 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001193 Ops.push_back(Chain);
1194
1195 if (hasFlag) {
Evan Chengab8297f2006-08-26 01:07:58 +00001196 Chain = N.getOperand(N.getNumOperands()-1);
1197 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001198 Ops.push_back(Chain);
1199 }
1200
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001201 return CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag,
1202 &Ops[0], Ops.size());
Chris Lattnerb055c872006-06-10 01:15:02 +00001203}
1204
1205// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1206// correctly.
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001207SDNode *PPCDAGToDAGISel::MySelect_PPCcall(SDOperand N) {
Chris Lattnerb055c872006-06-10 01:15:02 +00001208 SDOperand Chain(0, 0);
Chris Lattnerb055c872006-06-10 01:15:02 +00001209 SDOperand N1(0, 0);
1210 SDOperand Tmp0(0, 0);
1211 SDNode *ResNode;
1212 Chain = N.getOperand(0);
1213 N1 = N.getOperand(1);
1214
1215 // Pattern: (PPCcall:void (imm:i32):$func)
1216 // Emits: (BLA:void (imm:i32):$func)
1217 // Pattern complexity = 4 cost = 1
1218 if (N1.getOpcode() == ISD::Constant) {
1219 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1220
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001221 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb055c872006-06-10 01:15:02 +00001222 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1223
1224 bool hasFlag =
1225 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1226
1227 // Push varargs arguments, not including optional flag.
1228 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Chengab8297f2006-08-26 01:07:58 +00001229 Chain = N.getOperand(i);
1230 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001231 Ops.push_back(Chain);
1232 }
Evan Chengab8297f2006-08-26 01:07:58 +00001233 Chain = N.getOperand(0);
1234 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001235 Ops.push_back(Chain);
1236 if (hasFlag) {
Evan Chengab8297f2006-08-26 01:07:58 +00001237 Chain = N.getOperand(N.getNumOperands()-1);
1238 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001239 Ops.push_back(Chain);
1240 }
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001241 return CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag,
1242 &Ops[0], Ops.size());
Chris Lattnerb055c872006-06-10 01:15:02 +00001243 }
1244
1245 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1246 // Emits: (BL:void (tglobaladdr:i32):$dst)
1247 // Pattern complexity = 4 cost = 1
1248 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001249 SmallVector<SDOperand, 8> Ops;
Chris Lattnerb055c872006-06-10 01:15:02 +00001250 Ops.push_back(N1);
1251
1252 bool hasFlag =
1253 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1254
1255 // Push varargs arguments, not including optional flag.
1256 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Chengab8297f2006-08-26 01:07:58 +00001257 Chain = N.getOperand(i);
1258 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001259 Ops.push_back(Chain);
1260 }
Evan Chengab8297f2006-08-26 01:07:58 +00001261 Chain = N.getOperand(0);
1262 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001263 Ops.push_back(Chain);
1264 if (hasFlag) {
Evan Chengab8297f2006-08-26 01:07:58 +00001265 Chain = N.getOperand(N.getNumOperands()-1);
1266 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001267 Ops.push_back(Chain);
1268 }
1269
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001270 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1271 &Ops[0], Ops.size());
Chris Lattnerb055c872006-06-10 01:15:02 +00001272 }
1273
1274 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1275 // Emits: (BL:void (texternalsym:i32):$dst)
1276 // Pattern complexity = 4 cost = 1
1277 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1278 std::vector<SDOperand> Ops;
1279 Ops.push_back(N1);
1280
1281 bool hasFlag =
1282 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1283
1284 // Push varargs arguments, not including optional flag.
1285 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Chengab8297f2006-08-26 01:07:58 +00001286 Chain = N.getOperand(i);
1287 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001288 Ops.push_back(Chain);
1289 }
Evan Chengab8297f2006-08-26 01:07:58 +00001290 Chain = N.getOperand(0);
1291 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001292 Ops.push_back(Chain);
1293 if (hasFlag) {
Evan Chengab8297f2006-08-26 01:07:58 +00001294 Chain = N.getOperand(N.getNumOperands()-1);
1295 AddToISelQueue(Chain);
Chris Lattnerb055c872006-06-10 01:15:02 +00001296 Ops.push_back(Chain);
1297 }
1298
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001299 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1300 &Ops[0], Ops.size());
Chris Lattnerb055c872006-06-10 01:15:02 +00001301 }
1302 std::cerr << "Cannot yet select: ";
1303 N.Val->dump(CurDAG);
1304 std::cerr << '\n';
1305 abort();
Evan Chengbd1c5a82006-08-11 09:08:15 +00001306
1307 return NULL;
Chris Lattnerb055c872006-06-10 01:15:02 +00001308}
1309
1310
Nate Begeman0b71e002005-10-18 00:28:58 +00001311/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00001312/// PowerPC-specific DAG, ready for instruction scheduling.
1313///
Evan Cheng2dd2c652006-03-13 23:20:37 +00001314FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00001315 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001316}
1317