blob: f68674b5bf82c138e1f77ac03f32ce31f1bf7dee [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"
Hal Finkel65539e32015-12-12 00:32:00 +000019#include "llvm/Analysis/BranchProbabilityInfo.h"
20#include "llvm/CodeGen/FunctionLoweringInfo.h"
Chris Lattner45640392005-08-19 22:38:53 +000021#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
27#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000028#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/GlobalValue.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/Intrinsics.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000032#include "llvm/IR/Module.h"
Hal Finkel940ab932014-02-28 00:27:01 +000033#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000034#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000039using namespace llvm;
40
Chandler Carruth84e68b22014-04-22 02:41:26 +000041#define DEBUG_TYPE "ppc-codegen"
42
Hal Finkel940ab932014-02-28 00:27:01 +000043// FIXME: Remove this once the bug has been fixed!
44cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
45cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
46
Benjamin Kramer970eac42015-02-06 17:51:54 +000047static cl::opt<bool>
48 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
49 cl::desc("use aggressive ppc isel for bit permutations"),
50 cl::Hidden);
51static cl::opt<bool> BPermRewriterNoMasking(
52 "ppc-bit-perm-rewriter-stress-rotates",
53 cl::desc("stress rotate selection in aggressive ppc isel for "
54 "bit permutations"),
55 cl::Hidden);
Hal Finkelc58ce412015-01-01 02:53:29 +000056
Hal Finkel65539e32015-12-12 00:32:00 +000057static cl::opt<bool> EnableBranchHint(
58 "ppc-use-branch-hint", cl::init(true),
59 cl::desc("Enable static hinting of branches on ppc"),
60 cl::Hidden);
61
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000062namespace llvm {
63 void initializePPCDAGToDAGISelPass(PassRegistry&);
64}
65
Chris Lattner43ff01e2005-08-17 19:33:03 +000066namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000067 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000068 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000069 /// instructions for SelectionDAG operations.
70 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000071 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000072 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000073 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +000074 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +000075 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000076 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000077 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christophercccae792015-01-30 22:02:31 +000078 : SelectionDAGISel(tm), TM(tm) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000079 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
80 }
Andrew Trickc416ba62010-12-24 04:28:06 +000081
Craig Topper0d3fa922014-04-29 07:57:37 +000082 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000083 // Make sure we re-emit a set of the global base reg if necessary
84 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +000085 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
86 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +000087 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000088
Eric Christopher1b8e7632014-05-22 01:07:24 +000089 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000090 InsertVRSaveCode(MF);
91
Chris Lattner1678a6c2006-03-16 18:25:23 +000092 return true;
Chris Lattner45640392005-08-19 22:38:53 +000093 }
Andrew Trickc416ba62010-12-24 04:28:06 +000094
Hal Finkel4edc66b2015-01-03 01:16:37 +000095 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +000096 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000097
Chris Lattner43ff01e2005-08-17 19:33:03 +000098 /// getI32Imm - Return a target constant with the specified value, of type
99 /// i32.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000100 inline SDValue getI32Imm(unsigned Imm, SDLoc dl) {
101 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000102 }
Chris Lattner45640392005-08-19 22:38:53 +0000103
Chris Lattner97b3da12006-06-27 00:04:13 +0000104 /// getI64Imm - Return a target constant with the specified value, of type
105 /// i64.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000106 inline SDValue getI64Imm(uint64_t Imm, SDLoc dl) {
107 return CurDAG->getTargetConstant(Imm, dl, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000108 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000109
Chris Lattner97b3da12006-06-27 00:04:13 +0000110 /// getSmallIPtrImm - Return a target constant of pointer type.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000111 inline SDValue getSmallIPtrImm(unsigned Imm, SDLoc dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000112 return CurDAG->getTargetConstant(
113 Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner97b3da12006-06-27 00:04:13 +0000114 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000115
Nate Begemand31efd12006-09-22 05:01:56 +0000116 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
117 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000118 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000119 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000120
Chris Lattner45640392005-08-19 22:38:53 +0000121 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
122 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000123 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000124
Justin Bognerdc8af062016-05-20 21:43:23 +0000125 void selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
Hal Finkelb5e9b042014-12-11 22:51:06 +0000126
Chris Lattner43ff01e2005-08-17 19:33:03 +0000127 // Select - Convert the specified operand from a target-independent to a
128 // target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +0000129 void Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000130
Justin Bognerdc8af062016-05-20 21:43:23 +0000131 bool tryBitfieldInsert(SDNode *N);
132 bool tryBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000133
Chris Lattner2a1823d2005-08-21 18:50:37 +0000134 /// SelectCC - Select a comparison of the specified values with the
135 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000136 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000137
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000138 /// SelectAddrImm - Returns true if the address N can be represented by
139 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000140 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000141 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000142 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000143 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000144
Chris Lattner6f5840c2006-11-16 00:41:37 +0000145 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000146 /// immediate field. Note that the operand at this point is already the
147 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000148 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000149 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000150 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000151 Out = N;
152 return true;
153 }
154
155 return false;
156 }
157
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000158 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
159 /// represented as an indexed [r+r] operation. Returns false if it can
160 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000161 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000162 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000163 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000164
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000165 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
166 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000167 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000168 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000169 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000170
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000171 /// SelectAddrImmX4 - Returns true if the address N can be represented by
172 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
173 /// Suitable for use by STD and friends.
174 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000175 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000176 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000177
Hal Finkel756810f2013-03-21 21:37:52 +0000178 // Select an address into a single register.
179 bool SelectAddr(SDValue N, SDValue &Base) {
180 Base = N;
181 return true;
182 }
183
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000184 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000185 /// inline asm expressions. It is always correct to compute the value into
186 /// a register. The case of adding a (possibly relocatable) constant to a
187 /// register can be improved, but it is wrong to substitute Reg+Reg for
188 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000189 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000190 unsigned ConstraintID,
Craig Topper0d3fa922014-04-29 07:57:37 +0000191 std::vector<SDValue> &OutOps) override {
Hal Finkeld4338382014-12-03 23:40:13 +0000192
Daniel Sanders08288602015-03-17 11:09:13 +0000193 switch(ConstraintID) {
194 default:
195 errs() << "ConstraintID: " << ConstraintID << "\n";
196 llvm_unreachable("Unexpected asm memory constraint");
197 case InlineAsm::Constraint_es:
Daniel Sanders914b9472015-03-17 12:00:04 +0000198 case InlineAsm::Constraint_i:
Daniel Sanders08288602015-03-17 11:09:13 +0000199 case InlineAsm::Constraint_m:
200 case InlineAsm::Constraint_o:
201 case InlineAsm::Constraint_Q:
202 case InlineAsm::Constraint_Z:
203 case InlineAsm::Constraint_Zy:
204 // We need to make sure that this one operand does not end up in r0
205 // (because we might end up lowering this as 0(%op)).
206 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
207 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000208 SDLoc dl(Op);
209 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Daniel Sanders08288602015-03-17 11:09:13 +0000210 SDValue NewOp =
211 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000212 dl, Op.getValueType(),
Daniel Sanders08288602015-03-17 11:09:13 +0000213 Op, RC), 0);
214
215 OutOps.push_back(NewOp);
216 return false;
217 }
218 return true;
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000219 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000220
Dan Gohman5ea74d52009-07-31 18:16:33 +0000221 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000222
Craig Topper0d3fa922014-04-29 07:57:37 +0000223 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000224 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000225 }
226
Chris Lattner03e08ee2005-09-13 22:03:06 +0000227// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000228#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000229
Chris Lattner259e6c72005-10-06 18:45:51 +0000230private:
Justin Bognerdc8af062016-05-20 21:43:23 +0000231 bool trySETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000232
233 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000234 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000235 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000236
Hal Finkel4edc66b2015-01-03 01:16:37 +0000237 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000238 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000239
Hal Finkelb9989152014-02-28 06:11:16 +0000240 bool AllUsersSelectZero(SDNode *N);
241 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000242
Justin Bognerdc8af062016-05-20 21:43:23 +0000243 void transferMemOperands(SDNode *N, SDNode *Result);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000244 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000245}
Chris Lattner43ff01e2005-08-17 19:33:03 +0000246
Chris Lattner1678a6c2006-03-16 18:25:23 +0000247/// InsertVRSaveCode - Once the entire function has been instruction selected,
248/// all virtual registers are created and all machine instructions are built,
249/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000250void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000251 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000252 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000253 //
Dan Gohman4a618822010-02-10 16:03:48 +0000254 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000255 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000256 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000257 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
258 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
259 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000260 HasVectorVReg = true;
261 break;
262 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000263 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000264 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000265
Chris Lattner02e2c182006-03-13 21:52:10 +0000266 // If we have a vector register, we want to emit code into the entry and exit
267 // blocks to save and restore the VRSAVE register. We do this here (instead
268 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
269 //
270 // 1. This (trivially) reduces the load on the register allocator, by not
271 // having to represent the live range of the VRSAVE register.
272 // 2. This (more significantly) allows us to create a temporary virtual
273 // register to hold the saved VRSAVE value, allowing this temporary to be
274 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000275
276 // Create two vregs - one to hold the VRSAVE register that is live-in to the
277 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000278 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
279 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000280
Eric Christophercccae792015-01-30 22:02:31 +0000281 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000282 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000283 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000284 // Emit the following code into the entry block:
285 // InVRSAVE = MFVRSAVE
286 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
287 // MTVRSAVE UpdatedVRSAVE
288 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000289 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
290 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000291 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000292 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000293
Chris Lattner1678a6c2006-03-16 18:25:23 +0000294 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000295 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000296 if (BB->isReturnBlock()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000297 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000298
Chris Lattner1678a6c2006-03-16 18:25:23 +0000299 // Skip over all terminator instructions, which are part of the return
300 // sequence.
301 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000302 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000303 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000304
Chris Lattner1678a6c2006-03-16 18:25:23 +0000305 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000306 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000307 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000308 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000309}
Chris Lattner8ae95252005-09-03 01:17:22 +0000310
Chris Lattner1678a6c2006-03-16 18:25:23 +0000311
Chris Lattner45640392005-08-19 22:38:53 +0000312/// getGlobalBaseReg - Output the instructions required to put the
313/// base address to use for accessing globals into a register.
314///
Evan Cheng61413a32006-08-26 05:34:46 +0000315SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000316 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000317 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000318 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000319 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000320 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000321 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000322 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000323
Mehdi Amini44ede332015-07-09 02:09:04 +0000324 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000325 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000326 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000327 if (M->getPICLevel() == PICLevel::Small) {
328 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
329 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000330 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000331 } else {
332 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
333 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
334 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
335 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000336 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000337 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
338 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
339 }
340 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000341 GlobalBaseReg =
342 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000343 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
344 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000345 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000346 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000347 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000348 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000349 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000350 }
Chris Lattner45640392005-08-19 22:38:53 +0000351 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000352 return CurDAG->getRegister(GlobalBaseReg,
Mehdi Amini44ede332015-07-09 02:09:04 +0000353 PPCLowering->getPointerTy(CurDAG->getDataLayout()))
354 .getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000355}
356
357/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
358/// or 64-bit immediate, and if the value can be accurately represented as a
359/// sign extension from a 16-bit value. If so, this returns true and the
360/// immediate.
361static bool isIntS16Immediate(SDNode *N, short &Imm) {
362 if (N->getOpcode() != ISD::Constant)
363 return false;
364
Dan Gohmaneffb8942008-09-12 16:56:44 +0000365 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000366 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000367 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000368 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000369 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000370}
371
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000372static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000373 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000374}
375
376
Chris Lattner97b3da12006-06-27 00:04:13 +0000377/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
378/// operand. If so Imm will receive the 32-bit value.
379static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000380 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000381 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000382 return true;
383 }
384 return false;
385}
386
Chris Lattner97b3da12006-06-27 00:04:13 +0000387/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
388/// operand. If so Imm will receive the 64-bit value.
389static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000390 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000391 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000392 return true;
393 }
394 return false;
395}
396
397// isInt32Immediate - This method tests to see if a constant operand.
398// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000399static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000400 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000401}
402
Hal Finkel65539e32015-12-12 00:32:00 +0000403static unsigned getBranchHint(unsigned PCC, FunctionLoweringInfo *FuncInfo,
404 const SDValue &DestMBB) {
405 assert(isa<BasicBlockSDNode>(DestMBB));
406
407 if (!FuncInfo->BPI) return PPC::BR_NO_HINT;
408
409 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
410 const TerminatorInst *BBTerm = BB->getTerminator();
411
412 if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT;
413
414 const BasicBlock *TBB = BBTerm->getSuccessor(0);
415 const BasicBlock *FBB = BBTerm->getSuccessor(1);
416
Cong Houe93b8e12015-12-22 18:56:14 +0000417 auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);
418 auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);
Hal Finkel65539e32015-12-12 00:32:00 +0000419
420 // We only want to handle cases which are easy to predict at static time, e.g.
421 // C++ throw statement, that is very likely not taken, or calling never
422 // returned function, e.g. stdlib exit(). So we set Threshold to filter
423 // unwanted cases.
424 //
425 // Below is LLVM branch weight table, we only want to handle case 1, 2
426 //
427 // Case Taken:Nontaken Example
428 // 1. Unreachable 1048575:1 C++ throw, stdlib exit(),
429 // 2. Invoke-terminating 1:1048575
430 // 3. Coldblock 4:64 __builtin_expect
431 // 4. Loop Branch 124:4 For loop
432 // 5. PH/ZH/FPH 20:12
433 const uint32_t Threshold = 10000;
434
Cong Houe93b8e12015-12-22 18:56:14 +0000435 if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))
Hal Finkel65539e32015-12-12 00:32:00 +0000436 return PPC::BR_NO_HINT;
437
438 DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName() << "::"
439 << BB->getName() << "'\n"
Cong Houe93b8e12015-12-22 18:56:14 +0000440 << " -> " << TBB->getName() << ": " << TProb << "\n"
441 << " -> " << FBB->getName() << ": " << FProb << "\n");
Hal Finkel65539e32015-12-12 00:32:00 +0000442
443 const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);
444
Cong Houe93b8e12015-12-22 18:56:14 +0000445 // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities,
446 // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock
Hal Finkel65539e32015-12-12 00:32:00 +0000447 if (BBDN->getBasicBlock()->getBasicBlock() != TBB)
Cong Houe93b8e12015-12-22 18:56:14 +0000448 std::swap(TProb, FProb);
Hal Finkel65539e32015-12-12 00:32:00 +0000449
Cong Houe93b8e12015-12-22 18:56:14 +0000450 return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;
Hal Finkel65539e32015-12-12 00:32:00 +0000451}
Chris Lattner97b3da12006-06-27 00:04:13 +0000452
453// isOpcWithIntImmediate - This method tests to see if the node is a specific
454// opcode and that it has a immediate integer right operand.
455// If so Imm will receive the 32 bit value.
456static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000457 return N->getOpcode() == Opc
458 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000459}
460
Justin Bognerdc8af062016-05-20 21:43:23 +0000461void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
Hal Finkelb5e9b042014-12-11 22:51:06 +0000462 SDLoc dl(SN);
463 int FI = cast<FrameIndexSDNode>(N)->getIndex();
464 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
465 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
466 if (SN->hasOneUse())
Justin Bognerdc8af062016-05-20 21:43:23 +0000467 CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
468 getSmallIPtrImm(Offset, dl));
469 else
470 ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
471 getSmallIPtrImm(Offset, dl)));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000472}
473
Andrew Trickc416ba62010-12-24 04:28:06 +0000474bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
475 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000476 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000477 // Don't even go down this path for i64, since different logic will be
478 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000479 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000480 return false;
481
Nate Begemanb3821a32005-08-18 07:30:46 +0000482 unsigned Shift = 32;
483 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
484 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000485 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000486 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000487 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000488
Nate Begemanb3821a32005-08-18 07:30:46 +0000489 if (Opcode == ISD::SHL) {
490 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000491 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000492 // determine which bits are made indeterminant by shift
493 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000494 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000495 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000496 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000497 // determine which bits are made indeterminant by shift
498 Indeterminant = ~(0xFFFFFFFFu >> Shift);
499 // adjust for the left rotate
500 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000501 } else if (Opcode == ISD::ROTL) {
502 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000503 } else {
504 return false;
505 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000506
Nate Begemanb3821a32005-08-18 07:30:46 +0000507 // if the mask doesn't intersect any Indeterminant bits
508 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000509 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000510 // make sure the mask is still a mask (wrap arounds may not be)
511 return isRunOfOnes(Mask, MB, ME);
512 }
513 return false;
514}
515
Justin Bognerdc8af062016-05-20 21:43:23 +0000516/// Turn an or of two masked values into the rotate left word immediate then
517/// mask insert (rlwimi) instruction.
518bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000519 SDValue Op0 = N->getOperand(0);
520 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000521 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000522
Dan Gohmanf19609a2008-02-27 01:23:58 +0000523 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000524 CurDAG->computeKnownBits(Op0, LKZ, LKO);
525 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000526
Dan Gohmanf19609a2008-02-27 01:23:58 +0000527 unsigned TargetMask = LKZ.getZExtValue();
528 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000529
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000530 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
531 unsigned Op0Opc = Op0.getOpcode();
532 unsigned Op1Opc = Op1.getOpcode();
533 unsigned Value, SH = 0;
534 TargetMask = ~TargetMask;
535 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000536
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000537 // If the LHS has a foldable shift and the RHS does not, then swap it to the
538 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000539 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
540 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
541 Op0.getOperand(0).getOpcode() == ISD::SRL) {
542 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
543 Op1.getOperand(0).getOpcode() != ISD::SRL) {
544 std::swap(Op0, Op1);
545 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000546 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000547 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000548 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000549 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
550 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
551 Op1.getOperand(0).getOpcode() != ISD::SRL) {
552 std::swap(Op0, Op1);
553 std::swap(Op0Opc, Op1Opc);
554 std::swap(TargetMask, InsertMask);
555 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000556 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000557
Nate Begeman1333cea2006-05-07 00:23:38 +0000558 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000559 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000560 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000561
562 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000563 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000564 Op1 = Op1.getOperand(0);
565 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
566 }
567 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000568 // The AND mask might not be a constant, and we need to make sure that
569 // if we're going to fold the masking with the insert, all bits not
570 // know to be zero in the mask are known to be one.
571 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000572 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000573 bool CanFoldMask = InsertMask == MKO.getZExtValue();
574
Nate Begeman1333cea2006-05-07 00:23:38 +0000575 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000576 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000577 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000578 // Note that Value must be in range here (less than 32) because
579 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000580 Op1 = Op1.getOperand(0).getOperand(0);
581 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000582 }
583 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000584
Chris Lattnera2963392006-05-12 16:29:37 +0000585 SH &= 31;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000586 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
587 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +0000588 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
589 return true;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000590 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000591 }
Justin Bognerdc8af062016-05-20 21:43:23 +0000592 return false;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000593}
594
Hal Finkelc58ce412015-01-01 02:53:29 +0000595// Predict the number of instructions that would be generated by calling
Justin Bognerdc8af062016-05-20 21:43:23 +0000596// getInt64(N).
597static unsigned getInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000598 // Assume no remaining bits.
599 unsigned Remainder = 0;
600 // Assume no shift required.
601 unsigned Shift = 0;
602
603 // If it can't be represented as a 32 bit value.
604 if (!isInt<32>(Imm)) {
605 Shift = countTrailingZeros<uint64_t>(Imm);
606 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
607
608 // If the shifted value fits 32 bits.
609 if (isInt<32>(ImmSh)) {
610 // Go with the shifted value.
611 Imm = ImmSh;
612 } else {
613 // Still stuck with a 64 bit value.
614 Remainder = Imm;
615 Shift = 32;
616 Imm >>= 32;
617 }
618 }
619
620 // Intermediate operand.
621 unsigned Result = 0;
622
623 // Handle first 32 bits.
624 unsigned Lo = Imm & 0xFFFF;
Hal Finkelc58ce412015-01-01 02:53:29 +0000625
626 // Simple value.
627 if (isInt<16>(Imm)) {
628 // Just the Lo bits.
629 ++Result;
630 } else if (Lo) {
631 // Handle the Hi bits and Lo bits.
632 Result += 2;
633 } else {
634 // Just the Hi bits.
635 ++Result;
636 }
637
638 // If no shift, we're done.
639 if (!Shift) return Result;
640
641 // Shift for next step if the upper 32-bits were not zero.
642 if (Imm)
643 ++Result;
644
645 // Add in the last bits as required.
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000646 if ((Remainder >> 16) & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000647 ++Result;
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000648 if (Remainder & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000649 ++Result;
650
651 return Result;
652}
653
Hal Finkel241ba792015-01-04 15:43:55 +0000654static uint64_t Rot64(uint64_t Imm, unsigned R) {
655 return (Imm << R) | (Imm >> (64 - R));
656}
657
Justin Bognerdc8af062016-05-20 21:43:23 +0000658static unsigned getInt64Count(int64_t Imm) {
659 unsigned Count = getInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000660 if (Count == 1)
661 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000662
Hal Finkel241ba792015-01-04 15:43:55 +0000663 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000664 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000665 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000666 Count = std::min(Count, RCount);
667
Justin Bognerdc8af062016-05-20 21:43:23 +0000668 // See comments in getInt64 for an explanation of the logic below.
Hal Finkel2f618792015-01-05 03:41:38 +0000669 unsigned LS = findLastSet(RImm);
670 if (LS != r-1)
671 continue;
672
673 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
674 uint64_t RImmWithOnes = RImm | OnesMask;
675
Justin Bognerdc8af062016-05-20 21:43:23 +0000676 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000677 Count = std::min(Count, RCount);
678 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000679
Hal Finkel241ba792015-01-04 15:43:55 +0000680 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000681}
682
Justin Bognerdc8af062016-05-20 21:43:23 +0000683// Select a 64-bit constant. For cost-modeling purposes, getInt64Count
Hal Finkelc58ce412015-01-01 02:53:29 +0000684// (above) needs to be kept in sync with this function.
Justin Bognerdc8af062016-05-20 21:43:23 +0000685static SDNode *getInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000686 // Assume no remaining bits.
687 unsigned Remainder = 0;
688 // Assume no shift required.
689 unsigned Shift = 0;
690
691 // If it can't be represented as a 32 bit value.
692 if (!isInt<32>(Imm)) {
693 Shift = countTrailingZeros<uint64_t>(Imm);
694 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
695
696 // If the shifted value fits 32 bits.
697 if (isInt<32>(ImmSh)) {
698 // Go with the shifted value.
699 Imm = ImmSh;
700 } else {
701 // Still stuck with a 64 bit value.
702 Remainder = Imm;
703 Shift = 32;
704 Imm >>= 32;
705 }
706 }
707
708 // Intermediate operand.
709 SDNode *Result;
710
711 // Handle first 32 bits.
712 unsigned Lo = Imm & 0xFFFF;
713 unsigned Hi = (Imm >> 16) & 0xFFFF;
714
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000715 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
716 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000717 };
718
719 // Simple value.
720 if (isInt<16>(Imm)) {
721 // Just the Lo bits.
722 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
723 } else if (Lo) {
724 // Handle the Hi bits.
725 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
726 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
727 // And Lo bits.
728 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
729 SDValue(Result, 0), getI32Imm(Lo));
730 } else {
731 // Just the Hi bits.
732 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
733 }
734
735 // If no shift, we're done.
736 if (!Shift) return Result;
737
738 // Shift for next step if the upper 32-bits were not zero.
739 if (Imm) {
740 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
741 SDValue(Result, 0),
742 getI32Imm(Shift),
743 getI32Imm(63 - Shift));
744 }
745
746 // Add in the last bits as required.
747 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
748 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
749 SDValue(Result, 0), getI32Imm(Hi));
750 }
751 if ((Lo = Remainder & 0xFFFF)) {
752 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
753 SDValue(Result, 0), getI32Imm(Lo));
754 }
755
756 return Result;
757}
758
Justin Bognerdc8af062016-05-20 21:43:23 +0000759static SDNode *getInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
760 unsigned Count = getInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000761 if (Count == 1)
Justin Bognerdc8af062016-05-20 21:43:23 +0000762 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000763
Hal Finkel241ba792015-01-04 15:43:55 +0000764 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000765
Hal Finkel2f618792015-01-05 03:41:38 +0000766 int64_t MatImm;
767 unsigned MaskEnd;
768
Hal Finkel241ba792015-01-04 15:43:55 +0000769 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000770 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000771 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000772 if (RCount < Count) {
773 Count = RCount;
774 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000775 MatImm = RImm;
776 MaskEnd = 63;
777 }
778
779 // If the immediate to generate has many trailing zeros, it might be
780 // worthwhile to generate a rotated value with too many leading ones
781 // (because that's free with li/lis's sign-extension semantics), and then
782 // mask them off after rotation.
783
784 unsigned LS = findLastSet(RImm);
785 // We're adding (63-LS) higher-order ones, and we expect to mask them off
786 // after performing the inverse rotation by (64-r). So we need that:
787 // 63-LS == 64-r => LS == r-1
788 if (LS != r-1)
789 continue;
790
791 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
792 uint64_t RImmWithOnes = RImm | OnesMask;
793
Justin Bognerdc8af062016-05-20 21:43:23 +0000794 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000795 if (RCount < Count) {
796 Count = RCount;
797 RMin = r;
798 MatImm = RImmWithOnes;
799 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000800 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000801 }
802
Hal Finkel241ba792015-01-04 15:43:55 +0000803 if (!RMin)
Justin Bognerdc8af062016-05-20 21:43:23 +0000804 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel241ba792015-01-04 15:43:55 +0000805
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000806 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
807 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000808 };
809
Justin Bognerdc8af062016-05-20 21:43:23 +0000810 SDValue Val = SDValue(getInt64Direct(CurDAG, dl, MatImm), 0);
Hal Finkel2f618792015-01-05 03:41:38 +0000811 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
812 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000813}
814
Hal Finkelc58ce412015-01-01 02:53:29 +0000815// Select a 64-bit constant.
Justin Bognerdc8af062016-05-20 21:43:23 +0000816static SDNode *getInt64(SelectionDAG *CurDAG, SDNode *N) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000817 SDLoc dl(N);
818
819 // Get 64 bit value.
820 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Justin Bognerdc8af062016-05-20 21:43:23 +0000821 return getInt64(CurDAG, dl, Imm);
Hal Finkelc58ce412015-01-01 02:53:29 +0000822}
823
Hal Finkel8adf2252014-12-16 05:51:41 +0000824namespace {
825class BitPermutationSelector {
826 struct ValueBit {
827 SDValue V;
828
829 // The bit number in the value, using a convention where bit 0 is the
830 // lowest-order bit.
831 unsigned Idx;
832
833 enum Kind {
834 ConstZero,
835 Variable
836 } K;
837
838 ValueBit(SDValue V, unsigned I, Kind K = Variable)
839 : V(V), Idx(I), K(K) {}
840 ValueBit(Kind K = Variable)
841 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
842
843 bool isZero() const {
844 return K == ConstZero;
845 }
846
847 bool hasValue() const {
848 return K == Variable;
849 }
850
851 SDValue getValue() const {
852 assert(hasValue() && "Cannot get the value of a constant bit");
853 return V;
854 }
855
856 unsigned getValueBitIndex() const {
857 assert(hasValue() && "Cannot get the value bit index of a constant bit");
858 return Idx;
859 }
860 };
861
862 // A bit group has the same underlying value and the same rotate factor.
863 struct BitGroup {
864 SDValue V;
865 unsigned RLAmt;
866 unsigned StartIdx, EndIdx;
867
Hal Finkelc58ce412015-01-01 02:53:29 +0000868 // This rotation amount assumes that the lower 32 bits of the quantity are
869 // replicated in the high 32 bits by the rotation operator (which is done
870 // by rlwinm and friends in 64-bit mode).
871 bool Repl32;
872 // Did converting to Repl32 == true change the rotation factor? If it did,
873 // it decreased it by 32.
874 bool Repl32CR;
875 // Was this group coalesced after setting Repl32 to true?
876 bool Repl32Coalesced;
877
Hal Finkel8adf2252014-12-16 05:51:41 +0000878 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000879 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
880 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000881 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
882 " [" << S << ", " << E << "]\n");
883 }
884 };
885
886 // Information on each (Value, RLAmt) pair (like the number of groups
887 // associated with each) used to choose the lowering method.
888 struct ValueRotInfo {
889 SDValue V;
890 unsigned RLAmt;
891 unsigned NumGroups;
892 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000893 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000894
895 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000896 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
897 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000898
899 // For sorting (in reverse order) by NumGroups, and then by
900 // FirstGroupStartIdx.
901 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000902 // We need to sort so that the non-Repl32 come first because, when we're
903 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
904 // masking operation.
905 if (Repl32 < Other.Repl32)
906 return true;
907 else if (Repl32 > Other.Repl32)
908 return false;
909 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000910 return true;
911 else if (NumGroups < Other.NumGroups)
912 return false;
913 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
914 return true;
915 return false;
916 }
917 };
918
919 // Return true if something interesting was deduced, return false if we're
920 // providing only a generic representation of V (or something else likewise
921 // uninteresting for instruction selection).
922 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
923 switch (V.getOpcode()) {
924 default: break;
925 case ISD::ROTL:
926 if (isa<ConstantSDNode>(V.getOperand(1))) {
927 unsigned RotAmt = V.getConstantOperandVal(1);
928
929 SmallVector<ValueBit, 64> LHSBits(Bits.size());
930 getValueBits(V.getOperand(0), LHSBits);
931
932 for (unsigned i = 0; i < Bits.size(); ++i)
933 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
934
935 return true;
936 }
937 break;
938 case ISD::SHL:
939 if (isa<ConstantSDNode>(V.getOperand(1))) {
940 unsigned ShiftAmt = V.getConstantOperandVal(1);
941
942 SmallVector<ValueBit, 64> LHSBits(Bits.size());
943 getValueBits(V.getOperand(0), LHSBits);
944
945 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
946 Bits[i] = LHSBits[i - ShiftAmt];
947
948 for (unsigned i = 0; i < ShiftAmt; ++i)
949 Bits[i] = ValueBit(ValueBit::ConstZero);
950
951 return true;
952 }
953 break;
954 case ISD::SRL:
955 if (isa<ConstantSDNode>(V.getOperand(1))) {
956 unsigned ShiftAmt = V.getConstantOperandVal(1);
957
958 SmallVector<ValueBit, 64> LHSBits(Bits.size());
959 getValueBits(V.getOperand(0), LHSBits);
960
961 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
962 Bits[i] = LHSBits[i + ShiftAmt];
963
964 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
965 Bits[i] = ValueBit(ValueBit::ConstZero);
966
967 return true;
968 }
969 break;
970 case ISD::AND:
971 if (isa<ConstantSDNode>(V.getOperand(1))) {
972 uint64_t Mask = V.getConstantOperandVal(1);
973
974 SmallVector<ValueBit, 64> LHSBits(Bits.size());
975 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
976
977 for (unsigned i = 0; i < Bits.size(); ++i)
978 if (((Mask >> i) & 1) == 1)
979 Bits[i] = LHSBits[i];
980 else
981 Bits[i] = ValueBit(ValueBit::ConstZero);
982
983 // Mark this as interesting, only if the LHS was also interesting. This
984 // prevents the overall procedure from matching a single immediate 'and'
985 // (which is non-optimal because such an and might be folded with other
986 // things if we don't select it here).
987 return LHSTrivial;
988 }
989 break;
990 case ISD::OR: {
991 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
992 getValueBits(V.getOperand(0), LHSBits);
993 getValueBits(V.getOperand(1), RHSBits);
994
995 bool AllDisjoint = true;
996 for (unsigned i = 0; i < Bits.size(); ++i)
997 if (LHSBits[i].isZero())
998 Bits[i] = RHSBits[i];
999 else if (RHSBits[i].isZero())
1000 Bits[i] = LHSBits[i];
1001 else {
1002 AllDisjoint = false;
1003 break;
1004 }
1005
1006 if (!AllDisjoint)
1007 break;
1008
1009 return true;
1010 }
1011 }
1012
1013 for (unsigned i = 0; i < Bits.size(); ++i)
1014 Bits[i] = ValueBit(V, i);
1015
1016 return false;
1017 }
1018
1019 // For each value (except the constant ones), compute the left-rotate amount
1020 // to get it from its original to final position.
1021 void computeRotationAmounts() {
1022 HasZeros = false;
1023 RLAmt.resize(Bits.size());
1024 for (unsigned i = 0; i < Bits.size(); ++i)
1025 if (Bits[i].hasValue()) {
1026 unsigned VBI = Bits[i].getValueBitIndex();
1027 if (i >= VBI)
1028 RLAmt[i] = i - VBI;
1029 else
1030 RLAmt[i] = Bits.size() - (VBI - i);
1031 } else if (Bits[i].isZero()) {
1032 HasZeros = true;
1033 RLAmt[i] = UINT32_MAX;
1034 } else {
1035 llvm_unreachable("Unknown value bit type");
1036 }
1037 }
1038
1039 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +00001040 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
1041 // they break up groups.
1042 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001043 BitGroups.clear();
1044
1045 unsigned LastRLAmt = RLAmt[0];
1046 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1047 unsigned LastGroupStartIdx = 0;
1048 for (unsigned i = 1; i < Bits.size(); ++i) {
1049 unsigned ThisRLAmt = RLAmt[i];
1050 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001051 if (LateMask && !ThisValue) {
1052 ThisValue = LastValue;
1053 ThisRLAmt = LastRLAmt;
1054 // If we're doing late masking, then the first bit group always starts
1055 // at zero (even if the first bits were zero).
1056 if (BitGroups.empty())
1057 LastGroupStartIdx = 0;
1058 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001059
1060 // If this bit has the same underlying value and the same rotate factor as
1061 // the last one, then they're part of the same group.
1062 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1063 continue;
1064
1065 if (LastValue.getNode())
1066 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1067 i-1));
1068 LastRLAmt = ThisRLAmt;
1069 LastValue = ThisValue;
1070 LastGroupStartIdx = i;
1071 }
1072 if (LastValue.getNode())
1073 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1074 Bits.size()-1));
1075
1076 if (BitGroups.empty())
1077 return;
1078
1079 // We might be able to combine the first and last groups.
1080 if (BitGroups.size() > 1) {
1081 // If the first and last groups are the same, then remove the first group
1082 // in favor of the last group, making the ending index of the last group
1083 // equal to the ending index of the to-be-removed first group.
1084 if (BitGroups[0].StartIdx == 0 &&
1085 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1086 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1087 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001088 DEBUG(dbgs() << "\tcombining final bit group with initial one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001089 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1090 BitGroups.erase(BitGroups.begin());
1091 }
1092 }
1093 }
1094
1095 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1096 // associated with each. If there is a degeneracy, pick the one that occurs
1097 // first (in the final value).
1098 void collectValueRotInfo() {
1099 ValueRots.clear();
1100
1101 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001102 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1103 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001104 VRI.V = BG.V;
1105 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001106 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001107 VRI.NumGroups += 1;
1108 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1109 }
1110
1111 // Now that we've collected the various ValueRotInfo instances, we need to
1112 // sort them.
1113 ValueRotsVec.clear();
1114 for (auto &I : ValueRots) {
1115 ValueRotsVec.push_back(I.second);
1116 }
1117 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1118 }
1119
Hal Finkelc58ce412015-01-01 02:53:29 +00001120 // In 64-bit mode, rlwinm and friends have a rotation operator that
1121 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1122 // indices of these instructions can only be in the lower 32 bits, so they
1123 // can only represent some 64-bit bit groups. However, when they can be used,
1124 // the 32-bit replication can be used to represent, as a single bit group,
1125 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1126 // groups when possible. Returns true if any of the bit groups were
1127 // converted.
1128 void assignRepl32BitGroups() {
1129 // If we have bits like this:
1130 //
1131 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1132 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1133 // Groups: | RLAmt = 8 | RLAmt = 40 |
1134 //
1135 // But, making use of a 32-bit operation that replicates the low-order 32
1136 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1137 // of 8.
1138
1139 auto IsAllLow32 = [this](BitGroup & BG) {
1140 if (BG.StartIdx <= BG.EndIdx) {
1141 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1142 if (!Bits[i].hasValue())
1143 continue;
1144 if (Bits[i].getValueBitIndex() >= 32)
1145 return false;
1146 }
1147 } else {
1148 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1149 if (!Bits[i].hasValue())
1150 continue;
1151 if (Bits[i].getValueBitIndex() >= 32)
1152 return false;
1153 }
1154 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1155 if (!Bits[i].hasValue())
1156 continue;
1157 if (Bits[i].getValueBitIndex() >= 32)
1158 return false;
1159 }
1160 }
1161
1162 return true;
1163 };
1164
1165 for (auto &BG : BitGroups) {
1166 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1167 if (IsAllLow32(BG)) {
1168 if (BG.RLAmt >= 32) {
1169 BG.RLAmt -= 32;
1170 BG.Repl32CR = true;
1171 }
1172
1173 BG.Repl32 = true;
1174
1175 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1176 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1177 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1178 }
1179 }
1180 }
1181
1182 // Now walk through the bit groups, consolidating where possible.
1183 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1184 // We might want to remove this bit group by merging it with the previous
1185 // group (which might be the ending group).
1186 auto IP = (I == BitGroups.begin()) ?
1187 std::prev(BitGroups.end()) : std::prev(I);
1188 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1189 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1190
1191 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1192 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1193 " [" << I->StartIdx << ", " << I->EndIdx <<
1194 "] with group with range [" <<
1195 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1196
1197 IP->EndIdx = I->EndIdx;
1198 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1199 IP->Repl32Coalesced = true;
1200 I = BitGroups.erase(I);
1201 continue;
1202 } else {
1203 // There is a special case worth handling: If there is a single group
1204 // covering the entire upper 32 bits, and it can be merged with both
1205 // the next and previous groups (which might be the same group), then
1206 // do so. If it is the same group (so there will be only one group in
1207 // total), then we need to reverse the order of the range so that it
1208 // covers the entire 64 bits.
1209 if (I->StartIdx == 32 && I->EndIdx == 63) {
1210 assert(std::next(I) == BitGroups.end() &&
1211 "bit group ends at index 63 but there is another?");
1212 auto IN = BitGroups.begin();
1213
Justin Bognerb0126992016-05-05 23:19:08 +00001214 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
Hal Finkelc58ce412015-01-01 02:53:29 +00001215 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1216 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1217 IsAllLow32(*I)) {
1218
1219 DEBUG(dbgs() << "\tcombining bit group for " <<
1220 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1221 " [" << I->StartIdx << ", " << I->EndIdx <<
1222 "] with 32-bit replicated groups with ranges [" <<
1223 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1224 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1225
1226 if (IP == IN) {
1227 // There is only one other group; change it to cover the whole
1228 // range (backward, so that it can still be Repl32 but cover the
1229 // whole 64-bit range).
1230 IP->StartIdx = 31;
1231 IP->EndIdx = 30;
1232 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1233 IP->Repl32Coalesced = true;
1234 I = BitGroups.erase(I);
1235 } else {
1236 // There are two separate groups, one before this group and one
1237 // after us (at the beginning). We're going to remove this group,
1238 // but also the group at the very beginning.
1239 IP->EndIdx = IN->EndIdx;
1240 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1241 IP->Repl32Coalesced = true;
1242 I = BitGroups.erase(I);
1243 BitGroups.erase(BitGroups.begin());
1244 }
1245
1246 // This must be the last group in the vector (and we might have
1247 // just invalidated the iterator above), so break here.
1248 break;
1249 }
1250 }
1251 }
1252
1253 ++I;
1254 }
1255 }
1256
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001257 SDValue getI32Imm(unsigned Imm, SDLoc dl) {
1258 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001259 }
1260
Hal Finkelc58ce412015-01-01 02:53:29 +00001261 uint64_t getZerosMask() {
1262 uint64_t Mask = 0;
1263 for (unsigned i = 0; i < Bits.size(); ++i) {
1264 if (Bits[i].hasValue())
1265 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001266 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001267 }
1268
1269 return ~Mask;
1270 }
1271
Hal Finkel8adf2252014-12-16 05:51:41 +00001272 // Depending on the number of groups for a particular value, it might be
1273 // better to rotate, mask explicitly (using andi/andis), and then or the
1274 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001275 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1276 if (BPermRewriterNoMasking)
1277 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001278
1279 for (ValueRotInfo &VRI : ValueRotsVec) {
1280 unsigned Mask = 0;
1281 for (unsigned i = 0; i < Bits.size(); ++i) {
1282 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1283 continue;
1284 if (RLAmt[i] != VRI.RLAmt)
1285 continue;
1286 Mask |= (1u << i);
1287 }
1288
1289 // Compute the masks for andi/andis that would be necessary.
1290 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1291 assert((ANDIMask != 0 || ANDISMask != 0) &&
1292 "No set bits in mask for value bit groups");
1293 bool NeedsRotate = VRI.RLAmt != 0;
1294
1295 // We're trying to minimize the number of instructions. If we have one
1296 // group, using one of andi/andis can break even. If we have three
1297 // groups, we can use both andi and andis and break even (to use both
1298 // andi and andis we also need to or the results together). We need four
1299 // groups if we also need to rotate. To use andi/andis we need to do more
1300 // than break even because rotate-and-mask instructions tend to be easier
1301 // to schedule.
1302
1303 // FIXME: We've biased here against using andi/andis, which is right for
1304 // POWER cores, but not optimal everywhere. For example, on the A2,
1305 // andi/andis have single-cycle latency whereas the rotate-and-mask
1306 // instructions take two cycles, and it would be better to bias toward
1307 // andi/andis in break-even cases.
1308
1309 unsigned NumAndInsts = (unsigned) NeedsRotate +
1310 (unsigned) (ANDIMask != 0) +
1311 (unsigned) (ANDISMask != 0) +
1312 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1313 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001314
1315 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1316 " RL: " << VRI.RLAmt << ":" <<
1317 "\n\t\t\tisel using masking: " << NumAndInsts <<
1318 " using rotates: " << VRI.NumGroups << "\n");
1319
Hal Finkel8adf2252014-12-16 05:51:41 +00001320 if (NumAndInsts >= VRI.NumGroups)
1321 continue;
1322
Hal Finkelc58ce412015-01-01 02:53:29 +00001323 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1324
1325 if (InstCnt) *InstCnt += NumAndInsts;
1326
Hal Finkel8adf2252014-12-16 05:51:41 +00001327 SDValue VRot;
1328 if (VRI.RLAmt) {
1329 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001330 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1331 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001332 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1333 Ops), 0);
1334 } else {
1335 VRot = VRI.V;
1336 }
1337
1338 SDValue ANDIVal, ANDISVal;
1339 if (ANDIMask != 0)
1340 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001341 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001342 if (ANDISMask != 0)
1343 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001344 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001345
1346 SDValue TotalVal;
1347 if (!ANDIVal)
1348 TotalVal = ANDISVal;
1349 else if (!ANDISVal)
1350 TotalVal = ANDIVal;
1351 else
1352 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1353 ANDIVal, ANDISVal), 0);
1354
1355 if (!Res)
1356 Res = TotalVal;
1357 else
1358 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1359 Res, TotalVal), 0);
1360
1361 // Now, remove all groups with this underlying value and rotation
1362 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001363 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1364 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1365 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001366 }
1367 }
1368
1369 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001370 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001371 SDLoc dl(N);
1372 SDValue Res;
1373
Hal Finkelc58ce412015-01-01 02:53:29 +00001374 if (InstCnt) *InstCnt = 0;
1375
Hal Finkel8adf2252014-12-16 05:51:41 +00001376 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001377 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001378
1379 // If we've not yet selected a 'starting' instruction, and we have no zeros
1380 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1381 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001382 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001383 ValueRotInfo &VRI = ValueRotsVec[0];
1384 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001385 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001386 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001387 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1388 getI32Imm(31, dl) };
1389 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1390 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001391 } else {
1392 Res = VRI.V;
1393 }
1394
1395 // Now, remove all groups with this underlying value and rotation factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001396 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1397 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1398 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001399 }
1400
Hal Finkelc58ce412015-01-01 02:53:29 +00001401 if (InstCnt) *InstCnt += BitGroups.size();
1402
Hal Finkel8adf2252014-12-16 05:51:41 +00001403 // Insert the other groups (one at a time).
1404 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001405 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001406 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001407 { BG.V, getI32Imm(BG.RLAmt, dl),
1408 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1409 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001410 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1411 } else {
1412 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001413 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1414 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1415 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001416 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1417 }
1418 }
1419
Hal Finkelc58ce412015-01-01 02:53:29 +00001420 if (LateMask) {
1421 unsigned Mask = (unsigned) getZerosMask();
1422
1423 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1424 assert((ANDIMask != 0 || ANDISMask != 0) &&
1425 "No set bits in zeros mask?");
1426
1427 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1428 (unsigned) (ANDISMask != 0) +
1429 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1430
1431 SDValue ANDIVal, ANDISVal;
1432 if (ANDIMask != 0)
1433 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001434 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001435 if (ANDISMask != 0)
1436 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001438
1439 if (!ANDIVal)
1440 Res = ANDISVal;
1441 else if (!ANDISVal)
1442 Res = ANDIVal;
1443 else
1444 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1445 ANDIVal, ANDISVal), 0);
1446 }
1447
Hal Finkel8adf2252014-12-16 05:51:41 +00001448 return Res.getNode();
1449 }
1450
Hal Finkelc58ce412015-01-01 02:53:29 +00001451 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1452 unsigned MaskStart, unsigned MaskEnd,
1453 bool IsIns) {
1454 // In the notation used by the instructions, 'start' and 'end' are reversed
1455 // because bits are counted from high to low order.
1456 unsigned InstMaskStart = 64 - MaskEnd - 1,
1457 InstMaskEnd = 64 - MaskStart - 1;
1458
1459 if (Repl32)
1460 return 1;
1461
1462 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1463 InstMaskEnd == 63 - RLAmt)
1464 return 1;
1465
1466 return 2;
1467 }
1468
1469 // For 64-bit values, not all combinations of rotates and masks are
1470 // available. Produce one if it is available.
1471 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1472 unsigned MaskStart, unsigned MaskEnd,
1473 unsigned *InstCnt = nullptr) {
1474 // In the notation used by the instructions, 'start' and 'end' are reversed
1475 // because bits are counted from high to low order.
1476 unsigned InstMaskStart = 64 - MaskEnd - 1,
1477 InstMaskEnd = 64 - MaskStart - 1;
1478
1479 if (InstCnt) *InstCnt += 1;
1480
1481 if (Repl32) {
1482 // This rotation amount assumes that the lower 32 bits of the quantity
1483 // are replicated in the high 32 bits by the rotation operator (which is
1484 // done by rlwinm and friends).
1485 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1486 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1487 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001488 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1489 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001490 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1491 Ops), 0);
1492 }
1493
1494 if (InstMaskEnd == 63) {
1495 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001496 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001497 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1498 }
1499
1500 if (InstMaskStart == 0) {
1501 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001502 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001503 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1504 }
1505
1506 if (InstMaskEnd == 63 - RLAmt) {
1507 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001508 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001509 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1510 }
1511
1512 // We cannot do this with a single instruction, so we'll use two. The
1513 // problem is that we're not free to choose both a rotation amount and mask
1514 // start and end independently. We can choose an arbitrary mask start and
1515 // end, but then the rotation amount is fixed. Rotation, however, can be
1516 // inverted, and so by applying an "inverse" rotation first, we can get the
1517 // desired result.
1518 if (InstCnt) *InstCnt += 1;
1519
1520 // The rotation mask for the second instruction must be MaskStart.
1521 unsigned RLAmt2 = MaskStart;
1522 // The first instruction must rotate V so that the overall rotation amount
1523 // is RLAmt.
1524 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1525 if (RLAmt1)
1526 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1527 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1528 }
1529
1530 // For 64-bit values, not all combinations of rotates and masks are
1531 // available. Produce a rotate-mask-and-insert if one is available.
1532 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1533 bool Repl32, unsigned MaskStart,
1534 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1535 // In the notation used by the instructions, 'start' and 'end' are reversed
1536 // because bits are counted from high to low order.
1537 unsigned InstMaskStart = 64 - MaskEnd - 1,
1538 InstMaskEnd = 64 - MaskStart - 1;
1539
1540 if (InstCnt) *InstCnt += 1;
1541
1542 if (Repl32) {
1543 // This rotation amount assumes that the lower 32 bits of the quantity
1544 // are replicated in the high 32 bits by the rotation operator (which is
1545 // done by rlwinm and friends).
1546 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1547 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1548 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001549 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1550 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001551 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1552 Ops), 0);
1553 }
1554
1555 if (InstMaskEnd == 63 - RLAmt) {
1556 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001557 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001558 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1559 }
1560
1561 // We cannot do this with a single instruction, so we'll use two. The
1562 // problem is that we're not free to choose both a rotation amount and mask
1563 // start and end independently. We can choose an arbitrary mask start and
1564 // end, but then the rotation amount is fixed. Rotation, however, can be
1565 // inverted, and so by applying an "inverse" rotation first, we can get the
1566 // desired result.
1567 if (InstCnt) *InstCnt += 1;
1568
1569 // The rotation mask for the second instruction must be MaskStart.
1570 unsigned RLAmt2 = MaskStart;
1571 // The first instruction must rotate V so that the overall rotation amount
1572 // is RLAmt.
1573 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1574 if (RLAmt1)
1575 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1576 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1577 }
1578
1579 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1580 if (BPermRewriterNoMasking)
1581 return;
1582
1583 // The idea here is the same as in the 32-bit version, but with additional
1584 // complications from the fact that Repl32 might be true. Because we
1585 // aggressively convert bit groups to Repl32 form (which, for small
1586 // rotation factors, involves no other change), and then coalesce, it might
1587 // be the case that a single 64-bit masking operation could handle both
1588 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1589 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1590 // completely capture the new combined bit group.
1591
1592 for (ValueRotInfo &VRI : ValueRotsVec) {
1593 uint64_t Mask = 0;
1594
1595 // We need to add to the mask all bits from the associated bit groups.
1596 // If Repl32 is false, we need to add bits from bit groups that have
1597 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1598 // group is trivially convertable if it overlaps only with the lower 32
1599 // bits, and the group has not been coalesced.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001600 auto MatchingBG = [VRI](const BitGroup &BG) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001601 if (VRI.V != BG.V)
1602 return false;
1603
1604 unsigned EffRLAmt = BG.RLAmt;
1605 if (!VRI.Repl32 && BG.Repl32) {
1606 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1607 !BG.Repl32Coalesced) {
1608 if (BG.Repl32CR)
1609 EffRLAmt += 32;
1610 } else {
1611 return false;
1612 }
1613 } else if (VRI.Repl32 != BG.Repl32) {
1614 return false;
1615 }
1616
Alexander Kornienko175a7cb2015-12-28 13:38:42 +00001617 return VRI.RLAmt == EffRLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001618 };
1619
1620 for (auto &BG : BitGroups) {
1621 if (!MatchingBG(BG))
1622 continue;
1623
1624 if (BG.StartIdx <= BG.EndIdx) {
1625 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001626 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001627 } else {
1628 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001629 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001630 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001631 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001632 }
1633 }
1634
1635 // We can use the 32-bit andi/andis technique if the mask does not
1636 // require any higher-order bits. This can save an instruction compared
1637 // to always using the general 64-bit technique.
1638 bool Use32BitInsts = isUInt<32>(Mask);
1639 // Compute the masks for andi/andis that would be necessary.
1640 unsigned ANDIMask = (Mask & UINT16_MAX),
1641 ANDISMask = (Mask >> 16) & UINT16_MAX;
1642
1643 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1644
1645 unsigned NumAndInsts = (unsigned) NeedsRotate +
1646 (unsigned) (bool) Res;
1647 if (Use32BitInsts)
1648 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1649 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1650 else
Justin Bognerdc8af062016-05-20 21:43:23 +00001651 NumAndInsts += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001652
1653 unsigned NumRLInsts = 0;
1654 bool FirstBG = true;
1655 for (auto &BG : BitGroups) {
1656 if (!MatchingBG(BG))
1657 continue;
1658 NumRLInsts +=
1659 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1660 !FirstBG);
1661 FirstBG = false;
1662 }
1663
1664 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1665 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1666 "\n\t\t\tisel using masking: " << NumAndInsts <<
1667 " using rotates: " << NumRLInsts << "\n");
1668
1669 // When we'd use andi/andis, we bias toward using the rotates (andi only
1670 // has a record form, and is cracked on POWER cores). However, when using
1671 // general 64-bit constant formation, bias toward the constant form,
1672 // because that exposes more opportunities for CSE.
1673 if (NumAndInsts > NumRLInsts)
1674 continue;
1675 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1676 continue;
1677
1678 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1679
1680 if (InstCnt) *InstCnt += NumAndInsts;
1681
1682 SDValue VRot;
1683 // We actually need to generate a rotation if we have a non-zero rotation
1684 // factor or, in the Repl32 case, if we care about any of the
1685 // higher-order replicated bits. In the latter case, we generate a mask
1686 // backward so that it actually includes the entire 64 bits.
1687 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1688 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1689 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1690 else
1691 VRot = VRI.V;
1692
1693 SDValue TotalVal;
1694 if (Use32BitInsts) {
1695 assert((ANDIMask != 0 || ANDISMask != 0) &&
1696 "No set bits in mask when using 32-bit ands for 64-bit value");
1697
1698 SDValue ANDIVal, ANDISVal;
1699 if (ANDIMask != 0)
1700 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001701 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001702 if (ANDISMask != 0)
1703 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001704 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001705
1706 if (!ANDIVal)
1707 TotalVal = ANDISVal;
1708 else if (!ANDISVal)
1709 TotalVal = ANDIVal;
1710 else
1711 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1712 ANDIVal, ANDISVal), 0);
1713 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001714 TotalVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001715 TotalVal =
1716 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1717 VRot, TotalVal), 0);
1718 }
1719
1720 if (!Res)
1721 Res = TotalVal;
1722 else
1723 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1724 Res, TotalVal), 0);
1725
1726 // Now, remove all groups with this underlying value and rotation
1727 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001728 eraseMatchingBitGroups(MatchingBG);
Hal Finkelc58ce412015-01-01 02:53:29 +00001729 }
1730 }
1731
1732 // Instruction selection for the 64-bit case.
1733 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1734 SDLoc dl(N);
1735 SDValue Res;
1736
1737 if (InstCnt) *InstCnt = 0;
1738
1739 // Take care of cases that should use andi/andis first.
1740 SelectAndParts64(dl, Res, InstCnt);
1741
1742 // If we've not yet selected a 'starting' instruction, and we have no zeros
1743 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1744 // number of groups), and start with this rotated value.
1745 if ((!HasZeros || LateMask) && !Res) {
1746 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1747 // groups will come first, and so the VRI representing the largest number
1748 // of groups might not be first (it might be the first Repl32 groups).
1749 unsigned MaxGroupsIdx = 0;
1750 if (!ValueRotsVec[0].Repl32) {
1751 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1752 if (ValueRotsVec[i].Repl32) {
1753 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1754 MaxGroupsIdx = i;
1755 break;
1756 }
1757 }
1758
1759 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1760 bool NeedsRotate = false;
1761 if (VRI.RLAmt) {
1762 NeedsRotate = true;
1763 } else if (VRI.Repl32) {
1764 for (auto &BG : BitGroups) {
1765 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1766 BG.Repl32 != VRI.Repl32)
1767 continue;
1768
1769 // We don't need a rotate if the bit group is confined to the lower
1770 // 32 bits.
1771 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1772 continue;
1773
1774 NeedsRotate = true;
1775 break;
1776 }
1777 }
1778
1779 if (NeedsRotate)
1780 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1781 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1782 InstCnt);
1783 else
1784 Res = VRI.V;
1785
1786 // Now, remove all groups with this underlying value and rotation factor.
1787 if (Res)
Benjamin Kramere7561b82015-06-20 15:59:41 +00001788 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1789 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt &&
1790 BG.Repl32 == VRI.Repl32;
1791 });
Hal Finkelc58ce412015-01-01 02:53:29 +00001792 }
1793
1794 // Because 64-bit rotates are more flexible than inserts, we might have a
1795 // preference regarding which one we do first (to save one instruction).
1796 if (!Res)
1797 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1798 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1799 false) <
1800 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1801 true)) {
1802 if (I != BitGroups.begin()) {
1803 BitGroup BG = *I;
1804 BitGroups.erase(I);
1805 BitGroups.insert(BitGroups.begin(), BG);
1806 }
1807
1808 break;
1809 }
1810 }
1811
1812 // Insert the other groups (one at a time).
1813 for (auto &BG : BitGroups) {
1814 if (!Res)
1815 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1816 BG.EndIdx, InstCnt);
1817 else
1818 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1819 BG.StartIdx, BG.EndIdx, InstCnt);
1820 }
1821
1822 if (LateMask) {
1823 uint64_t Mask = getZerosMask();
1824
1825 // We can use the 32-bit andi/andis technique if the mask does not
1826 // require any higher-order bits. This can save an instruction compared
1827 // to always using the general 64-bit technique.
1828 bool Use32BitInsts = isUInt<32>(Mask);
1829 // Compute the masks for andi/andis that would be necessary.
1830 unsigned ANDIMask = (Mask & UINT16_MAX),
1831 ANDISMask = (Mask >> 16) & UINT16_MAX;
1832
1833 if (Use32BitInsts) {
1834 assert((ANDIMask != 0 || ANDISMask != 0) &&
1835 "No set bits in mask when using 32-bit ands for 64-bit value");
1836
1837 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1838 (unsigned) (ANDISMask != 0) +
1839 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1840
1841 SDValue ANDIVal, ANDISVal;
1842 if (ANDIMask != 0)
1843 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001844 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001845 if (ANDISMask != 0)
1846 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001847 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001848
1849 if (!ANDIVal)
1850 Res = ANDISVal;
1851 else if (!ANDISVal)
1852 Res = ANDIVal;
1853 else
1854 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1855 ANDIVal, ANDISVal), 0);
1856 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001857 if (InstCnt) *InstCnt += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001858
Justin Bognerdc8af062016-05-20 21:43:23 +00001859 SDValue MaskVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001860 Res =
1861 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1862 Res, MaskVal), 0);
1863 }
1864 }
1865
1866 return Res.getNode();
1867 }
1868
1869 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1870 // Fill in BitGroups.
1871 collectBitGroups(LateMask);
1872 if (BitGroups.empty())
1873 return nullptr;
1874
1875 // For 64-bit values, figure out when we can use 32-bit instructions.
1876 if (Bits.size() == 64)
1877 assignRepl32BitGroups();
1878
1879 // Fill in ValueRotsVec.
1880 collectValueRotInfo();
1881
1882 if (Bits.size() == 32) {
1883 return Select32(N, LateMask, InstCnt);
1884 } else {
1885 assert(Bits.size() == 64 && "Not 64 bits here?");
1886 return Select64(N, LateMask, InstCnt);
1887 }
1888
1889 return nullptr;
1890 }
1891
Benjamin Kramere7561b82015-06-20 15:59:41 +00001892 void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) {
1893 BitGroups.erase(std::remove_if(BitGroups.begin(), BitGroups.end(), F),
1894 BitGroups.end());
1895 }
1896
Hal Finkel8adf2252014-12-16 05:51:41 +00001897 SmallVector<ValueBit, 64> Bits;
1898
1899 bool HasZeros;
1900 SmallVector<unsigned, 64> RLAmt;
1901
1902 SmallVector<BitGroup, 16> BitGroups;
1903
1904 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1905 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1906
1907 SelectionDAG *CurDAG;
1908
1909public:
1910 BitPermutationSelector(SelectionDAG *DAG)
1911 : CurDAG(DAG) {}
1912
1913 // Here we try to match complex bit permutations into a set of
1914 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1915 // known to produce optimial code for common cases (like i32 byte swapping).
1916 SDNode *Select(SDNode *N) {
1917 Bits.resize(N->getValueType(0).getSizeInBits());
1918 if (!getValueBits(SDValue(N, 0), Bits))
1919 return nullptr;
1920
1921 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1922 " selection for: ");
1923 DEBUG(N->dump(CurDAG));
1924
1925 // Fill it RLAmt and set HasZeros.
1926 computeRotationAmounts();
1927
Hal Finkelc58ce412015-01-01 02:53:29 +00001928 if (!HasZeros)
1929 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001930
Hal Finkelc58ce412015-01-01 02:53:29 +00001931 // We currently have two techniques for handling results with zeros: early
1932 // masking (the default) and late masking. Late masking is sometimes more
1933 // efficient, but because the structure of the bit groups is different, it
1934 // is hard to tell without generating both and comparing the results. With
1935 // late masking, we ignore zeros in the resulting value when inserting each
1936 // set of bit groups, and then mask in the zeros at the end. With early
1937 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001938
Hal Finkelc58ce412015-01-01 02:53:29 +00001939 unsigned InstCnt, InstCntLateMask;
1940 DEBUG(dbgs() << "\tEarly masking:\n");
1941 SDNode *RN = Select(N, false, &InstCnt);
1942 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1943
1944 DEBUG(dbgs() << "\tLate masking:\n");
1945 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1946 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1947 " instructions\n");
1948
1949 if (InstCnt <= InstCntLateMask) {
1950 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1951 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001952 }
1953
Hal Finkelc58ce412015-01-01 02:53:29 +00001954 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1955 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001956 }
1957};
1958} // anonymous namespace
1959
Justin Bognerdc8af062016-05-20 21:43:23 +00001960bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001961 if (N->getValueType(0) != MVT::i32 &&
1962 N->getValueType(0) != MVT::i64)
Justin Bognerdc8af062016-05-20 21:43:23 +00001963 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00001964
Hal Finkelc58ce412015-01-01 02:53:29 +00001965 if (!UseBitPermRewriter)
Justin Bognerdc8af062016-05-20 21:43:23 +00001966 return false;
Hal Finkelc58ce412015-01-01 02:53:29 +00001967
Hal Finkel8adf2252014-12-16 05:51:41 +00001968 switch (N->getOpcode()) {
1969 default: break;
1970 case ISD::ROTL:
1971 case ISD::SHL:
1972 case ISD::SRL:
1973 case ISD::AND:
1974 case ISD::OR: {
1975 BitPermutationSelector BPS(CurDAG);
Justin Bognerdc8af062016-05-20 21:43:23 +00001976 if (SDNode *New = BPS.Select(N)) {
1977 ReplaceNode(N, New);
1978 return true;
1979 }
1980 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00001981 }
1982 }
1983
Justin Bognerdc8af062016-05-20 21:43:23 +00001984 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00001985}
1986
Chris Lattner2a1823d2005-08-21 18:50:37 +00001987/// SelectCC - Select a comparison of the specified values with the specified
1988/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001989SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001990 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001991 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001992 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001993
Owen Anderson9f944592009-08-11 20:47:22 +00001994 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001995 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001996 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1997 if (isInt32Immediate(RHS, Imm)) {
1998 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001999 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002000 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002001 getI32Imm(Imm & 0xFFFF, dl)),
2002 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002003 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002004 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002005 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002006 getI32Imm(Imm & 0xFFFF, dl)),
2007 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002008
Chris Lattneraa3926b2006-09-20 04:25:47 +00002009 // For non-equality comparisons, the default code would materialize the
2010 // constant, then compare against it, like this:
2011 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002012 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00002013 // cmpw cr0, r3, r2
2014 // Since we are just comparing for equality, we can emit this instead:
2015 // xoris r0,r3,0x1234
2016 // cmplwi cr0,r0,0x5678
2017 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00002018 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002019 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002020 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002021 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002022 }
2023 Opc = PPC::CMPLW;
2024 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002025 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002026 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002027 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002028 Opc = PPC::CMPLW;
2029 } else {
2030 short SImm;
2031 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002032 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002033 getI32Imm((int)SImm & 0xFFFF,
2034 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002035 0);
2036 Opc = PPC::CMPW;
2037 }
Owen Anderson9f944592009-08-11 20:47:22 +00002038 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002039 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002040 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002041 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002042 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002043 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002044 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002045 getI32Imm(Imm & 0xFFFF, dl)),
2046 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002047 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002048 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002049 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002050 getI32Imm(Imm & 0xFFFF, dl)),
2051 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002052
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002053 // For non-equality comparisons, the default code would materialize the
2054 // constant, then compare against it, like this:
2055 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002056 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002057 // cmpd cr0, r3, r2
2058 // Since we are just comparing for equality, we can emit this instead:
2059 // xoris r0,r3,0x1234
2060 // cmpldi cr0,r0,0x5678
2061 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002062 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002063 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002064 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002065 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002066 getI64Imm(Imm & 0xFFFF, dl)),
2067 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002068 }
2069 }
2070 Opc = PPC::CMPLD;
2071 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002072 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002073 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002074 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002075 Opc = PPC::CMPLD;
2076 } else {
2077 short SImm;
2078 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002079 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002080 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002081 0);
2082 Opc = PPC::CMPD;
2083 }
Owen Anderson9f944592009-08-11 20:47:22 +00002084 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002085 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002086 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002087 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002088 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002089 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002090 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002091}
2092
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002093static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002094 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002095 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002096 case ISD::SETONE:
2097 case ISD::SETOLE:
2098 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002099 llvm_unreachable("Should be lowered by legalize!");
2100 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002101 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002102 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002103 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002104 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002105 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002106 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002107 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002108 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002109 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002110 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002111 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002112 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002113 case ISD::SETO: return PPC::PRED_NU;
2114 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002115 // These two are invalid for floating point. Assume we have int.
2116 case ISD::SETULT: return PPC::PRED_LT;
2117 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002118 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002119}
2120
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002121/// getCRIdxForSetCC - Return the index of the condition register field
2122/// associated with the SetCC condition, and whether or not the field is
2123/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002124static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002125 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002126 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002127 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002128 case ISD::SETOLT:
2129 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2130 case ISD::SETOGT:
2131 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2132 case ISD::SETOEQ:
2133 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2134 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002135 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002136 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002137 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002138 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002139 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002140 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2141 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002142 case ISD::SETUEQ:
2143 case ISD::SETOGE:
2144 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002145 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002146 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002147 // These are invalid for floating point. Assume integer.
2148 case ISD::SETULT: return 0;
2149 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002150 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002151}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002152
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002153// getVCmpInst: return the vector compare instruction for the specified
2154// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002155// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002156static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2157 bool HasVSX, bool &Swap, bool &Negate) {
2158 Swap = false;
2159 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002160
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002161 if (VecVT.isFloatingPoint()) {
2162 /* Handle some cases by swapping input operands. */
2163 switch (CC) {
2164 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2165 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2166 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2167 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2168 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2169 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2170 default: break;
2171 }
2172 /* Handle some cases by negating the result. */
2173 switch (CC) {
2174 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2175 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2176 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2177 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2178 default: break;
2179 }
2180 /* We have instructions implementing the remaining cases. */
2181 switch (CC) {
2182 case ISD::SETEQ:
2183 case ISD::SETOEQ:
2184 if (VecVT == MVT::v4f32)
2185 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2186 else if (VecVT == MVT::v2f64)
2187 return PPC::XVCMPEQDP;
2188 break;
2189 case ISD::SETGT:
2190 case ISD::SETOGT:
2191 if (VecVT == MVT::v4f32)
2192 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2193 else if (VecVT == MVT::v2f64)
2194 return PPC::XVCMPGTDP;
2195 break;
2196 case ISD::SETGE:
2197 case ISD::SETOGE:
2198 if (VecVT == MVT::v4f32)
2199 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2200 else if (VecVT == MVT::v2f64)
2201 return PPC::XVCMPGEDP;
2202 break;
2203 default:
2204 break;
2205 }
2206 llvm_unreachable("Invalid floating-point vector compare condition");
2207 } else {
2208 /* Handle some cases by swapping input operands. */
2209 switch (CC) {
2210 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2211 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2212 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2213 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2214 default: break;
2215 }
2216 /* Handle some cases by negating the result. */
2217 switch (CC) {
2218 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2219 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2220 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2221 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2222 default: break;
2223 }
2224 /* We have instructions implementing the remaining cases. */
2225 switch (CC) {
2226 case ISD::SETEQ:
2227 case ISD::SETUEQ:
2228 if (VecVT == MVT::v16i8)
2229 return PPC::VCMPEQUB;
2230 else if (VecVT == MVT::v8i16)
2231 return PPC::VCMPEQUH;
2232 else if (VecVT == MVT::v4i32)
2233 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002234 else if (VecVT == MVT::v2i64)
2235 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002236 break;
2237 case ISD::SETGT:
2238 if (VecVT == MVT::v16i8)
2239 return PPC::VCMPGTSB;
2240 else if (VecVT == MVT::v8i16)
2241 return PPC::VCMPGTSH;
2242 else if (VecVT == MVT::v4i32)
2243 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002244 else if (VecVT == MVT::v2i64)
2245 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002246 break;
2247 case ISD::SETUGT:
2248 if (VecVT == MVT::v16i8)
2249 return PPC::VCMPGTUB;
2250 else if (VecVT == MVT::v8i16)
2251 return PPC::VCMPGTUH;
2252 else if (VecVT == MVT::v4i32)
2253 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002254 else if (VecVT == MVT::v2i64)
2255 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002256 break;
2257 default:
2258 break;
2259 }
2260 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002261 }
2262}
2263
Justin Bognerdc8af062016-05-20 21:43:23 +00002264bool PPCDAGToDAGISel::trySETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002265 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002266 unsigned Imm;
2267 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002268 EVT PtrVT =
2269 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002270 bool isPPC64 = (PtrVT == MVT::i64);
2271
Eric Christopher1b8e7632014-05-22 01:07:24 +00002272 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002273 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002274 // We can codegen setcc op, imm very efficiently compared to a brcond.
2275 // Check for those cases here.
2276 // setcc op, 0
2277 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002278 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002279 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002280 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002281 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002282 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002283 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
2284 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002285 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2286 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002287 }
Chris Lattnere2969492005-10-21 21:17:10 +00002288 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002289 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002290 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002291 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002292 Op, getI32Imm(~0U, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002293 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
2294 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002295 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002296 case ISD::SETLT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002297 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2298 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002299 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2300 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002301 }
Chris Lattnere2969492005-10-21 21:17:10 +00002302 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002303 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002304 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2305 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
2307 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002308 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2309 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002310 }
2311 }
Chris Lattner491b8292005-10-06 19:03:35 +00002312 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002313 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002314 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002315 default: break;
2316 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002317 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002318 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002319 Op, getI32Imm(1, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002320 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2321 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
2322 MVT::i32,
2323 getI32Imm(0, dl)),
2324 0), Op.getValue(1));
2325 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002326 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002327 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002328 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002329 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002330 Op, getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00002331 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op,
2332 SDValue(AD, 1));
2333 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002334 }
Chris Lattnere2969492005-10-21 21:17:10 +00002335 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002336 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002337 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002338 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2339 Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002340 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
2341 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002342 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2343 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002344 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002345 case ISD::SETGT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002346 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2347 getI32Imm(31, dl) };
2348 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002349 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl));
2350 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002351 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002352 }
Chris Lattner491b8292005-10-06 19:03:35 +00002353 }
2354 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002355
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002356 SDValue LHS = N->getOperand(0);
2357 SDValue RHS = N->getOperand(1);
2358
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002359 // Altivec Vector compare instructions do not set any CR register by default and
2360 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002361 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002362 if (PPCSubTarget->hasQPX())
Justin Bognerdc8af062016-05-20 21:43:23 +00002363 return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002364
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002365 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002366 bool Swap, Negate;
2367 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2368 PPCSubTarget->hasVSX(), Swap, Negate);
2369 if (Swap)
2370 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002371
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002372 EVT ResVT = VecVT.changeVectorElementTypeToInteger();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002373 if (Negate) {
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002374 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002375 CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR,
2376 ResVT, VCmp, VCmp);
2377 return true;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002378 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002379
Justin Bognerdc8af062016-05-20 21:43:23 +00002380 CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS);
2381 return true;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002382 }
2383
Eric Christopher1b8e7632014-05-22 01:07:24 +00002384 if (PPCSubTarget->useCRBits())
Justin Bognerdc8af062016-05-20 21:43:23 +00002385 return false;
Hal Finkel940ab932014-02-28 00:27:01 +00002386
Chris Lattner491b8292005-10-06 19:03:35 +00002387 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002388 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002389 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002390 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002391
Chris Lattner491b8292005-10-06 19:03:35 +00002392 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002393 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002394
Craig Topper062a2ba2014-04-25 05:30:21 +00002395 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002396 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002397 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002398
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002399 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2400 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002401
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002402 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
2403 getI32Imm(31, dl), getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002404 if (!Inv) {
2405 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2406 return true;
2407 }
Chris Lattner89f36e62008-01-08 06:46:30 +00002408
2409 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002410 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002411 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002412 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
2413 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002414}
Chris Lattner502a3692005-10-06 18:56:10 +00002415
Justin Bognerdc8af062016-05-20 21:43:23 +00002416void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
Hal Finkelcf599212015-02-25 21:36:59 +00002417 // Transfer memoperands.
2418 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2419 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2420 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
Hal Finkelcf599212015-02-25 21:36:59 +00002421}
2422
Chris Lattner318622f2005-10-06 19:07:45 +00002423
Chris Lattner43ff01e2005-08-17 19:33:03 +00002424// Select - Convert the specified operand from a target-independent to a
2425// target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +00002426void PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002427 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002428 if (N->isMachineOpcode()) {
2429 N->setNodeId(-1);
Justin Bognerdc8af062016-05-20 21:43:23 +00002430 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002431 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002432
Hal Finkel51b3fd12014-09-02 06:23:54 +00002433 // In case any misguided DAG-level optimizations form an ADD with a
2434 // TargetConstant operand, crash here instead of miscompiling (by selecting
2435 // an r+r add instead of some kind of r+i add).
2436 if (N->getOpcode() == ISD::ADD &&
2437 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2438 llvm_unreachable("Invalid ADD with TargetConstant operand");
2439
Hal Finkel8adf2252014-12-16 05:51:41 +00002440 // Try matching complex bit permutations before doing anything else.
Justin Bognerdc8af062016-05-20 21:43:23 +00002441 if (tryBitPermutation(N))
2442 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00002443
Chris Lattner43ff01e2005-08-17 19:33:03 +00002444 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002445 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002446
Jim Laskey095e6f32006-12-12 13:23:43 +00002447 case ISD::Constant: {
Justin Bognerdc8af062016-05-20 21:43:23 +00002448 if (N->getValueType(0) == MVT::i64) {
2449 ReplaceNode(N, getInt64(CurDAG, N));
2450 return;
2451 }
Jim Laskey095e6f32006-12-12 13:23:43 +00002452 break;
2453 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002454
Hal Finkel940ab932014-02-28 00:27:01 +00002455 case ISD::SETCC: {
Justin Bognerdc8af062016-05-20 21:43:23 +00002456 if (trySETCC(N))
2457 return;
Hal Finkel940ab932014-02-28 00:27:01 +00002458 break;
2459 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002460 case PPCISD::GlobalBaseReg:
Justin Bognerdc8af062016-05-20 21:43:23 +00002461 ReplaceNode(N, getGlobalBaseReg());
2462 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00002463
Hal Finkelb5e9b042014-12-11 22:51:06 +00002464 case ISD::FrameIndex:
Justin Bognerdc8af062016-05-20 21:43:23 +00002465 selectFrameIndex(N, N);
2466 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00002467
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002468 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469 SDValue InFlag = N->getOperand(1);
Justin Bognerdc8af062016-05-20 21:43:23 +00002470 ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2471 N->getOperand(0), InFlag));
2472 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00002473 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002474
Hal Finkelbbdee932014-12-02 22:01:00 +00002475 case PPCISD::READ_TIME_BASE: {
Justin Bognerdc8af062016-05-20 21:43:23 +00002476 ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2477 MVT::Other, N->getOperand(0)));
2478 return;
Hal Finkelbbdee932014-12-02 22:01:00 +00002479 }
2480
Hal Finkel13d104b2014-12-11 18:37:52 +00002481 case PPCISD::SRA_ADDZE: {
2482 SDValue N0 = N->getOperand(0);
2483 SDValue ShiftAmt =
2484 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002485 getConstantIntValue(), dl,
2486 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00002487 if (N->getValueType(0) == MVT::i64) {
2488 SDNode *Op =
2489 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2490 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00002491 CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0),
2492 SDValue(Op, 1));
2493 return;
Hal Finkel13d104b2014-12-11 18:37:52 +00002494 } else {
2495 assert(N->getValueType(0) == MVT::i32 &&
2496 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2497 SDNode *Op =
2498 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2499 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00002500 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0),
2501 SDValue(Op, 1));
2502 return;
Chris Lattnerdc664572005-08-25 17:50:06 +00002503 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002504 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002505
Chris Lattnerce645542006-11-10 02:08:47 +00002506 case ISD::LOAD: {
2507 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002508 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002509 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002510
Chris Lattnerce645542006-11-10 02:08:47 +00002511 // Normal loads are handled by code generated from the .td file.
2512 if (LD->getAddressingMode() != ISD::PRE_INC)
2513 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002514
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002516 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002517 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002518
Chris Lattner474b5b72006-11-15 19:55:13 +00002519 unsigned Opcode;
2520 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002521 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002522 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002523 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2524 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002525 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002526 case MVT::f64: Opcode = PPC::LFDU; break;
2527 case MVT::f32: Opcode = PPC::LFSU; break;
2528 case MVT::i32: Opcode = PPC::LWZU; break;
2529 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2530 case MVT::i1:
2531 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002532 }
2533 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002534 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2535 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2536 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002537 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002538 case MVT::i64: Opcode = PPC::LDU; break;
2539 case MVT::i32: Opcode = PPC::LWZU8; break;
2540 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2541 case MVT::i1:
2542 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002543 }
2544 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002545
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002546 SDValue Chain = LD->getChain();
2547 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002548 SDValue Ops[] = { Offset, Base, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00002549 SDNode *MN = CurDAG->getMachineNode(
2550 Opcode, dl, LD->getValueType(0),
2551 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
2552 transferMemOperands(N, MN);
2553 ReplaceNode(N, MN);
2554 return;
Chris Lattnerce645542006-11-10 02:08:47 +00002555 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002556 unsigned Opcode;
2557 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2558 if (LD->getValueType(0) != MVT::i64) {
2559 // Handle PPC32 integer and normal FP loads.
2560 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2561 switch (LoadedVT.getSimpleVT().SimpleTy) {
2562 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00002563 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
2564 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00002565 case MVT::f64: Opcode = PPC::LFDUX; break;
2566 case MVT::f32: Opcode = PPC::LFSUX; break;
2567 case MVT::i32: Opcode = PPC::LWZUX; break;
2568 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2569 case MVT::i1:
2570 case MVT::i8: Opcode = PPC::LBZUX; break;
2571 }
2572 } else {
2573 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2574 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2575 "Invalid sext update load");
2576 switch (LoadedVT.getSimpleVT().SimpleTy) {
2577 default: llvm_unreachable("Invalid PPC load type!");
2578 case MVT::i64: Opcode = PPC::LDUX; break;
2579 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2580 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2581 case MVT::i1:
2582 case MVT::i8: Opcode = PPC::LBZUX8; break;
2583 }
2584 }
2585
2586 SDValue Chain = LD->getChain();
2587 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002588 SDValue Ops[] = { Base, Offset, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00002589 SDNode *MN = CurDAG->getMachineNode(
2590 Opcode, dl, LD->getValueType(0),
2591 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
2592 transferMemOperands(N, MN);
2593 ReplaceNode(N, MN);
2594 return;
Chris Lattnerce645542006-11-10 02:08:47 +00002595 }
2596 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002597
Nate Begemanb3821a32005-08-18 07:30:46 +00002598 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002599 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002600 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002601
Nate Begemanb3821a32005-08-18 07:30:46 +00002602 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2603 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002604 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002605 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002606 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002607 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
2608 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002609 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2610 return;
Nate Begemanb3821a32005-08-18 07:30:46 +00002611 }
Nate Begemand31efd12006-09-22 05:01:56 +00002612 // If this is just a masked value where the input is not handled above, and
2613 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2614 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002615 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002616 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002617 SDValue Val = N->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002618 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
2619 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002620 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2621 return;
Nate Begemand31efd12006-09-22 05:01:56 +00002622 }
Hal Finkele39526a2012-08-28 02:10:15 +00002623 // If this is a 64-bit zero-extension mask, emit rldicl.
2624 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2625 isMask_64(Imm64)) {
2626 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002627 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002628 SH = 0;
2629
2630 // If the operand is a logical right shift, we can fold it into this
2631 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2632 // for n <= mb. The right shift is really a left rotate followed by a
2633 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2634 // by the shift.
2635 if (Val.getOpcode() == ISD::SRL &&
2636 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2637 assert(Imm < 64 && "Illegal shift amount");
2638 Val = Val.getOperand(0);
2639 SH = 64 - Imm;
2640 }
2641
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002642 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002643 CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
2644 return;
Hal Finkele39526a2012-08-28 02:10:15 +00002645 }
Nate Begemand31efd12006-09-22 05:01:56 +00002646 // AND X, 0 -> 0, not "rlwinm 32".
2647 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002648 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Justin Bognerdc8af062016-05-20 21:43:23 +00002649 return;
Nate Begemand31efd12006-09-22 05:01:56 +00002650 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002651 // ISD::OR doesn't get all the bitfield insertion fun.
Hal Finkelb1518d62015-09-05 00:02:59 +00002652 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
2653 // bitfield insert.
Andrew Trickc416ba62010-12-24 04:28:06 +00002654 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002655 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002656 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Hal Finkelb1518d62015-09-05 00:02:59 +00002657 // The idea here is to check whether this is equivalent to:
2658 // (c1 & m) | (x & ~m)
2659 // where m is a run-of-ones mask. The logic here is that, for each bit in
2660 // c1 and c2:
2661 // - if both are 1, then the output will be 1.
2662 // - if both are 0, then the output will be 0.
2663 // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
2664 // come from x.
2665 // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
2666 // be 0.
2667 // If that last condition is never the case, then we can form m from the
2668 // bits that are the same between c1 and c2.
Chris Lattner20c88df2006-01-05 18:32:49 +00002669 unsigned MB, ME;
Hal Finkelb1518d62015-09-05 00:02:59 +00002670 if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002671 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002672 N->getOperand(0).getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002673 getI32Imm(0, dl), getI32Imm(MB, dl),
2674 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002675 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
2676 return;
Nate Begeman9aea6e42005-12-24 01:00:15 +00002677 }
2678 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002679
Chris Lattner1de57062005-09-29 23:33:31 +00002680 // Other cases are autogenerated.
2681 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002682 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002683 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002684 if (N->getValueType(0) == MVT::i32)
Justin Bognerdc8af062016-05-20 21:43:23 +00002685 if (tryBitfieldInsert(N))
2686 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00002687
Hal Finkelb5e9b042014-12-11 22:51:06 +00002688 short Imm;
2689 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2690 isIntS16Immediate(N->getOperand(1), Imm)) {
2691 APInt LHSKnownZero, LHSKnownOne;
2692 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2693
2694 // If this is equivalent to an add, then we can fold it with the
2695 // FrameIndex calculation.
Justin Bognerdc8af062016-05-20 21:43:23 +00002696 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
2697 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2698 return;
2699 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002700 }
2701
Chris Lattner1de57062005-09-29 23:33:31 +00002702 // Other cases are autogenerated.
2703 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002704 }
2705 case ISD::ADD: {
2706 short Imm;
2707 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
Justin Bognerdc8af062016-05-20 21:43:23 +00002708 isIntS16Immediate(N->getOperand(1), Imm)) {
2709 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2710 return;
2711 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002712
2713 break;
2714 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002715 case ISD::SHL: {
2716 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002717 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002718 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002719 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002720 getI32Imm(SH, dl), getI32Imm(MB, dl),
2721 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002722 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2723 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002724 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002725
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002726 // Other cases are autogenerated.
2727 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002728 }
2729 case ISD::SRL: {
2730 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002731 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002732 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002733 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002734 getI32Imm(SH, dl), getI32Imm(MB, dl),
2735 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002736 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2737 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002738 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002739
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002740 // Other cases are autogenerated.
2741 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002742 }
Hal Finkel940ab932014-02-28 00:27:01 +00002743 // FIXME: Remove this once the ANDI glue bug is fixed:
2744 case PPCISD::ANDIo_1_EQ_BIT:
2745 case PPCISD::ANDIo_1_GT_BIT: {
2746 if (!ANDIGlueBug)
2747 break;
2748
2749 EVT InVT = N->getOperand(0).getValueType();
2750 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2751 "Invalid input type for ANDIo_1_EQ_BIT");
2752
2753 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2754 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2755 N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002756 CurDAG->getTargetConstant(1, dl, InVT)),
2757 0);
Hal Finkel940ab932014-02-28 00:27:01 +00002758 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2759 SDValue SRIdxVal =
2760 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002761 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00002762
Justin Bognerdc8af062016-05-20 21:43:23 +00002763 CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg,
2764 SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */);
2765 return;
Hal Finkel940ab932014-02-28 00:27:01 +00002766 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002767 case ISD::SELECT_CC: {
2768 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002769 EVT PtrVT =
2770 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002771 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002772
Hal Finkel940ab932014-02-28 00:27:01 +00002773 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002774 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002775 N->getOperand(0).getValueType() == MVT::i1)
2776 break;
2777
Chris Lattner97b3da12006-06-27 00:04:13 +00002778 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002779 if (!isPPC64)
2780 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2781 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2782 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2783 if (N1C->isNullValue() && N3C->isNullValue() &&
2784 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2785 // FIXME: Implement this optzn for PPC64.
2786 N->getValueType(0) == MVT::i32) {
2787 SDNode *Tmp =
2788 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002789 N->getOperand(0), getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00002790 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0),
2791 N->getOperand(0), SDValue(Tmp, 1));
2792 return;
Roman Divacky254f8212011-06-20 15:28:39 +00002793 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002794
Dale Johannesenab8e4422009-02-06 19:16:40 +00002795 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002796
2797 if (N->getValueType(0) == MVT::i1) {
2798 // An i1 select is: (c & t) | (!c & f).
2799 bool Inv;
2800 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2801
2802 unsigned SRI;
2803 switch (Idx) {
2804 default: llvm_unreachable("Invalid CC index");
2805 case 0: SRI = PPC::sub_lt; break;
2806 case 1: SRI = PPC::sub_gt; break;
2807 case 2: SRI = PPC::sub_eq; break;
2808 case 3: SRI = PPC::sub_un; break;
2809 }
2810
2811 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2812
2813 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2814 CCBit, CCBit), 0);
2815 SDValue C = Inv ? NotCCBit : CCBit,
2816 NotC = Inv ? CCBit : NotCCBit;
2817
2818 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2819 C, N->getOperand(2)), 0);
2820 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2821 NotC, N->getOperand(3)), 0);
2822
Justin Bognerdc8af062016-05-20 21:43:23 +00002823 CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2824 return;
Hal Finkel940ab932014-02-28 00:27:01 +00002825 }
2826
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002827 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002828
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002829 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002830 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002831 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002832 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002833 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002834 else if (N->getValueType(0) == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002835 if (PPCSubTarget->hasP8Vector())
2836 SelectCCOp = PPC::SELECT_CC_VSSRC;
2837 else
2838 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002839 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002840 if (PPCSubTarget->hasVSX())
2841 SelectCCOp = PPC::SELECT_CC_VSFRC;
2842 else
2843 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002844 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
2845 SelectCCOp = PPC::SELECT_CC_QFRC;
2846 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
2847 SelectCCOp = PPC::SELECT_CC_QSRC;
2848 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
2849 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00002850 else if (N->getValueType(0) == MVT::v2f64 ||
2851 N->getValueType(0) == MVT::v2i64)
2852 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002853 else
2854 SelectCCOp = PPC::SELECT_CC_VRRC;
2855
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002856 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002857 getI32Imm(BROpc, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002858 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
2859 return;
Chris Lattnerbec817c2005-08-26 18:46:49 +00002860 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002861 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002862 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002863 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002864 CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
2865 return;
Hal Finkel732f0f72014-03-26 12:49:28 +00002866 }
2867
2868 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002869 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002870 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002871 N->getValueType(0) == MVT::v2i64)) {
2872 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Kyle Butt015f4fc2015-12-02 18:53:33 +00002873
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002874 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2875 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2876 unsigned DM[2];
2877
2878 for (int i = 0; i < 2; ++i)
2879 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2880 DM[i] = 0;
2881 else
2882 DM[i] = 1;
2883
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002884 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2885 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2886 isa<LoadSDNode>(Op1.getOperand(0))) {
2887 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2888 SDValue Base, Offset;
2889
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +00002890 if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() &&
Bill Schmidt048cc972015-10-14 20:45:00 +00002891 (LD->getMemoryVT() == MVT::f64 ||
2892 LD->getMemoryVT() == MVT::i64) &&
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002893 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2894 SDValue Chain = LD->getChain();
2895 SDValue Ops[] = { Base, Offset, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00002896 CurDAG->SelectNodeTo(N, PPC::LXVDSX, N->getValueType(0), Ops);
2897 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002898 }
2899 }
2900
Bill Schmidtae94f112015-07-01 19:40:07 +00002901 // For little endian, we must swap the input operands and adjust
2902 // the mask elements (reverse and invert them).
2903 if (PPCSubTarget->isLittleEndian()) {
2904 std::swap(Op1, Op2);
2905 unsigned tmp = DM[0];
2906 DM[0] = 1 - DM[1];
2907 DM[1] = 1 - tmp;
2908 }
2909
2910 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
2911 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002912 SDValue Ops[] = { Op1, Op2, DMV };
Justin Bognerdc8af062016-05-20 21:43:23 +00002913 CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
2914 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002915 }
2916
2917 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002918 case PPCISD::BDNZ:
2919 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002920 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002921 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002922 CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ
2923 ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
2924 : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
2925 MVT::Other, Ops);
2926 return;
Hal Finkel25c19922013-05-15 21:37:41 +00002927 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002928 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002929 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002930 // Op #1 is the PPC::PRED_* number.
2931 // Op #2 is the CR#
2932 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002933 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002934 // Prevent PPC::PRED_* from being selected into LI.
Hal Finkel65539e32015-12-12 00:32:00 +00002935 unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2936 if (EnableBranchHint)
2937 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3));
2938
2939 SDValue Pred = getI32Imm(PCC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002940 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002941 N->getOperand(0), N->getOperand(4) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002942 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
2943 return;
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002944 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002945 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002946 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002947 unsigned PCC = getPredicateForSetCC(CC);
2948
2949 if (N->getOperand(2).getValueType() == MVT::i1) {
2950 unsigned Opc;
2951 bool Swap;
2952 switch (PCC) {
2953 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2954 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2955 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2956 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2957 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2958 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2959 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2960 }
2961
2962 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2963 N->getOperand(Swap ? 3 : 2),
2964 N->getOperand(Swap ? 2 : 3)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002965 CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4),
2966 N->getOperand(0));
2967 return;
Hal Finkel940ab932014-02-28 00:27:01 +00002968 }
2969
Hal Finkel65539e32015-12-12 00:32:00 +00002970 if (EnableBranchHint)
2971 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4));
2972
Dale Johannesenab8e4422009-02-06 19:16:40 +00002973 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002974 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002975 N->getOperand(4), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002976 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
2977 return;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002978 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002979 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002980 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002981 SDValue Chain = N->getOperand(0);
2982 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002983 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002984 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002985 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002986 Chain), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002987 CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
2988 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002989 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002990 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002991 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2992 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002993 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2994 SDValue GA = N->getOperand(0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002995 SDNode *MN = CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
2996 N->getOperand(1));
2997 transferMemOperands(N, MN);
2998 ReplaceNode(N, MN);
2999 return;
Justin Hibbits3476db42014-08-28 04:40:55 +00003000 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003001
Bill Schmidt27917782013-02-21 17:12:27 +00003002 // For medium and large code model, we generate two instructions as
3003 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003004 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00003005 CodeModel::Model CModel = TM.getCodeModel();
3006 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00003007 break;
3008
Bill Schmidt5d82f092014-06-16 21:36:02 +00003009 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
Eric Christopherc1808362015-11-20 20:51:31 +00003010 // If it must be toc-referenced according to PPCSubTarget, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00003011 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
3012 // Otherwise we generate:
3013 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
3014 SDValue GA = N->getOperand(0);
3015 SDValue TOCbase = N->getOperand(1);
3016 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00003017 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00003018
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003019 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
Justin Bognerdc8af062016-05-20 21:43:23 +00003020 CModel == CodeModel::Large) {
3021 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3022 SDValue(Tmp, 0));
3023 transferMemOperands(N, MN);
3024 ReplaceNode(N, MN);
3025 return;
3026 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003027
3028 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
Eric Christopherc1808362015-11-20 20:51:31 +00003029 const GlobalValue *GV = G->getGlobal();
3030 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
3031 if (GVFlags & PPCII::MO_NLP_FLAG) {
Justin Bognerdc8af062016-05-20 21:43:23 +00003032 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3033 SDValue(Tmp, 0));
3034 transferMemOperands(N, MN);
3035 ReplaceNode(N, MN);
3036 return;
Eric Christopherc1808362015-11-20 20:51:31 +00003037 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003038 }
3039
Justin Bognerdc8af062016-05-20 21:43:23 +00003040 ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
3041 SDValue(Tmp, 0), GA));
3042 return;
Bill Schmidt34627e32012-11-27 17:35:46 +00003043 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00003044 case PPCISD::PPC32_PICGOT: {
3045 // Generate a PIC-safe GOT reference.
3046 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
3047 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
Justin Bognerdc8af062016-05-20 21:43:23 +00003048 CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT,
3049 PPCLowering->getPointerTy(CurDAG->getDataLayout()),
3050 MVT::i32);
3051 return;
Hal Finkel7c8ae532014-07-25 17:47:22 +00003052 }
Bill Schmidt51e79512013-02-20 15:50:31 +00003053 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003054 // This expands into one of three sequences, depending on whether
3055 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00003056 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
3057 isa<ConstantSDNode>(N->getOperand(1)) &&
3058 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003059
3060 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00003061 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003062 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00003063 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003064
Bill Schmidt51e79512013-02-20 15:50:31 +00003065 if (EltSize == 1) {
3066 Opc1 = PPC::VSPLTISB;
3067 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003068 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003069 VT = MVT::v16i8;
3070 } else if (EltSize == 2) {
3071 Opc1 = PPC::VSPLTISH;
3072 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003073 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003074 VT = MVT::v8i16;
3075 } else {
3076 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
3077 Opc1 = PPC::VSPLTISW;
3078 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003079 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003080 VT = MVT::v4i32;
3081 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003082
3083 if ((Elt & 1) == 0) {
3084 // Elt is even, in the range [-32,-18] + [16,30].
3085 //
3086 // Convert: VADD_SPLAT elt, size
3087 // Into: tmp = VSPLTIS[BHW] elt
3088 // VADDU[BHW]M tmp, tmp
3089 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003090 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003091 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3092 SDValue TmpVal = SDValue(Tmp, 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003093 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal));
3094 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003095
3096 } else if (Elt > 0) {
3097 // Elt is odd and positive, in the range [17,31].
3098 //
3099 // Convert: VADD_SPLAT elt, size
3100 // Into: tmp1 = VSPLTIS[BHW] elt-16
3101 // tmp2 = VSPLTIS[BHW] -16
3102 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003103 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003104 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003105 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003106 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003107 ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
3108 SDValue(Tmp2, 0)));
3109 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003110
3111 } else {
3112 // Elt is odd and negative, in the range [-31,-17].
3113 //
3114 // Convert: VADD_SPLAT elt, size
3115 // Into: tmp1 = VSPLTIS[BHW] elt+16
3116 // tmp2 = VSPLTIS[BHW] -16
3117 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003118 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003119 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003120 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003121 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003122 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
3123 SDValue(Tmp2, 0)));
3124 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003125 }
Bill Schmidt51e79512013-02-20 15:50:31 +00003126 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00003127 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003128
Justin Bognerdc8af062016-05-20 21:43:23 +00003129 SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00003130}
3131
Hal Finkel4edc66b2015-01-03 01:16:37 +00003132// If the target supports the cmpb instruction, do the idiom recognition here.
3133// We don't do this as a DAG combine because we don't want to do it as nodes
3134// are being combined (because we might miss part of the eventual idiom). We
3135// don't want to do it during instruction selection because we want to reuse
3136// the logic for lowering the masking operations already part of the
3137// instruction selector.
3138SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3139 SDLoc dl(N);
3140
3141 assert(N->getOpcode() == ISD::OR &&
3142 "Only OR nodes are supported for CMPB");
3143
3144 SDValue Res;
3145 if (!PPCSubTarget->hasCMPB())
3146 return Res;
3147
3148 if (N->getValueType(0) != MVT::i32 &&
3149 N->getValueType(0) != MVT::i64)
3150 return Res;
3151
3152 EVT VT = N->getValueType(0);
3153
3154 SDValue RHS, LHS;
3155 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
3156 uint64_t Mask = 0, Alt = 0;
3157
3158 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3159 uint64_t &Mask, uint64_t &Alt,
3160 SDValue &LHS, SDValue &RHS) {
3161 if (O.getOpcode() != ISD::SELECT_CC)
3162 return false;
3163 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3164
3165 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3166 !isa<ConstantSDNode>(O.getOperand(3)))
3167 return false;
3168
3169 uint64_t PM = O.getConstantOperandVal(2);
3170 uint64_t PAlt = O.getConstantOperandVal(3);
3171 for (b = 0; b < 8; ++b) {
3172 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3173 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3174 break;
3175 }
3176
3177 if (b == 8)
3178 return false;
3179 Mask |= PM;
3180 Alt |= PAlt;
3181
3182 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3183 O.getConstantOperandVal(1) != 0) {
3184 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3185 if (Op0.getOpcode() == ISD::TRUNCATE)
3186 Op0 = Op0.getOperand(0);
3187 if (Op1.getOpcode() == ISD::TRUNCATE)
3188 Op1 = Op1.getOperand(0);
3189
3190 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3191 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3192 isa<ConstantSDNode>(Op0.getOperand(1))) {
3193
3194 unsigned Bits = Op0.getValueType().getSizeInBits();
3195 if (b != Bits/8-1)
3196 return false;
3197 if (Op0.getConstantOperandVal(1) != Bits-8)
3198 return false;
3199
3200 LHS = Op0.getOperand(0);
3201 RHS = Op1.getOperand(0);
3202 return true;
3203 }
3204
3205 // When we have small integers (i16 to be specific), the form present
3206 // post-legalization uses SETULT in the SELECT_CC for the
3207 // higher-order byte, depending on the fact that the
3208 // even-higher-order bytes are known to all be zero, for example:
3209 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3210 // (so when the second byte is the same, because all higher-order
3211 // bits from bytes 3 and 4 are known to be zero, the result of the
3212 // xor can be at most 255)
3213 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3214 isa<ConstantSDNode>(O.getOperand(1))) {
3215
3216 uint64_t ULim = O.getConstantOperandVal(1);
3217 if (ULim != (UINT64_C(1) << b*8))
3218 return false;
3219
3220 // Now we need to make sure that the upper bytes are known to be
3221 // zero.
3222 unsigned Bits = Op0.getValueType().getSizeInBits();
3223 if (!CurDAG->MaskedValueIsZero(Op0,
3224 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
3225 return false;
Kyle Butt015f4fc2015-12-02 18:53:33 +00003226
Hal Finkel4edc66b2015-01-03 01:16:37 +00003227 LHS = Op0.getOperand(0);
3228 RHS = Op0.getOperand(1);
3229 return true;
3230 }
3231
3232 return false;
3233 }
3234
3235 if (CC != ISD::SETEQ)
3236 return false;
3237
3238 SDValue Op = O.getOperand(0);
3239 if (Op.getOpcode() == ISD::AND) {
3240 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3241 return false;
3242 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3243 return false;
3244
3245 SDValue XOR = Op.getOperand(0);
3246 if (XOR.getOpcode() == ISD::TRUNCATE)
3247 XOR = XOR.getOperand(0);
3248 if (XOR.getOpcode() != ISD::XOR)
3249 return false;
3250
3251 LHS = XOR.getOperand(0);
3252 RHS = XOR.getOperand(1);
3253 return true;
3254 } else if (Op.getOpcode() == ISD::SRL) {
3255 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3256 return false;
3257 unsigned Bits = Op.getValueType().getSizeInBits();
3258 if (b != Bits/8-1)
3259 return false;
3260 if (Op.getConstantOperandVal(1) != Bits-8)
3261 return false;
3262
3263 SDValue XOR = Op.getOperand(0);
3264 if (XOR.getOpcode() == ISD::TRUNCATE)
3265 XOR = XOR.getOperand(0);
3266 if (XOR.getOpcode() != ISD::XOR)
3267 return false;
3268
3269 LHS = XOR.getOperand(0);
3270 RHS = XOR.getOperand(1);
3271 return true;
3272 }
3273
3274 return false;
3275 };
3276
3277 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3278 while (!Queue.empty()) {
3279 SDValue V = Queue.pop_back_val();
3280
3281 for (const SDValue &O : V.getNode()->ops()) {
3282 unsigned b;
3283 uint64_t M = 0, A = 0;
3284 SDValue OLHS, ORHS;
3285 if (O.getOpcode() == ISD::OR) {
3286 Queue.push_back(O);
3287 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3288 if (!LHS) {
3289 LHS = OLHS;
3290 RHS = ORHS;
3291 BytesFound[b] = true;
3292 Mask |= M;
3293 Alt |= A;
3294 } else if ((LHS == ORHS && RHS == OLHS) ||
3295 (RHS == ORHS && LHS == OLHS)) {
3296 BytesFound[b] = true;
3297 Mask |= M;
3298 Alt |= A;
3299 } else {
3300 return Res;
3301 }
3302 } else {
3303 return Res;
3304 }
3305 }
3306 }
3307
3308 unsigned LastB = 0, BCnt = 0;
3309 for (unsigned i = 0; i < 8; ++i)
3310 if (BytesFound[LastB]) {
3311 ++BCnt;
3312 LastB = i;
3313 }
3314
3315 if (!LastB || BCnt < 2)
3316 return Res;
3317
3318 // Because we'll be zero-extending the output anyway if don't have a specific
3319 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3320 if (LHS.getValueType() != VT) {
3321 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3322 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3323 }
3324
3325 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3326
3327 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3328 if (NonTrivialMask && !Alt) {
3329 // Res = Mask & CMPB
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003330 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3331 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003332 } else if (Alt) {
3333 // Res = (CMPB & Mask) | (~CMPB & Alt)
3334 // Which, as suggested here:
3335 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3336 // can be written as:
3337 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3338 // useful because the (Alt ^ Mask) can be pre-computed.
3339 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003340 CurDAG->getConstant(Mask ^ Alt, dl, VT));
3341 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
3342 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003343 }
3344
3345 return Res;
3346}
3347
Hal Finkel200d2ad2015-01-05 21:10:24 +00003348// When CR bit registers are enabled, an extension of an i1 variable to a i32
3349// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3350// involves constant materialization of a 0 or a 1 or both. If the result of
3351// the extension is then operated upon by some operator that can be constant
3352// folded with a constant 0 or 1, and that constant can be materialized using
3353// only one instruction (like a zero or one), then we should fold in those
3354// operations with the select.
3355void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3356 if (!PPCSubTarget->useCRBits())
3357 return;
3358
3359 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3360 N->getOpcode() != ISD::SIGN_EXTEND &&
3361 N->getOpcode() != ISD::ANY_EXTEND)
3362 return;
3363
3364 if (N->getOperand(0).getValueType() != MVT::i1)
3365 return;
3366
3367 if (!N->hasOneUse())
3368 return;
3369
3370 SDLoc dl(N);
3371 EVT VT = N->getValueType(0);
3372 SDValue Cond = N->getOperand(0);
3373 SDValue ConstTrue =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003374 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
3375 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00003376
3377 do {
3378 SDNode *User = *N->use_begin();
3379 if (User->getNumOperands() != 2)
3380 break;
3381
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003382 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00003383 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3384 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3385 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3386
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003387 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00003388 User->getValueType(0),
3389 O0.getNode(), O1.getNode());
3390 };
3391
3392 SDValue TrueRes = TryFold(ConstTrue);
3393 if (!TrueRes)
3394 break;
3395 SDValue FalseRes = TryFold(ConstFalse);
3396 if (!FalseRes)
3397 break;
3398
3399 // For us to materialize these using one instruction, we must be able to
3400 // represent them as signed 16-bit integers.
3401 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
3402 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
3403 if (!isInt<16>(True) || !isInt<16>(False))
3404 break;
3405
3406 // We can replace User with a new SELECT node, and try again to see if we
3407 // can fold the select with its user.
3408 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
3409 N = User;
3410 ConstTrue = TrueRes;
3411 ConstFalse = FalseRes;
3412 } while (N->hasOneUse());
3413}
3414
Hal Finkel4edc66b2015-01-03 01:16:37 +00003415void PPCDAGToDAGISel::PreprocessISelDAG() {
3416 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3417 ++Position;
3418
3419 bool MadeChange = false;
3420 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003421 SDNode *N = &*--Position;
Hal Finkel4edc66b2015-01-03 01:16:37 +00003422 if (N->use_empty())
3423 continue;
3424
3425 SDValue Res;
3426 switch (N->getOpcode()) {
3427 default: break;
3428 case ISD::OR:
3429 Res = combineToCMPB(N);
3430 break;
3431 }
3432
Hal Finkel200d2ad2015-01-05 21:10:24 +00003433 if (!Res)
3434 foldBoolExts(Res, N);
3435
Hal Finkel4edc66b2015-01-03 01:16:37 +00003436 if (Res) {
3437 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3438 DEBUG(N->dump(CurDAG));
3439 DEBUG(dbgs() << "\nNew: ");
3440 DEBUG(Res.getNode()->dump(CurDAG));
3441 DEBUG(dbgs() << "\n");
3442
3443 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3444 MadeChange = true;
3445 }
3446 }
3447
3448 if (MadeChange)
3449 CurDAG->RemoveDeadNodes();
3450}
3451
Hal Finkel860fa902014-01-02 22:09:39 +00003452/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003453/// on the DAG representation.
3454void PPCDAGToDAGISel::PostprocessISelDAG() {
3455
3456 // Skip peepholes at -O0.
3457 if (TM.getOptLevel() == CodeGenOpt::None)
3458 return;
3459
Hal Finkel940ab932014-02-28 00:27:01 +00003460 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003461 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003462 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003463}
3464
Hal Finkelb9989152014-02-28 06:11:16 +00003465// Check if all users of this node will become isel where the second operand
3466// is the constant zero. If this is so, and if we can negate the condition,
3467// then we can flip the true and false operands. This will allow the zero to
3468// be folded with the isel so that we don't need to materialize a register
3469// containing zero.
3470bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3471 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003472 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003473 return false;
3474
3475 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3476 UI != UE; ++UI) {
3477 SDNode *User = *UI;
3478 if (!User->isMachineOpcode())
3479 return false;
3480 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3481 User->getMachineOpcode() != PPC::SELECT_I8)
3482 return false;
3483
3484 SDNode *Op2 = User->getOperand(2).getNode();
3485 if (!Op2->isMachineOpcode())
3486 return false;
3487
3488 if (Op2->getMachineOpcode() != PPC::LI &&
3489 Op2->getMachineOpcode() != PPC::LI8)
3490 return false;
3491
3492 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3493 if (!C)
3494 return false;
3495
3496 if (!C->isNullValue())
3497 return false;
3498 }
3499
3500 return true;
3501}
3502
3503void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3504 SmallVector<SDNode *, 4> ToReplace;
3505 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3506 UI != UE; ++UI) {
3507 SDNode *User = *UI;
3508 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3509 User->getMachineOpcode() == PPC::SELECT_I8) &&
3510 "Must have all select users");
3511 ToReplace.push_back(User);
3512 }
3513
3514 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3515 UE = ToReplace.end(); UI != UE; ++UI) {
3516 SDNode *User = *UI;
3517 SDNode *ResNode =
3518 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3519 User->getValueType(0), User->getOperand(0),
3520 User->getOperand(2),
3521 User->getOperand(1));
3522
3523 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3524 DEBUG(User->dump(CurDAG));
3525 DEBUG(dbgs() << "\nNew: ");
3526 DEBUG(ResNode->dump(CurDAG));
3527 DEBUG(dbgs() << "\n");
3528
3529 ReplaceUses(User, ResNode);
3530 }
3531}
3532
Eric Christopher02e18042014-05-14 00:31:15 +00003533void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003534 bool IsModified;
3535 do {
3536 IsModified = false;
Pete Cooper65c69402015-07-14 22:10:54 +00003537 for (SDNode &Node : CurDAG->allnodes()) {
3538 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node);
Hal Finkel940ab932014-02-28 00:27:01 +00003539 if (!MachineNode || MachineNode->use_empty())
3540 continue;
3541 SDNode *ResNode = MachineNode;
3542
3543 bool Op1Set = false, Op1Unset = false,
3544 Op1Not = false,
3545 Op2Set = false, Op2Unset = false,
3546 Op2Not = false;
3547
3548 unsigned Opcode = MachineNode->getMachineOpcode();
3549 switch (Opcode) {
3550 default: break;
3551 case PPC::CRAND:
3552 case PPC::CRNAND:
3553 case PPC::CROR:
3554 case PPC::CRXOR:
3555 case PPC::CRNOR:
3556 case PPC::CREQV:
3557 case PPC::CRANDC:
3558 case PPC::CRORC: {
3559 SDValue Op = MachineNode->getOperand(1);
3560 if (Op.isMachineOpcode()) {
3561 if (Op.getMachineOpcode() == PPC::CRSET)
3562 Op2Set = true;
3563 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3564 Op2Unset = true;
3565 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3566 Op.getOperand(0) == Op.getOperand(1))
3567 Op2Not = true;
3568 }
3569 } // fallthrough
3570 case PPC::BC:
3571 case PPC::BCn:
3572 case PPC::SELECT_I4:
3573 case PPC::SELECT_I8:
3574 case PPC::SELECT_F4:
3575 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003576 case PPC::SELECT_QFRC:
3577 case PPC::SELECT_QSRC:
3578 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003579 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003580 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003581 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003582 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003583 SDValue Op = MachineNode->getOperand(0);
3584 if (Op.isMachineOpcode()) {
3585 if (Op.getMachineOpcode() == PPC::CRSET)
3586 Op1Set = true;
3587 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3588 Op1Unset = true;
3589 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3590 Op.getOperand(0) == Op.getOperand(1))
3591 Op1Not = true;
3592 }
3593 }
3594 break;
3595 }
3596
Hal Finkelb9989152014-02-28 06:11:16 +00003597 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003598 switch (Opcode) {
3599 default: break;
3600 case PPC::CRAND:
3601 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3602 // x & x = x
3603 ResNode = MachineNode->getOperand(0).getNode();
3604 else if (Op1Set)
3605 // 1 & y = y
3606 ResNode = MachineNode->getOperand(1).getNode();
3607 else if (Op2Set)
3608 // x & 1 = x
3609 ResNode = MachineNode->getOperand(0).getNode();
3610 else if (Op1Unset || Op2Unset)
3611 // x & 0 = 0 & y = 0
3612 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3613 MVT::i1);
3614 else if (Op1Not)
3615 // ~x & y = andc(y, x)
3616 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3617 MVT::i1, MachineNode->getOperand(1),
3618 MachineNode->getOperand(0).
3619 getOperand(0));
3620 else if (Op2Not)
3621 // x & ~y = andc(x, y)
3622 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3623 MVT::i1, MachineNode->getOperand(0),
3624 MachineNode->getOperand(1).
3625 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003626 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003627 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3628 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003629 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003630 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003631 }
Hal Finkel940ab932014-02-28 00:27:01 +00003632 break;
3633 case PPC::CRNAND:
3634 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3635 // nand(x, x) -> nor(x, x)
3636 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3637 MVT::i1, MachineNode->getOperand(0),
3638 MachineNode->getOperand(0));
3639 else if (Op1Set)
3640 // nand(1, y) -> nor(y, y)
3641 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3642 MVT::i1, MachineNode->getOperand(1),
3643 MachineNode->getOperand(1));
3644 else if (Op2Set)
3645 // nand(x, 1) -> nor(x, x)
3646 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3647 MVT::i1, MachineNode->getOperand(0),
3648 MachineNode->getOperand(0));
3649 else if (Op1Unset || Op2Unset)
3650 // nand(x, 0) = nand(0, y) = 1
3651 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3652 MVT::i1);
3653 else if (Op1Not)
3654 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3655 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3656 MVT::i1, MachineNode->getOperand(0).
3657 getOperand(0),
3658 MachineNode->getOperand(1));
3659 else if (Op2Not)
3660 // nand(x, ~y) = ~x | y = orc(y, x)
3661 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3662 MVT::i1, MachineNode->getOperand(1).
3663 getOperand(0),
3664 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003665 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003666 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3667 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003668 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003669 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003670 }
Hal Finkel940ab932014-02-28 00:27:01 +00003671 break;
3672 case PPC::CROR:
3673 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3674 // x | x = x
3675 ResNode = MachineNode->getOperand(0).getNode();
3676 else if (Op1Set || Op2Set)
3677 // x | 1 = 1 | y = 1
3678 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3679 MVT::i1);
3680 else if (Op1Unset)
3681 // 0 | y = y
3682 ResNode = MachineNode->getOperand(1).getNode();
3683 else if (Op2Unset)
3684 // x | 0 = x
3685 ResNode = MachineNode->getOperand(0).getNode();
3686 else if (Op1Not)
3687 // ~x | y = orc(y, x)
3688 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3689 MVT::i1, MachineNode->getOperand(1),
3690 MachineNode->getOperand(0).
3691 getOperand(0));
3692 else if (Op2Not)
3693 // x | ~y = orc(x, y)
3694 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3695 MVT::i1, MachineNode->getOperand(0),
3696 MachineNode->getOperand(1).
3697 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003698 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003699 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3700 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003701 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003702 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003703 }
Hal Finkel940ab932014-02-28 00:27:01 +00003704 break;
3705 case PPC::CRXOR:
3706 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3707 // xor(x, x) = 0
3708 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3709 MVT::i1);
3710 else if (Op1Set)
3711 // xor(1, y) -> nor(y, y)
3712 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3713 MVT::i1, MachineNode->getOperand(1),
3714 MachineNode->getOperand(1));
3715 else if (Op2Set)
3716 // xor(x, 1) -> nor(x, x)
3717 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3718 MVT::i1, MachineNode->getOperand(0),
3719 MachineNode->getOperand(0));
3720 else if (Op1Unset)
3721 // xor(0, y) = y
3722 ResNode = MachineNode->getOperand(1).getNode();
3723 else if (Op2Unset)
3724 // xor(x, 0) = x
3725 ResNode = MachineNode->getOperand(0).getNode();
3726 else if (Op1Not)
3727 // xor(~x, y) = eqv(x, y)
3728 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3729 MVT::i1, MachineNode->getOperand(0).
3730 getOperand(0),
3731 MachineNode->getOperand(1));
3732 else if (Op2Not)
3733 // xor(x, ~y) = eqv(x, y)
3734 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3735 MVT::i1, MachineNode->getOperand(0),
3736 MachineNode->getOperand(1).
3737 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003738 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003739 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3740 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003741 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003742 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003743 }
Hal Finkel940ab932014-02-28 00:27:01 +00003744 break;
3745 case PPC::CRNOR:
3746 if (Op1Set || Op2Set)
3747 // nor(1, y) -> 0
3748 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3749 MVT::i1);
3750 else if (Op1Unset)
3751 // nor(0, y) = ~y -> nor(y, y)
3752 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3753 MVT::i1, MachineNode->getOperand(1),
3754 MachineNode->getOperand(1));
3755 else if (Op2Unset)
3756 // nor(x, 0) = ~x
3757 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3758 MVT::i1, MachineNode->getOperand(0),
3759 MachineNode->getOperand(0));
3760 else if (Op1Not)
3761 // nor(~x, y) = andc(x, y)
3762 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3763 MVT::i1, MachineNode->getOperand(0).
3764 getOperand(0),
3765 MachineNode->getOperand(1));
3766 else if (Op2Not)
3767 // nor(x, ~y) = andc(y, x)
3768 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3769 MVT::i1, MachineNode->getOperand(1).
3770 getOperand(0),
3771 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003772 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003773 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3774 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003775 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003776 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003777 }
Hal Finkel940ab932014-02-28 00:27:01 +00003778 break;
3779 case PPC::CREQV:
3780 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3781 // eqv(x, x) = 1
3782 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3783 MVT::i1);
3784 else if (Op1Set)
3785 // eqv(1, y) = y
3786 ResNode = MachineNode->getOperand(1).getNode();
3787 else if (Op2Set)
3788 // eqv(x, 1) = x
3789 ResNode = MachineNode->getOperand(0).getNode();
3790 else if (Op1Unset)
3791 // eqv(0, y) = ~y -> nor(y, y)
3792 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3793 MVT::i1, MachineNode->getOperand(1),
3794 MachineNode->getOperand(1));
3795 else if (Op2Unset)
3796 // eqv(x, 0) = ~x
3797 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3798 MVT::i1, MachineNode->getOperand(0),
3799 MachineNode->getOperand(0));
3800 else if (Op1Not)
3801 // eqv(~x, y) = xor(x, y)
3802 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3803 MVT::i1, MachineNode->getOperand(0).
3804 getOperand(0),
3805 MachineNode->getOperand(1));
3806 else if (Op2Not)
3807 // eqv(x, ~y) = xor(x, y)
3808 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3809 MVT::i1, MachineNode->getOperand(0),
3810 MachineNode->getOperand(1).
3811 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003812 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003813 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3814 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003815 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003816 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003817 }
Hal Finkel940ab932014-02-28 00:27:01 +00003818 break;
3819 case PPC::CRANDC:
3820 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3821 // andc(x, x) = 0
3822 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3823 MVT::i1);
3824 else if (Op1Set)
3825 // andc(1, y) = ~y
3826 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3827 MVT::i1, MachineNode->getOperand(1),
3828 MachineNode->getOperand(1));
3829 else if (Op1Unset || Op2Set)
3830 // andc(0, y) = andc(x, 1) = 0
3831 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3832 MVT::i1);
3833 else if (Op2Unset)
3834 // andc(x, 0) = x
3835 ResNode = MachineNode->getOperand(0).getNode();
3836 else if (Op1Not)
3837 // andc(~x, y) = ~(x | y) = nor(x, y)
3838 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3839 MVT::i1, MachineNode->getOperand(0).
3840 getOperand(0),
3841 MachineNode->getOperand(1));
3842 else if (Op2Not)
3843 // andc(x, ~y) = x & y
3844 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3845 MVT::i1, MachineNode->getOperand(0),
3846 MachineNode->getOperand(1).
3847 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003848 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003849 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3850 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00003851 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003852 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003853 }
Hal Finkel940ab932014-02-28 00:27:01 +00003854 break;
3855 case PPC::CRORC:
3856 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3857 // orc(x, x) = 1
3858 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3859 MVT::i1);
3860 else if (Op1Set || Op2Unset)
3861 // orc(1, y) = orc(x, 0) = 1
3862 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3863 MVT::i1);
3864 else if (Op2Set)
3865 // orc(x, 1) = x
3866 ResNode = MachineNode->getOperand(0).getNode();
3867 else if (Op1Unset)
3868 // orc(0, y) = ~y
3869 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3870 MVT::i1, MachineNode->getOperand(1),
3871 MachineNode->getOperand(1));
3872 else if (Op1Not)
3873 // orc(~x, y) = ~(x & y) = nand(x, y)
3874 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3875 MVT::i1, MachineNode->getOperand(0).
3876 getOperand(0),
3877 MachineNode->getOperand(1));
3878 else if (Op2Not)
3879 // orc(x, ~y) = x | y
3880 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3881 MVT::i1, MachineNode->getOperand(0),
3882 MachineNode->getOperand(1).
3883 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003884 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003885 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3886 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00003887 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003888 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003889 }
Hal Finkel940ab932014-02-28 00:27:01 +00003890 break;
3891 case PPC::SELECT_I4:
3892 case PPC::SELECT_I8:
3893 case PPC::SELECT_F4:
3894 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003895 case PPC::SELECT_QFRC:
3896 case PPC::SELECT_QSRC:
3897 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003898 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003899 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003900 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003901 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003902 if (Op1Set)
3903 ResNode = MachineNode->getOperand(1).getNode();
3904 else if (Op1Unset)
3905 ResNode = MachineNode->getOperand(2).getNode();
3906 else if (Op1Not)
3907 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3908 SDLoc(MachineNode),
3909 MachineNode->getValueType(0),
3910 MachineNode->getOperand(0).
3911 getOperand(0),
3912 MachineNode->getOperand(2),
3913 MachineNode->getOperand(1));
3914 break;
3915 case PPC::BC:
3916 case PPC::BCn:
3917 if (Op1Not)
3918 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3919 PPC::BC,
3920 SDLoc(MachineNode),
3921 MVT::Other,
3922 MachineNode->getOperand(0).
3923 getOperand(0),
3924 MachineNode->getOperand(1),
3925 MachineNode->getOperand(2));
3926 // FIXME: Handle Op1Set, Op1Unset here too.
3927 break;
3928 }
3929
Hal Finkelb9989152014-02-28 06:11:16 +00003930 // If we're inverting this node because it is used only by selects that
3931 // we'd like to swap, then swap the selects before the node replacement.
3932 if (SelectSwap)
3933 SwapAllSelectUsers(MachineNode);
3934
Hal Finkel940ab932014-02-28 00:27:01 +00003935 if (ResNode != MachineNode) {
3936 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3937 DEBUG(MachineNode->dump(CurDAG));
3938 DEBUG(dbgs() << "\nNew: ");
3939 DEBUG(ResNode->dump(CurDAG));
3940 DEBUG(dbgs() << "\n");
3941
3942 ReplaceUses(MachineNode, ResNode);
3943 IsModified = true;
3944 }
3945 }
3946 if (IsModified)
3947 CurDAG->RemoveDeadNodes();
3948 } while (IsModified);
3949}
3950
Hal Finkel4c6658f2014-12-12 23:59:36 +00003951// Gather the set of 32-bit operations that are known to have their
3952// higher-order 32 bits zero, where ToPromote contains all such operations.
3953static bool PeepholePPC64ZExtGather(SDValue Op32,
3954 SmallPtrSetImpl<SDNode *> &ToPromote) {
3955 if (!Op32.isMachineOpcode())
3956 return false;
3957
3958 // First, check for the "frontier" instructions (those that will clear the
3959 // higher-order 32 bits.
3960
3961 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3962 // around. If it does not, then these instructions will clear the
3963 // higher-order bits.
3964 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3965 Op32.getMachineOpcode() == PPC::RLWNM) &&
3966 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3967 ToPromote.insert(Op32.getNode());
3968 return true;
3969 }
3970
3971 // SLW and SRW always clear the higher-order bits.
3972 if (Op32.getMachineOpcode() == PPC::SLW ||
3973 Op32.getMachineOpcode() == PPC::SRW) {
3974 ToPromote.insert(Op32.getNode());
3975 return true;
3976 }
3977
3978 // For LI and LIS, we need the immediate to be positive (so that it is not
3979 // sign extended).
3980 if (Op32.getMachineOpcode() == PPC::LI ||
3981 Op32.getMachineOpcode() == PPC::LIS) {
3982 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3983 return false;
3984
3985 ToPromote.insert(Op32.getNode());
3986 return true;
3987 }
3988
Hal Finkel4e2c7822015-01-05 18:09:06 +00003989 // LHBRX and LWBRX always clear the higher-order bits.
3990 if (Op32.getMachineOpcode() == PPC::LHBRX ||
3991 Op32.getMachineOpcode() == PPC::LWBRX) {
3992 ToPromote.insert(Op32.getNode());
3993 return true;
3994 }
3995
Hal Finkel49557f12015-01-05 18:52:29 +00003996 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended.
3997 if (Op32.getMachineOpcode() == PPC::CNTLZW) {
3998 ToPromote.insert(Op32.getNode());
3999 return true;
4000 }
4001
Hal Finkel4c6658f2014-12-12 23:59:36 +00004002 // Next, check for those instructions we can look through.
4003
4004 // Assuming the mask does not wrap around, then the higher-order bits are
4005 // taken directly from the first operand.
4006 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
4007 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
4008 SmallPtrSet<SDNode *, 16> ToPromote1;
4009 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4010 return false;
4011
4012 ToPromote.insert(Op32.getNode());
4013 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4014 return true;
4015 }
4016
4017 // For OR, the higher-order bits are zero if that is true for both operands.
4018 // For SELECT_I4, the same is true (but the relevant operand numbers are
4019 // shifted by 1).
4020 if (Op32.getMachineOpcode() == PPC::OR ||
4021 Op32.getMachineOpcode() == PPC::SELECT_I4) {
4022 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
4023 SmallPtrSet<SDNode *, 16> ToPromote1;
4024 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
4025 return false;
4026 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
4027 return false;
4028
4029 ToPromote.insert(Op32.getNode());
4030 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4031 return true;
4032 }
4033
4034 // For ORI and ORIS, we need the higher-order bits of the first operand to be
4035 // zero, and also for the constant to be positive (so that it is not sign
4036 // extended).
4037 if (Op32.getMachineOpcode() == PPC::ORI ||
4038 Op32.getMachineOpcode() == PPC::ORIS) {
4039 SmallPtrSet<SDNode *, 16> ToPromote1;
4040 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4041 return false;
4042 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
4043 return false;
4044
4045 ToPromote.insert(Op32.getNode());
4046 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4047 return true;
4048 }
4049
4050 // The higher-order bits of AND are zero if that is true for at least one of
4051 // the operands.
4052 if (Op32.getMachineOpcode() == PPC::AND) {
4053 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
4054 bool Op0OK =
4055 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4056 bool Op1OK =
4057 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
4058 if (!Op0OK && !Op1OK)
4059 return false;
4060
4061 ToPromote.insert(Op32.getNode());
4062
4063 if (Op0OK)
4064 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4065
4066 if (Op1OK)
4067 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
4068
4069 return true;
4070 }
4071
4072 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
4073 // of the first operand, or if the second operand is positive (so that it is
4074 // not sign extended).
4075 if (Op32.getMachineOpcode() == PPC::ANDIo ||
4076 Op32.getMachineOpcode() == PPC::ANDISo) {
4077 SmallPtrSet<SDNode *, 16> ToPromote1;
4078 bool Op0OK =
4079 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4080 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
4081 if (!Op0OK && !Op1OK)
4082 return false;
4083
4084 ToPromote.insert(Op32.getNode());
4085
4086 if (Op0OK)
4087 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4088
4089 return true;
4090 }
4091
4092 return false;
4093}
4094
4095void PPCDAGToDAGISel::PeepholePPC64ZExt() {
4096 if (!PPCSubTarget->isPPC64())
4097 return;
4098
4099 // When we zero-extend from i32 to i64, we use a pattern like this:
4100 // def : Pat<(i64 (zext i32:$in)),
4101 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
4102 // 0, 32)>;
4103 // There are several 32-bit shift/rotate instructions, however, that will
4104 // clear the higher-order bits of their output, rendering the RLDICL
4105 // unnecessary. When that happens, we remove it here, and redefine the
4106 // relevant 32-bit operation to be a 64-bit operation.
4107
4108 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4109 ++Position;
4110
4111 bool MadeChange = false;
4112 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004113 SDNode *N = &*--Position;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004114 // Skip dead nodes and any non-machine opcodes.
4115 if (N->use_empty() || !N->isMachineOpcode())
4116 continue;
4117
4118 if (N->getMachineOpcode() != PPC::RLDICL)
4119 continue;
4120
4121 if (N->getConstantOperandVal(1) != 0 ||
4122 N->getConstantOperandVal(2) != 32)
4123 continue;
4124
4125 SDValue ISR = N->getOperand(0);
4126 if (!ISR.isMachineOpcode() ||
4127 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
4128 continue;
4129
4130 if (!ISR.hasOneUse())
4131 continue;
4132
4133 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
4134 continue;
4135
4136 SDValue IDef = ISR.getOperand(0);
4137 if (!IDef.isMachineOpcode() ||
4138 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
4139 continue;
4140
4141 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
4142 // can get rid of it.
4143
4144 SDValue Op32 = ISR->getOperand(1);
4145 if (!Op32.isMachineOpcode())
4146 continue;
4147
4148 // There are some 32-bit instructions that always clear the high-order 32
4149 // bits, there are also some instructions (like AND) that we can look
4150 // through.
4151 SmallPtrSet<SDNode *, 16> ToPromote;
4152 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4153 continue;
4154
4155 // If the ToPromote set contains nodes that have uses outside of the set
4156 // (except for the original INSERT_SUBREG), then abort the transformation.
4157 bool OutsideUse = false;
4158 for (SDNode *PN : ToPromote) {
4159 for (SDNode *UN : PN->uses()) {
4160 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4161 OutsideUse = true;
4162 break;
4163 }
4164 }
4165
4166 if (OutsideUse)
4167 break;
4168 }
4169 if (OutsideUse)
4170 continue;
4171
4172 MadeChange = true;
4173
4174 // We now know that this zero extension can be removed by promoting to
4175 // nodes in ToPromote to 64-bit operations, where for operations in the
4176 // frontier of the set, we need to insert INSERT_SUBREGs for their
4177 // operands.
4178 for (SDNode *PN : ToPromote) {
4179 unsigned NewOpcode;
4180 switch (PN->getMachineOpcode()) {
4181 default:
4182 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4183 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4184 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4185 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4186 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4187 case PPC::LI: NewOpcode = PPC::LI8; break;
4188 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004189 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4190 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004191 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004192 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4193 case PPC::OR: NewOpcode = PPC::OR8; break;
4194 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4195 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4196 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4197 case PPC::AND: NewOpcode = PPC::AND8; break;
4198 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4199 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4200 }
4201
4202 // Note: During the replacement process, the nodes will be in an
4203 // inconsistent state (some instructions will have operands with values
4204 // of the wrong type). Once done, however, everything should be right
4205 // again.
4206
4207 SmallVector<SDValue, 4> Ops;
4208 for (const SDValue &V : PN->ops()) {
4209 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4210 !isa<ConstantSDNode>(V)) {
4211 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4212 SDNode *ReplOp =
4213 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4214 ISR.getNode()->getVTList(), ReplOpOps);
4215 Ops.push_back(SDValue(ReplOp, 0));
4216 } else {
4217 Ops.push_back(V);
4218 }
4219 }
4220
4221 // Because all to-be-promoted nodes only have users that are other
4222 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4223 // the i32 result value type with i64.
4224
4225 SmallVector<EVT, 2> NewVTs;
4226 SDVTList VTs = PN->getVTList();
4227 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4228 if (VTs.VTs[i] == MVT::i32)
4229 NewVTs.push_back(MVT::i64);
4230 else
4231 NewVTs.push_back(VTs.VTs[i]);
4232
4233 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4234 DEBUG(PN->dump(CurDAG));
4235
4236 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4237
4238 DEBUG(dbgs() << "\nNew: ");
4239 DEBUG(PN->dump(CurDAG));
4240 DEBUG(dbgs() << "\n");
4241 }
4242
4243 // Now we replace the original zero extend and its associated INSERT_SUBREG
4244 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4245 // return an i64).
4246
4247 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4248 DEBUG(N->dump(CurDAG));
4249 DEBUG(dbgs() << "\nNew: ");
4250 DEBUG(Op32.getNode()->dump(CurDAG));
4251 DEBUG(dbgs() << "\n");
4252
4253 ReplaceUses(N, Op32.getNode());
4254 }
4255
4256 if (MadeChange)
4257 CurDAG->RemoveDeadNodes();
4258}
4259
Hal Finkel940ab932014-02-28 00:27:01 +00004260void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004261 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004262 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004263 return;
4264
4265 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4266 ++Position;
4267
4268 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004269 SDNode *N = &*--Position;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004270 // Skip dead nodes and any non-machine opcodes.
4271 if (N->use_empty() || !N->isMachineOpcode())
4272 continue;
4273
4274 unsigned FirstOp;
4275 unsigned StorageOpcode = N->getMachineOpcode();
4276
4277 switch (StorageOpcode) {
4278 default: continue;
4279
4280 case PPC::LBZ:
4281 case PPC::LBZ8:
4282 case PPC::LD:
4283 case PPC::LFD:
4284 case PPC::LFS:
4285 case PPC::LHA:
4286 case PPC::LHA8:
4287 case PPC::LHZ:
4288 case PPC::LHZ8:
4289 case PPC::LWA:
4290 case PPC::LWZ:
4291 case PPC::LWZ8:
4292 FirstOp = 0;
4293 break;
4294
4295 case PPC::STB:
4296 case PPC::STB8:
4297 case PPC::STD:
4298 case PPC::STFD:
4299 case PPC::STFS:
4300 case PPC::STH:
4301 case PPC::STH8:
4302 case PPC::STW:
4303 case PPC::STW8:
4304 FirstOp = 1;
4305 break;
4306 }
4307
Kyle Butt1452b762015-12-11 00:47:36 +00004308 // If this is a load or store with a zero offset, or within the alignment,
4309 // we may be able to fold an add-immediate into the memory operation.
4310 // The check against alignment is below, as it can't occur until we check
4311 // the arguments to N
4312 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)))
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004313 continue;
4314
4315 SDValue Base = N->getOperand(FirstOp + 1);
4316 if (!Base.isMachineOpcode())
4317 continue;
4318
Kyle Butt1452b762015-12-11 00:47:36 +00004319 // On targets with fusion, we don't want this to fire and remove a fusion
4320 // opportunity, unless a) it results in another fusion opportunity or
4321 // b) optimizing for size.
4322 if (PPCSubTarget->hasFusion() &&
Hans Wennborga8e6b3e2015-12-11 00:58:32 +00004323 (!MF->getFunction()->optForSize() && !Base.hasOneUse()))
Kyle Butt1452b762015-12-11 00:47:36 +00004324 continue;
4325
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004326 unsigned Flags = 0;
4327 bool ReplaceFlags = true;
4328
4329 // When the feeding operation is an add-immediate of some sort,
4330 // determine whether we need to add relocation information to the
4331 // target flags on the immediate operand when we fold it into the
4332 // load instruction.
4333 //
4334 // For something like ADDItocL, the relocation information is
4335 // inferred from the opcode; when we process it in the AsmPrinter,
4336 // we add the necessary relocation there. A load, though, can receive
4337 // relocation from various flavors of ADDIxxx, so we need to carry
4338 // the relocation information in the target flags.
4339 switch (Base.getMachineOpcode()) {
4340 default: continue;
4341
4342 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004343 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004344 // In some cases (such as TLS) the relocation information
4345 // is already in place on the operand, so copying the operand
4346 // is sufficient.
4347 ReplaceFlags = false;
4348 // For these cases, the immediate may not be divisible by 4, in
4349 // which case the fold is illegal for DS-form instructions. (The
4350 // other cases provide aligned addresses and are always safe.)
4351 if ((StorageOpcode == PPC::LWA ||
4352 StorageOpcode == PPC::LD ||
4353 StorageOpcode == PPC::STD) &&
4354 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4355 Base.getConstantOperandVal(1) % 4 != 0))
4356 continue;
4357 break;
4358 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004359 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004360 break;
4361 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004362 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004363 break;
4364 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004365 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004366 break;
4367 }
4368
Kyle Butt1452b762015-12-11 00:47:36 +00004369 SDValue ImmOpnd = Base.getOperand(1);
4370 int MaxDisplacement = 0;
4371 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
4372 const GlobalValue *GV = GA->getGlobal();
4373 MaxDisplacement = GV->getAlignment() - 1;
4374 }
4375
4376 int Offset = N->getConstantOperandVal(FirstOp);
4377 if (Offset < 0 || Offset > MaxDisplacement)
4378 continue;
4379
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004380 // We found an opportunity. Reverse the operands from the add
4381 // immediate and substitute them into the load or store. If
4382 // needed, update the target flags for the immediate operand to
4383 // reflect the necessary relocation information.
4384 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4385 DEBUG(Base->dump(CurDAG));
4386 DEBUG(dbgs() << "\nN: ");
4387 DEBUG(N->dump(CurDAG));
4388 DEBUG(dbgs() << "\n");
4389
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004390 // If the relocation information isn't already present on the
4391 // immediate operand, add it now.
4392 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004393 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004394 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004395 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004396 // We can't perform this optimization for data whose alignment
4397 // is insufficient for the instruction encoding.
4398 if (GV->getAlignment() < 4 &&
4399 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
Kyle Butt1452b762015-12-11 00:47:36 +00004400 StorageOpcode == PPC::LWA || (Offset % 4) != 0)) {
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004401 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4402 continue;
4403 }
Kyle Butt1452b762015-12-11 00:47:36 +00004404 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004405 } else if (ConstantPoolSDNode *CP =
4406 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004407 const Constant *C = CP->getConstVal();
4408 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4409 CP->getAlignment(),
Kyle Butt1452b762015-12-11 00:47:36 +00004410 Offset, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004411 }
4412 }
4413
4414 if (FirstOp == 1) // Store
4415 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4416 Base.getOperand(0), N->getOperand(3));
4417 else // Load
4418 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4419 N->getOperand(2));
4420
4421 // The add-immediate may now be dead, in which case remove it.
4422 if (Base.getNode()->use_empty())
4423 CurDAG->RemoveDeadNode(Base.getNode());
4424 }
4425}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004426
Chris Lattnerb055c872006-06-10 01:15:02 +00004427
Andrew Trickc416ba62010-12-24 04:28:06 +00004428/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004429/// PowerPC-specific DAG, ready for instruction scheduling.
4430///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004431FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004432 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004433}
4434
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004435static void initializePassOnce(PassRegistry &Registry) {
4436 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004437 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4438 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004439 Registry.registerPass(*PI, true);
4440}
4441
4442void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4443 CALL_ONCE_INITIALIZATION(initializePassOnce);
4444}