blob: 74f6b18fd03a02f14ce9a52ca4834f086b0dfea1 [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"
Hal Finkel3ee2af72014-07-18 23:29:49 +000017#include "PPCMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000026#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Intrinsics.h"
Hal Finkel940ab932014-02-28 00:27:01 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000031#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000036using namespace llvm;
37
Chandler Carruth84e68b22014-04-22 02:41:26 +000038#define DEBUG_TYPE "ppc-codegen"
39
Hal Finkel940ab932014-02-28 00:27:01 +000040// FIXME: Remove this once the bug has been fixed!
41cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
42cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
43
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000044namespace llvm {
45 void initializePPCDAGToDAGISelPass(PassRegistry&);
46}
47
Chris Lattner43ff01e2005-08-17 19:33:03 +000048namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000049 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000050 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000051 /// instructions for SelectionDAG operations.
52 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000053 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000054 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000055 const PPCTargetLowering *PPCLowering;
56 const PPCSubtarget *PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000057 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000058 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000059 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christopherd9134482014-08-04 21:25:23 +000060 : SelectionDAGISel(tm), TM(tm),
61 PPCLowering(TM.getSubtargetImpl()->getTargetLowering()),
62 PPCSubTarget(TM.getSubtargetImpl()) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000063 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
64 }
Andrew Trickc416ba62010-12-24 04:28:06 +000065
Craig Topper0d3fa922014-04-29 07:57:37 +000066 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000067 // Make sure we re-emit a set of the global base reg if necessary
68 GlobalBaseReg = 0;
Eric Christopherd9134482014-08-04 21:25:23 +000069 PPCLowering = TM.getSubtargetImpl()->getTargetLowering();
Eric Christopher1b8e7632014-05-22 01:07:24 +000070 PPCSubTarget = TM.getSubtargetImpl();
Dan Gohman5ea74d52009-07-31 18:16:33 +000071 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000072
Eric Christopher1b8e7632014-05-22 01:07:24 +000073 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000074 InsertVRSaveCode(MF);
75
Chris Lattner1678a6c2006-03-16 18:25:23 +000076 return true;
Chris Lattner45640392005-08-19 22:38:53 +000077 }
Andrew Trickc416ba62010-12-24 04:28:06 +000078
Craig Topper0d3fa922014-04-29 07:57:37 +000079 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000080
Chris Lattner43ff01e2005-08-17 19:33:03 +000081 /// getI32Imm - Return a target constant with the specified value, of type
82 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000083 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000084 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000085 }
Chris Lattner45640392005-08-19 22:38:53 +000086
Chris Lattner97b3da12006-06-27 00:04:13 +000087 /// getI64Imm - Return a target constant with the specified value, of type
88 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000089 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000090 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000091 }
Andrew Trickc416ba62010-12-24 04:28:06 +000092
Chris Lattner97b3da12006-06-27 00:04:13 +000093 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000094 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +000095 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +000096 }
Andrew Trickc416ba62010-12-24 04:28:06 +000097
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000098 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000099 /// with any number of 0s on either side. The 1s are allowed to wrap from
100 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
101 /// 0x0F0F0000 is not, since all 1s are not contiguous.
102 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
103
104
105 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
106 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000107 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000108 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000109
Chris Lattner45640392005-08-19 22:38:53 +0000110 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
111 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000112 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000113
Chris Lattner43ff01e2005-08-17 19:33:03 +0000114 // Select - Convert the specified operand from a target-independent to a
115 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000116 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000117
Nate Begeman93c4bc62005-08-19 00:38:14 +0000118 SDNode *SelectBitfieldInsert(SDNode *N);
119
Chris Lattner2a1823d2005-08-21 18:50:37 +0000120 /// SelectCC - Select a comparison of the specified values with the
121 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000122 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000123
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000124 /// SelectAddrImm - Returns true if the address N can be represented by
125 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000126 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000127 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000128 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000129 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000130
Chris Lattner6f5840c2006-11-16 00:41:37 +0000131 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000132 /// immediate field. Note that the operand at this point is already the
133 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000134 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000135 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000136 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000137 Out = N;
138 return true;
139 }
140
141 return false;
142 }
143
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000144 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
145 /// represented as an indexed [r+r] operation. Returns false if it can
146 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000147 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000148 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000149 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000150
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000151 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
152 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000153 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000154 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000155 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000156
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000157 /// SelectAddrImmX4 - Returns true if the address N can be represented by
158 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
159 /// Suitable for use by STD and friends.
160 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000161 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000162 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000163
Hal Finkel756810f2013-03-21 21:37:52 +0000164 // Select an address into a single register.
165 bool SelectAddr(SDValue N, SDValue &Base) {
166 Base = N;
167 return true;
168 }
169
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000170 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000171 /// inline asm expressions. It is always correct to compute the value into
172 /// a register. The case of adding a (possibly relocatable) constant to a
173 /// register can be improved, but it is wrong to substitute Reg+Reg for
174 /// Reg in an asm, because the load or store opcode would have to change.
Craig Topper0d3fa922014-04-29 07:57:37 +0000175 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
176 char ConstraintCode,
177 std::vector<SDValue> &OutOps) override {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000178 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000179 return false;
180 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000181
Dan Gohman5ea74d52009-07-31 18:16:33 +0000182 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000183
Craig Topper0d3fa922014-04-29 07:57:37 +0000184 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000185 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000186 }
187
Chris Lattner03e08ee2005-09-13 22:03:06 +0000188// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000189#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000190
Chris Lattner259e6c72005-10-06 18:45:51 +0000191private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000192 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000193
194 void PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +0000195 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000196
197 bool AllUsersSelectZero(SDNode *N);
198 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000199 };
200}
201
Chris Lattner1678a6c2006-03-16 18:25:23 +0000202/// InsertVRSaveCode - Once the entire function has been instruction selected,
203/// all virtual registers are created and all machine instructions are built,
204/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000205void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000206 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000207 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000208 //
Dan Gohman4a618822010-02-10 16:03:48 +0000209 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000210 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000211 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000212 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
213 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
214 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000215 HasVectorVReg = true;
216 break;
217 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000218 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000219 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000220
Chris Lattner02e2c182006-03-13 21:52:10 +0000221 // If we have a vector register, we want to emit code into the entry and exit
222 // blocks to save and restore the VRSAVE register. We do this here (instead
223 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
224 //
225 // 1. This (trivially) reduces the load on the register allocator, by not
226 // having to represent the live range of the VRSAVE register.
227 // 2. This (more significantly) allows us to create a temporary virtual
228 // register to hold the saved VRSAVE value, allowing this temporary to be
229 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000230
231 // Create two vregs - one to hold the VRSAVE register that is live-in to the
232 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000233 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
234 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000235
Eric Christopherd9134482014-08-04 21:25:23 +0000236 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000237 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000238 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000239 // Emit the following code into the entry block:
240 // InVRSAVE = MFVRSAVE
241 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
242 // MTVRSAVE UpdatedVRSAVE
243 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000244 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
245 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000246 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000247 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000248
Chris Lattner1678a6c2006-03-16 18:25:23 +0000249 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000250 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000251 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000252 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000253
Chris Lattner1678a6c2006-03-16 18:25:23 +0000254 // Skip over all terminator instructions, which are part of the return
255 // sequence.
256 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000257 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000258 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000259
Chris Lattner1678a6c2006-03-16 18:25:23 +0000260 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000261 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000262 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000263 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000264}
Chris Lattner8ae95252005-09-03 01:17:22 +0000265
Chris Lattner1678a6c2006-03-16 18:25:23 +0000266
Chris Lattner45640392005-08-19 22:38:53 +0000267/// getGlobalBaseReg - Output the instructions required to put the
268/// base address to use for accessing globals into a register.
269///
Evan Cheng61413a32006-08-26 05:34:46 +0000270SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000271 if (!GlobalBaseReg) {
Eric Christopherd9134482014-08-04 21:25:23 +0000272 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000273 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000274 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000275 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000276 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000277
Eric Christopher1b8e7632014-05-22 01:07:24 +0000278 if (PPCLowering->getPointerTy() == MVT::i32) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000279 if (PPCSubTarget->isTargetELF())
280 GlobalBaseReg = PPC::R30;
281 else
282 GlobalBaseReg =
283 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000284 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000285 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000286 if (PPCSubTarget->isTargetELF()) {
287 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
288 BuildMI(FirstMBB, MBBI, dl,
289 TII.get(PPC::GetGBRO), TempReg).addReg(GlobalBaseReg);
290 BuildMI(FirstMBB, MBBI, dl,
291 TII.get(PPC::UpdateGBR)).addReg(GlobalBaseReg).addReg(TempReg);
292 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
293 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000294 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000295 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000296 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000297 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000298 }
Chris Lattner45640392005-08-19 22:38:53 +0000299 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000300 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000301 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000302}
303
304/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
305/// or 64-bit immediate, and if the value can be accurately represented as a
306/// sign extension from a 16-bit value. If so, this returns true and the
307/// immediate.
308static bool isIntS16Immediate(SDNode *N, short &Imm) {
309 if (N->getOpcode() != ISD::Constant)
310 return false;
311
Dan Gohmaneffb8942008-09-12 16:56:44 +0000312 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000313 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000314 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000315 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000316 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000317}
318
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000319static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000320 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000321}
322
323
Chris Lattner97b3da12006-06-27 00:04:13 +0000324/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
325/// operand. If so Imm will receive the 32-bit value.
326static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000327 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000328 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000329 return true;
330 }
331 return false;
332}
333
Chris Lattner97b3da12006-06-27 00:04:13 +0000334/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
335/// operand. If so Imm will receive the 64-bit value.
336static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000337 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000338 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000339 return true;
340 }
341 return false;
342}
343
344// isInt32Immediate - This method tests to see if a constant operand.
345// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000346static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000347 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000348}
349
350
351// isOpcWithIntImmediate - This method tests to see if the node is a specific
352// opcode and that it has a immediate integer right operand.
353// If so Imm will receive the 32 bit value.
354static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000355 return N->getOpcode() == Opc
356 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000357}
358
Nate Begemand31efd12006-09-22 05:01:56 +0000359bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000360 if (!Val)
361 return false;
362
Nate Begemanb3821a32005-08-18 07:30:46 +0000363 if (isShiftedMask_32(Val)) {
364 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000365 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000366 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000367 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000368 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000369 } else {
370 Val = ~Val; // invert mask
371 if (isShiftedMask_32(Val)) {
372 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000373 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000374 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000375 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000376 return true;
377 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000378 }
379 // no run present
380 return false;
381}
382
Andrew Trickc416ba62010-12-24 04:28:06 +0000383bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
384 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000385 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000386 // Don't even go down this path for i64, since different logic will be
387 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000388 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000389 return false;
390
Nate Begemanb3821a32005-08-18 07:30:46 +0000391 unsigned Shift = 32;
392 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
393 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000394 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000395 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000396 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000397
Nate Begemanb3821a32005-08-18 07:30:46 +0000398 if (Opcode == ISD::SHL) {
399 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000400 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000401 // determine which bits are made indeterminant by shift
402 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000403 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000404 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000405 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000406 // determine which bits are made indeterminant by shift
407 Indeterminant = ~(0xFFFFFFFFu >> Shift);
408 // adjust for the left rotate
409 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000410 } else if (Opcode == ISD::ROTL) {
411 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000412 } else {
413 return false;
414 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000415
Nate Begemanb3821a32005-08-18 07:30:46 +0000416 // if the mask doesn't intersect any Indeterminant bits
417 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000418 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000419 // make sure the mask is still a mask (wrap arounds may not be)
420 return isRunOfOnes(Mask, MB, ME);
421 }
422 return false;
423}
424
Nate Begeman93c4bc62005-08-19 00:38:14 +0000425/// SelectBitfieldInsert - turn an or of two masked values into
426/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000427SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000428 SDValue Op0 = N->getOperand(0);
429 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000430 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000431
Dan Gohmanf19609a2008-02-27 01:23:58 +0000432 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000433 CurDAG->computeKnownBits(Op0, LKZ, LKO);
434 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000435
Dan Gohmanf19609a2008-02-27 01:23:58 +0000436 unsigned TargetMask = LKZ.getZExtValue();
437 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000438
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000439 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
440 unsigned Op0Opc = Op0.getOpcode();
441 unsigned Op1Opc = Op1.getOpcode();
442 unsigned Value, SH = 0;
443 TargetMask = ~TargetMask;
444 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000445
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000446 // If the LHS has a foldable shift and the RHS does not, then swap it to the
447 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000448 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
449 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
450 Op0.getOperand(0).getOpcode() == ISD::SRL) {
451 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
452 Op1.getOperand(0).getOpcode() != ISD::SRL) {
453 std::swap(Op0, Op1);
454 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000455 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000456 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000457 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000458 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
459 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
460 Op1.getOperand(0).getOpcode() != ISD::SRL) {
461 std::swap(Op0, Op1);
462 std::swap(Op0Opc, Op1Opc);
463 std::swap(TargetMask, InsertMask);
464 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000465 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000466
Nate Begeman1333cea2006-05-07 00:23:38 +0000467 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000468 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000469 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000470
471 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000472 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000473 Op1 = Op1.getOperand(0);
474 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
475 }
476 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000477 // The AND mask might not be a constant, and we need to make sure that
478 // if we're going to fold the masking with the insert, all bits not
479 // know to be zero in the mask are known to be one.
480 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000481 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000482 bool CanFoldMask = InsertMask == MKO.getZExtValue();
483
Nate Begeman1333cea2006-05-07 00:23:38 +0000484 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000485 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000486 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000487 // Note that Value must be in range here (less than 32) because
488 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000489 Op1 = Op1.getOperand(0).getOperand(0);
490 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000491 }
492 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000493
Chris Lattnera2963392006-05-12 16:29:37 +0000494 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000495 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000496 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000497 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000498 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000499 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000500 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000501}
502
Chris Lattner2a1823d2005-08-21 18:50:37 +0000503/// SelectCC - Select a comparison of the specified values with the specified
504/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000505SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000506 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000507 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000508 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000509
Owen Anderson9f944592009-08-11 20:47:22 +0000510 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000511 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000512 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
513 if (isInt32Immediate(RHS, Imm)) {
514 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000515 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000516 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
517 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000518 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000519 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000520 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
521 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000522
Chris Lattneraa3926b2006-09-20 04:25:47 +0000523 // For non-equality comparisons, the default code would materialize the
524 // constant, then compare against it, like this:
525 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000526 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000527 // cmpw cr0, r3, r2
528 // Since we are just comparing for equality, we can emit this instead:
529 // xoris r0,r3,0x1234
530 // cmplwi cr0,r0,0x5678
531 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000532 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
533 getI32Imm(Imm >> 16)), 0);
534 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
535 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000536 }
537 Opc = PPC::CMPLW;
538 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000539 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000540 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
541 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000542 Opc = PPC::CMPLW;
543 } else {
544 short SImm;
545 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000546 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
547 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000548 0);
549 Opc = PPC::CMPW;
550 }
Owen Anderson9f944592009-08-11 20:47:22 +0000551 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000552 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000553 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000554 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000555 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000556 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000557 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
558 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000559 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000560 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000561 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
562 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000563
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000564 // For non-equality comparisons, the default code would materialize the
565 // constant, then compare against it, like this:
566 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000567 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000568 // cmpd cr0, r3, r2
569 // Since we are just comparing for equality, we can emit this instead:
570 // xoris r0,r3,0x1234
571 // cmpldi cr0,r0,0x5678
572 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000573 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000574 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
575 getI64Imm(Imm >> 16)), 0);
576 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
577 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000578 }
579 }
580 Opc = PPC::CMPLD;
581 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000582 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000583 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
584 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000585 Opc = PPC::CMPLD;
586 } else {
587 short SImm;
588 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000589 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
590 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000591 0);
592 Opc = PPC::CMPD;
593 }
Owen Anderson9f944592009-08-11 20:47:22 +0000594 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000595 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000596 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000597 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +0000598 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000599 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000600 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000601}
602
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000603static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000604 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000605 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000606 case ISD::SETONE:
607 case ISD::SETOLE:
608 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000609 llvm_unreachable("Should be lowered by legalize!");
610 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000611 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000612 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000613 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000614 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000615 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000616 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000617 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000618 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000619 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000620 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000621 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000622 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000623 case ISD::SETO: return PPC::PRED_NU;
624 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000625 // These two are invalid for floating point. Assume we have int.
626 case ISD::SETULT: return PPC::PRED_LT;
627 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000628 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000629}
630
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000631/// getCRIdxForSetCC - Return the index of the condition register field
632/// associated with the SetCC condition, and whether or not the field is
633/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000634static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000635 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000636 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000637 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000638 case ISD::SETOLT:
639 case ISD::SETLT: return 0; // Bit #0 = SETOLT
640 case ISD::SETOGT:
641 case ISD::SETGT: return 1; // Bit #1 = SETOGT
642 case ISD::SETOEQ:
643 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
644 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000645 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000646 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000647 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000648 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000649 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000650 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
651 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000652 case ISD::SETUEQ:
653 case ISD::SETOGE:
654 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000655 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000656 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000657 // These are invalid for floating point. Assume integer.
658 case ISD::SETULT: return 0;
659 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000660 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000661}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000662
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000663// getVCmpInst: return the vector compare instruction for the specified
664// vector type and condition code. Since this is for altivec specific code,
665// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000666static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
667 bool HasVSX, bool &Swap, bool &Negate) {
668 Swap = false;
669 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000670
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000671 if (VecVT.isFloatingPoint()) {
672 /* Handle some cases by swapping input operands. */
673 switch (CC) {
674 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
675 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
676 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
677 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
678 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
679 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
680 default: break;
681 }
682 /* Handle some cases by negating the result. */
683 switch (CC) {
684 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
685 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
686 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
687 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
688 default: break;
689 }
690 /* We have instructions implementing the remaining cases. */
691 switch (CC) {
692 case ISD::SETEQ:
693 case ISD::SETOEQ:
694 if (VecVT == MVT::v4f32)
695 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
696 else if (VecVT == MVT::v2f64)
697 return PPC::XVCMPEQDP;
698 break;
699 case ISD::SETGT:
700 case ISD::SETOGT:
701 if (VecVT == MVT::v4f32)
702 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
703 else if (VecVT == MVT::v2f64)
704 return PPC::XVCMPGTDP;
705 break;
706 case ISD::SETGE:
707 case ISD::SETOGE:
708 if (VecVT == MVT::v4f32)
709 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
710 else if (VecVT == MVT::v2f64)
711 return PPC::XVCMPGEDP;
712 break;
713 default:
714 break;
715 }
716 llvm_unreachable("Invalid floating-point vector compare condition");
717 } else {
718 /* Handle some cases by swapping input operands. */
719 switch (CC) {
720 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
721 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
722 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
723 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
724 default: break;
725 }
726 /* Handle some cases by negating the result. */
727 switch (CC) {
728 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
729 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
730 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
731 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
732 default: break;
733 }
734 /* We have instructions implementing the remaining cases. */
735 switch (CC) {
736 case ISD::SETEQ:
737 case ISD::SETUEQ:
738 if (VecVT == MVT::v16i8)
739 return PPC::VCMPEQUB;
740 else if (VecVT == MVT::v8i16)
741 return PPC::VCMPEQUH;
742 else if (VecVT == MVT::v4i32)
743 return PPC::VCMPEQUW;
744 break;
745 case ISD::SETGT:
746 if (VecVT == MVT::v16i8)
747 return PPC::VCMPGTSB;
748 else if (VecVT == MVT::v8i16)
749 return PPC::VCMPGTSH;
750 else if (VecVT == MVT::v4i32)
751 return PPC::VCMPGTSW;
752 break;
753 case ISD::SETUGT:
754 if (VecVT == MVT::v16i8)
755 return PPC::VCMPGTUB;
756 else if (VecVT == MVT::v8i16)
757 return PPC::VCMPGTUH;
758 else if (VecVT == MVT::v4i32)
759 return PPC::VCMPGTUW;
760 break;
761 default:
762 break;
763 }
764 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000765 }
766}
767
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000768SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000769 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000770 unsigned Imm;
771 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000772 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
773 bool isPPC64 = (PtrVT == MVT::i64);
774
Eric Christopher1b8e7632014-05-22 01:07:24 +0000775 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +0000776 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000777 // We can codegen setcc op, imm very efficiently compared to a brcond.
778 // Check for those cases here.
779 // setcc op, 0
780 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000781 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000782 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000783 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000784 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000785 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000786 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000787 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000788 }
Chris Lattnere2969492005-10-21 21:17:10 +0000789 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000790 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000791 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000792 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000793 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000794 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000795 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000796 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000797 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000798 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000799 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000800 }
Chris Lattnere2969492005-10-21 21:17:10 +0000801 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000802 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000803 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
804 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000805 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000806 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000807 }
808 }
Chris Lattner491b8292005-10-06 19:03:35 +0000809 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000810 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000811 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000812 default: break;
813 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000814 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000815 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000816 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000817 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
818 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000819 MVT::i32,
820 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000821 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000822 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000823 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000824 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000825 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000826 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000827 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000828 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000829 }
Chris Lattnere2969492005-10-21 21:17:10 +0000830 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000831 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
832 getI32Imm(1)), 0);
833 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
834 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000835 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000836 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000837 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000838 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000839 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000840 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000841 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000842 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000843 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000844 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000845 }
Chris Lattner491b8292005-10-06 19:03:35 +0000846 }
847 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000848
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000849 SDValue LHS = N->getOperand(0);
850 SDValue RHS = N->getOperand(1);
851
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000852 // Altivec Vector compare instructions do not set any CR register by default and
853 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000854 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000855 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000856 bool Swap, Negate;
857 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
858 PPCSubTarget->hasVSX(), Swap, Negate);
859 if (Swap)
860 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000861
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000862 if (Negate) {
863 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
864 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
865 PPC::VNOR,
866 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000867 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000868
869 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000870 }
871
Eric Christopher1b8e7632014-05-22 01:07:24 +0000872 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +0000873 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +0000874
Chris Lattner491b8292005-10-06 19:03:35 +0000875 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000876 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000877 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000878 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000879
Chris Lattner491b8292005-10-06 19:03:35 +0000880 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000881 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000882
Craig Topper062a2ba2014-04-25 05:30:21 +0000883 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000884 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000885 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000886
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000887 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
888 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000889
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000890 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000891 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000892 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +0000893 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +0000894
895 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000896 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000897 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000898 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000899}
Chris Lattner502a3692005-10-06 18:56:10 +0000900
Chris Lattner318622f2005-10-06 19:07:45 +0000901
Chris Lattner43ff01e2005-08-17 19:33:03 +0000902// Select - Convert the specified operand from a target-independent to a
903// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000904SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000905 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000906 if (N->isMachineOpcode()) {
907 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000908 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000909 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000910
Hal Finkel51b3fd12014-09-02 06:23:54 +0000911 // In case any misguided DAG-level optimizations form an ADD with a
912 // TargetConstant operand, crash here instead of miscompiling (by selecting
913 // an r+r add instead of some kind of r+i add).
914 if (N->getOpcode() == ISD::ADD &&
915 N->getOperand(1).getOpcode() == ISD::TargetConstant)
916 llvm_unreachable("Invalid ADD with TargetConstant operand");
917
Chris Lattner43ff01e2005-08-17 19:33:03 +0000918 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000919 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000920
Jim Laskey095e6f32006-12-12 13:23:43 +0000921 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000922 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000923 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000924 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000925 // Assume no remaining bits.
926 unsigned Remainder = 0;
927 // Assume no shift required.
928 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000929
Jim Laskey095e6f32006-12-12 13:23:43 +0000930 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000931 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000932 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000933 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000934
Jim Laskey095e6f32006-12-12 13:23:43 +0000935 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000936 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000937 // Go with the shifted value.
938 Imm = ImmSh;
939 } else {
940 // Still stuck with a 64 bit value.
941 Remainder = Imm;
942 Shift = 32;
943 Imm >>= 32;
944 }
945 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000946
Jim Laskey095e6f32006-12-12 13:23:43 +0000947 // Intermediate operand.
948 SDNode *Result;
949
950 // Handle first 32 bits.
951 unsigned Lo = Imm & 0xFFFF;
952 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000953
Jim Laskey095e6f32006-12-12 13:23:43 +0000954 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000955 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000956 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000957 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000958 } else if (Lo) {
959 // Handle the Hi bits.
960 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000961 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000962 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000963 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
964 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000965 } else {
966 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000967 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000968 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000969
Jim Laskey095e6f32006-12-12 13:23:43 +0000970 // If no shift, we're done.
971 if (!Shift) return Result;
972
973 // Shift for next step if the upper 32-bits were not zero.
974 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000975 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
976 SDValue(Result, 0),
977 getI32Imm(Shift),
978 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000979 }
980
981 // Add in the last bits as required.
982 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000983 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
984 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000985 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000986 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000987 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
988 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000989 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000990
Jim Laskey095e6f32006-12-12 13:23:43 +0000991 return Result;
992 }
993 break;
994 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000995
Hal Finkel940ab932014-02-28 00:27:01 +0000996 case ISD::SETCC: {
997 SDNode *SN = SelectSETCC(N);
998 if (SN)
999 return SN;
1000 break;
1001 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001002 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001003 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00001004
Chris Lattnere4c338d2005-08-25 00:45:43 +00001005 case ISD::FrameIndex: {
1006 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001007 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1008 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001009 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001010 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001011 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001012 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +00001013 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +00001014 }
Chris Lattner6961fc72006-03-26 10:06:40 +00001015
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001016 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001017 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001018 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1019 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00001020 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001021
Chris Lattner57693112005-09-28 22:50:24 +00001022 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001023 // FIXME: since this depends on the setting of the carry flag from the srawi
1024 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001025 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001026 // srl/add/sra pattern the dag combiner will generate for this as
1027 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001028 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001029 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001030 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001031 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001032 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001033 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001034 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001035 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001036 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001037 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001038 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001039 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001040 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001041 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001042 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1043 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001044 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001045 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001046 }
1047 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001048
Chris Lattner1de57062005-09-29 23:33:31 +00001049 // Other cases are autogenerated.
1050 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001051 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001052
Chris Lattnerce645542006-11-10 02:08:47 +00001053 case ISD::LOAD: {
1054 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001055 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001056 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001057
Chris Lattnerce645542006-11-10 02:08:47 +00001058 // Normal loads are handled by code generated from the .td file.
1059 if (LD->getAddressingMode() != ISD::PRE_INC)
1060 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001061
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001062 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001063 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001064 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001065
Chris Lattner474b5b72006-11-15 19:55:13 +00001066 unsigned Opcode;
1067 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001068 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001069 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001070 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1071 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001072 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001073 case MVT::f64: Opcode = PPC::LFDU; break;
1074 case MVT::f32: Opcode = PPC::LFSU; break;
1075 case MVT::i32: Opcode = PPC::LWZU; break;
1076 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1077 case MVT::i1:
1078 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001079 }
1080 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001081 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1082 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1083 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001084 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001085 case MVT::i64: Opcode = PPC::LDU; break;
1086 case MVT::i32: Opcode = PPC::LWZU8; break;
1087 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1088 case MVT::i1:
1089 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001090 }
1091 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001092
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001093 SDValue Chain = LD->getChain();
1094 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001095 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001096 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001097 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001098 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001099 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001100 unsigned Opcode;
1101 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1102 if (LD->getValueType(0) != MVT::i64) {
1103 // Handle PPC32 integer and normal FP loads.
1104 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1105 switch (LoadedVT.getSimpleVT().SimpleTy) {
1106 default: llvm_unreachable("Invalid PPC load type!");
1107 case MVT::f64: Opcode = PPC::LFDUX; break;
1108 case MVT::f32: Opcode = PPC::LFSUX; break;
1109 case MVT::i32: Opcode = PPC::LWZUX; break;
1110 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1111 case MVT::i1:
1112 case MVT::i8: Opcode = PPC::LBZUX; break;
1113 }
1114 } else {
1115 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1116 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1117 "Invalid sext update load");
1118 switch (LoadedVT.getSimpleVT().SimpleTy) {
1119 default: llvm_unreachable("Invalid PPC load type!");
1120 case MVT::i64: Opcode = PPC::LDUX; break;
1121 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1122 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1123 case MVT::i1:
1124 case MVT::i8: Opcode = PPC::LBZUX8; break;
1125 }
1126 }
1127
1128 SDValue Chain = LD->getChain();
1129 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001130 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001131 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001132 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001133 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001134 }
1135 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001136
Nate Begemanb3821a32005-08-18 07:30:46 +00001137 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001138 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001139 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001140
Nate Begemanb3821a32005-08-18 07:30:46 +00001141 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1142 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001143 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001144 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001145 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001146 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001147 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00001148 }
Nate Begemand31efd12006-09-22 05:01:56 +00001149 // If this is just a masked value where the input is not handled above, and
1150 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1151 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001152 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001153 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001154 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001155 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001156 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00001157 }
Hal Finkele39526a2012-08-28 02:10:15 +00001158 // If this is a 64-bit zero-extension mask, emit rldicl.
1159 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1160 isMask_64(Imm64)) {
1161 SDValue Val = N->getOperand(0);
1162 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001163 SH = 0;
1164
1165 // If the operand is a logical right shift, we can fold it into this
1166 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1167 // for n <= mb. The right shift is really a left rotate followed by a
1168 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1169 // by the shift.
1170 if (Val.getOpcode() == ISD::SRL &&
1171 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1172 assert(Imm < 64 && "Illegal shift amount");
1173 Val = Val.getOperand(0);
1174 SH = 64 - Imm;
1175 }
1176
1177 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00001178 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00001179 }
Nate Begemand31efd12006-09-22 05:01:56 +00001180 // AND X, 0 -> 0, not "rlwinm 32".
1181 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001182 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00001183 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00001184 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001185 // ISD::OR doesn't get all the bitfield insertion fun.
1186 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001187 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001188 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001189 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001190 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001191 Imm = ~(Imm^Imm2);
1192 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001193 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001194 N->getOperand(0).getOperand(1),
1195 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001196 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001197 }
1198 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001199
Chris Lattner1de57062005-09-29 23:33:31 +00001200 // Other cases are autogenerated.
1201 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001202 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001203 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001204 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001205 if (SDNode *I = SelectBitfieldInsert(N))
1206 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001207
Chris Lattner1de57062005-09-29 23:33:31 +00001208 // Other cases are autogenerated.
1209 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001210 case ISD::SHL: {
1211 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001212 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001213 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001214 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001215 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001216 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001217 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001218
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001219 // Other cases are autogenerated.
1220 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001221 }
1222 case ISD::SRL: {
1223 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001224 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001225 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001226 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001227 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001228 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001229 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001230
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001231 // Other cases are autogenerated.
1232 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001233 }
Hal Finkel940ab932014-02-28 00:27:01 +00001234 // FIXME: Remove this once the ANDI glue bug is fixed:
1235 case PPCISD::ANDIo_1_EQ_BIT:
1236 case PPCISD::ANDIo_1_GT_BIT: {
1237 if (!ANDIGlueBug)
1238 break;
1239
1240 EVT InVT = N->getOperand(0).getValueType();
1241 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1242 "Invalid input type for ANDIo_1_EQ_BIT");
1243
1244 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1245 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1246 N->getOperand(0),
1247 CurDAG->getTargetConstant(1, InVT)), 0);
1248 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1249 SDValue SRIdxVal =
1250 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1251 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1252
1253 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1254 CR0Reg, SRIdxVal,
1255 SDValue(AndI.getNode(), 1) /* glue */);
1256 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001257 case ISD::SELECT_CC: {
1258 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001259 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1260 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001261
Hal Finkel940ab932014-02-28 00:27:01 +00001262 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001263 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00001264 N->getOperand(0).getValueType() == MVT::i1)
1265 break;
1266
Chris Lattner97b3da12006-06-27 00:04:13 +00001267 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001268 if (!isPPC64)
1269 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1270 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1271 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1272 if (N1C->isNullValue() && N3C->isNullValue() &&
1273 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1274 // FIXME: Implement this optzn for PPC64.
1275 N->getValueType(0) == MVT::i32) {
1276 SDNode *Tmp =
1277 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1278 N->getOperand(0), getI32Imm(~0U));
1279 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1280 SDValue(Tmp, 0), N->getOperand(0),
1281 SDValue(Tmp, 1));
1282 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001283
Dale Johannesenab8e4422009-02-06 19:16:40 +00001284 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001285
1286 if (N->getValueType(0) == MVT::i1) {
1287 // An i1 select is: (c & t) | (!c & f).
1288 bool Inv;
1289 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1290
1291 unsigned SRI;
1292 switch (Idx) {
1293 default: llvm_unreachable("Invalid CC index");
1294 case 0: SRI = PPC::sub_lt; break;
1295 case 1: SRI = PPC::sub_gt; break;
1296 case 2: SRI = PPC::sub_eq; break;
1297 case 3: SRI = PPC::sub_un; break;
1298 }
1299
1300 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1301
1302 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1303 CCBit, CCBit), 0);
1304 SDValue C = Inv ? NotCCBit : CCBit,
1305 NotC = Inv ? CCBit : NotCCBit;
1306
1307 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1308 C, N->getOperand(2)), 0);
1309 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1310 NotC, N->getOperand(3)), 0);
1311
1312 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1313 }
1314
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001315 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001316
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001317 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001318 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001319 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001320 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001321 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001322 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001323 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001324 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001325 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001326 else
1327 SelectCCOp = PPC::SELECT_CC_VRRC;
1328
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001329 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001330 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00001331 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001332 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001333 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001334 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00001335 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001336 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00001337 }
1338
1339 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001340 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001341 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001342 N->getValueType(0) == MVT::v2i64)) {
1343 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1344
1345 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1346 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1347 unsigned DM[2];
1348
1349 for (int i = 0; i < 2; ++i)
1350 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1351 DM[i] = 0;
1352 else
1353 DM[i] = 1;
1354
Hal Finkel2583b062014-03-28 20:24:55 +00001355 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001356
1357 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1358 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1359 isa<LoadSDNode>(Op1.getOperand(0))) {
1360 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1361 SDValue Base, Offset;
1362
1363 if (LD->isUnindexed() &&
1364 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1365 SDValue Chain = LD->getChain();
1366 SDValue Ops[] = { Base, Offset, Chain };
1367 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00001368 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001369 }
1370 }
1371
1372 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00001373 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001374 }
1375
1376 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001377 case PPCISD::BDNZ:
1378 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00001379 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00001380 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1381 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1382 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1383 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00001384 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00001385 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001386 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001387 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001388 // Op #1 is the PPC::PRED_* number.
1389 // Op #2 is the CR#
1390 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001391 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001392 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001393 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001394 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001395 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001396 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00001397 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001398 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001399 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001400 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001401 unsigned PCC = getPredicateForSetCC(CC);
1402
1403 if (N->getOperand(2).getValueType() == MVT::i1) {
1404 unsigned Opc;
1405 bool Swap;
1406 switch (PCC) {
1407 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1408 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1409 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1410 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1411 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1412 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1413 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1414 }
1415
1416 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1417 N->getOperand(Swap ? 3 : 2),
1418 N->getOperand(Swap ? 2 : 3)), 0);
1419 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1420 BitComp, N->getOperand(4), N->getOperand(0));
1421 }
1422
Dale Johannesenab8e4422009-02-06 19:16:40 +00001423 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001424 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001425 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001426 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001427 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001428 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001429 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001430 SDValue Chain = N->getOperand(0);
1431 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001432 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001433 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001434 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001435 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001436 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001437 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001438 case PPCISD::TOC_ENTRY: {
Hal Finkel3ee2af72014-07-18 23:29:49 +00001439 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
1440 SDValue GA = N->getOperand(0);
1441 return CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
1442 N->getOperand(1));
Justin Hibbits3476db42014-08-28 04:40:55 +00001443 }
Hal Finkel3ee2af72014-07-18 23:29:49 +00001444 assert (PPCSubTarget->isPPC64() &&
1445 "Only supported for 64-bit ABI and 32-bit SVR4");
Bill Schmidt34627e32012-11-27 17:35:46 +00001446
Bill Schmidt27917782013-02-21 17:12:27 +00001447 // For medium and large code model, we generate two instructions as
1448 // described below. Otherwise we allow SelectCodeCommon to handle this,
1449 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1450 CodeModel::Model CModel = TM.getCodeModel();
1451 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001452 break;
1453
Bill Schmidt5d82f092014-06-16 21:36:02 +00001454 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
1455 // If it is an externally defined symbol, a symbol with common linkage,
1456 // a non-local function address, or a jump table address, or if we are
1457 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001458 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1459 // Otherwise we generate:
1460 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1461 SDValue GA = N->getOperand(0);
1462 SDValue TOCbase = N->getOperand(1);
1463 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1464 TOCbase, GA);
1465
Bill Schmidt27917782013-02-21 17:12:27 +00001466 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001467 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1468 SDValue(Tmp, 0));
1469
1470 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1471 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00001472 if ((GValue->getType()->getElementType()->isFunctionTy() &&
1473 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00001474 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
1475 GValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001476 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1477 SDValue(Tmp, 0));
1478 }
1479
1480 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1481 SDValue(Tmp, 0), GA);
1482 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00001483 case PPCISD::PPC32_PICGOT: {
1484 // Generate a PIC-safe GOT reference.
1485 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
1486 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
1487 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
1488 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001489 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001490 // This expands into one of three sequences, depending on whether
1491 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001492 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1493 isa<ConstantSDNode>(N->getOperand(1)) &&
1494 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001495
1496 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001497 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001498 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001499 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001500
Bill Schmidt51e79512013-02-20 15:50:31 +00001501 if (EltSize == 1) {
1502 Opc1 = PPC::VSPLTISB;
1503 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001504 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001505 VT = MVT::v16i8;
1506 } else if (EltSize == 2) {
1507 Opc1 = PPC::VSPLTISH;
1508 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001509 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001510 VT = MVT::v8i16;
1511 } else {
1512 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1513 Opc1 = PPC::VSPLTISW;
1514 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001515 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001516 VT = MVT::v4i32;
1517 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001518
1519 if ((Elt & 1) == 0) {
1520 // Elt is even, in the range [-32,-18] + [16,30].
1521 //
1522 // Convert: VADD_SPLAT elt, size
1523 // Into: tmp = VSPLTIS[BHW] elt
1524 // VADDU[BHW]M tmp, tmp
1525 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1526 SDValue EltVal = getI32Imm(Elt >> 1);
1527 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1528 SDValue TmpVal = SDValue(Tmp, 0);
1529 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1530
1531 } else if (Elt > 0) {
1532 // Elt is odd and positive, in the range [17,31].
1533 //
1534 // Convert: VADD_SPLAT elt, size
1535 // Into: tmp1 = VSPLTIS[BHW] elt-16
1536 // tmp2 = VSPLTIS[BHW] -16
1537 // VSUBU[BHW]M tmp1, tmp2
1538 SDValue EltVal = getI32Imm(Elt - 16);
1539 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1540 EltVal = getI32Imm(-16);
1541 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1542 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1543 SDValue(Tmp2, 0));
1544
1545 } else {
1546 // Elt is odd and negative, in the range [-31,-17].
1547 //
1548 // Convert: VADD_SPLAT elt, size
1549 // Into: tmp1 = VSPLTIS[BHW] elt+16
1550 // tmp2 = VSPLTIS[BHW] -16
1551 // VADDU[BHW]M tmp1, tmp2
1552 SDValue EltVal = getI32Imm(Elt + 16);
1553 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1554 EltVal = getI32Imm(-16);
1555 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1556 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1557 SDValue(Tmp2, 0));
1558 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001559 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001560 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001561
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001562 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001563}
1564
Hal Finkel860fa902014-01-02 22:09:39 +00001565/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001566/// on the DAG representation.
1567void PPCDAGToDAGISel::PostprocessISelDAG() {
1568
1569 // Skip peepholes at -O0.
1570 if (TM.getOptLevel() == CodeGenOpt::None)
1571 return;
1572
Hal Finkel940ab932014-02-28 00:27:01 +00001573 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00001574 PeepholeCROps();
Hal Finkel940ab932014-02-28 00:27:01 +00001575}
1576
Hal Finkelb9989152014-02-28 06:11:16 +00001577// Check if all users of this node will become isel where the second operand
1578// is the constant zero. If this is so, and if we can negate the condition,
1579// then we can flip the true and false operands. This will allow the zero to
1580// be folded with the isel so that we don't need to materialize a register
1581// containing zero.
1582bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1583 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001584 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00001585 return false;
1586
1587 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1588 UI != UE; ++UI) {
1589 SDNode *User = *UI;
1590 if (!User->isMachineOpcode())
1591 return false;
1592 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1593 User->getMachineOpcode() != PPC::SELECT_I8)
1594 return false;
1595
1596 SDNode *Op2 = User->getOperand(2).getNode();
1597 if (!Op2->isMachineOpcode())
1598 return false;
1599
1600 if (Op2->getMachineOpcode() != PPC::LI &&
1601 Op2->getMachineOpcode() != PPC::LI8)
1602 return false;
1603
1604 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1605 if (!C)
1606 return false;
1607
1608 if (!C->isNullValue())
1609 return false;
1610 }
1611
1612 return true;
1613}
1614
1615void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1616 SmallVector<SDNode *, 4> ToReplace;
1617 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1618 UI != UE; ++UI) {
1619 SDNode *User = *UI;
1620 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1621 User->getMachineOpcode() == PPC::SELECT_I8) &&
1622 "Must have all select users");
1623 ToReplace.push_back(User);
1624 }
1625
1626 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1627 UE = ToReplace.end(); UI != UE; ++UI) {
1628 SDNode *User = *UI;
1629 SDNode *ResNode =
1630 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1631 User->getValueType(0), User->getOperand(0),
1632 User->getOperand(2),
1633 User->getOperand(1));
1634
1635 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1636 DEBUG(User->dump(CurDAG));
1637 DEBUG(dbgs() << "\nNew: ");
1638 DEBUG(ResNode->dump(CurDAG));
1639 DEBUG(dbgs() << "\n");
1640
1641 ReplaceUses(User, ResNode);
1642 }
1643}
1644
Eric Christopher02e18042014-05-14 00:31:15 +00001645void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00001646 bool IsModified;
1647 do {
1648 IsModified = false;
1649 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1650 E = CurDAG->allnodes_end(); I != E; ++I) {
1651 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1652 if (!MachineNode || MachineNode->use_empty())
1653 continue;
1654 SDNode *ResNode = MachineNode;
1655
1656 bool Op1Set = false, Op1Unset = false,
1657 Op1Not = false,
1658 Op2Set = false, Op2Unset = false,
1659 Op2Not = false;
1660
1661 unsigned Opcode = MachineNode->getMachineOpcode();
1662 switch (Opcode) {
1663 default: break;
1664 case PPC::CRAND:
1665 case PPC::CRNAND:
1666 case PPC::CROR:
1667 case PPC::CRXOR:
1668 case PPC::CRNOR:
1669 case PPC::CREQV:
1670 case PPC::CRANDC:
1671 case PPC::CRORC: {
1672 SDValue Op = MachineNode->getOperand(1);
1673 if (Op.isMachineOpcode()) {
1674 if (Op.getMachineOpcode() == PPC::CRSET)
1675 Op2Set = true;
1676 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1677 Op2Unset = true;
1678 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1679 Op.getOperand(0) == Op.getOperand(1))
1680 Op2Not = true;
1681 }
1682 } // fallthrough
1683 case PPC::BC:
1684 case PPC::BCn:
1685 case PPC::SELECT_I4:
1686 case PPC::SELECT_I8:
1687 case PPC::SELECT_F4:
1688 case PPC::SELECT_F8:
1689 case PPC::SELECT_VRRC: {
1690 SDValue Op = MachineNode->getOperand(0);
1691 if (Op.isMachineOpcode()) {
1692 if (Op.getMachineOpcode() == PPC::CRSET)
1693 Op1Set = true;
1694 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1695 Op1Unset = true;
1696 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1697 Op.getOperand(0) == Op.getOperand(1))
1698 Op1Not = true;
1699 }
1700 }
1701 break;
1702 }
1703
Hal Finkelb9989152014-02-28 06:11:16 +00001704 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001705 switch (Opcode) {
1706 default: break;
1707 case PPC::CRAND:
1708 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1709 // x & x = x
1710 ResNode = MachineNode->getOperand(0).getNode();
1711 else if (Op1Set)
1712 // 1 & y = y
1713 ResNode = MachineNode->getOperand(1).getNode();
1714 else if (Op2Set)
1715 // x & 1 = x
1716 ResNode = MachineNode->getOperand(0).getNode();
1717 else if (Op1Unset || Op2Unset)
1718 // x & 0 = 0 & y = 0
1719 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1720 MVT::i1);
1721 else if (Op1Not)
1722 // ~x & y = andc(y, x)
1723 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1724 MVT::i1, MachineNode->getOperand(1),
1725 MachineNode->getOperand(0).
1726 getOperand(0));
1727 else if (Op2Not)
1728 // x & ~y = andc(x, y)
1729 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1730 MVT::i1, MachineNode->getOperand(0),
1731 MachineNode->getOperand(1).
1732 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001733 else if (AllUsersSelectZero(MachineNode))
1734 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1735 MVT::i1, MachineNode->getOperand(0),
1736 MachineNode->getOperand(1)),
1737 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001738 break;
1739 case PPC::CRNAND:
1740 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1741 // nand(x, x) -> nor(x, x)
1742 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1743 MVT::i1, MachineNode->getOperand(0),
1744 MachineNode->getOperand(0));
1745 else if (Op1Set)
1746 // nand(1, y) -> nor(y, y)
1747 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1748 MVT::i1, MachineNode->getOperand(1),
1749 MachineNode->getOperand(1));
1750 else if (Op2Set)
1751 // nand(x, 1) -> nor(x, x)
1752 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1753 MVT::i1, MachineNode->getOperand(0),
1754 MachineNode->getOperand(0));
1755 else if (Op1Unset || Op2Unset)
1756 // nand(x, 0) = nand(0, y) = 1
1757 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1758 MVT::i1);
1759 else if (Op1Not)
1760 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1761 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1762 MVT::i1, MachineNode->getOperand(0).
1763 getOperand(0),
1764 MachineNode->getOperand(1));
1765 else if (Op2Not)
1766 // nand(x, ~y) = ~x | y = orc(y, x)
1767 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1768 MVT::i1, MachineNode->getOperand(1).
1769 getOperand(0),
1770 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001771 else if (AllUsersSelectZero(MachineNode))
1772 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1773 MVT::i1, MachineNode->getOperand(0),
1774 MachineNode->getOperand(1)),
1775 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001776 break;
1777 case PPC::CROR:
1778 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1779 // x | x = x
1780 ResNode = MachineNode->getOperand(0).getNode();
1781 else if (Op1Set || Op2Set)
1782 // x | 1 = 1 | y = 1
1783 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1784 MVT::i1);
1785 else if (Op1Unset)
1786 // 0 | y = y
1787 ResNode = MachineNode->getOperand(1).getNode();
1788 else if (Op2Unset)
1789 // x | 0 = x
1790 ResNode = MachineNode->getOperand(0).getNode();
1791 else if (Op1Not)
1792 // ~x | y = orc(y, x)
1793 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1794 MVT::i1, MachineNode->getOperand(1),
1795 MachineNode->getOperand(0).
1796 getOperand(0));
1797 else if (Op2Not)
1798 // x | ~y = orc(x, y)
1799 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1800 MVT::i1, MachineNode->getOperand(0),
1801 MachineNode->getOperand(1).
1802 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001803 else if (AllUsersSelectZero(MachineNode))
1804 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1805 MVT::i1, MachineNode->getOperand(0),
1806 MachineNode->getOperand(1)),
1807 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001808 break;
1809 case PPC::CRXOR:
1810 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1811 // xor(x, x) = 0
1812 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1813 MVT::i1);
1814 else if (Op1Set)
1815 // xor(1, y) -> nor(y, y)
1816 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1817 MVT::i1, MachineNode->getOperand(1),
1818 MachineNode->getOperand(1));
1819 else if (Op2Set)
1820 // xor(x, 1) -> nor(x, x)
1821 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1822 MVT::i1, MachineNode->getOperand(0),
1823 MachineNode->getOperand(0));
1824 else if (Op1Unset)
1825 // xor(0, y) = y
1826 ResNode = MachineNode->getOperand(1).getNode();
1827 else if (Op2Unset)
1828 // xor(x, 0) = x
1829 ResNode = MachineNode->getOperand(0).getNode();
1830 else if (Op1Not)
1831 // xor(~x, y) = eqv(x, y)
1832 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1833 MVT::i1, MachineNode->getOperand(0).
1834 getOperand(0),
1835 MachineNode->getOperand(1));
1836 else if (Op2Not)
1837 // xor(x, ~y) = eqv(x, y)
1838 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1839 MVT::i1, MachineNode->getOperand(0),
1840 MachineNode->getOperand(1).
1841 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001842 else if (AllUsersSelectZero(MachineNode))
1843 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1844 MVT::i1, MachineNode->getOperand(0),
1845 MachineNode->getOperand(1)),
1846 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001847 break;
1848 case PPC::CRNOR:
1849 if (Op1Set || Op2Set)
1850 // nor(1, y) -> 0
1851 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1852 MVT::i1);
1853 else if (Op1Unset)
1854 // nor(0, y) = ~y -> nor(y, y)
1855 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1856 MVT::i1, MachineNode->getOperand(1),
1857 MachineNode->getOperand(1));
1858 else if (Op2Unset)
1859 // nor(x, 0) = ~x
1860 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1861 MVT::i1, MachineNode->getOperand(0),
1862 MachineNode->getOperand(0));
1863 else if (Op1Not)
1864 // nor(~x, y) = andc(x, y)
1865 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1866 MVT::i1, MachineNode->getOperand(0).
1867 getOperand(0),
1868 MachineNode->getOperand(1));
1869 else if (Op2Not)
1870 // nor(x, ~y) = andc(y, x)
1871 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1872 MVT::i1, MachineNode->getOperand(1).
1873 getOperand(0),
1874 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001875 else if (AllUsersSelectZero(MachineNode))
1876 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1877 MVT::i1, MachineNode->getOperand(0),
1878 MachineNode->getOperand(1)),
1879 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001880 break;
1881 case PPC::CREQV:
1882 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1883 // eqv(x, x) = 1
1884 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1885 MVT::i1);
1886 else if (Op1Set)
1887 // eqv(1, y) = y
1888 ResNode = MachineNode->getOperand(1).getNode();
1889 else if (Op2Set)
1890 // eqv(x, 1) = x
1891 ResNode = MachineNode->getOperand(0).getNode();
1892 else if (Op1Unset)
1893 // eqv(0, y) = ~y -> nor(y, y)
1894 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1895 MVT::i1, MachineNode->getOperand(1),
1896 MachineNode->getOperand(1));
1897 else if (Op2Unset)
1898 // eqv(x, 0) = ~x
1899 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1900 MVT::i1, MachineNode->getOperand(0),
1901 MachineNode->getOperand(0));
1902 else if (Op1Not)
1903 // eqv(~x, y) = xor(x, y)
1904 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1905 MVT::i1, MachineNode->getOperand(0).
1906 getOperand(0),
1907 MachineNode->getOperand(1));
1908 else if (Op2Not)
1909 // eqv(x, ~y) = xor(x, y)
1910 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1911 MVT::i1, MachineNode->getOperand(0),
1912 MachineNode->getOperand(1).
1913 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001914 else if (AllUsersSelectZero(MachineNode))
1915 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1916 MVT::i1, MachineNode->getOperand(0),
1917 MachineNode->getOperand(1)),
1918 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001919 break;
1920 case PPC::CRANDC:
1921 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1922 // andc(x, x) = 0
1923 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1924 MVT::i1);
1925 else if (Op1Set)
1926 // andc(1, y) = ~y
1927 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1928 MVT::i1, MachineNode->getOperand(1),
1929 MachineNode->getOperand(1));
1930 else if (Op1Unset || Op2Set)
1931 // andc(0, y) = andc(x, 1) = 0
1932 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1933 MVT::i1);
1934 else if (Op2Unset)
1935 // andc(x, 0) = x
1936 ResNode = MachineNode->getOperand(0).getNode();
1937 else if (Op1Not)
1938 // andc(~x, y) = ~(x | y) = nor(x, y)
1939 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1940 MVT::i1, MachineNode->getOperand(0).
1941 getOperand(0),
1942 MachineNode->getOperand(1));
1943 else if (Op2Not)
1944 // andc(x, ~y) = x & y
1945 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1946 MVT::i1, MachineNode->getOperand(0),
1947 MachineNode->getOperand(1).
1948 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001949 else if (AllUsersSelectZero(MachineNode))
1950 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1951 MVT::i1, MachineNode->getOperand(1),
1952 MachineNode->getOperand(0)),
1953 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001954 break;
1955 case PPC::CRORC:
1956 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1957 // orc(x, x) = 1
1958 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1959 MVT::i1);
1960 else if (Op1Set || Op2Unset)
1961 // orc(1, y) = orc(x, 0) = 1
1962 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1963 MVT::i1);
1964 else if (Op2Set)
1965 // orc(x, 1) = x
1966 ResNode = MachineNode->getOperand(0).getNode();
1967 else if (Op1Unset)
1968 // orc(0, y) = ~y
1969 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1970 MVT::i1, MachineNode->getOperand(1),
1971 MachineNode->getOperand(1));
1972 else if (Op1Not)
1973 // orc(~x, y) = ~(x & y) = nand(x, y)
1974 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1975 MVT::i1, MachineNode->getOperand(0).
1976 getOperand(0),
1977 MachineNode->getOperand(1));
1978 else if (Op2Not)
1979 // orc(x, ~y) = x | y
1980 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1981 MVT::i1, MachineNode->getOperand(0),
1982 MachineNode->getOperand(1).
1983 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001984 else if (AllUsersSelectZero(MachineNode))
1985 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1986 MVT::i1, MachineNode->getOperand(1),
1987 MachineNode->getOperand(0)),
1988 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001989 break;
1990 case PPC::SELECT_I4:
1991 case PPC::SELECT_I8:
1992 case PPC::SELECT_F4:
1993 case PPC::SELECT_F8:
1994 case PPC::SELECT_VRRC:
1995 if (Op1Set)
1996 ResNode = MachineNode->getOperand(1).getNode();
1997 else if (Op1Unset)
1998 ResNode = MachineNode->getOperand(2).getNode();
1999 else if (Op1Not)
2000 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2001 SDLoc(MachineNode),
2002 MachineNode->getValueType(0),
2003 MachineNode->getOperand(0).
2004 getOperand(0),
2005 MachineNode->getOperand(2),
2006 MachineNode->getOperand(1));
2007 break;
2008 case PPC::BC:
2009 case PPC::BCn:
2010 if (Op1Not)
2011 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2012 PPC::BC,
2013 SDLoc(MachineNode),
2014 MVT::Other,
2015 MachineNode->getOperand(0).
2016 getOperand(0),
2017 MachineNode->getOperand(1),
2018 MachineNode->getOperand(2));
2019 // FIXME: Handle Op1Set, Op1Unset here too.
2020 break;
2021 }
2022
Hal Finkelb9989152014-02-28 06:11:16 +00002023 // If we're inverting this node because it is used only by selects that
2024 // we'd like to swap, then swap the selects before the node replacement.
2025 if (SelectSwap)
2026 SwapAllSelectUsers(MachineNode);
2027
Hal Finkel940ab932014-02-28 00:27:01 +00002028 if (ResNode != MachineNode) {
2029 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2030 DEBUG(MachineNode->dump(CurDAG));
2031 DEBUG(dbgs() << "\nNew: ");
2032 DEBUG(ResNode->dump(CurDAG));
2033 DEBUG(dbgs() << "\n");
2034
2035 ReplaceUses(MachineNode, ResNode);
2036 IsModified = true;
2037 }
2038 }
2039 if (IsModified)
2040 CurDAG->RemoveDeadNodes();
2041 } while (IsModified);
2042}
2043
2044void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002045 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002046 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002047 return;
2048
2049 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2050 ++Position;
2051
2052 while (Position != CurDAG->allnodes_begin()) {
2053 SDNode *N = --Position;
2054 // Skip dead nodes and any non-machine opcodes.
2055 if (N->use_empty() || !N->isMachineOpcode())
2056 continue;
2057
2058 unsigned FirstOp;
2059 unsigned StorageOpcode = N->getMachineOpcode();
2060
2061 switch (StorageOpcode) {
2062 default: continue;
2063
2064 case PPC::LBZ:
2065 case PPC::LBZ8:
2066 case PPC::LD:
2067 case PPC::LFD:
2068 case PPC::LFS:
2069 case PPC::LHA:
2070 case PPC::LHA8:
2071 case PPC::LHZ:
2072 case PPC::LHZ8:
2073 case PPC::LWA:
2074 case PPC::LWZ:
2075 case PPC::LWZ8:
2076 FirstOp = 0;
2077 break;
2078
2079 case PPC::STB:
2080 case PPC::STB8:
2081 case PPC::STD:
2082 case PPC::STFD:
2083 case PPC::STFS:
2084 case PPC::STH:
2085 case PPC::STH8:
2086 case PPC::STW:
2087 case PPC::STW8:
2088 FirstOp = 1;
2089 break;
2090 }
2091
2092 // If this is a load or store with a zero offset, we may be able to
2093 // fold an add-immediate into the memory operation.
2094 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2095 N->getConstantOperandVal(FirstOp) != 0)
2096 continue;
2097
2098 SDValue Base = N->getOperand(FirstOp + 1);
2099 if (!Base.isMachineOpcode())
2100 continue;
2101
2102 unsigned Flags = 0;
2103 bool ReplaceFlags = true;
2104
2105 // When the feeding operation is an add-immediate of some sort,
2106 // determine whether we need to add relocation information to the
2107 // target flags on the immediate operand when we fold it into the
2108 // load instruction.
2109 //
2110 // For something like ADDItocL, the relocation information is
2111 // inferred from the opcode; when we process it in the AsmPrinter,
2112 // we add the necessary relocation there. A load, though, can receive
2113 // relocation from various flavors of ADDIxxx, so we need to carry
2114 // the relocation information in the target flags.
2115 switch (Base.getMachineOpcode()) {
2116 default: continue;
2117
2118 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002119 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002120 // In some cases (such as TLS) the relocation information
2121 // is already in place on the operand, so copying the operand
2122 // is sufficient.
2123 ReplaceFlags = false;
2124 // For these cases, the immediate may not be divisible by 4, in
2125 // which case the fold is illegal for DS-form instructions. (The
2126 // other cases provide aligned addresses and are always safe.)
2127 if ((StorageOpcode == PPC::LWA ||
2128 StorageOpcode == PPC::LD ||
2129 StorageOpcode == PPC::STD) &&
2130 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2131 Base.getConstantOperandVal(1) % 4 != 0))
2132 continue;
2133 break;
2134 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002135 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002136 break;
2137 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002138 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002139 break;
2140 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002141 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002142 break;
2143 }
2144
2145 // We found an opportunity. Reverse the operands from the add
2146 // immediate and substitute them into the load or store. If
2147 // needed, update the target flags for the immediate operand to
2148 // reflect the necessary relocation information.
2149 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2150 DEBUG(Base->dump(CurDAG));
2151 DEBUG(dbgs() << "\nN: ");
2152 DEBUG(N->dump(CurDAG));
2153 DEBUG(dbgs() << "\n");
2154
2155 SDValue ImmOpnd = Base.getOperand(1);
2156
2157 // If the relocation information isn't already present on the
2158 // immediate operand, add it now.
2159 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002160 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002161 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002162 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002163 // We can't perform this optimization for data whose alignment
2164 // is insufficient for the instruction encoding.
2165 if (GV->getAlignment() < 4 &&
2166 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2167 StorageOpcode == PPC::LWA)) {
2168 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2169 continue;
2170 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002171 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002172 } else if (ConstantPoolSDNode *CP =
2173 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002174 const Constant *C = CP->getConstVal();
2175 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2176 CP->getAlignment(),
2177 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002178 }
2179 }
2180
2181 if (FirstOp == 1) // Store
2182 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2183 Base.getOperand(0), N->getOperand(3));
2184 else // Load
2185 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2186 N->getOperand(2));
2187
2188 // The add-immediate may now be dead, in which case remove it.
2189 if (Base.getNode()->use_empty())
2190 CurDAG->RemoveDeadNode(Base.getNode());
2191 }
2192}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002193
Chris Lattnerb055c872006-06-10 01:15:02 +00002194
Andrew Trickc416ba62010-12-24 04:28:06 +00002195/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002196/// PowerPC-specific DAG, ready for instruction scheduling.
2197///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002198FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002199 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002200}
2201
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002202static void initializePassOnce(PassRegistry &Registry) {
2203 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00002204 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2205 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002206 Registry.registerPass(*PI, true);
2207}
2208
2209void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2210 CALL_ONCE_INITIALIZATION(initializePassOnce);
2211}
2212