blob: e26b87591c45334a22809450a3ecab943577db78 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattnera5a91b12005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattnera5a91b12005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000016#include "PPCTargetMachine.h"
17#include "PPCISelLowering.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "PPCHazardRecognizers.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000026#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000027#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000028#include "llvm/Intrinsics.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000031#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000032#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000033using namespace llvm;
34
35namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000036 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
37
38 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000039 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000040 /// instructions for SelectionDAG operations.
41 ///
Nate Begeman1d9d7422005-10-18 00:28:58 +000042 class PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000043 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000044 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000045 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000046 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000047 PPCDAGToDAGISel(PPCTargetMachine &tm)
48 : SelectionDAGISel(PPCLowering), TM(tm),
49 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000050
Chris Lattner4416f1a2005-08-19 22:38:53 +000051 virtual bool runOnFunction(Function &Fn) {
52 // Make sure we re-emit a set of the global base reg if necessary
53 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000054 SelectionDAGISel::runOnFunction(Fn);
55
56 InsertVRSaveCode(Fn);
57 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000058 }
59
Chris Lattnera5a91b12005-08-17 19:33:03 +000060 /// getI32Imm - Return a target constant with the specified value, of type
61 /// i32.
62 inline SDOperand getI32Imm(unsigned Imm) {
63 return CurDAG->getTargetConstant(Imm, MVT::i32);
64 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000065
66 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
67 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000068 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000069
70 // Select - Convert the specified operand from a target-independent to a
71 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000072 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000073
Nate Begeman02b88a42005-08-19 00:38:14 +000074 SDNode *SelectBitfieldInsert(SDNode *N);
75
Chris Lattner2fbb4572005-08-21 18:50:37 +000076 /// SelectCC - Select a comparison of the specified values with the
77 /// specified condition code, returning the CR# of the expression.
78 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
79
Nate Begeman7fd1edd2005-12-19 23:25:09 +000080 /// SelectAddrImm - Returns true if the address N can be represented by
81 /// a base register plus a signed 16-bit displacement [r+imm].
82 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
83
84 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
85 /// represented as an indexed [r+r] operation. Returns false if it can
86 /// be represented by [r+imm], which are preferred.
87 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +000088
Nate Begeman7fd1edd2005-12-19 23:25:09 +000089 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
90 /// represented as an indexed [r+r] operation.
91 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +000092
Chris Lattnere5ba5802006-03-22 05:26:03 +000093 /// SelectAddrImmShift - Returns true if the address N can be represented by
94 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
95 /// for use by STD and friends.
96 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
97
Chris Lattnere5d88612006-02-24 02:13:12 +000098 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
99 /// inline asm expressions.
100 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
101 char ConstraintCode,
102 std::vector<SDOperand> &OutOps,
103 SelectionDAG &DAG) {
104 SDOperand Op0, Op1;
105 switch (ConstraintCode) {
106 default: return true;
107 case 'm': // memory
108 if (!SelectAddrIdx(Op, Op0, Op1))
109 SelectAddrImm(Op, Op0, Op1);
110 break;
111 case 'o': // offsetable
112 if (!SelectAddrImm(Op, Op0, Op1)) {
113 Select(Op0, Op); // r+0.
114 Op1 = getI32Imm(0);
115 }
116 break;
117 case 'v': // not offsetable
118 SelectAddrIdxOnly(Op, Op0, Op1);
119 break;
120 }
121
122 OutOps.push_back(Op0);
123 OutOps.push_back(Op1);
124 return false;
125 }
126
Chris Lattner047b9522005-08-25 22:04:30 +0000127 SDOperand BuildSDIVSequence(SDNode *N);
128 SDOperand BuildUDIVSequence(SDNode *N);
129
Chris Lattnera5a91b12005-08-17 19:33:03 +0000130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133
Chris Lattner4bb18952006-03-16 18:25:23 +0000134 void InsertVRSaveCode(Function &Fn);
135
Chris Lattnera5a91b12005-08-17 19:33:03 +0000136 virtual const char *getPassName() const {
137 return "PowerPC DAG->DAG Pattern Instruction Selection";
138 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000139
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000140 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
141 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000142 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000143 // Should use subtarget info to pick the right hazard recognizer. For
144 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000145 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
146 assert(II && "No InstrInfo?");
147 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000148 }
Chris Lattneraf165382005-09-13 22:03:06 +0000149
150// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000151#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000152
153private:
Chris Lattner222adac2005-10-06 19:03:35 +0000154 SDOperand SelectSETCC(SDOperand Op);
Chris Lattnercf006312006-06-10 01:15:02 +0000155 void MySelect_PPCbctrl(SDOperand &Result, SDOperand N);
156 void MySelect_PPCcall(SDOperand &Result, SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000157 };
158}
159
Chris Lattnerbd937b92005-10-06 18:45:51 +0000160/// InstructionSelectBasicBlock - This callback is invoked by
161/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000162void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000163 DEBUG(BB->dump());
164
165 // The selection process is inherently a bottom-up recursive process (users
166 // select their uses before themselves). Given infinite stack space, we
167 // could just start selecting on the root and traverse the whole graph. In
168 // practice however, this causes us to run out of stack space on large basic
169 // blocks. To avoid this problem, select the entry node, then all its uses,
170 // iteratively instead of recursively.
171 std::vector<SDOperand> Worklist;
172 Worklist.push_back(DAG.getEntryNode());
173
174 // Note that we can do this in the PPC target (scanning forward across token
175 // chain edges) because no nodes ever get folded across these edges. On a
176 // target like X86 which supports load/modify/store operations, this would
177 // have to be more careful.
178 while (!Worklist.empty()) {
179 SDOperand Node = Worklist.back();
180 Worklist.pop_back();
181
Chris Lattnercf01a702005-10-07 22:10:27 +0000182 // Chose from the least deep of the top two nodes.
183 if (!Worklist.empty() &&
184 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
185 std::swap(Worklist.back(), Node);
186
Chris Lattnerbd937b92005-10-06 18:45:51 +0000187 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
188 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
189 CodeGenMap.count(Node)) continue;
190
191 for (SDNode::use_iterator UI = Node.Val->use_begin(),
192 E = Node.Val->use_end(); UI != E; ++UI) {
193 // Scan the values. If this use has a value that is a token chain, add it
194 // to the worklist.
195 SDNode *User = *UI;
196 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
197 if (User->getValueType(i) == MVT::Other) {
198 Worklist.push_back(SDOperand(User, i));
199 break;
200 }
201 }
202
203 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000204 SDOperand Dummy;
205 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000206 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000207
Chris Lattnerbd937b92005-10-06 18:45:51 +0000208 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000209 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000210 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Chris Lattnerbd937b92005-10-06 18:45:51 +0000211 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000212 HandleMap.clear();
213 ReplaceMap.clear();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000214 DAG.RemoveDeadNodes();
215
Chris Lattner1877ec92006-03-13 21:52:10 +0000216 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000217 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000218}
219
220/// InsertVRSaveCode - Once the entire function has been instruction selected,
221/// all virtual registers are created and all machine instructions are built,
222/// check to see if we need to save/restore VRSAVE. If so, do it.
223void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000224 // Check to see if this function uses vector registers, which means we have to
225 // save and restore the VRSAVE register and update it with the regs we use.
226 //
227 // In this case, there will be virtual registers of vector type type created
228 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000229 MachineFunction &Fn = MachineFunction::get(&F);
230 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000231 bool HasVectorVReg = false;
232 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000233 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000234 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
235 HasVectorVReg = true;
236 break;
237 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000238 if (!HasVectorVReg) return; // nothing to do.
239
Chris Lattner1877ec92006-03-13 21:52:10 +0000240 // If we have a vector register, we want to emit code into the entry and exit
241 // blocks to save and restore the VRSAVE register. We do this here (instead
242 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
243 //
244 // 1. This (trivially) reduces the load on the register allocator, by not
245 // having to represent the live range of the VRSAVE register.
246 // 2. This (more significantly) allows us to create a temporary virtual
247 // register to hold the saved VRSAVE value, allowing this temporary to be
248 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000249
250 // Create two vregs - one to hold the VRSAVE register that is live-in to the
251 // function and one for the value after having bits or'd into it.
252 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
253 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
254
255 MachineBasicBlock &EntryBB = *Fn.begin();
256 // Emit the following code into the entry block:
257 // InVRSAVE = MFVRSAVE
258 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
259 // MTVRSAVE UpdatedVRSAVE
260 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
261 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
262 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
263 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
264
265 // Find all return blocks, outputting a restore in each epilog.
266 const TargetInstrInfo &TII = *TM.getInstrInfo();
267 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
268 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
269 IP = BB->end(); --IP;
270
271 // Skip over all terminator instructions, which are part of the return
272 // sequence.
273 MachineBasicBlock::iterator I2 = IP;
274 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
275 IP = I2;
276
277 // Emit: MTVRSAVE InVRSave
278 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
279 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000280 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000281}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000282
Chris Lattner4bb18952006-03-16 18:25:23 +0000283
Chris Lattner4416f1a2005-08-19 22:38:53 +0000284/// getGlobalBaseReg - Output the instructions required to put the
285/// base address to use for accessing globals into a register.
286///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000287SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000288 if (!GlobalBaseReg) {
289 // Insert the set of GlobalBaseReg into the first MBB of the function
290 MachineBasicBlock &FirstMBB = BB->getParent()->front();
291 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
292 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Nate Begeman1d9d7422005-10-18 00:28:58 +0000293 // FIXME: when we get to LP64, we will need to create the appropriate
294 // type of register here.
295 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000296 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
297 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
298 }
Chris Lattner9944b762005-08-21 22:31:09 +0000299 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000300}
301
302
Nate Begeman0f3257a2005-08-18 05:00:13 +0000303// isIntImmediate - This method tests to see if a constant operand.
304// If so Imm will receive the 32 bit value.
305static bool isIntImmediate(SDNode *N, unsigned& Imm) {
306 if (N->getOpcode() == ISD::Constant) {
307 Imm = cast<ConstantSDNode>(N)->getValue();
308 return true;
309 }
310 return false;
311}
312
Nate Begemancffc32b2005-08-18 07:30:46 +0000313// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
314// any number of 0s on either side. The 1s are allowed to wrap from LSB to
315// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
316// not, since all 1s are not contiguous.
317static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
318 if (isShiftedMask_32(Val)) {
319 // look for the first non-zero bit
320 MB = CountLeadingZeros_32(Val);
321 // look for the first zero bit after the run of ones
322 ME = CountLeadingZeros_32((Val - 1) ^ Val);
323 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000324 } else {
325 Val = ~Val; // invert mask
326 if (isShiftedMask_32(Val)) {
327 // effectively look for the first zero bit
328 ME = CountLeadingZeros_32(Val) - 1;
329 // effectively look for the first one bit after the run of zeros
330 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
331 return true;
332 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000333 }
334 // no run present
335 return false;
336}
337
Chris Lattner65a419a2005-10-09 05:36:17 +0000338// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000339// and mask opcode and mask operation.
340static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
341 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000342 // Don't even go down this path for i64, since different logic will be
343 // necessary for rldicl/rldicr/rldimi.
344 if (N->getValueType(0) != MVT::i32)
345 return false;
346
Nate Begemancffc32b2005-08-18 07:30:46 +0000347 unsigned Shift = 32;
348 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
349 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000350 if (N->getNumOperands() != 2 ||
351 !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000352 return false;
353
354 if (Opcode == ISD::SHL) {
355 // apply shift left 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);
Chris Lattner651dea72005-10-15 21:40:12 +0000359 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000360 // apply shift right to mask if it comes first
361 if (IsShiftMask) Mask = Mask >> Shift;
362 // determine which bits are made indeterminant by shift
363 Indeterminant = ~(0xFFFFFFFFu >> Shift);
364 // adjust for the left rotate
365 Shift = 32 - Shift;
366 } else {
367 return false;
368 }
369
370 // if the mask doesn't intersect any Indeterminant bits
371 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000372 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000373 // make sure the mask is still a mask (wrap arounds may not be)
374 return isRunOfOnes(Mask, MB, ME);
375 }
376 return false;
377}
378
Nate Begeman0f3257a2005-08-18 05:00:13 +0000379// isOpcWithIntImmediate - This method tests to see if the node is a specific
380// opcode and that it has a immediate integer right operand.
381// If so Imm will receive the 32 bit value.
382static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
383 return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm);
384}
385
Chris Lattnera5a91b12005-08-17 19:33:03 +0000386// isIntImmediate - This method tests to see if a constant operand.
387// If so Imm will receive the 32 bit value.
388static bool isIntImmediate(SDOperand N, unsigned& Imm) {
389 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
390 Imm = (unsigned)CN->getSignExtended();
391 return true;
392 }
393 return false;
394}
395
Nate Begeman02b88a42005-08-19 00:38:14 +0000396/// SelectBitfieldInsert - turn an or of two masked values into
397/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000398SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000399 SDOperand Op0 = N->getOperand(0);
400 SDOperand Op1 = N->getOperand(1);
401
Nate Begeman77f361f2006-05-07 00:23:38 +0000402 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000403 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
404 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000405
Nate Begeman4667f2c2006-05-08 17:38:32 +0000406 unsigned TargetMask = LKZ;
407 unsigned InsertMask = RKZ;
408
409 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
410 unsigned Op0Opc = Op0.getOpcode();
411 unsigned Op1Opc = Op1.getOpcode();
412 unsigned Value, SH = 0;
413 TargetMask = ~TargetMask;
414 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000415
Nate Begeman4667f2c2006-05-08 17:38:32 +0000416 // If the LHS has a foldable shift and the RHS does not, then swap it to the
417 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000418 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
419 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
420 Op0.getOperand(0).getOpcode() == ISD::SRL) {
421 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
422 Op1.getOperand(0).getOpcode() != ISD::SRL) {
423 std::swap(Op0, Op1);
424 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000425 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000426 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000427 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000428 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
429 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
430 Op1.getOperand(0).getOpcode() != ISD::SRL) {
431 std::swap(Op0, Op1);
432 std::swap(Op0Opc, Op1Opc);
433 std::swap(TargetMask, InsertMask);
434 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000435 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000436
437 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000438 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000439 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000440 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000441
442 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
443 isIntImmediate(Op1.getOperand(1), Value)) {
444 Op1 = Op1.getOperand(0);
445 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
446 }
447 if (Op1Opc == ISD::AND) {
448 unsigned SHOpc = Op1.getOperand(0).getOpcode();
449 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
450 isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
451 Op1 = Op1.getOperand(0).getOperand(0);
452 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
453 } else {
454 Op1 = Op1.getOperand(0);
455 }
456 }
457
458 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
459 Select(Tmp1, Tmp3);
460 Select(Tmp2, Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000461 SH &= 31;
Nate Begeman77f361f2006-05-07 00:23:38 +0000462 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
463 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000464 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000465 }
466 return 0;
467}
468
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000469/// SelectAddrImm - Returns true if the address N can be represented by
470/// a base register plus a signed 16-bit displacement [r+imm].
471bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
472 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000473 // If this can be more profitably realized as r+r, fail.
474 if (SelectAddrIdx(N, Disp, Base))
475 return false;
476
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000477 if (N.getOpcode() == ISD::ADD) {
478 unsigned imm = 0;
479 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner17e82d22006-01-12 01:54:15 +0000480 Disp = getI32Imm(imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000481 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
482 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Chris Lattner9944b762005-08-21 22:31:09 +0000483 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000484 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000485 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000486 return true; // [r+i]
487 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000488 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000489 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000490 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000491 Disp = N.getOperand(1).getOperand(0); // The global address.
492 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000493 Disp.getOpcode() == ISD::TargetConstantPool ||
494 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000495 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000496 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000497 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000498 } else if (N.getOpcode() == ISD::OR) {
499 unsigned imm = 0;
500 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
501 // If this is an or of disjoint bitfields, we can codegen this as an add
502 // (for better address arithmetic) if the LHS and RHS of the OR are
503 // provably disjoint.
504 uint64_t LHSKnownZero, LHSKnownOne;
505 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
506 LHSKnownZero, LHSKnownOne);
507 if ((LHSKnownZero|~imm) == ~0U) {
508 // If all of the bits are known zero on the LHS or RHS, the add won't
509 // carry.
510 Base = N.getOperand(0);
511 Disp = getI32Imm(imm & 0xFFFF);
512 return true;
513 }
514 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000515 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
516 // Loading from a constant address.
517 int Addr = (int)CN->getValue();
518
519 // If this address fits entirely in a 16-bit sext immediate field, codegen
520 // this as "d, 0"
521 if (Addr == (short)Addr) {
522 Disp = getI32Imm(Addr);
523 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
524 return true;
525 }
526
527 // Otherwise, break this down into an LIS + disp.
528 Disp = getI32Imm((short)Addr);
529 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
530 return true;
Chris Lattner9944b762005-08-21 22:31:09 +0000531 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000532
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000533 Disp = getI32Imm(0);
534 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
535 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
Nate Begeman28a6b022005-12-10 02:36:00 +0000536 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000537 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000538 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000539}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000540
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000541/// SelectAddrIdx - Given the specified addressed, check to see if it can be
542/// represented as an indexed [r+r] operation. Returns false if it can
543/// be represented by [r+imm], which are preferred.
544bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
545 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000546 unsigned imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000547 if (N.getOpcode() == ISD::ADD) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000548 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
549 return false; // r+i
550 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
551 return false; // r+i
552
Evan Cheng7564e0b2006-02-05 08:45:01 +0000553 Base = N.getOperand(0);
554 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000555 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000556 } else if (N.getOpcode() == ISD::OR) {
557 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm))
558 return false; // r+i can fold it if we can.
559
560 // If this is an or of disjoint bitfields, we can codegen this as an add
561 // (for better address arithmetic) if the LHS and RHS of the OR are provably
562 // disjoint.
563 uint64_t LHSKnownZero, LHSKnownOne;
564 uint64_t RHSKnownZero, RHSKnownOne;
565 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
566 LHSKnownZero, LHSKnownOne);
567
568 if (LHSKnownZero) {
569 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
570 RHSKnownZero, RHSKnownOne);
571 // If all of the bits are known zero on the LHS or RHS, the add won't
572 // carry.
573 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
574 Base = N.getOperand(0);
575 Index = N.getOperand(1);
576 return true;
577 }
578 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000579 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000580
581 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000582}
583
584/// SelectAddrIdxOnly - Given the specified addressed, force it to be
585/// represented as an indexed [r+r] operation.
586bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
587 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000588 // Check to see if we can easily represent this as an [r+r] address. This
589 // will fail if it thinks that the address is more profitably represented as
590 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000591 if (SelectAddrIdx(N, Base, Index))
592 return true;
593
594 // If the operand is an addition, always emit this as [r+r], since this is
595 // better (for code size, and execution, as the memop does the add for free)
596 // than emitting an explicit add.
597 if (N.getOpcode() == ISD::ADD) {
598 Base = N.getOperand(0);
599 Index = N.getOperand(1);
600 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000601 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000602
603 // Otherwise, do it the hard way, using R0 as the base register.
604 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
605 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000606 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000607}
608
Chris Lattnere5ba5802006-03-22 05:26:03 +0000609/// SelectAddrImmShift - Returns true if the address N can be represented by
610/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
611/// for use by STD and friends.
612bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
613 SDOperand &Base) {
614 // If this can be more profitably realized as r+r, fail.
615 if (SelectAddrIdx(N, Disp, Base))
616 return false;
617
618 if (N.getOpcode() == ISD::ADD) {
619 unsigned imm = 0;
620 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
621 (imm & 3) == 0) {
622 Disp = getI32Imm((imm & 0xFFFF) >> 2);
623 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
624 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
625 } else {
626 Base = N.getOperand(0);
627 }
628 return true; // [r+i]
629 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
630 // Match LOAD (ADD (X, Lo(G))).
631 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
632 && "Cannot handle constant offsets yet!");
633 Disp = N.getOperand(1).getOperand(0); // The global address.
634 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000635 Disp.getOpcode() == ISD::TargetConstantPool ||
636 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000637 Base = N.getOperand(0);
638 return true; // [&g+r]
639 }
640 } else if (N.getOpcode() == ISD::OR) {
641 unsigned imm = 0;
642 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm) &&
643 (imm & 3) == 0) {
644 // If this is an or of disjoint bitfields, we can codegen this as an add
645 // (for better address arithmetic) if the LHS and RHS of the OR are
646 // provably disjoint.
647 uint64_t LHSKnownZero, LHSKnownOne;
648 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
649 LHSKnownZero, LHSKnownOne);
650 if ((LHSKnownZero|~imm) == ~0U) {
651 // If all of the bits are known zero on the LHS or RHS, the add won't
652 // carry.
653 Base = N.getOperand(0);
654 Disp = getI32Imm((imm & 0xFFFF) >> 2);
655 return true;
656 }
657 }
658 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
659 // Loading from a constant address.
660 int Addr = (int)CN->getValue();
661 if ((Addr & 3) == 0) {
662 // If this address fits entirely in a 16-bit sext immediate field, codegen
663 // this as "d, 0"
664 if (Addr == (short)Addr) {
665 Disp = getI32Imm(Addr >> 2);
666 Base = CurDAG->getRegister(PPC::R0, MVT::i32);
667 return true;
668 }
669
670 // Otherwise, break this down into an LIS + disp.
671 Disp = getI32Imm((short)Addr >> 2);
672 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
673 return true;
674 }
675 }
676
677 Disp = getI32Imm(0);
678 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
679 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32);
680 else
681 Base = N;
682 return true; // [r+0]
683}
684
685
Chris Lattner2fbb4572005-08-21 18:50:37 +0000686/// SelectCC - Select a comparison of the specified values with the specified
687/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000688SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
689 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000690 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000691 Select(LHS, LHS);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000692
693 // Use U to determine whether the SETCC immediate range is signed or not.
694 if (MVT::isInteger(LHS.getValueType())) {
695 bool U = ISD::isUnsignedIntSetCC(CC);
696 unsigned Imm;
697 if (isIntImmediate(RHS, Imm) &&
698 ((U && isUInt16(Imm)) || (!U && isInt16(Imm))))
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000699 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI,
700 MVT::i32, LHS, getI32Imm(Imm & 0xFFFF)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000701 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000702 return SDOperand(CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32,
703 LHS, RHS), 0);
Chris Lattner919c0322005-10-01 01:35:02 +0000704 } else if (LHS.getValueType() == MVT::f32) {
Evan Cheng34167212006-02-09 00:37:58 +0000705 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000706 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUS, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000707 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000708 Select(RHS, RHS);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000709 return SDOperand(CurDAG->getTargetNode(PPC::FCMPUD, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000710 }
711}
712
713/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
714/// to Condition.
715static unsigned getBCCForSetCC(ISD::CondCode CC) {
716 switch (CC) {
717 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000718 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000719 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000720 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000721 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000722 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000723 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000724 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000725 case ISD::SETULT:
726 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000727 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000728 case ISD::SETULE:
729 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000730 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000731 case ISD::SETUGT:
732 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000733 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000734 case ISD::SETUGE:
735 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000736
737 case ISD::SETO: return PPC::BUN;
738 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000739 }
740 return 0;
741}
742
Chris Lattner64906a02005-08-25 20:08:18 +0000743/// getCRIdxForSetCC - Return the index of the condition register field
744/// associated with the SetCC condition, and whether or not the field is
745/// treated as inverted. That is, lt = 0; ge = 0 inverted.
746static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
747 switch (CC) {
748 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000749 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000750 case ISD::SETULT:
751 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000752 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000753 case ISD::SETUGE:
754 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000755 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000756 case ISD::SETUGT:
757 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000758 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000759 case ISD::SETULE:
760 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000761 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000762 case ISD::SETUEQ:
Chris Lattner64906a02005-08-25 20:08:18 +0000763 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000764 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000765 case ISD::SETUNE:
Chris Lattner64906a02005-08-25 20:08:18 +0000766 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000767 case ISD::SETO: Inv = true; return 3;
768 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000769 }
770 return 0;
771}
Chris Lattner9944b762005-08-21 22:31:09 +0000772
Nate Begeman1d9d7422005-10-18 00:28:58 +0000773SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000774 SDNode *N = Op.Val;
775 unsigned Imm;
776 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
777 if (isIntImmediate(N->getOperand(1), Imm)) {
778 // We can codegen setcc op, imm very efficiently compared to a brcond.
779 // Check for those cases here.
780 // setcc op, 0
781 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000782 SDOperand Op;
783 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000784 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000785 default: break;
786 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000787 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000788 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
789 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000790 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000791 SDOperand AD =
792 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
793 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000794 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
795 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000796 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000797 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000798 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
799 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000800 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000801 SDOperand T =
802 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
803 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000804 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
805 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000806 }
807 }
Chris Lattner222adac2005-10-06 19:03:35 +0000808 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000809 SDOperand Op;
810 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000811 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000812 default: break;
813 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000814 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
815 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000816 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000817 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
818 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000819 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000820 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000821 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
822 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
823 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000824 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
825 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000826 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000827 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000828 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
829 getI32Imm(1)), 0);
830 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
831 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000832 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
833 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000834 }
835 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000836 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
837 getI32Imm(1), getI32Imm(31),
838 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000839 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000840 }
Chris Lattner222adac2005-10-06 19:03:35 +0000841 }
842 }
843
844 bool Inv;
845 unsigned Idx = getCRIdxForSetCC(CC, Inv);
846 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
847 SDOperand IntCR;
848
849 // Force the ccreg into CR7.
850 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
851
Chris Lattner85961d52005-12-06 20:56:18 +0000852 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000853 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
854 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000855
856 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000857 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
858 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000859 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000860 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000861
862 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000863 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
864 getI32Imm((32-(3-Idx)) & 31),
865 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000866 } else {
867 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000868 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
869 getI32Imm((32-(3-Idx)) & 31),
870 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000871 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000872 }
Chris Lattner222adac2005-10-06 19:03:35 +0000873}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000874
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000875
Chris Lattnera5a91b12005-08-17 19:33:03 +0000876// Select - Convert the specified operand from a target-independent to a
877// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000878void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000879 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000880 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000881 N->getOpcode() < PPCISD::FIRST_NUMBER) {
882 Result = Op;
883 return; // Already selected.
884 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000885
886 // If this has already been converted, use it.
887 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000888 if (CGMI != CodeGenMap.end()) {
889 Result = CGMI->second;
890 return;
891 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000892
893 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000894 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000895 case ISD::SETCC:
896 Result = SelectSETCC(Op);
897 return;
Evan Cheng34167212006-02-09 00:37:58 +0000898 case PPCISD::GlobalBaseReg:
899 Result = getGlobalBaseReg();
900 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000901
Chris Lattnere28e40a2005-08-25 00:45:43 +0000902 case ISD::FrameIndex: {
903 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000904 if (N->hasOneUse()) {
905 Result = CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32,
906 CurDAG->getTargetFrameIndex(FI, MVT::i32),
907 getI32Imm(0));
908 return;
909 }
910 Result = CodeGenMap[Op] =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000911 SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32,
912 CurDAG->getTargetFrameIndex(FI, MVT::i32),
913 getI32Imm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000914 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000915 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000916
917 case PPCISD::MFCR: {
918 SDOperand InFlag;
919 Select(InFlag, N->getOperand(1));
920 // Use MFOCRF if supported.
921 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
922 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
923 N->getOperand(0), InFlag), 0);
924 else
925 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
926 CodeGenMap[Op] = Result;
927 return;
928 }
929
Chris Lattner88add102005-09-28 22:50:24 +0000930 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000931 // FIXME: since this depends on the setting of the carry flag from the srawi
932 // we should really be making notes about that for the scheduler.
933 // FIXME: It sure would be nice if we could cheaply recognize the
934 // srl/add/sra pattern the dag combiner will generate for this as
935 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000936 unsigned Imm;
937 if (isIntImmediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +0000938 SDOperand N0;
939 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +0000940 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000941 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000942 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000943 N0, getI32Imm(Log2_32(Imm)));
944 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000945 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000946 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000947 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000948 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000949 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +0000950 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000951 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
952 SDOperand(Op, 0), SDOperand(Op, 1)),
953 0);
Evan Cheng34167212006-02-09 00:37:58 +0000954 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000955 }
Evan Cheng34167212006-02-09 00:37:58 +0000956 return;
Chris Lattner8784a232005-08-25 17:50:06 +0000957 }
Chris Lattner047b9522005-08-25 22:04:30 +0000958
Chris Lattner237733e2005-09-29 23:33:31 +0000959 // Other cases are autogenerated.
960 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000961 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000962 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000963 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +0000964 // If this is an and of a value rotated between 0 and 31 bits and then and'd
965 // with a mask, emit rlwinm
966 if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) ||
967 isShiftedMask_32(~Imm))) {
968 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +0000969 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +0000970 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000971 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +0000972 } else if (Imm == 0) {
973 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +0000974 Select(Result, N->getOperand(1));
975 return ;
Chris Lattner3393e802005-10-25 19:32:37 +0000976 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000977 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +0000978 isRunOfOnes(Imm, MB, ME);
979 SH = 0;
980 }
Evan Cheng34167212006-02-09 00:37:58 +0000981 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
982 getI32Imm(SH), getI32Imm(MB),
983 getI32Imm(ME));
984 return;
Nate Begemancffc32b2005-08-18 07:30:46 +0000985 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000986 // ISD::OR doesn't get all the bitfield insertion fun.
987 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
988 if (isIntImmediate(N->getOperand(1), Imm) &&
989 N->getOperand(0).getOpcode() == ISD::OR &&
990 isIntImmediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000991 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000992 Imm = ~(Imm^Imm2);
993 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +0000994 SDOperand Tmp1, Tmp2;
995 Select(Tmp1, N->getOperand(0).getOperand(0));
996 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000997 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
998 Tmp1, Tmp2,
999 getI32Imm(0), getI32Imm(MB),
1000 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001001 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001002 }
1003 }
Chris Lattner237733e2005-09-29 23:33:31 +00001004
1005 // Other cases are autogenerated.
1006 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001007 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001008 case ISD::OR:
Evan Cheng34167212006-02-09 00:37:58 +00001009 if (SDNode *I = SelectBitfieldInsert(N)) {
1010 Result = CodeGenMap[Op] = SDOperand(I, 0);
1011 return;
1012 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001013
Chris Lattner237733e2005-09-29 23:33:31 +00001014 // Other cases are autogenerated.
1015 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001016 case ISD::SHL: {
1017 unsigned Imm, SH, MB, ME;
1018 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001019 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001020 SDOperand Val;
1021 Select(Val, N->getOperand(0).getOperand(0));
1022 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1023 Val, getI32Imm(SH), getI32Imm(MB),
1024 getI32Imm(ME));
1025 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001026 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001027
1028 // Other cases are autogenerated.
1029 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001030 }
1031 case ISD::SRL: {
1032 unsigned Imm, SH, MB, ME;
1033 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001034 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001035 SDOperand Val;
1036 Select(Val, N->getOperand(0).getOperand(0));
1037 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Chris Lattner0949ed52006-05-12 16:29:37 +00001038 Val, getI32Imm(SH), getI32Imm(MB),
Evan Cheng34167212006-02-09 00:37:58 +00001039 getI32Imm(ME));
1040 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001041 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001042
1043 // Other cases are autogenerated.
1044 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001045 }
Chris Lattner13794f52005-08-26 18:46:49 +00001046 case ISD::SELECT_CC: {
1047 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1048
1049 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1050 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1051 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1052 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1053 if (N1C->isNullValue() && N3C->isNullValue() &&
1054 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Evan Cheng34167212006-02-09 00:37:58 +00001055 SDOperand LHS;
1056 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001057 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001058 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1059 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001060 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1061 SDOperand(Tmp, 0), LHS,
1062 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001063 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001064 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001065
Chris Lattner50ff55c2005-09-01 19:20:44 +00001066 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001067 unsigned BROpc = getBCCForSetCC(CC);
1068
1069 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001070 unsigned SelectCCOp;
1071 if (MVT::isInteger(N->getValueType(0)))
1072 SelectCCOp = PPC::SELECT_CC_Int;
1073 else if (N->getValueType(0) == MVT::f32)
1074 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001075 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001076 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001077 else
1078 SelectCCOp = PPC::SELECT_CC_VRRC;
1079
Evan Cheng34167212006-02-09 00:37:58 +00001080 SDOperand N2, N3;
1081 Select(N2, N->getOperand(2));
1082 Select(N3, N->getOperand(3));
1083 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1084 N2, N3, getI32Imm(BROpc));
1085 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001086 }
Nate Begeman81e80972006-03-17 01:40:33 +00001087 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001088 SDOperand Chain;
1089 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001090 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1091 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001092 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1093 CondCode, getI32Imm(getBCCForSetCC(CC)),
1094 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001095 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001096 }
Nate Begeman37efe672006-04-22 18:53:45 +00001097 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001098 // FIXME: Should custom lower this.
Nate Begeman37efe672006-04-22 18:53:45 +00001099 SDOperand Chain, Target;
1100 Select(Chain, N->getOperand(0));
1101 Select(Target,N->getOperand(1));
1102 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1103 Chain), 0);
1104 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1105 return;
1106 }
Chris Lattnercf006312006-06-10 01:15:02 +00001107 // FIXME: These are manually selected because tblgen isn't handling varargs
1108 // nodes correctly.
1109 case PPCISD::BCTRL: MySelect_PPCbctrl(Result, Op); return;
1110 case PPCISD::CALL: MySelect_PPCcall(Result, Op); return;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001111 }
Chris Lattner25dae722005-09-03 00:53:47 +00001112
Evan Cheng34167212006-02-09 00:37:58 +00001113 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001114}
1115
1116
Chris Lattnercf006312006-06-10 01:15:02 +00001117// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1118// correctly.
1119void PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand &Result, SDOperand N) {
1120 SDOperand Chain(0, 0);
1121 SDOperand InFlag(0, 0);
1122 SDNode *ResNode;
1123
1124 bool hasFlag =
1125 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1126
1127 std::vector<SDOperand> Ops;
1128 // Push varargs arguments, including optional flag.
1129 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1130 Select(Chain, N.getOperand(i));
1131 Ops.push_back(Chain);
1132 }
1133
1134 Select(Chain, N.getOperand(0));
1135 Ops.push_back(Chain);
1136
1137 if (hasFlag) {
1138 Select(Chain, N.getOperand(N.getNumOperands()-1));
1139 Ops.push_back(Chain);
1140 }
1141
1142 ResNode = CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag, Ops);
1143 Chain = SDOperand(ResNode, 0);
1144 InFlag = SDOperand(ResNode, 1);
1145 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1146 Chain.ResNo);
1147 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1148 InFlag.ResNo);
1149 Result = SDOperand(ResNode, N.ResNo);
1150 return;
1151}
1152
1153// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1154// correctly.
1155void PPCDAGToDAGISel::MySelect_PPCcall(SDOperand &Result, SDOperand N) {
1156 SDOperand Chain(0, 0);
1157 SDOperand InFlag(0, 0);
1158 SDOperand N1(0, 0);
1159 SDOperand Tmp0(0, 0);
1160 SDNode *ResNode;
1161 Chain = N.getOperand(0);
1162 N1 = N.getOperand(1);
1163
1164 // Pattern: (PPCcall:void (imm:i32):$func)
1165 // Emits: (BLA:void (imm:i32):$func)
1166 // Pattern complexity = 4 cost = 1
1167 if (N1.getOpcode() == ISD::Constant) {
1168 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1169
1170 std::vector<SDOperand> Ops;
1171 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1172
1173 bool hasFlag =
1174 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1175
1176 // Push varargs arguments, not including optional flag.
1177 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1178 Select(Chain, N.getOperand(i));
1179 Ops.push_back(Chain);
1180 }
1181 Select(Chain, N.getOperand(0));
1182 Ops.push_back(Chain);
1183 if (hasFlag) {
1184 Select(Chain, N.getOperand(N.getNumOperands()-1));
1185 Ops.push_back(Chain);
1186 }
1187 ResNode = CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag, Ops);
1188
1189 Chain = SDOperand(ResNode, 0);
1190 InFlag = SDOperand(ResNode, 1);
1191 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val, Chain.ResNo);
1192 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val, InFlag.ResNo);
1193 Result = SDOperand(ResNode, N.ResNo);
1194 return;
1195 }
1196
1197 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1198 // Emits: (BL:void (tglobaladdr:i32):$dst)
1199 // Pattern complexity = 4 cost = 1
1200 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
1201 std::vector<SDOperand> Ops;
1202 Ops.push_back(N1);
1203
1204 bool hasFlag =
1205 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1206
1207 // Push varargs arguments, not including optional flag.
1208 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1209 Select(Chain, N.getOperand(i));
1210 Ops.push_back(Chain);
1211 }
1212 Select(Chain, N.getOperand(0));
1213 Ops.push_back(Chain);
1214 if (hasFlag) {
1215 Select(Chain, N.getOperand(N.getNumOperands()-1));
1216 Ops.push_back(Chain);
1217 }
1218
1219 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1220
1221 Chain = SDOperand(ResNode, 0);
1222 InFlag = SDOperand(ResNode, 1);
1223 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val, Chain.ResNo);
1224 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val, InFlag.ResNo);
1225 Result = SDOperand(ResNode, N.ResNo);
1226 return;
1227 }
1228
1229 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1230 // Emits: (BL:void (texternalsym:i32):$dst)
1231 // Pattern complexity = 4 cost = 1
1232 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1233 std::vector<SDOperand> Ops;
1234 Ops.push_back(N1);
1235
1236 bool hasFlag =
1237 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1238
1239 // Push varargs arguments, not including optional flag.
1240 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1241 Select(Chain, N.getOperand(i));
1242 Ops.push_back(Chain);
1243 }
1244 Select(Chain, N.getOperand(0));
1245 Ops.push_back(Chain);
1246 if (hasFlag) {
1247 Select(Chain, N.getOperand(N.getNumOperands()-1));
1248 Ops.push_back(Chain);
1249 }
1250
1251 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1252
1253 Chain = SDOperand(ResNode, 0);
1254 InFlag = SDOperand(ResNode, 1);
1255 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val, Chain.ResNo);
1256 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val, InFlag.ResNo);
1257 Result = SDOperand(ResNode, N.ResNo);
1258 return;
1259 }
1260 std::cerr << "Cannot yet select: ";
1261 N.Val->dump(CurDAG);
1262 std::cerr << '\n';
1263 abort();
1264}
1265
1266
Nate Begeman1d9d7422005-10-18 00:28:58 +00001267/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001268/// PowerPC-specific DAG, ready for instruction scheduling.
1269///
Evan Chengc4c62572006-03-13 23:20:37 +00001270FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001271 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001272}
1273