blob: 10f074f6ef1dcb4380c82e46c72cce2217f9a2ec [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 Lattner2c2c6c62006-01-22 23:41:00 +000031#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000032#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000033using namespace llvm;
34
35namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000036 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
37
38 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000039 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000040 /// instructions for SelectionDAG operations.
41 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000042 class PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000043 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000044 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000045 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000046 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000047 PPCDAGToDAGISel(PPCTargetMachine &tm)
48 : SelectionDAGISel(PPCLowering), TM(tm),
49 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000050
Chris Lattner4416f1a2005-08-19 22:38:53 +000051 virtual bool runOnFunction(Function &Fn) {
52 // Make sure we re-emit a set of the global base reg if necessary
53 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000054 SelectionDAGISel::runOnFunction(Fn);
55
56 InsertVRSaveCode(Fn);
57 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000058 }
59
Chris Lattnera5a91b12005-08-17 19:33:03 +000060 /// getI32Imm - Return a target constant with the specified value, of type
61 /// i32.
62 inline SDOperand getI32Imm(unsigned Imm) {
63 return CurDAG->getTargetConstant(Imm, MVT::i32);
64 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000065
66 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
67 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000068 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000069
70 // Select - Convert the specified operand from a target-independent to a
71 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000072 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000073
Nate Begeman02b88a42005-08-19 00:38:14 +000074 SDNode *SelectBitfieldInsert(SDNode *N);
75
Chris Lattner2fbb4572005-08-21 18:50:37 +000076 /// SelectCC - Select a comparison of the specified values with the
77 /// specified condition code, returning the CR# of the expression.
78 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
79
Nate Begeman7fd1edd2005-12-19 23:25:09 +000080 /// SelectAddrImm - Returns true if the address N can be represented by
81 /// a base register plus a signed 16-bit displacement [r+imm].
82 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
83
84 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
85 /// represented as an indexed [r+r] operation. Returns false if it can
86 /// be represented by [r+imm], which are preferred.
87 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000088
Nate Begeman7fd1edd2005-12-19 23:25:09 +000089 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
90 /// represented as an indexed [r+r] operation.
91 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000092
Chris Lattnere5ba5802006-03-22 05:26:03 +000093 /// SelectAddrImmShift - Returns true if the address N can be represented by
94 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
95 /// for use by STD and friends.
96 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
97
Chris Lattnere5d88612006-02-24 02:13:12 +000098 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
99 /// inline asm expressions.
100 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
101 char ConstraintCode,
102 std::vector<SDOperand> &OutOps,
103 SelectionDAG &DAG) {
104 SDOperand Op0, Op1;
105 switch (ConstraintCode) {
106 default: return true;
107 case 'm': // memory
108 if (!SelectAddrIdx(Op, Op0, Op1))
109 SelectAddrImm(Op, Op0, Op1);
110 break;
111 case 'o': // offsetable
112 if (!SelectAddrImm(Op, Op0, Op1)) {
113 Select(Op0, Op); // r+0.
114 Op1 = getI32Imm(0);
115 }
116 break;
117 case 'v': // not offsetable
118 SelectAddrIdxOnly(Op, Op0, Op1);
119 break;
120 }
121
122 OutOps.push_back(Op0);
123 OutOps.push_back(Op1);
124 return false;
125 }
126
Chris Lattner047b9522005-08-25 22:04:30 +0000127 SDOperand BuildSDIVSequence(SDNode *N);
128 SDOperand BuildUDIVSequence(SDNode *N);
129
Chris Lattnera5a91b12005-08-17 19:33:03 +0000130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133
Chris Lattner4bb18952006-03-16 18:25:23 +0000134 void InsertVRSaveCode(Function &Fn);
135
Chris Lattnera5a91b12005-08-17 19:33:03 +0000136 virtual const char *getPassName() const {
137 return "PowerPC DAG->DAG Pattern Instruction Selection";
138 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000139
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000140 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for this
Chris Lattnerc6644182006-03-07 06:32:48 +0000141 /// target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000142 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000143 // Should use subtarget info to pick the right hazard recognizer. For
144 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000145 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
146 assert(II && "No InstrInfo?");
147 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000148 }
Chris Lattneraf165382005-09-13 22:03:06 +0000149
150// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000151#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000152
153private:
Chris Lattner222adac2005-10-06 19:03:35 +0000154 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000155 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000156 };
157}
158
Chris Lattnerbd937b92005-10-06 18:45:51 +0000159/// InstructionSelectBasicBlock - This callback is invoked by
160/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000161void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000162 DEBUG(BB->dump());
163
164 // The selection process is inherently a bottom-up recursive process (users
165 // select their uses before themselves). Given infinite stack space, we
166 // could just start selecting on the root and traverse the whole graph. In
167 // practice however, this causes us to run out of stack space on large basic
168 // blocks. To avoid this problem, select the entry node, then all its uses,
169 // iteratively instead of recursively.
170 std::vector<SDOperand> Worklist;
171 Worklist.push_back(DAG.getEntryNode());
172
173 // Note that we can do this in the PPC target (scanning forward across token
174 // chain edges) because no nodes ever get folded across these edges. On a
175 // target like X86 which supports load/modify/store operations, this would
176 // have to be more careful.
177 while (!Worklist.empty()) {
178 SDOperand Node = Worklist.back();
179 Worklist.pop_back();
180
Chris Lattnercf01a702005-10-07 22:10:27 +0000181 // Chose from the least deep of the top two nodes.
182 if (!Worklist.empty() &&
183 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
184 std::swap(Worklist.back(), Node);
185
Chris Lattnerbd937b92005-10-06 18:45:51 +0000186 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
187 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
188 CodeGenMap.count(Node)) continue;
189
190 for (SDNode::use_iterator UI = Node.Val->use_begin(),
191 E = Node.Val->use_end(); UI != E; ++UI) {
192 // Scan the values. If this use has a value that is a token chain, add it
193 // to the worklist.
194 SDNode *User = *UI;
195 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
196 if (User->getValueType(i) == MVT::Other) {
197 Worklist.push_back(SDOperand(User, i));
198 break;
199 }
200 }
201
202 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000203 SDOperand Dummy;
204 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000205 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000206
Chris Lattnerbd937b92005-10-06 18:45:51 +0000207 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000208 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000209 CodeGenMap.clear();
210 DAG.RemoveDeadNodes();
211
Chris Lattner1877ec92006-03-13 21:52:10 +0000212 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000213 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000214}
215
216/// InsertVRSaveCode - Once the entire function has been instruction selected,
217/// all virtual registers are created and all machine instructions are built,
218/// check to see if we need to save/restore VRSAVE. If so, do it.
219void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000220 // Check to see if this function uses vector registers, which means we have to
221 // save and restore the VRSAVE register and update it with the regs we use.
222 //
223 // In this case, there will be virtual registers of vector type type created
224 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000225 MachineFunction &Fn = MachineFunction::get(&F);
226 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000227 bool HasVectorVReg = false;
228 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000229 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000230 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
231 HasVectorVReg = true;
232 break;
233 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000234 if (!HasVectorVReg) return; // nothing to do.
235
Chris Lattner1877ec92006-03-13 21:52:10 +0000236 // If we have a vector register, we want to emit code into the entry and exit
237 // blocks to save and restore the VRSAVE register. We do this here (instead
238 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
239 //
240 // 1. This (trivially) reduces the load on the register allocator, by not
241 // having to represent the live range of the VRSAVE register.
242 // 2. This (more significantly) allows us to create a temporary virtual
243 // register to hold the saved VRSAVE value, allowing this temporary to be
244 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000245
246 // Create two vregs - one to hold the VRSAVE register that is live-in to the
247 // function and one for the value after having bits or'd into it.
248 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
249 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
250
251 MachineBasicBlock &EntryBB = *Fn.begin();
252 // Emit the following code into the entry block:
253 // InVRSAVE = MFVRSAVE
254 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
255 // MTVRSAVE UpdatedVRSAVE
256 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
257 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
258 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
259 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
260
261 // Find all return blocks, outputting a restore in each epilog.
262 const TargetInstrInfo &TII = *TM.getInstrInfo();
263 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
264 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
265 IP = BB->end(); --IP;
266
267 // Skip over all terminator instructions, which are part of the return
268 // sequence.
269 MachineBasicBlock::iterator I2 = IP;
270 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
271 IP = I2;
272
273 // Emit: MTVRSAVE InVRSave
274 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
275 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000276 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000277}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000278
Chris Lattner4bb18952006-03-16 18:25:23 +0000279
Chris Lattner4416f1a2005-08-19 22:38:53 +0000280/// getGlobalBaseReg - Output the instructions required to put the
281/// base address to use for accessing globals into a register.
282///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000283SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000284 if (!GlobalBaseReg) {
285 // Insert the set of GlobalBaseReg into the first MBB of the function
286 MachineBasicBlock &FirstMBB = BB->getParent()->front();
287 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
288 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000289 // FIXME: when we get to LP64, we will need to create the appropriate
290 // type of register here.
291 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000292 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
293 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
294 }
Chris Lattner9944b762005-08-21 22:31:09 +0000295 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000296}
297
298
Nate Begeman0f3257a2005-08-18 05:00:13 +0000299// isIntImmediate - This method tests to see if a constant operand.
300// If so Imm will receive the 32 bit value.
301static bool isIntImmediate(SDNode *N, unsigned& Imm) {
302 if (N->getOpcode() == ISD::Constant) {
303 Imm = cast<ConstantSDNode>(N)->getValue();
304 return true;
305 }
306 return false;
307}
308
Nate Begemancffc32b2005-08-18 07:30:46 +0000309// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
310// any number of 0s on either side. The 1s are allowed to wrap from LSB to
311// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
312// not, since all 1s are not contiguous.
313static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
314 if (isShiftedMask_32(Val)) {
315 // look for the first non-zero bit
316 MB = CountLeadingZeros_32(Val);
317 // look for the first zero bit after the run of ones
318 ME = CountLeadingZeros_32((Val - 1) ^ Val);
319 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000320 } else {
321 Val = ~Val; // invert mask
322 if (isShiftedMask_32(Val)) {
323 // effectively look for the first zero bit
324 ME = CountLeadingZeros_32(Val) - 1;
325 // effectively look for the first one bit after the run of zeros
326 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
327 return true;
328 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000329 }
330 // no run present
331 return false;
332}
333
Chris Lattner65a419a2005-10-09 05:36:17 +0000334// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000335// and mask opcode and mask operation.
336static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
337 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000338 // Don't even go down this path for i64, since different logic will be
339 // necessary for rldicl/rldicr/rldimi.
340 if (N->getValueType(0) != MVT::i32)
341 return false;
342
Nate Begemancffc32b2005-08-18 07:30:46 +0000343 unsigned Shift = 32;
344 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
345 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000346 if (N->getNumOperands() != 2 ||
347 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000348 return false;
349
350 if (Opcode == ISD::SHL) {
351 // apply shift left to mask if it comes first
352 if (IsShiftMask) Mask = Mask << Shift;
353 // determine which bits are made indeterminant by shift
354 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000355 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000356 // apply shift right to mask if it comes first
357 if (IsShiftMask) Mask = Mask >> Shift;
358 // determine which bits are made indeterminant by shift
359 Indeterminant = ~(0xFFFFFFFFu >> Shift);
360 // adjust for the left rotate
361 Shift = 32 - Shift;
362 } else {
363 return false;
364 }
365
366 // if the mask doesn't intersect any Indeterminant bits
367 if (Mask && !(Mask & Indeterminant)) {
368 SH = Shift;
369 // make sure the mask is still a mask (wrap arounds may not be)
370 return isRunOfOnes(Mask, MB, ME);
371 }
372 return false;
373}
374
Nate Begeman0f3257a2005-08-18 05:00:13 +0000375// isOpcWithIntImmediate - This method tests to see if the node is a specific
376// opcode and that it has a immediate integer right operand.
377// If so Imm will receive the 32 bit value.
378static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
379 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
380}
381
Chris Lattnera5a91b12005-08-17 19:33:03 +0000382// isIntImmediate - This method tests to see if a constant operand.
383// If so Imm will receive the 32 bit value.
384static bool isIntImmediate(SDOperand N, unsigned& Imm) {
385 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
386 Imm = (unsigned)CN->getSignExtended();
387 return true;
388 }
389 return false;
390}
391
Nate Begeman02b88a42005-08-19 00:38:14 +0000392/// SelectBitfieldInsert - turn an or of two masked values into
393/// the rotate left word immediate then mask insert (rlwimi) instruction.
394/// Returns true on success, false if the caller still needs to select OR.
395///
396/// Patterns matched:
397/// 1. or shl, and 5. or and, and
398/// 2. or and, shl 6. or shl, shr
399/// 3. or shr, and 7. or shr, shl
400/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000401SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000402 bool IsRotate = false;
403 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
404 unsigned Value;
405
406 SDOperand Op0 = N->getOperand(0);
407 SDOperand Op1 = N->getOperand(1);
408
409 unsigned Op0Opc = Op0.getOpcode();
410 unsigned Op1Opc = Op1.getOpcode();
411
412 // Verify that we have the correct opcodes
413 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
414 return false;
415 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
416 return false;
417
418 // Generate Mask value for Target
419 if (isIntImmediate(Op0.getOperand(1), Value)) {
420 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000421 case ISD::SHL: TgtMask <<= Value; break;
422 case ISD::SRL: TgtMask >>= Value; break;
423 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000424 }
425 } else {
426 return 0;
427 }
428
429 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000430 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000431 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000432
433 switch(Op1Opc) {
434 case ISD::SHL:
435 SH = Value;
436 InsMask <<= SH;
437 if (Op0Opc == ISD::SRL) IsRotate = true;
438 break;
439 case ISD::SRL:
440 SH = Value;
441 InsMask >>= SH;
442 SH = 32-SH;
443 if (Op0Opc == ISD::SHL) IsRotate = true;
444 break;
445 case ISD::AND:
446 InsMask &= Value;
447 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000448 }
449
450 // If both of the inputs are ANDs and one of them has a logical shift by
451 // constant as its input, make that AND the inserted value so that we can
452 // combine the shift into the rotate part of the rlwimi instruction
453 bool IsAndWithShiftOp = false;
454 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
455 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
456 Op1.getOperand(0).getOpcode() == ISD::SRL) {
457 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
458 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
459 IsAndWithShiftOp = true;
460 }
461 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
462 Op0.getOperand(0).getOpcode() == ISD::SRL) {
463 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
464 std::swap(Op0, Op1);
465 std::swap(TgtMask, InsMask);
466 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
467 IsAndWithShiftOp = true;
468 }
469 }
470 }
471
472 // Verify that the Target mask and Insert mask together form a full word mask
473 // and that the Insert mask is a run of set bits (which implies both are runs
474 // of set bits). Given that, Select the arguments and generate the rlwimi
475 // instruction.
476 unsigned MB, ME;
477 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
478 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
479 bool Op0IsAND = Op0Opc == ISD::AND;
480 // Check for rotlwi / rotrwi here, a special case of bitfield insert
481 // where both bitfield halves are sourced from the same value.
482 if (IsRotate && fullMask &&
483 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000484 SDOperand Tmp;
485 Select(Tmp, N->getOperand(0).getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000486 return CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Tmp,
487 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
Nate Begeman02b88a42005-08-19 00:38:14 +0000488 }
Evan Cheng34167212006-02-09 00:37:58 +0000489 SDOperand Tmp1, Tmp2;
490 Select(Tmp1, ((Op0IsAND && fullMask) ? Op0.getOperand(0) : Op0));
491 Select(Tmp2, (IsAndWithShiftOp ? Op1.getOperand(0).getOperand(0)
492 : Op1.getOperand(0)));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000493 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
494 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000495 }
496 return 0;
497}
498
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000499/// SelectAddrImm - Returns true if the address N can be represented by
500/// a base register plus a signed 16-bit displacement [r+imm].
501bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
502 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000503 // If this can be more profitably realized as r+r, fail.
504 if (SelectAddrIdx(N, Disp, Base))
505 return false;
506
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000507 if (N.getOpcode() == ISD::ADD) {
508 unsigned imm = 0;
509 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000510 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000511 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
512 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000513 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000514 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000515 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000516 return true; // [r+i]
517 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000518 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000519 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000520 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000521 Disp = N.getOperand(1).getOperand(0); // The global address.
522 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000523 Disp.getOpcode() == ISD::TargetConstantPool ||
524 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000525 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000526 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000527 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000528 } else if (N.getOpcode() == ISD::OR) {
529 unsigned imm = 0;
530 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
531 // If this is an or of disjoint bitfields, we can codegen this as an add
532 // (for better address arithmetic) if the LHS and RHS of the OR are
533 // provably disjoint.
534 uint64_t LHSKnownZero, LHSKnownOne;
535 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
536 LHSKnownZero, LHSKnownOne);
537 if ((LHSKnownZero|~imm) == ~0U) {
538 // If all of the bits are known zero on the LHS or RHS, the add won't
539 // carry.
540 Base = N.getOperand(0);
541 Disp = getI32Imm(imm & 0xFFFF);
542 return true;
543 }
544 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000545 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
546 // Loading from a constant address.
547 int Addr = (int)CN->getValue();
548
549 // If this address fits entirely in a 16-bit sext immediate field, codegen
550 // this as "d, 0"
551 if (Addr == (short)Addr) {
552 Disp = getI32Imm(Addr);
553 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
554 return true;
555 }
556
557 // Otherwise, break this down into an LIS + disp.
558 Disp = getI32Imm((short)Addr);
559 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
560 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000561 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000562
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000563 Disp = getI32Imm(0);
564 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
565 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000566 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000567 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000568 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000569}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000570
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000571/// SelectAddrIdx - Given the specified addressed, check to see if it can be
572/// represented as an indexed [r+r] operation. Returns false if it can
573/// be represented by [r+imm], which are preferred.
574bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
575 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000576 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000577 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000578 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
579 return false; // r+i
580 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
581 return false; // r+i
582
Evan Cheng7564e0b2006-02-05 08:45:01 +0000583 Base = N.getOperand(0);
584 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000585 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000586 } else if (N.getOpcode() == ISD::OR) {
587 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
588 return false; // r+i can fold it if we can.
589
590 // If this is an or of disjoint bitfields, we can codegen this as an add
591 // (for better address arithmetic) if the LHS and RHS of the OR are provably
592 // disjoint.
593 uint64_t LHSKnownZero, LHSKnownOne;
594 uint64_t RHSKnownZero, RHSKnownOne;
595 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
596 LHSKnownZero, LHSKnownOne);
597
598 if (LHSKnownZero) {
599 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
600 RHSKnownZero, RHSKnownOne);
601 // If all of the bits are known zero on the LHS or RHS, the add won't
602 // carry.
603 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
604 Base = N.getOperand(0);
605 Index = N.getOperand(1);
606 return true;
607 }
608 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000609 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000610
611 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000612}
613
614/// SelectAddrIdxOnly - Given the specified addressed, force it to be
615/// represented as an indexed [r+r] operation.
616bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
617 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000618 // Check to see if we can easily represent this as an [r+r] address. This
619 // will fail if it thinks that the address is more profitably represented as
620 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000621 if (SelectAddrIdx(N, Base, Index))
622 return true;
623
624 // If the operand is an addition, always emit this as [r+r], since this is
625 // better (for code size, and execution, as the memop does the add for free)
626 // than emitting an explicit add.
627 if (N.getOpcode() == ISD::ADD) {
628 Base = N.getOperand(0);
629 Index = N.getOperand(1);
630 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000631 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000632
633 // Otherwise, do it the hard way, using R0 as the base register.
634 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
635 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000636 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000637}
638
Chris Lattnere5ba5802006-03-22 05:26:03 +0000639/// SelectAddrImmShift - Returns true if the address N can be represented by
640/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
641/// for use by STD and friends.
642bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
643 SDOperand &Base) {
644 // If this can be more profitably realized as r+r, fail.
645 if (SelectAddrIdx(N, Disp, Base))
646 return false;
647
648 if (N.getOpcode() == ISD::ADD) {
649 unsigned imm = 0;
650 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
651 (imm & 3) == 0) {
652 Disp = getI32Imm((imm & 0xFFFF) >> 2);
653 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
654 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
655 } else {
656 Base = N.getOperand(0);
657 }
658 return true; // [r+i]
659 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
660 // Match LOAD (ADD (X, Lo(G))).
661 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
662 && "Cannot handle constant offsets yet!");
663 Disp = N.getOperand(1).getOperand(0); // The global address.
664 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000665 Disp.getOpcode() == ISD::TargetConstantPool ||
666 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000667 Base = N.getOperand(0);
668 return true; // [&g+r]
669 }
670 } else if (N.getOpcode() == ISD::OR) {
671 unsigned imm = 0;
672 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
673 (imm & 3) == 0) {
674 // If this is an or of disjoint bitfields, we can codegen this as an add
675 // (for better address arithmetic) if the LHS and RHS of the OR are
676 // provably disjoint.
677 uint64_t LHSKnownZero, LHSKnownOne;
678 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
679 LHSKnownZero, LHSKnownOne);
680 if ((LHSKnownZero|~imm) == ~0U) {
681 // If all of the bits are known zero on the LHS or RHS, the add won't
682 // carry.
683 Base = N.getOperand(0);
684 Disp = getI32Imm((imm & 0xFFFF) >> 2);
685 return true;
686 }
687 }
688 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
689 // Loading from a constant address.
690 int Addr = (int)CN->getValue();
691 if ((Addr & 3) == 0) {
692 // If this address fits entirely in a 16-bit sext immediate field, codegen
693 // this as "d, 0"
694 if (Addr == (short)Addr) {
695 Disp = getI32Imm(Addr >> 2);
696 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
697 return true;
698 }
699
700 // Otherwise, break this down into an LIS + disp.
701 Disp = getI32Imm((short)Addr >> 2);
702 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
703 return true;
704 }
705 }
706
707 Disp = getI32Imm(0);
708 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
709 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
710 else
711 Base = N;
712 return true; // [r+0]
713}
714
715
Chris Lattner2fbb4572005-08-21 18:50:37 +0000716/// SelectCC - Select a comparison of the specified values with the specified
717/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000718SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
719 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000720 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000721 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000722
723 // Use U to determine whether the SETCC immediate range is signed or not.
724 if (MVT::isInteger(LHS.getValueType())) {
725 bool U = ISD::isUnsignedIntSetCC(CC);
726 unsigned Imm;
727 if (isIntImmediate(RHS, Imm) &&
728 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000729 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
730 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000731 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000732 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
733 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000734 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000735 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000736 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000737 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000738 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000739 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000740 }
741}
742
743/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
744/// to Condition.
745static unsigned getBCCForSetCC(ISD::CondCode CC) {
746 switch (CC) {
747 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000748 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000749 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000750 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000751 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000752 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000753 case ISD::SETULT:
754 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000755 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000756 case ISD::SETULE:
757 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000758 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000759 case ISD::SETUGT:
760 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000761 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000762 case ISD::SETUGE:
763 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000764
765 case ISD::SETO: return PPC::BUN;
766 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000767 }
768 return 0;
769}
770
Chris Lattner64906a02005-08-25 20:08:18 +0000771/// getCRIdxForSetCC - Return the index of the condition register field
772/// associated with the SetCC condition, and whether or not the field is
773/// treated as inverted. That is, lt = 0; ge = 0 inverted.
774static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
775 switch (CC) {
776 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000777 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000778 case ISD::SETULT:
779 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000780 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000781 case ISD::SETUGE:
782 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000783 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000784 case ISD::SETUGT:
785 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000786 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000787 case ISD::SETULE:
788 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000789 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000790 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000791 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000792 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000793 case ISD::SETO: Inv = true; return 3;
794 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000795 }
796 return 0;
797}
Chris Lattner9944b762005-08-21 22:31:09 +0000798
Nate Begeman1d9d7422005-10-18 00:28:58 +0000799SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000800 SDNode *N = Op.Val;
801 unsigned Imm;
802 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
803 if (isIntImmediate(N->getOperand(1), Imm)) {
804 // We can codegen setcc op, imm very efficiently compared to a brcond.
805 // Check for those cases here.
806 // setcc op, 0
807 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000808 SDOperand Op;
809 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000810 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000811 default: break;
812 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000813 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000814 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
815 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000816 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000817 SDOperand AD =
818 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
819 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000820 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
821 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000822 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000823 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000824 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
825 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000826 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000827 SDOperand T =
828 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
829 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000830 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
831 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000832 }
833 }
Chris Lattner222adac2005-10-06 19:03:35 +0000834 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000835 SDOperand Op;
836 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000837 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000838 default: break;
839 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000840 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
841 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000842 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000843 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
844 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000845 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000846 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000847 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
848 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
849 Op, getI32Imm(~0U));
850 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
851 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000852 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000853 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000854 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
855 getI32Imm(1)), 0);
856 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
857 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000858 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
859 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000860 }
861 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000862 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
863 getI32Imm(1), getI32Imm(31),
864 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000865 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000866 }
Chris Lattner222adac2005-10-06 19:03:35 +0000867 }
868 }
869
870 bool Inv;
871 unsigned Idx = getCRIdxForSetCC(CC, Inv);
872 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
873 SDOperand IntCR;
874
875 // Force the ccreg into CR7.
876 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
877
Chris Lattner85961d52005-12-06 20:56:18 +0000878 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000879 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
880 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000881
882 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000883 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
884 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000885 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000886 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000887
888 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000889 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
890 getI32Imm((32-(3-Idx)) & 31),
891 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000892 } else {
893 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000894 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
895 getI32Imm((32-(3-Idx)) & 31),
896 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000897 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000898 }
Chris Lattner222adac2005-10-06 19:03:35 +0000899}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000900
Nate Begeman422b0ce2005-11-16 00:48:01 +0000901/// isCallCompatibleAddress - Return true if the specified 32-bit value is
902/// representable in the immediate field of a Bx instruction.
903static bool isCallCompatibleAddress(ConstantSDNode *C) {
904 int Addr = C->getValue();
905 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
906 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
907}
908
Nate Begeman1d9d7422005-10-18 00:28:58 +0000909SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000910 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000911 SDOperand Chain;
912 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000913
914 unsigned CallOpcode;
915 std::vector<SDOperand> CallOperands;
916
917 if (GlobalAddressSDNode *GASD =
918 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000919 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000920 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000921 } else if (ExternalSymbolSDNode *ESSDN =
922 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000923 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000924 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000925 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
926 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
927 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
928 CallOpcode = PPC::BLA;
929 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000930 } else {
931 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000932 SDOperand Callee;
933 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000934 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
935 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000936
937 // Copy the callee address into R12 on darwin.
938 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
939 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000940
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000941 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000942 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000943 }
944
945 unsigned GPR_idx = 0, FPR_idx = 0;
946 static const unsigned GPR[] = {
947 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
948 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
949 };
950 static const unsigned FPR[] = {
951 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
952 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
953 };
954
955 SDOperand InFlag; // Null incoming flag value.
956
957 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
958 unsigned DestReg = 0;
959 MVT::ValueType RegTy = N->getOperand(i).getValueType();
960 if (RegTy == MVT::i32) {
961 assert(GPR_idx < 8 && "Too many int args");
962 DestReg = GPR[GPR_idx++];
963 } else {
964 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
965 "Unpromoted integer arg?");
966 assert(FPR_idx < 13 && "Too many fp args");
967 DestReg = FPR[FPR_idx++];
968 }
969
970 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000971 SDOperand Val;
972 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000973 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
974 InFlag = Chain.getValue(1);
975 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
976 }
977 }
978
979 // Finally, once everything is in registers to pass to the call, emit the
980 // call itself.
981 if (InFlag.Val)
982 CallOperands.push_back(InFlag); // Strong dep on register copies.
983 else
984 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000985 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
986 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000987
988 std::vector<SDOperand> CallResults;
989
990 // If the call has results, copy the values out of the ret val registers.
991 switch (N->getValueType(0)) {
992 default: assert(0 && "Unexpected ret value!");
993 case MVT::Other: break;
994 case MVT::i32:
995 if (N->getValueType(1) == MVT::i32) {
996 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
997 Chain.getValue(1)).getValue(1);
998 CallResults.push_back(Chain.getValue(0));
999 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1000 Chain.getValue(2)).getValue(1);
1001 CallResults.push_back(Chain.getValue(0));
1002 } else {
1003 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
1004 Chain.getValue(1)).getValue(1);
1005 CallResults.push_back(Chain.getValue(0));
1006 }
1007 break;
1008 case MVT::f32:
1009 case MVT::f64:
1010 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
1011 Chain.getValue(1)).getValue(1);
1012 CallResults.push_back(Chain.getValue(0));
1013 break;
1014 }
1015
1016 CallResults.push_back(Chain);
1017 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
1018 CodeGenMap[Op.getValue(i)] = CallResults[i];
1019 return CallResults[Op.ResNo];
1020}
1021
Chris Lattnera5a91b12005-08-17 19:33:03 +00001022// Select - Convert the specified operand from a target-independent to a
1023// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +00001024void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001025 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +00001026 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +00001027 N->getOpcode() < PPCISD::FIRST_NUMBER) {
1028 Result = Op;
1029 return; // Already selected.
1030 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001031
1032 // If this has already been converted, use it.
1033 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +00001034 if (CGMI != CodeGenMap.end()) {
1035 Result = CGMI->second;
1036 return;
1037 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001038
1039 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +00001040 default: break;
Evan Cheng34167212006-02-09 00:37:58 +00001041 case ISD::SETCC:
1042 Result = SelectSETCC(Op);
1043 return;
1044 case PPCISD::CALL:
1045 Result = SelectCALL(Op);
1046 return;
1047 case PPCISD::GlobalBaseReg:
1048 Result = getGlobalBaseReg();
1049 return;
Chris Lattner860e8862005-11-17 07:30:41 +00001050
Chris Lattnere28e40a2005-08-25 00:45:43 +00001051 case ISD::FrameIndex: {
1052 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +00001053 if (N->hasOneUse()) {
1054 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
1055 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1056 getI32Imm(0));
1057 return;
1058 }
1059 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001060 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
1061 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1062 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001063 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +00001064 }
Chris Lattner6d92cad2006-03-26 10:06:40 +00001065
1066 case PPCISD::MFCR: {
1067 SDOperand InFlag;
1068 Select(InFlag, N->getOperand(1));
1069 // Use MFOCRF if supported.
1070 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
1071 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
1072 N->getOperand(0), InFlag), 0);
1073 else
1074 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
1075 CodeGenMap[Op] = Result;
1076 return;
1077 }
1078
Chris Lattner88add102005-09-28 22:50:24 +00001079 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001080 // FIXME: since this depends on the setting of the carry flag from the srawi
1081 // we should really be making notes about that for the scheduler.
1082 // FIXME: It sure would be nice if we could cheaply recognize the
1083 // srl/add/sra pattern the dag combiner will generate for this as
1084 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +00001085 unsigned Imm;
1086 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +00001087 SDOperand N0;
1088 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +00001089 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001090 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +00001091 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001092 N0, getI32Imm(Log2_32(Imm)));
1093 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001094 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001095 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001096 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001097 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001098 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001099 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001100 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1101 SDOperand(Op, 0), SDOperand(Op, 1)),
1102 0);
Evan Cheng34167212006-02-09 00:37:58 +00001103 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001104 }
Evan Cheng34167212006-02-09 00:37:58 +00001105 return;
Chris Lattner8784a232005-08-25 17:50:06 +00001106 }
Chris Lattner047b9522005-08-25 22:04:30 +00001107
Chris Lattner237733e2005-09-29 23:33:31 +00001108 // Other cases are autogenerated.
1109 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001110 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001111 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +00001112 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +00001113 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1114 // with a mask, emit rlwinm
1115 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
1116 isShiftedMask_32(~Imm))) {
1117 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001118 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001119 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001120 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +00001121 } else if (Imm == 0) {
1122 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +00001123 Select(Result, N->getOperand(1));
1124 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001125 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001126 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001127 isRunOfOnes(Imm, MB, ME);
1128 SH = 0;
1129 }
Evan Cheng34167212006-02-09 00:37:58 +00001130 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1131 getI32Imm(SH), getI32Imm(MB),
1132 getI32Imm(ME));
1133 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001134 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001135 // ISD::OR doesn't get all the bitfield insertion fun.
1136 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1137 if (isIntImmediate(N->getOperand(1), Imm) &&
1138 N->getOperand(0).getOpcode() == ISD::OR &&
1139 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001140 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001141 Imm = ~(Imm^Imm2);
1142 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001143 SDOperand Tmp1, Tmp2;
1144 Select(Tmp1, N->getOperand(0).getOperand(0));
1145 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001146 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1147 Tmp1, Tmp2,
1148 getI32Imm(0), getI32Imm(MB),
1149 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001150 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001151 }
1152 }
Chris Lattner237733e2005-09-29 23:33:31 +00001153
1154 // Other cases are autogenerated.
1155 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001156 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001157 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001158 if (SDNode *I = SelectBitfieldInsert(N)) {
1159 Result = CodeGenMap[Op] = SDOperand(I, 0);
1160 return;
1161 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001162
Chris Lattner237733e2005-09-29 23:33:31 +00001163 // Other cases are autogenerated.
1164 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001165 case ISD::SHL: {
1166 unsigned Imm, SH, MB, ME;
1167 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001168 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001169 SDOperand Val;
1170 Select(Val, N->getOperand(0).getOperand(0));
1171 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1172 Val, getI32Imm(SH), getI32Imm(MB),
1173 getI32Imm(ME));
1174 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001175 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001176
1177 // Other cases are autogenerated.
1178 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001179 }
1180 case ISD::SRL: {
1181 unsigned Imm, SH, MB, ME;
1182 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001183 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001184 SDOperand Val;
1185 Select(Val, N->getOperand(0).getOperand(0));
1186 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1187 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
1188 getI32Imm(ME));
1189 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001190 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001191
1192 // Other cases are autogenerated.
1193 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001194 }
Chris Lattner13794f52005-08-26 18:46:49 +00001195 case ISD::SELECT_CC: {
1196 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1197
1198 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1199 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1200 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1201 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1202 if (N1C->isNullValue() && N3C->isNullValue() &&
1203 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001204 SDOperand LHS;
1205 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001206 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001207 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1208 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001209 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1210 SDOperand(Tmp, 0), LHS,
1211 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001212 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001213 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001214
Chris Lattner50ff55c2005-09-01 19:20:44 +00001215 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001216 unsigned BROpc = getBCCForSetCC(CC);
1217
1218 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001219 unsigned SelectCCOp;
1220 if (MVT::isInteger(N->getValueType(0)))
1221 SelectCCOp = PPC::SELECT_CC_Int;
1222 else if (N->getValueType(0) == MVT::f32)
1223 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001224 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001225 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001226 else
1227 SelectCCOp = PPC::SELECT_CC_VRRC;
1228
Evan Cheng34167212006-02-09 00:37:58 +00001229 SDOperand N2, N3;
1230 Select(N2, N->getOperand(2));
1231 Select(N3, N->getOperand(3));
1232 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1233 N2, N3, getI32Imm(BROpc));
1234 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001235 }
Nate Begeman81e80972006-03-17 01:40:33 +00001236 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001237 SDOperand Chain;
1238 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001239 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1240 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001241 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1242 CondCode, getI32Imm(getBCCForSetCC(CC)),
1243 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001244 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001245 }
Nate Begeman37efe672006-04-22 18:53:45 +00001246 case ISD::BRIND: {
1247 SDOperand Chain, Target;
1248 Select(Chain, N->getOperand(0));
1249 Select(Target,N->getOperand(1));
1250 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1251 Chain), 0);
1252 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1253 return;
1254 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001255 }
Chris Lattner25dae722005-09-03 00:53:47 +00001256
Evan Cheng34167212006-02-09 00:37:58 +00001257 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001258}
1259
1260
Nate Begeman1d9d7422005-10-18 00:28:58 +00001261/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001262/// PowerPC-specific DAG, ready for instruction scheduling.
1263///
Evan Chengc4c62572006-03-13 23:20:37 +00001264FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001265 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001266}
1267