blob: 475bde18efb7ac92445cf45f51fb89e276b72224 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "ppc-codegen"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000016#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000017#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000026#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Intrinsics.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000030#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000035using namespace llvm;
36
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000037namespace llvm {
38 void initializePPCDAGToDAGISelPass(PassRegistry&);
39}
40
Chris Lattner43ff01e2005-08-17 19:33:03 +000041namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000042 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000043 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000044 /// instructions for SelectionDAG operations.
45 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000046 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000047 const PPCTargetMachine &TM;
48 const PPCTargetLowering &PPCLowering;
Evan Chengec271b12007-10-23 06:42:42 +000049 const PPCSubtarget &PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000050 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000051 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000052 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman619ef482009-01-15 19:20:50 +000053 : SelectionDAGISel(tm), TM(tm),
Evan Chengec271b12007-10-23 06:42:42 +000054 PPCLowering(*TM.getTargetLowering()),
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000055 PPCSubTarget(*TM.getSubtargetImpl()) {
56 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
57 }
Andrew Trickc416ba62010-12-24 04:28:06 +000058
Dan Gohman5ea74d52009-07-31 18:16:33 +000059 virtual bool runOnMachineFunction(MachineFunction &MF) {
Chris Lattner45640392005-08-19 22:38:53 +000060 // Make sure we re-emit a set of the global base reg if necessary
61 GlobalBaseReg = 0;
Dan Gohman5ea74d52009-07-31 18:16:33 +000062 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000063
Bill Schmidt38d94582012-10-10 20:54:15 +000064 if (!PPCSubTarget.isSVR4ABI())
65 InsertVRSaveCode(MF);
66
Chris Lattner1678a6c2006-03-16 18:25:23 +000067 return true;
Chris Lattner45640392005-08-19 22:38:53 +000068 }
Andrew Trickc416ba62010-12-24 04:28:06 +000069
Bill Schmidtf5b474c2013-02-21 00:38:25 +000070 virtual void PostprocessISelDAG();
71
Chris Lattner43ff01e2005-08-17 19:33:03 +000072 /// getI32Imm - Return a target constant with the specified value, of type
73 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000074 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000075 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000076 }
Chris Lattner45640392005-08-19 22:38:53 +000077
Chris Lattner97b3da12006-06-27 00:04:13 +000078 /// getI64Imm - Return a target constant with the specified value, of type
79 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000080 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000081 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000082 }
Andrew Trickc416ba62010-12-24 04:28:06 +000083
Chris Lattner97b3da12006-06-27 00:04:13 +000084 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000085 inline SDValue getSmallIPtrImm(unsigned Imm) {
Chris Lattner97b3da12006-06-27 00:04:13 +000086 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
87 }
Andrew Trickc416ba62010-12-24 04:28:06 +000088
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000089 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000090 /// with any number of 0s on either side. The 1s are allowed to wrap from
91 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
92 /// 0x0F0F0000 is not, since all 1s are not contiguous.
93 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
94
95
96 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
97 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +000098 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +000099 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000100
Chris Lattner45640392005-08-19 22:38:53 +0000101 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
102 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000103 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000104
Chris Lattner43ff01e2005-08-17 19:33:03 +0000105 // Select - Convert the specified operand from a target-independent to a
106 // target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000107 SDNode *Select(SDNode *N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000108
Nate Begeman93c4bc62005-08-19 00:38:14 +0000109 SDNode *SelectBitfieldInsert(SDNode *N);
110
Chris Lattner2a1823d2005-08-21 18:50:37 +0000111 /// SelectCC - Select a comparison of the specified values with the
112 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000113 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000114
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000115 /// SelectAddrImm - Returns true if the address N can be represented by
116 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000117 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000118 SDValue &Base) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000119 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000120 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000121
Chris Lattner6f5840c2006-11-16 00:41:37 +0000122 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000123 /// immediate field. Note that the operand at this point is already the
124 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000125 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000126 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000127 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000128 Out = N;
129 return true;
130 }
131
132 return false;
133 }
134
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000135 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
136 /// represented as an indexed [r+r] operation. Returns false if it can
137 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000138 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000139 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
140 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000141
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000142 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
143 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000144 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000145 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
146 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000147
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000148 /// SelectAddrImmX4 - Returns true if the address N can be represented by
149 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
150 /// Suitable for use by STD and friends.
151 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
152 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000153 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000154
Hal Finkel756810f2013-03-21 21:37:52 +0000155 // Select an address into a single register.
156 bool SelectAddr(SDValue N, SDValue &Base) {
157 Base = N;
158 return true;
159 }
160
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000161 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000162 /// inline asm expressions. It is always correct to compute the value into
163 /// a register. The case of adding a (possibly relocatable) constant to a
164 /// register can be improved, but it is wrong to substitute Reg+Reg for
165 /// Reg in an asm, because the load or store opcode would have to change.
166 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000167 char ConstraintCode,
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000168 std::vector<SDValue> &OutOps) {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000169 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000170 return false;
171 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000172
Dan Gohman5ea74d52009-07-31 18:16:33 +0000173 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000174
Chris Lattner43ff01e2005-08-17 19:33:03 +0000175 virtual const char *getPassName() const {
176 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000177 }
178
Chris Lattner03e08ee2005-09-13 22:03:06 +0000179// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000180#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000181
Chris Lattner259e6c72005-10-06 18:45:51 +0000182private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000183 SDNode *SelectSETCC(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000184 };
185}
186
Chris Lattner1678a6c2006-03-16 18:25:23 +0000187/// InsertVRSaveCode - Once the entire function has been instruction selected,
188/// all virtual registers are created and all machine instructions are built,
189/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000190void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000191 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000192 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000193 //
Dan Gohman4a618822010-02-10 16:03:48 +0000194 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000195 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000196 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000197 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
198 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
199 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000200 HasVectorVReg = true;
201 break;
202 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000203 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000204 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000205
Chris Lattner02e2c182006-03-13 21:52:10 +0000206 // If we have a vector register, we want to emit code into the entry and exit
207 // blocks to save and restore the VRSAVE register. We do this here (instead
208 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
209 //
210 // 1. This (trivially) reduces the load on the register allocator, by not
211 // having to represent the live range of the VRSAVE register.
212 // 2. This (more significantly) allows us to create a temporary virtual
213 // register to hold the saved VRSAVE value, allowing this temporary to be
214 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000215
216 // Create two vregs - one to hold the VRSAVE register that is live-in to the
217 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000218 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
219 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000220
Evan Cheng20350c42006-11-27 23:37:22 +0000221 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000222 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000223 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000224 // Emit the following code into the entry block:
225 // InVRSAVE = MFVRSAVE
226 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
227 // MTVRSAVE UpdatedVRSAVE
228 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000229 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
230 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000231 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000232 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000233
Chris Lattner1678a6c2006-03-16 18:25:23 +0000234 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000235 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000236 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000237 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000238
Chris Lattner1678a6c2006-03-16 18:25:23 +0000239 // Skip over all terminator instructions, which are part of the return
240 // sequence.
241 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000242 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000243 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000244
Chris Lattner1678a6c2006-03-16 18:25:23 +0000245 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000246 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000247 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000248 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000249}
Chris Lattner8ae95252005-09-03 01:17:22 +0000250
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251
Chris Lattner45640392005-08-19 22:38:53 +0000252/// getGlobalBaseReg - Output the instructions required to put the
253/// base address to use for accessing globals into a register.
254///
Evan Cheng61413a32006-08-26 05:34:46 +0000255SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000256 if (!GlobalBaseReg) {
Evan Cheng20350c42006-11-27 23:37:22 +0000257 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000258 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000259 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000260 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000261 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000262
Owen Anderson9f944592009-08-11 20:47:22 +0000263 if (PPCLowering.getPointerTy() == MVT::i32) {
Craig Topperabadc662012-04-20 06:31:50 +0000264 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000265 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000266 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000267 } else {
Craig Topperabadc662012-04-20 06:31:50 +0000268 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000269 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000270 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000271 }
Chris Lattner45640392005-08-19 22:38:53 +0000272 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000273 return CurDAG->getRegister(GlobalBaseReg,
274 PPCLowering.getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000275}
276
277/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
278/// or 64-bit immediate, and if the value can be accurately represented as a
279/// sign extension from a 16-bit value. If so, this returns true and the
280/// immediate.
281static bool isIntS16Immediate(SDNode *N, short &Imm) {
282 if (N->getOpcode() != ISD::Constant)
283 return false;
284
Dan Gohmaneffb8942008-09-12 16:56:44 +0000285 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000286 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000287 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000288 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000289 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000290}
291
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000292static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000293 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000294}
295
296
Chris Lattner97b3da12006-06-27 00:04:13 +0000297/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
298/// operand. If so Imm will receive the 32-bit value.
299static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000300 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000301 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000302 return true;
303 }
304 return false;
305}
306
Chris Lattner97b3da12006-06-27 00:04:13 +0000307/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
308/// operand. If so Imm will receive the 64-bit value.
309static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000310 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000311 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000312 return true;
313 }
314 return false;
315}
316
317// isInt32Immediate - This method tests to see if a constant operand.
318// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000319static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000320 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000321}
322
323
324// isOpcWithIntImmediate - This method tests to see if the node is a specific
325// opcode and that it has a immediate integer right operand.
326// If so Imm will receive the 32 bit value.
327static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000328 return N->getOpcode() == Opc
329 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000330}
331
Nate Begemand31efd12006-09-22 05:01:56 +0000332bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000333 if (!Val)
334 return false;
335
Nate Begemanb3821a32005-08-18 07:30:46 +0000336 if (isShiftedMask_32(Val)) {
337 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000338 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000339 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000340 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000341 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000342 } else {
343 Val = ~Val; // invert mask
344 if (isShiftedMask_32(Val)) {
345 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000346 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000347 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000348 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000349 return true;
350 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000351 }
352 // no run present
353 return false;
354}
355
Andrew Trickc416ba62010-12-24 04:28:06 +0000356bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
357 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000358 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000359 // Don't even go down this path for i64, since different logic will be
360 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000361 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000362 return false;
363
Nate Begemanb3821a32005-08-18 07:30:46 +0000364 unsigned Shift = 32;
365 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
366 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000367 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000368 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000369 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000370
Nate Begemanb3821a32005-08-18 07:30:46 +0000371 if (Opcode == ISD::SHL) {
372 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000373 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000374 // determine which bits are made indeterminant by shift
375 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000376 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000377 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000378 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000379 // determine which bits are made indeterminant by shift
380 Indeterminant = ~(0xFFFFFFFFu >> Shift);
381 // adjust for the left rotate
382 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000383 } else if (Opcode == ISD::ROTL) {
384 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 } else {
386 return false;
387 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000388
Nate Begemanb3821a32005-08-18 07:30:46 +0000389 // if the mask doesn't intersect any Indeterminant bits
390 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000391 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000392 // make sure the mask is still a mask (wrap arounds may not be)
393 return isRunOfOnes(Mask, MB, ME);
394 }
395 return false;
396}
397
Nate Begeman93c4bc62005-08-19 00:38:14 +0000398/// SelectBitfieldInsert - turn an or of two masked values into
399/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000400SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000401 SDValue Op0 = N->getOperand(0);
402 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000403 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000404
Dan Gohmanf19609a2008-02-27 01:23:58 +0000405 APInt LKZ, LKO, RKZ, RKO;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000406 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
407 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000408
Dan Gohmanf19609a2008-02-27 01:23:58 +0000409 unsigned TargetMask = LKZ.getZExtValue();
410 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000411
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000412 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
413 unsigned Op0Opc = Op0.getOpcode();
414 unsigned Op1Opc = Op1.getOpcode();
415 unsigned Value, SH = 0;
416 TargetMask = ~TargetMask;
417 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000418
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000419 // If the LHS has a foldable shift and the RHS does not, then swap it to the
420 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000421 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
422 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
423 Op0.getOperand(0).getOpcode() == ISD::SRL) {
424 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
425 Op1.getOperand(0).getOpcode() != ISD::SRL) {
426 std::swap(Op0, Op1);
427 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000428 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000429 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000430 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000431 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
432 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
433 Op1.getOperand(0).getOpcode() != ISD::SRL) {
434 std::swap(Op0, Op1);
435 std::swap(Op0Opc, Op1Opc);
436 std::swap(TargetMask, InsertMask);
437 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000438 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000439
Nate Begeman1333cea2006-05-07 00:23:38 +0000440 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000441 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000442 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000443
444 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000445 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000446 Op1 = Op1.getOperand(0);
447 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
448 }
449 if (Op1Opc == ISD::AND) {
450 unsigned SHOpc = Op1.getOperand(0).getOpcode();
451 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000452 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Hal Finkel4ca70102013-06-28 20:00:07 +0000453 // Note that Value must be in range here (less than 32) because
454 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000455 Op1 = Op1.getOperand(0).getOperand(0);
456 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000457 }
458 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000459
Chris Lattnera2963392006-05-12 16:29:37 +0000460 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000461 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000462 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000463 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000464 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000465 }
466 return 0;
467}
468
Chris Lattner2a1823d2005-08-21 18:50:37 +0000469/// SelectCC - Select a comparison of the specified values with the specified
470/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000471SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000472 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000473 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000474 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000475
Owen Anderson9f944592009-08-11 20:47:22 +0000476 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000477 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000478 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
479 if (isInt32Immediate(RHS, Imm)) {
480 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000481 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000482 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
483 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000484 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000485 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000486 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
487 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000488
Chris Lattneraa3926b2006-09-20 04:25:47 +0000489 // For non-equality comparisons, the default code would materialize the
490 // constant, then compare against it, like this:
491 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000492 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000493 // cmpw cr0, r3, r2
494 // Since we are just comparing for equality, we can emit this instead:
495 // xoris r0,r3,0x1234
496 // cmplwi cr0,r0,0x5678
497 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000498 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
499 getI32Imm(Imm >> 16)), 0);
500 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
501 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000502 }
503 Opc = PPC::CMPLW;
504 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000505 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000506 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
507 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000508 Opc = PPC::CMPLW;
509 } else {
510 short SImm;
511 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000512 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
513 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000514 0);
515 Opc = PPC::CMPW;
516 }
Owen Anderson9f944592009-08-11 20:47:22 +0000517 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000518 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000519 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000520 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000521 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000522 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000523 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
524 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000525 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000526 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000527 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
528 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000529
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000530 // For non-equality comparisons, the default code would materialize the
531 // constant, then compare against it, like this:
532 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000533 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000534 // cmpd cr0, r3, r2
535 // Since we are just comparing for equality, we can emit this instead:
536 // xoris r0,r3,0x1234
537 // cmpldi cr0,r0,0x5678
538 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000539 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000540 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
541 getI64Imm(Imm >> 16)), 0);
542 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
543 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000544 }
545 }
546 Opc = PPC::CMPLD;
547 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000548 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000549 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
550 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000551 Opc = PPC::CMPLD;
552 } else {
553 short SImm;
554 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000555 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
556 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000557 0);
558 Opc = PPC::CMPD;
559 }
Owen Anderson9f944592009-08-11 20:47:22 +0000560 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000561 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000562 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000563 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Chris Lattner97b3da12006-06-27 00:04:13 +0000564 Opc = PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000565 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000566 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000567}
568
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000569static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000570 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000571 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000572 case ISD::SETONE:
573 case ISD::SETOLE:
574 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000575 llvm_unreachable("Should be lowered by legalize!");
576 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000577 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000578 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000579 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000580 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000581 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000582 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000583 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000584 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000585 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000586 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000587 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000588 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000589 case ISD::SETO: return PPC::PRED_NU;
590 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000591 // These two are invalid for floating point. Assume we have int.
592 case ISD::SETULT: return PPC::PRED_LT;
593 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000594 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000595}
596
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000597/// getCRIdxForSetCC - Return the index of the condition register field
598/// associated with the SetCC condition, and whether or not the field is
599/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000600static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000601 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000602 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000603 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000604 case ISD::SETOLT:
605 case ISD::SETLT: return 0; // Bit #0 = SETOLT
606 case ISD::SETOGT:
607 case ISD::SETGT: return 1; // Bit #1 = SETOGT
608 case ISD::SETOEQ:
609 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
610 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000611 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000612 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000613 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000614 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000615 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000616 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
617 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000618 case ISD::SETUEQ:
619 case ISD::SETOGE:
620 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000621 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000622 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000623 // These are invalid for floating point. Assume integer.
624 case ISD::SETULT: return 0;
625 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000626 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000627}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000628
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000629// getVCmpInst: return the vector compare instruction for the specified
630// vector type and condition code. Since this is for altivec specific code,
631// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
632static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) {
633 switch (CC) {
634 case ISD::SETEQ:
635 case ISD::SETUEQ:
636 case ISD::SETNE:
637 case ISD::SETUNE:
638 if (VecVT == MVT::v16i8)
639 return PPC::VCMPEQUB;
640 else if (VecVT == MVT::v8i16)
641 return PPC::VCMPEQUH;
642 else if (VecVT == MVT::v4i32)
643 return PPC::VCMPEQUW;
644 // v4f32 != v4f32 could be translate to unordered not equal
645 else if (VecVT == MVT::v4f32)
646 return PPC::VCMPEQFP;
647 break;
648 case ISD::SETLT:
649 case ISD::SETGT:
650 case ISD::SETLE:
651 case ISD::SETGE:
652 if (VecVT == MVT::v16i8)
653 return PPC::VCMPGTSB;
654 else if (VecVT == MVT::v8i16)
655 return PPC::VCMPGTSH;
656 else if (VecVT == MVT::v4i32)
657 return PPC::VCMPGTSW;
658 else if (VecVT == MVT::v4f32)
659 return PPC::VCMPGTFP;
660 break;
661 case ISD::SETULT:
662 case ISD::SETUGT:
663 case ISD::SETUGE:
664 case ISD::SETULE:
665 if (VecVT == MVT::v16i8)
666 return PPC::VCMPGTUB;
667 else if (VecVT == MVT::v8i16)
668 return PPC::VCMPGTUH;
669 else if (VecVT == MVT::v4i32)
670 return PPC::VCMPGTUW;
671 break;
672 case ISD::SETOEQ:
673 if (VecVT == MVT::v4f32)
674 return PPC::VCMPEQFP;
675 break;
676 case ISD::SETOLT:
677 case ISD::SETOGT:
678 case ISD::SETOLE:
679 if (VecVT == MVT::v4f32)
680 return PPC::VCMPGTFP;
681 break;
682 case ISD::SETOGE:
683 if (VecVT == MVT::v4f32)
684 return PPC::VCMPGEFP;
685 break;
686 default:
687 break;
688 }
689 llvm_unreachable("Invalid integer vector compare condition");
690}
691
692// getVCmpEQInst: return the equal compare instruction for the specified vector
693// type. Since this is for altivec specific code, only support the altivec
694// types (v16i8, v8i16, v4i32, and v4f32).
695static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) {
696 switch (VecVT) {
697 case MVT::v16i8:
698 return PPC::VCMPEQUB;
699 case MVT::v8i16:
700 return PPC::VCMPEQUH;
701 case MVT::v4i32:
702 return PPC::VCMPEQUW;
703 case MVT::v4f32:
704 return PPC::VCMPEQFP;
705 default:
706 llvm_unreachable("Invalid integer vector compare condition");
707 }
708}
709
710
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000711SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000712 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000713 unsigned Imm;
714 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000715 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
716 bool isPPC64 = (PtrVT == MVT::i64);
717
Chris Lattner97b3da12006-06-27 00:04:13 +0000718 if (isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000719 // We can codegen setcc op, imm very efficiently compared to a brcond.
720 // Check for those cases here.
721 // setcc op, 0
722 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000723 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000724 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000725 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000726 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000727 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000728 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000729 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000730 }
Chris Lattnere2969492005-10-21 21:17:10 +0000731 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000732 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000733 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000734 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000735 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000736 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000737 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000738 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000739 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000740 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000741 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000742 }
Chris Lattnere2969492005-10-21 21:17:10 +0000743 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000744 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000745 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
746 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000747 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000748 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000749 }
750 }
Chris Lattner491b8292005-10-06 19:03:35 +0000751 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000752 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000753 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000754 default: break;
755 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000756 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000757 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000758 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000759 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
760 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000761 MVT::i32,
762 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000763 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000764 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000765 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000766 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000767 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000768 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000769 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000770 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000771 }
Chris Lattnere2969492005-10-21 21:17:10 +0000772 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000773 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
774 getI32Imm(1)), 0);
775 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
776 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000777 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000778 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000779 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000780 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000781 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000782 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000783 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000784 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000785 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000786 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000787 }
Chris Lattner491b8292005-10-06 19:03:35 +0000788 }
789 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000790
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000791 SDValue LHS = N->getOperand(0);
792 SDValue RHS = N->getOperand(1);
793
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000794 // Altivec Vector compare instructions do not set any CR register by default and
795 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000796 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000797 EVT VecVT = LHS.getValueType();
798 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
799 unsigned int VCmpInst = getVCmpInst(VT, CC);
800
801 switch (CC) {
802 case ISD::SETEQ:
803 case ISD::SETOEQ:
804 case ISD::SETUEQ:
805 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
806 case ISD::SETNE:
807 case ISD::SETONE:
808 case ISD::SETUNE: {
809 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
810 return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp);
811 }
812 case ISD::SETLT:
813 case ISD::SETOLT:
814 case ISD::SETULT:
815 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
816 case ISD::SETGT:
817 case ISD::SETOGT:
818 case ISD::SETUGT:
819 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
820 case ISD::SETGE:
821 case ISD::SETOGE:
822 case ISD::SETUGE: {
823 // Small optimization: Altivec provides a 'Vector Compare Greater Than
824 // or Equal To' instruction (vcmpgefp), so in this case there is no
825 // need for extra logic for the equal compare.
826 if (VecVT.getSimpleVT().isFloatingPoint()) {
827 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
828 } else {
829 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
830 unsigned int VCmpEQInst = getVCmpEQInst(VT);
831 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
832 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ);
833 }
834 }
835 case ISD::SETLE:
836 case ISD::SETOLE:
837 case ISD::SETULE: {
838 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
839 unsigned int VCmpEQInst = getVCmpEQInst(VT);
840 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
841 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ);
842 }
843 default:
844 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
845 }
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000846 }
847
Chris Lattner491b8292005-10-06 19:03:35 +0000848 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000849 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000850 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000851 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000852
Chris Lattner491b8292005-10-06 19:03:35 +0000853 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000854 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000855
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000856 SDValue InFlag(0, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000857 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000858 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000859
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000860 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
861 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000862
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000863 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000864 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000865 if (!Inv)
Owen Anderson9f944592009-08-11 20:47:22 +0000866 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner89f36e62008-01-08 06:46:30 +0000867
868 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000869 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000870 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000871 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000872}
Chris Lattner502a3692005-10-06 18:56:10 +0000873
Chris Lattner318622f2005-10-06 19:07:45 +0000874
Chris Lattner43ff01e2005-08-17 19:33:03 +0000875// Select - Convert the specified operand from a target-independent to a
876// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000877SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000878 SDLoc dl(N);
Dan Gohman17059682008-07-17 19:10:17 +0000879 if (N->isMachineOpcode())
Evan Chengbd1c5a82006-08-11 09:08:15 +0000880 return NULL; // Already selected.
Chris Lattner08c319f2005-09-29 00:59:32 +0000881
Chris Lattner43ff01e2005-08-17 19:33:03 +0000882 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000883 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000884
Jim Laskey095e6f32006-12-12 13:23:43 +0000885 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000886 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000887 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000888 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000889 // Assume no remaining bits.
890 unsigned Remainder = 0;
891 // Assume no shift required.
892 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000893
Jim Laskey095e6f32006-12-12 13:23:43 +0000894 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000895 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000896 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000897 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000898
Jim Laskey095e6f32006-12-12 13:23:43 +0000899 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000900 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000901 // Go with the shifted value.
902 Imm = ImmSh;
903 } else {
904 // Still stuck with a 64 bit value.
905 Remainder = Imm;
906 Shift = 32;
907 Imm >>= 32;
908 }
909 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000910
Jim Laskey095e6f32006-12-12 13:23:43 +0000911 // Intermediate operand.
912 SDNode *Result;
913
914 // Handle first 32 bits.
915 unsigned Lo = Imm & 0xFFFF;
916 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000917
Jim Laskey095e6f32006-12-12 13:23:43 +0000918 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000919 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000920 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000921 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000922 } else if (Lo) {
923 // Handle the Hi bits.
924 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000925 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000926 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000927 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
928 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000929 } else {
930 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000931 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000932 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000933
Jim Laskey095e6f32006-12-12 13:23:43 +0000934 // If no shift, we're done.
935 if (!Shift) return Result;
936
937 // Shift for next step if the upper 32-bits were not zero.
938 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000939 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
940 SDValue(Result, 0),
941 getI32Imm(Shift),
942 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000943 }
944
945 // Add in the last bits as required.
946 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000947 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
948 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000949 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000950 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000951 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
952 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000953 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000954
Jim Laskey095e6f32006-12-12 13:23:43 +0000955 return Result;
956 }
957 break;
958 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000959
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000960 case ISD::SETCC:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000961 return SelectSETCC(N);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000962 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000963 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000964
Chris Lattnere4c338d2005-08-25 00:45:43 +0000965 case ISD::FrameIndex: {
966 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000967 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
968 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +0000969 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000970 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000971 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000972 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +0000973 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +0000974 }
Chris Lattner6961fc72006-03-26 10:06:40 +0000975
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000976 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000977 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000978 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
979 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +0000980 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000981
Chris Lattner57693112005-09-28 22:50:24 +0000982 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +0000983 // FIXME: since this depends on the setting of the carry flag from the srawi
984 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +0000985 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +0000986 // srl/add/sra pattern the dag combiner will generate for this as
987 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +0000988 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +0000989 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000990 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +0000991 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +0000992 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000993 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000994 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +0000995 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000996 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +0000997 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +0000998 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000999 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001000 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001001 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001002 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1003 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001004 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001005 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001006 }
1007 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001008
Chris Lattner1de57062005-09-29 23:33:31 +00001009 // Other cases are autogenerated.
1010 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001011 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001012
Chris Lattnerce645542006-11-10 02:08:47 +00001013 case ISD::LOAD: {
1014 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001015 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001016 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001017
Chris Lattnerce645542006-11-10 02:08:47 +00001018 // Normal loads are handled by code generated from the .td file.
1019 if (LD->getAddressingMode() != ISD::PRE_INC)
1020 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001021
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001022 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001023 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001024 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001025
Chris Lattner474b5b72006-11-15 19:55:13 +00001026 unsigned Opcode;
1027 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001028 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001029 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001030 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1031 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001032 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001033 case MVT::f64: Opcode = PPC::LFDU; break;
1034 case MVT::f32: Opcode = PPC::LFSU; break;
1035 case MVT::i32: Opcode = PPC::LWZU; break;
1036 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1037 case MVT::i1:
1038 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001039 }
1040 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001041 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1042 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1043 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001044 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001045 case MVT::i64: Opcode = PPC::LDU; break;
1046 case MVT::i32: Opcode = PPC::LWZU8; break;
1047 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1048 case MVT::i1:
1049 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001050 }
1051 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001052
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001053 SDValue Chain = LD->getChain();
1054 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001055 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001056 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1057 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001058 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001059 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001060 unsigned Opcode;
1061 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1062 if (LD->getValueType(0) != MVT::i64) {
1063 // Handle PPC32 integer and normal FP loads.
1064 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1065 switch (LoadedVT.getSimpleVT().SimpleTy) {
1066 default: llvm_unreachable("Invalid PPC load type!");
1067 case MVT::f64: Opcode = PPC::LFDUX; break;
1068 case MVT::f32: Opcode = PPC::LFSUX; break;
1069 case MVT::i32: Opcode = PPC::LWZUX; break;
1070 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1071 case MVT::i1:
1072 case MVT::i8: Opcode = PPC::LBZUX; break;
1073 }
1074 } else {
1075 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1076 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1077 "Invalid sext update load");
1078 switch (LoadedVT.getSimpleVT().SimpleTy) {
1079 default: llvm_unreachable("Invalid PPC load type!");
1080 case MVT::i64: Opcode = PPC::LDUX; break;
1081 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1082 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1083 case MVT::i1:
1084 case MVT::i8: Opcode = PPC::LBZUX8; break;
1085 }
1086 }
1087
1088 SDValue Chain = LD->getChain();
1089 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001090 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001091 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1092 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001093 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001094 }
1095 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001096
Nate Begemanb3821a32005-08-18 07:30:46 +00001097 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001098 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001099 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001100
Nate Begemanb3821a32005-08-18 07:30:46 +00001101 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1102 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001103 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001104 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001105 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001106 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001107 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemanb3821a32005-08-18 07:30:46 +00001108 }
Nate Begemand31efd12006-09-22 05:01:56 +00001109 // If this is just a masked value where the input is not handled above, and
1110 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1111 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001112 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001113 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001115 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001116 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemand31efd12006-09-22 05:01:56 +00001117 }
Hal Finkele39526a2012-08-28 02:10:15 +00001118 // If this is a 64-bit zero-extension mask, emit rldicl.
1119 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1120 isMask_64(Imm64)) {
1121 SDValue Val = N->getOperand(0);
1122 MB = 64 - CountTrailingOnes_64(Imm64);
1123 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB) };
1124 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1125 }
Nate Begemand31efd12006-09-22 05:01:56 +00001126 // AND X, 0 -> 0, not "rlwinm 32".
1127 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001128 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Nate Begemand31efd12006-09-22 05:01:56 +00001129 return NULL;
1130 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001131 // ISD::OR doesn't get all the bitfield insertion fun.
1132 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001133 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001134 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001135 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001136 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001137 Imm = ~(Imm^Imm2);
1138 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001139 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001140 N->getOperand(0).getOperand(1),
1141 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001142 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001143 }
1144 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001145
Chris Lattner1de57062005-09-29 23:33:31 +00001146 // Other cases are autogenerated.
1147 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001148 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001149 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001150 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001151 if (SDNode *I = SelectBitfieldInsert(N))
1152 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001153
Chris Lattner1de57062005-09-29 23:33:31 +00001154 // Other cases are autogenerated.
1155 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001156 case ISD::SHL: {
1157 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001158 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001159 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001160 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001161 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001162 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001163 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001164
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001165 // Other cases are autogenerated.
1166 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001167 }
1168 case ISD::SRL: {
1169 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001170 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001171 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001172 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001173 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001174 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001175 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001176
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001177 // Other cases are autogenerated.
1178 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001179 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001180 case ISD::SELECT_CC: {
1181 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001182 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1183 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001184
Chris Lattner97b3da12006-06-27 00:04:13 +00001185 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001186 if (!isPPC64)
1187 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1188 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1189 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1190 if (N1C->isNullValue() && N3C->isNullValue() &&
1191 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1192 // FIXME: Implement this optzn for PPC64.
1193 N->getValueType(0) == MVT::i32) {
1194 SDNode *Tmp =
1195 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1196 N->getOperand(0), getI32Imm(~0U));
1197 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1198 SDValue(Tmp, 0), N->getOperand(0),
1199 SDValue(Tmp, 1));
1200 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001201
Dale Johannesenab8e4422009-02-06 19:16:40 +00001202 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001203 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001204
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001205 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001206 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001207 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001208 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001209 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001210 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001211 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001212 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001213 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001214 else
1215 SelectCCOp = PPC::SELECT_CC_VRRC;
1216
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001217 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001218 getI32Imm(BROpc) };
1219 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001220 }
Hal Finkel25c19922013-05-15 21:37:41 +00001221 case PPCISD::BDNZ:
1222 case PPCISD::BDZ: {
1223 bool IsPPC64 = PPCSubTarget.isPPC64();
1224 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1225 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1226 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1227 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1228 MVT::Other, Ops, 2);
1229 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001230 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001231 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001232 // Op #1 is the PPC::PRED_* number.
1233 // Op #2 is the CR#
1234 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001235 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001236 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001237 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001238 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001239 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001240 N->getOperand(0), N->getOperand(4) };
Owen Anderson9f944592009-08-11 20:47:22 +00001241 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001242 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001243 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001244 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dale Johannesenab8e4422009-02-06 19:16:40 +00001245 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Andrew Trickc416ba62010-12-24 04:28:06 +00001246 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001247 N->getOperand(4), N->getOperand(0) };
Owen Anderson9f944592009-08-11 20:47:22 +00001248 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001249 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001250 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001251 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001252 SDValue Chain = N->getOperand(0);
1253 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001254 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001255 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001256 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001257 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001258 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001259 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001260 case PPCISD::TOC_ENTRY: {
1261 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1262
Bill Schmidt27917782013-02-21 17:12:27 +00001263 // For medium and large code model, we generate two instructions as
1264 // described below. Otherwise we allow SelectCodeCommon to handle this,
1265 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1266 CodeModel::Model CModel = TM.getCodeModel();
1267 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001268 break;
1269
1270 // The first source operand is a TargetGlobalAddress or a
1271 // TargetJumpTable. If it is an externally defined symbol, a symbol
1272 // with common linkage, a function address, or a jump table address,
Bill Schmidt27917782013-02-21 17:12:27 +00001273 // or if we are generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001274 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1275 // Otherwise we generate:
1276 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1277 SDValue GA = N->getOperand(0);
1278 SDValue TOCbase = N->getOperand(1);
1279 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1280 TOCbase, GA);
1281
Bill Schmidt27917782013-02-21 17:12:27 +00001282 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001283 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1284 SDValue(Tmp, 0));
1285
1286 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1287 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001288 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
1289 const GlobalValue *RealGValue = GAlias ?
1290 GAlias->resolveAliasedGlobal(false) : GValue;
1291 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1292 assert((GVar || isa<Function>(RealGValue)) &&
Bill Schmidt34627e32012-11-27 17:35:46 +00001293 "Unexpected global value subclass!");
1294
1295 // An external variable is one without an initializer. For these,
1296 // for variables with common linkage, and for Functions, generate
1297 // the LDtocL form.
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001298 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1299 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001300 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1301 SDValue(Tmp, 0));
1302 }
1303
1304 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1305 SDValue(Tmp, 0), GA);
1306 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001307 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001308 // This expands into one of three sequences, depending on whether
1309 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001310 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1311 isa<ConstantSDNode>(N->getOperand(1)) &&
1312 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001313
1314 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001315 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001316 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001317 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001318
Bill Schmidt51e79512013-02-20 15:50:31 +00001319 if (EltSize == 1) {
1320 Opc1 = PPC::VSPLTISB;
1321 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001322 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001323 VT = MVT::v16i8;
1324 } else if (EltSize == 2) {
1325 Opc1 = PPC::VSPLTISH;
1326 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001327 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001328 VT = MVT::v8i16;
1329 } else {
1330 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1331 Opc1 = PPC::VSPLTISW;
1332 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001333 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001334 VT = MVT::v4i32;
1335 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001336
1337 if ((Elt & 1) == 0) {
1338 // Elt is even, in the range [-32,-18] + [16,30].
1339 //
1340 // Convert: VADD_SPLAT elt, size
1341 // Into: tmp = VSPLTIS[BHW] elt
1342 // VADDU[BHW]M tmp, tmp
1343 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1344 SDValue EltVal = getI32Imm(Elt >> 1);
1345 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1346 SDValue TmpVal = SDValue(Tmp, 0);
1347 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1348
1349 } else if (Elt > 0) {
1350 // Elt is odd and positive, in the range [17,31].
1351 //
1352 // Convert: VADD_SPLAT elt, size
1353 // Into: tmp1 = VSPLTIS[BHW] elt-16
1354 // tmp2 = VSPLTIS[BHW] -16
1355 // VSUBU[BHW]M tmp1, tmp2
1356 SDValue EltVal = getI32Imm(Elt - 16);
1357 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1358 EltVal = getI32Imm(-16);
1359 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1360 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1361 SDValue(Tmp2, 0));
1362
1363 } else {
1364 // Elt is odd and negative, in the range [-31,-17].
1365 //
1366 // Convert: VADD_SPLAT elt, size
1367 // Into: tmp1 = VSPLTIS[BHW] elt+16
1368 // tmp2 = VSPLTIS[BHW] -16
1369 // VADDU[BHW]M tmp1, tmp2
1370 SDValue EltVal = getI32Imm(Elt + 16);
1371 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1372 EltVal = getI32Imm(-16);
1373 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1374 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1375 SDValue(Tmp2, 0));
1376 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001377 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001378 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001379
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001380 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001381}
1382
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001383/// PostProcessISelDAG - Perform some late peephole optimizations
1384/// on the DAG representation.
1385void PPCDAGToDAGISel::PostprocessISelDAG() {
1386
1387 // Skip peepholes at -O0.
1388 if (TM.getOptLevel() == CodeGenOpt::None)
1389 return;
1390
1391 // These optimizations are currently supported only for 64-bit SVR4.
1392 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
1393 return;
1394
1395 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
1396 ++Position;
1397
1398 while (Position != CurDAG->allnodes_begin()) {
1399 SDNode *N = --Position;
1400 // Skip dead nodes and any non-machine opcodes.
1401 if (N->use_empty() || !N->isMachineOpcode())
1402 continue;
1403
1404 unsigned FirstOp;
1405 unsigned StorageOpcode = N->getMachineOpcode();
1406
1407 switch (StorageOpcode) {
1408 default: continue;
1409
1410 case PPC::LBZ:
1411 case PPC::LBZ8:
1412 case PPC::LD:
1413 case PPC::LFD:
1414 case PPC::LFS:
1415 case PPC::LHA:
1416 case PPC::LHA8:
1417 case PPC::LHZ:
1418 case PPC::LHZ8:
1419 case PPC::LWA:
1420 case PPC::LWZ:
1421 case PPC::LWZ8:
1422 FirstOp = 0;
1423 break;
1424
1425 case PPC::STB:
1426 case PPC::STB8:
1427 case PPC::STD:
1428 case PPC::STFD:
1429 case PPC::STFS:
1430 case PPC::STH:
1431 case PPC::STH8:
1432 case PPC::STW:
1433 case PPC::STW8:
1434 FirstOp = 1;
1435 break;
1436 }
1437
1438 // If this is a load or store with a zero offset, we may be able to
1439 // fold an add-immediate into the memory operation.
1440 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
1441 N->getConstantOperandVal(FirstOp) != 0)
1442 continue;
1443
1444 SDValue Base = N->getOperand(FirstOp + 1);
1445 if (!Base.isMachineOpcode())
1446 continue;
1447
1448 unsigned Flags = 0;
1449 bool ReplaceFlags = true;
1450
1451 // When the feeding operation is an add-immediate of some sort,
1452 // determine whether we need to add relocation information to the
1453 // target flags on the immediate operand when we fold it into the
1454 // load instruction.
1455 //
1456 // For something like ADDItocL, the relocation information is
1457 // inferred from the opcode; when we process it in the AsmPrinter,
1458 // we add the necessary relocation there. A load, though, can receive
1459 // relocation from various flavors of ADDIxxx, so we need to carry
1460 // the relocation information in the target flags.
1461 switch (Base.getMachineOpcode()) {
1462 default: continue;
1463
1464 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00001465 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001466 // In some cases (such as TLS) the relocation information
1467 // is already in place on the operand, so copying the operand
1468 // is sufficient.
1469 ReplaceFlags = false;
1470 // For these cases, the immediate may not be divisible by 4, in
1471 // which case the fold is illegal for DS-form instructions. (The
1472 // other cases provide aligned addresses and are always safe.)
1473 if ((StorageOpcode == PPC::LWA ||
1474 StorageOpcode == PPC::LD ||
1475 StorageOpcode == PPC::STD) &&
1476 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
1477 Base.getConstantOperandVal(1) % 4 != 0))
1478 continue;
1479 break;
1480 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001481 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001482 break;
1483 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001484 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001485 break;
1486 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00001487 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001488 break;
1489 }
1490
1491 // We found an opportunity. Reverse the operands from the add
1492 // immediate and substitute them into the load or store. If
1493 // needed, update the target flags for the immediate operand to
1494 // reflect the necessary relocation information.
1495 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
1496 DEBUG(Base->dump(CurDAG));
1497 DEBUG(dbgs() << "\nN: ");
1498 DEBUG(N->dump(CurDAG));
1499 DEBUG(dbgs() << "\n");
1500
1501 SDValue ImmOpnd = Base.getOperand(1);
1502
1503 // If the relocation information isn't already present on the
1504 // immediate operand, add it now.
1505 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00001506 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001507 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001508 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00001509 // We can't perform this optimization for data whose alignment
1510 // is insufficient for the instruction encoding.
1511 if (GV->getAlignment() < 4 &&
1512 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
1513 StorageOpcode == PPC::LWA)) {
1514 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
1515 continue;
1516 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001517 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00001518 } else if (ConstantPoolSDNode *CP =
1519 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00001520 const Constant *C = CP->getConstVal();
1521 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
1522 CP->getAlignment(),
1523 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001524 }
1525 }
1526
1527 if (FirstOp == 1) // Store
1528 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
1529 Base.getOperand(0), N->getOperand(3));
1530 else // Load
1531 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
1532 N->getOperand(2));
1533
1534 // The add-immediate may now be dead, in which case remove it.
1535 if (Base.getNode()->use_empty())
1536 CurDAG->RemoveDeadNode(Base.getNode());
1537 }
1538}
Chris Lattner43ff01e2005-08-17 19:33:03 +00001539
Chris Lattnerb055c872006-06-10 01:15:02 +00001540
Andrew Trickc416ba62010-12-24 04:28:06 +00001541/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00001542/// PowerPC-specific DAG, ready for instruction scheduling.
1543///
Evan Cheng2dd2c652006-03-13 23:20:37 +00001544FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00001545 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001546}
1547
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00001548static void initializePassOnce(PassRegistry &Registry) {
1549 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
1550 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
1551 false, false);
1552 Registry.registerPass(*PI, true);
1553}
1554
1555void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
1556 CALL_ONCE_INITIALIZATION(initializePassOnce);
1557}
1558