blob: 6c057fdb75b8fdee4b0a174a4f657c42215e2dbc [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 Lattner2a41a982006-06-28 22:00:36 +000031#include "llvm/Support/Visibility.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000033#include <set>
Chris Lattnera5a91b12005-08-17 19:33:03 +000034using namespace llvm;
35
36namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000037 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
38
39 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000040 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000041 /// instructions for SelectionDAG operations.
42 ///
Chris Lattner2a41a982006-06-28 22:00:36 +000043 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000044 PPCTargetMachine &TM;
Nate Begeman21e463b2005-10-16 05:39:50 +000045 PPCTargetLowering PPCLowering;
Chris Lattner4416f1a2005-08-19 22:38:53 +000046 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000047 public:
Chris Lattner4bb18952006-03-16 18:25:23 +000048 PPCDAGToDAGISel(PPCTargetMachine &tm)
49 : SelectionDAGISel(PPCLowering), TM(tm),
50 PPCLowering(*TM.getTargetLowering()) {}
Chris Lattnera5a91b12005-08-17 19:33:03 +000051
Chris Lattner4416f1a2005-08-19 22:38:53 +000052 virtual bool runOnFunction(Function &Fn) {
53 // Make sure we re-emit a set of the global base reg if necessary
54 GlobalBaseReg = 0;
Chris Lattner4bb18952006-03-16 18:25:23 +000055 SelectionDAGISel::runOnFunction(Fn);
56
57 InsertVRSaveCode(Fn);
58 return true;
Chris Lattner4416f1a2005-08-19 22:38:53 +000059 }
60
Chris Lattnera5a91b12005-08-17 19:33:03 +000061 /// getI32Imm - Return a target constant with the specified value, of type
62 /// i32.
63 inline SDOperand getI32Imm(unsigned Imm) {
64 return CurDAG->getTargetConstant(Imm, MVT::i32);
65 }
Chris Lattner4416f1a2005-08-19 22:38:53 +000066
Chris Lattnerc08f9022006-06-27 00:04:13 +000067 /// getI64Imm - Return a target constant with the specified value, of type
68 /// i64.
69 inline SDOperand getI64Imm(uint64_t Imm) {
70 return CurDAG->getTargetConstant(Imm, MVT::i64);
71 }
72
73 /// getSmallIPtrImm - Return a target constant of pointer type.
74 inline SDOperand getSmallIPtrImm(unsigned Imm) {
75 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
76 }
77
78
Chris Lattner4416f1a2005-08-19 22:38:53 +000079 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
80 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000081 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000082
83 // Select - Convert the specified operand from a target-independent to a
84 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000085 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000086
Nate Begeman02b88a42005-08-19 00:38:14 +000087 SDNode *SelectBitfieldInsert(SDNode *N);
88
Chris Lattner2fbb4572005-08-21 18:50:37 +000089 /// SelectCC - Select a comparison of the specified values with the
90 /// specified condition code, returning the CR# of the expression.
91 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
92
Nate Begeman7fd1edd2005-12-19 23:25:09 +000093 /// SelectAddrImm - Returns true if the address N can be represented by
94 /// a base register plus a signed 16-bit displacement [r+imm].
95 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
96
97 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
98 /// represented as an indexed [r+r] operation. Returns false if it can
99 /// be represented by [r+imm], which are preferred.
100 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000101
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000102 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
103 /// represented as an indexed [r+r] operation.
104 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +0000105
Chris Lattnere5ba5802006-03-22 05:26:03 +0000106 /// SelectAddrImmShift - Returns true if the address N can be represented by
107 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
108 /// for use by STD and friends.
109 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
110
Chris Lattnere5d88612006-02-24 02:13:12 +0000111 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
112 /// inline asm expressions.
113 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
114 char ConstraintCode,
115 std::vector<SDOperand> &OutOps,
116 SelectionDAG &DAG) {
117 SDOperand Op0, Op1;
118 switch (ConstraintCode) {
119 default: return true;
120 case 'm': // memory
121 if (!SelectAddrIdx(Op, Op0, Op1))
122 SelectAddrImm(Op, Op0, Op1);
123 break;
124 case 'o': // offsetable
125 if (!SelectAddrImm(Op, Op0, Op1)) {
126 Select(Op0, Op); // r+0.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000127 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000128 }
129 break;
130 case 'v': // not offsetable
131 SelectAddrIdxOnly(Op, Op0, Op1);
132 break;
133 }
134
135 OutOps.push_back(Op0);
136 OutOps.push_back(Op1);
137 return false;
138 }
139
Chris Lattner047b9522005-08-25 22:04:30 +0000140 SDOperand BuildSDIVSequence(SDNode *N);
141 SDOperand BuildUDIVSequence(SDNode *N);
142
Chris Lattnera5a91b12005-08-17 19:33:03 +0000143 /// InstructionSelectBasicBlock - This callback is invoked by
144 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000145 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
146
Chris Lattner4bb18952006-03-16 18:25:23 +0000147 void InsertVRSaveCode(Function &Fn);
148
Chris Lattnera5a91b12005-08-17 19:33:03 +0000149 virtual const char *getPassName() const {
150 return "PowerPC DAG->DAG Pattern Instruction Selection";
151 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000152
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000153 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
154 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000155 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000156 // Should use subtarget info to pick the right hazard recognizer. For
157 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000158 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
159 assert(II && "No InstrInfo?");
160 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 }
Chris Lattneraf165382005-09-13 22:03:06 +0000162
163// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000164#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000165
166private:
Chris Lattner222adac2005-10-06 19:03:35 +0000167 SDOperand SelectSETCC(SDOperand Op);
Chris Lattnercf006312006-06-10 01:15:02 +0000168 void MySelect_PPCbctrl(SDOperand &Result, SDOperand N);
169 void MySelect_PPCcall(SDOperand &Result, SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000170 };
171}
172
Chris Lattnerbd937b92005-10-06 18:45:51 +0000173/// InstructionSelectBasicBlock - This callback is invoked by
174/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000175void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000176 DEBUG(BB->dump());
177
178 // The selection process is inherently a bottom-up recursive process (users
179 // select their uses before themselves). Given infinite stack space, we
180 // could just start selecting on the root and traverse the whole graph. In
181 // practice however, this causes us to run out of stack space on large basic
182 // blocks. To avoid this problem, select the entry node, then all its uses,
183 // iteratively instead of recursively.
184 std::vector<SDOperand> Worklist;
185 Worklist.push_back(DAG.getEntryNode());
186
187 // Note that we can do this in the PPC target (scanning forward across token
188 // chain edges) because no nodes ever get folded across these edges. On a
189 // target like X86 which supports load/modify/store operations, this would
190 // have to be more careful.
191 while (!Worklist.empty()) {
192 SDOperand Node = Worklist.back();
193 Worklist.pop_back();
194
Chris Lattnercf01a702005-10-07 22:10:27 +0000195 // Chose from the least deep of the top two nodes.
196 if (!Worklist.empty() &&
197 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
198 std::swap(Worklist.back(), Node);
199
Chris Lattnerbd937b92005-10-06 18:45:51 +0000200 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
201 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
202 CodeGenMap.count(Node)) continue;
203
204 for (SDNode::use_iterator UI = Node.Val->use_begin(),
205 E = Node.Val->use_end(); UI != E; ++UI) {
206 // Scan the values. If this use has a value that is a token chain, add it
207 // to the worklist.
208 SDNode *User = *UI;
209 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
210 if (User->getValueType(i) == MVT::Other) {
211 Worklist.push_back(SDOperand(User, i));
212 break;
213 }
214 }
215
216 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000217 SDOperand Dummy;
218 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000219 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000220
Chris Lattnerbd937b92005-10-06 18:45:51 +0000221 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000222 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000223 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Chris Lattnerbd937b92005-10-06 18:45:51 +0000224 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000225 HandleMap.clear();
226 ReplaceMap.clear();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000227 DAG.RemoveDeadNodes();
228
Chris Lattner1877ec92006-03-13 21:52:10 +0000229 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000230 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000231}
232
233/// InsertVRSaveCode - Once the entire function has been instruction selected,
234/// all virtual registers are created and all machine instructions are built,
235/// check to see if we need to save/restore VRSAVE. If so, do it.
236void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000237 // Check to see if this function uses vector registers, which means we have to
238 // save and restore the VRSAVE register and update it with the regs we use.
239 //
240 // In this case, there will be virtual registers of vector type type created
241 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000242 MachineFunction &Fn = MachineFunction::get(&F);
243 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000244 bool HasVectorVReg = false;
245 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000246 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000247 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
248 HasVectorVReg = true;
249 break;
250 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000251 if (!HasVectorVReg) return; // nothing to do.
252
Chris Lattner1877ec92006-03-13 21:52:10 +0000253 // If we have a vector register, we want to emit code into the entry and exit
254 // blocks to save and restore the VRSAVE register. We do this here (instead
255 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
256 //
257 // 1. This (trivially) reduces the load on the register allocator, by not
258 // having to represent the live range of the VRSAVE register.
259 // 2. This (more significantly) allows us to create a temporary virtual
260 // register to hold the saved VRSAVE value, allowing this temporary to be
261 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000262
263 // Create two vregs - one to hold the VRSAVE register that is live-in to the
264 // function and one for the value after having bits or'd into it.
265 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
266 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
267
268 MachineBasicBlock &EntryBB = *Fn.begin();
269 // Emit the following code into the entry block:
270 // InVRSAVE = MFVRSAVE
271 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
272 // MTVRSAVE UpdatedVRSAVE
273 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
274 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
275 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
276 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
277
278 // Find all return blocks, outputting a restore in each epilog.
279 const TargetInstrInfo &TII = *TM.getInstrInfo();
280 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
281 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
282 IP = BB->end(); --IP;
283
284 // Skip over all terminator instructions, which are part of the return
285 // sequence.
286 MachineBasicBlock::iterator I2 = IP;
287 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
288 IP = I2;
289
290 // Emit: MTVRSAVE InVRSave
291 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
292 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000293 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000294}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000295
Chris Lattner4bb18952006-03-16 18:25:23 +0000296
Chris Lattner4416f1a2005-08-19 22:38:53 +0000297/// getGlobalBaseReg - Output the instructions required to put the
298/// base address to use for accessing globals into a register.
299///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000300SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000301 if (!GlobalBaseReg) {
302 // Insert the set of GlobalBaseReg into the first MBB of the function
303 MachineBasicBlock &FirstMBB = BB->getParent()->front();
304 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
305 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000306
307 if (PPCLowering.getPointerTy() == MVT::i32)
308 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
309 else
310 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
311
Chris Lattner4416f1a2005-08-19 22:38:53 +0000312 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
313 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
314 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000315 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy());
316}
317
318/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
319/// or 64-bit immediate, and if the value can be accurately represented as a
320/// sign extension from a 16-bit value. If so, this returns true and the
321/// immediate.
322static bool isIntS16Immediate(SDNode *N, short &Imm) {
323 if (N->getOpcode() != ISD::Constant)
324 return false;
325
326 Imm = (short)cast<ConstantSDNode>(N)->getValue();
327 if (N->getValueType(0) == MVT::i32)
328 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
329 else
330 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
331}
332
333static bool isIntS16Immediate(SDOperand Op, short &Imm) {
334 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000335}
336
337
Chris Lattnerc08f9022006-06-27 00:04:13 +0000338/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
339/// operand. If so Imm will receive the 32-bit value.
340static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
341 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000342 Imm = cast<ConstantSDNode>(N)->getValue();
343 return true;
344 }
345 return false;
346}
347
Chris Lattnerc08f9022006-06-27 00:04:13 +0000348/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
349/// operand. If so Imm will receive the 64-bit value.
350static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
351 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
352 Imm = cast<ConstantSDNode>(N)->getValue();
353 return true;
354 }
355 return false;
356}
357
358// isInt32Immediate - This method tests to see if a constant operand.
359// If so Imm will receive the 32 bit value.
360static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
361 return isInt32Immediate(N.Val, Imm);
362}
363
364
365// isOpcWithIntImmediate - This method tests to see if the node is a specific
366// opcode and that it has a immediate integer right operand.
367// If so Imm will receive the 32 bit value.
368static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
369 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
370}
371
372
Nate Begemancffc32b2005-08-18 07:30:46 +0000373// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
374// any number of 0s on either side. The 1s are allowed to wrap from LSB to
375// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
376// not, since all 1s are not contiguous.
377static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
378 if (isShiftedMask_32(Val)) {
379 // look for the first non-zero bit
380 MB = CountLeadingZeros_32(Val);
381 // look for the first zero bit after the run of ones
382 ME = CountLeadingZeros_32((Val - 1) ^ Val);
383 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000384 } else {
385 Val = ~Val; // invert mask
386 if (isShiftedMask_32(Val)) {
387 // effectively look for the first zero bit
388 ME = CountLeadingZeros_32(Val) - 1;
389 // effectively look for the first one bit after the run of zeros
390 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
391 return true;
392 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000393 }
394 // no run present
395 return false;
396}
397
Chris Lattner65a419a2005-10-09 05:36:17 +0000398// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000399// and mask opcode and mask operation.
400static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
401 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000402 // Don't even go down this path for i64, since different logic will be
403 // necessary for rldicl/rldicr/rldimi.
404 if (N->getValueType(0) != MVT::i32)
405 return false;
406
Nate Begemancffc32b2005-08-18 07:30:46 +0000407 unsigned Shift = 32;
408 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
409 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000410 if (N->getNumOperands() != 2 ||
Chris Lattnerc08f9022006-06-27 00:04:13 +0000411 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000412 return false;
413
414 if (Opcode == ISD::SHL) {
415 // apply shift left to mask if it comes first
416 if (IsShiftMask) Mask = Mask << Shift;
417 // determine which bits are made indeterminant by shift
418 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000419 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000420 // apply shift right to mask if it comes first
421 if (IsShiftMask) Mask = Mask >> Shift;
422 // determine which bits are made indeterminant by shift
423 Indeterminant = ~(0xFFFFFFFFu >> Shift);
424 // adjust for the left rotate
425 Shift = 32 - Shift;
426 } else {
427 return false;
428 }
429
430 // if the mask doesn't intersect any Indeterminant bits
431 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000432 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000433 // make sure the mask is still a mask (wrap arounds may not be)
434 return isRunOfOnes(Mask, MB, ME);
435 }
436 return false;
437}
438
Nate Begeman02b88a42005-08-19 00:38:14 +0000439/// SelectBitfieldInsert - turn an or of two masked values into
440/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000441SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000442 SDOperand Op0 = N->getOperand(0);
443 SDOperand Op1 = N->getOperand(1);
444
Nate Begeman77f361f2006-05-07 00:23:38 +0000445 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000446 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
447 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000448
Nate Begeman4667f2c2006-05-08 17:38:32 +0000449 unsigned TargetMask = LKZ;
450 unsigned InsertMask = RKZ;
451
452 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
453 unsigned Op0Opc = Op0.getOpcode();
454 unsigned Op1Opc = Op1.getOpcode();
455 unsigned Value, SH = 0;
456 TargetMask = ~TargetMask;
457 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000458
Nate Begeman4667f2c2006-05-08 17:38:32 +0000459 // If the LHS has a foldable shift and the RHS does not, then swap it to the
460 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000461 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
462 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
463 Op0.getOperand(0).getOpcode() == ISD::SRL) {
464 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
465 Op1.getOperand(0).getOpcode() != ISD::SRL) {
466 std::swap(Op0, Op1);
467 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000468 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000469 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000470 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000471 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
472 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
473 Op1.getOperand(0).getOpcode() != ISD::SRL) {
474 std::swap(Op0, Op1);
475 std::swap(Op0Opc, Op1Opc);
476 std::swap(TargetMask, InsertMask);
477 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000478 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000479
480 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000481 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000482 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000483 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000484
485 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000486 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000487 Op1 = Op1.getOperand(0);
488 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
489 }
490 if (Op1Opc == ISD::AND) {
491 unsigned SHOpc = Op1.getOperand(0).getOpcode();
492 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000493 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000494 Op1 = Op1.getOperand(0).getOperand(0);
495 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
496 } else {
497 Op1 = Op1.getOperand(0);
498 }
499 }
500
501 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
502 Select(Tmp1, Tmp3);
503 Select(Tmp2, Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000504 SH &= 31;
Nate Begeman77f361f2006-05-07 00:23:38 +0000505 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
506 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000507 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000508 }
509 return 0;
510}
511
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000512/// SelectAddrImm - Returns true if the address N can be represented by
513/// a base register plus a signed 16-bit displacement [r+imm].
514bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
515 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000516 // If this can be more profitably realized as r+r, fail.
517 if (SelectAddrIdx(N, Disp, Base))
518 return false;
519
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000520 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000521 short imm = 0;
522 if (isIntS16Immediate(N.getOperand(1), imm)) {
523 Disp = getI32Imm((int)imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000524 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000525 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner9944b762005-08-21 22:31:09 +0000526 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000527 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000528 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000529 return true; // [r+i]
530 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000531 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000532 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000533 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000534 Disp = N.getOperand(1).getOperand(0); // The global address.
535 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000536 Disp.getOpcode() == ISD::TargetConstantPool ||
537 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000538 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000539 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000540 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000541 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000542 short imm = 0;
543 if (isIntS16Immediate(N.getOperand(1), imm)) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000544 // If this is an or of disjoint bitfields, we can codegen this as an add
545 // (for better address arithmetic) if the LHS and RHS of the OR are
546 // provably disjoint.
547 uint64_t LHSKnownZero, LHSKnownOne;
548 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
549 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000550 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000551 // If all of the bits are known zero on the LHS or RHS, the add won't
552 // carry.
553 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000554 Disp = getI32Imm((int)imm & 0xFFFF);
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000555 return true;
556 }
557 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000558 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
559 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000560
Chris Lattnerd9796442006-03-20 22:38:22 +0000561 // If this address fits entirely in a 16-bit sext immediate field, codegen
562 // this as "d, 0"
Chris Lattnerc08f9022006-06-27 00:04:13 +0000563 short Imm;
564 if (isIntS16Immediate(CN, Imm)) {
565 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
566 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
Chris Lattnerd9796442006-03-20 22:38:22 +0000567 return true;
568 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000569
570 // FIXME: Handle small sext constant offsets in PPC64 mode also!
571 if (CN->getValueType(0) == MVT::i32) {
572 int Addr = (int)CN->getValue();
Chris Lattnerd9796442006-03-20 22:38:22 +0000573
Chris Lattnerc08f9022006-06-27 00:04:13 +0000574 // Otherwise, break this down into an LIS + disp.
575 Disp = getI32Imm((short)Addr);
576 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
577 return true;
578 }
Chris Lattner9944b762005-08-21 22:31:09 +0000579 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000580
Chris Lattnerc08f9022006-06-27 00:04:13 +0000581 Disp = getSmallIPtrImm(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000582 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000583 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Nate Begeman28a6b022005-12-10 02:36:00 +0000584 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000585 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000586 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000587}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000588
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000589/// SelectAddrIdx - Given the specified addressed, check to see if it can be
590/// represented as an indexed [r+r] operation. Returns false if it can
591/// be represented by [r+imm], which are preferred.
592bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
593 SDOperand &Index) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000594 short imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000595 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000596 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000597 return false; // r+i
598 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
599 return false; // r+i
600
Evan Cheng7564e0b2006-02-05 08:45:01 +0000601 Base = N.getOperand(0);
602 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000603 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000604 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000605 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000606 return false; // r+i can fold it if we can.
607
608 // If this is an or of disjoint bitfields, we can codegen this as an add
609 // (for better address arithmetic) if the LHS and RHS of the OR are provably
610 // disjoint.
611 uint64_t LHSKnownZero, LHSKnownOne;
612 uint64_t RHSKnownZero, RHSKnownOne;
613 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
614 LHSKnownZero, LHSKnownOne);
615
616 if (LHSKnownZero) {
617 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
618 RHSKnownZero, RHSKnownOne);
619 // If all of the bits are known zero on the LHS or RHS, the add won't
620 // carry.
621 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
622 Base = N.getOperand(0);
623 Index = N.getOperand(1);
624 return true;
625 }
626 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000627 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000628
629 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000630}
631
632/// SelectAddrIdxOnly - Given the specified addressed, force it to be
633/// represented as an indexed [r+r] operation.
634bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
635 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000636 // Check to see if we can easily represent this as an [r+r] address. This
637 // will fail if it thinks that the address is more profitably represented as
638 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000639 if (SelectAddrIdx(N, Base, Index))
640 return true;
641
642 // If the operand is an addition, always emit this as [r+r], since this is
643 // better (for code size, and execution, as the memop does the add for free)
644 // than emitting an explicit add.
645 if (N.getOpcode() == ISD::ADD) {
646 Base = N.getOperand(0);
647 Index = N.getOperand(1);
648 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000649 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000650
651 // Otherwise, do it the hard way, using R0 as the base register.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000652 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
Chris Lattner54e869e2006-03-24 17:58:06 +0000653 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000654 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000655}
656
Chris Lattnere5ba5802006-03-22 05:26:03 +0000657/// SelectAddrImmShift - Returns true if the address N can be represented by
658/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
659/// for use by STD and friends.
660bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
661 SDOperand &Base) {
662 // If this can be more profitably realized as r+r, fail.
663 if (SelectAddrIdx(N, Disp, Base))
664 return false;
665
666 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000667 short imm = 0;
668 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
669 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000670 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000671 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000672 } else {
673 Base = N.getOperand(0);
674 }
675 return true; // [r+i]
676 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
677 // Match LOAD (ADD (X, Lo(G))).
678 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
679 && "Cannot handle constant offsets yet!");
680 Disp = N.getOperand(1).getOperand(0); // The global address.
681 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000682 Disp.getOpcode() == ISD::TargetConstantPool ||
683 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000684 Base = N.getOperand(0);
685 return true; // [&g+r]
686 }
687 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000688 short imm = 0;
689 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000690 // If this is an or of disjoint bitfields, we can codegen this as an add
691 // (for better address arithmetic) if the LHS and RHS of the OR are
692 // provably disjoint.
693 uint64_t LHSKnownZero, LHSKnownOne;
694 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
695 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000696 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000697 // If all of the bits are known zero on the LHS or RHS, the add won't
698 // carry.
699 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000700 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000701 return true;
702 }
703 }
704 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
705 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000706
707 // If this address fits entirely in a 14-bit sext immediate field, codegen
708 // this as "d, 0"
709 short Imm;
710 if (isIntS16Immediate(CN, Imm)) {
711 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
712 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
713 return true;
714 }
715
716 // FIXME: Handle small sext constant offsets in PPC64 mode also!
717 if (CN->getValueType(0) == MVT::i32) {
718 int Addr = (int)CN->getValue();
Chris Lattnere5ba5802006-03-22 05:26:03 +0000719
720 // Otherwise, break this down into an LIS + disp.
721 Disp = getI32Imm((short)Addr >> 2);
722 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
723 return true;
724 }
725 }
726
Chris Lattnerc08f9022006-06-27 00:04:13 +0000727 Disp = getSmallIPtrImm(0);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000728 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000729 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000730 else
731 Base = N;
732 return true; // [r+0]
733}
734
735
Chris Lattner2fbb4572005-08-21 18:50:37 +0000736/// SelectCC - Select a comparison of the specified values with the specified
737/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000738SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
739 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000740 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000741 Select(LHS, LHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000742 unsigned Opc;
743
744 if (LHS.getValueType() == MVT::i32) {
Chris Lattner529c2332006-06-27 00:10:13 +0000745 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000746 if (ISD::isUnsignedIntSetCC(CC)) {
747 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
748 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
749 getI32Imm(Imm & 0xFFFF)), 0);
750 Opc = PPC::CMPLW;
751 } else {
752 short SImm;
753 if (isIntS16Immediate(RHS, SImm))
754 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
755 getI32Imm((int)SImm & 0xFFFF)),
756 0);
757 Opc = PPC::CMPW;
758 }
759 } else if (LHS.getValueType() == MVT::i64) {
760 uint64_t Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000761 if (ISD::isUnsignedIntSetCC(CC)) {
762 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
763 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
764 getI64Imm(Imm & 0xFFFF)), 0);
765 Opc = PPC::CMPLD;
766 } else {
767 short SImm;
768 if (isIntS16Immediate(RHS, SImm))
769 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
770 getI64Imm((int)SImm & 0xFFFF)),
771 0);
772 Opc = PPC::CMPD;
773 }
Chris Lattner919c0322005-10-01 01:35:02 +0000774 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000775 Opc = PPC::FCMPUS;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000776 } else {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000777 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
778 Opc = PPC::FCMPUD;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000779 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000780 Select(RHS, RHS);
781 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000782}
783
784/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
785/// to Condition.
786static unsigned getBCCForSetCC(ISD::CondCode CC) {
787 switch (CC) {
788 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000789 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000790 case ISD::SETUEQ:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000791 case ISD::SETEQ: return PPC::BEQ;
Chris Lattnered048c02005-10-28 20:49:47 +0000792 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner5d634ce2006-05-25 16:54:16 +0000793 case ISD::SETUNE:
Chris Lattner2fbb4572005-08-21 18:50:37 +0000794 case ISD::SETNE: return PPC::BNE;
Chris Lattnered048c02005-10-28 20:49:47 +0000795 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000796 case ISD::SETULT:
797 case ISD::SETLT: return PPC::BLT;
Chris Lattnered048c02005-10-28 20:49:47 +0000798 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000799 case ISD::SETULE:
800 case ISD::SETLE: return PPC::BLE;
Chris Lattnered048c02005-10-28 20:49:47 +0000801 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000802 case ISD::SETUGT:
803 case ISD::SETGT: return PPC::BGT;
Chris Lattnered048c02005-10-28 20:49:47 +0000804 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner2fbb4572005-08-21 18:50:37 +0000805 case ISD::SETUGE:
806 case ISD::SETGE: return PPC::BGE;
Chris Lattner6df25072005-10-28 20:32:44 +0000807
808 case ISD::SETO: return PPC::BUN;
809 case ISD::SETUO: return PPC::BNU;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000810 }
811 return 0;
812}
813
Chris Lattner64906a02005-08-25 20:08:18 +0000814/// getCRIdxForSetCC - Return the index of the condition register field
815/// associated with the SetCC condition, and whether or not the field is
816/// treated as inverted. That is, lt = 0; ge = 0 inverted.
817static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
818 switch (CC) {
819 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnered048c02005-10-28 20:49:47 +0000820 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000821 case ISD::SETULT:
822 case ISD::SETLT: Inv = false; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000823 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000824 case ISD::SETUGE:
825 case ISD::SETGE: Inv = true; return 0;
Chris Lattnered048c02005-10-28 20:49:47 +0000826 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000827 case ISD::SETUGT:
828 case ISD::SETGT: Inv = false; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000829 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
Chris Lattner64906a02005-08-25 20:08:18 +0000830 case ISD::SETULE:
831 case ISD::SETLE: Inv = true; return 1;
Chris Lattnered048c02005-10-28 20:49:47 +0000832 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000833 case ISD::SETUEQ:
Chris Lattner64906a02005-08-25 20:08:18 +0000834 case ISD::SETEQ: Inv = false; return 2;
Chris Lattnered048c02005-10-28 20:49:47 +0000835 case ISD::SETONE: // FIXME: This is incorrect see PR642.
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000836 case ISD::SETUNE:
Chris Lattner64906a02005-08-25 20:08:18 +0000837 case ISD::SETNE: Inv = true; return 2;
Chris Lattner6df25072005-10-28 20:32:44 +0000838 case ISD::SETO: Inv = true; return 3;
839 case ISD::SETUO: Inv = false; return 3;
Chris Lattner64906a02005-08-25 20:08:18 +0000840 }
841 return 0;
842}
Chris Lattner9944b762005-08-21 22:31:09 +0000843
Nate Begeman1d9d7422005-10-18 00:28:58 +0000844SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
Chris Lattner222adac2005-10-06 19:03:35 +0000845 SDNode *N = Op.Val;
846 unsigned Imm;
847 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000848 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner222adac2005-10-06 19:03:35 +0000849 // We can codegen setcc op, imm very efficiently compared to a brcond.
850 // Check for those cases here.
851 // setcc op, 0
852 if (Imm == 0) {
Evan Cheng34167212006-02-09 00:37:58 +0000853 SDOperand Op;
854 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000855 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000856 default: break;
857 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000858 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000859 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
860 getI32Imm(5), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000861 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000862 SDOperand AD =
863 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
864 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000865 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
866 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000867 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000868 case ISD::SETLT:
Chris Lattner71d3d502005-11-30 22:53:06 +0000869 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
870 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000871 case ISD::SETGT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000872 SDOperand T =
873 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
874 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000875 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
876 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000877 }
878 }
Chris Lattner222adac2005-10-06 19:03:35 +0000879 } else if (Imm == ~0U) { // setcc op, -1
Evan Cheng34167212006-02-09 00:37:58 +0000880 SDOperand Op;
881 Select(Op, N->getOperand(0));
Chris Lattner222adac2005-10-06 19:03:35 +0000882 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000883 default: break;
884 case ISD::SETEQ:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000885 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
886 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000887 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000888 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
889 getI32Imm(0)), 0),
Chris Lattner71d3d502005-11-30 22:53:06 +0000890 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000891 case ISD::SETNE: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000892 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
893 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
894 Op, getI32Imm(~0U));
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000895 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
896 Op, SDOperand(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000897 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000898 case ISD::SETLT: {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000899 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
900 getI32Imm(1)), 0);
901 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
902 Op), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000903 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
904 getI32Imm(31), getI32Imm(31));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000905 }
906 case ISD::SETGT:
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000907 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
908 getI32Imm(1), getI32Imm(31),
909 getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000910 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000911 }
Chris Lattner222adac2005-10-06 19:03:35 +0000912 }
913 }
914
915 bool Inv;
916 unsigned Idx = getCRIdxForSetCC(CC, Inv);
917 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
918 SDOperand IntCR;
919
920 // Force the ccreg into CR7.
921 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
922
Chris Lattner85961d52005-12-06 20:56:18 +0000923 SDOperand InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000924 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
925 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000926
927 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000928 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
929 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000930 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000931 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000932
933 if (!Inv) {
Chris Lattner71d3d502005-11-30 22:53:06 +0000934 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
935 getI32Imm((32-(3-Idx)) & 31),
936 getI32Imm(31), getI32Imm(31));
Chris Lattner222adac2005-10-06 19:03:35 +0000937 } else {
938 SDOperand Tmp =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000939 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
940 getI32Imm((32-(3-Idx)) & 31),
941 getI32Imm(31),getI32Imm(31)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000942 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000943 }
Chris Lattner222adac2005-10-06 19:03:35 +0000944}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000945
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000946
Chris Lattnera5a91b12005-08-17 19:33:03 +0000947// Select - Convert the specified operand from a target-independent to a
948// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000949void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Chris Lattnera5a91b12005-08-17 19:33:03 +0000950 SDNode *N = Op.Val;
Chris Lattner0bbea952005-08-26 20:25:03 +0000951 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000952 N->getOpcode() < PPCISD::FIRST_NUMBER) {
953 Result = Op;
954 return; // Already selected.
955 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000956
957 // If this has already been converted, use it.
958 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000959 if (CGMI != CodeGenMap.end()) {
960 Result = CGMI->second;
961 return;
962 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000963
964 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000965 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000966 case ISD::SETCC:
967 Result = SelectSETCC(Op);
968 return;
Evan Cheng34167212006-02-09 00:37:58 +0000969 case PPCISD::GlobalBaseReg:
970 Result = getGlobalBaseReg();
971 return;
Chris Lattner860e8862005-11-17 07:30:41 +0000972
Chris Lattnere28e40a2005-08-25 00:45:43 +0000973 case ISD::FrameIndex: {
974 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000975 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
976 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Evan Cheng34167212006-02-09 00:37:58 +0000977 if (N->hasOneUse()) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000978 Result = CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
979 getSmallIPtrImm(0));
Evan Cheng34167212006-02-09 00:37:58 +0000980 return;
981 }
982 Result = CodeGenMap[Op] =
Chris Lattnerc08f9022006-06-27 00:04:13 +0000983 SDOperand(CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
984 getSmallIPtrImm(0)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000985 return;
Chris Lattnere28e40a2005-08-25 00:45:43 +0000986 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000987
988 case PPCISD::MFCR: {
989 SDOperand InFlag;
990 Select(InFlag, N->getOperand(1));
991 // Use MFOCRF if supported.
992 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
993 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
994 N->getOperand(0), InFlag), 0);
995 else
996 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
997 CodeGenMap[Op] = Result;
998 return;
999 }
1000
Chris Lattner88add102005-09-28 22:50:24 +00001001 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001002 // FIXME: since this depends on the setting of the carry flag from the srawi
1003 // we should really be making notes about that for the scheduler.
1004 // FIXME: It sure would be nice if we could cheaply recognize the
1005 // srl/add/sra pattern the dag combiner will generate for this as
1006 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +00001007 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001008 if (isInt32Immediate(N->getOperand(1), Imm)) {
Evan Cheng34167212006-02-09 00:37:58 +00001009 SDOperand N0;
1010 Select(N0, N->getOperand(0));
Chris Lattner8784a232005-08-25 17:50:06 +00001011 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001012 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +00001013 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001014 N0, getI32Imm(Log2_32(Imm)));
1015 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001016 SDOperand(Op, 0), SDOperand(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +00001017 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001018 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +00001019 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +00001020 N0, getI32Imm(Log2_32(-Imm)));
Chris Lattner8784a232005-08-25 17:50:06 +00001021 SDOperand PT =
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001022 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1023 SDOperand(Op, 0), SDOperand(Op, 1)),
1024 0);
Evan Cheng34167212006-02-09 00:37:58 +00001025 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +00001026 }
Evan Cheng34167212006-02-09 00:37:58 +00001027 return;
Chris Lattner8784a232005-08-25 17:50:06 +00001028 }
Chris Lattner047b9522005-08-25 22:04:30 +00001029
Chris Lattner237733e2005-09-29 23:33:31 +00001030 // Other cases are autogenerated.
1031 break;
Chris Lattner047b9522005-08-25 22:04:30 +00001032 }
Nate Begemancffc32b2005-08-18 07:30:46 +00001033 case ISD::AND: {
Nate Begeman50fb3c42005-12-24 01:00:15 +00001034 unsigned Imm, Imm2;
Nate Begemancffc32b2005-08-18 07:30:46 +00001035 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1036 // with a mask, emit rlwinm
Chris Lattnerc08f9022006-06-27 00:04:13 +00001037 if (isInt32Immediate(N->getOperand(1), Imm) &&
1038 (isShiftedMask_32(Imm) || isShiftedMask_32(~Imm))) {
Nate Begemancffc32b2005-08-18 07:30:46 +00001039 SDOperand Val;
Nate Begemana6940472005-08-18 18:01:39 +00001040 unsigned SH, MB, ME;
Nate Begemancffc32b2005-08-18 07:30:46 +00001041 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001042 Select(Val, N->getOperand(0).getOperand(0));
Chris Lattner3393e802005-10-25 19:32:37 +00001043 } else if (Imm == 0) {
1044 // AND X, 0 -> 0, not "rlwinm 32".
Evan Cheng34167212006-02-09 00:37:58 +00001045 Select(Result, N->getOperand(1));
1046 return ;
Chris Lattner3393e802005-10-25 19:32:37 +00001047 } else {
Evan Cheng34167212006-02-09 00:37:58 +00001048 Select(Val, N->getOperand(0));
Nate Begemancffc32b2005-08-18 07:30:46 +00001049 isRunOfOnes(Imm, MB, ME);
1050 SH = 0;
1051 }
Evan Cheng34167212006-02-09 00:37:58 +00001052 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1053 getI32Imm(SH), getI32Imm(MB),
1054 getI32Imm(ME));
1055 return;
Nate Begemancffc32b2005-08-18 07:30:46 +00001056 }
Nate Begeman50fb3c42005-12-24 01:00:15 +00001057 // ISD::OR doesn't get all the bitfield insertion fun.
1058 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattnerc08f9022006-06-27 00:04:13 +00001059 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman50fb3c42005-12-24 01:00:15 +00001060 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001061 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +00001062 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001063 Imm = ~(Imm^Imm2);
1064 if (isRunOfOnes(Imm, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001065 SDOperand Tmp1, Tmp2;
1066 Select(Tmp1, N->getOperand(0).getOperand(0));
1067 Select(Tmp2, N->getOperand(0).getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001068 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1069 Tmp1, Tmp2,
1070 getI32Imm(0), getI32Imm(MB),
1071 getI32Imm(ME)), 0);
Evan Cheng34167212006-02-09 00:37:58 +00001072 return;
Nate Begeman50fb3c42005-12-24 01:00:15 +00001073 }
1074 }
Chris Lattner237733e2005-09-29 23:33:31 +00001075
1076 // Other cases are autogenerated.
1077 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001078 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001079 case ISD::OR:
Chris Lattnercccef1c2006-06-27 21:08:52 +00001080 if (N->getValueType(0) == MVT::i32)
1081 if (SDNode *I = SelectBitfieldInsert(N)) {
1082 Result = CodeGenMap[Op] = SDOperand(I, 0);
1083 return;
1084 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001085
Chris Lattner237733e2005-09-29 23:33:31 +00001086 // Other cases are autogenerated.
1087 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001088 case ISD::SHL: {
1089 unsigned Imm, SH, MB, ME;
1090 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001091 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001092 SDOperand Val;
1093 Select(Val, N->getOperand(0).getOperand(0));
1094 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1095 Val, getI32Imm(SH), getI32Imm(MB),
1096 getI32Imm(ME));
1097 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001098 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001099
1100 // Other cases are autogenerated.
1101 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001102 }
1103 case ISD::SRL: {
1104 unsigned Imm, SH, MB, ME;
1105 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001106 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001107 SDOperand Val;
1108 Select(Val, N->getOperand(0).getOperand(0));
1109 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Chris Lattner0949ed52006-05-12 16:29:37 +00001110 Val, getI32Imm(SH), getI32Imm(MB),
Evan Cheng34167212006-02-09 00:37:58 +00001111 getI32Imm(ME));
1112 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001113 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001114
1115 // Other cases are autogenerated.
1116 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001117 }
Chris Lattner13794f52005-08-26 18:46:49 +00001118 case ISD::SELECT_CC: {
1119 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1120
Chris Lattnerc08f9022006-06-27 00:04:13 +00001121 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +00001122 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1123 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1124 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1125 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001126 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1127 // FIXME: Implement this optzn for PPC64.
1128 N->getValueType(0) == MVT::i32) {
Evan Cheng34167212006-02-09 00:37:58 +00001129 SDOperand LHS;
1130 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001131 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001132 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1133 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001134 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1135 SDOperand(Tmp, 0), LHS,
1136 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001137 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001138 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001139
Chris Lattner50ff55c2005-09-01 19:20:44 +00001140 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001141 unsigned BROpc = getBCCForSetCC(CC);
1142
1143 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001144 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001145 if (N->getValueType(0) == MVT::i32)
1146 SelectCCOp = PPC::SELECT_CC_I4;
1147 else if (N->getValueType(0) == MVT::i64)
1148 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +00001149 else if (N->getValueType(0) == MVT::f32)
1150 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001151 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001152 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001153 else
1154 SelectCCOp = PPC::SELECT_CC_VRRC;
1155
Evan Cheng34167212006-02-09 00:37:58 +00001156 SDOperand N2, N3;
1157 Select(N2, N->getOperand(2));
1158 Select(N3, N->getOperand(3));
1159 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1160 N2, N3, getI32Imm(BROpc));
1161 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001162 }
Nate Begeman81e80972006-03-17 01:40:33 +00001163 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001164 SDOperand Chain;
1165 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001166 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1167 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001168 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1169 CondCode, getI32Imm(getBCCForSetCC(CC)),
1170 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001171 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001172 }
Nate Begeman37efe672006-04-22 18:53:45 +00001173 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001174 // FIXME: Should custom lower this.
Nate Begeman37efe672006-04-22 18:53:45 +00001175 SDOperand Chain, Target;
1176 Select(Chain, N->getOperand(0));
1177 Select(Target,N->getOperand(1));
Chris Lattner6b76b962006-06-27 20:46:17 +00001178 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1179 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman37efe672006-04-22 18:53:45 +00001180 Chain), 0);
1181 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1182 return;
1183 }
Chris Lattnercf006312006-06-10 01:15:02 +00001184 // FIXME: These are manually selected because tblgen isn't handling varargs
1185 // nodes correctly.
1186 case PPCISD::BCTRL: MySelect_PPCbctrl(Result, Op); return;
1187 case PPCISD::CALL: MySelect_PPCcall(Result, Op); return;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001188 }
Chris Lattner25dae722005-09-03 00:53:47 +00001189
Evan Cheng34167212006-02-09 00:37:58 +00001190 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001191}
1192
1193
Chris Lattnercf006312006-06-10 01:15:02 +00001194// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1195// correctly.
1196void PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand &Result, SDOperand N) {
1197 SDOperand Chain(0, 0);
1198 SDOperand InFlag(0, 0);
1199 SDNode *ResNode;
1200
1201 bool hasFlag =
1202 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1203
1204 std::vector<SDOperand> Ops;
1205 // Push varargs arguments, including optional flag.
1206 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1207 Select(Chain, N.getOperand(i));
1208 Ops.push_back(Chain);
1209 }
1210
1211 Select(Chain, N.getOperand(0));
1212 Ops.push_back(Chain);
1213
1214 if (hasFlag) {
1215 Select(Chain, N.getOperand(N.getNumOperands()-1));
1216 Ops.push_back(Chain);
1217 }
1218
1219 ResNode = CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag, Ops);
1220 Chain = SDOperand(ResNode, 0);
1221 InFlag = SDOperand(ResNode, 1);
1222 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1223 Chain.ResNo);
1224 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1225 InFlag.ResNo);
1226 Result = SDOperand(ResNode, N.ResNo);
1227 return;
1228}
1229
1230// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1231// correctly.
1232void PPCDAGToDAGISel::MySelect_PPCcall(SDOperand &Result, SDOperand N) {
1233 SDOperand Chain(0, 0);
1234 SDOperand InFlag(0, 0);
1235 SDOperand N1(0, 0);
1236 SDOperand Tmp0(0, 0);
1237 SDNode *ResNode;
1238 Chain = N.getOperand(0);
1239 N1 = N.getOperand(1);
1240
1241 // Pattern: (PPCcall:void (imm:i32):$func)
1242 // Emits: (BLA:void (imm:i32):$func)
1243 // Pattern complexity = 4 cost = 1
1244 if (N1.getOpcode() == ISD::Constant) {
1245 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1246
1247 std::vector<SDOperand> Ops;
1248 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1249
1250 bool hasFlag =
1251 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1252
1253 // Push varargs arguments, not including optional flag.
1254 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1255 Select(Chain, N.getOperand(i));
1256 Ops.push_back(Chain);
1257 }
1258 Select(Chain, N.getOperand(0));
1259 Ops.push_back(Chain);
1260 if (hasFlag) {
1261 Select(Chain, N.getOperand(N.getNumOperands()-1));
1262 Ops.push_back(Chain);
1263 }
1264 ResNode = CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag, Ops);
1265
1266 Chain = SDOperand(ResNode, 0);
1267 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001268 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1269 Chain.ResNo);
1270 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1271 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001272 Result = SDOperand(ResNode, N.ResNo);
1273 return;
1274 }
1275
1276 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1277 // Emits: (BL:void (tglobaladdr:i32):$dst)
1278 // Pattern complexity = 4 cost = 1
1279 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
1280 std::vector<SDOperand> Ops;
1281 Ops.push_back(N1);
1282
1283 bool hasFlag =
1284 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1285
1286 // Push varargs arguments, not including optional flag.
1287 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1288 Select(Chain, N.getOperand(i));
1289 Ops.push_back(Chain);
1290 }
1291 Select(Chain, N.getOperand(0));
1292 Ops.push_back(Chain);
1293 if (hasFlag) {
1294 Select(Chain, N.getOperand(N.getNumOperands()-1));
1295 Ops.push_back(Chain);
1296 }
1297
1298 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1299
1300 Chain = SDOperand(ResNode, 0);
1301 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001302 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1303 Chain.ResNo);
1304 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1305 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001306 Result = SDOperand(ResNode, N.ResNo);
1307 return;
1308 }
1309
1310 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1311 // Emits: (BL:void (texternalsym:i32):$dst)
1312 // Pattern complexity = 4 cost = 1
1313 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1314 std::vector<SDOperand> Ops;
1315 Ops.push_back(N1);
1316
1317 bool hasFlag =
1318 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1319
1320 // Push varargs arguments, not including optional flag.
1321 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1322 Select(Chain, N.getOperand(i));
1323 Ops.push_back(Chain);
1324 }
1325 Select(Chain, N.getOperand(0));
1326 Ops.push_back(Chain);
1327 if (hasFlag) {
1328 Select(Chain, N.getOperand(N.getNumOperands()-1));
1329 Ops.push_back(Chain);
1330 }
1331
1332 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1333
1334 Chain = SDOperand(ResNode, 0);
1335 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001336 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1337 Chain.ResNo);
1338 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1339 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001340 Result = SDOperand(ResNode, N.ResNo);
1341 return;
1342 }
1343 std::cerr << "Cannot yet select: ";
1344 N.Val->dump(CurDAG);
1345 std::cerr << '\n';
1346 abort();
1347}
1348
1349
Nate Begeman1d9d7422005-10-18 00:28:58 +00001350/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001351/// PowerPC-specific DAG, ready for instruction scheduling.
1352///
Evan Chengc4c62572006-03-13 23:20:37 +00001353FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001354 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001355}
1356