blob: 4bd303f211d68721589cf4d92e028096078030d5 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Hal Finkel3ee2af72014-07-18 23:29:49 +000017#include "PPCMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "PPCTargetMachine.h"
Chris Lattner45640392005-08-19 22:38:53 +000019#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000026#include "llvm/IR/GlobalAlias.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Intrinsics.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000030#include "llvm/IR/Module.h"
Hal Finkel940ab932014-02-28 00:27:01 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000032#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000037using namespace llvm;
38
Chandler Carruth84e68b22014-04-22 02:41:26 +000039#define DEBUG_TYPE "ppc-codegen"
40
Hal Finkel940ab932014-02-28 00:27:01 +000041// FIXME: Remove this once the bug has been fixed!
42cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
43cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
44
Benjamin Kramer970eac42015-02-06 17:51:54 +000045static cl::opt<bool>
46 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
47 cl::desc("use aggressive ppc isel for bit permutations"),
48 cl::Hidden);
49static cl::opt<bool> BPermRewriterNoMasking(
50 "ppc-bit-perm-rewriter-stress-rotates",
51 cl::desc("stress rotate selection in aggressive ppc isel for "
52 "bit permutations"),
53 cl::Hidden);
Hal Finkelc58ce412015-01-01 02:53:29 +000054
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000055namespace llvm {
56 void initializePPCDAGToDAGISelPass(PassRegistry&);
57}
58
Chris Lattner43ff01e2005-08-17 19:33:03 +000059namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000060 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000061 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000062 /// instructions for SelectionDAG operations.
63 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000064 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000065 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000066 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +000067 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +000068 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000069 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000070 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christophercccae792015-01-30 22:02:31 +000071 : SelectionDAGISel(tm), TM(tm) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000072 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
73 }
Andrew Trickc416ba62010-12-24 04:28:06 +000074
Craig Topper0d3fa922014-04-29 07:57:37 +000075 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000076 // Make sure we re-emit a set of the global base reg if necessary
77 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +000078 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
79 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +000080 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000081
Eric Christopher1b8e7632014-05-22 01:07:24 +000082 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000083 InsertVRSaveCode(MF);
84
Chris Lattner1678a6c2006-03-16 18:25:23 +000085 return true;
Chris Lattner45640392005-08-19 22:38:53 +000086 }
Andrew Trickc416ba62010-12-24 04:28:06 +000087
Hal Finkel4edc66b2015-01-03 01:16:37 +000088 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +000089 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000090
Chris Lattner43ff01e2005-08-17 19:33:03 +000091 /// getI32Imm - Return a target constant with the specified value, of type
92 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000093 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000094 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000095 }
Chris Lattner45640392005-08-19 22:38:53 +000096
Chris Lattner97b3da12006-06-27 00:04:13 +000097 /// getI64Imm - Return a target constant with the specified value, of type
98 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000099 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000100 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000101 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000102
Chris Lattner97b3da12006-06-27 00:04:13 +0000103 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000104 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000105 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +0000106 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000107
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000108 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +0000109 /// with any number of 0s on either side. The 1s are allowed to wrap from
110 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
111 /// 0x0F0F0000 is not, since all 1s are not contiguous.
112 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
113
114
115 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
116 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000117 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000118 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000119
Chris Lattner45640392005-08-19 22:38:53 +0000120 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
121 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000122 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000123
Hal Finkelb5e9b042014-12-11 22:51:06 +0000124 SDNode *getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
125
Chris Lattner43ff01e2005-08-17 19:33:03 +0000126 // Select - Convert the specified operand from a target-independent to a
127 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000128 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000129
Nate Begeman93c4bc62005-08-19 00:38:14 +0000130 SDNode *SelectBitfieldInsert(SDNode *N);
Hal Finkel8adf2252014-12-16 05:51:41 +0000131 SDNode *SelectBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000132
Chris Lattner2a1823d2005-08-21 18:50:37 +0000133 /// SelectCC - Select a comparison of the specified values with the
134 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000135 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000136
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000137 /// SelectAddrImm - Returns true if the address N can be represented by
138 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000139 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000140 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000141 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000142 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000143
Chris Lattner6f5840c2006-11-16 00:41:37 +0000144 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000145 /// immediate field. Note that the operand at this point is already the
146 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000147 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000148 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000149 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000150 Out = N;
151 return true;
152 }
153
154 return false;
155 }
156
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000157 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
158 /// represented as an indexed [r+r] operation. Returns false if it can
159 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000160 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000161 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000162 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000163
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000164 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
165 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000166 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000167 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000168 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000169
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000170 /// SelectAddrImmX4 - Returns true if the address N can be represented by
171 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
172 /// Suitable for use by STD and friends.
173 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000174 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000175 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000176
Hal Finkel756810f2013-03-21 21:37:52 +0000177 // Select an address into a single register.
178 bool SelectAddr(SDValue N, SDValue &Base) {
179 Base = N;
180 return true;
181 }
182
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000183 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000184 /// inline asm expressions. It is always correct to compute the value into
185 /// a register. The case of adding a (possibly relocatable) constant to a
186 /// register can be improved, but it is wrong to substitute Reg+Reg for
187 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000188 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Hal Finkele78e52b2015-03-12 20:09:39 +0000189 char ConstraintCode,
Craig Topper0d3fa922014-04-29 07:57:37 +0000190 std::vector<SDValue> &OutOps) override {
Hal Finkeld4338382014-12-03 23:40:13 +0000191 // We need to make sure that this one operand does not end up in r0
192 // (because we might end up lowering this as 0(%op)).
Eric Christophercccae792015-01-30 22:02:31 +0000193 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
Hal Finkeld4338382014-12-03 23:40:13 +0000194 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
195 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
196 SDValue NewOp =
197 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
198 SDLoc(Op), Op.getValueType(),
199 Op, RC), 0);
200
201 OutOps.push_back(NewOp);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000202 return false;
203 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000204
Dan Gohman5ea74d52009-07-31 18:16:33 +0000205 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000206
Craig Topper0d3fa922014-04-29 07:57:37 +0000207 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000208 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000209 }
210
Chris Lattner03e08ee2005-09-13 22:03:06 +0000211// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000212#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000213
Chris Lattner259e6c72005-10-06 18:45:51 +0000214private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000215 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000216
217 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000218 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000219 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000220
Hal Finkel4edc66b2015-01-03 01:16:37 +0000221 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000222 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000223
Hal Finkelb9989152014-02-28 06:11:16 +0000224 bool AllUsersSelectZero(SDNode *N);
225 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000226
227 SDNode *transferMemOperands(SDNode *N, SDNode *Result);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000228 };
229}
230
Chris Lattner1678a6c2006-03-16 18:25:23 +0000231/// InsertVRSaveCode - Once the entire function has been instruction selected,
232/// all virtual registers are created and all machine instructions are built,
233/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000234void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000235 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000236 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000237 //
Dan Gohman4a618822010-02-10 16:03:48 +0000238 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000239 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000240 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000241 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
242 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
243 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000244 HasVectorVReg = true;
245 break;
246 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000247 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000248 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000249
Chris Lattner02e2c182006-03-13 21:52:10 +0000250 // If we have a vector register, we want to emit code into the entry and exit
251 // blocks to save and restore the VRSAVE register. We do this here (instead
252 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
253 //
254 // 1. This (trivially) reduces the load on the register allocator, by not
255 // having to represent the live range of the VRSAVE register.
256 // 2. This (more significantly) allows us to create a temporary virtual
257 // register to hold the saved VRSAVE value, allowing this temporary to be
258 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000259
260 // Create two vregs - one to hold the VRSAVE register that is live-in to the
261 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000262 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
263 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000264
Eric Christophercccae792015-01-30 22:02:31 +0000265 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000266 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000267 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000268 // Emit the following code into the entry block:
269 // InVRSAVE = MFVRSAVE
270 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
271 // MTVRSAVE UpdatedVRSAVE
272 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000273 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
274 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000275 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000276 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000277
Chris Lattner1678a6c2006-03-16 18:25:23 +0000278 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000279 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000280 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000281 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000282
Chris Lattner1678a6c2006-03-16 18:25:23 +0000283 // Skip over all terminator instructions, which are part of the return
284 // sequence.
285 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000286 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000287 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000288
Chris Lattner1678a6c2006-03-16 18:25:23 +0000289 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000290 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000291 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000292 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000293}
Chris Lattner8ae95252005-09-03 01:17:22 +0000294
Chris Lattner1678a6c2006-03-16 18:25:23 +0000295
Chris Lattner45640392005-08-19 22:38:53 +0000296/// getGlobalBaseReg - Output the instructions required to put the
297/// base address to use for accessing globals into a register.
298///
Evan Cheng61413a32006-08-26 05:34:46 +0000299SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000300 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000301 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000302 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000303 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000304 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000305 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000306 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000307
Eric Christopher1b8e7632014-05-22 01:07:24 +0000308 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000309 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000310 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000311 if (M->getPICLevel() == PICLevel::Small) {
312 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
313 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000314 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000315 } else {
316 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
317 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
318 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
319 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000320 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000321 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
322 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
323 }
324 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000325 GlobalBaseReg =
326 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000327 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
328 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000329 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000330 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000331 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000332 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000333 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000334 }
Chris Lattner45640392005-08-19 22:38:53 +0000335 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000336 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000337 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000338}
339
340/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
341/// or 64-bit immediate, and if the value can be accurately represented as a
342/// sign extension from a 16-bit value. If so, this returns true and the
343/// immediate.
344static bool isIntS16Immediate(SDNode *N, short &Imm) {
345 if (N->getOpcode() != ISD::Constant)
346 return false;
347
Dan Gohmaneffb8942008-09-12 16:56:44 +0000348 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000349 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000350 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000351 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000352 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000353}
354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000355static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000356 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000357}
358
359
Chris Lattner97b3da12006-06-27 00:04:13 +0000360/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
361/// operand. If so Imm will receive the 32-bit value.
362static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000363 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000364 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000365 return true;
366 }
367 return false;
368}
369
Chris Lattner97b3da12006-06-27 00:04:13 +0000370/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
371/// operand. If so Imm will receive the 64-bit value.
372static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000373 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000374 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000375 return true;
376 }
377 return false;
378}
379
380// isInt32Immediate - This method tests to see if a constant operand.
381// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000382static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000383 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000384}
385
386
387// isOpcWithIntImmediate - This method tests to see if the node is a specific
388// opcode and that it has a immediate integer right operand.
389// If so Imm will receive the 32 bit value.
390static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000391 return N->getOpcode() == Opc
392 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000393}
394
Hal Finkelb5e9b042014-12-11 22:51:06 +0000395SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
396 SDLoc dl(SN);
397 int FI = cast<FrameIndexSDNode>(N)->getIndex();
398 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
399 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
400 if (SN->hasOneUse())
401 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
402 getSmallIPtrImm(Offset));
403 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
404 getSmallIPtrImm(Offset));
405}
406
Nate Begemand31efd12006-09-22 05:01:56 +0000407bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000408 if (!Val)
409 return false;
410
Nate Begemanb3821a32005-08-18 07:30:46 +0000411 if (isShiftedMask_32(Val)) {
412 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000413 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000414 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000415 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000416 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000417 } else {
418 Val = ~Val; // invert mask
419 if (isShiftedMask_32(Val)) {
420 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000421 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000422 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000423 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000424 return true;
425 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000426 }
427 // no run present
428 return false;
429}
430
Andrew Trickc416ba62010-12-24 04:28:06 +0000431bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
432 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000433 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000434 // Don't even go down this path for i64, since different logic will be
435 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000436 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000437 return false;
438
Nate Begemanb3821a32005-08-18 07:30:46 +0000439 unsigned Shift = 32;
440 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
441 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000442 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000443 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000444 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000445
Nate Begemanb3821a32005-08-18 07:30:46 +0000446 if (Opcode == ISD::SHL) {
447 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000448 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000449 // determine which bits are made indeterminant by shift
450 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000451 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000452 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000453 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000454 // determine which bits are made indeterminant by shift
455 Indeterminant = ~(0xFFFFFFFFu >> Shift);
456 // adjust for the left rotate
457 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000458 } else if (Opcode == ISD::ROTL) {
459 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000460 } else {
461 return false;
462 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000463
Nate Begemanb3821a32005-08-18 07:30:46 +0000464 // if the mask doesn't intersect any Indeterminant bits
465 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000466 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000467 // make sure the mask is still a mask (wrap arounds may not be)
468 return isRunOfOnes(Mask, MB, ME);
469 }
470 return false;
471}
472
Nate Begeman93c4bc62005-08-19 00:38:14 +0000473/// SelectBitfieldInsert - turn an or of two masked values into
474/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000475SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000476 SDValue Op0 = N->getOperand(0);
477 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000478 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000479
Dan Gohmanf19609a2008-02-27 01:23:58 +0000480 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000481 CurDAG->computeKnownBits(Op0, LKZ, LKO);
482 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000483
Dan Gohmanf19609a2008-02-27 01:23:58 +0000484 unsigned TargetMask = LKZ.getZExtValue();
485 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000486
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000487 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
488 unsigned Op0Opc = Op0.getOpcode();
489 unsigned Op1Opc = Op1.getOpcode();
490 unsigned Value, SH = 0;
491 TargetMask = ~TargetMask;
492 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000493
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000494 // If the LHS has a foldable shift and the RHS does not, then swap it to the
495 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000496 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
497 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
498 Op0.getOperand(0).getOpcode() == ISD::SRL) {
499 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
500 Op1.getOperand(0).getOpcode() != ISD::SRL) {
501 std::swap(Op0, Op1);
502 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000503 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000504 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000505 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000506 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
507 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
508 Op1.getOperand(0).getOpcode() != ISD::SRL) {
509 std::swap(Op0, Op1);
510 std::swap(Op0Opc, Op1Opc);
511 std::swap(TargetMask, InsertMask);
512 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000513 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000514
Nate Begeman1333cea2006-05-07 00:23:38 +0000515 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000516 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000517 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000518
519 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000520 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000521 Op1 = Op1.getOperand(0);
522 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
523 }
524 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000525 // The AND mask might not be a constant, and we need to make sure that
526 // if we're going to fold the masking with the insert, all bits not
527 // know to be zero in the mask are known to be one.
528 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000529 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000530 bool CanFoldMask = InsertMask == MKO.getZExtValue();
531
Nate Begeman1333cea2006-05-07 00:23:38 +0000532 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000533 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000534 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000535 // Note that Value must be in range here (less than 32) because
536 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000537 Op1 = Op1.getOperand(0).getOperand(0);
538 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000539 }
540 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000541
Chris Lattnera2963392006-05-12 16:29:37 +0000542 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000543 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000544 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000545 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000546 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000547 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000548 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000549}
550
Hal Finkelc58ce412015-01-01 02:53:29 +0000551// Predict the number of instructions that would be generated by calling
552// SelectInt64(N).
Hal Finkelca6375f2015-01-04 12:35:03 +0000553static unsigned SelectInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000554 // Assume no remaining bits.
555 unsigned Remainder = 0;
556 // Assume no shift required.
557 unsigned Shift = 0;
558
559 // If it can't be represented as a 32 bit value.
560 if (!isInt<32>(Imm)) {
561 Shift = countTrailingZeros<uint64_t>(Imm);
562 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
563
564 // If the shifted value fits 32 bits.
565 if (isInt<32>(ImmSh)) {
566 // Go with the shifted value.
567 Imm = ImmSh;
568 } else {
569 // Still stuck with a 64 bit value.
570 Remainder = Imm;
571 Shift = 32;
572 Imm >>= 32;
573 }
574 }
575
576 // Intermediate operand.
577 unsigned Result = 0;
578
579 // Handle first 32 bits.
580 unsigned Lo = Imm & 0xFFFF;
581 unsigned Hi = (Imm >> 16) & 0xFFFF;
582
583 // Simple value.
584 if (isInt<16>(Imm)) {
585 // Just the Lo bits.
586 ++Result;
587 } else if (Lo) {
588 // Handle the Hi bits and Lo bits.
589 Result += 2;
590 } else {
591 // Just the Hi bits.
592 ++Result;
593 }
594
595 // If no shift, we're done.
596 if (!Shift) return Result;
597
598 // Shift for next step if the upper 32-bits were not zero.
599 if (Imm)
600 ++Result;
601
602 // Add in the last bits as required.
603 if ((Hi = (Remainder >> 16) & 0xFFFF))
604 ++Result;
605 if ((Lo = Remainder & 0xFFFF))
606 ++Result;
607
608 return Result;
609}
610
Hal Finkel241ba792015-01-04 15:43:55 +0000611static uint64_t Rot64(uint64_t Imm, unsigned R) {
612 return (Imm << R) | (Imm >> (64 - R));
613}
614
Hal Finkelca6375f2015-01-04 12:35:03 +0000615static unsigned SelectInt64Count(int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000616 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000617 if (Count == 1)
618 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000619
Hal Finkel241ba792015-01-04 15:43:55 +0000620 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000621 uint64_t RImm = Rot64(Imm, r);
622 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
623 Count = std::min(Count, RCount);
624
625 // See comments in SelectInt64 for an explanation of the logic below.
626 unsigned LS = findLastSet(RImm);
627 if (LS != r-1)
628 continue;
629
630 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
631 uint64_t RImmWithOnes = RImm | OnesMask;
632
633 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000634 Count = std::min(Count, RCount);
635 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000636
Hal Finkel241ba792015-01-04 15:43:55 +0000637 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000638}
639
Hal Finkelc58ce412015-01-01 02:53:29 +0000640// Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count
641// (above) needs to be kept in sync with this function.
Hal Finkelca6375f2015-01-04 12:35:03 +0000642static SDNode *SelectInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000643 // Assume no remaining bits.
644 unsigned Remainder = 0;
645 // Assume no shift required.
646 unsigned Shift = 0;
647
648 // If it can't be represented as a 32 bit value.
649 if (!isInt<32>(Imm)) {
650 Shift = countTrailingZeros<uint64_t>(Imm);
651 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
652
653 // If the shifted value fits 32 bits.
654 if (isInt<32>(ImmSh)) {
655 // Go with the shifted value.
656 Imm = ImmSh;
657 } else {
658 // Still stuck with a 64 bit value.
659 Remainder = Imm;
660 Shift = 32;
661 Imm >>= 32;
662 }
663 }
664
665 // Intermediate operand.
666 SDNode *Result;
667
668 // Handle first 32 bits.
669 unsigned Lo = Imm & 0xFFFF;
670 unsigned Hi = (Imm >> 16) & 0xFFFF;
671
672 auto getI32Imm = [CurDAG](unsigned Imm) {
673 return CurDAG->getTargetConstant(Imm, MVT::i32);
674 };
675
676 // Simple value.
677 if (isInt<16>(Imm)) {
678 // Just the Lo bits.
679 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
680 } else if (Lo) {
681 // Handle the Hi bits.
682 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
683 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
684 // And Lo bits.
685 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
686 SDValue(Result, 0), getI32Imm(Lo));
687 } else {
688 // Just the Hi bits.
689 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
690 }
691
692 // If no shift, we're done.
693 if (!Shift) return Result;
694
695 // Shift for next step if the upper 32-bits were not zero.
696 if (Imm) {
697 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
698 SDValue(Result, 0),
699 getI32Imm(Shift),
700 getI32Imm(63 - Shift));
701 }
702
703 // Add in the last bits as required.
704 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
705 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
706 SDValue(Result, 0), getI32Imm(Hi));
707 }
708 if ((Lo = Remainder & 0xFFFF)) {
709 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
710 SDValue(Result, 0), getI32Imm(Lo));
711 }
712
713 return Result;
714}
715
Hal Finkelca6375f2015-01-04 12:35:03 +0000716static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000717 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000718 if (Count == 1)
719 return SelectInt64Direct(CurDAG, dl, Imm);
720
Hal Finkel241ba792015-01-04 15:43:55 +0000721 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000722
Hal Finkel2f618792015-01-05 03:41:38 +0000723 int64_t MatImm;
724 unsigned MaskEnd;
725
Hal Finkel241ba792015-01-04 15:43:55 +0000726 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000727 uint64_t RImm = Rot64(Imm, r);
728 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000729 if (RCount < Count) {
730 Count = RCount;
731 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000732 MatImm = RImm;
733 MaskEnd = 63;
734 }
735
736 // If the immediate to generate has many trailing zeros, it might be
737 // worthwhile to generate a rotated value with too many leading ones
738 // (because that's free with li/lis's sign-extension semantics), and then
739 // mask them off after rotation.
740
741 unsigned LS = findLastSet(RImm);
742 // We're adding (63-LS) higher-order ones, and we expect to mask them off
743 // after performing the inverse rotation by (64-r). So we need that:
744 // 63-LS == 64-r => LS == r-1
745 if (LS != r-1)
746 continue;
747
748 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
749 uint64_t RImmWithOnes = RImm | OnesMask;
750
751 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
752 if (RCount < Count) {
753 Count = RCount;
754 RMin = r;
755 MatImm = RImmWithOnes;
756 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000757 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000758 }
759
Hal Finkel241ba792015-01-04 15:43:55 +0000760 if (!RMin)
761 return SelectInt64Direct(CurDAG, dl, Imm);
762
763 auto getI32Imm = [CurDAG](unsigned Imm) {
764 return CurDAG->getTargetConstant(Imm, MVT::i32);
765 };
766
Hal Finkel2f618792015-01-05 03:41:38 +0000767 SDValue Val = SDValue(SelectInt64Direct(CurDAG, dl, MatImm), 0);
768 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
769 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000770}
771
Hal Finkelc58ce412015-01-01 02:53:29 +0000772// Select a 64-bit constant.
773static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) {
774 SDLoc dl(N);
775
776 // Get 64 bit value.
777 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
778 return SelectInt64(CurDAG, dl, Imm);
779}
780
Hal Finkel8adf2252014-12-16 05:51:41 +0000781namespace {
782class BitPermutationSelector {
783 struct ValueBit {
784 SDValue V;
785
786 // The bit number in the value, using a convention where bit 0 is the
787 // lowest-order bit.
788 unsigned Idx;
789
790 enum Kind {
791 ConstZero,
792 Variable
793 } K;
794
795 ValueBit(SDValue V, unsigned I, Kind K = Variable)
796 : V(V), Idx(I), K(K) {}
797 ValueBit(Kind K = Variable)
798 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
799
800 bool isZero() const {
801 return K == ConstZero;
802 }
803
804 bool hasValue() const {
805 return K == Variable;
806 }
807
808 SDValue getValue() const {
809 assert(hasValue() && "Cannot get the value of a constant bit");
810 return V;
811 }
812
813 unsigned getValueBitIndex() const {
814 assert(hasValue() && "Cannot get the value bit index of a constant bit");
815 return Idx;
816 }
817 };
818
819 // A bit group has the same underlying value and the same rotate factor.
820 struct BitGroup {
821 SDValue V;
822 unsigned RLAmt;
823 unsigned StartIdx, EndIdx;
824
Hal Finkelc58ce412015-01-01 02:53:29 +0000825 // This rotation amount assumes that the lower 32 bits of the quantity are
826 // replicated in the high 32 bits by the rotation operator (which is done
827 // by rlwinm and friends in 64-bit mode).
828 bool Repl32;
829 // Did converting to Repl32 == true change the rotation factor? If it did,
830 // it decreased it by 32.
831 bool Repl32CR;
832 // Was this group coalesced after setting Repl32 to true?
833 bool Repl32Coalesced;
834
Hal Finkel8adf2252014-12-16 05:51:41 +0000835 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000836 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
837 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000838 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
839 " [" << S << ", " << E << "]\n");
840 }
841 };
842
843 // Information on each (Value, RLAmt) pair (like the number of groups
844 // associated with each) used to choose the lowering method.
845 struct ValueRotInfo {
846 SDValue V;
847 unsigned RLAmt;
848 unsigned NumGroups;
849 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000850 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000851
852 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000853 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
854 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000855
856 // For sorting (in reverse order) by NumGroups, and then by
857 // FirstGroupStartIdx.
858 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000859 // We need to sort so that the non-Repl32 come first because, when we're
860 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
861 // masking operation.
862 if (Repl32 < Other.Repl32)
863 return true;
864 else if (Repl32 > Other.Repl32)
865 return false;
866 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000867 return true;
868 else if (NumGroups < Other.NumGroups)
869 return false;
870 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
871 return true;
872 return false;
873 }
874 };
875
876 // Return true if something interesting was deduced, return false if we're
877 // providing only a generic representation of V (or something else likewise
878 // uninteresting for instruction selection).
879 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
880 switch (V.getOpcode()) {
881 default: break;
882 case ISD::ROTL:
883 if (isa<ConstantSDNode>(V.getOperand(1))) {
884 unsigned RotAmt = V.getConstantOperandVal(1);
885
886 SmallVector<ValueBit, 64> LHSBits(Bits.size());
887 getValueBits(V.getOperand(0), LHSBits);
888
889 for (unsigned i = 0; i < Bits.size(); ++i)
890 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
891
892 return true;
893 }
894 break;
895 case ISD::SHL:
896 if (isa<ConstantSDNode>(V.getOperand(1))) {
897 unsigned ShiftAmt = V.getConstantOperandVal(1);
898
899 SmallVector<ValueBit, 64> LHSBits(Bits.size());
900 getValueBits(V.getOperand(0), LHSBits);
901
902 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
903 Bits[i] = LHSBits[i - ShiftAmt];
904
905 for (unsigned i = 0; i < ShiftAmt; ++i)
906 Bits[i] = ValueBit(ValueBit::ConstZero);
907
908 return true;
909 }
910 break;
911 case ISD::SRL:
912 if (isa<ConstantSDNode>(V.getOperand(1))) {
913 unsigned ShiftAmt = V.getConstantOperandVal(1);
914
915 SmallVector<ValueBit, 64> LHSBits(Bits.size());
916 getValueBits(V.getOperand(0), LHSBits);
917
918 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
919 Bits[i] = LHSBits[i + ShiftAmt];
920
921 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
922 Bits[i] = ValueBit(ValueBit::ConstZero);
923
924 return true;
925 }
926 break;
927 case ISD::AND:
928 if (isa<ConstantSDNode>(V.getOperand(1))) {
929 uint64_t Mask = V.getConstantOperandVal(1);
930
931 SmallVector<ValueBit, 64> LHSBits(Bits.size());
932 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
933
934 for (unsigned i = 0; i < Bits.size(); ++i)
935 if (((Mask >> i) & 1) == 1)
936 Bits[i] = LHSBits[i];
937 else
938 Bits[i] = ValueBit(ValueBit::ConstZero);
939
940 // Mark this as interesting, only if the LHS was also interesting. This
941 // prevents the overall procedure from matching a single immediate 'and'
942 // (which is non-optimal because such an and might be folded with other
943 // things if we don't select it here).
944 return LHSTrivial;
945 }
946 break;
947 case ISD::OR: {
948 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
949 getValueBits(V.getOperand(0), LHSBits);
950 getValueBits(V.getOperand(1), RHSBits);
951
952 bool AllDisjoint = true;
953 for (unsigned i = 0; i < Bits.size(); ++i)
954 if (LHSBits[i].isZero())
955 Bits[i] = RHSBits[i];
956 else if (RHSBits[i].isZero())
957 Bits[i] = LHSBits[i];
958 else {
959 AllDisjoint = false;
960 break;
961 }
962
963 if (!AllDisjoint)
964 break;
965
966 return true;
967 }
968 }
969
970 for (unsigned i = 0; i < Bits.size(); ++i)
971 Bits[i] = ValueBit(V, i);
972
973 return false;
974 }
975
976 // For each value (except the constant ones), compute the left-rotate amount
977 // to get it from its original to final position.
978 void computeRotationAmounts() {
979 HasZeros = false;
980 RLAmt.resize(Bits.size());
981 for (unsigned i = 0; i < Bits.size(); ++i)
982 if (Bits[i].hasValue()) {
983 unsigned VBI = Bits[i].getValueBitIndex();
984 if (i >= VBI)
985 RLAmt[i] = i - VBI;
986 else
987 RLAmt[i] = Bits.size() - (VBI - i);
988 } else if (Bits[i].isZero()) {
989 HasZeros = true;
990 RLAmt[i] = UINT32_MAX;
991 } else {
992 llvm_unreachable("Unknown value bit type");
993 }
994 }
995
996 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +0000997 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
998 // they break up groups.
999 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001000 BitGroups.clear();
1001
1002 unsigned LastRLAmt = RLAmt[0];
1003 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1004 unsigned LastGroupStartIdx = 0;
1005 for (unsigned i = 1; i < Bits.size(); ++i) {
1006 unsigned ThisRLAmt = RLAmt[i];
1007 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001008 if (LateMask && !ThisValue) {
1009 ThisValue = LastValue;
1010 ThisRLAmt = LastRLAmt;
1011 // If we're doing late masking, then the first bit group always starts
1012 // at zero (even if the first bits were zero).
1013 if (BitGroups.empty())
1014 LastGroupStartIdx = 0;
1015 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001016
1017 // If this bit has the same underlying value and the same rotate factor as
1018 // the last one, then they're part of the same group.
1019 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1020 continue;
1021
1022 if (LastValue.getNode())
1023 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1024 i-1));
1025 LastRLAmt = ThisRLAmt;
1026 LastValue = ThisValue;
1027 LastGroupStartIdx = i;
1028 }
1029 if (LastValue.getNode())
1030 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1031 Bits.size()-1));
1032
1033 if (BitGroups.empty())
1034 return;
1035
1036 // We might be able to combine the first and last groups.
1037 if (BitGroups.size() > 1) {
1038 // If the first and last groups are the same, then remove the first group
1039 // in favor of the last group, making the ending index of the last group
1040 // equal to the ending index of the to-be-removed first group.
1041 if (BitGroups[0].StartIdx == 0 &&
1042 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1043 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1044 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001045 DEBUG(dbgs() << "\tcombining final bit group with inital one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001046 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1047 BitGroups.erase(BitGroups.begin());
1048 }
1049 }
1050 }
1051
1052 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1053 // associated with each. If there is a degeneracy, pick the one that occurs
1054 // first (in the final value).
1055 void collectValueRotInfo() {
1056 ValueRots.clear();
1057
1058 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001059 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1060 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001061 VRI.V = BG.V;
1062 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001063 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001064 VRI.NumGroups += 1;
1065 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1066 }
1067
1068 // Now that we've collected the various ValueRotInfo instances, we need to
1069 // sort them.
1070 ValueRotsVec.clear();
1071 for (auto &I : ValueRots) {
1072 ValueRotsVec.push_back(I.second);
1073 }
1074 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1075 }
1076
Hal Finkelc58ce412015-01-01 02:53:29 +00001077 // In 64-bit mode, rlwinm and friends have a rotation operator that
1078 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1079 // indices of these instructions can only be in the lower 32 bits, so they
1080 // can only represent some 64-bit bit groups. However, when they can be used,
1081 // the 32-bit replication can be used to represent, as a single bit group,
1082 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1083 // groups when possible. Returns true if any of the bit groups were
1084 // converted.
1085 void assignRepl32BitGroups() {
1086 // If we have bits like this:
1087 //
1088 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1089 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1090 // Groups: | RLAmt = 8 | RLAmt = 40 |
1091 //
1092 // But, making use of a 32-bit operation that replicates the low-order 32
1093 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1094 // of 8.
1095
1096 auto IsAllLow32 = [this](BitGroup & BG) {
1097 if (BG.StartIdx <= BG.EndIdx) {
1098 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1099 if (!Bits[i].hasValue())
1100 continue;
1101 if (Bits[i].getValueBitIndex() >= 32)
1102 return false;
1103 }
1104 } else {
1105 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1106 if (!Bits[i].hasValue())
1107 continue;
1108 if (Bits[i].getValueBitIndex() >= 32)
1109 return false;
1110 }
1111 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1112 if (!Bits[i].hasValue())
1113 continue;
1114 if (Bits[i].getValueBitIndex() >= 32)
1115 return false;
1116 }
1117 }
1118
1119 return true;
1120 };
1121
1122 for (auto &BG : BitGroups) {
1123 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1124 if (IsAllLow32(BG)) {
1125 if (BG.RLAmt >= 32) {
1126 BG.RLAmt -= 32;
1127 BG.Repl32CR = true;
1128 }
1129
1130 BG.Repl32 = true;
1131
1132 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1133 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1134 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1135 }
1136 }
1137 }
1138
1139 // Now walk through the bit groups, consolidating where possible.
1140 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1141 // We might want to remove this bit group by merging it with the previous
1142 // group (which might be the ending group).
1143 auto IP = (I == BitGroups.begin()) ?
1144 std::prev(BitGroups.end()) : std::prev(I);
1145 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1146 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1147
1148 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1149 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1150 " [" << I->StartIdx << ", " << I->EndIdx <<
1151 "] with group with range [" <<
1152 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1153
1154 IP->EndIdx = I->EndIdx;
1155 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1156 IP->Repl32Coalesced = true;
1157 I = BitGroups.erase(I);
1158 continue;
1159 } else {
1160 // There is a special case worth handling: If there is a single group
1161 // covering the entire upper 32 bits, and it can be merged with both
1162 // the next and previous groups (which might be the same group), then
1163 // do so. If it is the same group (so there will be only one group in
1164 // total), then we need to reverse the order of the range so that it
1165 // covers the entire 64 bits.
1166 if (I->StartIdx == 32 && I->EndIdx == 63) {
1167 assert(std::next(I) == BitGroups.end() &&
1168 "bit group ends at index 63 but there is another?");
1169 auto IN = BitGroups.begin();
1170
1171 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
1172 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1173 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1174 IsAllLow32(*I)) {
1175
1176 DEBUG(dbgs() << "\tcombining bit group for " <<
1177 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1178 " [" << I->StartIdx << ", " << I->EndIdx <<
1179 "] with 32-bit replicated groups with ranges [" <<
1180 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1181 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1182
1183 if (IP == IN) {
1184 // There is only one other group; change it to cover the whole
1185 // range (backward, so that it can still be Repl32 but cover the
1186 // whole 64-bit range).
1187 IP->StartIdx = 31;
1188 IP->EndIdx = 30;
1189 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1190 IP->Repl32Coalesced = true;
1191 I = BitGroups.erase(I);
1192 } else {
1193 // There are two separate groups, one before this group and one
1194 // after us (at the beginning). We're going to remove this group,
1195 // but also the group at the very beginning.
1196 IP->EndIdx = IN->EndIdx;
1197 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1198 IP->Repl32Coalesced = true;
1199 I = BitGroups.erase(I);
1200 BitGroups.erase(BitGroups.begin());
1201 }
1202
1203 // This must be the last group in the vector (and we might have
1204 // just invalidated the iterator above), so break here.
1205 break;
1206 }
1207 }
1208 }
1209
1210 ++I;
1211 }
1212 }
1213
Hal Finkel8adf2252014-12-16 05:51:41 +00001214 SDValue getI32Imm(unsigned Imm) {
1215 return CurDAG->getTargetConstant(Imm, MVT::i32);
1216 }
1217
Hal Finkelc58ce412015-01-01 02:53:29 +00001218 uint64_t getZerosMask() {
1219 uint64_t Mask = 0;
1220 for (unsigned i = 0; i < Bits.size(); ++i) {
1221 if (Bits[i].hasValue())
1222 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001223 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001224 }
1225
1226 return ~Mask;
1227 }
1228
Hal Finkel8adf2252014-12-16 05:51:41 +00001229 // Depending on the number of groups for a particular value, it might be
1230 // better to rotate, mask explicitly (using andi/andis), and then or the
1231 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001232 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1233 if (BPermRewriterNoMasking)
1234 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001235
1236 for (ValueRotInfo &VRI : ValueRotsVec) {
1237 unsigned Mask = 0;
1238 for (unsigned i = 0; i < Bits.size(); ++i) {
1239 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1240 continue;
1241 if (RLAmt[i] != VRI.RLAmt)
1242 continue;
1243 Mask |= (1u << i);
1244 }
1245
1246 // Compute the masks for andi/andis that would be necessary.
1247 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1248 assert((ANDIMask != 0 || ANDISMask != 0) &&
1249 "No set bits in mask for value bit groups");
1250 bool NeedsRotate = VRI.RLAmt != 0;
1251
1252 // We're trying to minimize the number of instructions. If we have one
1253 // group, using one of andi/andis can break even. If we have three
1254 // groups, we can use both andi and andis and break even (to use both
1255 // andi and andis we also need to or the results together). We need four
1256 // groups if we also need to rotate. To use andi/andis we need to do more
1257 // than break even because rotate-and-mask instructions tend to be easier
1258 // to schedule.
1259
1260 // FIXME: We've biased here against using andi/andis, which is right for
1261 // POWER cores, but not optimal everywhere. For example, on the A2,
1262 // andi/andis have single-cycle latency whereas the rotate-and-mask
1263 // instructions take two cycles, and it would be better to bias toward
1264 // andi/andis in break-even cases.
1265
1266 unsigned NumAndInsts = (unsigned) NeedsRotate +
1267 (unsigned) (ANDIMask != 0) +
1268 (unsigned) (ANDISMask != 0) +
1269 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1270 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001271
1272 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1273 " RL: " << VRI.RLAmt << ":" <<
1274 "\n\t\t\tisel using masking: " << NumAndInsts <<
1275 " using rotates: " << VRI.NumGroups << "\n");
1276
Hal Finkel8adf2252014-12-16 05:51:41 +00001277 if (NumAndInsts >= VRI.NumGroups)
1278 continue;
1279
Hal Finkelc58ce412015-01-01 02:53:29 +00001280 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1281
1282 if (InstCnt) *InstCnt += NumAndInsts;
1283
Hal Finkel8adf2252014-12-16 05:51:41 +00001284 SDValue VRot;
1285 if (VRI.RLAmt) {
1286 SDValue Ops[] =
1287 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1288 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1289 Ops), 0);
1290 } else {
1291 VRot = VRI.V;
1292 }
1293
1294 SDValue ANDIVal, ANDISVal;
1295 if (ANDIMask != 0)
1296 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1297 VRot, getI32Imm(ANDIMask)), 0);
1298 if (ANDISMask != 0)
1299 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1300 VRot, getI32Imm(ANDISMask)), 0);
1301
1302 SDValue TotalVal;
1303 if (!ANDIVal)
1304 TotalVal = ANDISVal;
1305 else if (!ANDISVal)
1306 TotalVal = ANDIVal;
1307 else
1308 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1309 ANDIVal, ANDISVal), 0);
1310
1311 if (!Res)
1312 Res = TotalVal;
1313 else
1314 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1315 Res, TotalVal), 0);
1316
1317 // Now, remove all groups with this underlying value and rotation
1318 // factor.
1319 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1320 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1321 I = BitGroups.erase(I);
1322 else
1323 ++I;
1324 }
1325 }
1326 }
1327
1328 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001329 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001330 SDLoc dl(N);
1331 SDValue Res;
1332
Hal Finkelc58ce412015-01-01 02:53:29 +00001333 if (InstCnt) *InstCnt = 0;
1334
Hal Finkel8adf2252014-12-16 05:51:41 +00001335 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001336 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001337
1338 // If we've not yet selected a 'starting' instruction, and we have no zeros
1339 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1340 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001341 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001342 ValueRotInfo &VRI = ValueRotsVec[0];
1343 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001344 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001345 SDValue Ops[] =
1346 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1347 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1348 } else {
1349 Res = VRI.V;
1350 }
1351
1352 // Now, remove all groups with this underlying value and rotation factor.
1353 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1354 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1355 I = BitGroups.erase(I);
1356 else
1357 ++I;
1358 }
1359 }
1360
Hal Finkelc58ce412015-01-01 02:53:29 +00001361 if (InstCnt) *InstCnt += BitGroups.size();
1362
Hal Finkel8adf2252014-12-16 05:51:41 +00001363 // Insert the other groups (one at a time).
1364 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001365 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001366 SDValue Ops[] =
1367 { BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1368 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1369 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1370 } else {
1371 SDValue Ops[] =
1372 { Res, BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1373 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1374 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1375 }
1376 }
1377
Hal Finkelc58ce412015-01-01 02:53:29 +00001378 if (LateMask) {
1379 unsigned Mask = (unsigned) getZerosMask();
1380
1381 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1382 assert((ANDIMask != 0 || ANDISMask != 0) &&
1383 "No set bits in zeros mask?");
1384
1385 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1386 (unsigned) (ANDISMask != 0) +
1387 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1388
1389 SDValue ANDIVal, ANDISVal;
1390 if (ANDIMask != 0)
1391 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1392 Res, getI32Imm(ANDIMask)), 0);
1393 if (ANDISMask != 0)
1394 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1395 Res, getI32Imm(ANDISMask)), 0);
1396
1397 if (!ANDIVal)
1398 Res = ANDISVal;
1399 else if (!ANDISVal)
1400 Res = ANDIVal;
1401 else
1402 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1403 ANDIVal, ANDISVal), 0);
1404 }
1405
Hal Finkel8adf2252014-12-16 05:51:41 +00001406 return Res.getNode();
1407 }
1408
Hal Finkelc58ce412015-01-01 02:53:29 +00001409 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1410 unsigned MaskStart, unsigned MaskEnd,
1411 bool IsIns) {
1412 // In the notation used by the instructions, 'start' and 'end' are reversed
1413 // because bits are counted from high to low order.
1414 unsigned InstMaskStart = 64 - MaskEnd - 1,
1415 InstMaskEnd = 64 - MaskStart - 1;
1416
1417 if (Repl32)
1418 return 1;
1419
1420 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1421 InstMaskEnd == 63 - RLAmt)
1422 return 1;
1423
1424 return 2;
1425 }
1426
1427 // For 64-bit values, not all combinations of rotates and masks are
1428 // available. Produce one if it is available.
1429 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1430 unsigned MaskStart, unsigned MaskEnd,
1431 unsigned *InstCnt = nullptr) {
1432 // In the notation used by the instructions, 'start' and 'end' are reversed
1433 // because bits are counted from high to low order.
1434 unsigned InstMaskStart = 64 - MaskEnd - 1,
1435 InstMaskEnd = 64 - MaskStart - 1;
1436
1437 if (InstCnt) *InstCnt += 1;
1438
1439 if (Repl32) {
1440 // This rotation amount assumes that the lower 32 bits of the quantity
1441 // are replicated in the high 32 bits by the rotation operator (which is
1442 // done by rlwinm and friends).
1443 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1444 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1445 SDValue Ops[] =
1446 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1447 getI32Imm(InstMaskEnd - 32) };
1448 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1449 Ops), 0);
1450 }
1451
1452 if (InstMaskEnd == 63) {
1453 SDValue Ops[] =
1454 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1455 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1456 }
1457
1458 if (InstMaskStart == 0) {
1459 SDValue Ops[] =
1460 { V, getI32Imm(RLAmt), getI32Imm(InstMaskEnd) };
1461 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1462 }
1463
1464 if (InstMaskEnd == 63 - RLAmt) {
1465 SDValue Ops[] =
1466 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1467 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1468 }
1469
1470 // We cannot do this with a single instruction, so we'll use two. The
1471 // problem is that we're not free to choose both a rotation amount and mask
1472 // start and end independently. We can choose an arbitrary mask start and
1473 // end, but then the rotation amount is fixed. Rotation, however, can be
1474 // inverted, and so by applying an "inverse" rotation first, we can get the
1475 // desired result.
1476 if (InstCnt) *InstCnt += 1;
1477
1478 // The rotation mask for the second instruction must be MaskStart.
1479 unsigned RLAmt2 = MaskStart;
1480 // The first instruction must rotate V so that the overall rotation amount
1481 // is RLAmt.
1482 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1483 if (RLAmt1)
1484 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1485 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1486 }
1487
1488 // For 64-bit values, not all combinations of rotates and masks are
1489 // available. Produce a rotate-mask-and-insert if one is available.
1490 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1491 bool Repl32, unsigned MaskStart,
1492 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1493 // In the notation used by the instructions, 'start' and 'end' are reversed
1494 // because bits are counted from high to low order.
1495 unsigned InstMaskStart = 64 - MaskEnd - 1,
1496 InstMaskEnd = 64 - MaskStart - 1;
1497
1498 if (InstCnt) *InstCnt += 1;
1499
1500 if (Repl32) {
1501 // This rotation amount assumes that the lower 32 bits of the quantity
1502 // are replicated in the high 32 bits by the rotation operator (which is
1503 // done by rlwinm and friends).
1504 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1505 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1506 SDValue Ops[] =
1507 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1508 getI32Imm(InstMaskEnd - 32) };
1509 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1510 Ops), 0);
1511 }
1512
1513 if (InstMaskEnd == 63 - RLAmt) {
1514 SDValue Ops[] =
1515 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1516 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1517 }
1518
1519 // We cannot do this with a single instruction, so we'll use two. The
1520 // problem is that we're not free to choose both a rotation amount and mask
1521 // start and end independently. We can choose an arbitrary mask start and
1522 // end, but then the rotation amount is fixed. Rotation, however, can be
1523 // inverted, and so by applying an "inverse" rotation first, we can get the
1524 // desired result.
1525 if (InstCnt) *InstCnt += 1;
1526
1527 // The rotation mask for the second instruction must be MaskStart.
1528 unsigned RLAmt2 = MaskStart;
1529 // The first instruction must rotate V so that the overall rotation amount
1530 // is RLAmt.
1531 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1532 if (RLAmt1)
1533 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1534 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1535 }
1536
1537 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1538 if (BPermRewriterNoMasking)
1539 return;
1540
1541 // The idea here is the same as in the 32-bit version, but with additional
1542 // complications from the fact that Repl32 might be true. Because we
1543 // aggressively convert bit groups to Repl32 form (which, for small
1544 // rotation factors, involves no other change), and then coalesce, it might
1545 // be the case that a single 64-bit masking operation could handle both
1546 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1547 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1548 // completely capture the new combined bit group.
1549
1550 for (ValueRotInfo &VRI : ValueRotsVec) {
1551 uint64_t Mask = 0;
1552
1553 // We need to add to the mask all bits from the associated bit groups.
1554 // If Repl32 is false, we need to add bits from bit groups that have
1555 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1556 // group is trivially convertable if it overlaps only with the lower 32
1557 // bits, and the group has not been coalesced.
1558 auto MatchingBG = [VRI](BitGroup &BG) {
1559 if (VRI.V != BG.V)
1560 return false;
1561
1562 unsigned EffRLAmt = BG.RLAmt;
1563 if (!VRI.Repl32 && BG.Repl32) {
1564 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1565 !BG.Repl32Coalesced) {
1566 if (BG.Repl32CR)
1567 EffRLAmt += 32;
1568 } else {
1569 return false;
1570 }
1571 } else if (VRI.Repl32 != BG.Repl32) {
1572 return false;
1573 }
1574
1575 if (VRI.RLAmt != EffRLAmt)
1576 return false;
1577
1578 return true;
1579 };
1580
1581 for (auto &BG : BitGroups) {
1582 if (!MatchingBG(BG))
1583 continue;
1584
1585 if (BG.StartIdx <= BG.EndIdx) {
1586 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001587 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001588 } else {
1589 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001590 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001591 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001592 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001593 }
1594 }
1595
1596 // We can use the 32-bit andi/andis technique if the mask does not
1597 // require any higher-order bits. This can save an instruction compared
1598 // to always using the general 64-bit technique.
1599 bool Use32BitInsts = isUInt<32>(Mask);
1600 // Compute the masks for andi/andis that would be necessary.
1601 unsigned ANDIMask = (Mask & UINT16_MAX),
1602 ANDISMask = (Mask >> 16) & UINT16_MAX;
1603
1604 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1605
1606 unsigned NumAndInsts = (unsigned) NeedsRotate +
1607 (unsigned) (bool) Res;
1608 if (Use32BitInsts)
1609 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1610 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1611 else
1612 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1;
1613
1614 unsigned NumRLInsts = 0;
1615 bool FirstBG = true;
1616 for (auto &BG : BitGroups) {
1617 if (!MatchingBG(BG))
1618 continue;
1619 NumRLInsts +=
1620 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1621 !FirstBG);
1622 FirstBG = false;
1623 }
1624
1625 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1626 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1627 "\n\t\t\tisel using masking: " << NumAndInsts <<
1628 " using rotates: " << NumRLInsts << "\n");
1629
1630 // When we'd use andi/andis, we bias toward using the rotates (andi only
1631 // has a record form, and is cracked on POWER cores). However, when using
1632 // general 64-bit constant formation, bias toward the constant form,
1633 // because that exposes more opportunities for CSE.
1634 if (NumAndInsts > NumRLInsts)
1635 continue;
1636 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1637 continue;
1638
1639 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1640
1641 if (InstCnt) *InstCnt += NumAndInsts;
1642
1643 SDValue VRot;
1644 // We actually need to generate a rotation if we have a non-zero rotation
1645 // factor or, in the Repl32 case, if we care about any of the
1646 // higher-order replicated bits. In the latter case, we generate a mask
1647 // backward so that it actually includes the entire 64 bits.
1648 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1649 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1650 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1651 else
1652 VRot = VRI.V;
1653
1654 SDValue TotalVal;
1655 if (Use32BitInsts) {
1656 assert((ANDIMask != 0 || ANDISMask != 0) &&
1657 "No set bits in mask when using 32-bit ands for 64-bit value");
1658
1659 SDValue ANDIVal, ANDISVal;
1660 if (ANDIMask != 0)
1661 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1662 VRot, getI32Imm(ANDIMask)), 0);
1663 if (ANDISMask != 0)
1664 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1665 VRot, getI32Imm(ANDISMask)), 0);
1666
1667 if (!ANDIVal)
1668 TotalVal = ANDISVal;
1669 else if (!ANDISVal)
1670 TotalVal = ANDIVal;
1671 else
1672 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1673 ANDIVal, ANDISVal), 0);
1674 } else {
1675 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1676 TotalVal =
1677 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1678 VRot, TotalVal), 0);
1679 }
1680
1681 if (!Res)
1682 Res = TotalVal;
1683 else
1684 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1685 Res, TotalVal), 0);
1686
1687 // Now, remove all groups with this underlying value and rotation
1688 // factor.
1689 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1690 if (MatchingBG(*I))
1691 I = BitGroups.erase(I);
1692 else
1693 ++I;
1694 }
1695 }
1696 }
1697
1698 // Instruction selection for the 64-bit case.
1699 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1700 SDLoc dl(N);
1701 SDValue Res;
1702
1703 if (InstCnt) *InstCnt = 0;
1704
1705 // Take care of cases that should use andi/andis first.
1706 SelectAndParts64(dl, Res, InstCnt);
1707
1708 // If we've not yet selected a 'starting' instruction, and we have no zeros
1709 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1710 // number of groups), and start with this rotated value.
1711 if ((!HasZeros || LateMask) && !Res) {
1712 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1713 // groups will come first, and so the VRI representing the largest number
1714 // of groups might not be first (it might be the first Repl32 groups).
1715 unsigned MaxGroupsIdx = 0;
1716 if (!ValueRotsVec[0].Repl32) {
1717 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1718 if (ValueRotsVec[i].Repl32) {
1719 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1720 MaxGroupsIdx = i;
1721 break;
1722 }
1723 }
1724
1725 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1726 bool NeedsRotate = false;
1727 if (VRI.RLAmt) {
1728 NeedsRotate = true;
1729 } else if (VRI.Repl32) {
1730 for (auto &BG : BitGroups) {
1731 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1732 BG.Repl32 != VRI.Repl32)
1733 continue;
1734
1735 // We don't need a rotate if the bit group is confined to the lower
1736 // 32 bits.
1737 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1738 continue;
1739
1740 NeedsRotate = true;
1741 break;
1742 }
1743 }
1744
1745 if (NeedsRotate)
1746 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1747 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1748 InstCnt);
1749 else
1750 Res = VRI.V;
1751
1752 // Now, remove all groups with this underlying value and rotation factor.
1753 if (Res)
1754 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1755 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt && I->Repl32 == VRI.Repl32)
1756 I = BitGroups.erase(I);
1757 else
1758 ++I;
1759 }
1760 }
1761
1762 // Because 64-bit rotates are more flexible than inserts, we might have a
1763 // preference regarding which one we do first (to save one instruction).
1764 if (!Res)
1765 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1766 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1767 false) <
1768 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1769 true)) {
1770 if (I != BitGroups.begin()) {
1771 BitGroup BG = *I;
1772 BitGroups.erase(I);
1773 BitGroups.insert(BitGroups.begin(), BG);
1774 }
1775
1776 break;
1777 }
1778 }
1779
1780 // Insert the other groups (one at a time).
1781 for (auto &BG : BitGroups) {
1782 if (!Res)
1783 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1784 BG.EndIdx, InstCnt);
1785 else
1786 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1787 BG.StartIdx, BG.EndIdx, InstCnt);
1788 }
1789
1790 if (LateMask) {
1791 uint64_t Mask = getZerosMask();
1792
1793 // We can use the 32-bit andi/andis technique if the mask does not
1794 // require any higher-order bits. This can save an instruction compared
1795 // to always using the general 64-bit technique.
1796 bool Use32BitInsts = isUInt<32>(Mask);
1797 // Compute the masks for andi/andis that would be necessary.
1798 unsigned ANDIMask = (Mask & UINT16_MAX),
1799 ANDISMask = (Mask >> 16) & UINT16_MAX;
1800
1801 if (Use32BitInsts) {
1802 assert((ANDIMask != 0 || ANDISMask != 0) &&
1803 "No set bits in mask when using 32-bit ands for 64-bit value");
1804
1805 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1806 (unsigned) (ANDISMask != 0) +
1807 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1808
1809 SDValue ANDIVal, ANDISVal;
1810 if (ANDIMask != 0)
1811 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1812 Res, getI32Imm(ANDIMask)), 0);
1813 if (ANDISMask != 0)
1814 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1815 Res, getI32Imm(ANDISMask)), 0);
1816
1817 if (!ANDIVal)
1818 Res = ANDISVal;
1819 else if (!ANDISVal)
1820 Res = ANDIVal;
1821 else
1822 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1823 ANDIVal, ANDISVal), 0);
1824 } else {
1825 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1;
1826
1827 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1828 Res =
1829 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1830 Res, MaskVal), 0);
1831 }
1832 }
1833
1834 return Res.getNode();
1835 }
1836
1837 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1838 // Fill in BitGroups.
1839 collectBitGroups(LateMask);
1840 if (BitGroups.empty())
1841 return nullptr;
1842
1843 // For 64-bit values, figure out when we can use 32-bit instructions.
1844 if (Bits.size() == 64)
1845 assignRepl32BitGroups();
1846
1847 // Fill in ValueRotsVec.
1848 collectValueRotInfo();
1849
1850 if (Bits.size() == 32) {
1851 return Select32(N, LateMask, InstCnt);
1852 } else {
1853 assert(Bits.size() == 64 && "Not 64 bits here?");
1854 return Select64(N, LateMask, InstCnt);
1855 }
1856
1857 return nullptr;
1858 }
1859
Hal Finkel8adf2252014-12-16 05:51:41 +00001860 SmallVector<ValueBit, 64> Bits;
1861
1862 bool HasZeros;
1863 SmallVector<unsigned, 64> RLAmt;
1864
1865 SmallVector<BitGroup, 16> BitGroups;
1866
1867 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1868 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1869
1870 SelectionDAG *CurDAG;
1871
1872public:
1873 BitPermutationSelector(SelectionDAG *DAG)
1874 : CurDAG(DAG) {}
1875
1876 // Here we try to match complex bit permutations into a set of
1877 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1878 // known to produce optimial code for common cases (like i32 byte swapping).
1879 SDNode *Select(SDNode *N) {
1880 Bits.resize(N->getValueType(0).getSizeInBits());
1881 if (!getValueBits(SDValue(N, 0), Bits))
1882 return nullptr;
1883
1884 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1885 " selection for: ");
1886 DEBUG(N->dump(CurDAG));
1887
1888 // Fill it RLAmt and set HasZeros.
1889 computeRotationAmounts();
1890
Hal Finkelc58ce412015-01-01 02:53:29 +00001891 if (!HasZeros)
1892 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001893
Hal Finkelc58ce412015-01-01 02:53:29 +00001894 // We currently have two techniques for handling results with zeros: early
1895 // masking (the default) and late masking. Late masking is sometimes more
1896 // efficient, but because the structure of the bit groups is different, it
1897 // is hard to tell without generating both and comparing the results. With
1898 // late masking, we ignore zeros in the resulting value when inserting each
1899 // set of bit groups, and then mask in the zeros at the end. With early
1900 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001901
Hal Finkelc58ce412015-01-01 02:53:29 +00001902 unsigned InstCnt, InstCntLateMask;
1903 DEBUG(dbgs() << "\tEarly masking:\n");
1904 SDNode *RN = Select(N, false, &InstCnt);
1905 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1906
1907 DEBUG(dbgs() << "\tLate masking:\n");
1908 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1909 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1910 " instructions\n");
1911
1912 if (InstCnt <= InstCntLateMask) {
1913 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1914 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001915 }
1916
Hal Finkelc58ce412015-01-01 02:53:29 +00001917 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1918 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001919 }
1920};
1921} // anonymous namespace
1922
1923SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) {
1924 if (N->getValueType(0) != MVT::i32 &&
1925 N->getValueType(0) != MVT::i64)
1926 return nullptr;
1927
Hal Finkelc58ce412015-01-01 02:53:29 +00001928 if (!UseBitPermRewriter)
1929 return nullptr;
1930
Hal Finkel8adf2252014-12-16 05:51:41 +00001931 switch (N->getOpcode()) {
1932 default: break;
1933 case ISD::ROTL:
1934 case ISD::SHL:
1935 case ISD::SRL:
1936 case ISD::AND:
1937 case ISD::OR: {
1938 BitPermutationSelector BPS(CurDAG);
1939 return BPS.Select(N);
1940 }
1941 }
1942
1943 return nullptr;
1944}
1945
Chris Lattner2a1823d2005-08-21 18:50:37 +00001946/// SelectCC - Select a comparison of the specified values with the specified
1947/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001948SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001949 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001950 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001951 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001952
Owen Anderson9f944592009-08-11 20:47:22 +00001953 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001954 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001955 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1956 if (isInt32Immediate(RHS, Imm)) {
1957 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001958 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001959 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1960 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001961 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001962 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001963 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1964 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00001965
Chris Lattneraa3926b2006-09-20 04:25:47 +00001966 // For non-equality comparisons, the default code would materialize the
1967 // constant, then compare against it, like this:
1968 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00001969 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00001970 // cmpw cr0, r3, r2
1971 // Since we are just comparing for equality, we can emit this instead:
1972 // xoris r0,r3,0x1234
1973 // cmplwi cr0,r0,0x5678
1974 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00001975 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
1976 getI32Imm(Imm >> 16)), 0);
1977 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
1978 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001979 }
1980 Opc = PPC::CMPLW;
1981 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00001982 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001983 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1984 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00001985 Opc = PPC::CMPLW;
1986 } else {
1987 short SImm;
1988 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001989 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1990 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00001991 0);
1992 Opc = PPC::CMPW;
1993 }
Owen Anderson9f944592009-08-11 20:47:22 +00001994 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00001995 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001996 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001997 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001998 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001999 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002000 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
2001 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002002 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002003 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002004 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
2005 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002006
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002007 // For non-equality comparisons, the default code would materialize the
2008 // constant, then compare against it, like this:
2009 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002010 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002011 // cmpd cr0, r3, r2
2012 // Since we are just comparing for equality, we can emit this instead:
2013 // xoris r0,r3,0x1234
2014 // cmpldi cr0,r0,0x5678
2015 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002016 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002017 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
2018 getI64Imm(Imm >> 16)), 0);
2019 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
2020 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002021 }
2022 }
2023 Opc = PPC::CMPLD;
2024 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002025 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002026 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
2027 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002028 Opc = PPC::CMPLD;
2029 } else {
2030 short SImm;
2031 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002032 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
2033 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002034 0);
2035 Opc = PPC::CMPD;
2036 }
Owen Anderson9f944592009-08-11 20:47:22 +00002037 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002038 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002039 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002040 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002041 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002042 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002043 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002044}
2045
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002046static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002047 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002048 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002049 case ISD::SETONE:
2050 case ISD::SETOLE:
2051 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002052 llvm_unreachable("Should be lowered by legalize!");
2053 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002054 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002055 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002056 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002057 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002058 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002059 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002060 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002061 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002062 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002063 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002064 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002065 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002066 case ISD::SETO: return PPC::PRED_NU;
2067 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002068 // These two are invalid for floating point. Assume we have int.
2069 case ISD::SETULT: return PPC::PRED_LT;
2070 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002071 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002072}
2073
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002074/// getCRIdxForSetCC - Return the index of the condition register field
2075/// associated with the SetCC condition, and whether or not the field is
2076/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002077static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002078 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002079 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002080 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002081 case ISD::SETOLT:
2082 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2083 case ISD::SETOGT:
2084 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2085 case ISD::SETOEQ:
2086 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2087 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002088 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002089 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002090 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002091 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002092 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002093 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2094 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002095 case ISD::SETUEQ:
2096 case ISD::SETOGE:
2097 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002098 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002099 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002100 // These are invalid for floating point. Assume integer.
2101 case ISD::SETULT: return 0;
2102 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002103 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002104}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002105
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002106// getVCmpInst: return the vector compare instruction for the specified
2107// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002108// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002109static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2110 bool HasVSX, bool &Swap, bool &Negate) {
2111 Swap = false;
2112 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002113
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002114 if (VecVT.isFloatingPoint()) {
2115 /* Handle some cases by swapping input operands. */
2116 switch (CC) {
2117 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2118 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2119 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2120 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2121 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2122 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2123 default: break;
2124 }
2125 /* Handle some cases by negating the result. */
2126 switch (CC) {
2127 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2128 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2129 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2130 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2131 default: break;
2132 }
2133 /* We have instructions implementing the remaining cases. */
2134 switch (CC) {
2135 case ISD::SETEQ:
2136 case ISD::SETOEQ:
2137 if (VecVT == MVT::v4f32)
2138 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2139 else if (VecVT == MVT::v2f64)
2140 return PPC::XVCMPEQDP;
2141 break;
2142 case ISD::SETGT:
2143 case ISD::SETOGT:
2144 if (VecVT == MVT::v4f32)
2145 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2146 else if (VecVT == MVT::v2f64)
2147 return PPC::XVCMPGTDP;
2148 break;
2149 case ISD::SETGE:
2150 case ISD::SETOGE:
2151 if (VecVT == MVT::v4f32)
2152 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2153 else if (VecVT == MVT::v2f64)
2154 return PPC::XVCMPGEDP;
2155 break;
2156 default:
2157 break;
2158 }
2159 llvm_unreachable("Invalid floating-point vector compare condition");
2160 } else {
2161 /* Handle some cases by swapping input operands. */
2162 switch (CC) {
2163 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2164 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2165 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2166 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2167 default: break;
2168 }
2169 /* Handle some cases by negating the result. */
2170 switch (CC) {
2171 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2172 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2173 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2174 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2175 default: break;
2176 }
2177 /* We have instructions implementing the remaining cases. */
2178 switch (CC) {
2179 case ISD::SETEQ:
2180 case ISD::SETUEQ:
2181 if (VecVT == MVT::v16i8)
2182 return PPC::VCMPEQUB;
2183 else if (VecVT == MVT::v8i16)
2184 return PPC::VCMPEQUH;
2185 else if (VecVT == MVT::v4i32)
2186 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002187 else if (VecVT == MVT::v2i64)
2188 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002189 break;
2190 case ISD::SETGT:
2191 if (VecVT == MVT::v16i8)
2192 return PPC::VCMPGTSB;
2193 else if (VecVT == MVT::v8i16)
2194 return PPC::VCMPGTSH;
2195 else if (VecVT == MVT::v4i32)
2196 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002197 else if (VecVT == MVT::v2i64)
2198 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002199 break;
2200 case ISD::SETUGT:
2201 if (VecVT == MVT::v16i8)
2202 return PPC::VCMPGTUB;
2203 else if (VecVT == MVT::v8i16)
2204 return PPC::VCMPGTUH;
2205 else if (VecVT == MVT::v4i32)
2206 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002207 else if (VecVT == MVT::v2i64)
2208 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002209 break;
2210 default:
2211 break;
2212 }
2213 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002214 }
2215}
2216
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002217SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002218 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002219 unsigned Imm;
2220 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002221 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2222 bool isPPC64 = (PtrVT == MVT::i64);
2223
Eric Christopher1b8e7632014-05-22 01:07:24 +00002224 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002225 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002226 // We can codegen setcc op, imm very efficiently compared to a brcond.
2227 // Check for those cases here.
2228 // setcc op, 0
2229 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002230 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002231 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002232 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002233 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002234 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002235 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002236 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002237 }
Chris Lattnere2969492005-10-21 21:17:10 +00002238 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002239 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002240 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002241 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002242 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002243 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002244 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002245 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002246 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002247 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002248 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002249 }
Chris Lattnere2969492005-10-21 21:17:10 +00002250 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002251 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002252 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2253 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002254 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002255 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002256 }
2257 }
Chris Lattner491b8292005-10-06 19:03:35 +00002258 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002259 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002260 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002261 default: break;
2262 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002263 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002264 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002265 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002266 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2267 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +00002268 MVT::i32,
2269 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002270 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002271 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002272 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002273 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002274 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002275 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +00002276 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002277 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +00002278 }
Chris Lattnere2969492005-10-21 21:17:10 +00002279 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002280 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
2281 getI32Imm(1)), 0);
2282 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2283 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002284 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002285 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002286 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002287 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002288 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +00002289 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002290 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002291 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002292 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002293 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002294 }
Chris Lattner491b8292005-10-06 19:03:35 +00002295 }
2296 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002297
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002298 SDValue LHS = N->getOperand(0);
2299 SDValue RHS = N->getOperand(1);
2300
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002301 // Altivec Vector compare instructions do not set any CR register by default and
2302 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002303 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002304 if (PPCSubTarget->hasQPX())
2305 return nullptr;
2306
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002307 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002308 bool Swap, Negate;
2309 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2310 PPCSubTarget->hasVSX(), Swap, Negate);
2311 if (Swap)
2312 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002313
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002314 if (Negate) {
2315 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
2316 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
2317 PPC::VNOR,
2318 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002319 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002320
2321 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002322 }
2323
Eric Christopher1b8e7632014-05-22 01:07:24 +00002324 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +00002325 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +00002326
Chris Lattner491b8292005-10-06 19:03:35 +00002327 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002328 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002329 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002330 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002331
Chris Lattner491b8292005-10-06 19:03:35 +00002332 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002333 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002334
Craig Topper062a2ba2014-04-25 05:30:21 +00002335 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002336 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002337 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002338
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002339 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2340 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002341
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002342 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +00002343 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +00002344 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +00002345 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +00002346
2347 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002348 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002349 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +00002350 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002351}
Chris Lattner502a3692005-10-06 18:56:10 +00002352
Hal Finkelcf599212015-02-25 21:36:59 +00002353SDNode *PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
2354 // Transfer memoperands.
2355 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2356 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2357 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
2358 return Result;
2359}
2360
Chris Lattner318622f2005-10-06 19:07:45 +00002361
Chris Lattner43ff01e2005-08-17 19:33:03 +00002362// Select - Convert the specified operand from a target-independent to a
2363// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002364SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002365 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002366 if (N->isMachineOpcode()) {
2367 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002368 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002369 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002370
Hal Finkel51b3fd12014-09-02 06:23:54 +00002371 // In case any misguided DAG-level optimizations form an ADD with a
2372 // TargetConstant operand, crash here instead of miscompiling (by selecting
2373 // an r+r add instead of some kind of r+i add).
2374 if (N->getOpcode() == ISD::ADD &&
2375 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2376 llvm_unreachable("Invalid ADD with TargetConstant operand");
2377
Hal Finkel8adf2252014-12-16 05:51:41 +00002378 // Try matching complex bit permutations before doing anything else.
2379 if (SDNode *NN = SelectBitPermutation(N))
2380 return NN;
2381
Chris Lattner43ff01e2005-08-17 19:33:03 +00002382 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002383 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002384
Jim Laskey095e6f32006-12-12 13:23:43 +00002385 case ISD::Constant: {
Hal Finkelc58ce412015-01-01 02:53:29 +00002386 if (N->getValueType(0) == MVT::i64)
2387 return SelectInt64(CurDAG, N);
Jim Laskey095e6f32006-12-12 13:23:43 +00002388 break;
2389 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002390
Hal Finkel940ab932014-02-28 00:27:01 +00002391 case ISD::SETCC: {
2392 SDNode *SN = SelectSETCC(N);
2393 if (SN)
2394 return SN;
2395 break;
2396 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002397 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00002398 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00002399
Hal Finkelb5e9b042014-12-11 22:51:06 +00002400 case ISD::FrameIndex:
2401 return getFrameIndex(N, N);
Chris Lattner6961fc72006-03-26 10:06:40 +00002402
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002403 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002404 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002405 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2406 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00002407 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002408
Hal Finkelbbdee932014-12-02 22:01:00 +00002409 case PPCISD::READ_TIME_BASE: {
2410 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2411 MVT::Other, N->getOperand(0));
2412 }
2413
Hal Finkel13d104b2014-12-11 18:37:52 +00002414 case PPCISD::SRA_ADDZE: {
2415 SDValue N0 = N->getOperand(0);
2416 SDValue ShiftAmt =
2417 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
2418 getConstantIntValue(), N->getValueType(0));
2419 if (N->getValueType(0) == MVT::i64) {
2420 SDNode *Op =
2421 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2422 N0, ShiftAmt);
2423 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64,
2424 SDValue(Op, 0), SDValue(Op, 1));
2425 } else {
2426 assert(N->getValueType(0) == MVT::i32 &&
2427 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2428 SDNode *Op =
2429 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2430 N0, ShiftAmt);
2431 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2432 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00002433 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002434 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002435
Chris Lattnerce645542006-11-10 02:08:47 +00002436 case ISD::LOAD: {
2437 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002438 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002439 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002440
Chris Lattnerce645542006-11-10 02:08:47 +00002441 // Normal loads are handled by code generated from the .td file.
2442 if (LD->getAddressingMode() != ISD::PRE_INC)
2443 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002444
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002445 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002446 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002447 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002448
Chris Lattner474b5b72006-11-15 19:55:13 +00002449 unsigned Opcode;
2450 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002451 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002452 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002453 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2454 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002455 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002456 case MVT::f64: Opcode = PPC::LFDU; break;
2457 case MVT::f32: Opcode = PPC::LFSU; break;
2458 case MVT::i32: Opcode = PPC::LWZU; break;
2459 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2460 case MVT::i1:
2461 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002462 }
2463 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002464 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2465 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2466 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002467 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002468 case MVT::i64: Opcode = PPC::LDU; break;
2469 case MVT::i32: Opcode = PPC::LWZU8; break;
2470 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2471 case MVT::i1:
2472 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002473 }
2474 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002475
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002476 SDValue Chain = LD->getChain();
2477 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002478 SDValue Ops[] = { Offset, Base, Chain };
Hal Finkelcf599212015-02-25 21:36:59 +00002479 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl,
2480 LD->getValueType(0),
2481 PPCLowering->getPointerTy(),
2482 MVT::Other, Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002483 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002484 unsigned Opcode;
2485 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2486 if (LD->getValueType(0) != MVT::i64) {
2487 // Handle PPC32 integer and normal FP loads.
2488 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2489 switch (LoadedVT.getSimpleVT().SimpleTy) {
2490 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00002491 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
2492 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00002493 case MVT::f64: Opcode = PPC::LFDUX; break;
2494 case MVT::f32: Opcode = PPC::LFSUX; break;
2495 case MVT::i32: Opcode = PPC::LWZUX; break;
2496 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2497 case MVT::i1:
2498 case MVT::i8: Opcode = PPC::LBZUX; break;
2499 }
2500 } else {
2501 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2502 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2503 "Invalid sext update load");
2504 switch (LoadedVT.getSimpleVT().SimpleTy) {
2505 default: llvm_unreachable("Invalid PPC load type!");
2506 case MVT::i64: Opcode = PPC::LDUX; break;
2507 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2508 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2509 case MVT::i1:
2510 case MVT::i8: Opcode = PPC::LBZUX8; break;
2511 }
2512 }
2513
2514 SDValue Chain = LD->getChain();
2515 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002516 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelcf599212015-02-25 21:36:59 +00002517 return transferMemOperands(N, CurDAG->getMachineNode(Opcode, dl,
2518 LD->getValueType(0),
2519 PPCLowering->getPointerTy(),
2520 MVT::Other, Ops));
Chris Lattnerce645542006-11-10 02:08:47 +00002521 }
2522 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002523
Nate Begemanb3821a32005-08-18 07:30:46 +00002524 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002525 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002526 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002527
Nate Begemanb3821a32005-08-18 07:30:46 +00002528 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2529 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002530 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002531 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002533 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002534 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00002535 }
Nate Begemand31efd12006-09-22 05:01:56 +00002536 // If this is just a masked value where the input is not handled above, and
2537 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2538 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002539 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002540 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002541 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002542 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002543 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00002544 }
Hal Finkele39526a2012-08-28 02:10:15 +00002545 // If this is a 64-bit zero-extension mask, emit rldicl.
2546 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2547 isMask_64(Imm64)) {
2548 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002549 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002550 SH = 0;
2551
2552 // If the operand is a logical right shift, we can fold it into this
2553 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2554 // for n <= mb. The right shift is really a left rotate followed by a
2555 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2556 // by the shift.
2557 if (Val.getOpcode() == ISD::SRL &&
2558 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2559 assert(Imm < 64 && "Illegal shift amount");
2560 Val = Val.getOperand(0);
2561 SH = 64 - Imm;
2562 }
2563
2564 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00002565 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00002566 }
Nate Begemand31efd12006-09-22 05:01:56 +00002567 // AND X, 0 -> 0, not "rlwinm 32".
2568 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002569 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00002570 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00002571 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002572 // ISD::OR doesn't get all the bitfield insertion fun.
2573 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00002574 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002575 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002576 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00002577 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00002578 Imm = ~(Imm^Imm2);
2579 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002580 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002581 N->getOperand(0).getOperand(1),
2582 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00002583 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00002584 }
2585 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002586
Chris Lattner1de57062005-09-29 23:33:31 +00002587 // Other cases are autogenerated.
2588 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002589 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002590 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002591 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00002592 if (SDNode *I = SelectBitfieldInsert(N))
2593 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00002594
Hal Finkelb5e9b042014-12-11 22:51:06 +00002595 short Imm;
2596 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2597 isIntS16Immediate(N->getOperand(1), Imm)) {
2598 APInt LHSKnownZero, LHSKnownOne;
2599 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2600
2601 // If this is equivalent to an add, then we can fold it with the
2602 // FrameIndex calculation.
2603 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL)
2604 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2605 }
2606
Chris Lattner1de57062005-09-29 23:33:31 +00002607 // Other cases are autogenerated.
2608 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002609 }
2610 case ISD::ADD: {
2611 short Imm;
2612 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2613 isIntS16Immediate(N->getOperand(1), Imm))
2614 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2615
2616 break;
2617 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002618 case ISD::SHL: {
2619 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002620 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002621 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002622 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002623 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002624 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002625 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002626
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002627 // Other cases are autogenerated.
2628 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002629 }
2630 case ISD::SRL: {
2631 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002632 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002633 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002634 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002635 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002636 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002637 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002638
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002639 // Other cases are autogenerated.
2640 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002641 }
Hal Finkel940ab932014-02-28 00:27:01 +00002642 // FIXME: Remove this once the ANDI glue bug is fixed:
2643 case PPCISD::ANDIo_1_EQ_BIT:
2644 case PPCISD::ANDIo_1_GT_BIT: {
2645 if (!ANDIGlueBug)
2646 break;
2647
2648 EVT InVT = N->getOperand(0).getValueType();
2649 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2650 "Invalid input type for ANDIo_1_EQ_BIT");
2651
2652 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2653 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2654 N->getOperand(0),
2655 CurDAG->getTargetConstant(1, InVT)), 0);
2656 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2657 SDValue SRIdxVal =
2658 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
2659 PPC::sub_eq : PPC::sub_gt, MVT::i32);
2660
2661 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
2662 CR0Reg, SRIdxVal,
2663 SDValue(AndI.getNode(), 1) /* glue */);
2664 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002665 case ISD::SELECT_CC: {
2666 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002667 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2668 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002669
Hal Finkel940ab932014-02-28 00:27:01 +00002670 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002671 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002672 N->getOperand(0).getValueType() == MVT::i1)
2673 break;
2674
Chris Lattner97b3da12006-06-27 00:04:13 +00002675 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002676 if (!isPPC64)
2677 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2678 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2679 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2680 if (N1C->isNullValue() && N3C->isNullValue() &&
2681 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2682 // FIXME: Implement this optzn for PPC64.
2683 N->getValueType(0) == MVT::i32) {
2684 SDNode *Tmp =
2685 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
2686 N->getOperand(0), getI32Imm(~0U));
2687 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
2688 SDValue(Tmp, 0), N->getOperand(0),
2689 SDValue(Tmp, 1));
2690 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002691
Dale Johannesenab8e4422009-02-06 19:16:40 +00002692 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002693
2694 if (N->getValueType(0) == MVT::i1) {
2695 // An i1 select is: (c & t) | (!c & f).
2696 bool Inv;
2697 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2698
2699 unsigned SRI;
2700 switch (Idx) {
2701 default: llvm_unreachable("Invalid CC index");
2702 case 0: SRI = PPC::sub_lt; break;
2703 case 1: SRI = PPC::sub_gt; break;
2704 case 2: SRI = PPC::sub_eq; break;
2705 case 3: SRI = PPC::sub_un; break;
2706 }
2707
2708 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2709
2710 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2711 CCBit, CCBit), 0);
2712 SDValue C = Inv ? NotCCBit : CCBit,
2713 NotC = Inv ? CCBit : NotCCBit;
2714
2715 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2716 C, N->getOperand(2)), 0);
2717 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2718 NotC, N->getOperand(3)), 0);
2719
2720 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2721 }
2722
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002723 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002724
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002725 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002726 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002727 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002728 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002729 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002730 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002731 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002732 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002733 if (PPCSubTarget->hasVSX())
2734 SelectCCOp = PPC::SELECT_CC_VSFRC;
2735 else
2736 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002737 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
2738 SelectCCOp = PPC::SELECT_CC_QFRC;
2739 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
2740 SelectCCOp = PPC::SELECT_CC_QSRC;
2741 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
2742 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00002743 else if (N->getValueType(0) == MVT::v2f64 ||
2744 N->getValueType(0) == MVT::v2i64)
2745 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002746 else
2747 SelectCCOp = PPC::SELECT_CC_VRRC;
2748
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002749 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00002750 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00002751 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00002752 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002753 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002754 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002755 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002756 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00002757 }
2758
2759 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002760 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002761 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002762 N->getValueType(0) == MVT::v2i64)) {
2763 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
2764
2765 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2766 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2767 unsigned DM[2];
2768
2769 for (int i = 0; i < 2; ++i)
2770 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2771 DM[i] = 0;
2772 else
2773 DM[i] = 1;
2774
Bill Schmidt30144352014-12-09 16:52:29 +00002775 // For little endian, we must swap the input operands and adjust
2776 // the mask elements (reverse and invert them).
2777 if (PPCSubTarget->isLittleEndian()) {
2778 std::swap(Op1, Op2);
2779 unsigned tmp = DM[0];
2780 DM[0] = 1 - DM[1];
2781 DM[1] = 1 - tmp;
2782 }
2783
Hal Finkel2583b062014-03-28 20:24:55 +00002784 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002785
2786 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2787 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2788 isa<LoadSDNode>(Op1.getOperand(0))) {
2789 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2790 SDValue Base, Offset;
2791
2792 if (LD->isUnindexed() &&
2793 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2794 SDValue Chain = LD->getChain();
2795 SDValue Ops[] = { Base, Offset, Chain };
2796 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00002797 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002798 }
2799 }
2800
2801 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00002802 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002803 }
2804
2805 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002806 case PPCISD::BDNZ:
2807 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002808 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002809 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
2810 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
2811 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
2812 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00002813 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00002814 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002815 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002816 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002817 // Op #1 is the PPC::PRED_* number.
2818 // Op #2 is the CR#
2819 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002820 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002821 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002822 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00002823 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002824 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002825 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00002826 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002827 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002828 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002829 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002830 unsigned PCC = getPredicateForSetCC(CC);
2831
2832 if (N->getOperand(2).getValueType() == MVT::i1) {
2833 unsigned Opc;
2834 bool Swap;
2835 switch (PCC) {
2836 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2837 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2838 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2839 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2840 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2841 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2842 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2843 }
2844
2845 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2846 N->getOperand(Swap ? 3 : 2),
2847 N->getOperand(Swap ? 2 : 3)), 0);
2848 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
2849 BitComp, N->getOperand(4), N->getOperand(0));
2850 }
2851
Dale Johannesenab8e4422009-02-06 19:16:40 +00002852 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002853 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002854 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002855 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002856 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002857 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002858 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002859 SDValue Chain = N->getOperand(0);
2860 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002861 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002862 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002863 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002864 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00002865 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002866 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002867 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002868 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2869 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002870 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2871 SDValue GA = N->getOperand(0);
Hal Finkelcf599212015-02-25 21:36:59 +00002872 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LWZtoc, dl,
2873 MVT::i32, GA, N->getOperand(1)));
Justin Hibbits3476db42014-08-28 04:40:55 +00002874 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002875
Bill Schmidt27917782013-02-21 17:12:27 +00002876 // For medium and large code model, we generate two instructions as
2877 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002878 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00002879 CodeModel::Model CModel = TM.getCodeModel();
2880 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002881 break;
2882
Bill Schmidt5d82f092014-06-16 21:36:02 +00002883 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
2884 // If it is an externally defined symbol, a symbol with common linkage,
2885 // a non-local function address, or a jump table address, or if we are
2886 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00002887 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
2888 // Otherwise we generate:
2889 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
2890 SDValue GA = N->getOperand(0);
2891 SDValue TOCbase = N->getOperand(1);
2892 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00002893 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00002894
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002895 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
2896 CModel == CodeModel::Large)
Hal Finkelcf599212015-02-25 21:36:59 +00002897 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2898 MVT::i64, GA, SDValue(Tmp, 0)));
Bill Schmidt34627e32012-11-27 17:35:46 +00002899
2900 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
2901 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00002902 if ((GValue->getType()->getElementType()->isFunctionTy() &&
2903 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00002904 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
2905 GValue->hasAvailableExternallyLinkage())
Hal Finkelcf599212015-02-25 21:36:59 +00002906 return transferMemOperands(N, CurDAG->getMachineNode(PPC::LDtocL, dl,
2907 MVT::i64, GA, SDValue(Tmp, 0)));
Bill Schmidt34627e32012-11-27 17:35:46 +00002908 }
2909
2910 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
2911 SDValue(Tmp, 0), GA);
2912 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00002913 case PPCISD::PPC32_PICGOT: {
2914 // Generate a PIC-safe GOT reference.
2915 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
2916 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
2917 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
2918 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002919 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002920 // This expands into one of three sequences, depending on whether
2921 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00002922 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
2923 isa<ConstantSDNode>(N->getOperand(1)) &&
2924 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002925
2926 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00002927 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002928 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00002929 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002930
Bill Schmidt51e79512013-02-20 15:50:31 +00002931 if (EltSize == 1) {
2932 Opc1 = PPC::VSPLTISB;
2933 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002934 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002935 VT = MVT::v16i8;
2936 } else if (EltSize == 2) {
2937 Opc1 = PPC::VSPLTISH;
2938 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002939 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002940 VT = MVT::v8i16;
2941 } else {
2942 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
2943 Opc1 = PPC::VSPLTISW;
2944 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002945 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002946 VT = MVT::v4i32;
2947 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002948
2949 if ((Elt & 1) == 0) {
2950 // Elt is even, in the range [-32,-18] + [16,30].
2951 //
2952 // Convert: VADD_SPLAT elt, size
2953 // Into: tmp = VSPLTIS[BHW] elt
2954 // VADDU[BHW]M tmp, tmp
2955 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
2956 SDValue EltVal = getI32Imm(Elt >> 1);
2957 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2958 SDValue TmpVal = SDValue(Tmp, 0);
2959 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
2960
2961 } else if (Elt > 0) {
2962 // Elt is odd and positive, in the range [17,31].
2963 //
2964 // Convert: VADD_SPLAT elt, size
2965 // Into: tmp1 = VSPLTIS[BHW] elt-16
2966 // tmp2 = VSPLTIS[BHW] -16
2967 // VSUBU[BHW]M tmp1, tmp2
2968 SDValue EltVal = getI32Imm(Elt - 16);
2969 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2970 EltVal = getI32Imm(-16);
2971 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2972 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
2973 SDValue(Tmp2, 0));
2974
2975 } else {
2976 // Elt is odd and negative, in the range [-31,-17].
2977 //
2978 // Convert: VADD_SPLAT elt, size
2979 // Into: tmp1 = VSPLTIS[BHW] elt+16
2980 // tmp2 = VSPLTIS[BHW] -16
2981 // VADDU[BHW]M tmp1, tmp2
2982 SDValue EltVal = getI32Imm(Elt + 16);
2983 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2984 EltVal = getI32Imm(-16);
2985 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2986 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
2987 SDValue(Tmp2, 0));
2988 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002989 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00002990 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002991
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002992 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002993}
2994
Hal Finkel4edc66b2015-01-03 01:16:37 +00002995// If the target supports the cmpb instruction, do the idiom recognition here.
2996// We don't do this as a DAG combine because we don't want to do it as nodes
2997// are being combined (because we might miss part of the eventual idiom). We
2998// don't want to do it during instruction selection because we want to reuse
2999// the logic for lowering the masking operations already part of the
3000// instruction selector.
3001SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3002 SDLoc dl(N);
3003
3004 assert(N->getOpcode() == ISD::OR &&
3005 "Only OR nodes are supported for CMPB");
3006
3007 SDValue Res;
3008 if (!PPCSubTarget->hasCMPB())
3009 return Res;
3010
3011 if (N->getValueType(0) != MVT::i32 &&
3012 N->getValueType(0) != MVT::i64)
3013 return Res;
3014
3015 EVT VT = N->getValueType(0);
3016
3017 SDValue RHS, LHS;
3018 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
3019 uint64_t Mask = 0, Alt = 0;
3020
3021 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3022 uint64_t &Mask, uint64_t &Alt,
3023 SDValue &LHS, SDValue &RHS) {
3024 if (O.getOpcode() != ISD::SELECT_CC)
3025 return false;
3026 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3027
3028 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3029 !isa<ConstantSDNode>(O.getOperand(3)))
3030 return false;
3031
3032 uint64_t PM = O.getConstantOperandVal(2);
3033 uint64_t PAlt = O.getConstantOperandVal(3);
3034 for (b = 0; b < 8; ++b) {
3035 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3036 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3037 break;
3038 }
3039
3040 if (b == 8)
3041 return false;
3042 Mask |= PM;
3043 Alt |= PAlt;
3044
3045 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3046 O.getConstantOperandVal(1) != 0) {
3047 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3048 if (Op0.getOpcode() == ISD::TRUNCATE)
3049 Op0 = Op0.getOperand(0);
3050 if (Op1.getOpcode() == ISD::TRUNCATE)
3051 Op1 = Op1.getOperand(0);
3052
3053 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3054 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3055 isa<ConstantSDNode>(Op0.getOperand(1))) {
3056
3057 unsigned Bits = Op0.getValueType().getSizeInBits();
3058 if (b != Bits/8-1)
3059 return false;
3060 if (Op0.getConstantOperandVal(1) != Bits-8)
3061 return false;
3062
3063 LHS = Op0.getOperand(0);
3064 RHS = Op1.getOperand(0);
3065 return true;
3066 }
3067
3068 // When we have small integers (i16 to be specific), the form present
3069 // post-legalization uses SETULT in the SELECT_CC for the
3070 // higher-order byte, depending on the fact that the
3071 // even-higher-order bytes are known to all be zero, for example:
3072 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3073 // (so when the second byte is the same, because all higher-order
3074 // bits from bytes 3 and 4 are known to be zero, the result of the
3075 // xor can be at most 255)
3076 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3077 isa<ConstantSDNode>(O.getOperand(1))) {
3078
3079 uint64_t ULim = O.getConstantOperandVal(1);
3080 if (ULim != (UINT64_C(1) << b*8))
3081 return false;
3082
3083 // Now we need to make sure that the upper bytes are known to be
3084 // zero.
3085 unsigned Bits = Op0.getValueType().getSizeInBits();
3086 if (!CurDAG->MaskedValueIsZero(Op0,
3087 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
3088 return false;
3089
3090 LHS = Op0.getOperand(0);
3091 RHS = Op0.getOperand(1);
3092 return true;
3093 }
3094
3095 return false;
3096 }
3097
3098 if (CC != ISD::SETEQ)
3099 return false;
3100
3101 SDValue Op = O.getOperand(0);
3102 if (Op.getOpcode() == ISD::AND) {
3103 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3104 return false;
3105 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3106 return false;
3107
3108 SDValue XOR = Op.getOperand(0);
3109 if (XOR.getOpcode() == ISD::TRUNCATE)
3110 XOR = XOR.getOperand(0);
3111 if (XOR.getOpcode() != ISD::XOR)
3112 return false;
3113
3114 LHS = XOR.getOperand(0);
3115 RHS = XOR.getOperand(1);
3116 return true;
3117 } else if (Op.getOpcode() == ISD::SRL) {
3118 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3119 return false;
3120 unsigned Bits = Op.getValueType().getSizeInBits();
3121 if (b != Bits/8-1)
3122 return false;
3123 if (Op.getConstantOperandVal(1) != Bits-8)
3124 return false;
3125
3126 SDValue XOR = Op.getOperand(0);
3127 if (XOR.getOpcode() == ISD::TRUNCATE)
3128 XOR = XOR.getOperand(0);
3129 if (XOR.getOpcode() != ISD::XOR)
3130 return false;
3131
3132 LHS = XOR.getOperand(0);
3133 RHS = XOR.getOperand(1);
3134 return true;
3135 }
3136
3137 return false;
3138 };
3139
3140 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3141 while (!Queue.empty()) {
3142 SDValue V = Queue.pop_back_val();
3143
3144 for (const SDValue &O : V.getNode()->ops()) {
3145 unsigned b;
3146 uint64_t M = 0, A = 0;
3147 SDValue OLHS, ORHS;
3148 if (O.getOpcode() == ISD::OR) {
3149 Queue.push_back(O);
3150 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3151 if (!LHS) {
3152 LHS = OLHS;
3153 RHS = ORHS;
3154 BytesFound[b] = true;
3155 Mask |= M;
3156 Alt |= A;
3157 } else if ((LHS == ORHS && RHS == OLHS) ||
3158 (RHS == ORHS && LHS == OLHS)) {
3159 BytesFound[b] = true;
3160 Mask |= M;
3161 Alt |= A;
3162 } else {
3163 return Res;
3164 }
3165 } else {
3166 return Res;
3167 }
3168 }
3169 }
3170
3171 unsigned LastB = 0, BCnt = 0;
3172 for (unsigned i = 0; i < 8; ++i)
3173 if (BytesFound[LastB]) {
3174 ++BCnt;
3175 LastB = i;
3176 }
3177
3178 if (!LastB || BCnt < 2)
3179 return Res;
3180
3181 // Because we'll be zero-extending the output anyway if don't have a specific
3182 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3183 if (LHS.getValueType() != VT) {
3184 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3185 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3186 }
3187
3188 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3189
3190 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3191 if (NonTrivialMask && !Alt) {
3192 // Res = Mask & CMPB
3193 Res = CurDAG->getNode(ISD::AND, dl, VT, Res, CurDAG->getConstant(Mask, VT));
3194 } else if (Alt) {
3195 // Res = (CMPB & Mask) | (~CMPB & Alt)
3196 // Which, as suggested here:
3197 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3198 // can be written as:
3199 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3200 // useful because the (Alt ^ Mask) can be pre-computed.
3201 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3202 CurDAG->getConstant(Mask ^ Alt, VT));
3203 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, CurDAG->getConstant(Alt, VT));
3204 }
3205
3206 return Res;
3207}
3208
Hal Finkel200d2ad2015-01-05 21:10:24 +00003209// When CR bit registers are enabled, an extension of an i1 variable to a i32
3210// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3211// involves constant materialization of a 0 or a 1 or both. If the result of
3212// the extension is then operated upon by some operator that can be constant
3213// folded with a constant 0 or 1, and that constant can be materialized using
3214// only one instruction (like a zero or one), then we should fold in those
3215// operations with the select.
3216void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3217 if (!PPCSubTarget->useCRBits())
3218 return;
3219
3220 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3221 N->getOpcode() != ISD::SIGN_EXTEND &&
3222 N->getOpcode() != ISD::ANY_EXTEND)
3223 return;
3224
3225 if (N->getOperand(0).getValueType() != MVT::i1)
3226 return;
3227
3228 if (!N->hasOneUse())
3229 return;
3230
3231 SDLoc dl(N);
3232 EVT VT = N->getValueType(0);
3233 SDValue Cond = N->getOperand(0);
3234 SDValue ConstTrue =
3235 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, VT);
3236 SDValue ConstFalse = CurDAG->getConstant(0, VT);
3237
3238 do {
3239 SDNode *User = *N->use_begin();
3240 if (User->getNumOperands() != 2)
3241 break;
3242
3243 auto TryFold = [this, N, User](SDValue Val) {
3244 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3245 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3246 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3247
3248 return CurDAG->FoldConstantArithmetic(User->getOpcode(),
3249 User->getValueType(0),
3250 O0.getNode(), O1.getNode());
3251 };
3252
3253 SDValue TrueRes = TryFold(ConstTrue);
3254 if (!TrueRes)
3255 break;
3256 SDValue FalseRes = TryFold(ConstFalse);
3257 if (!FalseRes)
3258 break;
3259
3260 // For us to materialize these using one instruction, we must be able to
3261 // represent them as signed 16-bit integers.
3262 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
3263 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
3264 if (!isInt<16>(True) || !isInt<16>(False))
3265 break;
3266
3267 // We can replace User with a new SELECT node, and try again to see if we
3268 // can fold the select with its user.
3269 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
3270 N = User;
3271 ConstTrue = TrueRes;
3272 ConstFalse = FalseRes;
3273 } while (N->hasOneUse());
3274}
3275
Hal Finkel4edc66b2015-01-03 01:16:37 +00003276void PPCDAGToDAGISel::PreprocessISelDAG() {
3277 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3278 ++Position;
3279
3280 bool MadeChange = false;
3281 while (Position != CurDAG->allnodes_begin()) {
3282 SDNode *N = --Position;
3283 if (N->use_empty())
3284 continue;
3285
3286 SDValue Res;
3287 switch (N->getOpcode()) {
3288 default: break;
3289 case ISD::OR:
3290 Res = combineToCMPB(N);
3291 break;
3292 }
3293
Hal Finkel200d2ad2015-01-05 21:10:24 +00003294 if (!Res)
3295 foldBoolExts(Res, N);
3296
Hal Finkel4edc66b2015-01-03 01:16:37 +00003297 if (Res) {
3298 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3299 DEBUG(N->dump(CurDAG));
3300 DEBUG(dbgs() << "\nNew: ");
3301 DEBUG(Res.getNode()->dump(CurDAG));
3302 DEBUG(dbgs() << "\n");
3303
3304 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3305 MadeChange = true;
3306 }
3307 }
3308
3309 if (MadeChange)
3310 CurDAG->RemoveDeadNodes();
3311}
3312
Hal Finkel860fa902014-01-02 22:09:39 +00003313/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003314/// on the DAG representation.
3315void PPCDAGToDAGISel::PostprocessISelDAG() {
3316
3317 // Skip peepholes at -O0.
3318 if (TM.getOptLevel() == CodeGenOpt::None)
3319 return;
3320
Hal Finkel940ab932014-02-28 00:27:01 +00003321 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003322 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003323 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003324}
3325
Hal Finkelb9989152014-02-28 06:11:16 +00003326// Check if all users of this node will become isel where the second operand
3327// is the constant zero. If this is so, and if we can negate the condition,
3328// then we can flip the true and false operands. This will allow the zero to
3329// be folded with the isel so that we don't need to materialize a register
3330// containing zero.
3331bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3332 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003333 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003334 return false;
3335
3336 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3337 UI != UE; ++UI) {
3338 SDNode *User = *UI;
3339 if (!User->isMachineOpcode())
3340 return false;
3341 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3342 User->getMachineOpcode() != PPC::SELECT_I8)
3343 return false;
3344
3345 SDNode *Op2 = User->getOperand(2).getNode();
3346 if (!Op2->isMachineOpcode())
3347 return false;
3348
3349 if (Op2->getMachineOpcode() != PPC::LI &&
3350 Op2->getMachineOpcode() != PPC::LI8)
3351 return false;
3352
3353 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3354 if (!C)
3355 return false;
3356
3357 if (!C->isNullValue())
3358 return false;
3359 }
3360
3361 return true;
3362}
3363
3364void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3365 SmallVector<SDNode *, 4> ToReplace;
3366 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3367 UI != UE; ++UI) {
3368 SDNode *User = *UI;
3369 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3370 User->getMachineOpcode() == PPC::SELECT_I8) &&
3371 "Must have all select users");
3372 ToReplace.push_back(User);
3373 }
3374
3375 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3376 UE = ToReplace.end(); UI != UE; ++UI) {
3377 SDNode *User = *UI;
3378 SDNode *ResNode =
3379 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3380 User->getValueType(0), User->getOperand(0),
3381 User->getOperand(2),
3382 User->getOperand(1));
3383
3384 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3385 DEBUG(User->dump(CurDAG));
3386 DEBUG(dbgs() << "\nNew: ");
3387 DEBUG(ResNode->dump(CurDAG));
3388 DEBUG(dbgs() << "\n");
3389
3390 ReplaceUses(User, ResNode);
3391 }
3392}
3393
Eric Christopher02e18042014-05-14 00:31:15 +00003394void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003395 bool IsModified;
3396 do {
3397 IsModified = false;
3398 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
3399 E = CurDAG->allnodes_end(); I != E; ++I) {
3400 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
3401 if (!MachineNode || MachineNode->use_empty())
3402 continue;
3403 SDNode *ResNode = MachineNode;
3404
3405 bool Op1Set = false, Op1Unset = false,
3406 Op1Not = false,
3407 Op2Set = false, Op2Unset = false,
3408 Op2Not = false;
3409
3410 unsigned Opcode = MachineNode->getMachineOpcode();
3411 switch (Opcode) {
3412 default: break;
3413 case PPC::CRAND:
3414 case PPC::CRNAND:
3415 case PPC::CROR:
3416 case PPC::CRXOR:
3417 case PPC::CRNOR:
3418 case PPC::CREQV:
3419 case PPC::CRANDC:
3420 case PPC::CRORC: {
3421 SDValue Op = MachineNode->getOperand(1);
3422 if (Op.isMachineOpcode()) {
3423 if (Op.getMachineOpcode() == PPC::CRSET)
3424 Op2Set = true;
3425 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3426 Op2Unset = true;
3427 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3428 Op.getOperand(0) == Op.getOperand(1))
3429 Op2Not = true;
3430 }
3431 } // fallthrough
3432 case PPC::BC:
3433 case PPC::BCn:
3434 case PPC::SELECT_I4:
3435 case PPC::SELECT_I8:
3436 case PPC::SELECT_F4:
3437 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003438 case PPC::SELECT_QFRC:
3439 case PPC::SELECT_QSRC:
3440 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003441 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003442 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003443 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003444 SDValue Op = MachineNode->getOperand(0);
3445 if (Op.isMachineOpcode()) {
3446 if (Op.getMachineOpcode() == PPC::CRSET)
3447 Op1Set = true;
3448 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3449 Op1Unset = true;
3450 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3451 Op.getOperand(0) == Op.getOperand(1))
3452 Op1Not = true;
3453 }
3454 }
3455 break;
3456 }
3457
Hal Finkelb9989152014-02-28 06:11:16 +00003458 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003459 switch (Opcode) {
3460 default: break;
3461 case PPC::CRAND:
3462 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3463 // x & x = x
3464 ResNode = MachineNode->getOperand(0).getNode();
3465 else if (Op1Set)
3466 // 1 & y = y
3467 ResNode = MachineNode->getOperand(1).getNode();
3468 else if (Op2Set)
3469 // x & 1 = x
3470 ResNode = MachineNode->getOperand(0).getNode();
3471 else if (Op1Unset || Op2Unset)
3472 // x & 0 = 0 & y = 0
3473 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3474 MVT::i1);
3475 else if (Op1Not)
3476 // ~x & y = andc(y, x)
3477 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3478 MVT::i1, MachineNode->getOperand(1),
3479 MachineNode->getOperand(0).
3480 getOperand(0));
3481 else if (Op2Not)
3482 // x & ~y = andc(x, y)
3483 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3484 MVT::i1, MachineNode->getOperand(0),
3485 MachineNode->getOperand(1).
3486 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003487 else if (AllUsersSelectZero(MachineNode))
3488 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3489 MVT::i1, MachineNode->getOperand(0),
3490 MachineNode->getOperand(1)),
3491 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003492 break;
3493 case PPC::CRNAND:
3494 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3495 // nand(x, x) -> nor(x, x)
3496 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3497 MVT::i1, MachineNode->getOperand(0),
3498 MachineNode->getOperand(0));
3499 else if (Op1Set)
3500 // nand(1, y) -> nor(y, y)
3501 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3502 MVT::i1, MachineNode->getOperand(1),
3503 MachineNode->getOperand(1));
3504 else if (Op2Set)
3505 // nand(x, 1) -> nor(x, x)
3506 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3507 MVT::i1, MachineNode->getOperand(0),
3508 MachineNode->getOperand(0));
3509 else if (Op1Unset || Op2Unset)
3510 // nand(x, 0) = nand(0, y) = 1
3511 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3512 MVT::i1);
3513 else if (Op1Not)
3514 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3515 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3516 MVT::i1, MachineNode->getOperand(0).
3517 getOperand(0),
3518 MachineNode->getOperand(1));
3519 else if (Op2Not)
3520 // nand(x, ~y) = ~x | y = orc(y, x)
3521 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3522 MVT::i1, MachineNode->getOperand(1).
3523 getOperand(0),
3524 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003525 else if (AllUsersSelectZero(MachineNode))
3526 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3527 MVT::i1, MachineNode->getOperand(0),
3528 MachineNode->getOperand(1)),
3529 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003530 break;
3531 case PPC::CROR:
3532 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3533 // x | x = x
3534 ResNode = MachineNode->getOperand(0).getNode();
3535 else if (Op1Set || Op2Set)
3536 // x | 1 = 1 | y = 1
3537 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3538 MVT::i1);
3539 else if (Op1Unset)
3540 // 0 | y = y
3541 ResNode = MachineNode->getOperand(1).getNode();
3542 else if (Op2Unset)
3543 // x | 0 = x
3544 ResNode = MachineNode->getOperand(0).getNode();
3545 else if (Op1Not)
3546 // ~x | y = orc(y, x)
3547 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3548 MVT::i1, MachineNode->getOperand(1),
3549 MachineNode->getOperand(0).
3550 getOperand(0));
3551 else if (Op2Not)
3552 // x | ~y = orc(x, y)
3553 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3554 MVT::i1, MachineNode->getOperand(0),
3555 MachineNode->getOperand(1).
3556 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003557 else if (AllUsersSelectZero(MachineNode))
3558 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3559 MVT::i1, MachineNode->getOperand(0),
3560 MachineNode->getOperand(1)),
3561 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003562 break;
3563 case PPC::CRXOR:
3564 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3565 // xor(x, x) = 0
3566 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3567 MVT::i1);
3568 else if (Op1Set)
3569 // xor(1, y) -> nor(y, y)
3570 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3571 MVT::i1, MachineNode->getOperand(1),
3572 MachineNode->getOperand(1));
3573 else if (Op2Set)
3574 // xor(x, 1) -> nor(x, x)
3575 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3576 MVT::i1, MachineNode->getOperand(0),
3577 MachineNode->getOperand(0));
3578 else if (Op1Unset)
3579 // xor(0, y) = y
3580 ResNode = MachineNode->getOperand(1).getNode();
3581 else if (Op2Unset)
3582 // xor(x, 0) = x
3583 ResNode = MachineNode->getOperand(0).getNode();
3584 else if (Op1Not)
3585 // xor(~x, y) = eqv(x, y)
3586 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3587 MVT::i1, MachineNode->getOperand(0).
3588 getOperand(0),
3589 MachineNode->getOperand(1));
3590 else if (Op2Not)
3591 // xor(x, ~y) = eqv(x, y)
3592 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3593 MVT::i1, MachineNode->getOperand(0),
3594 MachineNode->getOperand(1).
3595 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003596 else if (AllUsersSelectZero(MachineNode))
3597 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3598 MVT::i1, MachineNode->getOperand(0),
3599 MachineNode->getOperand(1)),
3600 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003601 break;
3602 case PPC::CRNOR:
3603 if (Op1Set || Op2Set)
3604 // nor(1, y) -> 0
3605 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3606 MVT::i1);
3607 else if (Op1Unset)
3608 // nor(0, y) = ~y -> nor(y, y)
3609 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3610 MVT::i1, MachineNode->getOperand(1),
3611 MachineNode->getOperand(1));
3612 else if (Op2Unset)
3613 // nor(x, 0) = ~x
3614 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3615 MVT::i1, MachineNode->getOperand(0),
3616 MachineNode->getOperand(0));
3617 else if (Op1Not)
3618 // nor(~x, y) = andc(x, y)
3619 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3620 MVT::i1, MachineNode->getOperand(0).
3621 getOperand(0),
3622 MachineNode->getOperand(1));
3623 else if (Op2Not)
3624 // nor(x, ~y) = andc(y, x)
3625 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3626 MVT::i1, MachineNode->getOperand(1).
3627 getOperand(0),
3628 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003629 else if (AllUsersSelectZero(MachineNode))
3630 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3631 MVT::i1, MachineNode->getOperand(0),
3632 MachineNode->getOperand(1)),
3633 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003634 break;
3635 case PPC::CREQV:
3636 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3637 // eqv(x, x) = 1
3638 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3639 MVT::i1);
3640 else if (Op1Set)
3641 // eqv(1, y) = y
3642 ResNode = MachineNode->getOperand(1).getNode();
3643 else if (Op2Set)
3644 // eqv(x, 1) = x
3645 ResNode = MachineNode->getOperand(0).getNode();
3646 else if (Op1Unset)
3647 // eqv(0, y) = ~y -> nor(y, y)
3648 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3649 MVT::i1, MachineNode->getOperand(1),
3650 MachineNode->getOperand(1));
3651 else if (Op2Unset)
3652 // eqv(x, 0) = ~x
3653 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3654 MVT::i1, MachineNode->getOperand(0),
3655 MachineNode->getOperand(0));
3656 else if (Op1Not)
3657 // eqv(~x, y) = xor(x, y)
3658 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3659 MVT::i1, MachineNode->getOperand(0).
3660 getOperand(0),
3661 MachineNode->getOperand(1));
3662 else if (Op2Not)
3663 // eqv(x, ~y) = xor(x, y)
3664 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3665 MVT::i1, MachineNode->getOperand(0),
3666 MachineNode->getOperand(1).
3667 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003668 else if (AllUsersSelectZero(MachineNode))
3669 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3670 MVT::i1, MachineNode->getOperand(0),
3671 MachineNode->getOperand(1)),
3672 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003673 break;
3674 case PPC::CRANDC:
3675 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3676 // andc(x, x) = 0
3677 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3678 MVT::i1);
3679 else if (Op1Set)
3680 // andc(1, y) = ~y
3681 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3682 MVT::i1, MachineNode->getOperand(1),
3683 MachineNode->getOperand(1));
3684 else if (Op1Unset || Op2Set)
3685 // andc(0, y) = andc(x, 1) = 0
3686 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3687 MVT::i1);
3688 else if (Op2Unset)
3689 // andc(x, 0) = x
3690 ResNode = MachineNode->getOperand(0).getNode();
3691 else if (Op1Not)
3692 // andc(~x, y) = ~(x | y) = nor(x, y)
3693 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3694 MVT::i1, MachineNode->getOperand(0).
3695 getOperand(0),
3696 MachineNode->getOperand(1));
3697 else if (Op2Not)
3698 // andc(x, ~y) = x & y
3699 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3700 MVT::i1, MachineNode->getOperand(0),
3701 MachineNode->getOperand(1).
3702 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003703 else if (AllUsersSelectZero(MachineNode))
3704 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3705 MVT::i1, MachineNode->getOperand(1),
3706 MachineNode->getOperand(0)),
3707 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003708 break;
3709 case PPC::CRORC:
3710 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3711 // orc(x, x) = 1
3712 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3713 MVT::i1);
3714 else if (Op1Set || Op2Unset)
3715 // orc(1, y) = orc(x, 0) = 1
3716 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3717 MVT::i1);
3718 else if (Op2Set)
3719 // orc(x, 1) = x
3720 ResNode = MachineNode->getOperand(0).getNode();
3721 else if (Op1Unset)
3722 // orc(0, y) = ~y
3723 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3724 MVT::i1, MachineNode->getOperand(1),
3725 MachineNode->getOperand(1));
3726 else if (Op1Not)
3727 // orc(~x, y) = ~(x & y) = nand(x, y)
3728 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3729 MVT::i1, MachineNode->getOperand(0).
3730 getOperand(0),
3731 MachineNode->getOperand(1));
3732 else if (Op2Not)
3733 // orc(x, ~y) = x | y
3734 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3735 MVT::i1, MachineNode->getOperand(0),
3736 MachineNode->getOperand(1).
3737 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003738 else if (AllUsersSelectZero(MachineNode))
3739 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3740 MVT::i1, MachineNode->getOperand(1),
3741 MachineNode->getOperand(0)),
3742 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003743 break;
3744 case PPC::SELECT_I4:
3745 case PPC::SELECT_I8:
3746 case PPC::SELECT_F4:
3747 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00003748 case PPC::SELECT_QFRC:
3749 case PPC::SELECT_QSRC:
3750 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003751 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003752 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003753 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003754 if (Op1Set)
3755 ResNode = MachineNode->getOperand(1).getNode();
3756 else if (Op1Unset)
3757 ResNode = MachineNode->getOperand(2).getNode();
3758 else if (Op1Not)
3759 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3760 SDLoc(MachineNode),
3761 MachineNode->getValueType(0),
3762 MachineNode->getOperand(0).
3763 getOperand(0),
3764 MachineNode->getOperand(2),
3765 MachineNode->getOperand(1));
3766 break;
3767 case PPC::BC:
3768 case PPC::BCn:
3769 if (Op1Not)
3770 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3771 PPC::BC,
3772 SDLoc(MachineNode),
3773 MVT::Other,
3774 MachineNode->getOperand(0).
3775 getOperand(0),
3776 MachineNode->getOperand(1),
3777 MachineNode->getOperand(2));
3778 // FIXME: Handle Op1Set, Op1Unset here too.
3779 break;
3780 }
3781
Hal Finkelb9989152014-02-28 06:11:16 +00003782 // If we're inverting this node because it is used only by selects that
3783 // we'd like to swap, then swap the selects before the node replacement.
3784 if (SelectSwap)
3785 SwapAllSelectUsers(MachineNode);
3786
Hal Finkel940ab932014-02-28 00:27:01 +00003787 if (ResNode != MachineNode) {
3788 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3789 DEBUG(MachineNode->dump(CurDAG));
3790 DEBUG(dbgs() << "\nNew: ");
3791 DEBUG(ResNode->dump(CurDAG));
3792 DEBUG(dbgs() << "\n");
3793
3794 ReplaceUses(MachineNode, ResNode);
3795 IsModified = true;
3796 }
3797 }
3798 if (IsModified)
3799 CurDAG->RemoveDeadNodes();
3800 } while (IsModified);
3801}
3802
Hal Finkel4c6658f2014-12-12 23:59:36 +00003803// Gather the set of 32-bit operations that are known to have their
3804// higher-order 32 bits zero, where ToPromote contains all such operations.
3805static bool PeepholePPC64ZExtGather(SDValue Op32,
3806 SmallPtrSetImpl<SDNode *> &ToPromote) {
3807 if (!Op32.isMachineOpcode())
3808 return false;
3809
3810 // First, check for the "frontier" instructions (those that will clear the
3811 // higher-order 32 bits.
3812
3813 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3814 // around. If it does not, then these instructions will clear the
3815 // higher-order bits.
3816 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3817 Op32.getMachineOpcode() == PPC::RLWNM) &&
3818 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3819 ToPromote.insert(Op32.getNode());
3820 return true;
3821 }
3822
3823 // SLW and SRW always clear the higher-order bits.
3824 if (Op32.getMachineOpcode() == PPC::SLW ||
3825 Op32.getMachineOpcode() == PPC::SRW) {
3826 ToPromote.insert(Op32.getNode());
3827 return true;
3828 }
3829
3830 // For LI and LIS, we need the immediate to be positive (so that it is not
3831 // sign extended).
3832 if (Op32.getMachineOpcode() == PPC::LI ||
3833 Op32.getMachineOpcode() == PPC::LIS) {
3834 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3835 return false;
3836
3837 ToPromote.insert(Op32.getNode());
3838 return true;
3839 }
3840
Hal Finkel4e2c7822015-01-05 18:09:06 +00003841 // LHBRX and LWBRX always clear the higher-order bits.
3842 if (Op32.getMachineOpcode() == PPC::LHBRX ||
3843 Op32.getMachineOpcode() == PPC::LWBRX) {
3844 ToPromote.insert(Op32.getNode());
3845 return true;
3846 }
3847
Hal Finkel49557f12015-01-05 18:52:29 +00003848 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended.
3849 if (Op32.getMachineOpcode() == PPC::CNTLZW) {
3850 ToPromote.insert(Op32.getNode());
3851 return true;
3852 }
3853
Hal Finkel4c6658f2014-12-12 23:59:36 +00003854 // Next, check for those instructions we can look through.
3855
3856 // Assuming the mask does not wrap around, then the higher-order bits are
3857 // taken directly from the first operand.
3858 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
3859 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
3860 SmallPtrSet<SDNode *, 16> ToPromote1;
3861 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3862 return false;
3863
3864 ToPromote.insert(Op32.getNode());
3865 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3866 return true;
3867 }
3868
3869 // For OR, the higher-order bits are zero if that is true for both operands.
3870 // For SELECT_I4, the same is true (but the relevant operand numbers are
3871 // shifted by 1).
3872 if (Op32.getMachineOpcode() == PPC::OR ||
3873 Op32.getMachineOpcode() == PPC::SELECT_I4) {
3874 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
3875 SmallPtrSet<SDNode *, 16> ToPromote1;
3876 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
3877 return false;
3878 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
3879 return false;
3880
3881 ToPromote.insert(Op32.getNode());
3882 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3883 return true;
3884 }
3885
3886 // For ORI and ORIS, we need the higher-order bits of the first operand to be
3887 // zero, and also for the constant to be positive (so that it is not sign
3888 // extended).
3889 if (Op32.getMachineOpcode() == PPC::ORI ||
3890 Op32.getMachineOpcode() == PPC::ORIS) {
3891 SmallPtrSet<SDNode *, 16> ToPromote1;
3892 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3893 return false;
3894 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
3895 return false;
3896
3897 ToPromote.insert(Op32.getNode());
3898 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3899 return true;
3900 }
3901
3902 // The higher-order bits of AND are zero if that is true for at least one of
3903 // the operands.
3904 if (Op32.getMachineOpcode() == PPC::AND) {
3905 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
3906 bool Op0OK =
3907 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3908 bool Op1OK =
3909 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
3910 if (!Op0OK && !Op1OK)
3911 return false;
3912
3913 ToPromote.insert(Op32.getNode());
3914
3915 if (Op0OK)
3916 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3917
3918 if (Op1OK)
3919 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
3920
3921 return true;
3922 }
3923
3924 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
3925 // of the first operand, or if the second operand is positive (so that it is
3926 // not sign extended).
3927 if (Op32.getMachineOpcode() == PPC::ANDIo ||
3928 Op32.getMachineOpcode() == PPC::ANDISo) {
3929 SmallPtrSet<SDNode *, 16> ToPromote1;
3930 bool Op0OK =
3931 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3932 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
3933 if (!Op0OK && !Op1OK)
3934 return false;
3935
3936 ToPromote.insert(Op32.getNode());
3937
3938 if (Op0OK)
3939 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3940
3941 return true;
3942 }
3943
3944 return false;
3945}
3946
3947void PPCDAGToDAGISel::PeepholePPC64ZExt() {
3948 if (!PPCSubTarget->isPPC64())
3949 return;
3950
3951 // When we zero-extend from i32 to i64, we use a pattern like this:
3952 // def : Pat<(i64 (zext i32:$in)),
3953 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
3954 // 0, 32)>;
3955 // There are several 32-bit shift/rotate instructions, however, that will
3956 // clear the higher-order bits of their output, rendering the RLDICL
3957 // unnecessary. When that happens, we remove it here, and redefine the
3958 // relevant 32-bit operation to be a 64-bit operation.
3959
3960 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3961 ++Position;
3962
3963 bool MadeChange = false;
3964 while (Position != CurDAG->allnodes_begin()) {
3965 SDNode *N = --Position;
3966 // Skip dead nodes and any non-machine opcodes.
3967 if (N->use_empty() || !N->isMachineOpcode())
3968 continue;
3969
3970 if (N->getMachineOpcode() != PPC::RLDICL)
3971 continue;
3972
3973 if (N->getConstantOperandVal(1) != 0 ||
3974 N->getConstantOperandVal(2) != 32)
3975 continue;
3976
3977 SDValue ISR = N->getOperand(0);
3978 if (!ISR.isMachineOpcode() ||
3979 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
3980 continue;
3981
3982 if (!ISR.hasOneUse())
3983 continue;
3984
3985 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
3986 continue;
3987
3988 SDValue IDef = ISR.getOperand(0);
3989 if (!IDef.isMachineOpcode() ||
3990 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
3991 continue;
3992
3993 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
3994 // can get rid of it.
3995
3996 SDValue Op32 = ISR->getOperand(1);
3997 if (!Op32.isMachineOpcode())
3998 continue;
3999
4000 // There are some 32-bit instructions that always clear the high-order 32
4001 // bits, there are also some instructions (like AND) that we can look
4002 // through.
4003 SmallPtrSet<SDNode *, 16> ToPromote;
4004 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4005 continue;
4006
4007 // If the ToPromote set contains nodes that have uses outside of the set
4008 // (except for the original INSERT_SUBREG), then abort the transformation.
4009 bool OutsideUse = false;
4010 for (SDNode *PN : ToPromote) {
4011 for (SDNode *UN : PN->uses()) {
4012 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4013 OutsideUse = true;
4014 break;
4015 }
4016 }
4017
4018 if (OutsideUse)
4019 break;
4020 }
4021 if (OutsideUse)
4022 continue;
4023
4024 MadeChange = true;
4025
4026 // We now know that this zero extension can be removed by promoting to
4027 // nodes in ToPromote to 64-bit operations, where for operations in the
4028 // frontier of the set, we need to insert INSERT_SUBREGs for their
4029 // operands.
4030 for (SDNode *PN : ToPromote) {
4031 unsigned NewOpcode;
4032 switch (PN->getMachineOpcode()) {
4033 default:
4034 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4035 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4036 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4037 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4038 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4039 case PPC::LI: NewOpcode = PPC::LI8; break;
4040 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004041 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4042 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004043 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004044 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4045 case PPC::OR: NewOpcode = PPC::OR8; break;
4046 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4047 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4048 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4049 case PPC::AND: NewOpcode = PPC::AND8; break;
4050 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4051 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4052 }
4053
4054 // Note: During the replacement process, the nodes will be in an
4055 // inconsistent state (some instructions will have operands with values
4056 // of the wrong type). Once done, however, everything should be right
4057 // again.
4058
4059 SmallVector<SDValue, 4> Ops;
4060 for (const SDValue &V : PN->ops()) {
4061 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4062 !isa<ConstantSDNode>(V)) {
4063 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4064 SDNode *ReplOp =
4065 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4066 ISR.getNode()->getVTList(), ReplOpOps);
4067 Ops.push_back(SDValue(ReplOp, 0));
4068 } else {
4069 Ops.push_back(V);
4070 }
4071 }
4072
4073 // Because all to-be-promoted nodes only have users that are other
4074 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4075 // the i32 result value type with i64.
4076
4077 SmallVector<EVT, 2> NewVTs;
4078 SDVTList VTs = PN->getVTList();
4079 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4080 if (VTs.VTs[i] == MVT::i32)
4081 NewVTs.push_back(MVT::i64);
4082 else
4083 NewVTs.push_back(VTs.VTs[i]);
4084
4085 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4086 DEBUG(PN->dump(CurDAG));
4087
4088 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4089
4090 DEBUG(dbgs() << "\nNew: ");
4091 DEBUG(PN->dump(CurDAG));
4092 DEBUG(dbgs() << "\n");
4093 }
4094
4095 // Now we replace the original zero extend and its associated INSERT_SUBREG
4096 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4097 // return an i64).
4098
4099 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4100 DEBUG(N->dump(CurDAG));
4101 DEBUG(dbgs() << "\nNew: ");
4102 DEBUG(Op32.getNode()->dump(CurDAG));
4103 DEBUG(dbgs() << "\n");
4104
4105 ReplaceUses(N, Op32.getNode());
4106 }
4107
4108 if (MadeChange)
4109 CurDAG->RemoveDeadNodes();
4110}
4111
Hal Finkel940ab932014-02-28 00:27:01 +00004112void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004113 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004114 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004115 return;
4116
4117 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4118 ++Position;
4119
4120 while (Position != CurDAG->allnodes_begin()) {
4121 SDNode *N = --Position;
4122 // Skip dead nodes and any non-machine opcodes.
4123 if (N->use_empty() || !N->isMachineOpcode())
4124 continue;
4125
4126 unsigned FirstOp;
4127 unsigned StorageOpcode = N->getMachineOpcode();
4128
4129 switch (StorageOpcode) {
4130 default: continue;
4131
4132 case PPC::LBZ:
4133 case PPC::LBZ8:
4134 case PPC::LD:
4135 case PPC::LFD:
4136 case PPC::LFS:
4137 case PPC::LHA:
4138 case PPC::LHA8:
4139 case PPC::LHZ:
4140 case PPC::LHZ8:
4141 case PPC::LWA:
4142 case PPC::LWZ:
4143 case PPC::LWZ8:
4144 FirstOp = 0;
4145 break;
4146
4147 case PPC::STB:
4148 case PPC::STB8:
4149 case PPC::STD:
4150 case PPC::STFD:
4151 case PPC::STFS:
4152 case PPC::STH:
4153 case PPC::STH8:
4154 case PPC::STW:
4155 case PPC::STW8:
4156 FirstOp = 1;
4157 break;
4158 }
4159
4160 // If this is a load or store with a zero offset, we may be able to
4161 // fold an add-immediate into the memory operation.
4162 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
4163 N->getConstantOperandVal(FirstOp) != 0)
4164 continue;
4165
4166 SDValue Base = N->getOperand(FirstOp + 1);
4167 if (!Base.isMachineOpcode())
4168 continue;
4169
4170 unsigned Flags = 0;
4171 bool ReplaceFlags = true;
4172
4173 // When the feeding operation is an add-immediate of some sort,
4174 // determine whether we need to add relocation information to the
4175 // target flags on the immediate operand when we fold it into the
4176 // load instruction.
4177 //
4178 // For something like ADDItocL, the relocation information is
4179 // inferred from the opcode; when we process it in the AsmPrinter,
4180 // we add the necessary relocation there. A load, though, can receive
4181 // relocation from various flavors of ADDIxxx, so we need to carry
4182 // the relocation information in the target flags.
4183 switch (Base.getMachineOpcode()) {
4184 default: continue;
4185
4186 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004187 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004188 // In some cases (such as TLS) the relocation information
4189 // is already in place on the operand, so copying the operand
4190 // is sufficient.
4191 ReplaceFlags = false;
4192 // For these cases, the immediate may not be divisible by 4, in
4193 // which case the fold is illegal for DS-form instructions. (The
4194 // other cases provide aligned addresses and are always safe.)
4195 if ((StorageOpcode == PPC::LWA ||
4196 StorageOpcode == PPC::LD ||
4197 StorageOpcode == PPC::STD) &&
4198 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4199 Base.getConstantOperandVal(1) % 4 != 0))
4200 continue;
4201 break;
4202 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004203 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004204 break;
4205 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004206 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004207 break;
4208 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004209 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004210 break;
4211 }
4212
4213 // We found an opportunity. Reverse the operands from the add
4214 // immediate and substitute them into the load or store. If
4215 // needed, update the target flags for the immediate operand to
4216 // reflect the necessary relocation information.
4217 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4218 DEBUG(Base->dump(CurDAG));
4219 DEBUG(dbgs() << "\nN: ");
4220 DEBUG(N->dump(CurDAG));
4221 DEBUG(dbgs() << "\n");
4222
4223 SDValue ImmOpnd = Base.getOperand(1);
4224
4225 // If the relocation information isn't already present on the
4226 // immediate operand, add it now.
4227 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004228 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004229 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004230 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004231 // We can't perform this optimization for data whose alignment
4232 // is insufficient for the instruction encoding.
4233 if (GV->getAlignment() < 4 &&
4234 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
4235 StorageOpcode == PPC::LWA)) {
4236 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4237 continue;
4238 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004239 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004240 } else if (ConstantPoolSDNode *CP =
4241 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004242 const Constant *C = CP->getConstVal();
4243 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4244 CP->getAlignment(),
4245 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004246 }
4247 }
4248
4249 if (FirstOp == 1) // Store
4250 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4251 Base.getOperand(0), N->getOperand(3));
4252 else // Load
4253 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4254 N->getOperand(2));
4255
4256 // The add-immediate may now be dead, in which case remove it.
4257 if (Base.getNode()->use_empty())
4258 CurDAG->RemoveDeadNode(Base.getNode());
4259 }
4260}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004261
Chris Lattnerb055c872006-06-10 01:15:02 +00004262
Andrew Trickc416ba62010-12-24 04:28:06 +00004263/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004264/// PowerPC-specific DAG, ready for instruction scheduling.
4265///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004266FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004267 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004268}
4269
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004270static void initializePassOnce(PassRegistry &Registry) {
4271 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004272 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4273 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004274 Registry.registerPass(*PI, true);
4275}
4276
4277void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4278 CALL_ONCE_INITIALIZATION(initializePassOnce);
4279}
4280