blob: de77ef94af5a673a4c73c9a25bd09ee86afc7129 [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"
Hal Finkel940ab932014-02-28 00:27:01 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000031#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000036using namespace llvm;
37
Hal Finkel940ab932014-02-28 00:27:01 +000038// FIXME: Remove this once the bug has been fixed!
39cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
40cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
41
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000042namespace llvm {
43 void initializePPCDAGToDAGISelPass(PassRegistry&);
44}
45
Chris Lattner43ff01e2005-08-17 19:33:03 +000046namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000047 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000048 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000049 /// instructions for SelectionDAG operations.
50 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000051 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000052 const PPCTargetMachine &TM;
53 const PPCTargetLowering &PPCLowering;
Evan Chengec271b12007-10-23 06:42:42 +000054 const PPCSubtarget &PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000055 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000056 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000057 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman619ef482009-01-15 19:20:50 +000058 : SelectionDAGISel(tm), TM(tm),
Evan Chengec271b12007-10-23 06:42:42 +000059 PPCLowering(*TM.getTargetLowering()),
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000060 PPCSubTarget(*TM.getSubtargetImpl()) {
61 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
62 }
Andrew Trickc416ba62010-12-24 04:28:06 +000063
Dan Gohman5ea74d52009-07-31 18:16:33 +000064 virtual bool runOnMachineFunction(MachineFunction &MF) {
Chris Lattner45640392005-08-19 22:38:53 +000065 // Make sure we re-emit a set of the global base reg if necessary
66 GlobalBaseReg = 0;
Dan Gohman5ea74d52009-07-31 18:16:33 +000067 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000068
Bill Schmidt38d94582012-10-10 20:54:15 +000069 if (!PPCSubTarget.isSVR4ABI())
70 InsertVRSaveCode(MF);
71
Chris Lattner1678a6c2006-03-16 18:25:23 +000072 return true;
Chris Lattner45640392005-08-19 22:38:53 +000073 }
Andrew Trickc416ba62010-12-24 04:28:06 +000074
Bill Schmidtf5b474c2013-02-21 00:38:25 +000075 virtual void PostprocessISelDAG();
76
Chris Lattner43ff01e2005-08-17 19:33:03 +000077 /// getI32Imm - Return a target constant with the specified value, of type
78 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000079 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000080 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000081 }
Chris Lattner45640392005-08-19 22:38:53 +000082
Chris Lattner97b3da12006-06-27 00:04:13 +000083 /// getI64Imm - Return a target constant with the specified value, of type
84 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000085 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000086 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000087 }
Andrew Trickc416ba62010-12-24 04:28:06 +000088
Chris Lattner97b3da12006-06-27 00:04:13 +000089 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000090 inline SDValue getSmallIPtrImm(unsigned Imm) {
Chris Lattner97b3da12006-06-27 00:04:13 +000091 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
92 }
Andrew Trickc416ba62010-12-24 04:28:06 +000093
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000094 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000095 /// with any number of 0s on either side. The 1s are allowed to wrap from
96 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
97 /// 0x0F0F0000 is not, since all 1s are not contiguous.
98 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
99
100
101 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
102 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000103 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000104 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000105
Chris Lattner45640392005-08-19 22:38:53 +0000106 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
107 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000108 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000109
Chris Lattner43ff01e2005-08-17 19:33:03 +0000110 // Select - Convert the specified operand from a target-independent to a
111 // target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000112 SDNode *Select(SDNode *N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000113
Nate Begeman93c4bc62005-08-19 00:38:14 +0000114 SDNode *SelectBitfieldInsert(SDNode *N);
115
Chris Lattner2a1823d2005-08-21 18:50:37 +0000116 /// SelectCC - Select a comparison of the specified values with the
117 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000118 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000119
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000120 /// SelectAddrImm - Returns true if the address N can be represented by
121 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000122 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000123 SDValue &Base) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000124 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000125 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000126
Chris Lattner6f5840c2006-11-16 00:41:37 +0000127 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000128 /// immediate field. Note that the operand at this point is already the
129 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000130 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000131 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000132 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000133 Out = N;
134 return true;
135 }
136
137 return false;
138 }
139
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000140 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
141 /// represented as an indexed [r+r] operation. Returns false if it can
142 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000143 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000144 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
145 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000146
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000147 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
148 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000149 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000150 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
151 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000152
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000153 /// SelectAddrImmX4 - Returns true if the address N can be represented by
154 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
155 /// Suitable for use by STD and friends.
156 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
157 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000158 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000159
Hal Finkel756810f2013-03-21 21:37:52 +0000160 // Select an address into a single register.
161 bool SelectAddr(SDValue N, SDValue &Base) {
162 Base = N;
163 return true;
164 }
165
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000166 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000167 /// inline asm expressions. It is always correct to compute the value into
168 /// a register. The case of adding a (possibly relocatable) constant to a
169 /// register can be improved, but it is wrong to substitute Reg+Reg for
170 /// Reg in an asm, because the load or store opcode would have to change.
171 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000172 char ConstraintCode,
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000173 std::vector<SDValue> &OutOps) {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000174 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000175 return false;
176 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000177
Dan Gohman5ea74d52009-07-31 18:16:33 +0000178 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000179
Chris Lattner43ff01e2005-08-17 19:33:03 +0000180 virtual const char *getPassName() const {
181 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000182 }
183
Chris Lattner03e08ee2005-09-13 22:03:06 +0000184// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000185#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000186
Chris Lattner259e6c72005-10-06 18:45:51 +0000187private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000188 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000189
190 void PeepholePPC64();
191 void PeepholdCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000192
193 bool AllUsersSelectZero(SDNode *N);
194 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000195 };
196}
197
Chris Lattner1678a6c2006-03-16 18:25:23 +0000198/// InsertVRSaveCode - Once the entire function has been instruction selected,
199/// all virtual registers are created and all machine instructions are built,
200/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000201void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000202 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000203 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000204 //
Dan Gohman4a618822010-02-10 16:03:48 +0000205 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000206 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000207 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000208 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
209 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
210 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000211 HasVectorVReg = true;
212 break;
213 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000214 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000215 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000216
Chris Lattner02e2c182006-03-13 21:52:10 +0000217 // If we have a vector register, we want to emit code into the entry and exit
218 // blocks to save and restore the VRSAVE register. We do this here (instead
219 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
220 //
221 // 1. This (trivially) reduces the load on the register allocator, by not
222 // having to represent the live range of the VRSAVE register.
223 // 2. This (more significantly) allows us to create a temporary virtual
224 // register to hold the saved VRSAVE value, allowing this temporary to be
225 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000226
227 // Create two vregs - one to hold the VRSAVE register that is live-in to the
228 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000229 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
230 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000231
Evan Cheng20350c42006-11-27 23:37:22 +0000232 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000233 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000234 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000235 // Emit the following code into the entry block:
236 // InVRSAVE = MFVRSAVE
237 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
238 // MTVRSAVE UpdatedVRSAVE
239 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000240 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
241 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000242 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000243 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000244
Chris Lattner1678a6c2006-03-16 18:25:23 +0000245 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000246 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000247 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000248 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000249
Chris Lattner1678a6c2006-03-16 18:25:23 +0000250 // Skip over all terminator instructions, which are part of the return
251 // sequence.
252 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000253 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000254 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000255
Chris Lattner1678a6c2006-03-16 18:25:23 +0000256 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000257 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000258 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000259 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000260}
Chris Lattner8ae95252005-09-03 01:17:22 +0000261
Chris Lattner1678a6c2006-03-16 18:25:23 +0000262
Chris Lattner45640392005-08-19 22:38:53 +0000263/// getGlobalBaseReg - Output the instructions required to put the
264/// base address to use for accessing globals into a register.
265///
Evan Cheng61413a32006-08-26 05:34:46 +0000266SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000267 if (!GlobalBaseReg) {
Evan Cheng20350c42006-11-27 23:37:22 +0000268 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000269 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000270 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000271 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000272 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000273
Owen Anderson9f944592009-08-11 20:47:22 +0000274 if (PPCLowering.getPointerTy() == MVT::i32) {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000275 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000276 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000277 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000278 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000279 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000280 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000281 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000282 }
Chris Lattner45640392005-08-19 22:38:53 +0000283 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000284 return CurDAG->getRegister(GlobalBaseReg,
285 PPCLowering.getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000286}
287
288/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
289/// or 64-bit immediate, and if the value can be accurately represented as a
290/// sign extension from a 16-bit value. If so, this returns true and the
291/// immediate.
292static bool isIntS16Immediate(SDNode *N, short &Imm) {
293 if (N->getOpcode() != ISD::Constant)
294 return false;
295
Dan Gohmaneffb8942008-09-12 16:56:44 +0000296 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000297 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000298 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000299 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000300 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000301}
302
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000303static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000304 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000305}
306
307
Chris Lattner97b3da12006-06-27 00:04:13 +0000308/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
309/// operand. If so Imm will receive the 32-bit value.
310static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000311 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000312 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000313 return true;
314 }
315 return false;
316}
317
Chris Lattner97b3da12006-06-27 00:04:13 +0000318/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
319/// operand. If so Imm will receive the 64-bit value.
320static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000321 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000322 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000323 return true;
324 }
325 return false;
326}
327
328// isInt32Immediate - This method tests to see if a constant operand.
329// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000330static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000331 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000332}
333
334
335// isOpcWithIntImmediate - This method tests to see if the node is a specific
336// opcode and that it has a immediate integer right operand.
337// If so Imm will receive the 32 bit value.
338static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000339 return N->getOpcode() == Opc
340 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000341}
342
Nate Begemand31efd12006-09-22 05:01:56 +0000343bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000344 if (!Val)
345 return false;
346
Nate Begemanb3821a32005-08-18 07:30:46 +0000347 if (isShiftedMask_32(Val)) {
348 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000349 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000350 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000351 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000352 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000353 } else {
354 Val = ~Val; // invert mask
355 if (isShiftedMask_32(Val)) {
356 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000357 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000358 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000359 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000360 return true;
361 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000362 }
363 // no run present
364 return false;
365}
366
Andrew Trickc416ba62010-12-24 04:28:06 +0000367bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
368 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000369 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000370 // Don't even go down this path for i64, since different logic will be
371 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000372 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000373 return false;
374
Nate Begemanb3821a32005-08-18 07:30:46 +0000375 unsigned Shift = 32;
376 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
377 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000378 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000379 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000380 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000381
Nate Begemanb3821a32005-08-18 07:30:46 +0000382 if (Opcode == ISD::SHL) {
383 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000384 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 // determine which bits are made indeterminant by shift
386 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000387 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000388 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000389 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000390 // determine which bits are made indeterminant by shift
391 Indeterminant = ~(0xFFFFFFFFu >> Shift);
392 // adjust for the left rotate
393 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000394 } else if (Opcode == ISD::ROTL) {
395 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000396 } else {
397 return false;
398 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000399
Nate Begemanb3821a32005-08-18 07:30:46 +0000400 // if the mask doesn't intersect any Indeterminant bits
401 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000402 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000403 // make sure the mask is still a mask (wrap arounds may not be)
404 return isRunOfOnes(Mask, MB, ME);
405 }
406 return false;
407}
408
Nate Begeman93c4bc62005-08-19 00:38:14 +0000409/// SelectBitfieldInsert - turn an or of two masked values into
410/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000411SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000412 SDValue Op0 = N->getOperand(0);
413 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000414 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000415
Dan Gohmanf19609a2008-02-27 01:23:58 +0000416 APInt LKZ, LKO, RKZ, RKO;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000417 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
418 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000419
Dan Gohmanf19609a2008-02-27 01:23:58 +0000420 unsigned TargetMask = LKZ.getZExtValue();
421 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000422
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000423 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
424 unsigned Op0Opc = Op0.getOpcode();
425 unsigned Op1Opc = Op1.getOpcode();
426 unsigned Value, SH = 0;
427 TargetMask = ~TargetMask;
428 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000429
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000430 // If the LHS has a foldable shift and the RHS does not, then swap it to the
431 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000432 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
433 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
434 Op0.getOperand(0).getOpcode() == ISD::SRL) {
435 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
436 Op1.getOperand(0).getOpcode() != ISD::SRL) {
437 std::swap(Op0, Op1);
438 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000439 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000440 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000441 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000442 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
443 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
444 Op1.getOperand(0).getOpcode() != ISD::SRL) {
445 std::swap(Op0, Op1);
446 std::swap(Op0Opc, Op1Opc);
447 std::swap(TargetMask, InsertMask);
448 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000449 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000450
Nate Begeman1333cea2006-05-07 00:23:38 +0000451 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000452 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000453 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000454
455 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000456 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000457 Op1 = Op1.getOperand(0);
458 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
459 }
460 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000461 // The AND mask might not be a constant, and we need to make sure that
462 // if we're going to fold the masking with the insert, all bits not
463 // know to be zero in the mask are known to be one.
464 APInt MKZ, MKO;
465 CurDAG->ComputeMaskedBits(Op1.getOperand(1), MKZ, MKO);
466 bool CanFoldMask = InsertMask == MKO.getZExtValue();
467
Nate Begeman1333cea2006-05-07 00:23:38 +0000468 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000469 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000470 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Hal Finkel4ca70102013-06-28 20:00:07 +0000471 // Note that Value must be in range here (less than 32) because
472 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000473 Op1 = Op1.getOperand(0).getOperand(0);
474 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000475 }
476 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000477
Chris Lattnera2963392006-05-12 16:29:37 +0000478 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000479 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000480 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000481 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000482 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000483 }
484 return 0;
485}
486
Chris Lattner2a1823d2005-08-21 18:50:37 +0000487/// SelectCC - Select a comparison of the specified values with the specified
488/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000489SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000490 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000491 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000492 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000493
Owen Anderson9f944592009-08-11 20:47:22 +0000494 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000495 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000496 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
497 if (isInt32Immediate(RHS, Imm)) {
498 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000499 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000500 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
501 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000502 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000503 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000504 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
505 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000506
Chris Lattneraa3926b2006-09-20 04:25:47 +0000507 // For non-equality comparisons, the default code would materialize the
508 // constant, then compare against it, like this:
509 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000510 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000511 // cmpw cr0, r3, r2
512 // Since we are just comparing for equality, we can emit this instead:
513 // xoris r0,r3,0x1234
514 // cmplwi cr0,r0,0x5678
515 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000516 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
517 getI32Imm(Imm >> 16)), 0);
518 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
519 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000520 }
521 Opc = PPC::CMPLW;
522 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000523 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000524 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
525 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000526 Opc = PPC::CMPLW;
527 } else {
528 short SImm;
529 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000530 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
531 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000532 0);
533 Opc = PPC::CMPW;
534 }
Owen Anderson9f944592009-08-11 20:47:22 +0000535 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000536 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000537 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000538 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000539 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000540 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000541 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
542 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000543 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000544 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000545 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
546 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000547
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000548 // For non-equality comparisons, the default code would materialize the
549 // constant, then compare against it, like this:
550 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000551 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000552 // cmpd cr0, r3, r2
553 // Since we are just comparing for equality, we can emit this instead:
554 // xoris r0,r3,0x1234
555 // cmpldi cr0,r0,0x5678
556 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000557 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000558 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
559 getI64Imm(Imm >> 16)), 0);
560 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
561 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000562 }
563 }
564 Opc = PPC::CMPLD;
565 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000566 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000567 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
568 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000569 Opc = PPC::CMPLD;
570 } else {
571 short SImm;
572 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000573 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
574 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000575 0);
576 Opc = PPC::CMPD;
577 }
Owen Anderson9f944592009-08-11 20:47:22 +0000578 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000579 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000580 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000581 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Hal Finkel27774d92014-03-13 07:58:58 +0000582 Opc = PPCSubTarget.hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000583 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000584 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000585}
586
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000587static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000588 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000589 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000590 case ISD::SETONE:
591 case ISD::SETOLE:
592 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000593 llvm_unreachable("Should be lowered by legalize!");
594 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000595 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000596 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000597 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000598 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000599 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000600 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000601 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000602 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000603 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000604 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000605 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000606 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000607 case ISD::SETO: return PPC::PRED_NU;
608 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000609 // These two are invalid for floating point. Assume we have int.
610 case ISD::SETULT: return PPC::PRED_LT;
611 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000612 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000613}
614
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000615/// getCRIdxForSetCC - Return the index of the condition register field
616/// associated with the SetCC condition, and whether or not the field is
617/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000618static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000619 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000620 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000621 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000622 case ISD::SETOLT:
623 case ISD::SETLT: return 0; // Bit #0 = SETOLT
624 case ISD::SETOGT:
625 case ISD::SETGT: return 1; // Bit #1 = SETOGT
626 case ISD::SETOEQ:
627 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
628 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000629 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000630 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000631 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000632 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000633 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000634 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
635 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000636 case ISD::SETUEQ:
637 case ISD::SETOGE:
638 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000639 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000640 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000641 // These are invalid for floating point. Assume integer.
642 case ISD::SETULT: return 0;
643 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000644 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000645}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000646
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000647// getVCmpInst: return the vector compare instruction for the specified
648// vector type and condition code. Since this is for altivec specific code,
649// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000650static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC,
651 bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000652 switch (CC) {
653 case ISD::SETEQ:
654 case ISD::SETUEQ:
655 case ISD::SETNE:
656 case ISD::SETUNE:
657 if (VecVT == MVT::v16i8)
658 return PPC::VCMPEQUB;
659 else if (VecVT == MVT::v8i16)
660 return PPC::VCMPEQUH;
661 else if (VecVT == MVT::v4i32)
662 return PPC::VCMPEQUW;
663 // v4f32 != v4f32 could be translate to unordered not equal
664 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000665 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
666 else if (VecVT == MVT::v2f64)
667 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000668 break;
669 case ISD::SETLT:
670 case ISD::SETGT:
671 case ISD::SETLE:
672 case ISD::SETGE:
673 if (VecVT == MVT::v16i8)
674 return PPC::VCMPGTSB;
675 else if (VecVT == MVT::v8i16)
676 return PPC::VCMPGTSH;
677 else if (VecVT == MVT::v4i32)
678 return PPC::VCMPGTSW;
679 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000680 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
681 else if (VecVT == MVT::v2f64)
682 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000683 break;
684 case ISD::SETULT:
685 case ISD::SETUGT:
686 case ISD::SETUGE:
687 case ISD::SETULE:
688 if (VecVT == MVT::v16i8)
689 return PPC::VCMPGTUB;
690 else if (VecVT == MVT::v8i16)
691 return PPC::VCMPGTUH;
692 else if (VecVT == MVT::v4i32)
693 return PPC::VCMPGTUW;
694 break;
695 case ISD::SETOEQ:
696 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000697 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
698 else if (VecVT == MVT::v2f64)
699 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000700 break;
701 case ISD::SETOLT:
702 case ISD::SETOGT:
703 case ISD::SETOLE:
704 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000705 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
706 else if (VecVT == MVT::v2f64)
707 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000708 break;
709 case ISD::SETOGE:
710 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000711 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
712 else if (VecVT == MVT::v2f64)
713 return PPC::XVCMPGEDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000714 break;
715 default:
716 break;
717 }
718 llvm_unreachable("Invalid integer vector compare condition");
719}
720
721// getVCmpEQInst: return the equal compare instruction for the specified vector
722// type. Since this is for altivec specific code, only support the altivec
723// types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000724static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000725 switch (VecVT) {
726 case MVT::v16i8:
727 return PPC::VCMPEQUB;
728 case MVT::v8i16:
729 return PPC::VCMPEQUH;
730 case MVT::v4i32:
731 return PPC::VCMPEQUW;
732 case MVT::v4f32:
Hal Finkel27774d92014-03-13 07:58:58 +0000733 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
734 case MVT::v2f64:
735 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000736 default:
737 llvm_unreachable("Invalid integer vector compare condition");
738 }
739}
740
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000741SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000742 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000743 unsigned Imm;
744 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000745 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
746 bool isPPC64 = (PtrVT == MVT::i64);
747
Hal Finkel940ab932014-02-28 00:27:01 +0000748 if (!PPCSubTarget.useCRBits() &&
749 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000750 // We can codegen setcc op, imm very efficiently compared to a brcond.
751 // Check for those cases here.
752 // setcc op, 0
753 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000754 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000755 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000756 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000757 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000758 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000759 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000760 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000761 }
Chris Lattnere2969492005-10-21 21:17:10 +0000762 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000763 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000764 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000765 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000766 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000767 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000768 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000769 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000770 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000771 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000772 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Evan Chengc3acfc02006-08-27 08:14:06 +0000773 }
Chris Lattnere2969492005-10-21 21:17:10 +0000774 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000775 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000776 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
777 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000778 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000779 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000780 }
781 }
Chris Lattner491b8292005-10-06 19:03:35 +0000782 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000783 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000784 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000785 default: break;
786 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000787 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000788 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000789 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000790 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
791 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000792 MVT::i32,
793 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000794 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000795 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000796 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000797 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000798 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000799 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000800 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000801 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000802 }
Chris Lattnere2969492005-10-21 21:17:10 +0000803 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000804 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
805 getI32Imm(1)), 0);
806 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
807 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000808 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Owen Anderson9f944592009-08-11 20:47:22 +0000809 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattnere2969492005-10-21 21:17:10 +0000810 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000811 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000812 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000813 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000814 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000815 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000816 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000817 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000818 }
Chris Lattner491b8292005-10-06 19:03:35 +0000819 }
820 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000821
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000822 SDValue LHS = N->getOperand(0);
823 SDValue RHS = N->getOperand(1);
824
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000825 // Altivec Vector compare instructions do not set any CR register by default and
826 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000827 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000828 EVT VecVT = LHS.getValueType();
829 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
Hal Finkel27774d92014-03-13 07:58:58 +0000830 unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000831
832 switch (CC) {
833 case ISD::SETEQ:
834 case ISD::SETOEQ:
835 case ISD::SETUEQ:
836 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
837 case ISD::SETNE:
838 case ISD::SETONE:
839 case ISD::SETUNE: {
840 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000841 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLNOR :
842 PPC::VNOR,
843 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000844 }
845 case ISD::SETLT:
846 case ISD::SETOLT:
847 case ISD::SETULT:
848 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
849 case ISD::SETGT:
850 case ISD::SETOGT:
851 case ISD::SETUGT:
852 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
853 case ISD::SETGE:
854 case ISD::SETOGE:
855 case ISD::SETUGE: {
856 // Small optimization: Altivec provides a 'Vector Compare Greater Than
857 // or Equal To' instruction (vcmpgefp), so in this case there is no
858 // need for extra logic for the equal compare.
859 if (VecVT.getSimpleVT().isFloatingPoint()) {
860 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
861 } else {
862 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel27774d92014-03-13 07:58:58 +0000863 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000864 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000865 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
866 PPC::VOR,
867 VecVT, VCmpGT, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000868 }
869 }
870 case ISD::SETLE:
871 case ISD::SETOLE:
872 case ISD::SETULE: {
873 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
Hal Finkel27774d92014-03-13 07:58:58 +0000874 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000875 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000876 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
877 PPC::VOR,
878 VecVT, VCmpLE, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000879 }
880 default:
881 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
882 }
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000883 }
884
Hal Finkel940ab932014-02-28 00:27:01 +0000885 if (PPCSubTarget.useCRBits())
886 return 0;
887
Chris Lattner491b8292005-10-06 19:03:35 +0000888 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000889 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000890 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000891 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000892
Chris Lattner491b8292005-10-06 19:03:35 +0000893 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000894 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000895
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000896 SDValue InFlag(0, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000897 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000898 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000899
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000900 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
901 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000902
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000903 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000904 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000905 if (!Inv)
Owen Anderson9f944592009-08-11 20:47:22 +0000906 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Chris Lattner89f36e62008-01-08 06:46:30 +0000907
908 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000909 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000910 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000911 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000912}
Chris Lattner502a3692005-10-06 18:56:10 +0000913
Chris Lattner318622f2005-10-06 19:07:45 +0000914
Chris Lattner43ff01e2005-08-17 19:33:03 +0000915// Select - Convert the specified operand from a target-independent to a
916// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000917SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000918 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000919 if (N->isMachineOpcode()) {
920 N->setNodeId(-1);
Evan Chengbd1c5a82006-08-11 09:08:15 +0000921 return NULL; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000922 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000923
Chris Lattner43ff01e2005-08-17 19:33:03 +0000924 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000925 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000926
Jim Laskey095e6f32006-12-12 13:23:43 +0000927 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000928 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000929 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000930 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000931 // Assume no remaining bits.
932 unsigned Remainder = 0;
933 // Assume no shift required.
934 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000935
Jim Laskey095e6f32006-12-12 13:23:43 +0000936 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000937 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000938 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000939 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000940
Jim Laskey095e6f32006-12-12 13:23:43 +0000941 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000942 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000943 // Go with the shifted value.
944 Imm = ImmSh;
945 } else {
946 // Still stuck with a 64 bit value.
947 Remainder = Imm;
948 Shift = 32;
949 Imm >>= 32;
950 }
951 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000952
Jim Laskey095e6f32006-12-12 13:23:43 +0000953 // Intermediate operand.
954 SDNode *Result;
955
956 // Handle first 32 bits.
957 unsigned Lo = Imm & 0xFFFF;
958 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000959
Jim Laskey095e6f32006-12-12 13:23:43 +0000960 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000961 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000962 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000963 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000964 } else if (Lo) {
965 // Handle the Hi bits.
966 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000967 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000968 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000969 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
970 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000971 } else {
972 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000973 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000974 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000975
Jim Laskey095e6f32006-12-12 13:23:43 +0000976 // If no shift, we're done.
977 if (!Shift) return Result;
978
979 // Shift for next step if the upper 32-bits were not zero.
980 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000981 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
982 SDValue(Result, 0),
983 getI32Imm(Shift),
984 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000985 }
986
987 // Add in the last bits as required.
988 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000989 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
990 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000991 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000992 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000993 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
994 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000995 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000996
Jim Laskey095e6f32006-12-12 13:23:43 +0000997 return Result;
998 }
999 break;
1000 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001001
Hal Finkel940ab932014-02-28 00:27:01 +00001002 case ISD::SETCC: {
1003 SDNode *SN = SelectSETCC(N);
1004 if (SN)
1005 return SN;
1006 break;
1007 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001008 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001009 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00001010
Chris Lattnere4c338d2005-08-25 00:45:43 +00001011 case ISD::FrameIndex: {
1012 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001013 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1014 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001015 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001016 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001017 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001018 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +00001019 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +00001020 }
Chris Lattner6961fc72006-03-26 10:06:40 +00001021
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001022 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001023 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001024 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1025 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00001026 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001027
Chris Lattner57693112005-09-28 22:50:24 +00001028 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001029 // FIXME: since this depends on the setting of the carry flag from the srawi
1030 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001031 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001032 // srl/add/sra pattern the dag combiner will generate for this as
1033 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001034 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001035 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001036 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001037 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001038 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001039 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001040 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001041 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001042 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001043 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001044 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001045 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001046 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001047 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001048 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1049 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001050 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001051 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001052 }
1053 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001054
Chris Lattner1de57062005-09-29 23:33:31 +00001055 // Other cases are autogenerated.
1056 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001057 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001058
Chris Lattnerce645542006-11-10 02:08:47 +00001059 case ISD::LOAD: {
1060 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001061 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001062 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001063
Chris Lattnerce645542006-11-10 02:08:47 +00001064 // Normal loads are handled by code generated from the .td file.
1065 if (LD->getAddressingMode() != ISD::PRE_INC)
1066 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001067
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001068 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001069 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001070 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001071
Chris Lattner474b5b72006-11-15 19:55:13 +00001072 unsigned Opcode;
1073 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001074 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001075 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001076 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1077 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001078 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001079 case MVT::f64: Opcode = PPC::LFDU; break;
1080 case MVT::f32: Opcode = PPC::LFSU; break;
1081 case MVT::i32: Opcode = PPC::LWZU; break;
1082 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1083 case MVT::i1:
1084 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001085 }
1086 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001087 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1088 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1089 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001090 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001091 case MVT::i64: Opcode = PPC::LDU; break;
1092 case MVT::i32: Opcode = PPC::LWZU8; break;
1093 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1094 case MVT::i1:
1095 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001096 }
1097 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001098
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001099 SDValue Chain = LD->getChain();
1100 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001101 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001102 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1103 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001104 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001105 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001106 unsigned Opcode;
1107 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1108 if (LD->getValueType(0) != MVT::i64) {
1109 // Handle PPC32 integer and normal FP loads.
1110 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1111 switch (LoadedVT.getSimpleVT().SimpleTy) {
1112 default: llvm_unreachable("Invalid PPC load type!");
1113 case MVT::f64: Opcode = PPC::LFDUX; break;
1114 case MVT::f32: Opcode = PPC::LFSUX; break;
1115 case MVT::i32: Opcode = PPC::LWZUX; break;
1116 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1117 case MVT::i1:
1118 case MVT::i8: Opcode = PPC::LBZUX; break;
1119 }
1120 } else {
1121 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1122 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1123 "Invalid sext update load");
1124 switch (LoadedVT.getSimpleVT().SimpleTy) {
1125 default: llvm_unreachable("Invalid PPC load type!");
1126 case MVT::i64: Opcode = PPC::LDUX; break;
1127 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1128 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1129 case MVT::i1:
1130 case MVT::i8: Opcode = PPC::LBZUX8; break;
1131 }
1132 }
1133
1134 SDValue Chain = LD->getChain();
1135 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001136 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001137 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1138 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001139 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001140 }
1141 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001142
Nate Begemanb3821a32005-08-18 07:30:46 +00001143 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001144 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001145 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001146
Nate Begemanb3821a32005-08-18 07:30:46 +00001147 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1148 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001149 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001150 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001151 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001152 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001153 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemanb3821a32005-08-18 07:30:46 +00001154 }
Nate Begemand31efd12006-09-22 05:01:56 +00001155 // If this is just a masked value where the input is not handled above, and
1156 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1157 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001158 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001159 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001160 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001161 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001162 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begemand31efd12006-09-22 05:01:56 +00001163 }
Hal Finkele39526a2012-08-28 02:10:15 +00001164 // If this is a 64-bit zero-extension mask, emit rldicl.
1165 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1166 isMask_64(Imm64)) {
1167 SDValue Val = N->getOperand(0);
1168 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001169 SH = 0;
1170
1171 // If the operand is a logical right shift, we can fold it into this
1172 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1173 // for n <= mb. The right shift is really a left rotate followed by a
1174 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1175 // by the shift.
1176 if (Val.getOpcode() == ISD::SRL &&
1177 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1178 assert(Imm < 64 && "Illegal shift amount");
1179 Val = Val.getOperand(0);
1180 SH = 64 - Imm;
1181 }
1182
1183 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Hal Finkele39526a2012-08-28 02:10:15 +00001184 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1185 }
Nate Begemand31efd12006-09-22 05:01:56 +00001186 // AND X, 0 -> 0, not "rlwinm 32".
1187 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001188 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Nate Begemand31efd12006-09-22 05:01:56 +00001189 return NULL;
1190 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001191 // ISD::OR doesn't get all the bitfield insertion fun.
1192 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001193 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001194 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001195 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001196 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001197 Imm = ~(Imm^Imm2);
1198 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001199 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001200 N->getOperand(0).getOperand(1),
1201 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001202 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001203 }
1204 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001205
Chris Lattner1de57062005-09-29 23:33:31 +00001206 // Other cases are autogenerated.
1207 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001208 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001209 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001210 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001211 if (SDNode *I = SelectBitfieldInsert(N))
1212 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001213
Chris Lattner1de57062005-09-29 23:33:31 +00001214 // Other cases are autogenerated.
1215 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001216 case ISD::SHL: {
1217 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001218 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001219 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001220 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001221 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001222 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001223 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001224
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001225 // Other cases are autogenerated.
1226 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001227 }
1228 case ISD::SRL: {
1229 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001230 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001231 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001232 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001233 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Owen Anderson9f944592009-08-11 20:47:22 +00001234 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001235 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001236
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001237 // Other cases are autogenerated.
1238 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001239 }
Hal Finkel940ab932014-02-28 00:27:01 +00001240 // FIXME: Remove this once the ANDI glue bug is fixed:
1241 case PPCISD::ANDIo_1_EQ_BIT:
1242 case PPCISD::ANDIo_1_GT_BIT: {
1243 if (!ANDIGlueBug)
1244 break;
1245
1246 EVT InVT = N->getOperand(0).getValueType();
1247 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1248 "Invalid input type for ANDIo_1_EQ_BIT");
1249
1250 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1251 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1252 N->getOperand(0),
1253 CurDAG->getTargetConstant(1, InVT)), 0);
1254 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1255 SDValue SRIdxVal =
1256 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1257 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1258
1259 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1260 CR0Reg, SRIdxVal,
1261 SDValue(AndI.getNode(), 1) /* glue */);
1262 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001263 case ISD::SELECT_CC: {
1264 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001265 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1266 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001267
Hal Finkel940ab932014-02-28 00:27:01 +00001268 // If this is a select of i1 operands, we'll pattern match it.
1269 if (PPCSubTarget.useCRBits() &&
1270 N->getOperand(0).getValueType() == MVT::i1)
1271 break;
1272
Chris Lattner97b3da12006-06-27 00:04:13 +00001273 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001274 if (!isPPC64)
1275 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1276 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1277 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1278 if (N1C->isNullValue() && N3C->isNullValue() &&
1279 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1280 // FIXME: Implement this optzn for PPC64.
1281 N->getValueType(0) == MVT::i32) {
1282 SDNode *Tmp =
1283 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1284 N->getOperand(0), getI32Imm(~0U));
1285 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1286 SDValue(Tmp, 0), N->getOperand(0),
1287 SDValue(Tmp, 1));
1288 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001289
Dale Johannesenab8e4422009-02-06 19:16:40 +00001290 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001291
1292 if (N->getValueType(0) == MVT::i1) {
1293 // An i1 select is: (c & t) | (!c & f).
1294 bool Inv;
1295 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1296
1297 unsigned SRI;
1298 switch (Idx) {
1299 default: llvm_unreachable("Invalid CC index");
1300 case 0: SRI = PPC::sub_lt; break;
1301 case 1: SRI = PPC::sub_gt; break;
1302 case 2: SRI = PPC::sub_eq; break;
1303 case 3: SRI = PPC::sub_un; break;
1304 }
1305
1306 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1307
1308 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1309 CCBit, CCBit), 0);
1310 SDValue C = Inv ? NotCCBit : CCBit,
1311 NotC = Inv ? CCBit : NotCCBit;
1312
1313 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1314 C, N->getOperand(2)), 0);
1315 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1316 NotC, N->getOperand(3)), 0);
1317
1318 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1319 }
1320
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001321 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001322
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001323 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001324 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001325 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001326 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001327 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001328 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001329 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001330 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001331 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001332 else
1333 SelectCCOp = PPC::SELECT_CC_VRRC;
1334
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001335 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001336 getI32Imm(BROpc) };
1337 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001338 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001339 case ISD::VSELECT:
1340 if (PPCSubTarget.hasVSX()) {
1341 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
1342 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops, 3);
1343 }
1344
1345 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001346 case ISD::VECTOR_SHUFFLE:
1347 if (PPCSubTarget.hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
1348 N->getValueType(0) == MVT::v2i64)) {
1349 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1350
1351 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1352 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1353 unsigned DM[2];
1354
1355 for (int i = 0; i < 2; ++i)
1356 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1357 DM[i] = 0;
1358 else
1359 DM[i] = 1;
1360
Hal Finkel2583b062014-03-28 20:24:55 +00001361 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001362
1363 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1364 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1365 isa<LoadSDNode>(Op1.getOperand(0))) {
1366 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1367 SDValue Base, Offset;
1368
1369 if (LD->isUnindexed() &&
1370 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1371 SDValue Chain = LD->getChain();
1372 SDValue Ops[] = { Base, Offset, Chain };
1373 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
1374 N->getValueType(0), Ops, 3);
1375 }
1376 }
1377
1378 SDValue Ops[] = { Op1, Op2, DMV };
1379 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops, 3);
1380 }
1381
1382 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001383 case PPCISD::BDNZ:
1384 case PPCISD::BDZ: {
1385 bool IsPPC64 = PPCSubTarget.isPPC64();
1386 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1387 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1388 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1389 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1390 MVT::Other, Ops, 2);
1391 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001392 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001393 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001394 // Op #1 is the PPC::PRED_* number.
1395 // Op #2 is the CR#
1396 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001397 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001398 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001399 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001400 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001401 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001402 N->getOperand(0), N->getOperand(4) };
Owen Anderson9f944592009-08-11 20:47:22 +00001403 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001404 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001405 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001406 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001407 unsigned PCC = getPredicateForSetCC(CC);
1408
1409 if (N->getOperand(2).getValueType() == MVT::i1) {
1410 unsigned Opc;
1411 bool Swap;
1412 switch (PCC) {
1413 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1414 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1415 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1416 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1417 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1418 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1419 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1420 }
1421
1422 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1423 N->getOperand(Swap ? 3 : 2),
1424 N->getOperand(Swap ? 2 : 3)), 0);
1425 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1426 BitComp, N->getOperand(4), N->getOperand(0));
1427 }
1428
Dale Johannesenab8e4422009-02-06 19:16:40 +00001429 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001430 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001431 N->getOperand(4), N->getOperand(0) };
Owen Anderson9f944592009-08-11 20:47:22 +00001432 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001433 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001434 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001435 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001436 SDValue Chain = N->getOperand(0);
1437 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001438 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001439 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001440 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001441 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001442 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001443 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001444 case PPCISD::TOC_ENTRY: {
1445 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1446
Bill Schmidt27917782013-02-21 17:12:27 +00001447 // For medium and large code model, we generate two instructions as
1448 // described below. Otherwise we allow SelectCodeCommon to handle this,
1449 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1450 CodeModel::Model CModel = TM.getCodeModel();
1451 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001452 break;
1453
1454 // The first source operand is a TargetGlobalAddress or a
1455 // TargetJumpTable. If it is an externally defined symbol, a symbol
1456 // with common linkage, a function address, or a jump table address,
Bill Schmidt27917782013-02-21 17:12:27 +00001457 // or if we are generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001458 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1459 // Otherwise we generate:
1460 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1461 SDValue GA = N->getOperand(0);
1462 SDValue TOCbase = N->getOperand(1);
1463 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1464 TOCbase, GA);
1465
Bill Schmidt27917782013-02-21 17:12:27 +00001466 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001467 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1468 SDValue(Tmp, 0));
1469
1470 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1471 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001472 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
Rafael Espindola24a669d2014-03-27 15:26:56 +00001473 const GlobalValue *RealGValue =
1474 GAlias ? GAlias->getAliasedGlobal() : GValue;
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001475 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1476 assert((GVar || isa<Function>(RealGValue)) &&
Bill Schmidt34627e32012-11-27 17:35:46 +00001477 "Unexpected global value subclass!");
1478
1479 // An external variable is one without an initializer. For these,
1480 // for variables with common linkage, and for Functions, generate
1481 // the LDtocL form.
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001482 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1483 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001484 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1485 SDValue(Tmp, 0));
1486 }
1487
1488 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1489 SDValue(Tmp, 0), GA);
1490 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001491 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001492 // This expands into one of three sequences, depending on whether
1493 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001494 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1495 isa<ConstantSDNode>(N->getOperand(1)) &&
1496 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001497
1498 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001499 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001500 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001501 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001502
Bill Schmidt51e79512013-02-20 15:50:31 +00001503 if (EltSize == 1) {
1504 Opc1 = PPC::VSPLTISB;
1505 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001506 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001507 VT = MVT::v16i8;
1508 } else if (EltSize == 2) {
1509 Opc1 = PPC::VSPLTISH;
1510 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001511 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001512 VT = MVT::v8i16;
1513 } else {
1514 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1515 Opc1 = PPC::VSPLTISW;
1516 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001517 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001518 VT = MVT::v4i32;
1519 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001520
1521 if ((Elt & 1) == 0) {
1522 // Elt is even, in the range [-32,-18] + [16,30].
1523 //
1524 // Convert: VADD_SPLAT elt, size
1525 // Into: tmp = VSPLTIS[BHW] elt
1526 // VADDU[BHW]M tmp, tmp
1527 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1528 SDValue EltVal = getI32Imm(Elt >> 1);
1529 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1530 SDValue TmpVal = SDValue(Tmp, 0);
1531 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1532
1533 } else if (Elt > 0) {
1534 // Elt is odd and positive, in the range [17,31].
1535 //
1536 // Convert: VADD_SPLAT elt, size
1537 // Into: tmp1 = VSPLTIS[BHW] elt-16
1538 // tmp2 = VSPLTIS[BHW] -16
1539 // VSUBU[BHW]M tmp1, tmp2
1540 SDValue EltVal = getI32Imm(Elt - 16);
1541 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1542 EltVal = getI32Imm(-16);
1543 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1544 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1545 SDValue(Tmp2, 0));
1546
1547 } else {
1548 // Elt is odd and negative, in the range [-31,-17].
1549 //
1550 // Convert: VADD_SPLAT elt, size
1551 // Into: tmp1 = VSPLTIS[BHW] elt+16
1552 // tmp2 = VSPLTIS[BHW] -16
1553 // VADDU[BHW]M tmp1, tmp2
1554 SDValue EltVal = getI32Imm(Elt + 16);
1555 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1556 EltVal = getI32Imm(-16);
1557 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1558 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1559 SDValue(Tmp2, 0));
1560 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001561 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001562 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001563
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001564 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001565}
1566
Hal Finkel860fa902014-01-02 22:09:39 +00001567/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001568/// on the DAG representation.
1569void PPCDAGToDAGISel::PostprocessISelDAG() {
1570
1571 // Skip peepholes at -O0.
1572 if (TM.getOptLevel() == CodeGenOpt::None)
1573 return;
1574
Hal Finkel940ab932014-02-28 00:27:01 +00001575 PeepholePPC64();
1576 PeepholdCROps();
1577}
1578
Hal Finkelb9989152014-02-28 06:11:16 +00001579// Check if all users of this node will become isel where the second operand
1580// is the constant zero. If this is so, and if we can negate the condition,
1581// then we can flip the true and false operands. This will allow the zero to
1582// be folded with the isel so that we don't need to materialize a register
1583// containing zero.
1584bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1585 // If we're not using isel, then this does not matter.
1586 if (!PPCSubTarget.hasISEL())
1587 return false;
1588
1589 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1590 UI != UE; ++UI) {
1591 SDNode *User = *UI;
1592 if (!User->isMachineOpcode())
1593 return false;
1594 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1595 User->getMachineOpcode() != PPC::SELECT_I8)
1596 return false;
1597
1598 SDNode *Op2 = User->getOperand(2).getNode();
1599 if (!Op2->isMachineOpcode())
1600 return false;
1601
1602 if (Op2->getMachineOpcode() != PPC::LI &&
1603 Op2->getMachineOpcode() != PPC::LI8)
1604 return false;
1605
1606 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1607 if (!C)
1608 return false;
1609
1610 if (!C->isNullValue())
1611 return false;
1612 }
1613
1614 return true;
1615}
1616
1617void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1618 SmallVector<SDNode *, 4> ToReplace;
1619 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1620 UI != UE; ++UI) {
1621 SDNode *User = *UI;
1622 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1623 User->getMachineOpcode() == PPC::SELECT_I8) &&
1624 "Must have all select users");
1625 ToReplace.push_back(User);
1626 }
1627
1628 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1629 UE = ToReplace.end(); UI != UE; ++UI) {
1630 SDNode *User = *UI;
1631 SDNode *ResNode =
1632 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1633 User->getValueType(0), User->getOperand(0),
1634 User->getOperand(2),
1635 User->getOperand(1));
1636
1637 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1638 DEBUG(User->dump(CurDAG));
1639 DEBUG(dbgs() << "\nNew: ");
1640 DEBUG(ResNode->dump(CurDAG));
1641 DEBUG(dbgs() << "\n");
1642
1643 ReplaceUses(User, ResNode);
1644 }
1645}
1646
Hal Finkel940ab932014-02-28 00:27:01 +00001647void PPCDAGToDAGISel::PeepholdCROps() {
1648 bool IsModified;
1649 do {
1650 IsModified = false;
1651 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1652 E = CurDAG->allnodes_end(); I != E; ++I) {
1653 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1654 if (!MachineNode || MachineNode->use_empty())
1655 continue;
1656 SDNode *ResNode = MachineNode;
1657
1658 bool Op1Set = false, Op1Unset = false,
1659 Op1Not = false,
1660 Op2Set = false, Op2Unset = false,
1661 Op2Not = false;
1662
1663 unsigned Opcode = MachineNode->getMachineOpcode();
1664 switch (Opcode) {
1665 default: break;
1666 case PPC::CRAND:
1667 case PPC::CRNAND:
1668 case PPC::CROR:
1669 case PPC::CRXOR:
1670 case PPC::CRNOR:
1671 case PPC::CREQV:
1672 case PPC::CRANDC:
1673 case PPC::CRORC: {
1674 SDValue Op = MachineNode->getOperand(1);
1675 if (Op.isMachineOpcode()) {
1676 if (Op.getMachineOpcode() == PPC::CRSET)
1677 Op2Set = true;
1678 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1679 Op2Unset = true;
1680 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1681 Op.getOperand(0) == Op.getOperand(1))
1682 Op2Not = true;
1683 }
1684 } // fallthrough
1685 case PPC::BC:
1686 case PPC::BCn:
1687 case PPC::SELECT_I4:
1688 case PPC::SELECT_I8:
1689 case PPC::SELECT_F4:
1690 case PPC::SELECT_F8:
1691 case PPC::SELECT_VRRC: {
1692 SDValue Op = MachineNode->getOperand(0);
1693 if (Op.isMachineOpcode()) {
1694 if (Op.getMachineOpcode() == PPC::CRSET)
1695 Op1Set = true;
1696 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1697 Op1Unset = true;
1698 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1699 Op.getOperand(0) == Op.getOperand(1))
1700 Op1Not = true;
1701 }
1702 }
1703 break;
1704 }
1705
Hal Finkelb9989152014-02-28 06:11:16 +00001706 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001707 switch (Opcode) {
1708 default: break;
1709 case PPC::CRAND:
1710 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1711 // x & x = x
1712 ResNode = MachineNode->getOperand(0).getNode();
1713 else if (Op1Set)
1714 // 1 & y = y
1715 ResNode = MachineNode->getOperand(1).getNode();
1716 else if (Op2Set)
1717 // x & 1 = x
1718 ResNode = MachineNode->getOperand(0).getNode();
1719 else if (Op1Unset || Op2Unset)
1720 // x & 0 = 0 & y = 0
1721 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1722 MVT::i1);
1723 else if (Op1Not)
1724 // ~x & y = andc(y, x)
1725 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1726 MVT::i1, MachineNode->getOperand(1),
1727 MachineNode->getOperand(0).
1728 getOperand(0));
1729 else if (Op2Not)
1730 // x & ~y = andc(x, y)
1731 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1732 MVT::i1, MachineNode->getOperand(0),
1733 MachineNode->getOperand(1).
1734 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001735 else if (AllUsersSelectZero(MachineNode))
1736 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1737 MVT::i1, MachineNode->getOperand(0),
1738 MachineNode->getOperand(1)),
1739 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001740 break;
1741 case PPC::CRNAND:
1742 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1743 // nand(x, x) -> nor(x, x)
1744 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1745 MVT::i1, MachineNode->getOperand(0),
1746 MachineNode->getOperand(0));
1747 else if (Op1Set)
1748 // nand(1, y) -> nor(y, y)
1749 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1750 MVT::i1, MachineNode->getOperand(1),
1751 MachineNode->getOperand(1));
1752 else if (Op2Set)
1753 // nand(x, 1) -> nor(x, x)
1754 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1755 MVT::i1, MachineNode->getOperand(0),
1756 MachineNode->getOperand(0));
1757 else if (Op1Unset || Op2Unset)
1758 // nand(x, 0) = nand(0, y) = 1
1759 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1760 MVT::i1);
1761 else if (Op1Not)
1762 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1763 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1764 MVT::i1, MachineNode->getOperand(0).
1765 getOperand(0),
1766 MachineNode->getOperand(1));
1767 else if (Op2Not)
1768 // nand(x, ~y) = ~x | y = orc(y, x)
1769 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1770 MVT::i1, MachineNode->getOperand(1).
1771 getOperand(0),
1772 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001773 else if (AllUsersSelectZero(MachineNode))
1774 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1775 MVT::i1, MachineNode->getOperand(0),
1776 MachineNode->getOperand(1)),
1777 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001778 break;
1779 case PPC::CROR:
1780 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1781 // x | x = x
1782 ResNode = MachineNode->getOperand(0).getNode();
1783 else if (Op1Set || Op2Set)
1784 // x | 1 = 1 | y = 1
1785 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1786 MVT::i1);
1787 else if (Op1Unset)
1788 // 0 | y = y
1789 ResNode = MachineNode->getOperand(1).getNode();
1790 else if (Op2Unset)
1791 // x | 0 = x
1792 ResNode = MachineNode->getOperand(0).getNode();
1793 else if (Op1Not)
1794 // ~x | y = orc(y, x)
1795 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1796 MVT::i1, MachineNode->getOperand(1),
1797 MachineNode->getOperand(0).
1798 getOperand(0));
1799 else if (Op2Not)
1800 // x | ~y = orc(x, y)
1801 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1802 MVT::i1, MachineNode->getOperand(0),
1803 MachineNode->getOperand(1).
1804 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001805 else if (AllUsersSelectZero(MachineNode))
1806 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1807 MVT::i1, MachineNode->getOperand(0),
1808 MachineNode->getOperand(1)),
1809 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001810 break;
1811 case PPC::CRXOR:
1812 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1813 // xor(x, x) = 0
1814 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1815 MVT::i1);
1816 else if (Op1Set)
1817 // xor(1, y) -> nor(y, y)
1818 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1819 MVT::i1, MachineNode->getOperand(1),
1820 MachineNode->getOperand(1));
1821 else if (Op2Set)
1822 // xor(x, 1) -> nor(x, x)
1823 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1824 MVT::i1, MachineNode->getOperand(0),
1825 MachineNode->getOperand(0));
1826 else if (Op1Unset)
1827 // xor(0, y) = y
1828 ResNode = MachineNode->getOperand(1).getNode();
1829 else if (Op2Unset)
1830 // xor(x, 0) = x
1831 ResNode = MachineNode->getOperand(0).getNode();
1832 else if (Op1Not)
1833 // xor(~x, y) = eqv(x, y)
1834 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1835 MVT::i1, MachineNode->getOperand(0).
1836 getOperand(0),
1837 MachineNode->getOperand(1));
1838 else if (Op2Not)
1839 // xor(x, ~y) = eqv(x, y)
1840 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1841 MVT::i1, MachineNode->getOperand(0),
1842 MachineNode->getOperand(1).
1843 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001844 else if (AllUsersSelectZero(MachineNode))
1845 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1846 MVT::i1, MachineNode->getOperand(0),
1847 MachineNode->getOperand(1)),
1848 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001849 break;
1850 case PPC::CRNOR:
1851 if (Op1Set || Op2Set)
1852 // nor(1, y) -> 0
1853 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1854 MVT::i1);
1855 else if (Op1Unset)
1856 // nor(0, y) = ~y -> nor(y, y)
1857 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1858 MVT::i1, MachineNode->getOperand(1),
1859 MachineNode->getOperand(1));
1860 else if (Op2Unset)
1861 // nor(x, 0) = ~x
1862 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1863 MVT::i1, MachineNode->getOperand(0),
1864 MachineNode->getOperand(0));
1865 else if (Op1Not)
1866 // nor(~x, y) = andc(x, y)
1867 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1868 MVT::i1, MachineNode->getOperand(0).
1869 getOperand(0),
1870 MachineNode->getOperand(1));
1871 else if (Op2Not)
1872 // nor(x, ~y) = andc(y, x)
1873 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1874 MVT::i1, MachineNode->getOperand(1).
1875 getOperand(0),
1876 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001877 else if (AllUsersSelectZero(MachineNode))
1878 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1879 MVT::i1, MachineNode->getOperand(0),
1880 MachineNode->getOperand(1)),
1881 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001882 break;
1883 case PPC::CREQV:
1884 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1885 // eqv(x, x) = 1
1886 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1887 MVT::i1);
1888 else if (Op1Set)
1889 // eqv(1, y) = y
1890 ResNode = MachineNode->getOperand(1).getNode();
1891 else if (Op2Set)
1892 // eqv(x, 1) = x
1893 ResNode = MachineNode->getOperand(0).getNode();
1894 else if (Op1Unset)
1895 // eqv(0, y) = ~y -> nor(y, y)
1896 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1897 MVT::i1, MachineNode->getOperand(1),
1898 MachineNode->getOperand(1));
1899 else if (Op2Unset)
1900 // eqv(x, 0) = ~x
1901 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1902 MVT::i1, MachineNode->getOperand(0),
1903 MachineNode->getOperand(0));
1904 else if (Op1Not)
1905 // eqv(~x, y) = xor(x, y)
1906 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1907 MVT::i1, MachineNode->getOperand(0).
1908 getOperand(0),
1909 MachineNode->getOperand(1));
1910 else if (Op2Not)
1911 // eqv(x, ~y) = xor(x, y)
1912 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1913 MVT::i1, MachineNode->getOperand(0),
1914 MachineNode->getOperand(1).
1915 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001916 else if (AllUsersSelectZero(MachineNode))
1917 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1918 MVT::i1, MachineNode->getOperand(0),
1919 MachineNode->getOperand(1)),
1920 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001921 break;
1922 case PPC::CRANDC:
1923 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1924 // andc(x, x) = 0
1925 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1926 MVT::i1);
1927 else if (Op1Set)
1928 // andc(1, y) = ~y
1929 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1930 MVT::i1, MachineNode->getOperand(1),
1931 MachineNode->getOperand(1));
1932 else if (Op1Unset || Op2Set)
1933 // andc(0, y) = andc(x, 1) = 0
1934 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1935 MVT::i1);
1936 else if (Op2Unset)
1937 // andc(x, 0) = x
1938 ResNode = MachineNode->getOperand(0).getNode();
1939 else if (Op1Not)
1940 // andc(~x, y) = ~(x | y) = nor(x, y)
1941 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1942 MVT::i1, MachineNode->getOperand(0).
1943 getOperand(0),
1944 MachineNode->getOperand(1));
1945 else if (Op2Not)
1946 // andc(x, ~y) = x & y
1947 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1948 MVT::i1, MachineNode->getOperand(0),
1949 MachineNode->getOperand(1).
1950 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001951 else if (AllUsersSelectZero(MachineNode))
1952 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1953 MVT::i1, MachineNode->getOperand(1),
1954 MachineNode->getOperand(0)),
1955 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001956 break;
1957 case PPC::CRORC:
1958 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1959 // orc(x, x) = 1
1960 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1961 MVT::i1);
1962 else if (Op1Set || Op2Unset)
1963 // orc(1, y) = orc(x, 0) = 1
1964 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1965 MVT::i1);
1966 else if (Op2Set)
1967 // orc(x, 1) = x
1968 ResNode = MachineNode->getOperand(0).getNode();
1969 else if (Op1Unset)
1970 // orc(0, y) = ~y
1971 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1972 MVT::i1, MachineNode->getOperand(1),
1973 MachineNode->getOperand(1));
1974 else if (Op1Not)
1975 // orc(~x, y) = ~(x & y) = nand(x, y)
1976 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1977 MVT::i1, MachineNode->getOperand(0).
1978 getOperand(0),
1979 MachineNode->getOperand(1));
1980 else if (Op2Not)
1981 // orc(x, ~y) = x | y
1982 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1983 MVT::i1, MachineNode->getOperand(0),
1984 MachineNode->getOperand(1).
1985 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001986 else if (AllUsersSelectZero(MachineNode))
1987 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1988 MVT::i1, MachineNode->getOperand(1),
1989 MachineNode->getOperand(0)),
1990 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001991 break;
1992 case PPC::SELECT_I4:
1993 case PPC::SELECT_I8:
1994 case PPC::SELECT_F4:
1995 case PPC::SELECT_F8:
1996 case PPC::SELECT_VRRC:
1997 if (Op1Set)
1998 ResNode = MachineNode->getOperand(1).getNode();
1999 else if (Op1Unset)
2000 ResNode = MachineNode->getOperand(2).getNode();
2001 else if (Op1Not)
2002 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2003 SDLoc(MachineNode),
2004 MachineNode->getValueType(0),
2005 MachineNode->getOperand(0).
2006 getOperand(0),
2007 MachineNode->getOperand(2),
2008 MachineNode->getOperand(1));
2009 break;
2010 case PPC::BC:
2011 case PPC::BCn:
2012 if (Op1Not)
2013 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2014 PPC::BC,
2015 SDLoc(MachineNode),
2016 MVT::Other,
2017 MachineNode->getOperand(0).
2018 getOperand(0),
2019 MachineNode->getOperand(1),
2020 MachineNode->getOperand(2));
2021 // FIXME: Handle Op1Set, Op1Unset here too.
2022 break;
2023 }
2024
Hal Finkelb9989152014-02-28 06:11:16 +00002025 // If we're inverting this node because it is used only by selects that
2026 // we'd like to swap, then swap the selects before the node replacement.
2027 if (SelectSwap)
2028 SwapAllSelectUsers(MachineNode);
2029
Hal Finkel940ab932014-02-28 00:27:01 +00002030 if (ResNode != MachineNode) {
2031 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2032 DEBUG(MachineNode->dump(CurDAG));
2033 DEBUG(dbgs() << "\nNew: ");
2034 DEBUG(ResNode->dump(CurDAG));
2035 DEBUG(dbgs() << "\n");
2036
2037 ReplaceUses(MachineNode, ResNode);
2038 IsModified = true;
2039 }
2040 }
2041 if (IsModified)
2042 CurDAG->RemoveDeadNodes();
2043 } while (IsModified);
2044}
2045
2046void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002047 // These optimizations are currently supported only for 64-bit SVR4.
2048 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
2049 return;
2050
2051 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2052 ++Position;
2053
2054 while (Position != CurDAG->allnodes_begin()) {
2055 SDNode *N = --Position;
2056 // Skip dead nodes and any non-machine opcodes.
2057 if (N->use_empty() || !N->isMachineOpcode())
2058 continue;
2059
2060 unsigned FirstOp;
2061 unsigned StorageOpcode = N->getMachineOpcode();
2062
2063 switch (StorageOpcode) {
2064 default: continue;
2065
2066 case PPC::LBZ:
2067 case PPC::LBZ8:
2068 case PPC::LD:
2069 case PPC::LFD:
2070 case PPC::LFS:
2071 case PPC::LHA:
2072 case PPC::LHA8:
2073 case PPC::LHZ:
2074 case PPC::LHZ8:
2075 case PPC::LWA:
2076 case PPC::LWZ:
2077 case PPC::LWZ8:
2078 FirstOp = 0;
2079 break;
2080
2081 case PPC::STB:
2082 case PPC::STB8:
2083 case PPC::STD:
2084 case PPC::STFD:
2085 case PPC::STFS:
2086 case PPC::STH:
2087 case PPC::STH8:
2088 case PPC::STW:
2089 case PPC::STW8:
2090 FirstOp = 1;
2091 break;
2092 }
2093
2094 // If this is a load or store with a zero offset, we may be able to
2095 // fold an add-immediate into the memory operation.
2096 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2097 N->getConstantOperandVal(FirstOp) != 0)
2098 continue;
2099
2100 SDValue Base = N->getOperand(FirstOp + 1);
2101 if (!Base.isMachineOpcode())
2102 continue;
2103
2104 unsigned Flags = 0;
2105 bool ReplaceFlags = true;
2106
2107 // When the feeding operation is an add-immediate of some sort,
2108 // determine whether we need to add relocation information to the
2109 // target flags on the immediate operand when we fold it into the
2110 // load instruction.
2111 //
2112 // For something like ADDItocL, the relocation information is
2113 // inferred from the opcode; when we process it in the AsmPrinter,
2114 // we add the necessary relocation there. A load, though, can receive
2115 // relocation from various flavors of ADDIxxx, so we need to carry
2116 // the relocation information in the target flags.
2117 switch (Base.getMachineOpcode()) {
2118 default: continue;
2119
2120 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002121 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002122 // In some cases (such as TLS) the relocation information
2123 // is already in place on the operand, so copying the operand
2124 // is sufficient.
2125 ReplaceFlags = false;
2126 // For these cases, the immediate may not be divisible by 4, in
2127 // which case the fold is illegal for DS-form instructions. (The
2128 // other cases provide aligned addresses and are always safe.)
2129 if ((StorageOpcode == PPC::LWA ||
2130 StorageOpcode == PPC::LD ||
2131 StorageOpcode == PPC::STD) &&
2132 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2133 Base.getConstantOperandVal(1) % 4 != 0))
2134 continue;
2135 break;
2136 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002137 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002138 break;
2139 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002140 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002141 break;
2142 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002143 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002144 break;
2145 }
2146
2147 // We found an opportunity. Reverse the operands from the add
2148 // immediate and substitute them into the load or store. If
2149 // needed, update the target flags for the immediate operand to
2150 // reflect the necessary relocation information.
2151 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2152 DEBUG(Base->dump(CurDAG));
2153 DEBUG(dbgs() << "\nN: ");
2154 DEBUG(N->dump(CurDAG));
2155 DEBUG(dbgs() << "\n");
2156
2157 SDValue ImmOpnd = Base.getOperand(1);
2158
2159 // If the relocation information isn't already present on the
2160 // immediate operand, add it now.
2161 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002162 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002163 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002164 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002165 // We can't perform this optimization for data whose alignment
2166 // is insufficient for the instruction encoding.
2167 if (GV->getAlignment() < 4 &&
2168 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2169 StorageOpcode == PPC::LWA)) {
2170 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2171 continue;
2172 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002173 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002174 } else if (ConstantPoolSDNode *CP =
2175 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002176 const Constant *C = CP->getConstVal();
2177 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2178 CP->getAlignment(),
2179 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002180 }
2181 }
2182
2183 if (FirstOp == 1) // Store
2184 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2185 Base.getOperand(0), N->getOperand(3));
2186 else // Load
2187 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2188 N->getOperand(2));
2189
2190 // The add-immediate may now be dead, in which case remove it.
2191 if (Base.getNode()->use_empty())
2192 CurDAG->RemoveDeadNode(Base.getNode());
2193 }
2194}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002195
Chris Lattnerb055c872006-06-10 01:15:02 +00002196
Andrew Trickc416ba62010-12-24 04:28:06 +00002197/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002198/// PowerPC-specific DAG, ready for instruction scheduling.
2199///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002200FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002201 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002202}
2203
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002204static void initializePassOnce(PassRegistry &Registry) {
2205 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
2206 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
2207 false, false);
2208 Registry.registerPass(*PI, true);
2209}
2210
2211void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2212 CALL_ONCE_INITIALIZATION(initializePassOnce);
2213}
2214