blob: 3aaf7ef2c2a0293db4d7c80d846520b201dcb3ce [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
Eugene Zelenko8187c192017-01-13 00:58:58 +000015#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000017#include "PPC.h"
18#include "PPCISelLowering.h"
Hal Finkel3ee2af72014-07-18 23:29:49 +000019#include "PPCMachineFunctionInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000020#include "PPCSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "PPCTargetMachine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/ADT/Statistic.h"
Hal Finkel65539e32015-12-12 00:32:00 +000028#include "llvm/Analysis/BranchProbabilityInfo.h"
29#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000030#include "llvm/CodeGen/ISDOpcodes.h"
31#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner45640392005-08-19 22:38:53 +000032#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000035#include "llvm/CodeGen/MachineValueType.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000036#include "llvm/CodeGen/SelectionDAG.h"
37#include "llvm/CodeGen/SelectionDAGISel.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000038#include "llvm/CodeGen/SelectionDAGNodes.h"
39#include "llvm/CodeGen/ValueTypes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Function.h"
43#include "llvm/IR/GlobalValue.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000044#include "llvm/IR/InlineAsm.h"
45#include "llvm/IR/InstrTypes.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000046#include "llvm/IR/Module.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000047#include "llvm/Support/Casting.h"
48#include "llvm/Support/CodeGen.h"
Hal Finkel940ab932014-02-28 00:27:01 +000049#include "llvm/Support/CommandLine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000050#include "llvm/Support/Compiler.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000051#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000052#include "llvm/Support/ErrorHandling.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000053#include "llvm/Support/KnownBits.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000055#include "llvm/Support/raw_ostream.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000056#include "llvm/Target/TargetInstrInfo.h"
57#include "llvm/Target/TargetRegisterInfo.h"
58#include <algorithm>
59#include <cassert>
60#include <cstdint>
61#include <iterator>
62#include <limits>
63#include <memory>
64#include <new>
65#include <tuple>
66#include <utility>
67
Chris Lattner43ff01e2005-08-17 19:33:03 +000068using namespace llvm;
69
Chandler Carruth84e68b22014-04-22 02:41:26 +000070#define DEBUG_TYPE "ppc-codegen"
71
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +000072STATISTIC(NumSextSetcc,
73 "Number of (sext(setcc)) nodes expanded into GPR sequence.");
74STATISTIC(NumZextSetcc,
75 "Number of (zext(setcc)) nodes expanded into GPR sequence.");
76STATISTIC(SignExtensionsAdded,
77 "Number of sign extensions for compare inputs added.");
78STATISTIC(ZeroExtensionsAdded,
79 "Number of zero extensions for compare inputs added.");
Nemanja Ivanovice597bd82017-05-31 05:40:25 +000080STATISTIC(NumLogicOpsOnComparison,
81 "Number of logical ops on i1 values calculated in GPR.");
82STATISTIC(OmittedForNonExtendUses,
83 "Number of compares not eliminated as they have non-extending uses.");
84
Hal Finkel940ab932014-02-28 00:27:01 +000085// FIXME: Remove this once the bug has been fixed!
86cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
87cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
88
Benjamin Kramer970eac42015-02-06 17:51:54 +000089static cl::opt<bool>
90 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
91 cl::desc("use aggressive ppc isel for bit permutations"),
92 cl::Hidden);
93static cl::opt<bool> BPermRewriterNoMasking(
94 "ppc-bit-perm-rewriter-stress-rotates",
95 cl::desc("stress rotate selection in aggressive ppc isel for "
96 "bit permutations"),
97 cl::Hidden);
Hal Finkelc58ce412015-01-01 02:53:29 +000098
Hal Finkel65539e32015-12-12 00:32:00 +000099static cl::opt<bool> EnableBranchHint(
100 "ppc-use-branch-hint", cl::init(true),
101 cl::desc("Enable static hinting of branches on ppc"),
102 cl::Hidden);
103
Chris Lattner43ff01e2005-08-17 19:33:03 +0000104namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000105
Chris Lattner43ff01e2005-08-17 19:33:03 +0000106 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +0000107 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +0000108 /// instructions for SelectionDAG operations.
109 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000110 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +0000111 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000112 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +0000113 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +0000114 unsigned GlobalBaseReg;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000115
Chris Lattner43ff01e2005-08-17 19:33:03 +0000116 public:
Hiroshi Inoue51020282017-06-27 04:52:17 +0000117 explicit PPCDAGToDAGISel(PPCTargetMachine &tm, CodeGenOpt::Level OptLevel)
118 : SelectionDAGISel(tm, OptLevel), TM(tm) {}
Andrew Trickc416ba62010-12-24 04:28:06 +0000119
Craig Topper0d3fa922014-04-29 07:57:37 +0000120 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +0000121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +0000123 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
124 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +0000125 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +0000126
Eric Christopher1b8e7632014-05-22 01:07:24 +0000127 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +0000128 InsertVRSaveCode(MF);
129
Chris Lattner1678a6c2006-03-16 18:25:23 +0000130 return true;
Chris Lattner45640392005-08-19 22:38:53 +0000131 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000132
Hal Finkel4edc66b2015-01-03 01:16:37 +0000133 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000134 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +0000135
Chris Lattner43ff01e2005-08-17 19:33:03 +0000136 /// getI32Imm - Return a target constant with the specified value, of type
137 /// i32.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000138 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000139 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000140 }
Chris Lattner45640392005-08-19 22:38:53 +0000141
Chris Lattner97b3da12006-06-27 00:04:13 +0000142 /// getI64Imm - Return a target constant with the specified value, of type
143 /// i64.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000144 inline SDValue getI64Imm(uint64_t Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000145 return CurDAG->getTargetConstant(Imm, dl, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000146 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000147
Chris Lattner97b3da12006-06-27 00:04:13 +0000148 /// getSmallIPtrImm - Return a target constant of pointer type.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000149 inline SDValue getSmallIPtrImm(unsigned Imm, const SDLoc &dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000150 return CurDAG->getTargetConstant(
151 Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner97b3da12006-06-27 00:04:13 +0000152 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000153
Nate Begemand31efd12006-09-22 05:01:56 +0000154 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
155 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000156 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000157 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000158
Chris Lattner45640392005-08-19 22:38:53 +0000159 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
160 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000161 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000162
Justin Bognerdc8af062016-05-20 21:43:23 +0000163 void selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
Hal Finkelb5e9b042014-12-11 22:51:06 +0000164
Chris Lattner43ff01e2005-08-17 19:33:03 +0000165 // Select - Convert the specified operand from a target-independent to a
166 // target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +0000167 void Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000168
Justin Bognerdc8af062016-05-20 21:43:23 +0000169 bool tryBitfieldInsert(SDNode *N);
170 bool tryBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000171
Chris Lattner2a1823d2005-08-21 18:50:37 +0000172 /// SelectCC - Select a comparison of the specified values with the
173 /// specified condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000174 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
175 const SDLoc &dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000176
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000177 /// SelectAddrImm - Returns true if the address N can be represented by
178 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000179 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000180 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000181 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000182 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000183
Chris Lattner6f5840c2006-11-16 00:41:37 +0000184 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000185 /// immediate field. Note that the operand at this point is already the
186 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000187 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000188 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000189 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000190 Out = N;
191 return true;
192 }
193
194 return false;
195 }
196
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000197 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
198 /// represented as an indexed [r+r] operation. Returns false if it can
199 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000200 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000201 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000202 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000203
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000204 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
205 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000206 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000207 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000208 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000209
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000210 /// SelectAddrImmX4 - Returns true if the address N can be represented by
211 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
212 /// Suitable for use by STD and friends.
213 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000214 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000215 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000216
Hal Finkel756810f2013-03-21 21:37:52 +0000217 // Select an address into a single register.
218 bool SelectAddr(SDValue N, SDValue &Base) {
219 Base = N;
220 return true;
221 }
222
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000223 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000224 /// inline asm expressions. It is always correct to compute the value into
225 /// a register. The case of adding a (possibly relocatable) constant to a
226 /// register can be improved, but it is wrong to substitute Reg+Reg for
227 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000228 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000229 unsigned ConstraintID,
Craig Topper0d3fa922014-04-29 07:57:37 +0000230 std::vector<SDValue> &OutOps) override {
Daniel Sanders08288602015-03-17 11:09:13 +0000231 switch(ConstraintID) {
232 default:
233 errs() << "ConstraintID: " << ConstraintID << "\n";
234 llvm_unreachable("Unexpected asm memory constraint");
235 case InlineAsm::Constraint_es:
Daniel Sanders914b9472015-03-17 12:00:04 +0000236 case InlineAsm::Constraint_i:
Daniel Sanders08288602015-03-17 11:09:13 +0000237 case InlineAsm::Constraint_m:
238 case InlineAsm::Constraint_o:
239 case InlineAsm::Constraint_Q:
240 case InlineAsm::Constraint_Z:
241 case InlineAsm::Constraint_Zy:
242 // We need to make sure that this one operand does not end up in r0
243 // (because we might end up lowering this as 0(%op)).
244 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
245 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000246 SDLoc dl(Op);
247 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Daniel Sanders08288602015-03-17 11:09:13 +0000248 SDValue NewOp =
249 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000250 dl, Op.getValueType(),
Daniel Sanders08288602015-03-17 11:09:13 +0000251 Op, RC), 0);
252
253 OutOps.push_back(NewOp);
254 return false;
255 }
256 return true;
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000257 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000258
Dan Gohman5ea74d52009-07-31 18:16:33 +0000259 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000260
Mehdi Amini117296c2016-10-01 02:56:57 +0000261 StringRef getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000262 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000263 }
264
Chris Lattner03e08ee2005-09-13 22:03:06 +0000265// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000266#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000267
Chris Lattner259e6c72005-10-06 18:45:51 +0000268private:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000269 // Conversion type for interpreting results of a 32-bit instruction as
270 // a 64-bit value or vice versa.
271 enum ExtOrTruncConversion { Ext, Trunc };
272
273 // Modifiers to guide how an ISD::SETCC node's result is to be computed
274 // in a GPR.
275 // ZExtOrig - use the original condition code, zero-extend value
276 // ZExtInvert - invert the condition code, zero-extend value
277 // SExtOrig - use the original condition code, sign-extend value
278 // SExtInvert - invert the condition code, sign-extend value
279 enum SetccInGPROpts { ZExtOrig, ZExtInvert, SExtOrig, SExtInvert };
280
Justin Bognerdc8af062016-05-20 21:43:23 +0000281 bool trySETCC(SDNode *N);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000282 bool tryEXTEND(SDNode *N);
Nemanja Ivanovice597bd82017-05-31 05:40:25 +0000283 bool tryLogicOpOfCompares(SDNode *N);
284 SDValue computeLogicOpInGPR(SDValue LogicOp);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000285 SDValue signExtendInputIfNeeded(SDValue Input);
286 SDValue zeroExtendInputIfNeeded(SDValue Input);
287 SDValue addExtOrTrunc(SDValue NatWidthRes, ExtOrTruncConversion Conv);
288 SDValue get32BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
289 int64_t RHSValue, SDLoc dl);
290 SDValue get32BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
291 int64_t RHSValue, SDLoc dl);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +0000292 SDValue get64BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
293 int64_t RHSValue, SDLoc dl);
294 SDValue get64BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
295 int64_t RHSValue, SDLoc dl);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000296 SDValue getSETCCInGPR(SDValue Compare, SetccInGPROpts ConvOpts);
Hal Finkel940ab932014-02-28 00:27:01 +0000297
298 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000299 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000300 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000301
Hal Finkel4edc66b2015-01-03 01:16:37 +0000302 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000303 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000304
Hal Finkelb9989152014-02-28 06:11:16 +0000305 bool AllUsersSelectZero(SDNode *N);
306 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000307
Justin Bognerdc8af062016-05-20 21:43:23 +0000308 void transferMemOperands(SDNode *N, SDNode *Result);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000309 };
Eugene Zelenko8187c192017-01-13 00:58:58 +0000310
311} // end anonymous namespace
Chris Lattner43ff01e2005-08-17 19:33:03 +0000312
Chris Lattner1678a6c2006-03-16 18:25:23 +0000313/// InsertVRSaveCode - Once the entire function has been instruction selected,
314/// all virtual registers are created and all machine instructions are built,
315/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000316void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000317 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000318 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000319 //
Dan Gohman4a618822010-02-10 16:03:48 +0000320 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000321 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000322 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000323 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
324 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
325 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000326 HasVectorVReg = true;
327 break;
328 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000329 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000330 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000331
Chris Lattner02e2c182006-03-13 21:52:10 +0000332 // If we have a vector register, we want to emit code into the entry and exit
333 // blocks to save and restore the VRSAVE register. We do this here (instead
334 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
335 //
336 // 1. This (trivially) reduces the load on the register allocator, by not
337 // having to represent the live range of the VRSAVE register.
338 // 2. This (more significantly) allows us to create a temporary virtual
339 // register to hold the saved VRSAVE value, allowing this temporary to be
340 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000341
342 // Create two vregs - one to hold the VRSAVE register that is live-in to the
343 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000344 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
345 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000346
Eric Christophercccae792015-01-30 22:02:31 +0000347 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000348 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000349 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000350 // Emit the following code into the entry block:
351 // InVRSAVE = MFVRSAVE
352 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
353 // MTVRSAVE UpdatedVRSAVE
354 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000355 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
356 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000357 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000358 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000359
Chris Lattner1678a6c2006-03-16 18:25:23 +0000360 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000361 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000362 if (BB->isReturnBlock()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000363 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000364
Chris Lattner1678a6c2006-03-16 18:25:23 +0000365 // Skip over all terminator instructions, which are part of the return
366 // sequence.
367 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000368 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000369 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000370
Chris Lattner1678a6c2006-03-16 18:25:23 +0000371 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000372 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000373 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000374 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000375}
Chris Lattner8ae95252005-09-03 01:17:22 +0000376
Chris Lattner45640392005-08-19 22:38:53 +0000377/// getGlobalBaseReg - Output the instructions required to put the
378/// base address to use for accessing globals into a register.
379///
Evan Cheng61413a32006-08-26 05:34:46 +0000380SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000381 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000382 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000383 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000384 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000385 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000386 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000387 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000388
Mehdi Amini44ede332015-07-09 02:09:04 +0000389 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000390 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000391 GlobalBaseReg = PPC::R30;
Davide Italiano4cccc482016-06-17 18:07:14 +0000392 if (M->getPICLevel() == PICLevel::SmallPIC) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000393 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
394 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000395 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000396 } else {
397 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
398 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
399 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
400 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000401 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000402 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
403 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
404 }
405 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000406 GlobalBaseReg =
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000407 RegInfo->createVirtualRegister(&PPC::GPRC_and_GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000408 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
409 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000410 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000411 } else {
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000412 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_and_G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000413 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000414 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000415 }
Chris Lattner45640392005-08-19 22:38:53 +0000416 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000417 return CurDAG->getRegister(GlobalBaseReg,
Mehdi Amini44ede332015-07-09 02:09:04 +0000418 PPCLowering->getPointerTy(CurDAG->getDataLayout()))
419 .getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000420}
421
Chris Lattner97b3da12006-06-27 00:04:13 +0000422/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
423/// operand. If so Imm will receive the 32-bit value.
424static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000425 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000426 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000427 return true;
428 }
429 return false;
430}
431
Chris Lattner97b3da12006-06-27 00:04:13 +0000432/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
433/// operand. If so Imm will receive the 64-bit value.
434static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000435 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000436 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000437 return true;
438 }
439 return false;
440}
441
442// isInt32Immediate - This method tests to see if a constant operand.
443// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000444static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000445 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000446}
447
Hal Finkel65539e32015-12-12 00:32:00 +0000448static unsigned getBranchHint(unsigned PCC, FunctionLoweringInfo *FuncInfo,
449 const SDValue &DestMBB) {
450 assert(isa<BasicBlockSDNode>(DestMBB));
451
452 if (!FuncInfo->BPI) return PPC::BR_NO_HINT;
453
454 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
455 const TerminatorInst *BBTerm = BB->getTerminator();
456
457 if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT;
458
459 const BasicBlock *TBB = BBTerm->getSuccessor(0);
460 const BasicBlock *FBB = BBTerm->getSuccessor(1);
461
Cong Houe93b8e12015-12-22 18:56:14 +0000462 auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);
463 auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);
Hal Finkel65539e32015-12-12 00:32:00 +0000464
465 // We only want to handle cases which are easy to predict at static time, e.g.
466 // C++ throw statement, that is very likely not taken, or calling never
467 // returned function, e.g. stdlib exit(). So we set Threshold to filter
468 // unwanted cases.
469 //
470 // Below is LLVM branch weight table, we only want to handle case 1, 2
471 //
472 // Case Taken:Nontaken Example
473 // 1. Unreachable 1048575:1 C++ throw, stdlib exit(),
474 // 2. Invoke-terminating 1:1048575
475 // 3. Coldblock 4:64 __builtin_expect
476 // 4. Loop Branch 124:4 For loop
477 // 5. PH/ZH/FPH 20:12
478 const uint32_t Threshold = 10000;
479
Cong Houe93b8e12015-12-22 18:56:14 +0000480 if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))
Hal Finkel65539e32015-12-12 00:32:00 +0000481 return PPC::BR_NO_HINT;
482
483 DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName() << "::"
484 << BB->getName() << "'\n"
Cong Houe93b8e12015-12-22 18:56:14 +0000485 << " -> " << TBB->getName() << ": " << TProb << "\n"
486 << " -> " << FBB->getName() << ": " << FProb << "\n");
Hal Finkel65539e32015-12-12 00:32:00 +0000487
488 const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);
489
Cong Houe93b8e12015-12-22 18:56:14 +0000490 // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities,
491 // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock
Hal Finkel65539e32015-12-12 00:32:00 +0000492 if (BBDN->getBasicBlock()->getBasicBlock() != TBB)
Cong Houe93b8e12015-12-22 18:56:14 +0000493 std::swap(TProb, FProb);
Hal Finkel65539e32015-12-12 00:32:00 +0000494
Cong Houe93b8e12015-12-22 18:56:14 +0000495 return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;
Hal Finkel65539e32015-12-12 00:32:00 +0000496}
Chris Lattner97b3da12006-06-27 00:04:13 +0000497
498// isOpcWithIntImmediate - This method tests to see if the node is a specific
499// opcode and that it has a immediate integer right operand.
500// If so Imm will receive the 32 bit value.
501static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000502 return N->getOpcode() == Opc
503 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000504}
505
Justin Bognerdc8af062016-05-20 21:43:23 +0000506void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
Hal Finkelb5e9b042014-12-11 22:51:06 +0000507 SDLoc dl(SN);
508 int FI = cast<FrameIndexSDNode>(N)->getIndex();
509 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
510 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
511 if (SN->hasOneUse())
Justin Bognerdc8af062016-05-20 21:43:23 +0000512 CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
513 getSmallIPtrImm(Offset, dl));
514 else
515 ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
516 getSmallIPtrImm(Offset, dl)));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000517}
518
Andrew Trickc416ba62010-12-24 04:28:06 +0000519bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
520 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000521 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000522 // Don't even go down this path for i64, since different logic will be
523 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000524 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000525 return false;
526
Nate Begemanb3821a32005-08-18 07:30:46 +0000527 unsigned Shift = 32;
528 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
529 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000530 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000531 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000532 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000533
Nate Begemanb3821a32005-08-18 07:30:46 +0000534 if (Opcode == ISD::SHL) {
535 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000536 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000537 // determine which bits are made indeterminant by shift
538 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000539 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000540 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000541 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000542 // determine which bits are made indeterminant by shift
543 Indeterminant = ~(0xFFFFFFFFu >> Shift);
544 // adjust for the left rotate
545 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000546 } else if (Opcode == ISD::ROTL) {
547 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000548 } else {
549 return false;
550 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000551
Nate Begemanb3821a32005-08-18 07:30:46 +0000552 // if the mask doesn't intersect any Indeterminant bits
553 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000554 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000555 // make sure the mask is still a mask (wrap arounds may not be)
556 return isRunOfOnes(Mask, MB, ME);
557 }
558 return false;
559}
560
Justin Bognerdc8af062016-05-20 21:43:23 +0000561/// Turn an or of two masked values into the rotate left word immediate then
562/// mask insert (rlwimi) instruction.
563bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000564 SDValue Op0 = N->getOperand(0);
565 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000566 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000567
Craig Topperd0af7e82017-04-28 05:31:46 +0000568 KnownBits LKnown, RKnown;
569 CurDAG->computeKnownBits(Op0, LKnown);
570 CurDAG->computeKnownBits(Op1, RKnown);
Andrew Trickc416ba62010-12-24 04:28:06 +0000571
Craig Topperd0af7e82017-04-28 05:31:46 +0000572 unsigned TargetMask = LKnown.Zero.getZExtValue();
573 unsigned InsertMask = RKnown.Zero.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000574
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000575 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
576 unsigned Op0Opc = Op0.getOpcode();
577 unsigned Op1Opc = Op1.getOpcode();
578 unsigned Value, SH = 0;
579 TargetMask = ~TargetMask;
580 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000581
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000582 // If the LHS has a foldable shift and the RHS does not, then swap it to the
583 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000584 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
585 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
586 Op0.getOperand(0).getOpcode() == ISD::SRL) {
587 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
588 Op1.getOperand(0).getOpcode() != ISD::SRL) {
589 std::swap(Op0, Op1);
590 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000591 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000592 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000593 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000594 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
595 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
596 Op1.getOperand(0).getOpcode() != ISD::SRL) {
597 std::swap(Op0, Op1);
598 std::swap(Op0Opc, Op1Opc);
599 std::swap(TargetMask, InsertMask);
600 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000601 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000602
Nate Begeman1333cea2006-05-07 00:23:38 +0000603 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000604 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000605 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000606
607 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000608 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000609 Op1 = Op1.getOperand(0);
610 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
611 }
612 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000613 // The AND mask might not be a constant, and we need to make sure that
614 // if we're going to fold the masking with the insert, all bits not
615 // know to be zero in the mask are known to be one.
Craig Topperd0af7e82017-04-28 05:31:46 +0000616 KnownBits MKnown;
617 CurDAG->computeKnownBits(Op1.getOperand(1), MKnown);
618 bool CanFoldMask = InsertMask == MKnown.One.getZExtValue();
Hal Finkeld9963c72014-04-13 17:10:58 +0000619
Nate Begeman1333cea2006-05-07 00:23:38 +0000620 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000621 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000622 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000623 // Note that Value must be in range here (less than 32) because
624 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000625 Op1 = Op1.getOperand(0).getOperand(0);
626 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000627 }
628 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000629
Chris Lattnera2963392006-05-12 16:29:37 +0000630 SH &= 31;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000631 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
632 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +0000633 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
634 return true;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000635 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000636 }
Justin Bognerdc8af062016-05-20 21:43:23 +0000637 return false;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000638}
639
Hal Finkelc58ce412015-01-01 02:53:29 +0000640// Predict the number of instructions that would be generated by calling
Justin Bognerdc8af062016-05-20 21:43:23 +0000641// getInt64(N).
642static unsigned getInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000643 // Assume no remaining bits.
644 unsigned Remainder = 0;
645 // Assume no shift required.
646 unsigned Shift = 0;
647
648 // If it can't be represented as a 32 bit value.
649 if (!isInt<32>(Imm)) {
650 Shift = countTrailingZeros<uint64_t>(Imm);
651 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
652
653 // If the shifted value fits 32 bits.
654 if (isInt<32>(ImmSh)) {
655 // Go with the shifted value.
656 Imm = ImmSh;
657 } else {
658 // Still stuck with a 64 bit value.
659 Remainder = Imm;
660 Shift = 32;
661 Imm >>= 32;
662 }
663 }
664
665 // Intermediate operand.
666 unsigned Result = 0;
667
668 // Handle first 32 bits.
669 unsigned Lo = Imm & 0xFFFF;
Hal Finkelc58ce412015-01-01 02:53:29 +0000670
671 // Simple value.
672 if (isInt<16>(Imm)) {
673 // Just the Lo bits.
674 ++Result;
675 } else if (Lo) {
676 // Handle the Hi bits and Lo bits.
677 Result += 2;
678 } else {
679 // Just the Hi bits.
680 ++Result;
681 }
682
683 // If no shift, we're done.
684 if (!Shift) return Result;
685
Guozhi Wei0cd65422016-10-14 20:41:50 +0000686 // If Hi word == Lo word,
687 // we can use rldimi to insert the Lo word into Hi word.
688 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
689 ++Result;
690 return Result;
691 }
692
Hal Finkelc58ce412015-01-01 02:53:29 +0000693 // Shift for next step if the upper 32-bits were not zero.
694 if (Imm)
695 ++Result;
696
697 // Add in the last bits as required.
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000698 if ((Remainder >> 16) & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000699 ++Result;
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000700 if (Remainder & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000701 ++Result;
702
703 return Result;
704}
705
Hal Finkel241ba792015-01-04 15:43:55 +0000706static uint64_t Rot64(uint64_t Imm, unsigned R) {
707 return (Imm << R) | (Imm >> (64 - R));
708}
709
Justin Bognerdc8af062016-05-20 21:43:23 +0000710static unsigned getInt64Count(int64_t Imm) {
711 unsigned Count = getInt64CountDirect(Imm);
Hiroshi Inouef55ee1b2017-07-11 05:28:26 +0000712
713 // If the instruction count is 1 or 2, we do not need further analysis
714 // since rotate + load constant requires at least 2 instructions.
715 if (Count <= 2)
Hal Finkel2f618792015-01-05 03:41:38 +0000716 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000717
Hal Finkel241ba792015-01-04 15:43:55 +0000718 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000719 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000720 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000721 Count = std::min(Count, RCount);
722
Justin Bognerdc8af062016-05-20 21:43:23 +0000723 // See comments in getInt64 for an explanation of the logic below.
Hal Finkel2f618792015-01-05 03:41:38 +0000724 unsigned LS = findLastSet(RImm);
725 if (LS != r-1)
726 continue;
727
728 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
729 uint64_t RImmWithOnes = RImm | OnesMask;
730
Justin Bognerdc8af062016-05-20 21:43:23 +0000731 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000732 Count = std::min(Count, RCount);
733 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000734
Hal Finkel241ba792015-01-04 15:43:55 +0000735 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000736}
737
Justin Bognerdc8af062016-05-20 21:43:23 +0000738// Select a 64-bit constant. For cost-modeling purposes, getInt64Count
Hal Finkelc58ce412015-01-01 02:53:29 +0000739// (above) needs to be kept in sync with this function.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000740static SDNode *getInt64Direct(SelectionDAG *CurDAG, const SDLoc &dl,
741 int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000742 // Assume no remaining bits.
743 unsigned Remainder = 0;
744 // Assume no shift required.
745 unsigned Shift = 0;
746
747 // If it can't be represented as a 32 bit value.
748 if (!isInt<32>(Imm)) {
749 Shift = countTrailingZeros<uint64_t>(Imm);
750 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
751
752 // If the shifted value fits 32 bits.
753 if (isInt<32>(ImmSh)) {
754 // Go with the shifted value.
755 Imm = ImmSh;
756 } else {
757 // Still stuck with a 64 bit value.
758 Remainder = Imm;
759 Shift = 32;
760 Imm >>= 32;
761 }
762 }
763
764 // Intermediate operand.
765 SDNode *Result;
766
767 // Handle first 32 bits.
768 unsigned Lo = Imm & 0xFFFF;
769 unsigned Hi = (Imm >> 16) & 0xFFFF;
770
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000771 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
772 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000773 };
774
775 // Simple value.
776 if (isInt<16>(Imm)) {
777 // Just the Lo bits.
778 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
779 } else if (Lo) {
780 // Handle the Hi bits.
781 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
782 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
783 // And Lo bits.
784 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
785 SDValue(Result, 0), getI32Imm(Lo));
786 } else {
787 // Just the Hi bits.
788 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
789 }
790
791 // If no shift, we're done.
792 if (!Shift) return Result;
793
Guozhi Wei0cd65422016-10-14 20:41:50 +0000794 // If Hi word == Lo word,
795 // we can use rldimi to insert the Lo word into Hi word.
796 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
797 SDValue Ops[] =
798 { SDValue(Result, 0), SDValue(Result, 0), getI32Imm(Shift), getI32Imm(0)};
799 return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops);
800 }
801
Hal Finkelc58ce412015-01-01 02:53:29 +0000802 // Shift for next step if the upper 32-bits were not zero.
803 if (Imm) {
804 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
805 SDValue(Result, 0),
806 getI32Imm(Shift),
807 getI32Imm(63 - Shift));
808 }
809
810 // Add in the last bits as required.
811 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
812 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
813 SDValue(Result, 0), getI32Imm(Hi));
814 }
815 if ((Lo = Remainder & 0xFFFF)) {
816 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
817 SDValue(Result, 0), getI32Imm(Lo));
818 }
819
820 return Result;
821}
822
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000823static SDNode *getInt64(SelectionDAG *CurDAG, const SDLoc &dl, int64_t Imm) {
Justin Bognerdc8af062016-05-20 21:43:23 +0000824 unsigned Count = getInt64CountDirect(Imm);
Hiroshi Inouef55ee1b2017-07-11 05:28:26 +0000825
826 // If the instruction count is 1 or 2, we do not need further analysis
827 // since rotate + load constant requires at least 2 instructions.
828 if (Count <= 2)
Justin Bognerdc8af062016-05-20 21:43:23 +0000829 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000830
Hal Finkel241ba792015-01-04 15:43:55 +0000831 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000832
Hal Finkel2f618792015-01-05 03:41:38 +0000833 int64_t MatImm;
834 unsigned MaskEnd;
835
Hal Finkel241ba792015-01-04 15:43:55 +0000836 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000837 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000838 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000839 if (RCount < Count) {
840 Count = RCount;
841 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000842 MatImm = RImm;
843 MaskEnd = 63;
844 }
845
846 // If the immediate to generate has many trailing zeros, it might be
847 // worthwhile to generate a rotated value with too many leading ones
848 // (because that's free with li/lis's sign-extension semantics), and then
849 // mask them off after rotation.
850
851 unsigned LS = findLastSet(RImm);
852 // We're adding (63-LS) higher-order ones, and we expect to mask them off
853 // after performing the inverse rotation by (64-r). So we need that:
854 // 63-LS == 64-r => LS == r-1
855 if (LS != r-1)
856 continue;
857
858 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
859 uint64_t RImmWithOnes = RImm | OnesMask;
860
Justin Bognerdc8af062016-05-20 21:43:23 +0000861 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000862 if (RCount < Count) {
863 Count = RCount;
864 RMin = r;
865 MatImm = RImmWithOnes;
866 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000867 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000868 }
869
Hal Finkel241ba792015-01-04 15:43:55 +0000870 if (!RMin)
Justin Bognerdc8af062016-05-20 21:43:23 +0000871 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel241ba792015-01-04 15:43:55 +0000872
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000873 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
874 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000875 };
876
Justin Bognerdc8af062016-05-20 21:43:23 +0000877 SDValue Val = SDValue(getInt64Direct(CurDAG, dl, MatImm), 0);
Hal Finkel2f618792015-01-05 03:41:38 +0000878 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
879 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000880}
881
Hal Finkelc58ce412015-01-01 02:53:29 +0000882// Select a 64-bit constant.
Justin Bognerdc8af062016-05-20 21:43:23 +0000883static SDNode *getInt64(SelectionDAG *CurDAG, SDNode *N) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000884 SDLoc dl(N);
885
886 // Get 64 bit value.
887 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Justin Bognerdc8af062016-05-20 21:43:23 +0000888 return getInt64(CurDAG, dl, Imm);
Hal Finkelc58ce412015-01-01 02:53:29 +0000889}
890
Hal Finkel8adf2252014-12-16 05:51:41 +0000891namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000892
Hal Finkel8adf2252014-12-16 05:51:41 +0000893class BitPermutationSelector {
894 struct ValueBit {
895 SDValue V;
896
897 // The bit number in the value, using a convention where bit 0 is the
898 // lowest-order bit.
899 unsigned Idx;
900
901 enum Kind {
902 ConstZero,
903 Variable
904 } K;
905
906 ValueBit(SDValue V, unsigned I, Kind K = Variable)
907 : V(V), Idx(I), K(K) {}
908 ValueBit(Kind K = Variable)
909 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
910
911 bool isZero() const {
912 return K == ConstZero;
913 }
914
915 bool hasValue() const {
916 return K == Variable;
917 }
918
919 SDValue getValue() const {
920 assert(hasValue() && "Cannot get the value of a constant bit");
921 return V;
922 }
923
924 unsigned getValueBitIndex() const {
925 assert(hasValue() && "Cannot get the value bit index of a constant bit");
926 return Idx;
927 }
928 };
929
930 // A bit group has the same underlying value and the same rotate factor.
931 struct BitGroup {
932 SDValue V;
933 unsigned RLAmt;
934 unsigned StartIdx, EndIdx;
935
Hal Finkelc58ce412015-01-01 02:53:29 +0000936 // This rotation amount assumes that the lower 32 bits of the quantity are
937 // replicated in the high 32 bits by the rotation operator (which is done
938 // by rlwinm and friends in 64-bit mode).
939 bool Repl32;
940 // Did converting to Repl32 == true change the rotation factor? If it did,
941 // it decreased it by 32.
942 bool Repl32CR;
943 // Was this group coalesced after setting Repl32 to true?
944 bool Repl32Coalesced;
945
Hal Finkel8adf2252014-12-16 05:51:41 +0000946 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000947 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
948 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000949 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
950 " [" << S << ", " << E << "]\n");
951 }
952 };
953
954 // Information on each (Value, RLAmt) pair (like the number of groups
955 // associated with each) used to choose the lowering method.
956 struct ValueRotInfo {
957 SDValue V;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000958 unsigned RLAmt = std::numeric_limits<unsigned>::max();
959 unsigned NumGroups = 0;
960 unsigned FirstGroupStartIdx = std::numeric_limits<unsigned>::max();
961 bool Repl32 = false;
Hal Finkel8adf2252014-12-16 05:51:41 +0000962
Eugene Zelenko8187c192017-01-13 00:58:58 +0000963 ValueRotInfo() = default;
Hal Finkel8adf2252014-12-16 05:51:41 +0000964
965 // For sorting (in reverse order) by NumGroups, and then by
966 // FirstGroupStartIdx.
967 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000968 // We need to sort so that the non-Repl32 come first because, when we're
969 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
970 // masking operation.
971 if (Repl32 < Other.Repl32)
972 return true;
973 else if (Repl32 > Other.Repl32)
974 return false;
975 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000976 return true;
977 else if (NumGroups < Other.NumGroups)
978 return false;
979 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
980 return true;
981 return false;
982 }
983 };
984
Tim Shendc698c32016-08-12 18:40:04 +0000985 using ValueBitsMemoizedValue = std::pair<bool, SmallVector<ValueBit, 64>>;
986 using ValueBitsMemoizer =
987 DenseMap<SDValue, std::unique_ptr<ValueBitsMemoizedValue>>;
988 ValueBitsMemoizer Memoizer;
989
990 // Return a pair of bool and a SmallVector pointer to a memoization entry.
991 // The bool is true if something interesting was deduced, otherwise if we're
Hal Finkel8adf2252014-12-16 05:51:41 +0000992 // providing only a generic representation of V (or something else likewise
Tim Shendc698c32016-08-12 18:40:04 +0000993 // uninteresting for instruction selection) through the SmallVector.
994 std::pair<bool, SmallVector<ValueBit, 64> *> getValueBits(SDValue V,
995 unsigned NumBits) {
996 auto &ValueEntry = Memoizer[V];
997 if (ValueEntry)
998 return std::make_pair(ValueEntry->first, &ValueEntry->second);
999 ValueEntry.reset(new ValueBitsMemoizedValue());
1000 bool &Interesting = ValueEntry->first;
1001 SmallVector<ValueBit, 64> &Bits = ValueEntry->second;
1002 Bits.resize(NumBits);
1003
Hal Finkel8adf2252014-12-16 05:51:41 +00001004 switch (V.getOpcode()) {
1005 default: break;
1006 case ISD::ROTL:
1007 if (isa<ConstantSDNode>(V.getOperand(1))) {
1008 unsigned RotAmt = V.getConstantOperandVal(1);
1009
Tim Shendc698c32016-08-12 18:40:04 +00001010 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001011
Tim Shendc698c32016-08-12 18:40:04 +00001012 for (unsigned i = 0; i < NumBits; ++i)
1013 Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt];
Hal Finkel8adf2252014-12-16 05:51:41 +00001014
Tim Shendc698c32016-08-12 18:40:04 +00001015 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001016 }
1017 break;
1018 case ISD::SHL:
1019 if (isa<ConstantSDNode>(V.getOperand(1))) {
1020 unsigned ShiftAmt = V.getConstantOperandVal(1);
1021
Tim Shendc698c32016-08-12 18:40:04 +00001022 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001023
Tim Shendc698c32016-08-12 18:40:04 +00001024 for (unsigned i = ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001025 Bits[i] = LHSBits[i - ShiftAmt];
1026
1027 for (unsigned i = 0; i < ShiftAmt; ++i)
1028 Bits[i] = ValueBit(ValueBit::ConstZero);
1029
Tim Shendc698c32016-08-12 18:40:04 +00001030 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001031 }
1032 break;
1033 case ISD::SRL:
1034 if (isa<ConstantSDNode>(V.getOperand(1))) {
1035 unsigned ShiftAmt = V.getConstantOperandVal(1);
1036
Tim Shendc698c32016-08-12 18:40:04 +00001037 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001038
Tim Shendc698c32016-08-12 18:40:04 +00001039 for (unsigned i = 0; i < NumBits - ShiftAmt; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001040 Bits[i] = LHSBits[i + ShiftAmt];
1041
Tim Shendc698c32016-08-12 18:40:04 +00001042 for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001043 Bits[i] = ValueBit(ValueBit::ConstZero);
1044
Tim Shendc698c32016-08-12 18:40:04 +00001045 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001046 }
1047 break;
1048 case ISD::AND:
1049 if (isa<ConstantSDNode>(V.getOperand(1))) {
1050 uint64_t Mask = V.getConstantOperandVal(1);
1051
Tim Shendc698c32016-08-12 18:40:04 +00001052 const SmallVector<ValueBit, 64> *LHSBits;
Hal Finkel8adf2252014-12-16 05:51:41 +00001053 // Mark this as interesting, only if the LHS was also interesting. This
1054 // prevents the overall procedure from matching a single immediate 'and'
1055 // (which is non-optimal because such an and might be folded with other
1056 // things if we don't select it here).
Tim Shendc698c32016-08-12 18:40:04 +00001057 std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), NumBits);
1058
1059 for (unsigned i = 0; i < NumBits; ++i)
1060 if (((Mask >> i) & 1) == 1)
1061 Bits[i] = (*LHSBits)[i];
1062 else
1063 Bits[i] = ValueBit(ValueBit::ConstZero);
1064
1065 return std::make_pair(Interesting, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001066 }
1067 break;
1068 case ISD::OR: {
Tim Shendc698c32016-08-12 18:40:04 +00001069 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
1070 const auto &RHSBits = *getValueBits(V.getOperand(1), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001071
1072 bool AllDisjoint = true;
Tim Shendc698c32016-08-12 18:40:04 +00001073 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001074 if (LHSBits[i].isZero())
1075 Bits[i] = RHSBits[i];
1076 else if (RHSBits[i].isZero())
1077 Bits[i] = LHSBits[i];
1078 else {
1079 AllDisjoint = false;
1080 break;
1081 }
1082
1083 if (!AllDisjoint)
1084 break;
1085
Tim Shendc698c32016-08-12 18:40:04 +00001086 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001087 }
1088 }
1089
Tim Shendc698c32016-08-12 18:40:04 +00001090 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001091 Bits[i] = ValueBit(V, i);
1092
Tim Shendc698c32016-08-12 18:40:04 +00001093 return std::make_pair(Interesting = false, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001094 }
1095
1096 // For each value (except the constant ones), compute the left-rotate amount
1097 // to get it from its original to final position.
1098 void computeRotationAmounts() {
1099 HasZeros = false;
1100 RLAmt.resize(Bits.size());
1101 for (unsigned i = 0; i < Bits.size(); ++i)
1102 if (Bits[i].hasValue()) {
1103 unsigned VBI = Bits[i].getValueBitIndex();
1104 if (i >= VBI)
1105 RLAmt[i] = i - VBI;
1106 else
1107 RLAmt[i] = Bits.size() - (VBI - i);
1108 } else if (Bits[i].isZero()) {
1109 HasZeros = true;
1110 RLAmt[i] = UINT32_MAX;
1111 } else {
1112 llvm_unreachable("Unknown value bit type");
1113 }
1114 }
1115
1116 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +00001117 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
1118 // they break up groups.
1119 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001120 BitGroups.clear();
1121
1122 unsigned LastRLAmt = RLAmt[0];
1123 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1124 unsigned LastGroupStartIdx = 0;
1125 for (unsigned i = 1; i < Bits.size(); ++i) {
1126 unsigned ThisRLAmt = RLAmt[i];
1127 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001128 if (LateMask && !ThisValue) {
1129 ThisValue = LastValue;
1130 ThisRLAmt = LastRLAmt;
1131 // If we're doing late masking, then the first bit group always starts
1132 // at zero (even if the first bits were zero).
1133 if (BitGroups.empty())
1134 LastGroupStartIdx = 0;
1135 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001136
1137 // If this bit has the same underlying value and the same rotate factor as
1138 // the last one, then they're part of the same group.
1139 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1140 continue;
1141
1142 if (LastValue.getNode())
1143 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1144 i-1));
1145 LastRLAmt = ThisRLAmt;
1146 LastValue = ThisValue;
1147 LastGroupStartIdx = i;
1148 }
1149 if (LastValue.getNode())
1150 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1151 Bits.size()-1));
1152
1153 if (BitGroups.empty())
1154 return;
1155
1156 // We might be able to combine the first and last groups.
1157 if (BitGroups.size() > 1) {
1158 // If the first and last groups are the same, then remove the first group
1159 // in favor of the last group, making the ending index of the last group
1160 // equal to the ending index of the to-be-removed first group.
1161 if (BitGroups[0].StartIdx == 0 &&
1162 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1163 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1164 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001165 DEBUG(dbgs() << "\tcombining final bit group with initial one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001166 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1167 BitGroups.erase(BitGroups.begin());
1168 }
1169 }
1170 }
1171
1172 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1173 // associated with each. If there is a degeneracy, pick the one that occurs
1174 // first (in the final value).
1175 void collectValueRotInfo() {
1176 ValueRots.clear();
1177
1178 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001179 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1180 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001181 VRI.V = BG.V;
1182 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001183 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001184 VRI.NumGroups += 1;
1185 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1186 }
1187
1188 // Now that we've collected the various ValueRotInfo instances, we need to
1189 // sort them.
1190 ValueRotsVec.clear();
1191 for (auto &I : ValueRots) {
1192 ValueRotsVec.push_back(I.second);
1193 }
1194 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1195 }
1196
Hal Finkelc58ce412015-01-01 02:53:29 +00001197 // In 64-bit mode, rlwinm and friends have a rotation operator that
1198 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1199 // indices of these instructions can only be in the lower 32 bits, so they
1200 // can only represent some 64-bit bit groups. However, when they can be used,
1201 // the 32-bit replication can be used to represent, as a single bit group,
1202 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1203 // groups when possible. Returns true if any of the bit groups were
1204 // converted.
1205 void assignRepl32BitGroups() {
1206 // If we have bits like this:
1207 //
1208 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1209 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1210 // Groups: | RLAmt = 8 | RLAmt = 40 |
1211 //
1212 // But, making use of a 32-bit operation that replicates the low-order 32
1213 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1214 // of 8.
1215
1216 auto IsAllLow32 = [this](BitGroup & BG) {
1217 if (BG.StartIdx <= BG.EndIdx) {
1218 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1219 if (!Bits[i].hasValue())
1220 continue;
1221 if (Bits[i].getValueBitIndex() >= 32)
1222 return false;
1223 }
1224 } else {
1225 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1226 if (!Bits[i].hasValue())
1227 continue;
1228 if (Bits[i].getValueBitIndex() >= 32)
1229 return false;
1230 }
1231 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1232 if (!Bits[i].hasValue())
1233 continue;
1234 if (Bits[i].getValueBitIndex() >= 32)
1235 return false;
1236 }
1237 }
1238
1239 return true;
1240 };
1241
1242 for (auto &BG : BitGroups) {
1243 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1244 if (IsAllLow32(BG)) {
1245 if (BG.RLAmt >= 32) {
1246 BG.RLAmt -= 32;
1247 BG.Repl32CR = true;
1248 }
1249
1250 BG.Repl32 = true;
1251
1252 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1253 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1254 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1255 }
1256 }
1257 }
1258
1259 // Now walk through the bit groups, consolidating where possible.
1260 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1261 // We might want to remove this bit group by merging it with the previous
1262 // group (which might be the ending group).
1263 auto IP = (I == BitGroups.begin()) ?
1264 std::prev(BitGroups.end()) : std::prev(I);
1265 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1266 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1267
1268 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1269 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1270 " [" << I->StartIdx << ", " << I->EndIdx <<
1271 "] with group with range [" <<
1272 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1273
1274 IP->EndIdx = I->EndIdx;
1275 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1276 IP->Repl32Coalesced = true;
1277 I = BitGroups.erase(I);
1278 continue;
1279 } else {
1280 // There is a special case worth handling: If there is a single group
1281 // covering the entire upper 32 bits, and it can be merged with both
1282 // the next and previous groups (which might be the same group), then
1283 // do so. If it is the same group (so there will be only one group in
1284 // total), then we need to reverse the order of the range so that it
1285 // covers the entire 64 bits.
1286 if (I->StartIdx == 32 && I->EndIdx == 63) {
1287 assert(std::next(I) == BitGroups.end() &&
1288 "bit group ends at index 63 but there is another?");
1289 auto IN = BitGroups.begin();
1290
Justin Bognerb0126992016-05-05 23:19:08 +00001291 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
Hal Finkelc58ce412015-01-01 02:53:29 +00001292 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1293 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1294 IsAllLow32(*I)) {
1295
1296 DEBUG(dbgs() << "\tcombining bit group for " <<
1297 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1298 " [" << I->StartIdx << ", " << I->EndIdx <<
1299 "] with 32-bit replicated groups with ranges [" <<
1300 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1301 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1302
1303 if (IP == IN) {
1304 // There is only one other group; change it to cover the whole
1305 // range (backward, so that it can still be Repl32 but cover the
1306 // whole 64-bit range).
1307 IP->StartIdx = 31;
1308 IP->EndIdx = 30;
1309 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1310 IP->Repl32Coalesced = true;
1311 I = BitGroups.erase(I);
1312 } else {
1313 // There are two separate groups, one before this group and one
1314 // after us (at the beginning). We're going to remove this group,
1315 // but also the group at the very beginning.
1316 IP->EndIdx = IN->EndIdx;
1317 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1318 IP->Repl32Coalesced = true;
1319 I = BitGroups.erase(I);
1320 BitGroups.erase(BitGroups.begin());
1321 }
1322
1323 // This must be the last group in the vector (and we might have
1324 // just invalidated the iterator above), so break here.
1325 break;
1326 }
1327 }
1328 }
1329
1330 ++I;
1331 }
1332 }
1333
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001334 SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001335 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001336 }
1337
Hal Finkelc58ce412015-01-01 02:53:29 +00001338 uint64_t getZerosMask() {
1339 uint64_t Mask = 0;
1340 for (unsigned i = 0; i < Bits.size(); ++i) {
1341 if (Bits[i].hasValue())
1342 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001343 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001344 }
1345
1346 return ~Mask;
1347 }
1348
Hal Finkel8adf2252014-12-16 05:51:41 +00001349 // Depending on the number of groups for a particular value, it might be
1350 // better to rotate, mask explicitly (using andi/andis), and then or the
1351 // result. Select this part of the result first.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001352 void SelectAndParts32(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001353 if (BPermRewriterNoMasking)
1354 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001355
1356 for (ValueRotInfo &VRI : ValueRotsVec) {
1357 unsigned Mask = 0;
1358 for (unsigned i = 0; i < Bits.size(); ++i) {
1359 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1360 continue;
1361 if (RLAmt[i] != VRI.RLAmt)
1362 continue;
1363 Mask |= (1u << i);
1364 }
1365
1366 // Compute the masks for andi/andis that would be necessary.
1367 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1368 assert((ANDIMask != 0 || ANDISMask != 0) &&
1369 "No set bits in mask for value bit groups");
1370 bool NeedsRotate = VRI.RLAmt != 0;
1371
1372 // We're trying to minimize the number of instructions. If we have one
1373 // group, using one of andi/andis can break even. If we have three
1374 // groups, we can use both andi and andis and break even (to use both
1375 // andi and andis we also need to or the results together). We need four
1376 // groups if we also need to rotate. To use andi/andis we need to do more
1377 // than break even because rotate-and-mask instructions tend to be easier
1378 // to schedule.
1379
1380 // FIXME: We've biased here against using andi/andis, which is right for
1381 // POWER cores, but not optimal everywhere. For example, on the A2,
1382 // andi/andis have single-cycle latency whereas the rotate-and-mask
1383 // instructions take two cycles, and it would be better to bias toward
1384 // andi/andis in break-even cases.
1385
1386 unsigned NumAndInsts = (unsigned) NeedsRotate +
1387 (unsigned) (ANDIMask != 0) +
1388 (unsigned) (ANDISMask != 0) +
1389 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1390 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001391
1392 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1393 " RL: " << VRI.RLAmt << ":" <<
1394 "\n\t\t\tisel using masking: " << NumAndInsts <<
1395 " using rotates: " << VRI.NumGroups << "\n");
1396
Hal Finkel8adf2252014-12-16 05:51:41 +00001397 if (NumAndInsts >= VRI.NumGroups)
1398 continue;
1399
Hal Finkelc58ce412015-01-01 02:53:29 +00001400 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1401
1402 if (InstCnt) *InstCnt += NumAndInsts;
1403
Hal Finkel8adf2252014-12-16 05:51:41 +00001404 SDValue VRot;
1405 if (VRI.RLAmt) {
1406 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001407 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1408 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001409 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1410 Ops), 0);
1411 } else {
1412 VRot = VRI.V;
1413 }
1414
1415 SDValue ANDIVal, ANDISVal;
1416 if (ANDIMask != 0)
1417 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001418 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001419 if (ANDISMask != 0)
1420 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001421 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001422
1423 SDValue TotalVal;
1424 if (!ANDIVal)
1425 TotalVal = ANDISVal;
1426 else if (!ANDISVal)
1427 TotalVal = ANDIVal;
1428 else
1429 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1430 ANDIVal, ANDISVal), 0);
1431
1432 if (!Res)
1433 Res = TotalVal;
1434 else
1435 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1436 Res, TotalVal), 0);
1437
1438 // Now, remove all groups with this underlying value and rotation
1439 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001440 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1441 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1442 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001443 }
1444 }
1445
1446 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001447 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001448 SDLoc dl(N);
1449 SDValue Res;
1450
Hal Finkelc58ce412015-01-01 02:53:29 +00001451 if (InstCnt) *InstCnt = 0;
1452
Hal Finkel8adf2252014-12-16 05:51:41 +00001453 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001454 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001455
1456 // If we've not yet selected a 'starting' instruction, and we have no zeros
1457 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1458 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001459 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001460 ValueRotInfo &VRI = ValueRotsVec[0];
1461 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001462 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001463 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001464 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1465 getI32Imm(31, dl) };
1466 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1467 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001468 } else {
1469 Res = VRI.V;
1470 }
1471
1472 // Now, remove all groups with this underlying value and rotation factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001473 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1474 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1475 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001476 }
1477
Hal Finkelc58ce412015-01-01 02:53:29 +00001478 if (InstCnt) *InstCnt += BitGroups.size();
1479
Hal Finkel8adf2252014-12-16 05:51:41 +00001480 // Insert the other groups (one at a time).
1481 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001482 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001483 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001484 { BG.V, getI32Imm(BG.RLAmt, dl),
1485 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1486 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001487 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1488 } else {
1489 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001490 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1491 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1492 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001493 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1494 }
1495 }
1496
Hal Finkelc58ce412015-01-01 02:53:29 +00001497 if (LateMask) {
1498 unsigned Mask = (unsigned) getZerosMask();
1499
1500 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1501 assert((ANDIMask != 0 || ANDISMask != 0) &&
1502 "No set bits in zeros mask?");
1503
1504 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1505 (unsigned) (ANDISMask != 0) +
1506 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1507
1508 SDValue ANDIVal, ANDISVal;
1509 if (ANDIMask != 0)
1510 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001511 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001512 if (ANDISMask != 0)
1513 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001514 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001515
1516 if (!ANDIVal)
1517 Res = ANDISVal;
1518 else if (!ANDISVal)
1519 Res = ANDIVal;
1520 else
1521 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1522 ANDIVal, ANDISVal), 0);
1523 }
1524
Hal Finkel8adf2252014-12-16 05:51:41 +00001525 return Res.getNode();
1526 }
1527
Hal Finkelc58ce412015-01-01 02:53:29 +00001528 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1529 unsigned MaskStart, unsigned MaskEnd,
1530 bool IsIns) {
1531 // In the notation used by the instructions, 'start' and 'end' are reversed
1532 // because bits are counted from high to low order.
1533 unsigned InstMaskStart = 64 - MaskEnd - 1,
1534 InstMaskEnd = 64 - MaskStart - 1;
1535
1536 if (Repl32)
1537 return 1;
1538
1539 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1540 InstMaskEnd == 63 - RLAmt)
1541 return 1;
1542
1543 return 2;
1544 }
1545
1546 // For 64-bit values, not all combinations of rotates and masks are
1547 // available. Produce one if it is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001548 SDValue SelectRotMask64(SDValue V, const SDLoc &dl, unsigned RLAmt,
1549 bool Repl32, unsigned MaskStart, unsigned MaskEnd,
Hal Finkelc58ce412015-01-01 02:53:29 +00001550 unsigned *InstCnt = nullptr) {
1551 // In the notation used by the instructions, 'start' and 'end' are reversed
1552 // because bits are counted from high to low order.
1553 unsigned InstMaskStart = 64 - MaskEnd - 1,
1554 InstMaskEnd = 64 - MaskStart - 1;
1555
1556 if (InstCnt) *InstCnt += 1;
1557
1558 if (Repl32) {
1559 // This rotation amount assumes that the lower 32 bits of the quantity
1560 // are replicated in the high 32 bits by the rotation operator (which is
1561 // done by rlwinm and friends).
1562 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1563 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1564 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001565 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1566 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001567 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1568 Ops), 0);
1569 }
1570
1571 if (InstMaskEnd == 63) {
1572 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001573 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001574 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1575 }
1576
1577 if (InstMaskStart == 0) {
1578 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001579 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001580 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1581 }
1582
1583 if (InstMaskEnd == 63 - RLAmt) {
1584 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001585 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001586 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1587 }
1588
1589 // We cannot do this with a single instruction, so we'll use two. The
1590 // problem is that we're not free to choose both a rotation amount and mask
1591 // start and end independently. We can choose an arbitrary mask start and
1592 // end, but then the rotation amount is fixed. Rotation, however, can be
1593 // inverted, and so by applying an "inverse" rotation first, we can get the
1594 // desired result.
1595 if (InstCnt) *InstCnt += 1;
1596
1597 // The rotation mask for the second instruction must be MaskStart.
1598 unsigned RLAmt2 = MaskStart;
1599 // The first instruction must rotate V so that the overall rotation amount
1600 // is RLAmt.
1601 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1602 if (RLAmt1)
1603 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1604 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1605 }
1606
1607 // For 64-bit values, not all combinations of rotates and masks are
1608 // available. Produce a rotate-mask-and-insert if one is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001609 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, const SDLoc &dl,
1610 unsigned RLAmt, bool Repl32, unsigned MaskStart,
Hal Finkelc58ce412015-01-01 02:53:29 +00001611 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1612 // In the notation used by the instructions, 'start' and 'end' are reversed
1613 // because bits are counted from high to low order.
1614 unsigned InstMaskStart = 64 - MaskEnd - 1,
1615 InstMaskEnd = 64 - MaskStart - 1;
1616
1617 if (InstCnt) *InstCnt += 1;
1618
1619 if (Repl32) {
1620 // This rotation amount assumes that the lower 32 bits of the quantity
1621 // are replicated in the high 32 bits by the rotation operator (which is
1622 // done by rlwinm and friends).
1623 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1624 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1625 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1627 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001628 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1629 Ops), 0);
1630 }
1631
1632 if (InstMaskEnd == 63 - RLAmt) {
1633 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001634 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001635 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1636 }
1637
1638 // We cannot do this with a single instruction, so we'll use two. The
1639 // problem is that we're not free to choose both a rotation amount and mask
1640 // start and end independently. We can choose an arbitrary mask start and
1641 // end, but then the rotation amount is fixed. Rotation, however, can be
1642 // inverted, and so by applying an "inverse" rotation first, we can get the
1643 // desired result.
1644 if (InstCnt) *InstCnt += 1;
1645
1646 // The rotation mask for the second instruction must be MaskStart.
1647 unsigned RLAmt2 = MaskStart;
1648 // The first instruction must rotate V so that the overall rotation amount
1649 // is RLAmt.
1650 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1651 if (RLAmt1)
1652 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1653 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1654 }
1655
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001656 void SelectAndParts64(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001657 if (BPermRewriterNoMasking)
1658 return;
1659
1660 // The idea here is the same as in the 32-bit version, but with additional
1661 // complications from the fact that Repl32 might be true. Because we
1662 // aggressively convert bit groups to Repl32 form (which, for small
1663 // rotation factors, involves no other change), and then coalesce, it might
1664 // be the case that a single 64-bit masking operation could handle both
1665 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1666 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1667 // completely capture the new combined bit group.
1668
1669 for (ValueRotInfo &VRI : ValueRotsVec) {
1670 uint64_t Mask = 0;
1671
1672 // We need to add to the mask all bits from the associated bit groups.
1673 // If Repl32 is false, we need to add bits from bit groups that have
1674 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1675 // group is trivially convertable if it overlaps only with the lower 32
1676 // bits, and the group has not been coalesced.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001677 auto MatchingBG = [VRI](const BitGroup &BG) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001678 if (VRI.V != BG.V)
1679 return false;
1680
1681 unsigned EffRLAmt = BG.RLAmt;
1682 if (!VRI.Repl32 && BG.Repl32) {
1683 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1684 !BG.Repl32Coalesced) {
1685 if (BG.Repl32CR)
1686 EffRLAmt += 32;
1687 } else {
1688 return false;
1689 }
1690 } else if (VRI.Repl32 != BG.Repl32) {
1691 return false;
1692 }
1693
Alexander Kornienko175a7cb2015-12-28 13:38:42 +00001694 return VRI.RLAmt == EffRLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001695 };
1696
1697 for (auto &BG : BitGroups) {
1698 if (!MatchingBG(BG))
1699 continue;
1700
1701 if (BG.StartIdx <= BG.EndIdx) {
1702 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001703 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001704 } else {
1705 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001706 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001707 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001708 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001709 }
1710 }
1711
1712 // We can use the 32-bit andi/andis technique if the mask does not
1713 // require any higher-order bits. This can save an instruction compared
1714 // to always using the general 64-bit technique.
1715 bool Use32BitInsts = isUInt<32>(Mask);
1716 // Compute the masks for andi/andis that would be necessary.
1717 unsigned ANDIMask = (Mask & UINT16_MAX),
1718 ANDISMask = (Mask >> 16) & UINT16_MAX;
1719
1720 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1721
1722 unsigned NumAndInsts = (unsigned) NeedsRotate +
1723 (unsigned) (bool) Res;
1724 if (Use32BitInsts)
1725 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1726 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1727 else
Justin Bognerdc8af062016-05-20 21:43:23 +00001728 NumAndInsts += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001729
1730 unsigned NumRLInsts = 0;
1731 bool FirstBG = true;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001732 bool MoreBG = false;
Hal Finkelc58ce412015-01-01 02:53:29 +00001733 for (auto &BG : BitGroups) {
Guozhi Wei0cd65422016-10-14 20:41:50 +00001734 if (!MatchingBG(BG)) {
1735 MoreBG = true;
Hal Finkelc58ce412015-01-01 02:53:29 +00001736 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001737 }
Hal Finkelc58ce412015-01-01 02:53:29 +00001738 NumRLInsts +=
1739 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1740 !FirstBG);
1741 FirstBG = false;
1742 }
1743
1744 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1745 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1746 "\n\t\t\tisel using masking: " << NumAndInsts <<
1747 " using rotates: " << NumRLInsts << "\n");
1748
1749 // When we'd use andi/andis, we bias toward using the rotates (andi only
1750 // has a record form, and is cracked on POWER cores). However, when using
1751 // general 64-bit constant formation, bias toward the constant form,
1752 // because that exposes more opportunities for CSE.
1753 if (NumAndInsts > NumRLInsts)
1754 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001755 // When merging multiple bit groups, instruction or is used.
1756 // But when rotate is used, rldimi can inert the rotated value into any
1757 // register, so instruction or can be avoided.
1758 if ((Use32BitInsts || MoreBG) && NumAndInsts == NumRLInsts)
Hal Finkelc58ce412015-01-01 02:53:29 +00001759 continue;
1760
1761 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1762
1763 if (InstCnt) *InstCnt += NumAndInsts;
1764
1765 SDValue VRot;
1766 // We actually need to generate a rotation if we have a non-zero rotation
1767 // factor or, in the Repl32 case, if we care about any of the
1768 // higher-order replicated bits. In the latter case, we generate a mask
1769 // backward so that it actually includes the entire 64 bits.
1770 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1771 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1772 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1773 else
1774 VRot = VRI.V;
1775
1776 SDValue TotalVal;
1777 if (Use32BitInsts) {
1778 assert((ANDIMask != 0 || ANDISMask != 0) &&
1779 "No set bits in mask when using 32-bit ands for 64-bit value");
1780
1781 SDValue ANDIVal, ANDISVal;
1782 if (ANDIMask != 0)
1783 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001784 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001785 if (ANDISMask != 0)
1786 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001787 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001788
1789 if (!ANDIVal)
1790 TotalVal = ANDISVal;
1791 else if (!ANDISVal)
1792 TotalVal = ANDIVal;
1793 else
1794 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1795 ANDIVal, ANDISVal), 0);
1796 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001797 TotalVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001798 TotalVal =
1799 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1800 VRot, TotalVal), 0);
1801 }
1802
1803 if (!Res)
1804 Res = TotalVal;
1805 else
1806 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1807 Res, TotalVal), 0);
1808
1809 // Now, remove all groups with this underlying value and rotation
1810 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001811 eraseMatchingBitGroups(MatchingBG);
Hal Finkelc58ce412015-01-01 02:53:29 +00001812 }
1813 }
1814
1815 // Instruction selection for the 64-bit case.
1816 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1817 SDLoc dl(N);
1818 SDValue Res;
1819
1820 if (InstCnt) *InstCnt = 0;
1821
1822 // Take care of cases that should use andi/andis first.
1823 SelectAndParts64(dl, Res, InstCnt);
1824
1825 // If we've not yet selected a 'starting' instruction, and we have no zeros
1826 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1827 // number of groups), and start with this rotated value.
1828 if ((!HasZeros || LateMask) && !Res) {
1829 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1830 // groups will come first, and so the VRI representing the largest number
1831 // of groups might not be first (it might be the first Repl32 groups).
1832 unsigned MaxGroupsIdx = 0;
1833 if (!ValueRotsVec[0].Repl32) {
1834 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1835 if (ValueRotsVec[i].Repl32) {
1836 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1837 MaxGroupsIdx = i;
1838 break;
1839 }
1840 }
1841
1842 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1843 bool NeedsRotate = false;
1844 if (VRI.RLAmt) {
1845 NeedsRotate = true;
1846 } else if (VRI.Repl32) {
1847 for (auto &BG : BitGroups) {
1848 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1849 BG.Repl32 != VRI.Repl32)
1850 continue;
1851
1852 // We don't need a rotate if the bit group is confined to the lower
1853 // 32 bits.
1854 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1855 continue;
1856
1857 NeedsRotate = true;
1858 break;
1859 }
1860 }
1861
1862 if (NeedsRotate)
1863 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1864 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1865 InstCnt);
1866 else
1867 Res = VRI.V;
1868
1869 // Now, remove all groups with this underlying value and rotation factor.
1870 if (Res)
Benjamin Kramere7561b82015-06-20 15:59:41 +00001871 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1872 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt &&
1873 BG.Repl32 == VRI.Repl32;
1874 });
Hal Finkelc58ce412015-01-01 02:53:29 +00001875 }
1876
1877 // Because 64-bit rotates are more flexible than inserts, we might have a
1878 // preference regarding which one we do first (to save one instruction).
1879 if (!Res)
1880 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1881 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1882 false) <
1883 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1884 true)) {
1885 if (I != BitGroups.begin()) {
1886 BitGroup BG = *I;
1887 BitGroups.erase(I);
1888 BitGroups.insert(BitGroups.begin(), BG);
1889 }
1890
1891 break;
1892 }
1893 }
1894
1895 // Insert the other groups (one at a time).
1896 for (auto &BG : BitGroups) {
1897 if (!Res)
1898 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1899 BG.EndIdx, InstCnt);
1900 else
1901 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1902 BG.StartIdx, BG.EndIdx, InstCnt);
1903 }
1904
1905 if (LateMask) {
1906 uint64_t Mask = getZerosMask();
1907
1908 // We can use the 32-bit andi/andis technique if the mask does not
1909 // require any higher-order bits. This can save an instruction compared
1910 // to always using the general 64-bit technique.
1911 bool Use32BitInsts = isUInt<32>(Mask);
1912 // Compute the masks for andi/andis that would be necessary.
1913 unsigned ANDIMask = (Mask & UINT16_MAX),
1914 ANDISMask = (Mask >> 16) & UINT16_MAX;
1915
1916 if (Use32BitInsts) {
1917 assert((ANDIMask != 0 || ANDISMask != 0) &&
1918 "No set bits in mask when using 32-bit ands for 64-bit value");
1919
1920 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1921 (unsigned) (ANDISMask != 0) +
1922 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1923
1924 SDValue ANDIVal, ANDISVal;
1925 if (ANDIMask != 0)
1926 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001927 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001928 if (ANDISMask != 0)
1929 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001930 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001931
1932 if (!ANDIVal)
1933 Res = ANDISVal;
1934 else if (!ANDISVal)
1935 Res = ANDIVal;
1936 else
1937 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1938 ANDIVal, ANDISVal), 0);
1939 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001940 if (InstCnt) *InstCnt += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001941
Justin Bognerdc8af062016-05-20 21:43:23 +00001942 SDValue MaskVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001943 Res =
1944 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1945 Res, MaskVal), 0);
1946 }
1947 }
1948
1949 return Res.getNode();
1950 }
1951
1952 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1953 // Fill in BitGroups.
1954 collectBitGroups(LateMask);
1955 if (BitGroups.empty())
1956 return nullptr;
1957
1958 // For 64-bit values, figure out when we can use 32-bit instructions.
1959 if (Bits.size() == 64)
1960 assignRepl32BitGroups();
1961
1962 // Fill in ValueRotsVec.
1963 collectValueRotInfo();
1964
1965 if (Bits.size() == 32) {
1966 return Select32(N, LateMask, InstCnt);
1967 } else {
1968 assert(Bits.size() == 64 && "Not 64 bits here?");
1969 return Select64(N, LateMask, InstCnt);
1970 }
1971
1972 return nullptr;
1973 }
1974
Benjamin Kramere7561b82015-06-20 15:59:41 +00001975 void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) {
David Majnemerc7004902016-08-12 04:32:37 +00001976 BitGroups.erase(remove_if(BitGroups, F), BitGroups.end());
Benjamin Kramere7561b82015-06-20 15:59:41 +00001977 }
1978
Hal Finkel8adf2252014-12-16 05:51:41 +00001979 SmallVector<ValueBit, 64> Bits;
1980
1981 bool HasZeros;
1982 SmallVector<unsigned, 64> RLAmt;
1983
1984 SmallVector<BitGroup, 16> BitGroups;
1985
1986 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
1987 SmallVector<ValueRotInfo, 16> ValueRotsVec;
1988
1989 SelectionDAG *CurDAG;
1990
1991public:
1992 BitPermutationSelector(SelectionDAG *DAG)
1993 : CurDAG(DAG) {}
1994
1995 // Here we try to match complex bit permutations into a set of
1996 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
1997 // known to produce optimial code for common cases (like i32 byte swapping).
1998 SDNode *Select(SDNode *N) {
Tim Shendc698c32016-08-12 18:40:04 +00001999 Memoizer.clear();
2000 auto Result =
2001 getValueBits(SDValue(N, 0), N->getValueType(0).getSizeInBits());
2002 if (!Result.first)
Hal Finkel8adf2252014-12-16 05:51:41 +00002003 return nullptr;
Tim Shendc698c32016-08-12 18:40:04 +00002004 Bits = std::move(*Result.second);
Hal Finkel8adf2252014-12-16 05:51:41 +00002005
2006 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
2007 " selection for: ");
2008 DEBUG(N->dump(CurDAG));
2009
2010 // Fill it RLAmt and set HasZeros.
2011 computeRotationAmounts();
2012
Hal Finkelc58ce412015-01-01 02:53:29 +00002013 if (!HasZeros)
2014 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00002015
Hal Finkelc58ce412015-01-01 02:53:29 +00002016 // We currently have two techniques for handling results with zeros: early
2017 // masking (the default) and late masking. Late masking is sometimes more
2018 // efficient, but because the structure of the bit groups is different, it
2019 // is hard to tell without generating both and comparing the results. With
2020 // late masking, we ignore zeros in the resulting value when inserting each
2021 // set of bit groups, and then mask in the zeros at the end. With early
2022 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00002023
Hal Finkelc58ce412015-01-01 02:53:29 +00002024 unsigned InstCnt, InstCntLateMask;
2025 DEBUG(dbgs() << "\tEarly masking:\n");
2026 SDNode *RN = Select(N, false, &InstCnt);
2027 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
2028
2029 DEBUG(dbgs() << "\tLate masking:\n");
2030 SDNode *RNLM = Select(N, true, &InstCntLateMask);
2031 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
2032 " instructions\n");
2033
2034 if (InstCnt <= InstCntLateMask) {
2035 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
2036 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00002037 }
2038
Hal Finkelc58ce412015-01-01 02:53:29 +00002039 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
2040 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00002041 }
2042};
Eugene Zelenko8187c192017-01-13 00:58:58 +00002043
2044} // end anonymous namespace
Hal Finkel8adf2252014-12-16 05:51:41 +00002045
Justin Bognerdc8af062016-05-20 21:43:23 +00002046bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) {
Hal Finkel8adf2252014-12-16 05:51:41 +00002047 if (N->getValueType(0) != MVT::i32 &&
2048 N->getValueType(0) != MVT::i64)
Justin Bognerdc8af062016-05-20 21:43:23 +00002049 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002050
Hal Finkelc58ce412015-01-01 02:53:29 +00002051 if (!UseBitPermRewriter)
Justin Bognerdc8af062016-05-20 21:43:23 +00002052 return false;
Hal Finkelc58ce412015-01-01 02:53:29 +00002053
Hal Finkel8adf2252014-12-16 05:51:41 +00002054 switch (N->getOpcode()) {
2055 default: break;
2056 case ISD::ROTL:
2057 case ISD::SHL:
2058 case ISD::SRL:
2059 case ISD::AND:
2060 case ISD::OR: {
2061 BitPermutationSelector BPS(CurDAG);
Justin Bognerdc8af062016-05-20 21:43:23 +00002062 if (SDNode *New = BPS.Select(N)) {
2063 ReplaceNode(N, New);
2064 return true;
2065 }
2066 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002067 }
2068 }
2069
Justin Bognerdc8af062016-05-20 21:43:23 +00002070 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002071}
2072
Chris Lattner2a1823d2005-08-21 18:50:37 +00002073/// SelectCC - Select a comparison of the specified values with the specified
2074/// condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002075SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2076 const SDLoc &dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002077 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00002078 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00002079
Owen Anderson9f944592009-08-11 20:47:22 +00002080 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00002081 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00002082 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2083 if (isInt32Immediate(RHS, Imm)) {
2084 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002085 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002086 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002087 getI32Imm(Imm & 0xFFFF, dl)),
2088 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002089 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002090 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002091 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002092 getI32Imm(Imm & 0xFFFF, dl)),
2093 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002094
Chris Lattneraa3926b2006-09-20 04:25:47 +00002095 // For non-equality comparisons, the default code would materialize the
2096 // constant, then compare against it, like this:
2097 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002098 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00002099 // cmpw cr0, r3, r2
2100 // Since we are just comparing for equality, we can emit this instead:
2101 // xoris r0,r3,0x1234
2102 // cmplwi cr0,r0,0x5678
2103 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00002104 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002105 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002106 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002107 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002108 }
2109 Opc = PPC::CMPLW;
2110 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002111 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002112 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002113 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002114 Opc = PPC::CMPLW;
2115 } else {
Lei Huang31710412017-07-07 21:12:35 +00002116 int16_t SImm;
Chris Lattner97b3da12006-06-27 00:04:13 +00002117 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002118 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002119 getI32Imm((int)SImm & 0xFFFF,
2120 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002121 0);
2122 Opc = PPC::CMPW;
2123 }
Owen Anderson9f944592009-08-11 20:47:22 +00002124 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002125 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002126 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002127 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002128 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002129 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002130 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002131 getI32Imm(Imm & 0xFFFF, dl)),
2132 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002133 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002134 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002135 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002136 getI32Imm(Imm & 0xFFFF, dl)),
2137 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002138
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002139 // For non-equality comparisons, the default code would materialize the
2140 // constant, then compare against it, like this:
2141 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002142 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002143 // cmpd cr0, r3, r2
2144 // Since we are just comparing for equality, we can emit this instead:
2145 // xoris r0,r3,0x1234
2146 // cmpldi cr0,r0,0x5678
2147 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002148 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002149 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002150 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002151 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002152 getI64Imm(Imm & 0xFFFF, dl)),
2153 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002154 }
2155 }
2156 Opc = PPC::CMPLD;
2157 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002158 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002159 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002160 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002161 Opc = PPC::CMPLD;
2162 } else {
Lei Huang31710412017-07-07 21:12:35 +00002163 int16_t SImm;
Chris Lattner97b3da12006-06-27 00:04:13 +00002164 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002165 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002166 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002167 0);
2168 Opc = PPC::CMPD;
2169 }
Owen Anderson9f944592009-08-11 20:47:22 +00002170 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002171 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002172 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002173 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002174 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002175 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002176 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002177}
2178
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002179static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002180 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002181 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002182 case ISD::SETONE:
2183 case ISD::SETOLE:
2184 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002185 llvm_unreachable("Should be lowered by legalize!");
2186 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002187 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002188 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002189 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002190 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002191 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002192 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002193 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002194 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002195 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002196 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002197 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002198 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002199 case ISD::SETO: return PPC::PRED_NU;
2200 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002201 // These two are invalid for floating point. Assume we have int.
2202 case ISD::SETULT: return PPC::PRED_LT;
2203 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002204 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002205}
2206
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002207/// getCRIdxForSetCC - Return the index of the condition register field
2208/// associated with the SetCC condition, and whether or not the field is
2209/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002210static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002211 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002212 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002213 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002214 case ISD::SETOLT:
2215 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2216 case ISD::SETOGT:
2217 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2218 case ISD::SETOEQ:
2219 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2220 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002221 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002222 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002223 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002224 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002225 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002226 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2227 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002228 case ISD::SETUEQ:
2229 case ISD::SETOGE:
2230 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002231 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002232 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002233 // These are invalid for floating point. Assume integer.
2234 case ISD::SETULT: return 0;
2235 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002236 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002237}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002238
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002239// getVCmpInst: return the vector compare instruction for the specified
2240// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002241// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002242static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2243 bool HasVSX, bool &Swap, bool &Negate) {
2244 Swap = false;
2245 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002246
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002247 if (VecVT.isFloatingPoint()) {
2248 /* Handle some cases by swapping input operands. */
2249 switch (CC) {
2250 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2251 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2252 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2253 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2254 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2255 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2256 default: break;
2257 }
2258 /* Handle some cases by negating the result. */
2259 switch (CC) {
2260 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2261 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2262 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2263 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2264 default: break;
2265 }
2266 /* We have instructions implementing the remaining cases. */
2267 switch (CC) {
2268 case ISD::SETEQ:
2269 case ISD::SETOEQ:
2270 if (VecVT == MVT::v4f32)
2271 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2272 else if (VecVT == MVT::v2f64)
2273 return PPC::XVCMPEQDP;
2274 break;
2275 case ISD::SETGT:
2276 case ISD::SETOGT:
2277 if (VecVT == MVT::v4f32)
2278 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2279 else if (VecVT == MVT::v2f64)
2280 return PPC::XVCMPGTDP;
2281 break;
2282 case ISD::SETGE:
2283 case ISD::SETOGE:
2284 if (VecVT == MVT::v4f32)
2285 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2286 else if (VecVT == MVT::v2f64)
2287 return PPC::XVCMPGEDP;
2288 break;
2289 default:
2290 break;
2291 }
2292 llvm_unreachable("Invalid floating-point vector compare condition");
2293 } else {
2294 /* Handle some cases by swapping input operands. */
2295 switch (CC) {
2296 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2297 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2298 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2299 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2300 default: break;
2301 }
2302 /* Handle some cases by negating the result. */
2303 switch (CC) {
2304 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2305 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2306 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2307 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2308 default: break;
2309 }
2310 /* We have instructions implementing the remaining cases. */
2311 switch (CC) {
2312 case ISD::SETEQ:
2313 case ISD::SETUEQ:
2314 if (VecVT == MVT::v16i8)
2315 return PPC::VCMPEQUB;
2316 else if (VecVT == MVT::v8i16)
2317 return PPC::VCMPEQUH;
2318 else if (VecVT == MVT::v4i32)
2319 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002320 else if (VecVT == MVT::v2i64)
2321 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002322 break;
2323 case ISD::SETGT:
2324 if (VecVT == MVT::v16i8)
2325 return PPC::VCMPGTSB;
2326 else if (VecVT == MVT::v8i16)
2327 return PPC::VCMPGTSH;
2328 else if (VecVT == MVT::v4i32)
2329 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002330 else if (VecVT == MVT::v2i64)
2331 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002332 break;
2333 case ISD::SETUGT:
2334 if (VecVT == MVT::v16i8)
2335 return PPC::VCMPGTUB;
2336 else if (VecVT == MVT::v8i16)
2337 return PPC::VCMPGTUH;
2338 else if (VecVT == MVT::v4i32)
2339 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002340 else if (VecVT == MVT::v2i64)
2341 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002342 break;
2343 default:
2344 break;
2345 }
2346 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002347 }
2348}
2349
Justin Bognerdc8af062016-05-20 21:43:23 +00002350bool PPCDAGToDAGISel::trySETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002351 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002352 unsigned Imm;
2353 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002354 EVT PtrVT =
2355 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002356 bool isPPC64 = (PtrVT == MVT::i64);
2357
Eric Christopher1b8e7632014-05-22 01:07:24 +00002358 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002359 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002360 // We can codegen setcc op, imm very efficiently compared to a brcond.
2361 // Check for those cases here.
2362 // setcc op, 0
2363 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002364 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002365 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002366 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002367 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002368 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002369 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
2370 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002371 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2372 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002373 }
Chris Lattnere2969492005-10-21 21:17:10 +00002374 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002375 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002376 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002377 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002378 Op, getI32Imm(~0U, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002379 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
2380 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002381 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002382 case ISD::SETLT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002383 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2384 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002385 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2386 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002387 }
Chris Lattnere2969492005-10-21 21:17:10 +00002388 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002389 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002390 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2391 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002392 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
2393 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002394 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2395 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002396 }
2397 }
Chris Lattner491b8292005-10-06 19:03:35 +00002398 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002399 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002400 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002401 default: break;
2402 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002403 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002404 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002405 Op, getI32Imm(1, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002406 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2407 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
2408 MVT::i32,
2409 getI32Imm(0, dl)),
2410 0), Op.getValue(1));
2411 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002412 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002413 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002414 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002415 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002416 Op, getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00002417 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op,
2418 SDValue(AD, 1));
2419 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002420 }
Chris Lattnere2969492005-10-21 21:17:10 +00002421 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002422 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002423 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002424 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2425 Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002426 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
2427 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002428 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2429 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002430 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002431 case ISD::SETGT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002432 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2433 getI32Imm(31, dl) };
2434 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002435 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl));
2436 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002437 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002438 }
Chris Lattner491b8292005-10-06 19:03:35 +00002439 }
2440 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002441
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002442 SDValue LHS = N->getOperand(0);
2443 SDValue RHS = N->getOperand(1);
2444
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002445 // Altivec Vector compare instructions do not set any CR register by default and
2446 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002447 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002448 if (PPCSubTarget->hasQPX())
Justin Bognerdc8af062016-05-20 21:43:23 +00002449 return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002450
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002451 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002452 bool Swap, Negate;
2453 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2454 PPCSubTarget->hasVSX(), Swap, Negate);
2455 if (Swap)
2456 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002457
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002458 EVT ResVT = VecVT.changeVectorElementTypeToInteger();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002459 if (Negate) {
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002460 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002461 CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR,
2462 ResVT, VCmp, VCmp);
2463 return true;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002464 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002465
Justin Bognerdc8af062016-05-20 21:43:23 +00002466 CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS);
2467 return true;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002468 }
2469
Eric Christopher1b8e7632014-05-22 01:07:24 +00002470 if (PPCSubTarget->useCRBits())
Justin Bognerdc8af062016-05-20 21:43:23 +00002471 return false;
Hal Finkel940ab932014-02-28 00:27:01 +00002472
Chris Lattner491b8292005-10-06 19:03:35 +00002473 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002474 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002475 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002476 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002477
Chris Lattner491b8292005-10-06 19:03:35 +00002478 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002479 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002480
Craig Topper062a2ba2014-04-25 05:30:21 +00002481 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002482 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002483 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002484
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002485 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2486 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002487
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002488 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
2489 getI32Imm(31, dl), getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002490 if (!Inv) {
2491 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2492 return true;
2493 }
Chris Lattner89f36e62008-01-08 06:46:30 +00002494
2495 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002496 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002497 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002498 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
2499 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002500}
Chris Lattner502a3692005-10-06 18:56:10 +00002501
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002502// Is this opcode a bitwise logical operation?
2503static bool isLogicOp(unsigned Opc) {
2504 return Opc == ISD::AND || Opc == ISD::OR || Opc == ISD::XOR;
2505}
2506
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002507/// If this node is a sign/zero extension of an integer comparison,
2508/// it can usually be computed in GPR's rather than using comparison
2509/// instructions and ISEL. We only do this on 64-bit targets for now
2510/// as the code is specialized for 64-bit (it uses 64-bit instructions
2511/// and assumes 64-bit registers).
2512bool PPCDAGToDAGISel::tryEXTEND(SDNode *N) {
2513 if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64())
2514 return false;
2515 assert((N->getOpcode() == ISD::ZERO_EXTEND ||
2516 N->getOpcode() == ISD::SIGN_EXTEND) &&
2517 "Expecting a zero/sign extend node!");
2518
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002519 SDValue WideRes;
2520 // If we are zero-extending the result of a logical operation on i1
2521 // values, we can keep the values in GPRs.
2522 if (isLogicOp(N->getOperand(0).getOpcode()) &&
2523 N->getOperand(0).getValueType() == MVT::i1 &&
2524 N->getOpcode() == ISD::ZERO_EXTEND)
2525 WideRes = computeLogicOpInGPR(N->getOperand(0));
2526 else if (N->getOperand(0).getOpcode() != ISD::SETCC)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002527 return false;
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002528 else
2529 WideRes =
2530 getSETCCInGPR(N->getOperand(0),
2531 N->getOpcode() == ISD::SIGN_EXTEND ?
2532 SetccInGPROpts::SExtOrig : SetccInGPROpts::ZExtOrig);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002533
2534 if (!WideRes)
2535 return false;
2536
2537 SDLoc dl(N);
2538 bool Inputs32Bit = N->getOperand(0).getOperand(0).getValueType() == MVT::i32;
2539 bool Output32Bit = N->getValueType(0) == MVT::i32;
2540
2541 NumSextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 1 : 0;
2542 NumZextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 0 : 1;
2543
2544 SDValue ConvOp = WideRes;
2545 if (Inputs32Bit != Output32Bit)
2546 ConvOp = addExtOrTrunc(WideRes, Inputs32Bit ? ExtOrTruncConversion::Ext :
2547 ExtOrTruncConversion::Trunc);
2548 ReplaceNode(N, ConvOp.getNode());
2549
2550 return true;
2551}
2552
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002553// Lower a logical operation on i1 values into a GPR sequence if possible.
2554// The result can be kept in a GPR if requested.
2555// Three types of inputs can be handled:
2556// - SETCC
2557// - TRUNCATE
2558// - Logical operation (AND/OR/XOR)
2559// There is also a special case that is handled (namely a complement operation
2560// achieved with xor %a, -1).
2561SDValue PPCDAGToDAGISel::computeLogicOpInGPR(SDValue LogicOp) {
2562 assert(isLogicOp(LogicOp.getOpcode()) &&
2563 "Can only handle logic operations here.");
2564 assert(LogicOp.getValueType() == MVT::i1 &&
2565 "Can only handle logic operations on i1 values here.");
2566 SDLoc dl(LogicOp);
2567 SDValue LHS, RHS;
2568
2569 // Special case: xor %a, -1
2570 bool IsBitwiseNegation = isBitwiseNot(LogicOp);
2571
2572 // Produces a GPR sequence for each operand of the binary logic operation.
2573 // For SETCC, it produces the respective comparison, for TRUNCATE it truncates
2574 // the value in a GPR and for logic operations, it will recursively produce
2575 // a GPR sequence for the operation.
2576 auto getLogicOperand = [&] (SDValue Operand) -> SDValue {
2577 unsigned OperandOpcode = Operand.getOpcode();
2578 if (OperandOpcode == ISD::SETCC)
2579 return getSETCCInGPR(Operand, SetccInGPROpts::ZExtOrig);
2580 else if (OperandOpcode == ISD::TRUNCATE) {
2581 SDValue InputOp = Operand.getOperand(0);
2582 EVT InVT = InputOp.getValueType();
2583 return
2584 SDValue(CurDAG->getMachineNode(InVT == MVT::i32 ? PPC::RLDICL_32 :
2585 PPC::RLDICL, dl, InVT, InputOp,
2586 getI64Imm(0, dl), getI64Imm(63, dl)), 0);
2587 } else if (isLogicOp(OperandOpcode))
2588 return computeLogicOpInGPR(Operand);
2589 return SDValue();
2590 };
2591 LHS = getLogicOperand(LogicOp.getOperand(0));
2592 RHS = getLogicOperand(LogicOp.getOperand(1));
2593
2594 // If a GPR sequence can't be produced for the LHS we can't proceed.
2595 // Not producing a GPR sequence for the RHS is only a problem if this isn't
2596 // a bitwise negation operation.
2597 if (!LHS || (!RHS && !IsBitwiseNegation))
2598 return SDValue();
2599
2600 NumLogicOpsOnComparison++;
2601
2602 // We will use the inputs as 64-bit values.
2603 if (LHS.getValueType() == MVT::i32)
2604 LHS = addExtOrTrunc(LHS, ExtOrTruncConversion::Ext);
2605 if (!IsBitwiseNegation && RHS.getValueType() == MVT::i32)
2606 RHS = addExtOrTrunc(RHS, ExtOrTruncConversion::Ext);
2607
2608 unsigned NewOpc;
2609 switch (LogicOp.getOpcode()) {
2610 default: llvm_unreachable("Unknown logic operation.");
2611 case ISD::AND: NewOpc = PPC::AND8; break;
2612 case ISD::OR: NewOpc = PPC::OR8; break;
2613 case ISD::XOR: NewOpc = PPC::XOR8; break;
2614 }
2615
2616 if (IsBitwiseNegation) {
2617 RHS = getI64Imm(1, dl);
2618 NewOpc = PPC::XORI8;
2619 }
2620
2621 return SDValue(CurDAG->getMachineNode(NewOpc, dl, MVT::i64, LHS, RHS), 0);
2622
2623}
2624
2625/// Try performing logical operations on results of comparisons in GPRs.
2626/// It is typically preferred from a performance perspective over performing
2627/// the operations on individual bits in the CR. We only do this on 64-bit
2628/// targets for now as the code is specialized for 64-bit (it uses 64-bit
2629/// instructions and assumes 64-bit registers).
2630bool PPCDAGToDAGISel::tryLogicOpOfCompares(SDNode *N) {
2631 if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64())
2632 return false;
2633 if (N->getValueType(0) != MVT::i1)
2634 return false;
2635 assert(isLogicOp(N->getOpcode()) &&
2636 "Expected a logic operation on setcc results.");
2637 SDValue LoweredLogical = computeLogicOpInGPR(SDValue(N, 0));
2638 if (!LoweredLogical)
2639 return false;
2640
2641 SDLoc dl(N);
2642 bool IsBitwiseNegate = LoweredLogical.getMachineOpcode() == PPC::XORI8;
2643 unsigned SubRegToExtract = IsBitwiseNegate ? PPC::sub_eq : PPC::sub_gt;
2644 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2645 SDValue LHS = LoweredLogical.getOperand(0);
2646 SDValue RHS = LoweredLogical.getOperand(1);
2647 SDValue WideOp;
2648 SDValue OpToConvToRecForm;
2649
2650 // Look through any 32-bit to 64-bit implicit extend nodes to find the opcode
2651 // that is input to the XORI.
2652 if (IsBitwiseNegate &&
2653 LoweredLogical.getOperand(0).getMachineOpcode() == PPC::INSERT_SUBREG)
2654 OpToConvToRecForm = LoweredLogical.getOperand(0).getOperand(1);
2655 else if (IsBitwiseNegate)
2656 // If the input to the XORI isn't an extension, that's what we're after.
2657 OpToConvToRecForm = LoweredLogical.getOperand(0);
2658 else
2659 // If this is not an XORI, it is a reg-reg logical op and we can convert it
2660 // to record-form.
2661 OpToConvToRecForm = LoweredLogical;
2662
2663 // Get the record-form version of the node we're looking to use to get the
2664 // CR result from.
2665 uint16_t NonRecOpc = OpToConvToRecForm.getMachineOpcode();
2666 int NewOpc = PPCInstrInfo::getRecordFormOpcode(NonRecOpc);
2667
2668 // Convert the right node to record-form. This is either the logical we're
2669 // looking at or it is the input node to the negation (if we're looking at
2670 // a bitwise negation).
2671 if (NewOpc != -1 && IsBitwiseNegate) {
2672 // The input to the XORI has a record-form. Use it.
2673 assert(LoweredLogical.getConstantOperandVal(1) == 1 &&
2674 "Expected a PPC::XORI8 only for bitwise negation.");
2675 // Emit the record-form instruction.
2676 std::vector<SDValue> Ops;
2677 for (int i = 0, e = OpToConvToRecForm.getNumOperands(); i < e; i++)
2678 Ops.push_back(OpToConvToRecForm.getOperand(i));
2679
2680 WideOp =
2681 SDValue(CurDAG->getMachineNode(NewOpc, dl,
2682 OpToConvToRecForm.getValueType(),
2683 MVT::Glue, Ops), 0);
2684 } else {
2685 assert((NewOpc != -1 || !IsBitwiseNegate) &&
2686 "No record form available for AND8/OR8/XOR8?");
2687 WideOp =
2688 SDValue(CurDAG->getMachineNode(NewOpc == -1 ? PPC::ANDIo8 : NewOpc, dl,
2689 MVT::i64, MVT::Glue, LHS, RHS), 0);
2690 }
2691
2692 // Select this node to a single bit from CR0 set by the record-form node
2693 // just created. For bitwise negation, use the EQ bit which is the equivalent
2694 // of negating the result (i.e. it is a bit set when the result of the
2695 // operation is zero).
2696 SDValue SRIdxVal =
2697 CurDAG->getTargetConstant(SubRegToExtract, dl, MVT::i32);
2698 SDValue CRBit =
2699 SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
2700 MVT::i1, CR0Reg, SRIdxVal,
2701 WideOp.getValue(1)), 0);
2702 ReplaceNode(N, CRBit.getNode());
2703 return true;
2704}
2705
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002706/// If the value isn't guaranteed to be sign-extended to 64-bits, extend it.
2707/// Useful when emitting comparison code for 32-bit values without using
2708/// the compare instruction (which only considers the lower 32-bits).
2709SDValue PPCDAGToDAGISel::signExtendInputIfNeeded(SDValue Input) {
2710 assert(Input.getValueType() == MVT::i32 &&
2711 "Can only sign-extend 32-bit values here.");
2712 unsigned Opc = Input.getOpcode();
2713
2714 // The value was sign extended and then truncated to 32-bits. No need to
2715 // sign extend it again.
2716 if (Opc == ISD::TRUNCATE &&
2717 (Input.getOperand(0).getOpcode() == ISD::AssertSext ||
2718 Input.getOperand(0).getOpcode() == ISD::SIGN_EXTEND))
2719 return Input;
2720
2721 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2722 // The input is a sign-extending load. No reason to sign-extend.
2723 if (InputLoad && InputLoad->getExtensionType() == ISD::SEXTLOAD)
2724 return Input;
2725
2726 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2727 // We don't sign-extend constants and already sign-extended values.
2728 if (InputConst || Opc == ISD::AssertSext || Opc == ISD::SIGN_EXTEND_INREG ||
2729 Opc == ISD::SIGN_EXTEND)
2730 return Input;
2731
2732 SDLoc dl(Input);
2733 SignExtensionsAdded++;
2734 return SDValue(CurDAG->getMachineNode(PPC::EXTSW_32, dl, MVT::i32, Input), 0);
2735}
2736
2737/// If the value isn't guaranteed to be zero-extended to 64-bits, extend it.
2738/// Useful when emitting comparison code for 32-bit values without using
2739/// the compare instruction (which only considers the lower 32-bits).
2740SDValue PPCDAGToDAGISel::zeroExtendInputIfNeeded(SDValue Input) {
2741 assert(Input.getValueType() == MVT::i32 &&
2742 "Can only zero-extend 32-bit values here.");
2743 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2744 unsigned Opc = Input.getOpcode();
2745
2746 // No need to zero-extend loaded values (unless they're loaded with
2747 // a sign-extending load).
2748 if (InputLoad && InputLoad->getExtensionType() != ISD::SEXTLOAD)
2749 return Input;
2750
2751 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2752 bool InputZExtConst = InputConst && InputConst->getSExtValue() >= 0;
2753 // An ISD::TRUNCATE will be lowered to an EXTRACT_SUBREG so we have
2754 // to conservatively actually clear the high bits. We also don't need to
2755 // zero-extend constants or values that are already zero-extended.
2756 if (InputZExtConst || Opc == ISD::AssertZext || Opc == ISD::ZERO_EXTEND)
2757 return Input;
2758
2759 SDLoc dl(Input);
2760 ZeroExtensionsAdded++;
2761 return SDValue(CurDAG->getMachineNode(PPC::RLDICL_32, dl, MVT::i32, Input,
2762 getI64Imm(0, dl), getI64Imm(32, dl)),
2763 0);
2764}
2765
2766// Handle a 32-bit value in a 64-bit register and vice-versa. These are of
2767// course not actual zero/sign extensions that will generate machine code,
2768// they're just a way to reinterpret a 32 bit value in a register as a
2769// 64 bit value and vice-versa.
2770SDValue PPCDAGToDAGISel::addExtOrTrunc(SDValue NatWidthRes,
2771 ExtOrTruncConversion Conv) {
2772 SDLoc dl(NatWidthRes);
2773
2774 // For reinterpreting 32-bit values as 64 bit values, we generate
2775 // INSERT_SUBREG IMPLICIT_DEF:i64, <input>, TargetConstant:i32<1>
2776 if (Conv == ExtOrTruncConversion::Ext) {
2777 SDValue ImDef(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, MVT::i64), 0);
2778 SDValue SubRegIdx =
2779 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2780 return SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, MVT::i64,
2781 ImDef, NatWidthRes, SubRegIdx), 0);
2782 }
2783
2784 assert(Conv == ExtOrTruncConversion::Trunc &&
2785 "Unknown convertion between 32 and 64 bit values.");
2786 // For reinterpreting 64-bit values as 32-bit values, we just need to
2787 // EXTRACT_SUBREG (i.e. extract the low word).
2788 SDValue SubRegIdx =
2789 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2790 return SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, MVT::i32,
2791 NatWidthRes, SubRegIdx), 0);
2792}
2793
2794/// Produces a zero-extended result of comparing two 32-bit values according to
2795/// the passed condition code.
2796SDValue PPCDAGToDAGISel::get32BitZExtCompare(SDValue LHS, SDValue RHS,
2797 ISD::CondCode CC,
2798 int64_t RHSValue, SDLoc dl) {
2799 bool IsRHSZero = RHSValue == 0;
2800 switch (CC) {
2801 default: return SDValue();
2802 case ISD::SETEQ: {
2803 // (zext (setcc %a, %b, seteq)) -> (lshr (cntlzw (xor %a, %b)), 5)
2804 // (zext (setcc %a, 0, seteq)) -> (lshr (cntlzw %a), 5)
2805 SDValue Xor = IsRHSZero ? LHS :
2806 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2807 SDValue Clz =
2808 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2809 SDValue ShiftOps[] = { Clz, getI32Imm(27, dl), getI32Imm(5, dl),
2810 getI32Imm(31, dl) };
2811 return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
2812 ShiftOps), 0);
2813 }
Nemanja Ivanovicbb67f842017-06-07 12:23:41 +00002814 case ISD::SETNE: {
2815 // (zext (setcc %a, %b, setne)) -> (xor (lshr (cntlzw (xor %a, %b)), 5), 1)
2816 // (zext (setcc %a, 0, setne)) -> (xor (lshr (cntlzw %a), 5), 1)
2817 SDValue Xor = IsRHSZero ? LHS :
2818 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2819 SDValue Clz =
2820 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2821 SDValue ShiftOps[] = { Clz, getI32Imm(27, dl), getI32Imm(5, dl),
2822 getI32Imm(31, dl) };
2823 SDValue Shift =
2824 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0);
2825 return SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift,
2826 getI32Imm(1, dl)), 0);
2827 }
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002828 }
2829}
2830
2831/// Produces a sign-extended result of comparing two 32-bit values according to
2832/// the passed condition code.
2833SDValue PPCDAGToDAGISel::get32BitSExtCompare(SDValue LHS, SDValue RHS,
2834 ISD::CondCode CC,
2835 int64_t RHSValue, SDLoc dl) {
2836 bool IsRHSZero = RHSValue == 0;
2837 switch (CC) {
2838 default: return SDValue();
2839 case ISD::SETEQ: {
2840 // (sext (setcc %a, %b, seteq)) ->
2841 // (ashr (shl (ctlz (xor %a, %b)), 58), 63)
2842 // (sext (setcc %a, 0, seteq)) ->
2843 // (ashr (shl (ctlz %a), 58), 63)
2844 SDValue CountInput = IsRHSZero ? LHS :
2845 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2846 SDValue Cntlzw =
2847 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, CountInput), 0);
2848 SDValue SHLOps[] = { Cntlzw, getI32Imm(58, dl), getI32Imm(0, dl) };
2849 SDValue Sldi =
2850 SDValue(CurDAG->getMachineNode(PPC::RLDICR_32, dl, MVT::i32, SHLOps), 0);
2851 return SDValue(CurDAG->getMachineNode(PPC::SRADI_32, dl, MVT::i32, Sldi,
2852 getI32Imm(63, dl)), 0);
2853 }
Nemanja Ivanovicbb67f842017-06-07 12:23:41 +00002854 case ISD::SETNE: {
2855 // Bitwise xor the operands, count leading zeros, shift right by 5 bits and
2856 // flip the bit, finally take 2's complement.
2857 // (sext (setcc %a, %b, setne)) ->
2858 // (neg (xor (lshr (ctlz (xor %a, %b)), 5), 1))
2859 // Same as above, but the first xor is not needed.
2860 // (sext (setcc %a, 0, setne)) ->
2861 // (neg (xor (lshr (ctlz %a), 5), 1))
2862 SDValue Xor = IsRHSZero ? LHS :
2863 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2864 SDValue Clz =
2865 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2866 SDValue ShiftOps[] =
2867 { Clz, getI32Imm(27, dl), getI32Imm(5, dl), getI32Imm(31, dl) };
2868 SDValue Shift =
2869 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0);
2870 SDValue Xori =
2871 SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift,
2872 getI32Imm(1, dl)), 0);
2873 return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Xori), 0);
2874 }
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002875 }
2876}
2877
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002878/// Produces a zero-extended result of comparing two 64-bit values according to
2879/// the passed condition code.
2880SDValue PPCDAGToDAGISel::get64BitZExtCompare(SDValue LHS, SDValue RHS,
2881 ISD::CondCode CC,
2882 int64_t RHSValue, SDLoc dl) {
2883 bool IsRHSZero = RHSValue == 0;
2884 switch (CC) {
2885 default: return SDValue();
2886 case ISD::SETEQ: {
2887 // (zext (setcc %a, %b, seteq)) -> (lshr (ctlz (xor %a, %b)), 6)
2888 // (zext (setcc %a, 0, seteq)) -> (lshr (ctlz %a), 6)
2889 SDValue Xor = IsRHSZero ? LHS :
2890 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
2891 SDValue Clz =
2892 SDValue(CurDAG->getMachineNode(PPC::CNTLZD, dl, MVT::i64, Xor), 0);
2893 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Clz,
2894 getI64Imm(58, dl), getI64Imm(63, dl)),
2895 0);
2896 }
2897 }
2898}
2899
2900/// Produces a sign-extended result of comparing two 64-bit values according to
2901/// the passed condition code.
2902SDValue PPCDAGToDAGISel::get64BitSExtCompare(SDValue LHS, SDValue RHS,
2903 ISD::CondCode CC,
2904 int64_t RHSValue, SDLoc dl) {
2905 bool IsRHSZero = RHSValue == 0;
2906 switch (CC) {
2907 default: return SDValue();
2908 case ISD::SETEQ: {
2909 // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1)
2910 // (sext (setcc %a, %b, seteq)) -> (sube addc.reg, addc.reg, addc.CA)
2911 // {addcz.reg, addcz.CA} = (addcarry %a, -1)
2912 // (sext (setcc %a, 0, seteq)) -> (sube addcz.reg, addcz.reg, addcz.CA)
2913 SDValue AddInput = IsRHSZero ? LHS :
2914 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
2915 SDValue Addic =
2916 SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue,
2917 AddInput, getI32Imm(~0U, dl)), 0);
2918 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, Addic,
2919 Addic, Addic.getValue(1)), 0);
2920 }
2921 }
2922}
2923
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002924/// Does this SDValue have any uses for which keeping the value in a GPR is
2925/// appropriate. This is meant to be used on values that have type i1 since
2926/// it is somewhat meaningless to ask if values of other types can be kept in
2927/// GPR's.
2928static bool allUsesExtend(SDValue Compare, SelectionDAG *CurDAG) {
2929 assert(Compare.getOpcode() == ISD::SETCC &&
2930 "An ISD::SETCC node required here.");
2931
2932 // For values that have a single use, the caller should obviously already have
2933 // checked if that use is an extending use. We check the other uses here.
2934 if (Compare.hasOneUse())
2935 return true;
2936 // We want the value in a GPR if it is being extended, used for a select, or
2937 // used in logical operations.
2938 for (auto CompareUse : Compare.getNode()->uses())
2939 if (CompareUse->getOpcode() != ISD::SIGN_EXTEND &&
2940 CompareUse->getOpcode() != ISD::ZERO_EXTEND &&
2941 CompareUse->getOpcode() != ISD::SELECT &&
2942 !isLogicOp(CompareUse->getOpcode())) {
2943 OmittedForNonExtendUses++;
2944 return false;
2945 }
2946 return true;
2947}
2948
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002949/// Returns an equivalent of a SETCC node but with the result the same width as
2950/// the inputs. This can nalso be used for SELECT_CC if either the true or false
2951/// values is a power of two while the other is zero.
2952SDValue PPCDAGToDAGISel::getSETCCInGPR(SDValue Compare,
2953 SetccInGPROpts ConvOpts) {
2954 assert((Compare.getOpcode() == ISD::SETCC ||
2955 Compare.getOpcode() == ISD::SELECT_CC) &&
2956 "An ISD::SETCC node required here.");
2957
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002958 // Don't convert this comparison to a GPR sequence because there are uses
2959 // of the i1 result (i.e. uses that require the result in the CR).
2960 if ((Compare.getOpcode() == ISD::SETCC) && !allUsesExtend(Compare, CurDAG))
2961 return SDValue();
2962
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002963 SDValue LHS = Compare.getOperand(0);
2964 SDValue RHS = Compare.getOperand(1);
2965
2966 // The condition code is operand 2 for SETCC and operand 4 for SELECT_CC.
2967 int CCOpNum = Compare.getOpcode() == ISD::SELECT_CC ? 4 : 2;
2968 ISD::CondCode CC =
2969 cast<CondCodeSDNode>(Compare.getOperand(CCOpNum))->get();
2970 EVT InputVT = LHS.getValueType();
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002971 if (InputVT != MVT::i32 && InputVT != MVT::i64)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002972 return SDValue();
2973
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002974 if (ConvOpts == SetccInGPROpts::ZExtInvert ||
2975 ConvOpts == SetccInGPROpts::SExtInvert)
2976 CC = ISD::getSetCCInverse(CC, true);
2977
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002978 bool Inputs32Bit = InputVT == MVT::i32;
2979 if (ISD::isSignedIntSetCC(CC) && Inputs32Bit) {
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002980 LHS = signExtendInputIfNeeded(LHS);
2981 RHS = signExtendInputIfNeeded(RHS);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002982 } else if (ISD::isUnsignedIntSetCC(CC) && Inputs32Bit) {
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002983 LHS = zeroExtendInputIfNeeded(LHS);
2984 RHS = zeroExtendInputIfNeeded(RHS);
2985 }
2986
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002987 SDLoc dl(Compare);
2988 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
2989 int64_t RHSValue = RHSConst ? RHSConst->getSExtValue() : INT64_MAX;
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002990 bool IsSext = ConvOpts == SetccInGPROpts::SExtOrig ||
2991 ConvOpts == SetccInGPROpts::SExtInvert;
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002992
2993 if (IsSext && Inputs32Bit)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002994 return get32BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002995 else if (Inputs32Bit)
2996 return get32BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
2997 else if (IsSext)
2998 return get64BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
2999 return get64BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00003000}
3001
Justin Bognerdc8af062016-05-20 21:43:23 +00003002void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
Hal Finkelcf599212015-02-25 21:36:59 +00003003 // Transfer memoperands.
3004 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3005 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
3006 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
Hal Finkelcf599212015-02-25 21:36:59 +00003007}
3008
Chris Lattner43ff01e2005-08-17 19:33:03 +00003009// Select - Convert the specified operand from a target-independent to a
3010// target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +00003011void PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003012 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00003013 if (N->isMachineOpcode()) {
3014 N->setNodeId(-1);
Justin Bognerdc8af062016-05-20 21:43:23 +00003015 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00003016 }
Chris Lattner08c319f2005-09-29 00:59:32 +00003017
Hal Finkel51b3fd12014-09-02 06:23:54 +00003018 // In case any misguided DAG-level optimizations form an ADD with a
3019 // TargetConstant operand, crash here instead of miscompiling (by selecting
3020 // an r+r add instead of some kind of r+i add).
3021 if (N->getOpcode() == ISD::ADD &&
3022 N->getOperand(1).getOpcode() == ISD::TargetConstant)
3023 llvm_unreachable("Invalid ADD with TargetConstant operand");
3024
Hal Finkel8adf2252014-12-16 05:51:41 +00003025 // Try matching complex bit permutations before doing anything else.
Justin Bognerdc8af062016-05-20 21:43:23 +00003026 if (tryBitPermutation(N))
3027 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00003028
Chris Lattner43ff01e2005-08-17 19:33:03 +00003029 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00003030 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003031
Eugene Zelenko8187c192017-01-13 00:58:58 +00003032 case ISD::Constant:
Justin Bognerdc8af062016-05-20 21:43:23 +00003033 if (N->getValueType(0) == MVT::i64) {
3034 ReplaceNode(N, getInt64(CurDAG, N));
3035 return;
3036 }
Jim Laskey095e6f32006-12-12 13:23:43 +00003037 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003038
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00003039 case ISD::ZERO_EXTEND:
3040 case ISD::SIGN_EXTEND:
3041 if (tryEXTEND(N))
3042 return;
3043 break;
3044
Eugene Zelenko8187c192017-01-13 00:58:58 +00003045 case ISD::SETCC:
Justin Bognerdc8af062016-05-20 21:43:23 +00003046 if (trySETCC(N))
3047 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003048 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003049
Evan Cheng6dc90ca2006-02-09 00:37:58 +00003050 case PPCISD::GlobalBaseReg:
Justin Bognerdc8af062016-05-20 21:43:23 +00003051 ReplaceNode(N, getGlobalBaseReg());
3052 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00003053
Hal Finkelb5e9b042014-12-11 22:51:06 +00003054 case ISD::FrameIndex:
Justin Bognerdc8af062016-05-20 21:43:23 +00003055 selectFrameIndex(N, N);
3056 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00003057
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00003058 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003059 SDValue InFlag = N->getOperand(1);
Justin Bognerdc8af062016-05-20 21:43:23 +00003060 ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
3061 N->getOperand(0), InFlag));
3062 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00003063 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003064
Eugene Zelenko8187c192017-01-13 00:58:58 +00003065 case PPCISD::READ_TIME_BASE:
Justin Bognerdc8af062016-05-20 21:43:23 +00003066 ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
3067 MVT::Other, N->getOperand(0)));
3068 return;
Hal Finkelbbdee932014-12-02 22:01:00 +00003069
Hal Finkel13d104b2014-12-11 18:37:52 +00003070 case PPCISD::SRA_ADDZE: {
3071 SDValue N0 = N->getOperand(0);
3072 SDValue ShiftAmt =
3073 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003074 getConstantIntValue(), dl,
3075 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00003076 if (N->getValueType(0) == MVT::i64) {
3077 SDNode *Op =
3078 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
3079 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00003080 CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0),
3081 SDValue(Op, 1));
3082 return;
Hal Finkel13d104b2014-12-11 18:37:52 +00003083 } else {
3084 assert(N->getValueType(0) == MVT::i32 &&
3085 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
3086 SDNode *Op =
3087 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
3088 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00003089 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0),
3090 SDValue(Op, 1));
3091 return;
Chris Lattnerdc664572005-08-25 17:50:06 +00003092 }
Chris Lattner6e184f22005-08-25 22:04:30 +00003093 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003094
Chris Lattnerce645542006-11-10 02:08:47 +00003095 case ISD::LOAD: {
3096 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00003097 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003098 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00003099
Chris Lattnerce645542006-11-10 02:08:47 +00003100 // Normal loads are handled by code generated from the .td file.
3101 if (LD->getAddressingMode() != ISD::PRE_INC)
3102 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003103
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003104 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00003105 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00003106 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00003107
Chris Lattner474b5b72006-11-15 19:55:13 +00003108 unsigned Opcode;
3109 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00003110 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00003111 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00003112 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3113 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003114 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00003115 case MVT::f64: Opcode = PPC::LFDU; break;
3116 case MVT::f32: Opcode = PPC::LFSU; break;
3117 case MVT::i32: Opcode = PPC::LWZU; break;
3118 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
3119 case MVT::i1:
3120 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00003121 }
3122 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00003123 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
3124 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3125 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003126 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00003127 case MVT::i64: Opcode = PPC::LDU; break;
3128 case MVT::i32: Opcode = PPC::LWZU8; break;
3129 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
3130 case MVT::i1:
3131 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00003132 }
3133 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003134
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003135 SDValue Chain = LD->getChain();
3136 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003137 SDValue Ops[] = { Offset, Base, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00003138 SDNode *MN = CurDAG->getMachineNode(
3139 Opcode, dl, LD->getValueType(0),
3140 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
3141 transferMemOperands(N, MN);
3142 ReplaceNode(N, MN);
3143 return;
Chris Lattnerce645542006-11-10 02:08:47 +00003144 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00003145 unsigned Opcode;
3146 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
3147 if (LD->getValueType(0) != MVT::i64) {
3148 // Handle PPC32 integer and normal FP loads.
3149 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3150 switch (LoadedVT.getSimpleVT().SimpleTy) {
3151 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00003152 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
3153 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00003154 case MVT::f64: Opcode = PPC::LFDUX; break;
3155 case MVT::f32: Opcode = PPC::LFSUX; break;
3156 case MVT::i32: Opcode = PPC::LWZUX; break;
3157 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
3158 case MVT::i1:
3159 case MVT::i8: Opcode = PPC::LBZUX; break;
3160 }
3161 } else {
3162 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
3163 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
3164 "Invalid sext update load");
3165 switch (LoadedVT.getSimpleVT().SimpleTy) {
3166 default: llvm_unreachable("Invalid PPC load type!");
3167 case MVT::i64: Opcode = PPC::LDUX; break;
3168 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
3169 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
3170 case MVT::i1:
3171 case MVT::i8: Opcode = PPC::LBZUX8; break;
3172 }
3173 }
3174
3175 SDValue Chain = LD->getChain();
3176 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00003177 SDValue Ops[] = { Base, Offset, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00003178 SDNode *MN = CurDAG->getMachineNode(
3179 Opcode, dl, LD->getValueType(0),
3180 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
3181 transferMemOperands(N, MN);
3182 ReplaceNode(N, MN);
3183 return;
Chris Lattnerce645542006-11-10 02:08:47 +00003184 }
3185 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003186
Nate Begemanb3821a32005-08-18 07:30:46 +00003187 case ISD::AND: {
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003188 if (tryLogicOpOfCompares(N))
3189 return;
3190
Nate Begemand31efd12006-09-22 05:01:56 +00003191 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00003192 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00003193
Nate Begemanb3821a32005-08-18 07:30:46 +00003194 // If this is an and of a value rotated between 0 and 31 bits and then and'd
3195 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00003196 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00003197 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003198 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003199 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
3200 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003201 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3202 return;
Nate Begemanb3821a32005-08-18 07:30:46 +00003203 }
Nate Begemand31efd12006-09-22 05:01:56 +00003204 // If this is just a masked value where the input is not handled above, and
3205 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
3206 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00003207 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00003208 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003209 SDValue Val = N->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003210 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
3211 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003212 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3213 return;
Nate Begemand31efd12006-09-22 05:01:56 +00003214 }
Hal Finkele39526a2012-08-28 02:10:15 +00003215 // If this is a 64-bit zero-extension mask, emit rldicl.
3216 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
3217 isMask_64(Imm64)) {
3218 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00003219 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00003220 SH = 0;
3221
Ehsan Amiri1f31e912016-10-24 15:46:58 +00003222 if (Val.getOpcode() == ISD::ANY_EXTEND) {
3223 auto Op0 = Val.getOperand(0);
3224 if ( Op0.getOpcode() == ISD::SRL &&
3225 isInt32Immediate(Op0.getOperand(1).getNode(), Imm) && Imm <= MB) {
3226
3227 auto ResultType = Val.getNode()->getValueType(0);
3228 auto ImDef = CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl,
3229 ResultType);
3230 SDValue IDVal (ImDef, 0);
3231
3232 Val = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl,
3233 ResultType, IDVal, Op0.getOperand(0),
3234 getI32Imm(1, dl)), 0);
3235 SH = 64 - Imm;
3236 }
3237 }
3238
Hal Finkel22498fa2013-11-20 01:10:15 +00003239 // If the operand is a logical right shift, we can fold it into this
3240 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
3241 // for n <= mb. The right shift is really a left rotate followed by a
3242 // mask, and this mask is a more-restrictive sub-mask of the mask implied
3243 // by the shift.
3244 if (Val.getOpcode() == ISD::SRL &&
3245 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
3246 assert(Imm < 64 && "Illegal shift amount");
3247 Val = Val.getOperand(0);
3248 SH = 64 - Imm;
3249 }
3250
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003251 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003252 CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
3253 return;
Hal Finkele39526a2012-08-28 02:10:15 +00003254 }
Nemanja Ivanovic82d53ed2017-02-24 18:03:16 +00003255 // If this is a negated 64-bit zero-extension mask,
3256 // i.e. the immediate is a sequence of ones from most significant side
3257 // and all zero for reminder, we should use rldicr.
3258 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
3259 isMask_64(~Imm64)) {
3260 SDValue Val = N->getOperand(0);
3261 MB = 63 - countTrailingOnes(~Imm64);
3262 SH = 0;
3263 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
3264 CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops);
3265 return;
3266 }
3267
Nate Begemand31efd12006-09-22 05:01:56 +00003268 // AND X, 0 -> 0, not "rlwinm 32".
3269 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003270 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Justin Bognerdc8af062016-05-20 21:43:23 +00003271 return;
Nate Begemand31efd12006-09-22 05:01:56 +00003272 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00003273 // ISD::OR doesn't get all the bitfield insertion fun.
Hal Finkelb1518d62015-09-05 00:02:59 +00003274 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
3275 // bitfield insert.
Andrew Trickc416ba62010-12-24 04:28:06 +00003276 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00003277 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00003278 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Hal Finkelb1518d62015-09-05 00:02:59 +00003279 // The idea here is to check whether this is equivalent to:
3280 // (c1 & m) | (x & ~m)
3281 // where m is a run-of-ones mask. The logic here is that, for each bit in
3282 // c1 and c2:
3283 // - if both are 1, then the output will be 1.
3284 // - if both are 0, then the output will be 0.
3285 // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
3286 // come from x.
3287 // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
3288 // be 0.
3289 // If that last condition is never the case, then we can form m from the
3290 // bits that are the same between c1 and c2.
Chris Lattner20c88df2006-01-05 18:32:49 +00003291 unsigned MB, ME;
Hal Finkelb1518d62015-09-05 00:02:59 +00003292 if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003293 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00003294 N->getOperand(0).getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003295 getI32Imm(0, dl), getI32Imm(MB, dl),
3296 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003297 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
3298 return;
Nate Begeman9aea6e42005-12-24 01:00:15 +00003299 }
3300 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003301
Chris Lattner1de57062005-09-29 23:33:31 +00003302 // Other cases are autogenerated.
3303 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00003304 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003305 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00003306 if (N->getValueType(0) == MVT::i32)
Justin Bognerdc8af062016-05-20 21:43:23 +00003307 if (tryBitfieldInsert(N))
3308 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00003309
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003310 if (tryLogicOpOfCompares(N))
3311 return;
3312
Lei Huang31710412017-07-07 21:12:35 +00003313 int16_t Imm;
Hal Finkelb5e9b042014-12-11 22:51:06 +00003314 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
3315 isIntS16Immediate(N->getOperand(1), Imm)) {
Craig Topperd0af7e82017-04-28 05:31:46 +00003316 KnownBits LHSKnown;
3317 CurDAG->computeKnownBits(N->getOperand(0), LHSKnown);
Hal Finkelb5e9b042014-12-11 22:51:06 +00003318
3319 // If this is equivalent to an add, then we can fold it with the
3320 // FrameIndex calculation.
Craig Topperd0af7e82017-04-28 05:31:46 +00003321 if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
Justin Bognerdc8af062016-05-20 21:43:23 +00003322 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
3323 return;
3324 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003325 }
3326
Chris Lattner1de57062005-09-29 23:33:31 +00003327 // Other cases are autogenerated.
3328 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00003329 }
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003330 case ISD::XOR: {
3331 if (tryLogicOpOfCompares(N))
3332 return;
3333 break;
3334 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003335 case ISD::ADD: {
Lei Huang31710412017-07-07 21:12:35 +00003336 int16_t Imm;
Hal Finkelb5e9b042014-12-11 22:51:06 +00003337 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
Justin Bognerdc8af062016-05-20 21:43:23 +00003338 isIntS16Immediate(N->getOperand(1), Imm)) {
3339 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
3340 return;
3341 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003342
3343 break;
3344 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00003345 case ISD::SHL: {
3346 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00003347 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003348 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003349 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003350 getI32Imm(SH, dl), getI32Imm(MB, dl),
3351 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003352 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3353 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00003354 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003355
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003356 // Other cases are autogenerated.
3357 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00003358 }
3359 case ISD::SRL: {
3360 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00003361 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00003362 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003363 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003364 getI32Imm(SH, dl), getI32Imm(MB, dl),
3365 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003366 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3367 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00003368 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003369
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003370 // Other cases are autogenerated.
3371 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00003372 }
Hal Finkel940ab932014-02-28 00:27:01 +00003373 // FIXME: Remove this once the ANDI glue bug is fixed:
3374 case PPCISD::ANDIo_1_EQ_BIT:
3375 case PPCISD::ANDIo_1_GT_BIT: {
3376 if (!ANDIGlueBug)
3377 break;
3378
3379 EVT InVT = N->getOperand(0).getValueType();
3380 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
3381 "Invalid input type for ANDIo_1_EQ_BIT");
3382
3383 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
3384 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
3385 N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003386 CurDAG->getTargetConstant(1, dl, InVT)),
3387 0);
Hal Finkel940ab932014-02-28 00:27:01 +00003388 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
3389 SDValue SRIdxVal =
3390 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003391 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00003392
Justin Bognerdc8af062016-05-20 21:43:23 +00003393 CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg,
3394 SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */);
3395 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003396 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00003397 case ISD::SELECT_CC: {
3398 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00003399 EVT PtrVT =
3400 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00003401 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00003402
Hal Finkel940ab932014-02-28 00:27:01 +00003403 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003404 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00003405 N->getOperand(0).getValueType() == MVT::i1)
3406 break;
3407
Chris Lattner97b3da12006-06-27 00:04:13 +00003408 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00003409 if (!isPPC64)
3410 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
3411 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
3412 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
3413 if (N1C->isNullValue() && N3C->isNullValue() &&
3414 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
3415 // FIXME: Implement this optzn for PPC64.
3416 N->getValueType(0) == MVT::i32) {
3417 SDNode *Tmp =
3418 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003419 N->getOperand(0), getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00003420 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0),
3421 N->getOperand(0), SDValue(Tmp, 1));
3422 return;
Roman Divacky254f8212011-06-20 15:28:39 +00003423 }
Chris Lattner9b577f12005-08-26 21:23:58 +00003424
Dale Johannesenab8e4422009-02-06 19:16:40 +00003425 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00003426
3427 if (N->getValueType(0) == MVT::i1) {
3428 // An i1 select is: (c & t) | (!c & f).
3429 bool Inv;
3430 unsigned Idx = getCRIdxForSetCC(CC, Inv);
3431
3432 unsigned SRI;
3433 switch (Idx) {
3434 default: llvm_unreachable("Invalid CC index");
3435 case 0: SRI = PPC::sub_lt; break;
3436 case 1: SRI = PPC::sub_gt; break;
3437 case 2: SRI = PPC::sub_eq; break;
3438 case 3: SRI = PPC::sub_un; break;
3439 }
3440
3441 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
3442
3443 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
3444 CCBit, CCBit), 0);
3445 SDValue C = Inv ? NotCCBit : CCBit,
3446 NotC = Inv ? CCBit : NotCCBit;
3447
3448 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
3449 C, N->getOperand(2)), 0);
3450 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
3451 NotC, N->getOperand(3)), 0);
3452
Justin Bognerdc8af062016-05-20 21:43:23 +00003453 CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
3454 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003455 }
3456
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003457 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00003458
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00003459 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00003460 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00003461 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00003462 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00003463 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00003464 else if (N->getValueType(0) == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003465 if (PPCSubTarget->hasP8Vector())
3466 SelectCCOp = PPC::SELECT_CC_VSSRC;
3467 else
3468 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00003469 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003470 if (PPCSubTarget->hasVSX())
3471 SelectCCOp = PPC::SELECT_CC_VSFRC;
3472 else
3473 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003474 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
3475 SelectCCOp = PPC::SELECT_CC_QFRC;
3476 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
3477 SelectCCOp = PPC::SELECT_CC_QSRC;
3478 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
3479 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00003480 else if (N->getValueType(0) == MVT::v2f64 ||
3481 N->getValueType(0) == MVT::v2i64)
3482 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00003483 else
3484 SelectCCOp = PPC::SELECT_CC_VRRC;
3485
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003486 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003487 getI32Imm(BROpc, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003488 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
3489 return;
Chris Lattnerbec817c2005-08-26 18:46:49 +00003490 }
Hal Finkel732f0f72014-03-26 12:49:28 +00003491 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00003492 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00003493 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003494 CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
3495 return;
Hal Finkel732f0f72014-03-26 12:49:28 +00003496 }
Hal Finkel732f0f72014-03-26 12:49:28 +00003497 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003498
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003499 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00003500 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003501 N->getValueType(0) == MVT::v2i64)) {
3502 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Kyle Butt015f4fc2015-12-02 18:53:33 +00003503
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003504 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
3505 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
3506 unsigned DM[2];
3507
3508 for (int i = 0; i < 2; ++i)
3509 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
3510 DM[i] = 0;
3511 else
3512 DM[i] = 1;
3513
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003514 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
3515 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3516 isa<LoadSDNode>(Op1.getOperand(0))) {
3517 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
3518 SDValue Base, Offset;
3519
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +00003520 if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() &&
Bill Schmidt048cc972015-10-14 20:45:00 +00003521 (LD->getMemoryVT() == MVT::f64 ||
3522 LD->getMemoryVT() == MVT::i64) &&
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003523 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
3524 SDValue Chain = LD->getChain();
3525 SDValue Ops[] = { Base, Offset, Chain };
Sean Fertile3c8c3852017-01-26 18:59:15 +00003526 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3527 MemOp[0] = LD->getMemOperand();
Benjamin Kramer58dadd52017-04-20 18:29:14 +00003528 SDNode *NewN = CurDAG->SelectNodeTo(N, PPC::LXVDSX,
3529 N->getValueType(0), Ops);
Sean Fertile3c8c3852017-01-26 18:59:15 +00003530 cast<MachineSDNode>(NewN)->setMemRefs(MemOp, MemOp + 1);
Justin Bognerdc8af062016-05-20 21:43:23 +00003531 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003532 }
3533 }
3534
Bill Schmidtae94f112015-07-01 19:40:07 +00003535 // For little endian, we must swap the input operands and adjust
3536 // the mask elements (reverse and invert them).
3537 if (PPCSubTarget->isLittleEndian()) {
3538 std::swap(Op1, Op2);
3539 unsigned tmp = DM[0];
3540 DM[0] = 1 - DM[1];
3541 DM[1] = 1 - tmp;
3542 }
3543
3544 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
3545 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003546 SDValue Ops[] = { Op1, Op2, DMV };
Justin Bognerdc8af062016-05-20 21:43:23 +00003547 CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
3548 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003549 }
3550
3551 break;
Hal Finkel25c19922013-05-15 21:37:41 +00003552 case PPCISD::BDNZ:
3553 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00003554 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00003555 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003556 CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ
3557 ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
3558 : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
3559 MVT::Other, Ops);
3560 return;
Hal Finkel25c19922013-05-15 21:37:41 +00003561 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003562 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00003563 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003564 // Op #1 is the PPC::PRED_* number.
3565 // Op #2 is the CR#
3566 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00003567 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00003568 // Prevent PPC::PRED_* from being selected into LI.
Hal Finkel65539e32015-12-12 00:32:00 +00003569 unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3570 if (EnableBranchHint)
3571 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3));
3572
3573 SDValue Pred = getI32Imm(PCC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003574 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003575 N->getOperand(0), N->getOperand(4) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003576 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
3577 return;
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003578 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00003579 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00003580 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00003581 unsigned PCC = getPredicateForSetCC(CC);
3582
3583 if (N->getOperand(2).getValueType() == MVT::i1) {
3584 unsigned Opc;
3585 bool Swap;
3586 switch (PCC) {
3587 default: llvm_unreachable("Unexpected Boolean-operand predicate");
3588 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
3589 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
3590 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
3591 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
3592 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
3593 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
3594 }
3595
3596 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
3597 N->getOperand(Swap ? 3 : 2),
3598 N->getOperand(Swap ? 2 : 3)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003599 CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4),
3600 N->getOperand(0));
3601 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003602 }
3603
Hal Finkel65539e32015-12-12 00:32:00 +00003604 if (EnableBranchHint)
3605 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4));
3606
Dale Johannesenab8e4422009-02-06 19:16:40 +00003607 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003608 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00003609 N->getOperand(4), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003610 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
3611 return;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003612 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00003613 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00003614 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003615 SDValue Chain = N->getOperand(0);
3616 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003617 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00003618 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00003619 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00003620 Chain), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003621 CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
3622 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00003623 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003624 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00003625 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
3626 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00003627 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
3628 SDValue GA = N->getOperand(0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003629 SDNode *MN = CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
3630 N->getOperand(1));
3631 transferMemOperands(N, MN);
3632 ReplaceNode(N, MN);
3633 return;
Justin Hibbits3476db42014-08-28 04:40:55 +00003634 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003635
Bill Schmidt27917782013-02-21 17:12:27 +00003636 // For medium and large code model, we generate two instructions as
3637 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003638 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00003639 CodeModel::Model CModel = TM.getCodeModel();
3640 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00003641 break;
3642
Bill Schmidt5d82f092014-06-16 21:36:02 +00003643 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
Eric Christopherc1808362015-11-20 20:51:31 +00003644 // If it must be toc-referenced according to PPCSubTarget, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00003645 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
3646 // Otherwise we generate:
3647 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
3648 SDValue GA = N->getOperand(0);
3649 SDValue TOCbase = N->getOperand(1);
3650 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00003651 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00003652
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003653 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
Justin Bognerdc8af062016-05-20 21:43:23 +00003654 CModel == CodeModel::Large) {
3655 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3656 SDValue(Tmp, 0));
3657 transferMemOperands(N, MN);
3658 ReplaceNode(N, MN);
3659 return;
3660 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003661
3662 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
Eric Christopherc1808362015-11-20 20:51:31 +00003663 const GlobalValue *GV = G->getGlobal();
3664 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
3665 if (GVFlags & PPCII::MO_NLP_FLAG) {
Justin Bognerdc8af062016-05-20 21:43:23 +00003666 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3667 SDValue(Tmp, 0));
3668 transferMemOperands(N, MN);
3669 ReplaceNode(N, MN);
3670 return;
Eric Christopherc1808362015-11-20 20:51:31 +00003671 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003672 }
3673
Justin Bognerdc8af062016-05-20 21:43:23 +00003674 ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
3675 SDValue(Tmp, 0), GA));
3676 return;
Bill Schmidt34627e32012-11-27 17:35:46 +00003677 }
Eugene Zelenko8187c192017-01-13 00:58:58 +00003678 case PPCISD::PPC32_PICGOT:
Hal Finkel7c8ae532014-07-25 17:47:22 +00003679 // Generate a PIC-safe GOT reference.
3680 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
3681 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
Justin Bognerdc8af062016-05-20 21:43:23 +00003682 CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT,
3683 PPCLowering->getPointerTy(CurDAG->getDataLayout()),
3684 MVT::i32);
3685 return;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003686
Bill Schmidt51e79512013-02-20 15:50:31 +00003687 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003688 // This expands into one of three sequences, depending on whether
3689 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00003690 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
3691 isa<ConstantSDNode>(N->getOperand(1)) &&
3692 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003693
3694 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00003695 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003696 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00003697 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003698
Bill Schmidt51e79512013-02-20 15:50:31 +00003699 if (EltSize == 1) {
3700 Opc1 = PPC::VSPLTISB;
3701 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003702 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003703 VT = MVT::v16i8;
3704 } else if (EltSize == 2) {
3705 Opc1 = PPC::VSPLTISH;
3706 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003707 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003708 VT = MVT::v8i16;
3709 } else {
3710 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
3711 Opc1 = PPC::VSPLTISW;
3712 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003713 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003714 VT = MVT::v4i32;
3715 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003716
3717 if ((Elt & 1) == 0) {
3718 // Elt is even, in the range [-32,-18] + [16,30].
3719 //
3720 // Convert: VADD_SPLAT elt, size
3721 // Into: tmp = VSPLTIS[BHW] elt
3722 // VADDU[BHW]M tmp, tmp
3723 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003724 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003725 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3726 SDValue TmpVal = SDValue(Tmp, 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003727 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal));
3728 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003729 } else if (Elt > 0) {
3730 // Elt is odd and positive, in the range [17,31].
3731 //
3732 // Convert: VADD_SPLAT elt, size
3733 // Into: tmp1 = VSPLTIS[BHW] elt-16
3734 // tmp2 = VSPLTIS[BHW] -16
3735 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003736 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003737 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003738 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003739 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003740 ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
3741 SDValue(Tmp2, 0)));
3742 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003743 } else {
3744 // Elt is odd and negative, in the range [-31,-17].
3745 //
3746 // Convert: VADD_SPLAT elt, size
3747 // Into: tmp1 = VSPLTIS[BHW] elt+16
3748 // tmp2 = VSPLTIS[BHW] -16
3749 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003750 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003751 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003752 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003753 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003754 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
3755 SDValue(Tmp2, 0)));
3756 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003757 }
Bill Schmidt51e79512013-02-20 15:50:31 +00003758 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00003759 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003760
Justin Bognerdc8af062016-05-20 21:43:23 +00003761 SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00003762}
3763
Hal Finkel4edc66b2015-01-03 01:16:37 +00003764// If the target supports the cmpb instruction, do the idiom recognition here.
3765// We don't do this as a DAG combine because we don't want to do it as nodes
3766// are being combined (because we might miss part of the eventual idiom). We
3767// don't want to do it during instruction selection because we want to reuse
3768// the logic for lowering the masking operations already part of the
3769// instruction selector.
3770SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3771 SDLoc dl(N);
3772
3773 assert(N->getOpcode() == ISD::OR &&
3774 "Only OR nodes are supported for CMPB");
3775
3776 SDValue Res;
3777 if (!PPCSubTarget->hasCMPB())
3778 return Res;
3779
3780 if (N->getValueType(0) != MVT::i32 &&
3781 N->getValueType(0) != MVT::i64)
3782 return Res;
3783
3784 EVT VT = N->getValueType(0);
3785
3786 SDValue RHS, LHS;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003787 bool BytesFound[8] = {false, false, false, false, false, false, false, false};
Hal Finkel4edc66b2015-01-03 01:16:37 +00003788 uint64_t Mask = 0, Alt = 0;
3789
3790 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3791 uint64_t &Mask, uint64_t &Alt,
3792 SDValue &LHS, SDValue &RHS) {
3793 if (O.getOpcode() != ISD::SELECT_CC)
3794 return false;
3795 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3796
3797 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3798 !isa<ConstantSDNode>(O.getOperand(3)))
3799 return false;
3800
3801 uint64_t PM = O.getConstantOperandVal(2);
3802 uint64_t PAlt = O.getConstantOperandVal(3);
3803 for (b = 0; b < 8; ++b) {
3804 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3805 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3806 break;
3807 }
3808
3809 if (b == 8)
3810 return false;
3811 Mask |= PM;
3812 Alt |= PAlt;
3813
3814 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3815 O.getConstantOperandVal(1) != 0) {
3816 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3817 if (Op0.getOpcode() == ISD::TRUNCATE)
3818 Op0 = Op0.getOperand(0);
3819 if (Op1.getOpcode() == ISD::TRUNCATE)
3820 Op1 = Op1.getOperand(0);
3821
3822 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3823 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3824 isa<ConstantSDNode>(Op0.getOperand(1))) {
3825
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003826 unsigned Bits = Op0.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00003827 if (b != Bits/8-1)
3828 return false;
3829 if (Op0.getConstantOperandVal(1) != Bits-8)
3830 return false;
3831
3832 LHS = Op0.getOperand(0);
3833 RHS = Op1.getOperand(0);
3834 return true;
3835 }
3836
3837 // When we have small integers (i16 to be specific), the form present
3838 // post-legalization uses SETULT in the SELECT_CC for the
3839 // higher-order byte, depending on the fact that the
3840 // even-higher-order bytes are known to all be zero, for example:
3841 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3842 // (so when the second byte is the same, because all higher-order
3843 // bits from bytes 3 and 4 are known to be zero, the result of the
3844 // xor can be at most 255)
3845 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3846 isa<ConstantSDNode>(O.getOperand(1))) {
3847
3848 uint64_t ULim = O.getConstantOperandVal(1);
3849 if (ULim != (UINT64_C(1) << b*8))
3850 return false;
3851
3852 // Now we need to make sure that the upper bytes are known to be
3853 // zero.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003854 unsigned Bits = Op0.getValueSizeInBits();
3855 if (!CurDAG->MaskedValueIsZero(
3856 Op0, APInt::getHighBitsSet(Bits, Bits - (b + 1) * 8)))
Hal Finkel4edc66b2015-01-03 01:16:37 +00003857 return false;
Kyle Butt015f4fc2015-12-02 18:53:33 +00003858
Hal Finkel4edc66b2015-01-03 01:16:37 +00003859 LHS = Op0.getOperand(0);
3860 RHS = Op0.getOperand(1);
3861 return true;
3862 }
3863
3864 return false;
3865 }
3866
3867 if (CC != ISD::SETEQ)
3868 return false;
3869
3870 SDValue Op = O.getOperand(0);
3871 if (Op.getOpcode() == ISD::AND) {
3872 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3873 return false;
3874 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3875 return false;
3876
3877 SDValue XOR = Op.getOperand(0);
3878 if (XOR.getOpcode() == ISD::TRUNCATE)
3879 XOR = XOR.getOperand(0);
3880 if (XOR.getOpcode() != ISD::XOR)
3881 return false;
3882
3883 LHS = XOR.getOperand(0);
3884 RHS = XOR.getOperand(1);
3885 return true;
3886 } else if (Op.getOpcode() == ISD::SRL) {
3887 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3888 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003889 unsigned Bits = Op.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00003890 if (b != Bits/8-1)
3891 return false;
3892 if (Op.getConstantOperandVal(1) != Bits-8)
3893 return false;
3894
3895 SDValue XOR = Op.getOperand(0);
3896 if (XOR.getOpcode() == ISD::TRUNCATE)
3897 XOR = XOR.getOperand(0);
3898 if (XOR.getOpcode() != ISD::XOR)
3899 return false;
3900
3901 LHS = XOR.getOperand(0);
3902 RHS = XOR.getOperand(1);
3903 return true;
3904 }
3905
3906 return false;
3907 };
3908
3909 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3910 while (!Queue.empty()) {
3911 SDValue V = Queue.pop_back_val();
3912
3913 for (const SDValue &O : V.getNode()->ops()) {
3914 unsigned b;
3915 uint64_t M = 0, A = 0;
3916 SDValue OLHS, ORHS;
3917 if (O.getOpcode() == ISD::OR) {
3918 Queue.push_back(O);
3919 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3920 if (!LHS) {
3921 LHS = OLHS;
3922 RHS = ORHS;
3923 BytesFound[b] = true;
3924 Mask |= M;
3925 Alt |= A;
3926 } else if ((LHS == ORHS && RHS == OLHS) ||
3927 (RHS == ORHS && LHS == OLHS)) {
3928 BytesFound[b] = true;
3929 Mask |= M;
3930 Alt |= A;
3931 } else {
3932 return Res;
3933 }
3934 } else {
3935 return Res;
3936 }
3937 }
3938 }
3939
3940 unsigned LastB = 0, BCnt = 0;
3941 for (unsigned i = 0; i < 8; ++i)
3942 if (BytesFound[LastB]) {
3943 ++BCnt;
3944 LastB = i;
3945 }
3946
3947 if (!LastB || BCnt < 2)
3948 return Res;
3949
3950 // Because we'll be zero-extending the output anyway if don't have a specific
3951 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3952 if (LHS.getValueType() != VT) {
3953 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3954 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3955 }
3956
3957 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3958
3959 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3960 if (NonTrivialMask && !Alt) {
3961 // Res = Mask & CMPB
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003962 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3963 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003964 } else if (Alt) {
3965 // Res = (CMPB & Mask) | (~CMPB & Alt)
3966 // Which, as suggested here:
3967 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3968 // can be written as:
3969 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3970 // useful because the (Alt ^ Mask) can be pre-computed.
3971 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003972 CurDAG->getConstant(Mask ^ Alt, dl, VT));
3973 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
3974 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003975 }
3976
3977 return Res;
3978}
3979
Hal Finkel200d2ad2015-01-05 21:10:24 +00003980// When CR bit registers are enabled, an extension of an i1 variable to a i32
3981// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3982// involves constant materialization of a 0 or a 1 or both. If the result of
3983// the extension is then operated upon by some operator that can be constant
3984// folded with a constant 0 or 1, and that constant can be materialized using
3985// only one instruction (like a zero or one), then we should fold in those
3986// operations with the select.
3987void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3988 if (!PPCSubTarget->useCRBits())
3989 return;
3990
3991 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3992 N->getOpcode() != ISD::SIGN_EXTEND &&
3993 N->getOpcode() != ISD::ANY_EXTEND)
3994 return;
3995
3996 if (N->getOperand(0).getValueType() != MVT::i1)
3997 return;
3998
3999 if (!N->hasOneUse())
4000 return;
4001
4002 SDLoc dl(N);
4003 EVT VT = N->getValueType(0);
4004 SDValue Cond = N->getOperand(0);
4005 SDValue ConstTrue =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004006 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
4007 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00004008
4009 do {
4010 SDNode *User = *N->use_begin();
4011 if (User->getNumOperands() != 2)
4012 break;
4013
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004014 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00004015 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
4016 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
4017 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
4018
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004019 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00004020 User->getValueType(0),
4021 O0.getNode(), O1.getNode());
4022 };
4023
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00004024 // FIXME: When the semantics of the interaction between select and undef
4025 // are clearly defined, it may turn out to be unnecessary to break here.
Hal Finkel200d2ad2015-01-05 21:10:24 +00004026 SDValue TrueRes = TryFold(ConstTrue);
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00004027 if (!TrueRes || TrueRes.isUndef())
Hal Finkel200d2ad2015-01-05 21:10:24 +00004028 break;
4029 SDValue FalseRes = TryFold(ConstFalse);
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00004030 if (!FalseRes || FalseRes.isUndef())
Hal Finkel200d2ad2015-01-05 21:10:24 +00004031 break;
4032
4033 // For us to materialize these using one instruction, we must be able to
4034 // represent them as signed 16-bit integers.
4035 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
4036 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
4037 if (!isInt<16>(True) || !isInt<16>(False))
4038 break;
4039
4040 // We can replace User with a new SELECT node, and try again to see if we
4041 // can fold the select with its user.
4042 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
4043 N = User;
4044 ConstTrue = TrueRes;
4045 ConstFalse = FalseRes;
4046 } while (N->hasOneUse());
4047}
4048
Hal Finkel4edc66b2015-01-03 01:16:37 +00004049void PPCDAGToDAGISel::PreprocessISelDAG() {
4050 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4051 ++Position;
4052
4053 bool MadeChange = false;
4054 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004055 SDNode *N = &*--Position;
Hal Finkel4edc66b2015-01-03 01:16:37 +00004056 if (N->use_empty())
4057 continue;
4058
4059 SDValue Res;
4060 switch (N->getOpcode()) {
4061 default: break;
4062 case ISD::OR:
4063 Res = combineToCMPB(N);
4064 break;
4065 }
4066
Hal Finkel200d2ad2015-01-05 21:10:24 +00004067 if (!Res)
4068 foldBoolExts(Res, N);
4069
Hal Finkel4edc66b2015-01-03 01:16:37 +00004070 if (Res) {
4071 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
4072 DEBUG(N->dump(CurDAG));
4073 DEBUG(dbgs() << "\nNew: ");
4074 DEBUG(Res.getNode()->dump(CurDAG));
4075 DEBUG(dbgs() << "\n");
4076
4077 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
4078 MadeChange = true;
4079 }
4080 }
4081
4082 if (MadeChange)
4083 CurDAG->RemoveDeadNodes();
4084}
4085
Hal Finkel860fa902014-01-02 22:09:39 +00004086/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004087/// on the DAG representation.
4088void PPCDAGToDAGISel::PostprocessISelDAG() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004089 // Skip peepholes at -O0.
4090 if (TM.getOptLevel() == CodeGenOpt::None)
4091 return;
4092
Hal Finkel940ab932014-02-28 00:27:01 +00004093 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00004094 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00004095 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00004096}
4097
Hal Finkelb9989152014-02-28 06:11:16 +00004098// Check if all users of this node will become isel where the second operand
4099// is the constant zero. If this is so, and if we can negate the condition,
4100// then we can flip the true and false operands. This will allow the zero to
4101// be folded with the isel so that we don't need to materialize a register
4102// containing zero.
4103bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
Hal Finkelb9989152014-02-28 06:11:16 +00004104 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4105 UI != UE; ++UI) {
4106 SDNode *User = *UI;
4107 if (!User->isMachineOpcode())
4108 return false;
4109 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
4110 User->getMachineOpcode() != PPC::SELECT_I8)
4111 return false;
4112
4113 SDNode *Op2 = User->getOperand(2).getNode();
4114 if (!Op2->isMachineOpcode())
4115 return false;
4116
4117 if (Op2->getMachineOpcode() != PPC::LI &&
4118 Op2->getMachineOpcode() != PPC::LI8)
4119 return false;
4120
4121 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
4122 if (!C)
4123 return false;
4124
4125 if (!C->isNullValue())
4126 return false;
4127 }
4128
4129 return true;
4130}
4131
4132void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
4133 SmallVector<SDNode *, 4> ToReplace;
4134 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4135 UI != UE; ++UI) {
4136 SDNode *User = *UI;
4137 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
4138 User->getMachineOpcode() == PPC::SELECT_I8) &&
4139 "Must have all select users");
4140 ToReplace.push_back(User);
4141 }
4142
4143 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
4144 UE = ToReplace.end(); UI != UE; ++UI) {
4145 SDNode *User = *UI;
4146 SDNode *ResNode =
4147 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
4148 User->getValueType(0), User->getOperand(0),
4149 User->getOperand(2),
4150 User->getOperand(1));
4151
4152 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
4153 DEBUG(User->dump(CurDAG));
4154 DEBUG(dbgs() << "\nNew: ");
4155 DEBUG(ResNode->dump(CurDAG));
4156 DEBUG(dbgs() << "\n");
4157
4158 ReplaceUses(User, ResNode);
4159 }
4160}
4161
Eric Christopher02e18042014-05-14 00:31:15 +00004162void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00004163 bool IsModified;
4164 do {
4165 IsModified = false;
Pete Cooper65c69402015-07-14 22:10:54 +00004166 for (SDNode &Node : CurDAG->allnodes()) {
4167 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node);
Hal Finkel940ab932014-02-28 00:27:01 +00004168 if (!MachineNode || MachineNode->use_empty())
4169 continue;
4170 SDNode *ResNode = MachineNode;
4171
4172 bool Op1Set = false, Op1Unset = false,
4173 Op1Not = false,
4174 Op2Set = false, Op2Unset = false,
4175 Op2Not = false;
4176
4177 unsigned Opcode = MachineNode->getMachineOpcode();
4178 switch (Opcode) {
4179 default: break;
4180 case PPC::CRAND:
4181 case PPC::CRNAND:
4182 case PPC::CROR:
4183 case PPC::CRXOR:
4184 case PPC::CRNOR:
4185 case PPC::CREQV:
4186 case PPC::CRANDC:
4187 case PPC::CRORC: {
4188 SDValue Op = MachineNode->getOperand(1);
4189 if (Op.isMachineOpcode()) {
4190 if (Op.getMachineOpcode() == PPC::CRSET)
4191 Op2Set = true;
4192 else if (Op.getMachineOpcode() == PPC::CRUNSET)
4193 Op2Unset = true;
4194 else if (Op.getMachineOpcode() == PPC::CRNOR &&
4195 Op.getOperand(0) == Op.getOperand(1))
4196 Op2Not = true;
4197 }
Justin Bognerb03fd122016-08-17 05:10:15 +00004198 LLVM_FALLTHROUGH;
4199 }
Hal Finkel940ab932014-02-28 00:27:01 +00004200 case PPC::BC:
4201 case PPC::BCn:
4202 case PPC::SELECT_I4:
4203 case PPC::SELECT_I8:
4204 case PPC::SELECT_F4:
4205 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00004206 case PPC::SELECT_QFRC:
4207 case PPC::SELECT_QSRC:
4208 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004209 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00004210 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00004211 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004212 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00004213 SDValue Op = MachineNode->getOperand(0);
4214 if (Op.isMachineOpcode()) {
4215 if (Op.getMachineOpcode() == PPC::CRSET)
4216 Op1Set = true;
4217 else if (Op.getMachineOpcode() == PPC::CRUNSET)
4218 Op1Unset = true;
4219 else if (Op.getMachineOpcode() == PPC::CRNOR &&
4220 Op.getOperand(0) == Op.getOperand(1))
4221 Op1Not = true;
4222 }
4223 }
4224 break;
4225 }
4226
Hal Finkelb9989152014-02-28 06:11:16 +00004227 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00004228 switch (Opcode) {
4229 default: break;
4230 case PPC::CRAND:
4231 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4232 // x & x = x
4233 ResNode = MachineNode->getOperand(0).getNode();
4234 else if (Op1Set)
4235 // 1 & y = y
4236 ResNode = MachineNode->getOperand(1).getNode();
4237 else if (Op2Set)
4238 // x & 1 = x
4239 ResNode = MachineNode->getOperand(0).getNode();
4240 else if (Op1Unset || Op2Unset)
4241 // x & 0 = 0 & y = 0
4242 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4243 MVT::i1);
4244 else if (Op1Not)
4245 // ~x & y = andc(y, x)
4246 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4247 MVT::i1, MachineNode->getOperand(1),
4248 MachineNode->getOperand(0).
4249 getOperand(0));
4250 else if (Op2Not)
4251 // x & ~y = andc(x, y)
4252 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4253 MVT::i1, MachineNode->getOperand(0),
4254 MachineNode->getOperand(1).
4255 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004256 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004257 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
4258 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004259 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004260 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004261 }
Hal Finkel940ab932014-02-28 00:27:01 +00004262 break;
4263 case PPC::CRNAND:
4264 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4265 // nand(x, x) -> nor(x, x)
4266 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4267 MVT::i1, MachineNode->getOperand(0),
4268 MachineNode->getOperand(0));
4269 else if (Op1Set)
4270 // nand(1, y) -> nor(y, y)
4271 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4272 MVT::i1, MachineNode->getOperand(1),
4273 MachineNode->getOperand(1));
4274 else if (Op2Set)
4275 // nand(x, 1) -> nor(x, x)
4276 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4277 MVT::i1, MachineNode->getOperand(0),
4278 MachineNode->getOperand(0));
4279 else if (Op1Unset || Op2Unset)
4280 // nand(x, 0) = nand(0, y) = 1
4281 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4282 MVT::i1);
4283 else if (Op1Not)
4284 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
4285 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4286 MVT::i1, MachineNode->getOperand(0).
4287 getOperand(0),
4288 MachineNode->getOperand(1));
4289 else if (Op2Not)
4290 // nand(x, ~y) = ~x | y = orc(y, x)
4291 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4292 MVT::i1, MachineNode->getOperand(1).
4293 getOperand(0),
4294 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004295 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004296 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
4297 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004298 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004299 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004300 }
Hal Finkel940ab932014-02-28 00:27:01 +00004301 break;
4302 case PPC::CROR:
4303 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4304 // x | x = x
4305 ResNode = MachineNode->getOperand(0).getNode();
4306 else if (Op1Set || Op2Set)
4307 // x | 1 = 1 | y = 1
4308 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4309 MVT::i1);
4310 else if (Op1Unset)
4311 // 0 | y = y
4312 ResNode = MachineNode->getOperand(1).getNode();
4313 else if (Op2Unset)
4314 // x | 0 = x
4315 ResNode = MachineNode->getOperand(0).getNode();
4316 else if (Op1Not)
4317 // ~x | y = orc(y, x)
4318 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4319 MVT::i1, MachineNode->getOperand(1),
4320 MachineNode->getOperand(0).
4321 getOperand(0));
4322 else if (Op2Not)
4323 // x | ~y = orc(x, y)
4324 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4325 MVT::i1, MachineNode->getOperand(0),
4326 MachineNode->getOperand(1).
4327 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004328 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004329 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4330 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004331 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004332 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004333 }
Hal Finkel940ab932014-02-28 00:27:01 +00004334 break;
4335 case PPC::CRXOR:
4336 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4337 // xor(x, x) = 0
4338 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4339 MVT::i1);
4340 else if (Op1Set)
4341 // xor(1, y) -> nor(y, y)
4342 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4343 MVT::i1, MachineNode->getOperand(1),
4344 MachineNode->getOperand(1));
4345 else if (Op2Set)
4346 // xor(x, 1) -> nor(x, x)
4347 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4348 MVT::i1, MachineNode->getOperand(0),
4349 MachineNode->getOperand(0));
4350 else if (Op1Unset)
4351 // xor(0, y) = y
4352 ResNode = MachineNode->getOperand(1).getNode();
4353 else if (Op2Unset)
4354 // xor(x, 0) = x
4355 ResNode = MachineNode->getOperand(0).getNode();
4356 else if (Op1Not)
4357 // xor(~x, y) = eqv(x, y)
4358 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4359 MVT::i1, MachineNode->getOperand(0).
4360 getOperand(0),
4361 MachineNode->getOperand(1));
4362 else if (Op2Not)
4363 // xor(x, ~y) = eqv(x, y)
4364 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4365 MVT::i1, MachineNode->getOperand(0),
4366 MachineNode->getOperand(1).
4367 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004368 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004369 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4370 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004371 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004372 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004373 }
Hal Finkel940ab932014-02-28 00:27:01 +00004374 break;
4375 case PPC::CRNOR:
4376 if (Op1Set || Op2Set)
4377 // nor(1, y) -> 0
4378 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4379 MVT::i1);
4380 else if (Op1Unset)
4381 // nor(0, y) = ~y -> nor(y, y)
4382 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4383 MVT::i1, MachineNode->getOperand(1),
4384 MachineNode->getOperand(1));
4385 else if (Op2Unset)
4386 // nor(x, 0) = ~x
4387 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4388 MVT::i1, MachineNode->getOperand(0),
4389 MachineNode->getOperand(0));
4390 else if (Op1Not)
4391 // nor(~x, y) = andc(x, y)
4392 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4393 MVT::i1, MachineNode->getOperand(0).
4394 getOperand(0),
4395 MachineNode->getOperand(1));
4396 else if (Op2Not)
4397 // nor(x, ~y) = andc(y, x)
4398 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4399 MVT::i1, MachineNode->getOperand(1).
4400 getOperand(0),
4401 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004402 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004403 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
4404 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004405 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004406 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004407 }
Hal Finkel940ab932014-02-28 00:27:01 +00004408 break;
4409 case PPC::CREQV:
4410 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4411 // eqv(x, x) = 1
4412 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4413 MVT::i1);
4414 else if (Op1Set)
4415 // eqv(1, y) = y
4416 ResNode = MachineNode->getOperand(1).getNode();
4417 else if (Op2Set)
4418 // eqv(x, 1) = x
4419 ResNode = MachineNode->getOperand(0).getNode();
4420 else if (Op1Unset)
4421 // eqv(0, y) = ~y -> nor(y, y)
4422 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4423 MVT::i1, MachineNode->getOperand(1),
4424 MachineNode->getOperand(1));
4425 else if (Op2Unset)
4426 // eqv(x, 0) = ~x
4427 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4428 MVT::i1, MachineNode->getOperand(0),
4429 MachineNode->getOperand(0));
4430 else if (Op1Not)
4431 // eqv(~x, y) = xor(x, y)
4432 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4433 MVT::i1, MachineNode->getOperand(0).
4434 getOperand(0),
4435 MachineNode->getOperand(1));
4436 else if (Op2Not)
4437 // eqv(x, ~y) = xor(x, y)
4438 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4439 MVT::i1, MachineNode->getOperand(0),
4440 MachineNode->getOperand(1).
4441 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004442 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004443 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4444 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004445 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004446 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004447 }
Hal Finkel940ab932014-02-28 00:27:01 +00004448 break;
4449 case PPC::CRANDC:
4450 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4451 // andc(x, x) = 0
4452 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4453 MVT::i1);
4454 else if (Op1Set)
4455 // andc(1, y) = ~y
4456 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4457 MVT::i1, MachineNode->getOperand(1),
4458 MachineNode->getOperand(1));
4459 else if (Op1Unset || Op2Set)
4460 // andc(0, y) = andc(x, 1) = 0
4461 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4462 MVT::i1);
4463 else if (Op2Unset)
4464 // andc(x, 0) = x
4465 ResNode = MachineNode->getOperand(0).getNode();
4466 else if (Op1Not)
4467 // andc(~x, y) = ~(x | y) = nor(x, y)
4468 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4469 MVT::i1, MachineNode->getOperand(0).
4470 getOperand(0),
4471 MachineNode->getOperand(1));
4472 else if (Op2Not)
4473 // andc(x, ~y) = x & y
4474 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
4475 MVT::i1, MachineNode->getOperand(0),
4476 MachineNode->getOperand(1).
4477 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004478 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004479 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4480 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00004481 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00004482 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004483 }
Hal Finkel940ab932014-02-28 00:27:01 +00004484 break;
4485 case PPC::CRORC:
4486 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4487 // orc(x, x) = 1
4488 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4489 MVT::i1);
4490 else if (Op1Set || Op2Unset)
4491 // orc(1, y) = orc(x, 0) = 1
4492 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4493 MVT::i1);
4494 else if (Op2Set)
4495 // orc(x, 1) = x
4496 ResNode = MachineNode->getOperand(0).getNode();
4497 else if (Op1Unset)
4498 // orc(0, y) = ~y
4499 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4500 MVT::i1, MachineNode->getOperand(1),
4501 MachineNode->getOperand(1));
4502 else if (Op1Not)
4503 // orc(~x, y) = ~(x & y) = nand(x, y)
4504 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
4505 MVT::i1, MachineNode->getOperand(0).
4506 getOperand(0),
4507 MachineNode->getOperand(1));
4508 else if (Op2Not)
4509 // orc(x, ~y) = x | y
4510 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
4511 MVT::i1, MachineNode->getOperand(0),
4512 MachineNode->getOperand(1).
4513 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004514 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004515 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4516 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00004517 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00004518 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004519 }
Hal Finkel940ab932014-02-28 00:27:01 +00004520 break;
4521 case PPC::SELECT_I4:
4522 case PPC::SELECT_I8:
4523 case PPC::SELECT_F4:
4524 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00004525 case PPC::SELECT_QFRC:
4526 case PPC::SELECT_QSRC:
4527 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00004528 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00004529 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00004530 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004531 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00004532 if (Op1Set)
4533 ResNode = MachineNode->getOperand(1).getNode();
4534 else if (Op1Unset)
4535 ResNode = MachineNode->getOperand(2).getNode();
4536 else if (Op1Not)
4537 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
4538 SDLoc(MachineNode),
4539 MachineNode->getValueType(0),
4540 MachineNode->getOperand(0).
4541 getOperand(0),
4542 MachineNode->getOperand(2),
4543 MachineNode->getOperand(1));
4544 break;
4545 case PPC::BC:
4546 case PPC::BCn:
4547 if (Op1Not)
4548 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
4549 PPC::BC,
4550 SDLoc(MachineNode),
4551 MVT::Other,
4552 MachineNode->getOperand(0).
4553 getOperand(0),
4554 MachineNode->getOperand(1),
4555 MachineNode->getOperand(2));
4556 // FIXME: Handle Op1Set, Op1Unset here too.
4557 break;
4558 }
4559
Hal Finkelb9989152014-02-28 06:11:16 +00004560 // If we're inverting this node because it is used only by selects that
4561 // we'd like to swap, then swap the selects before the node replacement.
4562 if (SelectSwap)
4563 SwapAllSelectUsers(MachineNode);
4564
Hal Finkel940ab932014-02-28 00:27:01 +00004565 if (ResNode != MachineNode) {
4566 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
4567 DEBUG(MachineNode->dump(CurDAG));
4568 DEBUG(dbgs() << "\nNew: ");
4569 DEBUG(ResNode->dump(CurDAG));
4570 DEBUG(dbgs() << "\n");
4571
4572 ReplaceUses(MachineNode, ResNode);
4573 IsModified = true;
4574 }
4575 }
4576 if (IsModified)
4577 CurDAG->RemoveDeadNodes();
4578 } while (IsModified);
4579}
4580
Hal Finkel4c6658f2014-12-12 23:59:36 +00004581// Gather the set of 32-bit operations that are known to have their
4582// higher-order 32 bits zero, where ToPromote contains all such operations.
4583static bool PeepholePPC64ZExtGather(SDValue Op32,
4584 SmallPtrSetImpl<SDNode *> &ToPromote) {
4585 if (!Op32.isMachineOpcode())
4586 return false;
4587
4588 // First, check for the "frontier" instructions (those that will clear the
4589 // higher-order 32 bits.
4590
4591 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
4592 // around. If it does not, then these instructions will clear the
4593 // higher-order bits.
4594 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
4595 Op32.getMachineOpcode() == PPC::RLWNM) &&
4596 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
4597 ToPromote.insert(Op32.getNode());
4598 return true;
4599 }
4600
4601 // SLW and SRW always clear the higher-order bits.
4602 if (Op32.getMachineOpcode() == PPC::SLW ||
4603 Op32.getMachineOpcode() == PPC::SRW) {
4604 ToPromote.insert(Op32.getNode());
4605 return true;
4606 }
4607
4608 // For LI and LIS, we need the immediate to be positive (so that it is not
4609 // sign extended).
4610 if (Op32.getMachineOpcode() == PPC::LI ||
4611 Op32.getMachineOpcode() == PPC::LIS) {
4612 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
4613 return false;
4614
4615 ToPromote.insert(Op32.getNode());
4616 return true;
4617 }
4618
Hal Finkel4e2c7822015-01-05 18:09:06 +00004619 // LHBRX and LWBRX always clear the higher-order bits.
4620 if (Op32.getMachineOpcode() == PPC::LHBRX ||
4621 Op32.getMachineOpcode() == PPC::LWBRX) {
4622 ToPromote.insert(Op32.getNode());
4623 return true;
4624 }
4625
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00004626 // CNT[LT]ZW always produce a 64-bit value in [0,32], and so is zero extended.
4627 if (Op32.getMachineOpcode() == PPC::CNTLZW ||
4628 Op32.getMachineOpcode() == PPC::CNTTZW) {
Hal Finkel49557f12015-01-05 18:52:29 +00004629 ToPromote.insert(Op32.getNode());
4630 return true;
4631 }
4632
Hal Finkel4c6658f2014-12-12 23:59:36 +00004633 // Next, check for those instructions we can look through.
4634
4635 // Assuming the mask does not wrap around, then the higher-order bits are
4636 // taken directly from the first operand.
4637 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
4638 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
4639 SmallPtrSet<SDNode *, 16> ToPromote1;
4640 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4641 return false;
4642
4643 ToPromote.insert(Op32.getNode());
4644 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4645 return true;
4646 }
4647
4648 // For OR, the higher-order bits are zero if that is true for both operands.
4649 // For SELECT_I4, the same is true (but the relevant operand numbers are
4650 // shifted by 1).
4651 if (Op32.getMachineOpcode() == PPC::OR ||
4652 Op32.getMachineOpcode() == PPC::SELECT_I4) {
4653 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
4654 SmallPtrSet<SDNode *, 16> ToPromote1;
4655 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
4656 return false;
4657 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
4658 return false;
4659
4660 ToPromote.insert(Op32.getNode());
4661 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4662 return true;
4663 }
4664
4665 // For ORI and ORIS, we need the higher-order bits of the first operand to be
4666 // zero, and also for the constant to be positive (so that it is not sign
4667 // extended).
4668 if (Op32.getMachineOpcode() == PPC::ORI ||
4669 Op32.getMachineOpcode() == PPC::ORIS) {
4670 SmallPtrSet<SDNode *, 16> ToPromote1;
4671 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4672 return false;
4673 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
4674 return false;
4675
4676 ToPromote.insert(Op32.getNode());
4677 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4678 return true;
4679 }
4680
4681 // The higher-order bits of AND are zero if that is true for at least one of
4682 // the operands.
4683 if (Op32.getMachineOpcode() == PPC::AND) {
4684 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
4685 bool Op0OK =
4686 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4687 bool Op1OK =
4688 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
4689 if (!Op0OK && !Op1OK)
4690 return false;
4691
4692 ToPromote.insert(Op32.getNode());
4693
4694 if (Op0OK)
4695 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4696
4697 if (Op1OK)
4698 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
4699
4700 return true;
4701 }
4702
4703 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
4704 // of the first operand, or if the second operand is positive (so that it is
4705 // not sign extended).
4706 if (Op32.getMachineOpcode() == PPC::ANDIo ||
4707 Op32.getMachineOpcode() == PPC::ANDISo) {
4708 SmallPtrSet<SDNode *, 16> ToPromote1;
4709 bool Op0OK =
4710 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4711 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
4712 if (!Op0OK && !Op1OK)
4713 return false;
4714
4715 ToPromote.insert(Op32.getNode());
4716
4717 if (Op0OK)
4718 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4719
4720 return true;
4721 }
4722
4723 return false;
4724}
4725
4726void PPCDAGToDAGISel::PeepholePPC64ZExt() {
4727 if (!PPCSubTarget->isPPC64())
4728 return;
4729
4730 // When we zero-extend from i32 to i64, we use a pattern like this:
4731 // def : Pat<(i64 (zext i32:$in)),
4732 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
4733 // 0, 32)>;
4734 // There are several 32-bit shift/rotate instructions, however, that will
4735 // clear the higher-order bits of their output, rendering the RLDICL
4736 // unnecessary. When that happens, we remove it here, and redefine the
4737 // relevant 32-bit operation to be a 64-bit operation.
4738
4739 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4740 ++Position;
4741
4742 bool MadeChange = false;
4743 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004744 SDNode *N = &*--Position;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004745 // Skip dead nodes and any non-machine opcodes.
4746 if (N->use_empty() || !N->isMachineOpcode())
4747 continue;
4748
4749 if (N->getMachineOpcode() != PPC::RLDICL)
4750 continue;
4751
4752 if (N->getConstantOperandVal(1) != 0 ||
4753 N->getConstantOperandVal(2) != 32)
4754 continue;
4755
4756 SDValue ISR = N->getOperand(0);
4757 if (!ISR.isMachineOpcode() ||
4758 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
4759 continue;
4760
4761 if (!ISR.hasOneUse())
4762 continue;
4763
4764 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
4765 continue;
4766
4767 SDValue IDef = ISR.getOperand(0);
4768 if (!IDef.isMachineOpcode() ||
4769 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
4770 continue;
4771
4772 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
4773 // can get rid of it.
4774
4775 SDValue Op32 = ISR->getOperand(1);
4776 if (!Op32.isMachineOpcode())
4777 continue;
4778
4779 // There are some 32-bit instructions that always clear the high-order 32
4780 // bits, there are also some instructions (like AND) that we can look
4781 // through.
4782 SmallPtrSet<SDNode *, 16> ToPromote;
4783 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4784 continue;
4785
4786 // If the ToPromote set contains nodes that have uses outside of the set
4787 // (except for the original INSERT_SUBREG), then abort the transformation.
4788 bool OutsideUse = false;
4789 for (SDNode *PN : ToPromote) {
4790 for (SDNode *UN : PN->uses()) {
4791 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4792 OutsideUse = true;
4793 break;
4794 }
4795 }
4796
4797 if (OutsideUse)
4798 break;
4799 }
4800 if (OutsideUse)
4801 continue;
4802
4803 MadeChange = true;
4804
4805 // We now know that this zero extension can be removed by promoting to
4806 // nodes in ToPromote to 64-bit operations, where for operations in the
4807 // frontier of the set, we need to insert INSERT_SUBREGs for their
4808 // operands.
4809 for (SDNode *PN : ToPromote) {
4810 unsigned NewOpcode;
4811 switch (PN->getMachineOpcode()) {
4812 default:
4813 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4814 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4815 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4816 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4817 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4818 case PPC::LI: NewOpcode = PPC::LI8; break;
4819 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004820 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4821 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004822 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00004823 case PPC::CNTTZW: NewOpcode = PPC::CNTTZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004824 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4825 case PPC::OR: NewOpcode = PPC::OR8; break;
4826 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4827 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4828 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4829 case PPC::AND: NewOpcode = PPC::AND8; break;
4830 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4831 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4832 }
4833
4834 // Note: During the replacement process, the nodes will be in an
4835 // inconsistent state (some instructions will have operands with values
4836 // of the wrong type). Once done, however, everything should be right
4837 // again.
4838
4839 SmallVector<SDValue, 4> Ops;
4840 for (const SDValue &V : PN->ops()) {
4841 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4842 !isa<ConstantSDNode>(V)) {
4843 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4844 SDNode *ReplOp =
4845 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4846 ISR.getNode()->getVTList(), ReplOpOps);
4847 Ops.push_back(SDValue(ReplOp, 0));
4848 } else {
4849 Ops.push_back(V);
4850 }
4851 }
4852
4853 // Because all to-be-promoted nodes only have users that are other
4854 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4855 // the i32 result value type with i64.
4856
4857 SmallVector<EVT, 2> NewVTs;
4858 SDVTList VTs = PN->getVTList();
4859 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4860 if (VTs.VTs[i] == MVT::i32)
4861 NewVTs.push_back(MVT::i64);
4862 else
4863 NewVTs.push_back(VTs.VTs[i]);
4864
4865 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4866 DEBUG(PN->dump(CurDAG));
4867
4868 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4869
4870 DEBUG(dbgs() << "\nNew: ");
4871 DEBUG(PN->dump(CurDAG));
4872 DEBUG(dbgs() << "\n");
4873 }
4874
4875 // Now we replace the original zero extend and its associated INSERT_SUBREG
4876 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4877 // return an i64).
4878
4879 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4880 DEBUG(N->dump(CurDAG));
4881 DEBUG(dbgs() << "\nNew: ");
4882 DEBUG(Op32.getNode()->dump(CurDAG));
4883 DEBUG(dbgs() << "\n");
4884
4885 ReplaceUses(N, Op32.getNode());
4886 }
4887
4888 if (MadeChange)
4889 CurDAG->RemoveDeadNodes();
4890}
4891
Hal Finkel940ab932014-02-28 00:27:01 +00004892void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004893 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004894 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004895 return;
4896
4897 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4898 ++Position;
4899
4900 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004901 SDNode *N = &*--Position;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004902 // Skip dead nodes and any non-machine opcodes.
4903 if (N->use_empty() || !N->isMachineOpcode())
4904 continue;
4905
4906 unsigned FirstOp;
4907 unsigned StorageOpcode = N->getMachineOpcode();
4908
4909 switch (StorageOpcode) {
4910 default: continue;
4911
4912 case PPC::LBZ:
4913 case PPC::LBZ8:
4914 case PPC::LD:
4915 case PPC::LFD:
4916 case PPC::LFS:
4917 case PPC::LHA:
4918 case PPC::LHA8:
4919 case PPC::LHZ:
4920 case PPC::LHZ8:
4921 case PPC::LWA:
4922 case PPC::LWZ:
4923 case PPC::LWZ8:
4924 FirstOp = 0;
4925 break;
4926
4927 case PPC::STB:
4928 case PPC::STB8:
4929 case PPC::STD:
4930 case PPC::STFD:
4931 case PPC::STFS:
4932 case PPC::STH:
4933 case PPC::STH8:
4934 case PPC::STW:
4935 case PPC::STW8:
4936 FirstOp = 1;
4937 break;
4938 }
4939
Kyle Butt1452b762015-12-11 00:47:36 +00004940 // If this is a load or store with a zero offset, or within the alignment,
4941 // we may be able to fold an add-immediate into the memory operation.
4942 // The check against alignment is below, as it can't occur until we check
4943 // the arguments to N
4944 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)))
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004945 continue;
4946
4947 SDValue Base = N->getOperand(FirstOp + 1);
4948 if (!Base.isMachineOpcode())
4949 continue;
4950
4951 unsigned Flags = 0;
4952 bool ReplaceFlags = true;
4953
4954 // When the feeding operation is an add-immediate of some sort,
4955 // determine whether we need to add relocation information to the
4956 // target flags on the immediate operand when we fold it into the
4957 // load instruction.
4958 //
4959 // For something like ADDItocL, the relocation information is
4960 // inferred from the opcode; when we process it in the AsmPrinter,
4961 // we add the necessary relocation there. A load, though, can receive
4962 // relocation from various flavors of ADDIxxx, so we need to carry
4963 // the relocation information in the target flags.
4964 switch (Base.getMachineOpcode()) {
4965 default: continue;
4966
4967 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004968 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004969 // In some cases (such as TLS) the relocation information
4970 // is already in place on the operand, so copying the operand
4971 // is sufficient.
4972 ReplaceFlags = false;
4973 // For these cases, the immediate may not be divisible by 4, in
4974 // which case the fold is illegal for DS-form instructions. (The
4975 // other cases provide aligned addresses and are always safe.)
4976 if ((StorageOpcode == PPC::LWA ||
4977 StorageOpcode == PPC::LD ||
4978 StorageOpcode == PPC::STD) &&
4979 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4980 Base.getConstantOperandVal(1) % 4 != 0))
4981 continue;
4982 break;
4983 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004984 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004985 break;
4986 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004987 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004988 break;
4989 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004990 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004991 break;
4992 }
4993
Kyle Butt1452b762015-12-11 00:47:36 +00004994 SDValue ImmOpnd = Base.getOperand(1);
Hal Finkelb54579f2016-09-02 00:28:20 +00004995
4996 // On PPC64, the TOC base pointer is guaranteed by the ABI only to have
4997 // 8-byte alignment, and so we can only use offsets less than 8 (otherwise,
4998 // we might have needed different @ha relocation values for the offset
4999 // pointers).
5000 int MaxDisplacement = 7;
Kyle Butt1452b762015-12-11 00:47:36 +00005001 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
5002 const GlobalValue *GV = GA->getGlobal();
Hal Finkelb54579f2016-09-02 00:28:20 +00005003 MaxDisplacement = std::min((int) GV->getAlignment() - 1, MaxDisplacement);
Kyle Butt1452b762015-12-11 00:47:36 +00005004 }
5005
Hal Finkel7b104d42016-09-02 21:37:07 +00005006 bool UpdateHBase = false;
5007 SDValue HBase = Base.getOperand(0);
5008
Kyle Butt1452b762015-12-11 00:47:36 +00005009 int Offset = N->getConstantOperandVal(FirstOp);
Hal Finkel42c83f12016-09-07 07:36:11 +00005010 if (ReplaceFlags) {
5011 if (Offset < 0 || Offset > MaxDisplacement) {
5012 // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
5013 // one use, then we can do this for any offset, we just need to also
5014 // update the offset (i.e. the symbol addend) on the addis also.
5015 if (Base.getMachineOpcode() != PPC::ADDItocL)
5016 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005017
Hal Finkel42c83f12016-09-07 07:36:11 +00005018 if (!HBase.isMachineOpcode() ||
5019 HBase.getMachineOpcode() != PPC::ADDIStocHA)
5020 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005021
Hal Finkel42c83f12016-09-07 07:36:11 +00005022 if (!Base.hasOneUse() || !HBase.hasOneUse())
5023 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005024
Hal Finkel42c83f12016-09-07 07:36:11 +00005025 SDValue HImmOpnd = HBase.getOperand(1);
5026 if (HImmOpnd != ImmOpnd)
5027 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005028
Hal Finkel42c83f12016-09-07 07:36:11 +00005029 UpdateHBase = true;
5030 }
5031 } else {
5032 // If we're directly folding the addend from an addi instruction, then:
5033 // 1. In general, the offset on the memory access must be zero.
5034 // 2. If the addend is a constant, then it can be combined with a
5035 // non-zero offset, but only if the result meets the encoding
5036 // requirements.
5037 if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) {
5038 Offset += C->getSExtValue();
5039
5040 if ((StorageOpcode == PPC::LWA || StorageOpcode == PPC::LD ||
5041 StorageOpcode == PPC::STD) && (Offset % 4) != 0)
5042 continue;
5043
5044 if (!isInt<16>(Offset))
5045 continue;
5046
5047 ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd),
5048 ImmOpnd.getValueType());
5049 } else if (Offset != 0) {
5050 continue;
5051 }
Hal Finkel7b104d42016-09-02 21:37:07 +00005052 }
Kyle Butt1452b762015-12-11 00:47:36 +00005053
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005054 // We found an opportunity. Reverse the operands from the add
5055 // immediate and substitute them into the load or store. If
5056 // needed, update the target flags for the immediate operand to
5057 // reflect the necessary relocation information.
5058 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
5059 DEBUG(Base->dump(CurDAG));
5060 DEBUG(dbgs() << "\nN: ");
5061 DEBUG(N->dump(CurDAG));
5062 DEBUG(dbgs() << "\n");
5063
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005064 // If the relocation information isn't already present on the
5065 // immediate operand, add it now.
5066 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00005067 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005068 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005069 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00005070 // We can't perform this optimization for data whose alignment
5071 // is insufficient for the instruction encoding.
5072 if (GV->getAlignment() < 4 &&
5073 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
Kyle Butt1452b762015-12-11 00:47:36 +00005074 StorageOpcode == PPC::LWA || (Offset % 4) != 0)) {
Bill Schmidt48fc20a2013-07-01 20:52:27 +00005075 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
5076 continue;
5077 }
Kyle Butt1452b762015-12-11 00:47:36 +00005078 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00005079 } else if (ConstantPoolSDNode *CP =
5080 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00005081 const Constant *C = CP->getConstVal();
5082 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
5083 CP->getAlignment(),
Kyle Butt1452b762015-12-11 00:47:36 +00005084 Offset, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005085 }
5086 }
5087
5088 if (FirstOp == 1) // Store
5089 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
5090 Base.getOperand(0), N->getOperand(3));
5091 else // Load
5092 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
5093 N->getOperand(2));
5094
Hal Finkel7b104d42016-09-02 21:37:07 +00005095 if (UpdateHBase)
5096 (void)CurDAG->UpdateNodeOperands(HBase.getNode(), HBase.getOperand(0),
5097 ImmOpnd);
5098
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005099 // The add-immediate may now be dead, in which case remove it.
5100 if (Base.getNode()->use_empty())
5101 CurDAG->RemoveDeadNode(Base.getNode());
5102 }
5103}
Chris Lattner43ff01e2005-08-17 19:33:03 +00005104
Andrew Trickc416ba62010-12-24 04:28:06 +00005105/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00005106/// PowerPC-specific DAG, ready for instruction scheduling.
5107///
Hiroshi Inoue51020282017-06-27 04:52:17 +00005108FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM,
5109 CodeGenOpt::Level OptLevel) {
5110 return new PPCDAGToDAGISel(TM, OptLevel);
Chris Lattner43ff01e2005-08-17 19:33:03 +00005111}