blob: 512eddcb0da510062dec73c3e3ba506c84934695 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel3ee2af72014-07-18 23:29:49 +000017#include "PPCMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000026#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Intrinsics.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000030#include "llvm/IR/Module.h"
Hal Finkel940ab932014-02-28 00:27:01 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "ppc-codegen"
40
Hal Finkel940ab932014-02-28 00:27:01 +000041// FIXME: Remove this once the bug has been fixed!
42cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
43cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
44
Benjamin Kramer970eac42015-02-06 17:51:54 +000045static cl::opt<bool>
46 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
47 cl::desc("use aggressive ppc isel for bit permutations"),
48 cl::Hidden);
49static cl::opt<bool> BPermRewriterNoMasking(
50 "ppc-bit-perm-rewriter-stress-rotates",
51 cl::desc("stress rotate selection in aggressive ppc isel for "
52 "bit permutations"),
53 cl::Hidden);
Hal Finkelc58ce412015-01-01 02:53:29 +000054
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000055namespace llvm {
56 void initializePPCDAGToDAGISelPass(PassRegistry&);
57}
58
Chris Lattner43ff01e2005-08-17 19:33:03 +000059namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000060 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000061 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000062 /// instructions for SelectionDAG operations.
63 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000064 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000065 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000066 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +000067 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +000068 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000069 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000070 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christophercccae792015-01-30 22:02:31 +000071 : SelectionDAGISel(tm), TM(tm) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000072 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
73 }
Andrew Trickc416ba62010-12-24 04:28:06 +000074
Craig Topper0d3fa922014-04-29 07:57:37 +000075 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000076 // Make sure we re-emit a set of the global base reg if necessary
77 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +000078 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
79 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +000080 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000081
Eric Christopher1b8e7632014-05-22 01:07:24 +000082 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000083 InsertVRSaveCode(MF);
84
Chris Lattner1678a6c2006-03-16 18:25:23 +000085 return true;
Chris Lattner45640392005-08-19 22:38:53 +000086 }
Andrew Trickc416ba62010-12-24 04:28:06 +000087
Hal Finkel4edc66b2015-01-03 01:16:37 +000088 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +000089 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000090
Chris Lattner43ff01e2005-08-17 19:33:03 +000091 /// getI32Imm - Return a target constant with the specified value, of type
92 /// i32.
Sergey Dmitroukadb4c692015-04-28 11:56:37 +000093 inline SDValue getI32Imm(unsigned Imm, SDLoc dl) {
94 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000095 }
Chris Lattner45640392005-08-19 22:38:53 +000096
Chris Lattner97b3da12006-06-27 00:04:13 +000097 /// getI64Imm - Return a target constant with the specified value, of type
98 /// i64.
Sergey Dmitroukadb4c692015-04-28 11:56:37 +000099 inline SDValue getI64Imm(uint64_t Imm, SDLoc dl) {
100 return CurDAG->getTargetConstant(Imm, dl, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000101 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000102
Chris Lattner97b3da12006-06-27 00:04:13 +0000103 /// getSmallIPtrImm - Return a target constant of pointer type.
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000104 inline SDValue getSmallIPtrImm(unsigned Imm, SDLoc dl) {
105 return CurDAG->getTargetConstant(Imm, dl, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +0000106 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000107
Nate Begemand31efd12006-09-22 05:01:56 +0000108 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
109 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000110 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000111 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000112
Chris Lattner45640392005-08-19 22:38:53 +0000113 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
114 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000115 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000116
Hal Finkelb5e9b042014-12-11 22:51:06 +0000117 SDNode *getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
118
Chris Lattner43ff01e2005-08-17 19:33:03 +0000119 // Select - Convert the specified operand from a target-independent to a
120 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000121 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000122
Nate Begeman93c4bc62005-08-19 00:38:14 +0000123 SDNode *SelectBitfieldInsert(SDNode *N);
Hal Finkel8adf2252014-12-16 05:51:41 +0000124 SDNode *SelectBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000125
Chris Lattner2a1823d2005-08-21 18:50:37 +0000126 /// SelectCC - Select a comparison of the specified values with the
127 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000128 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000129
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000130 /// SelectAddrImm - Returns true if the address N can be represented by
131 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000132 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000133 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000134 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000135 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000136
Chris Lattner6f5840c2006-11-16 00:41:37 +0000137 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000138 /// immediate field. Note that the operand at this point is already the
139 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000140 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000141 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000142 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000143 Out = N;
144 return true;
145 }
146
147 return false;
148 }
149
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000150 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
151 /// represented as an indexed [r+r] operation. Returns false if it can
152 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000153 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000154 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000155 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000156
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000157 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
158 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000159 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000160 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000161 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000162
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000163 /// SelectAddrImmX4 - Returns true if the address N can be represented by
164 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
165 /// Suitable for use by STD and friends.
166 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000167 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000168 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000169
Hal Finkel756810f2013-03-21 21:37:52 +0000170 // Select an address into a single register.
171 bool SelectAddr(SDValue N, SDValue &Base) {
172 Base = N;
173 return true;
174 }
175
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000176 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000177 /// inline asm expressions. It is always correct to compute the value into
178 /// a register. The case of adding a (possibly relocatable) constant to a
179 /// register can be improved, but it is wrong to substitute Reg+Reg for
180 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000181 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000182 unsigned ConstraintID,
Craig Topper0d3fa922014-04-29 07:57:37 +0000183 std::vector<SDValue> &OutOps) override {
Hal Finkeld4338382014-12-03 23:40:13 +0000184
Daniel Sanders08288602015-03-17 11:09:13 +0000185 switch(ConstraintID) {
186 default:
187 errs() << "ConstraintID: " << ConstraintID << "\n";
188 llvm_unreachable("Unexpected asm memory constraint");
189 case InlineAsm::Constraint_es:
Daniel Sanders914b9472015-03-17 12:00:04 +0000190 case InlineAsm::Constraint_i:
Daniel Sanders08288602015-03-17 11:09:13 +0000191 case InlineAsm::Constraint_m:
192 case InlineAsm::Constraint_o:
193 case InlineAsm::Constraint_Q:
194 case InlineAsm::Constraint_Z:
195 case InlineAsm::Constraint_Zy:
196 // We need to make sure that this one operand does not end up in r0
197 // (because we might end up lowering this as 0(%op)).
198 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
199 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000200 SDLoc dl(Op);
201 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Daniel Sanders08288602015-03-17 11:09:13 +0000202 SDValue NewOp =
203 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000204 dl, Op.getValueType(),
Daniel Sanders08288602015-03-17 11:09:13 +0000205 Op, RC), 0);
206
207 OutOps.push_back(NewOp);
208 return false;
209 }
210 return true;
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000211 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000212
Dan Gohman5ea74d52009-07-31 18:16:33 +0000213 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000214
Craig Topper0d3fa922014-04-29 07:57:37 +0000215 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000216 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000217 }
218
Chris Lattner03e08ee2005-09-13 22:03:06 +0000219// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000220#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000221
Chris Lattner259e6c72005-10-06 18:45:51 +0000222private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000223 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000224
225 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000226 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000227 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000228
Hal Finkel4edc66b2015-01-03 01:16:37 +0000229 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000230 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000231
Hal Finkelb9989152014-02-28 06:11:16 +0000232 bool AllUsersSelectZero(SDNode *N);
233 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000234
235 SDNode *transferMemOperands(SDNode *N, SDNode *Result);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000236 };
237}
238
Chris Lattner1678a6c2006-03-16 18:25:23 +0000239/// InsertVRSaveCode - Once the entire function has been instruction selected,
240/// all virtual registers are created and all machine instructions are built,
241/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000242void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000243 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000244 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000245 //
Dan Gohman4a618822010-02-10 16:03:48 +0000246 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000247 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000248 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000249 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
250 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
251 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000252 HasVectorVReg = true;
253 break;
254 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000255 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000256 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000257
Chris Lattner02e2c182006-03-13 21:52:10 +0000258 // If we have a vector register, we want to emit code into the entry and exit
259 // blocks to save and restore the VRSAVE register. We do this here (instead
260 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
261 //
262 // 1. This (trivially) reduces the load on the register allocator, by not
263 // having to represent the live range of the VRSAVE register.
264 // 2. This (more significantly) allows us to create a temporary virtual
265 // register to hold the saved VRSAVE value, allowing this temporary to be
266 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000267
268 // Create two vregs - one to hold the VRSAVE register that is live-in to the
269 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000270 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
271 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000272
Eric Christophercccae792015-01-30 22:02:31 +0000273 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000274 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000275 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000276 // Emit the following code into the entry block:
277 // InVRSAVE = MFVRSAVE
278 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
279 // MTVRSAVE UpdatedVRSAVE
280 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000281 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
282 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000283 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000284 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000285
Chris Lattner1678a6c2006-03-16 18:25:23 +0000286 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000287 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000288 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000289 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000290
Chris Lattner1678a6c2006-03-16 18:25:23 +0000291 // Skip over all terminator instructions, which are part of the return
292 // sequence.
293 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000294 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000295 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000296
Chris Lattner1678a6c2006-03-16 18:25:23 +0000297 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000298 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000299 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000300 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000301}
Chris Lattner8ae95252005-09-03 01:17:22 +0000302
Chris Lattner1678a6c2006-03-16 18:25:23 +0000303
Chris Lattner45640392005-08-19 22:38:53 +0000304/// getGlobalBaseReg - Output the instructions required to put the
305/// base address to use for accessing globals into a register.
306///
Evan Cheng61413a32006-08-26 05:34:46 +0000307SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000308 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000309 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000310 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000311 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000312 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000313 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000314 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000315
Eric Christopher1b8e7632014-05-22 01:07:24 +0000316 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000317 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000318 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000319 if (M->getPICLevel() == PICLevel::Small) {
320 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
321 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000322 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000323 } else {
324 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
325 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
326 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
327 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000328 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000329 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
330 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
331 }
332 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000333 GlobalBaseReg =
334 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000335 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
336 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000337 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000338 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000339 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000340 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000341 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000342 }
Chris Lattner45640392005-08-19 22:38:53 +0000343 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000344 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000345 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000346}
347
348/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
349/// or 64-bit immediate, and if the value can be accurately represented as a
350/// sign extension from a 16-bit value. If so, this returns true and the
351/// immediate.
352static bool isIntS16Immediate(SDNode *N, short &Imm) {
353 if (N->getOpcode() != ISD::Constant)
354 return false;
355
Dan Gohmaneffb8942008-09-12 16:56:44 +0000356 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000357 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000358 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000359 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000360 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000361}
362
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000363static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000364 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000365}
366
367
Chris Lattner97b3da12006-06-27 00:04:13 +0000368/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
369/// operand. If so Imm will receive the 32-bit value.
370static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000371 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000372 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000373 return true;
374 }
375 return false;
376}
377
Chris Lattner97b3da12006-06-27 00:04:13 +0000378/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
379/// operand. If so Imm will receive the 64-bit value.
380static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000381 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000382 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000383 return true;
384 }
385 return false;
386}
387
388// isInt32Immediate - This method tests to see if a constant operand.
389// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000390static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000391 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000392}
393
394
395// isOpcWithIntImmediate - This method tests to see if the node is a specific
396// opcode and that it has a immediate integer right operand.
397// If so Imm will receive the 32 bit value.
398static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000399 return N->getOpcode() == Opc
400 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000401}
402
Hal Finkelb5e9b042014-12-11 22:51:06 +0000403SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
404 SDLoc dl(SN);
405 int FI = cast<FrameIndexSDNode>(N)->getIndex();
406 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
407 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
408 if (SN->hasOneUse())
409 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000410 getSmallIPtrImm(Offset, dl));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000411 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000412 getSmallIPtrImm(Offset, dl));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000413}
414
Andrew Trickc416ba62010-12-24 04:28:06 +0000415bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
416 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000417 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000418 // Don't even go down this path for i64, since different logic will be
419 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000420 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000421 return false;
422
Nate Begemanb3821a32005-08-18 07:30:46 +0000423 unsigned Shift = 32;
424 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
425 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000426 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000427 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000428 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000429
Nate Begemanb3821a32005-08-18 07:30:46 +0000430 if (Opcode == ISD::SHL) {
431 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000432 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000433 // determine which bits are made indeterminant by shift
434 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000435 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000436 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000437 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000438 // determine which bits are made indeterminant by shift
439 Indeterminant = ~(0xFFFFFFFFu >> Shift);
440 // adjust for the left rotate
441 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000442 } else if (Opcode == ISD::ROTL) {
443 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000444 } else {
445 return false;
446 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000447
Nate Begemanb3821a32005-08-18 07:30:46 +0000448 // if the mask doesn't intersect any Indeterminant bits
449 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000450 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000451 // make sure the mask is still a mask (wrap arounds may not be)
452 return isRunOfOnes(Mask, MB, ME);
453 }
454 return false;
455}
456
Nate Begeman93c4bc62005-08-19 00:38:14 +0000457/// SelectBitfieldInsert - turn an or of two masked values into
458/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000459SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000460 SDValue Op0 = N->getOperand(0);
461 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000462 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000463
Dan Gohmanf19609a2008-02-27 01:23:58 +0000464 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000465 CurDAG->computeKnownBits(Op0, LKZ, LKO);
466 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000467
Dan Gohmanf19609a2008-02-27 01:23:58 +0000468 unsigned TargetMask = LKZ.getZExtValue();
469 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000470
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000471 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
472 unsigned Op0Opc = Op0.getOpcode();
473 unsigned Op1Opc = Op1.getOpcode();
474 unsigned Value, SH = 0;
475 TargetMask = ~TargetMask;
476 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000477
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000478 // If the LHS has a foldable shift and the RHS does not, then swap it to the
479 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000480 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
481 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
482 Op0.getOperand(0).getOpcode() == ISD::SRL) {
483 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
484 Op1.getOperand(0).getOpcode() != ISD::SRL) {
485 std::swap(Op0, Op1);
486 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000487 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000488 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000489 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000490 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
491 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
492 Op1.getOperand(0).getOpcode() != ISD::SRL) {
493 std::swap(Op0, Op1);
494 std::swap(Op0Opc, Op1Opc);
495 std::swap(TargetMask, InsertMask);
496 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000497 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000498
Nate Begeman1333cea2006-05-07 00:23:38 +0000499 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000500 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000501 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000502
503 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000504 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000505 Op1 = Op1.getOperand(0);
506 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
507 }
508 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000509 // The AND mask might not be a constant, and we need to make sure that
510 // if we're going to fold the masking with the insert, all bits not
511 // know to be zero in the mask are known to be one.
512 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000513 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000514 bool CanFoldMask = InsertMask == MKO.getZExtValue();
515
Nate Begeman1333cea2006-05-07 00:23:38 +0000516 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000517 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000518 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000519 // Note that Value must be in range here (less than 32) because
520 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000521 Op1 = Op1.getOperand(0).getOperand(0);
522 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000523 }
524 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000525
Chris Lattnera2963392006-05-12 16:29:37 +0000526 SH &= 31;
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000527 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
528 getI32Imm(ME, dl) };
Michael Liaob53d8962013-04-19 22:22:57 +0000529 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000530 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000531 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000532 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000533}
534
Hal Finkelc58ce412015-01-01 02:53:29 +0000535// Predict the number of instructions that would be generated by calling
536// SelectInt64(N).
Hal Finkelca6375f2015-01-04 12:35:03 +0000537static unsigned SelectInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000538 // Assume no remaining bits.
539 unsigned Remainder = 0;
540 // Assume no shift required.
541 unsigned Shift = 0;
542
543 // If it can't be represented as a 32 bit value.
544 if (!isInt<32>(Imm)) {
545 Shift = countTrailingZeros<uint64_t>(Imm);
546 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
547
548 // If the shifted value fits 32 bits.
549 if (isInt<32>(ImmSh)) {
550 // Go with the shifted value.
551 Imm = ImmSh;
552 } else {
553 // Still stuck with a 64 bit value.
554 Remainder = Imm;
555 Shift = 32;
556 Imm >>= 32;
557 }
558 }
559
560 // Intermediate operand.
561 unsigned Result = 0;
562
563 // Handle first 32 bits.
564 unsigned Lo = Imm & 0xFFFF;
565 unsigned Hi = (Imm >> 16) & 0xFFFF;
566
567 // Simple value.
568 if (isInt<16>(Imm)) {
569 // Just the Lo bits.
570 ++Result;
571 } else if (Lo) {
572 // Handle the Hi bits and Lo bits.
573 Result += 2;
574 } else {
575 // Just the Hi bits.
576 ++Result;
577 }
578
579 // If no shift, we're done.
580 if (!Shift) return Result;
581
582 // Shift for next step if the upper 32-bits were not zero.
583 if (Imm)
584 ++Result;
585
586 // Add in the last bits as required.
587 if ((Hi = (Remainder >> 16) & 0xFFFF))
588 ++Result;
589 if ((Lo = Remainder & 0xFFFF))
590 ++Result;
591
592 return Result;
593}
594
Hal Finkel241ba792015-01-04 15:43:55 +0000595static uint64_t Rot64(uint64_t Imm, unsigned R) {
596 return (Imm << R) | (Imm >> (64 - R));
597}
598
Hal Finkelca6375f2015-01-04 12:35:03 +0000599static unsigned SelectInt64Count(int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000600 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000601 if (Count == 1)
602 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000603
Hal Finkel241ba792015-01-04 15:43:55 +0000604 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000605 uint64_t RImm = Rot64(Imm, r);
606 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
607 Count = std::min(Count, RCount);
608
609 // See comments in SelectInt64 for an explanation of the logic below.
610 unsigned LS = findLastSet(RImm);
611 if (LS != r-1)
612 continue;
613
614 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
615 uint64_t RImmWithOnes = RImm | OnesMask;
616
617 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000618 Count = std::min(Count, RCount);
619 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000620
Hal Finkel241ba792015-01-04 15:43:55 +0000621 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000622}
623
Hal Finkelc58ce412015-01-01 02:53:29 +0000624// Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count
625// (above) needs to be kept in sync with this function.
Hal Finkelca6375f2015-01-04 12:35:03 +0000626static SDNode *SelectInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000627 // Assume no remaining bits.
628 unsigned Remainder = 0;
629 // Assume no shift required.
630 unsigned Shift = 0;
631
632 // If it can't be represented as a 32 bit value.
633 if (!isInt<32>(Imm)) {
634 Shift = countTrailingZeros<uint64_t>(Imm);
635 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
636
637 // If the shifted value fits 32 bits.
638 if (isInt<32>(ImmSh)) {
639 // Go with the shifted value.
640 Imm = ImmSh;
641 } else {
642 // Still stuck with a 64 bit value.
643 Remainder = Imm;
644 Shift = 32;
645 Imm >>= 32;
646 }
647 }
648
649 // Intermediate operand.
650 SDNode *Result;
651
652 // Handle first 32 bits.
653 unsigned Lo = Imm & 0xFFFF;
654 unsigned Hi = (Imm >> 16) & 0xFFFF;
655
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000656 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
657 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000658 };
659
660 // Simple value.
661 if (isInt<16>(Imm)) {
662 // Just the Lo bits.
663 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
664 } else if (Lo) {
665 // Handle the Hi bits.
666 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
667 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
668 // And Lo bits.
669 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
670 SDValue(Result, 0), getI32Imm(Lo));
671 } else {
672 // Just the Hi bits.
673 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
674 }
675
676 // If no shift, we're done.
677 if (!Shift) return Result;
678
679 // Shift for next step if the upper 32-bits were not zero.
680 if (Imm) {
681 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
682 SDValue(Result, 0),
683 getI32Imm(Shift),
684 getI32Imm(63 - Shift));
685 }
686
687 // Add in the last bits as required.
688 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
689 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
690 SDValue(Result, 0), getI32Imm(Hi));
691 }
692 if ((Lo = Remainder & 0xFFFF)) {
693 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
694 SDValue(Result, 0), getI32Imm(Lo));
695 }
696
697 return Result;
698}
699
Hal Finkelca6375f2015-01-04 12:35:03 +0000700static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000701 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000702 if (Count == 1)
703 return SelectInt64Direct(CurDAG, dl, Imm);
704
Hal Finkel241ba792015-01-04 15:43:55 +0000705 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000706
Hal Finkel2f618792015-01-05 03:41:38 +0000707 int64_t MatImm;
708 unsigned MaskEnd;
709
Hal Finkel241ba792015-01-04 15:43:55 +0000710 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000711 uint64_t RImm = Rot64(Imm, r);
712 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000713 if (RCount < Count) {
714 Count = RCount;
715 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000716 MatImm = RImm;
717 MaskEnd = 63;
718 }
719
720 // If the immediate to generate has many trailing zeros, it might be
721 // worthwhile to generate a rotated value with too many leading ones
722 // (because that's free with li/lis's sign-extension semantics), and then
723 // mask them off after rotation.
724
725 unsigned LS = findLastSet(RImm);
726 // We're adding (63-LS) higher-order ones, and we expect to mask them off
727 // after performing the inverse rotation by (64-r). So we need that:
728 // 63-LS == 64-r => LS == r-1
729 if (LS != r-1)
730 continue;
731
732 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
733 uint64_t RImmWithOnes = RImm | OnesMask;
734
735 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
736 if (RCount < Count) {
737 Count = RCount;
738 RMin = r;
739 MatImm = RImmWithOnes;
740 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000741 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000742 }
743
Hal Finkel241ba792015-01-04 15:43:55 +0000744 if (!RMin)
745 return SelectInt64Direct(CurDAG, dl, Imm);
746
Sergey Dmitroukadb4c692015-04-28 11:56:37 +0000747 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
748 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000749 };
750
Hal Finkel2f618792015-01-05 03:41:38 +0000751 SDValue Val = SDValue(SelectInt64Direct(CurDAG, dl, MatImm), 0);
752 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
753 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000754}
755
Hal Finkelc58ce412015-01-01 02:53:29 +0000756// Select a 64-bit constant.
757static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) {
758 SDLoc dl(N);
759
760 // Get 64 bit value.
761 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
762 return SelectInt64(CurDAG, dl, Imm);
763}
764
Hal Finkel8adf2252014-12-16 05:51:41 +0000765namespace {
766class BitPermutationSelector {
767 struct ValueBit {
768 SDValue V;
769
770 // The bit number in the value, using a convention where bit 0 is the
771 // lowest-order bit.
772 unsigned Idx;
773
774 enum Kind {
775 ConstZero,
776 Variable
777 } K;
778
779 ValueBit(SDValue V, unsigned I, Kind K = Variable)
780 : V(V), Idx(I), K(K) {}
781 ValueBit(Kind K = Variable)
782 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
783
784 bool isZero() const {
785 return K == ConstZero;
786 }
787
788 bool hasValue() const {
789 return K == Variable;
790 }
791
792 SDValue getValue() const {
793 assert(hasValue() && "Cannot get the value of a constant bit");
794 return V;
795 }
796
797 unsigned getValueBitIndex() const {
798 assert(hasValue() && "Cannot get the value bit index of a constant bit");
799 return Idx;
800 }
801 };
802
803 // A bit group has the same underlying value and the same rotate factor.
804 struct BitGroup {
805 SDValue V;
806 unsigned RLAmt;
807 unsigned StartIdx, EndIdx;
808
Hal Finkelc58ce412015-01-01 02:53:29 +0000809 // This rotation amount assumes that the lower 32 bits of the quantity are
810 // replicated in the high 32 bits by the rotation operator (which is done
811 // by rlwinm and friends in 64-bit mode).
812 bool Repl32;
813 // Did converting to Repl32 == true change the rotation factor? If it did,
814 // it decreased it by 32.
815 bool Repl32CR;
816 // Was this group coalesced after setting Repl32 to true?
817 bool Repl32Coalesced;
818
Hal Finkel8adf2252014-12-16 05:51:41 +0000819 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000820 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
821 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000822 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
823 " [" << S << ", " << E << "]\n");
824 }
825 };
826
827 // Information on each (Value, RLAmt) pair (like the number of groups
828 // associated with each) used to choose the lowering method.
829 struct ValueRotInfo {
830 SDValue V;
831 unsigned RLAmt;
832 unsigned NumGroups;
833 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000834 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000835
836 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000837 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
838 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000839
840 // For sorting (in reverse order) by NumGroups, and then by
841 // FirstGroupStartIdx.
842 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000843 // We need to sort so that the non-Repl32 come first because, when we're
844 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
845 // masking operation.
846 if (Repl32 < Other.Repl32)
847 return true;
848 else if (Repl32 > Other.Repl32)
849 return false;
850 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000851 return true;
852 else if (NumGroups < Other.NumGroups)
853 return false;
854 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
855 return true;
856 return false;
857 }
858 };
859
860 // Return true if something interesting was deduced, return false if we're
861 // providing only a generic representation of V (or something else likewise
862 // uninteresting for instruction selection).
863 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
864 switch (V.getOpcode()) {
865 default: break;
866 case ISD::ROTL:
867 if (isa<ConstantSDNode>(V.getOperand(1))) {
868 unsigned RotAmt = V.getConstantOperandVal(1);
869
870 SmallVector<ValueBit, 64> LHSBits(Bits.size());
871 getValueBits(V.getOperand(0), LHSBits);
872
873 for (unsigned i = 0; i < Bits.size(); ++i)
874 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
875
876 return true;
877 }
878 break;
879 case ISD::SHL:
880 if (isa<ConstantSDNode>(V.getOperand(1))) {
881 unsigned ShiftAmt = V.getConstantOperandVal(1);
882
883 SmallVector<ValueBit, 64> LHSBits(Bits.size());
884 getValueBits(V.getOperand(0), LHSBits);
885
886 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
887 Bits[i] = LHSBits[i - ShiftAmt];
888
889 for (unsigned i = 0; i < ShiftAmt; ++i)
890 Bits[i] = ValueBit(ValueBit::ConstZero);
891
892 return true;
893 }
894 break;
895 case ISD::SRL:
896 if (isa<ConstantSDNode>(V.getOperand(1))) {
897 unsigned ShiftAmt = V.getConstantOperandVal(1);
898
899 SmallVector<ValueBit, 64> LHSBits(Bits.size());
900 getValueBits(V.getOperand(0), LHSBits);
901
902 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
903 Bits[i] = LHSBits[i + ShiftAmt];
904
905 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
906 Bits[i] = ValueBit(ValueBit::ConstZero);
907
908 return true;
909 }
910 break;
911 case ISD::AND:
912 if (isa<ConstantSDNode>(V.getOperand(1))) {
913 uint64_t Mask = V.getConstantOperandVal(1);
914
915 SmallVector<ValueBit, 64> LHSBits(Bits.size());
916 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
917
918 for (unsigned i = 0; i < Bits.size(); ++i)
919 if (((Mask >> i) & 1) == 1)
920 Bits[i] = LHSBits[i];
921 else
922 Bits[i] = ValueBit(ValueBit::ConstZero);
923
924 // Mark this as interesting, only if the LHS was also interesting. This
925 // prevents the overall procedure from matching a single immediate 'and'
926 // (which is non-optimal because such an and might be folded with other
927 // things if we don't select it here).
928 return LHSTrivial;
929 }
930 break;
931 case ISD::OR: {
932 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
933 getValueBits(V.getOperand(0), LHSBits);
934 getValueBits(V.getOperand(1), RHSBits);
935
936 bool AllDisjoint = true;
937 for (unsigned i = 0; i < Bits.size(); ++i)
938 if (LHSBits[i].isZero())
939 Bits[i] = RHSBits[i];
940 else if (RHSBits[i].isZero())
941 Bits[i] = LHSBits[i];
942 else {
943 AllDisjoint = false;
944 break;
945 }
946
947 if (!AllDisjoint)
948 break;
949
950 return true;
951 }
952 }
953
954 for (unsigned i = 0; i < Bits.size(); ++i)
955 Bits[i] = ValueBit(V, i);
956
957 return false;
958 }
959
960 // For each value (except the constant ones), compute the left-rotate amount
961 // to get it from its original to final position.
962 void computeRotationAmounts() {
963 HasZeros = false;
964 RLAmt.resize(Bits.size());
965 for (unsigned i = 0; i < Bits.size(); ++i)
966 if (Bits[i].hasValue()) {
967 unsigned VBI = Bits[i].getValueBitIndex();
968 if (i >= VBI)
969 RLAmt[i] = i - VBI;
970 else
971 RLAmt[i] = Bits.size() - (VBI - i);
972 } else if (Bits[i].isZero()) {
973 HasZeros = true;
974 RLAmt[i] = UINT32_MAX;
975 } else {
976 llvm_unreachable("Unknown value bit type");
977 }
978 }
979
980 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +0000981 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
982 // they break up groups.
983 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000984 BitGroups.clear();
985
986 unsigned LastRLAmt = RLAmt[0];
987 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
988 unsigned LastGroupStartIdx = 0;
989 for (unsigned i = 1; i < Bits.size(); ++i) {
990 unsigned ThisRLAmt = RLAmt[i];
991 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +0000992 if (LateMask && !ThisValue) {
993 ThisValue = LastValue;
994 ThisRLAmt = LastRLAmt;
995 // If we're doing late masking, then the first bit group always starts
996 // at zero (even if the first bits were zero).
997 if (BitGroups.empty())
998 LastGroupStartIdx = 0;
999 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001000
1001 // If this bit has the same underlying value and the same rotate factor as
1002 // the last one, then they're part of the same group.
1003 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1004 continue;
1005
1006 if (LastValue.getNode())
1007 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1008 i-1));
1009 LastRLAmt = ThisRLAmt;
1010 LastValue = ThisValue;
1011 LastGroupStartIdx = i;
1012 }
1013 if (LastValue.getNode())
1014 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1015 Bits.size()-1));
1016
1017 if (BitGroups.empty())
1018 return;
1019
1020 // We might be able to combine the first and last groups.
1021 if (BitGroups.size() > 1) {
1022 // If the first and last groups are the same, then remove the first group
1023 // in favor of the last group, making the ending index of the last group
1024 // equal to the ending index of the to-be-removed first group.
1025 if (BitGroups[0].StartIdx == 0 &&
1026 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1027 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1028 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001029 DEBUG(dbgs() << "\tcombining final bit group with inital one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001030 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1031 BitGroups.erase(BitGroups.begin());
1032 }
1033 }
1034 }
1035
1036 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1037 // associated with each. If there is a degeneracy, pick the one that occurs
1038 // first (in the final value).
1039 void collectValueRotInfo() {
1040 ValueRots.clear();
1041
1042 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001043 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1044 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001045 VRI.V = BG.V;
1046 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001047 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001048 VRI.NumGroups += 1;
1049 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1050 }
1051
1052 // Now that we've collected the various ValueRotInfo instances, we need to
1053 // sort them.
1054 ValueRotsVec.clear();
1055 for (auto &I : ValueRots) {
1056 ValueRotsVec.push_back(I.second);
1057 }
1058 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1059 }
1060
Hal Finkelc58ce412015-01-01 02:53:29 +00001061 // In 64-bit mode, rlwinm and friends have a rotation operator that
1062 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1063 // indices of these instructions can only be in the lower 32 bits, so they
1064 // can only represent some 64-bit bit groups. However, when they can be used,
1065 // the 32-bit replication can be used to represent, as a single bit group,
1066 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1067 // groups when possible. Returns true if any of the bit groups were
1068 // converted.
1069 void assignRepl32BitGroups() {
1070 // If we have bits like this:
1071 //
1072 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1073 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1074 // Groups: | RLAmt = 8 | RLAmt = 40 |
1075 //
1076 // But, making use of a 32-bit operation that replicates the low-order 32
1077 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1078 // of 8.
1079
1080 auto IsAllLow32 = [this](BitGroup & BG) {
1081 if (BG.StartIdx <= BG.EndIdx) {
1082 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1083 if (!Bits[i].hasValue())
1084 continue;
1085 if (Bits[i].getValueBitIndex() >= 32)
1086 return false;
1087 }
1088 } else {
1089 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1090 if (!Bits[i].hasValue())
1091 continue;
1092 if (Bits[i].getValueBitIndex() >= 32)
1093 return false;
1094 }
1095 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1096 if (!Bits[i].hasValue())
1097 continue;
1098 if (Bits[i].getValueBitIndex() >= 32)
1099 return false;
1100 }
1101 }
1102
1103 return true;
1104 };
1105
1106 for (auto &BG : BitGroups) {
1107 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1108 if (IsAllLow32(BG)) {
1109 if (BG.RLAmt >= 32) {
1110 BG.RLAmt -= 32;
1111 BG.Repl32CR = true;
1112 }
1113
1114 BG.Repl32 = true;
1115
1116 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1117 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1118 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1119 }
1120 }
1121 }
1122
1123 // Now walk through the bit groups, consolidating where possible.
1124 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1125 // We might want to remove this bit group by merging it with the previous
1126 // group (which might be the ending group).
1127 auto IP = (I == BitGroups.begin()) ?
1128 std::prev(BitGroups.end()) : std::prev(I);
1129 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1130 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1131
1132 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1133 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1134 " [" << I->StartIdx << ", " << I->EndIdx <<
1135 "] with group with range [" <<
1136 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1137
1138 IP->EndIdx = I->EndIdx;
1139 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1140 IP->Repl32Coalesced = true;
1141 I = BitGroups.erase(I);
1142 continue;
1143 } else {
1144 // There is a special case worth handling: If there is a single group
1145 // covering the entire upper 32 bits, and it can be merged with both
1146 // the next and previous groups (which might be the same group), then
1147 // do so. If it is the same group (so there will be only one group in
1148 // total), then we need to reverse the order of the range so that it
1149 // covers the entire 64 bits.
1150 if (I->StartIdx == 32 && I->EndIdx == 63) {
1151 assert(std::next(I) == BitGroups.end() &&
1152 "bit group ends at index 63 but there is another?");
1153 auto IN = BitGroups.begin();
1154
1155 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
1156 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1157 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1158 IsAllLow32(*I)) {
1159
1160 DEBUG(dbgs() << "\tcombining bit group for " <<
1161 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1162 " [" << I->StartIdx << ", " << I->EndIdx <<
1163 "] with 32-bit replicated groups with ranges [" <<
1164 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1165 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1166
1167 if (IP == IN) {
1168 // There is only one other group; change it to cover the whole
1169 // range (backward, so that it can still be Repl32 but cover the
1170 // whole 64-bit range).
1171 IP->StartIdx = 31;
1172 IP->EndIdx = 30;
1173 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1174 IP->Repl32Coalesced = true;
1175 I = BitGroups.erase(I);
1176 } else {
1177 // There are two separate groups, one before this group and one
1178 // after us (at the beginning). We're going to remove this group,
1179 // but also the group at the very beginning.
1180 IP->EndIdx = IN->EndIdx;
1181 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1182 IP->Repl32Coalesced = true;
1183 I = BitGroups.erase(I);
1184 BitGroups.erase(BitGroups.begin());
1185 }
1186
1187 // This must be the last group in the vector (and we might have
1188 // just invalidated the iterator above), so break here.
1189 break;
1190 }
1191 }
1192 }
1193
1194 ++I;
1195 }
1196 }
1197
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001198 SDValue getI32Imm(unsigned Imm, SDLoc dl) {
1199 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001200 }
1201
Hal Finkelc58ce412015-01-01 02:53:29 +00001202 uint64_t getZerosMask() {
1203 uint64_t Mask = 0;
1204 for (unsigned i = 0; i < Bits.size(); ++i) {
1205 if (Bits[i].hasValue())
1206 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001207 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001208 }
1209
1210 return ~Mask;
1211 }
1212
Hal Finkel8adf2252014-12-16 05:51:41 +00001213 // Depending on the number of groups for a particular value, it might be
1214 // better to rotate, mask explicitly (using andi/andis), and then or the
1215 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001216 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1217 if (BPermRewriterNoMasking)
1218 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001219
1220 for (ValueRotInfo &VRI : ValueRotsVec) {
1221 unsigned Mask = 0;
1222 for (unsigned i = 0; i < Bits.size(); ++i) {
1223 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1224 continue;
1225 if (RLAmt[i] != VRI.RLAmt)
1226 continue;
1227 Mask |= (1u << i);
1228 }
1229
1230 // Compute the masks for andi/andis that would be necessary.
1231 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1232 assert((ANDIMask != 0 || ANDISMask != 0) &&
1233 "No set bits in mask for value bit groups");
1234 bool NeedsRotate = VRI.RLAmt != 0;
1235
1236 // We're trying to minimize the number of instructions. If we have one
1237 // group, using one of andi/andis can break even. If we have three
1238 // groups, we can use both andi and andis and break even (to use both
1239 // andi and andis we also need to or the results together). We need four
1240 // groups if we also need to rotate. To use andi/andis we need to do more
1241 // than break even because rotate-and-mask instructions tend to be easier
1242 // to schedule.
1243
1244 // FIXME: We've biased here against using andi/andis, which is right for
1245 // POWER cores, but not optimal everywhere. For example, on the A2,
1246 // andi/andis have single-cycle latency whereas the rotate-and-mask
1247 // instructions take two cycles, and it would be better to bias toward
1248 // andi/andis in break-even cases.
1249
1250 unsigned NumAndInsts = (unsigned) NeedsRotate +
1251 (unsigned) (ANDIMask != 0) +
1252 (unsigned) (ANDISMask != 0) +
1253 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1254 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001255
1256 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1257 " RL: " << VRI.RLAmt << ":" <<
1258 "\n\t\t\tisel using masking: " << NumAndInsts <<
1259 " using rotates: " << VRI.NumGroups << "\n");
1260
Hal Finkel8adf2252014-12-16 05:51:41 +00001261 if (NumAndInsts >= VRI.NumGroups)
1262 continue;
1263
Hal Finkelc58ce412015-01-01 02:53:29 +00001264 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1265
1266 if (InstCnt) *InstCnt += NumAndInsts;
1267
Hal Finkel8adf2252014-12-16 05:51:41 +00001268 SDValue VRot;
1269 if (VRI.RLAmt) {
1270 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001271 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1272 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001273 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1274 Ops), 0);
1275 } else {
1276 VRot = VRI.V;
1277 }
1278
1279 SDValue ANDIVal, ANDISVal;
1280 if (ANDIMask != 0)
1281 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001282 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001283 if (ANDISMask != 0)
1284 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001285 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001286
1287 SDValue TotalVal;
1288 if (!ANDIVal)
1289 TotalVal = ANDISVal;
1290 else if (!ANDISVal)
1291 TotalVal = ANDIVal;
1292 else
1293 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1294 ANDIVal, ANDISVal), 0);
1295
1296 if (!Res)
1297 Res = TotalVal;
1298 else
1299 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1300 Res, TotalVal), 0);
1301
1302 // Now, remove all groups with this underlying value and rotation
1303 // factor.
1304 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1305 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1306 I = BitGroups.erase(I);
1307 else
1308 ++I;
1309 }
1310 }
1311 }
1312
1313 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001314 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001315 SDLoc dl(N);
1316 SDValue Res;
1317
Hal Finkelc58ce412015-01-01 02:53:29 +00001318 if (InstCnt) *InstCnt = 0;
1319
Hal Finkel8adf2252014-12-16 05:51:41 +00001320 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001321 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001322
1323 // If we've not yet selected a 'starting' instruction, and we have no zeros
1324 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1325 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001326 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001327 ValueRotInfo &VRI = ValueRotsVec[0];
1328 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001329 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001330 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001331 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1332 getI32Imm(31, dl) };
1333 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1334 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001335 } else {
1336 Res = VRI.V;
1337 }
1338
1339 // Now, remove all groups with this underlying value and rotation factor.
1340 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1341 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1342 I = BitGroups.erase(I);
1343 else
1344 ++I;
1345 }
1346 }
1347
Hal Finkelc58ce412015-01-01 02:53:29 +00001348 if (InstCnt) *InstCnt += BitGroups.size();
1349
Hal Finkel8adf2252014-12-16 05:51:41 +00001350 // Insert the other groups (one at a time).
1351 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001352 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001353 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001354 { BG.V, getI32Imm(BG.RLAmt, dl),
1355 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1356 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001357 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1358 } else {
1359 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001360 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1361 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1362 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001363 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1364 }
1365 }
1366
Hal Finkelc58ce412015-01-01 02:53:29 +00001367 if (LateMask) {
1368 unsigned Mask = (unsigned) getZerosMask();
1369
1370 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1371 assert((ANDIMask != 0 || ANDISMask != 0) &&
1372 "No set bits in zeros mask?");
1373
1374 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1375 (unsigned) (ANDISMask != 0) +
1376 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1377
1378 SDValue ANDIVal, ANDISVal;
1379 if (ANDIMask != 0)
1380 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001381 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001382 if (ANDISMask != 0)
1383 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001384 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001385
1386 if (!ANDIVal)
1387 Res = ANDISVal;
1388 else if (!ANDISVal)
1389 Res = ANDIVal;
1390 else
1391 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1392 ANDIVal, ANDISVal), 0);
1393 }
1394
Hal Finkel8adf2252014-12-16 05:51:41 +00001395 return Res.getNode();
1396 }
1397
Hal Finkelc58ce412015-01-01 02:53:29 +00001398 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1399 unsigned MaskStart, unsigned MaskEnd,
1400 bool IsIns) {
1401 // In the notation used by the instructions, 'start' and 'end' are reversed
1402 // because bits are counted from high to low order.
1403 unsigned InstMaskStart = 64 - MaskEnd - 1,
1404 InstMaskEnd = 64 - MaskStart - 1;
1405
1406 if (Repl32)
1407 return 1;
1408
1409 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1410 InstMaskEnd == 63 - RLAmt)
1411 return 1;
1412
1413 return 2;
1414 }
1415
1416 // For 64-bit values, not all combinations of rotates and masks are
1417 // available. Produce one if it is available.
1418 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1419 unsigned MaskStart, unsigned MaskEnd,
1420 unsigned *InstCnt = nullptr) {
1421 // In the notation used by the instructions, 'start' and 'end' are reversed
1422 // because bits are counted from high to low order.
1423 unsigned InstMaskStart = 64 - MaskEnd - 1,
1424 InstMaskEnd = 64 - MaskStart - 1;
1425
1426 if (InstCnt) *InstCnt += 1;
1427
1428 if (Repl32) {
1429 // This rotation amount assumes that the lower 32 bits of the quantity
1430 // are replicated in the high 32 bits by the rotation operator (which is
1431 // done by rlwinm and friends).
1432 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1433 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1434 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001435 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1436 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001437 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1438 Ops), 0);
1439 }
1440
1441 if (InstMaskEnd == 63) {
1442 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001443 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001444 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1445 }
1446
1447 if (InstMaskStart == 0) {
1448 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001449 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001450 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1451 }
1452
1453 if (InstMaskEnd == 63 - RLAmt) {
1454 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001455 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001456 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1457 }
1458
1459 // We cannot do this with a single instruction, so we'll use two. The
1460 // problem is that we're not free to choose both a rotation amount and mask
1461 // start and end independently. We can choose an arbitrary mask start and
1462 // end, but then the rotation amount is fixed. Rotation, however, can be
1463 // inverted, and so by applying an "inverse" rotation first, we can get the
1464 // desired result.
1465 if (InstCnt) *InstCnt += 1;
1466
1467 // The rotation mask for the second instruction must be MaskStart.
1468 unsigned RLAmt2 = MaskStart;
1469 // The first instruction must rotate V so that the overall rotation amount
1470 // is RLAmt.
1471 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1472 if (RLAmt1)
1473 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1474 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1475 }
1476
1477 // For 64-bit values, not all combinations of rotates and masks are
1478 // available. Produce a rotate-mask-and-insert if one is available.
1479 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1480 bool Repl32, unsigned MaskStart,
1481 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1482 // In the notation used by the instructions, 'start' and 'end' are reversed
1483 // because bits are counted from high to low order.
1484 unsigned InstMaskStart = 64 - MaskEnd - 1,
1485 InstMaskEnd = 64 - MaskStart - 1;
1486
1487 if (InstCnt) *InstCnt += 1;
1488
1489 if (Repl32) {
1490 // This rotation amount assumes that the lower 32 bits of the quantity
1491 // are replicated in the high 32 bits by the rotation operator (which is
1492 // done by rlwinm and friends).
1493 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1494 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1495 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001496 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1497 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001498 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1499 Ops), 0);
1500 }
1501
1502 if (InstMaskEnd == 63 - RLAmt) {
1503 SDValue Ops[] =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001504 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001505 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1506 }
1507
1508 // We cannot do this with a single instruction, so we'll use two. The
1509 // problem is that we're not free to choose both a rotation amount and mask
1510 // start and end independently. We can choose an arbitrary mask start and
1511 // end, but then the rotation amount is fixed. Rotation, however, can be
1512 // inverted, and so by applying an "inverse" rotation first, we can get the
1513 // desired result.
1514 if (InstCnt) *InstCnt += 1;
1515
1516 // The rotation mask for the second instruction must be MaskStart.
1517 unsigned RLAmt2 = MaskStart;
1518 // The first instruction must rotate V so that the overall rotation amount
1519 // is RLAmt.
1520 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1521 if (RLAmt1)
1522 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1523 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1524 }
1525
1526 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1527 if (BPermRewriterNoMasking)
1528 return;
1529
1530 // The idea here is the same as in the 32-bit version, but with additional
1531 // complications from the fact that Repl32 might be true. Because we
1532 // aggressively convert bit groups to Repl32 form (which, for small
1533 // rotation factors, involves no other change), and then coalesce, it might
1534 // be the case that a single 64-bit masking operation could handle both
1535 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1536 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1537 // completely capture the new combined bit group.
1538
1539 for (ValueRotInfo &VRI : ValueRotsVec) {
1540 uint64_t Mask = 0;
1541
1542 // We need to add to the mask all bits from the associated bit groups.
1543 // If Repl32 is false, we need to add bits from bit groups that have
1544 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1545 // group is trivially convertable if it overlaps only with the lower 32
1546 // bits, and the group has not been coalesced.
1547 auto MatchingBG = [VRI](BitGroup &BG) {
1548 if (VRI.V != BG.V)
1549 return false;
1550
1551 unsigned EffRLAmt = BG.RLAmt;
1552 if (!VRI.Repl32 && BG.Repl32) {
1553 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1554 !BG.Repl32Coalesced) {
1555 if (BG.Repl32CR)
1556 EffRLAmt += 32;
1557 } else {
1558 return false;
1559 }
1560 } else if (VRI.Repl32 != BG.Repl32) {
1561 return false;
1562 }
1563
1564 if (VRI.RLAmt != EffRLAmt)
1565 return false;
1566
1567 return true;
1568 };
1569
1570 for (auto &BG : BitGroups) {
1571 if (!MatchingBG(BG))
1572 continue;
1573
1574 if (BG.StartIdx <= BG.EndIdx) {
1575 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001576 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001577 } else {
1578 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001579 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001580 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001581 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001582 }
1583 }
1584
1585 // We can use the 32-bit andi/andis technique if the mask does not
1586 // require any higher-order bits. This can save an instruction compared
1587 // to always using the general 64-bit technique.
1588 bool Use32BitInsts = isUInt<32>(Mask);
1589 // Compute the masks for andi/andis that would be necessary.
1590 unsigned ANDIMask = (Mask & UINT16_MAX),
1591 ANDISMask = (Mask >> 16) & UINT16_MAX;
1592
1593 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1594
1595 unsigned NumAndInsts = (unsigned) NeedsRotate +
1596 (unsigned) (bool) Res;
1597 if (Use32BitInsts)
1598 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1599 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1600 else
1601 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1;
1602
1603 unsigned NumRLInsts = 0;
1604 bool FirstBG = true;
1605 for (auto &BG : BitGroups) {
1606 if (!MatchingBG(BG))
1607 continue;
1608 NumRLInsts +=
1609 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1610 !FirstBG);
1611 FirstBG = false;
1612 }
1613
1614 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1615 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1616 "\n\t\t\tisel using masking: " << NumAndInsts <<
1617 " using rotates: " << NumRLInsts << "\n");
1618
1619 // When we'd use andi/andis, we bias toward using the rotates (andi only
1620 // has a record form, and is cracked on POWER cores). However, when using
1621 // general 64-bit constant formation, bias toward the constant form,
1622 // because that exposes more opportunities for CSE.
1623 if (NumAndInsts > NumRLInsts)
1624 continue;
1625 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1626 continue;
1627
1628 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1629
1630 if (InstCnt) *InstCnt += NumAndInsts;
1631
1632 SDValue VRot;
1633 // We actually need to generate a rotation if we have a non-zero rotation
1634 // factor or, in the Repl32 case, if we care about any of the
1635 // higher-order replicated bits. In the latter case, we generate a mask
1636 // backward so that it actually includes the entire 64 bits.
1637 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1638 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1639 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1640 else
1641 VRot = VRI.V;
1642
1643 SDValue TotalVal;
1644 if (Use32BitInsts) {
1645 assert((ANDIMask != 0 || ANDISMask != 0) &&
1646 "No set bits in mask when using 32-bit ands for 64-bit value");
1647
1648 SDValue ANDIVal, ANDISVal;
1649 if (ANDIMask != 0)
1650 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001651 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001652 if (ANDISMask != 0)
1653 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001654 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001655
1656 if (!ANDIVal)
1657 TotalVal = ANDISVal;
1658 else if (!ANDISVal)
1659 TotalVal = ANDIVal;
1660 else
1661 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1662 ANDIVal, ANDISVal), 0);
1663 } else {
1664 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1665 TotalVal =
1666 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1667 VRot, TotalVal), 0);
1668 }
1669
1670 if (!Res)
1671 Res = TotalVal;
1672 else
1673 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1674 Res, TotalVal), 0);
1675
1676 // Now, remove all groups with this underlying value and rotation
1677 // factor.
1678 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1679 if (MatchingBG(*I))
1680 I = BitGroups.erase(I);
1681 else
1682 ++I;
1683 }
1684 }
1685 }
1686
1687 // Instruction selection for the 64-bit case.
1688 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1689 SDLoc dl(N);
1690 SDValue Res;
1691
1692 if (InstCnt) *InstCnt = 0;
1693
1694 // Take care of cases that should use andi/andis first.
1695 SelectAndParts64(dl, Res, InstCnt);
1696
1697 // If we've not yet selected a 'starting' instruction, and we have no zeros
1698 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1699 // number of groups), and start with this rotated value.
1700 if ((!HasZeros || LateMask) && !Res) {
1701 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1702 // groups will come first, and so the VRI representing the largest number
1703 // of groups might not be first (it might be the first Repl32 groups).
1704 unsigned MaxGroupsIdx = 0;
1705 if (!ValueRotsVec[0].Repl32) {
1706 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1707 if (ValueRotsVec[i].Repl32) {
1708 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1709 MaxGroupsIdx = i;
1710 break;
1711 }
1712 }
1713
1714 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1715 bool NeedsRotate = false;
1716 if (VRI.RLAmt) {
1717 NeedsRotate = true;
1718 } else if (VRI.Repl32) {
1719 for (auto &BG : BitGroups) {
1720 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1721 BG.Repl32 != VRI.Repl32)
1722 continue;
1723
1724 // We don't need a rotate if the bit group is confined to the lower
1725 // 32 bits.
1726 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1727 continue;
1728
1729 NeedsRotate = true;
1730 break;
1731 }
1732 }
1733
1734 if (NeedsRotate)
1735 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1736 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1737 InstCnt);
1738 else
1739 Res = VRI.V;
1740
1741 // Now, remove all groups with this underlying value and rotation factor.
1742 if (Res)
1743 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1744 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt && I->Repl32 == VRI.Repl32)
1745 I = BitGroups.erase(I);
1746 else
1747 ++I;
1748 }
1749 }
1750
1751 // Because 64-bit rotates are more flexible than inserts, we might have a
1752 // preference regarding which one we do first (to save one instruction).
1753 if (!Res)
1754 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1755 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1756 false) <
1757 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1758 true)) {
1759 if (I != BitGroups.begin()) {
1760 BitGroup BG = *I;
1761 BitGroups.erase(I);
1762 BitGroups.insert(BitGroups.begin(), BG);
1763 }
1764
1765 break;
1766 }
1767 }
1768
1769 // Insert the other groups (one at a time).
1770 for (auto &BG : BitGroups) {
1771 if (!Res)
1772 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1773 BG.EndIdx, InstCnt);
1774 else
1775 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1776 BG.StartIdx, BG.EndIdx, InstCnt);
1777 }
1778
1779 if (LateMask) {
1780 uint64_t Mask = getZerosMask();
1781
1782 // We can use the 32-bit andi/andis technique if the mask does not
1783 // require any higher-order bits. This can save an instruction compared
1784 // to always using the general 64-bit technique.
1785 bool Use32BitInsts = isUInt<32>(Mask);
1786 // Compute the masks for andi/andis that would be necessary.
1787 unsigned ANDIMask = (Mask & UINT16_MAX),
1788 ANDISMask = (Mask >> 16) & UINT16_MAX;
1789
1790 if (Use32BitInsts) {
1791 assert((ANDIMask != 0 || ANDISMask != 0) &&
1792 "No set bits in mask when using 32-bit ands for 64-bit value");
1793
1794 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1795 (unsigned) (ANDISMask != 0) +
1796 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1797
1798 SDValue ANDIVal, ANDISVal;
1799 if (ANDIMask != 0)
1800 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001801 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001802 if (ANDISMask != 0)
1803 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001804 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001805
1806 if (!ANDIVal)
1807 Res = ANDISVal;
1808 else if (!ANDISVal)
1809 Res = ANDIVal;
1810 else
1811 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1812 ANDIVal, ANDISVal), 0);
1813 } else {
1814 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1;
1815
1816 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1817 Res =
1818 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1819 Res, MaskVal), 0);
1820 }
1821 }
1822
1823 return Res.getNode();
1824 }
1825
1826 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1827 // Fill in BitGroups.
1828 collectBitGroups(LateMask);
1829 if (BitGroups.empty())
1830 return nullptr;
1831
1832 // For 64-bit values, figure out when we can use 32-bit instructions.
1833 if (Bits.size() == 64)
1834 assignRepl32BitGroups();
1835
1836 // Fill in ValueRotsVec.
1837 collectValueRotInfo();
1838
1839 if (Bits.size() == 32) {
1840 return Select32(N, LateMask, InstCnt);
1841 } else {
1842 assert(Bits.size() == 64 && "Not 64 bits here?");
1843 return Select64(N, LateMask, InstCnt);
1844 }
1845
1846 return nullptr;
1847 }
1848
Hal Finkel8adf2252014-12-16 05:51:41 +00001849 SmallVector<ValueBit, 64> Bits;
1850
1851 bool HasZeros;
1852 SmallVector<unsigned, 64> RLAmt;
1853
1854 SmallVector<BitGroup, 16> BitGroups;
1855
1856 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1857 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1858
1859 SelectionDAG *CurDAG;
1860
1861public:
1862 BitPermutationSelector(SelectionDAG *DAG)
1863 : CurDAG(DAG) {}
1864
1865 // Here we try to match complex bit permutations into a set of
1866 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1867 // known to produce optimial code for common cases (like i32 byte swapping).
1868 SDNode *Select(SDNode *N) {
1869 Bits.resize(N->getValueType(0).getSizeInBits());
1870 if (!getValueBits(SDValue(N, 0), Bits))
1871 return nullptr;
1872
1873 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1874 " selection for: ");
1875 DEBUG(N->dump(CurDAG));
1876
1877 // Fill it RLAmt and set HasZeros.
1878 computeRotationAmounts();
1879
Hal Finkelc58ce412015-01-01 02:53:29 +00001880 if (!HasZeros)
1881 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001882
Hal Finkelc58ce412015-01-01 02:53:29 +00001883 // We currently have two techniques for handling results with zeros: early
1884 // masking (the default) and late masking. Late masking is sometimes more
1885 // efficient, but because the structure of the bit groups is different, it
1886 // is hard to tell without generating both and comparing the results. With
1887 // late masking, we ignore zeros in the resulting value when inserting each
1888 // set of bit groups, and then mask in the zeros at the end. With early
1889 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001890
Hal Finkelc58ce412015-01-01 02:53:29 +00001891 unsigned InstCnt, InstCntLateMask;
1892 DEBUG(dbgs() << "\tEarly masking:\n");
1893 SDNode *RN = Select(N, false, &InstCnt);
1894 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1895
1896 DEBUG(dbgs() << "\tLate masking:\n");
1897 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1898 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1899 " instructions\n");
1900
1901 if (InstCnt <= InstCntLateMask) {
1902 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1903 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001904 }
1905
Hal Finkelc58ce412015-01-01 02:53:29 +00001906 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1907 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001908 }
1909};
1910} // anonymous namespace
1911
1912SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) {
1913 if (N->getValueType(0) != MVT::i32 &&
1914 N->getValueType(0) != MVT::i64)
1915 return nullptr;
1916
Hal Finkelc58ce412015-01-01 02:53:29 +00001917 if (!UseBitPermRewriter)
1918 return nullptr;
1919
Hal Finkel8adf2252014-12-16 05:51:41 +00001920 switch (N->getOpcode()) {
1921 default: break;
1922 case ISD::ROTL:
1923 case ISD::SHL:
1924 case ISD::SRL:
1925 case ISD::AND:
1926 case ISD::OR: {
1927 BitPermutationSelector BPS(CurDAG);
1928 return BPS.Select(N);
1929 }
1930 }
1931
1932 return nullptr;
1933}
1934
Chris Lattner2a1823d2005-08-21 18:50:37 +00001935/// SelectCC - Select a comparison of the specified values with the specified
1936/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001937SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001938 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001939 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001940 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001941
Owen Anderson9f944592009-08-11 20:47:22 +00001942 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001943 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001944 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1945 if (isInt32Immediate(RHS, Imm)) {
1946 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001947 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001948 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001949 getI32Imm(Imm & 0xFFFF, dl)),
1950 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001951 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001952 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001953 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001954 getI32Imm(Imm & 0xFFFF, dl)),
1955 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00001956
Chris Lattneraa3926b2006-09-20 04:25:47 +00001957 // For non-equality comparisons, the default code would materialize the
1958 // constant, then compare against it, like this:
1959 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00001960 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00001961 // cmpw cr0, r3, r2
1962 // Since we are just comparing for equality, we can emit this instead:
1963 // xoris r0,r3,0x1234
1964 // cmplwi cr0,r0,0x5678
1965 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00001966 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001967 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00001968 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001969 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001970 }
1971 Opc = PPC::CMPLW;
1972 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00001973 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001974 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001975 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00001976 Opc = PPC::CMPLW;
1977 } else {
1978 short SImm;
1979 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001980 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001981 getI32Imm((int)SImm & 0xFFFF,
1982 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00001983 0);
1984 Opc = PPC::CMPW;
1985 }
Owen Anderson9f944592009-08-11 20:47:22 +00001986 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00001987 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001988 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001989 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001990 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001991 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001992 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001993 getI32Imm(Imm & 0xFFFF, dl)),
1994 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001995 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001996 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001997 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00001998 getI32Imm(Imm & 0xFFFF, dl)),
1999 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002000
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002001 // For non-equality comparisons, the default code would materialize the
2002 // constant, then compare against it, like this:
2003 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002004 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002005 // cmpd cr0, r3, r2
2006 // Since we are just comparing for equality, we can emit this instead:
2007 // xoris r0,r3,0x1234
2008 // cmpldi cr0,r0,0x5678
2009 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002010 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002011 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002012 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002013 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002014 getI64Imm(Imm & 0xFFFF, dl)),
2015 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002016 }
2017 }
2018 Opc = PPC::CMPLD;
2019 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002020 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002021 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002022 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002023 Opc = PPC::CMPLD;
2024 } else {
2025 short SImm;
2026 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002027 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002028 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002029 0);
2030 Opc = PPC::CMPD;
2031 }
Owen Anderson9f944592009-08-11 20:47:22 +00002032 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002033 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002034 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002035 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002036 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002037 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002038 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002039}
2040
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002041static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002042 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002043 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002044 case ISD::SETONE:
2045 case ISD::SETOLE:
2046 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002047 llvm_unreachable("Should be lowered by legalize!");
2048 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002049 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002050 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002051 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002052 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002053 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002054 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002055 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002056 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002057 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002058 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002059 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002060 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002061 case ISD::SETO: return PPC::PRED_NU;
2062 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002063 // These two are invalid for floating point. Assume we have int.
2064 case ISD::SETULT: return PPC::PRED_LT;
2065 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002066 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002067}
2068
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002069/// getCRIdxForSetCC - Return the index of the condition register field
2070/// associated with the SetCC condition, and whether or not the field is
2071/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002072static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002073 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002074 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002075 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002076 case ISD::SETOLT:
2077 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2078 case ISD::SETOGT:
2079 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2080 case ISD::SETOEQ:
2081 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2082 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002083 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002084 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002085 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002086 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002087 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002088 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2089 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002090 case ISD::SETUEQ:
2091 case ISD::SETOGE:
2092 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002093 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002094 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002095 // These are invalid for floating point. Assume integer.
2096 case ISD::SETULT: return 0;
2097 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002098 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002099}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002100
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002101// getVCmpInst: return the vector compare instruction for the specified
2102// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002103// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002104static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2105 bool HasVSX, bool &Swap, bool &Negate) {
2106 Swap = false;
2107 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002108
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002109 if (VecVT.isFloatingPoint()) {
2110 /* Handle some cases by swapping input operands. */
2111 switch (CC) {
2112 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2113 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2114 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2115 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2116 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2117 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2118 default: break;
2119 }
2120 /* Handle some cases by negating the result. */
2121 switch (CC) {
2122 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2123 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2124 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2125 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2126 default: break;
2127 }
2128 /* We have instructions implementing the remaining cases. */
2129 switch (CC) {
2130 case ISD::SETEQ:
2131 case ISD::SETOEQ:
2132 if (VecVT == MVT::v4f32)
2133 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2134 else if (VecVT == MVT::v2f64)
2135 return PPC::XVCMPEQDP;
2136 break;
2137 case ISD::SETGT:
2138 case ISD::SETOGT:
2139 if (VecVT == MVT::v4f32)
2140 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2141 else if (VecVT == MVT::v2f64)
2142 return PPC::XVCMPGTDP;
2143 break;
2144 case ISD::SETGE:
2145 case ISD::SETOGE:
2146 if (VecVT == MVT::v4f32)
2147 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2148 else if (VecVT == MVT::v2f64)
2149 return PPC::XVCMPGEDP;
2150 break;
2151 default:
2152 break;
2153 }
2154 llvm_unreachable("Invalid floating-point vector compare condition");
2155 } else {
2156 /* Handle some cases by swapping input operands. */
2157 switch (CC) {
2158 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2159 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2160 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2161 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2162 default: break;
2163 }
2164 /* Handle some cases by negating the result. */
2165 switch (CC) {
2166 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2167 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2168 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2169 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2170 default: break;
2171 }
2172 /* We have instructions implementing the remaining cases. */
2173 switch (CC) {
2174 case ISD::SETEQ:
2175 case ISD::SETUEQ:
2176 if (VecVT == MVT::v16i8)
2177 return PPC::VCMPEQUB;
2178 else if (VecVT == MVT::v8i16)
2179 return PPC::VCMPEQUH;
2180 else if (VecVT == MVT::v4i32)
2181 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002182 else if (VecVT == MVT::v2i64)
2183 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002184 break;
2185 case ISD::SETGT:
2186 if (VecVT == MVT::v16i8)
2187 return PPC::VCMPGTSB;
2188 else if (VecVT == MVT::v8i16)
2189 return PPC::VCMPGTSH;
2190 else if (VecVT == MVT::v4i32)
2191 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002192 else if (VecVT == MVT::v2i64)
2193 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002194 break;
2195 case ISD::SETUGT:
2196 if (VecVT == MVT::v16i8)
2197 return PPC::VCMPGTUB;
2198 else if (VecVT == MVT::v8i16)
2199 return PPC::VCMPGTUH;
2200 else if (VecVT == MVT::v4i32)
2201 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002202 else if (VecVT == MVT::v2i64)
2203 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002204 break;
2205 default:
2206 break;
2207 }
2208 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002209 }
2210}
2211
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002212SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002213 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002214 unsigned Imm;
2215 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002216 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2217 bool isPPC64 = (PtrVT == MVT::i64);
2218
Eric Christopher1b8e7632014-05-22 01:07:24 +00002219 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002220 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002221 // We can codegen setcc op, imm very efficiently compared to a brcond.
2222 // Check for those cases here.
2223 // setcc op, 0
2224 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002225 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002226 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002227 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002228 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002229 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002230 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
2231 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002232 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002233 }
Chris Lattnere2969492005-10-21 21:17:10 +00002234 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002235 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002236 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002237 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002238 Op, getI32Imm(~0U, dl)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002239 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002240 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002241 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002242 case ISD::SETLT: {
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002243 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2244 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002245 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002246 }
Chris Lattnere2969492005-10-21 21:17:10 +00002247 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002248 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002249 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2250 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002251 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
2252 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002253 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002254 }
2255 }
Chris Lattner491b8292005-10-06 19:03:35 +00002256 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002257 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002258 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002259 default: break;
2260 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002261 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002262 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002263 Op, getI32Imm(1, dl)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002264 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2265 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +00002266 MVT::i32,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002267 getI32Imm(0, dl)),
2268 0), Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002269 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002270 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002271 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002272 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002273 Op, getI32Imm(~0U, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00002274 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002275 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +00002276 }
Chris Lattnere2969492005-10-21 21:17:10 +00002277 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002278 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002279 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002280 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2281 Op), 0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002282 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
2283 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002284 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002285 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002286 case ISD::SETGT: {
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002287 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2288 getI32Imm(31, dl) };
2289 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002290 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002291 getI32Imm(1, dl));
Chris Lattnere2969492005-10-21 21:17:10 +00002292 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002293 }
Chris Lattner491b8292005-10-06 19:03:35 +00002294 }
2295 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002296
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002297 SDValue LHS = N->getOperand(0);
2298 SDValue RHS = N->getOperand(1);
2299
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002300 // Altivec Vector compare instructions do not set any CR register by default and
2301 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002302 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002303 if (PPCSubTarget->hasQPX())
2304 return nullptr;
2305
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002306 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002307 bool Swap, Negate;
2308 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2309 PPCSubTarget->hasVSX(), Swap, Negate);
2310 if (Swap)
2311 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002312
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002313 if (Negate) {
2314 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
2315 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
2316 PPC::VNOR,
2317 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002318 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002319
2320 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002321 }
2322
Eric Christopher1b8e7632014-05-22 01:07:24 +00002323 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +00002324 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +00002325
Chris Lattner491b8292005-10-06 19:03:35 +00002326 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002327 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002328 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002329 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002330
Chris Lattner491b8292005-10-06 19:03:35 +00002331 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002332 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002333
Craig Topper062a2ba2014-04-25 05:30:21 +00002334 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002335 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002336 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002337
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002338 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2339 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002340
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002341 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
2342 getI32Imm(31, dl), getI32Imm(31, dl) };
Ulrich Weigand47e93282013-07-03 15:13:30 +00002343 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +00002344 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +00002345
2346 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002347 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002348 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002349 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
Chris Lattner491b8292005-10-06 19:03:35 +00002350}
Chris Lattner502a3692005-10-06 18:56:10 +00002351
Hal Finkelcf599212015-02-25 21:36:59 +00002352SDNode *PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
2353 // Transfer memoperands.
2354 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2355 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2356 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2357 return Result;
2358}
2359
Chris Lattner318622f2005-10-06 19:07:45 +00002360
Chris Lattner43ff01e2005-08-17 19:33:03 +00002361// Select - Convert the specified operand from a target-independent to a
2362// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002363SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002364 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002365 if (N->isMachineOpcode()) {
2366 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002367 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002368 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002369
Hal Finkel51b3fd12014-09-02 06:23:54 +00002370 // In case any misguided DAG-level optimizations form an ADD with a
2371 // TargetConstant operand, crash here instead of miscompiling (by selecting
2372 // an r+r add instead of some kind of r+i add).
2373 if (N->getOpcode() == ISD::ADD &&
2374 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2375 llvm_unreachable("Invalid ADD with TargetConstant operand");
2376
Hal Finkel8adf2252014-12-16 05:51:41 +00002377 // Try matching complex bit permutations before doing anything else.
2378 if (SDNode *NN = SelectBitPermutation(N))
2379 return NN;
2380
Chris Lattner43ff01e2005-08-17 19:33:03 +00002381 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002382 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002383
Jim Laskey095e6f32006-12-12 13:23:43 +00002384 case ISD::Constant: {
Hal Finkelc58ce412015-01-01 02:53:29 +00002385 if (N->getValueType(0) == MVT::i64)
2386 return SelectInt64(CurDAG, N);
Jim Laskey095e6f32006-12-12 13:23:43 +00002387 break;
2388 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002389
Hal Finkel940ab932014-02-28 00:27:01 +00002390 case ISD::SETCC: {
2391 SDNode *SN = SelectSETCC(N);
2392 if (SN)
2393 return SN;
2394 break;
2395 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002396 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00002397 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00002398
Hal Finkelb5e9b042014-12-11 22:51:06 +00002399 case ISD::FrameIndex:
2400 return getFrameIndex(N, N);
Chris Lattner6961fc72006-03-26 10:06:40 +00002401
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002402 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002403 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002404 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2405 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00002406 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002407
Hal Finkelbbdee932014-12-02 22:01:00 +00002408 case PPCISD::READ_TIME_BASE: {
2409 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2410 MVT::Other, N->getOperand(0));
2411 }
2412
Hal Finkel13d104b2014-12-11 18:37:52 +00002413 case PPCISD::SRA_ADDZE: {
2414 SDValue N0 = N->getOperand(0);
2415 SDValue ShiftAmt =
2416 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002417 getConstantIntValue(), dl,
2418 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00002419 if (N->getValueType(0) == MVT::i64) {
2420 SDNode *Op =
2421 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2422 N0, ShiftAmt);
2423 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64,
2424 SDValue(Op, 0), SDValue(Op, 1));
2425 } else {
2426 assert(N->getValueType(0) == MVT::i32 &&
2427 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2428 SDNode *Op =
2429 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2430 N0, ShiftAmt);
2431 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2432 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00002433 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002434 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002435
Chris Lattnerce645542006-11-10 02:08:47 +00002436 case ISD::LOAD: {
2437 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002438 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002439 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002440
Chris Lattnerce645542006-11-10 02:08:47 +00002441 // Normal loads are handled by code generated from the .td file.
2442 if (LD->getAddressingMode() != ISD::PRE_INC)
2443 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002444
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002446 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002447 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002448
Chris Lattner474b5b72006-11-15 19:55:13 +00002449 unsigned Opcode;
2450 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002451 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002452 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002453 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2454 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002455 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002456 case MVT::f64: Opcode = PPC::LFDU; break;
2457 case MVT::f32: Opcode = PPC::LFSU; break;
2458 case MVT::i32: Opcode = PPC::LWZU; break;
2459 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2460 case MVT::i1:
2461 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002462 }
2463 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002464 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2465 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2466 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002467 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002468 case MVT::i64: Opcode = PPC::LDU; break;
2469 case MVT::i32: Opcode = PPC::LWZU8; break;
2470 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2471 case MVT::i1:
2472 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002473 }
2474 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002475
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002476 SDValue Chain = LD->getChain();
2477 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002478 SDValue Ops[] = { Offset, Base, Chain };
Hal Finkelcf599212015-02-25 21:36:59 +00002479 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl,
2480 LD->getValueType(0),
2481 PPCLowering->getPointerTy(),
2482 MVT::Other, Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002483 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002484 unsigned Opcode;
2485 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2486 if (LD->getValueType(0) != MVT::i64) {
2487 // Handle PPC32 integer and normal FP loads.
2488 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2489 switch (LoadedVT.getSimpleVT().SimpleTy) {
2490 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00002491 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
2492 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00002493 case MVT::f64: Opcode = PPC::LFDUX; break;
2494 case MVT::f32: Opcode = PPC::LFSUX; break;
2495 case MVT::i32: Opcode = PPC::LWZUX; break;
2496 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2497 case MVT::i1:
2498 case MVT::i8: Opcode = PPC::LBZUX; break;
2499 }
2500 } else {
2501 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2502 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2503 "Invalid sext update load");
2504 switch (LoadedVT.getSimpleVT().SimpleTy) {
2505 default: llvm_unreachable("Invalid PPC load type!");
2506 case MVT::i64: Opcode = PPC::LDUX; break;
2507 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2508 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2509 case MVT::i1:
2510 case MVT::i8: Opcode = PPC::LBZUX8; break;
2511 }
2512 }
2513
2514 SDValue Chain = LD->getChain();
2515 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002516 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelcf599212015-02-25 21:36:59 +00002517 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl,
2518 LD->getValueType(0),
2519 PPCLowering->getPointerTy(),
2520 MVT::Other, Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002521 }
2522 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002523
Nate Begemanb3821a32005-08-18 07:30:46 +00002524 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002525 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002526 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002527
Nate Begemanb3821a32005-08-18 07:30:46 +00002528 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2529 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002530 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002531 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002533 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
2534 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002535 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00002536 }
Nate Begemand31efd12006-09-22 05:01:56 +00002537 // If this is just a masked value where the input is not handled above, and
2538 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2539 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002540 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002541 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002542 SDValue Val = N->getOperand(0);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002543 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
2544 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002545 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00002546 }
Hal Finkele39526a2012-08-28 02:10:15 +00002547 // If this is a 64-bit zero-extension mask, emit rldicl.
2548 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2549 isMask_64(Imm64)) {
2550 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002551 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002552 SH = 0;
2553
2554 // If the operand is a logical right shift, we can fold it into this
2555 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2556 // for n <= mb. The right shift is really a left rotate followed by a
2557 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2558 // by the shift.
2559 if (Val.getOpcode() == ISD::SRL &&
2560 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2561 assert(Imm < 64 && "Illegal shift amount");
2562 Val = Val.getOperand(0);
2563 SH = 64 - Imm;
2564 }
2565
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002566 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002567 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00002568 }
Nate Begemand31efd12006-09-22 05:01:56 +00002569 // AND X, 0 -> 0, not "rlwinm 32".
2570 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002571 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00002572 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00002573 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002574 // ISD::OR doesn't get all the bitfield insertion fun.
2575 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00002576 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002577 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002578 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00002579 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00002580 Imm = ~(Imm^Imm2);
2581 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002582 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002583 N->getOperand(0).getOperand(1),
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002584 getI32Imm(0, dl), getI32Imm(MB, dl),
2585 getI32Imm(ME, dl) };
Michael Liaob53d8962013-04-19 22:22:57 +00002586 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00002587 }
2588 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002589
Chris Lattner1de57062005-09-29 23:33:31 +00002590 // Other cases are autogenerated.
2591 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002592 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002593 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002594 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00002595 if (SDNode *I = SelectBitfieldInsert(N))
2596 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00002597
Hal Finkelb5e9b042014-12-11 22:51:06 +00002598 short Imm;
2599 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2600 isIntS16Immediate(N->getOperand(1), Imm)) {
2601 APInt LHSKnownZero, LHSKnownOne;
2602 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2603
2604 // If this is equivalent to an add, then we can fold it with the
2605 // FrameIndex calculation.
2606 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL)
2607 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2608 }
2609
Chris Lattner1de57062005-09-29 23:33:31 +00002610 // Other cases are autogenerated.
2611 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002612 }
2613 case ISD::ADD: {
2614 short Imm;
2615 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2616 isIntS16Immediate(N->getOperand(1), Imm))
2617 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2618
2619 break;
2620 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002621 case ISD::SHL: {
2622 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002623 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002624 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002625 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002626 getI32Imm(SH, dl), getI32Imm(MB, dl),
2627 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002628 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002629 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002630
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002631 // Other cases are autogenerated.
2632 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002633 }
2634 case ISD::SRL: {
2635 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002636 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002637 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002638 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002639 getI32Imm(SH, dl), getI32Imm(MB, dl),
2640 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002641 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002642 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002643
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002644 // Other cases are autogenerated.
2645 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002646 }
Hal Finkel940ab932014-02-28 00:27:01 +00002647 // FIXME: Remove this once the ANDI glue bug is fixed:
2648 case PPCISD::ANDIo_1_EQ_BIT:
2649 case PPCISD::ANDIo_1_GT_BIT: {
2650 if (!ANDIGlueBug)
2651 break;
2652
2653 EVT InVT = N->getOperand(0).getValueType();
2654 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2655 "Invalid input type for ANDIo_1_EQ_BIT");
2656
2657 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2658 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2659 N->getOperand(0),
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002660 CurDAG->getTargetConstant(1, dl, InVT)),
2661 0);
Hal Finkel940ab932014-02-28 00:27:01 +00002662 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2663 SDValue SRIdxVal =
2664 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002665 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00002666
2667 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
2668 CR0Reg, SRIdxVal,
2669 SDValue(AndI.getNode(), 1) /* glue */);
2670 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002671 case ISD::SELECT_CC: {
2672 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002673 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2674 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002675
Hal Finkel940ab932014-02-28 00:27:01 +00002676 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002677 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002678 N->getOperand(0).getValueType() == MVT::i1)
2679 break;
2680
Chris Lattner97b3da12006-06-27 00:04:13 +00002681 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002682 if (!isPPC64)
2683 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2684 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2685 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2686 if (N1C->isNullValue() && N3C->isNullValue() &&
2687 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2688 // FIXME: Implement this optzn for PPC64.
2689 N->getValueType(0) == MVT::i32) {
2690 SDNode *Tmp =
2691 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002692 N->getOperand(0), getI32Imm(~0U, dl));
Roman Divacky254f8212011-06-20 15:28:39 +00002693 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
2694 SDValue(Tmp, 0), N->getOperand(0),
2695 SDValue(Tmp, 1));
2696 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002697
Dale Johannesenab8e4422009-02-06 19:16:40 +00002698 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002699
2700 if (N->getValueType(0) == MVT::i1) {
2701 // An i1 select is: (c & t) | (!c & f).
2702 bool Inv;
2703 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2704
2705 unsigned SRI;
2706 switch (Idx) {
2707 default: llvm_unreachable("Invalid CC index");
2708 case 0: SRI = PPC::sub_lt; break;
2709 case 1: SRI = PPC::sub_gt; break;
2710 case 2: SRI = PPC::sub_eq; break;
2711 case 3: SRI = PPC::sub_un; break;
2712 }
2713
2714 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2715
2716 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2717 CCBit, CCBit), 0);
2718 SDValue C = Inv ? NotCCBit : CCBit,
2719 NotC = Inv ? CCBit : NotCCBit;
2720
2721 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2722 C, N->getOperand(2)), 0);
2723 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2724 NotC, N->getOperand(3)), 0);
2725
2726 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2727 }
2728
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002729 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002730
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002731 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002732 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002733 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002734 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002735 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002736 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002737 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002738 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002739 if (PPCSubTarget->hasVSX())
2740 SelectCCOp = PPC::SELECT_CC_VSFRC;
2741 else
2742 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002743 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
2744 SelectCCOp = PPC::SELECT_CC_QFRC;
2745 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
2746 SelectCCOp = PPC::SELECT_CC_QSRC;
2747 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
2748 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00002749 else if (N->getValueType(0) == MVT::v2f64 ||
2750 N->getValueType(0) == MVT::v2i64)
2751 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002752 else
2753 SelectCCOp = PPC::SELECT_CC_VRRC;
2754
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002755 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002756 getI32Imm(BROpc, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002757 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00002758 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002759 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002760 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002761 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002762 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00002763 }
2764
2765 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002766 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002767 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002768 N->getValueType(0) == MVT::v2i64)) {
2769 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
2770
2771 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2772 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2773 unsigned DM[2];
2774
2775 for (int i = 0; i < 2; ++i)
2776 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2777 DM[i] = 0;
2778 else
2779 DM[i] = 1;
2780
Bill Schmidt30144352014-12-09 16:52:29 +00002781 // For little endian, we must swap the input operands and adjust
2782 // the mask elements (reverse and invert them).
2783 if (PPCSubTarget->isLittleEndian()) {
2784 std::swap(Op1, Op2);
2785 unsigned tmp = DM[0];
2786 DM[0] = 1 - DM[1];
2787 DM[1] = 1 - tmp;
2788 }
2789
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002790 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
2791 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002792
2793 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2794 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2795 isa<LoadSDNode>(Op1.getOperand(0))) {
2796 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2797 SDValue Base, Offset;
2798
2799 if (LD->isUnindexed() &&
2800 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2801 SDValue Chain = LD->getChain();
2802 SDValue Ops[] = { Base, Offset, Chain };
2803 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00002804 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002805 }
2806 }
2807
2808 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00002809 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002810 }
2811
2812 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002813 case PPCISD::BDNZ:
2814 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002815 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002816 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
2817 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
2818 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
2819 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00002820 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00002821 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002822 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002823 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002824 // Op #1 is the PPC::PRED_* number.
2825 // Op #2 is the CR#
2826 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002827 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002828 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002829 SDValue Pred =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002830 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(), dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002831 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002832 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00002833 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002834 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002835 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002836 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002837 unsigned PCC = getPredicateForSetCC(CC);
2838
2839 if (N->getOperand(2).getValueType() == MVT::i1) {
2840 unsigned Opc;
2841 bool Swap;
2842 switch (PCC) {
2843 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2844 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2845 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2846 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2847 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2848 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2849 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2850 }
2851
2852 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2853 N->getOperand(Swap ? 3 : 2),
2854 N->getOperand(Swap ? 2 : 3)), 0);
2855 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
2856 BitComp, N->getOperand(4), N->getOperand(0));
2857 }
2858
Dale Johannesenab8e4422009-02-06 19:16:40 +00002859 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002860 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002861 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002862 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002863 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002864 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002865 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002866 SDValue Chain = N->getOperand(0);
2867 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002868 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002869 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002870 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002871 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00002872 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002873 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002874 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002875 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2876 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002877 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2878 SDValue GA = N->getOperand(0);
Hal Finkelcf599212015-02-25 21:36:59 +00002879 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LWZtoc, dl,
2880 MVT::i32, GA, N->getOperand(1)));
Justin Hibbits3476db42014-08-28 04:40:55 +00002881 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002882
Bill Schmidt27917782013-02-21 17:12:27 +00002883 // For medium and large code model, we generate two instructions as
2884 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002885 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00002886 CodeModel::Model CModel = TM.getCodeModel();
2887 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002888 break;
2889
Bill Schmidt5d82f092014-06-16 21:36:02 +00002890 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
2891 // If it is an externally defined symbol, a symbol with common linkage,
2892 // a non-local function address, or a jump table address, or if we are
2893 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00002894 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
2895 // Otherwise we generate:
2896 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
2897 SDValue GA = N->getOperand(0);
2898 SDValue TOCbase = N->getOperand(1);
2899 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00002900 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00002901
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002902 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
2903 CModel == CodeModel::Large)
Hal Finkelcf599212015-02-25 21:36:59 +00002904 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2905 MVT::i64, GA, SDValue(Tmp, 0)));
Bill Schmidt34627e32012-11-27 17:35:46 +00002906
2907 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
2908 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00002909 if ((GValue->getType()->getElementType()->isFunctionTy() &&
2910 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00002911 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
2912 GValue->hasAvailableExternallyLinkage())
Hal Finkelcf599212015-02-25 21:36:59 +00002913 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2914 MVT::i64, GA, SDValue(Tmp, 0)));
Bill Schmidt34627e32012-11-27 17:35:46 +00002915 }
2916
2917 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
2918 SDValue(Tmp, 0), GA);
2919 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00002920 case PPCISD::PPC32_PICGOT: {
2921 // Generate a PIC-safe GOT reference.
2922 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
2923 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
2924 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
2925 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002926 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002927 // This expands into one of three sequences, depending on whether
2928 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00002929 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
2930 isa<ConstantSDNode>(N->getOperand(1)) &&
2931 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002932
2933 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00002934 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002935 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00002936 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002937
Bill Schmidt51e79512013-02-20 15:50:31 +00002938 if (EltSize == 1) {
2939 Opc1 = PPC::VSPLTISB;
2940 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002941 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002942 VT = MVT::v16i8;
2943 } else if (EltSize == 2) {
2944 Opc1 = PPC::VSPLTISH;
2945 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002946 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002947 VT = MVT::v8i16;
2948 } else {
2949 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
2950 Opc1 = PPC::VSPLTISW;
2951 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002952 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002953 VT = MVT::v4i32;
2954 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002955
2956 if ((Elt & 1) == 0) {
2957 // Elt is even, in the range [-32,-18] + [16,30].
2958 //
2959 // Convert: VADD_SPLAT elt, size
2960 // Into: tmp = VSPLTIS[BHW] elt
2961 // VADDU[BHW]M tmp, tmp
2962 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002963 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002964 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2965 SDValue TmpVal = SDValue(Tmp, 0);
2966 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
2967
2968 } else if (Elt > 0) {
2969 // Elt is odd and positive, in the range [17,31].
2970 //
2971 // Convert: VADD_SPLAT elt, size
2972 // Into: tmp1 = VSPLTIS[BHW] elt-16
2973 // tmp2 = VSPLTIS[BHW] -16
2974 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002975 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002976 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002977 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002978 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2979 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
2980 SDValue(Tmp2, 0));
2981
2982 } else {
2983 // Elt is odd and negative, in the range [-31,-17].
2984 //
2985 // Convert: VADD_SPLAT elt, size
2986 // Into: tmp1 = VSPLTIS[BHW] elt+16
2987 // tmp2 = VSPLTIS[BHW] -16
2988 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002989 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002990 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00002991 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002992 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2993 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
2994 SDValue(Tmp2, 0));
2995 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002996 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00002997 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002998
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002999 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00003000}
3001
Hal Finkel4edc66b2015-01-03 01:16:37 +00003002// If the target supports the cmpb instruction, do the idiom recognition here.
3003// We don't do this as a DAG combine because we don't want to do it as nodes
3004// are being combined (because we might miss part of the eventual idiom). We
3005// don't want to do it during instruction selection because we want to reuse
3006// the logic for lowering the masking operations already part of the
3007// instruction selector.
3008SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3009 SDLoc dl(N);
3010
3011 assert(N->getOpcode() == ISD::OR &&
3012 "Only OR nodes are supported for CMPB");
3013
3014 SDValue Res;
3015 if (!PPCSubTarget->hasCMPB())
3016 return Res;
3017
3018 if (N->getValueType(0) != MVT::i32 &&
3019 N->getValueType(0) != MVT::i64)
3020 return Res;
3021
3022 EVT VT = N->getValueType(0);
3023
3024 SDValue RHS, LHS;
3025 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
3026 uint64_t Mask = 0, Alt = 0;
3027
3028 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3029 uint64_t &Mask, uint64_t &Alt,
3030 SDValue &LHS, SDValue &RHS) {
3031 if (O.getOpcode() != ISD::SELECT_CC)
3032 return false;
3033 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3034
3035 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3036 !isa<ConstantSDNode>(O.getOperand(3)))
3037 return false;
3038
3039 uint64_t PM = O.getConstantOperandVal(2);
3040 uint64_t PAlt = O.getConstantOperandVal(3);
3041 for (b = 0; b < 8; ++b) {
3042 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3043 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3044 break;
3045 }
3046
3047 if (b == 8)
3048 return false;
3049 Mask |= PM;
3050 Alt |= PAlt;
3051
3052 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3053 O.getConstantOperandVal(1) != 0) {
3054 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3055 if (Op0.getOpcode() == ISD::TRUNCATE)
3056 Op0 = Op0.getOperand(0);
3057 if (Op1.getOpcode() == ISD::TRUNCATE)
3058 Op1 = Op1.getOperand(0);
3059
3060 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3061 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3062 isa<ConstantSDNode>(Op0.getOperand(1))) {
3063
3064 unsigned Bits = Op0.getValueType().getSizeInBits();
3065 if (b != Bits/8-1)
3066 return false;
3067 if (Op0.getConstantOperandVal(1) != Bits-8)
3068 return false;
3069
3070 LHS = Op0.getOperand(0);
3071 RHS = Op1.getOperand(0);
3072 return true;
3073 }
3074
3075 // When we have small integers (i16 to be specific), the form present
3076 // post-legalization uses SETULT in the SELECT_CC for the
3077 // higher-order byte, depending on the fact that the
3078 // even-higher-order bytes are known to all be zero, for example:
3079 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3080 // (so when the second byte is the same, because all higher-order
3081 // bits from bytes 3 and 4 are known to be zero, the result of the
3082 // xor can be at most 255)
3083 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3084 isa<ConstantSDNode>(O.getOperand(1))) {
3085
3086 uint64_t ULim = O.getConstantOperandVal(1);
3087 if (ULim != (UINT64_C(1) << b*8))
3088 return false;
3089
3090 // Now we need to make sure that the upper bytes are known to be
3091 // zero.
3092 unsigned Bits = Op0.getValueType().getSizeInBits();
3093 if (!CurDAG->MaskedValueIsZero(Op0,
3094 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
3095 return false;
3096
3097 LHS = Op0.getOperand(0);
3098 RHS = Op0.getOperand(1);
3099 return true;
3100 }
3101
3102 return false;
3103 }
3104
3105 if (CC != ISD::SETEQ)
3106 return false;
3107
3108 SDValue Op = O.getOperand(0);
3109 if (Op.getOpcode() == ISD::AND) {
3110 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3111 return false;
3112 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3113 return false;
3114
3115 SDValue XOR = Op.getOperand(0);
3116 if (XOR.getOpcode() == ISD::TRUNCATE)
3117 XOR = XOR.getOperand(0);
3118 if (XOR.getOpcode() != ISD::XOR)
3119 return false;
3120
3121 LHS = XOR.getOperand(0);
3122 RHS = XOR.getOperand(1);
3123 return true;
3124 } else if (Op.getOpcode() == ISD::SRL) {
3125 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3126 return false;
3127 unsigned Bits = Op.getValueType().getSizeInBits();
3128 if (b != Bits/8-1)
3129 return false;
3130 if (Op.getConstantOperandVal(1) != Bits-8)
3131 return false;
3132
3133 SDValue XOR = Op.getOperand(0);
3134 if (XOR.getOpcode() == ISD::TRUNCATE)
3135 XOR = XOR.getOperand(0);
3136 if (XOR.getOpcode() != ISD::XOR)
3137 return false;
3138
3139 LHS = XOR.getOperand(0);
3140 RHS = XOR.getOperand(1);
3141 return true;
3142 }
3143
3144 return false;
3145 };
3146
3147 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3148 while (!Queue.empty()) {
3149 SDValue V = Queue.pop_back_val();
3150
3151 for (const SDValue &O : V.getNode()->ops()) {
3152 unsigned b;
3153 uint64_t M = 0, A = 0;
3154 SDValue OLHS, ORHS;
3155 if (O.getOpcode() == ISD::OR) {
3156 Queue.push_back(O);
3157 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3158 if (!LHS) {
3159 LHS = OLHS;
3160 RHS = ORHS;
3161 BytesFound[b] = true;
3162 Mask |= M;
3163 Alt |= A;
3164 } else if ((LHS == ORHS && RHS == OLHS) ||
3165 (RHS == ORHS && LHS == OLHS)) {
3166 BytesFound[b] = true;
3167 Mask |= M;
3168 Alt |= A;
3169 } else {
3170 return Res;
3171 }
3172 } else {
3173 return Res;
3174 }
3175 }
3176 }
3177
3178 unsigned LastB = 0, BCnt = 0;
3179 for (unsigned i = 0; i < 8; ++i)
3180 if (BytesFound[LastB]) {
3181 ++BCnt;
3182 LastB = i;
3183 }
3184
3185 if (!LastB || BCnt < 2)
3186 return Res;
3187
3188 // Because we'll be zero-extending the output anyway if don't have a specific
3189 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3190 if (LHS.getValueType() != VT) {
3191 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3192 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3193 }
3194
3195 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3196
3197 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3198 if (NonTrivialMask && !Alt) {
3199 // Res = Mask & CMPB
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00003200 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3201 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003202 } else if (Alt) {
3203 // Res = (CMPB & Mask) | (~CMPB & Alt)
3204 // Which, as suggested here:
3205 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3206 // can be written as:
3207 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3208 // useful because the (Alt ^ Mask) can be pre-computed.
3209 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00003210 CurDAG->getConstant(Mask ^ Alt, dl, VT));
3211 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
3212 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003213 }
3214
3215 return Res;
3216}
3217
Hal Finkel200d2ad2015-01-05 21:10:24 +00003218// When CR bit registers are enabled, an extension of an i1 variable to a i32
3219// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3220// involves constant materialization of a 0 or a 1 or both. If the result of
3221// the extension is then operated upon by some operator that can be constant
3222// folded with a constant 0 or 1, and that constant can be materialized using
3223// only one instruction (like a zero or one), then we should fold in those
3224// operations with the select.
3225void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3226 if (!PPCSubTarget->useCRBits())
3227 return;
3228
3229 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3230 N->getOpcode() != ISD::SIGN_EXTEND &&
3231 N->getOpcode() != ISD::ANY_EXTEND)
3232 return;
3233
3234 if (N->getOperand(0).getValueType() != MVT::i1)
3235 return;
3236
3237 if (!N->hasOneUse())
3238 return;
3239
3240 SDLoc dl(N);
3241 EVT VT = N->getValueType(0);
3242 SDValue Cond = N->getOperand(0);
3243 SDValue ConstTrue =
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00003244 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
3245 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00003246
3247 do {
3248 SDNode *User = *N->use_begin();
3249 if (User->getNumOperands() != 2)
3250 break;
3251
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00003252 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00003253 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3254 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3255 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3256
Sergey Dmitroukadb4c692015-04-28 11:56:37 +00003257 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00003258 User->getValueType(0),
3259 O0.getNode(), O1.getNode());
3260 };
3261
3262 SDValue TrueRes = TryFold(ConstTrue);
3263 if (!TrueRes)
3264 break;
3265 SDValue FalseRes = TryFold(ConstFalse);
3266 if (!FalseRes)
3267 break;
3268
3269 // For us to materialize these using one instruction, we must be able to
3270 // represent them as signed 16-bit integers.
3271 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
3272 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
3273 if (!isInt<16>(True) || !isInt<16>(False))
3274 break;
3275
3276 // We can replace User with a new SELECT node, and try again to see if we
3277 // can fold the select with its user.
3278 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
3279 N = User;
3280 ConstTrue = TrueRes;
3281 ConstFalse = FalseRes;
3282 } while (N->hasOneUse());
3283}
3284
Hal Finkel4edc66b2015-01-03 01:16:37 +00003285void PPCDAGToDAGISel::PreprocessISelDAG() {
3286 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3287 ++Position;
3288
3289 bool MadeChange = false;
3290 while (Position != CurDAG->allnodes_begin()) {
3291 SDNode *N = --Position;
3292 if (N->use_empty())
3293 continue;
3294
3295 SDValue Res;
3296 switch (N->getOpcode()) {
3297 default: break;
3298 case ISD::OR:
3299 Res = combineToCMPB(N);
3300 break;
3301 }
3302
Hal Finkel200d2ad2015-01-05 21:10:24 +00003303 if (!Res)
3304 foldBoolExts(Res, N);
3305
Hal Finkel4edc66b2015-01-03 01:16:37 +00003306 if (Res) {
3307 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3308 DEBUG(N->dump(CurDAG));
3309 DEBUG(dbgs() << "\nNew: ");
3310 DEBUG(Res.getNode()->dump(CurDAG));
3311 DEBUG(dbgs() << "\n");
3312
3313 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3314 MadeChange = true;
3315 }
3316 }
3317
3318 if (MadeChange)
3319 CurDAG->RemoveDeadNodes();
3320}
3321
Hal Finkel860fa902014-01-02 22:09:39 +00003322/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003323/// on the DAG representation.
3324void PPCDAGToDAGISel::PostprocessISelDAG() {
3325
3326 // Skip peepholes at -O0.
3327 if (TM.getOptLevel() == CodeGenOpt::None)
3328 return;
3329
Hal Finkel940ab932014-02-28 00:27:01 +00003330 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003331 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003332 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003333}
3334
Hal Finkelb9989152014-02-28 06:11:16 +00003335// Check if all users of this node will become isel where the second operand
3336// is the constant zero. If this is so, and if we can negate the condition,
3337// then we can flip the true and false operands. This will allow the zero to
3338// be folded with the isel so that we don't need to materialize a register
3339// containing zero.
3340bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3341 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003342 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003343 return false;
3344
3345 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3346 UI != UE; ++UI) {
3347 SDNode *User = *UI;
3348 if (!User->isMachineOpcode())
3349 return false;
3350 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3351 User->getMachineOpcode() != PPC::SELECT_I8)
3352 return false;
3353
3354 SDNode *Op2 = User->getOperand(2).getNode();
3355 if (!Op2->isMachineOpcode())
3356 return false;
3357
3358 if (Op2->getMachineOpcode() != PPC::LI &&
3359 Op2->getMachineOpcode() != PPC::LI8)
3360 return false;
3361
3362 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3363 if (!C)
3364 return false;
3365
3366 if (!C->isNullValue())
3367 return false;
3368 }
3369
3370 return true;
3371}
3372
3373void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3374 SmallVector<SDNode *, 4> ToReplace;
3375 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3376 UI != UE; ++UI) {
3377 SDNode *User = *UI;
3378 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3379 User->getMachineOpcode() == PPC::SELECT_I8) &&
3380 "Must have all select users");
3381 ToReplace.push_back(User);
3382 }
3383
3384 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3385 UE = ToReplace.end(); UI != UE; ++UI) {
3386 SDNode *User = *UI;
3387 SDNode *ResNode =
3388 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3389 User->getValueType(0), User->getOperand(0),
3390 User->getOperand(2),
3391 User->getOperand(1));
3392
3393 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3394 DEBUG(User->dump(CurDAG));
3395 DEBUG(dbgs() << "\nNew: ");
3396 DEBUG(ResNode->dump(CurDAG));
3397 DEBUG(dbgs() << "\n");
3398
3399 ReplaceUses(User, ResNode);
3400 }
3401}
3402
Eric Christopher02e18042014-05-14 00:31:15 +00003403void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003404 bool IsModified;
3405 do {
3406 IsModified = false;
3407 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
3408 E = CurDAG->allnodes_end(); I != E; ++I) {
3409 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
3410 if (!MachineNode || MachineNode->use_empty())
3411 continue;
3412 SDNode *ResNode = MachineNode;
3413
3414 bool Op1Set = false, Op1Unset = false,
3415 Op1Not = false,
3416 Op2Set = false, Op2Unset = false,
3417 Op2Not = false;
3418
3419 unsigned Opcode = MachineNode->getMachineOpcode();
3420 switch (Opcode) {
3421 default: break;
3422 case PPC::CRAND:
3423 case PPC::CRNAND:
3424 case PPC::CROR:
3425 case PPC::CRXOR:
3426 case PPC::CRNOR:
3427 case PPC::CREQV:
3428 case PPC::CRANDC:
3429 case PPC::CRORC: {
3430 SDValue Op = MachineNode->getOperand(1);
3431 if (Op.isMachineOpcode()) {
3432 if (Op.getMachineOpcode() == PPC::CRSET)
3433 Op2Set = true;
3434 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3435 Op2Unset = true;
3436 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3437 Op.getOperand(0) == Op.getOperand(1))
3438 Op2Not = true;
3439 }
3440 } // fallthrough
3441 case PPC::BC:
3442 case PPC::BCn:
3443 case PPC::SELECT_I4:
3444 case PPC::SELECT_I8:
3445 case PPC::SELECT_F4:
3446 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003447 case PPC::SELECT_QFRC:
3448 case PPC::SELECT_QSRC:
3449 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003450 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003451 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003452 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003453 SDValue Op = MachineNode->getOperand(0);
3454 if (Op.isMachineOpcode()) {
3455 if (Op.getMachineOpcode() == PPC::CRSET)
3456 Op1Set = true;
3457 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3458 Op1Unset = true;
3459 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3460 Op.getOperand(0) == Op.getOperand(1))
3461 Op1Not = true;
3462 }
3463 }
3464 break;
3465 }
3466
Hal Finkelb9989152014-02-28 06:11:16 +00003467 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003468 switch (Opcode) {
3469 default: break;
3470 case PPC::CRAND:
3471 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3472 // x & x = x
3473 ResNode = MachineNode->getOperand(0).getNode();
3474 else if (Op1Set)
3475 // 1 & y = y
3476 ResNode = MachineNode->getOperand(1).getNode();
3477 else if (Op2Set)
3478 // x & 1 = x
3479 ResNode = MachineNode->getOperand(0).getNode();
3480 else if (Op1Unset || Op2Unset)
3481 // x & 0 = 0 & y = 0
3482 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3483 MVT::i1);
3484 else if (Op1Not)
3485 // ~x & y = andc(y, x)
3486 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3487 MVT::i1, MachineNode->getOperand(1),
3488 MachineNode->getOperand(0).
3489 getOperand(0));
3490 else if (Op2Not)
3491 // x & ~y = andc(x, y)
3492 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3493 MVT::i1, MachineNode->getOperand(0),
3494 MachineNode->getOperand(1).
3495 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003496 else if (AllUsersSelectZero(MachineNode))
3497 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3498 MVT::i1, MachineNode->getOperand(0),
3499 MachineNode->getOperand(1)),
3500 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003501 break;
3502 case PPC::CRNAND:
3503 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3504 // nand(x, x) -> nor(x, x)
3505 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3506 MVT::i1, MachineNode->getOperand(0),
3507 MachineNode->getOperand(0));
3508 else if (Op1Set)
3509 // nand(1, y) -> nor(y, y)
3510 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3511 MVT::i1, MachineNode->getOperand(1),
3512 MachineNode->getOperand(1));
3513 else if (Op2Set)
3514 // nand(x, 1) -> nor(x, x)
3515 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3516 MVT::i1, MachineNode->getOperand(0),
3517 MachineNode->getOperand(0));
3518 else if (Op1Unset || Op2Unset)
3519 // nand(x, 0) = nand(0, y) = 1
3520 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3521 MVT::i1);
3522 else if (Op1Not)
3523 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3524 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3525 MVT::i1, MachineNode->getOperand(0).
3526 getOperand(0),
3527 MachineNode->getOperand(1));
3528 else if (Op2Not)
3529 // nand(x, ~y) = ~x | y = orc(y, x)
3530 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3531 MVT::i1, MachineNode->getOperand(1).
3532 getOperand(0),
3533 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003534 else if (AllUsersSelectZero(MachineNode))
3535 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3536 MVT::i1, MachineNode->getOperand(0),
3537 MachineNode->getOperand(1)),
3538 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003539 break;
3540 case PPC::CROR:
3541 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3542 // x | x = x
3543 ResNode = MachineNode->getOperand(0).getNode();
3544 else if (Op1Set || Op2Set)
3545 // x | 1 = 1 | y = 1
3546 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3547 MVT::i1);
3548 else if (Op1Unset)
3549 // 0 | y = y
3550 ResNode = MachineNode->getOperand(1).getNode();
3551 else if (Op2Unset)
3552 // x | 0 = x
3553 ResNode = MachineNode->getOperand(0).getNode();
3554 else if (Op1Not)
3555 // ~x | y = orc(y, x)
3556 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3557 MVT::i1, MachineNode->getOperand(1),
3558 MachineNode->getOperand(0).
3559 getOperand(0));
3560 else if (Op2Not)
3561 // x | ~y = orc(x, y)
3562 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3563 MVT::i1, MachineNode->getOperand(0),
3564 MachineNode->getOperand(1).
3565 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003566 else if (AllUsersSelectZero(MachineNode))
3567 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3568 MVT::i1, MachineNode->getOperand(0),
3569 MachineNode->getOperand(1)),
3570 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003571 break;
3572 case PPC::CRXOR:
3573 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3574 // xor(x, x) = 0
3575 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3576 MVT::i1);
3577 else if (Op1Set)
3578 // xor(1, y) -> nor(y, y)
3579 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3580 MVT::i1, MachineNode->getOperand(1),
3581 MachineNode->getOperand(1));
3582 else if (Op2Set)
3583 // xor(x, 1) -> nor(x, x)
3584 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3585 MVT::i1, MachineNode->getOperand(0),
3586 MachineNode->getOperand(0));
3587 else if (Op1Unset)
3588 // xor(0, y) = y
3589 ResNode = MachineNode->getOperand(1).getNode();
3590 else if (Op2Unset)
3591 // xor(x, 0) = x
3592 ResNode = MachineNode->getOperand(0).getNode();
3593 else if (Op1Not)
3594 // xor(~x, y) = eqv(x, y)
3595 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3596 MVT::i1, MachineNode->getOperand(0).
3597 getOperand(0),
3598 MachineNode->getOperand(1));
3599 else if (Op2Not)
3600 // xor(x, ~y) = eqv(x, y)
3601 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3602 MVT::i1, MachineNode->getOperand(0),
3603 MachineNode->getOperand(1).
3604 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003605 else if (AllUsersSelectZero(MachineNode))
3606 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3607 MVT::i1, MachineNode->getOperand(0),
3608 MachineNode->getOperand(1)),
3609 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003610 break;
3611 case PPC::CRNOR:
3612 if (Op1Set || Op2Set)
3613 // nor(1, y) -> 0
3614 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3615 MVT::i1);
3616 else if (Op1Unset)
3617 // nor(0, y) = ~y -> nor(y, y)
3618 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3619 MVT::i1, MachineNode->getOperand(1),
3620 MachineNode->getOperand(1));
3621 else if (Op2Unset)
3622 // nor(x, 0) = ~x
3623 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3624 MVT::i1, MachineNode->getOperand(0),
3625 MachineNode->getOperand(0));
3626 else if (Op1Not)
3627 // nor(~x, y) = andc(x, y)
3628 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3629 MVT::i1, MachineNode->getOperand(0).
3630 getOperand(0),
3631 MachineNode->getOperand(1));
3632 else if (Op2Not)
3633 // nor(x, ~y) = andc(y, x)
3634 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3635 MVT::i1, MachineNode->getOperand(1).
3636 getOperand(0),
3637 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003638 else if (AllUsersSelectZero(MachineNode))
3639 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3640 MVT::i1, MachineNode->getOperand(0),
3641 MachineNode->getOperand(1)),
3642 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003643 break;
3644 case PPC::CREQV:
3645 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3646 // eqv(x, x) = 1
3647 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3648 MVT::i1);
3649 else if (Op1Set)
3650 // eqv(1, y) = y
3651 ResNode = MachineNode->getOperand(1).getNode();
3652 else if (Op2Set)
3653 // eqv(x, 1) = x
3654 ResNode = MachineNode->getOperand(0).getNode();
3655 else if (Op1Unset)
3656 // eqv(0, y) = ~y -> nor(y, y)
3657 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3658 MVT::i1, MachineNode->getOperand(1),
3659 MachineNode->getOperand(1));
3660 else if (Op2Unset)
3661 // eqv(x, 0) = ~x
3662 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3663 MVT::i1, MachineNode->getOperand(0),
3664 MachineNode->getOperand(0));
3665 else if (Op1Not)
3666 // eqv(~x, y) = xor(x, y)
3667 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3668 MVT::i1, MachineNode->getOperand(0).
3669 getOperand(0),
3670 MachineNode->getOperand(1));
3671 else if (Op2Not)
3672 // eqv(x, ~y) = xor(x, y)
3673 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3674 MVT::i1, MachineNode->getOperand(0),
3675 MachineNode->getOperand(1).
3676 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003677 else if (AllUsersSelectZero(MachineNode))
3678 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3679 MVT::i1, MachineNode->getOperand(0),
3680 MachineNode->getOperand(1)),
3681 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003682 break;
3683 case PPC::CRANDC:
3684 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3685 // andc(x, x) = 0
3686 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3687 MVT::i1);
3688 else if (Op1Set)
3689 // andc(1, y) = ~y
3690 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3691 MVT::i1, MachineNode->getOperand(1),
3692 MachineNode->getOperand(1));
3693 else if (Op1Unset || Op2Set)
3694 // andc(0, y) = andc(x, 1) = 0
3695 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3696 MVT::i1);
3697 else if (Op2Unset)
3698 // andc(x, 0) = x
3699 ResNode = MachineNode->getOperand(0).getNode();
3700 else if (Op1Not)
3701 // andc(~x, y) = ~(x | y) = nor(x, y)
3702 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3703 MVT::i1, MachineNode->getOperand(0).
3704 getOperand(0),
3705 MachineNode->getOperand(1));
3706 else if (Op2Not)
3707 // andc(x, ~y) = x & y
3708 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3709 MVT::i1, MachineNode->getOperand(0),
3710 MachineNode->getOperand(1).
3711 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003712 else if (AllUsersSelectZero(MachineNode))
3713 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3714 MVT::i1, MachineNode->getOperand(1),
3715 MachineNode->getOperand(0)),
3716 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003717 break;
3718 case PPC::CRORC:
3719 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3720 // orc(x, x) = 1
3721 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3722 MVT::i1);
3723 else if (Op1Set || Op2Unset)
3724 // orc(1, y) = orc(x, 0) = 1
3725 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3726 MVT::i1);
3727 else if (Op2Set)
3728 // orc(x, 1) = x
3729 ResNode = MachineNode->getOperand(0).getNode();
3730 else if (Op1Unset)
3731 // orc(0, y) = ~y
3732 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3733 MVT::i1, MachineNode->getOperand(1),
3734 MachineNode->getOperand(1));
3735 else if (Op1Not)
3736 // orc(~x, y) = ~(x & y) = nand(x, y)
3737 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3738 MVT::i1, MachineNode->getOperand(0).
3739 getOperand(0),
3740 MachineNode->getOperand(1));
3741 else if (Op2Not)
3742 // orc(x, ~y) = x | y
3743 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3744 MVT::i1, MachineNode->getOperand(0),
3745 MachineNode->getOperand(1).
3746 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003747 else if (AllUsersSelectZero(MachineNode))
3748 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3749 MVT::i1, MachineNode->getOperand(1),
3750 MachineNode->getOperand(0)),
3751 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003752 break;
3753 case PPC::SELECT_I4:
3754 case PPC::SELECT_I8:
3755 case PPC::SELECT_F4:
3756 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003757 case PPC::SELECT_QFRC:
3758 case PPC::SELECT_QSRC:
3759 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003760 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003761 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003762 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003763 if (Op1Set)
3764 ResNode = MachineNode->getOperand(1).getNode();
3765 else if (Op1Unset)
3766 ResNode = MachineNode->getOperand(2).getNode();
3767 else if (Op1Not)
3768 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3769 SDLoc(MachineNode),
3770 MachineNode->getValueType(0),
3771 MachineNode->getOperand(0).
3772 getOperand(0),
3773 MachineNode->getOperand(2),
3774 MachineNode->getOperand(1));
3775 break;
3776 case PPC::BC:
3777 case PPC::BCn:
3778 if (Op1Not)
3779 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3780 PPC::BC,
3781 SDLoc(MachineNode),
3782 MVT::Other,
3783 MachineNode->getOperand(0).
3784 getOperand(0),
3785 MachineNode->getOperand(1),
3786 MachineNode->getOperand(2));
3787 // FIXME: Handle Op1Set, Op1Unset here too.
3788 break;
3789 }
3790
Hal Finkelb9989152014-02-28 06:11:16 +00003791 // If we're inverting this node because it is used only by selects that
3792 // we'd like to swap, then swap the selects before the node replacement.
3793 if (SelectSwap)
3794 SwapAllSelectUsers(MachineNode);
3795
Hal Finkel940ab932014-02-28 00:27:01 +00003796 if (ResNode != MachineNode) {
3797 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3798 DEBUG(MachineNode->dump(CurDAG));
3799 DEBUG(dbgs() << "\nNew: ");
3800 DEBUG(ResNode->dump(CurDAG));
3801 DEBUG(dbgs() << "\n");
3802
3803 ReplaceUses(MachineNode, ResNode);
3804 IsModified = true;
3805 }
3806 }
3807 if (IsModified)
3808 CurDAG->RemoveDeadNodes();
3809 } while (IsModified);
3810}
3811
Hal Finkel4c6658f2014-12-12 23:59:36 +00003812// Gather the set of 32-bit operations that are known to have their
3813// higher-order 32 bits zero, where ToPromote contains all such operations.
3814static bool PeepholePPC64ZExtGather(SDValue Op32,
3815 SmallPtrSetImpl<SDNode *> &ToPromote) {
3816 if (!Op32.isMachineOpcode())
3817 return false;
3818
3819 // First, check for the "frontier" instructions (those that will clear the
3820 // higher-order 32 bits.
3821
3822 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3823 // around. If it does not, then these instructions will clear the
3824 // higher-order bits.
3825 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3826 Op32.getMachineOpcode() == PPC::RLWNM) &&
3827 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3828 ToPromote.insert(Op32.getNode());
3829 return true;
3830 }
3831
3832 // SLW and SRW always clear the higher-order bits.
3833 if (Op32.getMachineOpcode() == PPC::SLW ||
3834 Op32.getMachineOpcode() == PPC::SRW) {
3835 ToPromote.insert(Op32.getNode());
3836 return true;
3837 }
3838
3839 // For LI and LIS, we need the immediate to be positive (so that it is not
3840 // sign extended).
3841 if (Op32.getMachineOpcode() == PPC::LI ||
3842 Op32.getMachineOpcode() == PPC::LIS) {
3843 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3844 return false;
3845
3846 ToPromote.insert(Op32.getNode());
3847 return true;
3848 }
3849
Hal Finkel4e2c7822015-01-05 18:09:06 +00003850 // LHBRX and LWBRX always clear the higher-order bits.
3851 if (Op32.getMachineOpcode() == PPC::LHBRX ||
3852 Op32.getMachineOpcode() == PPC::LWBRX) {
3853 ToPromote.insert(Op32.getNode());
3854 return true;
3855 }
3856
Hal Finkel49557f12015-01-05 18:52:29 +00003857 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended.
3858 if (Op32.getMachineOpcode() == PPC::CNTLZW) {
3859 ToPromote.insert(Op32.getNode());
3860 return true;
3861 }
3862
Hal Finkel4c6658f2014-12-12 23:59:36 +00003863 // Next, check for those instructions we can look through.
3864
3865 // Assuming the mask does not wrap around, then the higher-order bits are
3866 // taken directly from the first operand.
3867 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
3868 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
3869 SmallPtrSet<SDNode *, 16> ToPromote1;
3870 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3871 return false;
3872
3873 ToPromote.insert(Op32.getNode());
3874 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3875 return true;
3876 }
3877
3878 // For OR, the higher-order bits are zero if that is true for both operands.
3879 // For SELECT_I4, the same is true (but the relevant operand numbers are
3880 // shifted by 1).
3881 if (Op32.getMachineOpcode() == PPC::OR ||
3882 Op32.getMachineOpcode() == PPC::SELECT_I4) {
3883 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
3884 SmallPtrSet<SDNode *, 16> ToPromote1;
3885 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
3886 return false;
3887 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
3888 return false;
3889
3890 ToPromote.insert(Op32.getNode());
3891 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3892 return true;
3893 }
3894
3895 // For ORI and ORIS, we need the higher-order bits of the first operand to be
3896 // zero, and also for the constant to be positive (so that it is not sign
3897 // extended).
3898 if (Op32.getMachineOpcode() == PPC::ORI ||
3899 Op32.getMachineOpcode() == PPC::ORIS) {
3900 SmallPtrSet<SDNode *, 16> ToPromote1;
3901 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3902 return false;
3903 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
3904 return false;
3905
3906 ToPromote.insert(Op32.getNode());
3907 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3908 return true;
3909 }
3910
3911 // The higher-order bits of AND are zero if that is true for at least one of
3912 // the operands.
3913 if (Op32.getMachineOpcode() == PPC::AND) {
3914 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
3915 bool Op0OK =
3916 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3917 bool Op1OK =
3918 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
3919 if (!Op0OK && !Op1OK)
3920 return false;
3921
3922 ToPromote.insert(Op32.getNode());
3923
3924 if (Op0OK)
3925 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3926
3927 if (Op1OK)
3928 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
3929
3930 return true;
3931 }
3932
3933 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
3934 // of the first operand, or if the second operand is positive (so that it is
3935 // not sign extended).
3936 if (Op32.getMachineOpcode() == PPC::ANDIo ||
3937 Op32.getMachineOpcode() == PPC::ANDISo) {
3938 SmallPtrSet<SDNode *, 16> ToPromote1;
3939 bool Op0OK =
3940 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3941 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
3942 if (!Op0OK && !Op1OK)
3943 return false;
3944
3945 ToPromote.insert(Op32.getNode());
3946
3947 if (Op0OK)
3948 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3949
3950 return true;
3951 }
3952
3953 return false;
3954}
3955
3956void PPCDAGToDAGISel::PeepholePPC64ZExt() {
3957 if (!PPCSubTarget->isPPC64())
3958 return;
3959
3960 // When we zero-extend from i32 to i64, we use a pattern like this:
3961 // def : Pat<(i64 (zext i32:$in)),
3962 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
3963 // 0, 32)>;
3964 // There are several 32-bit shift/rotate instructions, however, that will
3965 // clear the higher-order bits of their output, rendering the RLDICL
3966 // unnecessary. When that happens, we remove it here, and redefine the
3967 // relevant 32-bit operation to be a 64-bit operation.
3968
3969 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3970 ++Position;
3971
3972 bool MadeChange = false;
3973 while (Position != CurDAG->allnodes_begin()) {
3974 SDNode *N = --Position;
3975 // Skip dead nodes and any non-machine opcodes.
3976 if (N->use_empty() || !N->isMachineOpcode())
3977 continue;
3978
3979 if (N->getMachineOpcode() != PPC::RLDICL)
3980 continue;
3981
3982 if (N->getConstantOperandVal(1) != 0 ||
3983 N->getConstantOperandVal(2) != 32)
3984 continue;
3985
3986 SDValue ISR = N->getOperand(0);
3987 if (!ISR.isMachineOpcode() ||
3988 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
3989 continue;
3990
3991 if (!ISR.hasOneUse())
3992 continue;
3993
3994 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
3995 continue;
3996
3997 SDValue IDef = ISR.getOperand(0);
3998 if (!IDef.isMachineOpcode() ||
3999 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
4000 continue;
4001
4002 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
4003 // can get rid of it.
4004
4005 SDValue Op32 = ISR->getOperand(1);
4006 if (!Op32.isMachineOpcode())
4007 continue;
4008
4009 // There are some 32-bit instructions that always clear the high-order 32
4010 // bits, there are also some instructions (like AND) that we can look
4011 // through.
4012 SmallPtrSet<SDNode *, 16> ToPromote;
4013 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4014 continue;
4015
4016 // If the ToPromote set contains nodes that have uses outside of the set
4017 // (except for the original INSERT_SUBREG), then abort the transformation.
4018 bool OutsideUse = false;
4019 for (SDNode *PN : ToPromote) {
4020 for (SDNode *UN : PN->uses()) {
4021 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4022 OutsideUse = true;
4023 break;
4024 }
4025 }
4026
4027 if (OutsideUse)
4028 break;
4029 }
4030 if (OutsideUse)
4031 continue;
4032
4033 MadeChange = true;
4034
4035 // We now know that this zero extension can be removed by promoting to
4036 // nodes in ToPromote to 64-bit operations, where for operations in the
4037 // frontier of the set, we need to insert INSERT_SUBREGs for their
4038 // operands.
4039 for (SDNode *PN : ToPromote) {
4040 unsigned NewOpcode;
4041 switch (PN->getMachineOpcode()) {
4042 default:
4043 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4044 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4045 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4046 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4047 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4048 case PPC::LI: NewOpcode = PPC::LI8; break;
4049 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004050 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4051 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004052 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004053 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4054 case PPC::OR: NewOpcode = PPC::OR8; break;
4055 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4056 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4057 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4058 case PPC::AND: NewOpcode = PPC::AND8; break;
4059 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4060 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4061 }
4062
4063 // Note: During the replacement process, the nodes will be in an
4064 // inconsistent state (some instructions will have operands with values
4065 // of the wrong type). Once done, however, everything should be right
4066 // again.
4067
4068 SmallVector<SDValue, 4> Ops;
4069 for (const SDValue &V : PN->ops()) {
4070 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4071 !isa<ConstantSDNode>(V)) {
4072 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4073 SDNode *ReplOp =
4074 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4075 ISR.getNode()->getVTList(), ReplOpOps);
4076 Ops.push_back(SDValue(ReplOp, 0));
4077 } else {
4078 Ops.push_back(V);
4079 }
4080 }
4081
4082 // Because all to-be-promoted nodes only have users that are other
4083 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4084 // the i32 result value type with i64.
4085
4086 SmallVector<EVT, 2> NewVTs;
4087 SDVTList VTs = PN->getVTList();
4088 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4089 if (VTs.VTs[i] == MVT::i32)
4090 NewVTs.push_back(MVT::i64);
4091 else
4092 NewVTs.push_back(VTs.VTs[i]);
4093
4094 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4095 DEBUG(PN->dump(CurDAG));
4096
4097 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4098
4099 DEBUG(dbgs() << "\nNew: ");
4100 DEBUG(PN->dump(CurDAG));
4101 DEBUG(dbgs() << "\n");
4102 }
4103
4104 // Now we replace the original zero extend and its associated INSERT_SUBREG
4105 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4106 // return an i64).
4107
4108 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4109 DEBUG(N->dump(CurDAG));
4110 DEBUG(dbgs() << "\nNew: ");
4111 DEBUG(Op32.getNode()->dump(CurDAG));
4112 DEBUG(dbgs() << "\n");
4113
4114 ReplaceUses(N, Op32.getNode());
4115 }
4116
4117 if (MadeChange)
4118 CurDAG->RemoveDeadNodes();
4119}
4120
Hal Finkel940ab932014-02-28 00:27:01 +00004121void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004122 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004123 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004124 return;
4125
4126 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4127 ++Position;
4128
4129 while (Position != CurDAG->allnodes_begin()) {
4130 SDNode *N = --Position;
4131 // Skip dead nodes and any non-machine opcodes.
4132 if (N->use_empty() || !N->isMachineOpcode())
4133 continue;
4134
4135 unsigned FirstOp;
4136 unsigned StorageOpcode = N->getMachineOpcode();
4137
4138 switch (StorageOpcode) {
4139 default: continue;
4140
4141 case PPC::LBZ:
4142 case PPC::LBZ8:
4143 case PPC::LD:
4144 case PPC::LFD:
4145 case PPC::LFS:
4146 case PPC::LHA:
4147 case PPC::LHA8:
4148 case PPC::LHZ:
4149 case PPC::LHZ8:
4150 case PPC::LWA:
4151 case PPC::LWZ:
4152 case PPC::LWZ8:
4153 FirstOp = 0;
4154 break;
4155
4156 case PPC::STB:
4157 case PPC::STB8:
4158 case PPC::STD:
4159 case PPC::STFD:
4160 case PPC::STFS:
4161 case PPC::STH:
4162 case PPC::STH8:
4163 case PPC::STW:
4164 case PPC::STW8:
4165 FirstOp = 1;
4166 break;
4167 }
4168
4169 // If this is a load or store with a zero offset, we may be able to
4170 // fold an add-immediate into the memory operation.
4171 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
4172 N->getConstantOperandVal(FirstOp) != 0)
4173 continue;
4174
4175 SDValue Base = N->getOperand(FirstOp + 1);
4176 if (!Base.isMachineOpcode())
4177 continue;
4178
4179 unsigned Flags = 0;
4180 bool ReplaceFlags = true;
4181
4182 // When the feeding operation is an add-immediate of some sort,
4183 // determine whether we need to add relocation information to the
4184 // target flags on the immediate operand when we fold it into the
4185 // load instruction.
4186 //
4187 // For something like ADDItocL, the relocation information is
4188 // inferred from the opcode; when we process it in the AsmPrinter,
4189 // we add the necessary relocation there. A load, though, can receive
4190 // relocation from various flavors of ADDIxxx, so we need to carry
4191 // the relocation information in the target flags.
4192 switch (Base.getMachineOpcode()) {
4193 default: continue;
4194
4195 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004196 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004197 // In some cases (such as TLS) the relocation information
4198 // is already in place on the operand, so copying the operand
4199 // is sufficient.
4200 ReplaceFlags = false;
4201 // For these cases, the immediate may not be divisible by 4, in
4202 // which case the fold is illegal for DS-form instructions. (The
4203 // other cases provide aligned addresses and are always safe.)
4204 if ((StorageOpcode == PPC::LWA ||
4205 StorageOpcode == PPC::LD ||
4206 StorageOpcode == PPC::STD) &&
4207 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4208 Base.getConstantOperandVal(1) % 4 != 0))
4209 continue;
4210 break;
4211 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004212 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004213 break;
4214 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004215 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004216 break;
4217 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004218 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004219 break;
4220 }
4221
4222 // We found an opportunity. Reverse the operands from the add
4223 // immediate and substitute them into the load or store. If
4224 // needed, update the target flags for the immediate operand to
4225 // reflect the necessary relocation information.
4226 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4227 DEBUG(Base->dump(CurDAG));
4228 DEBUG(dbgs() << "\nN: ");
4229 DEBUG(N->dump(CurDAG));
4230 DEBUG(dbgs() << "\n");
4231
4232 SDValue ImmOpnd = Base.getOperand(1);
4233
4234 // If the relocation information isn't already present on the
4235 // immediate operand, add it now.
4236 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004237 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004238 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004239 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004240 // We can't perform this optimization for data whose alignment
4241 // is insufficient for the instruction encoding.
4242 if (GV->getAlignment() < 4 &&
4243 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
4244 StorageOpcode == PPC::LWA)) {
4245 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4246 continue;
4247 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004248 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004249 } else if (ConstantPoolSDNode *CP =
4250 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004251 const Constant *C = CP->getConstVal();
4252 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4253 CP->getAlignment(),
4254 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004255 }
4256 }
4257
4258 if (FirstOp == 1) // Store
4259 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4260 Base.getOperand(0), N->getOperand(3));
4261 else // Load
4262 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4263 N->getOperand(2));
4264
4265 // The add-immediate may now be dead, in which case remove it.
4266 if (Base.getNode()->use_empty())
4267 CurDAG->RemoveDeadNode(Base.getNode());
4268 }
4269}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004270
Chris Lattnerb055c872006-06-10 01:15:02 +00004271
Andrew Trickc416ba62010-12-24 04:28:06 +00004272/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004273/// PowerPC-specific DAG, ready for instruction scheduling.
4274///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004275FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004276 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004277}
4278
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004279static void initializePassOnce(PassRegistry &Registry) {
4280 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004281 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4282 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004283 Registry.registerPass(*PI, true);
4284}
4285
4286void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4287 CALL_ONCE_INITIALIZATION(initializePassOnce);
4288}
4289