blob: a86604b1aa3a944cf1bda3c89756b30a89a4da1e [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5a91b12005-08-17 19:33:03 +00007//
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 Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "ppc-codegen"
Chris Lattner26689592005-10-14 23:51:18 +000016#include "PPC.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000017#include "PPCPredicates.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
19#include "PPCISelLowering.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000020#include "PPCHazardRecognizers.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/Target/TargetOptions.h"
Chris Lattner2fe76e52005-08-25 04:47:18 +000027#include "llvm/Constants.h"
Chris Lattner4416f1a2005-08-19 22:38:53 +000028#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000029#include "llvm/Intrinsics.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/MathExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000032#include "llvm/Support/Compiler.h"
Chris Lattnera5a91b12005-08-17 19:33:03 +000033using namespace llvm;
34
35namespace {
Chris Lattnera5a91b12005-08-17 19:33:03 +000036 //===--------------------------------------------------------------------===//
Nate Begeman1d9d7422005-10-18 00:28:58 +000037 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattnera5a91b12005-08-17 19:33:03 +000038 /// instructions for SelectionDAG operations.
39 ///
Chris Lattner2a41a982006-06-28 22:00:36 +000040 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
Chris Lattner4bb18952006-03-16 18:25:23 +000041 PPCTargetMachine &TM;
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000042 PPCTargetLowering &PPCLowering;
Evan Cheng152b7e12007-10-23 06:42:42 +000043 const PPCSubtarget &PPCSubTarget;
Chris Lattner4416f1a2005-08-19 22:38:53 +000044 unsigned GlobalBaseReg;
Chris Lattnera5a91b12005-08-17 19:33:03 +000045 public:
Dan Gohman1002c022008-07-07 18:00:37 +000046 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman79ce2762009-01-15 19:20:50 +000047 : SelectionDAGISel(tm), TM(tm),
Evan Cheng152b7e12007-10-23 06:42:42 +000048 PPCLowering(*TM.getTargetLowering()),
49 PPCSubTarget(*TM.getSubtargetImpl()) {}
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.
Dan Gohman475871a2008-07-27 21:46:04 +000062 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnera5a91b12005-08-17 19:33:03 +000063 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.
Dan Gohman475871a2008-07-27 21:46:04 +000068 inline SDValue getI64Imm(uint64_t Imm) {
Chris Lattnerc08f9022006-06-27 00:04:13 +000069 return CurDAG->getTargetConstant(Imm, MVT::i64);
70 }
71
72 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +000073 inline SDValue getSmallIPtrImm(unsigned Imm) {
Chris Lattnerc08f9022006-06-27 00:04:13 +000074 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
75 }
76
Nate Begemanf42f1332006-09-22 05:01:56 +000077 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
78 /// with any number of 0s on either side. The 1s are allowed to wrap from
79 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
80 /// 0x0F0F0000 is not, since all 1s are not contiguous.
81 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
82
83
84 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
85 /// rotate and mask opcode and mask operation.
86 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
87 unsigned &SH, unsigned &MB, unsigned &ME);
Chris Lattnerc08f9022006-06-27 00:04:13 +000088
Chris Lattner4416f1a2005-08-19 22:38:53 +000089 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
90 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +000091 SDNode *getGlobalBaseReg();
Chris Lattnera5a91b12005-08-17 19:33:03 +000092
93 // Select - Convert the specified operand from a target-independent to a
94 // target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +000095 SDNode *Select(SDValue Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +000096
Nate Begeman02b88a42005-08-19 00:38:14 +000097 SDNode *SelectBitfieldInsert(SDNode *N);
98
Chris Lattner2fbb4572005-08-21 18:50:37 +000099 /// SelectCC - Select a comparison of the specified values with the
100 /// specified condition code, returning the CR# of the expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000102
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000103 /// SelectAddrImm - Returns true if the address N can be represented by
104 /// a base register plus a signed 16-bit displacement [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000105 bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp,
106 SDValue &Base) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000107 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
108 }
Chris Lattner74531e42006-11-16 00:41:37 +0000109
110 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
111 /// immediate field. Because preinc imms have already been validated, just
112 /// accept it.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const {
Chris Lattner74531e42006-11-16 00:41:37 +0000114 Out = N;
115 return true;
116 }
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000117
118 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
119 /// represented as an indexed [r+r] operation. Returns false if it can
120 /// be represented by [r+imm], which are preferred.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base,
122 SDValue &Index) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000123 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
124 }
Nate Begemanf43a3ca2005-11-30 08:22:07 +0000125
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000126 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
127 /// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000128 bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base,
129 SDValue &Index) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000130 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
131 }
Chris Lattner9944b762005-08-21 22:31:09 +0000132
Chris Lattnere5ba5802006-03-22 05:26:03 +0000133 /// SelectAddrImmShift - Returns true if the address N can be represented by
134 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
135 /// for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp,
137 SDValue &Base) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000138 return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
139 }
140
Chris Lattnere5d88612006-02-24 02:13:12 +0000141 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
142 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000143 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnere5d88612006-02-24 02:13:12 +0000144 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000145 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000146 SDValue Op0, Op1;
Chris Lattnere5d88612006-02-24 02:13:12 +0000147 switch (ConstraintCode) {
148 default: return true;
149 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +0000150 if (!SelectAddrIdx(Op, Op, Op0, Op1))
151 SelectAddrImm(Op, Op, Op0, Op1);
Chris Lattnere5d88612006-02-24 02:13:12 +0000152 break;
153 case 'o': // offsetable
Evan Cheng0d538262006-11-08 20:34:28 +0000154 if (!SelectAddrImm(Op, Op, Op0, Op1)) {
Evan Cheng6da2f322006-08-26 01:07:58 +0000155 Op0 = Op;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000156 Op1 = getSmallIPtrImm(0);
Chris Lattnere5d88612006-02-24 02:13:12 +0000157 }
158 break;
159 case 'v': // not offsetable
Evan Cheng0d538262006-11-08 20:34:28 +0000160 SelectAddrIdxOnly(Op, Op, Op0, Op1);
Chris Lattnere5d88612006-02-24 02:13:12 +0000161 break;
162 }
163
164 OutOps.push_back(Op0);
165 OutOps.push_back(Op1);
166 return false;
167 }
168
Dan Gohman475871a2008-07-27 21:46:04 +0000169 SDValue BuildSDIVSequence(SDNode *N);
170 SDValue BuildUDIVSequence(SDNode *N);
Chris Lattner047b9522005-08-25 22:04:30 +0000171
Evan Chengdb8d56b2008-06-30 20:45:06 +0000172 /// InstructionSelect - This callback is invoked by
Chris Lattnera5a91b12005-08-17 19:33:03 +0000173 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000174 virtual void InstructionSelect();
Chris Lattnerbd937b92005-10-06 18:45:51 +0000175
Chris Lattner4bb18952006-03-16 18:25:23 +0000176 void InsertVRSaveCode(Function &Fn);
177
Chris Lattnera5a91b12005-08-17 19:33:03 +0000178 virtual const char *getPassName() const {
179 return "PowerPC DAG->DAG Pattern Instruction Selection";
180 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000181
Chris Lattnerc04ba7a2006-05-16 23:54:25 +0000182 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
183 /// this target when scheduling the DAG.
Chris Lattnerb0d21ef2006-03-08 04:25:59 +0000184 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
Chris Lattnerc6644182006-03-07 06:32:48 +0000185 // Should use subtarget info to pick the right hazard recognizer. For
186 // now, always return a PPC970 recognizer.
Dan Gohman6448d912008-09-04 15:39:15 +0000187 const TargetInstrInfo *II = TM.getInstrInfo();
Chris Lattner88d211f2006-03-12 09:13:49 +0000188 assert(II && "No InstrInfo?");
189 return new PPCHazardRecognizer970(*II);
Chris Lattnerc6644182006-03-07 06:32:48 +0000190 }
Chris Lattneraf165382005-09-13 22:03:06 +0000191
192// Include the pieces autogenerated from the target description.
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000193#include "PPCGenDAGISel.inc"
Chris Lattnerbd937b92005-10-06 18:45:51 +0000194
195private:
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDNode *SelectSETCC(SDValue Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +0000197 };
198}
199
Evan Chengdb8d56b2008-06-30 20:45:06 +0000200/// InstructionSelect - This callback is invoked by
Chris Lattnerbd937b92005-10-06 18:45:51 +0000201/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000202void PPCDAGToDAGISel::InstructionSelect() {
Chris Lattnerbd937b92005-10-06 18:45:51 +0000203 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000204
Chris Lattnerbd937b92005-10-06 18:45:51 +0000205 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000206 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000207 CurDAG->RemoveDeadNodes();
Chris Lattner4bb18952006-03-16 18:25:23 +0000208}
209
210/// InsertVRSaveCode - Once the entire function has been instruction selected,
211/// all virtual registers are created and all machine instructions are built,
212/// check to see if we need to save/restore VRSAVE. If so, do it.
213void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000214 // Check to see if this function uses vector registers, which means we have to
215 // save and restore the VRSAVE register and update it with the regs we use.
216 //
217 // In this case, there will be virtual registers of vector type type created
218 // by the scheduler. Detect them now.
Chris Lattner4bb18952006-03-16 18:25:23 +0000219 MachineFunction &Fn = MachineFunction::get(&F);
Chris Lattner1877ec92006-03-13 21:52:10 +0000220 bool HasVectorVReg = false;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000221 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +0000222 e = RegInfo->getLastVirtReg()+1; i != e; ++i)
223 if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000224 HasVectorVReg = true;
225 break;
226 }
Chris Lattner4bb18952006-03-16 18:25:23 +0000227 if (!HasVectorVReg) return; // nothing to do.
228
Chris Lattner1877ec92006-03-13 21:52:10 +0000229 // If we have a vector register, we want to emit code into the entry and exit
230 // blocks to save and restore the VRSAVE register. We do this here (instead
231 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
232 //
233 // 1. This (trivially) reduces the load on the register allocator, by not
234 // having to represent the live range of the VRSAVE register.
235 // 2. This (more significantly) allows us to create a temporary virtual
236 // register to hold the saved VRSAVE value, allowing this temporary to be
237 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner4bb18952006-03-16 18:25:23 +0000238
239 // Create two vregs - one to hold the VRSAVE register that is live-in to the
240 // function and one for the value after having bits or'd into it.
Chris Lattner84bc5422007-12-31 04:13:23 +0000241 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
242 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner4bb18952006-03-16 18:25:23 +0000243
Evan Chengc0f64ff2006-11-27 23:37:22 +0000244 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner4bb18952006-03-16 18:25:23 +0000245 MachineBasicBlock &EntryBB = *Fn.begin();
246 // Emit the following code into the entry block:
247 // InVRSAVE = MFVRSAVE
248 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
249 // MTVRSAVE UpdatedVRSAVE
250 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Evan Chengc0f64ff2006-11-27 23:37:22 +0000251 BuildMI(EntryBB, IP, TII.get(PPC::MFVRSAVE), InVRSAVE);
Chris Lattner69244302008-01-07 01:56:04 +0000252 BuildMI(EntryBB, IP, TII.get(PPC::UPDATE_VRSAVE),
253 UpdatedVRSAVE).addReg(InVRSAVE);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000254 BuildMI(EntryBB, IP, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Chris Lattner4bb18952006-03-16 18:25:23 +0000255
256 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner4bb18952006-03-16 18:25:23 +0000257 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000258 if (!BB->empty() && BB->back().getDesc().isReturn()) {
Chris Lattner4bb18952006-03-16 18:25:23 +0000259 IP = BB->end(); --IP;
260
261 // Skip over all terminator instructions, which are part of the return
262 // sequence.
263 MachineBasicBlock::iterator I2 = IP;
Chris Lattner749c6f62008-01-07 07:27:27 +0000264 while (I2 != BB->begin() && (--I2)->getDesc().isTerminator())
Chris Lattner4bb18952006-03-16 18:25:23 +0000265 IP = I2;
266
267 // Emit: MTVRSAVE InVRSave
Evan Chengc0f64ff2006-11-27 23:37:22 +0000268 BuildMI(*BB, IP, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Chris Lattner4bb18952006-03-16 18:25:23 +0000269 }
Chris Lattner1877ec92006-03-13 21:52:10 +0000270 }
Chris Lattnerbd937b92005-10-06 18:45:51 +0000271}
Chris Lattner6cd40d52005-09-03 01:17:22 +0000272
Chris Lattner4bb18952006-03-16 18:25:23 +0000273
Chris Lattner4416f1a2005-08-19 22:38:53 +0000274/// getGlobalBaseReg - Output the instructions required to put the
275/// base address to use for accessing globals into a register.
276///
Evan Cheng9ade2182006-08-26 05:34:46 +0000277SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner4416f1a2005-08-19 22:38:53 +0000278 if (!GlobalBaseReg) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000279 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner4416f1a2005-08-19 22:38:53 +0000280 // Insert the set of GlobalBaseReg into the first MBB of the function
281 MachineBasicBlock &FirstMBB = BB->getParent()->front();
282 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000283
Chris Lattnerd1043422006-11-14 18:43:11 +0000284 if (PPCLowering.getPointerTy() == MVT::i32) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000285 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000286 BuildMI(FirstMBB, MBBI, TII.get(PPC::MovePCtoLR), PPC::LR);
287 BuildMI(FirstMBB, MBBI, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerd1043422006-11-14 18:43:11 +0000288 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000289 GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000290 BuildMI(FirstMBB, MBBI, TII.get(PPC::MovePCtoLR8), PPC::LR8);
291 BuildMI(FirstMBB, MBBI, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerd1043422006-11-14 18:43:11 +0000292 }
Chris Lattner4416f1a2005-08-19 22:38:53 +0000293 }
Gabor Greif93c53e52008-08-31 15:37:04 +0000294 return CurDAG->getRegister(GlobalBaseReg,
295 PPCLowering.getPointerTy()).getNode();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000296}
297
298/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
299/// or 64-bit immediate, and if the value can be accurately represented as a
300/// sign extension from a 16-bit value. If so, this returns true and the
301/// immediate.
302static bool isIntS16Immediate(SDNode *N, short &Imm) {
303 if (N->getOpcode() != ISD::Constant)
304 return false;
305
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000306 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000307 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000308 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000309 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000310 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000311}
312
Dan Gohman475871a2008-07-27 21:46:04 +0000313static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000314 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner4416f1a2005-08-19 22:38:53 +0000315}
316
317
Chris Lattnerc08f9022006-06-27 00:04:13 +0000318/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
319/// operand. If so Imm will receive the 32-bit value.
320static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
321 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000322 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman0f3257a2005-08-18 05:00:13 +0000323 return true;
324 }
325 return false;
326}
327
Chris Lattnerc08f9022006-06-27 00:04:13 +0000328/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
329/// operand. If so Imm will receive the 64-bit value.
330static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Chris Lattner71176242006-09-20 04:33:27 +0000331 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000332 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000333 return true;
334 }
335 return false;
336}
337
338// isInt32Immediate - This method tests to see if a constant operand.
339// If so Imm will receive the 32 bit value.
Dan Gohman475871a2008-07-27 21:46:04 +0000340static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000341 return isInt32Immediate(N.getNode(), Imm);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000342}
343
344
345// isOpcWithIntImmediate - This method tests to see if the node is a specific
346// opcode and that it has a immediate integer right operand.
347// If so Imm will receive the 32 bit value.
348static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000349 return N->getOpcode() == Opc
350 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattnerc08f9022006-06-27 00:04:13 +0000351}
352
Nate Begemanf42f1332006-09-22 05:01:56 +0000353bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000354 if (isShiftedMask_32(Val)) {
355 // look for the first non-zero bit
356 MB = CountLeadingZeros_32(Val);
357 // look for the first zero bit after the run of ones
358 ME = CountLeadingZeros_32((Val - 1) ^ Val);
359 return true;
Chris Lattner2fe76e52005-08-25 04:47:18 +0000360 } else {
361 Val = ~Val; // invert mask
362 if (isShiftedMask_32(Val)) {
363 // effectively look for the first zero bit
364 ME = CountLeadingZeros_32(Val) - 1;
365 // effectively look for the first one bit after the run of zeros
366 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
367 return true;
368 }
Nate Begemancffc32b2005-08-18 07:30:46 +0000369 }
370 // no run present
371 return false;
372}
373
Nate Begemanf42f1332006-09-22 05:01:56 +0000374bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
375 bool IsShiftMask, unsigned &SH,
376 unsigned &MB, unsigned &ME) {
Nate Begemanda32c9e2005-10-19 00:05:37 +0000377 // Don't even go down this path for i64, since different logic will be
378 // necessary for rldicl/rldicr/rldimi.
379 if (N->getValueType(0) != MVT::i32)
380 return false;
381
Nate Begemancffc32b2005-08-18 07:30:46 +0000382 unsigned Shift = 32;
383 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
384 unsigned Opcode = N->getOpcode();
Chris Lattner15055732005-08-30 00:59:16 +0000385 if (N->getNumOperands() != 2 ||
Gabor Greifba36cb52008-08-28 21:40:38 +0000386 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemancffc32b2005-08-18 07:30:46 +0000387 return false;
388
389 if (Opcode == ISD::SHL) {
390 // apply shift left to mask if it comes first
391 if (IsShiftMask) Mask = Mask << Shift;
392 // determine which bits are made indeterminant by shift
393 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattner651dea72005-10-15 21:40:12 +0000394 } else if (Opcode == ISD::SRL) {
Nate Begemancffc32b2005-08-18 07:30:46 +0000395 // apply shift right to mask if it comes first
396 if (IsShiftMask) Mask = Mask >> Shift;
397 // determine which bits are made indeterminant by shift
398 Indeterminant = ~(0xFFFFFFFFu >> Shift);
399 // adjust for the left rotate
400 Shift = 32 - Shift;
Nate Begemanf42f1332006-09-22 05:01:56 +0000401 } else if (Opcode == ISD::ROTL) {
402 Indeterminant = 0;
Nate Begemancffc32b2005-08-18 07:30:46 +0000403 } else {
404 return false;
405 }
406
407 // if the mask doesn't intersect any Indeterminant bits
408 if (Mask && !(Mask & Indeterminant)) {
Chris Lattner0949ed52006-05-12 16:29:37 +0000409 SH = Shift & 31;
Nate Begemancffc32b2005-08-18 07:30:46 +0000410 // make sure the mask is still a mask (wrap arounds may not be)
411 return isRunOfOnes(Mask, MB, ME);
412 }
413 return false;
414}
415
Nate Begeman02b88a42005-08-19 00:38:14 +0000416/// SelectBitfieldInsert - turn an or of two masked values into
417/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman1d9d7422005-10-18 00:28:58 +0000418SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000419 SDValue Op0 = N->getOperand(0);
420 SDValue Op1 = N->getOperand(1);
Nate Begeman02b88a42005-08-19 00:38:14 +0000421
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000422 APInt LKZ, LKO, RKZ, RKO;
423 CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
424 CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
Nate Begeman02b88a42005-08-19 00:38:14 +0000425
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000426 unsigned TargetMask = LKZ.getZExtValue();
427 unsigned InsertMask = RKZ.getZExtValue();
Nate Begeman4667f2c2006-05-08 17:38:32 +0000428
429 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
430 unsigned Op0Opc = Op0.getOpcode();
431 unsigned Op1Opc = Op1.getOpcode();
432 unsigned Value, SH = 0;
433 TargetMask = ~TargetMask;
434 InsertMask = ~InsertMask;
Nate Begeman77f361f2006-05-07 00:23:38 +0000435
Nate Begeman4667f2c2006-05-08 17:38:32 +0000436 // If the LHS has a foldable shift and the RHS does not, then swap it to the
437 // RHS so that we can fold the shift into the insert.
Nate Begeman77f361f2006-05-07 00:23:38 +0000438 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
439 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
440 Op0.getOperand(0).getOpcode() == ISD::SRL) {
441 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
442 Op1.getOperand(0).getOpcode() != ISD::SRL) {
443 std::swap(Op0, Op1);
444 std::swap(Op0Opc, Op1Opc);
Nate Begeman4667f2c2006-05-08 17:38:32 +0000445 std::swap(TargetMask, InsertMask);
Nate Begeman77f361f2006-05-07 00:23:38 +0000446 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000447 }
Nate Begeman4667f2c2006-05-08 17:38:32 +0000448 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
449 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
450 Op1.getOperand(0).getOpcode() != ISD::SRL) {
451 std::swap(Op0, Op1);
452 std::swap(Op0Opc, Op1Opc);
453 std::swap(TargetMask, InsertMask);
454 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000455 }
Nate Begeman77f361f2006-05-07 00:23:38 +0000456
457 unsigned MB, ME;
Chris Lattner0949ed52006-05-12 16:29:37 +0000458 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000459 SDValue Tmp1, Tmp2, Tmp3;
Nate Begeman4667f2c2006-05-08 17:38:32 +0000460 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
Nate Begeman77f361f2006-05-07 00:23:38 +0000461
462 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000463 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000464 Op1 = Op1.getOperand(0);
465 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
466 }
467 if (Op1Opc == ISD::AND) {
468 unsigned SHOpc = Op1.getOperand(0).getOpcode();
469 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000470 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Nate Begeman77f361f2006-05-07 00:23:38 +0000471 Op1 = Op1.getOperand(0).getOperand(0);
472 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
473 } else {
474 Op1 = Op1.getOperand(0);
475 }
476 }
477
478 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
Chris Lattner0949ed52006-05-12 16:29:37 +0000479 SH &= 31;
Dan Gohman475871a2008-07-27 21:46:04 +0000480 SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Cheng0b828e02006-08-27 08:14:06 +0000481 getI32Imm(ME) };
482 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman02b88a42005-08-19 00:38:14 +0000483 }
Nate Begeman02b88a42005-08-19 00:38:14 +0000484 }
485 return 0;
486}
487
Chris Lattner2fbb4572005-08-21 18:50:37 +0000488/// SelectCC - Select a comparison of the specified values with the specified
489/// condition code, returning the CR# of the expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000490SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Nate Begeman1d9d7422005-10-18 00:28:58 +0000491 ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000492 // Always select the LHS.
Chris Lattnerc08f9022006-06-27 00:04:13 +0000493 unsigned Opc;
494
495 if (LHS.getValueType() == MVT::i32) {
Chris Lattner529c2332006-06-27 00:10:13 +0000496 unsigned Imm;
Chris Lattner3836dbd2006-09-20 04:25:47 +0000497 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
498 if (isInt32Immediate(RHS, Imm)) {
499 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
500 if (isUInt16(Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000501 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
Chris Lattner3836dbd2006-09-20 04:25:47 +0000502 getI32Imm(Imm & 0xFFFF)), 0);
503 // If this is a 16-bit signed immediate, fold it.
Chris Lattneraa43e9f2007-04-02 05:59:42 +0000504 if (isInt16((int)Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000505 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
Chris Lattner3836dbd2006-09-20 04:25:47 +0000506 getI32Imm(Imm & 0xFFFF)), 0);
507
508 // For non-equality comparisons, the default code would materialize the
509 // constant, then compare against it, like this:
510 // lis r2, 4660
511 // ori r2, r2, 22136
512 // cmpw cr0, r3, r2
513 // Since we are just comparing for equality, we can emit this instead:
514 // xoris r0,r3,0x1234
515 // cmplwi cr0,r0,0x5678
516 // beq cr0,L6
Dan Gohman475871a2008-07-27 21:46:04 +0000517 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, MVT::i32, LHS,
Chris Lattner3836dbd2006-09-20 04:25:47 +0000518 getI32Imm(Imm >> 16)), 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000519 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, Xor,
Chris Lattner3836dbd2006-09-20 04:25:47 +0000520 getI32Imm(Imm & 0xFFFF)), 0);
521 }
522 Opc = PPC::CMPLW;
523 } else if (ISD::isUnsignedIntSetCC(CC)) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000524 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000525 return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
Chris Lattnerc08f9022006-06-27 00:04:13 +0000526 getI32Imm(Imm & 0xFFFF)), 0);
527 Opc = PPC::CMPLW;
528 } else {
529 short SImm;
530 if (isIntS16Immediate(RHS, SImm))
Dan Gohman475871a2008-07-27 21:46:04 +0000531 return SDValue(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
Chris Lattnerc08f9022006-06-27 00:04:13 +0000532 getI32Imm((int)SImm & 0xFFFF)),
533 0);
534 Opc = PPC::CMPW;
535 }
536 } else if (LHS.getValueType() == MVT::i64) {
537 uint64_t Imm;
Chris Lattner71176242006-09-20 04:33:27 +0000538 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000539 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattner71176242006-09-20 04:33:27 +0000540 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
541 if (isUInt16(Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000542 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000543 getI32Imm(Imm & 0xFFFF)), 0);
544 // If this is a 16-bit signed immediate, fold it.
545 if (isInt16(Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000546 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000547 getI32Imm(Imm & 0xFFFF)), 0);
548
549 // For non-equality comparisons, the default code would materialize the
550 // constant, then compare against it, like this:
551 // lis r2, 4660
552 // ori r2, r2, 22136
553 // cmpd cr0, r3, r2
554 // Since we are just comparing for equality, we can emit this instead:
555 // xoris r0,r3,0x1234
556 // cmpldi cr0,r0,0x5678
557 // beq cr0,L6
558 if (isUInt32(Imm)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000560 getI64Imm(Imm >> 16)), 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000561 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, Xor,
Chris Lattner71176242006-09-20 04:33:27 +0000562 getI64Imm(Imm & 0xFFFF)), 0);
563 }
564 }
565 Opc = PPC::CMPLD;
566 } else if (ISD::isUnsignedIntSetCC(CC)) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000567 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm))
Dan Gohman475871a2008-07-27 21:46:04 +0000568 return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
Chris Lattnerc08f9022006-06-27 00:04:13 +0000569 getI64Imm(Imm & 0xFFFF)), 0);
570 Opc = PPC::CMPLD;
571 } else {
572 short SImm;
573 if (isIntS16Immediate(RHS, SImm))
Dan Gohman475871a2008-07-27 21:46:04 +0000574 return SDValue(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
Chris Lattner71176242006-09-20 04:33:27 +0000575 getI64Imm(SImm & 0xFFFF)),
Chris Lattnerc08f9022006-06-27 00:04:13 +0000576 0);
577 Opc = PPC::CMPD;
578 }
Chris Lattner919c0322005-10-01 01:35:02 +0000579 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000580 Opc = PPC::FCMPUS;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000581 } else {
Chris Lattnerc08f9022006-06-27 00:04:13 +0000582 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
583 Opc = PPC::FCMPUD;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000584 }
Dan Gohman475871a2008-07-27 21:46:04 +0000585 return SDValue(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
Chris Lattner2fbb4572005-08-21 18:50:37 +0000586}
587
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000588static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2fbb4572005-08-21 18:50:37 +0000589 switch (CC) {
Chris Lattner5d634ce2006-05-25 16:54:16 +0000590 case ISD::SETUEQ:
Dale Johannesen53e4e442008-11-07 22:54:33 +0000591 case ISD::SETONE:
592 case ISD::SETOLE:
593 case ISD::SETOGE:
594 assert(0 && "Should be lowered by legalize!");
595 default: assert(0 && "Unknown condition!"); abort();
596 case ISD::SETOEQ:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000597 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner5d634ce2006-05-25 16:54:16 +0000598 case ISD::SETUNE:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000599 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen53e4e442008-11-07 22:54:33 +0000600 case ISD::SETOLT:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000601 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000602 case ISD::SETULE:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000603 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen53e4e442008-11-07 22:54:33 +0000604 case ISD::SETOGT:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000605 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000606 case ISD::SETUGE:
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000607 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattnerdf4ed632006-11-17 22:10:59 +0000608 case ISD::SETO: return PPC::PRED_NU;
609 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen53e4e442008-11-07 22:54:33 +0000610 // These two are invalid for floating point. Assume we have int.
611 case ISD::SETULT: return PPC::PRED_LT;
612 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2fbb4572005-08-21 18:50:37 +0000613 }
Chris Lattner2fbb4572005-08-21 18:50:37 +0000614}
615
Chris Lattner64906a02005-08-25 20:08:18 +0000616/// getCRIdxForSetCC - Return the index of the condition register field
617/// associated with the SetCC condition, and whether or not the field is
618/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000619///
620/// If this returns with Other != -1, then the returned comparison is an or of
621/// two simpler comparisons. In this case, Invert is guaranteed to be false.
622static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
623 Invert = false;
624 Other = -1;
Chris Lattner64906a02005-08-25 20:08:18 +0000625 switch (CC) {
626 default: assert(0 && "Unknown condition!"); abort();
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000627 case ISD::SETOLT:
628 case ISD::SETLT: return 0; // Bit #0 = SETOLT
629 case ISD::SETOGT:
630 case ISD::SETGT: return 1; // Bit #1 = SETOGT
631 case ISD::SETOEQ:
632 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
633 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner64906a02005-08-25 20:08:18 +0000634 case ISD::SETUGE:
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000635 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner64906a02005-08-25 20:08:18 +0000636 case ISD::SETULE:
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000637 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner8e2a04e2006-05-25 18:06:16 +0000638 case ISD::SETUNE:
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000639 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
640 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Dale Johannesen53e4e442008-11-07 22:54:33 +0000641 case ISD::SETUEQ:
642 case ISD::SETOGE:
643 case ISD::SETOLE:
644 case ISD::SETONE:
645 assert(0 && "Invalid branch code: should be expanded by legalize");
646 // These are invalid for floating point. Assume integer.
647 case ISD::SETULT: return 0;
648 case ISD::SETUGT: return 1;
Chris Lattner64906a02005-08-25 20:08:18 +0000649 }
650 return 0;
651}
Chris Lattner9944b762005-08-21 22:31:09 +0000652
Dan Gohman475871a2008-07-27 21:46:04 +0000653SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000654 SDNode *N = Op.getNode();
Chris Lattner222adac2005-10-06 19:03:35 +0000655 unsigned Imm;
656 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Chris Lattnerc08f9022006-06-27 00:04:13 +0000657 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner222adac2005-10-06 19:03:35 +0000658 // We can codegen setcc op, imm very efficiently compared to a brcond.
659 // Check for those cases here.
660 // setcc op, 0
661 if (Imm == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000662 SDValue Op = N->getOperand(0);
Chris Lattner222adac2005-10-06 19:03:35 +0000663 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000664 default: break;
Evan Cheng0b828e02006-08-27 08:14:06 +0000665 case ISD::SETEQ: {
Dan Gohman475871a2008-07-27 21:46:04 +0000666 Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
667 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Evan Cheng0b828e02006-08-27 08:14:06 +0000668 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
669 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000670 case ISD::SETNE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000671 SDValue AD =
672 SDValue(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000673 Op, getI32Imm(~0U)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000674 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000675 AD.getValue(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000676 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000677 case ISD::SETLT: {
Dan Gohman475871a2008-07-27 21:46:04 +0000678 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Evan Cheng0b828e02006-08-27 08:14:06 +0000679 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
680 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000681 case ISD::SETGT: {
Dan Gohman475871a2008-07-27 21:46:04 +0000682 SDValue T =
683 SDValue(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
684 T = SDValue(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
685 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Evan Cheng0b828e02006-08-27 08:14:06 +0000686 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000687 }
688 }
Chris Lattner222adac2005-10-06 19:03:35 +0000689 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman475871a2008-07-27 21:46:04 +0000690 SDValue Op = N->getOperand(0);
Chris Lattner222adac2005-10-06 19:03:35 +0000691 switch (CC) {
Chris Lattnerdabb8292005-10-21 21:17:10 +0000692 default: break;
693 case ISD::SETEQ:
Dan Gohman475871a2008-07-27 21:46:04 +0000694 Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000695 Op, getI32Imm(1)), 0);
Chris Lattner71d3d502005-11-30 22:53:06 +0000696 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman475871a2008-07-27 21:46:04 +0000697 SDValue(CurDAG->getTargetNode(PPC::LI, MVT::i32,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000698 getI32Imm(0)), 0),
Evan Cheng95514ba2006-08-26 08:00:10 +0000699 Op.getValue(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000700 case ISD::SETNE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000701 Op = SDValue(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000702 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
703 Op, getI32Imm(~0U));
Dan Gohman475871a2008-07-27 21:46:04 +0000704 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
705 Op, SDValue(AD, 1));
Chris Lattner222adac2005-10-06 19:03:35 +0000706 }
Chris Lattnerdabb8292005-10-21 21:17:10 +0000707 case ISD::SETLT: {
Dan Gohman475871a2008-07-27 21:46:04 +0000708 SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000709 getI32Imm(1)), 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000710 SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000711 Op), 0);
Dan Gohman475871a2008-07-27 21:46:04 +0000712 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Evan Cheng0b828e02006-08-27 08:14:06 +0000713 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerdabb8292005-10-21 21:17:10 +0000714 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000715 case ISD::SETGT: {
Dan Gohman475871a2008-07-27 21:46:04 +0000716 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
717 Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000718 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng95514ba2006-08-26 08:00:10 +0000719 getI32Imm(1));
Chris Lattnerdabb8292005-10-21 21:17:10 +0000720 }
Evan Cheng0b828e02006-08-27 08:14:06 +0000721 }
Chris Lattner222adac2005-10-06 19:03:35 +0000722 }
723 }
724
725 bool Inv;
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000726 int OtherCondIdx;
727 unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
Dan Gohman475871a2008-07-27 21:46:04 +0000728 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
729 SDValue IntCR;
Chris Lattner222adac2005-10-06 19:03:35 +0000730
731 // Force the ccreg into CR7.
Dan Gohman475871a2008-07-27 21:46:04 +0000732 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Chris Lattner222adac2005-10-06 19:03:35 +0000733
Dan Gohman475871a2008-07-27 21:46:04 +0000734 SDValue InFlag(0, 0); // Null incoming flag value.
Chris Lattnerdb1cb2b2005-12-01 03:50:19 +0000735 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
736 InFlag).getValue(1);
Chris Lattner222adac2005-10-06 19:03:35 +0000737
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000738 if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
Dan Gohman475871a2008-07-27 21:46:04 +0000739 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000740 CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000741 else
Dan Gohman475871a2008-07-27 21:46:04 +0000742 IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
Chris Lattner222adac2005-10-06 19:03:35 +0000743
Dan Gohman475871a2008-07-27 21:46:04 +0000744 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Cheng0b828e02006-08-27 08:14:06 +0000745 getI32Imm(31), getI32Imm(31) };
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000746 if (OtherCondIdx == -1 && !Inv)
Evan Cheng0b828e02006-08-27 08:14:06 +0000747 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000748
749 // Get the specified bit.
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue Tmp =
751 SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000752 if (Inv) {
753 assert(OtherCondIdx == -1 && "Can't have split plus negation");
Evan Cheng95514ba2006-08-26 08:00:10 +0000754 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner222adac2005-10-06 19:03:35 +0000755 }
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000756
757 // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
758 // We already got the bit for the first part of the comparison (e.g. SETULE).
759
760 // Get the other bit of the comparison.
761 Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
Dan Gohman475871a2008-07-27 21:46:04 +0000762 SDValue OtherCond =
763 SDValue(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Ops, 4), 0);
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000764
765 return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
Chris Lattner222adac2005-10-06 19:03:35 +0000766}
Chris Lattner2b63e4c2005-10-06 18:56:10 +0000767
Chris Lattner6a16f6a2005-10-06 19:07:45 +0000768
Chris Lattnera5a91b12005-08-17 19:33:03 +0000769// Select - Convert the specified operand from a target-independent to a
770// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000771SDNode *PPCDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000772 SDNode *N = Op.getNode();
Dan Gohmane8be6c62008-07-17 19:10:17 +0000773 if (N->isMachineOpcode())
Evan Cheng64a752f2006-08-11 09:08:15 +0000774 return NULL; // Already selected.
Chris Lattnerd3d2cf52005-09-29 00:59:32 +0000775
Chris Lattnera5a91b12005-08-17 19:33:03 +0000776 switch (N->getOpcode()) {
Chris Lattner19c09072005-09-07 23:45:15 +0000777 default: break;
Jim Laskey78f97f32006-12-12 13:23:43 +0000778
779 case ISD::Constant: {
780 if (N->getValueType(0) == MVT::i64) {
781 // Get 64 bit value.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000782 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey78f97f32006-12-12 13:23:43 +0000783 // Assume no remaining bits.
784 unsigned Remainder = 0;
785 // Assume no shift required.
786 unsigned Shift = 0;
787
788 // If it can't be represented as a 32 bit value.
789 if (!isInt32(Imm)) {
790 Shift = CountTrailingZeros_64(Imm);
791 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
792
793 // If the shifted value fits 32 bits.
794 if (isInt32(ImmSh)) {
795 // Go with the shifted value.
796 Imm = ImmSh;
797 } else {
798 // Still stuck with a 64 bit value.
799 Remainder = Imm;
800 Shift = 32;
801 Imm >>= 32;
802 }
803 }
804
805 // Intermediate operand.
806 SDNode *Result;
807
808 // Handle first 32 bits.
809 unsigned Lo = Imm & 0xFFFF;
810 unsigned Hi = (Imm >> 16) & 0xFFFF;
811
812 // Simple value.
813 if (isInt16(Imm)) {
814 // Just the Lo bits.
815 Result = CurDAG->getTargetNode(PPC::LI8, MVT::i64, getI32Imm(Lo));
816 } else if (Lo) {
817 // Handle the Hi bits.
818 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
819 Result = CurDAG->getTargetNode(OpC, MVT::i64, getI32Imm(Hi));
820 // And Lo bits.
821 Result = CurDAG->getTargetNode(PPC::ORI8, MVT::i64,
Dan Gohman475871a2008-07-27 21:46:04 +0000822 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey78f97f32006-12-12 13:23:43 +0000823 } else {
824 // Just the Hi bits.
825 Result = CurDAG->getTargetNode(PPC::LIS8, MVT::i64, getI32Imm(Hi));
826 }
827
828 // If no shift, we're done.
829 if (!Shift) return Result;
830
831 // Shift for next step if the upper 32-bits were not zero.
832 if (Imm) {
833 Result = CurDAG->getTargetNode(PPC::RLDICR, MVT::i64,
Dan Gohman475871a2008-07-27 21:46:04 +0000834 SDValue(Result, 0),
Jim Laskey78f97f32006-12-12 13:23:43 +0000835 getI32Imm(Shift), getI32Imm(63 - Shift));
836 }
837
838 // Add in the last bits as required.
839 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
840 Result = CurDAG->getTargetNode(PPC::ORIS8, MVT::i64,
Dan Gohman475871a2008-07-27 21:46:04 +0000841 SDValue(Result, 0), getI32Imm(Hi));
Jim Laskey78f97f32006-12-12 13:23:43 +0000842 }
843 if ((Lo = Remainder & 0xFFFF)) {
844 Result = CurDAG->getTargetNode(PPC::ORI8, MVT::i64,
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey78f97f32006-12-12 13:23:43 +0000846 }
847
848 return Result;
849 }
850 break;
851 }
852
Evan Cheng34167212006-02-09 00:37:58 +0000853 case ISD::SETCC:
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000854 return SelectSETCC(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000855 case PPCISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000856 return getGlobalBaseReg();
Chris Lattner860e8862005-11-17 07:30:41 +0000857
Chris Lattnere28e40a2005-08-25 00:45:43 +0000858 case ISD::FrameIndex: {
859 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
Chris Lattnerc08f9022006-06-27 00:04:13 +0000861 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000862 if (N->hasOneUse())
863 return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
Evan Cheng95514ba2006-08-26 08:00:10 +0000864 getSmallIPtrImm(0));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000865 return CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
866 getSmallIPtrImm(0));
Chris Lattnere28e40a2005-08-25 00:45:43 +0000867 }
Chris Lattner6d92cad2006-03-26 10:06:40 +0000868
869 case PPCISD::MFCR: {
Dan Gohman475871a2008-07-27 21:46:04 +0000870 SDValue InFlag = N->getOperand(1);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000871 // Use MFOCRF if supported.
Evan Cheng152b7e12007-10-23 06:42:42 +0000872 if (PPCSubTarget.isGigaProcessor())
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000873 return CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
874 N->getOperand(0), InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000875 else
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000876 return CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000877 }
878
Chris Lattner88add102005-09-28 22:50:24 +0000879 case ISD::SDIV: {
Nate Begeman405e3ec2005-10-21 00:02:42 +0000880 // FIXME: since this depends on the setting of the carry flag from the srawi
881 // we should really be making notes about that for the scheduler.
882 // FIXME: It sure would be nice if we could cheaply recognize the
883 // srl/add/sra pattern the dag combiner will generate for this as
884 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattner8784a232005-08-25 17:50:06 +0000885 unsigned Imm;
Chris Lattnerc08f9022006-06-27 00:04:13 +0000886 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000887 SDValue N0 = N->getOperand(0);
Chris Lattner8784a232005-08-25 17:50:06 +0000888 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000889 SDNode *Op =
Chris Lattner8784a232005-08-25 17:50:06 +0000890 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000891 N0, getI32Imm(Log2_32(Imm)));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +0000892 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman475871a2008-07-27 21:46:04 +0000893 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattner8784a232005-08-25 17:50:06 +0000894 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000895 SDNode *Op =
Chris Lattner2501d5e2005-08-30 17:13:58 +0000896 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
Evan Cheng34167212006-02-09 00:37:58 +0000897 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman475871a2008-07-27 21:46:04 +0000898 SDValue PT =
899 SDValue(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
900 SDValue(Op, 0), SDValue(Op, 1)),
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000901 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000902 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattner8784a232005-08-25 17:50:06 +0000903 }
904 }
Chris Lattner047b9522005-08-25 22:04:30 +0000905
Chris Lattner237733e2005-09-29 23:33:31 +0000906 // Other cases are autogenerated.
907 break;
Chris Lattner047b9522005-08-25 22:04:30 +0000908 }
Chris Lattner4eab7142006-11-10 02:08:47 +0000909
910 case ISD::LOAD: {
911 // Handle preincrement loads.
912 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000913 MVT LoadedVT = LD->getMemoryVT();
Chris Lattner4eab7142006-11-10 02:08:47 +0000914
915 // Normal loads are handled by code generated from the .td file.
916 if (LD->getAddressingMode() != ISD::PRE_INC)
917 break;
918
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue Offset = LD->getOffset();
Chris Lattner5b3bbc72006-11-11 04:53:30 +0000920 if (isa<ConstantSDNode>(Offset) ||
921 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Chris Lattner0851b4f2006-11-15 19:55:13 +0000922
923 unsigned Opcode;
924 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
925 if (LD->getValueType(0) != MVT::i64) {
926 // Handle PPC32 integer and normal FP loads.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000927 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000928 switch (LoadedVT.getSimpleVT()) {
Chris Lattner0851b4f2006-11-15 19:55:13 +0000929 default: assert(0 && "Invalid PPC load type!");
930 case MVT::f64: Opcode = PPC::LFDU; break;
931 case MVT::f32: Opcode = PPC::LFSU; break;
932 case MVT::i32: Opcode = PPC::LWZU; break;
933 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
934 case MVT::i1:
935 case MVT::i8: Opcode = PPC::LBZU; break;
936 }
937 } else {
938 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000939 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000940 switch (LoadedVT.getSimpleVT()) {
Chris Lattner0851b4f2006-11-15 19:55:13 +0000941 default: assert(0 && "Invalid PPC load type!");
942 case MVT::i64: Opcode = PPC::LDU; break;
943 case MVT::i32: Opcode = PPC::LWZU8; break;
944 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
945 case MVT::i1:
946 case MVT::i8: Opcode = PPC::LBZU8; break;
947 }
948 }
949
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SDValue Chain = LD->getChain();
951 SDValue Base = LD->getBasePtr();
Dan Gohman475871a2008-07-27 21:46:04 +0000952 SDValue Ops[] = { Offset, Base, Chain };
Chris Lattner4eab7142006-11-10 02:08:47 +0000953 // FIXME: PPC64
Dan Gohmane8be6c62008-07-17 19:10:17 +0000954 return CurDAG->getTargetNode(Opcode, LD->getValueType(0),
955 PPCLowering.getPointerTy(),
Chris Lattner4eab7142006-11-10 02:08:47 +0000956 MVT::Other, Ops, 3);
957 } else {
958 assert(0 && "R+R preindex loads not supported yet!");
959 }
960 }
961
Nate Begemancffc32b2005-08-18 07:30:46 +0000962 case ISD::AND: {
Nate Begemanf42f1332006-09-22 05:01:56 +0000963 unsigned Imm, Imm2, SH, MB, ME;
964
Nate Begemancffc32b2005-08-18 07:30:46 +0000965 // If this is an and of a value rotated between 0 and 31 bits and then and'd
966 // with a mask, emit rlwinm
Chris Lattnerc08f9022006-06-27 00:04:13 +0000967 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000968 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000969 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000970 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Evan Cheng0b828e02006-08-27 08:14:06 +0000971 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemancffc32b2005-08-18 07:30:46 +0000972 }
Nate Begemanf42f1332006-09-22 05:01:56 +0000973 // If this is just a masked value where the input is not handled above, and
974 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
975 if (isInt32Immediate(N->getOperand(1), Imm) &&
976 isRunOfOnes(Imm, MB, ME) &&
977 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000978 SDValue Val = N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Nate Begemanf42f1332006-09-22 05:01:56 +0000980 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
981 }
982 // AND X, 0 -> 0, not "rlwinm 32".
983 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000984 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Nate Begemanf42f1332006-09-22 05:01:56 +0000985 return NULL;
986 }
Nate Begeman50fb3c42005-12-24 01:00:15 +0000987 // ISD::OR doesn't get all the bitfield insertion fun.
988 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Chris Lattnerc08f9022006-06-27 00:04:13 +0000989 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman50fb3c42005-12-24 01:00:15 +0000990 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattnerc08f9022006-06-27 00:04:13 +0000991 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattnerc9a5ef52006-01-05 18:32:49 +0000992 unsigned MB, ME;
Nate Begeman50fb3c42005-12-24 01:00:15 +0000993 Imm = ~(Imm^Imm2);
994 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000995 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Cheng0b828e02006-08-27 08:14:06 +0000996 N->getOperand(0).getOperand(1),
997 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
998 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Ops, 5);
Nate Begeman50fb3c42005-12-24 01:00:15 +0000999 }
1000 }
Chris Lattner237733e2005-09-29 23:33:31 +00001001
1002 // Other cases are autogenerated.
1003 break;
Nate Begemancffc32b2005-08-18 07:30:46 +00001004 }
Nate Begeman02b88a42005-08-19 00:38:14 +00001005 case ISD::OR:
Chris Lattnercccef1c2006-06-27 21:08:52 +00001006 if (N->getValueType(0) == MVT::i32)
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001007 if (SDNode *I = SelectBitfieldInsert(N))
1008 return I;
Chris Lattnerd3d2cf52005-09-29 00:59:32 +00001009
Chris Lattner237733e2005-09-29 23:33:31 +00001010 // Other cases are autogenerated.
1011 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001012 case ISD::SHL: {
1013 unsigned Imm, SH, MB, ME;
Gabor Greifba36cb52008-08-28 21:40:38 +00001014 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001015 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001016 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Cheng0b828e02006-08-27 08:14:06 +00001017 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1018 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +00001019 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001020
1021 // Other cases are autogenerated.
1022 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001023 }
1024 case ISD::SRL: {
1025 unsigned Imm, SH, MB, ME;
Gabor Greifba36cb52008-08-28 21:40:38 +00001026 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman2d5aff72005-10-19 18:42:01 +00001027 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001028 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Cheng0b828e02006-08-27 08:14:06 +00001029 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1030 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman8d948322005-10-19 01:12:32 +00001031 }
Nate Begeman2d5aff72005-10-19 18:42:01 +00001032
1033 // Other cases are autogenerated.
1034 break;
Nate Begemanc15ed442005-08-18 23:38:00 +00001035 }
Chris Lattner13794f52005-08-26 18:46:49 +00001036 case ISD::SELECT_CC: {
1037 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1038
Chris Lattnerc08f9022006-06-27 00:04:13 +00001039 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Chris Lattner13794f52005-08-26 18:46:49 +00001040 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1041 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1042 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1043 if (N1C->isNullValue() && N3C->isNullValue() &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001044 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
Chris Lattnerc08f9022006-06-27 00:04:13 +00001045 // FIXME: Implement this optzn for PPC64.
1046 N->getValueType(0) == MVT::i32) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001047 SDNode *Tmp =
Chris Lattner13794f52005-08-26 18:46:49 +00001048 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
Evan Cheng6da2f322006-08-26 01:07:58 +00001049 N->getOperand(0), getI32Imm(~0U));
Chris Lattnerccbe2ec2006-08-15 23:48:22 +00001050 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
Dan Gohman475871a2008-07-27 21:46:04 +00001051 SDValue(Tmp, 0), N->getOperand(0),
1052 SDValue(Tmp, 1));
Chris Lattner13794f52005-08-26 18:46:49 +00001053 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001054
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
Chris Lattnerdf4ed632006-11-17 22:10:59 +00001056 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001057
Chris Lattner919c0322005-10-01 01:35:02 +00001058 unsigned SelectCCOp;
Chris Lattnerc08f9022006-06-27 00:04:13 +00001059 if (N->getValueType(0) == MVT::i32)
1060 SelectCCOp = PPC::SELECT_CC_I4;
1061 else if (N->getValueType(0) == MVT::i64)
1062 SelectCCOp = PPC::SELECT_CC_I8;
Chris Lattner919c0322005-10-01 01:35:02 +00001063 else if (N->getValueType(0) == MVT::f32)
1064 SelectCCOp = PPC::SELECT_CC_F4;
Chris Lattner710ff322006-04-08 22:45:08 +00001065 else if (N->getValueType(0) == MVT::f64)
Chris Lattner919c0322005-10-01 01:35:02 +00001066 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner710ff322006-04-08 22:45:08 +00001067 else
1068 SelectCCOp = PPC::SELECT_CC_VRRC;
1069
Dan Gohman475871a2008-07-27 21:46:04 +00001070 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Cheng0b828e02006-08-27 08:14:06 +00001071 getI32Imm(BROpc) };
1072 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattner13794f52005-08-26 18:46:49 +00001073 }
Chris Lattner18258c62006-11-17 22:37:34 +00001074 case PPCISD::COND_BRANCH: {
Dan Gohmancbb7ab22008-11-05 17:16:24 +00001075 // Op #0 is the Chain.
Chris Lattner18258c62006-11-17 22:37:34 +00001076 // Op #1 is the PPC::PRED_* number.
1077 // Op #2 is the CR#
1078 // Op #3 is the Dest MBB
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001079 // Op #4 is the Flag.
Evan Cheng2bda17c2007-06-29 01:25:06 +00001080 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue Pred =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001082 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman475871a2008-07-27 21:46:04 +00001083 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattner18258c62006-11-17 22:37:34 +00001084 N->getOperand(0), N->getOperand(4) };
1085 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1086 }
Nate Begeman81e80972006-03-17 01:40:33 +00001087 case ISD::BR_CC: {
Chris Lattner2fbb4572005-08-21 18:50:37 +00001088 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00001089 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1090 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
Evan Cheng0b828e02006-08-27 08:14:06 +00001091 N->getOperand(4), N->getOperand(0) };
Chris Lattner289c2d52006-11-17 22:14:47 +00001092 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
Chris Lattner2fbb4572005-08-21 18:50:37 +00001093 }
Nate Begeman37efe672006-04-22 18:53:45 +00001094 case ISD::BRIND: {
Chris Lattnercf006312006-06-10 01:15:02 +00001095 // FIXME: Should custom lower this.
Dan Gohman475871a2008-07-27 21:46:04 +00001096 SDValue Chain = N->getOperand(0);
1097 SDValue Target = N->getOperand(1);
Chris Lattner6b76b962006-06-27 20:46:17 +00001098 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Dan Gohman475871a2008-07-27 21:46:04 +00001099 Chain = SDValue(CurDAG->getTargetNode(Opc, MVT::Other, Target,
Nate Begeman37efe672006-04-22 18:53:45 +00001100 Chain), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +00001101 return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
Nate Begeman37efe672006-04-22 18:53:45 +00001102 }
Chris Lattnera5a91b12005-08-17 19:33:03 +00001103 }
Chris Lattner25dae722005-09-03 00:53:47 +00001104
Evan Cheng9ade2182006-08-26 05:34:46 +00001105 return SelectCode(Op);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001106}
1107
1108
Chris Lattnercf006312006-06-10 01:15:02 +00001109
Nate Begeman1d9d7422005-10-18 00:28:58 +00001110/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattnera5a91b12005-08-17 19:33:03 +00001111/// PowerPC-specific DAG, ready for instruction scheduling.
1112///
Evan Chengc4c62572006-03-13 23:20:37 +00001113FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001114 return new PPCDAGToDAGISel(TM);
Chris Lattnera5a91b12005-08-17 19:33:03 +00001115}
1116