blob: 2bfaa2d72ebbfca78545fbc87900ebb33811a3b2 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattnera5a91b12005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattnera5a91b12005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
17#include "PPCISelLowering.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "PPCHazardRecognizers.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000028#include "llvm/Intrinsics.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000031#include "llvm/Support/Compiler.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000033#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000034#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000035using namespace llvm;
36
37namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000038 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
39
40 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000042 /// instructions for SelectionDAG operations.
43 ///
Chris Lattner2a41a982006-06-28 22:00:36 +000044 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000045 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000046 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000047 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000048 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000049 PPCDAGToDAGISel(PPCTargetMachine &tm)
50 : SelectionDAGISel(PPCLowering), TM(tm),
51 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000052
Chris Lattner4416f1a2005-08-19 22:38:53 +000053 virtual bool runOnFunction(Function &Fn) {
54 // Make sure we re-emit a set of the global base reg if necessary
55 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000056 SelectionDAGISel::runOnFunction(Fn);
57
58 InsertVRSaveCode(Fn);
59 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000060 }
61
Chris Lattnera5a91b12005-08-17 19:33:03 +000062 /// getI32Imm - Return a target constant with the specified value, of type
63 /// i32.
64 inline SDOperand getI32Imm(unsigned Imm) {
65 return CurDAG->getTargetConstant(Imm, MVT::i32);
66 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000067
Chris Lattnerc08f9022006-06-27 00:04:13 +000068 /// getI64Imm - Return a target constant with the specified value, of type
69 /// i64.
70 inline SDOperand getI64Imm(uint64_t Imm) {
71 return CurDAG->getTargetConstant(Imm, MVT::i64);
72 }
73
74 /// getSmallIPtrImm - Return a target constant of pointer type.
75 inline SDOperand getSmallIPtrImm(unsigned Imm) {
76 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
77 }
78
Nate Begemanf42f1332006-09-22 05:01:56 +000079 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
80 /// with any number of 0s on either side. The 1s are allowed to wrap from
81 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
82 /// 0x0F0F0000 is not, since all 1s are not contiguous.
83 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
84
85
86 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
87 /// rotate and mask opcode and mask operation.
88 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
89 unsigned &SH, unsigned &MB, unsigned &ME);
Chris Lattnerc08f9022006-06-27 00:04:13 +000090
Chris Lattner4416f1a2005-08-19 22:38:53 +000091 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
92 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +000093 SDNode *getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000094
95 // Select - Convert the specified operand from a target-independent to a
96 // target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +000097 SDNode *Select(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000098
Nate Begeman02b88a42005-08-19 00:38:14 +000099 SDNode *SelectBitfieldInsert(SDNode *N);
100
Chris Lattner2fbb4572005-08-21 18:50:37 +0000101 /// SelectCC - Select a comparison of the specified values with the
102 /// specified condition code, returning the CR# of the expression.
103 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
104
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000105 /// SelectAddrImm - Returns true if the address N can be represented by
106 /// a base register plus a signed 16-bit displacement [r+imm].
107 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
108
109 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
110 /// represented as an indexed [r+r] operation. Returns false if it can
111 /// be represented by [r+imm], which are preferred.
112 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000113
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000114 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
115 /// represented as an indexed [r+r] operation.
116 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +0000117
Chris Lattnere5ba5802006-03-22 05:26:03 +0000118 /// SelectAddrImmShift - Returns true if the address N can be represented by
119 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
120 /// for use by STD and friends.
121 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
122
Chris Lattnere5d88612006-02-24 02:13:12 +0000123 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
124 /// inline asm expressions.
125 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
126 char ConstraintCode,
127 std::vector<SDOperand> &OutOps,
128 SelectionDAG &DAG) {
129 SDOperand Op0, Op1;
130 switch (ConstraintCode) {
131 default: return true;
132 case 'm': // memory
133 if (!SelectAddrIdx(Op, Op0, Op1))
134 SelectAddrImm(Op, Op0, Op1);
135 break;
136 case 'o': // offsetable
137 if (!SelectAddrImm(Op, Op0, Op1)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000138 Op0 = Op;
139 AddToISelQueue(Op0); // r+0.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000140 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000141 }
142 break;
143 case 'v': // not offsetable
144 SelectAddrIdxOnly(Op, Op0, Op1);
145 break;
146 }
147
148 OutOps.push_back(Op0);
149 OutOps.push_back(Op1);
150 return false;
151 }
152
Chris Lattner047b9522005-08-25 22:04:30 +0000153 SDOperand BuildSDIVSequence(SDNode *N);
154 SDOperand BuildUDIVSequence(SDNode *N);
155
Chris Lattnera5a91b12005-08-17 19:33:03 +0000156 /// InstructionSelectBasicBlock - This callback is invoked by
157 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000158 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
159
Chris Lattner4bb18952006-03-16 18:25:23 +0000160 void InsertVRSaveCode(Function &Fn);
161
Chris Lattnera5a91b12005-08-17 19:33:03 +0000162 virtual const char *getPassName() const {
163 return "PowerPC DAG->DAG Pattern Instruction Selection";
164 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000165
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000166 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
167 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000168 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000169 // Should use subtarget info to pick the right hazard recognizer. For
170 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000171 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
172 assert(II && "No InstrInfo?");
173 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000174 }
Chris Lattneraf165382005-09-13 22:03:06 +0000175
176// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000177#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000178
179private:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000180 SDNode *SelectSETCC(SDOperand Op);
181 SDNode *MySelect_PPCbctrl(SDOperand N);
182 SDNode *MySelect_PPCcall(SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000183 };
184}
185
Chris Lattnerbd937b92005-10-06 18:45:51 +0000186/// InstructionSelectBasicBlock - This callback is invoked by
187/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000188void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000189 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000190
Chris Lattnerbd937b92005-10-06 18:45:51 +0000191 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000192 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000193 DAG.RemoveDeadNodes();
194
Chris Lattner1877ec92006-03-13 21:52:10 +0000195 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000196 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000197}
198
199/// InsertVRSaveCode - Once the entire function has been instruction selected,
200/// all virtual registers are created and all machine instructions are built,
201/// check to see if we need to save/restore VRSAVE. If so, do it.
202void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000203 // Check to see if this function uses vector registers, which means we have to
204 // save and restore the VRSAVE register and update it with the regs we use.
205 //
206 // In this case, there will be virtual registers of vector type type created
207 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000208 MachineFunction &Fn = MachineFunction::get(&F);
209 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000210 bool HasVectorVReg = false;
211 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000212 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000213 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
214 HasVectorVReg = true;
215 break;
216 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000217 if (!HasVectorVReg) return; // nothing to do.
218
Chris Lattner1877ec92006-03-13 21:52:10 +0000219 // If we have a vector register, we want to emit code into the entry and exit
220 // blocks to save and restore the VRSAVE register. We do this here (instead
221 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
222 //
223 // 1. This (trivially) reduces the load on the register allocator, by not
224 // having to represent the live range of the VRSAVE register.
225 // 2. This (more significantly) allows us to create a temporary virtual
226 // register to hold the saved VRSAVE value, allowing this temporary to be
227 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000228
229 // Create two vregs - one to hold the VRSAVE register that is live-in to the
230 // function and one for the value after having bits or'd into it.
231 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
232 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
233
234 MachineBasicBlock &EntryBB = *Fn.begin();
235 // Emit the following code into the entry block:
236 // InVRSAVE = MFVRSAVE
237 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
238 // MTVRSAVE UpdatedVRSAVE
239 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
240 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
241 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
242 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
243
244 // Find all return blocks, outputting a restore in each epilog.
245 const TargetInstrInfo &TII = *TM.getInstrInfo();
246 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
247 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
248 IP = BB->end(); --IP;
249
250 // Skip over all terminator instructions, which are part of the return
251 // sequence.
252 MachineBasicBlock::iterator I2 = IP;
253 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
254 IP = I2;
255
256 // Emit: MTVRSAVE InVRSave
257 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
258 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000259 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000260}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000261
Chris Lattner4bb18952006-03-16 18:25:23 +0000262
Chris Lattner4416f1a2005-08-19 22:38:53 +0000263/// getGlobalBaseReg - Output the instructions required to put the
264/// base address to use for accessing globals into a register.
265///
Evan Cheng9ade2182006-08-26 05:34:46 +0000266SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000267 if (!GlobalBaseReg) {
268 // Insert the set of GlobalBaseReg into the first MBB of the function
269 MachineBasicBlock &FirstMBB = BB->getParent()->front();
270 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
271 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000272
273 if (PPCLowering.getPointerTy() == MVT::i32)
274 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
275 else
276 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
277
Chris Lattner4416f1a2005-08-19 22:38:53 +0000278 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
279 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
280 }
Evan Cheng9ade2182006-08-26 05:34:46 +0000281 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy()).Val;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000282}
283
284/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
285/// or 64-bit immediate, and if the value can be accurately represented as a
286/// sign extension from a 16-bit value. If so, this returns true and the
287/// immediate.
288static bool isIntS16Immediate(SDNode *N, short &Imm) {
289 if (N->getOpcode() != ISD::Constant)
290 return false;
291
292 Imm = (short)cast<ConstantSDNode>(N)->getValue();
293 if (N->getValueType(0) == MVT::i32)
294 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
295 else
296 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
297}
298
299static bool isIntS16Immediate(SDOperand Op, short &Imm) {
300 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000301}
302
303
Chris Lattnerc08f9022006-06-27 00:04:13 +0000304/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
305/// operand. If so Imm will receive the 32-bit value.
306static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
307 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000308 Imm = cast<ConstantSDNode>(N)->getValue();
309 return true;
310 }
311 return false;
312}
313
Chris Lattnerc08f9022006-06-27 00:04:13 +0000314/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
315/// operand. If so Imm will receive the 64-bit value.
316static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Chris Lattner71176242006-09-20 04:33:27 +0000317 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000318 Imm = cast<ConstantSDNode>(N)->getValue();
319 return true;
320 }
321 return false;
322}
323
324// isInt32Immediate - This method tests to see if a constant operand.
325// If so Imm will receive the 32 bit value.
326static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
327 return isInt32Immediate(N.Val, Imm);
328}
329
330
331// isOpcWithIntImmediate - This method tests to see if the node is a specific
332// opcode and that it has a immediate integer right operand.
333// If so Imm will receive the 32 bit value.
334static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
335 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
336}
337
Nate Begemanf42f1332006-09-22 05:01:56 +0000338bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000339 if (isShiftedMask_32(Val)) {
340 // look for the first non-zero bit
341 MB = CountLeadingZeros_32(Val);
342 // look for the first zero bit after the run of ones
343 ME = CountLeadingZeros_32((Val - 1) ^ Val);
344 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000345 } else {
346 Val = ~Val; // invert mask
347 if (isShiftedMask_32(Val)) {
348 // effectively look for the first zero bit
349 ME = CountLeadingZeros_32(Val) - 1;
350 // effectively look for the first one bit after the run of zeros
351 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
352 return true;
353 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000354 }
355 // no run present
356 return false;
357}
358
Nate Begemanf42f1332006-09-22 05:01:56 +0000359bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
360 bool IsShiftMask, unsigned &SH,
361 unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000362 // Don't even go down this path for i64, since different logic will be
363 // necessary for rldicl/rldicr/rldimi.
364 if (N->getValueType(0) != MVT::i32)
365 return false;
366
Nate Begemancffc32b2005-08-18 07:30:46 +0000367 unsigned Shift = 32;
368 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
369 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000370 if (N->getNumOperands() != 2 ||
Chris Lattnerc08f9022006-06-27 00:04:13 +0000371 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000372 return false;
373
374 if (Opcode == ISD::SHL) {
375 // apply shift left 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);
Chris Lattner651dea72005-10-15 21:40:12 +0000379 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000380 // apply shift right to mask if it comes first
381 if (IsShiftMask) Mask = Mask >> Shift;
382 // determine which bits are made indeterminant by shift
383 Indeterminant = ~(0xFFFFFFFFu >> Shift);
384 // adjust for the left rotate
385 Shift = 32 - Shift;
Nate Begemanf42f1332006-09-22 05:01:56 +0000386 } else if (Opcode == ISD::ROTL) {
387 Indeterminant = 0;
Nate Begemancffc32b2005-08-18 07:30:46 +0000388 } else {
389 return false;
390 }
391
392 // if the mask doesn't intersect any Indeterminant bits
393 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000394 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000395 // make sure the mask is still a mask (wrap arounds may not be)
396 return isRunOfOnes(Mask, MB, ME);
397 }
398 return false;
399}
400
Nate Begeman02b88a42005-08-19 00:38:14 +0000401/// SelectBitfieldInsert - turn an or of two masked values into
402/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000403SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000404 SDOperand Op0 = N->getOperand(0);
405 SDOperand Op1 = N->getOperand(1);
406
Nate Begeman77f361f2006-05-07 00:23:38 +0000407 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000408 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
409 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000410
Nate Begeman4667f2c2006-05-08 17:38:32 +0000411 unsigned TargetMask = LKZ;
412 unsigned InsertMask = RKZ;
413
414 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
415 unsigned Op0Opc = Op0.getOpcode();
416 unsigned Op1Opc = Op1.getOpcode();
417 unsigned Value, SH = 0;
418 TargetMask = ~TargetMask;
419 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000420
Nate Begeman4667f2c2006-05-08 17:38:32 +0000421 // If the LHS has a foldable shift and the RHS does not, then swap it to the
422 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000423 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
424 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
425 Op0.getOperand(0).getOpcode() == ISD::SRL) {
426 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
427 Op1.getOperand(0).getOpcode() != ISD::SRL) {
428 std::swap(Op0, Op1);
429 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000430 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000431 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000432 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000433 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
434 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
435 Op1.getOperand(0).getOpcode() != ISD::SRL) {
436 std::swap(Op0, Op1);
437 std::swap(Op0Opc, Op1Opc);
438 std::swap(TargetMask, InsertMask);
439 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000440 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000441
442 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000443 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000444 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000445 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000446
447 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000448 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000449 Op1 = Op1.getOperand(0);
450 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
451 }
452 if (Op1Opc == ISD::AND) {
453 unsigned SHOpc = Op1.getOperand(0).getOpcode();
454 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000455 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000456 Op1 = Op1.getOperand(0).getOperand(0);
457 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
458 } else {
459 Op1 = Op1.getOperand(0);
460 }
461 }
462
463 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Evan Cheng6da2f322006-08-26 01:07:58 +0000464 AddToISelQueue(Tmp3);
465 AddToISelQueue(Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000466 SH &= 31;
Evan Cheng0b828e02006-08-27 08:14:06 +0000467 SDOperand Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
468 getI32Imm(ME) };
469 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman02b88a42005-08-19 00:38:14 +0000470 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000471 }
472 return 0;
473}
474
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000475/// SelectAddrImm - Returns true if the address N can be represented by
476/// a base register plus a signed 16-bit displacement [r+imm].
477bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
478 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000479 // If this can be more profitably realized as r+r, fail.
480 if (SelectAddrIdx(N, Disp, Base))
481 return false;
482
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000483 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000484 short imm = 0;
485 if (isIntS16Immediate(N.getOperand(1), imm)) {
486 Disp = getI32Imm((int)imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000487 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000488 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner9944b762005-08-21 22:31:09 +0000489 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000490 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000491 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000492 return true; // [r+i]
493 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000494 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000495 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000496 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000497 Disp = N.getOperand(1).getOperand(0); // The global address.
498 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000499 Disp.getOpcode() == ISD::TargetConstantPool ||
500 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000501 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000502 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000503 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000504 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000505 short imm = 0;
506 if (isIntS16Immediate(N.getOperand(1), imm)) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000507 // If this is an or of disjoint bitfields, we can codegen this as an add
508 // (for better address arithmetic) if the LHS and RHS of the OR are
509 // provably disjoint.
510 uint64_t LHSKnownZero, LHSKnownOne;
511 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
512 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000513 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000514 // If all of the bits are known zero on the LHS or RHS, the add won't
515 // carry.
516 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000517 Disp = getI32Imm((int)imm & 0xFFFF);
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000518 return true;
519 }
520 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000521 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
522 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000523
Chris Lattnerd9796442006-03-20 22:38:22 +0000524 // If this address fits entirely in a 16-bit sext immediate field, codegen
525 // this as "d, 0"
Chris Lattnerc08f9022006-06-27 00:04:13 +0000526 short Imm;
527 if (isIntS16Immediate(CN, Imm)) {
528 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
529 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
Chris Lattnerd9796442006-03-20 22:38:22 +0000530 return true;
531 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000532
533 // FIXME: Handle small sext constant offsets in PPC64 mode also!
534 if (CN->getValueType(0) == MVT::i32) {
535 int Addr = (int)CN->getValue();
Chris Lattnerd9796442006-03-20 22:38:22 +0000536
Chris Lattnerc08f9022006-06-27 00:04:13 +0000537 // Otherwise, break this down into an LIS + disp.
538 Disp = getI32Imm((short)Addr);
539 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
540 return true;
541 }
Chris Lattner9944b762005-08-21 22:31:09 +0000542 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000543
Chris Lattnerc08f9022006-06-27 00:04:13 +0000544 Disp = getSmallIPtrImm(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000545 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000546 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Nate Begeman28a6b022005-12-10 02:36:00 +0000547 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000548 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000549 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000550}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000551
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000552/// SelectAddrIdx - Given the specified addressed, check to see if it can be
553/// represented as an indexed [r+r] operation. Returns false if it can
554/// be represented by [r+imm], which are preferred.
555bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
556 SDOperand &Index) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000557 short imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000558 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000559 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000560 return false; // r+i
561 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
562 return false; // r+i
563
Evan Cheng7564e0b2006-02-05 08:45:01 +0000564 Base = N.getOperand(0);
565 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000566 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000567 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000568 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000569 return false; // r+i can fold it if we can.
570
571 // If this is an or of disjoint bitfields, we can codegen this as an add
572 // (for better address arithmetic) if the LHS and RHS of the OR are provably
573 // disjoint.
574 uint64_t LHSKnownZero, LHSKnownOne;
575 uint64_t RHSKnownZero, RHSKnownOne;
576 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
577 LHSKnownZero, LHSKnownOne);
578
579 if (LHSKnownZero) {
580 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
581 RHSKnownZero, RHSKnownOne);
582 // If all of the bits are known zero on the LHS or RHS, the add won't
583 // carry.
584 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
585 Base = N.getOperand(0);
586 Index = N.getOperand(1);
587 return true;
588 }
589 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000590 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000591
592 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000593}
594
595/// SelectAddrIdxOnly - Given the specified addressed, force it to be
596/// represented as an indexed [r+r] operation.
597bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
598 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000599 // Check to see if we can easily represent this as an [r+r] address. This
600 // will fail if it thinks that the address is more profitably represented as
601 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000602 if (SelectAddrIdx(N, Base, Index))
603 return true;
604
605 // If the operand is an addition, always emit this as [r+r], since this is
606 // better (for code size, and execution, as the memop does the add for free)
607 // than emitting an explicit add.
608 if (N.getOpcode() == ISD::ADD) {
609 Base = N.getOperand(0);
610 Index = N.getOperand(1);
611 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000612 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000613
614 // Otherwise, do it the hard way, using R0 as the base register.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000615 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
Chris Lattner54e869e2006-03-24 17:58:06 +0000616 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000617 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000618}
619
Chris Lattnere5ba5802006-03-22 05:26:03 +0000620/// SelectAddrImmShift - Returns true if the address N can be represented by
621/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
622/// for use by STD and friends.
623bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
624 SDOperand &Base) {
625 // If this can be more profitably realized as r+r, fail.
626 if (SelectAddrIdx(N, Disp, Base))
627 return false;
628
629 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000630 short imm = 0;
631 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
632 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000633 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000634 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000635 } else {
636 Base = N.getOperand(0);
637 }
638 return true; // [r+i]
639 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
640 // Match LOAD (ADD (X, Lo(G))).
641 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
642 && "Cannot handle constant offsets yet!");
643 Disp = N.getOperand(1).getOperand(0); // The global address.
644 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000645 Disp.getOpcode() == ISD::TargetConstantPool ||
646 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000647 Base = N.getOperand(0);
648 return true; // [&g+r]
649 }
650 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000651 short imm = 0;
652 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000653 // If this is an or of disjoint bitfields, we can codegen this as an add
654 // (for better address arithmetic) if the LHS and RHS of the OR are
655 // provably disjoint.
656 uint64_t LHSKnownZero, LHSKnownOne;
657 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
658 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000659 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000660 // If all of the bits are known zero on the LHS or RHS, the add won't
661 // carry.
662 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000663 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000664 return true;
665 }
666 }
667 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
668 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000669
670 // If this address fits entirely in a 14-bit sext immediate field, codegen
671 // this as "d, 0"
672 short Imm;
673 if (isIntS16Immediate(CN, Imm)) {
674 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
675 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
676 return true;
677 }
678
679 // FIXME: Handle small sext constant offsets in PPC64 mode also!
680 if (CN->getValueType(0) == MVT::i32) {
681 int Addr = (int)CN->getValue();
Chris Lattnere5ba5802006-03-22 05:26:03 +0000682
683 // Otherwise, break this down into an LIS + disp.
684 Disp = getI32Imm((short)Addr >> 2);
685 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
686 return true;
687 }
688 }
689
Chris Lattnerc08f9022006-06-27 00:04:13 +0000690 Disp = getSmallIPtrImm(0);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000691 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000692 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000693 else
694 Base = N;
695 return true; // [r+0]
696}
697
698
Chris Lattner2fbb4572005-08-21 18:50:37 +0000699/// SelectCC - Select a comparison of the specified values with the specified
700/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000701SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
702 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000703 // Always select the LHS.
Evan Cheng6da2f322006-08-26 01:07:58 +0000704 AddToISelQueue(LHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000705 unsigned Opc;
706
707 if (LHS.getValueType() == MVT::i32) {
Chris Lattner529c2332006-06-27 00:10:13 +0000708 unsigned Imm;
Chris Lattner3836dbd2006-09-20 04:25:47 +0000709 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
710 if (isInt32Immediate(RHS, Imm)) {
711 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
712 if (isUInt16(Imm))
713 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
714 getI32Imm(Imm & 0xFFFF)), 0);
715 // If this is a 16-bit signed immediate, fold it.
716 if (isInt16(Imm))
717 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
718 getI32Imm(Imm & 0xFFFF)), 0);
719
720 // For non-equality comparisons, the default code would materialize the
721 // constant, then compare against it, like this:
722 // lis r2, 4660
723 // ori r2, r2, 22136
724 // cmpw cr0, r3, r2
725 // Since we are just comparing for equality, we can emit this instead:
726 // xoris r0,r3,0x1234
727 // cmplwi cr0,r0,0x5678
728 // beq cr0,L6
729 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS, MVT::i32, LHS,
730 getI32Imm(Imm >> 16)), 0);
731 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, Xor,
732 getI32Imm(Imm & 0xFFFF)), 0);
733 }
734 Opc = PPC::CMPLW;
735 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000736 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
737 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
738 getI32Imm(Imm & 0xFFFF)), 0);
739 Opc = PPC::CMPLW;
740 } else {
741 short SImm;
742 if (isIntS16Immediate(RHS, SImm))
743 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
744 getI32Imm((int)SImm & 0xFFFF)),
745 0);
746 Opc = PPC::CMPW;
747 }
748 } else if (LHS.getValueType() == MVT::i64) {
749 uint64_t Imm;
Chris Lattner71176242006-09-20 04:33:27 +0000750 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
751 if (isInt64Immediate(RHS.Val, Imm)) {
752 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
753 if (isUInt16(Imm))
754 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
755 getI32Imm(Imm & 0xFFFF)), 0);
756 // If this is a 16-bit signed immediate, fold it.
757 if (isInt16(Imm))
758 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
759 getI32Imm(Imm & 0xFFFF)), 0);
760
761 // For non-equality comparisons, the default code would materialize the
762 // constant, then compare against it, like this:
763 // lis r2, 4660
764 // ori r2, r2, 22136
765 // cmpd cr0, r3, r2
766 // Since we are just comparing for equality, we can emit this instead:
767 // xoris r0,r3,0x1234
768 // cmpldi cr0,r0,0x5678
769 // beq cr0,L6
770 if (isUInt32(Imm)) {
771 SDOperand Xor(CurDAG->getTargetNode(PPC::XORIS8, MVT::i64, LHS,
772 getI64Imm(Imm >> 16)), 0);
773 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, Xor,
774 getI64Imm(Imm & 0xFFFF)), 0);
775 }
776 }
777 Opc = PPC::CMPLD;
778 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000779 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
780 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
781 getI64Imm(Imm & 0xFFFF)), 0);
782 Opc = PPC::CMPLD;
783 } else {
784 short SImm;
785 if (isIntS16Immediate(RHS, SImm))
786 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000787 getI64Imm(SImm & 0xFFFF)),
Chris Lattnerc08f9022006-06-27 00:04:13 +0000788 0);
789 Opc = PPC::CMPD;
790 }
Chris Lattner919c0322005-10-01 01:35:02 +0000791 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000792 Opc = PPC::FCMPUS;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000793 } else {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000794 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
795 Opc = PPC::FCMPUD;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000796 }
Evan Cheng6da2f322006-08-26 01:07:58 +0000797 AddToISelQueue(RHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000798 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000799}
800
801/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
802/// to Condition.
803static unsigned getBCCForSetCC(ISD::CondCode CC) {
804 switch (CC) {
805 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000806 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000807 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000808 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000809 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000810 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000811 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000812 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000813 case ISD::SETULT:
814 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000815 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000816 case ISD::SETULE:
817 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000818 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000819 case ISD::SETUGT:
820 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000821 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000822 case ISD::SETUGE:
823 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000824
Chris Lattner1d754002006-10-30 23:02:25 +0000825 case ISD::SETO: return PPC::BNU;
826 case ISD::SETUO: return PPC::BUN;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000827 }
828 return 0;
829}
830
Chris Lattner64906a02005-08-25 20:08:18 +0000831/// getCRIdxForSetCC - Return the index of the condition register field
832/// associated with the SetCC condition, and whether or not the field is
833/// treated as inverted. That is, lt = 0; ge = 0 inverted.
834static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
835 switch (CC) {
836 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000837 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000838 case ISD::SETULT:
839 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000840 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000841 case ISD::SETUGE:
842 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000843 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000844 case ISD::SETUGT:
845 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000846 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000847 case ISD::SETULE:
848 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000849 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000850 case ISD::SETUEQ:
Chris Lattner64906a02005-08-25 20:08:18 +0000851 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000852 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000853 case ISD::SETUNE:
Chris Lattner64906a02005-08-25 20:08:18 +0000854 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000855 case ISD::SETO: Inv = true; return 3;
856 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000857 }
858 return 0;
859}
Chris Lattner9944b762005-08-21 22:31:09 +0000860
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000861SDNode *PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000862 SDNode *N = Op.Val;
863 unsigned Imm;
864 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000865 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner222adac2005-10-06 19:03:35 +0000866 // We can codegen setcc op, imm very efficiently compared to a brcond.
867 // Check for those cases here.
868 // setcc op, 0
869 if (Imm == 0) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000870 SDOperand Op = N->getOperand(0);
871 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000872 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000873 default: break;
Evan Cheng0b828e02006-08-27 08:14:06 +0000874 case ISD::SETEQ: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000875 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000876 SDOperand Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
877 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
878 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000879 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000880 SDOperand AD =
881 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
882 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000883 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000884 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000885 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000886 case ISD::SETLT: {
887 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
888 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
889 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000890 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000891 SDOperand T =
892 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
893 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000894 SDOperand Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
895 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000896 }
897 }
Chris Lattner222adac2005-10-06 19:03:35 +0000898 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng6da2f322006-08-26 01:07:58 +0000899 SDOperand Op = N->getOperand(0);
900 AddToISelQueue(Op);
Chris Lattner222adac2005-10-06 19:03:35 +0000901 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000902 default: break;
903 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000904 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
905 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000906 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000907 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
908 getI32Imm(0)), 0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000909 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000910 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000911 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
912 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
913 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000914 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000915 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000916 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000917 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000918 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
919 getI32Imm(1)), 0);
920 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
921 Op), 0);
Evan Cheng0b828e02006-08-27 08:14:06 +0000922 SDOperand Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
923 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000924 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000925 case ISD::SETGT: {
926 SDOperand Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
927 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000928 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000929 getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000930 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000931 }
Chris Lattner222adac2005-10-06 19:03:35 +0000932 }
933 }
934
935 bool Inv;
936 unsigned Idx = getCRIdxForSetCC(CC, Inv);
937 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
938 SDOperand IntCR;
939
940 // Force the ccreg into CR7.
941 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
942
Chris Lattner85961d52005-12-06 20:56:18 +0000943 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000944 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
945 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000946
947 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000948 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
949 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000950 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000951 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000952
Evan Cheng0b828e02006-08-27 08:14:06 +0000953 SDOperand Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
954 getI32Imm(31), getI32Imm(31) };
Chris Lattner222adac2005-10-06 19:03:35 +0000955 if (!Inv) {
Evan Cheng0b828e02006-08-27 08:14:06 +0000956 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner222adac2005-10-06 19:03:35 +0000957 } else {
958 SDOperand Tmp =
Evan Cheng0b828e02006-08-27 08:14:06 +0000959 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000960 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000961 }
Chris Lattner222adac2005-10-06 19:03:35 +0000962}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000963
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000964
Chris Lattnera5a91b12005-08-17 19:33:03 +0000965// Select - Convert the specified operand from a target-independent to a
966// target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +0000967SDNode *PPCDAGToDAGISel::Select(SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000968 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000969 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng9ade2182006-08-26 05:34:46 +0000970 N->getOpcode() < PPCISD::FIRST_NUMBER)
Evan Cheng64a752f2006-08-11 09:08:15 +0000971 return NULL; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000972
Chris Lattnera5a91b12005-08-17 19:33:03 +0000973 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000974 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000975 case ISD::SETCC:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000976 return SelectSETCC(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000977 case PPCISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000978 return getGlobalBaseReg();
Chris Lattner860e8862005-11-17 07:30:41 +0000979
Chris Lattnere28e40a2005-08-25 00:45:43 +0000980 case ISD::FrameIndex: {
981 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000982 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
983 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000984 if (N->hasOneUse())
985 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
Evan Cheng95514ba2006-08-26 08:00:10 +0000986 getSmallIPtrImm(0));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000987 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
988 getSmallIPtrImm(0));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000989 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000990
991 case PPCISD::MFCR: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000992 SDOperand InFlag = N->getOperand(1);
993 AddToISelQueue(InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000994 // Use MFOCRF if supported.
995 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000996 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
997 N->getOperand(0), InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000998 else
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000999 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +00001000 }
1001
Chris Lattner88add102005-09-28 22:50:24 +00001002 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001003 // FIXME: since this depends on the setting of the carry flag from the srawi
1004 // we should really be making notes about that for the scheduler.
1005 // FIXME: It sure would be nice if we could cheaply recognize the
1006 // srl/add/sra pattern the dag combiner will generate for this as
1007 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +00001008 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001009 if (isInt32Immediate(N->getOperand(1), Imm)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001010 SDOperand N0 = N->getOperand(0);
1011 AddToISelQueue(N0);
Chris Lattner8784a232005-08-25 17:50:06 +00001012 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001013 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +00001014 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001015 N0, getI32Imm(Log2_32(Imm)));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001016 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng95514ba2006-08-26 08:00:10 +00001017 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001018 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001019 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001020 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001021 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001022 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001023 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1024 SDOperand(Op, 0), SDOperand(Op, 1)),
1025 0);
Evan Cheng95514ba2006-08-26 08:00:10 +00001026 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001027 }
1028 }
Chris Lattner047b9522005-08-25 22:04:30 +00001029
Chris Lattner237733e2005-09-29 23:33:31 +00001030 // Other cases are autogenerated.
1031 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001032 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001033 case ISD::AND: {
Nate Begemanf42f1332006-09-22 05:01:56 +00001034 unsigned Imm, Imm2, SH, MB, ME;
1035
Nate Begemancffc32b2005-08-18 07:30:46 +00001036 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1037 // with a mask, emit rlwinm
Chris Lattnerc08f9022006-06-27 00:04:13 +00001038 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begemanf42f1332006-09-22 05:01:56 +00001039 isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
1040 SDOperand Val = N->getOperand(0).getOperand(0);
1041 AddToISelQueue(Val);
Evan Cheng0b828e02006-08-27 08:14:06 +00001042 SDOperand Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1043 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemancffc32b2005-08-18 07:30:46 +00001044 }
Nate Begemanf42f1332006-09-22 05:01:56 +00001045 // If this is just a masked value where the input is not handled above, and
1046 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1047 if (isInt32Immediate(N->getOperand(1), Imm) &&
1048 isRunOfOnes(Imm, MB, ME) &&
1049 N->getOperand(0).getOpcode() != ISD::ROTL) {
1050 SDOperand Val = N->getOperand(0);
1051 AddToISelQueue(Val);
1052 SDOperand Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
1053 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1054 }
1055 // AND X, 0 -> 0, not "rlwinm 32".
1056 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
1057 AddToISelQueue(N->getOperand(1));
1058 ReplaceUses(SDOperand(N, 0), N->getOperand(1));
1059 return NULL;
1060 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001061 // ISD::OR doesn't get all the bitfield insertion fun.
1062 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattnerc08f9022006-06-27 00:04:13 +00001063 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman50fb3c42005-12-24 01:00:15 +00001064 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001065 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001066 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001067 Imm = ~(Imm^Imm2);
1068 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001069 AddToISelQueue(N->getOperand(0).getOperand(0));
1070 AddToISelQueue(N->getOperand(0).getOperand(1));
Evan Cheng0b828e02006-08-27 08:14:06 +00001071 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1072 N->getOperand(0).getOperand(1),
1073 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1074 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman50fb3c42005-12-24 01:00:15 +00001075 }
1076 }
Chris Lattner237733e2005-09-29 23:33:31 +00001077
1078 // Other cases are autogenerated.
1079 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001080 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001081 case ISD::OR:
Chris Lattnercccef1c2006-06-27 21:08:52 +00001082 if (N->getValueType(0) == MVT::i32)
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001083 if (SDNode *I = SelectBitfieldInsert(N))
1084 return I;
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001085
Chris Lattner237733e2005-09-29 23:33:31 +00001086 // Other cases are autogenerated.
1087 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001088 case ISD::SHL: {
1089 unsigned Imm, SH, MB, ME;
1090 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001091 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001092 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Cheng0b828e02006-08-27 08:14:06 +00001093 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1094 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1095 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +00001096 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001097
1098 // Other cases are autogenerated.
1099 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001100 }
1101 case ISD::SRL: {
1102 unsigned Imm, SH, MB, ME;
1103 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001104 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001105 AddToISelQueue(N->getOperand(0).getOperand(0));
Evan Cheng0b828e02006-08-27 08:14:06 +00001106 SDOperand Ops[] = { N->getOperand(0).getOperand(0),
1107 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1108 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +00001109 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001110
1111 // Other cases are autogenerated.
1112 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001113 }
Chris Lattner13794f52005-08-26 18:46:49 +00001114 case ISD::SELECT_CC: {
1115 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1116
Chris Lattnerc08f9022006-06-27 00:04:13 +00001117 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +00001118 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1119 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1120 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1121 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001122 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1123 // FIXME: Implement this optzn for PPC64.
1124 N->getValueType(0) == MVT::i32) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001125 AddToISelQueue(N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001126 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001127 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng6da2f322006-08-26 01:07:58 +00001128 N->getOperand(0), getI32Imm(~0U));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001129 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Evan Cheng6da2f322006-08-26 01:07:58 +00001130 SDOperand(Tmp, 0), N->getOperand(0),
Evan Cheng95514ba2006-08-26 08:00:10 +00001131 SDOperand(Tmp, 1));
Chris Lattner13794f52005-08-26 18:46:49 +00001132 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001133
Chris Lattner50ff55c2005-09-01 19:20:44 +00001134 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001135 unsigned BROpc = getBCCForSetCC(CC);
1136
1137 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001138 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001139 if (N->getValueType(0) == MVT::i32)
1140 SelectCCOp = PPC::SELECT_CC_I4;
1141 else if (N->getValueType(0) == MVT::i64)
1142 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +00001143 else if (N->getValueType(0) == MVT::f32)
1144 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001145 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001146 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001147 else
1148 SelectCCOp = PPC::SELECT_CC_VRRC;
1149
Evan Cheng6da2f322006-08-26 01:07:58 +00001150 AddToISelQueue(N->getOperand(2));
1151 AddToISelQueue(N->getOperand(3));
Evan Cheng0b828e02006-08-27 08:14:06 +00001152 SDOperand Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1153 getI32Imm(BROpc) };
1154 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattner13794f52005-08-26 18:46:49 +00001155 }
Nate Begeman81e80972006-03-17 01:40:33 +00001156 case ISD::BR_CC: {
Evan Cheng6da2f322006-08-26 01:07:58 +00001157 AddToISelQueue(N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001158 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1159 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Evan Cheng0b828e02006-08-27 08:14:06 +00001160 SDOperand Ops[] = { CondCode, getI32Imm(getBCCForSetCC(CC)),
1161 N->getOperand(4), N->getOperand(0) };
1162 return CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, Ops, 4);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001163 }
Nate Begeman37efe672006-04-22 18:53:45 +00001164 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001165 // FIXME: Should custom lower this.
Evan Cheng6da2f322006-08-26 01:07:58 +00001166 SDOperand Chain = N->getOperand(0);
1167 SDOperand Target = N->getOperand(1);
1168 AddToISelQueue(Chain);
1169 AddToISelQueue(Target);
Chris Lattner6b76b962006-06-27 20:46:17 +00001170 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1171 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman37efe672006-04-22 18:53:45 +00001172 Chain), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +00001173 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
Nate Begeman37efe672006-04-22 18:53:45 +00001174 }
Chris Lattnercf006312006-06-10 01:15:02 +00001175 // FIXME: These are manually selected because tblgen isn't handling varargs
1176 // nodes correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001177 case PPCISD::BCTRL: return MySelect_PPCbctrl(Op);
1178 case PPCISD::CALL: return MySelect_PPCcall(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001179 }
Chris Lattner25dae722005-09-03 00:53:47 +00001180
Evan Cheng9ade2182006-08-26 05:34:46 +00001181 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001182}
1183
1184
Chris Lattnercf006312006-06-10 01:15:02 +00001185// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1186// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001187SDNode *PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +00001188 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001189
1190 bool hasFlag =
1191 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1192
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001193 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001194 // Push varargs arguments, including optional flag.
1195 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001196 Chain = N.getOperand(i);
1197 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001198 Ops.push_back(Chain);
1199 }
1200
Evan Cheng6da2f322006-08-26 01:07:58 +00001201 Chain = N.getOperand(0);
1202 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001203 Ops.push_back(Chain);
1204
1205 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001206 Chain = N.getOperand(N.getNumOperands()-1);
1207 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001208 Ops.push_back(Chain);
1209 }
1210
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001211 return CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag,
1212 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001213}
1214
1215// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1216// correctly.
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001217SDNode *PPCDAGToDAGISel::MySelect_PPCcall(SDOperand N) {
Chris Lattnercf006312006-06-10 01:15:02 +00001218 SDOperand Chain(0, 0);
Chris Lattnercf006312006-06-10 01:15:02 +00001219 SDOperand N1(0, 0);
1220 SDOperand Tmp0(0, 0);
1221 SDNode *ResNode;
1222 Chain = N.getOperand(0);
1223 N1 = N.getOperand(1);
1224
1225 // Pattern: (PPCcall:void (imm:i32):$func)
1226 // Emits: (BLA:void (imm:i32):$func)
1227 // Pattern complexity = 4 cost = 1
1228 if (N1.getOpcode() == ISD::Constant) {
1229 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1230
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001231 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001232 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1233
1234 bool hasFlag =
1235 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1236
1237 // Push varargs arguments, not including optional flag.
1238 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001239 Chain = N.getOperand(i);
1240 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001241 Ops.push_back(Chain);
1242 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001243 Chain = N.getOperand(0);
1244 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001245 Ops.push_back(Chain);
1246 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001247 Chain = N.getOperand(N.getNumOperands()-1);
1248 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001249 Ops.push_back(Chain);
1250 }
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001251 return CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag,
1252 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001253 }
1254
1255 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1256 // Emits: (BL:void (tglobaladdr:i32):$dst)
1257 // Pattern complexity = 4 cost = 1
1258 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001259 SmallVector<SDOperand, 8> Ops;
Chris Lattnercf006312006-06-10 01:15:02 +00001260 Ops.push_back(N1);
1261
1262 bool hasFlag =
1263 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1264
1265 // Push varargs arguments, not including optional flag.
1266 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001267 Chain = N.getOperand(i);
1268 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001269 Ops.push_back(Chain);
1270 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001271 Chain = N.getOperand(0);
1272 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001273 Ops.push_back(Chain);
1274 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001275 Chain = N.getOperand(N.getNumOperands()-1);
1276 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001277 Ops.push_back(Chain);
1278 }
1279
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001280 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1281 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001282 }
1283
1284 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1285 // Emits: (BL:void (texternalsym:i32):$dst)
1286 // Pattern complexity = 4 cost = 1
1287 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1288 std::vector<SDOperand> Ops;
1289 Ops.push_back(N1);
1290
1291 bool hasFlag =
1292 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1293
1294 // Push varargs arguments, not including optional flag.
1295 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001296 Chain = N.getOperand(i);
1297 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001298 Ops.push_back(Chain);
1299 }
Evan Cheng6da2f322006-08-26 01:07:58 +00001300 Chain = N.getOperand(0);
1301 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001302 Ops.push_back(Chain);
1303 if (hasFlag) {
Evan Cheng6da2f322006-08-26 01:07:58 +00001304 Chain = N.getOperand(N.getNumOperands()-1);
1305 AddToISelQueue(Chain);
Chris Lattnercf006312006-06-10 01:15:02 +00001306 Ops.push_back(Chain);
1307 }
1308
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001309 return CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag,
1310 &Ops[0], Ops.size());
Chris Lattnercf006312006-06-10 01:15:02 +00001311 }
1312 std::cerr << "Cannot yet select: ";
1313 N.Val->dump(CurDAG);
1314 std::cerr << '\n';
1315 abort();
Evan Cheng64a752f2006-08-11 09:08:15 +00001316
1317 return NULL;
Chris Lattnercf006312006-06-10 01:15:02 +00001318}
1319
1320
Nate Begeman1d9d7422005-10-18 00:28:58 +00001321/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001322/// PowerPC-specific DAG, ready for instruction scheduling.
1323///
Evan Chengc4c62572006-03-13 23:20:37 +00001324FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001325 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001326}
1327