blob: 459ddfaeb8e02d3410a40e52cf48adbb62442651 [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 Lattnere5ba5802006-03-22 05:26:03 +000092 /// SelectAddrImmShift - Returns true if the address N can be represented by
93 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
94 /// for use by STD and friends.
95 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
96
Chris Lattnere5d88612006-02-24 02:13:12 +000097 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
98 /// inline asm expressions.
99 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
100 char ConstraintCode,
101 std::vector<SDOperand> &OutOps,
102 SelectionDAG &DAG) {
103 SDOperand Op0, Op1;
104 switch (ConstraintCode) {
105 default: return true;
106 case 'm': // memory
107 if (!SelectAddrIdx(Op, Op0, Op1))
108 SelectAddrImm(Op, Op0, Op1);
109 break;
110 case 'o': // offsetable
111 if (!SelectAddrImm(Op, Op0, Op1)) {
112 Select(Op0, Op); // r+0.
113 Op1 = getI32Imm(0);
114 }
115 break;
116 case 'v': // not offsetable
117 SelectAddrIdxOnly(Op, Op0, Op1);
118 break;
119 }
120
121 OutOps.push_back(Op0);
122 OutOps.push_back(Op1);
123 return false;
124 }
125
Chris Lattner047b9522005-08-25 22:04:30 +0000126 SDOperand BuildSDIVSequence(SDNode *N);
127 SDOperand BuildUDIVSequence(SDNode *N);
128
Chris Lattnera5a91b12005-08-17 19:33:03 +0000129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
Chris Lattner4bb18952006-03-16 18:25:23 +0000133 void InsertVRSaveCode(Function &Fn);
134
Chris Lattnera5a91b12005-08-17 19:33:03 +0000135 virtual const char *getPassName() const {
136 return "PowerPC DAG->DAG Pattern Instruction Selection";
137 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000138
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000139 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for this
Chris Lattnerc6644182006-03-07 06:32:48 +0000140 /// target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000141 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000142 // Should use subtarget info to pick the right hazard recognizer. For
143 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000144 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
145 assert(II && "No InstrInfo?");
146 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000147 }
Chris Lattneraf165382005-09-13 22:03:06 +0000148
149// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000150#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000151
152private:
Chris Lattner222adac2005-10-06 19:03:35 +0000153 SDOperand SelectSETCC(SDOperand Op);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000154 SDOperand SelectCALL(SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000155 };
156}
157
Chris Lattnerbd937b92005-10-06 18:45:51 +0000158/// InstructionSelectBasicBlock - This callback is invoked by
159/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000160void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000161 DEBUG(BB->dump());
162
163 // The selection process is inherently a bottom-up recursive process (users
164 // select their uses before themselves). Given infinite stack space, we
165 // could just start selecting on the root and traverse the whole graph. In
166 // practice however, this causes us to run out of stack space on large basic
167 // blocks. To avoid this problem, select the entry node, then all its uses,
168 // iteratively instead of recursively.
169 std::vector<SDOperand> Worklist;
170 Worklist.push_back(DAG.getEntryNode());
171
172 // Note that we can do this in the PPC target (scanning forward across token
173 // chain edges) because no nodes ever get folded across these edges. On a
174 // target like X86 which supports load/modify/store operations, this would
175 // have to be more careful.
176 while (!Worklist.empty()) {
177 SDOperand Node = Worklist.back();
178 Worklist.pop_back();
179
Chris Lattnercf01a702005-10-07 22:10:27 +0000180 // Chose from the least deep of the top two nodes.
181 if (!Worklist.empty() &&
182 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
183 std::swap(Worklist.back(), Node);
184
Chris Lattnerbd937b92005-10-06 18:45:51 +0000185 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
186 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
187 CodeGenMap.count(Node)) continue;
188
189 for (SDNode::use_iterator UI = Node.Val->use_begin(),
190 E = Node.Val->use_end(); UI != E; ++UI) {
191 // Scan the values. If this use has a value that is a token chain, add it
192 // to the worklist.
193 SDNode *User = *UI;
194 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
195 if (User->getValueType(i) == MVT::Other) {
196 Worklist.push_back(SDOperand(User, i));
197 break;
198 }
199 }
200
201 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000202 SDOperand Dummy;
203 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000204 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000205
Chris Lattnerbd937b92005-10-06 18:45:51 +0000206 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000207 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattnerbd937b92005-10-06 18:45:51 +0000208 CodeGenMap.clear();
209 DAG.RemoveDeadNodes();
210
Chris Lattner1877ec92006-03-13 21:52:10 +0000211 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000212 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000213}
214
215/// InsertVRSaveCode - Once the entire function has been instruction selected,
216/// all virtual registers are created and all machine instructions are built,
217/// check to see if we need to save/restore VRSAVE. If so, do it.
218void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000219 // Check to see if this function uses vector registers, which means we have to
220 // save and restore the VRSAVE register and update it with the regs we use.
221 //
222 // In this case, there will be virtual registers of vector type type created
223 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000224 MachineFunction &Fn = MachineFunction::get(&F);
225 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000226 bool HasVectorVReg = false;
227 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000228 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000229 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
230 HasVectorVReg = true;
231 break;
232 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000233 if (!HasVectorVReg) return; // nothing to do.
234
Chris Lattner1877ec92006-03-13 21:52:10 +0000235 // If we have a vector register, we want to emit code into the entry and exit
236 // blocks to save and restore the VRSAVE register. We do this here (instead
237 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
238 //
239 // 1. This (trivially) reduces the load on the register allocator, by not
240 // having to represent the live range of the VRSAVE register.
241 // 2. This (more significantly) allows us to create a temporary virtual
242 // register to hold the saved VRSAVE value, allowing this temporary to be
243 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000244
245 // Create two vregs - one to hold the VRSAVE register that is live-in to the
246 // function and one for the value after having bits or'd into it.
247 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
248 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
249
250 MachineBasicBlock &EntryBB = *Fn.begin();
251 // Emit the following code into the entry block:
252 // InVRSAVE = MFVRSAVE
253 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
254 // MTVRSAVE UpdatedVRSAVE
255 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
256 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
257 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
258 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
259
260 // Find all return blocks, outputting a restore in each epilog.
261 const TargetInstrInfo &TII = *TM.getInstrInfo();
262 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
263 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
264 IP = BB->end(); --IP;
265
266 // Skip over all terminator instructions, which are part of the return
267 // sequence.
268 MachineBasicBlock::iterator I2 = IP;
269 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
270 IP = I2;
271
272 // Emit: MTVRSAVE InVRSave
273 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
274 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000275 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000276}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000277
Chris Lattner4bb18952006-03-16 18:25:23 +0000278
Chris Lattner4416f1a2005-08-19 22:38:53 +0000279/// getGlobalBaseReg - Output the instructions required to put the
280/// base address to use for accessing globals into a register.
281///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000282SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000283 if (!GlobalBaseReg) {
284 // Insert the set of GlobalBaseReg into the first MBB of the function
285 MachineBasicBlock &FirstMBB = BB->getParent()->front();
286 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
287 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000288 // FIXME: when we get to LP64, we will need to create the appropriate
289 // type of register here.
290 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000291 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
292 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
293 }
Chris Lattner9944b762005-08-21 22:31:09 +0000294 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000295}
296
297
Nate Begeman0f3257a2005-08-18 05:00:13 +0000298// isIntImmediate - This method tests to see if a constant operand.
299// If so Imm will receive the 32 bit value.
300static bool isIntImmediate(SDNode *N, unsigned& Imm) {
301 if (N->getOpcode() == ISD::Constant) {
302 Imm = cast<ConstantSDNode>(N)->getValue();
303 return true;
304 }
305 return false;
306}
307
Nate Begemancffc32b2005-08-18 07:30:46 +0000308// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
309// any number of 0s on either side. The 1s are allowed to wrap from LSB to
310// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
311// not, since all 1s are not contiguous.
312static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
313 if (isShiftedMask_32(Val)) {
314 // look for the first non-zero bit
315 MB = CountLeadingZeros_32(Val);
316 // look for the first zero bit after the run of ones
317 ME = CountLeadingZeros_32((Val - 1) ^ Val);
318 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000319 } else {
320 Val = ~Val; // invert mask
321 if (isShiftedMask_32(Val)) {
322 // effectively look for the first zero bit
323 ME = CountLeadingZeros_32(Val) - 1;
324 // effectively look for the first one bit after the run of zeros
325 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
326 return true;
327 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000328 }
329 // no run present
330 return false;
331}
332
Chris Lattner65a419a2005-10-09 05:36:17 +0000333// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000334// and mask opcode and mask operation.
335static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
336 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000337 // Don't even go down this path for i64, since different logic will be
338 // necessary for rldicl/rldicr/rldimi.
339 if (N->getValueType(0) != MVT::i32)
340 return false;
341
Nate Begemancffc32b2005-08-18 07:30:46 +0000342 unsigned Shift = 32;
343 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
344 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000345 if (N->getNumOperands() != 2 ||
346 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000347 return false;
348
349 if (Opcode == ISD::SHL) {
350 // apply shift left 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);
Chris Lattner651dea72005-10-15 21:40:12 +0000354 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000355 // apply shift right to mask if it comes first
356 if (IsShiftMask) Mask = Mask >> Shift;
357 // determine which bits are made indeterminant by shift
358 Indeterminant = ~(0xFFFFFFFFu >> Shift);
359 // adjust for the left rotate
360 Shift = 32 - Shift;
361 } else {
362 return false;
363 }
364
365 // if the mask doesn't intersect any Indeterminant bits
366 if (Mask && !(Mask & Indeterminant)) {
367 SH = Shift;
368 // make sure the mask is still a mask (wrap arounds may not be)
369 return isRunOfOnes(Mask, MB, ME);
370 }
371 return false;
372}
373
Nate Begeman0f3257a2005-08-18 05:00:13 +0000374// isOpcWithIntImmediate - This method tests to see if the node is a specific
375// opcode and that it has a immediate integer right operand.
376// If so Imm will receive the 32 bit value.
377static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
378 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
379}
380
Chris Lattnera5a91b12005-08-17 19:33:03 +0000381// isIntImmediate - This method tests to see if a constant operand.
382// If so Imm will receive the 32 bit value.
383static bool isIntImmediate(SDOperand N, unsigned& Imm) {
384 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
385 Imm = (unsigned)CN->getSignExtended();
386 return true;
387 }
388 return false;
389}
390
Nate Begeman02b88a42005-08-19 00:38:14 +0000391/// SelectBitfieldInsert - turn an or of two masked values into
392/// the rotate left word immediate then mask insert (rlwimi) instruction.
393/// Returns true on success, false if the caller still needs to select OR.
394///
395/// Patterns matched:
396/// 1. or shl, and 5. or and, and
397/// 2. or and, shl 6. or shl, shr
398/// 3. or shr, and 7. or shr, shl
399/// 4. or and, shr
Nate Begeman1d9d7422005-10-18 00:28:58 +0000400SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000401 bool IsRotate = false;
402 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0;
403 unsigned Value;
404
405 SDOperand Op0 = N->getOperand(0);
406 SDOperand Op1 = N->getOperand(1);
407
408 unsigned Op0Opc = Op0.getOpcode();
409 unsigned Op1Opc = Op1.getOpcode();
410
411 // Verify that we have the correct opcodes
412 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
413 return false;
414 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
415 return false;
416
417 // Generate Mask value for Target
418 if (isIntImmediate(Op0.getOperand(1), Value)) {
419 switch(Op0Opc) {
Chris Lattner13687212005-08-30 18:37:48 +0000420 case ISD::SHL: TgtMask <<= Value; break;
421 case ISD::SRL: TgtMask >>= Value; break;
422 case ISD::AND: TgtMask &= Value; break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000423 }
424 } else {
425 return 0;
426 }
427
428 // Generate Mask value for Insert
Chris Lattner13687212005-08-30 18:37:48 +0000429 if (!isIntImmediate(Op1.getOperand(1), Value))
Nate Begeman02b88a42005-08-19 00:38:14 +0000430 return 0;
Chris Lattner13687212005-08-30 18:37:48 +0000431
432 switch(Op1Opc) {
433 case ISD::SHL:
434 SH = Value;
435 InsMask <<= SH;
436 if (Op0Opc == ISD::SRL) IsRotate = true;
437 break;
438 case ISD::SRL:
439 SH = Value;
440 InsMask >>= SH;
441 SH = 32-SH;
442 if (Op0Opc == ISD::SHL) IsRotate = true;
443 break;
444 case ISD::AND:
445 InsMask &= Value;
446 break;
Nate Begeman02b88a42005-08-19 00:38:14 +0000447 }
448
449 // If both of the inputs are ANDs and one of them has a logical shift by
450 // constant as its input, make that AND the inserted value so that we can
451 // combine the shift into the rotate part of the rlwimi instruction
452 bool IsAndWithShiftOp = false;
453 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
454 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
455 Op1.getOperand(0).getOpcode() == ISD::SRL) {
456 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
457 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
458 IsAndWithShiftOp = true;
459 }
460 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
461 Op0.getOperand(0).getOpcode() == ISD::SRL) {
462 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
463 std::swap(Op0, Op1);
464 std::swap(TgtMask, InsMask);
465 SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value;
466 IsAndWithShiftOp = true;
467 }
468 }
469 }
470
471 // Verify that the Target mask and Insert mask together form a full word mask
472 // and that the Insert mask is a run of set bits (which implies both are runs
473 // of set bits). Given that, Select the arguments and generate the rlwimi
474 // instruction.
475 unsigned MB, ME;
476 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
477 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
478 bool Op0IsAND = Op0Opc == ISD::AND;
479 // Check for rotlwi / rotrwi here, a special case of bitfield insert
480 // where both bitfield halves are sourced from the same value.
481 if (IsRotate && fullMask &&
482 N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000483 SDOperand Tmp;
484 Select(Tmp, N->getOperand(0).getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000485 return CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Tmp,
486 getI32Imm(SH), getI32Imm(0), getI32Imm(31));
Nate Begeman02b88a42005-08-19 00:38:14 +0000487 }
Evan Cheng34167212006-02-09 00:37:58 +0000488 SDOperand Tmp1, Tmp2;
489 Select(Tmp1, ((Op0IsAND && fullMask) ? Op0.getOperand(0) : Op0));
490 Select(Tmp2, (IsAndWithShiftOp ? Op1.getOperand(0).getOperand(0)
491 : Op1.getOperand(0)));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000492 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
493 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000494 }
495 return 0;
496}
497
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000498/// SelectAddrImm - Returns true if the address N can be represented by
499/// a base register plus a signed 16-bit displacement [r+imm].
500bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
501 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000502 // If this can be more profitably realized as r+r, fail.
503 if (SelectAddrIdx(N, Disp, Base))
504 return false;
505
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000506 if (N.getOpcode() == ISD::ADD) {
507 unsigned imm = 0;
508 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000509 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000510 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
511 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000512 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000513 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000514 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000515 return true; // [r+i]
516 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000517 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000518 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000519 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000520 Disp = N.getOperand(1).getOperand(0); // The global address.
521 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
522 Disp.getOpcode() == ISD::TargetConstantPool);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000523 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000524 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000525 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000526 } else if (N.getOpcode() == ISD::OR) {
527 unsigned imm = 0;
528 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
529 // If this is an or of disjoint bitfields, we can codegen this as an add
530 // (for better address arithmetic) if the LHS and RHS of the OR are
531 // provably disjoint.
532 uint64_t LHSKnownZero, LHSKnownOne;
533 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
534 LHSKnownZero, LHSKnownOne);
535 if ((LHSKnownZero|~imm) == ~0U) {
536 // If all of the bits are known zero on the LHS or RHS, the add won't
537 // carry.
538 Base = N.getOperand(0);
539 Disp = getI32Imm(imm & 0xFFFF);
540 return true;
541 }
542 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000543 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
544 // Loading from a constant address.
545 int Addr = (int)CN->getValue();
546
547 // If this address fits entirely in a 16-bit sext immediate field, codegen
548 // this as "d, 0"
549 if (Addr == (short)Addr) {
550 Disp = getI32Imm(Addr);
551 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
552 return true;
553 }
554
555 // Otherwise, break this down into an LIS + disp.
556 Disp = getI32Imm((short)Addr);
557 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
558 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000559 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000560
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000561 Disp = getI32Imm(0);
562 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
563 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000564 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000565 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000566 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000567}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000568
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000569/// SelectAddrIdx - Given the specified addressed, check to see if it can be
570/// represented as an indexed [r+r] operation. Returns false if it can
571/// be represented by [r+imm], which are preferred.
572bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
573 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000574 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000575 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000576 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
577 return false; // r+i
578 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
579 return false; // r+i
580
Evan Cheng7564e0b2006-02-05 08:45:01 +0000581 Base = N.getOperand(0);
582 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000583 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000584 } else if (N.getOpcode() == ISD::OR) {
585 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
586 return false; // r+i can fold it if we can.
587
588 // If this is an or of disjoint bitfields, we can codegen this as an add
589 // (for better address arithmetic) if the LHS and RHS of the OR are provably
590 // disjoint.
591 uint64_t LHSKnownZero, LHSKnownOne;
592 uint64_t RHSKnownZero, RHSKnownOne;
593 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
594 LHSKnownZero, LHSKnownOne);
595
596 if (LHSKnownZero) {
597 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
598 RHSKnownZero, RHSKnownOne);
599 // If all of the bits are known zero on the LHS or RHS, the add won't
600 // carry.
601 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
602 Base = N.getOperand(0);
603 Index = N.getOperand(1);
604 return true;
605 }
606 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000607 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000608
609 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000610}
611
612/// SelectAddrIdxOnly - Given the specified addressed, force it to be
613/// represented as an indexed [r+r] operation.
614bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
615 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000616 // Check to see if we can easily represent this as an [r+r] address. This
617 // will fail if it thinks that the address is more profitably represented as
618 // reg+imm, e.g. where imm = 0.
619 if (!SelectAddrIdx(N, Base, Index)) {
620 // Nope, do it the hard way.
621 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
622 Index = N;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000623 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000624 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000625}
626
Chris Lattnere5ba5802006-03-22 05:26:03 +0000627/// SelectAddrImmShift - Returns true if the address N can be represented by
628/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
629/// for use by STD and friends.
630bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
631 SDOperand &Base) {
632 // If this can be more profitably realized as r+r, fail.
633 if (SelectAddrIdx(N, Disp, Base))
634 return false;
635
636 if (N.getOpcode() == ISD::ADD) {
637 unsigned imm = 0;
638 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
639 (imm & 3) == 0) {
640 Disp = getI32Imm((imm & 0xFFFF) >> 2);
641 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
642 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
643 } else {
644 Base = N.getOperand(0);
645 }
646 return true; // [r+i]
647 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
648 // Match LOAD (ADD (X, Lo(G))).
649 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
650 && "Cannot handle constant offsets yet!");
651 Disp = N.getOperand(1).getOperand(0); // The global address.
652 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
653 Disp.getOpcode() == ISD::TargetConstantPool);
654 Base = N.getOperand(0);
655 return true; // [&g+r]
656 }
657 } else if (N.getOpcode() == ISD::OR) {
658 unsigned imm = 0;
659 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
660 (imm & 3) == 0) {
661 // If this is an or of disjoint bitfields, we can codegen this as an add
662 // (for better address arithmetic) if the LHS and RHS of the OR are
663 // provably disjoint.
664 uint64_t LHSKnownZero, LHSKnownOne;
665 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
666 LHSKnownZero, LHSKnownOne);
667 if ((LHSKnownZero|~imm) == ~0U) {
668 // If all of the bits are known zero on the LHS or RHS, the add won't
669 // carry.
670 Base = N.getOperand(0);
671 Disp = getI32Imm((imm & 0xFFFF) >> 2);
672 return true;
673 }
674 }
675 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
676 // Loading from a constant address.
677 int Addr = (int)CN->getValue();
678 if ((Addr & 3) == 0) {
679 // If this address fits entirely in a 16-bit sext immediate field, codegen
680 // this as "d, 0"
681 if (Addr == (short)Addr) {
682 Disp = getI32Imm(Addr >> 2);
683 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
684 return true;
685 }
686
687 // Otherwise, break this down into an LIS + disp.
688 Disp = getI32Imm((short)Addr >> 2);
689 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
690 return true;
691 }
692 }
693
694 Disp = getI32Imm(0);
695 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
696 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
697 else
698 Base = N;
699 return true; // [r+0]
700}
701
702
Chris Lattner2fbb4572005-08-21 18:50:37 +0000703/// SelectCC - Select a comparison of the specified values with the specified
704/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000705SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
706 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000707 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000708 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000709
710 // Use U to determine whether the SETCC immediate range is signed or not.
711 if (MVT::isInteger(LHS.getValueType())) {
712 bool U = ISD::isUnsignedIntSetCC(CC);
713 unsigned Imm;
714 if (isIntImmediate(RHS, Imm) &&
715 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000716 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
717 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000718 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000719 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
720 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000721 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000722 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000723 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000724 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000725 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000726 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000727 }
728}
729
730/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
731/// to Condition.
732static unsigned getBCCForSetCC(ISD::CondCode CC) {
733 switch (CC) {
734 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000735 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000736 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000737 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000738 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000739 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000740 case ISD::SETULT:
741 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000742 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000743 case ISD::SETULE:
744 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000745 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000746 case ISD::SETUGT:
747 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000748 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000749 case ISD::SETUGE:
750 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000751
752 case ISD::SETO: return PPC::BUN;
753 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000754 }
755 return 0;
756}
757
Chris Lattner64906a02005-08-25 20:08:18 +0000758/// getCRIdxForSetCC - Return the index of the condition register field
759/// associated with the SetCC condition, and whether or not the field is
760/// treated as inverted. That is, lt = 0; ge = 0 inverted.
761static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
762 switch (CC) {
763 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000764 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000765 case ISD::SETULT:
766 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000767 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000768 case ISD::SETUGE:
769 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000770 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000771 case ISD::SETUGT:
772 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000773 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000774 case ISD::SETULE:
775 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000776 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000777 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000778 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000779 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000780 case ISD::SETO: Inv = true; return 3;
781 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000782 }
783 return 0;
784}
Chris Lattner9944b762005-08-21 22:31:09 +0000785
Nate Begeman1d9d7422005-10-18 00:28:58 +0000786SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000787 SDNode *N = Op.Val;
788 unsigned Imm;
789 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
790 if (isIntImmediate(N->getOperand(1), Imm)) {
791 // We can codegen setcc op, imm very efficiently compared to a brcond.
792 // Check for those cases here.
793 // setcc op, 0
794 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000795 SDOperand Op;
796 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000797 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000798 default: break;
799 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000800 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000801 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
802 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000803 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000804 SDOperand AD =
805 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
806 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000807 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
808 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000809 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000810 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000811 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
812 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000813 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000814 SDOperand T =
815 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
816 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000817 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
818 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000819 }
820 }
Chris Lattner222adac2005-10-06 19:03:35 +0000821 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000822 SDOperand Op;
823 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000824 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000825 default: break;
826 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000827 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
828 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000829 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000830 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
831 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000832 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000833 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000834 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
835 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
836 Op, getI32Imm(~0U));
837 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0), Op,
838 SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000839 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000840 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000841 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
842 getI32Imm(1)), 0);
843 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
844 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000845 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
846 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000847 }
848 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000849 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
850 getI32Imm(1), getI32Imm(31),
851 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000852 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000853 }
Chris Lattner222adac2005-10-06 19:03:35 +0000854 }
855 }
856
857 bool Inv;
858 unsigned Idx = getCRIdxForSetCC(CC, Inv);
859 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
860 SDOperand IntCR;
861
862 // Force the ccreg into CR7.
863 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
864
Chris Lattner85961d52005-12-06 20:56:18 +0000865 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000866 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
867 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000868
869 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000870 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
871 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000872 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000873 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000874
875 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000876 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
877 getI32Imm((32-(3-Idx)) & 31),
878 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000879 } else {
880 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000881 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
882 getI32Imm((32-(3-Idx)) & 31),
883 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000884 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000885 }
Chris Lattner222adac2005-10-06 19:03:35 +0000886}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000887
Nate Begeman422b0ce2005-11-16 00:48:01 +0000888/// isCallCompatibleAddress - Return true if the specified 32-bit value is
889/// representable in the immediate field of a Bx instruction.
890static bool isCallCompatibleAddress(ConstantSDNode *C) {
891 int Addr = C->getValue();
892 if (Addr & 3) return false; // Low 2 bits are implicitly zero.
893 return (Addr << 6 >> 6) == Addr; // Top 6 bits have to be sext of immediate.
894}
895
Nate Begeman1d9d7422005-10-18 00:28:58 +0000896SDOperand PPCDAGToDAGISel::SelectCALL(SDOperand Op) {
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000897 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000898 SDOperand Chain;
899 Select(Chain, N->getOperand(0));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000900
901 unsigned CallOpcode;
902 std::vector<SDOperand> CallOperands;
903
904 if (GlobalAddressSDNode *GASD =
905 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000906 CallOpcode = PPC::BL;
Chris Lattner2823b3e2005-11-17 05:56:14 +0000907 CallOperands.push_back(N->getOperand(1));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000908 } else if (ExternalSymbolSDNode *ESSDN =
909 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
Nate Begeman422b0ce2005-11-16 00:48:01 +0000910 CallOpcode = PPC::BL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000911 CallOperands.push_back(N->getOperand(1));
Nate Begeman422b0ce2005-11-16 00:48:01 +0000912 } else if (isa<ConstantSDNode>(N->getOperand(1)) &&
913 isCallCompatibleAddress(cast<ConstantSDNode>(N->getOperand(1)))) {
914 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(1));
915 CallOpcode = PPC::BLA;
916 CallOperands.push_back(getI32Imm((int)C->getValue() >> 2));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000917 } else {
918 // Copy the callee address into the CTR register.
Evan Cheng34167212006-02-09 00:37:58 +0000919 SDOperand Callee;
920 Select(Callee, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000921 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee,
922 Chain), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000923
924 // Copy the callee address into R12 on darwin.
925 SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32);
926 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000927
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000928 CallOperands.push_back(R12);
Nate Begeman422b0ce2005-11-16 00:48:01 +0000929 CallOpcode = PPC::BCTRL;
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000930 }
931
932 unsigned GPR_idx = 0, FPR_idx = 0;
933 static const unsigned GPR[] = {
934 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
935 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
936 };
937 static const unsigned FPR[] = {
938 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
939 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
940 };
941
942 SDOperand InFlag; // Null incoming flag value.
943
944 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
945 unsigned DestReg = 0;
946 MVT::ValueType RegTy = N->getOperand(i).getValueType();
947 if (RegTy == MVT::i32) {
948 assert(GPR_idx < 8 && "Too many int args");
949 DestReg = GPR[GPR_idx++];
950 } else {
951 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
952 "Unpromoted integer arg?");
953 assert(FPR_idx < 13 && "Too many fp args");
954 DestReg = FPR[FPR_idx++];
955 }
956
957 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
Evan Cheng34167212006-02-09 00:37:58 +0000958 SDOperand Val;
959 Select(Val, N->getOperand(i));
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000960 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
961 InFlag = Chain.getValue(1);
962 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
963 }
964 }
965
966 // Finally, once everything is in registers to pass to the call, emit the
967 // call itself.
968 if (InFlag.Val)
969 CallOperands.push_back(InFlag); // Strong dep on register copies.
970 else
971 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000972 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
973 CallOperands), 0);
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000974
975 std::vector<SDOperand> CallResults;
976
977 // If the call has results, copy the values out of the ret val registers.
978 switch (N->getValueType(0)) {
979 default: assert(0 && "Unexpected ret value!");
980 case MVT::Other: break;
981 case MVT::i32:
982 if (N->getValueType(1) == MVT::i32) {
983 Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32,
984 Chain.getValue(1)).getValue(1);
985 CallResults.push_back(Chain.getValue(0));
986 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
987 Chain.getValue(2)).getValue(1);
988 CallResults.push_back(Chain.getValue(0));
989 } else {
990 Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32,
991 Chain.getValue(1)).getValue(1);
992 CallResults.push_back(Chain.getValue(0));
993 }
994 break;
995 case MVT::f32:
996 case MVT::f64:
997 Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0),
998 Chain.getValue(1)).getValue(1);
999 CallResults.push_back(Chain.getValue(0));
1000 break;
1001 }
1002
1003 CallResults.push_back(Chain);
1004 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
1005 CodeGenMap[Op.getValue(i)] = CallResults[i];
1006 return CallResults[Op.ResNo];
1007}
1008
Chris Lattnera5a91b12005-08-17 19:33:03 +00001009// Select - Convert the specified operand from a target-independent to a
1010// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +00001011void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +00001012 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +00001013 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +00001014 N->getOpcode() < PPCISD::FIRST_NUMBER) {
1015 Result = Op;
1016 return; // Already selected.
1017 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001018
1019 // If this has already been converted, use it.
1020 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +00001021 if (CGMI != CodeGenMap.end()) {
1022 Result = CGMI->second;
1023 return;
1024 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001025
1026 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +00001027 default: break;
Evan Cheng34167212006-02-09 00:37:58 +00001028 case ISD::SETCC:
1029 Result = SelectSETCC(Op);
1030 return;
1031 case PPCISD::CALL:
1032 Result = SelectCALL(Op);
1033 return;
1034 case PPCISD::GlobalBaseReg:
1035 Result = getGlobalBaseReg();
1036 return;
Chris Lattner860e8862005-11-17 07:30:41 +00001037
Chris Lattnere28e40a2005-08-25 00:45:43 +00001038 case ISD::FrameIndex: {
1039 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +00001040 if (N->hasOneUse()) {
1041 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
1042 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1043 getI32Imm(0));
1044 return;
1045 }
1046 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001047 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
1048 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1049 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001050 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +00001051 }
Chris Lattner88add102005-09-28 22:50:24 +00001052 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001053 // FIXME: since this depends on the setting of the carry flag from the srawi
1054 // we should really be making notes about that for the scheduler.
1055 // FIXME: It sure would be nice if we could cheaply recognize the
1056 // srl/add/sra pattern the dag combiner will generate for this as
1057 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +00001058 unsigned Imm;
1059 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +00001060 SDOperand N0;
1061 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +00001062 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001063 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +00001064 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001065 N0, getI32Imm(Log2_32(Imm)));
1066 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001067 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001068 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001069 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001070 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001071 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001072 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001073 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1074 SDOperand(Op, 0), SDOperand(Op, 1)),
1075 0);
Evan Cheng34167212006-02-09 00:37:58 +00001076 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001077 }
Evan Cheng34167212006-02-09 00:37:58 +00001078 return;
Chris Lattner8784a232005-08-25 17:50:06 +00001079 }
Chris Lattner047b9522005-08-25 22:04:30 +00001080
Chris Lattner237733e2005-09-29 23:33:31 +00001081 // Other cases are autogenerated.
1082 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001083 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001084 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +00001085 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +00001086 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1087 // with a mask, emit rlwinm
1088 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
1089 isShiftedMask_32(~Imm))) {
1090 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001091 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001092 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001093 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +00001094 } else if (Imm == 0) {
1095 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +00001096 Select(Result, N->getOperand(1));
1097 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001098 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001099 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001100 isRunOfOnes(Imm, MB, ME);
1101 SH = 0;
1102 }
Evan Cheng34167212006-02-09 00:37:58 +00001103 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1104 getI32Imm(SH), getI32Imm(MB),
1105 getI32Imm(ME));
1106 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001107 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001108 // ISD::OR doesn't get all the bitfield insertion fun.
1109 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1110 if (isIntImmediate(N->getOperand(1), Imm) &&
1111 N->getOperand(0).getOpcode() == ISD::OR &&
1112 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001113 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001114 Imm = ~(Imm^Imm2);
1115 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001116 SDOperand Tmp1, Tmp2;
1117 Select(Tmp1, N->getOperand(0).getOperand(0));
1118 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001119 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1120 Tmp1, Tmp2,
1121 getI32Imm(0), getI32Imm(MB),
1122 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001123 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001124 }
1125 }
Chris Lattner237733e2005-09-29 23:33:31 +00001126
1127 // Other cases are autogenerated.
1128 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001129 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001130 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001131 if (SDNode *I = SelectBitfieldInsert(N)) {
1132 Result = CodeGenMap[Op] = SDOperand(I, 0);
1133 return;
1134 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001135
Chris Lattner237733e2005-09-29 23:33:31 +00001136 // Other cases are autogenerated.
1137 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001138 case ISD::SHL: {
1139 unsigned Imm, SH, MB, ME;
1140 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001141 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001142 SDOperand Val;
1143 Select(Val, N->getOperand(0).getOperand(0));
1144 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1145 Val, getI32Imm(SH), getI32Imm(MB),
1146 getI32Imm(ME));
1147 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001148 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001149
1150 // Other cases are autogenerated.
1151 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001152 }
1153 case ISD::SRL: {
1154 unsigned Imm, SH, MB, ME;
1155 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001156 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001157 SDOperand Val;
1158 Select(Val, N->getOperand(0).getOperand(0));
1159 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1160 Val, getI32Imm(SH & 0x1F), getI32Imm(MB),
1161 getI32Imm(ME));
1162 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001163 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001164
1165 // Other cases are autogenerated.
1166 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001167 }
Chris Lattner13794f52005-08-26 18:46:49 +00001168 case ISD::SELECT_CC: {
1169 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1170
1171 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1172 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1173 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1174 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1175 if (N1C->isNullValue() && N3C->isNullValue() &&
1176 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001177 SDOperand LHS;
1178 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001179 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001180 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1181 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001182 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1183 SDOperand(Tmp, 0), LHS,
1184 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001185 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001186 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001187
Chris Lattner50ff55c2005-09-01 19:20:44 +00001188 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001189 unsigned BROpc = getBCCForSetCC(CC);
1190
1191 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001192 unsigned SelectCCOp;
1193 if (MVT::isInteger(N->getValueType(0)))
1194 SelectCCOp = PPC::SELECT_CC_Int;
1195 else if (N->getValueType(0) == MVT::f32)
1196 SelectCCOp = PPC::SELECT_CC_F4;
1197 else
1198 SelectCCOp = PPC::SELECT_CC_F8;
Evan Cheng34167212006-02-09 00:37:58 +00001199 SDOperand N2, N3;
1200 Select(N2, N->getOperand(2));
1201 Select(N3, N->getOperand(3));
1202 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1203 N2, N3, getI32Imm(BROpc));
1204 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001205 }
Nate Begeman81e80972006-03-17 01:40:33 +00001206 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001207 SDOperand Chain;
1208 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001209 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1210 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001211 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1212 CondCode, getI32Imm(getBCCForSetCC(CC)),
1213 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001214 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001215 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001216 }
Chris Lattner25dae722005-09-03 00:53:47 +00001217
Evan Cheng34167212006-02-09 00:37:58 +00001218 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001219}
1220
1221
Nate Begeman1d9d7422005-10-18 00:28:58 +00001222/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001223/// PowerPC-specific DAG, ready for instruction scheduling.
1224///
Evan Chengc4c62572006-03-13 23:20:37 +00001225FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001226 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001227}
1228