blob: d5a179cc5f6a73d8d028e4acff5d54cefcb0835f [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 Lattnerc04ba7a2006-05-16 23:54:25 +0000140 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
141 /// this 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 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()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000208 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Chris Lattnerbd937b92005-10-06 18:45:51 +0000209 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000210 HandleMap.clear();
211 ReplaceMap.clear();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000212 DAG.RemoveDeadNodes();
213
Chris Lattner1877ec92006-03-13 21:52:10 +0000214 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000215 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000216}
217
218/// InsertVRSaveCode - Once the entire function has been instruction selected,
219/// all virtual registers are created and all machine instructions are built,
220/// check to see if we need to save/restore VRSAVE. If so, do it.
221void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000222 // Check to see if this function uses vector registers, which means we have to
223 // save and restore the VRSAVE register and update it with the regs we use.
224 //
225 // In this case, there will be virtual registers of vector type type created
226 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000227 MachineFunction &Fn = MachineFunction::get(&F);
228 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000229 bool HasVectorVReg = false;
230 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000231 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000232 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
233 HasVectorVReg = true;
234 break;
235 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000236 if (!HasVectorVReg) return; // nothing to do.
237
Chris Lattner1877ec92006-03-13 21:52:10 +0000238 // If we have a vector register, we want to emit code into the entry and exit
239 // blocks to save and restore the VRSAVE register. We do this here (instead
240 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
241 //
242 // 1. This (trivially) reduces the load on the register allocator, by not
243 // having to represent the live range of the VRSAVE register.
244 // 2. This (more significantly) allows us to create a temporary virtual
245 // register to hold the saved VRSAVE value, allowing this temporary to be
246 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000247
248 // Create two vregs - one to hold the VRSAVE register that is live-in to the
249 // function and one for the value after having bits or'd into it.
250 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
251 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
252
253 MachineBasicBlock &EntryBB = *Fn.begin();
254 // Emit the following code into the entry block:
255 // InVRSAVE = MFVRSAVE
256 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
257 // MTVRSAVE UpdatedVRSAVE
258 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
259 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
260 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
261 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
262
263 // Find all return blocks, outputting a restore in each epilog.
264 const TargetInstrInfo &TII = *TM.getInstrInfo();
265 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
266 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
267 IP = BB->end(); --IP;
268
269 // Skip over all terminator instructions, which are part of the return
270 // sequence.
271 MachineBasicBlock::iterator I2 = IP;
272 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
273 IP = I2;
274
275 // Emit: MTVRSAVE InVRSave
276 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
277 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000278 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000279}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000280
Chris Lattner4bb18952006-03-16 18:25:23 +0000281
Chris Lattner4416f1a2005-08-19 22:38:53 +0000282/// getGlobalBaseReg - Output the instructions required to put the
283/// base address to use for accessing globals into a register.
284///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000285SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000286 if (!GlobalBaseReg) {
287 // Insert the set of GlobalBaseReg into the first MBB of the function
288 MachineBasicBlock &FirstMBB = BB->getParent()->front();
289 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
290 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000291 // FIXME: when we get to LP64, we will need to create the appropriate
292 // type of register here.
293 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000294 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
295 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
296 }
Chris Lattner9944b762005-08-21 22:31:09 +0000297 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000298}
299
300
Nate Begeman0f3257a2005-08-18 05:00:13 +0000301// isIntImmediate - This method tests to see if a constant operand.
302// If so Imm will receive the 32 bit value.
303static bool isIntImmediate(SDNode *N, unsigned& Imm) {
304 if (N->getOpcode() == ISD::Constant) {
305 Imm = cast<ConstantSDNode>(N)->getValue();
306 return true;
307 }
308 return false;
309}
310
Nate Begemancffc32b2005-08-18 07:30:46 +0000311// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
312// any number of 0s on either side. The 1s are allowed to wrap from LSB to
313// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
314// not, since all 1s are not contiguous.
315static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
316 if (isShiftedMask_32(Val)) {
317 // look for the first non-zero bit
318 MB = CountLeadingZeros_32(Val);
319 // look for the first zero bit after the run of ones
320 ME = CountLeadingZeros_32((Val - 1) ^ Val);
321 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000322 } else {
323 Val = ~Val; // invert mask
324 if (isShiftedMask_32(Val)) {
325 // effectively look for the first zero bit
326 ME = CountLeadingZeros_32(Val) - 1;
327 // effectively look for the first one bit after the run of zeros
328 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
329 return true;
330 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000331 }
332 // no run present
333 return false;
334}
335
Chris Lattner65a419a2005-10-09 05:36:17 +0000336// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000337// and mask opcode and mask operation.
338static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
339 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000340 // Don't even go down this path for i64, since different logic will be
341 // necessary for rldicl/rldicr/rldimi.
342 if (N->getValueType(0) != MVT::i32)
343 return false;
344
Nate Begemancffc32b2005-08-18 07:30:46 +0000345 unsigned Shift = 32;
346 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
347 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000348 if (N->getNumOperands() != 2 ||
349 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000350 return false;
351
352 if (Opcode == ISD::SHL) {
353 // apply shift left to mask if it comes first
354 if (IsShiftMask) Mask = Mask << Shift;
355 // determine which bits are made indeterminant by shift
356 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000357 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000358 // apply shift right to mask if it comes first
359 if (IsShiftMask) Mask = Mask >> Shift;
360 // determine which bits are made indeterminant by shift
361 Indeterminant = ~(0xFFFFFFFFu >> Shift);
362 // adjust for the left rotate
363 Shift = 32 - Shift;
364 } else {
365 return false;
366 }
367
368 // if the mask doesn't intersect any Indeterminant bits
369 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000370 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000371 // make sure the mask is still a mask (wrap arounds may not be)
372 return isRunOfOnes(Mask, MB, ME);
373 }
374 return false;
375}
376
Nate Begeman0f3257a2005-08-18 05:00:13 +0000377// isOpcWithIntImmediate - This method tests to see if the node is a specific
378// opcode and that it has a immediate integer right operand.
379// If so Imm will receive the 32 bit value.
380static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
381 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
382}
383
Chris Lattnera5a91b12005-08-17 19:33:03 +0000384// isIntImmediate - This method tests to see if a constant operand.
385// If so Imm will receive the 32 bit value.
386static bool isIntImmediate(SDOperand N, unsigned& Imm) {
387 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
388 Imm = (unsigned)CN->getSignExtended();
389 return true;
390 }
391 return false;
392}
393
Nate Begeman02b88a42005-08-19 00:38:14 +0000394/// SelectBitfieldInsert - turn an or of two masked values into
395/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000396SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000397 SDOperand Op0 = N->getOperand(0);
398 SDOperand Op1 = N->getOperand(1);
399
Nate Begeman77f361f2006-05-07 00:23:38 +0000400 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000401 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
402 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000403
Nate Begeman4667f2c2006-05-08 17:38:32 +0000404 unsigned TargetMask = LKZ;
405 unsigned InsertMask = RKZ;
406
407 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
408 unsigned Op0Opc = Op0.getOpcode();
409 unsigned Op1Opc = Op1.getOpcode();
410 unsigned Value, SH = 0;
411 TargetMask = ~TargetMask;
412 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000413
Nate Begeman4667f2c2006-05-08 17:38:32 +0000414 // If the LHS has a foldable shift and the RHS does not, then swap it to the
415 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000416 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
417 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
418 Op0.getOperand(0).getOpcode() == ISD::SRL) {
419 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
420 Op1.getOperand(0).getOpcode() != ISD::SRL) {
421 std::swap(Op0, Op1);
422 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000423 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000424 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000425 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000426 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
427 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
428 Op1.getOperand(0).getOpcode() != ISD::SRL) {
429 std::swap(Op0, Op1);
430 std::swap(Op0Opc, Op1Opc);
431 std::swap(TargetMask, InsertMask);
432 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000433 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000434
435 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000436 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000437 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000438 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000439
440 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
441 isIntImmediate(Op1.getOperand(1), Value)) {
442 Op1 = Op1.getOperand(0);
443 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
444 }
445 if (Op1Opc == ISD::AND) {
446 unsigned SHOpc = Op1.getOperand(0).getOpcode();
447 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
448 isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
449 Op1 = Op1.getOperand(0).getOperand(0);
450 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
451 } else {
452 Op1 = Op1.getOperand(0);
453 }
454 }
455
456 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
457 Select(Tmp1, Tmp3);
458 Select(Tmp2, Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000459 SH &= 31;
Nate Begeman77f361f2006-05-07 00:23:38 +0000460 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
461 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000462 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000463 }
464 return 0;
465}
466
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000467/// SelectAddrImm - Returns true if the address N can be represented by
468/// a base register plus a signed 16-bit displacement [r+imm].
469bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
470 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000471 // If this can be more profitably realized as r+r, fail.
472 if (SelectAddrIdx(N, Disp, Base))
473 return false;
474
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000475 if (N.getOpcode() == ISD::ADD) {
476 unsigned imm = 0;
477 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000478 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000479 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
480 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000481 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000482 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000483 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000484 return true; // [r+i]
485 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000486 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000487 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000488 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000489 Disp = N.getOperand(1).getOperand(0); // The global address.
490 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000491 Disp.getOpcode() == ISD::TargetConstantPool ||
492 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000493 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000494 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000495 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000496 } else if (N.getOpcode() == ISD::OR) {
497 unsigned imm = 0;
498 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
499 // If this is an or of disjoint bitfields, we can codegen this as an add
500 // (for better address arithmetic) if the LHS and RHS of the OR are
501 // provably disjoint.
502 uint64_t LHSKnownZero, LHSKnownOne;
503 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
504 LHSKnownZero, LHSKnownOne);
505 if ((LHSKnownZero|~imm) == ~0U) {
506 // If all of the bits are known zero on the LHS or RHS, the add won't
507 // carry.
508 Base = N.getOperand(0);
509 Disp = getI32Imm(imm & 0xFFFF);
510 return true;
511 }
512 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000513 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
514 // Loading from a constant address.
515 int Addr = (int)CN->getValue();
516
517 // If this address fits entirely in a 16-bit sext immediate field, codegen
518 // this as "d, 0"
519 if (Addr == (short)Addr) {
520 Disp = getI32Imm(Addr);
521 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
522 return true;
523 }
524
525 // Otherwise, break this down into an LIS + disp.
526 Disp = getI32Imm((short)Addr);
527 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
528 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000529 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000530
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000531 Disp = getI32Imm(0);
532 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
533 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000534 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000535 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000536 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000537}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000538
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000539/// SelectAddrIdx - Given the specified addressed, check to see if it can be
540/// represented as an indexed [r+r] operation. Returns false if it can
541/// be represented by [r+imm], which are preferred.
542bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
543 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000544 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000545 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000546 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
547 return false; // r+i
548 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
549 return false; // r+i
550
Evan Cheng7564e0b2006-02-05 08:45:01 +0000551 Base = N.getOperand(0);
552 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000553 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000554 } else if (N.getOpcode() == ISD::OR) {
555 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
556 return false; // r+i can fold it if we can.
557
558 // If this is an or of disjoint bitfields, we can codegen this as an add
559 // (for better address arithmetic) if the LHS and RHS of the OR are provably
560 // disjoint.
561 uint64_t LHSKnownZero, LHSKnownOne;
562 uint64_t RHSKnownZero, RHSKnownOne;
563 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
564 LHSKnownZero, LHSKnownOne);
565
566 if (LHSKnownZero) {
567 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
568 RHSKnownZero, RHSKnownOne);
569 // If all of the bits are known zero on the LHS or RHS, the add won't
570 // carry.
571 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
572 Base = N.getOperand(0);
573 Index = N.getOperand(1);
574 return true;
575 }
576 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000577 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000578
579 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000580}
581
582/// SelectAddrIdxOnly - Given the specified addressed, force it to be
583/// represented as an indexed [r+r] operation.
584bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
585 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000586 // Check to see if we can easily represent this as an [r+r] address. This
587 // will fail if it thinks that the address is more profitably represented as
588 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000589 if (SelectAddrIdx(N, Base, Index))
590 return true;
591
592 // If the operand is an addition, always emit this as [r+r], since this is
593 // better (for code size, and execution, as the memop does the add for free)
594 // than emitting an explicit add.
595 if (N.getOpcode() == ISD::ADD) {
596 Base = N.getOperand(0);
597 Index = N.getOperand(1);
598 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000599 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000600
601 // Otherwise, do it the hard way, using R0 as the base register.
602 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
603 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000604 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000605}
606
Chris Lattnere5ba5802006-03-22 05:26:03 +0000607/// SelectAddrImmShift - Returns true if the address N can be represented by
608/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
609/// for use by STD and friends.
610bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
611 SDOperand &Base) {
612 // If this can be more profitably realized as r+r, fail.
613 if (SelectAddrIdx(N, Disp, Base))
614 return false;
615
616 if (N.getOpcode() == ISD::ADD) {
617 unsigned imm = 0;
618 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
619 (imm & 3) == 0) {
620 Disp = getI32Imm((imm & 0xFFFF) >> 2);
621 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
622 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
623 } else {
624 Base = N.getOperand(0);
625 }
626 return true; // [r+i]
627 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
628 // Match LOAD (ADD (X, Lo(G))).
629 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
630 && "Cannot handle constant offsets yet!");
631 Disp = N.getOperand(1).getOperand(0); // The global address.
632 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000633 Disp.getOpcode() == ISD::TargetConstantPool ||
634 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000635 Base = N.getOperand(0);
636 return true; // [&g+r]
637 }
638 } else if (N.getOpcode() == ISD::OR) {
639 unsigned imm = 0;
640 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
641 (imm & 3) == 0) {
642 // If this is an or of disjoint bitfields, we can codegen this as an add
643 // (for better address arithmetic) if the LHS and RHS of the OR are
644 // provably disjoint.
645 uint64_t LHSKnownZero, LHSKnownOne;
646 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
647 LHSKnownZero, LHSKnownOne);
648 if ((LHSKnownZero|~imm) == ~0U) {
649 // If all of the bits are known zero on the LHS or RHS, the add won't
650 // carry.
651 Base = N.getOperand(0);
652 Disp = getI32Imm((imm & 0xFFFF) >> 2);
653 return true;
654 }
655 }
656 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
657 // Loading from a constant address.
658 int Addr = (int)CN->getValue();
659 if ((Addr & 3) == 0) {
660 // If this address fits entirely in a 16-bit sext immediate field, codegen
661 // this as "d, 0"
662 if (Addr == (short)Addr) {
663 Disp = getI32Imm(Addr >> 2);
664 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
665 return true;
666 }
667
668 // Otherwise, break this down into an LIS + disp.
669 Disp = getI32Imm((short)Addr >> 2);
670 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
671 return true;
672 }
673 }
674
675 Disp = getI32Imm(0);
676 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
677 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
678 else
679 Base = N;
680 return true; // [r+0]
681}
682
683
Chris Lattner2fbb4572005-08-21 18:50:37 +0000684/// SelectCC - Select a comparison of the specified values with the specified
685/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000686SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
687 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000688 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000689 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000690
691 // Use U to determine whether the SETCC immediate range is signed or not.
692 if (MVT::isInteger(LHS.getValueType())) {
693 bool U = ISD::isUnsignedIntSetCC(CC);
694 unsigned Imm;
695 if (isIntImmediate(RHS, Imm) &&
696 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000697 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
698 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000699 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000700 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
701 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000702 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000703 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000704 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000705 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000706 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000707 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000708 }
709}
710
711/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
712/// to Condition.
713static unsigned getBCCForSetCC(ISD::CondCode CC) {
714 switch (CC) {
715 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000716 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000717 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000718 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000719 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000720 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000721 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000722 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000723 case ISD::SETULT:
724 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000725 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000726 case ISD::SETULE:
727 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000728 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000729 case ISD::SETUGT:
730 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000731 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000732 case ISD::SETUGE:
733 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000734
735 case ISD::SETO: return PPC::BUN;
736 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000737 }
738 return 0;
739}
740
Chris Lattner64906a02005-08-25 20:08:18 +0000741/// getCRIdxForSetCC - Return the index of the condition register field
742/// associated with the SetCC condition, and whether or not the field is
743/// treated as inverted. That is, lt = 0; ge = 0 inverted.
744static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
745 switch (CC) {
746 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000747 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000748 case ISD::SETULT:
749 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000750 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000751 case ISD::SETUGE:
752 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000753 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000754 case ISD::SETUGT:
755 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000756 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000757 case ISD::SETULE:
758 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000759 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000760 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000761 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000762 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000763 case ISD::SETO: Inv = true; return 3;
764 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000765 }
766 return 0;
767}
Chris Lattner9944b762005-08-21 22:31:09 +0000768
Nate Begeman1d9d7422005-10-18 00:28:58 +0000769SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000770 SDNode *N = Op.Val;
771 unsigned Imm;
772 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
773 if (isIntImmediate(N->getOperand(1), Imm)) {
774 // We can codegen setcc op, imm very efficiently compared to a brcond.
775 // Check for those cases here.
776 // setcc op, 0
777 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000778 SDOperand Op;
779 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000780 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000781 default: break;
782 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000783 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000784 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
785 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000786 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000787 SDOperand AD =
788 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
789 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000790 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
791 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000792 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000793 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000794 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
795 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000796 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000797 SDOperand T =
798 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
799 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000800 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
801 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000802 }
803 }
Chris Lattner222adac2005-10-06 19:03:35 +0000804 } else if (Imm == ~0U) { // setcc op, -1
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::ADDIC, MVT::i32, MVT::Flag,
811 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000812 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000813 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
814 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000815 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000816 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000817 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
818 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
819 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000820 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
821 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000822 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000823 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000824 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
825 getI32Imm(1)), 0);
826 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
827 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000828 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
829 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000830 }
831 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000832 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
833 getI32Imm(1), getI32Imm(31),
834 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000835 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000836 }
Chris Lattner222adac2005-10-06 19:03:35 +0000837 }
838 }
839
840 bool Inv;
841 unsigned Idx = getCRIdxForSetCC(CC, Inv);
842 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
843 SDOperand IntCR;
844
845 // Force the ccreg into CR7.
846 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
847
Chris Lattner85961d52005-12-06 20:56:18 +0000848 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000849 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
850 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000851
852 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000853 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
854 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000855 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000856 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000857
858 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000859 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
860 getI32Imm((32-(3-Idx)) & 31),
861 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000862 } else {
863 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000864 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
865 getI32Imm((32-(3-Idx)) & 31),
866 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000867 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000868 }
Chris Lattner222adac2005-10-06 19:03:35 +0000869}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000870
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000871
Chris Lattnera5a91b12005-08-17 19:33:03 +0000872// Select - Convert the specified operand from a target-independent to a
873// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000874void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000875 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000876 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000877 N->getOpcode() < PPCISD::FIRST_NUMBER) {
878 Result = Op;
879 return; // Already selected.
880 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000881
882 // If this has already been converted, use it.
883 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000884 if (CGMI != CodeGenMap.end()) {
885 Result = CGMI->second;
886 return;
887 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000888
889 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000890 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000891 case ISD::SETCC:
892 Result = SelectSETCC(Op);
893 return;
Evan Cheng34167212006-02-09 00:37:58 +0000894 case PPCISD::GlobalBaseReg:
895 Result = getGlobalBaseReg();
896 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000897
Chris Lattnere28e40a2005-08-25 00:45:43 +0000898 case ISD::FrameIndex: {
899 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000900 if (N->hasOneUse()) {
901 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
902 CurDAG->getTargetFrameIndex(FI, MVT::i32),
903 getI32Imm(0));
904 return;
905 }
906 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000907 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
908 CurDAG->getTargetFrameIndex(FI, MVT::i32),
909 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000910 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000911 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000912
913 case PPCISD::MFCR: {
914 SDOperand InFlag;
915 Select(InFlag, N->getOperand(1));
916 // Use MFOCRF if supported.
917 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
918 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
919 N->getOperand(0), InFlag), 0);
920 else
921 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
922 CodeGenMap[Op] = Result;
923 return;
924 }
925
Chris Lattner88add102005-09-28 22:50:24 +0000926 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000927 // FIXME: since this depends on the setting of the carry flag from the srawi
928 // we should really be making notes about that for the scheduler.
929 // FIXME: It sure would be nice if we could cheaply recognize the
930 // srl/add/sra pattern the dag combiner will generate for this as
931 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000932 unsigned Imm;
933 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000934 SDOperand N0;
935 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000936 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000937 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000938 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000939 N0, getI32Imm(Log2_32(Imm)));
940 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000941 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000942 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000943 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000944 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000945 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000946 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000947 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
948 SDOperand(Op, 0), SDOperand(Op, 1)),
949 0);
Evan Cheng34167212006-02-09 00:37:58 +0000950 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000951 }
Evan Cheng34167212006-02-09 00:37:58 +0000952 return;
Chris Lattner8784a232005-08-25 17:50:06 +0000953 }
Chris Lattner047b9522005-08-25 22:04:30 +0000954
Chris Lattner237733e2005-09-29 23:33:31 +0000955 // Other cases are autogenerated.
956 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000957 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000958 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000959 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000960 // If this is an and of a value rotated between 0 and 31 bits and then and'd
961 // with a mask, emit rlwinm
962 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
963 isShiftedMask_32(~Imm))) {
964 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000965 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000966 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000967 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000968 } else if (Imm == 0) {
969 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +0000970 Select(Result, N->getOperand(1));
971 return ;
Chris Lattner3393e802005-10-25 19:32:37 +0000972 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000973 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000974 isRunOfOnes(Imm, MB, ME);
975 SH = 0;
976 }
Evan Cheng34167212006-02-09 00:37:58 +0000977 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
978 getI32Imm(SH), getI32Imm(MB),
979 getI32Imm(ME));
980 return;
Nate Begemancffc32b2005-08-18 07:30:46 +0000981 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000982 // ISD::OR doesn't get all the bitfield insertion fun.
983 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
984 if (isIntImmediate(N->getOperand(1), Imm) &&
985 N->getOperand(0).getOpcode() == ISD::OR &&
986 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000987 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000988 Imm = ~(Imm^Imm2);
989 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000990 SDOperand Tmp1, Tmp2;
991 Select(Tmp1, N->getOperand(0).getOperand(0));
992 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000993 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
994 Tmp1, Tmp2,
995 getI32Imm(0), getI32Imm(MB),
996 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000997 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000998 }
999 }
Chris Lattner237733e2005-09-29 23:33:31 +00001000
1001 // Other cases are autogenerated.
1002 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001003 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001004 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001005 if (SDNode *I = SelectBitfieldInsert(N)) {
1006 Result = CodeGenMap[Op] = SDOperand(I, 0);
1007 return;
1008 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001009
Chris Lattner237733e2005-09-29 23:33:31 +00001010 // Other cases are autogenerated.
1011 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001012 case ISD::SHL: {
1013 unsigned Imm, SH, MB, ME;
1014 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001015 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001016 SDOperand Val;
1017 Select(Val, N->getOperand(0).getOperand(0));
1018 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1019 Val, getI32Imm(SH), getI32Imm(MB),
1020 getI32Imm(ME));
1021 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001022 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001023
1024 // Other cases are autogenerated.
1025 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001026 }
1027 case ISD::SRL: {
1028 unsigned Imm, SH, MB, ME;
1029 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001030 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001031 SDOperand Val;
1032 Select(Val, N->getOperand(0).getOperand(0));
1033 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Chris Lattner0949ed52006-05-12 16:29:37 +00001034 Val, getI32Imm(SH), getI32Imm(MB),
Evan Cheng34167212006-02-09 00:37:58 +00001035 getI32Imm(ME));
1036 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001037 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001038
1039 // Other cases are autogenerated.
1040 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001041 }
Chris Lattner13794f52005-08-26 18:46:49 +00001042 case ISD::SELECT_CC: {
1043 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1044
1045 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1046 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1047 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1048 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1049 if (N1C->isNullValue() && N3C->isNullValue() &&
1050 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001051 SDOperand LHS;
1052 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001053 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001054 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1055 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001056 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1057 SDOperand(Tmp, 0), LHS,
1058 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001059 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001060 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001061
Chris Lattner50ff55c2005-09-01 19:20:44 +00001062 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001063 unsigned BROpc = getBCCForSetCC(CC);
1064
1065 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001066 unsigned SelectCCOp;
1067 if (MVT::isInteger(N->getValueType(0)))
1068 SelectCCOp = PPC::SELECT_CC_Int;
1069 else if (N->getValueType(0) == MVT::f32)
1070 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001071 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001072 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001073 else
1074 SelectCCOp = PPC::SELECT_CC_VRRC;
1075
Evan Cheng34167212006-02-09 00:37:58 +00001076 SDOperand N2, N3;
1077 Select(N2, N->getOperand(2));
1078 Select(N3, N->getOperand(3));
1079 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1080 N2, N3, getI32Imm(BROpc));
1081 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001082 }
Nate Begeman81e80972006-03-17 01:40:33 +00001083 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001084 SDOperand Chain;
1085 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001086 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1087 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001088 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1089 CondCode, getI32Imm(getBCCForSetCC(CC)),
1090 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001091 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001092 }
Nate Begeman37efe672006-04-22 18:53:45 +00001093 case ISD::BRIND: {
1094 SDOperand Chain, Target;
1095 Select(Chain, N->getOperand(0));
1096 Select(Target,N->getOperand(1));
1097 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1098 Chain), 0);
1099 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1100 return;
1101 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001102 }
Chris Lattner25dae722005-09-03 00:53:47 +00001103
Evan Cheng34167212006-02-09 00:37:58 +00001104 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001105}
1106
1107
Nate Begeman1d9d7422005-10-18 00:28:58 +00001108/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001109/// PowerPC-specific DAG, ready for instruction scheduling.
1110///
Evan Chengc4c62572006-03-13 23:20:37 +00001111FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001112 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001113}
1114