blob: 2941b3426606c0abd301ac77fed76d9b94cf8f52 [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 Lattnera5a91b12005-08-17 19:33:03 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000031#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000032using namespace llvm;
33
34namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000035 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
36
37 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000038 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000039 /// instructions for SelectionDAG operations.
40 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 class PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000042 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000043 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000044 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000045 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000046 PPCDAGToDAGISel(PPCTargetMachine &tm)
47 : SelectionDAGISel(PPCLowering), TM(tm),
48 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000049
Chris Lattner4416f1a2005-08-19 22:38:53 +000050 virtual bool runOnFunction(Function &Fn) {
51 // Make sure we re-emit a set of the global base reg if necessary
52 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000053 SelectionDAGISel::runOnFunction(Fn);
54
55 InsertVRSaveCode(Fn);
56 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000057 }
58
Chris Lattnera5a91b12005-08-17 19:33:03 +000059 /// getI32Imm - Return a target constant with the specified value, of type
60 /// i32.
61 inline SDOperand getI32Imm(unsigned Imm) {
62 return CurDAG->getTargetConstant(Imm, MVT::i32);
63 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000064
65 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
66 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000067 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000068
69 // Select - Convert the specified operand from a target-independent to a
70 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000071 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000072
Nate Begeman02b88a42005-08-19 00:38:14 +000073 SDNode *SelectBitfieldInsert(SDNode *N);
74
Chris Lattner2fbb4572005-08-21 18:50:37 +000075 /// SelectCC - Select a comparison of the specified values with the
76 /// specified condition code, returning the CR# of the expression.
77 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
78
Nate Begeman7fd1edd2005-12-19 23:25:09 +000079 /// SelectAddrImm - Returns true if the address N can be represented by
80 /// a base register plus a signed 16-bit displacement [r+imm].
81 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
82
83 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
84 /// represented as an indexed [r+r] operation. Returns false if it can
85 /// be represented by [r+imm], which are preferred.
86 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000087
Nate Begeman7fd1edd2005-12-19 23:25:09 +000088 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
89 /// represented as an indexed [r+r] operation.
90 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000091
Chris Lattnere5ba5802006-03-22 05:26:03 +000092 /// SelectAddrImmShift - Returns true if the address N can be represented by
93 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
94 /// for use by STD and friends.
95 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
96
Chris Lattnere5d88612006-02-24 02:13:12 +000097 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
98 /// inline asm expressions.
99 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
100 char ConstraintCode,
101 std::vector<SDOperand> &OutOps,
102 SelectionDAG &DAG) {
103 SDOperand Op0, Op1;
104 switch (ConstraintCode) {
105 default: return true;
106 case 'm': // memory
107 if (!SelectAddrIdx(Op, Op0, Op1))
108 SelectAddrImm(Op, Op0, Op1);
109 break;
110 case 'o': // offsetable
111 if (!SelectAddrImm(Op, Op0, Op1)) {
112 Select(Op0, Op); // r+0.
113 Op1 = getI32Imm(0);
114 }
115 break;
116 case 'v': // not offsetable
117 SelectAddrIdxOnly(Op, Op0, Op1);
118 break;
119 }
120
121 OutOps.push_back(Op0);
122 OutOps.push_back(Op1);
123 return false;
124 }
125
Chris Lattner047b9522005-08-25 22:04:30 +0000126 SDOperand BuildSDIVSequence(SDNode *N);
127 SDOperand BuildUDIVSequence(SDNode *N);
128
Chris Lattnera5a91b12005-08-17 19:33:03 +0000129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
Chris Lattner4bb18952006-03-16 18:25:23 +0000133 void InsertVRSaveCode(Function &Fn);
134
Chris Lattnera5a91b12005-08-17 19:33:03 +0000135 virtual const char *getPassName() const {
136 return "PowerPC DAG->DAG Pattern Instruction Selection";
137 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000138
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000139 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for this
Chris Lattnerc6644182006-03-07 06:32:48 +0000140 /// target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000141 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000142 // Should use subtarget info to pick the right hazard recognizer. For
143 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000144 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
145 assert(II && "No InstrInfo?");
146 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000147 }
Chris Lattneraf165382005-09-13 22:03:06 +0000148
149// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000150#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000151
152private:
Chris Lattner222adac2005-10-06 19:03:35 +0000153 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000154 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000155 };
156}
157
Chris Lattnerbd937b92005-10-06 18:45:51 +0000158/// InstructionSelectBasicBlock - This callback is invoked by
159/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000160void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000161 DEBUG(BB->dump());
162
163 // The selection process is inherently a bottom-up recursive process (users
164 // select their uses before themselves). Given infinite stack space, we
165 // could just start selecting on the root and traverse the whole graph. In
166 // practice however, this causes us to run out of stack space on large basic
167 // blocks. To avoid this problem, select the entry node, then all its uses,
168 // iteratively instead of recursively.
169 std::vector<SDOperand> Worklist;
170 Worklist.push_back(DAG.getEntryNode());
171
172 // Note that we can do this in the PPC target (scanning forward across token
173 // chain edges) because no nodes ever get folded across these edges. On a
174 // target like X86 which supports load/modify/store operations, this would
175 // have to be more careful.
176 while (!Worklist.empty()) {
177 SDOperand Node = Worklist.back();
178 Worklist.pop_back();
179
Chris Lattnercf01a702005-10-07 22:10:27 +0000180 // Chose from the least deep of the top two nodes.
181 if (!Worklist.empty() &&
182 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
183 std::swap(Worklist.back(), Node);
184
Chris Lattnerbd937b92005-10-06 18:45:51 +0000185 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
186 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
187 CodeGenMap.count(Node)) continue;
188
189 for (SDNode::use_iterator UI = Node.Val->use_begin(),
190 E = Node.Val->use_end(); UI != E; ++UI) {
191 // Scan the values. If this use has a value that is a token chain, add it
192 // to the worklist.
193 SDNode *User = *UI;
194 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
195 if (User->getValueType(i) == MVT::Other) {
196 Worklist.push_back(SDOperand(User, i));
197 break;
198 }
199 }
200
201 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000202 SDOperand Dummy;
203 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000204 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000205
Chris Lattnerbd937b92005-10-06 18:45:51 +0000206 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000207 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000208 CodeGenMap.clear();
209 DAG.RemoveDeadNodes();
210
Chris Lattner1877ec92006-03-13 21:52:10 +0000211 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000212 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000213}
214
215/// InsertVRSaveCode - Once the entire function has been instruction selected,
216/// all virtual registers are created and all machine instructions are built,
217/// check to see if we need to save/restore VRSAVE. If so, do it.
218void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000219 // Check to see if this function uses vector registers, which means we have to
220 // save and restore the VRSAVE register and update it with the regs we use.
221 //
222 // In this case, there will be virtual registers of vector type type created
223 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000224 MachineFunction &Fn = MachineFunction::get(&F);
225 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000226 bool HasVectorVReg = false;
227 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000228 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000229 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
230 HasVectorVReg = true;
231 break;
232 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000233 if (!HasVectorVReg) return; // nothing to do.
234
Chris Lattner1877ec92006-03-13 21:52:10 +0000235 // If we have a vector register, we want to emit code into the entry and exit
236 // blocks to save and restore the VRSAVE register. We do this here (instead
237 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
238 //
239 // 1. This (trivially) reduces the load on the register allocator, by not
240 // having to represent the live range of the VRSAVE register.
241 // 2. This (more significantly) allows us to create a temporary virtual
242 // register to hold the saved VRSAVE value, allowing this temporary to be
243 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000244
245 // Create two vregs - one to hold the VRSAVE register that is live-in to the
246 // function and one for the value after having bits or'd into it.
247 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
248 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
249
250 MachineBasicBlock &EntryBB = *Fn.begin();
251 // Emit the following code into the entry block:
252 // InVRSAVE = MFVRSAVE
253 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
254 // MTVRSAVE UpdatedVRSAVE
255 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
256 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
257 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
258 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
259
260 // Find all return blocks, outputting a restore in each epilog.
261 const TargetInstrInfo &TII = *TM.getInstrInfo();
262 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
263 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
264 IP = BB->end(); --IP;
265
266 // Skip over all terminator instructions, which are part of the return
267 // sequence.
268 MachineBasicBlock::iterator I2 = IP;
269 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
270 IP = I2;
271
272 // Emit: MTVRSAVE InVRSave
273 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
274 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000275 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000276}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000277
Chris Lattner4bb18952006-03-16 18:25:23 +0000278
Chris Lattner4416f1a2005-08-19 22:38:53 +0000279/// getGlobalBaseReg - Output the instructions required to put the
280/// base address to use for accessing globals into a register.
281///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000282SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000283 if (!GlobalBaseReg) {
284 // Insert the set of GlobalBaseReg into the first MBB of the function
285 MachineBasicBlock &FirstMBB = BB->getParent()->front();
286 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
287 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000288 // FIXME: when we get to LP64, we will need to create the appropriate
289 // type of register here.
290 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000291 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
292 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
293 }
Chris Lattner9944b762005-08-21 22:31:09 +0000294 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000295}
296
297
Nate Begeman0f3257a2005-08-18 05:00:13 +0000298// isIntImmediate - This method tests to see if a constant operand.
299// If so Imm will receive the 32 bit value.
300static bool isIntImmediate(SDNode *N, unsigned& Imm) {
301 if (N->getOpcode() == ISD::Constant) {
302 Imm = cast<ConstantSDNode>(N)->getValue();
303 return true;
304 }
305 return false;
306}
307
Nate Begemancffc32b2005-08-18 07:30:46 +0000308// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
309// any number of 0s on either side. The 1s are allowed to wrap from LSB to
310// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
311// not, since all 1s are not contiguous.
312static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
313 if (isShiftedMask_32(Val)) {
314 // look for the first non-zero bit
315 MB = CountLeadingZeros_32(Val);
316 // look for the first zero bit after the run of ones
317 ME = CountLeadingZeros_32((Val - 1) ^ Val);
318 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000319 } else {
320 Val = ~Val; // invert mask
321 if (isShiftedMask_32(Val)) {
322 // effectively look for the first zero bit
323 ME = CountLeadingZeros_32(Val) - 1;
324 // effectively look for the first one bit after the run of zeros
325 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
326 return true;
327 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000328 }
329 // no run present
330 return false;
331}
332
Chris Lattner65a419a2005-10-09 05:36:17 +0000333// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000334// and mask opcode and mask operation.
335static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
336 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000337 // Don't even go down this path for i64, since different logic will be
338 // necessary for rldicl/rldicr/rldimi.
339 if (N->getValueType(0) != MVT::i32)
340 return false;
341
Nate Begemancffc32b2005-08-18 07:30:46 +0000342 unsigned Shift = 32;
343 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
344 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000345 if (N->getNumOperands() != 2 ||
346 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000347 return false;
348
349 if (Opcode == ISD::SHL) {
350 // apply shift left to mask if it comes first
351 if (IsShiftMask) Mask = Mask << Shift;
352 // determine which bits are made indeterminant by shift
353 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000354 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000355 // apply shift right to mask if it comes first
356 if (IsShiftMask) Mask = Mask >> Shift;
357 // determine which bits are made indeterminant by shift
358 Indeterminant = ~(0xFFFFFFFFu >> Shift);
359 // adjust for the left rotate
360 Shift = 32 - Shift;
361 } else {
362 return false;
363 }
364
365 // if the mask doesn't intersect any Indeterminant bits
366 if (Mask && !(Mask & Indeterminant)) {
367 SH = Shift;
368 // make sure the mask is still a mask (wrap arounds may not be)
369 return isRunOfOnes(Mask, MB, ME);
370 }
371 return false;
372}
373
Nate Begeman0f3257a2005-08-18 05:00:13 +0000374// isOpcWithIntImmediate - This method tests to see if the node is a specific
375// opcode and that it has a immediate integer right operand.
376// If so Imm will receive the 32 bit value.
377static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
378 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
379}
380
Chris Lattnera5a91b12005-08-17 19:33:03 +0000381// isIntImmediate - This method tests to see if a constant operand.
382// If so Imm will receive the 32 bit value.
383static bool isIntImmediate(SDOperand N, unsigned& Imm) {
384 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
385 Imm = (unsigned)CN->getSignExtended();
386 return true;
387 }
388 return false;
389}
390
Nate Begeman02b88a42005-08-19 00:38:14 +0000391/// SelectBitfieldInsert - turn an or of two masked values into
392/// the rotate left word immediate then mask insert (rlwimi) instruction.
393/// Returns true on success, false if the caller still needs to select OR.
394///
395/// Patterns matched:
396/// 1. or shl, and 5. or and, and
397/// 2. or and, shl 6. or shl, shr
398/// 3. or shr, and 7. or shr, shl
399/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000400SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000401 bool IsRotate = false;
402 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
403 unsigned Value;
404
405 SDOperand Op0 = N->getOperand(0);
406 SDOperand Op1 = N->getOperand(1);
407
408 unsigned Op0Opc = Op0.getOpcode();
409 unsigned Op1Opc = Op1.getOpcode();
410
411 // Verify that we have the correct opcodes
412 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
413 return false;
414 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
415 return false;
416
417 // Generate Mask value for Target
418 if (isIntImmediate(Op0.getOperand(1), Value)) {
419 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000420 case ISD::SHL: TgtMask <<= Value; break;
421 case ISD::SRL: TgtMask >>= Value; break;
422 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000423 }
424 } else {
425 return 0;
426 }
427
428 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000429 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000430 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000431
432 switch(Op1Opc) {
433 case ISD::SHL:
434 SH = Value;
435 InsMask <<= SH;
436 if (Op0Opc == ISD::SRL) IsRotate = true;
437 break;
438 case ISD::SRL:
439 SH = Value;
440 InsMask >>= SH;
441 SH = 32-SH;
442 if (Op0Opc == ISD::SHL) IsRotate = true;
443 break;
444 case ISD::AND:
445 InsMask &= Value;
446 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000447 }
448
449 // If both of the inputs are ANDs and one of them has a logical shift by
450 // constant as its input, make that AND the inserted value so that we can
451 // combine the shift into the rotate part of the rlwimi instruction
452 bool IsAndWithShiftOp = false;
453 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
454 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
455 Op1.getOperand(0).getOpcode() == ISD::SRL) {
456 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
457 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
458 IsAndWithShiftOp = true;
459 }
460 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
461 Op0.getOperand(0).getOpcode() == ISD::SRL) {
462 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
463 std::swap(Op0, Op1);
464 std::swap(TgtMask, InsMask);
465 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
466 IsAndWithShiftOp = true;
467 }
468 }
469 }
470
471 // Verify that the Target mask and Insert mask together form a full word mask
472 // and that the Insert mask is a run of set bits (which implies both are runs
473 // of set bits). Given that, Select the arguments and generate the rlwimi
474 // instruction.
475 unsigned MB, ME;
476 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
477 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
478 bool Op0IsAND = Op0Opc == ISD::AND;
479 // Check for rotlwi / rotrwi here, a special case of bitfield insert
480 // where both bitfield halves are sourced from the same value.
481 if (IsRotate && fullMask &&
482 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000483 SDOperand Tmp;
484 Select(Tmp, N->getOperand(0).getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000485 return CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Tmp,
486 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
Nate Begeman02b88a42005-08-19 00:38:14 +0000487 }
Evan Cheng34167212006-02-09 00:37:58 +0000488 SDOperand Tmp1, Tmp2;
489 Select(Tmp1, ((Op0IsAND && fullMask) ? Op0.getOperand(0) : Op0));
490 Select(Tmp2, (IsAndWithShiftOp ? Op1.getOperand(0).getOperand(0)
491 : Op1.getOperand(0)));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000492 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
493 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000494 }
495 return 0;
496}
497
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000498/// SelectAddrImm - Returns true if the address N can be represented by
499/// a base register plus a signed 16-bit displacement [r+imm].
500bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
501 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000502 // If this can be more profitably realized as r+r, fail.
503 if (SelectAddrIdx(N, Disp, Base))
504 return false;
505
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000506 if (N.getOpcode() == ISD::ADD) {
507 unsigned imm = 0;
508 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000509 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000510 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
511 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000512 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000513 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000514 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000515 return true; // [r+i]
516 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000517 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000518 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000519 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000520 Disp = N.getOperand(1).getOperand(0); // The global address.
521 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
522 Disp.getOpcode() == ISD::TargetConstantPool);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000523 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000524 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000525 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000526 } else if (N.getOpcode() == ISD::OR) {
527 unsigned imm = 0;
528 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
529 // If this is an or of disjoint bitfields, we can codegen this as an add
530 // (for better address arithmetic) if the LHS and RHS of the OR are
531 // provably disjoint.
532 uint64_t LHSKnownZero, LHSKnownOne;
533 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
534 LHSKnownZero, LHSKnownOne);
535 if ((LHSKnownZero|~imm) == ~0U) {
536 // If all of the bits are known zero on the LHS or RHS, the add won't
537 // carry.
538 Base = N.getOperand(0);
539 Disp = getI32Imm(imm & 0xFFFF);
540 return true;
541 }
542 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000543 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
544 // Loading from a constant address.
545 int Addr = (int)CN->getValue();
546
547 // If this address fits entirely in a 16-bit sext immediate field, codegen
548 // this as "d, 0"
549 if (Addr == (short)Addr) {
550 Disp = getI32Imm(Addr);
551 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
552 return true;
553 }
554
555 // Otherwise, break this down into an LIS + disp.
556 Disp = getI32Imm((short)Addr);
557 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
558 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000559 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000560
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000561 Disp = getI32Imm(0);
562 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
563 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000564 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000565 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000566 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000567}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000568
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000569/// SelectAddrIdx - Given the specified addressed, check to see if it can be
570/// represented as an indexed [r+r] operation. Returns false if it can
571/// be represented by [r+imm], which are preferred.
572bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
573 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000574 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000575 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000576 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
577 return false; // r+i
578 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
579 return false; // r+i
580
Evan Cheng7564e0b2006-02-05 08:45:01 +0000581 Base = N.getOperand(0);
582 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000583 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000584 } else if (N.getOpcode() == ISD::OR) {
585 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
586 return false; // r+i can fold it if we can.
587
588 // If this is an or of disjoint bitfields, we can codegen this as an add
589 // (for better address arithmetic) if the LHS and RHS of the OR are provably
590 // disjoint.
591 uint64_t LHSKnownZero, LHSKnownOne;
592 uint64_t RHSKnownZero, RHSKnownOne;
593 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
594 LHSKnownZero, LHSKnownOne);
595
596 if (LHSKnownZero) {
597 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
598 RHSKnownZero, RHSKnownOne);
599 // If all of the bits are known zero on the LHS or RHS, the add won't
600 // carry.
601 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
602 Base = N.getOperand(0);
603 Index = N.getOperand(1);
604 return true;
605 }
606 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000607 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000608
609 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000610}
611
612/// SelectAddrIdxOnly - Given the specified addressed, force it to be
613/// represented as an indexed [r+r] operation.
614bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
615 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000616 // Check to see if we can easily represent this as an [r+r] address. This
617 // will fail if it thinks that the address is more profitably represented as
618 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000619 if (SelectAddrIdx(N, Base, Index))
620 return true;
621
622 // If the operand is an addition, always emit this as [r+r], since this is
623 // better (for code size, and execution, as the memop does the add for free)
624 // than emitting an explicit add.
625 if (N.getOpcode() == ISD::ADD) {
626 Base = N.getOperand(0);
627 Index = N.getOperand(1);
628 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000629 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000630
631 // Otherwise, do it the hard way, using R0 as the base register.
632 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
633 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000634 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000635}
636
Chris Lattnere5ba5802006-03-22 05:26:03 +0000637/// SelectAddrImmShift - Returns true if the address N can be represented by
638/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
639/// for use by STD and friends.
640bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
641 SDOperand &Base) {
642 // If this can be more profitably realized as r+r, fail.
643 if (SelectAddrIdx(N, Disp, Base))
644 return false;
645
646 if (N.getOpcode() == ISD::ADD) {
647 unsigned imm = 0;
648 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
649 (imm & 3) == 0) {
650 Disp = getI32Imm((imm & 0xFFFF) >> 2);
651 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
652 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
653 } else {
654 Base = N.getOperand(0);
655 }
656 return true; // [r+i]
657 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
658 // Match LOAD (ADD (X, Lo(G))).
659 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
660 && "Cannot handle constant offsets yet!");
661 Disp = N.getOperand(1).getOperand(0); // The global address.
662 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
663 Disp.getOpcode() == ISD::TargetConstantPool);
664 Base = N.getOperand(0);
665 return true; // [&g+r]
666 }
667 } else if (N.getOpcode() == ISD::OR) {
668 unsigned imm = 0;
669 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
670 (imm & 3) == 0) {
671 // If this is an or of disjoint bitfields, we can codegen this as an add
672 // (for better address arithmetic) if the LHS and RHS of the OR are
673 // provably disjoint.
674 uint64_t LHSKnownZero, LHSKnownOne;
675 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
676 LHSKnownZero, LHSKnownOne);
677 if ((LHSKnownZero|~imm) == ~0U) {
678 // If all of the bits are known zero on the LHS or RHS, the add won't
679 // carry.
680 Base = N.getOperand(0);
681 Disp = getI32Imm((imm & 0xFFFF) >> 2);
682 return true;
683 }
684 }
685 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
686 // Loading from a constant address.
687 int Addr = (int)CN->getValue();
688 if ((Addr & 3) == 0) {
689 // If this address fits entirely in a 16-bit sext immediate field, codegen
690 // this as "d, 0"
691 if (Addr == (short)Addr) {
692 Disp = getI32Imm(Addr >> 2);
693 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
694 return true;
695 }
696
697 // Otherwise, break this down into an LIS + disp.
698 Disp = getI32Imm((short)Addr >> 2);
699 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
700 return true;
701 }
702 }
703
704 Disp = getI32Imm(0);
705 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
706 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
707 else
708 Base = N;
709 return true; // [r+0]
710}
711
712
Chris Lattner2fbb4572005-08-21 18:50:37 +0000713/// SelectCC - Select a comparison of the specified values with the specified
714/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000715SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
716 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000717 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000718 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000719
720 // Use U to determine whether the SETCC immediate range is signed or not.
721 if (MVT::isInteger(LHS.getValueType())) {
722 bool U = ISD::isUnsignedIntSetCC(CC);
723 unsigned Imm;
724 if (isIntImmediate(RHS, Imm) &&
725 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000726 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
727 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000728 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000729 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
730 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000731 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000732 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000733 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000734 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000735 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000736 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000737 }
738}
739
740/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
741/// to Condition.
742static unsigned getBCCForSetCC(ISD::CondCode CC) {
743 switch (CC) {
744 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000745 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000746 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000747 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000748 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000749 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000750 case ISD::SETULT:
751 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000752 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000753 case ISD::SETULE:
754 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000755 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000756 case ISD::SETUGT:
757 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000758 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000759 case ISD::SETUGE:
760 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000761
762 case ISD::SETO: return PPC::BUN;
763 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000764 }
765 return 0;
766}
767
Chris Lattner64906a02005-08-25 20:08:18 +0000768/// getCRIdxForSetCC - Return the index of the condition register field
769/// associated with the SetCC condition, and whether or not the field is
770/// treated as inverted. That is, lt = 0; ge = 0 inverted.
771static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
772 switch (CC) {
773 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000774 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000775 case ISD::SETULT:
776 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000777 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000778 case ISD::SETUGE:
779 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000780 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000781 case ISD::SETUGT:
782 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000783 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000784 case ISD::SETULE:
785 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000786 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000787 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000788 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000789 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000790 case ISD::SETO: Inv = true; return 3;
791 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000792 }
793 return 0;
794}
Chris Lattner9944b762005-08-21 22:31:09 +0000795
Nate Begeman1d9d7422005-10-18 00:28:58 +0000796SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000797 SDNode *N = Op.Val;
798 unsigned Imm;
799 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
800 if (isIntImmediate(N->getOperand(1), Imm)) {
801 // We can codegen setcc op, imm very efficiently compared to a brcond.
802 // Check for those cases here.
803 // setcc op, 0
804 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000805 SDOperand Op;
806 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000807 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000808 default: break;
809 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000810 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000811 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
812 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000813 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000814 SDOperand AD =
815 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
816 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000817 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
818 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000819 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000820 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000821 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
822 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000823 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000824 SDOperand T =
825 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
826 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000827 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
828 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000829 }
830 }
Chris Lattner222adac2005-10-06 19:03:35 +0000831 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000832 SDOperand Op;
833 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000834 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000835 default: break;
836 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000837 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
838 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000839 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000840 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
841 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000842 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000843 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000844 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
845 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
846 Op, getI32Imm(~0U));
847 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
848 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000849 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000850 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000851 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
852 getI32Imm(1)), 0);
853 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
854 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000855 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
856 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000857 }
858 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000859 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
860 getI32Imm(1), getI32Imm(31),
861 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000862 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000863 }
Chris Lattner222adac2005-10-06 19:03:35 +0000864 }
865 }
866
867 bool Inv;
868 unsigned Idx = getCRIdxForSetCC(CC, Inv);
869 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
870 SDOperand IntCR;
871
872 // Force the ccreg into CR7.
873 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
874
Chris Lattner85961d52005-12-06 20:56:18 +0000875 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000876 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
877 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000878
879 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000880 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
881 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000882 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000883 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000884
885 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000886 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
887 getI32Imm((32-(3-Idx)) & 31),
888 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000889 } else {
890 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000891 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
892 getI32Imm((32-(3-Idx)) & 31),
893 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000894 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000895 }
Chris Lattner222adac2005-10-06 19:03:35 +0000896}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000897
Nate Begeman422b0ce2005-11-16 00:48:01 +0000898/// isCallCompatibleAddress - Return true if the specified 32-bit value is
899/// representable in the immediate field of a Bx instruction.
900static bool isCallCompatibleAddress(ConstantSDNode *C) {
901 int Addr = C->getValue();
902 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
903 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
904}
905
Nate Begeman1d9d7422005-10-18 00:28:58 +0000906SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000907 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000908 SDOperand Chain;
909 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000910
911 unsigned CallOpcode;
912 std::vector<SDOperand> CallOperands;
913
914 if (GlobalAddressSDNode *GASD =
915 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000916 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000917 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000918 } else if (ExternalSymbolSDNode *ESSDN =
919 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000920 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000921 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000922 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
923 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
924 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
925 CallOpcode = PPC::BLA;
926 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000927 } else {
928 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000929 SDOperand Callee;
930 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000931 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
932 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000933
934 // Copy the callee address into R12 on darwin.
935 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
936 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000937
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000938 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000939 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000940 }
941
942 unsigned GPR_idx = 0, FPR_idx = 0;
943 static const unsigned GPR[] = {
944 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
945 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
946 };
947 static const unsigned FPR[] = {
948 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
949 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
950 };
951
952 SDOperand InFlag; // Null incoming flag value.
953
954 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
955 unsigned DestReg = 0;
956 MVT::ValueType RegTy = N->getOperand(i).getValueType();
957 if (RegTy == MVT::i32) {
958 assert(GPR_idx < 8 && "Too many int args");
959 DestReg = GPR[GPR_idx++];
960 } else {
961 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
962 "Unpromoted integer arg?");
963 assert(FPR_idx < 13 && "Too many fp args");
964 DestReg = FPR[FPR_idx++];
965 }
966
967 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000968 SDOperand Val;
969 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000970 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
971 InFlag = Chain.getValue(1);
972 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
973 }
974 }
975
976 // Finally, once everything is in registers to pass to the call, emit the
977 // call itself.
978 if (InFlag.Val)
979 CallOperands.push_back(InFlag); // Strong dep on register copies.
980 else
981 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000982 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
983 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000984
985 std::vector<SDOperand> CallResults;
986
987 // If the call has results, copy the values out of the ret val registers.
988 switch (N->getValueType(0)) {
989 default: assert(0 && "Unexpected ret value!");
990 case MVT::Other: break;
991 case MVT::i32:
992 if (N->getValueType(1) == MVT::i32) {
993 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
994 Chain.getValue(1)).getValue(1);
995 CallResults.push_back(Chain.getValue(0));
996 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
997 Chain.getValue(2)).getValue(1);
998 CallResults.push_back(Chain.getValue(0));
999 } else {
1000 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1001 Chain.getValue(1)).getValue(1);
1002 CallResults.push_back(Chain.getValue(0));
1003 }
1004 break;
1005 case MVT::f32:
1006 case MVT::f64:
1007 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
1008 Chain.getValue(1)).getValue(1);
1009 CallResults.push_back(Chain.getValue(0));
1010 break;
1011 }
1012
1013 CallResults.push_back(Chain);
1014 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
1015 CodeGenMap[Op.getValue(i)] = CallResults[i];
1016 return CallResults[Op.ResNo];
1017}
1018
Chris Lattnera5a91b12005-08-17 19:33:03 +00001019// Select - Convert the specified operand from a target-independent to a
1020// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +00001021void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001022 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +00001023 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +00001024 N->getOpcode() < PPCISD::FIRST_NUMBER) {
1025 Result = Op;
1026 return; // Already selected.
1027 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001028
1029 // If this has already been converted, use it.
1030 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +00001031 if (CGMI != CodeGenMap.end()) {
1032 Result = CGMI->second;
1033 return;
1034 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001035
1036 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +00001037 default: break;
Evan Cheng34167212006-02-09 00:37:58 +00001038 case ISD::SETCC:
1039 Result = SelectSETCC(Op);
1040 return;
1041 case PPCISD::CALL:
1042 Result = SelectCALL(Op);
1043 return;
1044 case PPCISD::GlobalBaseReg:
1045 Result = getGlobalBaseReg();
1046 return;
Chris Lattner860e8862005-11-17 07:30:41 +00001047
Chris Lattnere28e40a2005-08-25 00:45:43 +00001048 case ISD::FrameIndex: {
1049 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +00001050 if (N->hasOneUse()) {
1051 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
1052 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1053 getI32Imm(0));
1054 return;
1055 }
1056 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001057 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
1058 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1059 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001060 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +00001061 }
Chris Lattner88add102005-09-28 22:50:24 +00001062 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001063 // FIXME: since this depends on the setting of the carry flag from the srawi
1064 // we should really be making notes about that for the scheduler.
1065 // FIXME: It sure would be nice if we could cheaply recognize the
1066 // srl/add/sra pattern the dag combiner will generate for this as
1067 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +00001068 unsigned Imm;
1069 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +00001070 SDOperand N0;
1071 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +00001072 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001073 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +00001074 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001075 N0, getI32Imm(Log2_32(Imm)));
1076 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001077 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001078 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001079 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001080 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001081 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001082 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001083 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1084 SDOperand(Op, 0), SDOperand(Op, 1)),
1085 0);
Evan Cheng34167212006-02-09 00:37:58 +00001086 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001087 }
Evan Cheng34167212006-02-09 00:37:58 +00001088 return;
Chris Lattner8784a232005-08-25 17:50:06 +00001089 }
Chris Lattner047b9522005-08-25 22:04:30 +00001090
Chris Lattner237733e2005-09-29 23:33:31 +00001091 // Other cases are autogenerated.
1092 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001093 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001094 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +00001095 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +00001096 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1097 // with a mask, emit rlwinm
1098 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
1099 isShiftedMask_32(~Imm))) {
1100 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001101 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001102 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001103 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +00001104 } else if (Imm == 0) {
1105 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +00001106 Select(Result, N->getOperand(1));
1107 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001108 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001109 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001110 isRunOfOnes(Imm, MB, ME);
1111 SH = 0;
1112 }
Evan Cheng34167212006-02-09 00:37:58 +00001113 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1114 getI32Imm(SH), getI32Imm(MB),
1115 getI32Imm(ME));
1116 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001117 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001118 // ISD::OR doesn't get all the bitfield insertion fun.
1119 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1120 if (isIntImmediate(N->getOperand(1), Imm) &&
1121 N->getOperand(0).getOpcode() == ISD::OR &&
1122 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001123 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001124 Imm = ~(Imm^Imm2);
1125 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001126 SDOperand Tmp1, Tmp2;
1127 Select(Tmp1, N->getOperand(0).getOperand(0));
1128 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001129 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1130 Tmp1, Tmp2,
1131 getI32Imm(0), getI32Imm(MB),
1132 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001133 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001134 }
1135 }
Chris Lattner237733e2005-09-29 23:33:31 +00001136
1137 // Other cases are autogenerated.
1138 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001139 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001140 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001141 if (SDNode *I = SelectBitfieldInsert(N)) {
1142 Result = CodeGenMap[Op] = SDOperand(I, 0);
1143 return;
1144 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001145
Chris Lattner237733e2005-09-29 23:33:31 +00001146 // Other cases are autogenerated.
1147 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001148 case ISD::SHL: {
1149 unsigned Imm, SH, MB, ME;
1150 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001151 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001152 SDOperand Val;
1153 Select(Val, N->getOperand(0).getOperand(0));
1154 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1155 Val, getI32Imm(SH), getI32Imm(MB),
1156 getI32Imm(ME));
1157 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001158 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001159
1160 // Other cases are autogenerated.
1161 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001162 }
1163 case ISD::SRL: {
1164 unsigned Imm, SH, MB, ME;
1165 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001166 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001167 SDOperand Val;
1168 Select(Val, N->getOperand(0).getOperand(0));
1169 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1170 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
1171 getI32Imm(ME));
1172 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001173 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001174
1175 // Other cases are autogenerated.
1176 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001177 }
Chris Lattner13794f52005-08-26 18:46:49 +00001178 case ISD::SELECT_CC: {
1179 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1180
1181 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1182 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1183 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1184 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1185 if (N1C->isNullValue() && N3C->isNullValue() &&
1186 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001187 SDOperand LHS;
1188 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001189 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001190 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1191 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001192 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1193 SDOperand(Tmp, 0), LHS,
1194 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001195 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001196 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001197
Chris Lattner50ff55c2005-09-01 19:20:44 +00001198 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001199 unsigned BROpc = getBCCForSetCC(CC);
1200
1201 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001202 unsigned SelectCCOp;
1203 if (MVT::isInteger(N->getValueType(0)))
1204 SelectCCOp = PPC::SELECT_CC_Int;
1205 else if (N->getValueType(0) == MVT::f32)
1206 SelectCCOp = PPC::SELECT_CC_F4;
1207 else
1208 SelectCCOp = PPC::SELECT_CC_F8;
Evan Cheng34167212006-02-09 00:37:58 +00001209 SDOperand N2, N3;
1210 Select(N2, N->getOperand(2));
1211 Select(N3, N->getOperand(3));
1212 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1213 N2, N3, getI32Imm(BROpc));
1214 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001215 }
Nate Begeman81e80972006-03-17 01:40:33 +00001216 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001217 SDOperand Chain;
1218 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001219 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1220 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001221 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1222 CondCode, getI32Imm(getBCCForSetCC(CC)),
1223 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001224 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001225 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001226 }
Chris Lattner25dae722005-09-03 00:53:47 +00001227
Evan Cheng34167212006-02-09 00:37:58 +00001228 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001229}
1230
1231
Nate Begeman1d9d7422005-10-18 00:28:58 +00001232/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001233/// PowerPC-specific DAG, ready for instruction scheduling.
1234///
Evan Chengc4c62572006-03-13 23:20:37 +00001235FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001236 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001237}
1238