blob: 7e8fc3099f76404cb6a3c971fe9fc8fadde6b96b [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
Hal Finkelb5e9b042014-12-11 22:51:06 +0000125 SDNode *getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
126
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.
Craig Topper0d3fa922014-04-29 07:57:37 +0000129 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000130
Nate Begeman93c4bc62005-08-19 00:38:14 +0000131 SDNode *SelectBitfieldInsert(SDNode *N);
Hal Finkel8adf2252014-12-16 05:51:41 +0000132 SDNode *SelectBitPermutation(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:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000231 SDNode *SelectSETCC(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
243 SDNode *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
Hal Finkelb5e9b042014-12-11 22:51:06 +0000461SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
462 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())
467 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000468 getSmallIPtrImm(Offset, dl));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000469 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000470 getSmallIPtrImm(Offset, dl));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000471}
472
Andrew Trickc416ba62010-12-24 04:28:06 +0000473bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
474 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000475 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000476 // Don't even go down this path for i64, since different logic will be
477 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000478 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000479 return false;
480
Nate Begemanb3821a32005-08-18 07:30:46 +0000481 unsigned Shift = 32;
482 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
483 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000484 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000485 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000486 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000487
Nate Begemanb3821a32005-08-18 07:30:46 +0000488 if (Opcode == ISD::SHL) {
489 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000490 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000491 // determine which bits are made indeterminant by shift
492 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000493 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000494 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000495 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000496 // determine which bits are made indeterminant by shift
497 Indeterminant = ~(0xFFFFFFFFu >> Shift);
498 // adjust for the left rotate
499 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000500 } else if (Opcode == ISD::ROTL) {
501 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000502 } else {
503 return false;
504 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000505
Nate Begemanb3821a32005-08-18 07:30:46 +0000506 // if the mask doesn't intersect any Indeterminant bits
507 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000508 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000509 // make sure the mask is still a mask (wrap arounds may not be)
510 return isRunOfOnes(Mask, MB, ME);
511 }
512 return false;
513}
514
Nate Begeman93c4bc62005-08-19 00:38:14 +0000515/// SelectBitfieldInsert - turn an or of two masked values into
516/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000517SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000518 SDValue Op0 = N->getOperand(0);
519 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000520 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000521
Dan Gohmanf19609a2008-02-27 01:23:58 +0000522 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000523 CurDAG->computeKnownBits(Op0, LKZ, LKO);
524 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000525
Dan Gohmanf19609a2008-02-27 01:23:58 +0000526 unsigned TargetMask = LKZ.getZExtValue();
527 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000528
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000529 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
530 unsigned Op0Opc = Op0.getOpcode();
531 unsigned Op1Opc = Op1.getOpcode();
532 unsigned Value, SH = 0;
533 TargetMask = ~TargetMask;
534 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000535
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000536 // If the LHS has a foldable shift and the RHS does not, then swap it to the
537 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000538 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
539 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
540 Op0.getOperand(0).getOpcode() == ISD::SRL) {
541 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
542 Op1.getOperand(0).getOpcode() != ISD::SRL) {
543 std::swap(Op0, Op1);
544 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000545 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000546 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000547 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000548 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
549 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
550 Op1.getOperand(0).getOpcode() != ISD::SRL) {
551 std::swap(Op0, Op1);
552 std::swap(Op0Opc, Op1Opc);
553 std::swap(TargetMask, InsertMask);
554 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000555 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000556
Nate Begeman1333cea2006-05-07 00:23:38 +0000557 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000558 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000559 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000560
561 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000562 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000563 Op1 = Op1.getOperand(0);
564 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
565 }
566 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000567 // The AND mask might not be a constant, and we need to make sure that
568 // if we're going to fold the masking with the insert, all bits not
569 // know to be zero in the mask are known to be one.
570 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000571 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000572 bool CanFoldMask = InsertMask == MKO.getZExtValue();
573
Nate Begeman1333cea2006-05-07 00:23:38 +0000574 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000575 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000576 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000577 // Note that Value must be in range here (less than 32) because
578 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000579 Op1 = Op1.getOperand(0).getOperand(0);
580 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000581 }
582 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000583
Chris Lattnera2963392006-05-12 16:29:37 +0000584 SH &= 31;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000585 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
586 getI32Imm(ME, dl) };
Michael Liaob53d8962013-04-19 22:22:57 +0000587 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000588 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000589 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000590 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000591}
592
Hal Finkelc58ce412015-01-01 02:53:29 +0000593// Predict the number of instructions that would be generated by calling
594// SelectInt64(N).
Hal Finkelca6375f2015-01-04 12:35:03 +0000595static unsigned SelectInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000596 // Assume no remaining bits.
597 unsigned Remainder = 0;
598 // Assume no shift required.
599 unsigned Shift = 0;
600
601 // If it can't be represented as a 32 bit value.
602 if (!isInt<32>(Imm)) {
603 Shift = countTrailingZeros<uint64_t>(Imm);
604 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
605
606 // If the shifted value fits 32 bits.
607 if (isInt<32>(ImmSh)) {
608 // Go with the shifted value.
609 Imm = ImmSh;
610 } else {
611 // Still stuck with a 64 bit value.
612 Remainder = Imm;
613 Shift = 32;
614 Imm >>= 32;
615 }
616 }
617
618 // Intermediate operand.
619 unsigned Result = 0;
620
621 // Handle first 32 bits.
622 unsigned Lo = Imm & 0xFFFF;
Hal Finkelc58ce412015-01-01 02:53:29 +0000623
624 // Simple value.
625 if (isInt<16>(Imm)) {
626 // Just the Lo bits.
627 ++Result;
628 } else if (Lo) {
629 // Handle the Hi bits and Lo bits.
630 Result += 2;
631 } else {
632 // Just the Hi bits.
633 ++Result;
634 }
635
636 // If no shift, we're done.
637 if (!Shift) return Result;
638
639 // Shift for next step if the upper 32-bits were not zero.
640 if (Imm)
641 ++Result;
642
643 // Add in the last bits as required.
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000644 if ((Remainder >> 16) & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000645 ++Result;
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000646 if (Remainder & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000647 ++Result;
648
649 return Result;
650}
651
Hal Finkel241ba792015-01-04 15:43:55 +0000652static uint64_t Rot64(uint64_t Imm, unsigned R) {
653 return (Imm << R) | (Imm >> (64 - R));
654}
655
Hal Finkelca6375f2015-01-04 12:35:03 +0000656static unsigned SelectInt64Count(int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000657 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000658 if (Count == 1)
659 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000660
Hal Finkel241ba792015-01-04 15:43:55 +0000661 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000662 uint64_t RImm = Rot64(Imm, r);
663 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
664 Count = std::min(Count, RCount);
665
666 // See comments in SelectInt64 for an explanation of the logic below.
667 unsigned LS = findLastSet(RImm);
668 if (LS != r-1)
669 continue;
670
671 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
672 uint64_t RImmWithOnes = RImm | OnesMask;
673
674 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000675 Count = std::min(Count, RCount);
676 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000677
Hal Finkel241ba792015-01-04 15:43:55 +0000678 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000679}
680
Hal Finkelc58ce412015-01-01 02:53:29 +0000681// Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count
682// (above) needs to be kept in sync with this function.
Hal Finkelca6375f2015-01-04 12:35:03 +0000683static SDNode *SelectInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000684 // Assume no remaining bits.
685 unsigned Remainder = 0;
686 // Assume no shift required.
687 unsigned Shift = 0;
688
689 // If it can't be represented as a 32 bit value.
690 if (!isInt<32>(Imm)) {
691 Shift = countTrailingZeros<uint64_t>(Imm);
692 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
693
694 // If the shifted value fits 32 bits.
695 if (isInt<32>(ImmSh)) {
696 // Go with the shifted value.
697 Imm = ImmSh;
698 } else {
699 // Still stuck with a 64 bit value.
700 Remainder = Imm;
701 Shift = 32;
702 Imm >>= 32;
703 }
704 }
705
706 // Intermediate operand.
707 SDNode *Result;
708
709 // Handle first 32 bits.
710 unsigned Lo = Imm & 0xFFFF;
711 unsigned Hi = (Imm >> 16) & 0xFFFF;
712
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000713 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
714 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000715 };
716
717 // Simple value.
718 if (isInt<16>(Imm)) {
719 // Just the Lo bits.
720 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
721 } else if (Lo) {
722 // Handle the Hi bits.
723 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
724 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
725 // And Lo bits.
726 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
727 SDValue(Result, 0), getI32Imm(Lo));
728 } else {
729 // Just the Hi bits.
730 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
731 }
732
733 // If no shift, we're done.
734 if (!Shift) return Result;
735
736 // Shift for next step if the upper 32-bits were not zero.
737 if (Imm) {
738 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
739 SDValue(Result, 0),
740 getI32Imm(Shift),
741 getI32Imm(63 - Shift));
742 }
743
744 // Add in the last bits as required.
745 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
746 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
747 SDValue(Result, 0), getI32Imm(Hi));
748 }
749 if ((Lo = Remainder & 0xFFFF)) {
750 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
751 SDValue(Result, 0), getI32Imm(Lo));
752 }
753
754 return Result;
755}
756
Hal Finkelca6375f2015-01-04 12:35:03 +0000757static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000758 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000759 if (Count == 1)
760 return SelectInt64Direct(CurDAG, dl, Imm);
761
Hal Finkel241ba792015-01-04 15:43:55 +0000762 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000763
Hal Finkel2f618792015-01-05 03:41:38 +0000764 int64_t MatImm;
765 unsigned MaskEnd;
766
Hal Finkel241ba792015-01-04 15:43:55 +0000767 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000768 uint64_t RImm = Rot64(Imm, r);
769 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000770 if (RCount < Count) {
771 Count = RCount;
772 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000773 MatImm = RImm;
774 MaskEnd = 63;
775 }
776
777 // If the immediate to generate has many trailing zeros, it might be
778 // worthwhile to generate a rotated value with too many leading ones
779 // (because that's free with li/lis's sign-extension semantics), and then
780 // mask them off after rotation.
781
782 unsigned LS = findLastSet(RImm);
783 // We're adding (63-LS) higher-order ones, and we expect to mask them off
784 // after performing the inverse rotation by (64-r). So we need that:
785 // 63-LS == 64-r => LS == r-1
786 if (LS != r-1)
787 continue;
788
789 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
790 uint64_t RImmWithOnes = RImm | OnesMask;
791
792 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
793 if (RCount < Count) {
794 Count = RCount;
795 RMin = r;
796 MatImm = RImmWithOnes;
797 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000798 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000799 }
800
Hal Finkel241ba792015-01-04 15:43:55 +0000801 if (!RMin)
802 return SelectInt64Direct(CurDAG, dl, Imm);
803
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000804 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
805 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000806 };
807
Hal Finkel2f618792015-01-05 03:41:38 +0000808 SDValue Val = SDValue(SelectInt64Direct(CurDAG, dl, MatImm), 0);
809 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
810 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000811}
812
Hal Finkelc58ce412015-01-01 02:53:29 +0000813// Select a 64-bit constant.
814static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) {
815 SDLoc dl(N);
816
817 // Get 64 bit value.
818 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
819 return SelectInt64(CurDAG, dl, Imm);
820}
821
Hal Finkel8adf2252014-12-16 05:51:41 +0000822namespace {
823class BitPermutationSelector {
824 struct ValueBit {
825 SDValue V;
826
827 // The bit number in the value, using a convention where bit 0 is the
828 // lowest-order bit.
829 unsigned Idx;
830
831 enum Kind {
832 ConstZero,
833 Variable
834 } K;
835
836 ValueBit(SDValue V, unsigned I, Kind K = Variable)
837 : V(V), Idx(I), K(K) {}
838 ValueBit(Kind K = Variable)
839 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
840
841 bool isZero() const {
842 return K == ConstZero;
843 }
844
845 bool hasValue() const {
846 return K == Variable;
847 }
848
849 SDValue getValue() const {
850 assert(hasValue() && "Cannot get the value of a constant bit");
851 return V;
852 }
853
854 unsigned getValueBitIndex() const {
855 assert(hasValue() && "Cannot get the value bit index of a constant bit");
856 return Idx;
857 }
858 };
859
860 // A bit group has the same underlying value and the same rotate factor.
861 struct BitGroup {
862 SDValue V;
863 unsigned RLAmt;
864 unsigned StartIdx, EndIdx;
865
Hal Finkelc58ce412015-01-01 02:53:29 +0000866 // This rotation amount assumes that the lower 32 bits of the quantity are
867 // replicated in the high 32 bits by the rotation operator (which is done
868 // by rlwinm and friends in 64-bit mode).
869 bool Repl32;
870 // Did converting to Repl32 == true change the rotation factor? If it did,
871 // it decreased it by 32.
872 bool Repl32CR;
873 // Was this group coalesced after setting Repl32 to true?
874 bool Repl32Coalesced;
875
Hal Finkel8adf2252014-12-16 05:51:41 +0000876 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000877 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
878 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000879 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
880 " [" << S << ", " << E << "]\n");
881 }
882 };
883
884 // Information on each (Value, RLAmt) pair (like the number of groups
885 // associated with each) used to choose the lowering method.
886 struct ValueRotInfo {
887 SDValue V;
888 unsigned RLAmt;
889 unsigned NumGroups;
890 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000891 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000892
893 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000894 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
895 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000896
897 // For sorting (in reverse order) by NumGroups, and then by
898 // FirstGroupStartIdx.
899 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000900 // We need to sort so that the non-Repl32 come first because, when we're
901 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
902 // masking operation.
903 if (Repl32 < Other.Repl32)
904 return true;
905 else if (Repl32 > Other.Repl32)
906 return false;
907 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000908 return true;
909 else if (NumGroups < Other.NumGroups)
910 return false;
911 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
912 return true;
913 return false;
914 }
915 };
916
917 // Return true if something interesting was deduced, return false if we're
918 // providing only a generic representation of V (or something else likewise
919 // uninteresting for instruction selection).
920 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
921 switch (V.getOpcode()) {
922 default: break;
923 case ISD::ROTL:
924 if (isa<ConstantSDNode>(V.getOperand(1))) {
925 unsigned RotAmt = V.getConstantOperandVal(1);
926
927 SmallVector<ValueBit, 64> LHSBits(Bits.size());
928 getValueBits(V.getOperand(0), LHSBits);
929
930 for (unsigned i = 0; i < Bits.size(); ++i)
931 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
932
933 return true;
934 }
935 break;
936 case ISD::SHL:
937 if (isa<ConstantSDNode>(V.getOperand(1))) {
938 unsigned ShiftAmt = V.getConstantOperandVal(1);
939
940 SmallVector<ValueBit, 64> LHSBits(Bits.size());
941 getValueBits(V.getOperand(0), LHSBits);
942
943 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
944 Bits[i] = LHSBits[i - ShiftAmt];
945
946 for (unsigned i = 0; i < ShiftAmt; ++i)
947 Bits[i] = ValueBit(ValueBit::ConstZero);
948
949 return true;
950 }
951 break;
952 case ISD::SRL:
953 if (isa<ConstantSDNode>(V.getOperand(1))) {
954 unsigned ShiftAmt = V.getConstantOperandVal(1);
955
956 SmallVector<ValueBit, 64> LHSBits(Bits.size());
957 getValueBits(V.getOperand(0), LHSBits);
958
959 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
960 Bits[i] = LHSBits[i + ShiftAmt];
961
962 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
963 Bits[i] = ValueBit(ValueBit::ConstZero);
964
965 return true;
966 }
967 break;
968 case ISD::AND:
969 if (isa<ConstantSDNode>(V.getOperand(1))) {
970 uint64_t Mask = V.getConstantOperandVal(1);
971
972 SmallVector<ValueBit, 64> LHSBits(Bits.size());
973 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
974
975 for (unsigned i = 0; i < Bits.size(); ++i)
976 if (((Mask >> i) & 1) == 1)
977 Bits[i] = LHSBits[i];
978 else
979 Bits[i] = ValueBit(ValueBit::ConstZero);
980
981 // Mark this as interesting, only if the LHS was also interesting. This
982 // prevents the overall procedure from matching a single immediate 'and'
983 // (which is non-optimal because such an and might be folded with other
984 // things if we don't select it here).
985 return LHSTrivial;
986 }
987 break;
988 case ISD::OR: {
989 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
990 getValueBits(V.getOperand(0), LHSBits);
991 getValueBits(V.getOperand(1), RHSBits);
992
993 bool AllDisjoint = true;
994 for (unsigned i = 0; i < Bits.size(); ++i)
995 if (LHSBits[i].isZero())
996 Bits[i] = RHSBits[i];
997 else if (RHSBits[i].isZero())
998 Bits[i] = LHSBits[i];
999 else {
1000 AllDisjoint = false;
1001 break;
1002 }
1003
1004 if (!AllDisjoint)
1005 break;
1006
1007 return true;
1008 }
1009 }
1010
1011 for (unsigned i = 0; i < Bits.size(); ++i)
1012 Bits[i] = ValueBit(V, i);
1013
1014 return false;
1015 }
1016
1017 // For each value (except the constant ones), compute the left-rotate amount
1018 // to get it from its original to final position.
1019 void computeRotationAmounts() {
1020 HasZeros = false;
1021 RLAmt.resize(Bits.size());
1022 for (unsigned i = 0; i < Bits.size(); ++i)
1023 if (Bits[i].hasValue()) {
1024 unsigned VBI = Bits[i].getValueBitIndex();
1025 if (i >= VBI)
1026 RLAmt[i] = i - VBI;
1027 else
1028 RLAmt[i] = Bits.size() - (VBI - i);
1029 } else if (Bits[i].isZero()) {
1030 HasZeros = true;
1031 RLAmt[i] = UINT32_MAX;
1032 } else {
1033 llvm_unreachable("Unknown value bit type");
1034 }
1035 }
1036
1037 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +00001038 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
1039 // they break up groups.
1040 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001041 BitGroups.clear();
1042
1043 unsigned LastRLAmt = RLAmt[0];
1044 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1045 unsigned LastGroupStartIdx = 0;
1046 for (unsigned i = 1; i < Bits.size(); ++i) {
1047 unsigned ThisRLAmt = RLAmt[i];
1048 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001049 if (LateMask && !ThisValue) {
1050 ThisValue = LastValue;
1051 ThisRLAmt = LastRLAmt;
1052 // If we're doing late masking, then the first bit group always starts
1053 // at zero (even if the first bits were zero).
1054 if (BitGroups.empty())
1055 LastGroupStartIdx = 0;
1056 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001057
1058 // If this bit has the same underlying value and the same rotate factor as
1059 // the last one, then they're part of the same group.
1060 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1061 continue;
1062
1063 if (LastValue.getNode())
1064 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1065 i-1));
1066 LastRLAmt = ThisRLAmt;
1067 LastValue = ThisValue;
1068 LastGroupStartIdx = i;
1069 }
1070 if (LastValue.getNode())
1071 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1072 Bits.size()-1));
1073
1074 if (BitGroups.empty())
1075 return;
1076
1077 // We might be able to combine the first and last groups.
1078 if (BitGroups.size() > 1) {
1079 // If the first and last groups are the same, then remove the first group
1080 // in favor of the last group, making the ending index of the last group
1081 // equal to the ending index of the to-be-removed first group.
1082 if (BitGroups[0].StartIdx == 0 &&
1083 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1084 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1085 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001086 DEBUG(dbgs() << "\tcombining final bit group with initial one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001087 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1088 BitGroups.erase(BitGroups.begin());
1089 }
1090 }
1091 }
1092
1093 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1094 // associated with each. If there is a degeneracy, pick the one that occurs
1095 // first (in the final value).
1096 void collectValueRotInfo() {
1097 ValueRots.clear();
1098
1099 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001100 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1101 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001102 VRI.V = BG.V;
1103 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001104 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001105 VRI.NumGroups += 1;
1106 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1107 }
1108
1109 // Now that we've collected the various ValueRotInfo instances, we need to
1110 // sort them.
1111 ValueRotsVec.clear();
1112 for (auto &I : ValueRots) {
1113 ValueRotsVec.push_back(I.second);
1114 }
1115 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1116 }
1117
Hal Finkelc58ce412015-01-01 02:53:29 +00001118 // In 64-bit mode, rlwinm and friends have a rotation operator that
1119 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1120 // indices of these instructions can only be in the lower 32 bits, so they
1121 // can only represent some 64-bit bit groups. However, when they can be used,
1122 // the 32-bit replication can be used to represent, as a single bit group,
1123 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1124 // groups when possible. Returns true if any of the bit groups were
1125 // converted.
1126 void assignRepl32BitGroups() {
1127 // If we have bits like this:
1128 //
1129 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1130 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1131 // Groups: | RLAmt = 8 | RLAmt = 40 |
1132 //
1133 // But, making use of a 32-bit operation that replicates the low-order 32
1134 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1135 // of 8.
1136
1137 auto IsAllLow32 = [this](BitGroup & BG) {
1138 if (BG.StartIdx <= BG.EndIdx) {
1139 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1140 if (!Bits[i].hasValue())
1141 continue;
1142 if (Bits[i].getValueBitIndex() >= 32)
1143 return false;
1144 }
1145 } else {
1146 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1147 if (!Bits[i].hasValue())
1148 continue;
1149 if (Bits[i].getValueBitIndex() >= 32)
1150 return false;
1151 }
1152 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1153 if (!Bits[i].hasValue())
1154 continue;
1155 if (Bits[i].getValueBitIndex() >= 32)
1156 return false;
1157 }
1158 }
1159
1160 return true;
1161 };
1162
1163 for (auto &BG : BitGroups) {
1164 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1165 if (IsAllLow32(BG)) {
1166 if (BG.RLAmt >= 32) {
1167 BG.RLAmt -= 32;
1168 BG.Repl32CR = true;
1169 }
1170
1171 BG.Repl32 = true;
1172
1173 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1174 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1175 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1176 }
1177 }
1178 }
1179
1180 // Now walk through the bit groups, consolidating where possible.
1181 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1182 // We might want to remove this bit group by merging it with the previous
1183 // group (which might be the ending group).
1184 auto IP = (I == BitGroups.begin()) ?
1185 std::prev(BitGroups.end()) : std::prev(I);
1186 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1187 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1188
1189 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1190 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1191 " [" << I->StartIdx << ", " << I->EndIdx <<
1192 "] with group with range [" <<
1193 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1194
1195 IP->EndIdx = I->EndIdx;
1196 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1197 IP->Repl32Coalesced = true;
1198 I = BitGroups.erase(I);
1199 continue;
1200 } else {
1201 // There is a special case worth handling: If there is a single group
1202 // covering the entire upper 32 bits, and it can be merged with both
1203 // the next and previous groups (which might be the same group), then
1204 // do so. If it is the same group (so there will be only one group in
1205 // total), then we need to reverse the order of the range so that it
1206 // covers the entire 64 bits.
1207 if (I->StartIdx == 32 && I->EndIdx == 63) {
1208 assert(std::next(I) == BitGroups.end() &&
1209 "bit group ends at index 63 but there is another?");
1210 auto IN = BitGroups.begin();
1211
1212 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
1213 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1214 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1215 IsAllLow32(*I)) {
1216
1217 DEBUG(dbgs() << "\tcombining bit group for " <<
1218 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1219 " [" << I->StartIdx << ", " << I->EndIdx <<
1220 "] with 32-bit replicated groups with ranges [" <<
1221 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1222 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1223
1224 if (IP == IN) {
1225 // There is only one other group; change it to cover the whole
1226 // range (backward, so that it can still be Repl32 but cover the
1227 // whole 64-bit range).
1228 IP->StartIdx = 31;
1229 IP->EndIdx = 30;
1230 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1231 IP->Repl32Coalesced = true;
1232 I = BitGroups.erase(I);
1233 } else {
1234 // There are two separate groups, one before this group and one
1235 // after us (at the beginning). We're going to remove this group,
1236 // but also the group at the very beginning.
1237 IP->EndIdx = IN->EndIdx;
1238 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1239 IP->Repl32Coalesced = true;
1240 I = BitGroups.erase(I);
1241 BitGroups.erase(BitGroups.begin());
1242 }
1243
1244 // This must be the last group in the vector (and we might have
1245 // just invalidated the iterator above), so break here.
1246 break;
1247 }
1248 }
1249 }
1250
1251 ++I;
1252 }
1253 }
1254
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001255 SDValue getI32Imm(unsigned Imm, SDLoc dl) {
1256 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001257 }
1258
Hal Finkelc58ce412015-01-01 02:53:29 +00001259 uint64_t getZerosMask() {
1260 uint64_t Mask = 0;
1261 for (unsigned i = 0; i < Bits.size(); ++i) {
1262 if (Bits[i].hasValue())
1263 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001264 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001265 }
1266
1267 return ~Mask;
1268 }
1269
Hal Finkel8adf2252014-12-16 05:51:41 +00001270 // Depending on the number of groups for a particular value, it might be
1271 // better to rotate, mask explicitly (using andi/andis), and then or the
1272 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001273 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1274 if (BPermRewriterNoMasking)
1275 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001276
1277 for (ValueRotInfo &VRI : ValueRotsVec) {
1278 unsigned Mask = 0;
1279 for (unsigned i = 0; i < Bits.size(); ++i) {
1280 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1281 continue;
1282 if (RLAmt[i] != VRI.RLAmt)
1283 continue;
1284 Mask |= (1u << i);
1285 }
1286
1287 // Compute the masks for andi/andis that would be necessary.
1288 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1289 assert((ANDIMask != 0 || ANDISMask != 0) &&
1290 "No set bits in mask for value bit groups");
1291 bool NeedsRotate = VRI.RLAmt != 0;
1292
1293 // We're trying to minimize the number of instructions. If we have one
1294 // group, using one of andi/andis can break even. If we have three
1295 // groups, we can use both andi and andis and break even (to use both
1296 // andi and andis we also need to or the results together). We need four
1297 // groups if we also need to rotate. To use andi/andis we need to do more
1298 // than break even because rotate-and-mask instructions tend to be easier
1299 // to schedule.
1300
1301 // FIXME: We've biased here against using andi/andis, which is right for
1302 // POWER cores, but not optimal everywhere. For example, on the A2,
1303 // andi/andis have single-cycle latency whereas the rotate-and-mask
1304 // instructions take two cycles, and it would be better to bias toward
1305 // andi/andis in break-even cases.
1306
1307 unsigned NumAndInsts = (unsigned) NeedsRotate +
1308 (unsigned) (ANDIMask != 0) +
1309 (unsigned) (ANDISMask != 0) +
1310 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1311 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001312
1313 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1314 " RL: " << VRI.RLAmt << ":" <<
1315 "\n\t\t\tisel using masking: " << NumAndInsts <<
1316 " using rotates: " << VRI.NumGroups << "\n");
1317
Hal Finkel8adf2252014-12-16 05:51:41 +00001318 if (NumAndInsts >= VRI.NumGroups)
1319 continue;
1320
Hal Finkelc58ce412015-01-01 02:53:29 +00001321 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1322
1323 if (InstCnt) *InstCnt += NumAndInsts;
1324
Hal Finkel8adf2252014-12-16 05:51:41 +00001325 SDValue VRot;
1326 if (VRI.RLAmt) {
1327 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001328 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1329 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001330 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1331 Ops), 0);
1332 } else {
1333 VRot = VRI.V;
1334 }
1335
1336 SDValue ANDIVal, ANDISVal;
1337 if (ANDIMask != 0)
1338 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001339 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001340 if (ANDISMask != 0)
1341 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001342 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001343
1344 SDValue TotalVal;
1345 if (!ANDIVal)
1346 TotalVal = ANDISVal;
1347 else if (!ANDISVal)
1348 TotalVal = ANDIVal;
1349 else
1350 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1351 ANDIVal, ANDISVal), 0);
1352
1353 if (!Res)
1354 Res = TotalVal;
1355 else
1356 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1357 Res, TotalVal), 0);
1358
1359 // Now, remove all groups with this underlying value and rotation
1360 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001361 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1362 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1363 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001364 }
1365 }
1366
1367 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001368 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001369 SDLoc dl(N);
1370 SDValue Res;
1371
Hal Finkelc58ce412015-01-01 02:53:29 +00001372 if (InstCnt) *InstCnt = 0;
1373
Hal Finkel8adf2252014-12-16 05:51:41 +00001374 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001375 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001376
1377 // If we've not yet selected a 'starting' instruction, and we have no zeros
1378 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1379 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001380 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001381 ValueRotInfo &VRI = ValueRotsVec[0];
1382 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001383 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001384 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001385 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1386 getI32Imm(31, dl) };
1387 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1388 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001389 } else {
1390 Res = VRI.V;
1391 }
1392
1393 // Now, remove all groups with this underlying value and rotation factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001394 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1395 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1396 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001397 }
1398
Hal Finkelc58ce412015-01-01 02:53:29 +00001399 if (InstCnt) *InstCnt += BitGroups.size();
1400
Hal Finkel8adf2252014-12-16 05:51:41 +00001401 // Insert the other groups (one at a time).
1402 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001403 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001404 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001405 { BG.V, getI32Imm(BG.RLAmt, dl),
1406 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1407 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001408 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1409 } else {
1410 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001411 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1412 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1413 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001414 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1415 }
1416 }
1417
Hal Finkelc58ce412015-01-01 02:53:29 +00001418 if (LateMask) {
1419 unsigned Mask = (unsigned) getZerosMask();
1420
1421 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1422 assert((ANDIMask != 0 || ANDISMask != 0) &&
1423 "No set bits in zeros mask?");
1424
1425 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1426 (unsigned) (ANDISMask != 0) +
1427 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1428
1429 SDValue ANDIVal, ANDISVal;
1430 if (ANDIMask != 0)
1431 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001432 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001433 if (ANDISMask != 0)
1434 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001435 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001436
1437 if (!ANDIVal)
1438 Res = ANDISVal;
1439 else if (!ANDISVal)
1440 Res = ANDIVal;
1441 else
1442 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1443 ANDIVal, ANDISVal), 0);
1444 }
1445
Hal Finkel8adf2252014-12-16 05:51:41 +00001446 return Res.getNode();
1447 }
1448
Hal Finkelc58ce412015-01-01 02:53:29 +00001449 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1450 unsigned MaskStart, unsigned MaskEnd,
1451 bool IsIns) {
1452 // In the notation used by the instructions, 'start' and 'end' are reversed
1453 // because bits are counted from high to low order.
1454 unsigned InstMaskStart = 64 - MaskEnd - 1,
1455 InstMaskEnd = 64 - MaskStart - 1;
1456
1457 if (Repl32)
1458 return 1;
1459
1460 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1461 InstMaskEnd == 63 - RLAmt)
1462 return 1;
1463
1464 return 2;
1465 }
1466
1467 // For 64-bit values, not all combinations of rotates and masks are
1468 // available. Produce one if it is available.
1469 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1470 unsigned MaskStart, unsigned MaskEnd,
1471 unsigned *InstCnt = nullptr) {
1472 // In the notation used by the instructions, 'start' and 'end' are reversed
1473 // because bits are counted from high to low order.
1474 unsigned InstMaskStart = 64 - MaskEnd - 1,
1475 InstMaskEnd = 64 - MaskStart - 1;
1476
1477 if (InstCnt) *InstCnt += 1;
1478
1479 if (Repl32) {
1480 // This rotation amount assumes that the lower 32 bits of the quantity
1481 // are replicated in the high 32 bits by the rotation operator (which is
1482 // done by rlwinm and friends).
1483 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1484 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1485 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001486 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1487 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001488 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1489 Ops), 0);
1490 }
1491
1492 if (InstMaskEnd == 63) {
1493 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001494 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001495 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1496 }
1497
1498 if (InstMaskStart == 0) {
1499 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001500 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001501 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1502 }
1503
1504 if (InstMaskEnd == 63 - RLAmt) {
1505 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001506 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001507 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1508 }
1509
1510 // We cannot do this with a single instruction, so we'll use two. The
1511 // problem is that we're not free to choose both a rotation amount and mask
1512 // start and end independently. We can choose an arbitrary mask start and
1513 // end, but then the rotation amount is fixed. Rotation, however, can be
1514 // inverted, and so by applying an "inverse" rotation first, we can get the
1515 // desired result.
1516 if (InstCnt) *InstCnt += 1;
1517
1518 // The rotation mask for the second instruction must be MaskStart.
1519 unsigned RLAmt2 = MaskStart;
1520 // The first instruction must rotate V so that the overall rotation amount
1521 // is RLAmt.
1522 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1523 if (RLAmt1)
1524 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1525 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1526 }
1527
1528 // For 64-bit values, not all combinations of rotates and masks are
1529 // available. Produce a rotate-mask-and-insert if one is available.
1530 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1531 bool Repl32, unsigned MaskStart,
1532 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1533 // In the notation used by the instructions, 'start' and 'end' are reversed
1534 // because bits are counted from high to low order.
1535 unsigned InstMaskStart = 64 - MaskEnd - 1,
1536 InstMaskEnd = 64 - MaskStart - 1;
1537
1538 if (InstCnt) *InstCnt += 1;
1539
1540 if (Repl32) {
1541 // This rotation amount assumes that the lower 32 bits of the quantity
1542 // are replicated in the high 32 bits by the rotation operator (which is
1543 // done by rlwinm and friends).
1544 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1545 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1546 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001547 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1548 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001549 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1550 Ops), 0);
1551 }
1552
1553 if (InstMaskEnd == 63 - RLAmt) {
1554 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001555 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001556 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1557 }
1558
1559 // We cannot do this with a single instruction, so we'll use two. The
1560 // problem is that we're not free to choose both a rotation amount and mask
1561 // start and end independently. We can choose an arbitrary mask start and
1562 // end, but then the rotation amount is fixed. Rotation, however, can be
1563 // inverted, and so by applying an "inverse" rotation first, we can get the
1564 // desired result.
1565 if (InstCnt) *InstCnt += 1;
1566
1567 // The rotation mask for the second instruction must be MaskStart.
1568 unsigned RLAmt2 = MaskStart;
1569 // The first instruction must rotate V so that the overall rotation amount
1570 // is RLAmt.
1571 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1572 if (RLAmt1)
1573 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1574 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1575 }
1576
1577 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1578 if (BPermRewriterNoMasking)
1579 return;
1580
1581 // The idea here is the same as in the 32-bit version, but with additional
1582 // complications from the fact that Repl32 might be true. Because we
1583 // aggressively convert bit groups to Repl32 form (which, for small
1584 // rotation factors, involves no other change), and then coalesce, it might
1585 // be the case that a single 64-bit masking operation could handle both
1586 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1587 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1588 // completely capture the new combined bit group.
1589
1590 for (ValueRotInfo &VRI : ValueRotsVec) {
1591 uint64_t Mask = 0;
1592
1593 // We need to add to the mask all bits from the associated bit groups.
1594 // If Repl32 is false, we need to add bits from bit groups that have
1595 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1596 // group is trivially convertable if it overlaps only with the lower 32
1597 // bits, and the group has not been coalesced.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001598 auto MatchingBG = [VRI](const BitGroup &BG) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001599 if (VRI.V != BG.V)
1600 return false;
1601
1602 unsigned EffRLAmt = BG.RLAmt;
1603 if (!VRI.Repl32 && BG.Repl32) {
1604 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1605 !BG.Repl32Coalesced) {
1606 if (BG.Repl32CR)
1607 EffRLAmt += 32;
1608 } else {
1609 return false;
1610 }
1611 } else if (VRI.Repl32 != BG.Repl32) {
1612 return false;
1613 }
1614
Alexander Kornienko175a7cb2015-12-28 13:38:42 +00001615 return VRI.RLAmt == EffRLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001616 };
1617
1618 for (auto &BG : BitGroups) {
1619 if (!MatchingBG(BG))
1620 continue;
1621
1622 if (BG.StartIdx <= BG.EndIdx) {
1623 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001624 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001625 } else {
1626 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001627 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001628 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001629 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001630 }
1631 }
1632
1633 // We can use the 32-bit andi/andis technique if the mask does not
1634 // require any higher-order bits. This can save an instruction compared
1635 // to always using the general 64-bit technique.
1636 bool Use32BitInsts = isUInt<32>(Mask);
1637 // Compute the masks for andi/andis that would be necessary.
1638 unsigned ANDIMask = (Mask & UINT16_MAX),
1639 ANDISMask = (Mask >> 16) & UINT16_MAX;
1640
1641 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1642
1643 unsigned NumAndInsts = (unsigned) NeedsRotate +
1644 (unsigned) (bool) Res;
1645 if (Use32BitInsts)
1646 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1647 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1648 else
1649 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1;
1650
1651 unsigned NumRLInsts = 0;
1652 bool FirstBG = true;
1653 for (auto &BG : BitGroups) {
1654 if (!MatchingBG(BG))
1655 continue;
1656 NumRLInsts +=
1657 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1658 !FirstBG);
1659 FirstBG = false;
1660 }
1661
1662 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1663 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1664 "\n\t\t\tisel using masking: " << NumAndInsts <<
1665 " using rotates: " << NumRLInsts << "\n");
1666
1667 // When we'd use andi/andis, we bias toward using the rotates (andi only
1668 // has a record form, and is cracked on POWER cores). However, when using
1669 // general 64-bit constant formation, bias toward the constant form,
1670 // because that exposes more opportunities for CSE.
1671 if (NumAndInsts > NumRLInsts)
1672 continue;
1673 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1674 continue;
1675
1676 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1677
1678 if (InstCnt) *InstCnt += NumAndInsts;
1679
1680 SDValue VRot;
1681 // We actually need to generate a rotation if we have a non-zero rotation
1682 // factor or, in the Repl32 case, if we care about any of the
1683 // higher-order replicated bits. In the latter case, we generate a mask
1684 // backward so that it actually includes the entire 64 bits.
1685 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1686 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1687 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1688 else
1689 VRot = VRI.V;
1690
1691 SDValue TotalVal;
1692 if (Use32BitInsts) {
1693 assert((ANDIMask != 0 || ANDISMask != 0) &&
1694 "No set bits in mask when using 32-bit ands for 64-bit value");
1695
1696 SDValue ANDIVal, ANDISVal;
1697 if (ANDIMask != 0)
1698 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001699 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001700 if (ANDISMask != 0)
1701 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001702 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001703
1704 if (!ANDIVal)
1705 TotalVal = ANDISVal;
1706 else if (!ANDISVal)
1707 TotalVal = ANDIVal;
1708 else
1709 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1710 ANDIVal, ANDISVal), 0);
1711 } else {
1712 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1713 TotalVal =
1714 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1715 VRot, TotalVal), 0);
1716 }
1717
1718 if (!Res)
1719 Res = TotalVal;
1720 else
1721 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1722 Res, TotalVal), 0);
1723
1724 // Now, remove all groups with this underlying value and rotation
1725 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001726 eraseMatchingBitGroups(MatchingBG);
Hal Finkelc58ce412015-01-01 02:53:29 +00001727 }
1728 }
1729
1730 // Instruction selection for the 64-bit case.
1731 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1732 SDLoc dl(N);
1733 SDValue Res;
1734
1735 if (InstCnt) *InstCnt = 0;
1736
1737 // Take care of cases that should use andi/andis first.
1738 SelectAndParts64(dl, Res, InstCnt);
1739
1740 // If we've not yet selected a 'starting' instruction, and we have no zeros
1741 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1742 // number of groups), and start with this rotated value.
1743 if ((!HasZeros || LateMask) && !Res) {
1744 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1745 // groups will come first, and so the VRI representing the largest number
1746 // of groups might not be first (it might be the first Repl32 groups).
1747 unsigned MaxGroupsIdx = 0;
1748 if (!ValueRotsVec[0].Repl32) {
1749 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1750 if (ValueRotsVec[i].Repl32) {
1751 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1752 MaxGroupsIdx = i;
1753 break;
1754 }
1755 }
1756
1757 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1758 bool NeedsRotate = false;
1759 if (VRI.RLAmt) {
1760 NeedsRotate = true;
1761 } else if (VRI.Repl32) {
1762 for (auto &BG : BitGroups) {
1763 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1764 BG.Repl32 != VRI.Repl32)
1765 continue;
1766
1767 // We don't need a rotate if the bit group is confined to the lower
1768 // 32 bits.
1769 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1770 continue;
1771
1772 NeedsRotate = true;
1773 break;
1774 }
1775 }
1776
1777 if (NeedsRotate)
1778 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1779 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1780 InstCnt);
1781 else
1782 Res = VRI.V;
1783
1784 // Now, remove all groups with this underlying value and rotation factor.
1785 if (Res)
Benjamin Kramere7561b82015-06-20 15:59:41 +00001786 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1787 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt &&
1788 BG.Repl32 == VRI.Repl32;
1789 });
Hal Finkelc58ce412015-01-01 02:53:29 +00001790 }
1791
1792 // Because 64-bit rotates are more flexible than inserts, we might have a
1793 // preference regarding which one we do first (to save one instruction).
1794 if (!Res)
1795 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1796 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1797 false) <
1798 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1799 true)) {
1800 if (I != BitGroups.begin()) {
1801 BitGroup BG = *I;
1802 BitGroups.erase(I);
1803 BitGroups.insert(BitGroups.begin(), BG);
1804 }
1805
1806 break;
1807 }
1808 }
1809
1810 // Insert the other groups (one at a time).
1811 for (auto &BG : BitGroups) {
1812 if (!Res)
1813 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1814 BG.EndIdx, InstCnt);
1815 else
1816 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1817 BG.StartIdx, BG.EndIdx, InstCnt);
1818 }
1819
1820 if (LateMask) {
1821 uint64_t Mask = getZerosMask();
1822
1823 // We can use the 32-bit andi/andis technique if the mask does not
1824 // require any higher-order bits. This can save an instruction compared
1825 // to always using the general 64-bit technique.
1826 bool Use32BitInsts = isUInt<32>(Mask);
1827 // Compute the masks for andi/andis that would be necessary.
1828 unsigned ANDIMask = (Mask & UINT16_MAX),
1829 ANDISMask = (Mask >> 16) & UINT16_MAX;
1830
1831 if (Use32BitInsts) {
1832 assert((ANDIMask != 0 || ANDISMask != 0) &&
1833 "No set bits in mask when using 32-bit ands for 64-bit value");
1834
1835 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1836 (unsigned) (ANDISMask != 0) +
1837 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1838
1839 SDValue ANDIVal, ANDISVal;
1840 if (ANDIMask != 0)
1841 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001842 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001843 if (ANDISMask != 0)
1844 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001845 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001846
1847 if (!ANDIVal)
1848 Res = ANDISVal;
1849 else if (!ANDISVal)
1850 Res = ANDIVal;
1851 else
1852 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1853 ANDIVal, ANDISVal), 0);
1854 } else {
1855 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1;
1856
1857 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1858 Res =
1859 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1860 Res, MaskVal), 0);
1861 }
1862 }
1863
1864 return Res.getNode();
1865 }
1866
1867 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1868 // Fill in BitGroups.
1869 collectBitGroups(LateMask);
1870 if (BitGroups.empty())
1871 return nullptr;
1872
1873 // For 64-bit values, figure out when we can use 32-bit instructions.
1874 if (Bits.size() == 64)
1875 assignRepl32BitGroups();
1876
1877 // Fill in ValueRotsVec.
1878 collectValueRotInfo();
1879
1880 if (Bits.size() == 32) {
1881 return Select32(N, LateMask, InstCnt);
1882 } else {
1883 assert(Bits.size() == 64 && "Not 64 bits here?");
1884 return Select64(N, LateMask, InstCnt);
1885 }
1886
1887 return nullptr;
1888 }
1889
Benjamin Kramere7561b82015-06-20 15:59:41 +00001890 void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) {
1891 BitGroups.erase(std::remove_if(BitGroups.begin(), BitGroups.end(), F),
1892 BitGroups.end());
1893 }
1894
Hal Finkel8adf2252014-12-16 05:51:41 +00001895 SmallVector<ValueBit, 64> Bits;
1896
1897 bool HasZeros;
1898 SmallVector<unsigned, 64> RLAmt;
1899
1900 SmallVector<BitGroup, 16> BitGroups;
1901
1902 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1903 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1904
1905 SelectionDAG *CurDAG;
1906
1907public:
1908 BitPermutationSelector(SelectionDAG *DAG)
1909 : CurDAG(DAG) {}
1910
1911 // Here we try to match complex bit permutations into a set of
1912 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1913 // known to produce optimial code for common cases (like i32 byte swapping).
1914 SDNode *Select(SDNode *N) {
1915 Bits.resize(N->getValueType(0).getSizeInBits());
1916 if (!getValueBits(SDValue(N, 0), Bits))
1917 return nullptr;
1918
1919 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1920 " selection for: ");
1921 DEBUG(N->dump(CurDAG));
1922
1923 // Fill it RLAmt and set HasZeros.
1924 computeRotationAmounts();
1925
Hal Finkelc58ce412015-01-01 02:53:29 +00001926 if (!HasZeros)
1927 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001928
Hal Finkelc58ce412015-01-01 02:53:29 +00001929 // We currently have two techniques for handling results with zeros: early
1930 // masking (the default) and late masking. Late masking is sometimes more
1931 // efficient, but because the structure of the bit groups is different, it
1932 // is hard to tell without generating both and comparing the results. With
1933 // late masking, we ignore zeros in the resulting value when inserting each
1934 // set of bit groups, and then mask in the zeros at the end. With early
1935 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001936
Hal Finkelc58ce412015-01-01 02:53:29 +00001937 unsigned InstCnt, InstCntLateMask;
1938 DEBUG(dbgs() << "\tEarly masking:\n");
1939 SDNode *RN = Select(N, false, &InstCnt);
1940 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1941
1942 DEBUG(dbgs() << "\tLate masking:\n");
1943 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1944 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1945 " instructions\n");
1946
1947 if (InstCnt <= InstCntLateMask) {
1948 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1949 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001950 }
1951
Hal Finkelc58ce412015-01-01 02:53:29 +00001952 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1953 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001954 }
1955};
1956} // anonymous namespace
1957
1958SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) {
1959 if (N->getValueType(0) != MVT::i32 &&
1960 N->getValueType(0) != MVT::i64)
1961 return nullptr;
1962
Hal Finkelc58ce412015-01-01 02:53:29 +00001963 if (!UseBitPermRewriter)
1964 return nullptr;
1965
Hal Finkel8adf2252014-12-16 05:51:41 +00001966 switch (N->getOpcode()) {
1967 default: break;
1968 case ISD::ROTL:
1969 case ISD::SHL:
1970 case ISD::SRL:
1971 case ISD::AND:
1972 case ISD::OR: {
1973 BitPermutationSelector BPS(CurDAG);
1974 return BPS.Select(N);
1975 }
1976 }
1977
1978 return nullptr;
1979}
1980
Chris Lattner2a1823d2005-08-21 18:50:37 +00001981/// SelectCC - Select a comparison of the specified values with the specified
1982/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001983SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001985 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001986 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001987
Owen Anderson9f944592009-08-11 20:47:22 +00001988 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001989 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001990 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1991 if (isInt32Immediate(RHS, Imm)) {
1992 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001993 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001994 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001995 getI32Imm(Imm & 0xFFFF, dl)),
1996 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001997 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001998 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001999 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002000 getI32Imm(Imm & 0xFFFF, dl)),
2001 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002002
Chris Lattneraa3926b2006-09-20 04:25:47 +00002003 // For non-equality comparisons, the default code would materialize the
2004 // constant, then compare against it, like this:
2005 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002006 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00002007 // cmpw cr0, r3, r2
2008 // Since we are just comparing for equality, we can emit this instead:
2009 // xoris r0,r3,0x1234
2010 // cmplwi cr0,r0,0x5678
2011 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00002012 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002013 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002014 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002015 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002016 }
2017 Opc = PPC::CMPLW;
2018 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002019 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002020 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002021 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002022 Opc = PPC::CMPLW;
2023 } else {
2024 short SImm;
2025 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002026 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002027 getI32Imm((int)SImm & 0xFFFF,
2028 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002029 0);
2030 Opc = PPC::CMPW;
2031 }
Owen Anderson9f944592009-08-11 20:47:22 +00002032 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002033 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002034 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002035 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002036 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002037 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002038 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002039 getI32Imm(Imm & 0xFFFF, dl)),
2040 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002041 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002042 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002043 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002044 getI32Imm(Imm & 0xFFFF, dl)),
2045 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002046
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002047 // For non-equality comparisons, the default code would materialize the
2048 // constant, then compare against it, like this:
2049 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002050 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002051 // cmpd cr0, r3, r2
2052 // Since we are just comparing for equality, we can emit this instead:
2053 // xoris r0,r3,0x1234
2054 // cmpldi cr0,r0,0x5678
2055 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002056 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002057 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002058 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002059 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002060 getI64Imm(Imm & 0xFFFF, dl)),
2061 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002062 }
2063 }
2064 Opc = PPC::CMPLD;
2065 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002066 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002067 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002068 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002069 Opc = PPC::CMPLD;
2070 } else {
2071 short SImm;
2072 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002073 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002074 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002075 0);
2076 Opc = PPC::CMPD;
2077 }
Owen Anderson9f944592009-08-11 20:47:22 +00002078 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002079 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002080 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002081 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002082 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002083 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002084 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002085}
2086
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002087static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002088 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002089 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002090 case ISD::SETONE:
2091 case ISD::SETOLE:
2092 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002093 llvm_unreachable("Should be lowered by legalize!");
2094 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002095 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002096 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002097 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002098 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002099 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002100 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002101 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002102 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002103 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002104 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002105 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002106 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002107 case ISD::SETO: return PPC::PRED_NU;
2108 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002109 // These two are invalid for floating point. Assume we have int.
2110 case ISD::SETULT: return PPC::PRED_LT;
2111 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002112 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002113}
2114
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002115/// getCRIdxForSetCC - Return the index of the condition register field
2116/// associated with the SetCC condition, and whether or not the field is
2117/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002118static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002119 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002120 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002121 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002122 case ISD::SETOLT:
2123 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2124 case ISD::SETOGT:
2125 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2126 case ISD::SETOEQ:
2127 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2128 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002129 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002130 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002131 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002132 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002133 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002134 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2135 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002136 case ISD::SETUEQ:
2137 case ISD::SETOGE:
2138 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002139 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002140 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002141 // These are invalid for floating point. Assume integer.
2142 case ISD::SETULT: return 0;
2143 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002144 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002145}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002146
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002147// getVCmpInst: return the vector compare instruction for the specified
2148// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002149// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002150static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2151 bool HasVSX, bool &Swap, bool &Negate) {
2152 Swap = false;
2153 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002154
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002155 if (VecVT.isFloatingPoint()) {
2156 /* Handle some cases by swapping input operands. */
2157 switch (CC) {
2158 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2159 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2160 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2161 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2162 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2163 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2164 default: break;
2165 }
2166 /* Handle some cases by negating the result. */
2167 switch (CC) {
2168 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2169 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2170 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2171 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2172 default: break;
2173 }
2174 /* We have instructions implementing the remaining cases. */
2175 switch (CC) {
2176 case ISD::SETEQ:
2177 case ISD::SETOEQ:
2178 if (VecVT == MVT::v4f32)
2179 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2180 else if (VecVT == MVT::v2f64)
2181 return PPC::XVCMPEQDP;
2182 break;
2183 case ISD::SETGT:
2184 case ISD::SETOGT:
2185 if (VecVT == MVT::v4f32)
2186 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2187 else if (VecVT == MVT::v2f64)
2188 return PPC::XVCMPGTDP;
2189 break;
2190 case ISD::SETGE:
2191 case ISD::SETOGE:
2192 if (VecVT == MVT::v4f32)
2193 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2194 else if (VecVT == MVT::v2f64)
2195 return PPC::XVCMPGEDP;
2196 break;
2197 default:
2198 break;
2199 }
2200 llvm_unreachable("Invalid floating-point vector compare condition");
2201 } else {
2202 /* Handle some cases by swapping input operands. */
2203 switch (CC) {
2204 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2205 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2206 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2207 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2208 default: break;
2209 }
2210 /* Handle some cases by negating the result. */
2211 switch (CC) {
2212 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2213 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2214 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2215 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2216 default: break;
2217 }
2218 /* We have instructions implementing the remaining cases. */
2219 switch (CC) {
2220 case ISD::SETEQ:
2221 case ISD::SETUEQ:
2222 if (VecVT == MVT::v16i8)
2223 return PPC::VCMPEQUB;
2224 else if (VecVT == MVT::v8i16)
2225 return PPC::VCMPEQUH;
2226 else if (VecVT == MVT::v4i32)
2227 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002228 else if (VecVT == MVT::v2i64)
2229 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002230 break;
2231 case ISD::SETGT:
2232 if (VecVT == MVT::v16i8)
2233 return PPC::VCMPGTSB;
2234 else if (VecVT == MVT::v8i16)
2235 return PPC::VCMPGTSH;
2236 else if (VecVT == MVT::v4i32)
2237 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002238 else if (VecVT == MVT::v2i64)
2239 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002240 break;
2241 case ISD::SETUGT:
2242 if (VecVT == MVT::v16i8)
2243 return PPC::VCMPGTUB;
2244 else if (VecVT == MVT::v8i16)
2245 return PPC::VCMPGTUH;
2246 else if (VecVT == MVT::v4i32)
2247 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002248 else if (VecVT == MVT::v2i64)
2249 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002250 break;
2251 default:
2252 break;
2253 }
2254 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002255 }
2256}
2257
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002258SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002259 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002260 unsigned Imm;
2261 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002262 EVT PtrVT =
2263 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002264 bool isPPC64 = (PtrVT == MVT::i64);
2265
Eric Christopher1b8e7632014-05-22 01:07:24 +00002266 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002267 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002268 // We can codegen setcc op, imm very efficiently compared to a brcond.
2269 // Check for those cases here.
2270 // setcc op, 0
2271 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002272 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002273 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002274 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002275 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002276 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002277 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
2278 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002279 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002280 }
Chris Lattnere2969492005-10-21 21:17:10 +00002281 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002282 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002283 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002284 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002285 Op, getI32Imm(~0U, dl)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002286 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002287 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002288 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002289 case ISD::SETLT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002290 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2291 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002292 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002293 }
Chris Lattnere2969492005-10-21 21:17:10 +00002294 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002295 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002296 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2297 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002298 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
2299 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002300 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002301 }
2302 }
Chris Lattner491b8292005-10-06 19:03:35 +00002303 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002304 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002305 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002306 default: break;
2307 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002308 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002309 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 Op, getI32Imm(1, dl)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002311 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2312 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +00002313 MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002314 getI32Imm(0, dl)),
2315 0), Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002316 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002317 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002318 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002319 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002320 Op, getI32Imm(~0U, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00002321 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002322 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +00002323 }
Chris Lattnere2969492005-10-21 21:17:10 +00002324 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002325 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002327 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2328 Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002329 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
2330 getI32Imm(31, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002331 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002332 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002333 case ISD::SETGT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002334 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2335 getI32Imm(31, dl) };
2336 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002337 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002338 getI32Imm(1, dl));
Chris Lattnere2969492005-10-21 21:17:10 +00002339 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002340 }
Chris Lattner491b8292005-10-06 19:03:35 +00002341 }
2342 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002343
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002344 SDValue LHS = N->getOperand(0);
2345 SDValue RHS = N->getOperand(1);
2346
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002347 // Altivec Vector compare instructions do not set any CR register by default and
2348 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002349 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002350 if (PPCSubTarget->hasQPX())
2351 return nullptr;
2352
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002353 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002354 bool Swap, Negate;
2355 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2356 PPCSubTarget->hasVSX(), Swap, Negate);
2357 if (Swap)
2358 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002359
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002360 EVT ResVT = VecVT.changeVectorElementTypeToInteger();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002361 if (Negate) {
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002362 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0);
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002363 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
2364 PPC::VNOR,
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002365 ResVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002366 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002367
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002368 return CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002369 }
2370
Eric Christopher1b8e7632014-05-22 01:07:24 +00002371 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +00002372 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +00002373
Chris Lattner491b8292005-10-06 19:03:35 +00002374 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002375 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002376 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002377 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002378
Chris Lattner491b8292005-10-06 19:03:35 +00002379 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002380 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002381
Craig Topper062a2ba2014-04-25 05:30:21 +00002382 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002383 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002384 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002385
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002386 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2387 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002388
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002389 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
2390 getI32Imm(31, dl), getI32Imm(31, dl) };
Ulrich Weigand47e93282013-07-03 15:13:30 +00002391 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +00002392 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +00002393
2394 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002395 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002396 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002397 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
Chris Lattner491b8292005-10-06 19:03:35 +00002398}
Chris Lattner502a3692005-10-06 18:56:10 +00002399
Hal Finkelcf599212015-02-25 21:36:59 +00002400SDNode *PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
2401 // Transfer memoperands.
2402 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2403 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2404 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2405 return Result;
2406}
2407
Chris Lattner318622f2005-10-06 19:07:45 +00002408
Chris Lattner43ff01e2005-08-17 19:33:03 +00002409// Select - Convert the specified operand from a target-independent to a
2410// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002411SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002412 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002413 if (N->isMachineOpcode()) {
2414 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002415 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002416 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002417
Hal Finkel51b3fd12014-09-02 06:23:54 +00002418 // In case any misguided DAG-level optimizations form an ADD with a
2419 // TargetConstant operand, crash here instead of miscompiling (by selecting
2420 // an r+r add instead of some kind of r+i add).
2421 if (N->getOpcode() == ISD::ADD &&
2422 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2423 llvm_unreachable("Invalid ADD with TargetConstant operand");
2424
Hal Finkel8adf2252014-12-16 05:51:41 +00002425 // Try matching complex bit permutations before doing anything else.
2426 if (SDNode *NN = SelectBitPermutation(N))
2427 return NN;
2428
Chris Lattner43ff01e2005-08-17 19:33:03 +00002429 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002430 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002431
Jim Laskey095e6f32006-12-12 13:23:43 +00002432 case ISD::Constant: {
Hal Finkelc58ce412015-01-01 02:53:29 +00002433 if (N->getValueType(0) == MVT::i64)
2434 return SelectInt64(CurDAG, N);
Jim Laskey095e6f32006-12-12 13:23:43 +00002435 break;
2436 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002437
Hal Finkel940ab932014-02-28 00:27:01 +00002438 case ISD::SETCC: {
2439 SDNode *SN = SelectSETCC(N);
2440 if (SN)
2441 return SN;
2442 break;
2443 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002444 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00002445 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00002446
Hal Finkelb5e9b042014-12-11 22:51:06 +00002447 case ISD::FrameIndex:
2448 return getFrameIndex(N, N);
Chris Lattner6961fc72006-03-26 10:06:40 +00002449
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002450 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002451 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002452 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2453 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00002454 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002455
Hal Finkelbbdee932014-12-02 22:01:00 +00002456 case PPCISD::READ_TIME_BASE: {
2457 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2458 MVT::Other, N->getOperand(0));
2459 }
2460
Hal Finkel13d104b2014-12-11 18:37:52 +00002461 case PPCISD::SRA_ADDZE: {
2462 SDValue N0 = N->getOperand(0);
2463 SDValue ShiftAmt =
2464 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002465 getConstantIntValue(), dl,
2466 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00002467 if (N->getValueType(0) == MVT::i64) {
2468 SDNode *Op =
2469 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2470 N0, ShiftAmt);
2471 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64,
2472 SDValue(Op, 0), SDValue(Op, 1));
2473 } else {
2474 assert(N->getValueType(0) == MVT::i32 &&
2475 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2476 SDNode *Op =
2477 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2478 N0, ShiftAmt);
2479 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2480 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00002481 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002482 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002483
Chris Lattnerce645542006-11-10 02:08:47 +00002484 case ISD::LOAD: {
2485 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002486 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002487 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002488
Chris Lattnerce645542006-11-10 02:08:47 +00002489 // Normal loads are handled by code generated from the .td file.
2490 if (LD->getAddressingMode() != ISD::PRE_INC)
2491 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002492
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002493 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002494 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002495 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002496
Chris Lattner474b5b72006-11-15 19:55:13 +00002497 unsigned Opcode;
2498 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002499 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002500 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002501 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2502 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002503 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002504 case MVT::f64: Opcode = PPC::LFDU; break;
2505 case MVT::f32: Opcode = PPC::LFSU; break;
2506 case MVT::i32: Opcode = PPC::LWZU; break;
2507 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2508 case MVT::i1:
2509 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002510 }
2511 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002512 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2513 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2514 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002515 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002516 case MVT::i64: Opcode = PPC::LDU; break;
2517 case MVT::i32: Opcode = PPC::LWZU8; break;
2518 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2519 case MVT::i1:
2520 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002521 }
2522 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002523
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002524 SDValue Chain = LD->getChain();
2525 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002526 SDValue Ops[] = { Offset, Base, Chain };
Mehdi Amini44ede332015-07-09 02:09:04 +00002527 return transferMemOperands(
2528 N, CurDAG->getMachineNode(
2529 Opcode, dl, LD->getValueType(0),
2530 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other,
2531 Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002532 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002533 unsigned Opcode;
2534 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2535 if (LD->getValueType(0) != MVT::i64) {
2536 // Handle PPC32 integer and normal FP loads.
2537 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2538 switch (LoadedVT.getSimpleVT().SimpleTy) {
2539 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00002540 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
2541 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00002542 case MVT::f64: Opcode = PPC::LFDUX; break;
2543 case MVT::f32: Opcode = PPC::LFSUX; break;
2544 case MVT::i32: Opcode = PPC::LWZUX; break;
2545 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2546 case MVT::i1:
2547 case MVT::i8: Opcode = PPC::LBZUX; break;
2548 }
2549 } else {
2550 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2551 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2552 "Invalid sext update load");
2553 switch (LoadedVT.getSimpleVT().SimpleTy) {
2554 default: llvm_unreachable("Invalid PPC load type!");
2555 case MVT::i64: Opcode = PPC::LDUX; break;
2556 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2557 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2558 case MVT::i1:
2559 case MVT::i8: Opcode = PPC::LBZUX8; break;
2560 }
2561 }
2562
2563 SDValue Chain = LD->getChain();
2564 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002565 SDValue Ops[] = { Base, Offset, Chain };
Mehdi Amini44ede332015-07-09 02:09:04 +00002566 return transferMemOperands(
2567 N, CurDAG->getMachineNode(
2568 Opcode, dl, LD->getValueType(0),
2569 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other,
2570 Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002571 }
2572 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002573
Nate Begemanb3821a32005-08-18 07:30:46 +00002574 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002575 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002576 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002577
Nate Begemanb3821a32005-08-18 07:30:46 +00002578 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2579 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002580 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002581 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002582 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002583 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
2584 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002585 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00002586 }
Nate Begemand31efd12006-09-22 05:01:56 +00002587 // If this is just a masked value where the input is not handled above, and
2588 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2589 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002590 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002591 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002592 SDValue Val = N->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002593 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
2594 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002595 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00002596 }
Hal Finkele39526a2012-08-28 02:10:15 +00002597 // If this is a 64-bit zero-extension mask, emit rldicl.
2598 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2599 isMask_64(Imm64)) {
2600 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002601 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002602 SH = 0;
2603
2604 // If the operand is a logical right shift, we can fold it into this
2605 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2606 // for n <= mb. The right shift is really a left rotate followed by a
2607 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2608 // by the shift.
2609 if (Val.getOpcode() == ISD::SRL &&
2610 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2611 assert(Imm < 64 && "Illegal shift amount");
2612 Val = Val.getOperand(0);
2613 SH = 64 - Imm;
2614 }
2615
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002616 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002617 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00002618 }
Nate Begemand31efd12006-09-22 05:01:56 +00002619 // AND X, 0 -> 0, not "rlwinm 32".
2620 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002621 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00002622 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00002623 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002624 // ISD::OR doesn't get all the bitfield insertion fun.
Hal Finkelb1518d62015-09-05 00:02:59 +00002625 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
2626 // bitfield insert.
Andrew Trickc416ba62010-12-24 04:28:06 +00002627 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002628 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002629 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Hal Finkelb1518d62015-09-05 00:02:59 +00002630 // The idea here is to check whether this is equivalent to:
2631 // (c1 & m) | (x & ~m)
2632 // where m is a run-of-ones mask. The logic here is that, for each bit in
2633 // c1 and c2:
2634 // - if both are 1, then the output will be 1.
2635 // - if both are 0, then the output will be 0.
2636 // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
2637 // come from x.
2638 // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
2639 // be 0.
2640 // If that last condition is never the case, then we can form m from the
2641 // bits that are the same between c1 and c2.
Chris Lattner20c88df2006-01-05 18:32:49 +00002642 unsigned MB, ME;
Hal Finkelb1518d62015-09-05 00:02:59 +00002643 if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002644 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002645 N->getOperand(0).getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002646 getI32Imm(0, dl), getI32Imm(MB, dl),
2647 getI32Imm(ME, dl) };
Michael Liaob53d8962013-04-19 22:22:57 +00002648 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00002649 }
2650 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002651
Chris Lattner1de57062005-09-29 23:33:31 +00002652 // Other cases are autogenerated.
2653 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002654 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002655 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002656 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00002657 if (SDNode *I = SelectBitfieldInsert(N))
2658 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00002659
Hal Finkelb5e9b042014-12-11 22:51:06 +00002660 short Imm;
2661 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2662 isIntS16Immediate(N->getOperand(1), Imm)) {
2663 APInt LHSKnownZero, LHSKnownOne;
2664 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2665
2666 // If this is equivalent to an add, then we can fold it with the
2667 // FrameIndex calculation.
2668 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL)
2669 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2670 }
2671
Chris Lattner1de57062005-09-29 23:33:31 +00002672 // Other cases are autogenerated.
2673 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002674 }
2675 case ISD::ADD: {
2676 short Imm;
2677 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2678 isIntS16Immediate(N->getOperand(1), Imm))
2679 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2680
2681 break;
2682 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002683 case ISD::SHL: {
2684 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002685 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002686 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002687 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002688 getI32Imm(SH, dl), getI32Imm(MB, dl),
2689 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002690 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002691 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002692
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002693 // Other cases are autogenerated.
2694 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002695 }
2696 case ISD::SRL: {
2697 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002698 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002699 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002700 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002701 getI32Imm(SH, dl), getI32Imm(MB, dl),
2702 getI32Imm(ME, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002703 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002704 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002705
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002706 // Other cases are autogenerated.
2707 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002708 }
Hal Finkel940ab932014-02-28 00:27:01 +00002709 // FIXME: Remove this once the ANDI glue bug is fixed:
2710 case PPCISD::ANDIo_1_EQ_BIT:
2711 case PPCISD::ANDIo_1_GT_BIT: {
2712 if (!ANDIGlueBug)
2713 break;
2714
2715 EVT InVT = N->getOperand(0).getValueType();
2716 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2717 "Invalid input type for ANDIo_1_EQ_BIT");
2718
2719 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2720 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2721 N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002722 CurDAG->getTargetConstant(1, dl, InVT)),
2723 0);
Hal Finkel940ab932014-02-28 00:27:01 +00002724 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2725 SDValue SRIdxVal =
2726 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002727 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00002728
2729 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
2730 CR0Reg, SRIdxVal,
2731 SDValue(AndI.getNode(), 1) /* glue */);
2732 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002733 case ISD::SELECT_CC: {
2734 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002735 EVT PtrVT =
2736 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002737 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002738
Hal Finkel940ab932014-02-28 00:27:01 +00002739 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002740 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002741 N->getOperand(0).getValueType() == MVT::i1)
2742 break;
2743
Chris Lattner97b3da12006-06-27 00:04:13 +00002744 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002745 if (!isPPC64)
2746 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2747 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2748 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2749 if (N1C->isNullValue() && N3C->isNullValue() &&
2750 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2751 // FIXME: Implement this optzn for PPC64.
2752 N->getValueType(0) == MVT::i32) {
2753 SDNode *Tmp =
2754 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 N->getOperand(0), getI32Imm(~0U, dl));
Roman Divacky254f8212011-06-20 15:28:39 +00002756 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
2757 SDValue(Tmp, 0), N->getOperand(0),
2758 SDValue(Tmp, 1));
2759 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002760
Dale Johannesenab8e4422009-02-06 19:16:40 +00002761 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002762
2763 if (N->getValueType(0) == MVT::i1) {
2764 // An i1 select is: (c & t) | (!c & f).
2765 bool Inv;
2766 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2767
2768 unsigned SRI;
2769 switch (Idx) {
2770 default: llvm_unreachable("Invalid CC index");
2771 case 0: SRI = PPC::sub_lt; break;
2772 case 1: SRI = PPC::sub_gt; break;
2773 case 2: SRI = PPC::sub_eq; break;
2774 case 3: SRI = PPC::sub_un; break;
2775 }
2776
2777 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2778
2779 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2780 CCBit, CCBit), 0);
2781 SDValue C = Inv ? NotCCBit : CCBit,
2782 NotC = Inv ? CCBit : NotCCBit;
2783
2784 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2785 C, N->getOperand(2)), 0);
2786 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2787 NotC, N->getOperand(3)), 0);
2788
2789 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2790 }
2791
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002792 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002793
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002794 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002795 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002796 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002797 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002798 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002799 else if (N->getValueType(0) == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00002800 if (PPCSubTarget->hasP8Vector())
2801 SelectCCOp = PPC::SELECT_CC_VSSRC;
2802 else
2803 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002804 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002805 if (PPCSubTarget->hasVSX())
2806 SelectCCOp = PPC::SELECT_CC_VSFRC;
2807 else
2808 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002809 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
2810 SelectCCOp = PPC::SELECT_CC_QFRC;
2811 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
2812 SelectCCOp = PPC::SELECT_CC_QSRC;
2813 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
2814 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00002815 else if (N->getValueType(0) == MVT::v2f64 ||
2816 N->getValueType(0) == MVT::v2i64)
2817 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002818 else
2819 SelectCCOp = PPC::SELECT_CC_VRRC;
2820
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002821 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002822 getI32Imm(BROpc, dl) };
Craig Topper481fb282014-04-27 19:21:11 +00002823 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00002824 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002825 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002826 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002827 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002828 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00002829 }
2830
2831 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002832 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002833 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002834 N->getValueType(0) == MVT::v2i64)) {
2835 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Kyle Butt015f4fc2015-12-02 18:53:33 +00002836
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002837 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2838 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2839 unsigned DM[2];
2840
2841 for (int i = 0; i < 2; ++i)
2842 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2843 DM[i] = 0;
2844 else
2845 DM[i] = 1;
2846
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002847 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2848 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2849 isa<LoadSDNode>(Op1.getOperand(0))) {
2850 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2851 SDValue Base, Offset;
2852
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +00002853 if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() &&
Bill Schmidt048cc972015-10-14 20:45:00 +00002854 (LD->getMemoryVT() == MVT::f64 ||
2855 LD->getMemoryVT() == MVT::i64) &&
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002856 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2857 SDValue Chain = LD->getChain();
2858 SDValue Ops[] = { Base, Offset, Chain };
2859 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00002860 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002861 }
2862 }
2863
Bill Schmidtae94f112015-07-01 19:40:07 +00002864 // For little endian, we must swap the input operands and adjust
2865 // the mask elements (reverse and invert them).
2866 if (PPCSubTarget->isLittleEndian()) {
2867 std::swap(Op1, Op2);
2868 unsigned tmp = DM[0];
2869 DM[0] = 1 - DM[1];
2870 DM[1] = 1 - tmp;
2871 }
2872
2873 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
2874 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002875 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00002876 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002877 }
2878
2879 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002880 case PPCISD::BDNZ:
2881 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002882 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002883 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
2884 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
2885 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
2886 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00002887 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00002888 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002889 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002890 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002891 // Op #1 is the PPC::PRED_* number.
2892 // Op #2 is the CR#
2893 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002894 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002895 // Prevent PPC::PRED_* from being selected into LI.
Hal Finkel65539e32015-12-12 00:32:00 +00002896 unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
2897 if (EnableBranchHint)
2898 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3));
2899
2900 SDValue Pred = getI32Imm(PCC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002901 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002902 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00002903 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002904 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002905 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002906 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002907 unsigned PCC = getPredicateForSetCC(CC);
2908
2909 if (N->getOperand(2).getValueType() == MVT::i1) {
2910 unsigned Opc;
2911 bool Swap;
2912 switch (PCC) {
2913 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2914 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2915 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2916 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2917 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2918 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2919 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2920 }
2921
2922 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2923 N->getOperand(Swap ? 3 : 2),
2924 N->getOperand(Swap ? 2 : 3)), 0);
2925 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
2926 BitComp, N->getOperand(4), N->getOperand(0));
2927 }
2928
Hal Finkel65539e32015-12-12 00:32:00 +00002929 if (EnableBranchHint)
2930 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4));
2931
Dale Johannesenab8e4422009-02-06 19:16:40 +00002932 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002933 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002934 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002935 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002936 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002937 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002938 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002939 SDValue Chain = N->getOperand(0);
2940 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002941 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002942 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002943 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002944 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00002945 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002946 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002947 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002948 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2949 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002950 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2951 SDValue GA = N->getOperand(0);
Hal Finkelcf599212015-02-25 21:36:59 +00002952 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LWZtoc, dl,
2953 MVT::i32, GA, N->getOperand(1)));
Justin Hibbits3476db42014-08-28 04:40:55 +00002954 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002955
Bill Schmidt27917782013-02-21 17:12:27 +00002956 // For medium and large code model, we generate two instructions as
2957 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002958 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00002959 CodeModel::Model CModel = TM.getCodeModel();
2960 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002961 break;
2962
Bill Schmidt5d82f092014-06-16 21:36:02 +00002963 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
Eric Christopherc1808362015-11-20 20:51:31 +00002964 // If it must be toc-referenced according to PPCSubTarget, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00002965 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
2966 // Otherwise we generate:
2967 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
2968 SDValue GA = N->getOperand(0);
2969 SDValue TOCbase = N->getOperand(1);
2970 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00002971 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00002972
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002973 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
2974 CModel == CodeModel::Large)
Hal Finkelcf599212015-02-25 21:36:59 +00002975 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2976 MVT::i64, GA, SDValue(Tmp, 0)));
Bill Schmidt34627e32012-11-27 17:35:46 +00002977
2978 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
Eric Christopherc1808362015-11-20 20:51:31 +00002979 const GlobalValue *GV = G->getGlobal();
2980 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
2981 if (GVFlags & PPCII::MO_NLP_FLAG) {
Hal Finkelcf599212015-02-25 21:36:59 +00002982 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2983 MVT::i64, GA, SDValue(Tmp, 0)));
Eric Christopherc1808362015-11-20 20:51:31 +00002984 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002985 }
2986
2987 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
2988 SDValue(Tmp, 0), GA);
2989 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00002990 case PPCISD::PPC32_PICGOT: {
2991 // Generate a PIC-safe GOT reference.
2992 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
2993 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
Mehdi Amini44ede332015-07-09 02:09:04 +00002994 return CurDAG->SelectNodeTo(
2995 N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(CurDAG->getDataLayout()),
2996 MVT::i32);
Hal Finkel7c8ae532014-07-25 17:47:22 +00002997 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002998 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002999 // This expands into one of three sequences, depending on whether
3000 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00003001 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
3002 isa<ConstantSDNode>(N->getOperand(1)) &&
3003 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003004
3005 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00003006 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003007 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00003008 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003009
Bill Schmidt51e79512013-02-20 15:50:31 +00003010 if (EltSize == 1) {
3011 Opc1 = PPC::VSPLTISB;
3012 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003013 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003014 VT = MVT::v16i8;
3015 } else if (EltSize == 2) {
3016 Opc1 = PPC::VSPLTISH;
3017 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003018 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003019 VT = MVT::v8i16;
3020 } else {
3021 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
3022 Opc1 = PPC::VSPLTISW;
3023 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003024 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003025 VT = MVT::v4i32;
3026 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003027
3028 if ((Elt & 1) == 0) {
3029 // Elt is even, in the range [-32,-18] + [16,30].
3030 //
3031 // Convert: VADD_SPLAT elt, size
3032 // Into: tmp = VSPLTIS[BHW] elt
3033 // VADDU[BHW]M tmp, tmp
3034 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003035 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003036 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3037 SDValue TmpVal = SDValue(Tmp, 0);
3038 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
3039
3040 } else if (Elt > 0) {
3041 // Elt is odd and positive, in the range [17,31].
3042 //
3043 // Convert: VADD_SPLAT elt, size
3044 // Into: tmp1 = VSPLTIS[BHW] elt-16
3045 // tmp2 = VSPLTIS[BHW] -16
3046 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003047 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003048 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003049 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003050 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3051 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
3052 SDValue(Tmp2, 0));
3053
3054 } else {
3055 // Elt is odd and negative, in the range [-31,-17].
3056 //
3057 // Convert: VADD_SPLAT elt, size
3058 // Into: tmp1 = VSPLTIS[BHW] elt+16
3059 // tmp2 = VSPLTIS[BHW] -16
3060 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003061 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003062 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003063 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003064 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3065 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
3066 SDValue(Tmp2, 0));
3067 }
Bill Schmidt51e79512013-02-20 15:50:31 +00003068 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00003069 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003070
Dan Gohmanea6f91f2010-01-05 01:24:18 +00003071 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00003072}
3073
Hal Finkel4edc66b2015-01-03 01:16:37 +00003074// If the target supports the cmpb instruction, do the idiom recognition here.
3075// We don't do this as a DAG combine because we don't want to do it as nodes
3076// are being combined (because we might miss part of the eventual idiom). We
3077// don't want to do it during instruction selection because we want to reuse
3078// the logic for lowering the masking operations already part of the
3079// instruction selector.
3080SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3081 SDLoc dl(N);
3082
3083 assert(N->getOpcode() == ISD::OR &&
3084 "Only OR nodes are supported for CMPB");
3085
3086 SDValue Res;
3087 if (!PPCSubTarget->hasCMPB())
3088 return Res;
3089
3090 if (N->getValueType(0) != MVT::i32 &&
3091 N->getValueType(0) != MVT::i64)
3092 return Res;
3093
3094 EVT VT = N->getValueType(0);
3095
3096 SDValue RHS, LHS;
3097 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
3098 uint64_t Mask = 0, Alt = 0;
3099
3100 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3101 uint64_t &Mask, uint64_t &Alt,
3102 SDValue &LHS, SDValue &RHS) {
3103 if (O.getOpcode() != ISD::SELECT_CC)
3104 return false;
3105 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3106
3107 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3108 !isa<ConstantSDNode>(O.getOperand(3)))
3109 return false;
3110
3111 uint64_t PM = O.getConstantOperandVal(2);
3112 uint64_t PAlt = O.getConstantOperandVal(3);
3113 for (b = 0; b < 8; ++b) {
3114 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3115 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3116 break;
3117 }
3118
3119 if (b == 8)
3120 return false;
3121 Mask |= PM;
3122 Alt |= PAlt;
3123
3124 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3125 O.getConstantOperandVal(1) != 0) {
3126 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3127 if (Op0.getOpcode() == ISD::TRUNCATE)
3128 Op0 = Op0.getOperand(0);
3129 if (Op1.getOpcode() == ISD::TRUNCATE)
3130 Op1 = Op1.getOperand(0);
3131
3132 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3133 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3134 isa<ConstantSDNode>(Op0.getOperand(1))) {
3135
3136 unsigned Bits = Op0.getValueType().getSizeInBits();
3137 if (b != Bits/8-1)
3138 return false;
3139 if (Op0.getConstantOperandVal(1) != Bits-8)
3140 return false;
3141
3142 LHS = Op0.getOperand(0);
3143 RHS = Op1.getOperand(0);
3144 return true;
3145 }
3146
3147 // When we have small integers (i16 to be specific), the form present
3148 // post-legalization uses SETULT in the SELECT_CC for the
3149 // higher-order byte, depending on the fact that the
3150 // even-higher-order bytes are known to all be zero, for example:
3151 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3152 // (so when the second byte is the same, because all higher-order
3153 // bits from bytes 3 and 4 are known to be zero, the result of the
3154 // xor can be at most 255)
3155 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3156 isa<ConstantSDNode>(O.getOperand(1))) {
3157
3158 uint64_t ULim = O.getConstantOperandVal(1);
3159 if (ULim != (UINT64_C(1) << b*8))
3160 return false;
3161
3162 // Now we need to make sure that the upper bytes are known to be
3163 // zero.
3164 unsigned Bits = Op0.getValueType().getSizeInBits();
3165 if (!CurDAG->MaskedValueIsZero(Op0,
3166 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
3167 return false;
Kyle Butt015f4fc2015-12-02 18:53:33 +00003168
Hal Finkel4edc66b2015-01-03 01:16:37 +00003169 LHS = Op0.getOperand(0);
3170 RHS = Op0.getOperand(1);
3171 return true;
3172 }
3173
3174 return false;
3175 }
3176
3177 if (CC != ISD::SETEQ)
3178 return false;
3179
3180 SDValue Op = O.getOperand(0);
3181 if (Op.getOpcode() == ISD::AND) {
3182 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3183 return false;
3184 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3185 return false;
3186
3187 SDValue XOR = Op.getOperand(0);
3188 if (XOR.getOpcode() == ISD::TRUNCATE)
3189 XOR = XOR.getOperand(0);
3190 if (XOR.getOpcode() != ISD::XOR)
3191 return false;
3192
3193 LHS = XOR.getOperand(0);
3194 RHS = XOR.getOperand(1);
3195 return true;
3196 } else if (Op.getOpcode() == ISD::SRL) {
3197 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3198 return false;
3199 unsigned Bits = Op.getValueType().getSizeInBits();
3200 if (b != Bits/8-1)
3201 return false;
3202 if (Op.getConstantOperandVal(1) != Bits-8)
3203 return false;
3204
3205 SDValue XOR = Op.getOperand(0);
3206 if (XOR.getOpcode() == ISD::TRUNCATE)
3207 XOR = XOR.getOperand(0);
3208 if (XOR.getOpcode() != ISD::XOR)
3209 return false;
3210
3211 LHS = XOR.getOperand(0);
3212 RHS = XOR.getOperand(1);
3213 return true;
3214 }
3215
3216 return false;
3217 };
3218
3219 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3220 while (!Queue.empty()) {
3221 SDValue V = Queue.pop_back_val();
3222
3223 for (const SDValue &O : V.getNode()->ops()) {
3224 unsigned b;
3225 uint64_t M = 0, A = 0;
3226 SDValue OLHS, ORHS;
3227 if (O.getOpcode() == ISD::OR) {
3228 Queue.push_back(O);
3229 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3230 if (!LHS) {
3231 LHS = OLHS;
3232 RHS = ORHS;
3233 BytesFound[b] = true;
3234 Mask |= M;
3235 Alt |= A;
3236 } else if ((LHS == ORHS && RHS == OLHS) ||
3237 (RHS == ORHS && LHS == OLHS)) {
3238 BytesFound[b] = true;
3239 Mask |= M;
3240 Alt |= A;
3241 } else {
3242 return Res;
3243 }
3244 } else {
3245 return Res;
3246 }
3247 }
3248 }
3249
3250 unsigned LastB = 0, BCnt = 0;
3251 for (unsigned i = 0; i < 8; ++i)
3252 if (BytesFound[LastB]) {
3253 ++BCnt;
3254 LastB = i;
3255 }
3256
3257 if (!LastB || BCnt < 2)
3258 return Res;
3259
3260 // Because we'll be zero-extending the output anyway if don't have a specific
3261 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3262 if (LHS.getValueType() != VT) {
3263 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3264 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3265 }
3266
3267 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3268
3269 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3270 if (NonTrivialMask && !Alt) {
3271 // Res = Mask & CMPB
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003272 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3273 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003274 } else if (Alt) {
3275 // Res = (CMPB & Mask) | (~CMPB & Alt)
3276 // Which, as suggested here:
3277 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3278 // can be written as:
3279 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3280 // useful because the (Alt ^ Mask) can be pre-computed.
3281 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003282 CurDAG->getConstant(Mask ^ Alt, dl, VT));
3283 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
3284 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003285 }
3286
3287 return Res;
3288}
3289
Hal Finkel200d2ad2015-01-05 21:10:24 +00003290// When CR bit registers are enabled, an extension of an i1 variable to a i32
3291// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3292// involves constant materialization of a 0 or a 1 or both. If the result of
3293// the extension is then operated upon by some operator that can be constant
3294// folded with a constant 0 or 1, and that constant can be materialized using
3295// only one instruction (like a zero or one), then we should fold in those
3296// operations with the select.
3297void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3298 if (!PPCSubTarget->useCRBits())
3299 return;
3300
3301 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3302 N->getOpcode() != ISD::SIGN_EXTEND &&
3303 N->getOpcode() != ISD::ANY_EXTEND)
3304 return;
3305
3306 if (N->getOperand(0).getValueType() != MVT::i1)
3307 return;
3308
3309 if (!N->hasOneUse())
3310 return;
3311
3312 SDLoc dl(N);
3313 EVT VT = N->getValueType(0);
3314 SDValue Cond = N->getOperand(0);
3315 SDValue ConstTrue =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003316 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
3317 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00003318
3319 do {
3320 SDNode *User = *N->use_begin();
3321 if (User->getNumOperands() != 2)
3322 break;
3323
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00003325 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3326 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3327 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3328
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003329 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00003330 User->getValueType(0),
3331 O0.getNode(), O1.getNode());
3332 };
3333
3334 SDValue TrueRes = TryFold(ConstTrue);
3335 if (!TrueRes)
3336 break;
3337 SDValue FalseRes = TryFold(ConstFalse);
3338 if (!FalseRes)
3339 break;
3340
3341 // For us to materialize these using one instruction, we must be able to
3342 // represent them as signed 16-bit integers.
3343 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
3344 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
3345 if (!isInt<16>(True) || !isInt<16>(False))
3346 break;
3347
3348 // We can replace User with a new SELECT node, and try again to see if we
3349 // can fold the select with its user.
3350 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
3351 N = User;
3352 ConstTrue = TrueRes;
3353 ConstFalse = FalseRes;
3354 } while (N->hasOneUse());
3355}
3356
Hal Finkel4edc66b2015-01-03 01:16:37 +00003357void PPCDAGToDAGISel::PreprocessISelDAG() {
3358 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3359 ++Position;
3360
3361 bool MadeChange = false;
3362 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00003363 SDNode *N = &*--Position;
Hal Finkel4edc66b2015-01-03 01:16:37 +00003364 if (N->use_empty())
3365 continue;
3366
3367 SDValue Res;
3368 switch (N->getOpcode()) {
3369 default: break;
3370 case ISD::OR:
3371 Res = combineToCMPB(N);
3372 break;
3373 }
3374
Hal Finkel200d2ad2015-01-05 21:10:24 +00003375 if (!Res)
3376 foldBoolExts(Res, N);
3377
Hal Finkel4edc66b2015-01-03 01:16:37 +00003378 if (Res) {
3379 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3380 DEBUG(N->dump(CurDAG));
3381 DEBUG(dbgs() << "\nNew: ");
3382 DEBUG(Res.getNode()->dump(CurDAG));
3383 DEBUG(dbgs() << "\n");
3384
3385 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3386 MadeChange = true;
3387 }
3388 }
3389
3390 if (MadeChange)
3391 CurDAG->RemoveDeadNodes();
3392}
3393
Hal Finkel860fa902014-01-02 22:09:39 +00003394/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003395/// on the DAG representation.
3396void PPCDAGToDAGISel::PostprocessISelDAG() {
3397
3398 // Skip peepholes at -O0.
3399 if (TM.getOptLevel() == CodeGenOpt::None)
3400 return;
3401
Hal Finkel940ab932014-02-28 00:27:01 +00003402 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003403 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003404 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003405}
3406
Hal Finkelb9989152014-02-28 06:11:16 +00003407// Check if all users of this node will become isel where the second operand
3408// is the constant zero. If this is so, and if we can negate the condition,
3409// then we can flip the true and false operands. This will allow the zero to
3410// be folded with the isel so that we don't need to materialize a register
3411// containing zero.
3412bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3413 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003414 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003415 return false;
3416
3417 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3418 UI != UE; ++UI) {
3419 SDNode *User = *UI;
3420 if (!User->isMachineOpcode())
3421 return false;
3422 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3423 User->getMachineOpcode() != PPC::SELECT_I8)
3424 return false;
3425
3426 SDNode *Op2 = User->getOperand(2).getNode();
3427 if (!Op2->isMachineOpcode())
3428 return false;
3429
3430 if (Op2->getMachineOpcode() != PPC::LI &&
3431 Op2->getMachineOpcode() != PPC::LI8)
3432 return false;
3433
3434 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3435 if (!C)
3436 return false;
3437
3438 if (!C->isNullValue())
3439 return false;
3440 }
3441
3442 return true;
3443}
3444
3445void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3446 SmallVector<SDNode *, 4> ToReplace;
3447 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3448 UI != UE; ++UI) {
3449 SDNode *User = *UI;
3450 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3451 User->getMachineOpcode() == PPC::SELECT_I8) &&
3452 "Must have all select users");
3453 ToReplace.push_back(User);
3454 }
3455
3456 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3457 UE = ToReplace.end(); UI != UE; ++UI) {
3458 SDNode *User = *UI;
3459 SDNode *ResNode =
3460 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3461 User->getValueType(0), User->getOperand(0),
3462 User->getOperand(2),
3463 User->getOperand(1));
3464
3465 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3466 DEBUG(User->dump(CurDAG));
3467 DEBUG(dbgs() << "\nNew: ");
3468 DEBUG(ResNode->dump(CurDAG));
3469 DEBUG(dbgs() << "\n");
3470
3471 ReplaceUses(User, ResNode);
3472 }
3473}
3474
Eric Christopher02e18042014-05-14 00:31:15 +00003475void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003476 bool IsModified;
3477 do {
3478 IsModified = false;
Pete Cooper65c69402015-07-14 22:10:54 +00003479 for (SDNode &Node : CurDAG->allnodes()) {
3480 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node);
Hal Finkel940ab932014-02-28 00:27:01 +00003481 if (!MachineNode || MachineNode->use_empty())
3482 continue;
3483 SDNode *ResNode = MachineNode;
3484
3485 bool Op1Set = false, Op1Unset = false,
3486 Op1Not = false,
3487 Op2Set = false, Op2Unset = false,
3488 Op2Not = false;
3489
3490 unsigned Opcode = MachineNode->getMachineOpcode();
3491 switch (Opcode) {
3492 default: break;
3493 case PPC::CRAND:
3494 case PPC::CRNAND:
3495 case PPC::CROR:
3496 case PPC::CRXOR:
3497 case PPC::CRNOR:
3498 case PPC::CREQV:
3499 case PPC::CRANDC:
3500 case PPC::CRORC: {
3501 SDValue Op = MachineNode->getOperand(1);
3502 if (Op.isMachineOpcode()) {
3503 if (Op.getMachineOpcode() == PPC::CRSET)
3504 Op2Set = true;
3505 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3506 Op2Unset = true;
3507 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3508 Op.getOperand(0) == Op.getOperand(1))
3509 Op2Not = true;
3510 }
3511 } // fallthrough
3512 case PPC::BC:
3513 case PPC::BCn:
3514 case PPC::SELECT_I4:
3515 case PPC::SELECT_I8:
3516 case PPC::SELECT_F4:
3517 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003518 case PPC::SELECT_QFRC:
3519 case PPC::SELECT_QSRC:
3520 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003521 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003522 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003523 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003524 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003525 SDValue Op = MachineNode->getOperand(0);
3526 if (Op.isMachineOpcode()) {
3527 if (Op.getMachineOpcode() == PPC::CRSET)
3528 Op1Set = true;
3529 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3530 Op1Unset = true;
3531 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3532 Op.getOperand(0) == Op.getOperand(1))
3533 Op1Not = true;
3534 }
3535 }
3536 break;
3537 }
3538
Hal Finkelb9989152014-02-28 06:11:16 +00003539 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003540 switch (Opcode) {
3541 default: break;
3542 case PPC::CRAND:
3543 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3544 // x & x = x
3545 ResNode = MachineNode->getOperand(0).getNode();
3546 else if (Op1Set)
3547 // 1 & y = y
3548 ResNode = MachineNode->getOperand(1).getNode();
3549 else if (Op2Set)
3550 // x & 1 = x
3551 ResNode = MachineNode->getOperand(0).getNode();
3552 else if (Op1Unset || Op2Unset)
3553 // x & 0 = 0 & y = 0
3554 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3555 MVT::i1);
3556 else if (Op1Not)
3557 // ~x & y = andc(y, x)
3558 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3559 MVT::i1, MachineNode->getOperand(1),
3560 MachineNode->getOperand(0).
3561 getOperand(0));
3562 else if (Op2Not)
3563 // x & ~y = andc(x, y)
3564 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3565 MVT::i1, MachineNode->getOperand(0),
3566 MachineNode->getOperand(1).
3567 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003568 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003569 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3570 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003571 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003572 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003573 }
Hal Finkel940ab932014-02-28 00:27:01 +00003574 break;
3575 case PPC::CRNAND:
3576 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3577 // nand(x, x) -> nor(x, x)
3578 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3579 MVT::i1, MachineNode->getOperand(0),
3580 MachineNode->getOperand(0));
3581 else if (Op1Set)
3582 // nand(1, y) -> nor(y, y)
3583 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3584 MVT::i1, MachineNode->getOperand(1),
3585 MachineNode->getOperand(1));
3586 else if (Op2Set)
3587 // nand(x, 1) -> nor(x, x)
3588 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3589 MVT::i1, MachineNode->getOperand(0),
3590 MachineNode->getOperand(0));
3591 else if (Op1Unset || Op2Unset)
3592 // nand(x, 0) = nand(0, y) = 1
3593 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3594 MVT::i1);
3595 else if (Op1Not)
3596 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3597 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3598 MVT::i1, MachineNode->getOperand(0).
3599 getOperand(0),
3600 MachineNode->getOperand(1));
3601 else if (Op2Not)
3602 // nand(x, ~y) = ~x | y = orc(y, x)
3603 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3604 MVT::i1, MachineNode->getOperand(1).
3605 getOperand(0),
3606 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003607 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003608 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3609 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003610 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003611 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003612 }
Hal Finkel940ab932014-02-28 00:27:01 +00003613 break;
3614 case PPC::CROR:
3615 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3616 // x | x = x
3617 ResNode = MachineNode->getOperand(0).getNode();
3618 else if (Op1Set || Op2Set)
3619 // x | 1 = 1 | y = 1
3620 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3621 MVT::i1);
3622 else if (Op1Unset)
3623 // 0 | y = y
3624 ResNode = MachineNode->getOperand(1).getNode();
3625 else if (Op2Unset)
3626 // x | 0 = x
3627 ResNode = MachineNode->getOperand(0).getNode();
3628 else if (Op1Not)
3629 // ~x | y = orc(y, x)
3630 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3631 MVT::i1, MachineNode->getOperand(1),
3632 MachineNode->getOperand(0).
3633 getOperand(0));
3634 else if (Op2Not)
3635 // x | ~y = orc(x, y)
3636 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3637 MVT::i1, MachineNode->getOperand(0),
3638 MachineNode->getOperand(1).
3639 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003640 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003641 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3642 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003643 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003644 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003645 }
Hal Finkel940ab932014-02-28 00:27:01 +00003646 break;
3647 case PPC::CRXOR:
3648 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3649 // xor(x, x) = 0
3650 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3651 MVT::i1);
3652 else if (Op1Set)
3653 // xor(1, y) -> nor(y, y)
3654 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3655 MVT::i1, MachineNode->getOperand(1),
3656 MachineNode->getOperand(1));
3657 else if (Op2Set)
3658 // xor(x, 1) -> nor(x, x)
3659 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3660 MVT::i1, MachineNode->getOperand(0),
3661 MachineNode->getOperand(0));
3662 else if (Op1Unset)
3663 // xor(0, y) = y
3664 ResNode = MachineNode->getOperand(1).getNode();
3665 else if (Op2Unset)
3666 // xor(x, 0) = x
3667 ResNode = MachineNode->getOperand(0).getNode();
3668 else if (Op1Not)
3669 // xor(~x, y) = eqv(x, y)
3670 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3671 MVT::i1, MachineNode->getOperand(0).
3672 getOperand(0),
3673 MachineNode->getOperand(1));
3674 else if (Op2Not)
3675 // xor(x, ~y) = eqv(x, y)
3676 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3677 MVT::i1, MachineNode->getOperand(0),
3678 MachineNode->getOperand(1).
3679 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003680 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003681 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3682 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003683 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003684 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003685 }
Hal Finkel940ab932014-02-28 00:27:01 +00003686 break;
3687 case PPC::CRNOR:
3688 if (Op1Set || Op2Set)
3689 // nor(1, y) -> 0
3690 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3691 MVT::i1);
3692 else if (Op1Unset)
3693 // nor(0, y) = ~y -> nor(y, y)
3694 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3695 MVT::i1, MachineNode->getOperand(1),
3696 MachineNode->getOperand(1));
3697 else if (Op2Unset)
3698 // nor(x, 0) = ~x
3699 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3700 MVT::i1, MachineNode->getOperand(0),
3701 MachineNode->getOperand(0));
3702 else if (Op1Not)
3703 // nor(~x, y) = andc(x, y)
3704 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3705 MVT::i1, MachineNode->getOperand(0).
3706 getOperand(0),
3707 MachineNode->getOperand(1));
3708 else if (Op2Not)
3709 // nor(x, ~y) = andc(y, x)
3710 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3711 MVT::i1, MachineNode->getOperand(1).
3712 getOperand(0),
3713 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003714 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003715 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3716 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003717 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003718 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003719 }
Hal Finkel940ab932014-02-28 00:27:01 +00003720 break;
3721 case PPC::CREQV:
3722 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3723 // eqv(x, x) = 1
3724 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3725 MVT::i1);
3726 else if (Op1Set)
3727 // eqv(1, y) = y
3728 ResNode = MachineNode->getOperand(1).getNode();
3729 else if (Op2Set)
3730 // eqv(x, 1) = x
3731 ResNode = MachineNode->getOperand(0).getNode();
3732 else if (Op1Unset)
3733 // eqv(0, y) = ~y -> nor(y, y)
3734 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3735 MVT::i1, MachineNode->getOperand(1),
3736 MachineNode->getOperand(1));
3737 else if (Op2Unset)
3738 // eqv(x, 0) = ~x
3739 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3740 MVT::i1, MachineNode->getOperand(0),
3741 MachineNode->getOperand(0));
3742 else if (Op1Not)
3743 // eqv(~x, y) = xor(x, y)
3744 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3745 MVT::i1, MachineNode->getOperand(0).
3746 getOperand(0),
3747 MachineNode->getOperand(1));
3748 else if (Op2Not)
3749 // eqv(x, ~y) = xor(x, y)
3750 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3751 MVT::i1, MachineNode->getOperand(0),
3752 MachineNode->getOperand(1).
3753 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003754 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003755 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3756 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00003757 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00003758 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003759 }
Hal Finkel940ab932014-02-28 00:27:01 +00003760 break;
3761 case PPC::CRANDC:
3762 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3763 // andc(x, x) = 0
3764 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3765 MVT::i1);
3766 else if (Op1Set)
3767 // andc(1, y) = ~y
3768 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3769 MVT::i1, MachineNode->getOperand(1),
3770 MachineNode->getOperand(1));
3771 else if (Op1Unset || Op2Set)
3772 // andc(0, y) = andc(x, 1) = 0
3773 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3774 MVT::i1);
3775 else if (Op2Unset)
3776 // andc(x, 0) = x
3777 ResNode = MachineNode->getOperand(0).getNode();
3778 else if (Op1Not)
3779 // andc(~x, y) = ~(x | y) = nor(x, y)
3780 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3781 MVT::i1, MachineNode->getOperand(0).
3782 getOperand(0),
3783 MachineNode->getOperand(1));
3784 else if (Op2Not)
3785 // andc(x, ~y) = x & y
3786 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3787 MVT::i1, MachineNode->getOperand(0),
3788 MachineNode->getOperand(1).
3789 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003790 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003791 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3792 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00003793 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003794 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003795 }
Hal Finkel940ab932014-02-28 00:27:01 +00003796 break;
3797 case PPC::CRORC:
3798 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3799 // orc(x, x) = 1
3800 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3801 MVT::i1);
3802 else if (Op1Set || Op2Unset)
3803 // orc(1, y) = orc(x, 0) = 1
3804 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3805 MVT::i1);
3806 else if (Op2Set)
3807 // orc(x, 1) = x
3808 ResNode = MachineNode->getOperand(0).getNode();
3809 else if (Op1Unset)
3810 // orc(0, y) = ~y
3811 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3812 MVT::i1, MachineNode->getOperand(1),
3813 MachineNode->getOperand(1));
3814 else if (Op1Not)
3815 // orc(~x, y) = ~(x & y) = nand(x, y)
3816 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3817 MVT::i1, MachineNode->getOperand(0).
3818 getOperand(0),
3819 MachineNode->getOperand(1));
3820 else if (Op2Not)
3821 // orc(x, ~y) = x | y
3822 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3823 MVT::i1, MachineNode->getOperand(0),
3824 MachineNode->getOperand(1).
3825 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00003826 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00003827 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3828 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00003829 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003830 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00003831 }
Hal Finkel940ab932014-02-28 00:27:01 +00003832 break;
3833 case PPC::SELECT_I4:
3834 case PPC::SELECT_I8:
3835 case PPC::SELECT_F4:
3836 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003837 case PPC::SELECT_QFRC:
3838 case PPC::SELECT_QSRC:
3839 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003840 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003841 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003842 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003843 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003844 if (Op1Set)
3845 ResNode = MachineNode->getOperand(1).getNode();
3846 else if (Op1Unset)
3847 ResNode = MachineNode->getOperand(2).getNode();
3848 else if (Op1Not)
3849 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3850 SDLoc(MachineNode),
3851 MachineNode->getValueType(0),
3852 MachineNode->getOperand(0).
3853 getOperand(0),
3854 MachineNode->getOperand(2),
3855 MachineNode->getOperand(1));
3856 break;
3857 case PPC::BC:
3858 case PPC::BCn:
3859 if (Op1Not)
3860 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3861 PPC::BC,
3862 SDLoc(MachineNode),
3863 MVT::Other,
3864 MachineNode->getOperand(0).
3865 getOperand(0),
3866 MachineNode->getOperand(1),
3867 MachineNode->getOperand(2));
3868 // FIXME: Handle Op1Set, Op1Unset here too.
3869 break;
3870 }
3871
Hal Finkelb9989152014-02-28 06:11:16 +00003872 // If we're inverting this node because it is used only by selects that
3873 // we'd like to swap, then swap the selects before the node replacement.
3874 if (SelectSwap)
3875 SwapAllSelectUsers(MachineNode);
3876
Hal Finkel940ab932014-02-28 00:27:01 +00003877 if (ResNode != MachineNode) {
3878 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3879 DEBUG(MachineNode->dump(CurDAG));
3880 DEBUG(dbgs() << "\nNew: ");
3881 DEBUG(ResNode->dump(CurDAG));
3882 DEBUG(dbgs() << "\n");
3883
3884 ReplaceUses(MachineNode, ResNode);
3885 IsModified = true;
3886 }
3887 }
3888 if (IsModified)
3889 CurDAG->RemoveDeadNodes();
3890 } while (IsModified);
3891}
3892
Hal Finkel4c6658f2014-12-12 23:59:36 +00003893// Gather the set of 32-bit operations that are known to have their
3894// higher-order 32 bits zero, where ToPromote contains all such operations.
3895static bool PeepholePPC64ZExtGather(SDValue Op32,
3896 SmallPtrSetImpl<SDNode *> &ToPromote) {
3897 if (!Op32.isMachineOpcode())
3898 return false;
3899
3900 // First, check for the "frontier" instructions (those that will clear the
3901 // higher-order 32 bits.
3902
3903 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3904 // around. If it does not, then these instructions will clear the
3905 // higher-order bits.
3906 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3907 Op32.getMachineOpcode() == PPC::RLWNM) &&
3908 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3909 ToPromote.insert(Op32.getNode());
3910 return true;
3911 }
3912
3913 // SLW and SRW always clear the higher-order bits.
3914 if (Op32.getMachineOpcode() == PPC::SLW ||
3915 Op32.getMachineOpcode() == PPC::SRW) {
3916 ToPromote.insert(Op32.getNode());
3917 return true;
3918 }
3919
3920 // For LI and LIS, we need the immediate to be positive (so that it is not
3921 // sign extended).
3922 if (Op32.getMachineOpcode() == PPC::LI ||
3923 Op32.getMachineOpcode() == PPC::LIS) {
3924 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3925 return false;
3926
3927 ToPromote.insert(Op32.getNode());
3928 return true;
3929 }
3930
Hal Finkel4e2c7822015-01-05 18:09:06 +00003931 // LHBRX and LWBRX always clear the higher-order bits.
3932 if (Op32.getMachineOpcode() == PPC::LHBRX ||
3933 Op32.getMachineOpcode() == PPC::LWBRX) {
3934 ToPromote.insert(Op32.getNode());
3935 return true;
3936 }
3937
Hal Finkel49557f12015-01-05 18:52:29 +00003938 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended.
3939 if (Op32.getMachineOpcode() == PPC::CNTLZW) {
3940 ToPromote.insert(Op32.getNode());
3941 return true;
3942 }
3943
Hal Finkel4c6658f2014-12-12 23:59:36 +00003944 // Next, check for those instructions we can look through.
3945
3946 // Assuming the mask does not wrap around, then the higher-order bits are
3947 // taken directly from the first operand.
3948 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
3949 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
3950 SmallPtrSet<SDNode *, 16> ToPromote1;
3951 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3952 return false;
3953
3954 ToPromote.insert(Op32.getNode());
3955 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3956 return true;
3957 }
3958
3959 // For OR, the higher-order bits are zero if that is true for both operands.
3960 // For SELECT_I4, the same is true (but the relevant operand numbers are
3961 // shifted by 1).
3962 if (Op32.getMachineOpcode() == PPC::OR ||
3963 Op32.getMachineOpcode() == PPC::SELECT_I4) {
3964 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
3965 SmallPtrSet<SDNode *, 16> ToPromote1;
3966 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
3967 return false;
3968 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
3969 return false;
3970
3971 ToPromote.insert(Op32.getNode());
3972 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3973 return true;
3974 }
3975
3976 // For ORI and ORIS, we need the higher-order bits of the first operand to be
3977 // zero, and also for the constant to be positive (so that it is not sign
3978 // extended).
3979 if (Op32.getMachineOpcode() == PPC::ORI ||
3980 Op32.getMachineOpcode() == PPC::ORIS) {
3981 SmallPtrSet<SDNode *, 16> ToPromote1;
3982 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3983 return false;
3984 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
3985 return false;
3986
3987 ToPromote.insert(Op32.getNode());
3988 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3989 return true;
3990 }
3991
3992 // The higher-order bits of AND are zero if that is true for at least one of
3993 // the operands.
3994 if (Op32.getMachineOpcode() == PPC::AND) {
3995 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
3996 bool Op0OK =
3997 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3998 bool Op1OK =
3999 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
4000 if (!Op0OK && !Op1OK)
4001 return false;
4002
4003 ToPromote.insert(Op32.getNode());
4004
4005 if (Op0OK)
4006 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4007
4008 if (Op1OK)
4009 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
4010
4011 return true;
4012 }
4013
4014 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
4015 // of the first operand, or if the second operand is positive (so that it is
4016 // not sign extended).
4017 if (Op32.getMachineOpcode() == PPC::ANDIo ||
4018 Op32.getMachineOpcode() == PPC::ANDISo) {
4019 SmallPtrSet<SDNode *, 16> ToPromote1;
4020 bool Op0OK =
4021 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4022 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
4023 if (!Op0OK && !Op1OK)
4024 return false;
4025
4026 ToPromote.insert(Op32.getNode());
4027
4028 if (Op0OK)
4029 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4030
4031 return true;
4032 }
4033
4034 return false;
4035}
4036
4037void PPCDAGToDAGISel::PeepholePPC64ZExt() {
4038 if (!PPCSubTarget->isPPC64())
4039 return;
4040
4041 // When we zero-extend from i32 to i64, we use a pattern like this:
4042 // def : Pat<(i64 (zext i32:$in)),
4043 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
4044 // 0, 32)>;
4045 // There are several 32-bit shift/rotate instructions, however, that will
4046 // clear the higher-order bits of their output, rendering the RLDICL
4047 // unnecessary. When that happens, we remove it here, and redefine the
4048 // relevant 32-bit operation to be a 64-bit operation.
4049
4050 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4051 ++Position;
4052
4053 bool MadeChange = false;
4054 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004055 SDNode *N = &*--Position;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004056 // Skip dead nodes and any non-machine opcodes.
4057 if (N->use_empty() || !N->isMachineOpcode())
4058 continue;
4059
4060 if (N->getMachineOpcode() != PPC::RLDICL)
4061 continue;
4062
4063 if (N->getConstantOperandVal(1) != 0 ||
4064 N->getConstantOperandVal(2) != 32)
4065 continue;
4066
4067 SDValue ISR = N->getOperand(0);
4068 if (!ISR.isMachineOpcode() ||
4069 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
4070 continue;
4071
4072 if (!ISR.hasOneUse())
4073 continue;
4074
4075 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
4076 continue;
4077
4078 SDValue IDef = ISR.getOperand(0);
4079 if (!IDef.isMachineOpcode() ||
4080 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
4081 continue;
4082
4083 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
4084 // can get rid of it.
4085
4086 SDValue Op32 = ISR->getOperand(1);
4087 if (!Op32.isMachineOpcode())
4088 continue;
4089
4090 // There are some 32-bit instructions that always clear the high-order 32
4091 // bits, there are also some instructions (like AND) that we can look
4092 // through.
4093 SmallPtrSet<SDNode *, 16> ToPromote;
4094 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4095 continue;
4096
4097 // If the ToPromote set contains nodes that have uses outside of the set
4098 // (except for the original INSERT_SUBREG), then abort the transformation.
4099 bool OutsideUse = false;
4100 for (SDNode *PN : ToPromote) {
4101 for (SDNode *UN : PN->uses()) {
4102 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4103 OutsideUse = true;
4104 break;
4105 }
4106 }
4107
4108 if (OutsideUse)
4109 break;
4110 }
4111 if (OutsideUse)
4112 continue;
4113
4114 MadeChange = true;
4115
4116 // We now know that this zero extension can be removed by promoting to
4117 // nodes in ToPromote to 64-bit operations, where for operations in the
4118 // frontier of the set, we need to insert INSERT_SUBREGs for their
4119 // operands.
4120 for (SDNode *PN : ToPromote) {
4121 unsigned NewOpcode;
4122 switch (PN->getMachineOpcode()) {
4123 default:
4124 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4125 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4126 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4127 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4128 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4129 case PPC::LI: NewOpcode = PPC::LI8; break;
4130 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004131 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4132 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004133 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004134 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4135 case PPC::OR: NewOpcode = PPC::OR8; break;
4136 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4137 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4138 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4139 case PPC::AND: NewOpcode = PPC::AND8; break;
4140 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4141 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4142 }
4143
4144 // Note: During the replacement process, the nodes will be in an
4145 // inconsistent state (some instructions will have operands with values
4146 // of the wrong type). Once done, however, everything should be right
4147 // again.
4148
4149 SmallVector<SDValue, 4> Ops;
4150 for (const SDValue &V : PN->ops()) {
4151 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4152 !isa<ConstantSDNode>(V)) {
4153 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4154 SDNode *ReplOp =
4155 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4156 ISR.getNode()->getVTList(), ReplOpOps);
4157 Ops.push_back(SDValue(ReplOp, 0));
4158 } else {
4159 Ops.push_back(V);
4160 }
4161 }
4162
4163 // Because all to-be-promoted nodes only have users that are other
4164 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4165 // the i32 result value type with i64.
4166
4167 SmallVector<EVT, 2> NewVTs;
4168 SDVTList VTs = PN->getVTList();
4169 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4170 if (VTs.VTs[i] == MVT::i32)
4171 NewVTs.push_back(MVT::i64);
4172 else
4173 NewVTs.push_back(VTs.VTs[i]);
4174
4175 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4176 DEBUG(PN->dump(CurDAG));
4177
4178 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4179
4180 DEBUG(dbgs() << "\nNew: ");
4181 DEBUG(PN->dump(CurDAG));
4182 DEBUG(dbgs() << "\n");
4183 }
4184
4185 // Now we replace the original zero extend and its associated INSERT_SUBREG
4186 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4187 // return an i64).
4188
4189 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4190 DEBUG(N->dump(CurDAG));
4191 DEBUG(dbgs() << "\nNew: ");
4192 DEBUG(Op32.getNode()->dump(CurDAG));
4193 DEBUG(dbgs() << "\n");
4194
4195 ReplaceUses(N, Op32.getNode());
4196 }
4197
4198 if (MadeChange)
4199 CurDAG->RemoveDeadNodes();
4200}
4201
Hal Finkel940ab932014-02-28 00:27:01 +00004202void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004203 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004204 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004205 return;
4206
4207 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4208 ++Position;
4209
4210 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004211 SDNode *N = &*--Position;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004212 // Skip dead nodes and any non-machine opcodes.
4213 if (N->use_empty() || !N->isMachineOpcode())
4214 continue;
4215
4216 unsigned FirstOp;
4217 unsigned StorageOpcode = N->getMachineOpcode();
4218
4219 switch (StorageOpcode) {
4220 default: continue;
4221
4222 case PPC::LBZ:
4223 case PPC::LBZ8:
4224 case PPC::LD:
4225 case PPC::LFD:
4226 case PPC::LFS:
4227 case PPC::LHA:
4228 case PPC::LHA8:
4229 case PPC::LHZ:
4230 case PPC::LHZ8:
4231 case PPC::LWA:
4232 case PPC::LWZ:
4233 case PPC::LWZ8:
4234 FirstOp = 0;
4235 break;
4236
4237 case PPC::STB:
4238 case PPC::STB8:
4239 case PPC::STD:
4240 case PPC::STFD:
4241 case PPC::STFS:
4242 case PPC::STH:
4243 case PPC::STH8:
4244 case PPC::STW:
4245 case PPC::STW8:
4246 FirstOp = 1;
4247 break;
4248 }
4249
Kyle Butt1452b762015-12-11 00:47:36 +00004250 // If this is a load or store with a zero offset, or within the alignment,
4251 // we may be able to fold an add-immediate into the memory operation.
4252 // The check against alignment is below, as it can't occur until we check
4253 // the arguments to N
4254 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)))
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004255 continue;
4256
4257 SDValue Base = N->getOperand(FirstOp + 1);
4258 if (!Base.isMachineOpcode())
4259 continue;
4260
Kyle Butt1452b762015-12-11 00:47:36 +00004261 // On targets with fusion, we don't want this to fire and remove a fusion
4262 // opportunity, unless a) it results in another fusion opportunity or
4263 // b) optimizing for size.
4264 if (PPCSubTarget->hasFusion() &&
Hans Wennborga8e6b3e2015-12-11 00:58:32 +00004265 (!MF->getFunction()->optForSize() && !Base.hasOneUse()))
Kyle Butt1452b762015-12-11 00:47:36 +00004266 continue;
4267
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004268 unsigned Flags = 0;
4269 bool ReplaceFlags = true;
4270
4271 // When the feeding operation is an add-immediate of some sort,
4272 // determine whether we need to add relocation information to the
4273 // target flags on the immediate operand when we fold it into the
4274 // load instruction.
4275 //
4276 // For something like ADDItocL, the relocation information is
4277 // inferred from the opcode; when we process it in the AsmPrinter,
4278 // we add the necessary relocation there. A load, though, can receive
4279 // relocation from various flavors of ADDIxxx, so we need to carry
4280 // the relocation information in the target flags.
4281 switch (Base.getMachineOpcode()) {
4282 default: continue;
4283
4284 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004285 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004286 // In some cases (such as TLS) the relocation information
4287 // is already in place on the operand, so copying the operand
4288 // is sufficient.
4289 ReplaceFlags = false;
4290 // For these cases, the immediate may not be divisible by 4, in
4291 // which case the fold is illegal for DS-form instructions. (The
4292 // other cases provide aligned addresses and are always safe.)
4293 if ((StorageOpcode == PPC::LWA ||
4294 StorageOpcode == PPC::LD ||
4295 StorageOpcode == PPC::STD) &&
4296 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4297 Base.getConstantOperandVal(1) % 4 != 0))
4298 continue;
4299 break;
4300 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004301 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004302 break;
4303 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004304 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004305 break;
4306 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004307 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004308 break;
4309 }
4310
Kyle Butt1452b762015-12-11 00:47:36 +00004311 SDValue ImmOpnd = Base.getOperand(1);
4312 int MaxDisplacement = 0;
4313 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
4314 const GlobalValue *GV = GA->getGlobal();
4315 MaxDisplacement = GV->getAlignment() - 1;
4316 }
4317
4318 int Offset = N->getConstantOperandVal(FirstOp);
4319 if (Offset < 0 || Offset > MaxDisplacement)
4320 continue;
4321
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004322 // We found an opportunity. Reverse the operands from the add
4323 // immediate and substitute them into the load or store. If
4324 // needed, update the target flags for the immediate operand to
4325 // reflect the necessary relocation information.
4326 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4327 DEBUG(Base->dump(CurDAG));
4328 DEBUG(dbgs() << "\nN: ");
4329 DEBUG(N->dump(CurDAG));
4330 DEBUG(dbgs() << "\n");
4331
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004332 // If the relocation information isn't already present on the
4333 // immediate operand, add it now.
4334 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004335 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004336 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004337 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004338 // We can't perform this optimization for data whose alignment
4339 // is insufficient for the instruction encoding.
4340 if (GV->getAlignment() < 4 &&
4341 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
Kyle Butt1452b762015-12-11 00:47:36 +00004342 StorageOpcode == PPC::LWA || (Offset % 4) != 0)) {
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004343 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4344 continue;
4345 }
Kyle Butt1452b762015-12-11 00:47:36 +00004346 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004347 } else if (ConstantPoolSDNode *CP =
4348 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004349 const Constant *C = CP->getConstVal();
4350 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4351 CP->getAlignment(),
Kyle Butt1452b762015-12-11 00:47:36 +00004352 Offset, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004353 }
4354 }
4355
4356 if (FirstOp == 1) // Store
4357 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4358 Base.getOperand(0), N->getOperand(3));
4359 else // Load
4360 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4361 N->getOperand(2));
4362
4363 // The add-immediate may now be dead, in which case remove it.
4364 if (Base.getNode()->use_empty())
4365 CurDAG->RemoveDeadNode(Base.getNode());
4366 }
4367}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004368
Chris Lattnerb055c872006-06-10 01:15:02 +00004369
Andrew Trickc416ba62010-12-24 04:28:06 +00004370/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004371/// PowerPC-specific DAG, ready for instruction scheduling.
4372///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004373FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004374 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004375}
4376
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004377static void initializePassOnce(PassRegistry &Registry) {
4378 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004379 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4380 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004381 Registry.registerPass(*PI, true);
4382}
4383
4384void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4385 CALL_ONCE_INITIALIZATION(initializePassOnce);
4386}
4387