blob: 9e22f1145b70369c570e7081bcd970e6c60f9ef0 [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()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000208 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000209 HandleMap.clear();
210 ReplaceMap.clear();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000211 DAG.RemoveDeadNodes();
212
Chris Lattner1877ec92006-03-13 21:52:10 +0000213 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000214 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000215}
216
217/// InsertVRSaveCode - Once the entire function has been instruction selected,
218/// all virtual registers are created and all machine instructions are built,
219/// check to see if we need to save/restore VRSAVE. If so, do it.
220void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000221 // Check to see if this function uses vector registers, which means we have to
222 // save and restore the VRSAVE register and update it with the regs we use.
223 //
224 // In this case, there will be virtual registers of vector type type created
225 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000226 MachineFunction &Fn = MachineFunction::get(&F);
227 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000228 bool HasVectorVReg = false;
229 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000230 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000231 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
232 HasVectorVReg = true;
233 break;
234 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000235 if (!HasVectorVReg) return; // nothing to do.
236
Chris Lattner1877ec92006-03-13 21:52:10 +0000237 // If we have a vector register, we want to emit code into the entry and exit
238 // blocks to save and restore the VRSAVE register. We do this here (instead
239 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
240 //
241 // 1. This (trivially) reduces the load on the register allocator, by not
242 // having to represent the live range of the VRSAVE register.
243 // 2. This (more significantly) allows us to create a temporary virtual
244 // register to hold the saved VRSAVE value, allowing this temporary to be
245 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000246
247 // Create two vregs - one to hold the VRSAVE register that is live-in to the
248 // function and one for the value after having bits or'd into it.
249 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
250 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
251
252 MachineBasicBlock &EntryBB = *Fn.begin();
253 // Emit the following code into the entry block:
254 // InVRSAVE = MFVRSAVE
255 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
256 // MTVRSAVE UpdatedVRSAVE
257 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
258 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
259 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
260 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
261
262 // Find all return blocks, outputting a restore in each epilog.
263 const TargetInstrInfo &TII = *TM.getInstrInfo();
264 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
265 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
266 IP = BB->end(); --IP;
267
268 // Skip over all terminator instructions, which are part of the return
269 // sequence.
270 MachineBasicBlock::iterator I2 = IP;
271 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
272 IP = I2;
273
274 // Emit: MTVRSAVE InVRSave
275 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
276 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000277 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000278}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000279
Chris Lattner4bb18952006-03-16 18:25:23 +0000280
Chris Lattner4416f1a2005-08-19 22:38:53 +0000281/// getGlobalBaseReg - Output the instructions required to put the
282/// base address to use for accessing globals into a register.
283///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000284SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000285 if (!GlobalBaseReg) {
286 // Insert the set of GlobalBaseReg into the first MBB of the function
287 MachineBasicBlock &FirstMBB = BB->getParent()->front();
288 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
289 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000290 // FIXME: when we get to LP64, we will need to create the appropriate
291 // type of register here.
292 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000293 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
294 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
295 }
Chris Lattner9944b762005-08-21 22:31:09 +0000296 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000297}
298
299
Nate Begeman0f3257a2005-08-18 05:00:13 +0000300// isIntImmediate - This method tests to see if a constant operand.
301// If so Imm will receive the 32 bit value.
302static bool isIntImmediate(SDNode *N, unsigned& Imm) {
303 if (N->getOpcode() == ISD::Constant) {
304 Imm = cast<ConstantSDNode>(N)->getValue();
305 return true;
306 }
307 return false;
308}
309
Nate Begemancffc32b2005-08-18 07:30:46 +0000310// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
311// any number of 0s on either side. The 1s are allowed to wrap from LSB to
312// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
313// not, since all 1s are not contiguous.
314static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
315 if (isShiftedMask_32(Val)) {
316 // look for the first non-zero bit
317 MB = CountLeadingZeros_32(Val);
318 // look for the first zero bit after the run of ones
319 ME = CountLeadingZeros_32((Val - 1) ^ Val);
320 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000321 } else {
322 Val = ~Val; // invert mask
323 if (isShiftedMask_32(Val)) {
324 // effectively look for the first zero bit
325 ME = CountLeadingZeros_32(Val) - 1;
326 // effectively look for the first one bit after the run of zeros
327 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
328 return true;
329 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000330 }
331 // no run present
332 return false;
333}
334
Chris Lattner65a419a2005-10-09 05:36:17 +0000335// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000336// and mask opcode and mask operation.
337static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
338 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000339 // Don't even go down this path for i64, since different logic will be
340 // necessary for rldicl/rldicr/rldimi.
341 if (N->getValueType(0) != MVT::i32)
342 return false;
343
Nate Begemancffc32b2005-08-18 07:30:46 +0000344 unsigned Shift = 32;
345 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
346 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000347 if (N->getNumOperands() != 2 ||
348 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000349 return false;
350
351 if (Opcode == ISD::SHL) {
352 // apply shift left to mask if it comes first
353 if (IsShiftMask) Mask = Mask << Shift;
354 // determine which bits are made indeterminant by shift
355 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000356 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000357 // apply shift right to mask if it comes first
358 if (IsShiftMask) Mask = Mask >> Shift;
359 // determine which bits are made indeterminant by shift
360 Indeterminant = ~(0xFFFFFFFFu >> Shift);
361 // adjust for the left rotate
362 Shift = 32 - Shift;
363 } else {
364 return false;
365 }
366
367 // if the mask doesn't intersect any Indeterminant bits
368 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000369 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000370 // make sure the mask is still a mask (wrap arounds may not be)
371 return isRunOfOnes(Mask, MB, ME);
372 }
373 return false;
374}
375
Nate Begeman0f3257a2005-08-18 05:00:13 +0000376// isOpcWithIntImmediate - This method tests to see if the node is a specific
377// opcode and that it has a immediate integer right operand.
378// If so Imm will receive the 32 bit value.
379static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
380 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
381}
382
Chris Lattnera5a91b12005-08-17 19:33:03 +0000383// isIntImmediate - This method tests to see if a constant operand.
384// If so Imm will receive the 32 bit value.
385static bool isIntImmediate(SDOperand N, unsigned& Imm) {
386 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
387 Imm = (unsigned)CN->getSignExtended();
388 return true;
389 }
390 return false;
391}
392
Nate Begeman02b88a42005-08-19 00:38:14 +0000393/// SelectBitfieldInsert - turn an or of two masked values into
394/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000395SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000396 SDOperand Op0 = N->getOperand(0);
397 SDOperand Op1 = N->getOperand(1);
398
Nate Begeman77f361f2006-05-07 00:23:38 +0000399 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000400 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
401 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000402
Nate Begeman4667f2c2006-05-08 17:38:32 +0000403 unsigned TargetMask = LKZ;
404 unsigned InsertMask = RKZ;
405
406 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
407 unsigned Op0Opc = Op0.getOpcode();
408 unsigned Op1Opc = Op1.getOpcode();
409 unsigned Value, SH = 0;
410 TargetMask = ~TargetMask;
411 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000412
Nate Begeman4667f2c2006-05-08 17:38:32 +0000413 // If the LHS has a foldable shift and the RHS does not, then swap it to the
414 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000415 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
416 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
417 Op0.getOperand(0).getOpcode() == ISD::SRL) {
418 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
419 Op1.getOperand(0).getOpcode() != ISD::SRL) {
420 std::swap(Op0, Op1);
421 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000422 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000423 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000424 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000425 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
426 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
427 Op1.getOperand(0).getOpcode() != ISD::SRL) {
428 std::swap(Op0, Op1);
429 std::swap(Op0Opc, Op1Opc);
430 std::swap(TargetMask, InsertMask);
431 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000432 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000433
434 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000435 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000436 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000437 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000438
439 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
440 isIntImmediate(Op1.getOperand(1), Value)) {
441 Op1 = Op1.getOperand(0);
442 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
443 }
444 if (Op1Opc == ISD::AND) {
445 unsigned SHOpc = Op1.getOperand(0).getOpcode();
446 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
447 isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
448 Op1 = Op1.getOperand(0).getOperand(0);
449 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
450 } else {
451 Op1 = Op1.getOperand(0);
452 }
453 }
454
455 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
456 Select(Tmp1, Tmp3);
457 Select(Tmp2, Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000458 SH &= 31;
Nate Begeman77f361f2006-05-07 00:23:38 +0000459 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
460 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000461 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000462 }
463 return 0;
464}
465
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000466/// SelectAddrImm - Returns true if the address N can be represented by
467/// a base register plus a signed 16-bit displacement [r+imm].
468bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
469 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000470 // If this can be more profitably realized as r+r, fail.
471 if (SelectAddrIdx(N, Disp, Base))
472 return false;
473
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000474 if (N.getOpcode() == ISD::ADD) {
475 unsigned imm = 0;
476 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000477 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000478 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
479 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000480 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000481 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000482 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000483 return true; // [r+i]
484 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000485 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000486 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000487 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000488 Disp = N.getOperand(1).getOperand(0); // The global address.
489 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000490 Disp.getOpcode() == ISD::TargetConstantPool ||
491 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000492 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000493 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000494 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000495 } else if (N.getOpcode() == ISD::OR) {
496 unsigned imm = 0;
497 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
498 // If this is an or of disjoint bitfields, we can codegen this as an add
499 // (for better address arithmetic) if the LHS and RHS of the OR are
500 // provably disjoint.
501 uint64_t LHSKnownZero, LHSKnownOne;
502 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
503 LHSKnownZero, LHSKnownOne);
504 if ((LHSKnownZero|~imm) == ~0U) {
505 // If all of the bits are known zero on the LHS or RHS, the add won't
506 // carry.
507 Base = N.getOperand(0);
508 Disp = getI32Imm(imm & 0xFFFF);
509 return true;
510 }
511 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000512 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
513 // Loading from a constant address.
514 int Addr = (int)CN->getValue();
515
516 // If this address fits entirely in a 16-bit sext immediate field, codegen
517 // this as "d, 0"
518 if (Addr == (short)Addr) {
519 Disp = getI32Imm(Addr);
520 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
521 return true;
522 }
523
524 // Otherwise, break this down into an LIS + disp.
525 Disp = getI32Imm((short)Addr);
526 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
527 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000528 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000529
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000530 Disp = getI32Imm(0);
531 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
532 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000533 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000534 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000535 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000536}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000537
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000538/// SelectAddrIdx - Given the specified addressed, check to see if it can be
539/// represented as an indexed [r+r] operation. Returns false if it can
540/// be represented by [r+imm], which are preferred.
541bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
542 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000543 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000544 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000545 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
546 return false; // r+i
547 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
548 return false; // r+i
549
Evan Cheng7564e0b2006-02-05 08:45:01 +0000550 Base = N.getOperand(0);
551 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000552 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000553 } else if (N.getOpcode() == ISD::OR) {
554 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
555 return false; // r+i can fold it if we can.
556
557 // If this is an or of disjoint bitfields, we can codegen this as an add
558 // (for better address arithmetic) if the LHS and RHS of the OR are provably
559 // disjoint.
560 uint64_t LHSKnownZero, LHSKnownOne;
561 uint64_t RHSKnownZero, RHSKnownOne;
562 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
563 LHSKnownZero, LHSKnownOne);
564
565 if (LHSKnownZero) {
566 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
567 RHSKnownZero, RHSKnownOne);
568 // If all of the bits are known zero on the LHS or RHS, the add won't
569 // carry.
570 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
571 Base = N.getOperand(0);
572 Index = N.getOperand(1);
573 return true;
574 }
575 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000576 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000577
578 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000579}
580
581/// SelectAddrIdxOnly - Given the specified addressed, force it to be
582/// represented as an indexed [r+r] operation.
583bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
584 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000585 // Check to see if we can easily represent this as an [r+r] address. This
586 // will fail if it thinks that the address is more profitably represented as
587 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000588 if (SelectAddrIdx(N, Base, Index))
589 return true;
590
591 // If the operand is an addition, always emit this as [r+r], since this is
592 // better (for code size, and execution, as the memop does the add for free)
593 // than emitting an explicit add.
594 if (N.getOpcode() == ISD::ADD) {
595 Base = N.getOperand(0);
596 Index = N.getOperand(1);
597 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000598 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000599
600 // Otherwise, do it the hard way, using R0 as the base register.
601 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
602 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000603 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000604}
605
Chris Lattnere5ba5802006-03-22 05:26:03 +0000606/// SelectAddrImmShift - Returns true if the address N can be represented by
607/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
608/// for use by STD and friends.
609bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
610 SDOperand &Base) {
611 // If this can be more profitably realized as r+r, fail.
612 if (SelectAddrIdx(N, Disp, Base))
613 return false;
614
615 if (N.getOpcode() == ISD::ADD) {
616 unsigned imm = 0;
617 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
618 (imm & 3) == 0) {
619 Disp = getI32Imm((imm & 0xFFFF) >> 2);
620 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
621 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
622 } else {
623 Base = N.getOperand(0);
624 }
625 return true; // [r+i]
626 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
627 // Match LOAD (ADD (X, Lo(G))).
628 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
629 && "Cannot handle constant offsets yet!");
630 Disp = N.getOperand(1).getOperand(0); // The global address.
631 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000632 Disp.getOpcode() == ISD::TargetConstantPool ||
633 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000634 Base = N.getOperand(0);
635 return true; // [&g+r]
636 }
637 } else if (N.getOpcode() == ISD::OR) {
638 unsigned imm = 0;
639 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
640 (imm & 3) == 0) {
641 // If this is an or of disjoint bitfields, we can codegen this as an add
642 // (for better address arithmetic) if the LHS and RHS of the OR are
643 // provably disjoint.
644 uint64_t LHSKnownZero, LHSKnownOne;
645 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
646 LHSKnownZero, LHSKnownOne);
647 if ((LHSKnownZero|~imm) == ~0U) {
648 // If all of the bits are known zero on the LHS or RHS, the add won't
649 // carry.
650 Base = N.getOperand(0);
651 Disp = getI32Imm((imm & 0xFFFF) >> 2);
652 return true;
653 }
654 }
655 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
656 // Loading from a constant address.
657 int Addr = (int)CN->getValue();
658 if ((Addr & 3) == 0) {
659 // If this address fits entirely in a 16-bit sext immediate field, codegen
660 // this as "d, 0"
661 if (Addr == (short)Addr) {
662 Disp = getI32Imm(Addr >> 2);
663 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
664 return true;
665 }
666
667 // Otherwise, break this down into an LIS + disp.
668 Disp = getI32Imm((short)Addr >> 2);
669 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
670 return true;
671 }
672 }
673
674 Disp = getI32Imm(0);
675 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
676 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
677 else
678 Base = N;
679 return true; // [r+0]
680}
681
682
Chris Lattner2fbb4572005-08-21 18:50:37 +0000683/// SelectCC - Select a comparison of the specified values with the specified
684/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000685SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
686 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000687 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000688 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000689
690 // Use U to determine whether the SETCC immediate range is signed or not.
691 if (MVT::isInteger(LHS.getValueType())) {
692 bool U = ISD::isUnsignedIntSetCC(CC);
693 unsigned Imm;
694 if (isIntImmediate(RHS, Imm) &&
695 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000696 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
697 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000698 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000699 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
700 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000701 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000702 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000703 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000704 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000705 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000706 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000707 }
708}
709
710/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
711/// to Condition.
712static unsigned getBCCForSetCC(ISD::CondCode CC) {
713 switch (CC) {
714 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000715 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000716 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000717 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000718 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000719 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000720 case ISD::SETULT:
721 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000722 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000723 case ISD::SETULE:
724 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000725 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000726 case ISD::SETUGT:
727 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000728 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000729 case ISD::SETUGE:
730 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000731
732 case ISD::SETO: return PPC::BUN;
733 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000734 }
735 return 0;
736}
737
Chris Lattner64906a02005-08-25 20:08:18 +0000738/// getCRIdxForSetCC - Return the index of the condition register field
739/// associated with the SetCC condition, and whether or not the field is
740/// treated as inverted. That is, lt = 0; ge = 0 inverted.
741static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
742 switch (CC) {
743 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000744 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000745 case ISD::SETULT:
746 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000747 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000748 case ISD::SETUGE:
749 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000750 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000751 case ISD::SETUGT:
752 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000753 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000754 case ISD::SETULE:
755 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000756 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000757 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000758 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000759 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000760 case ISD::SETO: Inv = true; return 3;
761 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000762 }
763 return 0;
764}
Chris Lattner9944b762005-08-21 22:31:09 +0000765
Nate Begeman1d9d7422005-10-18 00:28:58 +0000766SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000767 SDNode *N = Op.Val;
768 unsigned Imm;
769 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
770 if (isIntImmediate(N->getOperand(1), Imm)) {
771 // We can codegen setcc op, imm very efficiently compared to a brcond.
772 // Check for those cases here.
773 // setcc op, 0
774 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000775 SDOperand Op;
776 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000777 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000778 default: break;
779 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000780 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000781 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
782 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000783 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000784 SDOperand AD =
785 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
786 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000787 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
788 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000789 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000790 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000791 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
792 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000793 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000794 SDOperand T =
795 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
796 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000797 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
798 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000799 }
800 }
Chris Lattner222adac2005-10-06 19:03:35 +0000801 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000802 SDOperand Op;
803 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000804 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000805 default: break;
806 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000807 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
808 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000809 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000810 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
811 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000812 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000813 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000814 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
815 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
816 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000817 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
818 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000819 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000820 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000821 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
822 getI32Imm(1)), 0);
823 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
824 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000825 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
826 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000827 }
828 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000829 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
830 getI32Imm(1), getI32Imm(31),
831 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000832 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000833 }
Chris Lattner222adac2005-10-06 19:03:35 +0000834 }
835 }
836
837 bool Inv;
838 unsigned Idx = getCRIdxForSetCC(CC, Inv);
839 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
840 SDOperand IntCR;
841
842 // Force the ccreg into CR7.
843 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
844
Chris Lattner85961d52005-12-06 20:56:18 +0000845 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000846 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
847 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000848
849 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000850 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
851 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000852 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000853 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000854
855 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000856 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
857 getI32Imm((32-(3-Idx)) & 31),
858 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000859 } else {
860 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000861 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
862 getI32Imm((32-(3-Idx)) & 31),
863 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000864 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000865 }
Chris Lattner222adac2005-10-06 19:03:35 +0000866}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000867
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000868
Chris Lattnera5a91b12005-08-17 19:33:03 +0000869// Select - Convert the specified operand from a target-independent to a
870// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000871void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000872 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000873 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000874 N->getOpcode() < PPCISD::FIRST_NUMBER) {
875 Result = Op;
876 return; // Already selected.
877 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000878
879 // If this has already been converted, use it.
880 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000881 if (CGMI != CodeGenMap.end()) {
882 Result = CGMI->second;
883 return;
884 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000885
886 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000887 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000888 case ISD::SETCC:
889 Result = SelectSETCC(Op);
890 return;
Evan Cheng34167212006-02-09 00:37:58 +0000891 case PPCISD::GlobalBaseReg:
892 Result = getGlobalBaseReg();
893 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000894
Chris Lattnere28e40a2005-08-25 00:45:43 +0000895 case ISD::FrameIndex: {
896 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000897 if (N->hasOneUse()) {
898 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
899 CurDAG->getTargetFrameIndex(FI, MVT::i32),
900 getI32Imm(0));
901 return;
902 }
903 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000904 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
905 CurDAG->getTargetFrameIndex(FI, MVT::i32),
906 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000907 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000908 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000909
910 case PPCISD::MFCR: {
911 SDOperand InFlag;
912 Select(InFlag, N->getOperand(1));
913 // Use MFOCRF if supported.
914 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
915 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
916 N->getOperand(0), InFlag), 0);
917 else
918 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
919 CodeGenMap[Op] = Result;
920 return;
921 }
922
Chris Lattner88add102005-09-28 22:50:24 +0000923 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000924 // FIXME: since this depends on the setting of the carry flag from the srawi
925 // we should really be making notes about that for the scheduler.
926 // FIXME: It sure would be nice if we could cheaply recognize the
927 // srl/add/sra pattern the dag combiner will generate for this as
928 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000929 unsigned Imm;
930 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000931 SDOperand N0;
932 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000933 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000934 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000935 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000936 N0, getI32Imm(Log2_32(Imm)));
937 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000938 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000939 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000940 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000941 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000942 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000943 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000944 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
945 SDOperand(Op, 0), SDOperand(Op, 1)),
946 0);
Evan Cheng34167212006-02-09 00:37:58 +0000947 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000948 }
Evan Cheng34167212006-02-09 00:37:58 +0000949 return;
Chris Lattner8784a232005-08-25 17:50:06 +0000950 }
Chris Lattner047b9522005-08-25 22:04:30 +0000951
Chris Lattner237733e2005-09-29 23:33:31 +0000952 // Other cases are autogenerated.
953 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000954 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000955 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000956 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000957 // If this is an and of a value rotated between 0 and 31 bits and then and'd
958 // with a mask, emit rlwinm
959 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
960 isShiftedMask_32(~Imm))) {
961 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000962 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000963 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000964 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000965 } else if (Imm == 0) {
966 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +0000967 Select(Result, N->getOperand(1));
968 return ;
Chris Lattner3393e802005-10-25 19:32:37 +0000969 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000970 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000971 isRunOfOnes(Imm, MB, ME);
972 SH = 0;
973 }
Evan Cheng34167212006-02-09 00:37:58 +0000974 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
975 getI32Imm(SH), getI32Imm(MB),
976 getI32Imm(ME));
977 return;
Nate Begemancffc32b2005-08-18 07:30:46 +0000978 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000979 // ISD::OR doesn't get all the bitfield insertion fun.
980 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
981 if (isIntImmediate(N->getOperand(1), Imm) &&
982 N->getOperand(0).getOpcode() == ISD::OR &&
983 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000984 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000985 Imm = ~(Imm^Imm2);
986 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000987 SDOperand Tmp1, Tmp2;
988 Select(Tmp1, N->getOperand(0).getOperand(0));
989 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000990 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
991 Tmp1, Tmp2,
992 getI32Imm(0), getI32Imm(MB),
993 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000994 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000995 }
996 }
Chris Lattner237733e2005-09-29 23:33:31 +0000997
998 // Other cases are autogenerated.
999 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001000 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001001 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001002 if (SDNode *I = SelectBitfieldInsert(N)) {
1003 Result = CodeGenMap[Op] = SDOperand(I, 0);
1004 return;
1005 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001006
Chris Lattner237733e2005-09-29 23:33:31 +00001007 // Other cases are autogenerated.
1008 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001009 case ISD::SHL: {
1010 unsigned Imm, SH, MB, ME;
1011 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001012 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001013 SDOperand Val;
1014 Select(Val, N->getOperand(0).getOperand(0));
1015 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1016 Val, getI32Imm(SH), getI32Imm(MB),
1017 getI32Imm(ME));
1018 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001019 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001020
1021 // Other cases are autogenerated.
1022 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001023 }
1024 case ISD::SRL: {
1025 unsigned Imm, SH, MB, ME;
1026 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001027 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001028 SDOperand Val;
1029 Select(Val, N->getOperand(0).getOperand(0));
1030 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Chris Lattner0949ed52006-05-12 16:29:37 +00001031 Val, getI32Imm(SH), getI32Imm(MB),
Evan Cheng34167212006-02-09 00:37:58 +00001032 getI32Imm(ME));
1033 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001034 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001035
1036 // Other cases are autogenerated.
1037 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001038 }
Chris Lattner13794f52005-08-26 18:46:49 +00001039 case ISD::SELECT_CC: {
1040 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1041
1042 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1043 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1044 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1045 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1046 if (N1C->isNullValue() && N3C->isNullValue() &&
1047 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001048 SDOperand LHS;
1049 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001050 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001051 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1052 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001053 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1054 SDOperand(Tmp, 0), LHS,
1055 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001056 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001057 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001058
Chris Lattner50ff55c2005-09-01 19:20:44 +00001059 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001060 unsigned BROpc = getBCCForSetCC(CC);
1061
1062 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001063 unsigned SelectCCOp;
1064 if (MVT::isInteger(N->getValueType(0)))
1065 SelectCCOp = PPC::SELECT_CC_Int;
1066 else if (N->getValueType(0) == MVT::f32)
1067 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001068 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001069 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001070 else
1071 SelectCCOp = PPC::SELECT_CC_VRRC;
1072
Evan Cheng34167212006-02-09 00:37:58 +00001073 SDOperand N2, N3;
1074 Select(N2, N->getOperand(2));
1075 Select(N3, N->getOperand(3));
1076 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1077 N2, N3, getI32Imm(BROpc));
1078 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001079 }
Nate Begeman81e80972006-03-17 01:40:33 +00001080 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001081 SDOperand Chain;
1082 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001083 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1084 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001085 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1086 CondCode, getI32Imm(getBCCForSetCC(CC)),
1087 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001088 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001089 }
Nate Begeman37efe672006-04-22 18:53:45 +00001090 case ISD::BRIND: {
1091 SDOperand Chain, Target;
1092 Select(Chain, N->getOperand(0));
1093 Select(Target,N->getOperand(1));
1094 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1095 Chain), 0);
1096 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1097 return;
1098 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001099 }
Chris Lattner25dae722005-09-03 00:53:47 +00001100
Evan Cheng34167212006-02-09 00:37:58 +00001101 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001102}
1103
1104
Nate Begeman1d9d7422005-10-18 00:28:58 +00001105/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001106/// PowerPC-specific DAG, ready for instruction scheduling.
1107///
Evan Chengc4c62572006-03-13 23:20:37 +00001108FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001109 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001110}
1111