blob: 75ab3430b49dde5b3984edc76ed65901db54a97c [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);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000220 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000221
Hal Finkelb9989152014-02-28 06:11:16 +0000222 bool AllUsersSelectZero(SDNode *N);
223 void SwapAllSelectUsers(SDNode *N);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000224 };
225}
226
Chris Lattner1678a6c2006-03-16 18:25:23 +0000227/// InsertVRSaveCode - Once the entire function has been instruction selected,
228/// all virtual registers are created and all machine instructions are built,
229/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000230void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000231 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000232 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000233 //
Dan Gohman4a618822010-02-10 16:03:48 +0000234 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000235 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000236 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000237 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
238 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
239 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000240 HasVectorVReg = true;
241 break;
242 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000243 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000244 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000245
Chris Lattner02e2c182006-03-13 21:52:10 +0000246 // If we have a vector register, we want to emit code into the entry and exit
247 // blocks to save and restore the VRSAVE register. We do this here (instead
248 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
249 //
250 // 1. This (trivially) reduces the load on the register allocator, by not
251 // having to represent the live range of the VRSAVE register.
252 // 2. This (more significantly) allows us to create a temporary virtual
253 // register to hold the saved VRSAVE value, allowing this temporary to be
254 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000255
256 // Create two vregs - one to hold the VRSAVE register that is live-in to the
257 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000258 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
259 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000260
Eric Christopherd9134482014-08-04 21:25:23 +0000261 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000262 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000263 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000264 // Emit the following code into the entry block:
265 // InVRSAVE = MFVRSAVE
266 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
267 // MTVRSAVE UpdatedVRSAVE
268 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000269 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
270 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000271 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000272 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000273
Chris Lattner1678a6c2006-03-16 18:25:23 +0000274 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000275 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000276 if (!BB->empty() && BB->back().isReturn()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000277 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000278
Chris Lattner1678a6c2006-03-16 18:25:23 +0000279 // Skip over all terminator instructions, which are part of the return
280 // sequence.
281 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000282 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000283 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000284
Chris Lattner1678a6c2006-03-16 18:25:23 +0000285 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000286 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000287 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000288 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000289}
Chris Lattner8ae95252005-09-03 01:17:22 +0000290
Chris Lattner1678a6c2006-03-16 18:25:23 +0000291
Chris Lattner45640392005-08-19 22:38:53 +0000292/// getGlobalBaseReg - Output the instructions required to put the
293/// base address to use for accessing globals into a register.
294///
Evan Cheng61413a32006-08-26 05:34:46 +0000295SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000296 if (!GlobalBaseReg) {
Eric Christopherd9134482014-08-04 21:25:23 +0000297 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000298 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000299 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000300 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000301 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000302 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000303
Eric Christopher1b8e7632014-05-22 01:07:24 +0000304 if (PPCLowering->getPointerTy() == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000305 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000306 GlobalBaseReg = PPC::R30;
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000307 if (M->getPICLevel() == PICLevel::Small) {
308 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
309 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000310 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000311 } else {
312 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
313 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
314 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
315 BuildMI(FirstMBB, MBBI, dl,
316 TII.get(PPC::UpdateGBR)).addReg(GlobalBaseReg)
317 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
318 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
319 }
320 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000321 GlobalBaseReg =
322 RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000323 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
324 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000325 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000326 } else {
Hal Finkel6daf2aa2014-03-06 01:28:23 +0000327 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000328 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000329 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000330 }
Chris Lattner45640392005-08-19 22:38:53 +0000331 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000332 return CurDAG->getRegister(GlobalBaseReg,
Eric Christopher1b8e7632014-05-22 01:07:24 +0000333 PPCLowering->getPointerTy()).getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000334}
335
336/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
337/// or 64-bit immediate, and if the value can be accurately represented as a
338/// sign extension from a 16-bit value. If so, this returns true and the
339/// immediate.
340static bool isIntS16Immediate(SDNode *N, short &Imm) {
341 if (N->getOpcode() != ISD::Constant)
342 return false;
343
Dan Gohmaneffb8942008-09-12 16:56:44 +0000344 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000345 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000346 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000347 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000348 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000349}
350
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000351static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000352 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000353}
354
355
Chris Lattner97b3da12006-06-27 00:04:13 +0000356/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
357/// operand. If so Imm will receive the 32-bit value.
358static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000359 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000360 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000361 return true;
362 }
363 return false;
364}
365
Chris Lattner97b3da12006-06-27 00:04:13 +0000366/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
367/// operand. If so Imm will receive the 64-bit value.
368static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000369 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000370 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000371 return true;
372 }
373 return false;
374}
375
376// isInt32Immediate - This method tests to see if a constant operand.
377// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000378static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000379 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000380}
381
382
383// isOpcWithIntImmediate - This method tests to see if the node is a specific
384// opcode and that it has a immediate integer right operand.
385// If so Imm will receive the 32 bit value.
386static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000387 return N->getOpcode() == Opc
388 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000389}
390
Hal Finkelb5e9b042014-12-11 22:51:06 +0000391SDNode *PPCDAGToDAGISel::getFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
392 SDLoc dl(SN);
393 int FI = cast<FrameIndexSDNode>(N)->getIndex();
394 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
395 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
396 if (SN->hasOneUse())
397 return CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
398 getSmallIPtrImm(Offset));
399 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
400 getSmallIPtrImm(Offset));
401}
402
Nate Begemand31efd12006-09-22 05:01:56 +0000403bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
Hal Finkelff3ea802013-07-11 16:31:51 +0000404 if (!Val)
405 return false;
406
Nate Begemanb3821a32005-08-18 07:30:46 +0000407 if (isShiftedMask_32(Val)) {
408 // look for the first non-zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000409 MB = countLeadingZeros(Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000410 // look for the first zero bit after the run of ones
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000411 ME = countLeadingZeros((Val - 1) ^ Val);
Nate Begemanb3821a32005-08-18 07:30:46 +0000412 return true;
Chris Lattner666512c2005-08-25 04:47:18 +0000413 } else {
414 Val = ~Val; // invert mask
415 if (isShiftedMask_32(Val)) {
416 // effectively look for the first zero bit
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000417 ME = countLeadingZeros(Val) - 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000418 // effectively look for the first one bit after the run of zeros
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000419 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
Chris Lattner666512c2005-08-25 04:47:18 +0000420 return true;
421 }
Nate Begemanb3821a32005-08-18 07:30:46 +0000422 }
423 // no run present
424 return false;
425}
426
Andrew Trickc416ba62010-12-24 04:28:06 +0000427bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
428 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000429 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000430 // Don't even go down this path for i64, since different logic will be
431 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000432 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000433 return false;
434
Nate Begemanb3821a32005-08-18 07:30:46 +0000435 unsigned Shift = 32;
436 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
437 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000438 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000439 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000440 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000441
Nate Begemanb3821a32005-08-18 07:30:46 +0000442 if (Opcode == ISD::SHL) {
443 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000444 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000445 // determine which bits are made indeterminant by shift
446 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000447 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000448 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000449 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000450 // determine which bits are made indeterminant by shift
451 Indeterminant = ~(0xFFFFFFFFu >> Shift);
452 // adjust for the left rotate
453 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000454 } else if (Opcode == ISD::ROTL) {
455 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000456 } else {
457 return false;
458 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000459
Nate Begemanb3821a32005-08-18 07:30:46 +0000460 // if the mask doesn't intersect any Indeterminant bits
461 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000462 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000463 // make sure the mask is still a mask (wrap arounds may not be)
464 return isRunOfOnes(Mask, MB, ME);
465 }
466 return false;
467}
468
Nate Begeman93c4bc62005-08-19 00:38:14 +0000469/// SelectBitfieldInsert - turn an or of two masked values into
470/// the rotate left word immediate then mask insert (rlwimi) instruction.
Nate Begeman0b71e002005-10-18 00:28:58 +0000471SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000472 SDValue Op0 = N->getOperand(0);
473 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000474 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000475
Dan Gohmanf19609a2008-02-27 01:23:58 +0000476 APInt LKZ, LKO, RKZ, RKO;
Jay Foada0653a32014-05-14 21:14:37 +0000477 CurDAG->computeKnownBits(Op0, LKZ, LKO);
478 CurDAG->computeKnownBits(Op1, RKZ, RKO);
Andrew Trickc416ba62010-12-24 04:28:06 +0000479
Dan Gohmanf19609a2008-02-27 01:23:58 +0000480 unsigned TargetMask = LKZ.getZExtValue();
481 unsigned InsertMask = RKZ.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000482
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000483 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
484 unsigned Op0Opc = Op0.getOpcode();
485 unsigned Op1Opc = Op1.getOpcode();
486 unsigned Value, SH = 0;
487 TargetMask = ~TargetMask;
488 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000489
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000490 // If the LHS has a foldable shift and the RHS does not, then swap it to the
491 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000492 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
493 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
494 Op0.getOperand(0).getOpcode() == ISD::SRL) {
495 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
496 Op1.getOperand(0).getOpcode() != ISD::SRL) {
497 std::swap(Op0, Op1);
498 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000499 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000500 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000501 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000502 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
503 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
504 Op1.getOperand(0).getOpcode() != ISD::SRL) {
505 std::swap(Op0, Op1);
506 std::swap(Op0Opc, Op1Opc);
507 std::swap(TargetMask, InsertMask);
508 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000509 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000510
Nate Begeman1333cea2006-05-07 00:23:38 +0000511 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000512 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000513 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000514
515 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000516 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000517 Op1 = Op1.getOperand(0);
518 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
519 }
520 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000521 // The AND mask might not be a constant, and we need to make sure that
522 // if we're going to fold the masking with the insert, all bits not
523 // know to be zero in the mask are known to be one.
524 APInt MKZ, MKO;
Jay Foada0653a32014-05-14 21:14:37 +0000525 CurDAG->computeKnownBits(Op1.getOperand(1), MKZ, MKO);
Hal Finkeld9963c72014-04-13 17:10:58 +0000526 bool CanFoldMask = InsertMask == MKO.getZExtValue();
527
Nate Begeman1333cea2006-05-07 00:23:38 +0000528 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000529 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000530 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000531 // Note that Value must be in range here (less than 32) because
532 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000533 Op1 = Op1.getOperand(0).getOperand(0);
534 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000535 }
536 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000537
Chris Lattnera2963392006-05-12 16:29:37 +0000538 SH &= 31;
Dale Johannesen8495a502009-11-20 22:16:40 +0000539 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
Evan Chengc3acfc02006-08-27 08:14:06 +0000540 getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +0000541 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000542 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000543 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000544 return nullptr;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000545}
546
Hal Finkelc58ce412015-01-01 02:53:29 +0000547// Predict the number of instructions that would be generated by calling
548// SelectInt64(N).
Hal Finkelca6375f2015-01-04 12:35:03 +0000549static unsigned SelectInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000550 // Assume no remaining bits.
551 unsigned Remainder = 0;
552 // Assume no shift required.
553 unsigned Shift = 0;
554
555 // If it can't be represented as a 32 bit value.
556 if (!isInt<32>(Imm)) {
557 Shift = countTrailingZeros<uint64_t>(Imm);
558 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
559
560 // If the shifted value fits 32 bits.
561 if (isInt<32>(ImmSh)) {
562 // Go with the shifted value.
563 Imm = ImmSh;
564 } else {
565 // Still stuck with a 64 bit value.
566 Remainder = Imm;
567 Shift = 32;
568 Imm >>= 32;
569 }
570 }
571
572 // Intermediate operand.
573 unsigned Result = 0;
574
575 // Handle first 32 bits.
576 unsigned Lo = Imm & 0xFFFF;
577 unsigned Hi = (Imm >> 16) & 0xFFFF;
578
579 // Simple value.
580 if (isInt<16>(Imm)) {
581 // Just the Lo bits.
582 ++Result;
583 } else if (Lo) {
584 // Handle the Hi bits and Lo bits.
585 Result += 2;
586 } else {
587 // Just the Hi bits.
588 ++Result;
589 }
590
591 // If no shift, we're done.
592 if (!Shift) return Result;
593
594 // Shift for next step if the upper 32-bits were not zero.
595 if (Imm)
596 ++Result;
597
598 // Add in the last bits as required.
599 if ((Hi = (Remainder >> 16) & 0xFFFF))
600 ++Result;
601 if ((Lo = Remainder & 0xFFFF))
602 ++Result;
603
604 return Result;
605}
606
Hal Finkel241ba792015-01-04 15:43:55 +0000607static uint64_t Rot64(uint64_t Imm, unsigned R) {
608 return (Imm << R) | (Imm >> (64 - R));
609}
610
Hal Finkelca6375f2015-01-04 12:35:03 +0000611static unsigned SelectInt64Count(int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000612 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000613 if (Count == 1)
614 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000615
Hal Finkel241ba792015-01-04 15:43:55 +0000616 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000617 uint64_t RImm = Rot64(Imm, r);
618 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
619 Count = std::min(Count, RCount);
620
621 // See comments in SelectInt64 for an explanation of the logic below.
622 unsigned LS = findLastSet(RImm);
623 if (LS != r-1)
624 continue;
625
626 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
627 uint64_t RImmWithOnes = RImm | OnesMask;
628
629 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000630 Count = std::min(Count, RCount);
631 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000632
Hal Finkel241ba792015-01-04 15:43:55 +0000633 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000634}
635
Hal Finkelc58ce412015-01-01 02:53:29 +0000636// Select a 64-bit constant. For cost-modeling purposes, SelectInt64Count
637// (above) needs to be kept in sync with this function.
Hal Finkelca6375f2015-01-04 12:35:03 +0000638static SDNode *SelectInt64Direct(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000639 // Assume no remaining bits.
640 unsigned Remainder = 0;
641 // Assume no shift required.
642 unsigned Shift = 0;
643
644 // If it can't be represented as a 32 bit value.
645 if (!isInt<32>(Imm)) {
646 Shift = countTrailingZeros<uint64_t>(Imm);
647 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
648
649 // If the shifted value fits 32 bits.
650 if (isInt<32>(ImmSh)) {
651 // Go with the shifted value.
652 Imm = ImmSh;
653 } else {
654 // Still stuck with a 64 bit value.
655 Remainder = Imm;
656 Shift = 32;
657 Imm >>= 32;
658 }
659 }
660
661 // Intermediate operand.
662 SDNode *Result;
663
664 // Handle first 32 bits.
665 unsigned Lo = Imm & 0xFFFF;
666 unsigned Hi = (Imm >> 16) & 0xFFFF;
667
668 auto getI32Imm = [CurDAG](unsigned Imm) {
669 return CurDAG->getTargetConstant(Imm, MVT::i32);
670 };
671
672 // Simple value.
673 if (isInt<16>(Imm)) {
674 // Just the Lo bits.
675 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
676 } else if (Lo) {
677 // Handle the Hi bits.
678 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
679 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
680 // And Lo bits.
681 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
682 SDValue(Result, 0), getI32Imm(Lo));
683 } else {
684 // Just the Hi bits.
685 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
686 }
687
688 // If no shift, we're done.
689 if (!Shift) return Result;
690
691 // Shift for next step if the upper 32-bits were not zero.
692 if (Imm) {
693 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
694 SDValue(Result, 0),
695 getI32Imm(Shift),
696 getI32Imm(63 - Shift));
697 }
698
699 // Add in the last bits as required.
700 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
701 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
702 SDValue(Result, 0), getI32Imm(Hi));
703 }
704 if ((Lo = Remainder & 0xFFFF)) {
705 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
706 SDValue(Result, 0), getI32Imm(Lo));
707 }
708
709 return Result;
710}
711
Hal Finkelca6375f2015-01-04 12:35:03 +0000712static SDNode *SelectInt64(SelectionDAG *CurDAG, SDLoc dl, int64_t Imm) {
Hal Finkel241ba792015-01-04 15:43:55 +0000713 unsigned Count = SelectInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000714 if (Count == 1)
715 return SelectInt64Direct(CurDAG, dl, Imm);
716
Hal Finkel241ba792015-01-04 15:43:55 +0000717 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000718
Hal Finkel2f618792015-01-05 03:41:38 +0000719 int64_t MatImm;
720 unsigned MaskEnd;
721
Hal Finkel241ba792015-01-04 15:43:55 +0000722 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000723 uint64_t RImm = Rot64(Imm, r);
724 unsigned RCount = SelectInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000725 if (RCount < Count) {
726 Count = RCount;
727 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000728 MatImm = RImm;
729 MaskEnd = 63;
730 }
731
732 // If the immediate to generate has many trailing zeros, it might be
733 // worthwhile to generate a rotated value with too many leading ones
734 // (because that's free with li/lis's sign-extension semantics), and then
735 // mask them off after rotation.
736
737 unsigned LS = findLastSet(RImm);
738 // We're adding (63-LS) higher-order ones, and we expect to mask them off
739 // after performing the inverse rotation by (64-r). So we need that:
740 // 63-LS == 64-r => LS == r-1
741 if (LS != r-1)
742 continue;
743
744 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
745 uint64_t RImmWithOnes = RImm | OnesMask;
746
747 RCount = SelectInt64CountDirect(RImmWithOnes) + 1;
748 if (RCount < Count) {
749 Count = RCount;
750 RMin = r;
751 MatImm = RImmWithOnes;
752 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000753 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000754 }
755
Hal Finkel241ba792015-01-04 15:43:55 +0000756 if (!RMin)
757 return SelectInt64Direct(CurDAG, dl, Imm);
758
759 auto getI32Imm = [CurDAG](unsigned Imm) {
760 return CurDAG->getTargetConstant(Imm, MVT::i32);
761 };
762
Hal Finkel2f618792015-01-05 03:41:38 +0000763 SDValue Val = SDValue(SelectInt64Direct(CurDAG, dl, MatImm), 0);
764 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
765 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000766}
767
Hal Finkelc58ce412015-01-01 02:53:29 +0000768// Select a 64-bit constant.
769static SDNode *SelectInt64(SelectionDAG *CurDAG, SDNode *N) {
770 SDLoc dl(N);
771
772 // Get 64 bit value.
773 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
774 return SelectInt64(CurDAG, dl, Imm);
775}
776
Hal Finkel8adf2252014-12-16 05:51:41 +0000777namespace {
778class BitPermutationSelector {
779 struct ValueBit {
780 SDValue V;
781
782 // The bit number in the value, using a convention where bit 0 is the
783 // lowest-order bit.
784 unsigned Idx;
785
786 enum Kind {
787 ConstZero,
788 Variable
789 } K;
790
791 ValueBit(SDValue V, unsigned I, Kind K = Variable)
792 : V(V), Idx(I), K(K) {}
793 ValueBit(Kind K = Variable)
794 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
795
796 bool isZero() const {
797 return K == ConstZero;
798 }
799
800 bool hasValue() const {
801 return K == Variable;
802 }
803
804 SDValue getValue() const {
805 assert(hasValue() && "Cannot get the value of a constant bit");
806 return V;
807 }
808
809 unsigned getValueBitIndex() const {
810 assert(hasValue() && "Cannot get the value bit index of a constant bit");
811 return Idx;
812 }
813 };
814
815 // A bit group has the same underlying value and the same rotate factor.
816 struct BitGroup {
817 SDValue V;
818 unsigned RLAmt;
819 unsigned StartIdx, EndIdx;
820
Hal Finkelc58ce412015-01-01 02:53:29 +0000821 // This rotation amount assumes that the lower 32 bits of the quantity are
822 // replicated in the high 32 bits by the rotation operator (which is done
823 // by rlwinm and friends in 64-bit mode).
824 bool Repl32;
825 // Did converting to Repl32 == true change the rotation factor? If it did,
826 // it decreased it by 32.
827 bool Repl32CR;
828 // Was this group coalesced after setting Repl32 to true?
829 bool Repl32Coalesced;
830
Hal Finkel8adf2252014-12-16 05:51:41 +0000831 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000832 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
833 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000834 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
835 " [" << S << ", " << E << "]\n");
836 }
837 };
838
839 // Information on each (Value, RLAmt) pair (like the number of groups
840 // associated with each) used to choose the lowering method.
841 struct ValueRotInfo {
842 SDValue V;
843 unsigned RLAmt;
844 unsigned NumGroups;
845 unsigned FirstGroupStartIdx;
Hal Finkelc58ce412015-01-01 02:53:29 +0000846 bool Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +0000847
848 ValueRotInfo()
Hal Finkelc58ce412015-01-01 02:53:29 +0000849 : RLAmt(UINT32_MAX), NumGroups(0), FirstGroupStartIdx(UINT32_MAX),
850 Repl32(false) {}
Hal Finkel8adf2252014-12-16 05:51:41 +0000851
852 // For sorting (in reverse order) by NumGroups, and then by
853 // FirstGroupStartIdx.
854 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000855 // We need to sort so that the non-Repl32 come first because, when we're
856 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
857 // masking operation.
858 if (Repl32 < Other.Repl32)
859 return true;
860 else if (Repl32 > Other.Repl32)
861 return false;
862 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000863 return true;
864 else if (NumGroups < Other.NumGroups)
865 return false;
866 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
867 return true;
868 return false;
869 }
870 };
871
872 // Return true if something interesting was deduced, return false if we're
873 // providing only a generic representation of V (or something else likewise
874 // uninteresting for instruction selection).
875 bool getValueBits(SDValue V, SmallVector<ValueBit, 64> &Bits) {
876 switch (V.getOpcode()) {
877 default: break;
878 case ISD::ROTL:
879 if (isa<ConstantSDNode>(V.getOperand(1))) {
880 unsigned RotAmt = V.getConstantOperandVal(1);
881
882 SmallVector<ValueBit, 64> LHSBits(Bits.size());
883 getValueBits(V.getOperand(0), LHSBits);
884
885 for (unsigned i = 0; i < Bits.size(); ++i)
886 Bits[i] = LHSBits[i < RotAmt ? i + (Bits.size() - RotAmt) : i - RotAmt];
887
888 return true;
889 }
890 break;
891 case ISD::SHL:
892 if (isa<ConstantSDNode>(V.getOperand(1))) {
893 unsigned ShiftAmt = V.getConstantOperandVal(1);
894
895 SmallVector<ValueBit, 64> LHSBits(Bits.size());
896 getValueBits(V.getOperand(0), LHSBits);
897
898 for (unsigned i = ShiftAmt; i < Bits.size(); ++i)
899 Bits[i] = LHSBits[i - ShiftAmt];
900
901 for (unsigned i = 0; i < ShiftAmt; ++i)
902 Bits[i] = ValueBit(ValueBit::ConstZero);
903
904 return true;
905 }
906 break;
907 case ISD::SRL:
908 if (isa<ConstantSDNode>(V.getOperand(1))) {
909 unsigned ShiftAmt = V.getConstantOperandVal(1);
910
911 SmallVector<ValueBit, 64> LHSBits(Bits.size());
912 getValueBits(V.getOperand(0), LHSBits);
913
914 for (unsigned i = 0; i < Bits.size() - ShiftAmt; ++i)
915 Bits[i] = LHSBits[i + ShiftAmt];
916
917 for (unsigned i = Bits.size() - ShiftAmt; i < Bits.size(); ++i)
918 Bits[i] = ValueBit(ValueBit::ConstZero);
919
920 return true;
921 }
922 break;
923 case ISD::AND:
924 if (isa<ConstantSDNode>(V.getOperand(1))) {
925 uint64_t Mask = V.getConstantOperandVal(1);
926
927 SmallVector<ValueBit, 64> LHSBits(Bits.size());
928 bool LHSTrivial = getValueBits(V.getOperand(0), LHSBits);
929
930 for (unsigned i = 0; i < Bits.size(); ++i)
931 if (((Mask >> i) & 1) == 1)
932 Bits[i] = LHSBits[i];
933 else
934 Bits[i] = ValueBit(ValueBit::ConstZero);
935
936 // Mark this as interesting, only if the LHS was also interesting. This
937 // prevents the overall procedure from matching a single immediate 'and'
938 // (which is non-optimal because such an and might be folded with other
939 // things if we don't select it here).
940 return LHSTrivial;
941 }
942 break;
943 case ISD::OR: {
944 SmallVector<ValueBit, 64> LHSBits(Bits.size()), RHSBits(Bits.size());
945 getValueBits(V.getOperand(0), LHSBits);
946 getValueBits(V.getOperand(1), RHSBits);
947
948 bool AllDisjoint = true;
949 for (unsigned i = 0; i < Bits.size(); ++i)
950 if (LHSBits[i].isZero())
951 Bits[i] = RHSBits[i];
952 else if (RHSBits[i].isZero())
953 Bits[i] = LHSBits[i];
954 else {
955 AllDisjoint = false;
956 break;
957 }
958
959 if (!AllDisjoint)
960 break;
961
962 return true;
963 }
964 }
965
966 for (unsigned i = 0; i < Bits.size(); ++i)
967 Bits[i] = ValueBit(V, i);
968
969 return false;
970 }
971
972 // For each value (except the constant ones), compute the left-rotate amount
973 // to get it from its original to final position.
974 void computeRotationAmounts() {
975 HasZeros = false;
976 RLAmt.resize(Bits.size());
977 for (unsigned i = 0; i < Bits.size(); ++i)
978 if (Bits[i].hasValue()) {
979 unsigned VBI = Bits[i].getValueBitIndex();
980 if (i >= VBI)
981 RLAmt[i] = i - VBI;
982 else
983 RLAmt[i] = Bits.size() - (VBI - i);
984 } else if (Bits[i].isZero()) {
985 HasZeros = true;
986 RLAmt[i] = UINT32_MAX;
987 } else {
988 llvm_unreachable("Unknown value bit type");
989 }
990 }
991
992 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +0000993 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
994 // they break up groups.
995 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000996 BitGroups.clear();
997
998 unsigned LastRLAmt = RLAmt[0];
999 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1000 unsigned LastGroupStartIdx = 0;
1001 for (unsigned i = 1; i < Bits.size(); ++i) {
1002 unsigned ThisRLAmt = RLAmt[i];
1003 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001004 if (LateMask && !ThisValue) {
1005 ThisValue = LastValue;
1006 ThisRLAmt = LastRLAmt;
1007 // If we're doing late masking, then the first bit group always starts
1008 // at zero (even if the first bits were zero).
1009 if (BitGroups.empty())
1010 LastGroupStartIdx = 0;
1011 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001012
1013 // If this bit has the same underlying value and the same rotate factor as
1014 // the last one, then they're part of the same group.
1015 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1016 continue;
1017
1018 if (LastValue.getNode())
1019 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1020 i-1));
1021 LastRLAmt = ThisRLAmt;
1022 LastValue = ThisValue;
1023 LastGroupStartIdx = i;
1024 }
1025 if (LastValue.getNode())
1026 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1027 Bits.size()-1));
1028
1029 if (BitGroups.empty())
1030 return;
1031
1032 // We might be able to combine the first and last groups.
1033 if (BitGroups.size() > 1) {
1034 // If the first and last groups are the same, then remove the first group
1035 // in favor of the last group, making the ending index of the last group
1036 // equal to the ending index of the to-be-removed first group.
1037 if (BitGroups[0].StartIdx == 0 &&
1038 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1039 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1040 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001041 DEBUG(dbgs() << "\tcombining final bit group with inital one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001042 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1043 BitGroups.erase(BitGroups.begin());
1044 }
1045 }
1046 }
1047
1048 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1049 // associated with each. If there is a degeneracy, pick the one that occurs
1050 // first (in the final value).
1051 void collectValueRotInfo() {
1052 ValueRots.clear();
1053
1054 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001055 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1056 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001057 VRI.V = BG.V;
1058 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001059 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001060 VRI.NumGroups += 1;
1061 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1062 }
1063
1064 // Now that we've collected the various ValueRotInfo instances, we need to
1065 // sort them.
1066 ValueRotsVec.clear();
1067 for (auto &I : ValueRots) {
1068 ValueRotsVec.push_back(I.second);
1069 }
1070 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1071 }
1072
Hal Finkelc58ce412015-01-01 02:53:29 +00001073 // In 64-bit mode, rlwinm and friends have a rotation operator that
1074 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1075 // indices of these instructions can only be in the lower 32 bits, so they
1076 // can only represent some 64-bit bit groups. However, when they can be used,
1077 // the 32-bit replication can be used to represent, as a single bit group,
1078 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1079 // groups when possible. Returns true if any of the bit groups were
1080 // converted.
1081 void assignRepl32BitGroups() {
1082 // If we have bits like this:
1083 //
1084 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1085 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1086 // Groups: | RLAmt = 8 | RLAmt = 40 |
1087 //
1088 // But, making use of a 32-bit operation that replicates the low-order 32
1089 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1090 // of 8.
1091
1092 auto IsAllLow32 = [this](BitGroup & BG) {
1093 if (BG.StartIdx <= BG.EndIdx) {
1094 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1095 if (!Bits[i].hasValue())
1096 continue;
1097 if (Bits[i].getValueBitIndex() >= 32)
1098 return false;
1099 }
1100 } else {
1101 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1102 if (!Bits[i].hasValue())
1103 continue;
1104 if (Bits[i].getValueBitIndex() >= 32)
1105 return false;
1106 }
1107 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1108 if (!Bits[i].hasValue())
1109 continue;
1110 if (Bits[i].getValueBitIndex() >= 32)
1111 return false;
1112 }
1113 }
1114
1115 return true;
1116 };
1117
1118 for (auto &BG : BitGroups) {
1119 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1120 if (IsAllLow32(BG)) {
1121 if (BG.RLAmt >= 32) {
1122 BG.RLAmt -= 32;
1123 BG.Repl32CR = true;
1124 }
1125
1126 BG.Repl32 = true;
1127
1128 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1129 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1130 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1131 }
1132 }
1133 }
1134
1135 // Now walk through the bit groups, consolidating where possible.
1136 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1137 // We might want to remove this bit group by merging it with the previous
1138 // group (which might be the ending group).
1139 auto IP = (I == BitGroups.begin()) ?
1140 std::prev(BitGroups.end()) : std::prev(I);
1141 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1142 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1143
1144 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1145 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1146 " [" << I->StartIdx << ", " << I->EndIdx <<
1147 "] with group with range [" <<
1148 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1149
1150 IP->EndIdx = I->EndIdx;
1151 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1152 IP->Repl32Coalesced = true;
1153 I = BitGroups.erase(I);
1154 continue;
1155 } else {
1156 // There is a special case worth handling: If there is a single group
1157 // covering the entire upper 32 bits, and it can be merged with both
1158 // the next and previous groups (which might be the same group), then
1159 // do so. If it is the same group (so there will be only one group in
1160 // total), then we need to reverse the order of the range so that it
1161 // covers the entire 64 bits.
1162 if (I->StartIdx == 32 && I->EndIdx == 63) {
1163 assert(std::next(I) == BitGroups.end() &&
1164 "bit group ends at index 63 but there is another?");
1165 auto IN = BitGroups.begin();
1166
1167 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
1168 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1169 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1170 IsAllLow32(*I)) {
1171
1172 DEBUG(dbgs() << "\tcombining bit group for " <<
1173 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1174 " [" << I->StartIdx << ", " << I->EndIdx <<
1175 "] with 32-bit replicated groups with ranges [" <<
1176 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1177 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1178
1179 if (IP == IN) {
1180 // There is only one other group; change it to cover the whole
1181 // range (backward, so that it can still be Repl32 but cover the
1182 // whole 64-bit range).
1183 IP->StartIdx = 31;
1184 IP->EndIdx = 30;
1185 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1186 IP->Repl32Coalesced = true;
1187 I = BitGroups.erase(I);
1188 } else {
1189 // There are two separate groups, one before this group and one
1190 // after us (at the beginning). We're going to remove this group,
1191 // but also the group at the very beginning.
1192 IP->EndIdx = IN->EndIdx;
1193 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1194 IP->Repl32Coalesced = true;
1195 I = BitGroups.erase(I);
1196 BitGroups.erase(BitGroups.begin());
1197 }
1198
1199 // This must be the last group in the vector (and we might have
1200 // just invalidated the iterator above), so break here.
1201 break;
1202 }
1203 }
1204 }
1205
1206 ++I;
1207 }
1208 }
1209
Hal Finkel8adf2252014-12-16 05:51:41 +00001210 SDValue getI32Imm(unsigned Imm) {
1211 return CurDAG->getTargetConstant(Imm, MVT::i32);
1212 }
1213
Hal Finkelc58ce412015-01-01 02:53:29 +00001214 uint64_t getZerosMask() {
1215 uint64_t Mask = 0;
1216 for (unsigned i = 0; i < Bits.size(); ++i) {
1217 if (Bits[i].hasValue())
1218 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001219 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001220 }
1221
1222 return ~Mask;
1223 }
1224
Hal Finkel8adf2252014-12-16 05:51:41 +00001225 // Depending on the number of groups for a particular value, it might be
1226 // better to rotate, mask explicitly (using andi/andis), and then or the
1227 // result. Select this part of the result first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001228 void SelectAndParts32(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1229 if (BPermRewriterNoMasking)
1230 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001231
1232 for (ValueRotInfo &VRI : ValueRotsVec) {
1233 unsigned Mask = 0;
1234 for (unsigned i = 0; i < Bits.size(); ++i) {
1235 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1236 continue;
1237 if (RLAmt[i] != VRI.RLAmt)
1238 continue;
1239 Mask |= (1u << i);
1240 }
1241
1242 // Compute the masks for andi/andis that would be necessary.
1243 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1244 assert((ANDIMask != 0 || ANDISMask != 0) &&
1245 "No set bits in mask for value bit groups");
1246 bool NeedsRotate = VRI.RLAmt != 0;
1247
1248 // We're trying to minimize the number of instructions. If we have one
1249 // group, using one of andi/andis can break even. If we have three
1250 // groups, we can use both andi and andis and break even (to use both
1251 // andi and andis we also need to or the results together). We need four
1252 // groups if we also need to rotate. To use andi/andis we need to do more
1253 // than break even because rotate-and-mask instructions tend to be easier
1254 // to schedule.
1255
1256 // FIXME: We've biased here against using andi/andis, which is right for
1257 // POWER cores, but not optimal everywhere. For example, on the A2,
1258 // andi/andis have single-cycle latency whereas the rotate-and-mask
1259 // instructions take two cycles, and it would be better to bias toward
1260 // andi/andis in break-even cases.
1261
1262 unsigned NumAndInsts = (unsigned) NeedsRotate +
1263 (unsigned) (ANDIMask != 0) +
1264 (unsigned) (ANDISMask != 0) +
1265 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1266 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001267
1268 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1269 " RL: " << VRI.RLAmt << ":" <<
1270 "\n\t\t\tisel using masking: " << NumAndInsts <<
1271 " using rotates: " << VRI.NumGroups << "\n");
1272
Hal Finkel8adf2252014-12-16 05:51:41 +00001273 if (NumAndInsts >= VRI.NumGroups)
1274 continue;
1275
Hal Finkelc58ce412015-01-01 02:53:29 +00001276 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1277
1278 if (InstCnt) *InstCnt += NumAndInsts;
1279
Hal Finkel8adf2252014-12-16 05:51:41 +00001280 SDValue VRot;
1281 if (VRI.RLAmt) {
1282 SDValue Ops[] =
1283 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1284 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1285 Ops), 0);
1286 } else {
1287 VRot = VRI.V;
1288 }
1289
1290 SDValue ANDIVal, ANDISVal;
1291 if (ANDIMask != 0)
1292 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1293 VRot, getI32Imm(ANDIMask)), 0);
1294 if (ANDISMask != 0)
1295 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1296 VRot, getI32Imm(ANDISMask)), 0);
1297
1298 SDValue TotalVal;
1299 if (!ANDIVal)
1300 TotalVal = ANDISVal;
1301 else if (!ANDISVal)
1302 TotalVal = ANDIVal;
1303 else
1304 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1305 ANDIVal, ANDISVal), 0);
1306
1307 if (!Res)
1308 Res = TotalVal;
1309 else
1310 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1311 Res, TotalVal), 0);
1312
1313 // Now, remove all groups with this underlying value and rotation
1314 // factor.
1315 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1316 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1317 I = BitGroups.erase(I);
1318 else
1319 ++I;
1320 }
1321 }
1322 }
1323
1324 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001325 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001326 SDLoc dl(N);
1327 SDValue Res;
1328
Hal Finkelc58ce412015-01-01 02:53:29 +00001329 if (InstCnt) *InstCnt = 0;
1330
Hal Finkel8adf2252014-12-16 05:51:41 +00001331 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001332 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001333
1334 // If we've not yet selected a 'starting' instruction, and we have no zeros
1335 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1336 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001337 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001338 ValueRotInfo &VRI = ValueRotsVec[0];
1339 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001340 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001341 SDValue Ops[] =
1342 { VRI.V, getI32Imm(VRI.RLAmt), getI32Imm(0), getI32Imm(31) };
1343 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1344 } else {
1345 Res = VRI.V;
1346 }
1347
1348 // Now, remove all groups with this underlying value and rotation factor.
1349 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1350 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt)
1351 I = BitGroups.erase(I);
1352 else
1353 ++I;
1354 }
1355 }
1356
Hal Finkelc58ce412015-01-01 02:53:29 +00001357 if (InstCnt) *InstCnt += BitGroups.size();
1358
Hal Finkel8adf2252014-12-16 05:51:41 +00001359 // Insert the other groups (one at a time).
1360 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001361 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001362 SDValue Ops[] =
1363 { BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1364 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1365 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1366 } else {
1367 SDValue Ops[] =
1368 { Res, BG.V, getI32Imm(BG.RLAmt), getI32Imm(Bits.size() - BG.EndIdx - 1),
1369 getI32Imm(Bits.size() - BG.StartIdx - 1) };
1370 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1371 }
1372 }
1373
Hal Finkelc58ce412015-01-01 02:53:29 +00001374 if (LateMask) {
1375 unsigned Mask = (unsigned) getZerosMask();
1376
1377 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1378 assert((ANDIMask != 0 || ANDISMask != 0) &&
1379 "No set bits in zeros mask?");
1380
1381 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1382 (unsigned) (ANDISMask != 0) +
1383 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1384
1385 SDValue ANDIVal, ANDISVal;
1386 if (ANDIMask != 0)
1387 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
1388 Res, getI32Imm(ANDIMask)), 0);
1389 if (ANDISMask != 0)
1390 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
1391 Res, getI32Imm(ANDISMask)), 0);
1392
1393 if (!ANDIVal)
1394 Res = ANDISVal;
1395 else if (!ANDISVal)
1396 Res = ANDIVal;
1397 else
1398 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1399 ANDIVal, ANDISVal), 0);
1400 }
1401
Hal Finkel8adf2252014-12-16 05:51:41 +00001402 return Res.getNode();
1403 }
1404
Hal Finkelc58ce412015-01-01 02:53:29 +00001405 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1406 unsigned MaskStart, unsigned MaskEnd,
1407 bool IsIns) {
1408 // In the notation used by the instructions, 'start' and 'end' are reversed
1409 // because bits are counted from high to low order.
1410 unsigned InstMaskStart = 64 - MaskEnd - 1,
1411 InstMaskEnd = 64 - MaskStart - 1;
1412
1413 if (Repl32)
1414 return 1;
1415
1416 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1417 InstMaskEnd == 63 - RLAmt)
1418 return 1;
1419
1420 return 2;
1421 }
1422
1423 // For 64-bit values, not all combinations of rotates and masks are
1424 // available. Produce one if it is available.
1425 SDValue SelectRotMask64(SDValue V, SDLoc dl, unsigned RLAmt, bool Repl32,
1426 unsigned MaskStart, unsigned MaskEnd,
1427 unsigned *InstCnt = nullptr) {
1428 // In the notation used by the instructions, 'start' and 'end' are reversed
1429 // because bits are counted from high to low order.
1430 unsigned InstMaskStart = 64 - MaskEnd - 1,
1431 InstMaskEnd = 64 - MaskStart - 1;
1432
1433 if (InstCnt) *InstCnt += 1;
1434
1435 if (Repl32) {
1436 // This rotation amount assumes that the lower 32 bits of the quantity
1437 // are replicated in the high 32 bits by the rotation operator (which is
1438 // done by rlwinm and friends).
1439 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1440 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1441 SDValue Ops[] =
1442 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1443 getI32Imm(InstMaskEnd - 32) };
1444 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1445 Ops), 0);
1446 }
1447
1448 if (InstMaskEnd == 63) {
1449 SDValue Ops[] =
1450 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1451 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1452 }
1453
1454 if (InstMaskStart == 0) {
1455 SDValue Ops[] =
1456 { V, getI32Imm(RLAmt), getI32Imm(InstMaskEnd) };
1457 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1458 }
1459
1460 if (InstMaskEnd == 63 - RLAmt) {
1461 SDValue Ops[] =
1462 { V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1463 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1464 }
1465
1466 // We cannot do this with a single instruction, so we'll use two. The
1467 // problem is that we're not free to choose both a rotation amount and mask
1468 // start and end independently. We can choose an arbitrary mask start and
1469 // end, but then the rotation amount is fixed. Rotation, however, can be
1470 // inverted, and so by applying an "inverse" rotation first, we can get the
1471 // desired result.
1472 if (InstCnt) *InstCnt += 1;
1473
1474 // The rotation mask for the second instruction must be MaskStart.
1475 unsigned RLAmt2 = MaskStart;
1476 // The first instruction must rotate V so that the overall rotation amount
1477 // is RLAmt.
1478 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1479 if (RLAmt1)
1480 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1481 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1482 }
1483
1484 // For 64-bit values, not all combinations of rotates and masks are
1485 // available. Produce a rotate-mask-and-insert if one is available.
1486 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, SDLoc dl, unsigned RLAmt,
1487 bool Repl32, unsigned MaskStart,
1488 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1489 // In the notation used by the instructions, 'start' and 'end' are reversed
1490 // because bits are counted from high to low order.
1491 unsigned InstMaskStart = 64 - MaskEnd - 1,
1492 InstMaskEnd = 64 - MaskStart - 1;
1493
1494 if (InstCnt) *InstCnt += 1;
1495
1496 if (Repl32) {
1497 // This rotation amount assumes that the lower 32 bits of the quantity
1498 // are replicated in the high 32 bits by the rotation operator (which is
1499 // done by rlwinm and friends).
1500 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1501 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1502 SDValue Ops[] =
1503 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart - 32),
1504 getI32Imm(InstMaskEnd - 32) };
1505 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1506 Ops), 0);
1507 }
1508
1509 if (InstMaskEnd == 63 - RLAmt) {
1510 SDValue Ops[] =
1511 { Base, V, getI32Imm(RLAmt), getI32Imm(InstMaskStart) };
1512 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1513 }
1514
1515 // We cannot do this with a single instruction, so we'll use two. The
1516 // problem is that we're not free to choose both a rotation amount and mask
1517 // start and end independently. We can choose an arbitrary mask start and
1518 // end, but then the rotation amount is fixed. Rotation, however, can be
1519 // inverted, and so by applying an "inverse" rotation first, we can get the
1520 // desired result.
1521 if (InstCnt) *InstCnt += 1;
1522
1523 // The rotation mask for the second instruction must be MaskStart.
1524 unsigned RLAmt2 = MaskStart;
1525 // The first instruction must rotate V so that the overall rotation amount
1526 // is RLAmt.
1527 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1528 if (RLAmt1)
1529 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1530 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1531 }
1532
1533 void SelectAndParts64(SDLoc dl, SDValue &Res, unsigned *InstCnt) {
1534 if (BPermRewriterNoMasking)
1535 return;
1536
1537 // The idea here is the same as in the 32-bit version, but with additional
1538 // complications from the fact that Repl32 might be true. Because we
1539 // aggressively convert bit groups to Repl32 form (which, for small
1540 // rotation factors, involves no other change), and then coalesce, it might
1541 // be the case that a single 64-bit masking operation could handle both
1542 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1543 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1544 // completely capture the new combined bit group.
1545
1546 for (ValueRotInfo &VRI : ValueRotsVec) {
1547 uint64_t Mask = 0;
1548
1549 // We need to add to the mask all bits from the associated bit groups.
1550 // If Repl32 is false, we need to add bits from bit groups that have
1551 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1552 // group is trivially convertable if it overlaps only with the lower 32
1553 // bits, and the group has not been coalesced.
1554 auto MatchingBG = [VRI](BitGroup &BG) {
1555 if (VRI.V != BG.V)
1556 return false;
1557
1558 unsigned EffRLAmt = BG.RLAmt;
1559 if (!VRI.Repl32 && BG.Repl32) {
1560 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1561 !BG.Repl32Coalesced) {
1562 if (BG.Repl32CR)
1563 EffRLAmt += 32;
1564 } else {
1565 return false;
1566 }
1567 } else if (VRI.Repl32 != BG.Repl32) {
1568 return false;
1569 }
1570
1571 if (VRI.RLAmt != EffRLAmt)
1572 return false;
1573
1574 return true;
1575 };
1576
1577 for (auto &BG : BitGroups) {
1578 if (!MatchingBG(BG))
1579 continue;
1580
1581 if (BG.StartIdx <= BG.EndIdx) {
1582 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001583 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001584 } else {
1585 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001586 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001587 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001588 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001589 }
1590 }
1591
1592 // We can use the 32-bit andi/andis technique if the mask does not
1593 // require any higher-order bits. This can save an instruction compared
1594 // to always using the general 64-bit technique.
1595 bool Use32BitInsts = isUInt<32>(Mask);
1596 // Compute the masks for andi/andis that would be necessary.
1597 unsigned ANDIMask = (Mask & UINT16_MAX),
1598 ANDISMask = (Mask >> 16) & UINT16_MAX;
1599
1600 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1601
1602 unsigned NumAndInsts = (unsigned) NeedsRotate +
1603 (unsigned) (bool) Res;
1604 if (Use32BitInsts)
1605 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1606 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1607 else
1608 NumAndInsts += SelectInt64Count(Mask) + /* and */ 1;
1609
1610 unsigned NumRLInsts = 0;
1611 bool FirstBG = true;
1612 for (auto &BG : BitGroups) {
1613 if (!MatchingBG(BG))
1614 continue;
1615 NumRLInsts +=
1616 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1617 !FirstBG);
1618 FirstBG = false;
1619 }
1620
1621 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1622 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1623 "\n\t\t\tisel using masking: " << NumAndInsts <<
1624 " using rotates: " << NumRLInsts << "\n");
1625
1626 // When we'd use andi/andis, we bias toward using the rotates (andi only
1627 // has a record form, and is cracked on POWER cores). However, when using
1628 // general 64-bit constant formation, bias toward the constant form,
1629 // because that exposes more opportunities for CSE.
1630 if (NumAndInsts > NumRLInsts)
1631 continue;
1632 if (Use32BitInsts && NumAndInsts == NumRLInsts)
1633 continue;
1634
1635 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1636
1637 if (InstCnt) *InstCnt += NumAndInsts;
1638
1639 SDValue VRot;
1640 // We actually need to generate a rotation if we have a non-zero rotation
1641 // factor or, in the Repl32 case, if we care about any of the
1642 // higher-order replicated bits. In the latter case, we generate a mask
1643 // backward so that it actually includes the entire 64 bits.
1644 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1645 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1646 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1647 else
1648 VRot = VRI.V;
1649
1650 SDValue TotalVal;
1651 if (Use32BitInsts) {
1652 assert((ANDIMask != 0 || ANDISMask != 0) &&
1653 "No set bits in mask when using 32-bit ands for 64-bit value");
1654
1655 SDValue ANDIVal, ANDISVal;
1656 if (ANDIMask != 0)
1657 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1658 VRot, getI32Imm(ANDIMask)), 0);
1659 if (ANDISMask != 0)
1660 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1661 VRot, getI32Imm(ANDISMask)), 0);
1662
1663 if (!ANDIVal)
1664 TotalVal = ANDISVal;
1665 else if (!ANDISVal)
1666 TotalVal = ANDIVal;
1667 else
1668 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1669 ANDIVal, ANDISVal), 0);
1670 } else {
1671 TotalVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1672 TotalVal =
1673 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1674 VRot, TotalVal), 0);
1675 }
1676
1677 if (!Res)
1678 Res = TotalVal;
1679 else
1680 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1681 Res, TotalVal), 0);
1682
1683 // Now, remove all groups with this underlying value and rotation
1684 // factor.
1685 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1686 if (MatchingBG(*I))
1687 I = BitGroups.erase(I);
1688 else
1689 ++I;
1690 }
1691 }
1692 }
1693
1694 // Instruction selection for the 64-bit case.
1695 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1696 SDLoc dl(N);
1697 SDValue Res;
1698
1699 if (InstCnt) *InstCnt = 0;
1700
1701 // Take care of cases that should use andi/andis first.
1702 SelectAndParts64(dl, Res, InstCnt);
1703
1704 // If we've not yet selected a 'starting' instruction, and we have no zeros
1705 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1706 // number of groups), and start with this rotated value.
1707 if ((!HasZeros || LateMask) && !Res) {
1708 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1709 // groups will come first, and so the VRI representing the largest number
1710 // of groups might not be first (it might be the first Repl32 groups).
1711 unsigned MaxGroupsIdx = 0;
1712 if (!ValueRotsVec[0].Repl32) {
1713 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1714 if (ValueRotsVec[i].Repl32) {
1715 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1716 MaxGroupsIdx = i;
1717 break;
1718 }
1719 }
1720
1721 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1722 bool NeedsRotate = false;
1723 if (VRI.RLAmt) {
1724 NeedsRotate = true;
1725 } else if (VRI.Repl32) {
1726 for (auto &BG : BitGroups) {
1727 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1728 BG.Repl32 != VRI.Repl32)
1729 continue;
1730
1731 // We don't need a rotate if the bit group is confined to the lower
1732 // 32 bits.
1733 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1734 continue;
1735
1736 NeedsRotate = true;
1737 break;
1738 }
1739 }
1740
1741 if (NeedsRotate)
1742 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1743 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1744 InstCnt);
1745 else
1746 Res = VRI.V;
1747
1748 // Now, remove all groups with this underlying value and rotation factor.
1749 if (Res)
1750 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1751 if (I->V == VRI.V && I->RLAmt == VRI.RLAmt && I->Repl32 == VRI.Repl32)
1752 I = BitGroups.erase(I);
1753 else
1754 ++I;
1755 }
1756 }
1757
1758 // Because 64-bit rotates are more flexible than inserts, we might have a
1759 // preference regarding which one we do first (to save one instruction).
1760 if (!Res)
1761 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1762 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1763 false) <
1764 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1765 true)) {
1766 if (I != BitGroups.begin()) {
1767 BitGroup BG = *I;
1768 BitGroups.erase(I);
1769 BitGroups.insert(BitGroups.begin(), BG);
1770 }
1771
1772 break;
1773 }
1774 }
1775
1776 // Insert the other groups (one at a time).
1777 for (auto &BG : BitGroups) {
1778 if (!Res)
1779 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1780 BG.EndIdx, InstCnt);
1781 else
1782 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1783 BG.StartIdx, BG.EndIdx, InstCnt);
1784 }
1785
1786 if (LateMask) {
1787 uint64_t Mask = getZerosMask();
1788
1789 // We can use the 32-bit andi/andis technique if the mask does not
1790 // require any higher-order bits. This can save an instruction compared
1791 // to always using the general 64-bit technique.
1792 bool Use32BitInsts = isUInt<32>(Mask);
1793 // Compute the masks for andi/andis that would be necessary.
1794 unsigned ANDIMask = (Mask & UINT16_MAX),
1795 ANDISMask = (Mask >> 16) & UINT16_MAX;
1796
1797 if (Use32BitInsts) {
1798 assert((ANDIMask != 0 || ANDISMask != 0) &&
1799 "No set bits in mask when using 32-bit ands for 64-bit value");
1800
1801 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1802 (unsigned) (ANDISMask != 0) +
1803 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1804
1805 SDValue ANDIVal, ANDISVal;
1806 if (ANDIMask != 0)
1807 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
1808 Res, getI32Imm(ANDIMask)), 0);
1809 if (ANDISMask != 0)
1810 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
1811 Res, getI32Imm(ANDISMask)), 0);
1812
1813 if (!ANDIVal)
1814 Res = ANDISVal;
1815 else if (!ANDISVal)
1816 Res = ANDIVal;
1817 else
1818 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1819 ANDIVal, ANDISVal), 0);
1820 } else {
1821 if (InstCnt) *InstCnt += SelectInt64Count(Mask) + /* and */ 1;
1822
1823 SDValue MaskVal = SDValue(SelectInt64(CurDAG, dl, Mask), 0);
1824 Res =
1825 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1826 Res, MaskVal), 0);
1827 }
1828 }
1829
1830 return Res.getNode();
1831 }
1832
1833 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1834 // Fill in BitGroups.
1835 collectBitGroups(LateMask);
1836 if (BitGroups.empty())
1837 return nullptr;
1838
1839 // For 64-bit values, figure out when we can use 32-bit instructions.
1840 if (Bits.size() == 64)
1841 assignRepl32BitGroups();
1842
1843 // Fill in ValueRotsVec.
1844 collectValueRotInfo();
1845
1846 if (Bits.size() == 32) {
1847 return Select32(N, LateMask, InstCnt);
1848 } else {
1849 assert(Bits.size() == 64 && "Not 64 bits here?");
1850 return Select64(N, LateMask, InstCnt);
1851 }
1852
1853 return nullptr;
1854 }
1855
Hal Finkel8adf2252014-12-16 05:51:41 +00001856 SmallVector<ValueBit, 64> Bits;
1857
1858 bool HasZeros;
1859 SmallVector<unsigned, 64> RLAmt;
1860
1861 SmallVector<BitGroup, 16> BitGroups;
1862
1863 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1864 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1865
1866 SelectionDAG *CurDAG;
1867
1868public:
1869 BitPermutationSelector(SelectionDAG *DAG)
1870 : CurDAG(DAG) {}
1871
1872 // Here we try to match complex bit permutations into a set of
1873 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1874 // known to produce optimial code for common cases (like i32 byte swapping).
1875 SDNode *Select(SDNode *N) {
1876 Bits.resize(N->getValueType(0).getSizeInBits());
1877 if (!getValueBits(SDValue(N, 0), Bits))
1878 return nullptr;
1879
1880 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
1881 " selection for: ");
1882 DEBUG(N->dump(CurDAG));
1883
1884 // Fill it RLAmt and set HasZeros.
1885 computeRotationAmounts();
1886
Hal Finkelc58ce412015-01-01 02:53:29 +00001887 if (!HasZeros)
1888 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00001889
Hal Finkelc58ce412015-01-01 02:53:29 +00001890 // We currently have two techniques for handling results with zeros: early
1891 // masking (the default) and late masking. Late masking is sometimes more
1892 // efficient, but because the structure of the bit groups is different, it
1893 // is hard to tell without generating both and comparing the results. With
1894 // late masking, we ignore zeros in the resulting value when inserting each
1895 // set of bit groups, and then mask in the zeros at the end. With early
1896 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00001897
Hal Finkelc58ce412015-01-01 02:53:29 +00001898 unsigned InstCnt, InstCntLateMask;
1899 DEBUG(dbgs() << "\tEarly masking:\n");
1900 SDNode *RN = Select(N, false, &InstCnt);
1901 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
1902
1903 DEBUG(dbgs() << "\tLate masking:\n");
1904 SDNode *RNLM = Select(N, true, &InstCntLateMask);
1905 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
1906 " instructions\n");
1907
1908 if (InstCnt <= InstCntLateMask) {
1909 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
1910 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00001911 }
1912
Hal Finkelc58ce412015-01-01 02:53:29 +00001913 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
1914 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00001915 }
1916};
1917} // anonymous namespace
1918
1919SDNode *PPCDAGToDAGISel::SelectBitPermutation(SDNode *N) {
1920 if (N->getValueType(0) != MVT::i32 &&
1921 N->getValueType(0) != MVT::i64)
1922 return nullptr;
1923
Hal Finkelc58ce412015-01-01 02:53:29 +00001924 if (!UseBitPermRewriter)
1925 return nullptr;
1926
Hal Finkel8adf2252014-12-16 05:51:41 +00001927 switch (N->getOpcode()) {
1928 default: break;
1929 case ISD::ROTL:
1930 case ISD::SHL:
1931 case ISD::SRL:
1932 case ISD::AND:
1933 case ISD::OR: {
1934 BitPermutationSelector BPS(CurDAG);
1935 return BPS.Select(N);
1936 }
1937 }
1938
1939 return nullptr;
1940}
1941
Chris Lattner2a1823d2005-08-21 18:50:37 +00001942/// SelectCC - Select a comparison of the specified values with the specified
1943/// condition code, returning the CR# of the expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001944SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001945 ISD::CondCode CC, SDLoc dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00001946 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00001947 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00001948
Owen Anderson9f944592009-08-11 20:47:22 +00001949 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00001950 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00001951 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
1952 if (isInt32Immediate(RHS, Imm)) {
1953 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001954 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001955 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1956 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001957 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001958 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001959 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1960 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00001961
Chris Lattneraa3926b2006-09-20 04:25:47 +00001962 // For non-equality comparisons, the default code would materialize the
1963 // constant, then compare against it, like this:
1964 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00001965 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00001966 // cmpw cr0, r3, r2
1967 // Since we are just comparing for equality, we can emit this instead:
1968 // xoris r0,r3,0x1234
1969 // cmplwi cr0,r0,0x5678
1970 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00001971 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
1972 getI32Imm(Imm >> 16)), 0);
1973 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
1974 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00001975 }
1976 Opc = PPC::CMPLW;
1977 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00001978 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001979 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
1980 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00001981 Opc = PPC::CMPLW;
1982 } else {
1983 short SImm;
1984 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001985 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
1986 getI32Imm((int)SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00001987 0);
1988 Opc = PPC::CMPW;
1989 }
Owen Anderson9f944592009-08-11 20:47:22 +00001990 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00001991 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001992 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001993 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001994 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001995 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00001996 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
1997 getI32Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00001998 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00001999 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002000 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
2001 getI32Imm(Imm & 0xFFFF)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002002
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002003 // For non-equality comparisons, the default code would materialize the
2004 // constant, then compare against it, like this:
2005 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002006 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002007 // cmpd cr0, r3, r2
2008 // Since we are just comparing for equality, we can emit this instead:
2009 // xoris r0,r3,0x1234
2010 // cmpldi cr0,r0,0x5678
2011 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002012 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002013 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
2014 getI64Imm(Imm >> 16)), 0);
2015 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
2016 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002017 }
2018 }
2019 Opc = PPC::CMPLD;
2020 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002021 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002022 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
2023 getI64Imm(Imm & 0xFFFF)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002024 Opc = PPC::CMPLD;
2025 } else {
2026 short SImm;
2027 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002028 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
2029 getI64Imm(SImm & 0xFFFF)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002030 0);
2031 Opc = PPC::CMPD;
2032 }
Owen Anderson9f944592009-08-11 20:47:22 +00002033 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002034 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002035 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002036 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002037 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002038 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002039 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002040}
2041
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002042static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002043 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002044 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002045 case ISD::SETONE:
2046 case ISD::SETOLE:
2047 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002048 llvm_unreachable("Should be lowered by legalize!");
2049 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002050 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002051 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002052 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002053 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002054 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002055 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002056 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002057 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002058 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002059 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002060 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002061 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002062 case ISD::SETO: return PPC::PRED_NU;
2063 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002064 // These two are invalid for floating point. Assume we have int.
2065 case ISD::SETULT: return PPC::PRED_LT;
2066 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002067 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002068}
2069
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002070/// getCRIdxForSetCC - Return the index of the condition register field
2071/// associated with the SetCC condition, and whether or not the field is
2072/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002073static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002074 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002075 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002076 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002077 case ISD::SETOLT:
2078 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2079 case ISD::SETOGT:
2080 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2081 case ISD::SETOEQ:
2082 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2083 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002084 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002085 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002086 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002087 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002088 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002089 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2090 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002091 case ISD::SETUEQ:
2092 case ISD::SETOGE:
2093 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002094 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002095 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002096 // These are invalid for floating point. Assume integer.
2097 case ISD::SETULT: return 0;
2098 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002099 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002100}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002101
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002102// getVCmpInst: return the vector compare instruction for the specified
2103// vector type and condition code. Since this is for altivec specific code,
2104// only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002105static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2106 bool HasVSX, bool &Swap, bool &Negate) {
2107 Swap = false;
2108 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002109
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002110 if (VecVT.isFloatingPoint()) {
2111 /* Handle some cases by swapping input operands. */
2112 switch (CC) {
2113 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2114 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2115 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2116 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2117 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2118 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2119 default: break;
2120 }
2121 /* Handle some cases by negating the result. */
2122 switch (CC) {
2123 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2124 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2125 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2126 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2127 default: break;
2128 }
2129 /* We have instructions implementing the remaining cases. */
2130 switch (CC) {
2131 case ISD::SETEQ:
2132 case ISD::SETOEQ:
2133 if (VecVT == MVT::v4f32)
2134 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2135 else if (VecVT == MVT::v2f64)
2136 return PPC::XVCMPEQDP;
2137 break;
2138 case ISD::SETGT:
2139 case ISD::SETOGT:
2140 if (VecVT == MVT::v4f32)
2141 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2142 else if (VecVT == MVT::v2f64)
2143 return PPC::XVCMPGTDP;
2144 break;
2145 case ISD::SETGE:
2146 case ISD::SETOGE:
2147 if (VecVT == MVT::v4f32)
2148 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2149 else if (VecVT == MVT::v2f64)
2150 return PPC::XVCMPGEDP;
2151 break;
2152 default:
2153 break;
2154 }
2155 llvm_unreachable("Invalid floating-point vector compare condition");
2156 } else {
2157 /* Handle some cases by swapping input operands. */
2158 switch (CC) {
2159 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2160 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2161 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2162 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2163 default: break;
2164 }
2165 /* Handle some cases by negating the result. */
2166 switch (CC) {
2167 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2168 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2169 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2170 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2171 default: break;
2172 }
2173 /* We have instructions implementing the remaining cases. */
2174 switch (CC) {
2175 case ISD::SETEQ:
2176 case ISD::SETUEQ:
2177 if (VecVT == MVT::v16i8)
2178 return PPC::VCMPEQUB;
2179 else if (VecVT == MVT::v8i16)
2180 return PPC::VCMPEQUH;
2181 else if (VecVT == MVT::v4i32)
2182 return PPC::VCMPEQUW;
2183 break;
2184 case ISD::SETGT:
2185 if (VecVT == MVT::v16i8)
2186 return PPC::VCMPGTSB;
2187 else if (VecVT == MVT::v8i16)
2188 return PPC::VCMPGTSH;
2189 else if (VecVT == MVT::v4i32)
2190 return PPC::VCMPGTSW;
2191 break;
2192 case ISD::SETUGT:
2193 if (VecVT == MVT::v16i8)
2194 return PPC::VCMPGTUB;
2195 else if (VecVT == MVT::v8i16)
2196 return PPC::VCMPGTUH;
2197 else if (VecVT == MVT::v4i32)
2198 return PPC::VCMPGTUW;
2199 break;
2200 default:
2201 break;
2202 }
2203 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002204 }
2205}
2206
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002207SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002208 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002209 unsigned Imm;
2210 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002211 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2212 bool isPPC64 = (PtrVT == MVT::i64);
2213
Eric Christopher1b8e7632014-05-22 01:07:24 +00002214 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002215 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002216 // We can codegen setcc op, imm very efficiently compared to a brcond.
2217 // Check for those cases here.
2218 // setcc op, 0
2219 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002220 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002221 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002222 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002223 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002224 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002225 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002226 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002227 }
Chris Lattnere2969492005-10-21 21:17:10 +00002228 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002229 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002230 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002231 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002232 Op, getI32Imm(~0U)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002233 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002234 AD.getValue(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002235 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002236 case ISD::SETLT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002237 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002238 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Evan Chengc3acfc02006-08-27 08:14:06 +00002239 }
Chris Lattnere2969492005-10-21 21:17:10 +00002240 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002241 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002242 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2243 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002244 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002245 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002246 }
2247 }
Chris Lattner491b8292005-10-06 19:03:35 +00002248 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002249 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002250 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002251 default: break;
2252 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002253 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002254 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002255 Op, getI32Imm(1)), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002256 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2257 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
Dan Gohman32f71d72009-09-25 18:54:59 +00002258 MVT::i32,
2259 getI32Imm(0)), 0),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002260 Op.getValue(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002261 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002262 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002263 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002264 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +00002265 Op, getI32Imm(~0U));
Owen Anderson9f944592009-08-11 20:47:22 +00002266 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002267 Op, SDValue(AD, 1));
Chris Lattner491b8292005-10-06 19:03:35 +00002268 }
Chris Lattnere2969492005-10-21 21:17:10 +00002269 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002270 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
2271 getI32Imm(1)), 0);
2272 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2273 Op), 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002274 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Craig Topper481fb282014-04-27 19:21:11 +00002275 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattnere2969492005-10-21 21:17:10 +00002276 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002277 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002278 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
Michael Liaob53d8962013-04-19 22:22:57 +00002279 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002280 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002281 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
Evan Cheng34b70ee2006-08-26 08:00:10 +00002282 getI32Imm(1));
Chris Lattnere2969492005-10-21 21:17:10 +00002283 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002284 }
Chris Lattner491b8292005-10-06 19:03:35 +00002285 }
2286 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002287
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002288 SDValue LHS = N->getOperand(0);
2289 SDValue RHS = N->getOperand(1);
2290
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002291 // Altivec Vector compare instructions do not set any CR register by default and
2292 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002293 if (LHS.getValueType().isVector()) {
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002294 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002295 bool Swap, Negate;
2296 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2297 PPCSubTarget->hasVSX(), Swap, Negate);
2298 if (Swap)
2299 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002300
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002301 if (Negate) {
2302 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
2303 return CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR :
2304 PPC::VNOR,
2305 VecVT, VCmp, VCmp);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002306 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002307
2308 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002309 }
2310
Eric Christopher1b8e7632014-05-22 01:07:24 +00002311 if (PPCSubTarget->useCRBits())
Craig Topper062a2ba2014-04-25 05:30:21 +00002312 return nullptr;
Hal Finkel940ab932014-02-28 00:27:01 +00002313
Chris Lattner491b8292005-10-06 19:03:35 +00002314 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002315 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002316 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002317 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002318
Chris Lattner491b8292005-10-06 19:03:35 +00002319 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002320 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002321
Craig Topper062a2ba2014-04-25 05:30:21 +00002322 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002323 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002324 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002325
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002326 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2327 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002328
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002329 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
Evan Chengc3acfc02006-08-27 08:14:06 +00002330 getI32Imm(31), getI32Imm(31) };
Ulrich Weigand47e93282013-07-03 15:13:30 +00002331 if (!Inv)
Craig Topper481fb282014-04-27 19:21:11 +00002332 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Chris Lattner89f36e62008-01-08 06:46:30 +00002333
2334 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002335 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002336 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Ulrich Weigand47e93282013-07-03 15:13:30 +00002337 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
Chris Lattner491b8292005-10-06 19:03:35 +00002338}
Chris Lattner502a3692005-10-06 18:56:10 +00002339
Chris Lattner318622f2005-10-06 19:07:45 +00002340
Chris Lattner43ff01e2005-08-17 19:33:03 +00002341// Select - Convert the specified operand from a target-independent to a
2342// target-specific node if it hasn't already been changed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002343SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002344 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002345 if (N->isMachineOpcode()) {
2346 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002347 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002348 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002349
Hal Finkel51b3fd12014-09-02 06:23:54 +00002350 // In case any misguided DAG-level optimizations form an ADD with a
2351 // TargetConstant operand, crash here instead of miscompiling (by selecting
2352 // an r+r add instead of some kind of r+i add).
2353 if (N->getOpcode() == ISD::ADD &&
2354 N->getOperand(1).getOpcode() == ISD::TargetConstant)
2355 llvm_unreachable("Invalid ADD with TargetConstant operand");
2356
Hal Finkel8adf2252014-12-16 05:51:41 +00002357 // Try matching complex bit permutations before doing anything else.
2358 if (SDNode *NN = SelectBitPermutation(N))
2359 return NN;
2360
Chris Lattner43ff01e2005-08-17 19:33:03 +00002361 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00002362 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002363
Jim Laskey095e6f32006-12-12 13:23:43 +00002364 case ISD::Constant: {
Hal Finkelc58ce412015-01-01 02:53:29 +00002365 if (N->getValueType(0) == MVT::i64)
2366 return SelectInt64(CurDAG, N);
Jim Laskey095e6f32006-12-12 13:23:43 +00002367 break;
2368 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002369
Hal Finkel940ab932014-02-28 00:27:01 +00002370 case ISD::SETCC: {
2371 SDNode *SN = SelectSETCC(N);
2372 if (SN)
2373 return SN;
2374 break;
2375 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002376 case PPCISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +00002377 return getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +00002378
Hal Finkelb5e9b042014-12-11 22:51:06 +00002379 case ISD::FrameIndex:
2380 return getFrameIndex(N, N);
Chris Lattner6961fc72006-03-26 10:06:40 +00002381
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002382 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002383 SDValue InFlag = N->getOperand(1);
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002384 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
2385 N->getOperand(0), InFlag);
Chris Lattner6961fc72006-03-26 10:06:40 +00002386 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002387
Hal Finkelbbdee932014-12-02 22:01:00 +00002388 case PPCISD::READ_TIME_BASE: {
2389 return CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
2390 MVT::Other, N->getOperand(0));
2391 }
2392
Hal Finkel13d104b2014-12-11 18:37:52 +00002393 case PPCISD::SRA_ADDZE: {
2394 SDValue N0 = N->getOperand(0);
2395 SDValue ShiftAmt =
2396 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
2397 getConstantIntValue(), N->getValueType(0));
2398 if (N->getValueType(0) == MVT::i64) {
2399 SDNode *Op =
2400 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
2401 N0, ShiftAmt);
2402 return CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64,
2403 SDValue(Op, 0), SDValue(Op, 1));
2404 } else {
2405 assert(N->getValueType(0) == MVT::i32 &&
2406 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
2407 SDNode *Op =
2408 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
2409 N0, ShiftAmt);
2410 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2411 SDValue(Op, 0), SDValue(Op, 1));
Chris Lattnerdc664572005-08-25 17:50:06 +00002412 }
Chris Lattner6e184f22005-08-25 22:04:30 +00002413 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002414
Chris Lattnerce645542006-11-10 02:08:47 +00002415 case ISD::LOAD: {
2416 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002417 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002418 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00002419
Chris Lattnerce645542006-11-10 02:08:47 +00002420 // Normal loads are handled by code generated from the .td file.
2421 if (LD->getAddressingMode() != ISD::PRE_INC)
2422 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00002423
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002424 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00002425 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00002426 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00002427
Chris Lattner474b5b72006-11-15 19:55:13 +00002428 unsigned Opcode;
2429 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00002430 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00002431 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00002432 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2433 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002434 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002435 case MVT::f64: Opcode = PPC::LFDU; break;
2436 case MVT::f32: Opcode = PPC::LFSU; break;
2437 case MVT::i32: Opcode = PPC::LWZU; break;
2438 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
2439 case MVT::i1:
2440 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002441 }
2442 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002443 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2444 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2445 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002446 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00002447 case MVT::i64: Opcode = PPC::LDU; break;
2448 case MVT::i32: Opcode = PPC::LWZU8; break;
2449 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
2450 case MVT::i1:
2451 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00002452 }
2453 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002454
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002455 SDValue Chain = LD->getChain();
2456 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002457 SDValue Ops[] = { Offset, Base, Chain };
Dan Gohman32f71d72009-09-25 18:54:59 +00002458 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00002459 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00002460 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00002461 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00002462 unsigned Opcode;
2463 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
2464 if (LD->getValueType(0) != MVT::i64) {
2465 // Handle PPC32 integer and normal FP loads.
2466 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
2467 switch (LoadedVT.getSimpleVT().SimpleTy) {
2468 default: llvm_unreachable("Invalid PPC load type!");
2469 case MVT::f64: Opcode = PPC::LFDUX; break;
2470 case MVT::f32: Opcode = PPC::LFSUX; break;
2471 case MVT::i32: Opcode = PPC::LWZUX; break;
2472 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
2473 case MVT::i1:
2474 case MVT::i8: Opcode = PPC::LBZUX; break;
2475 }
2476 } else {
2477 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
2478 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
2479 "Invalid sext update load");
2480 switch (LoadedVT.getSimpleVT().SimpleTy) {
2481 default: llvm_unreachable("Invalid PPC load type!");
2482 case MVT::i64: Opcode = PPC::LDUX; break;
2483 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
2484 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
2485 case MVT::i1:
2486 case MVT::i8: Opcode = PPC::LBZUX8; break;
2487 }
2488 }
2489
2490 SDValue Chain = LD->getChain();
2491 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00002492 SDValue Ops[] = { Base, Offset, Chain };
Hal Finkelca542be2012-06-20 15:43:03 +00002493 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
Eric Christopher1b8e7632014-05-22 01:07:24 +00002494 PPCLowering->getPointerTy(),
Michael Liaob53d8962013-04-19 22:22:57 +00002495 MVT::Other, Ops);
Chris Lattnerce645542006-11-10 02:08:47 +00002496 }
2497 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002498
Nate Begemanb3821a32005-08-18 07:30:46 +00002499 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00002500 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00002501 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00002502
Nate Begemanb3821a32005-08-18 07:30:46 +00002503 // If this is an and of a value rotated between 0 and 31 bits and then and'd
2504 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00002505 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00002506 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002507 SDValue Val = N->getOperand(0).getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002508 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002509 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemanb3821a32005-08-18 07:30:46 +00002510 }
Nate Begemand31efd12006-09-22 05:01:56 +00002511 // If this is just a masked value where the input is not handled above, and
2512 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
2513 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002514 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00002515 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002516 SDValue Val = N->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002517 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002518 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begemand31efd12006-09-22 05:01:56 +00002519 }
Hal Finkele39526a2012-08-28 02:10:15 +00002520 // If this is a 64-bit zero-extension mask, emit rldicl.
2521 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
2522 isMask_64(Imm64)) {
2523 SDValue Val = N->getOperand(0);
2524 MB = 64 - CountTrailingOnes_64(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00002525 SH = 0;
2526
2527 // If the operand is a logical right shift, we can fold it into this
2528 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
2529 // for n <= mb. The right shift is really a left rotate followed by a
2530 // mask, and this mask is a more-restrictive sub-mask of the mask implied
2531 // by the shift.
2532 if (Val.getOpcode() == ISD::SRL &&
2533 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
2534 assert(Imm < 64 && "Illegal shift amount");
2535 Val = Val.getOperand(0);
2536 SH = 64 - Imm;
2537 }
2538
2539 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
Craig Topper481fb282014-04-27 19:21:11 +00002540 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
Hal Finkele39526a2012-08-28 02:10:15 +00002541 }
Nate Begemand31efd12006-09-22 05:01:56 +00002542 // AND X, 0 -> 0, not "rlwinm 32".
2543 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002544 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Craig Topper062a2ba2014-04-25 05:30:21 +00002545 return nullptr;
Nate Begemand31efd12006-09-22 05:01:56 +00002546 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00002547 // ISD::OR doesn't get all the bitfield insertion fun.
2548 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
Andrew Trickc416ba62010-12-24 04:28:06 +00002549 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00002550 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00002551 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Chris Lattner20c88df2006-01-05 18:32:49 +00002552 unsigned MB, ME;
Nate Begeman9aea6e42005-12-24 01:00:15 +00002553 Imm = ~(Imm^Imm2);
2554 if (isRunOfOnes(Imm, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002555 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002556 N->getOperand(0).getOperand(1),
2557 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
Michael Liaob53d8962013-04-19 22:22:57 +00002558 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
Nate Begeman9aea6e42005-12-24 01:00:15 +00002559 }
2560 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002561
Chris Lattner1de57062005-09-29 23:33:31 +00002562 // Other cases are autogenerated.
2563 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00002564 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00002565 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00002566 if (N->getValueType(0) == MVT::i32)
Chris Lattnerbc485fd2006-08-15 23:48:22 +00002567 if (SDNode *I = SelectBitfieldInsert(N))
2568 return I;
Andrew Trickc416ba62010-12-24 04:28:06 +00002569
Hal Finkelb5e9b042014-12-11 22:51:06 +00002570 short Imm;
2571 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2572 isIntS16Immediate(N->getOperand(1), Imm)) {
2573 APInt LHSKnownZero, LHSKnownOne;
2574 CurDAG->computeKnownBits(N->getOperand(0), LHSKnownZero, LHSKnownOne);
2575
2576 // If this is equivalent to an add, then we can fold it with the
2577 // FrameIndex calculation.
2578 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL)
2579 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2580 }
2581
Chris Lattner1de57062005-09-29 23:33:31 +00002582 // Other cases are autogenerated.
2583 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00002584 }
2585 case ISD::ADD: {
2586 short Imm;
2587 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
2588 isIntS16Immediate(N->getOperand(1), Imm))
2589 return getFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
2590
2591 break;
2592 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00002593 case ISD::SHL: {
2594 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002595 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002596 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002597 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002598 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002599 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002600 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002601
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002602 // Other cases are autogenerated.
2603 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002604 }
2605 case ISD::SRL: {
2606 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00002607 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00002608 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002609 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00002610 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
Craig Topper481fb282014-04-27 19:21:11 +00002611 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00002612 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002613
Nate Begeman9f3c26c2005-10-19 18:42:01 +00002614 // Other cases are autogenerated.
2615 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00002616 }
Hal Finkel940ab932014-02-28 00:27:01 +00002617 // FIXME: Remove this once the ANDI glue bug is fixed:
2618 case PPCISD::ANDIo_1_EQ_BIT:
2619 case PPCISD::ANDIo_1_GT_BIT: {
2620 if (!ANDIGlueBug)
2621 break;
2622
2623 EVT InVT = N->getOperand(0).getValueType();
2624 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
2625 "Invalid input type for ANDIo_1_EQ_BIT");
2626
2627 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
2628 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
2629 N->getOperand(0),
2630 CurDAG->getTargetConstant(1, InVT)), 0);
2631 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2632 SDValue SRIdxVal =
2633 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
2634 PPC::sub_eq : PPC::sub_gt, MVT::i32);
2635
2636 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
2637 CR0Reg, SRIdxVal,
2638 SDValue(AndI.getNode(), 1) /* glue */);
2639 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00002640 case ISD::SELECT_CC: {
2641 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Roman Divacky254f8212011-06-20 15:28:39 +00002642 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
2643 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00002644
Hal Finkel940ab932014-02-28 00:27:01 +00002645 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00002646 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002647 N->getOperand(0).getValueType() == MVT::i1)
2648 break;
2649
Chris Lattner97b3da12006-06-27 00:04:13 +00002650 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00002651 if (!isPPC64)
2652 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2653 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
2654 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
2655 if (N1C->isNullValue() && N3C->isNullValue() &&
2656 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
2657 // FIXME: Implement this optzn for PPC64.
2658 N->getValueType(0) == MVT::i32) {
2659 SDNode *Tmp =
2660 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
2661 N->getOperand(0), getI32Imm(~0U));
2662 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
2663 SDValue(Tmp, 0), N->getOperand(0),
2664 SDValue(Tmp, 1));
2665 }
Chris Lattner9b577f12005-08-26 21:23:58 +00002666
Dale Johannesenab8e4422009-02-06 19:16:40 +00002667 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002668
2669 if (N->getValueType(0) == MVT::i1) {
2670 // An i1 select is: (c & t) | (!c & f).
2671 bool Inv;
2672 unsigned Idx = getCRIdxForSetCC(CC, Inv);
2673
2674 unsigned SRI;
2675 switch (Idx) {
2676 default: llvm_unreachable("Invalid CC index");
2677 case 0: SRI = PPC::sub_lt; break;
2678 case 1: SRI = PPC::sub_gt; break;
2679 case 2: SRI = PPC::sub_eq; break;
2680 case 3: SRI = PPC::sub_un; break;
2681 }
2682
2683 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
2684
2685 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
2686 CCBit, CCBit), 0);
2687 SDValue C = Inv ? NotCCBit : CCBit,
2688 NotC = Inv ? CCBit : NotCCBit;
2689
2690 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2691 C, N->getOperand(2)), 0);
2692 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
2693 NotC, N->getOperand(3)), 0);
2694
2695 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
2696 }
2697
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002698 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00002699
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002700 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00002701 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00002702 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00002703 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00002704 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00002705 else if (N->getValueType(0) == MVT::f32)
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00002706 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00002707 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00002708 if (PPCSubTarget->hasVSX())
2709 SelectCCOp = PPC::SELECT_CC_VSFRC;
2710 else
2711 SelectCCOp = PPC::SELECT_CC_F8;
Bill Schmidt61e65232014-10-22 13:13:40 +00002712 else if (N->getValueType(0) == MVT::v2f64 ||
2713 N->getValueType(0) == MVT::v2i64)
2714 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00002715 else
2716 SelectCCOp = PPC::SELECT_CC_VRRC;
2717
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002718 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Evan Chengc3acfc02006-08-27 08:14:06 +00002719 getI32Imm(BROpc) };
Craig Topper481fb282014-04-27 19:21:11 +00002720 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
Chris Lattnerbec817c2005-08-26 18:46:49 +00002721 }
Hal Finkel732f0f72014-03-26 12:49:28 +00002722 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002723 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00002724 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002725 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
Hal Finkel732f0f72014-03-26 12:49:28 +00002726 }
2727
2728 break;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002729 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00002730 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002731 N->getValueType(0) == MVT::v2i64)) {
2732 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
2733
2734 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
2735 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
2736 unsigned DM[2];
2737
2738 for (int i = 0; i < 2; ++i)
2739 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
2740 DM[i] = 0;
2741 else
2742 DM[i] = 1;
2743
Bill Schmidt30144352014-12-09 16:52:29 +00002744 // For little endian, we must swap the input operands and adjust
2745 // the mask elements (reverse and invert them).
2746 if (PPCSubTarget->isLittleEndian()) {
2747 std::swap(Op1, Op2);
2748 unsigned tmp = DM[0];
2749 DM[0] = 1 - DM[1];
2750 DM[1] = 1 - tmp;
2751 }
2752
Hal Finkel2583b062014-03-28 20:24:55 +00002753 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002754
2755 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
2756 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
2757 isa<LoadSDNode>(Op1.getOperand(0))) {
2758 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
2759 SDValue Base, Offset;
2760
2761 if (LD->isUnindexed() &&
2762 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
2763 SDValue Chain = LD->getChain();
2764 SDValue Ops[] = { Base, Offset, Chain };
2765 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
Craig Topper481fb282014-04-27 19:21:11 +00002766 N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002767 }
2768 }
2769
2770 SDValue Ops[] = { Op1, Op2, DMV };
Craig Topper481fb282014-04-27 19:21:11 +00002771 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00002772 }
2773
2774 break;
Hal Finkel25c19922013-05-15 21:37:41 +00002775 case PPCISD::BDNZ:
2776 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00002777 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00002778 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
2779 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
2780 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
2781 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
Craig Topper481fb282014-04-27 19:21:11 +00002782 MVT::Other, Ops);
Hal Finkel25c19922013-05-15 21:37:41 +00002783 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002784 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00002785 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002786 // Op #1 is the PPC::PRED_* number.
2787 // Op #2 is the CR#
2788 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00002789 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00002790 // Prevent PPC::PRED_* from being selected into LI.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002791 SDValue Pred =
Dan Gohmaneffb8942008-09-12 16:56:44 +00002792 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002793 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002794 N->getOperand(0), N->getOperand(4) };
Craig Topper481fb282014-04-27 19:21:11 +00002795 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattnerbe9377a2006-11-17 22:37:34 +00002796 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00002797 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002798 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00002799 unsigned PCC = getPredicateForSetCC(CC);
2800
2801 if (N->getOperand(2).getValueType() == MVT::i1) {
2802 unsigned Opc;
2803 bool Swap;
2804 switch (PCC) {
2805 default: llvm_unreachable("Unexpected Boolean-operand predicate");
2806 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
2807 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
2808 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
2809 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
2810 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
2811 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
2812 }
2813
2814 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
2815 N->getOperand(Swap ? 3 : 2),
2816 N->getOperand(Swap ? 2 : 3)), 0);
2817 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
2818 BitComp, N->getOperand(4), N->getOperand(0));
2819 }
2820
Dale Johannesenab8e4422009-02-06 19:16:40 +00002821 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00002822 SDValue Ops[] = { getI32Imm(PCC), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00002823 N->getOperand(4), N->getOperand(0) };
Craig Topper481fb282014-04-27 19:21:11 +00002824 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002825 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002826 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00002827 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002828 SDValue Chain = N->getOperand(0);
2829 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00002830 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00002831 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00002832 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00002833 Chain), 0);
Roman Divackya4a59ae2011-06-03 15:47:49 +00002834 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00002835 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002836 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00002837 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
2838 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00002839 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
2840 SDValue GA = N->getOperand(0);
2841 return CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
2842 N->getOperand(1));
Justin Hibbits3476db42014-08-28 04:40:55 +00002843 }
Bill Schmidt34627e32012-11-27 17:35:46 +00002844
Bill Schmidt27917782013-02-21 17:12:27 +00002845 // For medium and large code model, we generate two instructions as
2846 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002847 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00002848 CodeModel::Model CModel = TM.getCodeModel();
2849 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002850 break;
2851
Bill Schmidt5d82f092014-06-16 21:36:02 +00002852 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
2853 // If it is an externally defined symbol, a symbol with common linkage,
2854 // a non-local function address, or a jump table address, or if we are
2855 // generating code for large code model, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00002856 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
2857 // Otherwise we generate:
2858 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
2859 SDValue GA = N->getOperand(0);
2860 SDValue TOCbase = N->getOperand(1);
2861 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
2862 TOCbase, GA);
2863
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00002864 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
2865 CModel == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00002866 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
2867 SDValue(Tmp, 0));
2868
2869 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
2870 const GlobalValue *GValue = G->getGlobal();
Bill Schmidt5d82f092014-06-16 21:36:02 +00002871 if ((GValue->getType()->getElementType()->isFunctionTy() &&
2872 (GValue->isDeclaration() || GValue->isWeakForLinker())) ||
Rafael Espindola04902862014-05-29 15:41:38 +00002873 GValue->isDeclaration() || GValue->hasCommonLinkage() ||
2874 GValue->hasAvailableExternallyLinkage())
Bill Schmidt34627e32012-11-27 17:35:46 +00002875 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
2876 SDValue(Tmp, 0));
2877 }
2878
2879 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
2880 SDValue(Tmp, 0), GA);
2881 }
Hal Finkel7c8ae532014-07-25 17:47:22 +00002882 case PPCISD::PPC32_PICGOT: {
2883 // Generate a PIC-safe GOT reference.
2884 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
2885 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
2886 return CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT, PPCLowering->getPointerTy(), MVT::i32);
2887 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002888 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002889 // This expands into one of three sequences, depending on whether
2890 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00002891 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
2892 isa<ConstantSDNode>(N->getOperand(1)) &&
2893 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002894
2895 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00002896 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002897 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00002898 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002899
Bill Schmidt51e79512013-02-20 15:50:31 +00002900 if (EltSize == 1) {
2901 Opc1 = PPC::VSPLTISB;
2902 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002903 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002904 VT = MVT::v16i8;
2905 } else if (EltSize == 2) {
2906 Opc1 = PPC::VSPLTISH;
2907 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002908 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002909 VT = MVT::v8i16;
2910 } else {
2911 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
2912 Opc1 = PPC::VSPLTISW;
2913 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002914 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00002915 VT = MVT::v4i32;
2916 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00002917
2918 if ((Elt & 1) == 0) {
2919 // Elt is even, in the range [-32,-18] + [16,30].
2920 //
2921 // Convert: VADD_SPLAT elt, size
2922 // Into: tmp = VSPLTIS[BHW] elt
2923 // VADDU[BHW]M tmp, tmp
2924 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
2925 SDValue EltVal = getI32Imm(Elt >> 1);
2926 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2927 SDValue TmpVal = SDValue(Tmp, 0);
2928 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
2929
2930 } else if (Elt > 0) {
2931 // Elt is odd and positive, in the range [17,31].
2932 //
2933 // Convert: VADD_SPLAT elt, size
2934 // Into: tmp1 = VSPLTIS[BHW] elt-16
2935 // tmp2 = VSPLTIS[BHW] -16
2936 // VSUBU[BHW]M tmp1, tmp2
2937 SDValue EltVal = getI32Imm(Elt - 16);
2938 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2939 EltVal = getI32Imm(-16);
2940 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2941 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
2942 SDValue(Tmp2, 0));
2943
2944 } else {
2945 // Elt is odd and negative, in the range [-31,-17].
2946 //
2947 // Convert: VADD_SPLAT elt, size
2948 // Into: tmp1 = VSPLTIS[BHW] elt+16
2949 // tmp2 = VSPLTIS[BHW] -16
2950 // VADDU[BHW]M tmp1, tmp2
2951 SDValue EltVal = getI32Imm(Elt + 16);
2952 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2953 EltVal = getI32Imm(-16);
2954 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
2955 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
2956 SDValue(Tmp2, 0));
2957 }
Bill Schmidt51e79512013-02-20 15:50:31 +00002958 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00002959 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002960
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002961 return SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00002962}
2963
Hal Finkel4edc66b2015-01-03 01:16:37 +00002964// If the target supports the cmpb instruction, do the idiom recognition here.
2965// We don't do this as a DAG combine because we don't want to do it as nodes
2966// are being combined (because we might miss part of the eventual idiom). We
2967// don't want to do it during instruction selection because we want to reuse
2968// the logic for lowering the masking operations already part of the
2969// instruction selector.
2970SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
2971 SDLoc dl(N);
2972
2973 assert(N->getOpcode() == ISD::OR &&
2974 "Only OR nodes are supported for CMPB");
2975
2976 SDValue Res;
2977 if (!PPCSubTarget->hasCMPB())
2978 return Res;
2979
2980 if (N->getValueType(0) != MVT::i32 &&
2981 N->getValueType(0) != MVT::i64)
2982 return Res;
2983
2984 EVT VT = N->getValueType(0);
2985
2986 SDValue RHS, LHS;
2987 bool BytesFound[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
2988 uint64_t Mask = 0, Alt = 0;
2989
2990 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
2991 uint64_t &Mask, uint64_t &Alt,
2992 SDValue &LHS, SDValue &RHS) {
2993 if (O.getOpcode() != ISD::SELECT_CC)
2994 return false;
2995 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
2996
2997 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
2998 !isa<ConstantSDNode>(O.getOperand(3)))
2999 return false;
3000
3001 uint64_t PM = O.getConstantOperandVal(2);
3002 uint64_t PAlt = O.getConstantOperandVal(3);
3003 for (b = 0; b < 8; ++b) {
3004 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3005 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3006 break;
3007 }
3008
3009 if (b == 8)
3010 return false;
3011 Mask |= PM;
3012 Alt |= PAlt;
3013
3014 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3015 O.getConstantOperandVal(1) != 0) {
3016 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3017 if (Op0.getOpcode() == ISD::TRUNCATE)
3018 Op0 = Op0.getOperand(0);
3019 if (Op1.getOpcode() == ISD::TRUNCATE)
3020 Op1 = Op1.getOperand(0);
3021
3022 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3023 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3024 isa<ConstantSDNode>(Op0.getOperand(1))) {
3025
3026 unsigned Bits = Op0.getValueType().getSizeInBits();
3027 if (b != Bits/8-1)
3028 return false;
3029 if (Op0.getConstantOperandVal(1) != Bits-8)
3030 return false;
3031
3032 LHS = Op0.getOperand(0);
3033 RHS = Op1.getOperand(0);
3034 return true;
3035 }
3036
3037 // When we have small integers (i16 to be specific), the form present
3038 // post-legalization uses SETULT in the SELECT_CC for the
3039 // higher-order byte, depending on the fact that the
3040 // even-higher-order bytes are known to all be zero, for example:
3041 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3042 // (so when the second byte is the same, because all higher-order
3043 // bits from bytes 3 and 4 are known to be zero, the result of the
3044 // xor can be at most 255)
3045 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3046 isa<ConstantSDNode>(O.getOperand(1))) {
3047
3048 uint64_t ULim = O.getConstantOperandVal(1);
3049 if (ULim != (UINT64_C(1) << b*8))
3050 return false;
3051
3052 // Now we need to make sure that the upper bytes are known to be
3053 // zero.
3054 unsigned Bits = Op0.getValueType().getSizeInBits();
3055 if (!CurDAG->MaskedValueIsZero(Op0,
3056 APInt::getHighBitsSet(Bits, Bits - (b+1)*8)))
3057 return false;
3058
3059 LHS = Op0.getOperand(0);
3060 RHS = Op0.getOperand(1);
3061 return true;
3062 }
3063
3064 return false;
3065 }
3066
3067 if (CC != ISD::SETEQ)
3068 return false;
3069
3070 SDValue Op = O.getOperand(0);
3071 if (Op.getOpcode() == ISD::AND) {
3072 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3073 return false;
3074 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3075 return false;
3076
3077 SDValue XOR = Op.getOperand(0);
3078 if (XOR.getOpcode() == ISD::TRUNCATE)
3079 XOR = XOR.getOperand(0);
3080 if (XOR.getOpcode() != ISD::XOR)
3081 return false;
3082
3083 LHS = XOR.getOperand(0);
3084 RHS = XOR.getOperand(1);
3085 return true;
3086 } else if (Op.getOpcode() == ISD::SRL) {
3087 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3088 return false;
3089 unsigned Bits = Op.getValueType().getSizeInBits();
3090 if (b != Bits/8-1)
3091 return false;
3092 if (Op.getConstantOperandVal(1) != Bits-8)
3093 return false;
3094
3095 SDValue XOR = Op.getOperand(0);
3096 if (XOR.getOpcode() == ISD::TRUNCATE)
3097 XOR = XOR.getOperand(0);
3098 if (XOR.getOpcode() != ISD::XOR)
3099 return false;
3100
3101 LHS = XOR.getOperand(0);
3102 RHS = XOR.getOperand(1);
3103 return true;
3104 }
3105
3106 return false;
3107 };
3108
3109 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3110 while (!Queue.empty()) {
3111 SDValue V = Queue.pop_back_val();
3112
3113 for (const SDValue &O : V.getNode()->ops()) {
3114 unsigned b;
3115 uint64_t M = 0, A = 0;
3116 SDValue OLHS, ORHS;
3117 if (O.getOpcode() == ISD::OR) {
3118 Queue.push_back(O);
3119 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3120 if (!LHS) {
3121 LHS = OLHS;
3122 RHS = ORHS;
3123 BytesFound[b] = true;
3124 Mask |= M;
3125 Alt |= A;
3126 } else if ((LHS == ORHS && RHS == OLHS) ||
3127 (RHS == ORHS && LHS == OLHS)) {
3128 BytesFound[b] = true;
3129 Mask |= M;
3130 Alt |= A;
3131 } else {
3132 return Res;
3133 }
3134 } else {
3135 return Res;
3136 }
3137 }
3138 }
3139
3140 unsigned LastB = 0, BCnt = 0;
3141 for (unsigned i = 0; i < 8; ++i)
3142 if (BytesFound[LastB]) {
3143 ++BCnt;
3144 LastB = i;
3145 }
3146
3147 if (!LastB || BCnt < 2)
3148 return Res;
3149
3150 // Because we'll be zero-extending the output anyway if don't have a specific
3151 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3152 if (LHS.getValueType() != VT) {
3153 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3154 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3155 }
3156
3157 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3158
3159 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3160 if (NonTrivialMask && !Alt) {
3161 // Res = Mask & CMPB
3162 Res = CurDAG->getNode(ISD::AND, dl, VT, Res, CurDAG->getConstant(Mask, VT));
3163 } else if (Alt) {
3164 // Res = (CMPB & Mask) | (~CMPB & Alt)
3165 // Which, as suggested here:
3166 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3167 // can be written as:
3168 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3169 // useful because the (Alt ^ Mask) can be pre-computed.
3170 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3171 CurDAG->getConstant(Mask ^ Alt, VT));
3172 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res, CurDAG->getConstant(Alt, VT));
3173 }
3174
3175 return Res;
3176}
3177
Hal Finkel200d2ad2015-01-05 21:10:24 +00003178// When CR bit registers are enabled, an extension of an i1 variable to a i32
3179// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3180// involves constant materialization of a 0 or a 1 or both. If the result of
3181// the extension is then operated upon by some operator that can be constant
3182// folded with a constant 0 or 1, and that constant can be materialized using
3183// only one instruction (like a zero or one), then we should fold in those
3184// operations with the select.
3185void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3186 if (!PPCSubTarget->useCRBits())
3187 return;
3188
3189 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3190 N->getOpcode() != ISD::SIGN_EXTEND &&
3191 N->getOpcode() != ISD::ANY_EXTEND)
3192 return;
3193
3194 if (N->getOperand(0).getValueType() != MVT::i1)
3195 return;
3196
3197 if (!N->hasOneUse())
3198 return;
3199
3200 SDLoc dl(N);
3201 EVT VT = N->getValueType(0);
3202 SDValue Cond = N->getOperand(0);
3203 SDValue ConstTrue =
3204 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, VT);
3205 SDValue ConstFalse = CurDAG->getConstant(0, VT);
3206
3207 do {
3208 SDNode *User = *N->use_begin();
3209 if (User->getNumOperands() != 2)
3210 break;
3211
3212 auto TryFold = [this, N, User](SDValue Val) {
3213 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3214 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3215 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3216
3217 return CurDAG->FoldConstantArithmetic(User->getOpcode(),
3218 User->getValueType(0),
3219 O0.getNode(), O1.getNode());
3220 };
3221
3222 SDValue TrueRes = TryFold(ConstTrue);
3223 if (!TrueRes)
3224 break;
3225 SDValue FalseRes = TryFold(ConstFalse);
3226 if (!FalseRes)
3227 break;
3228
3229 // For us to materialize these using one instruction, we must be able to
3230 // represent them as signed 16-bit integers.
3231 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
3232 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
3233 if (!isInt<16>(True) || !isInt<16>(False))
3234 break;
3235
3236 // We can replace User with a new SELECT node, and try again to see if we
3237 // can fold the select with its user.
3238 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
3239 N = User;
3240 ConstTrue = TrueRes;
3241 ConstFalse = FalseRes;
3242 } while (N->hasOneUse());
3243}
3244
Hal Finkel4edc66b2015-01-03 01:16:37 +00003245void PPCDAGToDAGISel::PreprocessISelDAG() {
3246 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3247 ++Position;
3248
3249 bool MadeChange = false;
3250 while (Position != CurDAG->allnodes_begin()) {
3251 SDNode *N = --Position;
3252 if (N->use_empty())
3253 continue;
3254
3255 SDValue Res;
3256 switch (N->getOpcode()) {
3257 default: break;
3258 case ISD::OR:
3259 Res = combineToCMPB(N);
3260 break;
3261 }
3262
Hal Finkel200d2ad2015-01-05 21:10:24 +00003263 if (!Res)
3264 foldBoolExts(Res, N);
3265
Hal Finkel4edc66b2015-01-03 01:16:37 +00003266 if (Res) {
3267 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
3268 DEBUG(N->dump(CurDAG));
3269 DEBUG(dbgs() << "\nNew: ");
3270 DEBUG(Res.getNode()->dump(CurDAG));
3271 DEBUG(dbgs() << "\n");
3272
3273 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
3274 MadeChange = true;
3275 }
3276 }
3277
3278 if (MadeChange)
3279 CurDAG->RemoveDeadNodes();
3280}
3281
Hal Finkel860fa902014-01-02 22:09:39 +00003282/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00003283/// on the DAG representation.
3284void PPCDAGToDAGISel::PostprocessISelDAG() {
3285
3286 // Skip peepholes at -O0.
3287 if (TM.getOptLevel() == CodeGenOpt::None)
3288 return;
3289
Hal Finkel940ab932014-02-28 00:27:01 +00003290 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00003291 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00003292 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00003293}
3294
Hal Finkelb9989152014-02-28 06:11:16 +00003295// Check if all users of this node will become isel where the second operand
3296// is the constant zero. If this is so, and if we can negate the condition,
3297// then we can flip the true and false operands. This will allow the zero to
3298// be folded with the isel so that we don't need to materialize a register
3299// containing zero.
3300bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
3301 // If we're not using isel, then this does not matter.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003302 if (!PPCSubTarget->hasISEL())
Hal Finkelb9989152014-02-28 06:11:16 +00003303 return false;
3304
3305 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3306 UI != UE; ++UI) {
3307 SDNode *User = *UI;
3308 if (!User->isMachineOpcode())
3309 return false;
3310 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
3311 User->getMachineOpcode() != PPC::SELECT_I8)
3312 return false;
3313
3314 SDNode *Op2 = User->getOperand(2).getNode();
3315 if (!Op2->isMachineOpcode())
3316 return false;
3317
3318 if (Op2->getMachineOpcode() != PPC::LI &&
3319 Op2->getMachineOpcode() != PPC::LI8)
3320 return false;
3321
3322 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
3323 if (!C)
3324 return false;
3325
3326 if (!C->isNullValue())
3327 return false;
3328 }
3329
3330 return true;
3331}
3332
3333void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
3334 SmallVector<SDNode *, 4> ToReplace;
3335 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3336 UI != UE; ++UI) {
3337 SDNode *User = *UI;
3338 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
3339 User->getMachineOpcode() == PPC::SELECT_I8) &&
3340 "Must have all select users");
3341 ToReplace.push_back(User);
3342 }
3343
3344 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
3345 UE = ToReplace.end(); UI != UE; ++UI) {
3346 SDNode *User = *UI;
3347 SDNode *ResNode =
3348 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
3349 User->getValueType(0), User->getOperand(0),
3350 User->getOperand(2),
3351 User->getOperand(1));
3352
3353 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3354 DEBUG(User->dump(CurDAG));
3355 DEBUG(dbgs() << "\nNew: ");
3356 DEBUG(ResNode->dump(CurDAG));
3357 DEBUG(dbgs() << "\n");
3358
3359 ReplaceUses(User, ResNode);
3360 }
3361}
3362
Eric Christopher02e18042014-05-14 00:31:15 +00003363void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00003364 bool IsModified;
3365 do {
3366 IsModified = false;
3367 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
3368 E = CurDAG->allnodes_end(); I != E; ++I) {
3369 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
3370 if (!MachineNode || MachineNode->use_empty())
3371 continue;
3372 SDNode *ResNode = MachineNode;
3373
3374 bool Op1Set = false, Op1Unset = false,
3375 Op1Not = false,
3376 Op2Set = false, Op2Unset = false,
3377 Op2Not = false;
3378
3379 unsigned Opcode = MachineNode->getMachineOpcode();
3380 switch (Opcode) {
3381 default: break;
3382 case PPC::CRAND:
3383 case PPC::CRNAND:
3384 case PPC::CROR:
3385 case PPC::CRXOR:
3386 case PPC::CRNOR:
3387 case PPC::CREQV:
3388 case PPC::CRANDC:
3389 case PPC::CRORC: {
3390 SDValue Op = MachineNode->getOperand(1);
3391 if (Op.isMachineOpcode()) {
3392 if (Op.getMachineOpcode() == PPC::CRSET)
3393 Op2Set = true;
3394 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3395 Op2Unset = true;
3396 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3397 Op.getOperand(0) == Op.getOperand(1))
3398 Op2Not = true;
3399 }
3400 } // fallthrough
3401 case PPC::BC:
3402 case PPC::BCn:
3403 case PPC::SELECT_I4:
3404 case PPC::SELECT_I8:
3405 case PPC::SELECT_F4:
3406 case PPC::SELECT_F8:
Bill Schmidt61e65232014-10-22 13:13:40 +00003407 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003408 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003409 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00003410 SDValue Op = MachineNode->getOperand(0);
3411 if (Op.isMachineOpcode()) {
3412 if (Op.getMachineOpcode() == PPC::CRSET)
3413 Op1Set = true;
3414 else if (Op.getMachineOpcode() == PPC::CRUNSET)
3415 Op1Unset = true;
3416 else if (Op.getMachineOpcode() == PPC::CRNOR &&
3417 Op.getOperand(0) == Op.getOperand(1))
3418 Op1Not = true;
3419 }
3420 }
3421 break;
3422 }
3423
Hal Finkelb9989152014-02-28 06:11:16 +00003424 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00003425 switch (Opcode) {
3426 default: break;
3427 case PPC::CRAND:
3428 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3429 // x & x = x
3430 ResNode = MachineNode->getOperand(0).getNode();
3431 else if (Op1Set)
3432 // 1 & y = y
3433 ResNode = MachineNode->getOperand(1).getNode();
3434 else if (Op2Set)
3435 // x & 1 = x
3436 ResNode = MachineNode->getOperand(0).getNode();
3437 else if (Op1Unset || Op2Unset)
3438 // x & 0 = 0 & y = 0
3439 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3440 MVT::i1);
3441 else if (Op1Not)
3442 // ~x & y = andc(y, x)
3443 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3444 MVT::i1, MachineNode->getOperand(1),
3445 MachineNode->getOperand(0).
3446 getOperand(0));
3447 else if (Op2Not)
3448 // x & ~y = andc(x, y)
3449 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3450 MVT::i1, MachineNode->getOperand(0),
3451 MachineNode->getOperand(1).
3452 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003453 else if (AllUsersSelectZero(MachineNode))
3454 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3455 MVT::i1, MachineNode->getOperand(0),
3456 MachineNode->getOperand(1)),
3457 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003458 break;
3459 case PPC::CRNAND:
3460 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3461 // nand(x, x) -> nor(x, x)
3462 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3463 MVT::i1, MachineNode->getOperand(0),
3464 MachineNode->getOperand(0));
3465 else if (Op1Set)
3466 // nand(1, y) -> nor(y, y)
3467 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3468 MVT::i1, MachineNode->getOperand(1),
3469 MachineNode->getOperand(1));
3470 else if (Op2Set)
3471 // nand(x, 1) -> nor(x, x)
3472 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3473 MVT::i1, MachineNode->getOperand(0),
3474 MachineNode->getOperand(0));
3475 else if (Op1Unset || Op2Unset)
3476 // nand(x, 0) = nand(0, y) = 1
3477 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3478 MVT::i1);
3479 else if (Op1Not)
3480 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
3481 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3482 MVT::i1, MachineNode->getOperand(0).
3483 getOperand(0),
3484 MachineNode->getOperand(1));
3485 else if (Op2Not)
3486 // nand(x, ~y) = ~x | y = orc(y, x)
3487 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3488 MVT::i1, MachineNode->getOperand(1).
3489 getOperand(0),
3490 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003491 else if (AllUsersSelectZero(MachineNode))
3492 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3493 MVT::i1, MachineNode->getOperand(0),
3494 MachineNode->getOperand(1)),
3495 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003496 break;
3497 case PPC::CROR:
3498 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3499 // x | x = x
3500 ResNode = MachineNode->getOperand(0).getNode();
3501 else if (Op1Set || Op2Set)
3502 // x | 1 = 1 | y = 1
3503 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3504 MVT::i1);
3505 else if (Op1Unset)
3506 // 0 | y = y
3507 ResNode = MachineNode->getOperand(1).getNode();
3508 else if (Op2Unset)
3509 // x | 0 = x
3510 ResNode = MachineNode->getOperand(0).getNode();
3511 else if (Op1Not)
3512 // ~x | y = orc(y, x)
3513 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3514 MVT::i1, MachineNode->getOperand(1),
3515 MachineNode->getOperand(0).
3516 getOperand(0));
3517 else if (Op2Not)
3518 // x | ~y = orc(x, y)
3519 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3520 MVT::i1, MachineNode->getOperand(0),
3521 MachineNode->getOperand(1).
3522 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003523 else if (AllUsersSelectZero(MachineNode))
3524 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3525 MVT::i1, MachineNode->getOperand(0),
3526 MachineNode->getOperand(1)),
3527 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003528 break;
3529 case PPC::CRXOR:
3530 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3531 // xor(x, x) = 0
3532 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3533 MVT::i1);
3534 else if (Op1Set)
3535 // xor(1, y) -> nor(y, y)
3536 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3537 MVT::i1, MachineNode->getOperand(1),
3538 MachineNode->getOperand(1));
3539 else if (Op2Set)
3540 // xor(x, 1) -> nor(x, x)
3541 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3542 MVT::i1, MachineNode->getOperand(0),
3543 MachineNode->getOperand(0));
3544 else if (Op1Unset)
3545 // xor(0, y) = y
3546 ResNode = MachineNode->getOperand(1).getNode();
3547 else if (Op2Unset)
3548 // xor(x, 0) = x
3549 ResNode = MachineNode->getOperand(0).getNode();
3550 else if (Op1Not)
3551 // xor(~x, y) = eqv(x, y)
3552 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3553 MVT::i1, MachineNode->getOperand(0).
3554 getOperand(0),
3555 MachineNode->getOperand(1));
3556 else if (Op2Not)
3557 // xor(x, ~y) = eqv(x, y)
3558 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3559 MVT::i1, MachineNode->getOperand(0),
3560 MachineNode->getOperand(1).
3561 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003562 else if (AllUsersSelectZero(MachineNode))
3563 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
3564 MVT::i1, MachineNode->getOperand(0),
3565 MachineNode->getOperand(1)),
3566 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003567 break;
3568 case PPC::CRNOR:
3569 if (Op1Set || Op2Set)
3570 // nor(1, y) -> 0
3571 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3572 MVT::i1);
3573 else if (Op1Unset)
3574 // nor(0, y) = ~y -> nor(y, y)
3575 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3576 MVT::i1, MachineNode->getOperand(1),
3577 MachineNode->getOperand(1));
3578 else if (Op2Unset)
3579 // nor(x, 0) = ~x
3580 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3581 MVT::i1, MachineNode->getOperand(0),
3582 MachineNode->getOperand(0));
3583 else if (Op1Not)
3584 // nor(~x, y) = andc(x, y)
3585 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3586 MVT::i1, MachineNode->getOperand(0).
3587 getOperand(0),
3588 MachineNode->getOperand(1));
3589 else if (Op2Not)
3590 // nor(x, ~y) = andc(y, x)
3591 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3592 MVT::i1, MachineNode->getOperand(1).
3593 getOperand(0),
3594 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003595 else if (AllUsersSelectZero(MachineNode))
3596 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3597 MVT::i1, MachineNode->getOperand(0),
3598 MachineNode->getOperand(1)),
3599 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003600 break;
3601 case PPC::CREQV:
3602 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3603 // eqv(x, x) = 1
3604 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3605 MVT::i1);
3606 else if (Op1Set)
3607 // eqv(1, y) = y
3608 ResNode = MachineNode->getOperand(1).getNode();
3609 else if (Op2Set)
3610 // eqv(x, 1) = x
3611 ResNode = MachineNode->getOperand(0).getNode();
3612 else if (Op1Unset)
3613 // eqv(0, y) = ~y -> nor(y, y)
3614 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3615 MVT::i1, MachineNode->getOperand(1),
3616 MachineNode->getOperand(1));
3617 else if (Op2Unset)
3618 // eqv(x, 0) = ~x
3619 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3620 MVT::i1, MachineNode->getOperand(0),
3621 MachineNode->getOperand(0));
3622 else if (Op1Not)
3623 // eqv(~x, y) = xor(x, y)
3624 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3625 MVT::i1, MachineNode->getOperand(0).
3626 getOperand(0),
3627 MachineNode->getOperand(1));
3628 else if (Op2Not)
3629 // eqv(x, ~y) = xor(x, y)
3630 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3631 MVT::i1, MachineNode->getOperand(0),
3632 MachineNode->getOperand(1).
3633 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003634 else if (AllUsersSelectZero(MachineNode))
3635 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
3636 MVT::i1, MachineNode->getOperand(0),
3637 MachineNode->getOperand(1)),
3638 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003639 break;
3640 case PPC::CRANDC:
3641 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3642 // andc(x, x) = 0
3643 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3644 MVT::i1);
3645 else if (Op1Set)
3646 // andc(1, y) = ~y
3647 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3648 MVT::i1, MachineNode->getOperand(1),
3649 MachineNode->getOperand(1));
3650 else if (Op1Unset || Op2Set)
3651 // andc(0, y) = andc(x, 1) = 0
3652 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
3653 MVT::i1);
3654 else if (Op2Unset)
3655 // andc(x, 0) = x
3656 ResNode = MachineNode->getOperand(0).getNode();
3657 else if (Op1Not)
3658 // andc(~x, y) = ~(x | y) = nor(x, y)
3659 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3660 MVT::i1, MachineNode->getOperand(0).
3661 getOperand(0),
3662 MachineNode->getOperand(1));
3663 else if (Op2Not)
3664 // andc(x, ~y) = x & y
3665 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
3666 MVT::i1, MachineNode->getOperand(0),
3667 MachineNode->getOperand(1).
3668 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003669 else if (AllUsersSelectZero(MachineNode))
3670 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
3671 MVT::i1, MachineNode->getOperand(1),
3672 MachineNode->getOperand(0)),
3673 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003674 break;
3675 case PPC::CRORC:
3676 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
3677 // orc(x, x) = 1
3678 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3679 MVT::i1);
3680 else if (Op1Set || Op2Unset)
3681 // orc(1, y) = orc(x, 0) = 1
3682 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
3683 MVT::i1);
3684 else if (Op2Set)
3685 // orc(x, 1) = x
3686 ResNode = MachineNode->getOperand(0).getNode();
3687 else if (Op1Unset)
3688 // orc(0, y) = ~y
3689 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
3690 MVT::i1, MachineNode->getOperand(1),
3691 MachineNode->getOperand(1));
3692 else if (Op1Not)
3693 // orc(~x, y) = ~(x & y) = nand(x, y)
3694 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
3695 MVT::i1, MachineNode->getOperand(0).
3696 getOperand(0),
3697 MachineNode->getOperand(1));
3698 else if (Op2Not)
3699 // orc(x, ~y) = x | y
3700 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
3701 MVT::i1, MachineNode->getOperand(0),
3702 MachineNode->getOperand(1).
3703 getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00003704 else if (AllUsersSelectZero(MachineNode))
3705 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
3706 MVT::i1, MachineNode->getOperand(1),
3707 MachineNode->getOperand(0)),
3708 SelectSwap = true;
Hal Finkel940ab932014-02-28 00:27:01 +00003709 break;
3710 case PPC::SELECT_I4:
3711 case PPC::SELECT_I8:
3712 case PPC::SELECT_F4:
3713 case PPC::SELECT_F8:
3714 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003715 case PPC::SELECT_VSFRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00003716 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00003717 if (Op1Set)
3718 ResNode = MachineNode->getOperand(1).getNode();
3719 else if (Op1Unset)
3720 ResNode = MachineNode->getOperand(2).getNode();
3721 else if (Op1Not)
3722 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
3723 SDLoc(MachineNode),
3724 MachineNode->getValueType(0),
3725 MachineNode->getOperand(0).
3726 getOperand(0),
3727 MachineNode->getOperand(2),
3728 MachineNode->getOperand(1));
3729 break;
3730 case PPC::BC:
3731 case PPC::BCn:
3732 if (Op1Not)
3733 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
3734 PPC::BC,
3735 SDLoc(MachineNode),
3736 MVT::Other,
3737 MachineNode->getOperand(0).
3738 getOperand(0),
3739 MachineNode->getOperand(1),
3740 MachineNode->getOperand(2));
3741 // FIXME: Handle Op1Set, Op1Unset here too.
3742 break;
3743 }
3744
Hal Finkelb9989152014-02-28 06:11:16 +00003745 // If we're inverting this node because it is used only by selects that
3746 // we'd like to swap, then swap the selects before the node replacement.
3747 if (SelectSwap)
3748 SwapAllSelectUsers(MachineNode);
3749
Hal Finkel940ab932014-02-28 00:27:01 +00003750 if (ResNode != MachineNode) {
3751 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
3752 DEBUG(MachineNode->dump(CurDAG));
3753 DEBUG(dbgs() << "\nNew: ");
3754 DEBUG(ResNode->dump(CurDAG));
3755 DEBUG(dbgs() << "\n");
3756
3757 ReplaceUses(MachineNode, ResNode);
3758 IsModified = true;
3759 }
3760 }
3761 if (IsModified)
3762 CurDAG->RemoveDeadNodes();
3763 } while (IsModified);
3764}
3765
Hal Finkel4c6658f2014-12-12 23:59:36 +00003766// Gather the set of 32-bit operations that are known to have their
3767// higher-order 32 bits zero, where ToPromote contains all such operations.
3768static bool PeepholePPC64ZExtGather(SDValue Op32,
3769 SmallPtrSetImpl<SDNode *> &ToPromote) {
3770 if (!Op32.isMachineOpcode())
3771 return false;
3772
3773 // First, check for the "frontier" instructions (those that will clear the
3774 // higher-order 32 bits.
3775
3776 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
3777 // around. If it does not, then these instructions will clear the
3778 // higher-order bits.
3779 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
3780 Op32.getMachineOpcode() == PPC::RLWNM) &&
3781 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
3782 ToPromote.insert(Op32.getNode());
3783 return true;
3784 }
3785
3786 // SLW and SRW always clear the higher-order bits.
3787 if (Op32.getMachineOpcode() == PPC::SLW ||
3788 Op32.getMachineOpcode() == PPC::SRW) {
3789 ToPromote.insert(Op32.getNode());
3790 return true;
3791 }
3792
3793 // For LI and LIS, we need the immediate to be positive (so that it is not
3794 // sign extended).
3795 if (Op32.getMachineOpcode() == PPC::LI ||
3796 Op32.getMachineOpcode() == PPC::LIS) {
3797 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
3798 return false;
3799
3800 ToPromote.insert(Op32.getNode());
3801 return true;
3802 }
3803
Hal Finkel4e2c7822015-01-05 18:09:06 +00003804 // LHBRX and LWBRX always clear the higher-order bits.
3805 if (Op32.getMachineOpcode() == PPC::LHBRX ||
3806 Op32.getMachineOpcode() == PPC::LWBRX) {
3807 ToPromote.insert(Op32.getNode());
3808 return true;
3809 }
3810
Hal Finkel49557f12015-01-05 18:52:29 +00003811 // CNTLZW always produces a 64-bit value in [0,32], and so is zero extended.
3812 if (Op32.getMachineOpcode() == PPC::CNTLZW) {
3813 ToPromote.insert(Op32.getNode());
3814 return true;
3815 }
3816
Hal Finkel4c6658f2014-12-12 23:59:36 +00003817 // Next, check for those instructions we can look through.
3818
3819 // Assuming the mask does not wrap around, then the higher-order bits are
3820 // taken directly from the first operand.
3821 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
3822 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
3823 SmallPtrSet<SDNode *, 16> ToPromote1;
3824 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3825 return false;
3826
3827 ToPromote.insert(Op32.getNode());
3828 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3829 return true;
3830 }
3831
3832 // For OR, the higher-order bits are zero if that is true for both operands.
3833 // For SELECT_I4, the same is true (but the relevant operand numbers are
3834 // shifted by 1).
3835 if (Op32.getMachineOpcode() == PPC::OR ||
3836 Op32.getMachineOpcode() == PPC::SELECT_I4) {
3837 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
3838 SmallPtrSet<SDNode *, 16> ToPromote1;
3839 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
3840 return false;
3841 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
3842 return false;
3843
3844 ToPromote.insert(Op32.getNode());
3845 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3846 return true;
3847 }
3848
3849 // For ORI and ORIS, we need the higher-order bits of the first operand to be
3850 // zero, and also for the constant to be positive (so that it is not sign
3851 // extended).
3852 if (Op32.getMachineOpcode() == PPC::ORI ||
3853 Op32.getMachineOpcode() == PPC::ORIS) {
3854 SmallPtrSet<SDNode *, 16> ToPromote1;
3855 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
3856 return false;
3857 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
3858 return false;
3859
3860 ToPromote.insert(Op32.getNode());
3861 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3862 return true;
3863 }
3864
3865 // The higher-order bits of AND are zero if that is true for at least one of
3866 // the operands.
3867 if (Op32.getMachineOpcode() == PPC::AND) {
3868 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
3869 bool Op0OK =
3870 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3871 bool Op1OK =
3872 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
3873 if (!Op0OK && !Op1OK)
3874 return false;
3875
3876 ToPromote.insert(Op32.getNode());
3877
3878 if (Op0OK)
3879 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3880
3881 if (Op1OK)
3882 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
3883
3884 return true;
3885 }
3886
3887 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
3888 // of the first operand, or if the second operand is positive (so that it is
3889 // not sign extended).
3890 if (Op32.getMachineOpcode() == PPC::ANDIo ||
3891 Op32.getMachineOpcode() == PPC::ANDISo) {
3892 SmallPtrSet<SDNode *, 16> ToPromote1;
3893 bool Op0OK =
3894 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
3895 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
3896 if (!Op0OK && !Op1OK)
3897 return false;
3898
3899 ToPromote.insert(Op32.getNode());
3900
3901 if (Op0OK)
3902 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
3903
3904 return true;
3905 }
3906
3907 return false;
3908}
3909
3910void PPCDAGToDAGISel::PeepholePPC64ZExt() {
3911 if (!PPCSubTarget->isPPC64())
3912 return;
3913
3914 // When we zero-extend from i32 to i64, we use a pattern like this:
3915 // def : Pat<(i64 (zext i32:$in)),
3916 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
3917 // 0, 32)>;
3918 // There are several 32-bit shift/rotate instructions, however, that will
3919 // clear the higher-order bits of their output, rendering the RLDICL
3920 // unnecessary. When that happens, we remove it here, and redefine the
3921 // relevant 32-bit operation to be a 64-bit operation.
3922
3923 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
3924 ++Position;
3925
3926 bool MadeChange = false;
3927 while (Position != CurDAG->allnodes_begin()) {
3928 SDNode *N = --Position;
3929 // Skip dead nodes and any non-machine opcodes.
3930 if (N->use_empty() || !N->isMachineOpcode())
3931 continue;
3932
3933 if (N->getMachineOpcode() != PPC::RLDICL)
3934 continue;
3935
3936 if (N->getConstantOperandVal(1) != 0 ||
3937 N->getConstantOperandVal(2) != 32)
3938 continue;
3939
3940 SDValue ISR = N->getOperand(0);
3941 if (!ISR.isMachineOpcode() ||
3942 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
3943 continue;
3944
3945 if (!ISR.hasOneUse())
3946 continue;
3947
3948 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
3949 continue;
3950
3951 SDValue IDef = ISR.getOperand(0);
3952 if (!IDef.isMachineOpcode() ||
3953 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
3954 continue;
3955
3956 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
3957 // can get rid of it.
3958
3959 SDValue Op32 = ISR->getOperand(1);
3960 if (!Op32.isMachineOpcode())
3961 continue;
3962
3963 // There are some 32-bit instructions that always clear the high-order 32
3964 // bits, there are also some instructions (like AND) that we can look
3965 // through.
3966 SmallPtrSet<SDNode *, 16> ToPromote;
3967 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
3968 continue;
3969
3970 // If the ToPromote set contains nodes that have uses outside of the set
3971 // (except for the original INSERT_SUBREG), then abort the transformation.
3972 bool OutsideUse = false;
3973 for (SDNode *PN : ToPromote) {
3974 for (SDNode *UN : PN->uses()) {
3975 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
3976 OutsideUse = true;
3977 break;
3978 }
3979 }
3980
3981 if (OutsideUse)
3982 break;
3983 }
3984 if (OutsideUse)
3985 continue;
3986
3987 MadeChange = true;
3988
3989 // We now know that this zero extension can be removed by promoting to
3990 // nodes in ToPromote to 64-bit operations, where for operations in the
3991 // frontier of the set, we need to insert INSERT_SUBREGs for their
3992 // operands.
3993 for (SDNode *PN : ToPromote) {
3994 unsigned NewOpcode;
3995 switch (PN->getMachineOpcode()) {
3996 default:
3997 llvm_unreachable("Don't know the 64-bit variant of this instruction");
3998 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
3999 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4000 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4001 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4002 case PPC::LI: NewOpcode = PPC::LI8; break;
4003 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004004 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4005 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004006 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004007 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4008 case PPC::OR: NewOpcode = PPC::OR8; break;
4009 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4010 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4011 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4012 case PPC::AND: NewOpcode = PPC::AND8; break;
4013 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4014 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4015 }
4016
4017 // Note: During the replacement process, the nodes will be in an
4018 // inconsistent state (some instructions will have operands with values
4019 // of the wrong type). Once done, however, everything should be right
4020 // again.
4021
4022 SmallVector<SDValue, 4> Ops;
4023 for (const SDValue &V : PN->ops()) {
4024 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4025 !isa<ConstantSDNode>(V)) {
4026 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4027 SDNode *ReplOp =
4028 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4029 ISR.getNode()->getVTList(), ReplOpOps);
4030 Ops.push_back(SDValue(ReplOp, 0));
4031 } else {
4032 Ops.push_back(V);
4033 }
4034 }
4035
4036 // Because all to-be-promoted nodes only have users that are other
4037 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4038 // the i32 result value type with i64.
4039
4040 SmallVector<EVT, 2> NewVTs;
4041 SDVTList VTs = PN->getVTList();
4042 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4043 if (VTs.VTs[i] == MVT::i32)
4044 NewVTs.push_back(MVT::i64);
4045 else
4046 NewVTs.push_back(VTs.VTs[i]);
4047
4048 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4049 DEBUG(PN->dump(CurDAG));
4050
4051 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4052
4053 DEBUG(dbgs() << "\nNew: ");
4054 DEBUG(PN->dump(CurDAG));
4055 DEBUG(dbgs() << "\n");
4056 }
4057
4058 // Now we replace the original zero extend and its associated INSERT_SUBREG
4059 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4060 // return an i64).
4061
4062 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4063 DEBUG(N->dump(CurDAG));
4064 DEBUG(dbgs() << "\nNew: ");
4065 DEBUG(Op32.getNode()->dump(CurDAG));
4066 DEBUG(dbgs() << "\n");
4067
4068 ReplaceUses(N, Op32.getNode());
4069 }
4070
4071 if (MadeChange)
4072 CurDAG->RemoveDeadNodes();
4073}
4074
Hal Finkel940ab932014-02-28 00:27:01 +00004075void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004076 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004077 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004078 return;
4079
4080 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4081 ++Position;
4082
4083 while (Position != CurDAG->allnodes_begin()) {
4084 SDNode *N = --Position;
4085 // Skip dead nodes and any non-machine opcodes.
4086 if (N->use_empty() || !N->isMachineOpcode())
4087 continue;
4088
4089 unsigned FirstOp;
4090 unsigned StorageOpcode = N->getMachineOpcode();
4091
4092 switch (StorageOpcode) {
4093 default: continue;
4094
4095 case PPC::LBZ:
4096 case PPC::LBZ8:
4097 case PPC::LD:
4098 case PPC::LFD:
4099 case PPC::LFS:
4100 case PPC::LHA:
4101 case PPC::LHA8:
4102 case PPC::LHZ:
4103 case PPC::LHZ8:
4104 case PPC::LWA:
4105 case PPC::LWZ:
4106 case PPC::LWZ8:
4107 FirstOp = 0;
4108 break;
4109
4110 case PPC::STB:
4111 case PPC::STB8:
4112 case PPC::STD:
4113 case PPC::STFD:
4114 case PPC::STFS:
4115 case PPC::STH:
4116 case PPC::STH8:
4117 case PPC::STW:
4118 case PPC::STW8:
4119 FirstOp = 1;
4120 break;
4121 }
4122
4123 // If this is a load or store with a zero offset, we may be able to
4124 // fold an add-immediate into the memory operation.
4125 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
4126 N->getConstantOperandVal(FirstOp) != 0)
4127 continue;
4128
4129 SDValue Base = N->getOperand(FirstOp + 1);
4130 if (!Base.isMachineOpcode())
4131 continue;
4132
4133 unsigned Flags = 0;
4134 bool ReplaceFlags = true;
4135
4136 // When the feeding operation is an add-immediate of some sort,
4137 // determine whether we need to add relocation information to the
4138 // target flags on the immediate operand when we fold it into the
4139 // load instruction.
4140 //
4141 // For something like ADDItocL, the relocation information is
4142 // inferred from the opcode; when we process it in the AsmPrinter,
4143 // we add the necessary relocation there. A load, though, can receive
4144 // relocation from various flavors of ADDIxxx, so we need to carry
4145 // the relocation information in the target flags.
4146 switch (Base.getMachineOpcode()) {
4147 default: continue;
4148
4149 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004150 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004151 // In some cases (such as TLS) the relocation information
4152 // is already in place on the operand, so copying the operand
4153 // is sufficient.
4154 ReplaceFlags = false;
4155 // For these cases, the immediate may not be divisible by 4, in
4156 // which case the fold is illegal for DS-form instructions. (The
4157 // other cases provide aligned addresses and are always safe.)
4158 if ((StorageOpcode == PPC::LWA ||
4159 StorageOpcode == PPC::LD ||
4160 StorageOpcode == PPC::STD) &&
4161 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4162 Base.getConstantOperandVal(1) % 4 != 0))
4163 continue;
4164 break;
4165 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004166 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004167 break;
4168 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004169 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004170 break;
4171 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004172 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004173 break;
4174 }
4175
4176 // We found an opportunity. Reverse the operands from the add
4177 // immediate and substitute them into the load or store. If
4178 // needed, update the target flags for the immediate operand to
4179 // reflect the necessary relocation information.
4180 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
4181 DEBUG(Base->dump(CurDAG));
4182 DEBUG(dbgs() << "\nN: ");
4183 DEBUG(N->dump(CurDAG));
4184 DEBUG(dbgs() << "\n");
4185
4186 SDValue ImmOpnd = Base.getOperand(1);
4187
4188 // If the relocation information isn't already present on the
4189 // immediate operand, add it now.
4190 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004191 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004192 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004193 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00004194 // We can't perform this optimization for data whose alignment
4195 // is insufficient for the instruction encoding.
4196 if (GV->getAlignment() < 4 &&
4197 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
4198 StorageOpcode == PPC::LWA)) {
4199 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
4200 continue;
4201 }
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004202 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00004203 } else if (ConstantPoolSDNode *CP =
4204 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00004205 const Constant *C = CP->getConstVal();
4206 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
4207 CP->getAlignment(),
4208 0, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004209 }
4210 }
4211
4212 if (FirstOp == 1) // Store
4213 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
4214 Base.getOperand(0), N->getOperand(3));
4215 else // Load
4216 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
4217 N->getOperand(2));
4218
4219 // The add-immediate may now be dead, in which case remove it.
4220 if (Base.getNode()->use_empty())
4221 CurDAG->RemoveDeadNode(Base.getNode());
4222 }
4223}
Chris Lattner43ff01e2005-08-17 19:33:03 +00004224
Chris Lattnerb055c872006-06-10 01:15:02 +00004225
Andrew Trickc416ba62010-12-24 04:28:06 +00004226/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00004227/// PowerPC-specific DAG, ready for instruction scheduling.
4228///
Evan Cheng2dd2c652006-03-13 23:20:37 +00004229FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00004230 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004231}
4232
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004233static void initializePassOnce(PassRegistry &Registry) {
4234 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
Craig Topper062a2ba2014-04-25 05:30:21 +00004235 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID,
4236 nullptr, false, false);
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +00004237 Registry.registerPass(*PI, true);
4238}
4239
4240void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
4241 CALL_ONCE_INITIALIZATION(initializePassOnce);
4242}
4243