blob: bc6573b13c59052b55ce94b5ad1c64843ef39621 [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 Lattnerd9796442006-03-20 22:38:22 +0000538 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
539 // Loading from a constant address.
540 int Addr = (int)CN->getValue();
541
542 // If this address fits entirely in a 16-bit sext immediate field, codegen
543 // this as "d, 0"
544 if (Addr == (short)Addr) {
545 Disp = getI32Imm(Addr);
546 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
547 return true;
548 }
549
550 // Otherwise, break this down into an LIS + disp.
551 Disp = getI32Imm((short)Addr);
552 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
553 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000554 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000555
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000556 Disp = getI32Imm(0);
557 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
558 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000559 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000560 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000561 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000562}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000563
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000564/// SelectAddrIdx - Given the specified addressed, check to see if it can be
565/// represented as an indexed [r+r] operation. Returns false if it can
566/// be represented by [r+imm], which are preferred.
567bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
568 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000569 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000570 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000571 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
572 return false; // r+i
573 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
574 return false; // r+i
575
Evan Cheng7564e0b2006-02-05 08:45:01 +0000576 Base = N.getOperand(0);
577 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000578 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000579 } else if (N.getOpcode() == ISD::OR) {
580 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
581 return false; // r+i can fold it if we can.
582
583 // If this is an or of disjoint bitfields, we can codegen this as an add
584 // (for better address arithmetic) if the LHS and RHS of the OR are provably
585 // disjoint.
586 uint64_t LHSKnownZero, LHSKnownOne;
587 uint64_t RHSKnownZero, RHSKnownOne;
588 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
589 LHSKnownZero, LHSKnownOne);
590
591 if (LHSKnownZero) {
592 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
593 RHSKnownZero, RHSKnownOne);
594 // If all of the bits are known zero on the LHS or RHS, the add won't
595 // carry.
596 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
597 Base = N.getOperand(0);
598 Index = N.getOperand(1);
599 return true;
600 }
601 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000602 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000603
604 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000605}
606
607/// SelectAddrIdxOnly - Given the specified addressed, force it to be
608/// represented as an indexed [r+r] operation.
609bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
610 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000611 // Check to see if we can easily represent this as an [r+r] address. This
612 // will fail if it thinks that the address is more profitably represented as
613 // reg+imm, e.g. where imm = 0.
614 if (!SelectAddrIdx(N, Base, Index)) {
615 // Nope, do it the hard way.
616 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
617 Index = N;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000618 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000619 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000620}
621
Chris Lattner2fbb4572005-08-21 18:50:37 +0000622/// SelectCC - Select a comparison of the specified values with the specified
623/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000624SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
625 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000626 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000627 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000628
629 // Use U to determine whether the SETCC immediate range is signed or not.
630 if (MVT::isInteger(LHS.getValueType())) {
631 bool U = ISD::isUnsignedIntSetCC(CC);
632 unsigned Imm;
633 if (isIntImmediate(RHS, Imm) &&
634 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000635 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
636 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000637 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000638 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
639 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000640 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000641 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000642 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000643 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000644 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000645 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000646 }
647}
648
649/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
650/// to Condition.
651static unsigned getBCCForSetCC(ISD::CondCode CC) {
652 switch (CC) {
653 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000654 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000655 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000656 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000657 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000658 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000659 case ISD::SETULT:
660 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000661 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000662 case ISD::SETULE:
663 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000664 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000665 case ISD::SETUGT:
666 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000667 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000668 case ISD::SETUGE:
669 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000670
671 case ISD::SETO: return PPC::BUN;
672 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000673 }
674 return 0;
675}
676
Chris Lattner64906a02005-08-25 20:08:18 +0000677/// getCRIdxForSetCC - Return the index of the condition register field
678/// associated with the SetCC condition, and whether or not the field is
679/// treated as inverted. That is, lt = 0; ge = 0 inverted.
680static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
681 switch (CC) {
682 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000683 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000684 case ISD::SETULT:
685 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000686 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000687 case ISD::SETUGE:
688 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000689 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000690 case ISD::SETUGT:
691 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000692 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000693 case ISD::SETULE:
694 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000695 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000696 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000697 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000698 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000699 case ISD::SETO: Inv = true; return 3;
700 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000701 }
702 return 0;
703}
Chris Lattner9944b762005-08-21 22:31:09 +0000704
Nate Begeman1d9d7422005-10-18 00:28:58 +0000705SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000706 SDNode *N = Op.Val;
707 unsigned Imm;
708 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
709 if (isIntImmediate(N->getOperand(1), Imm)) {
710 // We can codegen setcc op, imm very efficiently compared to a brcond.
711 // Check for those cases here.
712 // setcc op, 0
713 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000714 SDOperand Op;
715 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000716 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000717 default: break;
718 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000719 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000720 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
721 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000722 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000723 SDOperand AD =
724 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
725 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000726 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
727 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000728 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000729 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000730 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
731 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000732 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000733 SDOperand T =
734 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
735 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000736 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
737 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000738 }
739 }
Chris Lattner222adac2005-10-06 19:03:35 +0000740 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000741 SDOperand Op;
742 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000743 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000744 default: break;
745 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000746 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
747 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000748 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000749 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
750 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000751 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000752 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000753 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
754 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
755 Op, getI32Imm(~0U));
756 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
757 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000758 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000759 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000760 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
761 getI32Imm(1)), 0);
762 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
763 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000764 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
765 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000766 }
767 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000768 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
769 getI32Imm(1), getI32Imm(31),
770 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000771 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000772 }
Chris Lattner222adac2005-10-06 19:03:35 +0000773 }
774 }
775
776 bool Inv;
777 unsigned Idx = getCRIdxForSetCC(CC, Inv);
778 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
779 SDOperand IntCR;
780
781 // Force the ccreg into CR7.
782 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
783
Chris Lattner85961d52005-12-06 20:56:18 +0000784 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000785 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
786 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000787
788 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000789 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
790 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000791 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000792 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000793
794 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000795 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
796 getI32Imm((32-(3-Idx)) & 31),
797 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000798 } else {
799 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000800 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
801 getI32Imm((32-(3-Idx)) & 31),
802 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000803 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000804 }
Chris Lattner222adac2005-10-06 19:03:35 +0000805}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000806
Nate Begeman422b0ce2005-11-16 00:48:01 +0000807/// isCallCompatibleAddress - Return true if the specified 32-bit value is
808/// representable in the immediate field of a Bx instruction.
809static bool isCallCompatibleAddress(ConstantSDNode *C) {
810 int Addr = C->getValue();
811 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
812 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
813}
814
Nate Begeman1d9d7422005-10-18 00:28:58 +0000815SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000816 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000817 SDOperand Chain;
818 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000819
820 unsigned CallOpcode;
821 std::vector<SDOperand> CallOperands;
822
823 if (GlobalAddressSDNode *GASD =
824 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000825 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000826 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000827 } else if (ExternalSymbolSDNode *ESSDN =
828 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000829 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000830 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000831 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
832 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
833 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
834 CallOpcode = PPC::BLA;
835 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000836 } else {
837 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000838 SDOperand Callee;
839 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000840 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
841 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000842
843 // Copy the callee address into R12 on darwin.
844 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
845 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000846
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000847 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000848 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000849 }
850
851 unsigned GPR_idx = 0, FPR_idx = 0;
852 static const unsigned GPR[] = {
853 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
854 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
855 };
856 static const unsigned FPR[] = {
857 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
858 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
859 };
860
861 SDOperand InFlag; // Null incoming flag value.
862
863 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
864 unsigned DestReg = 0;
865 MVT::ValueType RegTy = N->getOperand(i).getValueType();
866 if (RegTy == MVT::i32) {
867 assert(GPR_idx < 8 && "Too many int args");
868 DestReg = GPR[GPR_idx++];
869 } else {
870 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
871 "Unpromoted integer arg?");
872 assert(FPR_idx < 13 && "Too many fp args");
873 DestReg = FPR[FPR_idx++];
874 }
875
876 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000877 SDOperand Val;
878 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000879 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
880 InFlag = Chain.getValue(1);
881 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
882 }
883 }
884
885 // Finally, once everything is in registers to pass to the call, emit the
886 // call itself.
887 if (InFlag.Val)
888 CallOperands.push_back(InFlag); // Strong dep on register copies.
889 else
890 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000891 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
892 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000893
894 std::vector<SDOperand> CallResults;
895
896 // If the call has results, copy the values out of the ret val registers.
897 switch (N->getValueType(0)) {
898 default: assert(0 && "Unexpected ret value!");
899 case MVT::Other: break;
900 case MVT::i32:
901 if (N->getValueType(1) == MVT::i32) {
902 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
903 Chain.getValue(1)).getValue(1);
904 CallResults.push_back(Chain.getValue(0));
905 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
906 Chain.getValue(2)).getValue(1);
907 CallResults.push_back(Chain.getValue(0));
908 } else {
909 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
910 Chain.getValue(1)).getValue(1);
911 CallResults.push_back(Chain.getValue(0));
912 }
913 break;
914 case MVT::f32:
915 case MVT::f64:
916 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
917 Chain.getValue(1)).getValue(1);
918 CallResults.push_back(Chain.getValue(0));
919 break;
920 }
921
922 CallResults.push_back(Chain);
923 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
924 CodeGenMap[Op.getValue(i)] = CallResults[i];
925 return CallResults[Op.ResNo];
926}
927
Chris Lattnera5a91b12005-08-17 19:33:03 +0000928// Select - Convert the specified operand from a target-independent to a
929// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000930void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000931 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000932 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000933 N->getOpcode() < PPCISD::FIRST_NUMBER) {
934 Result = Op;
935 return; // Already selected.
936 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000937
938 // If this has already been converted, use it.
939 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000940 if (CGMI != CodeGenMap.end()) {
941 Result = CGMI->second;
942 return;
943 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000944
945 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000946 default: break;
Chris Lattnere376e002006-03-20 17:54:43 +0000947 case ISD::VECTOR_SHUFFLE:
948 // FIXME: This should be autogenerated from the .td file, it is here for now
949 // due to bugs in tblgen.
950 if (Op.getOperand(1).getOpcode() == ISD::UNDEF &&
951 (Op.getValueType() == MVT::v4f32 || Op.getValueType() == MVT::v4i32) &&
952 PPC::isSplatShuffleMask(Op.getOperand(2).Val)) {
953 SDOperand N0;
954 Select(N0, N->getOperand(0));
955 Result = CodeGenMap[Op] =
956 SDOperand(CurDAG->getTargetNode(PPC::VSPLTW, MVT::v4f32,
957 getI32Imm(PPC::getVSPLTImmediate(Op.getOperand(2).Val)),
958 N0), 0);
959 return;
960 }
961 assert(0 && "ILLEGAL VECTOR_SHUFFLE!");
962
Evan Cheng34167212006-02-09 00:37:58 +0000963 case ISD::SETCC:
964 Result = SelectSETCC(Op);
965 return;
966 case PPCISD::CALL:
967 Result = SelectCALL(Op);
968 return;
969 case PPCISD::GlobalBaseReg:
970 Result = getGlobalBaseReg();
971 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000972
Chris Lattnere28e40a2005-08-25 00:45:43 +0000973 case ISD::FrameIndex: {
974 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000975 if (N->hasOneUse()) {
976 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
977 CurDAG->getTargetFrameIndex(FI, MVT::i32),
978 getI32Imm(0));
979 return;
980 }
981 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000982 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
983 CurDAG->getTargetFrameIndex(FI, MVT::i32),
984 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000985 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000986 }
Chris Lattner88add102005-09-28 22:50:24 +0000987 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000988 // FIXME: since this depends on the setting of the carry flag from the srawi
989 // we should really be making notes about that for the scheduler.
990 // FIXME: It sure would be nice if we could cheaply recognize the
991 // srl/add/sra pattern the dag combiner will generate for this as
992 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000993 unsigned Imm;
994 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000995 SDOperand N0;
996 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000997 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000998 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000999 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001000 N0, getI32Imm(Log2_32(Imm)));
1001 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001002 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001003 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001004 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001005 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001006 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001007 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001008 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1009 SDOperand(Op, 0), SDOperand(Op, 1)),
1010 0);
Evan Cheng34167212006-02-09 00:37:58 +00001011 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001012 }
Evan Cheng34167212006-02-09 00:37:58 +00001013 return;
Chris Lattner8784a232005-08-25 17:50:06 +00001014 }
Chris Lattner047b9522005-08-25 22:04:30 +00001015
Chris Lattner237733e2005-09-29 23:33:31 +00001016 // Other cases are autogenerated.
1017 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001018 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001019 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +00001020 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +00001021 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1022 // with a mask, emit rlwinm
1023 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
1024 isShiftedMask_32(~Imm))) {
1025 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001026 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001027 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001028 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +00001029 } else if (Imm == 0) {
1030 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +00001031 Select(Result, N->getOperand(1));
1032 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001033 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001034 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001035 isRunOfOnes(Imm, MB, ME);
1036 SH = 0;
1037 }
Evan Cheng34167212006-02-09 00:37:58 +00001038 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1039 getI32Imm(SH), getI32Imm(MB),
1040 getI32Imm(ME));
1041 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001042 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001043 // ISD::OR doesn't get all the bitfield insertion fun.
1044 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1045 if (isIntImmediate(N->getOperand(1), Imm) &&
1046 N->getOperand(0).getOpcode() == ISD::OR &&
1047 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001048 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001049 Imm = ~(Imm^Imm2);
1050 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001051 SDOperand Tmp1, Tmp2;
1052 Select(Tmp1, N->getOperand(0).getOperand(0));
1053 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001054 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1055 Tmp1, Tmp2,
1056 getI32Imm(0), getI32Imm(MB),
1057 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001058 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001059 }
1060 }
Chris Lattner237733e2005-09-29 23:33:31 +00001061
1062 // Other cases are autogenerated.
1063 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001064 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001065 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001066 if (SDNode *I = SelectBitfieldInsert(N)) {
1067 Result = CodeGenMap[Op] = SDOperand(I, 0);
1068 return;
1069 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001070
Chris Lattner237733e2005-09-29 23:33:31 +00001071 // Other cases are autogenerated.
1072 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001073 case ISD::SHL: {
1074 unsigned Imm, SH, MB, ME;
1075 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001076 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001077 SDOperand Val;
1078 Select(Val, N->getOperand(0).getOperand(0));
1079 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1080 Val, getI32Imm(SH), getI32Imm(MB),
1081 getI32Imm(ME));
1082 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001083 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001084
1085 // Other cases are autogenerated.
1086 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001087 }
1088 case ISD::SRL: {
1089 unsigned Imm, SH, MB, ME;
1090 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001091 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001092 SDOperand Val;
1093 Select(Val, N->getOperand(0).getOperand(0));
1094 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1095 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
1096 getI32Imm(ME));
1097 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001098 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001099
1100 // Other cases are autogenerated.
1101 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001102 }
Chris Lattner13794f52005-08-26 18:46:49 +00001103 case ISD::SELECT_CC: {
1104 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1105
1106 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1107 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1108 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1109 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1110 if (N1C->isNullValue() && N3C->isNullValue() &&
1111 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001112 SDOperand LHS;
1113 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001114 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001115 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1116 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001117 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1118 SDOperand(Tmp, 0), LHS,
1119 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001120 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001121 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001122
Chris Lattner50ff55c2005-09-01 19:20:44 +00001123 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001124 unsigned BROpc = getBCCForSetCC(CC);
1125
1126 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001127 unsigned SelectCCOp;
1128 if (MVT::isInteger(N->getValueType(0)))
1129 SelectCCOp = PPC::SELECT_CC_Int;
1130 else if (N->getValueType(0) == MVT::f32)
1131 SelectCCOp = PPC::SELECT_CC_F4;
1132 else
1133 SelectCCOp = PPC::SELECT_CC_F8;
Evan Cheng34167212006-02-09 00:37:58 +00001134 SDOperand N2, N3;
1135 Select(N2, N->getOperand(2));
1136 Select(N3, N->getOperand(3));
1137 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1138 N2, N3, getI32Imm(BROpc));
1139 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001140 }
Nate Begeman81e80972006-03-17 01:40:33 +00001141 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001142 SDOperand Chain;
1143 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001144 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1145 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001146 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1147 CondCode, getI32Imm(getBCCForSetCC(CC)),
1148 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001149 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001150 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001151 }
Chris Lattner25dae722005-09-03 00:53:47 +00001152
Evan Cheng34167212006-02-09 00:37:58 +00001153 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001154}
1155
1156
Nate Begeman1d9d7422005-10-18 00:28:58 +00001157/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001158/// PowerPC-specific DAG, ready for instruction scheduling.
1159///
Evan Chengc4c62572006-03-13 23:20:37 +00001160FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001161 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001162}
1163