blob: 37b3137bd342e03e48bd1b99f5ef89fcf7ecaf5f [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000018#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000025#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/GlobalVariable.h"
28#include "llvm/IR/Intrinsics.h"
Hal Finkel940ab932014-02-28 00:27:01 +000029#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000030#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000035using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "ppc-codegen"
38
Hal Finkel940ab932014-02-28 00:27:01 +000039// FIXME: Remove this once the bug has been fixed!
40cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
41cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
42
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000043namespace llvm {
44 void initializePPCDAGToDAGISelPass(PassRegistry&);
45}
46
Chris Lattner43ff01e2005-08-17 19:33:03 +000047namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000048 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000049 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000050 /// instructions for SelectionDAG operations.
51 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000052 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000053 const PPCTargetMachine &TM;
54 const PPCTargetLowering &PPCLowering;
Evan Chengec271b12007-10-23 06:42:42 +000055 const PPCSubtarget &PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000056 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000057 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000058 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Dan Gohman619ef482009-01-15 19:20:50 +000059 : SelectionDAGISel(tm), TM(tm),
Evan Chengec271b12007-10-23 06:42:42 +000060 PPCLowering(*TM.getTargetLowering()),
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000061 PPCSubTarget(*TM.getSubtargetImpl()) {
62 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
63 }
Andrew Trickc416ba62010-12-24 04:28:06 +000064
Craig Topper0d3fa922014-04-29 07:57:37 +000065 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000066 // Make sure we re-emit a set of the global base reg if necessary
67 GlobalBaseReg = 0;
Dan Gohman5ea74d52009-07-31 18:16:33 +000068 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000069
Bill Schmidt38d94582012-10-10 20:54:15 +000070 if (!PPCSubTarget.isSVR4ABI())
71 InsertVRSaveCode(MF);
72
Chris Lattner1678a6c2006-03-16 18:25:23 +000073 return true;
Chris Lattner45640392005-08-19 22:38:53 +000074 }
Andrew Trickc416ba62010-12-24 04:28:06 +000075
Craig Topper0d3fa922014-04-29 07:57:37 +000076 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000077
Chris Lattner43ff01e2005-08-17 19:33:03 +000078 /// getI32Imm - Return a target constant with the specified value, of type
79 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000080 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000081 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000082 }
Chris Lattner45640392005-08-19 22:38:53 +000083
Chris Lattner97b3da12006-06-27 00:04:13 +000084 /// getI64Imm - Return a target constant with the specified value, of type
85 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000086 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000087 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000088 }
Andrew Trickc416ba62010-12-24 04:28:06 +000089
Chris Lattner97b3da12006-06-27 00:04:13 +000090 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000091 inline SDValue getSmallIPtrImm(unsigned Imm) {
Chris Lattner97b3da12006-06-27 00:04:13 +000092 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
93 }
Andrew Trickc416ba62010-12-24 04:28:06 +000094
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000095 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +000096 /// with any number of 0s on either side. The 1s are allowed to wrap from
97 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
98 /// 0x0F0F0000 is not, since all 1s are not contiguous.
99 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
100
101
102 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
103 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000104 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000105 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000106
Chris Lattner45640392005-08-19 22:38:53 +0000107 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
108 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000109 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000110
Chris Lattner43ff01e2005-08-17 19:33:03 +0000111 // Select - Convert the specified operand from a target-independent to a
112 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000113 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000114
Nate Begeman93c4bc62005-08-19 00:38:14 +0000115 SDNode *SelectBitfieldInsert(SDNode *N);
116
Chris Lattner2a1823d2005-08-21 18:50:37 +0000117 /// SelectCC - Select a comparison of the specified values with the
118 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000119 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000120
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000121 /// SelectAddrImm - Returns true if the address N can be represented by
122 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000123 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000124 SDValue &Base) {
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000125 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000126 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000127
Chris Lattner6f5840c2006-11-16 00:41:37 +0000128 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000129 /// immediate field. Note that the operand at this point is already the
130 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000131 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000132 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000133 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000134 Out = N;
135 return true;
136 }
137
138 return false;
139 }
140
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000141 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
142 /// represented as an indexed [r+r] operation. Returns false if it can
143 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000144 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000145 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
146 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000147
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000148 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
149 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000150 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Chris Lattnera801fced2006-11-08 02:15:41 +0000151 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
152 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000153
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000154 /// SelectAddrImmX4 - Returns true if the address N can be represented by
155 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
156 /// Suitable for use by STD and friends.
157 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
158 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000159 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000160
Hal Finkel756810f2013-03-21 21:37:52 +0000161 // Select an address into a single register.
162 bool SelectAddr(SDValue N, SDValue &Base) {
163 Base = N;
164 return true;
165 }
166
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000167 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000168 /// inline asm expressions. It is always correct to compute the value into
169 /// a register. The case of adding a (possibly relocatable) constant to a
170 /// register can be improved, but it is wrong to substitute Reg+Reg for
171 /// Reg in an asm, because the load or store opcode would have to change.
Craig Topper0d3fa922014-04-29 07:57:37 +0000172 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
173 char ConstraintCode,
174 std::vector<SDValue> &OutOps) override {
Dale Johannesen4a50e682009-08-18 00:18:39 +0000175 OutOps.push_back(Op);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000176 return false;
177 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000178
Dan Gohman5ea74d52009-07-31 18:16:33 +0000179 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000180
Craig Topper0d3fa922014-04-29 07:57:37 +0000181 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000182 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000183 }
184
Chris Lattner03e08ee2005-09-13 22:03:06 +0000185// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000186#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000187
Chris Lattner259e6c72005-10-06 18:45:51 +0000188private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000189 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000190
191 void PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +0000192 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000193
194 bool AllUsersSelectZero(SDNode *N);
195 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000196 };
197}
198
Chris Lattner1678a6c2006-03-16 18:25:23 +0000199/// InsertVRSaveCode - Once the entire function has been instruction selected,
200/// all virtual registers are created and all machine instructions are built,
201/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000202void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000203 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000204 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000205 //
Dan Gohman4a618822010-02-10 16:03:48 +0000206 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000207 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000208 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000209 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
210 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
211 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000212 HasVectorVReg = true;
213 break;
214 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000215 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000216 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000217
Chris Lattner02e2c182006-03-13 21:52:10 +0000218 // If we have a vector register, we want to emit code into the entry and exit
219 // blocks to save and restore the VRSAVE register. We do this here (instead
220 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
221 //
222 // 1. This (trivially) reduces the load on the register allocator, by not
223 // having to represent the live range of the VRSAVE register.
224 // 2. This (more significantly) allows us to create a temporary virtual
225 // register to hold the saved VRSAVE value, allowing this temporary to be
226 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000227
228 // Create two vregs - one to hold the VRSAVE register that is live-in to the
229 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000230 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
231 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000232
Evan Cheng20350c42006-11-27 23:37:22 +0000233 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000234 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000235 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000236 // Emit the following code into the entry block:
237 // InVRSAVE = MFVRSAVE
238 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
239 // MTVRSAVE UpdatedVRSAVE
240 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000241 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
242 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000243 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000244 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000245
Chris Lattner1678a6c2006-03-16 18:25:23 +0000246 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000247 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000248 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000249 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000250
Chris Lattner1678a6c2006-03-16 18:25:23 +0000251 // Skip over all terminator instructions, which are part of the return
252 // sequence.
253 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000254 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000255 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000256
Chris Lattner1678a6c2006-03-16 18:25:23 +0000257 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000258 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000259 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000260 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000261}
Chris Lattner8ae95252005-09-03 01:17:22 +0000262
Chris Lattner1678a6c2006-03-16 18:25:23 +0000263
Chris Lattner45640392005-08-19 22:38:53 +0000264/// getGlobalBaseReg - Output the instructions required to put the
265/// base address to use for accessing globals into a register.
266///
Evan Cheng61413a32006-08-26 05:34:46 +0000267SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000268 if (!GlobalBaseReg) {
Evan Cheng20350c42006-11-27 23:37:22 +0000269 const TargetInstrInfo &TII = *TM.getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000270 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000271 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000272 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000273 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000274
Owen Anderson9f944592009-08-11 20:47:22 +0000275 if (PPCLowering.getPointerTy() == MVT::i32) {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000276 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000277 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000278 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000279 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000280 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000281 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000282 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000283 }
Chris Lattner45640392005-08-19 22:38:53 +0000284 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000285 return CurDAG->getRegister(GlobalBaseReg,
286 PPCLowering.getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000287}
288
289/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
290/// or 64-bit immediate, and if the value can be accurately represented as a
291/// sign extension from a 16-bit value. If so, this returns true and the
292/// immediate.
293static bool isIntS16Immediate(SDNode *N, short &Imm) {
294 if (N->getOpcode() != ISD::Constant)
295 return false;
296
Dan Gohmaneffb8942008-09-12 16:56:44 +0000297 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000298 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000299 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000300 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000301 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000302}
303
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000304static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000305 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000306}
307
308
Chris Lattner97b3da12006-06-27 00:04:13 +0000309/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
310/// operand. If so Imm will receive the 32-bit value.
311static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000312 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000313 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000314 return true;
315 }
316 return false;
317}
318
Chris Lattner97b3da12006-06-27 00:04:13 +0000319/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
320/// operand. If so Imm will receive the 64-bit value.
321static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000322 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000323 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000324 return true;
325 }
326 return false;
327}
328
329// isInt32Immediate - This method tests to see if a constant operand.
330// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000331static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000332 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000333}
334
335
336// isOpcWithIntImmediate - This method tests to see if the node is a specific
337// opcode and that it has a immediate integer right operand.
338// If so Imm will receive the 32 bit value.
339static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000340 return N->getOpcode() == Opc
341 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000342}
343
Nate Begemand31efd12006-09-22 05:01:56 +0000344bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000345 if (!Val)
346 return false;
347
Nate Begemanb3821a32005-08-18 07:30:46 +0000348 if (isShiftedMask_32(Val)) {
349 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000350 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000351 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000352 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000353 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000354 } else {
355 Val = ~Val; // invert mask
356 if (isShiftedMask_32(Val)) {
357 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000358 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000359 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000360 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000361 return true;
362 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000363 }
364 // no run present
365 return false;
366}
367
Andrew Trickc416ba62010-12-24 04:28:06 +0000368bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
369 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000370 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000371 // Don't even go down this path for i64, since different logic will be
372 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000373 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000374 return false;
375
Nate Begemanb3821a32005-08-18 07:30:46 +0000376 unsigned Shift = 32;
377 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
378 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000379 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000380 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000381 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000382
Nate Begemanb3821a32005-08-18 07:30:46 +0000383 if (Opcode == ISD::SHL) {
384 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000385 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000386 // determine which bits are made indeterminant by shift
387 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000388 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000389 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000390 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000391 // determine which bits are made indeterminant by shift
392 Indeterminant = ~(0xFFFFFFFFu >> Shift);
393 // adjust for the left rotate
394 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000395 } else if (Opcode == ISD::ROTL) {
396 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000397 } else {
398 return false;
399 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000400
Nate Begemanb3821a32005-08-18 07:30:46 +0000401 // if the mask doesn't intersect any Indeterminant bits
402 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000403 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000404 // make sure the mask is still a mask (wrap arounds may not be)
405 return isRunOfOnes(Mask, MB, ME);
406 }
407 return false;
408}
409
Nate Begeman93c4bc62005-08-19 00:38:14 +0000410/// SelectBitfieldInsert - turn an or of two masked values into
411/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000412SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000413 SDValue Op0 = N->getOperand(0);
414 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000415 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000416
Dan Gohmanf19609a2008-02-27 01:23:58 +0000417 APInt LKZ, LKO, RKZ, RKO;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +0000418 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
419 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000420
Dan Gohmanf19609a2008-02-27 01:23:58 +0000421 unsigned TargetMask = LKZ.getZExtValue();
422 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000423
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000424 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
425 unsigned Op0Opc = Op0.getOpcode();
426 unsigned Op1Opc = Op1.getOpcode();
427 unsigned Value, SH = 0;
428 TargetMask = ~TargetMask;
429 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000430
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000431 // If the LHS has a foldable shift and the RHS does not, then swap it to the
432 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000433 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
434 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
435 Op0.getOperand(0).getOpcode() == ISD::SRL) {
436 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
437 Op1.getOperand(0).getOpcode() != ISD::SRL) {
438 std::swap(Op0, Op1);
439 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000440 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000441 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000442 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000443 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
444 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
445 Op1.getOperand(0).getOpcode() != ISD::SRL) {
446 std::swap(Op0, Op1);
447 std::swap(Op0Opc, Op1Opc);
448 std::swap(TargetMask, InsertMask);
449 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000450 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000451
Nate Begeman1333cea2006-05-07 00:23:38 +0000452 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000453 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000454 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000455
456 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000457 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000458 Op1 = Op1.getOperand(0);
459 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
460 }
461 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000462 // The AND mask might not be a constant, and we need to make sure that
463 // if we're going to fold the masking with the insert, all bits not
464 // know to be zero in the mask are known to be one.
465 APInt MKZ, MKO;
466 CurDAG->ComputeMaskedBits(Op1.getOperand(1), MKZ, MKO);
467 bool CanFoldMask = InsertMask == MKO.getZExtValue();
468
Nate Begeman1333cea2006-05-07 00:23:38 +0000469 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000470 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000471 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000472 // Note that Value must be in range here (less than 32) because
473 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000474 Op1 = Op1.getOperand(0).getOperand(0);
475 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000476 }
477 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000478
Chris Lattnera2963392006-05-12 16:29:37 +0000479 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000480 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000481 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000482 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000483 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000484 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000485 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000486}
487
Chris Lattner2a1823d2005-08-21 18:50:37 +0000488/// SelectCC - Select a comparison of the specified values with the specified
489/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000490SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000491 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000492 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +0000493 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +0000494
Owen Anderson9f944592009-08-11 20:47:22 +0000495 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +0000496 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +0000497 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
498 if (isInt32Immediate(RHS, Imm)) {
499 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000500 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000501 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
502 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000503 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000504 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000505 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
506 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000507
Chris Lattneraa3926b2006-09-20 04:25:47 +0000508 // For non-equality comparisons, the default code would materialize the
509 // constant, then compare against it, like this:
510 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000511 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +0000512 // cmpw cr0, r3, r2
513 // Since we are just comparing for equality, we can emit this instead:
514 // xoris r0,r3,0x1234
515 // cmplwi cr0,r0,0x5678
516 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +0000517 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
518 getI32Imm(Imm >> 16)), 0);
519 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
520 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +0000521 }
522 Opc = PPC::CMPLW;
523 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000524 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000525 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
526 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000527 Opc = PPC::CMPLW;
528 } else {
529 short SImm;
530 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000531 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
532 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000533 0);
534 Opc = PPC::CMPW;
535 }
Owen Anderson9f944592009-08-11 20:47:22 +0000536 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000537 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000538 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000539 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000540 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000541 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000542 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
543 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000544 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000545 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000546 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
547 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000548
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000549 // For non-equality comparisons, the default code would materialize the
550 // constant, then compare against it, like this:
551 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +0000552 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000553 // cmpd cr0, r3, r2
554 // Since we are just comparing for equality, we can emit this instead:
555 // xoris r0,r3,0x1234
556 // cmpldi cr0,r0,0x5678
557 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +0000558 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000559 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
560 getI64Imm(Imm >> 16)), 0);
561 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
562 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +0000563 }
564 }
565 Opc = PPC::CMPLD;
566 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +0000567 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000568 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
569 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +0000570 Opc = PPC::CMPLD;
571 } else {
572 short SImm;
573 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +0000574 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
575 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +0000576 0);
577 Opc = PPC::CMPD;
578 }
Owen Anderson9f944592009-08-11 20:47:22 +0000579 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +0000580 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000581 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000582 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Hal Finkel27774d92014-03-13 07:58:58 +0000583 Opc = PPCSubTarget.hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000584 }
Dan Gohman32f71d72009-09-25 18:54:59 +0000585 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000586}
587
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000588static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +0000589 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +0000590 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000591 case ISD::SETONE:
592 case ISD::SETOLE:
593 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000594 llvm_unreachable("Should be lowered by legalize!");
595 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000596 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000597 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +0000598 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000599 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000600 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000601 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000602 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000603 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000604 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000605 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000606 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000607 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +0000608 case ISD::SETO: return PPC::PRED_NU;
609 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +0000610 // These two are invalid for floating point. Assume we have int.
611 case ISD::SETULT: return PPC::PRED_LT;
612 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +0000613 }
Chris Lattner2a1823d2005-08-21 18:50:37 +0000614}
615
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000616/// getCRIdxForSetCC - Return the index of the condition register field
617/// associated with the SetCC condition, and whether or not the field is
618/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +0000619static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +0000620 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000621 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000622 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +0000623 case ISD::SETOLT:
624 case ISD::SETLT: return 0; // Bit #0 = SETOLT
625 case ISD::SETOGT:
626 case ISD::SETGT: return 1; // Bit #1 = SETOGT
627 case ISD::SETOEQ:
628 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
629 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000630 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000631 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000632 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000633 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +0000634 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +0000635 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
636 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +0000637 case ISD::SETUEQ:
638 case ISD::SETOGE:
639 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +0000640 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000641 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +0000642 // These are invalid for floating point. Assume integer.
643 case ISD::SETULT: return 0;
644 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000645 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +0000646}
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000647
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000648// getVCmpInst: return the vector compare instruction for the specified
649// vector type and condition code. Since this is for altivec specific code,
650// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000651static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC,
652 bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000653 switch (CC) {
654 case ISD::SETEQ:
655 case ISD::SETUEQ:
656 case ISD::SETNE:
657 case ISD::SETUNE:
658 if (VecVT == MVT::v16i8)
659 return PPC::VCMPEQUB;
660 else if (VecVT == MVT::v8i16)
661 return PPC::VCMPEQUH;
662 else if (VecVT == MVT::v4i32)
663 return PPC::VCMPEQUW;
664 // v4f32 != v4f32 could be translate to unordered not equal
665 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000666 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
667 else if (VecVT == MVT::v2f64)
668 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000669 break;
670 case ISD::SETLT:
671 case ISD::SETGT:
672 case ISD::SETLE:
673 case ISD::SETGE:
674 if (VecVT == MVT::v16i8)
675 return PPC::VCMPGTSB;
676 else if (VecVT == MVT::v8i16)
677 return PPC::VCMPGTSH;
678 else if (VecVT == MVT::v4i32)
679 return PPC::VCMPGTSW;
680 else if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000681 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
682 else if (VecVT == MVT::v2f64)
683 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000684 break;
685 case ISD::SETULT:
686 case ISD::SETUGT:
687 case ISD::SETUGE:
688 case ISD::SETULE:
689 if (VecVT == MVT::v16i8)
690 return PPC::VCMPGTUB;
691 else if (VecVT == MVT::v8i16)
692 return PPC::VCMPGTUH;
693 else if (VecVT == MVT::v4i32)
694 return PPC::VCMPGTUW;
695 break;
696 case ISD::SETOEQ:
697 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000698 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
699 else if (VecVT == MVT::v2f64)
700 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000701 break;
702 case ISD::SETOLT:
703 case ISD::SETOGT:
704 case ISD::SETOLE:
705 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000706 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
707 else if (VecVT == MVT::v2f64)
708 return PPC::XVCMPGTDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000709 break;
710 case ISD::SETOGE:
711 if (VecVT == MVT::v4f32)
Hal Finkel27774d92014-03-13 07:58:58 +0000712 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
713 else if (VecVT == MVT::v2f64)
714 return PPC::XVCMPGEDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000715 break;
716 default:
717 break;
718 }
719 llvm_unreachable("Invalid integer vector compare condition");
720}
721
722// getVCmpEQInst: return the equal compare instruction for the specified vector
723// type. Since this is for altivec specific code, only support the altivec
724// types (v16i8, v8i16, v4i32, and v4f32).
Hal Finkel27774d92014-03-13 07:58:58 +0000725static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000726 switch (VecVT) {
727 case MVT::v16i8:
728 return PPC::VCMPEQUB;
729 case MVT::v8i16:
730 return PPC::VCMPEQUH;
731 case MVT::v4i32:
732 return PPC::VCMPEQUW;
733 case MVT::v4f32:
Hal Finkel27774d92014-03-13 07:58:58 +0000734 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
735 case MVT::v2f64:
736 return PPC::XVCMPEQDP;
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000737 default:
738 llvm_unreachable("Invalid integer vector compare condition");
739 }
740}
741
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000742SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000743 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +0000744 unsigned Imm;
745 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +0000746 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
747 bool isPPC64 = (PtrVT == MVT::i64);
748
Hal Finkel940ab932014-02-28 00:27:01 +0000749 if (!PPCSubTarget.useCRBits() &&
750 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +0000751 // We can codegen setcc op, imm very efficiently compared to a brcond.
752 // Check for those cases here.
753 // setcc op, 0
754 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000755 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000756 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000757 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +0000758 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000759 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000760 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000761 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000762 }
Chris Lattnere2969492005-10-21 21:17:10 +0000763 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000764 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000765 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000766 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000767 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000768 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000769 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000770 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000771 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000772 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000773 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +0000774 }
Chris Lattnere2969492005-10-21 21:17:10 +0000775 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000776 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +0000777 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
778 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000779 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000780 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000781 }
782 }
Chris Lattner491b8292005-10-06 19:03:35 +0000783 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000784 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +0000785 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +0000786 default: break;
787 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +0000788 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000789 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000790 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000791 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
792 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +0000793 MVT::i32,
794 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000795 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000796 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +0000797 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +0000798 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000799 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000800 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +0000801 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000802 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +0000803 }
Chris Lattnere2969492005-10-21 21:17:10 +0000804 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +0000805 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
806 getI32Imm(1)), 0);
807 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
808 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000809 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +0000810 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +0000811 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000812 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000813 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +0000814 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +0000815 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000816 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000817 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +0000818 }
Evan Chengc3acfc02006-08-27 08:14:06 +0000819 }
Chris Lattner491b8292005-10-06 19:03:35 +0000820 }
821 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000822
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000823 SDValue LHS = N->getOperand(0);
824 SDValue RHS = N->getOperand(1);
825
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000826 // Altivec Vector compare instructions do not set any CR register by default and
827 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000828 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000829 EVT VecVT = LHS.getValueType();
830 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
Hal Finkel27774d92014-03-13 07:58:58 +0000831 unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000832
833 switch (CC) {
834 case ISD::SETEQ:
835 case ISD::SETOEQ:
836 case ISD::SETUEQ:
837 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
838 case ISD::SETNE:
839 case ISD::SETONE:
840 case ISD::SETUNE: {
841 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000842 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLNOR :
843 PPC::VNOR,
844 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000845 }
846 case ISD::SETLT:
847 case ISD::SETOLT:
848 case ISD::SETULT:
849 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
850 case ISD::SETGT:
851 case ISD::SETOGT:
852 case ISD::SETUGT:
853 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
854 case ISD::SETGE:
855 case ISD::SETOGE:
856 case ISD::SETUGE: {
857 // Small optimization: Altivec provides a 'Vector Compare Greater Than
858 // or Equal To' instruction (vcmpgefp), so in this case there is no
859 // need for extra logic for the equal compare.
860 if (VecVT.getSimpleVT().isFloatingPoint()) {
861 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
862 } else {
863 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel27774d92014-03-13 07:58:58 +0000864 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000865 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000866 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
867 PPC::VOR,
868 VecVT, VCmpGT, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000869 }
870 }
871 case ISD::SETLE:
872 case ISD::SETOLE:
873 case ISD::SETULE: {
874 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
Hal Finkel27774d92014-03-13 07:58:58 +0000875 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000876 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
Hal Finkel732f0f72014-03-26 12:49:28 +0000877 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
878 PPC::VOR,
879 VecVT, VCmpLE, VCmpEQ);
Adhemerval Zanella56775e02012-10-30 13:50:19 +0000880 }
881 default:
882 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
883 }
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000884 }
885
Hal Finkel940ab932014-02-28 00:27:01 +0000886 if (PPCSubTarget.useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +0000887 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +0000888
Chris Lattner491b8292005-10-06 19:03:35 +0000889 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +0000890 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000891 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000892 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +0000893
Chris Lattner491b8292005-10-06 19:03:35 +0000894 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +0000895 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +0000896
Craig Topper062a2ba2014-04-25 05:30:21 +0000897 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +0000898 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +0000899 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +0000900
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000901 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
902 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +0000903
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000904 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +0000905 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +0000906 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +0000907 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +0000908
909 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000910 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +0000911 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +0000912 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +0000913}
Chris Lattner502a3692005-10-06 18:56:10 +0000914
Chris Lattner318622f2005-10-06 19:07:45 +0000915
Chris Lattner43ff01e2005-08-17 19:33:03 +0000916// Select - Convert the specified operand from a target-independent to a
917// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000918SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000919 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000920 if (N->isMachineOpcode()) {
921 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000922 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000923 }
Chris Lattner08c319f2005-09-29 00:59:32 +0000924
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
Chris Lattner57693112005-09-28 22:50:24 +00001029 case ISD::SDIV: {
Nate Begeman4dd38312005-10-21 00:02:42 +00001030 // FIXME: since this depends on the setting of the carry flag from the srawi
1031 // we should really be making notes about that for the scheduler.
Andrew Trickc416ba62010-12-24 04:28:06 +00001032 // FIXME: It sure would be nice if we could cheaply recognize the
Nate Begeman4dd38312005-10-21 00:02:42 +00001033 // srl/add/sra pattern the dag combiner will generate for this as
1034 // sra/addze rather than having to handle sdiv ourselves. oh well.
Chris Lattnerdc664572005-08-25 17:50:06 +00001035 unsigned Imm;
Chris Lattner97b3da12006-06-27 00:04:13 +00001036 if (isInt32Immediate(N->getOperand(1), Imm)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001037 SDValue N0 = N->getOperand(0);
Chris Lattnerdc664572005-08-25 17:50:06 +00001038 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001039 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001040 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001041 N0, getI32Imm(Log2_32(Imm)));
Andrew Trickc416ba62010-12-24 04:28:06 +00001042 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001043 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00001044 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
Evan Chengd1b82d82006-02-09 07:17:49 +00001045 SDNode *Op =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001046 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00001047 N0, getI32Imm(Log2_32(-Imm)));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001048 SDValue PT =
Dan Gohman32f71d72009-09-25 18:54:59 +00001049 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1050 SDValue(Op, 0), SDValue(Op, 1)),
Evan Chengd1b82d82006-02-09 07:17:49 +00001051 0);
Owen Anderson9f944592009-08-11 20:47:22 +00001052 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
Chris Lattnerdc664572005-08-25 17:50:06 +00001053 }
1054 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001055
Chris Lattner1de57062005-09-29 23:33:31 +00001056 // Other cases are autogenerated.
1057 break;
Chris Lattner6e184f22005-08-25 22:04:30 +00001058 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001059
Chris Lattnerce645542006-11-10 02:08:47 +00001060 case ISD::LOAD: {
1061 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001062 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001063 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00001064
Chris Lattnerce645542006-11-10 02:08:47 +00001065 // Normal loads are handled by code generated from the .td file.
1066 if (LD->getAddressingMode() != ISD::PRE_INC)
1067 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00001068
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001069 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00001070 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00001071 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00001072
Chris Lattner474b5b72006-11-15 19:55:13 +00001073 unsigned Opcode;
1074 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00001075 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00001076 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00001077 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1078 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001079 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001080 case MVT::f64: Opcode = PPC::LFDU; break;
1081 case MVT::f32: Opcode = PPC::LFSU; break;
1082 case MVT::i32: Opcode = PPC::LWZU; break;
1083 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1084 case MVT::i1:
1085 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001086 }
1087 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001088 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1089 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1090 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001091 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00001092 case MVT::i64: Opcode = PPC::LDU; break;
1093 case MVT::i32: Opcode = PPC::LWZU8; break;
1094 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1095 case MVT::i1:
1096 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00001097 }
1098 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001099
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001100 SDValue Chain = LD->getChain();
1101 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001102 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00001103 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1104 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001105 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001106 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00001107 unsigned Opcode;
1108 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1109 if (LD->getValueType(0) != MVT::i64) {
1110 // Handle PPC32 integer and normal FP loads.
1111 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1112 switch (LoadedVT.getSimpleVT().SimpleTy) {
1113 default: llvm_unreachable("Invalid PPC load type!");
1114 case MVT::f64: Opcode = PPC::LFDUX; break;
1115 case MVT::f32: Opcode = PPC::LFSUX; break;
1116 case MVT::i32: Opcode = PPC::LWZUX; break;
1117 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1118 case MVT::i1:
1119 case MVT::i8: Opcode = PPC::LBZUX; break;
1120 }
1121 } else {
1122 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1123 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1124 "Invalid sext update load");
1125 switch (LoadedVT.getSimpleVT().SimpleTy) {
1126 default: llvm_unreachable("Invalid PPC load type!");
1127 case MVT::i64: Opcode = PPC::LDUX; break;
1128 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1129 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1130 case MVT::i1:
1131 case MVT::i8: Opcode = PPC::LBZUX8; break;
1132 }
1133 }
1134
1135 SDValue Chain = LD->getChain();
1136 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00001137 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00001138 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1139 PPCLowering.getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00001140 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00001141 }
1142 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001143
Nate Begemanb3821a32005-08-18 07:30:46 +00001144 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00001145 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00001146 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00001147
Nate Begemanb3821a32005-08-18 07:30:46 +00001148 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1149 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00001150 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001151 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001152 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001153 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001154 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00001155 }
Nate Begemand31efd12006-09-22 05:01:56 +00001156 // If this is just a masked value where the input is not handled above, and
1157 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1158 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001159 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00001160 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001161 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001162 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001163 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00001164 }
Hal Finkele39526a2012-08-28 02:10:15 +00001165 // If this is a 64-bit zero-extension mask, emit rldicl.
1166 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1167 isMask_64(Imm64)) {
1168 SDValue Val = N->getOperand(0);
1169 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00001170 SH = 0;
1171
1172 // If the operand is a logical right shift, we can fold it into this
1173 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1174 // for n <= mb. The right shift is really a left rotate followed by a
1175 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1176 // by the shift.
1177 if (Val.getOpcode() == ISD::SRL &&
1178 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1179 assert(Imm < 64 && "Illegal shift amount");
1180 Val = Val.getOperand(0);
1181 SH = 64 - Imm;
1182 }
1183
1184 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00001185 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00001186 }
Nate Begemand31efd12006-09-22 05:01:56 +00001187 // AND X, 0 -> 0, not "rlwinm 32".
1188 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001189 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00001190 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00001191 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00001192 // ISD::OR doesn't get all the bitfield insertion fun.
1193 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00001194 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00001195 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00001196 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00001197 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00001198 Imm = ~(Imm^Imm2);
1199 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001200 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001201 N->getOperand(0).getOperand(1),
1202 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00001203 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00001204 }
1205 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001206
Chris Lattner1de57062005-09-29 23:33:31 +00001207 // Other cases are autogenerated.
1208 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00001209 }
Nate Begeman93c4bc62005-08-19 00:38:14 +00001210 case ISD::OR:
Owen Anderson9f944592009-08-11 20:47:22 +00001211 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00001212 if (SDNode *I = SelectBitfieldInsert(N))
1213 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00001214
Chris Lattner1de57062005-09-29 23:33:31 +00001215 // Other cases are autogenerated.
1216 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001217 case ISD::SHL: {
1218 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001219 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001220 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001221 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001222 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001223 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001224 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001225
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001226 // Other cases are autogenerated.
1227 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001228 }
1229 case ISD::SRL: {
1230 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001231 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00001232 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001233 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00001234 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00001235 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00001236 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001237
Nate Begeman9f3c26c2005-10-19 18:42:01 +00001238 // Other cases are autogenerated.
1239 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00001240 }
Hal Finkel940ab932014-02-28 00:27:01 +00001241 // FIXME: Remove this once the ANDI glue bug is fixed:
1242 case PPCISD::ANDIo_1_EQ_BIT:
1243 case PPCISD::ANDIo_1_GT_BIT: {
1244 if (!ANDIGlueBug)
1245 break;
1246
1247 EVT InVT = N->getOperand(0).getValueType();
1248 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1249 "Invalid input type for ANDIo_1_EQ_BIT");
1250
1251 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1252 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1253 N->getOperand(0),
1254 CurDAG->getTargetConstant(1, InVT)), 0);
1255 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1256 SDValue SRIdxVal =
1257 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1258 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1259
1260 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1261 CR0Reg, SRIdxVal,
1262 SDValue(AndI.getNode(), 1) /* glue */);
1263 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00001264 case ISD::SELECT_CC: {
1265 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00001266 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1267 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00001268
Hal Finkel940ab932014-02-28 00:27:01 +00001269 // If this is a select of i1 operands, we'll pattern match it.
1270 if (PPCSubTarget.useCRBits() &&
1271 N->getOperand(0).getValueType() == MVT::i1)
1272 break;
1273
Chris Lattner97b3da12006-06-27 00:04:13 +00001274 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00001275 if (!isPPC64)
1276 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1277 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1278 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1279 if (N1C->isNullValue() && N3C->isNullValue() &&
1280 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1281 // FIXME: Implement this optzn for PPC64.
1282 N->getValueType(0) == MVT::i32) {
1283 SDNode *Tmp =
1284 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1285 N->getOperand(0), getI32Imm(~0U));
1286 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1287 SDValue(Tmp, 0), N->getOperand(0),
1288 SDValue(Tmp, 1));
1289 }
Chris Lattner9b577f12005-08-26 21:23:58 +00001290
Dale Johannesenab8e4422009-02-06 19:16:40 +00001291 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001292
1293 if (N->getValueType(0) == MVT::i1) {
1294 // An i1 select is: (c & t) | (!c & f).
1295 bool Inv;
1296 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1297
1298 unsigned SRI;
1299 switch (Idx) {
1300 default: llvm_unreachable("Invalid CC index");
1301 case 0: SRI = PPC::sub_lt; break;
1302 case 1: SRI = PPC::sub_gt; break;
1303 case 2: SRI = PPC::sub_eq; break;
1304 case 3: SRI = PPC::sub_un; break;
1305 }
1306
1307 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1308
1309 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1310 CCBit, CCBit), 0);
1311 SDValue C = Inv ? NotCCBit : CCBit,
1312 NotC = Inv ? CCBit : NotCCBit;
1313
1314 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1315 C, N->getOperand(2)), 0);
1316 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1317 NotC, N->getOperand(3)), 0);
1318
1319 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1320 }
1321
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001322 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00001323
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001324 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00001325 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00001326 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00001327 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00001328 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00001329 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001330 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00001331 else if (N->getValueType(0) == MVT::f64)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00001332 SelectCCOp = PPC::SELECT_CC_F8;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00001333 else
1334 SelectCCOp = PPC::SELECT_CC_VRRC;
1335
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001336 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00001337 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00001338 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00001339 }
Hal Finkel732f0f72014-03-26 12:49:28 +00001340 case ISD::VSELECT:
1341 if (PPCSubTarget.hasVSX()) {
1342 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001343 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00001344 }
1345
1346 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001347 case ISD::VECTOR_SHUFFLE:
1348 if (PPCSubTarget.hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
1349 N->getValueType(0) == MVT::v2i64)) {
1350 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1351
1352 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1353 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1354 unsigned DM[2];
1355
1356 for (int i = 0; i < 2; ++i)
1357 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1358 DM[i] = 0;
1359 else
1360 DM[i] = 1;
1361
Hal Finkel2583b062014-03-28 20:24:55 +00001362 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001363
1364 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1365 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1366 isa<LoadSDNode>(Op1.getOperand(0))) {
1367 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1368 SDValue Base, Offset;
1369
1370 if (LD->isUnindexed() &&
1371 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1372 SDValue Chain = LD->getChain();
1373 SDValue Ops[] = { Base, Offset, Chain };
1374 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00001375 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001376 }
1377 }
1378
1379 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00001380 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00001381 }
1382
1383 break;
Hal Finkel25c19922013-05-15 21:37:41 +00001384 case PPCISD::BDNZ:
1385 case PPCISD::BDZ: {
1386 bool IsPPC64 = PPCSubTarget.isPPC64();
1387 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1388 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1389 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1390 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00001391 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00001392 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001393 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00001394 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001395 // Op #1 is the PPC::PRED_* number.
1396 // Op #2 is the CR#
1397 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001398 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00001399 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001400 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00001401 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001402 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001403 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00001404 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00001405 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00001406 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001407 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00001408 unsigned PCC = getPredicateForSetCC(CC);
1409
1410 if (N->getOperand(2).getValueType() == MVT::i1) {
1411 unsigned Opc;
1412 bool Swap;
1413 switch (PCC) {
1414 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1415 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1416 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1417 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1418 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1419 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1420 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1421 }
1422
1423 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1424 N->getOperand(Swap ? 3 : 2),
1425 N->getOperand(Swap ? 2 : 3)), 0);
1426 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1427 BitComp, N->getOperand(4), N->getOperand(0));
1428 }
1429
Dale Johannesenab8e4422009-02-06 19:16:40 +00001430 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00001431 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00001432 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00001433 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001434 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001435 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00001436 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001437 SDValue Chain = N->getOperand(0);
1438 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00001439 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00001440 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00001441 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00001442 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00001443 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001444 }
Bill Schmidt34627e32012-11-27 17:35:46 +00001445 case PPCISD::TOC_ENTRY: {
1446 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1447
Bill Schmidt27917782013-02-21 17:12:27 +00001448 // For medium and large code model, we generate two instructions as
1449 // described below. Otherwise we allow SelectCodeCommon to handle this,
1450 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1451 CodeModel::Model CModel = TM.getCodeModel();
1452 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001453 break;
1454
1455 // The first source operand is a TargetGlobalAddress or a
1456 // TargetJumpTable. If it is an externally defined symbol, a symbol
1457 // with common linkage, a function address, or a jump table address,
Bill Schmidt27917782013-02-21 17:12:27 +00001458 // or if we are generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00001459 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1460 // Otherwise we generate:
1461 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1462 SDValue GA = N->getOperand(0);
1463 SDValue TOCbase = N->getOperand(1);
1464 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1465 TOCbase, GA);
1466
Bill Schmidt27917782013-02-21 17:12:27 +00001467 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00001468 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1469 SDValue(Tmp, 0));
1470
1471 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1472 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001473 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
Rafael Espindola24a669d2014-03-27 15:26:56 +00001474 const GlobalValue *RealGValue =
1475 GAlias ? GAlias->getAliasedGlobal() : GValue;
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001476 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1477 assert((GVar || isa<Function>(RealGValue)) &&
Bill Schmidt34627e32012-11-27 17:35:46 +00001478 "Unexpected global value subclass!");
1479
1480 // An external variable is one without an initializer. For these,
1481 // for variables with common linkage, and for Functions, generate
1482 // the LDtocL form.
Bill Schmidt9b1e3e22013-01-07 19:29:18 +00001483 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1484 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00001485 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1486 SDValue(Tmp, 0));
1487 }
1488
1489 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1490 SDValue(Tmp, 0), GA);
1491 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001492 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001493 // This expands into one of three sequences, depending on whether
1494 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00001495 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1496 isa<ConstantSDNode>(N->getOperand(1)) &&
1497 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001498
1499 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00001500 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001501 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00001502 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001503
Bill Schmidt51e79512013-02-20 15:50:31 +00001504 if (EltSize == 1) {
1505 Opc1 = PPC::VSPLTISB;
1506 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001507 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001508 VT = MVT::v16i8;
1509 } else if (EltSize == 2) {
1510 Opc1 = PPC::VSPLTISH;
1511 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001512 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001513 VT = MVT::v8i16;
1514 } else {
1515 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1516 Opc1 = PPC::VSPLTISW;
1517 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001518 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00001519 VT = MVT::v4i32;
1520 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00001521
1522 if ((Elt & 1) == 0) {
1523 // Elt is even, in the range [-32,-18] + [16,30].
1524 //
1525 // Convert: VADD_SPLAT elt, size
1526 // Into: tmp = VSPLTIS[BHW] elt
1527 // VADDU[BHW]M tmp, tmp
1528 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1529 SDValue EltVal = getI32Imm(Elt >> 1);
1530 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1531 SDValue TmpVal = SDValue(Tmp, 0);
1532 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1533
1534 } else if (Elt > 0) {
1535 // Elt is odd and positive, in the range [17,31].
1536 //
1537 // Convert: VADD_SPLAT elt, size
1538 // Into: tmp1 = VSPLTIS[BHW] elt-16
1539 // tmp2 = VSPLTIS[BHW] -16
1540 // VSUBU[BHW]M tmp1, tmp2
1541 SDValue EltVal = getI32Imm(Elt - 16);
1542 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1543 EltVal = getI32Imm(-16);
1544 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1545 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1546 SDValue(Tmp2, 0));
1547
1548 } else {
1549 // Elt is odd and negative, in the range [-31,-17].
1550 //
1551 // Convert: VADD_SPLAT elt, size
1552 // Into: tmp1 = VSPLTIS[BHW] elt+16
1553 // tmp2 = VSPLTIS[BHW] -16
1554 // VADDU[BHW]M tmp1, tmp2
1555 SDValue EltVal = getI32Imm(Elt + 16);
1556 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1557 EltVal = getI32Imm(-16);
1558 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1559 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1560 SDValue(Tmp2, 0));
1561 }
Bill Schmidt51e79512013-02-20 15:50:31 +00001562 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00001563 }
Andrew Trickc416ba62010-12-24 04:28:06 +00001564
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001565 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00001566}
1567
Hal Finkel860fa902014-01-02 22:09:39 +00001568/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00001569/// on the DAG representation.
1570void PPCDAGToDAGISel::PostprocessISelDAG() {
1571
1572 // Skip peepholes at -O0.
1573 if (TM.getOptLevel() == CodeGenOpt::None)
1574 return;
1575
Hal Finkel940ab932014-02-28 00:27:01 +00001576 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00001577 PeepholeCROps();
Hal Finkel940ab932014-02-28 00:27:01 +00001578}
1579
Hal Finkelb9989152014-02-28 06:11:16 +00001580// Check if all users of this node will become isel where the second operand
1581// is the constant zero. If this is so, and if we can negate the condition,
1582// then we can flip the true and false operands. This will allow the zero to
1583// be folded with the isel so that we don't need to materialize a register
1584// containing zero.
1585bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1586 // If we're not using isel, then this does not matter.
1587 if (!PPCSubTarget.hasISEL())
1588 return false;
1589
1590 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1591 UI != UE; ++UI) {
1592 SDNode *User = *UI;
1593 if (!User->isMachineOpcode())
1594 return false;
1595 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1596 User->getMachineOpcode() != PPC::SELECT_I8)
1597 return false;
1598
1599 SDNode *Op2 = User->getOperand(2).getNode();
1600 if (!Op2->isMachineOpcode())
1601 return false;
1602
1603 if (Op2->getMachineOpcode() != PPC::LI &&
1604 Op2->getMachineOpcode() != PPC::LI8)
1605 return false;
1606
1607 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1608 if (!C)
1609 return false;
1610
1611 if (!C->isNullValue())
1612 return false;
1613 }
1614
1615 return true;
1616}
1617
1618void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1619 SmallVector<SDNode *, 4> ToReplace;
1620 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1621 UI != UE; ++UI) {
1622 SDNode *User = *UI;
1623 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1624 User->getMachineOpcode() == PPC::SELECT_I8) &&
1625 "Must have all select users");
1626 ToReplace.push_back(User);
1627 }
1628
1629 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1630 UE = ToReplace.end(); UI != UE; ++UI) {
1631 SDNode *User = *UI;
1632 SDNode *ResNode =
1633 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1634 User->getValueType(0), User->getOperand(0),
1635 User->getOperand(2),
1636 User->getOperand(1));
1637
1638 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1639 DEBUG(User->dump(CurDAG));
1640 DEBUG(dbgs() << "\nNew: ");
1641 DEBUG(ResNode->dump(CurDAG));
1642 DEBUG(dbgs() << "\n");
1643
1644 ReplaceUses(User, ResNode);
1645 }
1646}
1647
Eric Christopher02e18042014-05-14 00:31:15 +00001648void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00001649 bool IsModified;
1650 do {
1651 IsModified = false;
1652 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1653 E = CurDAG->allnodes_end(); I != E; ++I) {
1654 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1655 if (!MachineNode || MachineNode->use_empty())
1656 continue;
1657 SDNode *ResNode = MachineNode;
1658
1659 bool Op1Set = false, Op1Unset = false,
1660 Op1Not = false,
1661 Op2Set = false, Op2Unset = false,
1662 Op2Not = false;
1663
1664 unsigned Opcode = MachineNode->getMachineOpcode();
1665 switch (Opcode) {
1666 default: break;
1667 case PPC::CRAND:
1668 case PPC::CRNAND:
1669 case PPC::CROR:
1670 case PPC::CRXOR:
1671 case PPC::CRNOR:
1672 case PPC::CREQV:
1673 case PPC::CRANDC:
1674 case PPC::CRORC: {
1675 SDValue Op = MachineNode->getOperand(1);
1676 if (Op.isMachineOpcode()) {
1677 if (Op.getMachineOpcode() == PPC::CRSET)
1678 Op2Set = true;
1679 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1680 Op2Unset = true;
1681 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1682 Op.getOperand(0) == Op.getOperand(1))
1683 Op2Not = true;
1684 }
1685 } // fallthrough
1686 case PPC::BC:
1687 case PPC::BCn:
1688 case PPC::SELECT_I4:
1689 case PPC::SELECT_I8:
1690 case PPC::SELECT_F4:
1691 case PPC::SELECT_F8:
1692 case PPC::SELECT_VRRC: {
1693 SDValue Op = MachineNode->getOperand(0);
1694 if (Op.isMachineOpcode()) {
1695 if (Op.getMachineOpcode() == PPC::CRSET)
1696 Op1Set = true;
1697 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1698 Op1Unset = true;
1699 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1700 Op.getOperand(0) == Op.getOperand(1))
1701 Op1Not = true;
1702 }
1703 }
1704 break;
1705 }
1706
Hal Finkelb9989152014-02-28 06:11:16 +00001707 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00001708 switch (Opcode) {
1709 default: break;
1710 case PPC::CRAND:
1711 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1712 // x & x = x
1713 ResNode = MachineNode->getOperand(0).getNode();
1714 else if (Op1Set)
1715 // 1 & y = y
1716 ResNode = MachineNode->getOperand(1).getNode();
1717 else if (Op2Set)
1718 // x & 1 = x
1719 ResNode = MachineNode->getOperand(0).getNode();
1720 else if (Op1Unset || Op2Unset)
1721 // x & 0 = 0 & y = 0
1722 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1723 MVT::i1);
1724 else if (Op1Not)
1725 // ~x & y = andc(y, x)
1726 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1727 MVT::i1, MachineNode->getOperand(1),
1728 MachineNode->getOperand(0).
1729 getOperand(0));
1730 else if (Op2Not)
1731 // x & ~y = andc(x, y)
1732 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1733 MVT::i1, MachineNode->getOperand(0),
1734 MachineNode->getOperand(1).
1735 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001736 else if (AllUsersSelectZero(MachineNode))
1737 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1738 MVT::i1, MachineNode->getOperand(0),
1739 MachineNode->getOperand(1)),
1740 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001741 break;
1742 case PPC::CRNAND:
1743 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1744 // nand(x, x) -> nor(x, x)
1745 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1746 MVT::i1, MachineNode->getOperand(0),
1747 MachineNode->getOperand(0));
1748 else if (Op1Set)
1749 // nand(1, y) -> nor(y, y)
1750 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1751 MVT::i1, MachineNode->getOperand(1),
1752 MachineNode->getOperand(1));
1753 else if (Op2Set)
1754 // nand(x, 1) -> nor(x, x)
1755 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1756 MVT::i1, MachineNode->getOperand(0),
1757 MachineNode->getOperand(0));
1758 else if (Op1Unset || Op2Unset)
1759 // nand(x, 0) = nand(0, y) = 1
1760 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1761 MVT::i1);
1762 else if (Op1Not)
1763 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1764 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1765 MVT::i1, MachineNode->getOperand(0).
1766 getOperand(0),
1767 MachineNode->getOperand(1));
1768 else if (Op2Not)
1769 // nand(x, ~y) = ~x | y = orc(y, x)
1770 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1771 MVT::i1, MachineNode->getOperand(1).
1772 getOperand(0),
1773 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001774 else if (AllUsersSelectZero(MachineNode))
1775 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1776 MVT::i1, MachineNode->getOperand(0),
1777 MachineNode->getOperand(1)),
1778 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001779 break;
1780 case PPC::CROR:
1781 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1782 // x | x = x
1783 ResNode = MachineNode->getOperand(0).getNode();
1784 else if (Op1Set || Op2Set)
1785 // x | 1 = 1 | y = 1
1786 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1787 MVT::i1);
1788 else if (Op1Unset)
1789 // 0 | y = y
1790 ResNode = MachineNode->getOperand(1).getNode();
1791 else if (Op2Unset)
1792 // x | 0 = x
1793 ResNode = MachineNode->getOperand(0).getNode();
1794 else if (Op1Not)
1795 // ~x | y = orc(y, x)
1796 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1797 MVT::i1, MachineNode->getOperand(1),
1798 MachineNode->getOperand(0).
1799 getOperand(0));
1800 else if (Op2Not)
1801 // x | ~y = orc(x, y)
1802 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1803 MVT::i1, MachineNode->getOperand(0),
1804 MachineNode->getOperand(1).
1805 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001806 else if (AllUsersSelectZero(MachineNode))
1807 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1808 MVT::i1, MachineNode->getOperand(0),
1809 MachineNode->getOperand(1)),
1810 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001811 break;
1812 case PPC::CRXOR:
1813 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1814 // xor(x, x) = 0
1815 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1816 MVT::i1);
1817 else if (Op1Set)
1818 // xor(1, y) -> nor(y, y)
1819 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1820 MVT::i1, MachineNode->getOperand(1),
1821 MachineNode->getOperand(1));
1822 else if (Op2Set)
1823 // xor(x, 1) -> nor(x, x)
1824 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1825 MVT::i1, MachineNode->getOperand(0),
1826 MachineNode->getOperand(0));
1827 else if (Op1Unset)
1828 // xor(0, y) = y
1829 ResNode = MachineNode->getOperand(1).getNode();
1830 else if (Op2Unset)
1831 // xor(x, 0) = x
1832 ResNode = MachineNode->getOperand(0).getNode();
1833 else if (Op1Not)
1834 // xor(~x, y) = eqv(x, y)
1835 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1836 MVT::i1, MachineNode->getOperand(0).
1837 getOperand(0),
1838 MachineNode->getOperand(1));
1839 else if (Op2Not)
1840 // xor(x, ~y) = eqv(x, y)
1841 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1842 MVT::i1, MachineNode->getOperand(0),
1843 MachineNode->getOperand(1).
1844 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001845 else if (AllUsersSelectZero(MachineNode))
1846 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1847 MVT::i1, MachineNode->getOperand(0),
1848 MachineNode->getOperand(1)),
1849 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001850 break;
1851 case PPC::CRNOR:
1852 if (Op1Set || Op2Set)
1853 // nor(1, y) -> 0
1854 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1855 MVT::i1);
1856 else if (Op1Unset)
1857 // nor(0, y) = ~y -> nor(y, y)
1858 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1859 MVT::i1, MachineNode->getOperand(1),
1860 MachineNode->getOperand(1));
1861 else if (Op2Unset)
1862 // nor(x, 0) = ~x
1863 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1864 MVT::i1, MachineNode->getOperand(0),
1865 MachineNode->getOperand(0));
1866 else if (Op1Not)
1867 // nor(~x, y) = andc(x, y)
1868 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1869 MVT::i1, MachineNode->getOperand(0).
1870 getOperand(0),
1871 MachineNode->getOperand(1));
1872 else if (Op2Not)
1873 // nor(x, ~y) = andc(y, x)
1874 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1875 MVT::i1, MachineNode->getOperand(1).
1876 getOperand(0),
1877 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001878 else if (AllUsersSelectZero(MachineNode))
1879 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1880 MVT::i1, MachineNode->getOperand(0),
1881 MachineNode->getOperand(1)),
1882 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001883 break;
1884 case PPC::CREQV:
1885 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1886 // eqv(x, x) = 1
1887 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1888 MVT::i1);
1889 else if (Op1Set)
1890 // eqv(1, y) = y
1891 ResNode = MachineNode->getOperand(1).getNode();
1892 else if (Op2Set)
1893 // eqv(x, 1) = x
1894 ResNode = MachineNode->getOperand(0).getNode();
1895 else if (Op1Unset)
1896 // eqv(0, y) = ~y -> nor(y, y)
1897 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1898 MVT::i1, MachineNode->getOperand(1),
1899 MachineNode->getOperand(1));
1900 else if (Op2Unset)
1901 // eqv(x, 0) = ~x
1902 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1903 MVT::i1, MachineNode->getOperand(0),
1904 MachineNode->getOperand(0));
1905 else if (Op1Not)
1906 // eqv(~x, y) = xor(x, y)
1907 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1908 MVT::i1, MachineNode->getOperand(0).
1909 getOperand(0),
1910 MachineNode->getOperand(1));
1911 else if (Op2Not)
1912 // eqv(x, ~y) = xor(x, y)
1913 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1914 MVT::i1, MachineNode->getOperand(0),
1915 MachineNode->getOperand(1).
1916 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001917 else if (AllUsersSelectZero(MachineNode))
1918 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1919 MVT::i1, MachineNode->getOperand(0),
1920 MachineNode->getOperand(1)),
1921 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001922 break;
1923 case PPC::CRANDC:
1924 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1925 // andc(x, x) = 0
1926 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1927 MVT::i1);
1928 else if (Op1Set)
1929 // andc(1, y) = ~y
1930 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1931 MVT::i1, MachineNode->getOperand(1),
1932 MachineNode->getOperand(1));
1933 else if (Op1Unset || Op2Set)
1934 // andc(0, y) = andc(x, 1) = 0
1935 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1936 MVT::i1);
1937 else if (Op2Unset)
1938 // andc(x, 0) = x
1939 ResNode = MachineNode->getOperand(0).getNode();
1940 else if (Op1Not)
1941 // andc(~x, y) = ~(x | y) = nor(x, y)
1942 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1943 MVT::i1, MachineNode->getOperand(0).
1944 getOperand(0),
1945 MachineNode->getOperand(1));
1946 else if (Op2Not)
1947 // andc(x, ~y) = x & y
1948 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1949 MVT::i1, MachineNode->getOperand(0),
1950 MachineNode->getOperand(1).
1951 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001952 else if (AllUsersSelectZero(MachineNode))
1953 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1954 MVT::i1, MachineNode->getOperand(1),
1955 MachineNode->getOperand(0)),
1956 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001957 break;
1958 case PPC::CRORC:
1959 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1960 // orc(x, x) = 1
1961 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1962 MVT::i1);
1963 else if (Op1Set || Op2Unset)
1964 // orc(1, y) = orc(x, 0) = 1
1965 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1966 MVT::i1);
1967 else if (Op2Set)
1968 // orc(x, 1) = x
1969 ResNode = MachineNode->getOperand(0).getNode();
1970 else if (Op1Unset)
1971 // orc(0, y) = ~y
1972 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1973 MVT::i1, MachineNode->getOperand(1),
1974 MachineNode->getOperand(1));
1975 else if (Op1Not)
1976 // orc(~x, y) = ~(x & y) = nand(x, y)
1977 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1978 MVT::i1, MachineNode->getOperand(0).
1979 getOperand(0),
1980 MachineNode->getOperand(1));
1981 else if (Op2Not)
1982 // orc(x, ~y) = x | y
1983 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1984 MVT::i1, MachineNode->getOperand(0),
1985 MachineNode->getOperand(1).
1986 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00001987 else if (AllUsersSelectZero(MachineNode))
1988 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1989 MVT::i1, MachineNode->getOperand(1),
1990 MachineNode->getOperand(0)),
1991 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00001992 break;
1993 case PPC::SELECT_I4:
1994 case PPC::SELECT_I8:
1995 case PPC::SELECT_F4:
1996 case PPC::SELECT_F8:
1997 case PPC::SELECT_VRRC:
1998 if (Op1Set)
1999 ResNode = MachineNode->getOperand(1).getNode();
2000 else if (Op1Unset)
2001 ResNode = MachineNode->getOperand(2).getNode();
2002 else if (Op1Not)
2003 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
2004 SDLoc(MachineNode),
2005 MachineNode->getValueType(0),
2006 MachineNode->getOperand(0).
2007 getOperand(0),
2008 MachineNode->getOperand(2),
2009 MachineNode->getOperand(1));
2010 break;
2011 case PPC::BC:
2012 case PPC::BCn:
2013 if (Op1Not)
2014 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2015 PPC::BC,
2016 SDLoc(MachineNode),
2017 MVT::Other,
2018 MachineNode->getOperand(0).
2019 getOperand(0),
2020 MachineNode->getOperand(1),
2021 MachineNode->getOperand(2));
2022 // FIXME: Handle Op1Set, Op1Unset here too.
2023 break;
2024 }
2025
Hal Finkelb9989152014-02-28 06:11:16 +00002026 // If we're inverting this node because it is used only by selects that
2027 // we'd like to swap, then swap the selects before the node replacement.
2028 if (SelectSwap)
2029 SwapAllSelectUsers(MachineNode);
2030
Hal Finkel940ab932014-02-28 00:27:01 +00002031 if (ResNode != MachineNode) {
2032 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2033 DEBUG(MachineNode->dump(CurDAG));
2034 DEBUG(dbgs() << "\nNew: ");
2035 DEBUG(ResNode->dump(CurDAG));
2036 DEBUG(dbgs() << "\n");
2037
2038 ReplaceUses(MachineNode, ResNode);
2039 IsModified = true;
2040 }
2041 }
2042 if (IsModified)
2043 CurDAG->RemoveDeadNodes();
2044 } while (IsModified);
2045}
2046
2047void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002048 // These optimizations are currently supported only for 64-bit SVR4.
2049 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
2050 return;
2051
2052 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2053 ++Position;
2054
2055 while (Position != CurDAG->allnodes_begin()) {
2056 SDNode *N = --Position;
2057 // Skip dead nodes and any non-machine opcodes.
2058 if (N->use_empty() || !N->isMachineOpcode())
2059 continue;
2060
2061 unsigned FirstOp;
2062 unsigned StorageOpcode = N->getMachineOpcode();
2063
2064 switch (StorageOpcode) {
2065 default: continue;
2066
2067 case PPC::LBZ:
2068 case PPC::LBZ8:
2069 case PPC::LD:
2070 case PPC::LFD:
2071 case PPC::LFS:
2072 case PPC::LHA:
2073 case PPC::LHA8:
2074 case PPC::LHZ:
2075 case PPC::LHZ8:
2076 case PPC::LWA:
2077 case PPC::LWZ:
2078 case PPC::LWZ8:
2079 FirstOp = 0;
2080 break;
2081
2082 case PPC::STB:
2083 case PPC::STB8:
2084 case PPC::STD:
2085 case PPC::STFD:
2086 case PPC::STFS:
2087 case PPC::STH:
2088 case PPC::STH8:
2089 case PPC::STW:
2090 case PPC::STW8:
2091 FirstOp = 1;
2092 break;
2093 }
2094
2095 // If this is a load or store with a zero offset, we may be able to
2096 // fold an add-immediate into the memory operation.
2097 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2098 N->getConstantOperandVal(FirstOp) != 0)
2099 continue;
2100
2101 SDValue Base = N->getOperand(FirstOp + 1);
2102 if (!Base.isMachineOpcode())
2103 continue;
2104
2105 unsigned Flags = 0;
2106 bool ReplaceFlags = true;
2107
2108 // When the feeding operation is an add-immediate of some sort,
2109 // determine whether we need to add relocation information to the
2110 // target flags on the immediate operand when we fold it into the
2111 // load instruction.
2112 //
2113 // For something like ADDItocL, the relocation information is
2114 // inferred from the opcode; when we process it in the AsmPrinter,
2115 // we add the necessary relocation there. A load, though, can receive
2116 // relocation from various flavors of ADDIxxx, so we need to carry
2117 // the relocation information in the target flags.
2118 switch (Base.getMachineOpcode()) {
2119 default: continue;
2120
2121 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00002122 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002123 // In some cases (such as TLS) the relocation information
2124 // is already in place on the operand, so copying the operand
2125 // is sufficient.
2126 ReplaceFlags = false;
2127 // For these cases, the immediate may not be divisible by 4, in
2128 // which case the fold is illegal for DS-form instructions. (The
2129 // other cases provide aligned addresses and are always safe.)
2130 if ((StorageOpcode == PPC::LWA ||
2131 StorageOpcode == PPC::LD ||
2132 StorageOpcode == PPC::STD) &&
2133 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2134 Base.getConstantOperandVal(1) % 4 != 0))
2135 continue;
2136 break;
2137 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002138 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002139 break;
2140 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002141 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002142 break;
2143 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00002144 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002145 break;
2146 }
2147
2148 // We found an opportunity. Reverse the operands from the add
2149 // immediate and substitute them into the load or store. If
2150 // needed, update the target flags for the immediate operand to
2151 // reflect the necessary relocation information.
2152 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2153 DEBUG(Base->dump(CurDAG));
2154 DEBUG(dbgs() << "\nN: ");
2155 DEBUG(N->dump(CurDAG));
2156 DEBUG(dbgs() << "\n");
2157
2158 SDValue ImmOpnd = Base.getOperand(1);
2159
2160 // If the relocation information isn't already present on the
2161 // immediate operand, add it now.
2162 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002163 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002164 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002165 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00002166 // We can't perform this optimization for data whose alignment
2167 // is insufficient for the instruction encoding.
2168 if (GV->getAlignment() < 4 &&
2169 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2170 StorageOpcode == PPC::LWA)) {
2171 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2172 continue;
2173 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002174 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00002175 } else if (ConstantPoolSDNode *CP =
2176 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00002177 const Constant *C = CP->getConstVal();
2178 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2179 CP->getAlignment(),
2180 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00002181 }
2182 }
2183
2184 if (FirstOp == 1) // Store
2185 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2186 Base.getOperand(0), N->getOperand(3));
2187 else // Load
2188 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2189 N->getOperand(2));
2190
2191 // The add-immediate may now be dead, in which case remove it.
2192 if (Base.getNode()->use_empty())
2193 CurDAG->RemoveDeadNode(Base.getNode());
2194 }
2195}
Chris Lattner43ff01e2005-08-17 19:33:03 +00002196
Chris Lattnerb055c872006-06-10 01:15:02 +00002197
Andrew Trickc416ba62010-12-24 04:28:06 +00002198/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00002199/// PowerPC-specific DAG, ready for instruction scheduling.
2200///
Evan Cheng2dd2c652006-03-13 23:20:37 +00002201FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00002202 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002203}
2204
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002205static void initializePassOnce(PassRegistry &Registry) {
2206 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00002207 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
2208 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00002209 Registry.registerPass(*PI, true);
2210}
2211
2212void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2213 CALL_ONCE_INITIALIZATION(initializePassOnce);
2214}
2215