blob: c6dc50143c03d64f3e0f7a0b7a69dd1bbcf146bd [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
Hal Finkelc58ce412015-01-01 02:53:29 +000045cl::opt<bool> UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
46 cl::desc("use aggressive ppc isel for bit permutations"), cl::Hidden);
47cl::opt<bool> BPermRewriterNoMasking("ppc-bit-perm-rewriter-stress-rotates",
48 cl::desc("stress rotate selection in aggressive ppc isel for "
49 "bit permutations"), cl::Hidden);
50
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000051namespace llvm {
52 void initializePPCDAGToDAGISelPass(PassRegistry&);
53}
54
Chris Lattner43ff01e2005-08-17 19:33:03 +000055namespace {
Chris Lattner43ff01e2005-08-17 19:33:03 +000056 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +000057 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +000058 /// instructions for SelectionDAG operations.
59 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000060 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +000061 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +000062 const PPCTargetLowering *PPCLowering;
63 const PPCSubtarget *PPCSubTarget;
Chris Lattner45640392005-08-19 22:38:53 +000064 unsigned GlobalBaseReg;
Chris Lattner43ff01e2005-08-17 19:33:03 +000065 public:
Dan Gohman56e3f632008-07-07 18:00:37 +000066 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Eric Christopherd9134482014-08-04 21:25:23 +000067 : SelectionDAGISel(tm), TM(tm),
68 PPCLowering(TM.getSubtargetImpl()->getTargetLowering()),
69 PPCSubTarget(TM.getSubtargetImpl()) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000070 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
71 }
Andrew Trickc416ba62010-12-24 04:28:06 +000072
Craig Topper0d3fa922014-04-29 07:57:37 +000073 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +000074 // Make sure we re-emit a set of the global base reg if necessary
75 GlobalBaseReg = 0;
Eric Christopherd9134482014-08-04 21:25:23 +000076 PPCLowering = TM.getSubtargetImpl()->getTargetLowering();
Eric Christopher1b8e7632014-05-22 01:07:24 +000077 PPCSubTarget = TM.getSubtargetImpl();
Dan Gohman5ea74d52009-07-31 18:16:33 +000078 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +000079
Eric Christopher1b8e7632014-05-22 01:07:24 +000080 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +000081 InsertVRSaveCode(MF);
82
Chris Lattner1678a6c2006-03-16 18:25:23 +000083 return true;
Chris Lattner45640392005-08-19 22:38:53 +000084 }
Andrew Trickc416ba62010-12-24 04:28:06 +000085
Hal Finkel4edc66b2015-01-03 01:16:37 +000086 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +000087 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +000088
Chris Lattner43ff01e2005-08-17 19:33:03 +000089 /// getI32Imm - Return a target constant with the specified value, of type
90 /// i32.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000091 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000092 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +000093 }
Chris Lattner45640392005-08-19 22:38:53 +000094
Chris Lattner97b3da12006-06-27 00:04:13 +000095 /// getI64Imm - Return a target constant with the specified value, of type
96 /// i64.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000097 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +000098 return CurDAG->getTargetConstant(Imm, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +000099 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000100
Chris Lattner97b3da12006-06-27 00:04:13 +0000101 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000102 inline SDValue getSmallIPtrImm(unsigned Imm) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000103 return CurDAG->getTargetConstant(Imm, PPCLowering->getPointerTy());
Chris Lattner97b3da12006-06-27 00:04:13 +0000104 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000105
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000106 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
Nate Begemand31efd12006-09-22 05:01:56 +0000107 /// with any number of 0s on either side. The 1s are allowed to wrap from
108 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
109 /// 0x0F0F0000 is not, since all 1s are not contiguous.
110 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
111
112
113 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
114 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000115 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000116 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000117
Chris Lattner45640392005-08-19 22:38:53 +0000118 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
119 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000120 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000121
Hal Finkelb5e9b042014-12-11 22:51:06 +0000122 SDNode *getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
123
Chris Lattner43ff01e2005-08-17 19:33:03 +0000124 // Select - Convert the specified operand from a target-independent to a
125 // target-specific node if it hasn't already been changed.
Craig Topper0d3fa922014-04-29 07:57:37 +0000126 SDNode *Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000127
Nate Begeman93c4bc62005-08-19 00:38:14 +0000128 SDNode *SelectBitfieldInsert(SDNode *N);
Hal Finkel8adf2252014-12-16 05:51:41 +0000129 SDNode *SelectBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000130
Chris Lattner2a1823d2005-08-21 18:50:37 +0000131 /// SelectCC - Select a comparison of the specified values with the
132 /// specified condition code, returning the CR# of the expression.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000133 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000134
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000135 /// SelectAddrImm - Returns true if the address N can be represented by
136 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000137 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000138 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000139 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000140 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000141
Chris Lattner6f5840c2006-11-16 00:41:37 +0000142 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000143 /// immediate field. Note that the operand at this point is already the
144 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000145 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000146 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000147 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000148 Out = N;
149 return true;
150 }
151
152 return false;
153 }
154
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000155 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
156 /// represented as an indexed [r+r] operation. Returns false if it can
157 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000158 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000159 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000160 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000161
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000162 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
163 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000164 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000165 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000166 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000167
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000168 /// SelectAddrImmX4 - Returns true if the address N can be represented by
169 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
170 /// Suitable for use by STD and friends.
171 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000172 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000173 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000174
Hal Finkel756810f2013-03-21 21:37:52 +0000175 // Select an address into a single register.
176 bool SelectAddr(SDValue N, SDValue &Base) {
177 Base = N;
178 return true;
179 }
180
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000181 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000182 /// inline asm expressions. It is always correct to compute the value into
183 /// a register. The case of adding a (possibly relocatable) constant to a
184 /// register can be improved, but it is wrong to substitute Reg+Reg for
185 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000186 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Craig Topper0d3fa922014-04-29 07:57:37 +0000187 char ConstraintCode,
188 std::vector<SDValue> &OutOps) override {
Hal Finkeld4338382014-12-03 23:40:13 +0000189 // We need to make sure that this one operand does not end up in r0
190 // (because we might end up lowering this as 0(%op)).
191 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
192 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
193 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
194 SDValue NewOp =
195 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
196 SDLoc(Op), Op.getValueType(),
197 Op, RC), 0);
198
199 OutOps.push_back(NewOp);
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000200 return false;
201 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000202
Dan Gohman5ea74d52009-07-31 18:16:33 +0000203 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000204
Craig Topper0d3fa922014-04-29 07:57:37 +0000205 const char *getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000206 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000207 }
208
Chris Lattner03e08ee2005-09-13 22:03:06 +0000209// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000210#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000211
Chris Lattner259e6c72005-10-06 18:45:51 +0000212private:
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000213 SDNode *SelectSETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000214
215 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000216 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000217 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000218
Hal Finkel4edc66b2015-01-03 01:16:37 +0000219 SDValue combineToCMPB(SDNode *N);
220
Hal Finkelb9989152014-02-28 06:11:16 +0000221 bool AllUsersSelectZero(SDNode *N);
222 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000223 };
224}
225
Chris Lattner1678a6c2006-03-16 18:25:23 +0000226/// InsertVRSaveCode - Once the entire function has been instruction selected,
227/// all virtual registers are created and all machine instructions are built,
228/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000229void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000230 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000231 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000232 //
Dan Gohman4a618822010-02-10 16:03:48 +0000233 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000234 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000235 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000236 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
237 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
238 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000239 HasVectorVReg = true;
240 break;
241 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000242 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000243 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000244
Chris Lattner02e2c182006-03-13 21:52:10 +0000245 // If we have a vector register, we want to emit code into the entry and exit
246 // blocks to save and restore the VRSAVE register. We do this here (instead
247 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
248 //
249 // 1. This (trivially) reduces the load on the register allocator, by not
250 // having to represent the live range of the VRSAVE register.
251 // 2. This (more significantly) allows us to create a temporary virtual
252 // register to hold the saved VRSAVE value, allowing this temporary to be
253 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000254
255 // Create two vregs - one to hold the VRSAVE register that is live-in to the
256 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000257 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
258 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000259
Eric Christopherd9134482014-08-04 21:25:23 +0000260 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000261 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000262 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000263 // Emit the following code into the entry block:
264 // InVRSAVE = MFVRSAVE
265 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
266 // MTVRSAVE UpdatedVRSAVE
267 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000268 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
269 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000270 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000271 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000272
Chris Lattner1678a6c2006-03-16 18:25:23 +0000273 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000274 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000275 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000276 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000277
Chris Lattner1678a6c2006-03-16 18:25:23 +0000278 // Skip over all terminator instructions, which are part of the return
279 // sequence.
280 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000281 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000282 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000283
Chris Lattner1678a6c2006-03-16 18:25:23 +0000284 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000285 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000286 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000287 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000288}
Chris Lattner8ae95252005-09-03 01:17:22 +0000289
Chris Lattner1678a6c2006-03-16 18:25:23 +0000290
Chris Lattner45640392005-08-19 22:38:53 +0000291/// getGlobalBaseReg - Output the instructions required to put the
292/// base address to use for accessing globals into a register.
293///
Evan Cheng61413a32006-08-26 05:34:46 +0000294SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000295 if (!GlobalBaseReg) {
Eric Christopherd9134482014-08-04 21:25:23 +0000296 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000297 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000298 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000299 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000300 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000301 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000302
Eric Christopher1b8e7632014-05-22 01:07:24 +0000303 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000304 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000305 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000306 if (M->getPICLevel() == PICLevel::Small) {
307 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
308 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
309 } else {
310 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
311 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
312 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
313 BuildMI(FirstMBB, MBBI, dl,
314 TII.get(PPC::UpdateGBR)).addReg(GlobalBaseReg)
315 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
316 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
317 }
318 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000319 GlobalBaseReg =
320 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000321 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
322 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000323 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000324 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000325 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000326 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000327 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000328 }
Chris Lattner45640392005-08-19 22:38:53 +0000329 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000330 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000331 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000332}
333
334/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
335/// or 64-bit immediate, and if the value can be accurately represented as a
336/// sign extension from a 16-bit value. If so, this returns true and the
337/// immediate.
338static bool isIntS16Immediate(SDNode *N, short &Imm) {
339 if (N->getOpcode() != ISD::Constant)
340 return false;
341
Dan Gohmaneffb8942008-09-12 16:56:44 +0000342 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000343 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000344 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000345 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000346 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000347}
348
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000349static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000350 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000351}
352
353
Chris Lattner97b3da12006-06-27 00:04:13 +0000354/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
355/// operand. If so Imm will receive the 32-bit value.
356static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000357 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000358 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000359 return true;
360 }
361 return false;
362}
363
Chris Lattner97b3da12006-06-27 00:04:13 +0000364/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
365/// operand. If so Imm will receive the 64-bit value.
366static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000367 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000368 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000369 return true;
370 }
371 return false;
372}
373
374// isInt32Immediate - This method tests to see if a constant operand.
375// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000376static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000377 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000378}
379
380
381// isOpcWithIntImmediate - This method tests to see if the node is a specific
382// opcode and that it has a immediate integer right operand.
383// If so Imm will receive the 32 bit value.
384static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000385 return N->getOpcode() == Opc
386 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000387}
388
Hal Finkelb5e9b042014-12-11 22:51:06 +0000389SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
390 SDLoc dl(SN);
391 int FI = cast<FrameIndexSDNode>(N)->getIndex();
392 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
393 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
394 if (SN->hasOneUse())
395 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
396 getSmallIPtrImm(Offset));
397 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
398 getSmallIPtrImm(Offset));
399}
400
Nate Begemand31efd12006-09-22 05:01:56 +0000401bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000402 if (!Val)
403 return false;
404
Nate Begemanb3821a32005-08-18 07:30:46 +0000405 if (isShiftedMask_32(Val)) {
406 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000407 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000408 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000409 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000410 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000411 } else {
412 Val = ~Val; // invert mask
413 if (isShiftedMask_32(Val)) {
414 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000415 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000416 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000417 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000418 return true;
419 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000420 }
421 // no run present
422 return false;
423}
424
Andrew Trickc416ba62010-12-24 04:28:06 +0000425bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
426 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000427 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000428 // Don't even go down this path for i64, since different logic will be
429 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000430 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000431 return false;
432
Nate Begemanb3821a32005-08-18 07:30:46 +0000433 unsigned Shift = 32;
434 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
435 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000436 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000437 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000438 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000439
Nate Begemanb3821a32005-08-18 07:30:46 +0000440 if (Opcode == ISD::SHL) {
441 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000442 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000443 // determine which bits are made indeterminant by shift
444 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000445 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000446 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000447 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000448 // determine which bits are made indeterminant by shift
449 Indeterminant = ~(0xFFFFFFFFu >> Shift);
450 // adjust for the left rotate
451 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000452 } else if (Opcode == ISD::ROTL) {
453 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000454 } else {
455 return false;
456 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000457
Nate Begemanb3821a32005-08-18 07:30:46 +0000458 // if the mask doesn't intersect any Indeterminant bits
459 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000460 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000461 // make sure the mask is still a mask (wrap arounds may not be)
462 return isRunOfOnes(Mask, MB, ME);
463 }
464 return false;
465}
466
Nate Begeman93c4bc62005-08-19 00:38:14 +0000467/// SelectBitfieldInsert - turn an or of two masked values into
468/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000469SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000470 SDValue Op0 = N->getOperand(0);
471 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000472 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000473
Dan Gohmanf19609a2008-02-27 01:23:58 +0000474 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000475 CurDAG->computeKnownBits(Op0, LKZ, LKO);
476 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000477
Dan Gohmanf19609a2008-02-27 01:23:58 +0000478 unsigned TargetMask = LKZ.getZExtValue();
479 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000480
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000481 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
482 unsigned Op0Opc = Op0.getOpcode();
483 unsigned Op1Opc = Op1.getOpcode();
484 unsigned Value, SH = 0;
485 TargetMask = ~TargetMask;
486 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000487
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000488 // If the LHS has a foldable shift and the RHS does not, then swap it to the
489 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000490 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
491 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
492 Op0.getOperand(0).getOpcode() == ISD::SRL) {
493 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
494 Op1.getOperand(0).getOpcode() != ISD::SRL) {
495 std::swap(Op0, Op1);
496 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000497 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000498 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000499 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000500 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
501 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
502 Op1.getOperand(0).getOpcode() != ISD::SRL) {
503 std::swap(Op0, Op1);
504 std::swap(Op0Opc, Op1Opc);
505 std::swap(TargetMask, InsertMask);
506 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000507 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000508
Nate Begeman1333cea2006-05-07 00:23:38 +0000509 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000510 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000511 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000512
513 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000514 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000515 Op1 = Op1.getOperand(0);
516 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
517 }
518 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000519 // The AND mask might not be a constant, and we need to make sure that
520 // if we're going to fold the masking with the insert, all bits not
521 // know to be zero in the mask are known to be one.
522 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000523 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000524 bool CanFoldMask = InsertMask == MKO.getZExtValue();
525
Nate Begeman1333cea2006-05-07 00:23:38 +0000526 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000527 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000528 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000529 // Note that Value must be in range here (less than 32) because
530 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000531 Op1 = Op1.getOperand(0).getOperand(0);
532 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000533 }
534 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000535
Chris Lattnera2963392006-05-12 16:29:37 +0000536 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000537 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000538 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000539 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000540 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000541 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000542 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000543}
544
Hal Finkelc58ce412015-01-01 02:53:29 +0000545// Predict the number of instructions that would be generated by calling
546// SelectInt64(N).
547static unsigned SelectInt64Count(int64_t Imm) {
548 // Assume no remaining bits.
549 unsigned Remainder = 0;
550 // Assume no shift required.
551 unsigned Shift = 0;
552
553 // If it can't be represented as a 32 bit value.
554 if (!isInt<32>(Imm)) {
555 Shift = countTrailingZeros<uint64_t>(Imm);
556 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
557
558 // If the shifted value fits 32 bits.
559 if (isInt<32>(ImmSh)) {
560 // Go with the shifted value.
561 Imm = ImmSh;
562 } else {
563 // Still stuck with a 64 bit value.
564 Remainder = Imm;
565 Shift = 32;
566 Imm >>= 32;
567 }
568 }
569
570 // Intermediate operand.
571 unsigned Result = 0;
572
573 // Handle first 32 bits.
574 unsigned Lo = Imm & 0xFFFF;
575 unsigned Hi = (Imm >> 16) & 0xFFFF;
576
577 // Simple value.
578 if (isInt<16>(Imm)) {
579 // Just the Lo bits.
580 ++Result;
581 } else if (Lo) {
582 // Handle the Hi bits and Lo bits.
583 Result += 2;
584 } else {
585 // Just the Hi bits.
586 ++Result;
587 }
588
589 // If no shift, we're done.
590 if (!Shift) return Result;
591
592 // Shift for next step if the upper 32-bits were not zero.
593 if (Imm)
594 ++Result;
595
596 // Add in the last bits as required.
597 if ((Hi = (Remainder >> 16) & 0xFFFF))
598 ++Result;
599 if ((Lo = Remainder & 0xFFFF))
600 ++Result;
601
602 return Result;
603}
604
605// Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count
606// (above) needs to be kept in sync with this function.
607static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
608 // Assume no remaining bits.
609 unsigned Remainder = 0;
610 // Assume no shift required.
611 unsigned Shift = 0;
612
613 // If it can't be represented as a 32 bit value.
614 if (!isInt<32>(Imm)) {
615 Shift = countTrailingZeros<uint64_t>(Imm);
616 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
617
618 // If the shifted value fits 32 bits.
619 if (isInt<32>(ImmSh)) {
620 // Go with the shifted value.
621 Imm = ImmSh;
622 } else {
623 // Still stuck with a 64 bit value.
624 Remainder = Imm;
625 Shift = 32;
626 Imm >>= 32;
627 }
628 }
629
630 // Intermediate operand.
631 SDNode *Result;
632
633 // Handle first 32 bits.
634 unsigned Lo = Imm & 0xFFFF;
635 unsigned Hi = (Imm >> 16) & 0xFFFF;
636
637 auto getI32Imm = [CurDAG](unsigned Imm) {
638 return CurDAG->getTargetConstant(Imm, MVT::i32);
639 };
640
641 // Simple value.
642 if (isInt<16>(Imm)) {
643 // Just the Lo bits.
644 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
645 } else if (Lo) {
646 // Handle the Hi bits.
647 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
648 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
649 // And Lo bits.
650 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
651 SDValue(Result, 0), getI32Imm(Lo));
652 } else {
653 // Just the Hi bits.
654 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
655 }
656
657 // If no shift, we're done.
658 if (!Shift) return Result;
659
660 // Shift for next step if the upper 32-bits were not zero.
661 if (Imm) {
662 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
663 SDValue(Result, 0),
664 getI32Imm(Shift),
665 getI32Imm(63 - Shift));
666 }
667
668 // Add in the last bits as required.
669 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
670 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
671 SDValue(Result, 0), getI32Imm(Hi));
672 }
673 if ((Lo = Remainder & 0xFFFF)) {
674 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
675 SDValue(Result, 0), getI32Imm(Lo));
676 }
677
678 return Result;
679}
680
681// Select a 64-bit constant.
682static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) {
683 SDLoc dl(N);
684
685 // Get 64 bit value.
686 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
687 return SelectInt64(CurDAG, dl, Imm);
688}
689
Hal Finkel8adf2252014-12-16 05:51:41 +0000690namespace {
691class BitPermutationSelector {
692 struct ValueBit {
693 SDValue V;
694
695 // The bit number in the value, using a convention where bit 0 is the
696 // lowest-order bit.
697 unsigned Idx;
698
699 enum Kind {
700 ConstZero,
701 Variable
702 } K;
703
704 ValueBit(SDValue V, unsigned I, Kind K = Variable)
705 : V(V), Idx(I), K(K) {}
706 ValueBit(Kind K = Variable)
707 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
708
709 bool isZero() const {
710 return K == ConstZero;
711 }
712
713 bool hasValue() const {
714 return K == Variable;
715 }
716
717 SDValue getValue() const {
718 assert(hasValue() && "Cannot get the value of a constant bit");
719 return V;
720 }
721
722 unsigned getValueBitIndex() const {
723 assert(hasValue() && "Cannot get the value bit index of a constant bit");
724 return Idx;
725 }
726 };
727
728 // A bit group has the same underlying value and the same rotate factor.
729 struct BitGroup {
730 SDValue V;
731 unsigned RLAmt;
732 unsigned StartIdx, EndIdx;
733
Hal Finkelc58ce412015-01-01 02:53:29 +0000734 // This rotation amount assumes that the lower 32 bits of the quantity are
735 // replicated in the high 32 bits by the rotation operator (which is done
736 // by rlwinm and friends in 64-bit mode).
737 bool Repl32;
738 // Did converting to Repl32 == true change the rotation factor? If it did,
739 // it decreased it by 32.
740 bool Repl32CR;
741 // Was this group coalesced after setting Repl32 to true?
742 bool Repl32Coalesced;
743
Hal Finkel8adf2252014-12-16 05:51:41 +0000744 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000745 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
746 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000747 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
748 " [" << S << ", " << E << "]\n");
749 }
750 };
751
752 // Information on each (Value, RLAmt) pair (like the number of groups
753 // associated with each) used to choose the lowering method.
754 struct ValueRotInfo {
755 SDValue V;
756 unsigned RLAmt;
757 unsigned NumGroups;
758 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000759 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000760
761 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000762 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
763 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000764
765 // For sorting (in reverse order) by NumGroups, and then by
766 // FirstGroupStartIdx.
767 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000768 // We need to sort so that the non-Repl32 come first because, when we're
769 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
770 // masking operation.
771 if (Repl32 < Other.Repl32)
772 return true;
773 else if (Repl32 > Other.Repl32)
774 return false;
775 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000776 return true;
777 else if (NumGroups < Other.NumGroups)
778 return false;
779 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
780 return true;
781 return false;
782 }
783 };
784
785 // Return true if something interesting was deduced, return false if we're
786 // providing only a generic representation of V (or something else likewise
787 // uninteresting for instruction selection).
788 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
789 switch (V.getOpcode()) {
790 default: break;
791 case ISD::ROTL:
792 if (isa<ConstantSDNode>(V.getOperand(1))) {
793 unsigned RotAmt = V.getConstantOperandVal(1);
794
795 SmallVector<ValueBit, 64> LHSBits(Bits.size());
796 getValueBits(V.getOperand(0), LHSBits);
797
798 for (unsigned i = 0; i < Bits.size(); ++i)
799 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
800
801 return true;
802 }
803 break;
804 case ISD::SHL:
805 if (isa<ConstantSDNode>(V.getOperand(1))) {
806 unsigned ShiftAmt = V.getConstantOperandVal(1);
807
808 SmallVector<ValueBit, 64> LHSBits(Bits.size());
809 getValueBits(V.getOperand(0), LHSBits);
810
811 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
812 Bits[i] = LHSBits[i - ShiftAmt];
813
814 for (unsigned i = 0; i < ShiftAmt; ++i)
815 Bits[i] = ValueBit(ValueBit::ConstZero);
816
817 return true;
818 }
819 break;
820 case ISD::SRL:
821 if (isa<ConstantSDNode>(V.getOperand(1))) {
822 unsigned ShiftAmt = V.getConstantOperandVal(1);
823
824 SmallVector<ValueBit, 64> LHSBits(Bits.size());
825 getValueBits(V.getOperand(0), LHSBits);
826
827 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
828 Bits[i] = LHSBits[i + ShiftAmt];
829
830 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
831 Bits[i] = ValueBit(ValueBit::ConstZero);
832
833 return true;
834 }
835 break;
836 case ISD::AND:
837 if (isa<ConstantSDNode>(V.getOperand(1))) {
838 uint64_t Mask = V.getConstantOperandVal(1);
839
840 SmallVector<ValueBit, 64> LHSBits(Bits.size());
841 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
842
843 for (unsigned i = 0; i < Bits.size(); ++i)
844 if (((Mask >> i) & 1) == 1)
845 Bits[i] = LHSBits[i];
846 else
847 Bits[i] = ValueBit(ValueBit::ConstZero);
848
849 // Mark this as interesting, only if the LHS was also interesting. This
850 // prevents the overall procedure from matching a single immediate 'and'
851 // (which is non-optimal because such an and might be folded with other
852 // things if we don't select it here).
853 return LHSTrivial;
854 }
855 break;
856 case ISD::OR: {
857 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
858 getValueBits(V.getOperand(0), LHSBits);
859 getValueBits(V.getOperand(1), RHSBits);
860
861 bool AllDisjoint = true;
862 for (unsigned i = 0; i < Bits.size(); ++i)
863 if (LHSBits[i].isZero())
864 Bits[i] = RHSBits[i];
865 else if (RHSBits[i].isZero())
866 Bits[i] = LHSBits[i];
867 else {
868 AllDisjoint = false;
869 break;
870 }
871
872 if (!AllDisjoint)
873 break;
874
875 return true;
876 }
877 }
878
879 for (unsigned i = 0; i < Bits.size(); ++i)
880 Bits[i] = ValueBit(V, i);
881
882 return false;
883 }
884
885 // For each value (except the constant ones), compute the left-rotate amount
886 // to get it from its original to final position.
887 void computeRotationAmounts() {
888 HasZeros = false;
889 RLAmt.resize(Bits.size());
890 for (unsigned i = 0; i < Bits.size(); ++i)
891 if (Bits[i].hasValue()) {
892 unsigned VBI = Bits[i].getValueBitIndex();
893 if (i >= VBI)
894 RLAmt[i] = i - VBI;
895 else
896 RLAmt[i] = Bits.size() - (VBI - i);
897 } else if (Bits[i].isZero()) {
898 HasZeros = true;
899 RLAmt[i] = UINT32_MAX;
900 } else {
901 llvm_unreachable("Unknown value bit type");
902 }
903 }
904
905 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +0000906 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
907 // they break up groups.
908 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000909 BitGroups.clear();
910
911 unsigned LastRLAmt = RLAmt[0];
912 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
913 unsigned LastGroupStartIdx = 0;
914 for (unsigned i = 1; i < Bits.size(); ++i) {
915 unsigned ThisRLAmt = RLAmt[i];
916 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +0000917 if (LateMask && !ThisValue) {
918 ThisValue = LastValue;
919 ThisRLAmt = LastRLAmt;
920 // If we're doing late masking, then the first bit group always starts
921 // at zero (even if the first bits were zero).
922 if (BitGroups.empty())
923 LastGroupStartIdx = 0;
924 }
Hal Finkel8adf2252014-12-16 05:51:41 +0000925
926 // If this bit has the same underlying value and the same rotate factor as
927 // the last one, then they're part of the same group.
928 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
929 continue;
930
931 if (LastValue.getNode())
932 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
933 i-1));
934 LastRLAmt = ThisRLAmt;
935 LastValue = ThisValue;
936 LastGroupStartIdx = i;
937 }
938 if (LastValue.getNode())
939 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
940 Bits.size()-1));
941
942 if (BitGroups.empty())
943 return;
944
945 // We might be able to combine the first and last groups.
946 if (BitGroups.size() > 1) {
947 // If the first and last groups are the same, then remove the first group
948 // in favor of the last group, making the ending index of the last group
949 // equal to the ending index of the to-be-removed first group.
950 if (BitGroups[0].StartIdx == 0 &&
951 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
952 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
953 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000954 DEBUG(dbgs() << "\tcombining final bit group with inital one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +0000955 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
956 BitGroups.erase(BitGroups.begin());
957 }
958 }
959 }
960
961 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
962 // associated with each. If there is a degeneracy, pick the one that occurs
963 // first (in the final value).
964 void collectValueRotInfo() {
965 ValueRots.clear();
966
967 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000968 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
969 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +0000970 VRI.V = BG.V;
971 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +0000972 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000973 VRI.NumGroups += 1;
974 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
975 }
976
977 // Now that we've collected the various ValueRotInfo instances, we need to
978 // sort them.
979 ValueRotsVec.clear();
980 for (auto &I : ValueRots) {
981 ValueRotsVec.push_back(I.second);
982 }
983 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
984 }
985
Hal Finkelc58ce412015-01-01 02:53:29 +0000986 // In 64-bit mode, rlwinm and friends have a rotation operator that
987 // replicates the low-order 32 bits into the high-order 32-bits. The mask
988 // indices of these instructions can only be in the lower 32 bits, so they
989 // can only represent some 64-bit bit groups. However, when they can be used,
990 // the 32-bit replication can be used to represent, as a single bit group,
991 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
992 // groups when possible. Returns true if any of the bit groups were
993 // converted.
994 void assignRepl32BitGroups() {
995 // If we have bits like this:
996 //
997 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
998 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
999 // Groups: | RLAmt = 8 | RLAmt = 40 |
1000 //
1001 // But, making use of a 32-bit operation that replicates the low-order 32
1002 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1003 // of 8.
1004
1005 auto IsAllLow32 = [this](BitGroup & BG) {
1006 if (BG.StartIdx <= BG.EndIdx) {
1007 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1008 if (!Bits[i].hasValue())
1009 continue;
1010 if (Bits[i].getValueBitIndex() >= 32)
1011 return false;
1012 }
1013 } else {
1014 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1015 if (!Bits[i].hasValue())
1016 continue;
1017 if (Bits[i].getValueBitIndex() >= 32)
1018 return false;
1019 }
1020 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1021 if (!Bits[i].hasValue())
1022 continue;
1023 if (Bits[i].getValueBitIndex() >= 32)
1024 return false;
1025 }
1026 }
1027
1028 return true;
1029 };
1030
1031 for (auto &BG : BitGroups) {
1032 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1033 if (IsAllLow32(BG)) {
1034 if (BG.RLAmt >= 32) {
1035 BG.RLAmt -= 32;
1036 BG.Repl32CR = true;
1037 }
1038
1039 BG.Repl32 = true;
1040
1041 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1042 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1043 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1044 }
1045 }
1046 }
1047
1048 // Now walk through the bit groups, consolidating where possible.
1049 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1050 // We might want to remove this bit group by merging it with the previous
1051 // group (which might be the ending group).
1052 auto IP = (I == BitGroups.begin()) ?
1053 std::prev(BitGroups.end()) : std::prev(I);
1054 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1055 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1056
1057 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1058 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1059 " [" << I->StartIdx << ", " << I->EndIdx <<
1060 "] with group with range [" <<
1061 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1062
1063 IP->EndIdx = I->EndIdx;
1064 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1065 IP->Repl32Coalesced = true;
1066 I = BitGroups.erase(I);
1067 continue;
1068 } else {
1069 // There is a special case worth handling: If there is a single group
1070 // covering the entire upper 32 bits, and it can be merged with both
1071 // the next and previous groups (which might be the same group), then
1072 // do so. If it is the same group (so there will be only one group in
1073 // total), then we need to reverse the order of the range so that it
1074 // covers the entire 64 bits.
1075 if (I->StartIdx == 32 && I->EndIdx == 63) {
1076 assert(std::next(I) == BitGroups.end() &&
1077 "bit group ends at index 63 but there is another?");
1078 auto IN = BitGroups.begin();
1079
1080 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
1081 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1082 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1083 IsAllLow32(*I)) {
1084
1085 DEBUG(dbgs() << "\tcombining bit group for " <<
1086 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1087 " [" << I->StartIdx << ", " << I->EndIdx <<
1088 "] with 32-bit replicated groups with ranges [" <<
1089 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1090 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1091
1092 if (IP == IN) {
1093 // There is only one other group; change it to cover the whole
1094 // range (backward, so that it can still be Repl32 but cover the
1095 // whole 64-bit range).
1096 IP->StartIdx = 31;
1097 IP->EndIdx = 30;
1098 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1099 IP->Repl32Coalesced = true;
1100 I = BitGroups.erase(I);
1101 } else {
1102 // There are two separate groups, one before this group and one
1103 // after us (at the beginning). We're going to remove this group,
1104 // but also the group at the very beginning.
1105 IP->EndIdx = IN->EndIdx;
1106 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1107 IP->Repl32Coalesced = true;
1108 I = BitGroups.erase(I);
1109 BitGroups.erase(BitGroups.begin());
1110 }
1111
1112 // This must be the last group in the vector (and we might have
1113 // just invalidated the iterator above), so break here.
1114 break;
1115 }
1116 }
1117 }
1118
1119 ++I;
1120 }
1121 }
1122
Hal Finkel8adf2252014-12-16 05:51:41 +00001123 SDValue getI32Imm(unsigned Imm) {
1124 return CurDAG->getTargetConstant(Imm, MVT::i32);
1125 }
1126
Hal Finkelc58ce412015-01-01 02:53:29 +00001127 uint64_t getZerosMask() {
1128 uint64_t Mask = 0;
1129 for (unsigned i = 0; i < Bits.size(); ++i) {
1130 if (Bits[i].hasValue())
1131 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001132 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001133 }
1134
1135 return ~Mask;
1136 }
1137
Hal Finkel8adf2252014-12-16 05:51:41 +00001138 // Depending on the number of groups for a particular value, it might be
1139 // better to rotate, mask explicitly (using andi/andis), and then or the
1140 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001141 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1142 if (BPermRewriterNoMasking)
1143 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001144
1145 for (ValueRotInfo &VRI : ValueRotsVec) {
1146 unsigned Mask = 0;
1147 for (unsigned i = 0; i < Bits.size(); ++i) {
1148 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1149 continue;
1150 if (RLAmt[i] != VRI.RLAmt)
1151 continue;
1152 Mask |= (1u << i);
1153 }
1154
1155 // Compute the masks for andi/andis that would be necessary.
1156 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1157 assert((ANDIMask != 0 || ANDISMask != 0) &&
1158 "No set bits in mask for value bit groups");
1159 bool NeedsRotate = VRI.RLAmt != 0;
1160
1161 // We're trying to minimize the number of instructions. If we have one
1162 // group, using one of andi/andis can break even. If we have three
1163 // groups, we can use both andi and andis and break even (to use both
1164 // andi and andis we also need to or the results together). We need four
1165 // groups if we also need to rotate. To use andi/andis we need to do more
1166 // than break even because rotate-and-mask instructions tend to be easier
1167 // to schedule.
1168
1169 // FIXME: We've biased here against using andi/andis, which is right for
1170 // POWER cores, but not optimal everywhere. For example, on the A2,
1171 // andi/andis have single-cycle latency whereas the rotate-and-mask
1172 // instructions take two cycles, and it would be better to bias toward
1173 // andi/andis in break-even cases.
1174
1175 unsigned NumAndInsts = (unsigned) NeedsRotate +
1176 (unsigned) (ANDIMask != 0) +
1177 (unsigned) (ANDISMask != 0) +
1178 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1179 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001180
1181 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1182 " RL: " << VRI.RLAmt << ":" <<
1183 "\n\t\t\tisel using masking: " << NumAndInsts <<
1184 " using rotates: " << VRI.NumGroups << "\n");
1185
Hal Finkel8adf2252014-12-16 05:51:41 +00001186 if (NumAndInsts >= VRI.NumGroups)
1187 continue;
1188
Hal Finkelc58ce412015-01-01 02:53:29 +00001189 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1190
1191 if (InstCnt) *InstCnt += NumAndInsts;
1192
Hal Finkel8adf2252014-12-16 05:51:41 +00001193 SDValue VRot;
1194 if (VRI.RLAmt) {
1195 SDValue Ops[] =
1196 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1197 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1198 Ops), 0);
1199 } else {
1200 VRot = VRI.V;
1201 }
1202
1203 SDValue ANDIVal, ANDISVal;
1204 if (ANDIMask != 0)
1205 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1206 VRot, getI32Imm(ANDIMask)), 0);
1207 if (ANDISMask != 0)
1208 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1209 VRot, getI32Imm(ANDISMask)), 0);
1210
1211 SDValue TotalVal;
1212 if (!ANDIVal)
1213 TotalVal = ANDISVal;
1214 else if (!ANDISVal)
1215 TotalVal = ANDIVal;
1216 else
1217 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1218 ANDIVal, ANDISVal), 0);
1219
1220 if (!Res)
1221 Res = TotalVal;
1222 else
1223 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1224 Res, TotalVal), 0);
1225
1226 // Now, remove all groups with this underlying value and rotation
1227 // factor.
1228 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1229 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1230 I = BitGroups.erase(I);
1231 else
1232 ++I;
1233 }
1234 }
1235 }
1236
1237 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001238 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001239 SDLoc dl(N);
1240 SDValue Res;
1241
Hal Finkelc58ce412015-01-01 02:53:29 +00001242 if (InstCnt) *InstCnt = 0;
1243
Hal Finkel8adf2252014-12-16 05:51:41 +00001244 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001245 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001246
1247 // If we've not yet selected a 'starting' instruction, and we have no zeros
1248 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1249 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001250 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001251 ValueRotInfo &VRI = ValueRotsVec[0];
1252 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001253 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001254 SDValue Ops[] =
1255 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1256 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1257 } else {
1258 Res = VRI.V;
1259 }
1260
1261 // Now, remove all groups with this underlying value and rotation factor.
1262 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1263 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1264 I = BitGroups.erase(I);
1265 else
1266 ++I;
1267 }
1268 }
1269
Hal Finkelc58ce412015-01-01 02:53:29 +00001270 if (InstCnt) *InstCnt += BitGroups.size();
1271
Hal Finkel8adf2252014-12-16 05:51:41 +00001272 // Insert the other groups (one at a time).
1273 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001274 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001275 SDValue Ops[] =
1276 { BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1277 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1278 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1279 } else {
1280 SDValue Ops[] =
1281 { Res, BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1282 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1283 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1284 }
1285 }
1286
Hal Finkelc58ce412015-01-01 02:53:29 +00001287 if (LateMask) {
1288 unsigned Mask = (unsigned) getZerosMask();
1289
1290 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1291 assert((ANDIMask != 0 || ANDISMask != 0) &&
1292 "No set bits in zeros mask?");
1293
1294 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1295 (unsigned) (ANDISMask != 0) +
1296 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1297
1298 SDValue ANDIVal, ANDISVal;
1299 if (ANDIMask != 0)
1300 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1301 Res, getI32Imm(ANDIMask)), 0);
1302 if (ANDISMask != 0)
1303 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1304 Res, getI32Imm(ANDISMask)), 0);
1305
1306 if (!ANDIVal)
1307 Res = ANDISVal;
1308 else if (!ANDISVal)
1309 Res = ANDIVal;
1310 else
1311 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1312 ANDIVal, ANDISVal), 0);
1313 }
1314
Hal Finkel8adf2252014-12-16 05:51:41 +00001315 return Res.getNode();
1316 }
1317
Hal Finkelc58ce412015-01-01 02:53:29 +00001318 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1319 unsigned MaskStart, unsigned MaskEnd,
1320 bool IsIns) {
1321 // In the notation used by the instructions, 'start' and 'end' are reversed
1322 // because bits are counted from high to low order.
1323 unsigned InstMaskStart = 64 - MaskEnd - 1,
1324 InstMaskEnd = 64 - MaskStart - 1;
1325
1326 if (Repl32)
1327 return 1;
1328
1329 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1330 InstMaskEnd == 63 - RLAmt)
1331 return 1;
1332
1333 return 2;
1334 }
1335
1336 // For 64-bit values, not all combinations of rotates and masks are
1337 // available. Produce one if it is available.
1338 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1339 unsigned MaskStart, unsigned MaskEnd,
1340 unsigned *InstCnt = nullptr) {
1341 // In the notation used by the instructions, 'start' and 'end' are reversed
1342 // because bits are counted from high to low order.
1343 unsigned InstMaskStart = 64 - MaskEnd - 1,
1344 InstMaskEnd = 64 - MaskStart - 1;
1345
1346 if (InstCnt) *InstCnt += 1;
1347
1348 if (Repl32) {
1349 // This rotation amount assumes that the lower 32 bits of the quantity
1350 // are replicated in the high 32 bits by the rotation operator (which is
1351 // done by rlwinm and friends).
1352 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1353 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1354 SDValue Ops[] =
1355 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1356 getI32Imm(InstMaskEnd - 32) };
1357 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1358 Ops), 0);
1359 }
1360
1361 if (InstMaskEnd == 63) {
1362 SDValue Ops[] =
1363 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1364 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1365 }
1366
1367 if (InstMaskStart == 0) {
1368 SDValue Ops[] =
1369 { V, getI32Imm(RLAmt), getI32Imm(InstMaskEnd) };
1370 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1371 }
1372
1373 if (InstMaskEnd == 63 - RLAmt) {
1374 SDValue Ops[] =
1375 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1376 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1377 }
1378
1379 // We cannot do this with a single instruction, so we'll use two. The
1380 // problem is that we're not free to choose both a rotation amount and mask
1381 // start and end independently. We can choose an arbitrary mask start and
1382 // end, but then the rotation amount is fixed. Rotation, however, can be
1383 // inverted, and so by applying an "inverse" rotation first, we can get the
1384 // desired result.
1385 if (InstCnt) *InstCnt += 1;
1386
1387 // The rotation mask for the second instruction must be MaskStart.
1388 unsigned RLAmt2 = MaskStart;
1389 // The first instruction must rotate V so that the overall rotation amount
1390 // is RLAmt.
1391 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1392 if (RLAmt1)
1393 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1394 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1395 }
1396
1397 // For 64-bit values, not all combinations of rotates and masks are
1398 // available. Produce a rotate-mask-and-insert if one is available.
1399 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1400 bool Repl32, unsigned MaskStart,
1401 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1402 // In the notation used by the instructions, 'start' and 'end' are reversed
1403 // because bits are counted from high to low order.
1404 unsigned InstMaskStart = 64 - MaskEnd - 1,
1405 InstMaskEnd = 64 - MaskStart - 1;
1406
1407 if (InstCnt) *InstCnt += 1;
1408
1409 if (Repl32) {
1410 // This rotation amount assumes that the lower 32 bits of the quantity
1411 // are replicated in the high 32 bits by the rotation operator (which is
1412 // done by rlwinm and friends).
1413 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1414 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1415 SDValue Ops[] =
1416 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1417 getI32Imm(InstMaskEnd - 32) };
1418 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1419 Ops), 0);
1420 }
1421
1422 if (InstMaskEnd == 63 - RLAmt) {
1423 SDValue Ops[] =
1424 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1425 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1426 }
1427
1428 // We cannot do this with a single instruction, so we'll use two. The
1429 // problem is that we're not free to choose both a rotation amount and mask
1430 // start and end independently. We can choose an arbitrary mask start and
1431 // end, but then the rotation amount is fixed. Rotation, however, can be
1432 // inverted, and so by applying an "inverse" rotation first, we can get the
1433 // desired result.
1434 if (InstCnt) *InstCnt += 1;
1435
1436 // The rotation mask for the second instruction must be MaskStart.
1437 unsigned RLAmt2 = MaskStart;
1438 // The first instruction must rotate V so that the overall rotation amount
1439 // is RLAmt.
1440 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1441 if (RLAmt1)
1442 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1443 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1444 }
1445
1446 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1447 if (BPermRewriterNoMasking)
1448 return;
1449
1450 // The idea here is the same as in the 32-bit version, but with additional
1451 // complications from the fact that Repl32 might be true. Because we
1452 // aggressively convert bit groups to Repl32 form (which, for small
1453 // rotation factors, involves no other change), and then coalesce, it might
1454 // be the case that a single 64-bit masking operation could handle both
1455 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1456 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1457 // completely capture the new combined bit group.
1458
1459 for (ValueRotInfo &VRI : ValueRotsVec) {
1460 uint64_t Mask = 0;
1461
1462 // We need to add to the mask all bits from the associated bit groups.
1463 // If Repl32 is false, we need to add bits from bit groups that have
1464 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1465 // group is trivially convertable if it overlaps only with the lower 32
1466 // bits, and the group has not been coalesced.
1467 auto MatchingBG = [VRI](BitGroup &BG) {
1468 if (VRI.V != BG.V)
1469 return false;
1470
1471 unsigned EffRLAmt = BG.RLAmt;
1472 if (!VRI.Repl32 && BG.Repl32) {
1473 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1474 !BG.Repl32Coalesced) {
1475 if (BG.Repl32CR)
1476 EffRLAmt += 32;
1477 } else {
1478 return false;
1479 }
1480 } else if (VRI.Repl32 != BG.Repl32) {
1481 return false;
1482 }
1483
1484 if (VRI.RLAmt != EffRLAmt)
1485 return false;
1486
1487 return true;
1488 };
1489
1490 for (auto &BG : BitGroups) {
1491 if (!MatchingBG(BG))
1492 continue;
1493
1494 if (BG.StartIdx <= BG.EndIdx) {
1495 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001496 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001497 } else {
1498 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001499 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001500 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001501 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001502 }
1503 }
1504
1505 // We can use the 32-bit andi/andis technique if the mask does not
1506 // require any higher-order bits. This can save an instruction compared
1507 // to always using the general 64-bit technique.
1508 bool Use32BitInsts = isUInt<32>(Mask);
1509 // Compute the masks for andi/andis that would be necessary.
1510 unsigned ANDIMask = (Mask & UINT16_MAX),
1511 ANDISMask = (Mask >> 16) & UINT16_MAX;
1512
1513 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1514
1515 unsigned NumAndInsts = (unsigned) NeedsRotate +
1516 (unsigned) (bool) Res;
1517 if (Use32BitInsts)
1518 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1519 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1520 else
1521 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1;
1522
1523 unsigned NumRLInsts = 0;
1524 bool FirstBG = true;
1525 for (auto &BG : BitGroups) {
1526 if (!MatchingBG(BG))
1527 continue;
1528 NumRLInsts +=
1529 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1530 !FirstBG);
1531 FirstBG = false;
1532 }
1533
1534 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1535 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1536 "\n\t\t\tisel using masking: " << NumAndInsts <<
1537 " using rotates: " << NumRLInsts << "\n");
1538
1539 // When we'd use andi/andis, we bias toward using the rotates (andi only
1540 // has a record form, and is cracked on POWER cores). However, when using
1541 // general 64-bit constant formation, bias toward the constant form,
1542 // because that exposes more opportunities for CSE.
1543 if (NumAndInsts > NumRLInsts)
1544 continue;
1545 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1546 continue;
1547
1548 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1549
1550 if (InstCnt) *InstCnt += NumAndInsts;
1551
1552 SDValue VRot;
1553 // We actually need to generate a rotation if we have a non-zero rotation
1554 // factor or, in the Repl32 case, if we care about any of the
1555 // higher-order replicated bits. In the latter case, we generate a mask
1556 // backward so that it actually includes the entire 64 bits.
1557 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1558 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1559 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1560 else
1561 VRot = VRI.V;
1562
1563 SDValue TotalVal;
1564 if (Use32BitInsts) {
1565 assert((ANDIMask != 0 || ANDISMask != 0) &&
1566 "No set bits in mask when using 32-bit ands for 64-bit value");
1567
1568 SDValue ANDIVal, ANDISVal;
1569 if (ANDIMask != 0)
1570 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1571 VRot, getI32Imm(ANDIMask)), 0);
1572 if (ANDISMask != 0)
1573 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1574 VRot, getI32Imm(ANDISMask)), 0);
1575
1576 if (!ANDIVal)
1577 TotalVal = ANDISVal;
1578 else if (!ANDISVal)
1579 TotalVal = ANDIVal;
1580 else
1581 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1582 ANDIVal, ANDISVal), 0);
1583 } else {
1584 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1585 TotalVal =
1586 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1587 VRot, TotalVal), 0);
1588 }
1589
1590 if (!Res)
1591 Res = TotalVal;
1592 else
1593 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1594 Res, TotalVal), 0);
1595
1596 // Now, remove all groups with this underlying value and rotation
1597 // factor.
1598 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1599 if (MatchingBG(*I))
1600 I = BitGroups.erase(I);
1601 else
1602 ++I;
1603 }
1604 }
1605 }
1606
1607 // Instruction selection for the 64-bit case.
1608 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1609 SDLoc dl(N);
1610 SDValue Res;
1611
1612 if (InstCnt) *InstCnt = 0;
1613
1614 // Take care of cases that should use andi/andis first.
1615 SelectAndParts64(dl, Res, InstCnt);
1616
1617 // If we've not yet selected a 'starting' instruction, and we have no zeros
1618 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1619 // number of groups), and start with this rotated value.
1620 if ((!HasZeros || LateMask) && !Res) {
1621 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1622 // groups will come first, and so the VRI representing the largest number
1623 // of groups might not be first (it might be the first Repl32 groups).
1624 unsigned MaxGroupsIdx = 0;
1625 if (!ValueRotsVec[0].Repl32) {
1626 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1627 if (ValueRotsVec[i].Repl32) {
1628 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1629 MaxGroupsIdx = i;
1630 break;
1631 }
1632 }
1633
1634 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1635 bool NeedsRotate = false;
1636 if (VRI.RLAmt) {
1637 NeedsRotate = true;
1638 } else if (VRI.Repl32) {
1639 for (auto &BG : BitGroups) {
1640 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1641 BG.Repl32 != VRI.Repl32)
1642 continue;
1643
1644 // We don't need a rotate if the bit group is confined to the lower
1645 // 32 bits.
1646 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1647 continue;
1648
1649 NeedsRotate = true;
1650 break;
1651 }
1652 }
1653
1654 if (NeedsRotate)
1655 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1656 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1657 InstCnt);
1658 else
1659 Res = VRI.V;
1660
1661 // Now, remove all groups with this underlying value and rotation factor.
1662 if (Res)
1663 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1664 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt && I->Repl32 == VRI.Repl32)
1665 I = BitGroups.erase(I);
1666 else
1667 ++I;
1668 }
1669 }
1670
1671 // Because 64-bit rotates are more flexible than inserts, we might have a
1672 // preference regarding which one we do first (to save one instruction).
1673 if (!Res)
1674 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1675 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1676 false) <
1677 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1678 true)) {
1679 if (I != BitGroups.begin()) {
1680 BitGroup BG = *I;
1681 BitGroups.erase(I);
1682 BitGroups.insert(BitGroups.begin(), BG);
1683 }
1684
1685 break;
1686 }
1687 }
1688
1689 // Insert the other groups (one at a time).
1690 for (auto &BG : BitGroups) {
1691 if (!Res)
1692 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1693 BG.EndIdx, InstCnt);
1694 else
1695 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1696 BG.StartIdx, BG.EndIdx, InstCnt);
1697 }
1698
1699 if (LateMask) {
1700 uint64_t Mask = getZerosMask();
1701
1702 // We can use the 32-bit andi/andis technique if the mask does not
1703 // require any higher-order bits. This can save an instruction compared
1704 // to always using the general 64-bit technique.
1705 bool Use32BitInsts = isUInt<32>(Mask);
1706 // Compute the masks for andi/andis that would be necessary.
1707 unsigned ANDIMask = (Mask & UINT16_MAX),
1708 ANDISMask = (Mask >> 16) & UINT16_MAX;
1709
1710 if (Use32BitInsts) {
1711 assert((ANDIMask != 0 || ANDISMask != 0) &&
1712 "No set bits in mask when using 32-bit ands for 64-bit value");
1713
1714 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1715 (unsigned) (ANDISMask != 0) +
1716 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1717
1718 SDValue ANDIVal, ANDISVal;
1719 if (ANDIMask != 0)
1720 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1721 Res, getI32Imm(ANDIMask)), 0);
1722 if (ANDISMask != 0)
1723 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1724 Res, getI32Imm(ANDISMask)), 0);
1725
1726 if (!ANDIVal)
1727 Res = ANDISVal;
1728 else if (!ANDISVal)
1729 Res = ANDIVal;
1730 else
1731 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1732 ANDIVal, ANDISVal), 0);
1733 } else {
1734 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1;
1735
1736 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1737 Res =
1738 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1739 Res, MaskVal), 0);
1740 }
1741 }
1742
1743 return Res.getNode();
1744 }
1745
1746 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1747 // Fill in BitGroups.
1748 collectBitGroups(LateMask);
1749 if (BitGroups.empty())
1750 return nullptr;
1751
1752 // For 64-bit values, figure out when we can use 32-bit instructions.
1753 if (Bits.size() == 64)
1754 assignRepl32BitGroups();
1755
1756 // Fill in ValueRotsVec.
1757 collectValueRotInfo();
1758
1759 if (Bits.size() == 32) {
1760 return Select32(N, LateMask, InstCnt);
1761 } else {
1762 assert(Bits.size() == 64 && "Not 64 bits here?");
1763 return Select64(N, LateMask, InstCnt);
1764 }
1765
1766 return nullptr;
1767 }
1768
Hal Finkel8adf2252014-12-16 05:51:41 +00001769 SmallVector<ValueBit, 64> Bits;
1770
1771 bool HasZeros;
1772 SmallVector<unsigned, 64> RLAmt;
1773
1774 SmallVector<BitGroup, 16> BitGroups;
1775
1776 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1777 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1778
1779 SelectionDAG *CurDAG;
1780
1781public:
1782 BitPermutationSelector(SelectionDAG *DAG)
1783 : CurDAG(DAG) {}
1784
1785 // Here we try to match complex bit permutations into a set of
1786 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1787 // known to produce optimial code for common cases (like i32 byte swapping).
1788 SDNode *Select(SDNode *N) {
1789 Bits.resize(N->getValueType(0).getSizeInBits());
1790 if (!getValueBits(SDValue(N, 0), Bits))
1791 return nullptr;
1792
1793 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1794 " selection for: ");
1795 DEBUG(N->dump(CurDAG));
1796
1797 // Fill it RLAmt and set HasZeros.
1798 computeRotationAmounts();
1799
Hal Finkelc58ce412015-01-01 02:53:29 +00001800 if (!HasZeros)
1801 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001802
Hal Finkelc58ce412015-01-01 02:53:29 +00001803 // We currently have two techniques for handling results with zeros: early
1804 // masking (the default) and late masking. Late masking is sometimes more
1805 // efficient, but because the structure of the bit groups is different, it
1806 // is hard to tell without generating both and comparing the results. With
1807 // late masking, we ignore zeros in the resulting value when inserting each
1808 // set of bit groups, and then mask in the zeros at the end. With early
1809 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001810
Hal Finkelc58ce412015-01-01 02:53:29 +00001811 unsigned InstCnt, InstCntLateMask;
1812 DEBUG(dbgs() << "\tEarly masking:\n");
1813 SDNode *RN = Select(N, false, &InstCnt);
1814 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1815
1816 DEBUG(dbgs() << "\tLate masking:\n");
1817 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1818 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1819 " instructions\n");
1820
1821 if (InstCnt <= InstCntLateMask) {
1822 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1823 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001824 }
1825
Hal Finkelc58ce412015-01-01 02:53:29 +00001826 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1827 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001828 }
1829};
1830} // anonymous namespace
1831
1832SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) {
1833 if (N->getValueType(0) != MVT::i32 &&
1834 N->getValueType(0) != MVT::i64)
1835 return nullptr;
1836
Hal Finkelc58ce412015-01-01 02:53:29 +00001837 if (!UseBitPermRewriter)
1838 return nullptr;
1839
Hal Finkel8adf2252014-12-16 05:51:41 +00001840 switch (N->getOpcode()) {
1841 default: break;
1842 case ISD::ROTL:
1843 case ISD::SHL:
1844 case ISD::SRL:
1845 case ISD::AND:
1846 case ISD::OR: {
1847 BitPermutationSelector BPS(CurDAG);
1848 return BPS.Select(N);
1849 }
1850 }
1851
1852 return nullptr;
1853}
1854
Chris Lattner2a1823d2005-08-21 18:50:37 +00001855/// SelectCC - Select a comparison of the specified values with the specified
1856/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001857SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001858 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001859 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001860 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001861
Owen Anderson9f944592009-08-11 20:47:22 +00001862 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001863 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001864 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1865 if (isInt32Immediate(RHS, Imm)) {
1866 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001867 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001868 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1869 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001870 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001871 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001872 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1873 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00001874
Chris Lattneraa3926b2006-09-20 04:25:47 +00001875 // For non-equality comparisons, the default code would materialize the
1876 // constant, then compare against it, like this:
1877 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00001878 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00001879 // cmpw cr0, r3, r2
1880 // Since we are just comparing for equality, we can emit this instead:
1881 // xoris r0,r3,0x1234
1882 // cmplwi cr0,r0,0x5678
1883 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00001884 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
1885 getI32Imm(Imm >> 16)), 0);
1886 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
1887 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001888 }
1889 Opc = PPC::CMPLW;
1890 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00001891 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001892 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1893 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00001894 Opc = PPC::CMPLW;
1895 } else {
1896 short SImm;
1897 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001898 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1899 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00001900 0);
1901 Opc = PPC::CMPW;
1902 }
Owen Anderson9f944592009-08-11 20:47:22 +00001903 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00001904 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001905 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001906 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001907 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001908 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001909 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
1910 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001911 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001912 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001913 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
1914 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00001915
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001916 // For non-equality comparisons, the default code would materialize the
1917 // constant, then compare against it, like this:
1918 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00001919 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001920 // cmpd cr0, r3, r2
1921 // Since we are just comparing for equality, we can emit this instead:
1922 // xoris r0,r3,0x1234
1923 // cmpldi cr0,r0,0x5678
1924 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00001925 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00001926 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
1927 getI64Imm(Imm >> 16)), 0);
1928 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
1929 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001930 }
1931 }
1932 Opc = PPC::CMPLD;
1933 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00001934 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001935 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
1936 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00001937 Opc = PPC::CMPLD;
1938 } else {
1939 short SImm;
1940 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001941 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
1942 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00001943 0);
1944 Opc = PPC::CMPD;
1945 }
Owen Anderson9f944592009-08-11 20:47:22 +00001946 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00001947 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00001948 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001949 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00001950 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00001951 }
Dan Gohman32f71d72009-09-25 18:54:59 +00001952 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00001953}
1954
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001955static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001956 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00001957 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00001958 case ISD::SETONE:
1959 case ISD::SETOLE:
1960 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001961 llvm_unreachable("Should be lowered by legalize!");
1962 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00001963 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001964 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00001965 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001966 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00001967 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001968 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00001969 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001970 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00001971 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001972 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00001973 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001974 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00001975 case ISD::SETO: return PPC::PRED_NU;
1976 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00001977 // These two are invalid for floating point. Assume we have int.
1978 case ISD::SETULT: return PPC::PRED_LT;
1979 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00001980 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00001981}
1982
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001983/// getCRIdxForSetCC - Return the index of the condition register field
1984/// associated with the SetCC condition, and whether or not the field is
1985/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00001986static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00001987 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001988 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001989 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00001990 case ISD::SETOLT:
1991 case ISD::SETLT: return 0; // Bit #0 = SETOLT
1992 case ISD::SETOGT:
1993 case ISD::SETGT: return 1; // Bit #1 = SETOGT
1994 case ISD::SETOEQ:
1995 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
1996 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001997 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00001998 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00001999 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002000 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002001 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002002 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2003 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002004 case ISD::SETUEQ:
2005 case ISD::SETOGE:
2006 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002007 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002008 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002009 // These are invalid for floating point. Assume integer.
2010 case ISD::SETULT: return 0;
2011 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002012 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002013}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002014
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002015// getVCmpInst: return the vector compare instruction for the specified
2016// vector type and condition code. Since this is for altivec specific code,
2017// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002018static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2019 bool HasVSX, bool &Swap, bool &Negate) {
2020 Swap = false;
2021 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002022
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002023 if (VecVT.isFloatingPoint()) {
2024 /* Handle some cases by swapping input operands. */
2025 switch (CC) {
2026 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2027 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2028 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2029 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2030 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2031 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2032 default: break;
2033 }
2034 /* Handle some cases by negating the result. */
2035 switch (CC) {
2036 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2037 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2038 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2039 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2040 default: break;
2041 }
2042 /* We have instructions implementing the remaining cases. */
2043 switch (CC) {
2044 case ISD::SETEQ:
2045 case ISD::SETOEQ:
2046 if (VecVT == MVT::v4f32)
2047 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2048 else if (VecVT == MVT::v2f64)
2049 return PPC::XVCMPEQDP;
2050 break;
2051 case ISD::SETGT:
2052 case ISD::SETOGT:
2053 if (VecVT == MVT::v4f32)
2054 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2055 else if (VecVT == MVT::v2f64)
2056 return PPC::XVCMPGTDP;
2057 break;
2058 case ISD::SETGE:
2059 case ISD::SETOGE:
2060 if (VecVT == MVT::v4f32)
2061 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2062 else if (VecVT == MVT::v2f64)
2063 return PPC::XVCMPGEDP;
2064 break;
2065 default:
2066 break;
2067 }
2068 llvm_unreachable("Invalid floating-point vector compare condition");
2069 } else {
2070 /* Handle some cases by swapping input operands. */
2071 switch (CC) {
2072 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2073 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2074 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2075 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2076 default: break;
2077 }
2078 /* Handle some cases by negating the result. */
2079 switch (CC) {
2080 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2081 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2082 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2083 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2084 default: break;
2085 }
2086 /* We have instructions implementing the remaining cases. */
2087 switch (CC) {
2088 case ISD::SETEQ:
2089 case ISD::SETUEQ:
2090 if (VecVT == MVT::v16i8)
2091 return PPC::VCMPEQUB;
2092 else if (VecVT == MVT::v8i16)
2093 return PPC::VCMPEQUH;
2094 else if (VecVT == MVT::v4i32)
2095 return PPC::VCMPEQUW;
2096 break;
2097 case ISD::SETGT:
2098 if (VecVT == MVT::v16i8)
2099 return PPC::VCMPGTSB;
2100 else if (VecVT == MVT::v8i16)
2101 return PPC::VCMPGTSH;
2102 else if (VecVT == MVT::v4i32)
2103 return PPC::VCMPGTSW;
2104 break;
2105 case ISD::SETUGT:
2106 if (VecVT == MVT::v16i8)
2107 return PPC::VCMPGTUB;
2108 else if (VecVT == MVT::v8i16)
2109 return PPC::VCMPGTUH;
2110 else if (VecVT == MVT::v4i32)
2111 return PPC::VCMPGTUW;
2112 break;
2113 default:
2114 break;
2115 }
2116 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002117 }
2118}
2119
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002120SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002121 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002122 unsigned Imm;
2123 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002124 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2125 bool isPPC64 = (PtrVT == MVT::i64);
2126
Eric Christopher1b8e7632014-05-22 01:07:24 +00002127 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002128 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002129 // We can codegen setcc op, imm very efficiently compared to a brcond.
2130 // Check for those cases here.
2131 // setcc op, 0
2132 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002133 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002134 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002135 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002136 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002137 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002138 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002139 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002140 }
Chris Lattnere2969492005-10-21 21:17:10 +00002141 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002142 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002143 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002144 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002145 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002146 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002147 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002148 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002149 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002150 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002151 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002152 }
Chris Lattnere2969492005-10-21 21:17:10 +00002153 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002154 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002155 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2156 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002157 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002158 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002159 }
2160 }
Chris Lattner491b8292005-10-06 19:03:35 +00002161 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002162 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002163 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002164 default: break;
2165 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002166 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002167 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002168 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002169 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2170 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +00002171 MVT::i32,
2172 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002173 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002174 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002175 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002176 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002177 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002178 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +00002179 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002180 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +00002181 }
Chris Lattnere2969492005-10-21 21:17:10 +00002182 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002183 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
2184 getI32Imm(1)), 0);
2185 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2186 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002187 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002188 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002189 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002190 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002191 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +00002192 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002193 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002194 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002195 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002196 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002197 }
Chris Lattner491b8292005-10-06 19:03:35 +00002198 }
2199 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002200
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002201 SDValue LHS = N->getOperand(0);
2202 SDValue RHS = N->getOperand(1);
2203
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002204 // Altivec Vector compare instructions do not set any CR register by default and
2205 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002206 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002207 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002208 bool Swap, Negate;
2209 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2210 PPCSubTarget->hasVSX(), Swap, Negate);
2211 if (Swap)
2212 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002213
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002214 if (Negate) {
2215 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
2216 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
2217 PPC::VNOR,
2218 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002219 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002220
2221 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002222 }
2223
Eric Christopher1b8e7632014-05-22 01:07:24 +00002224 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +00002225 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +00002226
Chris Lattner491b8292005-10-06 19:03:35 +00002227 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002228 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002229 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002230 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002231
Chris Lattner491b8292005-10-06 19:03:35 +00002232 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002233 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002234
Craig Topper062a2ba2014-04-25 05:30:21 +00002235 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002236 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002237 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002238
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002239 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2240 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002241
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002242 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +00002243 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +00002244 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +00002245 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +00002246
2247 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002248 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002249 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +00002250 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002251}
Chris Lattner502a3692005-10-06 18:56:10 +00002252
Chris Lattner318622f2005-10-06 19:07:45 +00002253
Chris Lattner43ff01e2005-08-17 19:33:03 +00002254// Select - Convert the specified operand from a target-independent to a
2255// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002256SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002257 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002258 if (N->isMachineOpcode()) {
2259 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002260 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002261 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002262
Hal Finkel51b3fd12014-09-02 06:23:54 +00002263 // In case any misguided DAG-level optimizations form an ADD with a
2264 // TargetConstant operand, crash here instead of miscompiling (by selecting
2265 // an r+r add instead of some kind of r+i add).
2266 if (N->getOpcode() == ISD::ADD &&
2267 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2268 llvm_unreachable("Invalid ADD with TargetConstant operand");
2269
Hal Finkel8adf2252014-12-16 05:51:41 +00002270 // Try matching complex bit permutations before doing anything else.
2271 if (SDNode *NN = SelectBitPermutation(N))
2272 return NN;
2273
Chris Lattner43ff01e2005-08-17 19:33:03 +00002274 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002275 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002276
Jim Laskey095e6f32006-12-12 13:23:43 +00002277 case ISD::Constant: {
Hal Finkelc58ce412015-01-01 02:53:29 +00002278 if (N->getValueType(0) == MVT::i64)
2279 return SelectInt64(CurDAG, N);
Jim Laskey095e6f32006-12-12 13:23:43 +00002280 break;
2281 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002282
Hal Finkel940ab932014-02-28 00:27:01 +00002283 case ISD::SETCC: {
2284 SDNode *SN = SelectSETCC(N);
2285 if (SN)
2286 return SN;
2287 break;
2288 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002289 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00002290 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00002291
Hal Finkelb5e9b042014-12-11 22:51:06 +00002292 case ISD::FrameIndex:
2293 return getFrameIndex(N, N);
Chris Lattner6961fc72006-03-26 10:06:40 +00002294
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002295 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002296 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002297 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2298 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00002299 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002300
Hal Finkelbbdee932014-12-02 22:01:00 +00002301 case PPCISD::READ_TIME_BASE: {
2302 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2303 MVT::Other, N->getOperand(0));
2304 }
2305
Hal Finkel13d104b2014-12-11 18:37:52 +00002306 case PPCISD::SRA_ADDZE: {
2307 SDValue N0 = N->getOperand(0);
2308 SDValue ShiftAmt =
2309 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
2310 getConstantIntValue(), N->getValueType(0));
2311 if (N->getValueType(0) == MVT::i64) {
2312 SDNode *Op =
2313 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2314 N0, ShiftAmt);
2315 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64,
2316 SDValue(Op, 0), SDValue(Op, 1));
2317 } else {
2318 assert(N->getValueType(0) == MVT::i32 &&
2319 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2320 SDNode *Op =
2321 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2322 N0, ShiftAmt);
2323 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2324 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00002325 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002326 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002327
Chris Lattnerce645542006-11-10 02:08:47 +00002328 case ISD::LOAD: {
2329 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002330 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002331 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002332
Chris Lattnerce645542006-11-10 02:08:47 +00002333 // Normal loads are handled by code generated from the .td file.
2334 if (LD->getAddressingMode() != ISD::PRE_INC)
2335 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002336
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002337 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002338 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002339 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002340
Chris Lattner474b5b72006-11-15 19:55:13 +00002341 unsigned Opcode;
2342 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002343 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002344 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002345 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2346 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002347 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002348 case MVT::f64: Opcode = PPC::LFDU; break;
2349 case MVT::f32: Opcode = PPC::LFSU; break;
2350 case MVT::i32: Opcode = PPC::LWZU; break;
2351 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2352 case MVT::i1:
2353 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002354 }
2355 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002356 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2357 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2358 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002359 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002360 case MVT::i64: Opcode = PPC::LDU; break;
2361 case MVT::i32: Opcode = PPC::LWZU8; break;
2362 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2363 case MVT::i1:
2364 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002365 }
2366 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002367
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002368 SDValue Chain = LD->getChain();
2369 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002370 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00002371 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00002372 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00002373 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00002374 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002375 unsigned Opcode;
2376 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2377 if (LD->getValueType(0) != MVT::i64) {
2378 // Handle PPC32 integer and normal FP loads.
2379 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2380 switch (LoadedVT.getSimpleVT().SimpleTy) {
2381 default: llvm_unreachable("Invalid PPC load type!");
2382 case MVT::f64: Opcode = PPC::LFDUX; break;
2383 case MVT::f32: Opcode = PPC::LFSUX; break;
2384 case MVT::i32: Opcode = PPC::LWZUX; break;
2385 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2386 case MVT::i1:
2387 case MVT::i8: Opcode = PPC::LBZUX; break;
2388 }
2389 } else {
2390 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2391 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2392 "Invalid sext update load");
2393 switch (LoadedVT.getSimpleVT().SimpleTy) {
2394 default: llvm_unreachable("Invalid PPC load type!");
2395 case MVT::i64: Opcode = PPC::LDUX; break;
2396 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2397 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2398 case MVT::i1:
2399 case MVT::i8: Opcode = PPC::LBZUX8; break;
2400 }
2401 }
2402
2403 SDValue Chain = LD->getChain();
2404 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002405 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00002406 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00002407 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00002408 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00002409 }
2410 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002411
Nate Begemanb3821a32005-08-18 07:30:46 +00002412 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002413 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002414 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002415
Nate Begemanb3821a32005-08-18 07:30:46 +00002416 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2417 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002418 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002419 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002420 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002421 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002422 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00002423 }
Nate Begemand31efd12006-09-22 05:01:56 +00002424 // If this is just a masked value where the input is not handled above, and
2425 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2426 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002427 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002428 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002429 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002430 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002431 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00002432 }
Hal Finkele39526a2012-08-28 02:10:15 +00002433 // If this is a 64-bit zero-extension mask, emit rldicl.
2434 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2435 isMask_64(Imm64)) {
2436 SDValue Val = N->getOperand(0);
2437 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002438 SH = 0;
2439
2440 // If the operand is a logical right shift, we can fold it into this
2441 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2442 // for n <= mb. The right shift is really a left rotate followed by a
2443 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2444 // by the shift.
2445 if (Val.getOpcode() == ISD::SRL &&
2446 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2447 assert(Imm < 64 && "Illegal shift amount");
2448 Val = Val.getOperand(0);
2449 SH = 64 - Imm;
2450 }
2451
2452 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00002453 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00002454 }
Nate Begemand31efd12006-09-22 05:01:56 +00002455 // AND X, 0 -> 0, not "rlwinm 32".
2456 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002457 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00002458 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00002459 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002460 // ISD::OR doesn't get all the bitfield insertion fun.
2461 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00002462 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002463 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002464 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00002465 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00002466 Imm = ~(Imm^Imm2);
2467 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002468 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002469 N->getOperand(0).getOperand(1),
2470 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00002471 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00002472 }
2473 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002474
Chris Lattner1de57062005-09-29 23:33:31 +00002475 // Other cases are autogenerated.
2476 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002477 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002478 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002479 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00002480 if (SDNode *I = SelectBitfieldInsert(N))
2481 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00002482
Hal Finkelb5e9b042014-12-11 22:51:06 +00002483 short Imm;
2484 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2485 isIntS16Immediate(N->getOperand(1), Imm)) {
2486 APInt LHSKnownZero, LHSKnownOne;
2487 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2488
2489 // If this is equivalent to an add, then we can fold it with the
2490 // FrameIndex calculation.
2491 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL)
2492 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2493 }
2494
Chris Lattner1de57062005-09-29 23:33:31 +00002495 // Other cases are autogenerated.
2496 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002497 }
2498 case ISD::ADD: {
2499 short Imm;
2500 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2501 isIntS16Immediate(N->getOperand(1), Imm))
2502 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2503
2504 break;
2505 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002506 case ISD::SHL: {
2507 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002508 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002509 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002510 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002511 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002512 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002513 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002514
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002515 // Other cases are autogenerated.
2516 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002517 }
2518 case ISD::SRL: {
2519 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002520 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002521 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002522 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002523 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002524 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002525 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002526
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002527 // Other cases are autogenerated.
2528 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002529 }
Hal Finkel940ab932014-02-28 00:27:01 +00002530 // FIXME: Remove this once the ANDI glue bug is fixed:
2531 case PPCISD::ANDIo_1_EQ_BIT:
2532 case PPCISD::ANDIo_1_GT_BIT: {
2533 if (!ANDIGlueBug)
2534 break;
2535
2536 EVT InVT = N->getOperand(0).getValueType();
2537 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2538 "Invalid input type for ANDIo_1_EQ_BIT");
2539
2540 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2541 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2542 N->getOperand(0),
2543 CurDAG->getTargetConstant(1, InVT)), 0);
2544 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2545 SDValue SRIdxVal =
2546 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
2547 PPC::sub_eq : PPC::sub_gt, MVT::i32);
2548
2549 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
2550 CR0Reg, SRIdxVal,
2551 SDValue(AndI.getNode(), 1) /* glue */);
2552 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002553 case ISD::SELECT_CC: {
2554 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002555 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2556 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002557
Hal Finkel940ab932014-02-28 00:27:01 +00002558 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002559 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002560 N->getOperand(0).getValueType() == MVT::i1)
2561 break;
2562
Chris Lattner97b3da12006-06-27 00:04:13 +00002563 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002564 if (!isPPC64)
2565 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2566 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2567 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2568 if (N1C->isNullValue() && N3C->isNullValue() &&
2569 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2570 // FIXME: Implement this optzn for PPC64.
2571 N->getValueType(0) == MVT::i32) {
2572 SDNode *Tmp =
2573 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
2574 N->getOperand(0), getI32Imm(~0U));
2575 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
2576 SDValue(Tmp, 0), N->getOperand(0),
2577 SDValue(Tmp, 1));
2578 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002579
Dale Johannesenab8e4422009-02-06 19:16:40 +00002580 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002581
2582 if (N->getValueType(0) == MVT::i1) {
2583 // An i1 select is: (c & t) | (!c & f).
2584 bool Inv;
2585 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2586
2587 unsigned SRI;
2588 switch (Idx) {
2589 default: llvm_unreachable("Invalid CC index");
2590 case 0: SRI = PPC::sub_lt; break;
2591 case 1: SRI = PPC::sub_gt; break;
2592 case 2: SRI = PPC::sub_eq; break;
2593 case 3: SRI = PPC::sub_un; break;
2594 }
2595
2596 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2597
2598 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2599 CCBit, CCBit), 0);
2600 SDValue C = Inv ? NotCCBit : CCBit,
2601 NotC = Inv ? CCBit : NotCCBit;
2602
2603 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2604 C, N->getOperand(2)), 0);
2605 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2606 NotC, N->getOperand(3)), 0);
2607
2608 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2609 }
2610
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002611 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002612
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002613 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002614 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002615 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002616 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002617 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002618 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002619 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002620 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002621 if (PPCSubTarget->hasVSX())
2622 SelectCCOp = PPC::SELECT_CC_VSFRC;
2623 else
2624 SelectCCOp = PPC::SELECT_CC_F8;
Bill Schmidt61e65232014-10-22 13:13:40 +00002625 else if (N->getValueType(0) == MVT::v2f64 ||
2626 N->getValueType(0) == MVT::v2i64)
2627 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002628 else
2629 SelectCCOp = PPC::SELECT_CC_VRRC;
2630
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002631 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00002632 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00002633 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00002634 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002635 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002636 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002637 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002638 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00002639 }
2640
2641 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002642 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002643 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002644 N->getValueType(0) == MVT::v2i64)) {
2645 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
2646
2647 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2648 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2649 unsigned DM[2];
2650
2651 for (int i = 0; i < 2; ++i)
2652 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2653 DM[i] = 0;
2654 else
2655 DM[i] = 1;
2656
Bill Schmidt30144352014-12-09 16:52:29 +00002657 // For little endian, we must swap the input operands and adjust
2658 // the mask elements (reverse and invert them).
2659 if (PPCSubTarget->isLittleEndian()) {
2660 std::swap(Op1, Op2);
2661 unsigned tmp = DM[0];
2662 DM[0] = 1 - DM[1];
2663 DM[1] = 1 - tmp;
2664 }
2665
Hal Finkel2583b062014-03-28 20:24:55 +00002666 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002667
2668 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2669 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2670 isa<LoadSDNode>(Op1.getOperand(0))) {
2671 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2672 SDValue Base, Offset;
2673
2674 if (LD->isUnindexed() &&
2675 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2676 SDValue Chain = LD->getChain();
2677 SDValue Ops[] = { Base, Offset, Chain };
2678 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00002679 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002680 }
2681 }
2682
2683 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00002684 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002685 }
2686
2687 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002688 case PPCISD::BDNZ:
2689 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002690 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002691 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
2692 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
2693 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
2694 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00002695 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00002696 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002697 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002698 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002699 // Op #1 is the PPC::PRED_* number.
2700 // Op #2 is the CR#
2701 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002702 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002703 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002704 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00002705 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002706 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002707 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00002708 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002709 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002710 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002711 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002712 unsigned PCC = getPredicateForSetCC(CC);
2713
2714 if (N->getOperand(2).getValueType() == MVT::i1) {
2715 unsigned Opc;
2716 bool Swap;
2717 switch (PCC) {
2718 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2719 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2720 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2721 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2722 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2723 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2724 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2725 }
2726
2727 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2728 N->getOperand(Swap ? 3 : 2),
2729 N->getOperand(Swap ? 2 : 3)), 0);
2730 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
2731 BitComp, N->getOperand(4), N->getOperand(0));
2732 }
2733
Dale Johannesenab8e4422009-02-06 19:16:40 +00002734 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002735 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002736 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002737 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002738 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002739 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002740 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002741 SDValue Chain = N->getOperand(0);
2742 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002743 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002744 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002745 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002746 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00002747 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002748 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002749 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002750 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2751 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002752 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2753 SDValue GA = N->getOperand(0);
2754 return CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
2755 N->getOperand(1));
Justin Hibbits3476db42014-08-28 04:40:55 +00002756 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002757
Bill Schmidt27917782013-02-21 17:12:27 +00002758 // For medium and large code model, we generate two instructions as
2759 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002760 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00002761 CodeModel::Model CModel = TM.getCodeModel();
2762 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002763 break;
2764
Bill Schmidt5d82f092014-06-16 21:36:02 +00002765 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
2766 // If it is an externally defined symbol, a symbol with common linkage,
2767 // a non-local function address, or a jump table address, or if we are
2768 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00002769 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
2770 // Otherwise we generate:
2771 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
2772 SDValue GA = N->getOperand(0);
2773 SDValue TOCbase = N->getOperand(1);
2774 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
2775 TOCbase, GA);
2776
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002777 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
2778 CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002779 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
2780 SDValue(Tmp, 0));
2781
2782 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
2783 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00002784 if ((GValue->getType()->getElementType()->isFunctionTy() &&
2785 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00002786 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
2787 GValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00002788 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
2789 SDValue(Tmp, 0));
2790 }
2791
2792 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
2793 SDValue(Tmp, 0), GA);
2794 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00002795 case PPCISD::PPC32_PICGOT: {
2796 // Generate a PIC-safe GOT reference.
2797 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
2798 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
2799 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
2800 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002801 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002802 // This expands into one of three sequences, depending on whether
2803 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00002804 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
2805 isa<ConstantSDNode>(N->getOperand(1)) &&
2806 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002807
2808 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00002809 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002810 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00002811 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002812
Bill Schmidt51e79512013-02-20 15:50:31 +00002813 if (EltSize == 1) {
2814 Opc1 = PPC::VSPLTISB;
2815 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002816 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002817 VT = MVT::v16i8;
2818 } else if (EltSize == 2) {
2819 Opc1 = PPC::VSPLTISH;
2820 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002821 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002822 VT = MVT::v8i16;
2823 } else {
2824 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
2825 Opc1 = PPC::VSPLTISW;
2826 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002827 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002828 VT = MVT::v4i32;
2829 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002830
2831 if ((Elt & 1) == 0) {
2832 // Elt is even, in the range [-32,-18] + [16,30].
2833 //
2834 // Convert: VADD_SPLAT elt, size
2835 // Into: tmp = VSPLTIS[BHW] elt
2836 // VADDU[BHW]M tmp, tmp
2837 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
2838 SDValue EltVal = getI32Imm(Elt >> 1);
2839 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2840 SDValue TmpVal = SDValue(Tmp, 0);
2841 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
2842
2843 } else if (Elt > 0) {
2844 // Elt is odd and positive, in the range [17,31].
2845 //
2846 // Convert: VADD_SPLAT elt, size
2847 // Into: tmp1 = VSPLTIS[BHW] elt-16
2848 // tmp2 = VSPLTIS[BHW] -16
2849 // VSUBU[BHW]M tmp1, tmp2
2850 SDValue EltVal = getI32Imm(Elt - 16);
2851 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2852 EltVal = getI32Imm(-16);
2853 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2854 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
2855 SDValue(Tmp2, 0));
2856
2857 } else {
2858 // Elt is odd and negative, in the range [-31,-17].
2859 //
2860 // Convert: VADD_SPLAT elt, size
2861 // Into: tmp1 = VSPLTIS[BHW] elt+16
2862 // tmp2 = VSPLTIS[BHW] -16
2863 // VADDU[BHW]M tmp1, tmp2
2864 SDValue EltVal = getI32Imm(Elt + 16);
2865 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2866 EltVal = getI32Imm(-16);
2867 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2868 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
2869 SDValue(Tmp2, 0));
2870 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002871 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00002872 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002873
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002874 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002875}
2876
Hal Finkel4edc66b2015-01-03 01:16:37 +00002877// If the target supports the cmpb instruction, do the idiom recognition here.
2878// We don't do this as a DAG combine because we don't want to do it as nodes
2879// are being combined (because we might miss part of the eventual idiom). We
2880// don't want to do it during instruction selection because we want to reuse
2881// the logic for lowering the masking operations already part of the
2882// instruction selector.
2883SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
2884 SDLoc dl(N);
2885
2886 assert(N->getOpcode() == ISD::OR &&
2887 "Only OR nodes are supported for CMPB");
2888
2889 SDValue Res;
2890 if (!PPCSubTarget->hasCMPB())
2891 return Res;
2892
2893 if (N->getValueType(0) != MVT::i32 &&
2894 N->getValueType(0) != MVT::i64)
2895 return Res;
2896
2897 EVT VT = N->getValueType(0);
2898
2899 SDValue RHS, LHS;
2900 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
2901 uint64_t Mask = 0, Alt = 0;
2902
2903 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
2904 uint64_t &Mask, uint64_t &Alt,
2905 SDValue &LHS, SDValue &RHS) {
2906 if (O.getOpcode() != ISD::SELECT_CC)
2907 return false;
2908 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
2909
2910 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
2911 !isa<ConstantSDNode>(O.getOperand(3)))
2912 return false;
2913
2914 uint64_t PM = O.getConstantOperandVal(2);
2915 uint64_t PAlt = O.getConstantOperandVal(3);
2916 for (b = 0; b < 8; ++b) {
2917 uint64_t Mask = UINT64_C(0xFF) << (8*b);
2918 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
2919 break;
2920 }
2921
2922 if (b == 8)
2923 return false;
2924 Mask |= PM;
2925 Alt |= PAlt;
2926
2927 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
2928 O.getConstantOperandVal(1) != 0) {
2929 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
2930 if (Op0.getOpcode() == ISD::TRUNCATE)
2931 Op0 = Op0.getOperand(0);
2932 if (Op1.getOpcode() == ISD::TRUNCATE)
2933 Op1 = Op1.getOperand(0);
2934
2935 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
2936 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
2937 isa<ConstantSDNode>(Op0.getOperand(1))) {
2938
2939 unsigned Bits = Op0.getValueType().getSizeInBits();
2940 if (b != Bits/8-1)
2941 return false;
2942 if (Op0.getConstantOperandVal(1) != Bits-8)
2943 return false;
2944
2945 LHS = Op0.getOperand(0);
2946 RHS = Op1.getOperand(0);
2947 return true;
2948 }
2949
2950 // When we have small integers (i16 to be specific), the form present
2951 // post-legalization uses SETULT in the SELECT_CC for the
2952 // higher-order byte, depending on the fact that the
2953 // even-higher-order bytes are known to all be zero, for example:
2954 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
2955 // (so when the second byte is the same, because all higher-order
2956 // bits from bytes 3 and 4 are known to be zero, the result of the
2957 // xor can be at most 255)
2958 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
2959 isa<ConstantSDNode>(O.getOperand(1))) {
2960
2961 uint64_t ULim = O.getConstantOperandVal(1);
2962 if (ULim != (UINT64_C(1) << b*8))
2963 return false;
2964
2965 // Now we need to make sure that the upper bytes are known to be
2966 // zero.
2967 unsigned Bits = Op0.getValueType().getSizeInBits();
2968 if (!CurDAG->MaskedValueIsZero(Op0,
2969 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
2970 return false;
2971
2972 LHS = Op0.getOperand(0);
2973 RHS = Op0.getOperand(1);
2974 return true;
2975 }
2976
2977 return false;
2978 }
2979
2980 if (CC != ISD::SETEQ)
2981 return false;
2982
2983 SDValue Op = O.getOperand(0);
2984 if (Op.getOpcode() == ISD::AND) {
2985 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2986 return false;
2987 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
2988 return false;
2989
2990 SDValue XOR = Op.getOperand(0);
2991 if (XOR.getOpcode() == ISD::TRUNCATE)
2992 XOR = XOR.getOperand(0);
2993 if (XOR.getOpcode() != ISD::XOR)
2994 return false;
2995
2996 LHS = XOR.getOperand(0);
2997 RHS = XOR.getOperand(1);
2998 return true;
2999 } else if (Op.getOpcode() == ISD::SRL) {
3000 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3001 return false;
3002 unsigned Bits = Op.getValueType().getSizeInBits();
3003 if (b != Bits/8-1)
3004 return false;
3005 if (Op.getConstantOperandVal(1) != Bits-8)
3006 return false;
3007
3008 SDValue XOR = Op.getOperand(0);
3009 if (XOR.getOpcode() == ISD::TRUNCATE)
3010 XOR = XOR.getOperand(0);
3011 if (XOR.getOpcode() != ISD::XOR)
3012 return false;
3013
3014 LHS = XOR.getOperand(0);
3015 RHS = XOR.getOperand(1);
3016 return true;
3017 }
3018
3019 return false;
3020 };
3021
3022 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3023 while (!Queue.empty()) {
3024 SDValue V = Queue.pop_back_val();
3025
3026 for (const SDValue &O : V.getNode()->ops()) {
3027 unsigned b;
3028 uint64_t M = 0, A = 0;
3029 SDValue OLHS, ORHS;
3030 if (O.getOpcode() == ISD::OR) {
3031 Queue.push_back(O);
3032 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3033 if (!LHS) {
3034 LHS = OLHS;
3035 RHS = ORHS;
3036 BytesFound[b] = true;
3037 Mask |= M;
3038 Alt |= A;
3039 } else if ((LHS == ORHS && RHS == OLHS) ||
3040 (RHS == ORHS && LHS == OLHS)) {
3041 BytesFound[b] = true;
3042 Mask |= M;
3043 Alt |= A;
3044 } else {
3045 return Res;
3046 }
3047 } else {
3048 return Res;
3049 }
3050 }
3051 }
3052
3053 unsigned LastB = 0, BCnt = 0;
3054 for (unsigned i = 0; i < 8; ++i)
3055 if (BytesFound[LastB]) {
3056 ++BCnt;
3057 LastB = i;
3058 }
3059
3060 if (!LastB || BCnt < 2)
3061 return Res;
3062
3063 // Because we'll be zero-extending the output anyway if don't have a specific
3064 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3065 if (LHS.getValueType() != VT) {
3066 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3067 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3068 }
3069
3070 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3071
3072 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3073 if (NonTrivialMask && !Alt) {
3074 // Res = Mask & CMPB
3075 Res = CurDAG->getNode(ISD::AND, dl, VT, Res, CurDAG->getConstant(Mask, VT));
3076 } else if (Alt) {
3077 // Res = (CMPB & Mask) | (~CMPB & Alt)
3078 // Which, as suggested here:
3079 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3080 // can be written as:
3081 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3082 // useful because the (Alt ^ Mask) can be pre-computed.
3083 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3084 CurDAG->getConstant(Mask ^ Alt, VT));
3085 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, CurDAG->getConstant(Alt, VT));
3086 }
3087
3088 return Res;
3089}
3090
3091void PPCDAGToDAGISel::PreprocessISelDAG() {
3092 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3093 ++Position;
3094
3095 bool MadeChange = false;
3096 while (Position != CurDAG->allnodes_begin()) {
3097 SDNode *N = --Position;
3098 if (N->use_empty())
3099 continue;
3100
3101 SDValue Res;
3102 switch (N->getOpcode()) {
3103 default: break;
3104 case ISD::OR:
3105 Res = combineToCMPB(N);
3106 break;
3107 }
3108
3109 if (Res) {
3110 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3111 DEBUG(N->dump(CurDAG));
3112 DEBUG(dbgs() << "\nNew: ");
3113 DEBUG(Res.getNode()->dump(CurDAG));
3114 DEBUG(dbgs() << "\n");
3115
3116 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3117 MadeChange = true;
3118 }
3119 }
3120
3121 if (MadeChange)
3122 CurDAG->RemoveDeadNodes();
3123}
3124
Hal Finkel860fa902014-01-02 22:09:39 +00003125/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003126/// on the DAG representation.
3127void PPCDAGToDAGISel::PostprocessISelDAG() {
3128
3129 // Skip peepholes at -O0.
3130 if (TM.getOptLevel() == CodeGenOpt::None)
3131 return;
3132
Hal Finkel940ab932014-02-28 00:27:01 +00003133 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003134 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003135 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003136}
3137
Hal Finkelb9989152014-02-28 06:11:16 +00003138// Check if all users of this node will become isel where the second operand
3139// is the constant zero. If this is so, and if we can negate the condition,
3140// then we can flip the true and false operands. This will allow the zero to
3141// be folded with the isel so that we don't need to materialize a register
3142// containing zero.
3143bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3144 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003145 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003146 return false;
3147
3148 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3149 UI != UE; ++UI) {
3150 SDNode *User = *UI;
3151 if (!User->isMachineOpcode())
3152 return false;
3153 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3154 User->getMachineOpcode() != PPC::SELECT_I8)
3155 return false;
3156
3157 SDNode *Op2 = User->getOperand(2).getNode();
3158 if (!Op2->isMachineOpcode())
3159 return false;
3160
3161 if (Op2->getMachineOpcode() != PPC::LI &&
3162 Op2->getMachineOpcode() != PPC::LI8)
3163 return false;
3164
3165 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3166 if (!C)
3167 return false;
3168
3169 if (!C->isNullValue())
3170 return false;
3171 }
3172
3173 return true;
3174}
3175
3176void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3177 SmallVector<SDNode *, 4> ToReplace;
3178 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3179 UI != UE; ++UI) {
3180 SDNode *User = *UI;
3181 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3182 User->getMachineOpcode() == PPC::SELECT_I8) &&
3183 "Must have all select users");
3184 ToReplace.push_back(User);
3185 }
3186
3187 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3188 UE = ToReplace.end(); UI != UE; ++UI) {
3189 SDNode *User = *UI;
3190 SDNode *ResNode =
3191 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3192 User->getValueType(0), User->getOperand(0),
3193 User->getOperand(2),
3194 User->getOperand(1));
3195
3196 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3197 DEBUG(User->dump(CurDAG));
3198 DEBUG(dbgs() << "\nNew: ");
3199 DEBUG(ResNode->dump(CurDAG));
3200 DEBUG(dbgs() << "\n");
3201
3202 ReplaceUses(User, ResNode);
3203 }
3204}
3205
Eric Christopher02e18042014-05-14 00:31:15 +00003206void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003207 bool IsModified;
3208 do {
3209 IsModified = false;
3210 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
3211 E = CurDAG->allnodes_end(); I != E; ++I) {
3212 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
3213 if (!MachineNode || MachineNode->use_empty())
3214 continue;
3215 SDNode *ResNode = MachineNode;
3216
3217 bool Op1Set = false, Op1Unset = false,
3218 Op1Not = false,
3219 Op2Set = false, Op2Unset = false,
3220 Op2Not = false;
3221
3222 unsigned Opcode = MachineNode->getMachineOpcode();
3223 switch (Opcode) {
3224 default: break;
3225 case PPC::CRAND:
3226 case PPC::CRNAND:
3227 case PPC::CROR:
3228 case PPC::CRXOR:
3229 case PPC::CRNOR:
3230 case PPC::CREQV:
3231 case PPC::CRANDC:
3232 case PPC::CRORC: {
3233 SDValue Op = MachineNode->getOperand(1);
3234 if (Op.isMachineOpcode()) {
3235 if (Op.getMachineOpcode() == PPC::CRSET)
3236 Op2Set = true;
3237 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3238 Op2Unset = true;
3239 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3240 Op.getOperand(0) == Op.getOperand(1))
3241 Op2Not = true;
3242 }
3243 } // fallthrough
3244 case PPC::BC:
3245 case PPC::BCn:
3246 case PPC::SELECT_I4:
3247 case PPC::SELECT_I8:
3248 case PPC::SELECT_F4:
3249 case PPC::SELECT_F8:
Bill Schmidt61e65232014-10-22 13:13:40 +00003250 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003251 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003252 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003253 SDValue Op = MachineNode->getOperand(0);
3254 if (Op.isMachineOpcode()) {
3255 if (Op.getMachineOpcode() == PPC::CRSET)
3256 Op1Set = true;
3257 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3258 Op1Unset = true;
3259 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3260 Op.getOperand(0) == Op.getOperand(1))
3261 Op1Not = true;
3262 }
3263 }
3264 break;
3265 }
3266
Hal Finkelb9989152014-02-28 06:11:16 +00003267 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003268 switch (Opcode) {
3269 default: break;
3270 case PPC::CRAND:
3271 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3272 // x & x = x
3273 ResNode = MachineNode->getOperand(0).getNode();
3274 else if (Op1Set)
3275 // 1 & y = y
3276 ResNode = MachineNode->getOperand(1).getNode();
3277 else if (Op2Set)
3278 // x & 1 = x
3279 ResNode = MachineNode->getOperand(0).getNode();
3280 else if (Op1Unset || Op2Unset)
3281 // x & 0 = 0 & y = 0
3282 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3283 MVT::i1);
3284 else if (Op1Not)
3285 // ~x & y = andc(y, x)
3286 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3287 MVT::i1, MachineNode->getOperand(1),
3288 MachineNode->getOperand(0).
3289 getOperand(0));
3290 else if (Op2Not)
3291 // x & ~y = andc(x, y)
3292 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3293 MVT::i1, MachineNode->getOperand(0),
3294 MachineNode->getOperand(1).
3295 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003296 else if (AllUsersSelectZero(MachineNode))
3297 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3298 MVT::i1, MachineNode->getOperand(0),
3299 MachineNode->getOperand(1)),
3300 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003301 break;
3302 case PPC::CRNAND:
3303 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3304 // nand(x, x) -> nor(x, x)
3305 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3306 MVT::i1, MachineNode->getOperand(0),
3307 MachineNode->getOperand(0));
3308 else if (Op1Set)
3309 // nand(1, y) -> nor(y, y)
3310 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3311 MVT::i1, MachineNode->getOperand(1),
3312 MachineNode->getOperand(1));
3313 else if (Op2Set)
3314 // nand(x, 1) -> nor(x, x)
3315 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3316 MVT::i1, MachineNode->getOperand(0),
3317 MachineNode->getOperand(0));
3318 else if (Op1Unset || Op2Unset)
3319 // nand(x, 0) = nand(0, y) = 1
3320 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3321 MVT::i1);
3322 else if (Op1Not)
3323 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3324 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3325 MVT::i1, MachineNode->getOperand(0).
3326 getOperand(0),
3327 MachineNode->getOperand(1));
3328 else if (Op2Not)
3329 // nand(x, ~y) = ~x | y = orc(y, x)
3330 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3331 MVT::i1, MachineNode->getOperand(1).
3332 getOperand(0),
3333 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003334 else if (AllUsersSelectZero(MachineNode))
3335 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3336 MVT::i1, MachineNode->getOperand(0),
3337 MachineNode->getOperand(1)),
3338 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003339 break;
3340 case PPC::CROR:
3341 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3342 // x | x = x
3343 ResNode = MachineNode->getOperand(0).getNode();
3344 else if (Op1Set || Op2Set)
3345 // x | 1 = 1 | y = 1
3346 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3347 MVT::i1);
3348 else if (Op1Unset)
3349 // 0 | y = y
3350 ResNode = MachineNode->getOperand(1).getNode();
3351 else if (Op2Unset)
3352 // x | 0 = x
3353 ResNode = MachineNode->getOperand(0).getNode();
3354 else if (Op1Not)
3355 // ~x | y = orc(y, x)
3356 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3357 MVT::i1, MachineNode->getOperand(1),
3358 MachineNode->getOperand(0).
3359 getOperand(0));
3360 else if (Op2Not)
3361 // x | ~y = orc(x, y)
3362 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3363 MVT::i1, MachineNode->getOperand(0),
3364 MachineNode->getOperand(1).
3365 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003366 else if (AllUsersSelectZero(MachineNode))
3367 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3368 MVT::i1, MachineNode->getOperand(0),
3369 MachineNode->getOperand(1)),
3370 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003371 break;
3372 case PPC::CRXOR:
3373 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3374 // xor(x, x) = 0
3375 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3376 MVT::i1);
3377 else if (Op1Set)
3378 // xor(1, y) -> nor(y, y)
3379 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3380 MVT::i1, MachineNode->getOperand(1),
3381 MachineNode->getOperand(1));
3382 else if (Op2Set)
3383 // xor(x, 1) -> nor(x, x)
3384 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3385 MVT::i1, MachineNode->getOperand(0),
3386 MachineNode->getOperand(0));
3387 else if (Op1Unset)
3388 // xor(0, y) = y
3389 ResNode = MachineNode->getOperand(1).getNode();
3390 else if (Op2Unset)
3391 // xor(x, 0) = x
3392 ResNode = MachineNode->getOperand(0).getNode();
3393 else if (Op1Not)
3394 // xor(~x, y) = eqv(x, y)
3395 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3396 MVT::i1, MachineNode->getOperand(0).
3397 getOperand(0),
3398 MachineNode->getOperand(1));
3399 else if (Op2Not)
3400 // xor(x, ~y) = eqv(x, y)
3401 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3402 MVT::i1, MachineNode->getOperand(0),
3403 MachineNode->getOperand(1).
3404 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003405 else if (AllUsersSelectZero(MachineNode))
3406 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3407 MVT::i1, MachineNode->getOperand(0),
3408 MachineNode->getOperand(1)),
3409 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003410 break;
3411 case PPC::CRNOR:
3412 if (Op1Set || Op2Set)
3413 // nor(1, y) -> 0
3414 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3415 MVT::i1);
3416 else if (Op1Unset)
3417 // nor(0, y) = ~y -> nor(y, y)
3418 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3419 MVT::i1, MachineNode->getOperand(1),
3420 MachineNode->getOperand(1));
3421 else if (Op2Unset)
3422 // nor(x, 0) = ~x
3423 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3424 MVT::i1, MachineNode->getOperand(0),
3425 MachineNode->getOperand(0));
3426 else if (Op1Not)
3427 // nor(~x, y) = andc(x, y)
3428 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3429 MVT::i1, MachineNode->getOperand(0).
3430 getOperand(0),
3431 MachineNode->getOperand(1));
3432 else if (Op2Not)
3433 // nor(x, ~y) = andc(y, x)
3434 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3435 MVT::i1, MachineNode->getOperand(1).
3436 getOperand(0),
3437 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003438 else if (AllUsersSelectZero(MachineNode))
3439 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3440 MVT::i1, MachineNode->getOperand(0),
3441 MachineNode->getOperand(1)),
3442 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003443 break;
3444 case PPC::CREQV:
3445 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3446 // eqv(x, x) = 1
3447 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3448 MVT::i1);
3449 else if (Op1Set)
3450 // eqv(1, y) = y
3451 ResNode = MachineNode->getOperand(1).getNode();
3452 else if (Op2Set)
3453 // eqv(x, 1) = x
3454 ResNode = MachineNode->getOperand(0).getNode();
3455 else if (Op1Unset)
3456 // eqv(0, y) = ~y -> nor(y, y)
3457 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3458 MVT::i1, MachineNode->getOperand(1),
3459 MachineNode->getOperand(1));
3460 else if (Op2Unset)
3461 // eqv(x, 0) = ~x
3462 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3463 MVT::i1, MachineNode->getOperand(0),
3464 MachineNode->getOperand(0));
3465 else if (Op1Not)
3466 // eqv(~x, y) = xor(x, y)
3467 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3468 MVT::i1, MachineNode->getOperand(0).
3469 getOperand(0),
3470 MachineNode->getOperand(1));
3471 else if (Op2Not)
3472 // eqv(x, ~y) = xor(x, y)
3473 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3474 MVT::i1, MachineNode->getOperand(0),
3475 MachineNode->getOperand(1).
3476 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003477 else if (AllUsersSelectZero(MachineNode))
3478 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3479 MVT::i1, MachineNode->getOperand(0),
3480 MachineNode->getOperand(1)),
3481 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003482 break;
3483 case PPC::CRANDC:
3484 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3485 // andc(x, x) = 0
3486 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3487 MVT::i1);
3488 else if (Op1Set)
3489 // andc(1, y) = ~y
3490 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3491 MVT::i1, MachineNode->getOperand(1),
3492 MachineNode->getOperand(1));
3493 else if (Op1Unset || Op2Set)
3494 // andc(0, y) = andc(x, 1) = 0
3495 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3496 MVT::i1);
3497 else if (Op2Unset)
3498 // andc(x, 0) = x
3499 ResNode = MachineNode->getOperand(0).getNode();
3500 else if (Op1Not)
3501 // andc(~x, y) = ~(x | y) = nor(x, y)
3502 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3503 MVT::i1, MachineNode->getOperand(0).
3504 getOperand(0),
3505 MachineNode->getOperand(1));
3506 else if (Op2Not)
3507 // andc(x, ~y) = x & y
3508 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3509 MVT::i1, MachineNode->getOperand(0),
3510 MachineNode->getOperand(1).
3511 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003512 else if (AllUsersSelectZero(MachineNode))
3513 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3514 MVT::i1, MachineNode->getOperand(1),
3515 MachineNode->getOperand(0)),
3516 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003517 break;
3518 case PPC::CRORC:
3519 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3520 // orc(x, x) = 1
3521 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3522 MVT::i1);
3523 else if (Op1Set || Op2Unset)
3524 // orc(1, y) = orc(x, 0) = 1
3525 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3526 MVT::i1);
3527 else if (Op2Set)
3528 // orc(x, 1) = x
3529 ResNode = MachineNode->getOperand(0).getNode();
3530 else if (Op1Unset)
3531 // orc(0, y) = ~y
3532 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3533 MVT::i1, MachineNode->getOperand(1),
3534 MachineNode->getOperand(1));
3535 else if (Op1Not)
3536 // orc(~x, y) = ~(x & y) = nand(x, y)
3537 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3538 MVT::i1, MachineNode->getOperand(0).
3539 getOperand(0),
3540 MachineNode->getOperand(1));
3541 else if (Op2Not)
3542 // orc(x, ~y) = x | y
3543 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3544 MVT::i1, MachineNode->getOperand(0),
3545 MachineNode->getOperand(1).
3546 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003547 else if (AllUsersSelectZero(MachineNode))
3548 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3549 MVT::i1, MachineNode->getOperand(1),
3550 MachineNode->getOperand(0)),
3551 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003552 break;
3553 case PPC::SELECT_I4:
3554 case PPC::SELECT_I8:
3555 case PPC::SELECT_F4:
3556 case PPC::SELECT_F8:
3557 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003558 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003559 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003560 if (Op1Set)
3561 ResNode = MachineNode->getOperand(1).getNode();
3562 else if (Op1Unset)
3563 ResNode = MachineNode->getOperand(2).getNode();
3564 else if (Op1Not)
3565 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3566 SDLoc(MachineNode),
3567 MachineNode->getValueType(0),
3568 MachineNode->getOperand(0).
3569 getOperand(0),
3570 MachineNode->getOperand(2),
3571 MachineNode->getOperand(1));
3572 break;
3573 case PPC::BC:
3574 case PPC::BCn:
3575 if (Op1Not)
3576 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3577 PPC::BC,
3578 SDLoc(MachineNode),
3579 MVT::Other,
3580 MachineNode->getOperand(0).
3581 getOperand(0),
3582 MachineNode->getOperand(1),
3583 MachineNode->getOperand(2));
3584 // FIXME: Handle Op1Set, Op1Unset here too.
3585 break;
3586 }
3587
Hal Finkelb9989152014-02-28 06:11:16 +00003588 // If we're inverting this node because it is used only by selects that
3589 // we'd like to swap, then swap the selects before the node replacement.
3590 if (SelectSwap)
3591 SwapAllSelectUsers(MachineNode);
3592
Hal Finkel940ab932014-02-28 00:27:01 +00003593 if (ResNode != MachineNode) {
3594 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3595 DEBUG(MachineNode->dump(CurDAG));
3596 DEBUG(dbgs() << "\nNew: ");
3597 DEBUG(ResNode->dump(CurDAG));
3598 DEBUG(dbgs() << "\n");
3599
3600 ReplaceUses(MachineNode, ResNode);
3601 IsModified = true;
3602 }
3603 }
3604 if (IsModified)
3605 CurDAG->RemoveDeadNodes();
3606 } while (IsModified);
3607}
3608
Hal Finkel4c6658f2014-12-12 23:59:36 +00003609// Gather the set of 32-bit operations that are known to have their
3610// higher-order 32 bits zero, where ToPromote contains all such operations.
3611static bool PeepholePPC64ZExtGather(SDValue Op32,
3612 SmallPtrSetImpl<SDNode *> &ToPromote) {
3613 if (!Op32.isMachineOpcode())
3614 return false;
3615
3616 // First, check for the "frontier" instructions (those that will clear the
3617 // higher-order 32 bits.
3618
3619 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3620 // around. If it does not, then these instructions will clear the
3621 // higher-order bits.
3622 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3623 Op32.getMachineOpcode() == PPC::RLWNM) &&
3624 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3625 ToPromote.insert(Op32.getNode());
3626 return true;
3627 }
3628
3629 // SLW and SRW always clear the higher-order bits.
3630 if (Op32.getMachineOpcode() == PPC::SLW ||
3631 Op32.getMachineOpcode() == PPC::SRW) {
3632 ToPromote.insert(Op32.getNode());
3633 return true;
3634 }
3635
3636 // For LI and LIS, we need the immediate to be positive (so that it is not
3637 // sign extended).
3638 if (Op32.getMachineOpcode() == PPC::LI ||
3639 Op32.getMachineOpcode() == PPC::LIS) {
3640 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3641 return false;
3642
3643 ToPromote.insert(Op32.getNode());
3644 return true;
3645 }
3646
3647 // Next, check for those instructions we can look through.
3648
3649 // Assuming the mask does not wrap around, then the higher-order bits are
3650 // taken directly from the first operand.
3651 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
3652 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
3653 SmallPtrSet<SDNode *, 16> ToPromote1;
3654 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3655 return false;
3656
3657 ToPromote.insert(Op32.getNode());
3658 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3659 return true;
3660 }
3661
3662 // For OR, the higher-order bits are zero if that is true for both operands.
3663 // For SELECT_I4, the same is true (but the relevant operand numbers are
3664 // shifted by 1).
3665 if (Op32.getMachineOpcode() == PPC::OR ||
3666 Op32.getMachineOpcode() == PPC::SELECT_I4) {
3667 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
3668 SmallPtrSet<SDNode *, 16> ToPromote1;
3669 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
3670 return false;
3671 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
3672 return false;
3673
3674 ToPromote.insert(Op32.getNode());
3675 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3676 return true;
3677 }
3678
3679 // For ORI and ORIS, we need the higher-order bits of the first operand to be
3680 // zero, and also for the constant to be positive (so that it is not sign
3681 // extended).
3682 if (Op32.getMachineOpcode() == PPC::ORI ||
3683 Op32.getMachineOpcode() == PPC::ORIS) {
3684 SmallPtrSet<SDNode *, 16> ToPromote1;
3685 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3686 return false;
3687 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
3688 return false;
3689
3690 ToPromote.insert(Op32.getNode());
3691 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3692 return true;
3693 }
3694
3695 // The higher-order bits of AND are zero if that is true for at least one of
3696 // the operands.
3697 if (Op32.getMachineOpcode() == PPC::AND) {
3698 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
3699 bool Op0OK =
3700 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3701 bool Op1OK =
3702 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
3703 if (!Op0OK && !Op1OK)
3704 return false;
3705
3706 ToPromote.insert(Op32.getNode());
3707
3708 if (Op0OK)
3709 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3710
3711 if (Op1OK)
3712 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
3713
3714 return true;
3715 }
3716
3717 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
3718 // of the first operand, or if the second operand is positive (so that it is
3719 // not sign extended).
3720 if (Op32.getMachineOpcode() == PPC::ANDIo ||
3721 Op32.getMachineOpcode() == PPC::ANDISo) {
3722 SmallPtrSet<SDNode *, 16> ToPromote1;
3723 bool Op0OK =
3724 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3725 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
3726 if (!Op0OK && !Op1OK)
3727 return false;
3728
3729 ToPromote.insert(Op32.getNode());
3730
3731 if (Op0OK)
3732 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3733
3734 return true;
3735 }
3736
3737 return false;
3738}
3739
3740void PPCDAGToDAGISel::PeepholePPC64ZExt() {
3741 if (!PPCSubTarget->isPPC64())
3742 return;
3743
3744 // When we zero-extend from i32 to i64, we use a pattern like this:
3745 // def : Pat<(i64 (zext i32:$in)),
3746 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
3747 // 0, 32)>;
3748 // There are several 32-bit shift/rotate instructions, however, that will
3749 // clear the higher-order bits of their output, rendering the RLDICL
3750 // unnecessary. When that happens, we remove it here, and redefine the
3751 // relevant 32-bit operation to be a 64-bit operation.
3752
3753 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3754 ++Position;
3755
3756 bool MadeChange = false;
3757 while (Position != CurDAG->allnodes_begin()) {
3758 SDNode *N = --Position;
3759 // Skip dead nodes and any non-machine opcodes.
3760 if (N->use_empty() || !N->isMachineOpcode())
3761 continue;
3762
3763 if (N->getMachineOpcode() != PPC::RLDICL)
3764 continue;
3765
3766 if (N->getConstantOperandVal(1) != 0 ||
3767 N->getConstantOperandVal(2) != 32)
3768 continue;
3769
3770 SDValue ISR = N->getOperand(0);
3771 if (!ISR.isMachineOpcode() ||
3772 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
3773 continue;
3774
3775 if (!ISR.hasOneUse())
3776 continue;
3777
3778 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
3779 continue;
3780
3781 SDValue IDef = ISR.getOperand(0);
3782 if (!IDef.isMachineOpcode() ||
3783 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
3784 continue;
3785
3786 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
3787 // can get rid of it.
3788
3789 SDValue Op32 = ISR->getOperand(1);
3790 if (!Op32.isMachineOpcode())
3791 continue;
3792
3793 // There are some 32-bit instructions that always clear the high-order 32
3794 // bits, there are also some instructions (like AND) that we can look
3795 // through.
3796 SmallPtrSet<SDNode *, 16> ToPromote;
3797 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
3798 continue;
3799
3800 // If the ToPromote set contains nodes that have uses outside of the set
3801 // (except for the original INSERT_SUBREG), then abort the transformation.
3802 bool OutsideUse = false;
3803 for (SDNode *PN : ToPromote) {
3804 for (SDNode *UN : PN->uses()) {
3805 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
3806 OutsideUse = true;
3807 break;
3808 }
3809 }
3810
3811 if (OutsideUse)
3812 break;
3813 }
3814 if (OutsideUse)
3815 continue;
3816
3817 MadeChange = true;
3818
3819 // We now know that this zero extension can be removed by promoting to
3820 // nodes in ToPromote to 64-bit operations, where for operations in the
3821 // frontier of the set, we need to insert INSERT_SUBREGs for their
3822 // operands.
3823 for (SDNode *PN : ToPromote) {
3824 unsigned NewOpcode;
3825 switch (PN->getMachineOpcode()) {
3826 default:
3827 llvm_unreachable("Don't know the 64-bit variant of this instruction");
3828 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
3829 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
3830 case PPC::SLW: NewOpcode = PPC::SLW8; break;
3831 case PPC::SRW: NewOpcode = PPC::SRW8; break;
3832 case PPC::LI: NewOpcode = PPC::LI8; break;
3833 case PPC::LIS: NewOpcode = PPC::LIS8; break;
3834 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
3835 case PPC::OR: NewOpcode = PPC::OR8; break;
3836 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
3837 case PPC::ORI: NewOpcode = PPC::ORI8; break;
3838 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
3839 case PPC::AND: NewOpcode = PPC::AND8; break;
3840 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
3841 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
3842 }
3843
3844 // Note: During the replacement process, the nodes will be in an
3845 // inconsistent state (some instructions will have operands with values
3846 // of the wrong type). Once done, however, everything should be right
3847 // again.
3848
3849 SmallVector<SDValue, 4> Ops;
3850 for (const SDValue &V : PN->ops()) {
3851 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
3852 !isa<ConstantSDNode>(V)) {
3853 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
3854 SDNode *ReplOp =
3855 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
3856 ISR.getNode()->getVTList(), ReplOpOps);
3857 Ops.push_back(SDValue(ReplOp, 0));
3858 } else {
3859 Ops.push_back(V);
3860 }
3861 }
3862
3863 // Because all to-be-promoted nodes only have users that are other
3864 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
3865 // the i32 result value type with i64.
3866
3867 SmallVector<EVT, 2> NewVTs;
3868 SDVTList VTs = PN->getVTList();
3869 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
3870 if (VTs.VTs[i] == MVT::i32)
3871 NewVTs.push_back(MVT::i64);
3872 else
3873 NewVTs.push_back(VTs.VTs[i]);
3874
3875 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
3876 DEBUG(PN->dump(CurDAG));
3877
3878 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
3879
3880 DEBUG(dbgs() << "\nNew: ");
3881 DEBUG(PN->dump(CurDAG));
3882 DEBUG(dbgs() << "\n");
3883 }
3884
3885 // Now we replace the original zero extend and its associated INSERT_SUBREG
3886 // with the value feeding the INSERT_SUBREG (which has now been promoted to
3887 // return an i64).
3888
3889 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
3890 DEBUG(N->dump(CurDAG));
3891 DEBUG(dbgs() << "\nNew: ");
3892 DEBUG(Op32.getNode()->dump(CurDAG));
3893 DEBUG(dbgs() << "\n");
3894
3895 ReplaceUses(N, Op32.getNode());
3896 }
3897
3898 if (MadeChange)
3899 CurDAG->RemoveDeadNodes();
3900}
3901
Hal Finkel940ab932014-02-28 00:27:01 +00003902void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003903 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003904 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003905 return;
3906
3907 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3908 ++Position;
3909
3910 while (Position != CurDAG->allnodes_begin()) {
3911 SDNode *N = --Position;
3912 // Skip dead nodes and any non-machine opcodes.
3913 if (N->use_empty() || !N->isMachineOpcode())
3914 continue;
3915
3916 unsigned FirstOp;
3917 unsigned StorageOpcode = N->getMachineOpcode();
3918
3919 switch (StorageOpcode) {
3920 default: continue;
3921
3922 case PPC::LBZ:
3923 case PPC::LBZ8:
3924 case PPC::LD:
3925 case PPC::LFD:
3926 case PPC::LFS:
3927 case PPC::LHA:
3928 case PPC::LHA8:
3929 case PPC::LHZ:
3930 case PPC::LHZ8:
3931 case PPC::LWA:
3932 case PPC::LWZ:
3933 case PPC::LWZ8:
3934 FirstOp = 0;
3935 break;
3936
3937 case PPC::STB:
3938 case PPC::STB8:
3939 case PPC::STD:
3940 case PPC::STFD:
3941 case PPC::STFS:
3942 case PPC::STH:
3943 case PPC::STH8:
3944 case PPC::STW:
3945 case PPC::STW8:
3946 FirstOp = 1;
3947 break;
3948 }
3949
3950 // If this is a load or store with a zero offset, we may be able to
3951 // fold an add-immediate into the memory operation.
3952 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
3953 N->getConstantOperandVal(FirstOp) != 0)
3954 continue;
3955
3956 SDValue Base = N->getOperand(FirstOp + 1);
3957 if (!Base.isMachineOpcode())
3958 continue;
3959
3960 unsigned Flags = 0;
3961 bool ReplaceFlags = true;
3962
3963 // When the feeding operation is an add-immediate of some sort,
3964 // determine whether we need to add relocation information to the
3965 // target flags on the immediate operand when we fold it into the
3966 // load instruction.
3967 //
3968 // For something like ADDItocL, the relocation information is
3969 // inferred from the opcode; when we process it in the AsmPrinter,
3970 // we add the necessary relocation there. A load, though, can receive
3971 // relocation from various flavors of ADDIxxx, so we need to carry
3972 // the relocation information in the target flags.
3973 switch (Base.getMachineOpcode()) {
3974 default: continue;
3975
3976 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00003977 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003978 // In some cases (such as TLS) the relocation information
3979 // is already in place on the operand, so copying the operand
3980 // is sufficient.
3981 ReplaceFlags = false;
3982 // For these cases, the immediate may not be divisible by 4, in
3983 // which case the fold is illegal for DS-form instructions. (The
3984 // other cases provide aligned addresses and are always safe.)
3985 if ((StorageOpcode == PPC::LWA ||
3986 StorageOpcode == PPC::LD ||
3987 StorageOpcode == PPC::STD) &&
3988 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
3989 Base.getConstantOperandVal(1) % 4 != 0))
3990 continue;
3991 break;
3992 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00003993 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003994 break;
3995 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00003996 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003997 break;
3998 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00003999 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004000 break;
4001 }
4002
4003 // We found an opportunity. Reverse the operands from the add
4004 // immediate and substitute them into the load or store. If
4005 // needed, update the target flags for the immediate operand to
4006 // reflect the necessary relocation information.
4007 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4008 DEBUG(Base->dump(CurDAG));
4009 DEBUG(dbgs() << "\nN: ");
4010 DEBUG(N->dump(CurDAG));
4011 DEBUG(dbgs() << "\n");
4012
4013 SDValue ImmOpnd = Base.getOperand(1);
4014
4015 // If the relocation information isn't already present on the
4016 // immediate operand, add it now.
4017 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004018 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004019 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004020 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004021 // We can't perform this optimization for data whose alignment
4022 // is insufficient for the instruction encoding.
4023 if (GV->getAlignment() < 4 &&
4024 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
4025 StorageOpcode == PPC::LWA)) {
4026 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4027 continue;
4028 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004029 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004030 } else if (ConstantPoolSDNode *CP =
4031 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004032 const Constant *C = CP->getConstVal();
4033 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4034 CP->getAlignment(),
4035 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004036 }
4037 }
4038
4039 if (FirstOp == 1) // Store
4040 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4041 Base.getOperand(0), N->getOperand(3));
4042 else // Load
4043 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4044 N->getOperand(2));
4045
4046 // The add-immediate may now be dead, in which case remove it.
4047 if (Base.getNode()->use_empty())
4048 CurDAG->RemoveDeadNode(Base.getNode());
4049 }
4050}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004051
Chris Lattnerb055c872006-06-10 01:15:02 +00004052
Andrew Trickc416ba62010-12-24 04:28:06 +00004053/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004054/// PowerPC-specific DAG, ready for instruction scheduling.
4055///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004056FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004057 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004058}
4059
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004060static void initializePassOnce(PassRegistry &Registry) {
4061 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004062 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4063 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004064 Registry.registerPass(*PI, true);
4065}
4066
4067void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4068 CALL_ONCE_INITIALIZATION(initializePassOnce);
4069}
4070