blob: a00812017380efea463c8b8f848e979a6e690aa0 [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
Chris Lattnerc08f9022006-06-27 00:04:13 +000066 /// getI64Imm - Return a target constant with the specified value, of type
67 /// i64.
68 inline SDOperand getI64Imm(uint64_t Imm) {
69 return CurDAG->getTargetConstant(Imm, MVT::i64);
70 }
71
72 /// getSmallIPtrImm - Return a target constant of pointer type.
73 inline SDOperand getSmallIPtrImm(unsigned Imm) {
74 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
75 }
76
77
Chris Lattner4416f1a2005-08-19 22:38:53 +000078 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
79 /// base register. Return the virtual register that holds this value.
Chris Lattner9944b762005-08-21 22:31:09 +000080 SDOperand getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000081
82 // Select - Convert the specified operand from a target-independent to a
83 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000084 void Select(SDOperand &Result, SDOperand Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000085
Nate Begeman02b88a42005-08-19 00:38:14 +000086 SDNode *SelectBitfieldInsert(SDNode *N);
87
Chris Lattner2fbb4572005-08-21 18:50:37 +000088 /// SelectCC - Select a comparison of the specified values with the
89 /// specified condition code, returning the CR# of the expression.
90 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
91
Nate Begeman7fd1edd2005-12-19 23:25:09 +000092 /// SelectAddrImm - Returns true if the address N can be represented by
93 /// a base register plus a signed 16-bit displacement [r+imm].
94 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
95
96 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
97 /// represented as an indexed [r+r] operation. Returns false if it can
98 /// be represented by [r+imm], which are preferred.
99 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000100
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000101 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
102 /// represented as an indexed [r+r] operation.
103 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
Chris Lattner9944b762005-08-21 22:31:09 +0000104
Chris Lattnere5ba5802006-03-22 05:26:03 +0000105 /// SelectAddrImmShift - Returns true if the address N can be represented by
106 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
107 /// for use by STD and friends.
108 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
109
Chris Lattnere5d88612006-02-24 02:13:12 +0000110 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
111 /// inline asm expressions.
112 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
113 char ConstraintCode,
114 std::vector<SDOperand> &OutOps,
115 SelectionDAG &DAG) {
116 SDOperand Op0, Op1;
117 switch (ConstraintCode) {
118 default: return true;
119 case 'm': // memory
120 if (!SelectAddrIdx(Op, Op0, Op1))
121 SelectAddrImm(Op, Op0, Op1);
122 break;
123 case 'o': // offsetable
124 if (!SelectAddrImm(Op, Op0, Op1)) {
125 Select(Op0, Op); // r+0.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000126 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000127 }
128 break;
129 case 'v': // not offsetable
130 SelectAddrIdxOnly(Op, Op0, Op1);
131 break;
132 }
133
134 OutOps.push_back(Op0);
135 OutOps.push_back(Op1);
136 return false;
137 }
138
Chris Lattner047b9522005-08-25 22:04:30 +0000139 SDOperand BuildSDIVSequence(SDNode *N);
140 SDOperand BuildUDIVSequence(SDNode *N);
141
Chris Lattnera5a91b12005-08-17 19:33:03 +0000142 /// InstructionSelectBasicBlock - This callback is invoked by
143 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000144 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
145
Chris Lattner4bb18952006-03-16 18:25:23 +0000146 void InsertVRSaveCode(Function &Fn);
147
Chris Lattnera5a91b12005-08-17 19:33:03 +0000148 virtual const char *getPassName() const {
149 return "PowerPC DAG->DAG Pattern Instruction Selection";
150 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000151
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000152 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
153 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000154 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000155 // Should use subtarget info to pick the right hazard recognizer. For
156 // now, always return a PPC970 recognizer.
Chris Lattner88d211f2006-03-12 09:13:49 +0000157 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
158 assert(II && "No InstrInfo?");
159 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000160 }
Chris Lattneraf165382005-09-13 22:03:06 +0000161
162// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000163#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000164
165private:
Chris Lattner222adac2005-10-06 19:03:35 +0000166 SDOperand SelectSETCC(SDOperand Op);
Chris Lattnercf006312006-06-10 01:15:02 +0000167 void MySelect_PPCbctrl(SDOperand &Result, SDOperand N);
168 void MySelect_PPCcall(SDOperand &Result, SDOperand N);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000169 };
170}
171
Chris Lattnerbd937b92005-10-06 18:45:51 +0000172/// InstructionSelectBasicBlock - This callback is invoked by
173/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000174void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000175 DEBUG(BB->dump());
176
177 // The selection process is inherently a bottom-up recursive process (users
178 // select their uses before themselves). Given infinite stack space, we
179 // could just start selecting on the root and traverse the whole graph. In
180 // practice however, this causes us to run out of stack space on large basic
181 // blocks. To avoid this problem, select the entry node, then all its uses,
182 // iteratively instead of recursively.
183 std::vector<SDOperand> Worklist;
184 Worklist.push_back(DAG.getEntryNode());
185
186 // Note that we can do this in the PPC target (scanning forward across token
187 // chain edges) because no nodes ever get folded across these edges. On a
188 // target like X86 which supports load/modify/store operations, this would
189 // have to be more careful.
190 while (!Worklist.empty()) {
191 SDOperand Node = Worklist.back();
192 Worklist.pop_back();
193
Chris Lattnercf01a702005-10-07 22:10:27 +0000194 // Chose from the least deep of the top two nodes.
195 if (!Worklist.empty() &&
196 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
197 std::swap(Worklist.back(), Node);
198
Chris Lattnerbd937b92005-10-06 18:45:51 +0000199 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
200 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
201 CodeGenMap.count(Node)) continue;
202
203 for (SDNode::use_iterator UI = Node.Val->use_begin(),
204 E = Node.Val->use_end(); UI != E; ++UI) {
205 // Scan the values. If this use has a value that is a token chain, add it
206 // to the worklist.
207 SDNode *User = *UI;
208 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
209 if (User->getValueType(i) == MVT::Other) {
210 Worklist.push_back(SDOperand(User, i));
211 break;
212 }
213 }
214
215 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000216 SDOperand Dummy;
217 Select(Dummy, Node);
Chris Lattnerbd937b92005-10-06 18:45:51 +0000218 }
Chris Lattnercf01a702005-10-07 22:10:27 +0000219
Chris Lattnerbd937b92005-10-06 18:45:51 +0000220 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000221 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000222 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Chris Lattnerbd937b92005-10-06 18:45:51 +0000223 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000224 HandleMap.clear();
225 ReplaceMap.clear();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000226 DAG.RemoveDeadNodes();
227
Chris Lattner1877ec92006-03-13 21:52:10 +0000228 // Emit machine code to BB.
Chris Lattnerbd937b92005-10-06 18:45:51 +0000229 ScheduleAndEmitDAG(DAG);
Chris Lattner4bb18952006-03-16 18:25:23 +0000230}
231
232/// InsertVRSaveCode - Once the entire function has been instruction selected,
233/// all virtual registers are created and all machine instructions are built,
234/// check to see if we need to save/restore VRSAVE. If so, do it.
235void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000236 // Check to see if this function uses vector registers, which means we have to
237 // save and restore the VRSAVE register and update it with the regs we use.
238 //
239 // In this case, there will be virtual registers of vector type type created
240 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000241 MachineFunction &Fn = MachineFunction::get(&F);
242 SSARegMap *RegMap = Fn.getSSARegMap();
Chris Lattner1877ec92006-03-13 21:52:10 +0000243 bool HasVectorVReg = false;
244 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
Chris Lattnera08610c2006-03-14 17:56:49 +0000245 e = RegMap->getLastVirtReg()+1; i != e; ++i)
Chris Lattner1877ec92006-03-13 21:52:10 +0000246 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
247 HasVectorVReg = true;
248 break;
249 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000250 if (!HasVectorVReg) return; // nothing to do.
251
Chris Lattner1877ec92006-03-13 21:52:10 +0000252 // If we have a vector register, we want to emit code into the entry and exit
253 // blocks to save and restore the VRSAVE register. We do this here (instead
254 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
255 //
256 // 1. This (trivially) reduces the load on the register allocator, by not
257 // having to represent the live range of the VRSAVE register.
258 // 2. This (more significantly) allows us to create a temporary virtual
259 // register to hold the saved VRSAVE value, allowing this temporary to be
260 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000261
262 // Create two vregs - one to hold the VRSAVE register that is live-in to the
263 // function and one for the value after having bits or'd into it.
264 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
265 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
266
267 MachineBasicBlock &EntryBB = *Fn.begin();
268 // Emit the following code into the entry block:
269 // InVRSAVE = MFVRSAVE
270 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
271 // MTVRSAVE UpdatedVRSAVE
272 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
273 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
274 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
275 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
276
277 // Find all return blocks, outputting a restore in each epilog.
278 const TargetInstrInfo &TII = *TM.getInstrInfo();
279 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
280 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
281 IP = BB->end(); --IP;
282
283 // Skip over all terminator instructions, which are part of the return
284 // sequence.
285 MachineBasicBlock::iterator I2 = IP;
286 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
287 IP = I2;
288
289 // Emit: MTVRSAVE InVRSave
290 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
291 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000292 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000293}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000294
Chris Lattner4bb18952006-03-16 18:25:23 +0000295
Chris Lattner4416f1a2005-08-19 22:38:53 +0000296/// getGlobalBaseReg - Output the instructions required to put the
297/// base address to use for accessing globals into a register.
298///
Nate Begeman1d9d7422005-10-18 00:28:58 +0000299SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000300 if (!GlobalBaseReg) {
301 // Insert the set of GlobalBaseReg into the first MBB of the function
302 MachineBasicBlock &FirstMBB = BB->getParent()->front();
303 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
304 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000305
306 if (PPCLowering.getPointerTy() == MVT::i32)
307 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
308 else
309 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
310
Chris Lattner4416f1a2005-08-19 22:38:53 +0000311 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
312 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
313 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000314 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy());
315}
316
317/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
318/// or 64-bit immediate, and if the value can be accurately represented as a
319/// sign extension from a 16-bit value. If so, this returns true and the
320/// immediate.
321static bool isIntS16Immediate(SDNode *N, short &Imm) {
322 if (N->getOpcode() != ISD::Constant)
323 return false;
324
325 Imm = (short)cast<ConstantSDNode>(N)->getValue();
326 if (N->getValueType(0) == MVT::i32)
327 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
328 else
329 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
330}
331
332static bool isIntS16Immediate(SDOperand Op, short &Imm) {
333 return isIntS16Immediate(Op.Val, Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000334}
335
336
Chris Lattnerc08f9022006-06-27 00:04:13 +0000337/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
338/// operand. If so Imm will receive the 32-bit value.
339static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
340 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Nate Begeman0f3257a2005-08-18 05:00:13 +0000341 Imm = cast<ConstantSDNode>(N)->getValue();
342 return true;
343 }
344 return false;
345}
346
Chris Lattnerc08f9022006-06-27 00:04:13 +0000347/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
348/// operand. If so Imm will receive the 64-bit value.
349static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
350 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
351 Imm = cast<ConstantSDNode>(N)->getValue();
352 return true;
353 }
354 return false;
355}
356
357// isInt32Immediate - This method tests to see if a constant operand.
358// If so Imm will receive the 32 bit value.
359static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
360 return isInt32Immediate(N.Val, Imm);
361}
362
363
364// isOpcWithIntImmediate - This method tests to see if the node is a specific
365// opcode and that it has a immediate integer right operand.
366// If so Imm will receive the 32 bit value.
367static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
368 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
369}
370
371
Nate Begemancffc32b2005-08-18 07:30:46 +0000372// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
373// any number of 0s on either side. The 1s are allowed to wrap from LSB to
374// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
375// not, since all 1s are not contiguous.
376static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
377 if (isShiftedMask_32(Val)) {
378 // look for the first non-zero bit
379 MB = CountLeadingZeros_32(Val);
380 // look for the first zero bit after the run of ones
381 ME = CountLeadingZeros_32((Val - 1) ^ Val);
382 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000383 } else {
384 Val = ~Val; // invert mask
385 if (isShiftedMask_32(Val)) {
386 // effectively look for the first zero bit
387 ME = CountLeadingZeros_32(Val) - 1;
388 // effectively look for the first one bit after the run of zeros
389 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
390 return true;
391 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000392 }
393 // no run present
394 return false;
395}
396
Chris Lattner65a419a2005-10-09 05:36:17 +0000397// isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
Nate Begemancffc32b2005-08-18 07:30:46 +0000398// and mask opcode and mask operation.
399static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
400 unsigned &SH, unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000401 // Don't even go down this path for i64, since different logic will be
402 // necessary for rldicl/rldicr/rldimi.
403 if (N->getValueType(0) != MVT::i32)
404 return false;
405
Nate Begemancffc32b2005-08-18 07:30:46 +0000406 unsigned Shift = 32;
407 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
408 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000409 if (N->getNumOperands() != 2 ||
Chris Lattnerc08f9022006-06-27 00:04:13 +0000410 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000411 return false;
412
413 if (Opcode == ISD::SHL) {
414 // apply shift left to mask if it comes first
415 if (IsShiftMask) Mask = Mask << Shift;
416 // determine which bits are made indeterminant by shift
417 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000418 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000419 // apply shift right to mask if it comes first
420 if (IsShiftMask) Mask = Mask >> Shift;
421 // determine which bits are made indeterminant by shift
422 Indeterminant = ~(0xFFFFFFFFu >> Shift);
423 // adjust for the left rotate
424 Shift = 32 - Shift;
425 } else {
426 return false;
427 }
428
429 // if the mask doesn't intersect any Indeterminant bits
430 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000431 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000432 // make sure the mask is still a mask (wrap arounds may not be)
433 return isRunOfOnes(Mask, MB, ME);
434 }
435 return false;
436}
437
Nate Begeman02b88a42005-08-19 00:38:14 +0000438/// SelectBitfieldInsert - turn an or of two masked values into
439/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000440SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Nate Begeman02b88a42005-08-19 00:38:14 +0000441 SDOperand Op0 = N->getOperand(0);
442 SDOperand Op1 = N->getOperand(1);
443
Nate Begeman77f361f2006-05-07 00:23:38 +0000444 uint64_t LKZ, LKO, RKZ, RKO;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000445 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
446 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000447
Nate Begeman4667f2c2006-05-08 17:38:32 +0000448 unsigned TargetMask = LKZ;
449 unsigned InsertMask = RKZ;
450
451 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
452 unsigned Op0Opc = Op0.getOpcode();
453 unsigned Op1Opc = Op1.getOpcode();
454 unsigned Value, SH = 0;
455 TargetMask = ~TargetMask;
456 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000457
Nate Begeman4667f2c2006-05-08 17:38:32 +0000458 // If the LHS has a foldable shift and the RHS does not, then swap it to the
459 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000460 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
461 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
462 Op0.getOperand(0).getOpcode() == ISD::SRL) {
463 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
464 Op1.getOperand(0).getOpcode() != ISD::SRL) {
465 std::swap(Op0, Op1);
466 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000467 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000468 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000469 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000470 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
471 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
472 Op1.getOperand(0).getOpcode() != ISD::SRL) {
473 std::swap(Op0, Op1);
474 std::swap(Op0Opc, Op1Opc);
475 std::swap(TargetMask, InsertMask);
476 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000477 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000478
479 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000480 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000481 SDOperand Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000482 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000483
484 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000485 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000486 Op1 = Op1.getOperand(0);
487 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
488 }
489 if (Op1Opc == ISD::AND) {
490 unsigned SHOpc = Op1.getOperand(0).getOpcode();
491 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000492 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000493 Op1 = Op1.getOperand(0).getOperand(0);
494 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
495 } else {
496 Op1 = Op1.getOperand(0);
497 }
498 }
499
500 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
501 Select(Tmp1, Tmp3);
502 Select(Tmp2, Op1);
Chris Lattner0949ed52006-05-12 16:29:37 +0000503 SH &= 31;
Nate Begeman77f361f2006-05-07 00:23:38 +0000504 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
505 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
Nate Begeman02b88a42005-08-19 00:38:14 +0000506 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000507 }
508 return 0;
509}
510
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000511/// SelectAddrImm - Returns true if the address N can be represented by
512/// a base register plus a signed 16-bit displacement [r+imm].
513bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
514 SDOperand &Base) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000515 // If this can be more profitably realized as r+r, fail.
516 if (SelectAddrIdx(N, Disp, Base))
517 return false;
518
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000519 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000520 short imm = 0;
521 if (isIntS16Immediate(N.getOperand(1), imm)) {
522 Disp = getI32Imm((int)imm & 0xFFFF);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000523 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000524 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattner9944b762005-08-21 22:31:09 +0000525 } else {
Evan Cheng7564e0b2006-02-05 08:45:01 +0000526 Base = N.getOperand(0);
Chris Lattner9944b762005-08-21 22:31:09 +0000527 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000528 return true; // [r+i]
529 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000530 // Match LOAD (ADD (X, Lo(G))).
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000531 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
Chris Lattner4f0f86d2005-11-17 18:02:16 +0000532 && "Cannot handle constant offsets yet!");
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000533 Disp = N.getOperand(1).getOperand(0); // The global address.
534 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000535 Disp.getOpcode() == ISD::TargetConstantPool ||
536 Disp.getOpcode() == ISD::TargetJumpTable);
Evan Cheng7564e0b2006-02-05 08:45:01 +0000537 Base = N.getOperand(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000538 return true; // [&g+r]
Chris Lattner9944b762005-08-21 22:31:09 +0000539 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000540 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000541 short imm = 0;
542 if (isIntS16Immediate(N.getOperand(1), imm)) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000543 // If this is an or of disjoint bitfields, we can codegen this as an add
544 // (for better address arithmetic) if the LHS and RHS of the OR are
545 // provably disjoint.
546 uint64_t LHSKnownZero, LHSKnownOne;
547 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
548 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000549 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000550 // If all of the bits are known zero on the LHS or RHS, the add won't
551 // carry.
552 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000553 Disp = getI32Imm((int)imm & 0xFFFF);
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000554 return true;
555 }
556 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000557 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
558 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000559
Chris Lattnerd9796442006-03-20 22:38:22 +0000560 // If this address fits entirely in a 16-bit sext immediate field, codegen
561 // this as "d, 0"
Chris Lattnerc08f9022006-06-27 00:04:13 +0000562 short Imm;
563 if (isIntS16Immediate(CN, Imm)) {
564 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
565 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
Chris Lattnerd9796442006-03-20 22:38:22 +0000566 return true;
567 }
Chris Lattnerc08f9022006-06-27 00:04:13 +0000568
569 // FIXME: Handle small sext constant offsets in PPC64 mode also!
570 if (CN->getValueType(0) == MVT::i32) {
571 int Addr = (int)CN->getValue();
Chris Lattnerd9796442006-03-20 22:38:22 +0000572
Chris Lattnerc08f9022006-06-27 00:04:13 +0000573 // Otherwise, break this down into an LIS + disp.
574 Disp = getI32Imm((short)Addr);
575 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
576 return true;
577 }
Chris Lattner9944b762005-08-21 22:31:09 +0000578 }
Chris Lattnerd9796442006-03-20 22:38:22 +0000579
Chris Lattnerc08f9022006-06-27 00:04:13 +0000580 Disp = getSmallIPtrImm(0);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000581 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000582 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Nate Begeman28a6b022005-12-10 02:36:00 +0000583 else
Evan Cheng7564e0b2006-02-05 08:45:01 +0000584 Base = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000585 return true; // [r+0]
Chris Lattner9944b762005-08-21 22:31:09 +0000586}
Chris Lattnera5a91b12005-08-17 19:33:03 +0000587
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000588/// SelectAddrIdx - Given the specified addressed, check to see if it can be
589/// represented as an indexed [r+r] operation. Returns false if it can
590/// be represented by [r+imm], which are preferred.
591bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
592 SDOperand &Index) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000593 short imm = 0;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000594 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000595 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000596 return false; // r+i
597 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
598 return false; // r+i
599
Evan Cheng7564e0b2006-02-05 08:45:01 +0000600 Base = N.getOperand(0);
601 Index = N.getOperand(1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000602 return true;
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000603 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000604 if (isIntS16Immediate(N.getOperand(1), imm))
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000605 return false; // r+i can fold it if we can.
606
607 // If this is an or of disjoint bitfields, we can codegen this as an add
608 // (for better address arithmetic) if the LHS and RHS of the OR are provably
609 // disjoint.
610 uint64_t LHSKnownZero, LHSKnownOne;
611 uint64_t RHSKnownZero, RHSKnownOne;
612 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
613 LHSKnownZero, LHSKnownOne);
614
615 if (LHSKnownZero) {
616 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
617 RHSKnownZero, RHSKnownOne);
618 // If all of the bits are known zero on the LHS or RHS, the add won't
619 // carry.
620 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
621 Base = N.getOperand(0);
622 Index = N.getOperand(1);
623 return true;
624 }
625 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000626 }
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000627
628 return false;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000629}
630
631/// SelectAddrIdxOnly - Given the specified addressed, force it to be
632/// represented as an indexed [r+r] operation.
633bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
634 SDOperand &Index) {
Chris Lattner0f6ab6f2006-03-01 07:14:48 +0000635 // Check to see if we can easily represent this as an [r+r] address. This
636 // will fail if it thinks that the address is more profitably represented as
637 // reg+imm, e.g. where imm = 0.
Chris Lattner54e869e2006-03-24 17:58:06 +0000638 if (SelectAddrIdx(N, Base, Index))
639 return true;
640
641 // If the operand is an addition, always emit this as [r+r], since this is
642 // better (for code size, and execution, as the memop does the add for free)
643 // than emitting an explicit add.
644 if (N.getOpcode() == ISD::ADD) {
645 Base = N.getOperand(0);
646 Index = N.getOperand(1);
647 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000648 }
Chris Lattner54e869e2006-03-24 17:58:06 +0000649
650 // Otherwise, do it the hard way, using R0 as the base register.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000651 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
Chris Lattner54e869e2006-03-24 17:58:06 +0000652 Index = N;
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000653 return true;
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000654}
655
Chris Lattnere5ba5802006-03-22 05:26:03 +0000656/// SelectAddrImmShift - Returns true if the address N can be represented by
657/// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
658/// for use by STD and friends.
659bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
660 SDOperand &Base) {
661 // If this can be more profitably realized as r+r, fail.
662 if (SelectAddrIdx(N, Disp, Base))
663 return false;
664
665 if (N.getOpcode() == ISD::ADD) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000666 short imm = 0;
667 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
668 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000669 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000670 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000671 } else {
672 Base = N.getOperand(0);
673 }
674 return true; // [r+i]
675 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
676 // Match LOAD (ADD (X, Lo(G))).
677 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
678 && "Cannot handle constant offsets yet!");
679 Disp = N.getOperand(1).getOperand(0); // The global address.
680 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Nate Begeman37efe672006-04-22 18:53:45 +0000681 Disp.getOpcode() == ISD::TargetConstantPool ||
682 Disp.getOpcode() == ISD::TargetJumpTable);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000683 Base = N.getOperand(0);
684 return true; // [&g+r]
685 }
686 } else if (N.getOpcode() == ISD::OR) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000687 short imm = 0;
688 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000689 // If this is an or of disjoint bitfields, we can codegen this as an add
690 // (for better address arithmetic) if the LHS and RHS of the OR are
691 // provably disjoint.
692 uint64_t LHSKnownZero, LHSKnownOne;
693 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
694 LHSKnownZero, LHSKnownOne);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000695 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
Chris Lattnere5ba5802006-03-22 05:26:03 +0000696 // If all of the bits are known zero on the LHS or RHS, the add won't
697 // carry.
698 Base = N.getOperand(0);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000699 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000700 return true;
701 }
702 }
703 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
704 // Loading from a constant address.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000705
706 // If this address fits entirely in a 14-bit sext immediate field, codegen
707 // this as "d, 0"
708 short Imm;
709 if (isIntS16Immediate(CN, Imm)) {
710 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
711 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
712 return true;
713 }
714
715 // FIXME: Handle small sext constant offsets in PPC64 mode also!
716 if (CN->getValueType(0) == MVT::i32) {
717 int Addr = (int)CN->getValue();
Chris Lattnere5ba5802006-03-22 05:26:03 +0000718
719 // Otherwise, break this down into an LIS + disp.
720 Disp = getI32Imm((short)Addr >> 2);
721 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
722 return true;
723 }
724 }
725
Chris Lattnerc08f9022006-06-27 00:04:13 +0000726 Disp = getSmallIPtrImm(0);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000727 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
Chris Lattnerc08f9022006-06-27 00:04:13 +0000728 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
Chris Lattnere5ba5802006-03-22 05:26:03 +0000729 else
730 Base = N;
731 return true; // [r+0]
732}
733
734
Chris Lattner2fbb4572005-08-21 18:50:37 +0000735/// SelectCC - Select a comparison of the specified values with the specified
736/// condition code, returning the CR# of the expression.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000737SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
738 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000739 // Always select the LHS.
Evan Cheng34167212006-02-09 00:37:58 +0000740 Select(LHS, LHS);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000741 unsigned Opc;
742
743 if (LHS.getValueType() == MVT::i32) {
744 unsigned Imm, Opc;
745 if (ISD::isUnsignedIntSetCC(CC)) {
746 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
747 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
748 getI32Imm(Imm & 0xFFFF)), 0);
749 Opc = PPC::CMPLW;
750 } else {
751 short SImm;
752 if (isIntS16Immediate(RHS, SImm))
753 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
754 getI32Imm((int)SImm & 0xFFFF)),
755 0);
756 Opc = PPC::CMPW;
757 }
758 } else if (LHS.getValueType() == MVT::i64) {
759 uint64_t Imm;
760 unsigned Opc;
761 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:
Evan Cheng34167212006-02-09 00:37:58 +00001080 if (SDNode *I = SelectBitfieldInsert(N)) {
1081 Result = CodeGenMap[Op] = SDOperand(I, 0);
1082 return;
1083 }
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001084
Chris Lattner237733e2005-09-29 23:33:31 +00001085 // Other cases are autogenerated.
1086 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001087 case ISD::SHL: {
1088 unsigned Imm, SH, MB, ME;
1089 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001090 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001091 SDOperand Val;
1092 Select(Val, N->getOperand(0).getOperand(0));
1093 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1094 Val, getI32Imm(SH), getI32Imm(MB),
1095 getI32Imm(ME));
1096 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001097 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001098
1099 // Other cases are autogenerated.
1100 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001101 }
1102 case ISD::SRL: {
1103 unsigned Imm, SH, MB, ME;
1104 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001105 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Evan Cheng34167212006-02-09 00:37:58 +00001106 SDOperand Val;
1107 Select(Val, N->getOperand(0).getOperand(0));
1108 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
Chris Lattner0949ed52006-05-12 16:29:37 +00001109 Val, getI32Imm(SH), getI32Imm(MB),
Evan Cheng34167212006-02-09 00:37:58 +00001110 getI32Imm(ME));
1111 return;
Nate Begeman8d948322005-10-19 01:12:32 +00001112 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001113
1114 // Other cases are autogenerated.
1115 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001116 }
Chris Lattner13794f52005-08-26 18:46:49 +00001117 case ISD::SELECT_CC: {
1118 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1119
Chris Lattnerc08f9022006-06-27 00:04:13 +00001120 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +00001121 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1122 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1123 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1124 if (N1C->isNullValue() && N3C->isNullValue() &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001125 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1126 // FIXME: Implement this optzn for PPC64.
1127 N->getValueType(0) == MVT::i32) {
Evan Cheng34167212006-02-09 00:37:58 +00001128 SDOperand LHS;
1129 Select(LHS, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001130 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001131 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1132 LHS, getI32Imm(~0U));
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001133 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1134 SDOperand(Tmp, 0), LHS,
1135 SDOperand(Tmp, 1));
Evan Cheng34167212006-02-09 00:37:58 +00001136 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001137 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001138
Chris Lattner50ff55c2005-09-01 19:20:44 +00001139 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001140 unsigned BROpc = getBCCForSetCC(CC);
1141
1142 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001143 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001144 if (N->getValueType(0) == MVT::i32)
1145 SelectCCOp = PPC::SELECT_CC_I4;
1146 else if (N->getValueType(0) == MVT::i64)
1147 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +00001148 else if (N->getValueType(0) == MVT::f32)
1149 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001150 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001151 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001152 else
1153 SelectCCOp = PPC::SELECT_CC_VRRC;
1154
Evan Cheng34167212006-02-09 00:37:58 +00001155 SDOperand N2, N3;
1156 Select(N2, N->getOperand(2));
1157 Select(N3, N->getOperand(3));
1158 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1159 N2, N3, getI32Imm(BROpc));
1160 return;
Chris Lattner13794f52005-08-26 18:46:49 +00001161 }
Nate Begeman81e80972006-03-17 01:40:33 +00001162 case ISD::BR_CC: {
Evan Cheng34167212006-02-09 00:37:58 +00001163 SDOperand Chain;
1164 Select(Chain, N->getOperand(0));
Chris Lattner2fbb4572005-08-21 18:50:37 +00001165 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1166 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
Nate Begeman81e80972006-03-17 01:40:33 +00001167 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1168 CondCode, getI32Imm(getBCCForSetCC(CC)),
1169 N->getOperand(4), Chain);
Evan Cheng34167212006-02-09 00:37:58 +00001170 return;
Chris Lattner2fbb4572005-08-21 18:50:37 +00001171 }
Nate Begeman37efe672006-04-22 18:53:45 +00001172 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001173 // FIXME: Should custom lower this.
Nate Begeman37efe672006-04-22 18:53:45 +00001174 SDOperand Chain, Target;
1175 Select(Chain, N->getOperand(0));
1176 Select(Target,N->getOperand(1));
1177 Chain = SDOperand(CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Target,
1178 Chain), 0);
1179 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1180 return;
1181 }
Chris Lattnercf006312006-06-10 01:15:02 +00001182 // FIXME: These are manually selected because tblgen isn't handling varargs
1183 // nodes correctly.
1184 case PPCISD::BCTRL: MySelect_PPCbctrl(Result, Op); return;
1185 case PPCISD::CALL: MySelect_PPCcall(Result, Op); return;
Chris Lattnera5a91b12005-08-17 19:33:03 +00001186 }
Chris Lattner25dae722005-09-03 00:53:47 +00001187
Evan Cheng34167212006-02-09 00:37:58 +00001188 SelectCode(Result, Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001189}
1190
1191
Chris Lattnercf006312006-06-10 01:15:02 +00001192// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1193// correctly.
1194void PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand &Result, SDOperand N) {
1195 SDOperand Chain(0, 0);
1196 SDOperand InFlag(0, 0);
1197 SDNode *ResNode;
1198
1199 bool hasFlag =
1200 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1201
1202 std::vector<SDOperand> Ops;
1203 // Push varargs arguments, including optional flag.
1204 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1205 Select(Chain, N.getOperand(i));
1206 Ops.push_back(Chain);
1207 }
1208
1209 Select(Chain, N.getOperand(0));
1210 Ops.push_back(Chain);
1211
1212 if (hasFlag) {
1213 Select(Chain, N.getOperand(N.getNumOperands()-1));
1214 Ops.push_back(Chain);
1215 }
1216
1217 ResNode = CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag, Ops);
1218 Chain = SDOperand(ResNode, 0);
1219 InFlag = SDOperand(ResNode, 1);
1220 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1221 Chain.ResNo);
1222 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1223 InFlag.ResNo);
1224 Result = SDOperand(ResNode, N.ResNo);
1225 return;
1226}
1227
1228// FIXME: This is manually selected because tblgen isn't handling varargs nodes
1229// correctly.
1230void PPCDAGToDAGISel::MySelect_PPCcall(SDOperand &Result, SDOperand N) {
1231 SDOperand Chain(0, 0);
1232 SDOperand InFlag(0, 0);
1233 SDOperand N1(0, 0);
1234 SDOperand Tmp0(0, 0);
1235 SDNode *ResNode;
1236 Chain = N.getOperand(0);
1237 N1 = N.getOperand(1);
1238
1239 // Pattern: (PPCcall:void (imm:i32):$func)
1240 // Emits: (BLA:void (imm:i32):$func)
1241 // Pattern complexity = 4 cost = 1
1242 if (N1.getOpcode() == ISD::Constant) {
1243 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1244
1245 std::vector<SDOperand> Ops;
1246 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1247
1248 bool hasFlag =
1249 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1250
1251 // Push varargs arguments, not including optional flag.
1252 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1253 Select(Chain, N.getOperand(i));
1254 Ops.push_back(Chain);
1255 }
1256 Select(Chain, N.getOperand(0));
1257 Ops.push_back(Chain);
1258 if (hasFlag) {
1259 Select(Chain, N.getOperand(N.getNumOperands()-1));
1260 Ops.push_back(Chain);
1261 }
1262 ResNode = CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag, Ops);
1263
1264 Chain = SDOperand(ResNode, 0);
1265 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001266 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1267 Chain.ResNo);
1268 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1269 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001270 Result = SDOperand(ResNode, N.ResNo);
1271 return;
1272 }
1273
1274 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1275 // Emits: (BL:void (tglobaladdr:i32):$dst)
1276 // Pattern complexity = 4 cost = 1
1277 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
1278 std::vector<SDOperand> Ops;
1279 Ops.push_back(N1);
1280
1281 bool hasFlag =
1282 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1283
1284 // Push varargs arguments, not including optional flag.
1285 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1286 Select(Chain, N.getOperand(i));
1287 Ops.push_back(Chain);
1288 }
1289 Select(Chain, N.getOperand(0));
1290 Ops.push_back(Chain);
1291 if (hasFlag) {
1292 Select(Chain, N.getOperand(N.getNumOperands()-1));
1293 Ops.push_back(Chain);
1294 }
1295
1296 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1297
1298 Chain = SDOperand(ResNode, 0);
1299 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001300 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1301 Chain.ResNo);
1302 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1303 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001304 Result = SDOperand(ResNode, N.ResNo);
1305 return;
1306 }
1307
1308 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1309 // Emits: (BL:void (texternalsym:i32):$dst)
1310 // Pattern complexity = 4 cost = 1
1311 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1312 std::vector<SDOperand> Ops;
1313 Ops.push_back(N1);
1314
1315 bool hasFlag =
1316 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1317
1318 // Push varargs arguments, not including optional flag.
1319 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1320 Select(Chain, N.getOperand(i));
1321 Ops.push_back(Chain);
1322 }
1323 Select(Chain, N.getOperand(0));
1324 Ops.push_back(Chain);
1325 if (hasFlag) {
1326 Select(Chain, N.getOperand(N.getNumOperands()-1));
1327 Ops.push_back(Chain);
1328 }
1329
1330 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1331
1332 Chain = SDOperand(ResNode, 0);
1333 InFlag = SDOperand(ResNode, 1);
Chris Lattnerc08f9022006-06-27 00:04:13 +00001334 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1335 Chain.ResNo);
1336 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1337 InFlag.ResNo);
Chris Lattnercf006312006-06-10 01:15:02 +00001338 Result = SDOperand(ResNode, N.ResNo);
1339 return;
1340 }
1341 std::cerr << "Cannot yet select: ";
1342 N.Val->dump(CurDAG);
1343 std::cerr << '\n';
1344 abort();
1345}
1346
1347
Nate Begeman1d9d7422005-10-18 00:28:58 +00001348/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001349/// PowerPC-specific DAG, ready for instruction scheduling.
1350///
Evan Chengc4c62572006-03-13 23:20:37 +00001351FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001352 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001353}
1354