blob: 7ddf8c0104ddaf77dc987f204732edeaa617cba3 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattnera5a91b12005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattnera5a91b12005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
17#include "PPCISelLowering.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "PPCHazardRecognizers.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000031#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000032using namespace llvm;
33
34namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000035 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
36
37 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000038 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000039 /// instructions for SelectionDAG operations.
40 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 class PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000042 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000043 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000044 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000045 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000046 PPCDAGToDAGISel(PPCTargetMachine &tm)
47 : SelectionDAGISel(PPCLowering), TM(tm),
48 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000049
Chris Lattner4416f1a2005-08-19 22:38:53 +000050 virtual bool runOnFunction(Function &Fn) {
51 // Make sure we re-emit a set of the global base reg if necessary
52 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000053 SelectionDAGISel::runOnFunction(Fn);
54
55 InsertVRSaveCode(Fn);
56 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000057 }
58
Chris Lattnera5a91b12005-08-17 19:33:03 +000059 /// getI32Imm - Return a target constant with the specified value, of type
60 /// i32.
61 inline SDOperand getI32Imm(unsigned Imm) {
62 return CurDAG->getTargetConstant(Imm, MVT::i32);
63 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000064
65 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
66 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000067 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000068
69 // Select - Convert the specified operand from a target-independent to a
70 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000071 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000072
Nate Begeman02b88a42005-08-19 00:38:14 +000073 SDNode *SelectBitfieldInsert(SDNode *N);
74
Chris Lattner2fbb4572005-08-21 18:50:37 +000075 /// SelectCC - Select a comparison of the specified values with the
76 /// specified condition code, returning the CR# of the expression.
77 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
78
Nate Begeman7fd1edd2005-12-19 23:25:09 +000079 /// SelectAddrImm - Returns true if the address N can be represented by
80 /// a base register plus a signed 16-bit displacement [r+imm].
81 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
82
83 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
84 /// represented as an indexed [r+r] operation. Returns false if it can
85 /// be represented by [r+imm], which are preferred.
86 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000087
Nate Begeman7fd1edd2005-12-19 23:25:09 +000088 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
89 /// represented as an indexed [r+r] operation.
90 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000091
Chris Lattnere5d88612006-02-24 02:13:12 +000092 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
93 /// inline asm expressions.
94 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
95 char ConstraintCode,
96 std::vector<SDOperand> &OutOps,
97 SelectionDAG &DAG) {
98 SDOperand Op0, Op1;
99 switch (ConstraintCode) {
100 default: return true;
101 case 'm': // memory
102 if (!SelectAddrIdx(Op, Op0, Op1))
103 SelectAddrImm(Op, Op0, Op1);
104 break;
105 case 'o': // offsetable
106 if (!SelectAddrImm(Op, Op0, Op1)) {
107 Select(Op0, Op); // r+0.
108 Op1 = getI32Imm(0);
109 }
110 break;
111 case 'v': // not offsetable
112 SelectAddrIdxOnly(Op, Op0, Op1);
113 break;
114 }
115
116 OutOps.push_back(Op0);
117 OutOps.push_back(Op1);
118 return false;
119 }
120
Chris Lattner047b9522005-08-25 22:04:30 +0000121 SDOperand BuildSDIVSequence(SDNode *N);
122 SDOperand BuildUDIVSequence(SDNode *N);
123
Chris Lattnera5a91b12005-08-17 19:33:03 +0000124 /// InstructionSelectBasicBlock - This callback is invoked by
125 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000126 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
127
Chris Lattner4bb18952006-03-16 18:25:23 +0000128 void InsertVRSaveCode(Function &Fn);
129
Chris Lattnera5a91b12005-08-17 19:33:03 +0000130 virtual const char *getPassName() const {
131 return "PowerPC DAG->DAG Pattern Instruction Selection";
132 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000133
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000134 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for this
Chris Lattnerc6644182006-03-07 06:32:48 +0000135 /// target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000136 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000137 // Should use subtarget info to pick the right hazard recognizer. For
138 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000139 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
140 assert(II && "No InstrInfo?");
141 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000142 }
Chris Lattneraf165382005-09-13 22:03:06 +0000143
144// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000145#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000146
147private:
Chris Lattner222adac2005-10-06 19:03:35 +0000148 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000149 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000150 };
151}
152
Chris Lattnerbd937b92005-10-06 18:45:51 +0000153/// InstructionSelectBasicBlock - This callback is invoked by
154/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000155void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000156 DEBUG(BB->dump());
157
158 // The selection process is inherently a bottom-up recursive process (users
159 // select their uses before themselves). Given infinite stack space, we
160 // could just start selecting on the root and traverse the whole graph. In
161 // practice however, this causes us to run out of stack space on large basic
162 // blocks. To avoid this problem, select the entry node, then all its uses,
163 // iteratively instead of recursively.
164 std::vector<SDOperand> Worklist;
165 Worklist.push_back(DAG.getEntryNode());
166
167 // Note that we can do this in the PPC target (scanning forward across token
168 // chain edges) because no nodes ever get folded across these edges. On a
169 // target like X86 which supports load/modify/store operations, this would
170 // have to be more careful.
171 while (!Worklist.empty()) {
172 SDOperand Node = Worklist.back();
173 Worklist.pop_back();
174
Chris Lattnercf01a702005-10-07 22:10:27 +0000175 // Chose from the least deep of the top two nodes.
176 if (!Worklist.empty() &&
177 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
178 std::swap(Worklist.back(), Node);
179
Chris Lattnerbd937b92005-10-06 18:45:51 +0000180 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
181 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
182 CodeGenMap.count(Node)) continue;
183
184 for (SDNode::use_iterator UI = Node.Val->use_begin(),
185 E = Node.Val->use_end(); UI != E; ++UI) {
186 // Scan the values. If this use has a value that is a token chain, add it
187 // to the worklist.
188 SDNode *User = *UI;
189 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
190 if (User->getValueType(i) == MVT::Other) {
191 Worklist.push_back(SDOperand(User, i));
192 break;
193 }
194 }
195
196 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000197 SDOperand Dummy;
198 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000199 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000200
Chris Lattnerbd937b92005-10-06 18:45:51 +0000201 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000202 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000203 CodeGenMap.clear();
204 DAG.RemoveDeadNodes();
205
Chris Lattner1877ec92006-03-13 21:52:10 +0000206 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000207 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000208}
209
210/// InsertVRSaveCode - Once the entire function has been instruction selected,
211/// all virtual registers are created and all machine instructions are built,
212/// check to see if we need to save/restore VRSAVE. If so, do it.
213void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000214 // Check to see if this function uses vector registers, which means we have to
215 // save and restore the VRSAVE register and update it with the regs we use.
216 //
217 // In this case, there will be virtual registers of vector type type created
218 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000219 MachineFunction &Fn = MachineFunction::get(&F);
220 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000221 bool HasVectorVReg = false;
222 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000223 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000224 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
225 HasVectorVReg = true;
226 break;
227 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000228 if (!HasVectorVReg) return; // nothing to do.
229
Chris Lattner1877ec92006-03-13 21:52:10 +0000230 // If we have a vector register, we want to emit code into the entry and exit
231 // blocks to save and restore the VRSAVE register. We do this here (instead
232 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
233 //
234 // 1. This (trivially) reduces the load on the register allocator, by not
235 // having to represent the live range of the VRSAVE register.
236 // 2. This (more significantly) allows us to create a temporary virtual
237 // register to hold the saved VRSAVE value, allowing this temporary to be
238 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000239
240 // Create two vregs - one to hold the VRSAVE register that is live-in to the
241 // function and one for the value after having bits or'd into it.
242 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
243 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
244
245 MachineBasicBlock &EntryBB = *Fn.begin();
246 // Emit the following code into the entry block:
247 // InVRSAVE = MFVRSAVE
248 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
249 // MTVRSAVE UpdatedVRSAVE
250 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
251 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
252 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
253 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
254
255 // Find all return blocks, outputting a restore in each epilog.
256 const TargetInstrInfo &TII = *TM.getInstrInfo();
257 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
258 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
259 IP = BB->end(); --IP;
260
261 // Skip over all terminator instructions, which are part of the return
262 // sequence.
263 MachineBasicBlock::iterator I2 = IP;
264 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
265 IP = I2;
266
267 // Emit: MTVRSAVE InVRSave
268 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
269 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000270 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000271}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000272
Chris Lattner4bb18952006-03-16 18:25:23 +0000273
Chris Lattner4416f1a2005-08-19 22:38:53 +0000274/// getGlobalBaseReg - Output the instructions required to put the
275/// base address to use for accessing globals into a register.
276///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000277SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000278 if (!GlobalBaseReg) {
279 // Insert the set of GlobalBaseReg into the first MBB of the function
280 MachineBasicBlock &FirstMBB = BB->getParent()->front();
281 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
282 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000283 // FIXME: when we get to LP64, we will need to create the appropriate
284 // type of register here.
285 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000286 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
287 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
288 }
Chris Lattner9944b762005-08-21 22:31:09 +0000289 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000290}
291
292
Nate Begeman0f3257a2005-08-18 05:00:13 +0000293// isIntImmediate - This method tests to see if a constant operand.
294// If so Imm will receive the 32 bit value.
295static bool isIntImmediate(SDNode *N, unsigned& Imm) {
296 if (N->getOpcode() == ISD::Constant) {
297 Imm = cast<ConstantSDNode>(N)->getValue();
298 return true;
299 }
300 return false;
301}
302
Nate Begemancffc32b2005-08-18 07:30:46 +0000303// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
304// any number of 0s on either side. The 1s are allowed to wrap from LSB to
305// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
306// not, since all 1s are not contiguous.
307static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
308 if (isShiftedMask_32(Val)) {
309 // look for the first non-zero bit
310 MB = CountLeadingZeros_32(Val);
311 // look for the first zero bit after the run of ones
312 ME = CountLeadingZeros_32((Val - 1) ^ Val);
313 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000314 } else {
315 Val = ~Val; // invert mask
316 if (isShiftedMask_32(Val)) {
317 // effectively look for the first zero bit
318 ME = CountLeadingZeros_32(Val) - 1;
319 // effectively look for the first one bit after the run of zeros
320 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
321 return true;
322 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000323 }
324 // no run present
325 return false;
326}
327
Chris Lattner65a419a2005-10-09 05:36:17 +0000328// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000329// and mask opcode and mask operation.
330static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
331 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000332 // Don't even go down this path for i64, since different logic will be
333 // necessary for rldicl/rldicr/rldimi.
334 if (N->getValueType(0) != MVT::i32)
335 return false;
336
Nate Begemancffc32b2005-08-18 07:30:46 +0000337 unsigned Shift = 32;
338 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
339 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000340 if (N->getNumOperands() != 2 ||
341 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000342 return false;
343
344 if (Opcode == ISD::SHL) {
345 // apply shift left to mask if it comes first
346 if (IsShiftMask) Mask = Mask << Shift;
347 // determine which bits are made indeterminant by shift
348 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000349 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000350 // apply shift right to mask if it comes first
351 if (IsShiftMask) Mask = Mask >> Shift;
352 // determine which bits are made indeterminant by shift
353 Indeterminant = ~(0xFFFFFFFFu >> Shift);
354 // adjust for the left rotate
355 Shift = 32 - Shift;
356 } else {
357 return false;
358 }
359
360 // if the mask doesn't intersect any Indeterminant bits
361 if (Mask && !(Mask & Indeterminant)) {
362 SH = Shift;
363 // make sure the mask is still a mask (wrap arounds may not be)
364 return isRunOfOnes(Mask, MB, ME);
365 }
366 return false;
367}
368
Nate Begeman0f3257a2005-08-18 05:00:13 +0000369// isOpcWithIntImmediate - This method tests to see if the node is a specific
370// opcode and that it has a immediate integer right operand.
371// If so Imm will receive the 32 bit value.
372static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
373 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
374}
375
Chris Lattnera5a91b12005-08-17 19:33:03 +0000376// isIntImmediate - This method tests to see if a constant operand.
377// If so Imm will receive the 32 bit value.
378static bool isIntImmediate(SDOperand N, unsigned& Imm) {
379 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
380 Imm = (unsigned)CN->getSignExtended();
381 return true;
382 }
383 return false;
384}
385
Nate Begeman02b88a42005-08-19 00:38:14 +0000386/// SelectBitfieldInsert - turn an or of two masked values into
387/// the rotate left word immediate then mask insert (rlwimi) instruction.
388/// Returns true on success, false if the caller still needs to select OR.
389///
390/// Patterns matched:
391/// 1. or shl, and 5. or and, and
392/// 2. or and, shl 6. or shl, shr
393/// 3. or shr, and 7. or shr, shl
394/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000395SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000396 bool IsRotate = false;
397 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
398 unsigned Value;
399
400 SDOperand Op0 = N->getOperand(0);
401 SDOperand Op1 = N->getOperand(1);
402
403 unsigned Op0Opc = Op0.getOpcode();
404 unsigned Op1Opc = Op1.getOpcode();
405
406 // Verify that we have the correct opcodes
407 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
408 return false;
409 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
410 return false;
411
412 // Generate Mask value for Target
413 if (isIntImmediate(Op0.getOperand(1), Value)) {
414 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000415 case ISD::SHL: TgtMask <<= Value; break;
416 case ISD::SRL: TgtMask >>= Value; break;
417 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000418 }
419 } else {
420 return 0;
421 }
422
423 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000424 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000425 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000426
427 switch(Op1Opc) {
428 case ISD::SHL:
429 SH = Value;
430 InsMask <<= SH;
431 if (Op0Opc == ISD::SRL) IsRotate = true;
432 break;
433 case ISD::SRL:
434 SH = Value;
435 InsMask >>= SH;
436 SH = 32-SH;
437 if (Op0Opc == ISD::SHL) IsRotate = true;
438 break;
439 case ISD::AND:
440 InsMask &= Value;
441 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000442 }
443
444 // If both of the inputs are ANDs and one of them has a logical shift by
445 // constant as its input, make that AND the inserted value so that we can
446 // combine the shift into the rotate part of the rlwimi instruction
447 bool IsAndWithShiftOp = false;
448 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
449 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
450 Op1.getOperand(0).getOpcode() == ISD::SRL) {
451 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
452 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
453 IsAndWithShiftOp = true;
454 }
455 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
456 Op0.getOperand(0).getOpcode() == ISD::SRL) {
457 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
458 std::swap(Op0, Op1);
459 std::swap(TgtMask, InsMask);
460 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
461 IsAndWithShiftOp = true;
462 }
463 }
464 }
465
466 // Verify that the Target mask and Insert mask together form a full word mask
467 // and that the Insert mask is a run of set bits (which implies both are runs
468 // of set bits). Given that, Select the arguments and generate the rlwimi
469 // instruction.
470 unsigned MB, ME;
471 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
472 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
473 bool Op0IsAND = Op0Opc == ISD::AND;
474 // Check for rotlwi / rotrwi here, a special case of bitfield insert
475 // where both bitfield halves are sourced from the same value.
476 if (IsRotate && fullMask &&
477 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000478 SDOperand Tmp;
479 Select(Tmp, N->getOperand(0).getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000480 return CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Tmp,
481 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
Nate Begeman02b88a42005-08-19 00:38:14 +0000482 }
Evan Cheng34167212006-02-09 00:37:58 +0000483 SDOperand Tmp1, Tmp2;
484 Select(Tmp1, ((Op0IsAND && fullMask) ? Op0.getOperand(0) : Op0));
485 Select(Tmp2, (IsAndWithShiftOp ? Op1.getOperand(0).getOperand(0)
486 : Op1.getOperand(0)));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000487 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
488 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000489 }
490 return 0;
491}
492
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000493/// SelectAddrImm - Returns true if the address N can be represented by
494/// a base register plus a signed 16-bit displacement [r+imm].
495bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
496 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000497 // If this can be more profitably realized as r+r, fail.
498 if (SelectAddrIdx(N, Disp, Base))
499 return false;
500
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000501 if (N.getOpcode() == ISD::ADD) {
502 unsigned imm = 0;
503 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000504 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000505 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
506 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000507 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000508 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000509 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000510 return true; // [r+i]
511 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000512 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000513 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000514 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000515 Disp = N.getOperand(1).getOperand(0); // The global address.
516 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
517 Disp.getOpcode() == ISD::TargetConstantPool);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000518 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000519 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000520 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000521 } else if (N.getOpcode() == ISD::OR) {
522 unsigned imm = 0;
523 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
524 // If this is an or of disjoint bitfields, we can codegen this as an add
525 // (for better address arithmetic) if the LHS and RHS of the OR are
526 // provably disjoint.
527 uint64_t LHSKnownZero, LHSKnownOne;
528 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
529 LHSKnownZero, LHSKnownOne);
530 if ((LHSKnownZero|~imm) == ~0U) {
531 // If all of the bits are known zero on the LHS or RHS, the add won't
532 // carry.
533 Base = N.getOperand(0);
534 Disp = getI32Imm(imm & 0xFFFF);
535 return true;
536 }
537 }
Chris Lattner9944b762005-08-21 22:31:09 +0000538 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000539 Disp = getI32Imm(0);
540 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
541 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000542 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000543 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000544 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000545}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000546
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000547/// SelectAddrIdx - Given the specified addressed, check to see if it can be
548/// represented as an indexed [r+r] operation. Returns false if it can
549/// be represented by [r+imm], which are preferred.
550bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
551 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000552 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000553 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000554 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
555 return false; // r+i
556 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
557 return false; // r+i
558
Evan Cheng7564e0b2006-02-05 08:45:01 +0000559 Base = N.getOperand(0);
560 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000561 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000562 } else if (N.getOpcode() == ISD::OR) {
563 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
564 return false; // r+i can fold it if we can.
565
566 // If this is an or of disjoint bitfields, we can codegen this as an add
567 // (for better address arithmetic) if the LHS and RHS of the OR are provably
568 // disjoint.
569 uint64_t LHSKnownZero, LHSKnownOne;
570 uint64_t RHSKnownZero, RHSKnownOne;
571 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
572 LHSKnownZero, LHSKnownOne);
573
574 if (LHSKnownZero) {
575 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
576 RHSKnownZero, RHSKnownOne);
577 // If all of the bits are known zero on the LHS or RHS, the add won't
578 // carry.
579 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
580 Base = N.getOperand(0);
581 Index = N.getOperand(1);
582 return true;
583 }
584 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000585 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000586
587 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000588}
589
590/// SelectAddrIdxOnly - Given the specified addressed, force it to be
591/// represented as an indexed [r+r] operation.
592bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
593 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000594 // Check to see if we can easily represent this as an [r+r] address. This
595 // will fail if it thinks that the address is more profitably represented as
596 // reg+imm, e.g. where imm = 0.
597 if (!SelectAddrIdx(N, Base, Index)) {
598 // Nope, do it the hard way.
599 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
600 Index = N;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000601 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000602 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000603}
604
Chris Lattner2fbb4572005-08-21 18:50:37 +0000605/// SelectCC - Select a comparison of the specified values with the specified
606/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000607SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
608 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000609 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000610 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000611
612 // Use U to determine whether the SETCC immediate range is signed or not.
613 if (MVT::isInteger(LHS.getValueType())) {
614 bool U = ISD::isUnsignedIntSetCC(CC);
615 unsigned Imm;
616 if (isIntImmediate(RHS, Imm) &&
617 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000618 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
619 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000620 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000621 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
622 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000623 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000624 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000625 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000626 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000627 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000628 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000629 }
630}
631
632/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
633/// to Condition.
634static unsigned getBCCForSetCC(ISD::CondCode CC) {
635 switch (CC) {
636 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000637 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000638 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000639 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000640 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000641 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000642 case ISD::SETULT:
643 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000644 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000645 case ISD::SETULE:
646 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000647 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000648 case ISD::SETUGT:
649 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000650 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000651 case ISD::SETUGE:
652 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000653
654 case ISD::SETO: return PPC::BUN;
655 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000656 }
657 return 0;
658}
659
Chris Lattner64906a02005-08-25 20:08:18 +0000660/// getCRIdxForSetCC - Return the index of the condition register field
661/// associated with the SetCC condition, and whether or not the field is
662/// treated as inverted. That is, lt = 0; ge = 0 inverted.
663static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
664 switch (CC) {
665 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000666 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000667 case ISD::SETULT:
668 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000669 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000670 case ISD::SETUGE:
671 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000672 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000673 case ISD::SETUGT:
674 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000675 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000676 case ISD::SETULE:
677 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000678 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000679 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000680 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000681 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000682 case ISD::SETO: Inv = true; return 3;
683 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000684 }
685 return 0;
686}
Chris Lattner9944b762005-08-21 22:31:09 +0000687
Nate Begeman1d9d7422005-10-18 00:28:58 +0000688SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000689 SDNode *N = Op.Val;
690 unsigned Imm;
691 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
692 if (isIntImmediate(N->getOperand(1), Imm)) {
693 // We can codegen setcc op, imm very efficiently compared to a brcond.
694 // Check for those cases here.
695 // setcc op, 0
696 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000697 SDOperand Op;
698 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000699 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000700 default: break;
701 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000702 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000703 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
704 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000705 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000706 SDOperand AD =
707 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
708 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000709 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
710 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000711 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000712 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000713 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
714 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000715 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000716 SDOperand T =
717 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
718 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000719 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
720 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000721 }
722 }
Chris Lattner222adac2005-10-06 19:03:35 +0000723 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000724 SDOperand Op;
725 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000726 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000727 default: break;
728 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000729 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
730 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000731 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000732 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
733 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000734 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000735 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000736 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
737 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
738 Op, getI32Imm(~0U));
739 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
740 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000741 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000742 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000743 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
744 getI32Imm(1)), 0);
745 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
746 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000747 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
748 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000749 }
750 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000751 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
752 getI32Imm(1), getI32Imm(31),
753 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000754 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000755 }
Chris Lattner222adac2005-10-06 19:03:35 +0000756 }
757 }
758
759 bool Inv;
760 unsigned Idx = getCRIdxForSetCC(CC, Inv);
761 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
762 SDOperand IntCR;
763
764 // Force the ccreg into CR7.
765 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
766
Chris Lattner85961d52005-12-06 20:56:18 +0000767 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000768 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
769 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000770
771 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000772 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
773 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000774 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000775 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000776
777 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000778 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
779 getI32Imm((32-(3-Idx)) & 31),
780 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000781 } else {
782 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000783 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
784 getI32Imm((32-(3-Idx)) & 31),
785 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000786 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000787 }
Chris Lattner222adac2005-10-06 19:03:35 +0000788}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000789
Nate Begeman422b0ce2005-11-16 00:48:01 +0000790/// isCallCompatibleAddress - Return true if the specified 32-bit value is
791/// representable in the immediate field of a Bx instruction.
792static bool isCallCompatibleAddress(ConstantSDNode *C) {
793 int Addr = C->getValue();
794 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
795 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
796}
797
Nate Begeman1d9d7422005-10-18 00:28:58 +0000798SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000799 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000800 SDOperand Chain;
801 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000802
803 unsigned CallOpcode;
804 std::vector<SDOperand> CallOperands;
805
806 if (GlobalAddressSDNode *GASD =
807 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000808 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000809 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000810 } else if (ExternalSymbolSDNode *ESSDN =
811 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000812 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000813 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000814 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
815 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
816 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
817 CallOpcode = PPC::BLA;
818 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000819 } else {
820 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000821 SDOperand Callee;
822 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000823 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
824 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000825
826 // Copy the callee address into R12 on darwin.
827 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
828 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000829
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000830 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000831 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000832 }
833
834 unsigned GPR_idx = 0, FPR_idx = 0;
835 static const unsigned GPR[] = {
836 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
837 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
838 };
839 static const unsigned FPR[] = {
840 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
841 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
842 };
843
844 SDOperand InFlag; // Null incoming flag value.
845
846 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
847 unsigned DestReg = 0;
848 MVT::ValueType RegTy = N->getOperand(i).getValueType();
849 if (RegTy == MVT::i32) {
850 assert(GPR_idx < 8 && "Too many int args");
851 DestReg = GPR[GPR_idx++];
852 } else {
853 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
854 "Unpromoted integer arg?");
855 assert(FPR_idx < 13 && "Too many fp args");
856 DestReg = FPR[FPR_idx++];
857 }
858
859 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000860 SDOperand Val;
861 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000862 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
863 InFlag = Chain.getValue(1);
864 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
865 }
866 }
867
868 // Finally, once everything is in registers to pass to the call, emit the
869 // call itself.
870 if (InFlag.Val)
871 CallOperands.push_back(InFlag); // Strong dep on register copies.
872 else
873 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000874 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
875 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000876
877 std::vector<SDOperand> CallResults;
878
879 // If the call has results, copy the values out of the ret val registers.
880 switch (N->getValueType(0)) {
881 default: assert(0 && "Unexpected ret value!");
882 case MVT::Other: break;
883 case MVT::i32:
884 if (N->getValueType(1) == MVT::i32) {
885 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
886 Chain.getValue(1)).getValue(1);
887 CallResults.push_back(Chain.getValue(0));
888 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
889 Chain.getValue(2)).getValue(1);
890 CallResults.push_back(Chain.getValue(0));
891 } else {
892 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
893 Chain.getValue(1)).getValue(1);
894 CallResults.push_back(Chain.getValue(0));
895 }
896 break;
897 case MVT::f32:
898 case MVT::f64:
899 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
900 Chain.getValue(1)).getValue(1);
901 CallResults.push_back(Chain.getValue(0));
902 break;
903 }
904
905 CallResults.push_back(Chain);
906 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
907 CodeGenMap[Op.getValue(i)] = CallResults[i];
908 return CallResults[Op.ResNo];
909}
910
Chris Lattnera5a91b12005-08-17 19:33:03 +0000911// Select - Convert the specified operand from a target-independent to a
912// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000913void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000914 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000915 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000916 N->getOpcode() < PPCISD::FIRST_NUMBER) {
917 Result = Op;
918 return; // Already selected.
919 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000920
921 // If this has already been converted, use it.
922 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000923 if (CGMI != CodeGenMap.end()) {
924 Result = CGMI->second;
925 return;
926 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000927
928 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000929 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000930 case ISD::SETCC:
931 Result = SelectSETCC(Op);
932 return;
933 case PPCISD::CALL:
934 Result = SelectCALL(Op);
935 return;
936 case PPCISD::GlobalBaseReg:
937 Result = getGlobalBaseReg();
938 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000939
Chris Lattnere28e40a2005-08-25 00:45:43 +0000940 case ISD::FrameIndex: {
941 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000942 if (N->hasOneUse()) {
943 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
944 CurDAG->getTargetFrameIndex(FI, MVT::i32),
945 getI32Imm(0));
946 return;
947 }
948 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000949 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
950 CurDAG->getTargetFrameIndex(FI, MVT::i32),
951 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000952 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000953 }
Chris Lattner88add102005-09-28 22:50:24 +0000954 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000955 // FIXME: since this depends on the setting of the carry flag from the srawi
956 // we should really be making notes about that for the scheduler.
957 // FIXME: It sure would be nice if we could cheaply recognize the
958 // srl/add/sra pattern the dag combiner will generate for this as
959 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000960 unsigned Imm;
961 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000962 SDOperand N0;
963 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000964 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000965 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000966 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000967 N0, getI32Imm(Log2_32(Imm)));
968 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000969 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000970 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000971 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000972 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000973 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000974 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000975 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
976 SDOperand(Op, 0), SDOperand(Op, 1)),
977 0);
Evan Cheng34167212006-02-09 00:37:58 +0000978 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000979 }
Evan Cheng34167212006-02-09 00:37:58 +0000980 return;
Chris Lattner8784a232005-08-25 17:50:06 +0000981 }
Chris Lattner047b9522005-08-25 22:04:30 +0000982
Chris Lattner237733e2005-09-29 23:33:31 +0000983 // Other cases are autogenerated.
984 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000985 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000986 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000987 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000988 // If this is an and of a value rotated between 0 and 31 bits and then and'd
989 // with a mask, emit rlwinm
990 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
991 isShiftedMask_32(~Imm))) {
992 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000993 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000994 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000995 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000996 } else if (Imm == 0) {
997 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +0000998 Select(Result, N->getOperand(1));
999 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001000 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001001 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001002 isRunOfOnes(Imm, MB, ME);
1003 SH = 0;
1004 }
Evan Cheng34167212006-02-09 00:37:58 +00001005 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1006 getI32Imm(SH), getI32Imm(MB),
1007 getI32Imm(ME));
1008 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001009 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001010 // ISD::OR doesn't get all the bitfield insertion fun.
1011 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1012 if (isIntImmediate(N->getOperand(1), Imm) &&
1013 N->getOperand(0).getOpcode() == ISD::OR &&
1014 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001015 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001016 Imm = ~(Imm^Imm2);
1017 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001018 SDOperand Tmp1, Tmp2;
1019 Select(Tmp1, N->getOperand(0).getOperand(0));
1020 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001021 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1022 Tmp1, Tmp2,
1023 getI32Imm(0), getI32Imm(MB),
1024 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001025 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001026 }
1027 }
Chris Lattner237733e2005-09-29 23:33:31 +00001028
1029 // Other cases are autogenerated.
1030 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001031 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001032 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001033 if (SDNode *I = SelectBitfieldInsert(N)) {
1034 Result = CodeGenMap[Op] = SDOperand(I, 0);
1035 return;
1036 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001037
Chris Lattner237733e2005-09-29 23:33:31 +00001038 // Other cases are autogenerated.
1039 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001040 case ISD::SHL: {
1041 unsigned Imm, SH, MB, ME;
1042 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001043 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001044 SDOperand Val;
1045 Select(Val, N->getOperand(0).getOperand(0));
1046 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1047 Val, getI32Imm(SH), getI32Imm(MB),
1048 getI32Imm(ME));
1049 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001050 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001051
1052 // Other cases are autogenerated.
1053 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001054 }
1055 case ISD::SRL: {
1056 unsigned Imm, SH, MB, ME;
1057 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001058 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001059 SDOperand Val;
1060 Select(Val, N->getOperand(0).getOperand(0));
1061 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1062 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
1063 getI32Imm(ME));
1064 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001065 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001066
1067 // Other cases are autogenerated.
1068 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001069 }
Chris Lattner13794f52005-08-26 18:46:49 +00001070 case ISD::SELECT_CC: {
1071 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1072
1073 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1074 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1075 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1076 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1077 if (N1C->isNullValue() && N3C->isNullValue() &&
1078 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001079 SDOperand LHS;
1080 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001081 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001082 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1083 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001084 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1085 SDOperand(Tmp, 0), LHS,
1086 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001087 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001088 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001089
Chris Lattner50ff55c2005-09-01 19:20:44 +00001090 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001091 unsigned BROpc = getBCCForSetCC(CC);
1092
1093 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001094 unsigned SelectCCOp;
1095 if (MVT::isInteger(N->getValueType(0)))
1096 SelectCCOp = PPC::SELECT_CC_Int;
1097 else if (N->getValueType(0) == MVT::f32)
1098 SelectCCOp = PPC::SELECT_CC_F4;
1099 else
1100 SelectCCOp = PPC::SELECT_CC_F8;
Evan Cheng34167212006-02-09 00:37:58 +00001101 SDOperand N2, N3;
1102 Select(N2, N->getOperand(2));
1103 Select(N3, N->getOperand(3));
1104 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1105 N2, N3, getI32Imm(BROpc));
1106 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001107 }
Nate Begeman81e80972006-03-17 01:40:33 +00001108 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001109 SDOperand Chain;
1110 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001111 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1112 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001113 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1114 CondCode, getI32Imm(getBCCForSetCC(CC)),
1115 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001116 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001117 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001118 }
Chris Lattner25dae722005-09-03 00:53:47 +00001119
Evan Cheng34167212006-02-09 00:37:58 +00001120 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001121}
1122
1123
Nate Begeman1d9d7422005-10-18 00:28:58 +00001124/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001125/// PowerPC-specific DAG, ready for instruction scheduling.
1126///
Evan Chengc4c62572006-03-13 23:20:37 +00001127FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001128 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001129}
1130