blob: cb7d73a04c2f1791535868caa0ccc710c92c3b9d [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"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000030#include "llvm/IR/Module.h"
Hal Finkel940ab932014-02-28 00:27:01 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "ppc-codegen"
40
Hal Finkel940ab932014-02-28 00:27:01 +000041// FIXME: Remove this once the bug has been fixed!
42cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
43cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
44
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000045namespace llvm {
46 void initializePPCDAGToDAGISelPass(PassRegistry&);
47}
48
Chris Lattner43ff01e2005-08-17 19:33:03 +000049namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000050 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000051 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000052 /// instructions for SelectionDAG operations.
53 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000054 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000055 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000056 const PPCTargetLowering *PPCLowering;
57 const PPCSubtarget *PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000058 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000059 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000060 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christopherd9134482014-08-04 21:25:23 +000061 : SelectionDAGISel(tm), TM(tm),
62 PPCLowering(TM.getSubtargetImpl()->getTargetLowering()),
63 PPCSubTarget(TM.getSubtargetImpl()) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000064 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
65 }
Andrew Trickc416ba62010-12-24 04:28:06 +000066
Craig Topper0d3fa922014-04-29 07:57:37 +000067 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000068 // Make sure we re-emit a set of the global base reg if necessary
69 GlobalBaseReg = 0;
Eric Christopherd9134482014-08-04 21:25:23 +000070 PPCLowering = TM.getSubtargetImpl()->getTargetLowering();
Eric Christopher1b8e7632014-05-22 01:07:24 +000071 PPCSubTarget = TM.getSubtargetImpl();
Dan Gohman5ea74d52009-07-31 18:16:33 +000072 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000073
Eric Christopher1b8e7632014-05-22 01:07:24 +000074 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000075 InsertVRSaveCode(MF);
76
Chris Lattner1678a6c2006-03-16 18:25:23 +000077 return true;
Chris Lattner45640392005-08-19 22:38:53 +000078 }
Andrew Trickc416ba62010-12-24 04:28:06 +000079
Craig Topper0d3fa922014-04-29 07:57:37 +000080 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000081
Chris Lattner43ff01e2005-08-17 19:33:03 +000082 /// getI32Imm - Return a target constant with the specified value, of type
83 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000084 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000085 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000086 }
Chris Lattner45640392005-08-19 22:38:53 +000087
Chris Lattner97b3da12006-06-27 00:04:13 +000088 /// getI64Imm - Return a target constant with the specified value, of type
89 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000090 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000091 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000092 }
Andrew Trickc416ba62010-12-24 04:28:06 +000093
Chris Lattner97b3da12006-06-27 00:04:13 +000094 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000095 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +000096 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +000097 }
Andrew Trickc416ba62010-12-24 04:28:06 +000098
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000099 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +0000100 /// with any number of 0s on either side. The 1s are allowed to wrap from
101 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
102 /// 0x0F0F0000 is not, since all 1s are not contiguous.
103 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
104
105
106 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
107 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000108 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000109 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000110
Chris Lattner45640392005-08-19 22:38:53 +0000111 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
112 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000113 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000114
Chris Lattner43ff01e2005-08-17 19:33:03 +0000115 // Select - Convert the specified operand from a target-independent to a
116 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000117 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000118
Nate Begeman93c4bc62005-08-19 00:38:14 +0000119 SDNode *SelectBitfieldInsert(SDNode *N);
120
Chris Lattner2a1823d2005-08-21 18:50:37 +0000121 /// SelectCC - Select a comparison of the specified values with the
122 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000123 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000124
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000125 /// SelectAddrImm - Returns true if the address N can be represented by
126 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000127 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000128 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000129 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000130 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000131
Chris Lattner6f5840c2006-11-16 00:41:37 +0000132 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000133 /// immediate field. Note that the operand at this point is already the
134 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000135 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000136 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000137 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000138 Out = N;
139 return true;
140 }
141
142 return false;
143 }
144
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000145 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
146 /// represented as an indexed [r+r] operation. Returns false if it can
147 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000148 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000149 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000150 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000151
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000152 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
153 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000154 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000155 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000156 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000157
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000158 /// SelectAddrImmX4 - Returns true if the address N can be represented by
159 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
160 /// Suitable for use by STD and friends.
161 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000162 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000163 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000164
Hal Finkel756810f2013-03-21 21:37:52 +0000165 // Select an address into a single register.
166 bool SelectAddr(SDValue N, SDValue &Base) {
167 Base = N;
168 return true;
169 }
170
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000171 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000172 /// inline asm expressions. It is always correct to compute the value into
173 /// a register. The case of adding a (possibly relocatable) constant to a
174 /// register can be improved, but it is wrong to substitute Reg+Reg for
175 /// Reg in an asm, because the load or store opcode would have to change.
Craig Topper0d3fa922014-04-29 07:57:37 +0000176 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
177 char ConstraintCode,
178 std::vector<SDValue> &OutOps) override {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000179 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000180 return false;
181 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000182
Dan Gohman5ea74d52009-07-31 18:16:33 +0000183 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000184
Craig Topper0d3fa922014-04-29 07:57:37 +0000185 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000186 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000187 }
188
Chris Lattner03e08ee2005-09-13 22:03:06 +0000189// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000190#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000191
Chris Lattner259e6c72005-10-06 18:45:51 +0000192private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000193 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000194
195 void PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +0000196 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000197
198 bool AllUsersSelectZero(SDNode *N);
199 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000200 };
201}
202
Chris Lattner1678a6c2006-03-16 18:25:23 +0000203/// InsertVRSaveCode - Once the entire function has been instruction selected,
204/// all virtual registers are created and all machine instructions are built,
205/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000206void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000207 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000208 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000209 //
Dan Gohman4a618822010-02-10 16:03:48 +0000210 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000211 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000212 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000213 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
214 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
215 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000216 HasVectorVReg = true;
217 break;
218 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000219 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000220 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000221
Chris Lattner02e2c182006-03-13 21:52:10 +0000222 // If we have a vector register, we want to emit code into the entry and exit
223 // blocks to save and restore the VRSAVE register. We do this here (instead
224 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
225 //
226 // 1. This (trivially) reduces the load on the register allocator, by not
227 // having to represent the live range of the VRSAVE register.
228 // 2. This (more significantly) allows us to create a temporary virtual
229 // register to hold the saved VRSAVE value, allowing this temporary to be
230 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000231
232 // Create two vregs - one to hold the VRSAVE register that is live-in to the
233 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000234 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
235 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000236
Eric Christopherd9134482014-08-04 21:25:23 +0000237 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000238 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000239 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000240 // Emit the following code into the entry block:
241 // InVRSAVE = MFVRSAVE
242 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
243 // MTVRSAVE UpdatedVRSAVE
244 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000245 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
246 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000247 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000248 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000249
Chris Lattner1678a6c2006-03-16 18:25:23 +0000250 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000252 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000253 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000254
Chris Lattner1678a6c2006-03-16 18:25:23 +0000255 // Skip over all terminator instructions, which are part of the return
256 // sequence.
257 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000258 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000259 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000260
Chris Lattner1678a6c2006-03-16 18:25:23 +0000261 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000262 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000263 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000264 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000265}
Chris Lattner8ae95252005-09-03 01:17:22 +0000266
Chris Lattner1678a6c2006-03-16 18:25:23 +0000267
Chris Lattner45640392005-08-19 22:38:53 +0000268/// getGlobalBaseReg - Output the instructions required to put the
269/// base address to use for accessing globals into a register.
270///
Evan Cheng61413a32006-08-26 05:34:46 +0000271SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000272 if (!GlobalBaseReg) {
Eric Christopherd9134482014-08-04 21:25:23 +0000273 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000274 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000275 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000276 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000277 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000278 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000279
Eric Christopher1b8e7632014-05-22 01:07:24 +0000280 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000281 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000282 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000283 if (M->getPICLevel() == PICLevel::Small) {
284 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
285 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
286 } else {
287 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
288 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
289 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
290 BuildMI(FirstMBB, MBBI, dl,
291 TII.get(PPC::UpdateGBR)).addReg(GlobalBaseReg)
292 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
293 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
294 }
295 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000296 GlobalBaseReg =
297 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000298 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
299 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000300 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000301 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000302 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000303 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000304 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000305 }
Chris Lattner45640392005-08-19 22:38:53 +0000306 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000307 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000308 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000309}
310
311/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
312/// or 64-bit immediate, and if the value can be accurately represented as a
313/// sign extension from a 16-bit value. If so, this returns true and the
314/// immediate.
315static bool isIntS16Immediate(SDNode *N, short &Imm) {
316 if (N->getOpcode() != ISD::Constant)
317 return false;
318
Dan Gohmaneffb8942008-09-12 16:56:44 +0000319 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000320 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000321 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000322 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000323 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000324}
325
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000326static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000327 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000328}
329
330
Chris Lattner97b3da12006-06-27 00:04:13 +0000331/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
332/// operand. If so Imm will receive the 32-bit value.
333static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000334 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000335 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000336 return true;
337 }
338 return false;
339}
340
Chris Lattner97b3da12006-06-27 00:04:13 +0000341/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
342/// operand. If so Imm will receive the 64-bit value.
343static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000344 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000345 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000346 return true;
347 }
348 return false;
349}
350
351// isInt32Immediate - This method tests to see if a constant operand.
352// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000353static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000354 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000355}
356
357
358// isOpcWithIntImmediate - This method tests to see if the node is a specific
359// opcode and that it has a immediate integer right operand.
360// If so Imm will receive the 32 bit value.
361static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000362 return N->getOpcode() == Opc
363 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000364}
365
Nate Begemand31efd12006-09-22 05:01:56 +0000366bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000367 if (!Val)
368 return false;
369
Nate Begemanb3821a32005-08-18 07:30:46 +0000370 if (isShiftedMask_32(Val)) {
371 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000372 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000373 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000374 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000375 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000376 } else {
377 Val = ~Val; // invert mask
378 if (isShiftedMask_32(Val)) {
379 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000380 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000381 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000382 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000383 return true;
384 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000385 }
386 // no run present
387 return false;
388}
389
Andrew Trickc416ba62010-12-24 04:28:06 +0000390bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
391 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000392 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000393 // Don't even go down this path for i64, since different logic will be
394 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000395 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000396 return false;
397
Nate Begemanb3821a32005-08-18 07:30:46 +0000398 unsigned Shift = 32;
399 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
400 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000401 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000402 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000403 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000404
Nate Begemanb3821a32005-08-18 07:30:46 +0000405 if (Opcode == ISD::SHL) {
406 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000407 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000408 // determine which bits are made indeterminant by shift
409 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000410 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000411 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000412 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000413 // determine which bits are made indeterminant by shift
414 Indeterminant = ~(0xFFFFFFFFu >> Shift);
415 // adjust for the left rotate
416 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000417 } else if (Opcode == ISD::ROTL) {
418 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000419 } else {
420 return false;
421 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000422
Nate Begemanb3821a32005-08-18 07:30:46 +0000423 // if the mask doesn't intersect any Indeterminant bits
424 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000425 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000426 // make sure the mask is still a mask (wrap arounds may not be)
427 return isRunOfOnes(Mask, MB, ME);
428 }
429 return false;
430}
431
Nate Begeman93c4bc62005-08-19 00:38:14 +0000432/// SelectBitfieldInsert - turn an or of two masked values into
433/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000434SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000435 SDValue Op0 = N->getOperand(0);
436 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000437 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000438
Dan Gohmanf19609a2008-02-27 01:23:58 +0000439 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000440 CurDAG->computeKnownBits(Op0, LKZ, LKO);
441 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000442
Dan Gohmanf19609a2008-02-27 01:23:58 +0000443 unsigned TargetMask = LKZ.getZExtValue();
444 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000445
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000446 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
447 unsigned Op0Opc = Op0.getOpcode();
448 unsigned Op1Opc = Op1.getOpcode();
449 unsigned Value, SH = 0;
450 TargetMask = ~TargetMask;
451 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000452
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000453 // If the LHS has a foldable shift and the RHS does not, then swap it to the
454 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000455 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
456 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
457 Op0.getOperand(0).getOpcode() == ISD::SRL) {
458 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
459 Op1.getOperand(0).getOpcode() != ISD::SRL) {
460 std::swap(Op0, Op1);
461 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000462 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000463 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000464 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000465 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
466 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
467 Op1.getOperand(0).getOpcode() != ISD::SRL) {
468 std::swap(Op0, Op1);
469 std::swap(Op0Opc, Op1Opc);
470 std::swap(TargetMask, InsertMask);
471 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000472 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000473
Nate Begeman1333cea2006-05-07 00:23:38 +0000474 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000475 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000476 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000477
478 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000479 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000480 Op1 = Op1.getOperand(0);
481 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
482 }
483 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000484 // The AND mask might not be a constant, and we need to make sure that
485 // if we're going to fold the masking with the insert, all bits not
486 // know to be zero in the mask are known to be one.
487 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000488 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000489 bool CanFoldMask = InsertMask == MKO.getZExtValue();
490
Nate Begeman1333cea2006-05-07 00:23:38 +0000491 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000492 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000493 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000494 // Note that Value must be in range here (less than 32) because
495 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000496 Op1 = Op1.getOperand(0).getOperand(0);
497 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000498 }
499 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000500
Chris Lattnera2963392006-05-12 16:29:37 +0000501 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000502 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000503 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000504 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000505 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000506 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000507 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000508}
509
Chris Lattner2a1823d2005-08-21 18:50:37 +0000510/// SelectCC - Select a comparison of the specified values with the specified
511/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000512SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000513 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000514 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000515 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000516
Owen Anderson9f944592009-08-11 20:47:22 +0000517 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000518 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000519 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
520 if (isInt32Immediate(RHS, Imm)) {
521 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000522 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000523 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
524 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000525 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000526 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000527 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
528 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000529
Chris Lattneraa3926b2006-09-20 04:25:47 +0000530 // For non-equality comparisons, the default code would materialize the
531 // constant, then compare against it, like this:
532 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000533 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000534 // cmpw cr0, r3, r2
535 // Since we are just comparing for equality, we can emit this instead:
536 // xoris r0,r3,0x1234
537 // cmplwi cr0,r0,0x5678
538 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000539 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
540 getI32Imm(Imm >> 16)), 0);
541 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
542 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000543 }
544 Opc = PPC::CMPLW;
545 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000546 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000547 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
548 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000549 Opc = PPC::CMPLW;
550 } else {
551 short SImm;
552 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000553 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
554 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000555 0);
556 Opc = PPC::CMPW;
557 }
Owen Anderson9f944592009-08-11 20:47:22 +0000558 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000559 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000560 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000561 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000562 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000563 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000564 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
565 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000566 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000567 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000568 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
569 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000570
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000571 // For non-equality comparisons, the default code would materialize the
572 // constant, then compare against it, like this:
573 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000574 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000575 // cmpd cr0, r3, r2
576 // Since we are just comparing for equality, we can emit this instead:
577 // xoris r0,r3,0x1234
578 // cmpldi cr0,r0,0x5678
579 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000580 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000581 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
582 getI64Imm(Imm >> 16)), 0);
583 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
584 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000585 }
586 }
587 Opc = PPC::CMPLD;
588 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000589 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000590 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
591 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000592 Opc = PPC::CMPLD;
593 } else {
594 short SImm;
595 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000596 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
597 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000598 0);
599 Opc = PPC::CMPD;
600 }
Owen Anderson9f944592009-08-11 20:47:22 +0000601 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000602 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000603 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000604 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +0000605 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000606 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000607 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000608}
609
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000610static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000611 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000612 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000613 case ISD::SETONE:
614 case ISD::SETOLE:
615 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000616 llvm_unreachable("Should be lowered by legalize!");
617 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000618 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000619 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000620 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000621 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000622 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000623 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000624 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000625 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000626 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000627 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000628 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000629 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000630 case ISD::SETO: return PPC::PRED_NU;
631 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000632 // These two are invalid for floating point. Assume we have int.
633 case ISD::SETULT: return PPC::PRED_LT;
634 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000635 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000636}
637
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000638/// getCRIdxForSetCC - Return the index of the condition register field
639/// associated with the SetCC condition, and whether or not the field is
640/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000641static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000642 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000643 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000644 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000645 case ISD::SETOLT:
646 case ISD::SETLT: return 0; // Bit #0 = SETOLT
647 case ISD::SETOGT:
648 case ISD::SETGT: return 1; // Bit #1 = SETOGT
649 case ISD::SETOEQ:
650 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
651 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000652 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000653 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000654 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000655 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000656 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000657 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
658 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000659 case ISD::SETUEQ:
660 case ISD::SETOGE:
661 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000662 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000663 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000664 // These are invalid for floating point. Assume integer.
665 case ISD::SETULT: return 0;
666 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000667 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000668}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000669
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000670// getVCmpInst: return the vector compare instruction for the specified
671// vector type and condition code. Since this is for altivec specific code,
672// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000673static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
674 bool HasVSX, bool &Swap, bool &Negate) {
675 Swap = false;
676 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000677
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000678 if (VecVT.isFloatingPoint()) {
679 /* Handle some cases by swapping input operands. */
680 switch (CC) {
681 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
682 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
683 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
684 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
685 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
686 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
687 default: break;
688 }
689 /* Handle some cases by negating the result. */
690 switch (CC) {
691 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
692 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
693 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
694 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
695 default: break;
696 }
697 /* We have instructions implementing the remaining cases. */
698 switch (CC) {
699 case ISD::SETEQ:
700 case ISD::SETOEQ:
701 if (VecVT == MVT::v4f32)
702 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
703 else if (VecVT == MVT::v2f64)
704 return PPC::XVCMPEQDP;
705 break;
706 case ISD::SETGT:
707 case ISD::SETOGT:
708 if (VecVT == MVT::v4f32)
709 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
710 else if (VecVT == MVT::v2f64)
711 return PPC::XVCMPGTDP;
712 break;
713 case ISD::SETGE:
714 case ISD::SETOGE:
715 if (VecVT == MVT::v4f32)
716 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
717 else if (VecVT == MVT::v2f64)
718 return PPC::XVCMPGEDP;
719 break;
720 default:
721 break;
722 }
723 llvm_unreachable("Invalid floating-point vector compare condition");
724 } else {
725 /* Handle some cases by swapping input operands. */
726 switch (CC) {
727 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
728 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
729 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
730 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
731 default: break;
732 }
733 /* Handle some cases by negating the result. */
734 switch (CC) {
735 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
736 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
737 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
738 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
739 default: break;
740 }
741 /* We have instructions implementing the remaining cases. */
742 switch (CC) {
743 case ISD::SETEQ:
744 case ISD::SETUEQ:
745 if (VecVT == MVT::v16i8)
746 return PPC::VCMPEQUB;
747 else if (VecVT == MVT::v8i16)
748 return PPC::VCMPEQUH;
749 else if (VecVT == MVT::v4i32)
750 return PPC::VCMPEQUW;
751 break;
752 case ISD::SETGT:
753 if (VecVT == MVT::v16i8)
754 return PPC::VCMPGTSB;
755 else if (VecVT == MVT::v8i16)
756 return PPC::VCMPGTSH;
757 else if (VecVT == MVT::v4i32)
758 return PPC::VCMPGTSW;
759 break;
760 case ISD::SETUGT:
761 if (VecVT == MVT::v16i8)
762 return PPC::VCMPGTUB;
763 else if (VecVT == MVT::v8i16)
764 return PPC::VCMPGTUH;
765 else if (VecVT == MVT::v4i32)
766 return PPC::VCMPGTUW;
767 break;
768 default:
769 break;
770 }
771 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000772 }
773}
774
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000775SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000776 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000777 unsigned Imm;
778 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000779 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
780 bool isPPC64 = (PtrVT == MVT::i64);
781
Eric Christopher1b8e7632014-05-22 01:07:24 +0000782 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +0000783 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000784 // We can codegen setcc op, imm very efficiently compared to a brcond.
785 // Check for those cases here.
786 // setcc op, 0
787 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000788 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000789 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000790 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000791 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000792 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000793 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000794 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000795 }
Chris Lattnere2969492005-10-21 21:17:10 +0000796 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000797 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000798 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000799 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000800 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000801 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000802 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000803 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000804 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000805 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000806 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000807 }
Chris Lattnere2969492005-10-21 21:17:10 +0000808 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000809 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000810 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
811 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000812 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000813 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000814 }
815 }
Chris Lattner491b8292005-10-06 19:03:35 +0000816 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000817 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000818 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000819 default: break;
820 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000821 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000822 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000823 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000824 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
825 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000826 MVT::i32,
827 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000828 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000829 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000830 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000831 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000832 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000833 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000834 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000835 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000836 }
Chris Lattnere2969492005-10-21 21:17:10 +0000837 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000838 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
839 getI32Imm(1)), 0);
840 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
841 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000842 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000843 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000844 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000845 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000846 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000847 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000848 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000849 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000850 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000851 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000852 }
Chris Lattner491b8292005-10-06 19:03:35 +0000853 }
854 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000855
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000856 SDValue LHS = N->getOperand(0);
857 SDValue RHS = N->getOperand(1);
858
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000859 // Altivec Vector compare instructions do not set any CR register by default and
860 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000861 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000862 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000863 bool Swap, Negate;
864 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
865 PPCSubTarget->hasVSX(), Swap, Negate);
866 if (Swap)
867 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000868
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000869 if (Negate) {
870 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
871 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
872 PPC::VNOR,
873 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000874 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +0000875
876 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000877 }
878
Eric Christopher1b8e7632014-05-22 01:07:24 +0000879 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +0000880 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +0000881
Chris Lattner491b8292005-10-06 19:03:35 +0000882 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000883 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000884 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000885 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000886
Chris Lattner491b8292005-10-06 19:03:35 +0000887 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000888 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000889
Craig Topper062a2ba2014-04-25 05:30:21 +0000890 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000891 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000892 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000893
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000894 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
895 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000896
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000897 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000898 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000899 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +0000900 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +0000901
902 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000903 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000904 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000905 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000906}
Chris Lattner502a3692005-10-06 18:56:10 +0000907
Chris Lattner318622f2005-10-06 19:07:45 +0000908
Chris Lattner43ff01e2005-08-17 19:33:03 +0000909// Select - Convert the specified operand from a target-independent to a
910// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000911SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000912 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000913 if (N->isMachineOpcode()) {
914 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000915 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000916 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000917
Hal Finkel51b3fd12014-09-02 06:23:54 +0000918 // In case any misguided DAG-level optimizations form an ADD with a
919 // TargetConstant operand, crash here instead of miscompiling (by selecting
920 // an r+r add instead of some kind of r+i add).
921 if (N->getOpcode() == ISD::ADD &&
922 N->getOperand(1).getOpcode() == ISD::TargetConstant)
923 llvm_unreachable("Invalid ADD with TargetConstant operand");
924
Chris Lattner43ff01e2005-08-17 19:33:03 +0000925 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +0000926 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +0000927
Jim Laskey095e6f32006-12-12 13:23:43 +0000928 case ISD::Constant: {
Owen Anderson9f944592009-08-11 20:47:22 +0000929 if (N->getValueType(0) == MVT::i64) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000930 // Get 64 bit value.
Dan Gohmaneffb8942008-09-12 16:56:44 +0000931 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Jim Laskey095e6f32006-12-12 13:23:43 +0000932 // Assume no remaining bits.
933 unsigned Remainder = 0;
934 // Assume no shift required.
935 unsigned Shift = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000936
Jim Laskey095e6f32006-12-12 13:23:43 +0000937 // If it can't be represented as a 32 bit value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000938 if (!isInt<32>(Imm)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000939 Shift = countTrailingZeros<uint64_t>(Imm);
Jim Laskey095e6f32006-12-12 13:23:43 +0000940 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
Andrew Trickc416ba62010-12-24 04:28:06 +0000941
Jim Laskey095e6f32006-12-12 13:23:43 +0000942 // If the shifted value fits 32 bits.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000943 if (isInt<32>(ImmSh)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000944 // Go with the shifted value.
945 Imm = ImmSh;
946 } else {
947 // Still stuck with a 64 bit value.
948 Remainder = Imm;
949 Shift = 32;
950 Imm >>= 32;
951 }
952 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000953
Jim Laskey095e6f32006-12-12 13:23:43 +0000954 // Intermediate operand.
955 SDNode *Result;
956
957 // Handle first 32 bits.
958 unsigned Lo = Imm & 0xFFFF;
959 unsigned Hi = (Imm >> 16) & 0xFFFF;
Andrew Trickc416ba62010-12-24 04:28:06 +0000960
Jim Laskey095e6f32006-12-12 13:23:43 +0000961 // Simple value.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000962 if (isInt<16>(Imm)) {
Jim Laskey095e6f32006-12-12 13:23:43 +0000963 // Just the Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000964 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000965 } else if (Lo) {
966 // Handle the Hi bits.
967 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
Dan Gohman32f71d72009-09-25 18:54:59 +0000968 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000969 // And Lo bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000970 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
971 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000972 } else {
973 // Just the Hi bits.
Dan Gohman32f71d72009-09-25 18:54:59 +0000974 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
Jim Laskey095e6f32006-12-12 13:23:43 +0000975 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000976
Jim Laskey095e6f32006-12-12 13:23:43 +0000977 // If no shift, we're done.
978 if (!Shift) return Result;
979
980 // Shift for next step if the upper 32-bits were not zero.
981 if (Imm) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000982 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
983 SDValue(Result, 0),
984 getI32Imm(Shift),
985 getI32Imm(63 - Shift));
Jim Laskey095e6f32006-12-12 13:23:43 +0000986 }
987
988 // Add in the last bits as required.
989 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000990 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
991 SDValue(Result, 0), getI32Imm(Hi));
Andrew Trickc416ba62010-12-24 04:28:06 +0000992 }
Jim Laskey095e6f32006-12-12 13:23:43 +0000993 if ((Lo = Remainder & 0xFFFF)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000994 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
995 SDValue(Result, 0), getI32Imm(Lo));
Jim Laskey095e6f32006-12-12 13:23:43 +0000996 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000997
Jim Laskey095e6f32006-12-12 13:23:43 +0000998 return Result;
999 }
1000 break;
1001 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001002
Hal Finkel940ab932014-02-28 00:27:01 +00001003 case ISD::SETCC: {
1004 SDNode *SN = SelectSETCC(N);
1005 if (SN)
1006 return SN;
1007 break;
1008 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00001009 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00001010 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00001011
Chris Lattnere4c338d2005-08-25 00:45:43 +00001012 case ISD::FrameIndex: {
1013 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001014 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1015 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001016 if (N->hasOneUse())
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001017 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
Evan Cheng34b70ee2006-08-26 08:00:10 +00001018 getSmallIPtrImm(0));
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001019 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Dan Gohman32f71d72009-09-25 18:54:59 +00001020 getSmallIPtrImm(0));
Chris Lattnere4c338d2005-08-25 00:45:43 +00001021 }
Chris Lattner6961fc72006-03-26 10:06:40 +00001022
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001023 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001024 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00001025 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1026 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00001027 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001028
Hal Finkelbbdee932014-12-02 22:01:00 +00001029 case PPCISD::READ_TIME_BASE: {
1030 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
1031 MVT::Other, N->getOperand(0));
1032 }
1033
Chris Lattner57693112005-09-28 22:50:24 +00001034 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001035 // FIXME: since this depends on the setting of the carry flag from the srawi
1036 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001037 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001038 // srl/add/sra pattern the dag combiner will generate for this as
1039 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001040 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001041 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001042 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001043 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001044 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001045 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001046 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001047 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001048 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001049 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001050 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001051 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001052 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001053 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001054 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1055 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001056 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001057 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001058 }
1059 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001060
Chris Lattner1de57062005-09-29 23:33:31 +00001061 // Other cases are autogenerated.
1062 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001063 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001064
Chris Lattnerce645542006-11-10 02:08:47 +00001065 case ISD::LOAD: {
1066 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001067 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001068 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001069
Chris Lattnerce645542006-11-10 02:08:47 +00001070 // Normal loads are handled by code generated from the .td file.
1071 if (LD->getAddressingMode() != ISD::PRE_INC)
1072 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001073
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001074 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001075 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001076 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001077
Chris Lattner474b5b72006-11-15 19:55:13 +00001078 unsigned Opcode;
1079 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001080 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001081 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001082 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::f64: Opcode = PPC::LFDU; break;
1086 case MVT::f32: Opcode = PPC::LFSU; break;
1087 case MVT::i32: Opcode = PPC::LWZU; break;
1088 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1089 case MVT::i1:
1090 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001091 }
1092 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001093 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1094 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1095 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001096 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001097 case MVT::i64: Opcode = PPC::LDU; break;
1098 case MVT::i32: Opcode = PPC::LWZU8; break;
1099 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1100 case MVT::i1:
1101 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001102 }
1103 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001104
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001105 SDValue Chain = LD->getChain();
1106 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001107 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001108 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001109 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001110 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001111 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001112 unsigned Opcode;
1113 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1114 if (LD->getValueType(0) != MVT::i64) {
1115 // Handle PPC32 integer and normal FP loads.
1116 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1117 switch (LoadedVT.getSimpleVT().SimpleTy) {
1118 default: llvm_unreachable("Invalid PPC load type!");
1119 case MVT::f64: Opcode = PPC::LFDUX; break;
1120 case MVT::f32: Opcode = PPC::LFSUX; break;
1121 case MVT::i32: Opcode = PPC::LWZUX; break;
1122 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1123 case MVT::i1:
1124 case MVT::i8: Opcode = PPC::LBZUX; break;
1125 }
1126 } else {
1127 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1128 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1129 "Invalid sext update load");
1130 switch (LoadedVT.getSimpleVT().SimpleTy) {
1131 default: llvm_unreachable("Invalid PPC load type!");
1132 case MVT::i64: Opcode = PPC::LDUX; break;
1133 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1134 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1135 case MVT::i1:
1136 case MVT::i8: Opcode = PPC::LBZUX8; break;
1137 }
1138 }
1139
1140 SDValue Chain = LD->getChain();
1141 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001142 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001143 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00001144 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001145 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001146 }
1147 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001148
Nate Begemanb3821a32005-08-18 07:30:46 +00001149 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001150 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001151 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001152
Nate Begemanb3821a32005-08-18 07:30:46 +00001153 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1154 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001155 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001156 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001157 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001158 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001159 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00001160 }
Nate Begemand31efd12006-09-22 05:01:56 +00001161 // If this is just a masked value where the input is not handled above, and
1162 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1163 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001164 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001165 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001166 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001167 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001168 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00001169 }
Hal Finkele39526a2012-08-28 02:10:15 +00001170 // If this is a 64-bit zero-extension mask, emit rldicl.
1171 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1172 isMask_64(Imm64)) {
1173 SDValue Val = N->getOperand(0);
1174 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001175 SH = 0;
1176
1177 // If the operand is a logical right shift, we can fold it into this
1178 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1179 // for n <= mb. The right shift is really a left rotate followed by a
1180 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1181 // by the shift.
1182 if (Val.getOpcode() == ISD::SRL &&
1183 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1184 assert(Imm < 64 && "Illegal shift amount");
1185 Val = Val.getOperand(0);
1186 SH = 64 - Imm;
1187 }
1188
1189 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00001190 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00001191 }
Nate Begemand31efd12006-09-22 05:01:56 +00001192 // AND X, 0 -> 0, not "rlwinm 32".
1193 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001194 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00001195 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00001196 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001197 // ISD::OR doesn't get all the bitfield insertion fun.
1198 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001199 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001200 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001201 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001202 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001203 Imm = ~(Imm^Imm2);
1204 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001205 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001206 N->getOperand(0).getOperand(1),
1207 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001208 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001209 }
1210 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001211
Chris Lattner1de57062005-09-29 23:33:31 +00001212 // Other cases are autogenerated.
1213 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001214 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001215 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001216 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001217 if (SDNode *I = SelectBitfieldInsert(N))
1218 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001219
Chris Lattner1de57062005-09-29 23:33:31 +00001220 // Other cases are autogenerated.
1221 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001222 case ISD::SHL: {
1223 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001224 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +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 }
1234 case ISD::SRL: {
1235 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001236 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001237 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001238 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001239 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001240 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001241 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001242
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001243 // Other cases are autogenerated.
1244 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001245 }
Hal Finkel940ab932014-02-28 00:27:01 +00001246 // FIXME: Remove this once the ANDI glue bug is fixed:
1247 case PPCISD::ANDIo_1_EQ_BIT:
1248 case PPCISD::ANDIo_1_GT_BIT: {
1249 if (!ANDIGlueBug)
1250 break;
1251
1252 EVT InVT = N->getOperand(0).getValueType();
1253 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1254 "Invalid input type for ANDIo_1_EQ_BIT");
1255
1256 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1257 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1258 N->getOperand(0),
1259 CurDAG->getTargetConstant(1, InVT)), 0);
1260 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1261 SDValue SRIdxVal =
1262 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1263 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1264
1265 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1266 CR0Reg, SRIdxVal,
1267 SDValue(AndI.getNode(), 1) /* glue */);
1268 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001269 case ISD::SELECT_CC: {
1270 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001271 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1272 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001273
Hal Finkel940ab932014-02-28 00:27:01 +00001274 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001275 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00001276 N->getOperand(0).getValueType() == MVT::i1)
1277 break;
1278
Chris Lattner97b3da12006-06-27 00:04:13 +00001279 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001280 if (!isPPC64)
1281 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1282 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1283 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1284 if (N1C->isNullValue() && N3C->isNullValue() &&
1285 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1286 // FIXME: Implement this optzn for PPC64.
1287 N->getValueType(0) == MVT::i32) {
1288 SDNode *Tmp =
1289 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1290 N->getOperand(0), getI32Imm(~0U));
1291 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1292 SDValue(Tmp, 0), N->getOperand(0),
1293 SDValue(Tmp, 1));
1294 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001295
Dale Johannesenab8e4422009-02-06 19:16:40 +00001296 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001297
1298 if (N->getValueType(0) == MVT::i1) {
1299 // An i1 select is: (c & t) | (!c & f).
1300 bool Inv;
1301 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1302
1303 unsigned SRI;
1304 switch (Idx) {
1305 default: llvm_unreachable("Invalid CC index");
1306 case 0: SRI = PPC::sub_lt; break;
1307 case 1: SRI = PPC::sub_gt; break;
1308 case 2: SRI = PPC::sub_eq; break;
1309 case 3: SRI = PPC::sub_un; break;
1310 }
1311
1312 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1313
1314 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1315 CCBit, CCBit), 0);
1316 SDValue C = Inv ? NotCCBit : CCBit,
1317 NotC = Inv ? CCBit : NotCCBit;
1318
1319 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1320 C, N->getOperand(2)), 0);
1321 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1322 NotC, N->getOperand(3)), 0);
1323
1324 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1325 }
1326
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001327 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001328
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001329 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001330 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001331 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001332 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001333 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001334 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001335 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001336 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00001337 if (PPCSubTarget->hasVSX())
1338 SelectCCOp = PPC::SELECT_CC_VSFRC;
1339 else
1340 SelectCCOp = PPC::SELECT_CC_F8;
Bill Schmidt61e65232014-10-22 13:13:40 +00001341 else if (N->getValueType(0) == MVT::v2f64 ||
1342 N->getValueType(0) == MVT::v2i64)
1343 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001344 else
1345 SelectCCOp = PPC::SELECT_CC_VRRC;
1346
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001347 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001348 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00001349 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001350 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001351 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001352 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00001353 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001354 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00001355 }
1356
1357 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001358 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00001359 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001360 N->getValueType(0) == MVT::v2i64)) {
1361 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1362
1363 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1364 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1365 unsigned DM[2];
1366
1367 for (int i = 0; i < 2; ++i)
1368 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1369 DM[i] = 0;
1370 else
1371 DM[i] = 1;
1372
Hal Finkel2583b062014-03-28 20:24:55 +00001373 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001374
1375 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1376 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1377 isa<LoadSDNode>(Op1.getOperand(0))) {
1378 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1379 SDValue Base, Offset;
1380
1381 if (LD->isUnindexed() &&
1382 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1383 SDValue Chain = LD->getChain();
1384 SDValue Ops[] = { Base, Offset, Chain };
1385 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00001386 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001387 }
1388 }
1389
1390 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00001391 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001392 }
1393
1394 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001395 case PPCISD::BDNZ:
1396 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00001397 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00001398 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1399 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1400 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1401 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00001402 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00001403 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001404 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001405 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001406 // Op #1 is the PPC::PRED_* number.
1407 // Op #2 is the CR#
1408 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001409 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001410 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001411 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001412 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001413 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001414 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00001415 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001416 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001417 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001418 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001419 unsigned PCC = getPredicateForSetCC(CC);
1420
1421 if (N->getOperand(2).getValueType() == MVT::i1) {
1422 unsigned Opc;
1423 bool Swap;
1424 switch (PCC) {
1425 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1426 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1427 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1428 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1429 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1430 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1431 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1432 }
1433
1434 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1435 N->getOperand(Swap ? 3 : 2),
1436 N->getOperand(Swap ? 2 : 3)), 0);
1437 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1438 BitComp, N->getOperand(4), N->getOperand(0));
1439 }
1440
Dale Johannesenab8e4422009-02-06 19:16:40 +00001441 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001442 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001443 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001444 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001445 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001446 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001447 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001448 SDValue Chain = N->getOperand(0);
1449 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001450 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001451 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001452 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001453 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001454 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001455 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001456 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00001457 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
1458 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00001459 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
1460 SDValue GA = N->getOperand(0);
1461 return CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
1462 N->getOperand(1));
Justin Hibbits3476db42014-08-28 04:40:55 +00001463 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001464
Bill Schmidt27917782013-02-21 17:12:27 +00001465 // For medium and large code model, we generate two instructions as
1466 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001467 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00001468 CodeModel::Model CModel = TM.getCodeModel();
1469 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001470 break;
1471
Bill Schmidt5d82f092014-06-16 21:36:02 +00001472 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
1473 // If it is an externally defined symbol, a symbol with common linkage,
1474 // a non-local function address, or a jump table address, or if we are
1475 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001476 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1477 // Otherwise we generate:
1478 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1479 SDValue GA = N->getOperand(0);
1480 SDValue TOCbase = N->getOperand(1);
1481 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1482 TOCbase, GA);
1483
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00001484 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
1485 CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001486 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1487 SDValue(Tmp, 0));
1488
1489 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1490 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00001491 if ((GValue->getType()->getElementType()->isFunctionTy() &&
1492 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00001493 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
1494 GValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001495 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1496 SDValue(Tmp, 0));
1497 }
1498
1499 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1500 SDValue(Tmp, 0), GA);
1501 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00001502 case PPCISD::PPC32_PICGOT: {
1503 // Generate a PIC-safe GOT reference.
1504 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
1505 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
1506 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
1507 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001508 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001509 // This expands into one of three sequences, depending on whether
1510 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001511 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1512 isa<ConstantSDNode>(N->getOperand(1)) &&
1513 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001514
1515 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001516 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001517 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001518 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001519
Bill Schmidt51e79512013-02-20 15:50:31 +00001520 if (EltSize == 1) {
1521 Opc1 = PPC::VSPLTISB;
1522 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001523 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001524 VT = MVT::v16i8;
1525 } else if (EltSize == 2) {
1526 Opc1 = PPC::VSPLTISH;
1527 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001528 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001529 VT = MVT::v8i16;
1530 } else {
1531 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1532 Opc1 = PPC::VSPLTISW;
1533 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001534 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001535 VT = MVT::v4i32;
1536 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001537
1538 if ((Elt & 1) == 0) {
1539 // Elt is even, in the range [-32,-18] + [16,30].
1540 //
1541 // Convert: VADD_SPLAT elt, size
1542 // Into: tmp = VSPLTIS[BHW] elt
1543 // VADDU[BHW]M tmp, tmp
1544 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1545 SDValue EltVal = getI32Imm(Elt >> 1);
1546 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1547 SDValue TmpVal = SDValue(Tmp, 0);
1548 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1549
1550 } else if (Elt > 0) {
1551 // Elt is odd and positive, in the range [17,31].
1552 //
1553 // Convert: VADD_SPLAT elt, size
1554 // Into: tmp1 = VSPLTIS[BHW] elt-16
1555 // tmp2 = VSPLTIS[BHW] -16
1556 // VSUBU[BHW]M tmp1, tmp2
1557 SDValue EltVal = getI32Imm(Elt - 16);
1558 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1559 EltVal = getI32Imm(-16);
1560 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1561 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1562 SDValue(Tmp2, 0));
1563
1564 } else {
1565 // Elt is odd and negative, in the range [-31,-17].
1566 //
1567 // Convert: VADD_SPLAT elt, size
1568 // Into: tmp1 = VSPLTIS[BHW] elt+16
1569 // tmp2 = VSPLTIS[BHW] -16
1570 // VADDU[BHW]M tmp1, tmp2
1571 SDValue EltVal = getI32Imm(Elt + 16);
1572 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1573 EltVal = getI32Imm(-16);
1574 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1575 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1576 SDValue(Tmp2, 0));
1577 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001578 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001579 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001580
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001581 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001582}
1583
Hal Finkel860fa902014-01-02 22:09:39 +00001584/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001585/// on the DAG representation.
1586void PPCDAGToDAGISel::PostprocessISelDAG() {
1587
1588 // Skip peepholes at -O0.
1589 if (TM.getOptLevel() == CodeGenOpt::None)
1590 return;
1591
Hal Finkel940ab932014-02-28 00:27:01 +00001592 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00001593 PeepholeCROps();
Hal Finkel940ab932014-02-28 00:27:01 +00001594}
1595
Hal Finkelb9989152014-02-28 06:11:16 +00001596// Check if all users of this node will become isel where the second operand
1597// is the constant zero. If this is so, and if we can negate the condition,
1598// then we can flip the true and false operands. This will allow the zero to
1599// be folded with the isel so that we don't need to materialize a register
1600// containing zero.
1601bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1602 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00001603 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00001604 return false;
1605
1606 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1607 UI != UE; ++UI) {
1608 SDNode *User = *UI;
1609 if (!User->isMachineOpcode())
1610 return false;
1611 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1612 User->getMachineOpcode() != PPC::SELECT_I8)
1613 return false;
1614
1615 SDNode *Op2 = User->getOperand(2).getNode();
1616 if (!Op2->isMachineOpcode())
1617 return false;
1618
1619 if (Op2->getMachineOpcode() != PPC::LI &&
1620 Op2->getMachineOpcode() != PPC::LI8)
1621 return false;
1622
1623 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1624 if (!C)
1625 return false;
1626
1627 if (!C->isNullValue())
1628 return false;
1629 }
1630
1631 return true;
1632}
1633
1634void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1635 SmallVector<SDNode *, 4> ToReplace;
1636 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1637 UI != UE; ++UI) {
1638 SDNode *User = *UI;
1639 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1640 User->getMachineOpcode() == PPC::SELECT_I8) &&
1641 "Must have all select users");
1642 ToReplace.push_back(User);
1643 }
1644
1645 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1646 UE = ToReplace.end(); UI != UE; ++UI) {
1647 SDNode *User = *UI;
1648 SDNode *ResNode =
1649 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1650 User->getValueType(0), User->getOperand(0),
1651 User->getOperand(2),
1652 User->getOperand(1));
1653
1654 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1655 DEBUG(User->dump(CurDAG));
1656 DEBUG(dbgs() << "\nNew: ");
1657 DEBUG(ResNode->dump(CurDAG));
1658 DEBUG(dbgs() << "\n");
1659
1660 ReplaceUses(User, ResNode);
1661 }
1662}
1663
Eric Christopher02e18042014-05-14 00:31:15 +00001664void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00001665 bool IsModified;
1666 do {
1667 IsModified = false;
1668 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1669 E = CurDAG->allnodes_end(); I != E; ++I) {
1670 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1671 if (!MachineNode || MachineNode->use_empty())
1672 continue;
1673 SDNode *ResNode = MachineNode;
1674
1675 bool Op1Set = false, Op1Unset = false,
1676 Op1Not = false,
1677 Op2Set = false, Op2Unset = false,
1678 Op2Not = false;
1679
1680 unsigned Opcode = MachineNode->getMachineOpcode();
1681 switch (Opcode) {
1682 default: break;
1683 case PPC::CRAND:
1684 case PPC::CRNAND:
1685 case PPC::CROR:
1686 case PPC::CRXOR:
1687 case PPC::CRNOR:
1688 case PPC::CREQV:
1689 case PPC::CRANDC:
1690 case PPC::CRORC: {
1691 SDValue Op = MachineNode->getOperand(1);
1692 if (Op.isMachineOpcode()) {
1693 if (Op.getMachineOpcode() == PPC::CRSET)
1694 Op2Set = true;
1695 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1696 Op2Unset = true;
1697 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1698 Op.getOperand(0) == Op.getOperand(1))
1699 Op2Not = true;
1700 }
1701 } // fallthrough
1702 case PPC::BC:
1703 case PPC::BCn:
1704 case PPC::SELECT_I4:
1705 case PPC::SELECT_I8:
1706 case PPC::SELECT_F4:
1707 case PPC::SELECT_F8:
Bill Schmidt61e65232014-10-22 13:13:40 +00001708 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00001709 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00001710 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00001711 SDValue Op = MachineNode->getOperand(0);
1712 if (Op.isMachineOpcode()) {
1713 if (Op.getMachineOpcode() == PPC::CRSET)
1714 Op1Set = true;
1715 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1716 Op1Unset = true;
1717 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1718 Op.getOperand(0) == Op.getOperand(1))
1719 Op1Not = true;
1720 }
1721 }
1722 break;
1723 }
1724
Hal Finkelb9989152014-02-28 06:11:16 +00001725 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001726 switch (Opcode) {
1727 default: break;
1728 case PPC::CRAND:
1729 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1730 // x & x = x
1731 ResNode = MachineNode->getOperand(0).getNode();
1732 else if (Op1Set)
1733 // 1 & y = y
1734 ResNode = MachineNode->getOperand(1).getNode();
1735 else if (Op2Set)
1736 // x & 1 = x
1737 ResNode = MachineNode->getOperand(0).getNode();
1738 else if (Op1Unset || Op2Unset)
1739 // x & 0 = 0 & y = 0
1740 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1741 MVT::i1);
1742 else if (Op1Not)
1743 // ~x & y = andc(y, x)
1744 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1745 MVT::i1, MachineNode->getOperand(1),
1746 MachineNode->getOperand(0).
1747 getOperand(0));
1748 else if (Op2Not)
1749 // x & ~y = andc(x, y)
1750 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1751 MVT::i1, MachineNode->getOperand(0),
1752 MachineNode->getOperand(1).
1753 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001754 else if (AllUsersSelectZero(MachineNode))
1755 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1756 MVT::i1, MachineNode->getOperand(0),
1757 MachineNode->getOperand(1)),
1758 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001759 break;
1760 case PPC::CRNAND:
1761 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1762 // nand(x, x) -> nor(x, x)
1763 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1764 MVT::i1, MachineNode->getOperand(0),
1765 MachineNode->getOperand(0));
1766 else if (Op1Set)
1767 // nand(1, y) -> nor(y, y)
1768 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1769 MVT::i1, MachineNode->getOperand(1),
1770 MachineNode->getOperand(1));
1771 else if (Op2Set)
1772 // nand(x, 1) -> nor(x, x)
1773 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1774 MVT::i1, MachineNode->getOperand(0),
1775 MachineNode->getOperand(0));
1776 else if (Op1Unset || Op2Unset)
1777 // nand(x, 0) = nand(0, y) = 1
1778 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1779 MVT::i1);
1780 else if (Op1Not)
1781 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1782 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1783 MVT::i1, MachineNode->getOperand(0).
1784 getOperand(0),
1785 MachineNode->getOperand(1));
1786 else if (Op2Not)
1787 // nand(x, ~y) = ~x | y = orc(y, x)
1788 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1789 MVT::i1, MachineNode->getOperand(1).
1790 getOperand(0),
1791 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001792 else if (AllUsersSelectZero(MachineNode))
1793 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1794 MVT::i1, MachineNode->getOperand(0),
1795 MachineNode->getOperand(1)),
1796 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001797 break;
1798 case PPC::CROR:
1799 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1800 // x | x = x
1801 ResNode = MachineNode->getOperand(0).getNode();
1802 else if (Op1Set || Op2Set)
1803 // x | 1 = 1 | y = 1
1804 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1805 MVT::i1);
1806 else if (Op1Unset)
1807 // 0 | y = y
1808 ResNode = MachineNode->getOperand(1).getNode();
1809 else if (Op2Unset)
1810 // x | 0 = x
1811 ResNode = MachineNode->getOperand(0).getNode();
1812 else if (Op1Not)
1813 // ~x | y = orc(y, x)
1814 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1815 MVT::i1, MachineNode->getOperand(1),
1816 MachineNode->getOperand(0).
1817 getOperand(0));
1818 else if (Op2Not)
1819 // x | ~y = orc(x, y)
1820 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1821 MVT::i1, MachineNode->getOperand(0),
1822 MachineNode->getOperand(1).
1823 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001824 else if (AllUsersSelectZero(MachineNode))
1825 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1826 MVT::i1, MachineNode->getOperand(0),
1827 MachineNode->getOperand(1)),
1828 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001829 break;
1830 case PPC::CRXOR:
1831 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1832 // xor(x, x) = 0
1833 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1834 MVT::i1);
1835 else if (Op1Set)
1836 // xor(1, y) -> nor(y, y)
1837 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1838 MVT::i1, MachineNode->getOperand(1),
1839 MachineNode->getOperand(1));
1840 else if (Op2Set)
1841 // xor(x, 1) -> nor(x, x)
1842 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1843 MVT::i1, MachineNode->getOperand(0),
1844 MachineNode->getOperand(0));
1845 else if (Op1Unset)
1846 // xor(0, y) = y
1847 ResNode = MachineNode->getOperand(1).getNode();
1848 else if (Op2Unset)
1849 // xor(x, 0) = x
1850 ResNode = MachineNode->getOperand(0).getNode();
1851 else if (Op1Not)
1852 // xor(~x, y) = eqv(x, y)
1853 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1854 MVT::i1, MachineNode->getOperand(0).
1855 getOperand(0),
1856 MachineNode->getOperand(1));
1857 else if (Op2Not)
1858 // xor(x, ~y) = eqv(x, y)
1859 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1860 MVT::i1, MachineNode->getOperand(0),
1861 MachineNode->getOperand(1).
1862 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001863 else if (AllUsersSelectZero(MachineNode))
1864 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1865 MVT::i1, MachineNode->getOperand(0),
1866 MachineNode->getOperand(1)),
1867 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001868 break;
1869 case PPC::CRNOR:
1870 if (Op1Set || Op2Set)
1871 // nor(1, y) -> 0
1872 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1873 MVT::i1);
1874 else if (Op1Unset)
1875 // nor(0, y) = ~y -> nor(y, y)
1876 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1877 MVT::i1, MachineNode->getOperand(1),
1878 MachineNode->getOperand(1));
1879 else if (Op2Unset)
1880 // nor(x, 0) = ~x
1881 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1882 MVT::i1, MachineNode->getOperand(0),
1883 MachineNode->getOperand(0));
1884 else if (Op1Not)
1885 // nor(~x, y) = andc(x, y)
1886 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1887 MVT::i1, MachineNode->getOperand(0).
1888 getOperand(0),
1889 MachineNode->getOperand(1));
1890 else if (Op2Not)
1891 // nor(x, ~y) = andc(y, x)
1892 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1893 MVT::i1, MachineNode->getOperand(1).
1894 getOperand(0),
1895 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001896 else if (AllUsersSelectZero(MachineNode))
1897 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1898 MVT::i1, MachineNode->getOperand(0),
1899 MachineNode->getOperand(1)),
1900 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001901 break;
1902 case PPC::CREQV:
1903 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1904 // eqv(x, x) = 1
1905 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1906 MVT::i1);
1907 else if (Op1Set)
1908 // eqv(1, y) = y
1909 ResNode = MachineNode->getOperand(1).getNode();
1910 else if (Op2Set)
1911 // eqv(x, 1) = x
1912 ResNode = MachineNode->getOperand(0).getNode();
1913 else if (Op1Unset)
1914 // eqv(0, y) = ~y -> nor(y, y)
1915 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1916 MVT::i1, MachineNode->getOperand(1),
1917 MachineNode->getOperand(1));
1918 else if (Op2Unset)
1919 // eqv(x, 0) = ~x
1920 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1921 MVT::i1, MachineNode->getOperand(0),
1922 MachineNode->getOperand(0));
1923 else if (Op1Not)
1924 // eqv(~x, y) = xor(x, y)
1925 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1926 MVT::i1, MachineNode->getOperand(0).
1927 getOperand(0),
1928 MachineNode->getOperand(1));
1929 else if (Op2Not)
1930 // eqv(x, ~y) = xor(x, y)
1931 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1932 MVT::i1, MachineNode->getOperand(0),
1933 MachineNode->getOperand(1).
1934 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001935 else if (AllUsersSelectZero(MachineNode))
1936 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1937 MVT::i1, MachineNode->getOperand(0),
1938 MachineNode->getOperand(1)),
1939 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001940 break;
1941 case PPC::CRANDC:
1942 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1943 // andc(x, x) = 0
1944 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1945 MVT::i1);
1946 else if (Op1Set)
1947 // andc(1, y) = ~y
1948 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1949 MVT::i1, MachineNode->getOperand(1),
1950 MachineNode->getOperand(1));
1951 else if (Op1Unset || Op2Set)
1952 // andc(0, y) = andc(x, 1) = 0
1953 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1954 MVT::i1);
1955 else if (Op2Unset)
1956 // andc(x, 0) = x
1957 ResNode = MachineNode->getOperand(0).getNode();
1958 else if (Op1Not)
1959 // andc(~x, y) = ~(x | y) = nor(x, y)
1960 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1961 MVT::i1, MachineNode->getOperand(0).
1962 getOperand(0),
1963 MachineNode->getOperand(1));
1964 else if (Op2Not)
1965 // andc(x, ~y) = x & y
1966 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1967 MVT::i1, MachineNode->getOperand(0),
1968 MachineNode->getOperand(1).
1969 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001970 else if (AllUsersSelectZero(MachineNode))
1971 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1972 MVT::i1, MachineNode->getOperand(1),
1973 MachineNode->getOperand(0)),
1974 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001975 break;
1976 case PPC::CRORC:
1977 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1978 // orc(x, x) = 1
1979 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1980 MVT::i1);
1981 else if (Op1Set || Op2Unset)
1982 // orc(1, y) = orc(x, 0) = 1
1983 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1984 MVT::i1);
1985 else if (Op2Set)
1986 // orc(x, 1) = x
1987 ResNode = MachineNode->getOperand(0).getNode();
1988 else if (Op1Unset)
1989 // orc(0, y) = ~y
1990 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1991 MVT::i1, MachineNode->getOperand(1),
1992 MachineNode->getOperand(1));
1993 else if (Op1Not)
1994 // orc(~x, y) = ~(x & y) = nand(x, y)
1995 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1996 MVT::i1, MachineNode->getOperand(0).
1997 getOperand(0),
1998 MachineNode->getOperand(1));
1999 else if (Op2Not)
2000 // orc(x, ~y) = x | y
2001 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
2002 MVT::i1, MachineNode->getOperand(0),
2003 MachineNode->getOperand(1).
2004 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00002005 else if (AllUsersSelectZero(MachineNode))
2006 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
2007 MVT::i1, MachineNode->getOperand(1),
2008 MachineNode->getOperand(0)),
2009 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00002010 break;
2011 case PPC::SELECT_I4:
2012 case PPC::SELECT_I8:
2013 case PPC::SELECT_F4:
2014 case PPC::SELECT_F8:
2015 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002016 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00002017 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00002018 if (Op1Set)
2019 ResNode = MachineNode->getOperand(1).getNode();
2020 else if (Op1Unset)
2021 ResNode = MachineNode->getOperand(2).getNode();
2022 else if (Op1Not)
2023 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2024 SDLoc(MachineNode),
2025 MachineNode->getValueType(0),
2026 MachineNode->getOperand(0).
2027 getOperand(0),
2028 MachineNode->getOperand(2),
2029 MachineNode->getOperand(1));
2030 break;
2031 case PPC::BC:
2032 case PPC::BCn:
2033 if (Op1Not)
2034 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2035 PPC::BC,
2036 SDLoc(MachineNode),
2037 MVT::Other,
2038 MachineNode->getOperand(0).
2039 getOperand(0),
2040 MachineNode->getOperand(1),
2041 MachineNode->getOperand(2));
2042 // FIXME: Handle Op1Set, Op1Unset here too.
2043 break;
2044 }
2045
Hal Finkelb9989152014-02-28 06:11:16 +00002046 // If we're inverting this node because it is used only by selects that
2047 // we'd like to swap, then swap the selects before the node replacement.
2048 if (SelectSwap)
2049 SwapAllSelectUsers(MachineNode);
2050
Hal Finkel940ab932014-02-28 00:27:01 +00002051 if (ResNode != MachineNode) {
2052 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2053 DEBUG(MachineNode->dump(CurDAG));
2054 DEBUG(dbgs() << "\nNew: ");
2055 DEBUG(ResNode->dump(CurDAG));
2056 DEBUG(dbgs() << "\n");
2057
2058 ReplaceUses(MachineNode, ResNode);
2059 IsModified = true;
2060 }
2061 }
2062 if (IsModified)
2063 CurDAG->RemoveDeadNodes();
2064 } while (IsModified);
2065}
2066
2067void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002068 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002069 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002070 return;
2071
2072 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2073 ++Position;
2074
2075 while (Position != CurDAG->allnodes_begin()) {
2076 SDNode *N = --Position;
2077 // Skip dead nodes and any non-machine opcodes.
2078 if (N->use_empty() || !N->isMachineOpcode())
2079 continue;
2080
2081 unsigned FirstOp;
2082 unsigned StorageOpcode = N->getMachineOpcode();
2083
2084 switch (StorageOpcode) {
2085 default: continue;
2086
2087 case PPC::LBZ:
2088 case PPC::LBZ8:
2089 case PPC::LD:
2090 case PPC::LFD:
2091 case PPC::LFS:
2092 case PPC::LHA:
2093 case PPC::LHA8:
2094 case PPC::LHZ:
2095 case PPC::LHZ8:
2096 case PPC::LWA:
2097 case PPC::LWZ:
2098 case PPC::LWZ8:
2099 FirstOp = 0;
2100 break;
2101
2102 case PPC::STB:
2103 case PPC::STB8:
2104 case PPC::STD:
2105 case PPC::STFD:
2106 case PPC::STFS:
2107 case PPC::STH:
2108 case PPC::STH8:
2109 case PPC::STW:
2110 case PPC::STW8:
2111 FirstOp = 1;
2112 break;
2113 }
2114
2115 // If this is a load or store with a zero offset, we may be able to
2116 // fold an add-immediate into the memory operation.
2117 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2118 N->getConstantOperandVal(FirstOp) != 0)
2119 continue;
2120
2121 SDValue Base = N->getOperand(FirstOp + 1);
2122 if (!Base.isMachineOpcode())
2123 continue;
2124
2125 unsigned Flags = 0;
2126 bool ReplaceFlags = true;
2127
2128 // When the feeding operation is an add-immediate of some sort,
2129 // determine whether we need to add relocation information to the
2130 // target flags on the immediate operand when we fold it into the
2131 // load instruction.
2132 //
2133 // For something like ADDItocL, the relocation information is
2134 // inferred from the opcode; when we process it in the AsmPrinter,
2135 // we add the necessary relocation there. A load, though, can receive
2136 // relocation from various flavors of ADDIxxx, so we need to carry
2137 // the relocation information in the target flags.
2138 switch (Base.getMachineOpcode()) {
2139 default: continue;
2140
2141 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002142 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002143 // In some cases (such as TLS) the relocation information
2144 // is already in place on the operand, so copying the operand
2145 // is sufficient.
2146 ReplaceFlags = false;
2147 // For these cases, the immediate may not be divisible by 4, in
2148 // which case the fold is illegal for DS-form instructions. (The
2149 // other cases provide aligned addresses and are always safe.)
2150 if ((StorageOpcode == PPC::LWA ||
2151 StorageOpcode == PPC::LD ||
2152 StorageOpcode == PPC::STD) &&
2153 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2154 Base.getConstantOperandVal(1) % 4 != 0))
2155 continue;
2156 break;
2157 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002158 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002159 break;
2160 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002161 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002162 break;
2163 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002164 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002165 break;
2166 }
2167
2168 // We found an opportunity. Reverse the operands from the add
2169 // immediate and substitute them into the load or store. If
2170 // needed, update the target flags for the immediate operand to
2171 // reflect the necessary relocation information.
2172 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2173 DEBUG(Base->dump(CurDAG));
2174 DEBUG(dbgs() << "\nN: ");
2175 DEBUG(N->dump(CurDAG));
2176 DEBUG(dbgs() << "\n");
2177
2178 SDValue ImmOpnd = Base.getOperand(1);
2179
2180 // If the relocation information isn't already present on the
2181 // immediate operand, add it now.
2182 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002183 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002184 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002185 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002186 // We can't perform this optimization for data whose alignment
2187 // is insufficient for the instruction encoding.
2188 if (GV->getAlignment() < 4 &&
2189 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2190 StorageOpcode == PPC::LWA)) {
2191 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2192 continue;
2193 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002194 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002195 } else if (ConstantPoolSDNode *CP =
2196 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002197 const Constant *C = CP->getConstVal();
2198 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2199 CP->getAlignment(),
2200 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002201 }
2202 }
2203
2204 if (FirstOp == 1) // Store
2205 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2206 Base.getOperand(0), N->getOperand(3));
2207 else // Load
2208 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2209 N->getOperand(2));
2210
2211 // The add-immediate may now be dead, in which case remove it.
2212 if (Base.getNode()->use_empty())
2213 CurDAG->RemoveDeadNode(Base.getNode());
2214 }
2215}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002216
Chris Lattnerb055c872006-06-10 01:15:02 +00002217
Andrew Trickc416ba62010-12-24 04:28:06 +00002218/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002219/// PowerPC-specific DAG, ready for instruction scheduling.
2220///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002221FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002222 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002223}
2224
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002225static void initializePassOnce(PassRegistry &Registry) {
2226 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00002227 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2228 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002229 Registry.registerPass(*PI, true);
2230}
2231
2232void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2233 CALL_ONCE_INITIALIZATION(initializePassOnce);
2234}
2235