blob: f6e075d27193057b7edcac97347a3f84a55e6cd1 [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 Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000018#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000025#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/GlobalVariable.h"
28#include "llvm/IR/Intrinsics.h"
Hal Finkel940ab932014-02-28 00:27:01 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000030#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000035using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "ppc-codegen"
38
Hal Finkel940ab932014-02-28 00:27:01 +000039// FIXME: Remove this once the bug has been fixed!
40cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
41cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
42
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000043namespace llvm {
44 void initializePPCDAGToDAGISelPass(PassRegistry&);
45}
46
Chris Lattner43ff01e2005-08-17 19:33:03 +000047namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000048 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000049 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000050 /// instructions for SelectionDAG operations.
51 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000052 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000053 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000054 const PPCTargetLowering *PPCLowering;
55 const PPCSubtarget *PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000056 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000057 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000058 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman619ef482009-01-15 19:20:50 +000059 : SelectionDAGISel(tm), TM(tm),
Eric Christopher1b8e7632014-05-22 01:07:24 +000060 PPCLowering(TM.getTargetLowering()),
61 PPCSubTarget(TM.getSubtargetImpl()) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000062 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
63 }
Andrew Trickc416ba62010-12-24 04:28:06 +000064
Craig Topper0d3fa922014-04-29 07:57:37 +000065 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000066 // Make sure we re-emit a set of the global base reg if necessary
67 GlobalBaseReg = 0;
Eric Christopher1b8e7632014-05-22 01:07:24 +000068 PPCLowering = TM.getTargetLowering();
69 PPCSubTarget = TM.getSubtargetImpl();
Dan Gohman5ea74d52009-07-31 18:16:33 +000070 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000071
Eric Christopher1b8e7632014-05-22 01:07:24 +000072 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000073 InsertVRSaveCode(MF);
74
Chris Lattner1678a6c2006-03-16 18:25:23 +000075 return true;
Chris Lattner45640392005-08-19 22:38:53 +000076 }
Andrew Trickc416ba62010-12-24 04:28:06 +000077
Craig Topper0d3fa922014-04-29 07:57:37 +000078 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000079
Chris Lattner43ff01e2005-08-17 19:33:03 +000080 /// getI32Imm - Return a target constant with the specified value, of type
81 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000082 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000083 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000084 }
Chris Lattner45640392005-08-19 22:38:53 +000085
Chris Lattner97b3da12006-06-27 00:04:13 +000086 /// getI64Imm - Return a target constant with the specified value, of type
87 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000088 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000089 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000090 }
Andrew Trickc416ba62010-12-24 04:28:06 +000091
Chris Lattner97b3da12006-06-27 00:04:13 +000092 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000093 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +000094 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +000095 }
Andrew Trickc416ba62010-12-24 04:28:06 +000096
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000097 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000098 /// with any number of 0s on either side. The 1s are allowed to wrap from
99 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
100 /// 0x0F0F0000 is not, since all 1s are not contiguous.
101 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
102
103
104 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
105 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000106 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000107 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000108
Chris Lattner45640392005-08-19 22:38:53 +0000109 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
110 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000111 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000112
Chris Lattner43ff01e2005-08-17 19:33:03 +0000113 // Select - Convert the specified operand from a target-independent to a
114 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000115 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000116
Nate Begeman93c4bc62005-08-19 00:38:14 +0000117 SDNode *SelectBitfieldInsert(SDNode *N);
118
Chris Lattner2a1823d2005-08-21 18:50:37 +0000119 /// SelectCC - Select a comparison of the specified values with the
120 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000121 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000122
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000123 /// SelectAddrImm - Returns true if the address N can be represented by
124 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000125 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000126 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000127 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000128 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000129
Chris Lattner6f5840c2006-11-16 00:41:37 +0000130 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000131 /// immediate field. Note that the operand at this point is already the
132 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000133 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000134 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000135 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000136 Out = N;
137 return true;
138 }
139
140 return false;
141 }
142
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000143 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
144 /// represented as an indexed [r+r] operation. Returns false if it can
145 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000146 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000147 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000148 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000149
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000150 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
151 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000152 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000153 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000154 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000155
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000156 /// SelectAddrImmX4 - Returns true if the address N can be represented by
157 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
158 /// Suitable for use by STD and friends.
159 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000160 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000161 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000162
Hal Finkel756810f2013-03-21 21:37:52 +0000163 // Select an address into a single register.
164 bool SelectAddr(SDValue N, SDValue &Base) {
165 Base = N;
166 return true;
167 }
168
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000169 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000170 /// inline asm expressions. It is always correct to compute the value into
171 /// a register. The case of adding a (possibly relocatable) constant to a
172 /// register can be improved, but it is wrong to substitute Reg+Reg for
173 /// Reg in an asm, because the load or store opcode would have to change.
Craig Topper0d3fa922014-04-29 07:57:37 +0000174 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
175 char ConstraintCode,
176 std::vector<SDValue> &OutOps) override {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000177 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000178 return false;
179 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000180
Dan Gohman5ea74d52009-07-31 18:16:33 +0000181 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000182
Craig Topper0d3fa922014-04-29 07:57:37 +0000183 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000184 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000185 }
186
Chris Lattner03e08ee2005-09-13 22:03:06 +0000187// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000188#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000189
Chris Lattner259e6c72005-10-06 18:45:51 +0000190private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000191 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000192
193 void PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +0000194 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000195
196 bool AllUsersSelectZero(SDNode *N);
197 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000198 };
199}
200
Chris Lattner1678a6c2006-03-16 18:25:23 +0000201/// InsertVRSaveCode - Once the entire function has been instruction selected,
202/// all virtual registers are created and all machine instructions are built,
203/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000204void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000205 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000206 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000207 //
Dan Gohman4a618822010-02-10 16:03:48 +0000208 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000209 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000210 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000211 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
212 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
213 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000214 HasVectorVReg = true;
215 break;
216 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000217 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000218 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000219
Chris Lattner02e2c182006-03-13 21:52:10 +0000220 // If we have a vector register, we want to emit code into the entry and exit
221 // blocks to save and restore the VRSAVE register. We do this here (instead
222 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
223 //
224 // 1. This (trivially) reduces the load on the register allocator, by not
225 // having to represent the live range of the VRSAVE register.
226 // 2. This (more significantly) allows us to create a temporary virtual
227 // register to hold the saved VRSAVE value, allowing this temporary to be
228 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000229
230 // Create two vregs - one to hold the VRSAVE register that is live-in to the
231 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000232 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
233 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000234
Evan Cheng20350c42006-11-27 23:37:22 +0000235 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000236 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000237 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000238 // Emit the following code into the entry block:
239 // InVRSAVE = MFVRSAVE
240 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
241 // MTVRSAVE UpdatedVRSAVE
242 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000243 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
244 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000245 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000246 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000247
Chris Lattner1678a6c2006-03-16 18:25:23 +0000248 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000249 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000250 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000252
Chris Lattner1678a6c2006-03-16 18:25:23 +0000253 // Skip over all terminator instructions, which are part of the return
254 // sequence.
255 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000256 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000257 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000258
Chris Lattner1678a6c2006-03-16 18:25:23 +0000259 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000260 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000261 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000262 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000263}
Chris Lattner8ae95252005-09-03 01:17:22 +0000264
Chris Lattner1678a6c2006-03-16 18:25:23 +0000265
Chris Lattner45640392005-08-19 22:38:53 +0000266/// getGlobalBaseReg - Output the instructions required to put the
267/// base address to use for accessing globals into a register.
268///
Evan Cheng61413a32006-08-26 05:34:46 +0000269SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000270 if (!GlobalBaseReg) {
Evan Cheng20350c42006-11-27 23:37:22 +0000271 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000272 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000273 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000274 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000275 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000276
Eric Christopher1b8e7632014-05-22 01:07:24 +0000277 if (PPCLowering->getPointerTy() == MVT::i32) {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000278 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000279 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000280 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000281 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000282 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000283 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000284 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000285 }
Chris Lattner45640392005-08-19 22:38:53 +0000286 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000287 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000288 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000289}
290
291/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
292/// or 64-bit immediate, and if the value can be accurately represented as a
293/// sign extension from a 16-bit value. If so, this returns true and the
294/// immediate.
295static bool isIntS16Immediate(SDNode *N, short &Imm) {
296 if (N->getOpcode() != ISD::Constant)
297 return false;
298
Dan Gohmaneffb8942008-09-12 16:56:44 +0000299 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000300 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000301 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000302 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000303 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000304}
305
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000306static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000307 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000308}
309
310
Chris Lattner97b3da12006-06-27 00:04:13 +0000311/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
312/// operand. If so Imm will receive the 32-bit value.
313static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000314 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000315 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000316 return true;
317 }
318 return false;
319}
320
Chris Lattner97b3da12006-06-27 00:04:13 +0000321/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
322/// operand. If so Imm will receive the 64-bit value.
323static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000324 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000325 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000326 return true;
327 }
328 return false;
329}
330
331// isInt32Immediate - This method tests to see if a constant operand.
332// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000333static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000334 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000335}
336
337
338// isOpcWithIntImmediate - This method tests to see if the node is a specific
339// opcode and that it has a immediate integer right operand.
340// If so Imm will receive the 32 bit value.
341static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000342 return N->getOpcode() == Opc
343 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000344}
345
Nate Begemand31efd12006-09-22 05:01:56 +0000346bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000347 if (!Val)
348 return false;
349
Nate Begemanb3821a32005-08-18 07:30:46 +0000350 if (isShiftedMask_32(Val)) {
351 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000352 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000353 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000354 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000355 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000356 } else {
357 Val = ~Val; // invert mask
358 if (isShiftedMask_32(Val)) {
359 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000360 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000361 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000362 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000363 return true;
364 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000365 }
366 // no run present
367 return false;
368}
369
Andrew Trickc416ba62010-12-24 04:28:06 +0000370bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
371 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000372 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000373 // Don't even go down this path for i64, since different logic will be
374 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000375 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000376 return false;
377
Nate Begemanb3821a32005-08-18 07:30:46 +0000378 unsigned Shift = 32;
379 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
380 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000381 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000382 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000383 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000384
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 if (Opcode == ISD::SHL) {
386 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000387 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000388 // determine which bits are made indeterminant by shift
389 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000390 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000391 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000392 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000393 // determine which bits are made indeterminant by shift
394 Indeterminant = ~(0xFFFFFFFFu >> Shift);
395 // adjust for the left rotate
396 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000397 } else if (Opcode == ISD::ROTL) {
398 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000399 } else {
400 return false;
401 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000402
Nate Begemanb3821a32005-08-18 07:30:46 +0000403 // if the mask doesn't intersect any Indeterminant bits
404 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000405 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000406 // make sure the mask is still a mask (wrap arounds may not be)
407 return isRunOfOnes(Mask, MB, ME);
408 }
409 return false;
410}
411
Nate Begeman93c4bc62005-08-19 00:38:14 +0000412/// SelectBitfieldInsert - turn an or of two masked values into
413/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000414SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000415 SDValue Op0 = N->getOperand(0);
416 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000417 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000418
Dan Gohmanf19609a2008-02-27 01:23:58 +0000419 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000420 CurDAG->computeKnownBits(Op0, LKZ, LKO);
421 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000422
Dan Gohmanf19609a2008-02-27 01:23:58 +0000423 unsigned TargetMask = LKZ.getZExtValue();
424 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000425
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000426 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
427 unsigned Op0Opc = Op0.getOpcode();
428 unsigned Op1Opc = Op1.getOpcode();
429 unsigned Value, SH = 0;
430 TargetMask = ~TargetMask;
431 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000432
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000433 // If the LHS has a foldable shift and the RHS does not, then swap it to the
434 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000435 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
436 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
437 Op0.getOperand(0).getOpcode() == ISD::SRL) {
438 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
439 Op1.getOperand(0).getOpcode() != ISD::SRL) {
440 std::swap(Op0, Op1);
441 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000442 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000443 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000444 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000445 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
446 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
447 Op1.getOperand(0).getOpcode() != ISD::SRL) {
448 std::swap(Op0, Op1);
449 std::swap(Op0Opc, Op1Opc);
450 std::swap(TargetMask, InsertMask);
451 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000452 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000453
Nate Begeman1333cea2006-05-07 00:23:38 +0000454 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000455 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000456 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000457
458 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000459 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000460 Op1 = Op1.getOperand(0);
461 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
462 }
463 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000464 // The AND mask might not be a constant, and we need to make sure that
465 // if we're going to fold the masking with the insert, all bits not
466 // know to be zero in the mask are known to be one.
467 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000468 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000469 bool CanFoldMask = InsertMask == MKO.getZExtValue();
470
Nate Begeman1333cea2006-05-07 00:23:38 +0000471 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000472 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000473 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000474 // Note that Value must be in range here (less than 32) because
475 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000476 Op1 = Op1.getOperand(0).getOperand(0);
477 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000478 }
479 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000480
Chris Lattnera2963392006-05-12 16:29:37 +0000481 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000482 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000483 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000484 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000485 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000486 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000487 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000488}
489
Chris Lattner2a1823d2005-08-21 18:50:37 +0000490/// SelectCC - Select a comparison of the specified values with the specified
491/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000492SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000493 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000494 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000495 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000496
Owen Anderson9f944592009-08-11 20:47:22 +0000497 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000498 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000499 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
500 if (isInt32Immediate(RHS, Imm)) {
501 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000502 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000503 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
504 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000505 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000506 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000507 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
508 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000509
Chris Lattneraa3926b2006-09-20 04:25:47 +0000510 // For non-equality comparisons, the default code would materialize the
511 // constant, then compare against it, like this:
512 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000513 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000514 // cmpw cr0, r3, r2
515 // Since we are just comparing for equality, we can emit this instead:
516 // xoris r0,r3,0x1234
517 // cmplwi cr0,r0,0x5678
518 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000519 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
520 getI32Imm(Imm >> 16)), 0);
521 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
522 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000523 }
524 Opc = PPC::CMPLW;
525 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000526 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000527 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
528 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000529 Opc = PPC::CMPLW;
530 } else {
531 short SImm;
532 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000533 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
534 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000535 0);
536 Opc = PPC::CMPW;
537 }
Owen Anderson9f944592009-08-11 20:47:22 +0000538 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000539 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000540 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000541 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000542 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000543 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000544 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
545 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000546 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000547 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000548 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
549 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000550
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000551 // For non-equality comparisons, the default code would materialize the
552 // constant, then compare against it, like this:
553 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000554 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000555 // cmpd cr0, r3, r2
556 // Since we are just comparing for equality, we can emit this instead:
557 // xoris r0,r3,0x1234
558 // cmpldi cr0,r0,0x5678
559 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000560 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000561 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
562 getI64Imm(Imm >> 16)), 0);
563 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
564 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000565 }
566 }
567 Opc = PPC::CMPLD;
568 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000569 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000570 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
571 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000572 Opc = PPC::CMPLD;
573 } else {
574 short SImm;
575 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000576 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
577 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000578 0);
579 Opc = PPC::CMPD;
580 }
Owen Anderson9f944592009-08-11 20:47:22 +0000581 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000582 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000583 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000584 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +0000585 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000586 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000587 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000588}
589
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000590static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000591 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000592 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000593 case ISD::SETONE:
594 case ISD::SETOLE:
595 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000596 llvm_unreachable("Should be lowered by legalize!");
597 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000598 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000599 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000600 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000601 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000602 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000603 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000604 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000605 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000606 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000607 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000608 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000609 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000610 case ISD::SETO: return PPC::PRED_NU;
611 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000612 // These two are invalid for floating point. Assume we have int.
613 case ISD::SETULT: return PPC::PRED_LT;
614 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000615 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000616}
617
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000618/// getCRIdxForSetCC - Return the index of the condition register field
619/// associated with the SetCC condition, and whether or not the field is
620/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000621static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000622 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000623 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000624 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000625 case ISD::SETOLT:
626 case ISD::SETLT: return 0; // Bit #0 = SETOLT
627 case ISD::SETOGT:
628 case ISD::SETGT: return 1; // Bit #1 = SETOGT
629 case ISD::SETOEQ:
630 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
631 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000632 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000633 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000634 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000635 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000636 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000637 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
638 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000639 case ISD::SETUEQ:
640 case ISD::SETOGE:
641 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000642 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000643 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000644 // These are invalid for floating point. Assume integer.
645 case ISD::SETULT: return 0;
646 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000647 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000648}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000649
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000650// getVCmpInst: return the vector compare instruction for the specified
651// vector type and condition code. Since this is for altivec specific code,
652// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000653static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC,
654 bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000655 switch (CC) {
656 case ISD::SETEQ:
657 case ISD::SETUEQ:
658 case ISD::SETNE:
659 case ISD::SETUNE:
660 if (VecVT == MVT::v16i8)
661 return PPC::VCMPEQUB;
662 else if (VecVT == MVT::v8i16)
663 return PPC::VCMPEQUH;
664 else if (VecVT == MVT::v4i32)
665 return PPC::VCMPEQUW;
666 // v4f32 != v4f32 could be translate to unordered not equal
667 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000668 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
669 else if (VecVT == MVT::v2f64)
670 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000671 break;
672 case ISD::SETLT:
673 case ISD::SETGT:
674 case ISD::SETLE:
675 case ISD::SETGE:
676 if (VecVT == MVT::v16i8)
677 return PPC::VCMPGTSB;
678 else if (VecVT == MVT::v8i16)
679 return PPC::VCMPGTSH;
680 else if (VecVT == MVT::v4i32)
681 return PPC::VCMPGTSW;
682 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000683 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
684 else if (VecVT == MVT::v2f64)
685 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000686 break;
687 case ISD::SETULT:
688 case ISD::SETUGT:
689 case ISD::SETUGE:
690 case ISD::SETULE:
691 if (VecVT == MVT::v16i8)
692 return PPC::VCMPGTUB;
693 else if (VecVT == MVT::v8i16)
694 return PPC::VCMPGTUH;
695 else if (VecVT == MVT::v4i32)
696 return PPC::VCMPGTUW;
697 break;
698 case ISD::SETOEQ:
699 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000700 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
701 else if (VecVT == MVT::v2f64)
702 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000703 break;
704 case ISD::SETOLT:
705 case ISD::SETOGT:
706 case ISD::SETOLE:
707 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000708 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
709 else if (VecVT == MVT::v2f64)
710 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000711 break;
712 case ISD::SETOGE:
713 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000714 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
715 else if (VecVT == MVT::v2f64)
716 return PPC::XVCMPGEDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000717 break;
718 default:
719 break;
720 }
721 llvm_unreachable("Invalid integer vector compare condition");
722}
723
724// getVCmpEQInst: return the equal compare instruction for the specified vector
725// type. Since this is for altivec specific code, only support the altivec
726// types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000727static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000728 switch (VecVT) {
729 case MVT::v16i8:
730 return PPC::VCMPEQUB;
731 case MVT::v8i16:
732 return PPC::VCMPEQUH;
733 case MVT::v4i32:
734 return PPC::VCMPEQUW;
735 case MVT::v4f32:
Hal Finkel27774d92014-03-13 07:58:58 +0000736 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
737 case MVT::v2f64:
738 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000739 default:
740 llvm_unreachable("Invalid integer vector compare condition");
741 }
742}
743
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000744SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000745 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000746 unsigned Imm;
747 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000748 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
749 bool isPPC64 = (PtrVT == MVT::i64);
750
Eric Christopher1b8e7632014-05-22 01:07:24 +0000751 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +0000752 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000753 // We can codegen setcc op, imm very efficiently compared to a brcond.
754 // Check for those cases here.
755 // setcc op, 0
756 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000757 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000758 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000759 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000760 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000761 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000762 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000763 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000764 }
Chris Lattnere2969492005-10-21 21:17:10 +0000765 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000766 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000767 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000768 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000769 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000770 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000771 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000772 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000773 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000774 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000775 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000776 }
Chris Lattnere2969492005-10-21 21:17:10 +0000777 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000778 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000779 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
780 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000781 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000782 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000783 }
784 }
Chris Lattner491b8292005-10-06 19:03:35 +0000785 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000787 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000788 default: break;
789 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000790 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000791 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000792 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000793 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
794 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000795 MVT::i32,
796 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000797 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000798 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000799 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000800 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000801 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000802 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000803 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000804 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000805 }
Chris Lattnere2969492005-10-21 21:17:10 +0000806 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000807 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
808 getI32Imm(1)), 0);
809 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
810 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000811 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000812 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000813 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000814 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000815 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000816 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000817 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000818 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000819 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000820 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000821 }
Chris Lattner491b8292005-10-06 19:03:35 +0000822 }
823 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000824
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000825 SDValue LHS = N->getOperand(0);
826 SDValue RHS = N->getOperand(1);
827
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000828 // Altivec Vector compare instructions do not set any CR register by default and
829 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000830 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000831 EVT VecVT = LHS.getValueType();
832 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000833 unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget->hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000834
835 switch (CC) {
836 case ISD::SETEQ:
837 case ISD::SETOEQ:
838 case ISD::SETUEQ:
839 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
840 case ISD::SETNE:
841 case ISD::SETONE:
842 case ISD::SETUNE: {
843 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Eric Christopher1b8e7632014-05-22 01:07:24 +0000844 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
Hal Finkel732f0f72014-03-26 12:49:28 +0000845 PPC::VNOR,
846 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000847 }
848 case ISD::SETLT:
849 case ISD::SETOLT:
850 case ISD::SETULT:
851 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
852 case ISD::SETGT:
853 case ISD::SETOGT:
854 case ISD::SETUGT:
855 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
856 case ISD::SETGE:
857 case ISD::SETOGE:
858 case ISD::SETUGE: {
859 // Small optimization: Altivec provides a 'Vector Compare Greater Than
860 // or Equal To' instruction (vcmpgefp), so in this case there is no
861 // need for extra logic for the equal compare.
862 if (VecVT.getSimpleVT().isFloatingPoint()) {
863 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
864 } else {
865 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Eric Christopher1b8e7632014-05-22 01:07:24 +0000866 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000867 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Eric Christopher1b8e7632014-05-22 01:07:24 +0000868 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR :
Hal Finkel732f0f72014-03-26 12:49:28 +0000869 PPC::VOR,
870 VecVT, VCmpGT, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000871 }
872 }
873 case ISD::SETLE:
874 case ISD::SETOLE:
875 case ISD::SETULE: {
876 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
Eric Christopher1b8e7632014-05-22 01:07:24 +0000877 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget->hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000878 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Eric Christopher1b8e7632014-05-22 01:07:24 +0000879 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLOR :
Hal Finkel732f0f72014-03-26 12:49:28 +0000880 PPC::VOR,
881 VecVT, VCmpLE, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000882 }
883 default:
884 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
885 }
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000886 }
887
Eric Christopher1b8e7632014-05-22 01:07:24 +0000888 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +0000889 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +0000890
Chris Lattner491b8292005-10-06 19:03:35 +0000891 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000892 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000893 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000894 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000895
Chris Lattner491b8292005-10-06 19:03:35 +0000896 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000897 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000898
Craig Topper062a2ba2014-04-25 05:30:21 +0000899 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000900 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000901 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000902
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000903 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
904 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000905
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000906 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000907 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000908 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +0000909 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +0000910
911 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000912 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000913 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000914 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000915}
Chris Lattner502a3692005-10-06 18:56:10 +0000916
Chris Lattner318622f2005-10-06 19:07:45 +0000917
Chris Lattner43ff01e2005-08-17 19:33:03 +0000918// Select - Convert the specified operand from a target-independent to a
919// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000920SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000921 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000922 if (N->isMachineOpcode()) {
923 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000924 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000925 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000926
Chris Lattner43ff01e2005-08-17 19:33:03 +0000927 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000928 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000929
Jim Laskey095e6f32006-12-12 13:23:43 +0000930 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000931 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000932 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000933 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000934 // Assume no remaining bits.
935 unsigned Remainder = 0;
936 // Assume no shift required.
937 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000938
Jim Laskey095e6f32006-12-12 13:23:43 +0000939 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000940 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000941 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000942 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000943
Jim Laskey095e6f32006-12-12 13:23:43 +0000944 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000945 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000946 // Go with the shifted value.
947 Imm = ImmSh;
948 } else {
949 // Still stuck with a 64 bit value.
950 Remainder = Imm;
951 Shift = 32;
952 Imm >>= 32;
953 }
954 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000955
Jim Laskey095e6f32006-12-12 13:23:43 +0000956 // Intermediate operand.
957 SDNode *Result;
958
959 // Handle first 32 bits.
960 unsigned Lo = Imm & 0xFFFF;
961 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000962
Jim Laskey095e6f32006-12-12 13:23:43 +0000963 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000964 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000965 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000966 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000967 } else if (Lo) {
968 // Handle the Hi bits.
969 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000970 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000971 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000972 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
973 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000974 } else {
975 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000976 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000977 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000978
Jim Laskey095e6f32006-12-12 13:23:43 +0000979 // If no shift, we're done.
980 if (!Shift) return Result;
981
982 // Shift for next step if the upper 32-bits were not zero.
983 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000984 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
985 SDValue(Result, 0),
986 getI32Imm(Shift),
987 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000988 }
989
990 // Add in the last bits as required.
991 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000992 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
993 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000994 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000995 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000996 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
997 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000998 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000999
Jim Laskey095e6f32006-12-12 13:23:43 +00001000 return Result;
1001 }
1002 break;
1003 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001004
Hal Finkel940ab932014-02-28 00:27:01 +00001005 case ISD::SETCC: {
1006 SDNode *SN = SelectSETCC(N);
1007 if (SN)
1008 return SN;
1009 break;
1010 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001011 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001012 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00001013
Chris Lattnere4c338d2005-08-25 00:45:43 +00001014 case ISD::FrameIndex: {
1015 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001016 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1017 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001018 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001019 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001020 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001021 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +00001022 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +00001023 }
Chris Lattner6961fc72006-03-26 10:06:40 +00001024
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001025 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001026 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001027 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1028 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00001029 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001030
Chris Lattner57693112005-09-28 22:50:24 +00001031 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001032 // FIXME: since this depends on the setting of the carry flag from the srawi
1033 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001034 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001035 // srl/add/sra pattern the dag combiner will generate for this as
1036 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001037 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001038 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001039 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001040 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001041 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001042 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001043 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001044 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001045 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001046 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001047 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001048 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001049 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001050 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001051 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1052 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001053 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001054 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001055 }
1056 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001057
Chris Lattner1de57062005-09-29 23:33:31 +00001058 // Other cases are autogenerated.
1059 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001060 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001061
Chris Lattnerce645542006-11-10 02:08:47 +00001062 case ISD::LOAD: {
1063 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001064 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001065 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001066
Chris Lattnerce645542006-11-10 02:08:47 +00001067 // Normal loads are handled by code generated from the .td file.
1068 if (LD->getAddressingMode() != ISD::PRE_INC)
1069 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001070
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001071 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001072 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001073 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001074
Chris Lattner474b5b72006-11-15 19:55:13 +00001075 unsigned Opcode;
1076 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001077 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001078 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001079 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1080 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001081 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001082 case MVT::f64: Opcode = PPC::LFDU; break;
1083 case MVT::f32: Opcode = PPC::LFSU; break;
1084 case MVT::i32: Opcode = PPC::LWZU; break;
1085 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1086 case MVT::i1:
1087 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001088 }
1089 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001090 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1091 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1092 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001093 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001094 case MVT::i64: Opcode = PPC::LDU; break;
1095 case MVT::i32: Opcode = PPC::LWZU8; break;
1096 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1097 case MVT::i1:
1098 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001099 }
1100 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001101
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001102 SDValue Chain = LD->getChain();
1103 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001104 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001105 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001106 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001107 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001108 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001109 unsigned Opcode;
1110 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1111 if (LD->getValueType(0) != MVT::i64) {
1112 // Handle PPC32 integer and normal FP loads.
1113 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1114 switch (LoadedVT.getSimpleVT().SimpleTy) {
1115 default: llvm_unreachable("Invalid PPC load type!");
1116 case MVT::f64: Opcode = PPC::LFDUX; break;
1117 case MVT::f32: Opcode = PPC::LFSUX; break;
1118 case MVT::i32: Opcode = PPC::LWZUX; break;
1119 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1120 case MVT::i1:
1121 case MVT::i8: Opcode = PPC::LBZUX; break;
1122 }
1123 } else {
1124 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1125 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1126 "Invalid sext update load");
1127 switch (LoadedVT.getSimpleVT().SimpleTy) {
1128 default: llvm_unreachable("Invalid PPC load type!");
1129 case MVT::i64: Opcode = PPC::LDUX; break;
1130 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1131 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1132 case MVT::i1:
1133 case MVT::i8: Opcode = PPC::LBZUX8; break;
1134 }
1135 }
1136
1137 SDValue Chain = LD->getChain();
1138 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001139 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001140 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001141 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001142 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001143 }
1144 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001145
Nate Begemanb3821a32005-08-18 07:30:46 +00001146 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001147 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001148 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001149
Nate Begemanb3821a32005-08-18 07:30:46 +00001150 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1151 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001152 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001153 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001154 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001155 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001156 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00001157 }
Nate Begemand31efd12006-09-22 05:01:56 +00001158 // If this is just a masked value where the input is not handled above, and
1159 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1160 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001161 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001162 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001163 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001164 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001165 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00001166 }
Hal Finkele39526a2012-08-28 02:10:15 +00001167 // If this is a 64-bit zero-extension mask, emit rldicl.
1168 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1169 isMask_64(Imm64)) {
1170 SDValue Val = N->getOperand(0);
1171 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001172 SH = 0;
1173
1174 // If the operand is a logical right shift, we can fold it into this
1175 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1176 // for n <= mb. The right shift is really a left rotate followed by a
1177 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1178 // by the shift.
1179 if (Val.getOpcode() == ISD::SRL &&
1180 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1181 assert(Imm < 64 && "Illegal shift amount");
1182 Val = Val.getOperand(0);
1183 SH = 64 - Imm;
1184 }
1185
1186 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00001187 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00001188 }
Nate Begemand31efd12006-09-22 05:01:56 +00001189 // AND X, 0 -> 0, not "rlwinm 32".
1190 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001191 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00001192 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00001193 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001194 // ISD::OR doesn't get all the bitfield insertion fun.
1195 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001196 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001197 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001198 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001199 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001200 Imm = ~(Imm^Imm2);
1201 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001202 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001203 N->getOperand(0).getOperand(1),
1204 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001205 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001206 }
1207 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001208
Chris Lattner1de57062005-09-29 23:33:31 +00001209 // Other cases are autogenerated.
1210 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001211 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001212 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001213 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001214 if (SDNode *I = SelectBitfieldInsert(N))
1215 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001216
Chris Lattner1de57062005-09-29 23:33:31 +00001217 // Other cases are autogenerated.
1218 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001219 case ISD::SHL: {
1220 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001221 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001222 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001223 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001224 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001225 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001226 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001227
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001228 // Other cases are autogenerated.
1229 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001230 }
1231 case ISD::SRL: {
1232 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001233 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001234 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001235 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001236 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001237 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001238 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001239
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001240 // Other cases are autogenerated.
1241 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001242 }
Hal Finkel940ab932014-02-28 00:27:01 +00001243 // FIXME: Remove this once the ANDI glue bug is fixed:
1244 case PPCISD::ANDIo_1_EQ_BIT:
1245 case PPCISD::ANDIo_1_GT_BIT: {
1246 if (!ANDIGlueBug)
1247 break;
1248
1249 EVT InVT = N->getOperand(0).getValueType();
1250 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1251 "Invalid input type for ANDIo_1_EQ_BIT");
1252
1253 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1254 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1255 N->getOperand(0),
1256 CurDAG->getTargetConstant(1, InVT)), 0);
1257 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1258 SDValue SRIdxVal =
1259 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1260 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1261
1262 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1263 CR0Reg, SRIdxVal,
1264 SDValue(AndI.getNode(), 1) /* glue */);
1265 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001266 case ISD::SELECT_CC: {
1267 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001268 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1269 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001270
Hal Finkel940ab932014-02-28 00:27:01 +00001271 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001272 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00001273 N->getOperand(0).getValueType() == MVT::i1)
1274 break;
1275
Chris Lattner97b3da12006-06-27 00:04:13 +00001276 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001277 if (!isPPC64)
1278 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1279 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1280 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1281 if (N1C->isNullValue() && N3C->isNullValue() &&
1282 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1283 // FIXME: Implement this optzn for PPC64.
1284 N->getValueType(0) == MVT::i32) {
1285 SDNode *Tmp =
1286 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1287 N->getOperand(0), getI32Imm(~0U));
1288 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1289 SDValue(Tmp, 0), N->getOperand(0),
1290 SDValue(Tmp, 1));
1291 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001292
Dale Johannesenab8e4422009-02-06 19:16:40 +00001293 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001294
1295 if (N->getValueType(0) == MVT::i1) {
1296 // An i1 select is: (c & t) | (!c & f).
1297 bool Inv;
1298 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1299
1300 unsigned SRI;
1301 switch (Idx) {
1302 default: llvm_unreachable("Invalid CC index");
1303 case 0: SRI = PPC::sub_lt; break;
1304 case 1: SRI = PPC::sub_gt; break;
1305 case 2: SRI = PPC::sub_eq; break;
1306 case 3: SRI = PPC::sub_un; break;
1307 }
1308
1309 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1310
1311 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1312 CCBit, CCBit), 0);
1313 SDValue C = Inv ? NotCCBit : CCBit,
1314 NotC = Inv ? CCBit : NotCCBit;
1315
1316 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1317 C, N->getOperand(2)), 0);
1318 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1319 NotC, N->getOperand(3)), 0);
1320
1321 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1322 }
1323
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001324 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001325
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001326 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001327 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001328 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001329 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001330 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001331 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001332 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001333 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001334 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001335 else
1336 SelectCCOp = PPC::SELECT_CC_VRRC;
1337
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001338 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001339 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00001340 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001341 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001342 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001343 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00001344 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001345 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00001346 }
1347
1348 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001349 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001350 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001351 N->getValueType(0) == MVT::v2i64)) {
1352 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1353
1354 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1355 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1356 unsigned DM[2];
1357
1358 for (int i = 0; i < 2; ++i)
1359 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1360 DM[i] = 0;
1361 else
1362 DM[i] = 1;
1363
Hal Finkel2583b062014-03-28 20:24:55 +00001364 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001365
1366 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1367 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1368 isa<LoadSDNode>(Op1.getOperand(0))) {
1369 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1370 SDValue Base, Offset;
1371
1372 if (LD->isUnindexed() &&
1373 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1374 SDValue Chain = LD->getChain();
1375 SDValue Ops[] = { Base, Offset, Chain };
1376 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00001377 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001378 }
1379 }
1380
1381 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00001382 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001383 }
1384
1385 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001386 case PPCISD::BDNZ:
1387 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00001388 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00001389 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1390 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1391 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1392 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00001393 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00001394 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001395 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001396 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001397 // Op #1 is the PPC::PRED_* number.
1398 // Op #2 is the CR#
1399 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001400 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001401 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001402 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001403 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001404 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001405 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00001406 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001407 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001408 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001409 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001410 unsigned PCC = getPredicateForSetCC(CC);
1411
1412 if (N->getOperand(2).getValueType() == MVT::i1) {
1413 unsigned Opc;
1414 bool Swap;
1415 switch (PCC) {
1416 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1417 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1418 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1419 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1420 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1421 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1422 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1423 }
1424
1425 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1426 N->getOperand(Swap ? 3 : 2),
1427 N->getOperand(Swap ? 2 : 3)), 0);
1428 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1429 BitComp, N->getOperand(4), N->getOperand(0));
1430 }
1431
Dale Johannesenab8e4422009-02-06 19:16:40 +00001432 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001433 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001434 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001435 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001436 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001437 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001438 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001439 SDValue Chain = N->getOperand(0);
1440 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001441 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001442 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001443 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001444 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001445 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001446 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001447 case PPCISD::TOC_ENTRY: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00001448 assert (PPCSubTarget->isPPC64() && "Only supported for 64-bit ABI");
Bill Schmidt34627e32012-11-27 17:35:46 +00001449
Bill Schmidt27917782013-02-21 17:12:27 +00001450 // For medium and large code model, we generate two instructions as
1451 // described below. Otherwise we allow SelectCodeCommon to handle this,
1452 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1453 CodeModel::Model CModel = TM.getCodeModel();
1454 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001455 break;
1456
1457 // The first source operand is a TargetGlobalAddress or a
1458 // TargetJumpTable. If it is an externally defined symbol, a symbol
1459 // with common linkage, a function address, or a jump table address,
Bill Schmidt27917782013-02-21 17:12:27 +00001460 // or if we are generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001461 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1462 // Otherwise we generate:
1463 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1464 SDValue GA = N->getOperand(0);
1465 SDValue TOCbase = N->getOperand(1);
1466 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1467 TOCbase, GA);
1468
Bill Schmidt27917782013-02-21 17:12:27 +00001469 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001470 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1471 SDValue(Tmp, 0));
1472
1473 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1474 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001475 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
Rafael Espindolae0098922014-05-16 22:37:03 +00001476 const GlobalValue *RealGValue = GAlias ? GAlias->getAliasee() : GValue;
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001477 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1478 assert((GVar || isa<Function>(RealGValue)) &&
Bill Schmidt34627e32012-11-27 17:35:46 +00001479 "Unexpected global value subclass!");
1480
1481 // An external variable is one without an initializer. For these,
1482 // for variables with common linkage, and for Functions, generate
1483 // the LDtocL form.
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001484 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1485 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001486 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1487 SDValue(Tmp, 0));
1488 }
1489
1490 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1491 SDValue(Tmp, 0), GA);
1492 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001493 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001494 // This expands into one of three sequences, depending on whether
1495 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001496 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1497 isa<ConstantSDNode>(N->getOperand(1)) &&
1498 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001499
1500 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001501 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001502 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001503 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001504
Bill Schmidt51e79512013-02-20 15:50:31 +00001505 if (EltSize == 1) {
1506 Opc1 = PPC::VSPLTISB;
1507 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001508 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001509 VT = MVT::v16i8;
1510 } else if (EltSize == 2) {
1511 Opc1 = PPC::VSPLTISH;
1512 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001513 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001514 VT = MVT::v8i16;
1515 } else {
1516 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1517 Opc1 = PPC::VSPLTISW;
1518 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001519 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001520 VT = MVT::v4i32;
1521 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001522
1523 if ((Elt & 1) == 0) {
1524 // Elt is even, in the range [-32,-18] + [16,30].
1525 //
1526 // Convert: VADD_SPLAT elt, size
1527 // Into: tmp = VSPLTIS[BHW] elt
1528 // VADDU[BHW]M tmp, tmp
1529 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1530 SDValue EltVal = getI32Imm(Elt >> 1);
1531 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1532 SDValue TmpVal = SDValue(Tmp, 0);
1533 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1534
1535 } else if (Elt > 0) {
1536 // Elt is odd and positive, in the range [17,31].
1537 //
1538 // Convert: VADD_SPLAT elt, size
1539 // Into: tmp1 = VSPLTIS[BHW] elt-16
1540 // tmp2 = VSPLTIS[BHW] -16
1541 // VSUBU[BHW]M tmp1, tmp2
1542 SDValue EltVal = getI32Imm(Elt - 16);
1543 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1544 EltVal = getI32Imm(-16);
1545 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1546 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1547 SDValue(Tmp2, 0));
1548
1549 } else {
1550 // Elt is odd and negative, in the range [-31,-17].
1551 //
1552 // Convert: VADD_SPLAT elt, size
1553 // Into: tmp1 = VSPLTIS[BHW] elt+16
1554 // tmp2 = VSPLTIS[BHW] -16
1555 // VADDU[BHW]M tmp1, tmp2
1556 SDValue EltVal = getI32Imm(Elt + 16);
1557 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1558 EltVal = getI32Imm(-16);
1559 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1560 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1561 SDValue(Tmp2, 0));
1562 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001563 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001564 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001565
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001566 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001567}
1568
Hal Finkel860fa902014-01-02 22:09:39 +00001569/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001570/// on the DAG representation.
1571void PPCDAGToDAGISel::PostprocessISelDAG() {
1572
1573 // Skip peepholes at -O0.
1574 if (TM.getOptLevel() == CodeGenOpt::None)
1575 return;
1576
Hal Finkel940ab932014-02-28 00:27:01 +00001577 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00001578 PeepholeCROps();
Hal Finkel940ab932014-02-28 00:27:01 +00001579}
1580
Hal Finkelb9989152014-02-28 06:11:16 +00001581// Check if all users of this node will become isel where the second operand
1582// is the constant zero. If this is so, and if we can negate the condition,
1583// then we can flip the true and false operands. This will allow the zero to
1584// be folded with the isel so that we don't need to materialize a register
1585// containing zero.
1586bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1587 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001588 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00001589 return false;
1590
1591 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1592 UI != UE; ++UI) {
1593 SDNode *User = *UI;
1594 if (!User->isMachineOpcode())
1595 return false;
1596 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1597 User->getMachineOpcode() != PPC::SELECT_I8)
1598 return false;
1599
1600 SDNode *Op2 = User->getOperand(2).getNode();
1601 if (!Op2->isMachineOpcode())
1602 return false;
1603
1604 if (Op2->getMachineOpcode() != PPC::LI &&
1605 Op2->getMachineOpcode() != PPC::LI8)
1606 return false;
1607
1608 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1609 if (!C)
1610 return false;
1611
1612 if (!C->isNullValue())
1613 return false;
1614 }
1615
1616 return true;
1617}
1618
1619void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1620 SmallVector<SDNode *, 4> ToReplace;
1621 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1622 UI != UE; ++UI) {
1623 SDNode *User = *UI;
1624 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1625 User->getMachineOpcode() == PPC::SELECT_I8) &&
1626 "Must have all select users");
1627 ToReplace.push_back(User);
1628 }
1629
1630 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1631 UE = ToReplace.end(); UI != UE; ++UI) {
1632 SDNode *User = *UI;
1633 SDNode *ResNode =
1634 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1635 User->getValueType(0), User->getOperand(0),
1636 User->getOperand(2),
1637 User->getOperand(1));
1638
1639 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1640 DEBUG(User->dump(CurDAG));
1641 DEBUG(dbgs() << "\nNew: ");
1642 DEBUG(ResNode->dump(CurDAG));
1643 DEBUG(dbgs() << "\n");
1644
1645 ReplaceUses(User, ResNode);
1646 }
1647}
1648
Eric Christopher02e18042014-05-14 00:31:15 +00001649void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00001650 bool IsModified;
1651 do {
1652 IsModified = false;
1653 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1654 E = CurDAG->allnodes_end(); I != E; ++I) {
1655 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1656 if (!MachineNode || MachineNode->use_empty())
1657 continue;
1658 SDNode *ResNode = MachineNode;
1659
1660 bool Op1Set = false, Op1Unset = false,
1661 Op1Not = false,
1662 Op2Set = false, Op2Unset = false,
1663 Op2Not = false;
1664
1665 unsigned Opcode = MachineNode->getMachineOpcode();
1666 switch (Opcode) {
1667 default: break;
1668 case PPC::CRAND:
1669 case PPC::CRNAND:
1670 case PPC::CROR:
1671 case PPC::CRXOR:
1672 case PPC::CRNOR:
1673 case PPC::CREQV:
1674 case PPC::CRANDC:
1675 case PPC::CRORC: {
1676 SDValue Op = MachineNode->getOperand(1);
1677 if (Op.isMachineOpcode()) {
1678 if (Op.getMachineOpcode() == PPC::CRSET)
1679 Op2Set = true;
1680 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1681 Op2Unset = true;
1682 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1683 Op.getOperand(0) == Op.getOperand(1))
1684 Op2Not = true;
1685 }
1686 } // fallthrough
1687 case PPC::BC:
1688 case PPC::BCn:
1689 case PPC::SELECT_I4:
1690 case PPC::SELECT_I8:
1691 case PPC::SELECT_F4:
1692 case PPC::SELECT_F8:
1693 case PPC::SELECT_VRRC: {
1694 SDValue Op = MachineNode->getOperand(0);
1695 if (Op.isMachineOpcode()) {
1696 if (Op.getMachineOpcode() == PPC::CRSET)
1697 Op1Set = true;
1698 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1699 Op1Unset = true;
1700 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1701 Op.getOperand(0) == Op.getOperand(1))
1702 Op1Not = true;
1703 }
1704 }
1705 break;
1706 }
1707
Hal Finkelb9989152014-02-28 06:11:16 +00001708 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001709 switch (Opcode) {
1710 default: break;
1711 case PPC::CRAND:
1712 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1713 // x & x = x
1714 ResNode = MachineNode->getOperand(0).getNode();
1715 else if (Op1Set)
1716 // 1 & y = y
1717 ResNode = MachineNode->getOperand(1).getNode();
1718 else if (Op2Set)
1719 // x & 1 = x
1720 ResNode = MachineNode->getOperand(0).getNode();
1721 else if (Op1Unset || Op2Unset)
1722 // x & 0 = 0 & y = 0
1723 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1724 MVT::i1);
1725 else if (Op1Not)
1726 // ~x & y = andc(y, x)
1727 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1728 MVT::i1, MachineNode->getOperand(1),
1729 MachineNode->getOperand(0).
1730 getOperand(0));
1731 else if (Op2Not)
1732 // x & ~y = andc(x, y)
1733 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1734 MVT::i1, MachineNode->getOperand(0),
1735 MachineNode->getOperand(1).
1736 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001737 else if (AllUsersSelectZero(MachineNode))
1738 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1739 MVT::i1, MachineNode->getOperand(0),
1740 MachineNode->getOperand(1)),
1741 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001742 break;
1743 case PPC::CRNAND:
1744 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1745 // nand(x, x) -> nor(x, x)
1746 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1747 MVT::i1, MachineNode->getOperand(0),
1748 MachineNode->getOperand(0));
1749 else if (Op1Set)
1750 // nand(1, y) -> nor(y, y)
1751 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1752 MVT::i1, MachineNode->getOperand(1),
1753 MachineNode->getOperand(1));
1754 else if (Op2Set)
1755 // nand(x, 1) -> nor(x, x)
1756 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1757 MVT::i1, MachineNode->getOperand(0),
1758 MachineNode->getOperand(0));
1759 else if (Op1Unset || Op2Unset)
1760 // nand(x, 0) = nand(0, y) = 1
1761 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1762 MVT::i1);
1763 else if (Op1Not)
1764 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1765 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1766 MVT::i1, MachineNode->getOperand(0).
1767 getOperand(0),
1768 MachineNode->getOperand(1));
1769 else if (Op2Not)
1770 // nand(x, ~y) = ~x | y = orc(y, x)
1771 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1772 MVT::i1, MachineNode->getOperand(1).
1773 getOperand(0),
1774 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001775 else if (AllUsersSelectZero(MachineNode))
1776 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1777 MVT::i1, MachineNode->getOperand(0),
1778 MachineNode->getOperand(1)),
1779 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001780 break;
1781 case PPC::CROR:
1782 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1783 // x | x = x
1784 ResNode = MachineNode->getOperand(0).getNode();
1785 else if (Op1Set || Op2Set)
1786 // x | 1 = 1 | y = 1
1787 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1788 MVT::i1);
1789 else if (Op1Unset)
1790 // 0 | y = y
1791 ResNode = MachineNode->getOperand(1).getNode();
1792 else if (Op2Unset)
1793 // x | 0 = x
1794 ResNode = MachineNode->getOperand(0).getNode();
1795 else if (Op1Not)
1796 // ~x | y = orc(y, x)
1797 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1798 MVT::i1, MachineNode->getOperand(1),
1799 MachineNode->getOperand(0).
1800 getOperand(0));
1801 else if (Op2Not)
1802 // x | ~y = orc(x, y)
1803 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1804 MVT::i1, MachineNode->getOperand(0),
1805 MachineNode->getOperand(1).
1806 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001807 else if (AllUsersSelectZero(MachineNode))
1808 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1809 MVT::i1, MachineNode->getOperand(0),
1810 MachineNode->getOperand(1)),
1811 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001812 break;
1813 case PPC::CRXOR:
1814 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1815 // xor(x, x) = 0
1816 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1817 MVT::i1);
1818 else if (Op1Set)
1819 // xor(1, y) -> nor(y, y)
1820 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1821 MVT::i1, MachineNode->getOperand(1),
1822 MachineNode->getOperand(1));
1823 else if (Op2Set)
1824 // xor(x, 1) -> nor(x, x)
1825 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1826 MVT::i1, MachineNode->getOperand(0),
1827 MachineNode->getOperand(0));
1828 else if (Op1Unset)
1829 // xor(0, y) = y
1830 ResNode = MachineNode->getOperand(1).getNode();
1831 else if (Op2Unset)
1832 // xor(x, 0) = x
1833 ResNode = MachineNode->getOperand(0).getNode();
1834 else if (Op1Not)
1835 // xor(~x, y) = eqv(x, y)
1836 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1837 MVT::i1, MachineNode->getOperand(0).
1838 getOperand(0),
1839 MachineNode->getOperand(1));
1840 else if (Op2Not)
1841 // xor(x, ~y) = eqv(x, y)
1842 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1843 MVT::i1, MachineNode->getOperand(0),
1844 MachineNode->getOperand(1).
1845 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001846 else if (AllUsersSelectZero(MachineNode))
1847 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1848 MVT::i1, MachineNode->getOperand(0),
1849 MachineNode->getOperand(1)),
1850 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001851 break;
1852 case PPC::CRNOR:
1853 if (Op1Set || Op2Set)
1854 // nor(1, y) -> 0
1855 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1856 MVT::i1);
1857 else if (Op1Unset)
1858 // nor(0, y) = ~y -> nor(y, y)
1859 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1860 MVT::i1, MachineNode->getOperand(1),
1861 MachineNode->getOperand(1));
1862 else if (Op2Unset)
1863 // nor(x, 0) = ~x
1864 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1865 MVT::i1, MachineNode->getOperand(0),
1866 MachineNode->getOperand(0));
1867 else if (Op1Not)
1868 // nor(~x, y) = andc(x, y)
1869 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1870 MVT::i1, MachineNode->getOperand(0).
1871 getOperand(0),
1872 MachineNode->getOperand(1));
1873 else if (Op2Not)
1874 // nor(x, ~y) = andc(y, x)
1875 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1876 MVT::i1, MachineNode->getOperand(1).
1877 getOperand(0),
1878 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001879 else if (AllUsersSelectZero(MachineNode))
1880 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1881 MVT::i1, MachineNode->getOperand(0),
1882 MachineNode->getOperand(1)),
1883 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001884 break;
1885 case PPC::CREQV:
1886 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1887 // eqv(x, x) = 1
1888 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1889 MVT::i1);
1890 else if (Op1Set)
1891 // eqv(1, y) = y
1892 ResNode = MachineNode->getOperand(1).getNode();
1893 else if (Op2Set)
1894 // eqv(x, 1) = x
1895 ResNode = MachineNode->getOperand(0).getNode();
1896 else if (Op1Unset)
1897 // eqv(0, y) = ~y -> nor(y, y)
1898 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1899 MVT::i1, MachineNode->getOperand(1),
1900 MachineNode->getOperand(1));
1901 else if (Op2Unset)
1902 // eqv(x, 0) = ~x
1903 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1904 MVT::i1, MachineNode->getOperand(0),
1905 MachineNode->getOperand(0));
1906 else if (Op1Not)
1907 // eqv(~x, y) = xor(x, y)
1908 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1909 MVT::i1, MachineNode->getOperand(0).
1910 getOperand(0),
1911 MachineNode->getOperand(1));
1912 else if (Op2Not)
1913 // eqv(x, ~y) = xor(x, y)
1914 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1915 MVT::i1, MachineNode->getOperand(0),
1916 MachineNode->getOperand(1).
1917 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001918 else if (AllUsersSelectZero(MachineNode))
1919 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1920 MVT::i1, MachineNode->getOperand(0),
1921 MachineNode->getOperand(1)),
1922 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001923 break;
1924 case PPC::CRANDC:
1925 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1926 // andc(x, x) = 0
1927 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1928 MVT::i1);
1929 else if (Op1Set)
1930 // andc(1, y) = ~y
1931 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1932 MVT::i1, MachineNode->getOperand(1),
1933 MachineNode->getOperand(1));
1934 else if (Op1Unset || Op2Set)
1935 // andc(0, y) = andc(x, 1) = 0
1936 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1937 MVT::i1);
1938 else if (Op2Unset)
1939 // andc(x, 0) = x
1940 ResNode = MachineNode->getOperand(0).getNode();
1941 else if (Op1Not)
1942 // andc(~x, y) = ~(x | y) = nor(x, y)
1943 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1944 MVT::i1, MachineNode->getOperand(0).
1945 getOperand(0),
1946 MachineNode->getOperand(1));
1947 else if (Op2Not)
1948 // andc(x, ~y) = x & y
1949 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1950 MVT::i1, MachineNode->getOperand(0),
1951 MachineNode->getOperand(1).
1952 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001953 else if (AllUsersSelectZero(MachineNode))
1954 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1955 MVT::i1, MachineNode->getOperand(1),
1956 MachineNode->getOperand(0)),
1957 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001958 break;
1959 case PPC::CRORC:
1960 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1961 // orc(x, x) = 1
1962 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1963 MVT::i1);
1964 else if (Op1Set || Op2Unset)
1965 // orc(1, y) = orc(x, 0) = 1
1966 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1967 MVT::i1);
1968 else if (Op2Set)
1969 // orc(x, 1) = x
1970 ResNode = MachineNode->getOperand(0).getNode();
1971 else if (Op1Unset)
1972 // orc(0, y) = ~y
1973 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1974 MVT::i1, MachineNode->getOperand(1),
1975 MachineNode->getOperand(1));
1976 else if (Op1Not)
1977 // orc(~x, y) = ~(x & y) = nand(x, y)
1978 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1979 MVT::i1, MachineNode->getOperand(0).
1980 getOperand(0),
1981 MachineNode->getOperand(1));
1982 else if (Op2Not)
1983 // orc(x, ~y) = x | y
1984 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1985 MVT::i1, MachineNode->getOperand(0),
1986 MachineNode->getOperand(1).
1987 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001988 else if (AllUsersSelectZero(MachineNode))
1989 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1990 MVT::i1, MachineNode->getOperand(1),
1991 MachineNode->getOperand(0)),
1992 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001993 break;
1994 case PPC::SELECT_I4:
1995 case PPC::SELECT_I8:
1996 case PPC::SELECT_F4:
1997 case PPC::SELECT_F8:
1998 case PPC::SELECT_VRRC:
1999 if (Op1Set)
2000 ResNode = MachineNode->getOperand(1).getNode();
2001 else if (Op1Unset)
2002 ResNode = MachineNode->getOperand(2).getNode();
2003 else if (Op1Not)
2004 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2005 SDLoc(MachineNode),
2006 MachineNode->getValueType(0),
2007 MachineNode->getOperand(0).
2008 getOperand(0),
2009 MachineNode->getOperand(2),
2010 MachineNode->getOperand(1));
2011 break;
2012 case PPC::BC:
2013 case PPC::BCn:
2014 if (Op1Not)
2015 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2016 PPC::BC,
2017 SDLoc(MachineNode),
2018 MVT::Other,
2019 MachineNode->getOperand(0).
2020 getOperand(0),
2021 MachineNode->getOperand(1),
2022 MachineNode->getOperand(2));
2023 // FIXME: Handle Op1Set, Op1Unset here too.
2024 break;
2025 }
2026
Hal Finkelb9989152014-02-28 06:11:16 +00002027 // If we're inverting this node because it is used only by selects that
2028 // we'd like to swap, then swap the selects before the node replacement.
2029 if (SelectSwap)
2030 SwapAllSelectUsers(MachineNode);
2031
Hal Finkel940ab932014-02-28 00:27:01 +00002032 if (ResNode != MachineNode) {
2033 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2034 DEBUG(MachineNode->dump(CurDAG));
2035 DEBUG(dbgs() << "\nNew: ");
2036 DEBUG(ResNode->dump(CurDAG));
2037 DEBUG(dbgs() << "\n");
2038
2039 ReplaceUses(MachineNode, ResNode);
2040 IsModified = true;
2041 }
2042 }
2043 if (IsModified)
2044 CurDAG->RemoveDeadNodes();
2045 } while (IsModified);
2046}
2047
2048void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002049 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002050 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002051 return;
2052
2053 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2054 ++Position;
2055
2056 while (Position != CurDAG->allnodes_begin()) {
2057 SDNode *N = --Position;
2058 // Skip dead nodes and any non-machine opcodes.
2059 if (N->use_empty() || !N->isMachineOpcode())
2060 continue;
2061
2062 unsigned FirstOp;
2063 unsigned StorageOpcode = N->getMachineOpcode();
2064
2065 switch (StorageOpcode) {
2066 default: continue;
2067
2068 case PPC::LBZ:
2069 case PPC::LBZ8:
2070 case PPC::LD:
2071 case PPC::LFD:
2072 case PPC::LFS:
2073 case PPC::LHA:
2074 case PPC::LHA8:
2075 case PPC::LHZ:
2076 case PPC::LHZ8:
2077 case PPC::LWA:
2078 case PPC::LWZ:
2079 case PPC::LWZ8:
2080 FirstOp = 0;
2081 break;
2082
2083 case PPC::STB:
2084 case PPC::STB8:
2085 case PPC::STD:
2086 case PPC::STFD:
2087 case PPC::STFS:
2088 case PPC::STH:
2089 case PPC::STH8:
2090 case PPC::STW:
2091 case PPC::STW8:
2092 FirstOp = 1;
2093 break;
2094 }
2095
2096 // If this is a load or store with a zero offset, we may be able to
2097 // fold an add-immediate into the memory operation.
2098 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2099 N->getConstantOperandVal(FirstOp) != 0)
2100 continue;
2101
2102 SDValue Base = N->getOperand(FirstOp + 1);
2103 if (!Base.isMachineOpcode())
2104 continue;
2105
2106 unsigned Flags = 0;
2107 bool ReplaceFlags = true;
2108
2109 // When the feeding operation is an add-immediate of some sort,
2110 // determine whether we need to add relocation information to the
2111 // target flags on the immediate operand when we fold it into the
2112 // load instruction.
2113 //
2114 // For something like ADDItocL, the relocation information is
2115 // inferred from the opcode; when we process it in the AsmPrinter,
2116 // we add the necessary relocation there. A load, though, can receive
2117 // relocation from various flavors of ADDIxxx, so we need to carry
2118 // the relocation information in the target flags.
2119 switch (Base.getMachineOpcode()) {
2120 default: continue;
2121
2122 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002123 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002124 // In some cases (such as TLS) the relocation information
2125 // is already in place on the operand, so copying the operand
2126 // is sufficient.
2127 ReplaceFlags = false;
2128 // For these cases, the immediate may not be divisible by 4, in
2129 // which case the fold is illegal for DS-form instructions. (The
2130 // other cases provide aligned addresses and are always safe.)
2131 if ((StorageOpcode == PPC::LWA ||
2132 StorageOpcode == PPC::LD ||
2133 StorageOpcode == PPC::STD) &&
2134 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2135 Base.getConstantOperandVal(1) % 4 != 0))
2136 continue;
2137 break;
2138 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002139 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002140 break;
2141 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002142 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002143 break;
2144 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002145 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002146 break;
2147 }
2148
2149 // We found an opportunity. Reverse the operands from the add
2150 // immediate and substitute them into the load or store. If
2151 // needed, update the target flags for the immediate operand to
2152 // reflect the necessary relocation information.
2153 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2154 DEBUG(Base->dump(CurDAG));
2155 DEBUG(dbgs() << "\nN: ");
2156 DEBUG(N->dump(CurDAG));
2157 DEBUG(dbgs() << "\n");
2158
2159 SDValue ImmOpnd = Base.getOperand(1);
2160
2161 // If the relocation information isn't already present on the
2162 // immediate operand, add it now.
2163 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002164 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002165 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002166 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002167 // We can't perform this optimization for data whose alignment
2168 // is insufficient for the instruction encoding.
2169 if (GV->getAlignment() < 4 &&
2170 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2171 StorageOpcode == PPC::LWA)) {
2172 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2173 continue;
2174 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002175 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002176 } else if (ConstantPoolSDNode *CP =
2177 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002178 const Constant *C = CP->getConstVal();
2179 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2180 CP->getAlignment(),
2181 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002182 }
2183 }
2184
2185 if (FirstOp == 1) // Store
2186 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2187 Base.getOperand(0), N->getOperand(3));
2188 else // Load
2189 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2190 N->getOperand(2));
2191
2192 // The add-immediate may now be dead, in which case remove it.
2193 if (Base.getNode()->use_empty())
2194 CurDAG->RemoveDeadNode(Base.getNode());
2195 }
2196}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002197
Chris Lattnerb055c872006-06-10 01:15:02 +00002198
Andrew Trickc416ba62010-12-24 04:28:06 +00002199/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002200/// PowerPC-specific DAG, ready for instruction scheduling.
2201///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002202FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002203 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002204}
2205
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002206static void initializePassOnce(PassRegistry &Registry) {
2207 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00002208 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2209 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002210 Registry.registerPass(*PI, true);
2211}
2212
2213void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2214 CALL_ONCE_INITIALIZATION(initializePassOnce);
2215}
2216