blob: 06152d0510707c16dec5808f3485a9f11270d6a0 [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"
Chris Lattner43ff01e2005-08-17 19:33:03 +000035#include "llvm/CodeGen/SelectionDAG.h"
36#include "llvm/CodeGen/SelectionDAGISel.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000037#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000038#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetRegisterInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000040#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000041#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Function.h"
44#include "llvm/IR/GlobalValue.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000045#include "llvm/IR/InlineAsm.h"
46#include "llvm/IR/InstrTypes.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000047#include "llvm/IR/Module.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000048#include "llvm/Support/Casting.h"
49#include "llvm/Support/CodeGen.h"
Hal Finkel940ab932014-02-28 00:27:01 +000050#include "llvm/Support/CommandLine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000051#include "llvm/Support/Compiler.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000052#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000053#include "llvm/Support/ErrorHandling.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000054#include "llvm/Support/KnownBits.h"
David Blaikie13e77db2018-03-23 23:58:25 +000055#include "llvm/Support/MachineValueType.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000056#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000057#include "llvm/Support/raw_ostream.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000058#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 Ivanovicdb7e7702017-11-30 13:39:10 +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.");
80STATISTIC(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
Zaara Syeda1110c4d2018-03-15 15:34:41 +0000104static cl::opt<bool> EnableTLSOpt(
105 "ppc-tls-opt", cl::init(true),
106 cl::desc("Enable tls optimization peephole"),
107 cl::Hidden);
108
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +0000109enum ICmpInGPRType { ICGPR_All, ICGPR_None, ICGPR_I32, ICGPR_I64,
110 ICGPR_NonExtIn, ICGPR_Zext, ICGPR_Sext, ICGPR_ZextI32,
111 ICGPR_SextI32, ICGPR_ZextI64, ICGPR_SextI64 };
112
113static cl::opt<ICmpInGPRType> CmpInGPR(
Nemanja Ivanovic43645132017-12-01 12:02:59 +0000114 "ppc-gpr-icmps", cl::Hidden, cl::init(ICGPR_All),
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +0000115 cl::desc("Specify the types of comparisons to emit GPR-only code for."),
116 cl::values(clEnumValN(ICGPR_None, "none", "Do not modify integer comparisons."),
117 clEnumValN(ICGPR_All, "all", "All possible int comparisons in GPRs."),
118 clEnumValN(ICGPR_I32, "i32", "Only i32 comparisons in GPRs."),
119 clEnumValN(ICGPR_I64, "i64", "Only i64 comparisons in GPRs."),
120 clEnumValN(ICGPR_NonExtIn, "nonextin",
121 "Only comparisons where inputs don't need [sz]ext."),
122 clEnumValN(ICGPR_Zext, "zext", "Only comparisons with zext result."),
123 clEnumValN(ICGPR_ZextI32, "zexti32",
124 "Only i32 comparisons with zext result."),
125 clEnumValN(ICGPR_ZextI64, "zexti64",
126 "Only i64 comparisons with zext result."),
127 clEnumValN(ICGPR_Sext, "sext", "Only comparisons with sext result."),
128 clEnumValN(ICGPR_SextI32, "sexti32",
129 "Only i32 comparisons with sext result."),
130 clEnumValN(ICGPR_SextI64, "sexti64",
131 "Only i64 comparisons with sext result.")));
Chris Lattner43ff01e2005-08-17 19:33:03 +0000132namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000133
Chris Lattner43ff01e2005-08-17 19:33:03 +0000134 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +0000135 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +0000136 /// instructions for SelectionDAG operations.
137 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000138 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +0000139 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000140 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +0000141 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +0000142 unsigned GlobalBaseReg;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000143
Chris Lattner43ff01e2005-08-17 19:33:03 +0000144 public:
Hiroshi Inoue51020282017-06-27 04:52:17 +0000145 explicit PPCDAGToDAGISel(PPCTargetMachine &tm, CodeGenOpt::Level OptLevel)
146 : SelectionDAGISel(tm, OptLevel), TM(tm) {}
Andrew Trickc416ba62010-12-24 04:28:06 +0000147
Craig Topper0d3fa922014-04-29 07:57:37 +0000148 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +0000149 // Make sure we re-emit a set of the global base reg if necessary
150 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +0000151 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
152 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +0000153 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +0000154
Eric Christopher1b8e7632014-05-22 01:07:24 +0000155 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +0000156 InsertVRSaveCode(MF);
157
Chris Lattner1678a6c2006-03-16 18:25:23 +0000158 return true;
Chris Lattner45640392005-08-19 22:38:53 +0000159 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000160
Hal Finkel4edc66b2015-01-03 01:16:37 +0000161 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000162 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +0000163
Hiroshi Inouecc555bd2017-08-23 08:55:18 +0000164 /// getI16Imm - Return a target constant with the specified value, of type
165 /// i16.
166 inline SDValue getI16Imm(unsigned Imm, const SDLoc &dl) {
167 return CurDAG->getTargetConstant(Imm, dl, MVT::i16);
168 }
169
Chris Lattner43ff01e2005-08-17 19:33:03 +0000170 /// getI32Imm - Return a target constant with the specified value, of type
171 /// i32.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000172 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000173 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000174 }
Chris Lattner45640392005-08-19 22:38:53 +0000175
Chris Lattner97b3da12006-06-27 00:04:13 +0000176 /// getI64Imm - Return a target constant with the specified value, of type
177 /// i64.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000178 inline SDValue getI64Imm(uint64_t Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000179 return CurDAG->getTargetConstant(Imm, dl, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000180 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000181
Chris Lattner97b3da12006-06-27 00:04:13 +0000182 /// getSmallIPtrImm - Return a target constant of pointer type.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000183 inline SDValue getSmallIPtrImm(unsigned Imm, const SDLoc &dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000184 return CurDAG->getTargetConstant(
185 Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner97b3da12006-06-27 00:04:13 +0000186 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000187
Nate Begemand31efd12006-09-22 05:01:56 +0000188 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
189 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000190 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000191 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000192
Chris Lattner45640392005-08-19 22:38:53 +0000193 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
194 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000195 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000196
Justin Bognerdc8af062016-05-20 21:43:23 +0000197 void selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
Hal Finkelb5e9b042014-12-11 22:51:06 +0000198
Chris Lattner43ff01e2005-08-17 19:33:03 +0000199 // Select - Convert the specified operand from a target-independent to a
200 // target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +0000201 void Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000202
Justin Bognerdc8af062016-05-20 21:43:23 +0000203 bool tryBitfieldInsert(SDNode *N);
204 bool tryBitPermutation(SDNode *N);
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +0000205 bool tryIntCompareInGPR(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000206
Zaara Syeda1110c4d2018-03-15 15:34:41 +0000207 // tryTLSXFormLoad - Convert an ISD::LOAD fed by a PPCISD::ADD_TLS into
208 // an X-Form load instruction with the offset being a relocation coming from
209 // the PPCISD::ADD_TLS.
210 bool tryTLSXFormLoad(LoadSDNode *N);
211 // tryTLSXFormStore - Convert an ISD::STORE fed by a PPCISD::ADD_TLS into
212 // an X-Form store instruction with the offset being a relocation coming from
213 // the PPCISD::ADD_TLS.
214 bool tryTLSXFormStore(StoreSDNode *N);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000215 /// SelectCC - Select a comparison of the specified values with the
216 /// specified condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000217 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
218 const SDLoc &dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000219
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000220 /// SelectAddrImm - Returns true if the address N can be represented by
221 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000222 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000223 SDValue &Base) {
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +0000224 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 0);
Chris Lattnera801fced2006-11-08 02:15:41 +0000225 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000226
Chris Lattner6f5840c2006-11-16 00:41:37 +0000227 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000228 /// immediate field. Note that the operand at this point is already the
229 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000230 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000231 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000232 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000233 Out = N;
234 return true;
235 }
236
237 return false;
238 }
239
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000240 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
241 /// represented as an indexed [r+r] operation. Returns false if it can
242 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000243 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000244 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000245 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000246
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000247 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
248 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000249 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000250 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000251 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000252
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000253 /// SelectAddrImmX4 - Returns true if the address N can be represented by
254 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
255 /// Suitable for use by STD and friends.
256 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +0000257 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 4);
258 }
259
260 bool SelectAddrImmX16(SDValue N, SDValue &Disp, SDValue &Base) {
261 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, 16);
Chris Lattnera801fced2006-11-08 02:15:41 +0000262 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000263
Hal Finkel756810f2013-03-21 21:37:52 +0000264 // Select an address into a single register.
265 bool SelectAddr(SDValue N, SDValue &Base) {
266 Base = N;
267 return true;
268 }
269
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000270 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000271 /// inline asm expressions. It is always correct to compute the value into
272 /// a register. The case of adding a (possibly relocatable) constant to a
273 /// register can be improved, but it is wrong to substitute Reg+Reg for
274 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000275 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000276 unsigned ConstraintID,
Craig Topper0d3fa922014-04-29 07:57:37 +0000277 std::vector<SDValue> &OutOps) override {
Daniel Sanders08288602015-03-17 11:09:13 +0000278 switch(ConstraintID) {
279 default:
280 errs() << "ConstraintID: " << ConstraintID << "\n";
281 llvm_unreachable("Unexpected asm memory constraint");
282 case InlineAsm::Constraint_es:
Daniel Sanders914b9472015-03-17 12:00:04 +0000283 case InlineAsm::Constraint_i:
Daniel Sanders08288602015-03-17 11:09:13 +0000284 case InlineAsm::Constraint_m:
285 case InlineAsm::Constraint_o:
286 case InlineAsm::Constraint_Q:
287 case InlineAsm::Constraint_Z:
288 case InlineAsm::Constraint_Zy:
289 // We need to make sure that this one operand does not end up in r0
290 // (because we might end up lowering this as 0(%op)).
291 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
292 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000293 SDLoc dl(Op);
294 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Daniel Sanders08288602015-03-17 11:09:13 +0000295 SDValue NewOp =
296 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000297 dl, Op.getValueType(),
Daniel Sanders08288602015-03-17 11:09:13 +0000298 Op, RC), 0);
299
300 OutOps.push_back(NewOp);
301 return false;
302 }
303 return true;
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000304 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000305
Dan Gohman5ea74d52009-07-31 18:16:33 +0000306 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000307
Mehdi Amini117296c2016-10-01 02:56:57 +0000308 StringRef getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000309 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000310 }
311
Chris Lattner03e08ee2005-09-13 22:03:06 +0000312// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000313#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000314
Chris Lattner259e6c72005-10-06 18:45:51 +0000315private:
Justin Bognerdc8af062016-05-20 21:43:23 +0000316 bool trySETCC(SDNode *N);
Hal Finkel940ab932014-02-28 00:27:01 +0000317
318 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000319 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000320 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000321
Hal Finkel4edc66b2015-01-03 01:16:37 +0000322 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000323 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000324
Hal Finkelb9989152014-02-28 06:11:16 +0000325 bool AllUsersSelectZero(SDNode *N);
326 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000327
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +0000328 bool isOffsetMultipleOf(SDNode *N, unsigned Val) const;
Justin Bognerdc8af062016-05-20 21:43:23 +0000329 void transferMemOperands(SDNode *N, SDNode *Result);
Hiroshi Inoue33486782018-04-21 09:32:17 +0000330 MachineSDNode *flipSignBit(const SDValue &N, SDNode **SignBit = nullptr);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000331 };
Eugene Zelenko8187c192017-01-13 00:58:58 +0000332
333} // end anonymous namespace
Chris Lattner43ff01e2005-08-17 19:33:03 +0000334
Chris Lattner1678a6c2006-03-16 18:25:23 +0000335/// InsertVRSaveCode - Once the entire function has been instruction selected,
336/// all virtual registers are created and all machine instructions are built,
337/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000338void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000339 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000340 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000341 //
Dan Gohman4a618822010-02-10 16:03:48 +0000342 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000343 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000344 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000345 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
346 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
347 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000348 HasVectorVReg = true;
349 break;
350 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000351 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000352 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000353
Chris Lattner02e2c182006-03-13 21:52:10 +0000354 // If we have a vector register, we want to emit code into the entry and exit
355 // blocks to save and restore the VRSAVE register. We do this here (instead
356 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
357 //
358 // 1. This (trivially) reduces the load on the register allocator, by not
359 // having to represent the live range of the VRSAVE register.
360 // 2. This (more significantly) allows us to create a temporary virtual
361 // register to hold the saved VRSAVE value, allowing this temporary to be
362 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000363
364 // Create two vregs - one to hold the VRSAVE register that is live-in to the
365 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000366 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
367 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000368
Eric Christophercccae792015-01-30 22:02:31 +0000369 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000370 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000371 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000372 // Emit the following code into the entry block:
373 // InVRSAVE = MFVRSAVE
374 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
375 // MTVRSAVE UpdatedVRSAVE
376 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000377 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
378 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000379 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000380 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000381
Chris Lattner1678a6c2006-03-16 18:25:23 +0000382 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000383 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000384 if (BB->isReturnBlock()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000385 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000386
Chris Lattner1678a6c2006-03-16 18:25:23 +0000387 // Skip over all terminator instructions, which are part of the return
388 // sequence.
389 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000390 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000391 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000392
Chris Lattner1678a6c2006-03-16 18:25:23 +0000393 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000394 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000395 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000396 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000397}
Chris Lattner8ae95252005-09-03 01:17:22 +0000398
Chris Lattner45640392005-08-19 22:38:53 +0000399/// getGlobalBaseReg - Output the instructions required to put the
400/// base address to use for accessing globals into a register.
401///
Evan Cheng61413a32006-08-26 05:34:46 +0000402SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000403 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000404 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000405 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000406 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000407 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Matthias Braunf1caa282017-12-15 22:22:58 +0000408 const Module *M = MF->getFunction().getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000409 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000410
Mehdi Amini44ede332015-07-09 02:09:04 +0000411 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000412 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000413 GlobalBaseReg = PPC::R30;
Davide Italiano4cccc482016-06-17 18:07:14 +0000414 if (M->getPICLevel() == PICLevel::SmallPIC) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000415 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
416 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000417 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000418 } else {
419 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
420 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
421 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
422 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000423 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000424 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
425 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
426 }
427 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000428 GlobalBaseReg =
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000429 RegInfo->createVirtualRegister(&PPC::GPRC_and_GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000430 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
431 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000432 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000433 } else {
Nemanja Ivanovicbcc82c92018-02-23 23:08:34 +0000434 // We must ensure that this sequence is dominated by the prologue.
435 // FIXME: This is a bit of a big hammer since we don't get the benefits
436 // of shrink-wrapping whenever we emit this instruction. Considering
437 // this is used in any function where we emit a jump table, this may be
438 // a significant limitation. We should consider inserting this in the
439 // block where it is used and then commoning this sequence up if it
440 // appears in multiple places.
Hiroshi Inoueb5578462018-06-07 12:49:12 +0000441 // Note: on ISA 3.0 cores, we can use lnia (addpcis) instead of
Nemanja Ivanovicbcc82c92018-02-23 23:08:34 +0000442 // MovePCtoLR8.
443 MF->getInfo<PPCFunctionInfo>()->setShrinkWrapDisabled(true);
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000444 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_and_G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000445 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000446 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000447 }
Chris Lattner45640392005-08-19 22:38:53 +0000448 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000449 return CurDAG->getRegister(GlobalBaseReg,
Mehdi Amini44ede332015-07-09 02:09:04 +0000450 PPCLowering->getPointerTy(CurDAG->getDataLayout()))
451 .getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000452}
453
Chris Lattner97b3da12006-06-27 00:04:13 +0000454/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
455/// operand. If so Imm will receive the 32-bit value.
456static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000457 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000458 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000459 return true;
460 }
461 return false;
462}
463
Chris Lattner97b3da12006-06-27 00:04:13 +0000464/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
465/// operand. If so Imm will receive the 64-bit value.
466static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000467 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000468 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000469 return true;
470 }
471 return false;
472}
473
474// isInt32Immediate - This method tests to see if a constant operand.
475// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000476static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000477 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000478}
479
Hiroshi Inouecc555bd2017-08-23 08:55:18 +0000480/// isInt64Immediate - This method tests to see if the value is a 64-bit
481/// constant operand. If so Imm will receive the 64-bit value.
482static bool isInt64Immediate(SDValue N, uint64_t &Imm) {
483 return isInt64Immediate(N.getNode(), Imm);
484}
485
Hal Finkel65539e32015-12-12 00:32:00 +0000486static unsigned getBranchHint(unsigned PCC, FunctionLoweringInfo *FuncInfo,
487 const SDValue &DestMBB) {
488 assert(isa<BasicBlockSDNode>(DestMBB));
489
490 if (!FuncInfo->BPI) return PPC::BR_NO_HINT;
491
492 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
493 const TerminatorInst *BBTerm = BB->getTerminator();
494
495 if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT;
496
497 const BasicBlock *TBB = BBTerm->getSuccessor(0);
498 const BasicBlock *FBB = BBTerm->getSuccessor(1);
499
Cong Houe93b8e12015-12-22 18:56:14 +0000500 auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);
501 auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);
Hal Finkel65539e32015-12-12 00:32:00 +0000502
503 // We only want to handle cases which are easy to predict at static time, e.g.
504 // C++ throw statement, that is very likely not taken, or calling never
505 // returned function, e.g. stdlib exit(). So we set Threshold to filter
506 // unwanted cases.
507 //
508 // Below is LLVM branch weight table, we only want to handle case 1, 2
509 //
510 // Case Taken:Nontaken Example
511 // 1. Unreachable 1048575:1 C++ throw, stdlib exit(),
512 // 2. Invoke-terminating 1:1048575
513 // 3. Coldblock 4:64 __builtin_expect
514 // 4. Loop Branch 124:4 For loop
515 // 5. PH/ZH/FPH 20:12
516 const uint32_t Threshold = 10000;
517
Cong Houe93b8e12015-12-22 18:56:14 +0000518 if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))
Hal Finkel65539e32015-12-12 00:32:00 +0000519 return PPC::BR_NO_HINT;
520
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000521 LLVM_DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName()
522 << "::" << BB->getName() << "'\n"
523 << " -> " << TBB->getName() << ": " << TProb << "\n"
524 << " -> " << FBB->getName() << ": " << FProb << "\n");
Hal Finkel65539e32015-12-12 00:32:00 +0000525
526 const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);
527
Cong Houe93b8e12015-12-22 18:56:14 +0000528 // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities,
529 // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock
Hal Finkel65539e32015-12-12 00:32:00 +0000530 if (BBDN->getBasicBlock()->getBasicBlock() != TBB)
Cong Houe93b8e12015-12-22 18:56:14 +0000531 std::swap(TProb, FProb);
Hal Finkel65539e32015-12-12 00:32:00 +0000532
Cong Houe93b8e12015-12-22 18:56:14 +0000533 return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;
Hal Finkel65539e32015-12-12 00:32:00 +0000534}
Chris Lattner97b3da12006-06-27 00:04:13 +0000535
536// isOpcWithIntImmediate - This method tests to see if the node is a specific
537// opcode and that it has a immediate integer right operand.
538// If so Imm will receive the 32 bit value.
539static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000540 return N->getOpcode() == Opc
541 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000542}
543
Justin Bognerdc8af062016-05-20 21:43:23 +0000544void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
Hal Finkelb5e9b042014-12-11 22:51:06 +0000545 SDLoc dl(SN);
546 int FI = cast<FrameIndexSDNode>(N)->getIndex();
547 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
548 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
549 if (SN->hasOneUse())
Justin Bognerdc8af062016-05-20 21:43:23 +0000550 CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
551 getSmallIPtrImm(Offset, dl));
552 else
553 ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
554 getSmallIPtrImm(Offset, dl)));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000555}
556
Andrew Trickc416ba62010-12-24 04:28:06 +0000557bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
558 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000559 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000560 // Don't even go down this path for i64, since different logic will be
561 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000562 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000563 return false;
564
Nate Begemanb3821a32005-08-18 07:30:46 +0000565 unsigned Shift = 32;
566 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
567 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000568 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000569 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000570 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000571
Nate Begemanb3821a32005-08-18 07:30:46 +0000572 if (Opcode == ISD::SHL) {
573 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000574 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000575 // determine which bits are made indeterminant by shift
576 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000577 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000578 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000579 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000580 // determine which bits are made indeterminant by shift
581 Indeterminant = ~(0xFFFFFFFFu >> Shift);
582 // adjust for the left rotate
583 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000584 } else if (Opcode == ISD::ROTL) {
585 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000586 } else {
587 return false;
588 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000589
Nate Begemanb3821a32005-08-18 07:30:46 +0000590 // if the mask doesn't intersect any Indeterminant bits
591 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000592 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000593 // make sure the mask is still a mask (wrap arounds may not be)
594 return isRunOfOnes(Mask, MB, ME);
595 }
596 return false;
597}
598
Zaara Syeda1110c4d2018-03-15 15:34:41 +0000599bool PPCDAGToDAGISel::tryTLSXFormStore(StoreSDNode *ST) {
600 SDValue Base = ST->getBasePtr();
601 if (Base.getOpcode() != PPCISD::ADD_TLS)
602 return false;
603 SDValue Offset = ST->getOffset();
604 if (!Offset.isUndef())
605 return false;
606
607 SDLoc dl(ST);
608 EVT MemVT = ST->getMemoryVT();
609 EVT RegVT = ST->getValue().getValueType();
610
611 unsigned Opcode;
612 switch (MemVT.getSimpleVT().SimpleTy) {
613 default:
614 return false;
615 case MVT::i8: {
616 Opcode = (RegVT == MVT::i32) ? PPC::STBXTLS_32 : PPC::STBXTLS;
617 break;
618 }
619 case MVT::i16: {
620 Opcode = (RegVT == MVT::i32) ? PPC::STHXTLS_32 : PPC::STHXTLS;
621 break;
622 }
623 case MVT::i32: {
624 Opcode = (RegVT == MVT::i32) ? PPC::STWXTLS_32 : PPC::STWXTLS;
625 break;
626 }
627 case MVT::i64: {
628 Opcode = PPC::STDXTLS;
629 break;
630 }
631 }
632 SDValue Chain = ST->getChain();
633 SDVTList VTs = ST->getVTList();
634 SDValue Ops[] = {ST->getValue(), Base.getOperand(0), Base.getOperand(1),
635 Chain};
636 SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
637 transferMemOperands(ST, MN);
638 ReplaceNode(ST, MN);
639 return true;
640}
641
642bool PPCDAGToDAGISel::tryTLSXFormLoad(LoadSDNode *LD) {
643 SDValue Base = LD->getBasePtr();
644 if (Base.getOpcode() != PPCISD::ADD_TLS)
645 return false;
646 SDValue Offset = LD->getOffset();
647 if (!Offset.isUndef())
648 return false;
649
650 SDLoc dl(LD);
651 EVT MemVT = LD->getMemoryVT();
652 EVT RegVT = LD->getValueType(0);
653 unsigned Opcode;
654 switch (MemVT.getSimpleVT().SimpleTy) {
655 default:
656 return false;
657 case MVT::i8: {
658 Opcode = (RegVT == MVT::i32) ? PPC::LBZXTLS_32 : PPC::LBZXTLS;
659 break;
660 }
661 case MVT::i16: {
662 Opcode = (RegVT == MVT::i32) ? PPC::LHZXTLS_32 : PPC::LHZXTLS;
663 break;
664 }
665 case MVT::i32: {
666 Opcode = (RegVT == MVT::i32) ? PPC::LWZXTLS_32 : PPC::LWZXTLS;
667 break;
668 }
669 case MVT::i64: {
670 Opcode = PPC::LDXTLS;
671 break;
672 }
673 }
674 SDValue Chain = LD->getChain();
675 SDVTList VTs = LD->getVTList();
676 SDValue Ops[] = {Base.getOperand(0), Base.getOperand(1), Chain};
677 SDNode *MN = CurDAG->getMachineNode(Opcode, dl, VTs, Ops);
678 transferMemOperands(LD, MN);
679 ReplaceNode(LD, MN);
680 return true;
681}
682
Justin Bognerdc8af062016-05-20 21:43:23 +0000683/// Turn an or of two masked values into the rotate left word immediate then
684/// mask insert (rlwimi) instruction.
685bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000686 SDValue Op0 = N->getOperand(0);
687 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000688 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000689
Craig Topperd0af7e82017-04-28 05:31:46 +0000690 KnownBits LKnown, RKnown;
691 CurDAG->computeKnownBits(Op0, LKnown);
692 CurDAG->computeKnownBits(Op1, RKnown);
Andrew Trickc416ba62010-12-24 04:28:06 +0000693
Craig Topperd0af7e82017-04-28 05:31:46 +0000694 unsigned TargetMask = LKnown.Zero.getZExtValue();
695 unsigned InsertMask = RKnown.Zero.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000696
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000697 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
698 unsigned Op0Opc = Op0.getOpcode();
699 unsigned Op1Opc = Op1.getOpcode();
700 unsigned Value, SH = 0;
701 TargetMask = ~TargetMask;
702 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000703
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000704 // If the LHS has a foldable shift and the RHS does not, then swap it to the
705 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000706 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
707 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
708 Op0.getOperand(0).getOpcode() == ISD::SRL) {
709 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
710 Op1.getOperand(0).getOpcode() != ISD::SRL) {
711 std::swap(Op0, Op1);
712 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000713 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000714 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000715 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000716 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
717 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
718 Op1.getOperand(0).getOpcode() != ISD::SRL) {
719 std::swap(Op0, Op1);
720 std::swap(Op0Opc, Op1Opc);
721 std::swap(TargetMask, InsertMask);
722 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000723 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000724
Nate Begeman1333cea2006-05-07 00:23:38 +0000725 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000726 if (isRunOfOnes(InsertMask, MB, ME)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000727 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000728 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000729 Op1 = Op1.getOperand(0);
730 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
731 }
732 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000733 // The AND mask might not be a constant, and we need to make sure that
734 // if we're going to fold the masking with the insert, all bits not
735 // know to be zero in the mask are known to be one.
Craig Topperd0af7e82017-04-28 05:31:46 +0000736 KnownBits MKnown;
737 CurDAG->computeKnownBits(Op1.getOperand(1), MKnown);
738 bool CanFoldMask = InsertMask == MKnown.One.getZExtValue();
Hal Finkeld9963c72014-04-13 17:10:58 +0000739
Nate Begeman1333cea2006-05-07 00:23:38 +0000740 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000741 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000742 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000743 // Note that Value must be in range here (less than 32) because
744 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000745 Op1 = Op1.getOperand(0).getOperand(0);
746 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000747 }
748 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000749
Chris Lattnera2963392006-05-12 16:29:37 +0000750 SH &= 31;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000751 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
752 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +0000753 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
754 return true;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000755 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000756 }
Justin Bognerdc8af062016-05-20 21:43:23 +0000757 return false;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000758}
759
Hal Finkelc58ce412015-01-01 02:53:29 +0000760// Predict the number of instructions that would be generated by calling
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000761// selectI64Imm(N).
762static unsigned selectI64ImmInstrCountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000763 // Assume no remaining bits.
764 unsigned Remainder = 0;
765 // Assume no shift required.
766 unsigned Shift = 0;
767
768 // If it can't be represented as a 32 bit value.
769 if (!isInt<32>(Imm)) {
770 Shift = countTrailingZeros<uint64_t>(Imm);
771 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
772
773 // If the shifted value fits 32 bits.
774 if (isInt<32>(ImmSh)) {
775 // Go with the shifted value.
776 Imm = ImmSh;
777 } else {
778 // Still stuck with a 64 bit value.
779 Remainder = Imm;
780 Shift = 32;
781 Imm >>= 32;
782 }
783 }
784
785 // Intermediate operand.
786 unsigned Result = 0;
787
788 // Handle first 32 bits.
789 unsigned Lo = Imm & 0xFFFF;
Hal Finkelc58ce412015-01-01 02:53:29 +0000790
791 // Simple value.
792 if (isInt<16>(Imm)) {
793 // Just the Lo bits.
794 ++Result;
795 } else if (Lo) {
796 // Handle the Hi bits and Lo bits.
797 Result += 2;
798 } else {
799 // Just the Hi bits.
800 ++Result;
801 }
802
803 // If no shift, we're done.
804 if (!Shift) return Result;
805
Guozhi Wei0cd65422016-10-14 20:41:50 +0000806 // If Hi word == Lo word,
807 // we can use rldimi to insert the Lo word into Hi word.
808 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
809 ++Result;
810 return Result;
811 }
812
Hal Finkelc58ce412015-01-01 02:53:29 +0000813 // Shift for next step if the upper 32-bits were not zero.
814 if (Imm)
815 ++Result;
816
817 // Add in the last bits as required.
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000818 if ((Remainder >> 16) & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000819 ++Result;
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000820 if (Remainder & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000821 ++Result;
822
823 return Result;
824}
825
Hal Finkel241ba792015-01-04 15:43:55 +0000826static uint64_t Rot64(uint64_t Imm, unsigned R) {
827 return (Imm << R) | (Imm >> (64 - R));
828}
829
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000830static unsigned selectI64ImmInstrCount(int64_t Imm) {
831 unsigned Count = selectI64ImmInstrCountDirect(Imm);
Hiroshi Inouef55ee1b2017-07-11 05:28:26 +0000832
833 // If the instruction count is 1 or 2, we do not need further analysis
834 // since rotate + load constant requires at least 2 instructions.
835 if (Count <= 2)
Hal Finkel2f618792015-01-05 03:41:38 +0000836 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000837
Hal Finkel241ba792015-01-04 15:43:55 +0000838 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000839 uint64_t RImm = Rot64(Imm, r);
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000840 unsigned RCount = selectI64ImmInstrCountDirect(RImm) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000841 Count = std::min(Count, RCount);
842
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000843 // See comments in selectI64Imm for an explanation of the logic below.
Hal Finkel2f618792015-01-05 03:41:38 +0000844 unsigned LS = findLastSet(RImm);
845 if (LS != r-1)
846 continue;
847
848 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
849 uint64_t RImmWithOnes = RImm | OnesMask;
850
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000851 RCount = selectI64ImmInstrCountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000852 Count = std::min(Count, RCount);
853 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000854
Hal Finkel241ba792015-01-04 15:43:55 +0000855 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000856}
857
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000858// Select a 64-bit constant. For cost-modeling purposes, selectI64ImmInstrCount
Hal Finkelc58ce412015-01-01 02:53:29 +0000859// (above) needs to be kept in sync with this function.
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000860static SDNode *selectI64ImmDirect(SelectionDAG *CurDAG, const SDLoc &dl,
861 int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000862 // Assume no remaining bits.
863 unsigned Remainder = 0;
864 // Assume no shift required.
865 unsigned Shift = 0;
866
867 // If it can't be represented as a 32 bit value.
868 if (!isInt<32>(Imm)) {
869 Shift = countTrailingZeros<uint64_t>(Imm);
870 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
871
872 // If the shifted value fits 32 bits.
873 if (isInt<32>(ImmSh)) {
874 // Go with the shifted value.
875 Imm = ImmSh;
876 } else {
877 // Still stuck with a 64 bit value.
878 Remainder = Imm;
879 Shift = 32;
880 Imm >>= 32;
881 }
882 }
883
884 // Intermediate operand.
885 SDNode *Result;
886
887 // Handle first 32 bits.
888 unsigned Lo = Imm & 0xFFFF;
889 unsigned Hi = (Imm >> 16) & 0xFFFF;
890
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000891 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
892 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000893 };
894
895 // Simple value.
896 if (isInt<16>(Imm)) {
Nemanja Ivanovicb0783cc2017-12-12 12:09:34 +0000897 uint64_t SextImm = SignExtend64(Lo, 16);
898 SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64);
Hal Finkelc58ce412015-01-01 02:53:29 +0000899 // Just the Lo bits.
Nemanja Ivanovicb0783cc2017-12-12 12:09:34 +0000900 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm);
Hal Finkelc58ce412015-01-01 02:53:29 +0000901 } else if (Lo) {
902 // Handle the Hi bits.
903 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
904 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
905 // And Lo bits.
906 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
907 SDValue(Result, 0), getI32Imm(Lo));
908 } else {
909 // Just the Hi bits.
910 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
911 }
912
913 // If no shift, we're done.
914 if (!Shift) return Result;
915
Guozhi Wei0cd65422016-10-14 20:41:50 +0000916 // If Hi word == Lo word,
917 // we can use rldimi to insert the Lo word into Hi word.
918 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
919 SDValue Ops[] =
920 { SDValue(Result, 0), SDValue(Result, 0), getI32Imm(Shift), getI32Imm(0)};
921 return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops);
922 }
923
Hal Finkelc58ce412015-01-01 02:53:29 +0000924 // Shift for next step if the upper 32-bits were not zero.
925 if (Imm) {
926 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
927 SDValue(Result, 0),
928 getI32Imm(Shift),
929 getI32Imm(63 - Shift));
930 }
931
932 // Add in the last bits as required.
933 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
934 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
935 SDValue(Result, 0), getI32Imm(Hi));
936 }
937 if ((Lo = Remainder & 0xFFFF)) {
938 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
939 SDValue(Result, 0), getI32Imm(Lo));
940 }
941
942 return Result;
943}
944
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000945static SDNode *selectI64Imm(SelectionDAG *CurDAG, const SDLoc &dl,
946 int64_t Imm) {
947 unsigned Count = selectI64ImmInstrCountDirect(Imm);
Hiroshi Inouef55ee1b2017-07-11 05:28:26 +0000948
949 // If the instruction count is 1 or 2, we do not need further analysis
950 // since rotate + load constant requires at least 2 instructions.
951 if (Count <= 2)
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000952 return selectI64ImmDirect(CurDAG, dl, Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000953
Hal Finkel241ba792015-01-04 15:43:55 +0000954 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000955
Hal Finkel2f618792015-01-05 03:41:38 +0000956 int64_t MatImm;
957 unsigned MaskEnd;
958
Hal Finkel241ba792015-01-04 15:43:55 +0000959 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000960 uint64_t RImm = Rot64(Imm, r);
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000961 unsigned RCount = selectI64ImmInstrCountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000962 if (RCount < Count) {
963 Count = RCount;
964 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000965 MatImm = RImm;
966 MaskEnd = 63;
967 }
968
969 // If the immediate to generate has many trailing zeros, it might be
970 // worthwhile to generate a rotated value with too many leading ones
971 // (because that's free with li/lis's sign-extension semantics), and then
972 // mask them off after rotation.
973
974 unsigned LS = findLastSet(RImm);
975 // We're adding (63-LS) higher-order ones, and we expect to mask them off
976 // after performing the inverse rotation by (64-r). So we need that:
977 // 63-LS == 64-r => LS == r-1
978 if (LS != r-1)
979 continue;
980
981 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
982 uint64_t RImmWithOnes = RImm | OnesMask;
983
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000984 RCount = selectI64ImmInstrCountDirect(RImmWithOnes) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000985 if (RCount < Count) {
986 Count = RCount;
987 RMin = r;
988 MatImm = RImmWithOnes;
989 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000990 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000991 }
992
Hal Finkel241ba792015-01-04 15:43:55 +0000993 if (!RMin)
Hiroshi Inoue5703fe32017-07-31 06:27:09 +0000994 return selectI64ImmDirect(CurDAG, dl, Imm);
Hal Finkel241ba792015-01-04 15:43:55 +0000995
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000996 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
997 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000998 };
999
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00001000 SDValue Val = SDValue(selectI64ImmDirect(CurDAG, dl, MatImm), 0);
Hal Finkel2f618792015-01-05 03:41:38 +00001001 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
1002 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +00001003}
1004
Nemanja Ivanovicb0783cc2017-12-12 12:09:34 +00001005static unsigned allUsesTruncate(SelectionDAG *CurDAG, SDNode *N) {
1006 unsigned MaxTruncation = 0;
1007 // Cannot use range-based for loop here as we need the actual use (i.e. we
1008 // need the operand number corresponding to the use). A range-based for
1009 // will unbox the use and provide an SDNode*.
1010 for (SDNode::use_iterator Use = N->use_begin(), UseEnd = N->use_end();
1011 Use != UseEnd; ++Use) {
1012 unsigned Opc =
1013 Use->isMachineOpcode() ? Use->getMachineOpcode() : Use->getOpcode();
1014 switch (Opc) {
1015 default: return 0;
1016 case ISD::TRUNCATE:
1017 if (Use->isMachineOpcode())
1018 return 0;
1019 MaxTruncation =
1020 std::max(MaxTruncation, Use->getValueType(0).getSizeInBits());
1021 continue;
1022 case ISD::STORE: {
1023 if (Use->isMachineOpcode())
1024 return 0;
1025 StoreSDNode *STN = cast<StoreSDNode>(*Use);
1026 unsigned MemVTSize = STN->getMemoryVT().getSizeInBits();
1027 if (MemVTSize == 64 || Use.getOperandNo() != 0)
1028 return 0;
1029 MaxTruncation = std::max(MaxTruncation, MemVTSize);
1030 continue;
1031 }
1032 case PPC::STW8:
1033 case PPC::STWX8:
1034 case PPC::STWU8:
1035 case PPC::STWUX8:
1036 if (Use.getOperandNo() != 0)
1037 return 0;
1038 MaxTruncation = std::max(MaxTruncation, 32u);
1039 continue;
1040 case PPC::STH8:
1041 case PPC::STHX8:
1042 case PPC::STHU8:
1043 case PPC::STHUX8:
1044 if (Use.getOperandNo() != 0)
1045 return 0;
1046 MaxTruncation = std::max(MaxTruncation, 16u);
1047 continue;
1048 case PPC::STB8:
1049 case PPC::STBX8:
1050 case PPC::STBU8:
1051 case PPC::STBUX8:
1052 if (Use.getOperandNo() != 0)
1053 return 0;
1054 MaxTruncation = std::max(MaxTruncation, 8u);
1055 continue;
1056 }
1057 }
1058 return MaxTruncation;
1059}
1060
Hal Finkelc58ce412015-01-01 02:53:29 +00001061// Select a 64-bit constant.
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00001062static SDNode *selectI64Imm(SelectionDAG *CurDAG, SDNode *N) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001063 SDLoc dl(N);
1064
1065 // Get 64 bit value.
1066 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nemanja Ivanovicb0783cc2017-12-12 12:09:34 +00001067 if (unsigned MinSize = allUsesTruncate(CurDAG, N)) {
1068 uint64_t SextImm = SignExtend64(Imm, MinSize);
1069 SDValue SDImm = CurDAG->getTargetConstant(SextImm, dl, MVT::i64);
1070 if (isInt<16>(SextImm))
1071 return CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, SDImm);
1072 }
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00001073 return selectI64Imm(CurDAG, dl, Imm);
Hal Finkelc58ce412015-01-01 02:53:29 +00001074}
1075
Hal Finkel8adf2252014-12-16 05:51:41 +00001076namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +00001077
Hal Finkel8adf2252014-12-16 05:51:41 +00001078class BitPermutationSelector {
1079 struct ValueBit {
1080 SDValue V;
1081
1082 // The bit number in the value, using a convention where bit 0 is the
1083 // lowest-order bit.
1084 unsigned Idx;
1085
1086 enum Kind {
1087 ConstZero,
1088 Variable
1089 } K;
1090
1091 ValueBit(SDValue V, unsigned I, Kind K = Variable)
1092 : V(V), Idx(I), K(K) {}
1093 ValueBit(Kind K = Variable)
1094 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
1095
1096 bool isZero() const {
1097 return K == ConstZero;
1098 }
1099
1100 bool hasValue() const {
1101 return K == Variable;
1102 }
1103
1104 SDValue getValue() const {
1105 assert(hasValue() && "Cannot get the value of a constant bit");
1106 return V;
1107 }
1108
1109 unsigned getValueBitIndex() const {
1110 assert(hasValue() && "Cannot get the value bit index of a constant bit");
1111 return Idx;
1112 }
1113 };
1114
1115 // A bit group has the same underlying value and the same rotate factor.
1116 struct BitGroup {
1117 SDValue V;
1118 unsigned RLAmt;
1119 unsigned StartIdx, EndIdx;
1120
Hal Finkelc58ce412015-01-01 02:53:29 +00001121 // This rotation amount assumes that the lower 32 bits of the quantity are
1122 // replicated in the high 32 bits by the rotation operator (which is done
1123 // by rlwinm and friends in 64-bit mode).
1124 bool Repl32;
1125 // Did converting to Repl32 == true change the rotation factor? If it did,
1126 // it decreased it by 32.
1127 bool Repl32CR;
1128 // Was this group coalesced after setting Repl32 to true?
1129 bool Repl32Coalesced;
1130
Hal Finkel8adf2252014-12-16 05:51:41 +00001131 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +00001132 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
1133 Repl32Coalesced(false) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001134 LLVM_DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R
1135 << " [" << S << ", " << E << "]\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001136 }
1137 };
1138
1139 // Information on each (Value, RLAmt) pair (like the number of groups
1140 // associated with each) used to choose the lowering method.
1141 struct ValueRotInfo {
1142 SDValue V;
Eugene Zelenko8187c192017-01-13 00:58:58 +00001143 unsigned RLAmt = std::numeric_limits<unsigned>::max();
1144 unsigned NumGroups = 0;
1145 unsigned FirstGroupStartIdx = std::numeric_limits<unsigned>::max();
1146 bool Repl32 = false;
Hal Finkel8adf2252014-12-16 05:51:41 +00001147
Eugene Zelenko8187c192017-01-13 00:58:58 +00001148 ValueRotInfo() = default;
Hal Finkel8adf2252014-12-16 05:51:41 +00001149
1150 // For sorting (in reverse order) by NumGroups, and then by
1151 // FirstGroupStartIdx.
1152 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +00001153 // We need to sort so that the non-Repl32 come first because, when we're
1154 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
1155 // masking operation.
1156 if (Repl32 < Other.Repl32)
1157 return true;
1158 else if (Repl32 > Other.Repl32)
1159 return false;
1160 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +00001161 return true;
1162 else if (NumGroups < Other.NumGroups)
1163 return false;
Hiroshi Inoue955655f2018-06-05 11:58:01 +00001164 else if (RLAmt == 0 && Other.RLAmt != 0)
1165 return true;
1166 else if (RLAmt != 0 && Other.RLAmt == 0)
1167 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00001168 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
1169 return true;
1170 return false;
1171 }
1172 };
1173
Tim Shendc698c32016-08-12 18:40:04 +00001174 using ValueBitsMemoizedValue = std::pair<bool, SmallVector<ValueBit, 64>>;
1175 using ValueBitsMemoizer =
1176 DenseMap<SDValue, std::unique_ptr<ValueBitsMemoizedValue>>;
1177 ValueBitsMemoizer Memoizer;
1178
1179 // Return a pair of bool and a SmallVector pointer to a memoization entry.
1180 // The bool is true if something interesting was deduced, otherwise if we're
Hal Finkel8adf2252014-12-16 05:51:41 +00001181 // providing only a generic representation of V (or something else likewise
Tim Shendc698c32016-08-12 18:40:04 +00001182 // uninteresting for instruction selection) through the SmallVector.
1183 std::pair<bool, SmallVector<ValueBit, 64> *> getValueBits(SDValue V,
1184 unsigned NumBits) {
1185 auto &ValueEntry = Memoizer[V];
1186 if (ValueEntry)
1187 return std::make_pair(ValueEntry->first, &ValueEntry->second);
1188 ValueEntry.reset(new ValueBitsMemoizedValue());
1189 bool &Interesting = ValueEntry->first;
1190 SmallVector<ValueBit, 64> &Bits = ValueEntry->second;
1191 Bits.resize(NumBits);
1192
Hal Finkel8adf2252014-12-16 05:51:41 +00001193 switch (V.getOpcode()) {
1194 default: break;
1195 case ISD::ROTL:
1196 if (isa<ConstantSDNode>(V.getOperand(1))) {
1197 unsigned RotAmt = V.getConstantOperandVal(1);
1198
Tim Shendc698c32016-08-12 18:40:04 +00001199 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001200
Tim Shendc698c32016-08-12 18:40:04 +00001201 for (unsigned i = 0; i < NumBits; ++i)
1202 Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt];
Hal Finkel8adf2252014-12-16 05:51:41 +00001203
Tim Shendc698c32016-08-12 18:40:04 +00001204 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001205 }
1206 break;
1207 case ISD::SHL:
1208 if (isa<ConstantSDNode>(V.getOperand(1))) {
1209 unsigned ShiftAmt = V.getConstantOperandVal(1);
1210
Tim Shendc698c32016-08-12 18:40:04 +00001211 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001212
Tim Shendc698c32016-08-12 18:40:04 +00001213 for (unsigned i = ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001214 Bits[i] = LHSBits[i - ShiftAmt];
1215
1216 for (unsigned i = 0; i < ShiftAmt; ++i)
1217 Bits[i] = ValueBit(ValueBit::ConstZero);
1218
Tim Shendc698c32016-08-12 18:40:04 +00001219 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001220 }
1221 break;
1222 case ISD::SRL:
1223 if (isa<ConstantSDNode>(V.getOperand(1))) {
1224 unsigned ShiftAmt = V.getConstantOperandVal(1);
1225
Tim Shendc698c32016-08-12 18:40:04 +00001226 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001227
Tim Shendc698c32016-08-12 18:40:04 +00001228 for (unsigned i = 0; i < NumBits - ShiftAmt; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001229 Bits[i] = LHSBits[i + ShiftAmt];
1230
Tim Shendc698c32016-08-12 18:40:04 +00001231 for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001232 Bits[i] = ValueBit(ValueBit::ConstZero);
1233
Tim Shendc698c32016-08-12 18:40:04 +00001234 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001235 }
1236 break;
1237 case ISD::AND:
1238 if (isa<ConstantSDNode>(V.getOperand(1))) {
1239 uint64_t Mask = V.getConstantOperandVal(1);
1240
Tim Shendc698c32016-08-12 18:40:04 +00001241 const SmallVector<ValueBit, 64> *LHSBits;
Hal Finkel8adf2252014-12-16 05:51:41 +00001242 // Mark this as interesting, only if the LHS was also interesting. This
1243 // prevents the overall procedure from matching a single immediate 'and'
1244 // (which is non-optimal because such an and might be folded with other
1245 // things if we don't select it here).
Tim Shendc698c32016-08-12 18:40:04 +00001246 std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), NumBits);
1247
1248 for (unsigned i = 0; i < NumBits; ++i)
1249 if (((Mask >> i) & 1) == 1)
1250 Bits[i] = (*LHSBits)[i];
1251 else
1252 Bits[i] = ValueBit(ValueBit::ConstZero);
1253
1254 return std::make_pair(Interesting, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001255 }
1256 break;
1257 case ISD::OR: {
Tim Shendc698c32016-08-12 18:40:04 +00001258 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
1259 const auto &RHSBits = *getValueBits(V.getOperand(1), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001260
1261 bool AllDisjoint = true;
Tim Shendc698c32016-08-12 18:40:04 +00001262 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001263 if (LHSBits[i].isZero())
1264 Bits[i] = RHSBits[i];
1265 else if (RHSBits[i].isZero())
1266 Bits[i] = LHSBits[i];
1267 else {
1268 AllDisjoint = false;
1269 break;
1270 }
1271
1272 if (!AllDisjoint)
1273 break;
1274
Tim Shendc698c32016-08-12 18:40:04 +00001275 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001276 }
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001277 case ISD::ZERO_EXTEND: {
1278 // We support only the case with zero extension from i32 to i64 so far.
1279 if (V.getValueType() != MVT::i64 ||
1280 V.getOperand(0).getValueType() != MVT::i32)
1281 break;
1282
1283 const SmallVector<ValueBit, 64> *LHSBits;
1284 const unsigned NumOperandBits = 32;
1285 std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0),
1286 NumOperandBits);
1287
1288 for (unsigned i = 0; i < NumOperandBits; ++i)
1289 Bits[i] = (*LHSBits)[i];
1290
1291 for (unsigned i = NumOperandBits; i < NumBits; ++i)
1292 Bits[i] = ValueBit(ValueBit::ConstZero);
1293
1294 return std::make_pair(Interesting, &Bits);
Hiroshi Inoue863fb7a2018-06-08 04:00:54 +00001295 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001296 }
1297
Tim Shendc698c32016-08-12 18:40:04 +00001298 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001299 Bits[i] = ValueBit(V, i);
1300
Tim Shendc698c32016-08-12 18:40:04 +00001301 return std::make_pair(Interesting = false, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001302 }
1303
1304 // For each value (except the constant ones), compute the left-rotate amount
1305 // to get it from its original to final position.
1306 void computeRotationAmounts() {
1307 HasZeros = false;
1308 RLAmt.resize(Bits.size());
1309 for (unsigned i = 0; i < Bits.size(); ++i)
1310 if (Bits[i].hasValue()) {
1311 unsigned VBI = Bits[i].getValueBitIndex();
1312 if (i >= VBI)
1313 RLAmt[i] = i - VBI;
1314 else
1315 RLAmt[i] = Bits.size() - (VBI - i);
1316 } else if (Bits[i].isZero()) {
1317 HasZeros = true;
1318 RLAmt[i] = UINT32_MAX;
1319 } else {
1320 llvm_unreachable("Unknown value bit type");
1321 }
1322 }
1323
1324 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +00001325 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
1326 // they break up groups.
1327 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001328 BitGroups.clear();
1329
1330 unsigned LastRLAmt = RLAmt[0];
1331 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1332 unsigned LastGroupStartIdx = 0;
1333 for (unsigned i = 1; i < Bits.size(); ++i) {
1334 unsigned ThisRLAmt = RLAmt[i];
1335 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001336 if (LateMask && !ThisValue) {
1337 ThisValue = LastValue;
1338 ThisRLAmt = LastRLAmt;
1339 // If we're doing late masking, then the first bit group always starts
1340 // at zero (even if the first bits were zero).
1341 if (BitGroups.empty())
1342 LastGroupStartIdx = 0;
1343 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001344
1345 // If this bit has the same underlying value and the same rotate factor as
1346 // the last one, then they're part of the same group.
1347 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1348 continue;
1349
1350 if (LastValue.getNode())
1351 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1352 i-1));
1353 LastRLAmt = ThisRLAmt;
1354 LastValue = ThisValue;
1355 LastGroupStartIdx = i;
1356 }
1357 if (LastValue.getNode())
1358 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1359 Bits.size()-1));
1360
1361 if (BitGroups.empty())
1362 return;
1363
1364 // We might be able to combine the first and last groups.
1365 if (BitGroups.size() > 1) {
1366 // If the first and last groups are the same, then remove the first group
1367 // in favor of the last group, making the ending index of the last group
1368 // equal to the ending index of the to-be-removed first group.
1369 if (BitGroups[0].StartIdx == 0 &&
1370 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1371 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1372 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001373 LLVM_DEBUG(dbgs() << "\tcombining final bit group with initial one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001374 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1375 BitGroups.erase(BitGroups.begin());
1376 }
1377 }
1378 }
1379
1380 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
Hiroshi Inoue955655f2018-06-05 11:58:01 +00001381 // associated with each. If the number of groups are same, we prefer a group
1382 // which does not require rotate, i.e. RLAmt is 0, to avoid the first rotate
1383 // instruction. If there is a degeneracy, pick the one that occurs
Hal Finkel8adf2252014-12-16 05:51:41 +00001384 // first (in the final value).
1385 void collectValueRotInfo() {
1386 ValueRots.clear();
1387
1388 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001389 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1390 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001391 VRI.V = BG.V;
1392 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001393 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001394 VRI.NumGroups += 1;
1395 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1396 }
1397
1398 // Now that we've collected the various ValueRotInfo instances, we need to
1399 // sort them.
1400 ValueRotsVec.clear();
1401 for (auto &I : ValueRots) {
1402 ValueRotsVec.push_back(I.second);
1403 }
Mandeep Singh Grang327fd5e2018-04-08 16:45:04 +00001404 llvm::sort(ValueRotsVec.begin(), ValueRotsVec.end());
Hal Finkel8adf2252014-12-16 05:51:41 +00001405 }
1406
Hal Finkelc58ce412015-01-01 02:53:29 +00001407 // In 64-bit mode, rlwinm and friends have a rotation operator that
1408 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1409 // indices of these instructions can only be in the lower 32 bits, so they
1410 // can only represent some 64-bit bit groups. However, when they can be used,
1411 // the 32-bit replication can be used to represent, as a single bit group,
1412 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1413 // groups when possible. Returns true if any of the bit groups were
1414 // converted.
1415 void assignRepl32BitGroups() {
1416 // If we have bits like this:
1417 //
1418 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1419 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1420 // Groups: | RLAmt = 8 | RLAmt = 40 |
1421 //
1422 // But, making use of a 32-bit operation that replicates the low-order 32
1423 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1424 // of 8.
1425
1426 auto IsAllLow32 = [this](BitGroup & BG) {
1427 if (BG.StartIdx <= BG.EndIdx) {
1428 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1429 if (!Bits[i].hasValue())
1430 continue;
1431 if (Bits[i].getValueBitIndex() >= 32)
1432 return false;
1433 }
1434 } else {
1435 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1436 if (!Bits[i].hasValue())
1437 continue;
1438 if (Bits[i].getValueBitIndex() >= 32)
1439 return false;
1440 }
1441 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1442 if (!Bits[i].hasValue())
1443 continue;
1444 if (Bits[i].getValueBitIndex() >= 32)
1445 return false;
1446 }
1447 }
1448
1449 return true;
1450 };
1451
1452 for (auto &BG : BitGroups) {
Hiroshi Inoue01ef4c22018-06-07 13:21:14 +00001453 // If this bit group has RLAmt of 0 and will not be merged with
1454 // another bit group, we don't benefit from Repl32. We don't mark
1455 // such group to give more freedom for later instruction selection.
1456 if (BG.RLAmt == 0) {
1457 auto PotentiallyMerged = [this](BitGroup & BG) {
1458 for (auto &BG2 : BitGroups)
1459 if (&BG != &BG2 && BG.V == BG2.V &&
1460 (BG2.RLAmt == 0 || BG2.RLAmt == 32))
1461 return true;
1462 return false;
1463 };
1464 if (!PotentiallyMerged(BG))
1465 continue;
1466 }
Hal Finkelc58ce412015-01-01 02:53:29 +00001467 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1468 if (IsAllLow32(BG)) {
1469 if (BG.RLAmt >= 32) {
1470 BG.RLAmt -= 32;
1471 BG.Repl32CR = true;
1472 }
1473
1474 BG.Repl32 = true;
1475
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001476 LLVM_DEBUG(dbgs() << "\t32-bit replicated bit group for "
1477 << BG.V.getNode() << " RLAmt = " << BG.RLAmt << " ["
1478 << BG.StartIdx << ", " << BG.EndIdx << "]\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001479 }
1480 }
1481 }
1482
1483 // Now walk through the bit groups, consolidating where possible.
1484 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1485 // We might want to remove this bit group by merging it with the previous
1486 // group (which might be the ending group).
1487 auto IP = (I == BitGroups.begin()) ?
1488 std::prev(BitGroups.end()) : std::prev(I);
1489 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1490 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1491
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001492 LLVM_DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for "
1493 << I->V.getNode() << " RLAmt = " << I->RLAmt << " ["
1494 << I->StartIdx << ", " << I->EndIdx
1495 << "] with group with range [" << IP->StartIdx << ", "
1496 << IP->EndIdx << "]\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001497
1498 IP->EndIdx = I->EndIdx;
1499 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1500 IP->Repl32Coalesced = true;
1501 I = BitGroups.erase(I);
1502 continue;
1503 } else {
1504 // There is a special case worth handling: If there is a single group
1505 // covering the entire upper 32 bits, and it can be merged with both
1506 // the next and previous groups (which might be the same group), then
1507 // do so. If it is the same group (so there will be only one group in
1508 // total), then we need to reverse the order of the range so that it
1509 // covers the entire 64 bits.
1510 if (I->StartIdx == 32 && I->EndIdx == 63) {
1511 assert(std::next(I) == BitGroups.end() &&
1512 "bit group ends at index 63 but there is another?");
1513 auto IN = BitGroups.begin();
1514
Justin Bognerb0126992016-05-05 23:19:08 +00001515 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
Hal Finkelc58ce412015-01-01 02:53:29 +00001516 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1517 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1518 IsAllLow32(*I)) {
1519
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001520 LLVM_DEBUG(dbgs() << "\tcombining bit group for " << I->V.getNode()
1521 << " RLAmt = " << I->RLAmt << " [" << I->StartIdx
1522 << ", " << I->EndIdx
1523 << "] with 32-bit replicated groups with ranges ["
1524 << IP->StartIdx << ", " << IP->EndIdx << "] and ["
1525 << IN->StartIdx << ", " << IN->EndIdx << "]\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001526
1527 if (IP == IN) {
1528 // There is only one other group; change it to cover the whole
1529 // range (backward, so that it can still be Repl32 but cover the
1530 // whole 64-bit range).
1531 IP->StartIdx = 31;
1532 IP->EndIdx = 30;
1533 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1534 IP->Repl32Coalesced = true;
1535 I = BitGroups.erase(I);
1536 } else {
1537 // There are two separate groups, one before this group and one
1538 // after us (at the beginning). We're going to remove this group,
1539 // but also the group at the very beginning.
1540 IP->EndIdx = IN->EndIdx;
1541 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1542 IP->Repl32Coalesced = true;
1543 I = BitGroups.erase(I);
1544 BitGroups.erase(BitGroups.begin());
1545 }
1546
1547 // This must be the last group in the vector (and we might have
1548 // just invalidated the iterator above), so break here.
1549 break;
1550 }
1551 }
1552 }
1553
1554 ++I;
1555 }
1556 }
1557
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001558 SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001559 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001560 }
1561
Hal Finkelc58ce412015-01-01 02:53:29 +00001562 uint64_t getZerosMask() {
1563 uint64_t Mask = 0;
1564 for (unsigned i = 0; i < Bits.size(); ++i) {
1565 if (Bits[i].hasValue())
1566 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001567 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001568 }
1569
1570 return ~Mask;
1571 }
1572
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001573 // This method extends an input value to 64 bit if input is 32-bit integer.
1574 // While selecting instructions in BitPermutationSelector in 64-bit mode,
1575 // an input value can be a 32-bit integer if a ZERO_EXTEND node is included.
1576 // In such case, we extend it to 64 bit to be consistent with other values.
1577 SDValue ExtendToInt64(SDValue V, const SDLoc &dl) {
1578 if (V.getValueSizeInBits() == 64)
1579 return V;
1580
1581 assert(V.getValueSizeInBits() == 32);
1582 SDValue SubRegIdx = CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
1583 SDValue ImDef = SDValue(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl,
1584 MVT::i64), 0);
1585 SDValue ExtVal = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl,
1586 MVT::i64, ImDef, V,
1587 SubRegIdx), 0);
1588 return ExtVal;
1589 }
1590
Hal Finkel8adf2252014-12-16 05:51:41 +00001591 // Depending on the number of groups for a particular value, it might be
1592 // better to rotate, mask explicitly (using andi/andis), and then or the
1593 // result. Select this part of the result first.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001594 void SelectAndParts32(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001595 if (BPermRewriterNoMasking)
1596 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001597
1598 for (ValueRotInfo &VRI : ValueRotsVec) {
1599 unsigned Mask = 0;
1600 for (unsigned i = 0; i < Bits.size(); ++i) {
1601 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1602 continue;
1603 if (RLAmt[i] != VRI.RLAmt)
1604 continue;
1605 Mask |= (1u << i);
1606 }
1607
1608 // Compute the masks for andi/andis that would be necessary.
1609 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1610 assert((ANDIMask != 0 || ANDISMask != 0) &&
1611 "No set bits in mask for value bit groups");
1612 bool NeedsRotate = VRI.RLAmt != 0;
1613
1614 // We're trying to minimize the number of instructions. If we have one
1615 // group, using one of andi/andis can break even. If we have three
1616 // groups, we can use both andi and andis and break even (to use both
1617 // andi and andis we also need to or the results together). We need four
1618 // groups if we also need to rotate. To use andi/andis we need to do more
1619 // than break even because rotate-and-mask instructions tend to be easier
1620 // to schedule.
1621
1622 // FIXME: We've biased here against using andi/andis, which is right for
1623 // POWER cores, but not optimal everywhere. For example, on the A2,
1624 // andi/andis have single-cycle latency whereas the rotate-and-mask
1625 // instructions take two cycles, and it would be better to bias toward
1626 // andi/andis in break-even cases.
1627
1628 unsigned NumAndInsts = (unsigned) NeedsRotate +
1629 (unsigned) (ANDIMask != 0) +
1630 (unsigned) (ANDISMask != 0) +
1631 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1632 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001633
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001634 LLVM_DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode()
1635 << " RL: " << VRI.RLAmt << ":"
1636 << "\n\t\t\tisel using masking: " << NumAndInsts
1637 << " using rotates: " << VRI.NumGroups << "\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001638
Hal Finkel8adf2252014-12-16 05:51:41 +00001639 if (NumAndInsts >= VRI.NumGroups)
1640 continue;
1641
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001642 LLVM_DEBUG(dbgs() << "\t\t\t\tusing masking\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001643
1644 if (InstCnt) *InstCnt += NumAndInsts;
1645
Hal Finkel8adf2252014-12-16 05:51:41 +00001646 SDValue VRot;
1647 if (VRI.RLAmt) {
1648 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001649 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1650 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001651 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1652 Ops), 0);
1653 } else {
1654 VRot = VRI.V;
1655 }
1656
1657 SDValue ANDIVal, ANDISVal;
1658 if (ANDIMask != 0)
1659 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001660 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001661 if (ANDISMask != 0)
1662 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001663 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001664
1665 SDValue TotalVal;
1666 if (!ANDIVal)
1667 TotalVal = ANDISVal;
1668 else if (!ANDISVal)
1669 TotalVal = ANDIVal;
1670 else
1671 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1672 ANDIVal, ANDISVal), 0);
1673
1674 if (!Res)
1675 Res = TotalVal;
1676 else
1677 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1678 Res, TotalVal), 0);
1679
1680 // Now, remove all groups with this underlying value and rotation
1681 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001682 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1683 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1684 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001685 }
1686 }
1687
1688 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001689 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001690 SDLoc dl(N);
1691 SDValue Res;
1692
Hal Finkelc58ce412015-01-01 02:53:29 +00001693 if (InstCnt) *InstCnt = 0;
1694
Hal Finkel8adf2252014-12-16 05:51:41 +00001695 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001696 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001697
1698 // If we've not yet selected a 'starting' instruction, and we have no zeros
1699 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1700 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001701 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001702 ValueRotInfo &VRI = ValueRotsVec[0];
1703 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001704 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001705 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001706 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1707 getI32Imm(31, dl) };
1708 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1709 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001710 } else {
1711 Res = VRI.V;
1712 }
1713
1714 // Now, remove all groups with this underlying value and rotation factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001715 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1716 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1717 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001718 }
1719
Hal Finkelc58ce412015-01-01 02:53:29 +00001720 if (InstCnt) *InstCnt += BitGroups.size();
1721
Hal Finkel8adf2252014-12-16 05:51:41 +00001722 // Insert the other groups (one at a time).
1723 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001724 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001725 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001726 { BG.V, getI32Imm(BG.RLAmt, dl),
1727 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1728 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001729 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1730 } else {
1731 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001732 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1733 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1734 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001735 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1736 }
1737 }
1738
Hal Finkelc58ce412015-01-01 02:53:29 +00001739 if (LateMask) {
1740 unsigned Mask = (unsigned) getZerosMask();
1741
1742 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1743 assert((ANDIMask != 0 || ANDISMask != 0) &&
1744 "No set bits in zeros mask?");
1745
1746 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1747 (unsigned) (ANDISMask != 0) +
1748 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1749
1750 SDValue ANDIVal, ANDISVal;
1751 if (ANDIMask != 0)
1752 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001753 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001754 if (ANDISMask != 0)
1755 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001756 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001757
1758 if (!ANDIVal)
1759 Res = ANDISVal;
1760 else if (!ANDISVal)
1761 Res = ANDIVal;
1762 else
1763 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1764 ANDIVal, ANDISVal), 0);
1765 }
1766
Hal Finkel8adf2252014-12-16 05:51:41 +00001767 return Res.getNode();
1768 }
1769
Hal Finkelc58ce412015-01-01 02:53:29 +00001770 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1771 unsigned MaskStart, unsigned MaskEnd,
1772 bool IsIns) {
1773 // In the notation used by the instructions, 'start' and 'end' are reversed
1774 // because bits are counted from high to low order.
1775 unsigned InstMaskStart = 64 - MaskEnd - 1,
1776 InstMaskEnd = 64 - MaskStart - 1;
1777
1778 if (Repl32)
1779 return 1;
1780
1781 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1782 InstMaskEnd == 63 - RLAmt)
1783 return 1;
1784
1785 return 2;
1786 }
1787
1788 // For 64-bit values, not all combinations of rotates and masks are
1789 // available. Produce one if it is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001790 SDValue SelectRotMask64(SDValue V, const SDLoc &dl, unsigned RLAmt,
1791 bool Repl32, unsigned MaskStart, unsigned MaskEnd,
Hal Finkelc58ce412015-01-01 02:53:29 +00001792 unsigned *InstCnt = nullptr) {
1793 // In the notation used by the instructions, 'start' and 'end' are reversed
1794 // because bits are counted from high to low order.
1795 unsigned InstMaskStart = 64 - MaskEnd - 1,
1796 InstMaskEnd = 64 - MaskStart - 1;
1797
1798 if (InstCnt) *InstCnt += 1;
1799
1800 if (Repl32) {
1801 // This rotation amount assumes that the lower 32 bits of the quantity
1802 // are replicated in the high 32 bits by the rotation operator (which is
1803 // done by rlwinm and friends).
1804 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1805 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1806 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001807 { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1808 getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001809 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1810 Ops), 0);
1811 }
1812
1813 if (InstMaskEnd == 63) {
1814 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001815 { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1816 getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001817 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1818 }
1819
1820 if (InstMaskStart == 0) {
1821 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001822 { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1823 getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001824 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1825 }
1826
1827 if (InstMaskEnd == 63 - RLAmt) {
1828 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001829 { ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1830 getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001831 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1832 }
1833
1834 // We cannot do this with a single instruction, so we'll use two. The
1835 // problem is that we're not free to choose both a rotation amount and mask
1836 // start and end independently. We can choose an arbitrary mask start and
1837 // end, but then the rotation amount is fixed. Rotation, however, can be
1838 // inverted, and so by applying an "inverse" rotation first, we can get the
1839 // desired result.
1840 if (InstCnt) *InstCnt += 1;
1841
1842 // The rotation mask for the second instruction must be MaskStart.
1843 unsigned RLAmt2 = MaskStart;
1844 // The first instruction must rotate V so that the overall rotation amount
1845 // is RLAmt.
1846 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1847 if (RLAmt1)
1848 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1849 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1850 }
1851
1852 // For 64-bit values, not all combinations of rotates and masks are
1853 // available. Produce a rotate-mask-and-insert if one is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001854 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, const SDLoc &dl,
1855 unsigned RLAmt, bool Repl32, unsigned MaskStart,
Hal Finkelc58ce412015-01-01 02:53:29 +00001856 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1857 // In the notation used by the instructions, 'start' and 'end' are reversed
1858 // because bits are counted from high to low order.
1859 unsigned InstMaskStart = 64 - MaskEnd - 1,
1860 InstMaskEnd = 64 - MaskStart - 1;
1861
1862 if (InstCnt) *InstCnt += 1;
1863
1864 if (Repl32) {
1865 // This rotation amount assumes that the lower 32 bits of the quantity
1866 // are replicated in the high 32 bits by the rotation operator (which is
1867 // done by rlwinm and friends).
1868 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1869 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1870 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001871 { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1872 getI32Imm(InstMaskStart - 32, dl), getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001873 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1874 Ops), 0);
1875 }
1876
1877 if (InstMaskEnd == 63 - RLAmt) {
1878 SDValue Ops[] =
Hiroshi Inouedcedd662017-10-02 09:24:00 +00001879 { ExtendToInt64(Base, dl), ExtendToInt64(V, dl), getI32Imm(RLAmt, dl),
1880 getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001881 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1882 }
1883
1884 // We cannot do this with a single instruction, so we'll use two. The
1885 // problem is that we're not free to choose both a rotation amount and mask
1886 // start and end independently. We can choose an arbitrary mask start and
1887 // end, but then the rotation amount is fixed. Rotation, however, can be
1888 // inverted, and so by applying an "inverse" rotation first, we can get the
1889 // desired result.
1890 if (InstCnt) *InstCnt += 1;
1891
1892 // The rotation mask for the second instruction must be MaskStart.
1893 unsigned RLAmt2 = MaskStart;
1894 // The first instruction must rotate V so that the overall rotation amount
1895 // is RLAmt.
1896 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1897 if (RLAmt1)
1898 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1899 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1900 }
1901
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001902 void SelectAndParts64(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001903 if (BPermRewriterNoMasking)
1904 return;
1905
1906 // The idea here is the same as in the 32-bit version, but with additional
1907 // complications from the fact that Repl32 might be true. Because we
1908 // aggressively convert bit groups to Repl32 form (which, for small
1909 // rotation factors, involves no other change), and then coalesce, it might
1910 // be the case that a single 64-bit masking operation could handle both
1911 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1912 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1913 // completely capture the new combined bit group.
1914
1915 for (ValueRotInfo &VRI : ValueRotsVec) {
1916 uint64_t Mask = 0;
1917
1918 // We need to add to the mask all bits from the associated bit groups.
1919 // If Repl32 is false, we need to add bits from bit groups that have
1920 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1921 // group is trivially convertable if it overlaps only with the lower 32
1922 // bits, and the group has not been coalesced.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001923 auto MatchingBG = [VRI](const BitGroup &BG) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001924 if (VRI.V != BG.V)
1925 return false;
1926
1927 unsigned EffRLAmt = BG.RLAmt;
1928 if (!VRI.Repl32 && BG.Repl32) {
1929 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1930 !BG.Repl32Coalesced) {
1931 if (BG.Repl32CR)
1932 EffRLAmt += 32;
1933 } else {
1934 return false;
1935 }
1936 } else if (VRI.Repl32 != BG.Repl32) {
1937 return false;
1938 }
1939
Alexander Kornienko175a7cb2015-12-28 13:38:42 +00001940 return VRI.RLAmt == EffRLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001941 };
1942
1943 for (auto &BG : BitGroups) {
1944 if (!MatchingBG(BG))
1945 continue;
1946
1947 if (BG.StartIdx <= BG.EndIdx) {
1948 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001949 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001950 } else {
1951 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001952 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001953 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001954 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001955 }
1956 }
1957
1958 // We can use the 32-bit andi/andis technique if the mask does not
1959 // require any higher-order bits. This can save an instruction compared
1960 // to always using the general 64-bit technique.
1961 bool Use32BitInsts = isUInt<32>(Mask);
1962 // Compute the masks for andi/andis that would be necessary.
1963 unsigned ANDIMask = (Mask & UINT16_MAX),
1964 ANDISMask = (Mask >> 16) & UINT16_MAX;
1965
1966 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1967
1968 unsigned NumAndInsts = (unsigned) NeedsRotate +
1969 (unsigned) (bool) Res;
1970 if (Use32BitInsts)
1971 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1972 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1973 else
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00001974 NumAndInsts += selectI64ImmInstrCount(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001975
1976 unsigned NumRLInsts = 0;
1977 bool FirstBG = true;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001978 bool MoreBG = false;
Hal Finkelc58ce412015-01-01 02:53:29 +00001979 for (auto &BG : BitGroups) {
Guozhi Wei0cd65422016-10-14 20:41:50 +00001980 if (!MatchingBG(BG)) {
1981 MoreBG = true;
Hal Finkelc58ce412015-01-01 02:53:29 +00001982 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001983 }
Hal Finkelc58ce412015-01-01 02:53:29 +00001984 NumRLInsts +=
1985 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1986 !FirstBG);
1987 FirstBG = false;
1988 }
1989
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001990 LLVM_DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode()
1991 << " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":")
1992 << "\n\t\t\tisel using masking: " << NumAndInsts
1993 << " using rotates: " << NumRLInsts << "\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00001994
1995 // When we'd use andi/andis, we bias toward using the rotates (andi only
1996 // has a record form, and is cracked on POWER cores). However, when using
1997 // general 64-bit constant formation, bias toward the constant form,
1998 // because that exposes more opportunities for CSE.
1999 if (NumAndInsts > NumRLInsts)
2000 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00002001 // When merging multiple bit groups, instruction or is used.
2002 // But when rotate is used, rldimi can inert the rotated value into any
2003 // register, so instruction or can be avoided.
2004 if ((Use32BitInsts || MoreBG) && NumAndInsts == NumRLInsts)
Hal Finkelc58ce412015-01-01 02:53:29 +00002005 continue;
2006
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002007 LLVM_DEBUG(dbgs() << "\t\t\t\tusing masking\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002008
2009 if (InstCnt) *InstCnt += NumAndInsts;
2010
2011 SDValue VRot;
2012 // We actually need to generate a rotation if we have a non-zero rotation
2013 // factor or, in the Repl32 case, if we care about any of the
2014 // higher-order replicated bits. In the latter case, we generate a mask
2015 // backward so that it actually includes the entire 64 bits.
2016 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
2017 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
2018 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
2019 else
2020 VRot = VRI.V;
2021
2022 SDValue TotalVal;
2023 if (Use32BitInsts) {
2024 assert((ANDIMask != 0 || ANDISMask != 0) &&
2025 "No set bits in mask when using 32-bit ands for 64-bit value");
2026
2027 SDValue ANDIVal, ANDISVal;
2028 if (ANDIMask != 0)
2029 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002030 ExtendToInt64(VRot, dl),
2031 getI32Imm(ANDIMask, dl)),
2032 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002033 if (ANDISMask != 0)
2034 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002035 ExtendToInt64(VRot, dl),
2036 getI32Imm(ANDISMask, dl)),
2037 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002038
2039 if (!ANDIVal)
2040 TotalVal = ANDISVal;
2041 else if (!ANDISVal)
2042 TotalVal = ANDIVal;
2043 else
2044 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002045 ExtendToInt64(ANDIVal, dl), ANDISVal), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002046 } else {
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00002047 TotalVal = SDValue(selectI64Imm(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002048 TotalVal =
2049 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002050 ExtendToInt64(VRot, dl), TotalVal),
2051 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002052 }
2053
2054 if (!Res)
2055 Res = TotalVal;
2056 else
2057 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002058 ExtendToInt64(Res, dl), TotalVal),
2059 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002060
2061 // Now, remove all groups with this underlying value and rotation
2062 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00002063 eraseMatchingBitGroups(MatchingBG);
Hal Finkelc58ce412015-01-01 02:53:29 +00002064 }
2065 }
2066
2067 // Instruction selection for the 64-bit case.
2068 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
2069 SDLoc dl(N);
2070 SDValue Res;
2071
2072 if (InstCnt) *InstCnt = 0;
2073
2074 // Take care of cases that should use andi/andis first.
2075 SelectAndParts64(dl, Res, InstCnt);
2076
2077 // If we've not yet selected a 'starting' instruction, and we have no zeros
2078 // to fill in, select the (Value, RLAmt) with the highest priority (largest
2079 // number of groups), and start with this rotated value.
2080 if ((!HasZeros || LateMask) && !Res) {
2081 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
2082 // groups will come first, and so the VRI representing the largest number
2083 // of groups might not be first (it might be the first Repl32 groups).
2084 unsigned MaxGroupsIdx = 0;
2085 if (!ValueRotsVec[0].Repl32) {
2086 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
2087 if (ValueRotsVec[i].Repl32) {
2088 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
2089 MaxGroupsIdx = i;
2090 break;
2091 }
2092 }
2093
2094 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
2095 bool NeedsRotate = false;
2096 if (VRI.RLAmt) {
2097 NeedsRotate = true;
2098 } else if (VRI.Repl32) {
2099 for (auto &BG : BitGroups) {
2100 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
2101 BG.Repl32 != VRI.Repl32)
2102 continue;
2103
2104 // We don't need a rotate if the bit group is confined to the lower
2105 // 32 bits.
2106 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
2107 continue;
2108
2109 NeedsRotate = true;
2110 break;
2111 }
2112 }
2113
2114 if (NeedsRotate)
2115 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
2116 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
2117 InstCnt);
2118 else
2119 Res = VRI.V;
2120
2121 // Now, remove all groups with this underlying value and rotation factor.
2122 if (Res)
Benjamin Kramere7561b82015-06-20 15:59:41 +00002123 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
2124 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt &&
2125 BG.Repl32 == VRI.Repl32;
2126 });
Hal Finkelc58ce412015-01-01 02:53:29 +00002127 }
2128
2129 // Because 64-bit rotates are more flexible than inserts, we might have a
2130 // preference regarding which one we do first (to save one instruction).
2131 if (!Res)
2132 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
2133 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
2134 false) <
2135 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
2136 true)) {
2137 if (I != BitGroups.begin()) {
2138 BitGroup BG = *I;
2139 BitGroups.erase(I);
2140 BitGroups.insert(BitGroups.begin(), BG);
2141 }
2142
2143 break;
2144 }
2145 }
2146
2147 // Insert the other groups (one at a time).
2148 for (auto &BG : BitGroups) {
2149 if (!Res)
2150 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
2151 BG.EndIdx, InstCnt);
2152 else
2153 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
2154 BG.StartIdx, BG.EndIdx, InstCnt);
2155 }
2156
2157 if (LateMask) {
2158 uint64_t Mask = getZerosMask();
2159
2160 // We can use the 32-bit andi/andis technique if the mask does not
2161 // require any higher-order bits. This can save an instruction compared
2162 // to always using the general 64-bit technique.
2163 bool Use32BitInsts = isUInt<32>(Mask);
2164 // Compute the masks for andi/andis that would be necessary.
2165 unsigned ANDIMask = (Mask & UINT16_MAX),
2166 ANDISMask = (Mask >> 16) & UINT16_MAX;
2167
2168 if (Use32BitInsts) {
2169 assert((ANDIMask != 0 || ANDISMask != 0) &&
2170 "No set bits in mask when using 32-bit ands for 64-bit value");
2171
2172 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
2173 (unsigned) (ANDISMask != 0) +
2174 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
2175
2176 SDValue ANDIVal, ANDISVal;
2177 if (ANDIMask != 0)
2178 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002179 ExtendToInt64(Res, dl), getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002180 if (ANDISMask != 0)
2181 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002182 ExtendToInt64(Res, dl), getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002183
2184 if (!ANDIVal)
2185 Res = ANDISVal;
2186 else if (!ANDISVal)
2187 Res = ANDIVal;
2188 else
2189 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002190 ExtendToInt64(ANDIVal, dl), ANDISVal), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002191 } else {
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00002192 if (InstCnt) *InstCnt += selectI64ImmInstrCount(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00002193
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00002194 SDValue MaskVal = SDValue(selectI64Imm(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002195 Res =
2196 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
Hiroshi Inouedcedd662017-10-02 09:24:00 +00002197 ExtendToInt64(Res, dl), MaskVal), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00002198 }
2199 }
2200
2201 return Res.getNode();
2202 }
2203
2204 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
2205 // Fill in BitGroups.
2206 collectBitGroups(LateMask);
2207 if (BitGroups.empty())
2208 return nullptr;
2209
2210 // For 64-bit values, figure out when we can use 32-bit instructions.
2211 if (Bits.size() == 64)
2212 assignRepl32BitGroups();
2213
2214 // Fill in ValueRotsVec.
2215 collectValueRotInfo();
2216
2217 if (Bits.size() == 32) {
2218 return Select32(N, LateMask, InstCnt);
2219 } else {
2220 assert(Bits.size() == 64 && "Not 64 bits here?");
2221 return Select64(N, LateMask, InstCnt);
2222 }
2223
2224 return nullptr;
2225 }
2226
Benjamin Kramere7561b82015-06-20 15:59:41 +00002227 void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) {
David Majnemerc7004902016-08-12 04:32:37 +00002228 BitGroups.erase(remove_if(BitGroups, F), BitGroups.end());
Benjamin Kramere7561b82015-06-20 15:59:41 +00002229 }
2230
Hal Finkel8adf2252014-12-16 05:51:41 +00002231 SmallVector<ValueBit, 64> Bits;
2232
2233 bool HasZeros;
2234 SmallVector<unsigned, 64> RLAmt;
2235
2236 SmallVector<BitGroup, 16> BitGroups;
2237
2238 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
2239 SmallVector<ValueRotInfo, 16> ValueRotsVec;
2240
2241 SelectionDAG *CurDAG;
2242
2243public:
2244 BitPermutationSelector(SelectionDAG *DAG)
2245 : CurDAG(DAG) {}
2246
2247 // Here we try to match complex bit permutations into a set of
2248 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
2249 // known to produce optimial code for common cases (like i32 byte swapping).
2250 SDNode *Select(SDNode *N) {
Tim Shendc698c32016-08-12 18:40:04 +00002251 Memoizer.clear();
2252 auto Result =
2253 getValueBits(SDValue(N, 0), N->getValueType(0).getSizeInBits());
2254 if (!Result.first)
Hal Finkel8adf2252014-12-16 05:51:41 +00002255 return nullptr;
Tim Shendc698c32016-08-12 18:40:04 +00002256 Bits = std::move(*Result.second);
Hal Finkel8adf2252014-12-16 05:51:41 +00002257
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002258 LLVM_DEBUG(dbgs() << "Considering bit-permutation-based instruction"
2259 " selection for: ");
2260 LLVM_DEBUG(N->dump(CurDAG));
Hal Finkel8adf2252014-12-16 05:51:41 +00002261
2262 // Fill it RLAmt and set HasZeros.
2263 computeRotationAmounts();
2264
Hal Finkelc58ce412015-01-01 02:53:29 +00002265 if (!HasZeros)
2266 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00002267
Hal Finkelc58ce412015-01-01 02:53:29 +00002268 // We currently have two techniques for handling results with zeros: early
2269 // masking (the default) and late masking. Late masking is sometimes more
2270 // efficient, but because the structure of the bit groups is different, it
2271 // is hard to tell without generating both and comparing the results. With
2272 // late masking, we ignore zeros in the resulting value when inserting each
2273 // set of bit groups, and then mask in the zeros at the end. With early
2274 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00002275
Hiroshi Inoue9796b472018-06-01 14:23:15 +00002276 unsigned InstCnt = 0, InstCntLateMask = 0;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002277 LLVM_DEBUG(dbgs() << "\tEarly masking:\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002278 SDNode *RN = Select(N, false, &InstCnt);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002279 LLVM_DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002280
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002281 LLVM_DEBUG(dbgs() << "\tLate masking:\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002282 SDNode *RNLM = Select(N, true, &InstCntLateMask);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002283 LLVM_DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask
2284 << " instructions\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002285
2286 if (InstCnt <= InstCntLateMask) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002287 LLVM_DEBUG(dbgs() << "\tUsing early-masking for isel\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002288 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00002289 }
2290
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002291 LLVM_DEBUG(dbgs() << "\tUsing late-masking for isel\n");
Hal Finkelc58ce412015-01-01 02:53:29 +00002292 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00002293 }
2294};
Eugene Zelenko8187c192017-01-13 00:58:58 +00002295
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +00002296class IntegerCompareEliminator {
2297 SelectionDAG *CurDAG;
2298 PPCDAGToDAGISel *S;
2299 // Conversion type for interpreting results of a 32-bit instruction as
2300 // a 64-bit value or vice versa.
2301 enum ExtOrTruncConversion { Ext, Trunc };
2302
2303 // Modifiers to guide how an ISD::SETCC node's result is to be computed
2304 // in a GPR.
2305 // ZExtOrig - use the original condition code, zero-extend value
2306 // ZExtInvert - invert the condition code, zero-extend value
2307 // SExtOrig - use the original condition code, sign-extend value
2308 // SExtInvert - invert the condition code, sign-extend value
2309 enum SetccInGPROpts { ZExtOrig, ZExtInvert, SExtOrig, SExtInvert };
2310
2311 // Comparisons against zero to emit GPR code sequences for. Each of these
2312 // sequences may need to be emitted for two or more equivalent patterns.
2313 // For example (a >= 0) == (a > -1). The direction of the comparison (</>)
2314 // matters as well as the extension type: sext (-1/0), zext (1/0).
2315 // GEZExt - (zext (LHS >= 0))
2316 // GESExt - (sext (LHS >= 0))
2317 // LEZExt - (zext (LHS <= 0))
2318 // LESExt - (sext (LHS <= 0))
2319 enum ZeroCompare { GEZExt, GESExt, LEZExt, LESExt };
2320
2321 SDNode *tryEXTEND(SDNode *N);
2322 SDNode *tryLogicOpOfCompares(SDNode *N);
2323 SDValue computeLogicOpInGPR(SDValue LogicOp);
2324 SDValue signExtendInputIfNeeded(SDValue Input);
2325 SDValue zeroExtendInputIfNeeded(SDValue Input);
2326 SDValue addExtOrTrunc(SDValue NatWidthRes, ExtOrTruncConversion Conv);
2327 SDValue getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl,
2328 ZeroCompare CmpTy);
2329 SDValue get32BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2330 int64_t RHSValue, SDLoc dl);
2331 SDValue get32BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2332 int64_t RHSValue, SDLoc dl);
2333 SDValue get64BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2334 int64_t RHSValue, SDLoc dl);
2335 SDValue get64BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2336 int64_t RHSValue, SDLoc dl);
2337 SDValue getSETCCInGPR(SDValue Compare, SetccInGPROpts ConvOpts);
2338
2339public:
2340 IntegerCompareEliminator(SelectionDAG *DAG,
2341 PPCDAGToDAGISel *Sel) : CurDAG(DAG), S(Sel) {
2342 assert(CurDAG->getTargetLoweringInfo()
2343 .getPointerTy(CurDAG->getDataLayout()).getSizeInBits() == 64 &&
2344 "Only expecting to use this on 64 bit targets.");
2345 }
2346 SDNode *Select(SDNode *N) {
2347 if (CmpInGPR == ICGPR_None)
2348 return nullptr;
2349 switch (N->getOpcode()) {
2350 default: break;
2351 case ISD::ZERO_EXTEND:
2352 if (CmpInGPR == ICGPR_Sext || CmpInGPR == ICGPR_SextI32 ||
2353 CmpInGPR == ICGPR_SextI64)
2354 return nullptr;
Nemanja Ivanovic1794cdc2017-12-15 11:47:48 +00002355 LLVM_FALLTHROUGH;
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +00002356 case ISD::SIGN_EXTEND:
2357 if (CmpInGPR == ICGPR_Zext || CmpInGPR == ICGPR_ZextI32 ||
2358 CmpInGPR == ICGPR_ZextI64)
2359 return nullptr;
2360 return tryEXTEND(N);
2361 case ISD::AND:
2362 case ISD::OR:
2363 case ISD::XOR:
2364 return tryLogicOpOfCompares(N);
2365 }
2366 return nullptr;
2367 }
2368};
2369
2370static bool isLogicOp(unsigned Opc) {
2371 return Opc == ISD::AND || Opc == ISD::OR || Opc == ISD::XOR;
2372}
2373// The obvious case for wanting to keep the value in a GPR. Namely, the
2374// result of the comparison is actually needed in a GPR.
2375SDNode *IntegerCompareEliminator::tryEXTEND(SDNode *N) {
2376 assert((N->getOpcode() == ISD::ZERO_EXTEND ||
2377 N->getOpcode() == ISD::SIGN_EXTEND) &&
2378 "Expecting a zero/sign extend node!");
2379 SDValue WideRes;
2380 // If we are zero-extending the result of a logical operation on i1
2381 // values, we can keep the values in GPRs.
2382 if (isLogicOp(N->getOperand(0).getOpcode()) &&
2383 N->getOperand(0).getValueType() == MVT::i1 &&
2384 N->getOpcode() == ISD::ZERO_EXTEND)
2385 WideRes = computeLogicOpInGPR(N->getOperand(0));
2386 else if (N->getOperand(0).getOpcode() != ISD::SETCC)
2387 return nullptr;
2388 else
2389 WideRes =
2390 getSETCCInGPR(N->getOperand(0),
2391 N->getOpcode() == ISD::SIGN_EXTEND ?
2392 SetccInGPROpts::SExtOrig : SetccInGPROpts::ZExtOrig);
2393
2394 if (!WideRes)
2395 return nullptr;
2396
2397 SDLoc dl(N);
2398 bool Input32Bit = WideRes.getValueType() == MVT::i32;
2399 bool Output32Bit = N->getValueType(0) == MVT::i32;
2400
2401 NumSextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 1 : 0;
2402 NumZextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 0 : 1;
2403
2404 SDValue ConvOp = WideRes;
2405 if (Input32Bit != Output32Bit)
2406 ConvOp = addExtOrTrunc(WideRes, Input32Bit ? ExtOrTruncConversion::Ext :
2407 ExtOrTruncConversion::Trunc);
2408 return ConvOp.getNode();
2409}
2410
2411// Attempt to perform logical operations on the results of comparisons while
2412// keeping the values in GPRs. Without doing so, these would end up being
2413// lowered to CR-logical operations which suffer from significant latency and
2414// low ILP.
2415SDNode *IntegerCompareEliminator::tryLogicOpOfCompares(SDNode *N) {
2416 if (N->getValueType(0) != MVT::i1)
2417 return nullptr;
2418 assert(isLogicOp(N->getOpcode()) &&
2419 "Expected a logic operation on setcc results.");
2420 SDValue LoweredLogical = computeLogicOpInGPR(SDValue(N, 0));
2421 if (!LoweredLogical)
2422 return nullptr;
2423
2424 SDLoc dl(N);
2425 bool IsBitwiseNegate = LoweredLogical.getMachineOpcode() == PPC::XORI8;
2426 unsigned SubRegToExtract = IsBitwiseNegate ? PPC::sub_eq : PPC::sub_gt;
2427 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2428 SDValue LHS = LoweredLogical.getOperand(0);
2429 SDValue RHS = LoweredLogical.getOperand(1);
2430 SDValue WideOp;
2431 SDValue OpToConvToRecForm;
2432
2433 // Look through any 32-bit to 64-bit implicit extend nodes to find the
2434 // opcode that is input to the XORI.
2435 if (IsBitwiseNegate &&
2436 LoweredLogical.getOperand(0).getMachineOpcode() == PPC::INSERT_SUBREG)
2437 OpToConvToRecForm = LoweredLogical.getOperand(0).getOperand(1);
2438 else if (IsBitwiseNegate)
2439 // If the input to the XORI isn't an extension, that's what we're after.
2440 OpToConvToRecForm = LoweredLogical.getOperand(0);
2441 else
2442 // If this is not an XORI, it is a reg-reg logical op and we can convert
2443 // it to record-form.
2444 OpToConvToRecForm = LoweredLogical;
2445
2446 // Get the record-form version of the node we're looking to use to get the
2447 // CR result from.
2448 uint16_t NonRecOpc = OpToConvToRecForm.getMachineOpcode();
2449 int NewOpc = PPCInstrInfo::getRecordFormOpcode(NonRecOpc);
2450
2451 // Convert the right node to record-form. This is either the logical we're
2452 // looking at or it is the input node to the negation (if we're looking at
2453 // a bitwise negation).
2454 if (NewOpc != -1 && IsBitwiseNegate) {
2455 // The input to the XORI has a record-form. Use it.
2456 assert(LoweredLogical.getConstantOperandVal(1) == 1 &&
2457 "Expected a PPC::XORI8 only for bitwise negation.");
2458 // Emit the record-form instruction.
2459 std::vector<SDValue> Ops;
2460 for (int i = 0, e = OpToConvToRecForm.getNumOperands(); i < e; i++)
2461 Ops.push_back(OpToConvToRecForm.getOperand(i));
2462
2463 WideOp =
2464 SDValue(CurDAG->getMachineNode(NewOpc, dl,
2465 OpToConvToRecForm.getValueType(),
2466 MVT::Glue, Ops), 0);
2467 } else {
2468 assert((NewOpc != -1 || !IsBitwiseNegate) &&
2469 "No record form available for AND8/OR8/XOR8?");
2470 WideOp =
2471 SDValue(CurDAG->getMachineNode(NewOpc == -1 ? PPC::ANDIo8 : NewOpc, dl,
2472 MVT::i64, MVT::Glue, LHS, RHS), 0);
2473 }
2474
2475 // Select this node to a single bit from CR0 set by the record-form node
2476 // just created. For bitwise negation, use the EQ bit which is the equivalent
2477 // of negating the result (i.e. it is a bit set when the result of the
2478 // operation is zero).
2479 SDValue SRIdxVal =
2480 CurDAG->getTargetConstant(SubRegToExtract, dl, MVT::i32);
2481 SDValue CRBit =
2482 SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
2483 MVT::i1, CR0Reg, SRIdxVal,
2484 WideOp.getValue(1)), 0);
2485 return CRBit.getNode();
2486}
2487
2488// Lower a logical operation on i1 values into a GPR sequence if possible.
2489// The result can be kept in a GPR if requested.
2490// Three types of inputs can be handled:
2491// - SETCC
2492// - TRUNCATE
2493// - Logical operation (AND/OR/XOR)
2494// There is also a special case that is handled (namely a complement operation
2495// achieved with xor %a, -1).
2496SDValue IntegerCompareEliminator::computeLogicOpInGPR(SDValue LogicOp) {
2497 assert(isLogicOp(LogicOp.getOpcode()) &&
2498 "Can only handle logic operations here.");
2499 assert(LogicOp.getValueType() == MVT::i1 &&
2500 "Can only handle logic operations on i1 values here.");
2501 SDLoc dl(LogicOp);
2502 SDValue LHS, RHS;
2503
2504 // Special case: xor %a, -1
2505 bool IsBitwiseNegation = isBitwiseNot(LogicOp);
2506
2507 // Produces a GPR sequence for each operand of the binary logic operation.
2508 // For SETCC, it produces the respective comparison, for TRUNCATE it truncates
2509 // the value in a GPR and for logic operations, it will recursively produce
2510 // a GPR sequence for the operation.
2511 auto getLogicOperand = [&] (SDValue Operand) -> SDValue {
2512 unsigned OperandOpcode = Operand.getOpcode();
2513 if (OperandOpcode == ISD::SETCC)
2514 return getSETCCInGPR(Operand, SetccInGPROpts::ZExtOrig);
2515 else if (OperandOpcode == ISD::TRUNCATE) {
2516 SDValue InputOp = Operand.getOperand(0);
2517 EVT InVT = InputOp.getValueType();
2518 return SDValue(CurDAG->getMachineNode(InVT == MVT::i32 ? PPC::RLDICL_32 :
2519 PPC::RLDICL, dl, InVT, InputOp,
2520 S->getI64Imm(0, dl),
2521 S->getI64Imm(63, dl)), 0);
2522 } else if (isLogicOp(OperandOpcode))
2523 return computeLogicOpInGPR(Operand);
2524 return SDValue();
2525 };
2526 LHS = getLogicOperand(LogicOp.getOperand(0));
2527 RHS = getLogicOperand(LogicOp.getOperand(1));
2528
2529 // If a GPR sequence can't be produced for the LHS we can't proceed.
2530 // Not producing a GPR sequence for the RHS is only a problem if this isn't
2531 // a bitwise negation operation.
2532 if (!LHS || (!RHS && !IsBitwiseNegation))
2533 return SDValue();
2534
2535 NumLogicOpsOnComparison++;
2536
2537 // We will use the inputs as 64-bit values.
2538 if (LHS.getValueType() == MVT::i32)
2539 LHS = addExtOrTrunc(LHS, ExtOrTruncConversion::Ext);
2540 if (!IsBitwiseNegation && RHS.getValueType() == MVT::i32)
2541 RHS = addExtOrTrunc(RHS, ExtOrTruncConversion::Ext);
2542
2543 unsigned NewOpc;
2544 switch (LogicOp.getOpcode()) {
2545 default: llvm_unreachable("Unknown logic operation.");
2546 case ISD::AND: NewOpc = PPC::AND8; break;
2547 case ISD::OR: NewOpc = PPC::OR8; break;
2548 case ISD::XOR: NewOpc = PPC::XOR8; break;
2549 }
2550
2551 if (IsBitwiseNegation) {
2552 RHS = S->getI64Imm(1, dl);
2553 NewOpc = PPC::XORI8;
2554 }
2555
2556 return SDValue(CurDAG->getMachineNode(NewOpc, dl, MVT::i64, LHS, RHS), 0);
2557
2558}
2559
2560/// If the value isn't guaranteed to be sign-extended to 64-bits, extend it.
2561/// Otherwise just reinterpret it as a 64-bit value.
2562/// Useful when emitting comparison code for 32-bit values without using
2563/// the compare instruction (which only considers the lower 32-bits).
2564SDValue IntegerCompareEliminator::signExtendInputIfNeeded(SDValue Input) {
2565 assert(Input.getValueType() == MVT::i32 &&
2566 "Can only sign-extend 32-bit values here.");
2567 unsigned Opc = Input.getOpcode();
2568
2569 // The value was sign extended and then truncated to 32-bits. No need to
2570 // sign extend it again.
2571 if (Opc == ISD::TRUNCATE &&
2572 (Input.getOperand(0).getOpcode() == ISD::AssertSext ||
2573 Input.getOperand(0).getOpcode() == ISD::SIGN_EXTEND))
2574 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2575
2576 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2577 // The input is a sign-extending load. All ppc sign-extending loads
2578 // sign-extend to the full 64-bits.
2579 if (InputLoad && InputLoad->getExtensionType() == ISD::SEXTLOAD)
2580 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2581
2582 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2583 // We don't sign-extend constants.
2584 if (InputConst)
2585 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2586
2587 SDLoc dl(Input);
2588 SignExtensionsAdded++;
2589 return SDValue(CurDAG->getMachineNode(PPC::EXTSW_32_64, dl,
2590 MVT::i64, Input), 0);
2591}
2592
2593/// If the value isn't guaranteed to be zero-extended to 64-bits, extend it.
2594/// Otherwise just reinterpret it as a 64-bit value.
2595/// Useful when emitting comparison code for 32-bit values without using
2596/// the compare instruction (which only considers the lower 32-bits).
2597SDValue IntegerCompareEliminator::zeroExtendInputIfNeeded(SDValue Input) {
2598 assert(Input.getValueType() == MVT::i32 &&
2599 "Can only zero-extend 32-bit values here.");
2600 unsigned Opc = Input.getOpcode();
2601
2602 // The only condition under which we can omit the actual extend instruction:
2603 // - The value is a positive constant
2604 // - The value comes from a load that isn't a sign-extending load
2605 // An ISD::TRUNCATE needs to be zero-extended unless it is fed by a zext.
2606 bool IsTruncateOfZExt = Opc == ISD::TRUNCATE &&
2607 (Input.getOperand(0).getOpcode() == ISD::AssertZext ||
2608 Input.getOperand(0).getOpcode() == ISD::ZERO_EXTEND);
2609 if (IsTruncateOfZExt)
2610 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2611
2612 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2613 if (InputConst && InputConst->getSExtValue() >= 0)
2614 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2615
2616 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2617 // The input is a load that doesn't sign-extend (it will be zero-extended).
2618 if (InputLoad && InputLoad->getExtensionType() != ISD::SEXTLOAD)
2619 return addExtOrTrunc(Input, ExtOrTruncConversion::Ext);
2620
2621 // None of the above, need to zero-extend.
2622 SDLoc dl(Input);
2623 ZeroExtensionsAdded++;
2624 return SDValue(CurDAG->getMachineNode(PPC::RLDICL_32_64, dl, MVT::i64, Input,
2625 S->getI64Imm(0, dl),
2626 S->getI64Imm(32, dl)), 0);
2627}
2628
2629// Handle a 32-bit value in a 64-bit register and vice-versa. These are of
2630// course not actual zero/sign extensions that will generate machine code,
2631// they're just a way to reinterpret a 32 bit value in a register as a
2632// 64 bit value and vice-versa.
2633SDValue IntegerCompareEliminator::addExtOrTrunc(SDValue NatWidthRes,
2634 ExtOrTruncConversion Conv) {
2635 SDLoc dl(NatWidthRes);
2636
2637 // For reinterpreting 32-bit values as 64 bit values, we generate
2638 // INSERT_SUBREG IMPLICIT_DEF:i64, <input>, TargetConstant:i32<1>
2639 if (Conv == ExtOrTruncConversion::Ext) {
2640 SDValue ImDef(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, MVT::i64), 0);
2641 SDValue SubRegIdx =
2642 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2643 return SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, MVT::i64,
2644 ImDef, NatWidthRes, SubRegIdx), 0);
2645 }
2646
2647 assert(Conv == ExtOrTruncConversion::Trunc &&
2648 "Unknown convertion between 32 and 64 bit values.");
2649 // For reinterpreting 64-bit values as 32-bit values, we just need to
2650 // EXTRACT_SUBREG (i.e. extract the low word).
2651 SDValue SubRegIdx =
2652 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2653 return SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, MVT::i32,
2654 NatWidthRes, SubRegIdx), 0);
2655}
2656
2657// Produce a GPR sequence for compound comparisons (<=, >=) against zero.
2658// Handle both zero-extensions and sign-extensions.
2659SDValue
2660IntegerCompareEliminator::getCompoundZeroComparisonInGPR(SDValue LHS, SDLoc dl,
2661 ZeroCompare CmpTy) {
2662 EVT InVT = LHS.getValueType();
2663 bool Is32Bit = InVT == MVT::i32;
2664 SDValue ToExtend;
2665
2666 // Produce the value that needs to be either zero or sign extended.
2667 switch (CmpTy) {
2668 case ZeroCompare::GEZExt:
2669 case ZeroCompare::GESExt:
2670 ToExtend = SDValue(CurDAG->getMachineNode(Is32Bit ? PPC::NOR : PPC::NOR8,
2671 dl, InVT, LHS, LHS), 0);
2672 break;
2673 case ZeroCompare::LEZExt:
2674 case ZeroCompare::LESExt: {
2675 if (Is32Bit) {
2676 // Upper 32 bits cannot be undefined for this sequence.
2677 LHS = signExtendInputIfNeeded(LHS);
2678 SDValue Neg =
2679 SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0);
2680 ToExtend =
2681 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2682 Neg, S->getI64Imm(1, dl),
2683 S->getI64Imm(63, dl)), 0);
2684 } else {
2685 SDValue Addi =
2686 SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS,
2687 S->getI64Imm(~0ULL, dl)), 0);
2688 ToExtend = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
2689 Addi, LHS), 0);
2690 }
2691 break;
2692 }
2693 }
2694
2695 // For 64-bit sequences, the extensions are the same for the GE/LE cases.
2696 if (!Is32Bit &&
2697 (CmpTy == ZeroCompare::GEZExt || CmpTy == ZeroCompare::LEZExt))
2698 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2699 ToExtend, S->getI64Imm(1, dl),
2700 S->getI64Imm(63, dl)), 0);
2701 if (!Is32Bit &&
2702 (CmpTy == ZeroCompare::GESExt || CmpTy == ZeroCompare::LESExt))
2703 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, ToExtend,
2704 S->getI64Imm(63, dl)), 0);
2705
2706 assert(Is32Bit && "Should have handled the 32-bit sequences above.");
2707 // For 32-bit sequences, the extensions differ between GE/LE cases.
2708 switch (CmpTy) {
2709 case ZeroCompare::GEZExt: {
2710 SDValue ShiftOps[] = { ToExtend, S->getI32Imm(1, dl), S->getI32Imm(31, dl),
2711 S->getI32Imm(31, dl) };
2712 return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
2713 ShiftOps), 0);
2714 }
2715 case ZeroCompare::GESExt:
2716 return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, ToExtend,
2717 S->getI32Imm(31, dl)), 0);
2718 case ZeroCompare::LEZExt:
2719 return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, ToExtend,
2720 S->getI32Imm(1, dl)), 0);
2721 case ZeroCompare::LESExt:
2722 return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, ToExtend,
2723 S->getI32Imm(-1, dl)), 0);
2724 }
2725
2726 // The above case covers all the enumerators so it can't have a default clause
2727 // to avoid compiler warnings.
2728 llvm_unreachable("Unknown zero-comparison type.");
2729}
2730
2731/// Produces a zero-extended result of comparing two 32-bit values according to
2732/// the passed condition code.
2733SDValue
2734IntegerCompareEliminator::get32BitZExtCompare(SDValue LHS, SDValue RHS,
2735 ISD::CondCode CC,
2736 int64_t RHSValue, SDLoc dl) {
2737 if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 ||
2738 CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Sext)
2739 return SDValue();
2740 bool IsRHSZero = RHSValue == 0;
2741 bool IsRHSOne = RHSValue == 1;
2742 bool IsRHSNegOne = RHSValue == -1LL;
2743 switch (CC) {
2744 default: return SDValue();
2745 case ISD::SETEQ: {
2746 // (zext (setcc %a, %b, seteq)) -> (lshr (cntlzw (xor %a, %b)), 5)
2747 // (zext (setcc %a, 0, seteq)) -> (lshr (cntlzw %a), 5)
2748 SDValue Xor = IsRHSZero ? LHS :
2749 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2750 SDValue Clz =
2751 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2752 SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl),
2753 S->getI32Imm(31, dl) };
2754 return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
2755 ShiftOps), 0);
2756 }
2757 case ISD::SETNE: {
2758 // (zext (setcc %a, %b, setne)) -> (xor (lshr (cntlzw (xor %a, %b)), 5), 1)
2759 // (zext (setcc %a, 0, setne)) -> (xor (lshr (cntlzw %a), 5), 1)
2760 SDValue Xor = IsRHSZero ? LHS :
2761 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2762 SDValue Clz =
2763 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2764 SDValue ShiftOps[] = { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl),
2765 S->getI32Imm(31, dl) };
2766 SDValue Shift =
2767 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0);
2768 return SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift,
2769 S->getI32Imm(1, dl)), 0);
2770 }
2771 case ISD::SETGE: {
2772 // (zext (setcc %a, %b, setge)) -> (xor (lshr (sub %a, %b), 63), 1)
2773 // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 31)
2774 if(IsRHSZero)
2775 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt);
2776
2777 // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a)
2778 // by swapping inputs and falling through.
2779 std::swap(LHS, RHS);
2780 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
2781 IsRHSZero = RHSConst && RHSConst->isNullValue();
2782 LLVM_FALLTHROUGH;
2783 }
2784 case ISD::SETLE: {
2785 if (CmpInGPR == ICGPR_NonExtIn)
2786 return SDValue();
2787 // (zext (setcc %a, %b, setle)) -> (xor (lshr (sub %b, %a), 63), 1)
2788 // (zext (setcc %a, 0, setle)) -> (xor (lshr (- %a), 63), 1)
2789 if(IsRHSZero) {
2790 if (CmpInGPR == ICGPR_NonExtIn)
2791 return SDValue();
2792 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt);
2793 }
2794
2795 // The upper 32-bits of the register can't be undefined for this sequence.
2796 LHS = signExtendInputIfNeeded(LHS);
2797 RHS = signExtendInputIfNeeded(RHS);
2798 SDValue Sub =
2799 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0);
2800 SDValue Shift =
2801 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Sub,
2802 S->getI64Imm(1, dl), S->getI64Imm(63, dl)),
2803 0);
2804 return
2805 SDValue(CurDAG->getMachineNode(PPC::XORI8, dl,
2806 MVT::i64, Shift, S->getI32Imm(1, dl)), 0);
2807 }
2808 case ISD::SETGT: {
2809 // (zext (setcc %a, %b, setgt)) -> (lshr (sub %b, %a), 63)
2810 // (zext (setcc %a, -1, setgt)) -> (lshr (~ %a), 31)
2811 // (zext (setcc %a, 0, setgt)) -> (lshr (- %a), 63)
2812 // Handle SETLT -1 (which is equivalent to SETGE 0).
2813 if (IsRHSNegOne)
2814 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt);
2815
2816 if (IsRHSZero) {
2817 if (CmpInGPR == ICGPR_NonExtIn)
2818 return SDValue();
2819 // The upper 32-bits of the register can't be undefined for this sequence.
2820 LHS = signExtendInputIfNeeded(LHS);
2821 RHS = signExtendInputIfNeeded(RHS);
2822 SDValue Neg =
2823 SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0);
2824 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2825 Neg, S->getI32Imm(1, dl), S->getI32Imm(63, dl)), 0);
2826 }
2827 // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as
2828 // (%b < %a) by swapping inputs and falling through.
2829 std::swap(LHS, RHS);
2830 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
2831 IsRHSZero = RHSConst && RHSConst->isNullValue();
2832 IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1;
2833 LLVM_FALLTHROUGH;
2834 }
2835 case ISD::SETLT: {
2836 // (zext (setcc %a, %b, setlt)) -> (lshr (sub %a, %b), 63)
2837 // (zext (setcc %a, 1, setlt)) -> (xor (lshr (- %a), 63), 1)
2838 // (zext (setcc %a, 0, setlt)) -> (lshr %a, 31)
2839 // Handle SETLT 1 (which is equivalent to SETLE 0).
2840 if (IsRHSOne) {
2841 if (CmpInGPR == ICGPR_NonExtIn)
2842 return SDValue();
2843 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt);
2844 }
2845
2846 if (IsRHSZero) {
2847 SDValue ShiftOps[] = { LHS, S->getI32Imm(1, dl), S->getI32Imm(31, dl),
2848 S->getI32Imm(31, dl) };
2849 return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
2850 ShiftOps), 0);
2851 }
2852
2853 if (CmpInGPR == ICGPR_NonExtIn)
2854 return SDValue();
2855 // The upper 32-bits of the register can't be undefined for this sequence.
2856 LHS = signExtendInputIfNeeded(LHS);
2857 RHS = signExtendInputIfNeeded(RHS);
2858 SDValue SUBFNode =
2859 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0);
2860 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2861 SUBFNode, S->getI64Imm(1, dl),
2862 S->getI64Imm(63, dl)), 0);
2863 }
2864 case ISD::SETUGE:
2865 // (zext (setcc %a, %b, setuge)) -> (xor (lshr (sub %b, %a), 63), 1)
2866 // (zext (setcc %a, %b, setule)) -> (xor (lshr (sub %a, %b), 63), 1)
2867 std::swap(LHS, RHS);
2868 LLVM_FALLTHROUGH;
2869 case ISD::SETULE: {
2870 if (CmpInGPR == ICGPR_NonExtIn)
2871 return SDValue();
2872 // The upper 32-bits of the register can't be undefined for this sequence.
2873 LHS = zeroExtendInputIfNeeded(LHS);
2874 RHS = zeroExtendInputIfNeeded(RHS);
2875 SDValue Subtract =
2876 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0);
2877 SDValue SrdiNode =
2878 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2879 Subtract, S->getI64Imm(1, dl),
2880 S->getI64Imm(63, dl)), 0);
2881 return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64, SrdiNode,
2882 S->getI32Imm(1, dl)), 0);
2883 }
2884 case ISD::SETUGT:
2885 // (zext (setcc %a, %b, setugt)) -> (lshr (sub %b, %a), 63)
2886 // (zext (setcc %a, %b, setult)) -> (lshr (sub %a, %b), 63)
2887 std::swap(LHS, RHS);
2888 LLVM_FALLTHROUGH;
2889 case ISD::SETULT: {
2890 if (CmpInGPR == ICGPR_NonExtIn)
2891 return SDValue();
2892 // The upper 32-bits of the register can't be undefined for this sequence.
2893 LHS = zeroExtendInputIfNeeded(LHS);
2894 RHS = zeroExtendInputIfNeeded(RHS);
2895 SDValue Subtract =
2896 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0);
2897 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2898 Subtract, S->getI64Imm(1, dl),
2899 S->getI64Imm(63, dl)), 0);
2900 }
2901 }
2902}
2903
2904/// Produces a sign-extended result of comparing two 32-bit values according to
2905/// the passed condition code.
2906SDValue
2907IntegerCompareEliminator::get32BitSExtCompare(SDValue LHS, SDValue RHS,
2908 ISD::CondCode CC,
2909 int64_t RHSValue, SDLoc dl) {
2910 if (CmpInGPR == ICGPR_I64 || CmpInGPR == ICGPR_SextI64 ||
2911 CmpInGPR == ICGPR_ZextI64 || CmpInGPR == ICGPR_Zext)
2912 return SDValue();
2913 bool IsRHSZero = RHSValue == 0;
2914 bool IsRHSOne = RHSValue == 1;
2915 bool IsRHSNegOne = RHSValue == -1LL;
2916
2917 switch (CC) {
2918 default: return SDValue();
2919 case ISD::SETEQ: {
2920 // (sext (setcc %a, %b, seteq)) ->
2921 // (ashr (shl (ctlz (xor %a, %b)), 58), 63)
2922 // (sext (setcc %a, 0, seteq)) ->
2923 // (ashr (shl (ctlz %a), 58), 63)
2924 SDValue CountInput = IsRHSZero ? LHS :
2925 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2926 SDValue Cntlzw =
2927 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, CountInput), 0);
2928 SDValue SHLOps[] = { Cntlzw, S->getI32Imm(27, dl),
2929 S->getI32Imm(5, dl), S->getI32Imm(31, dl) };
2930 SDValue Slwi =
2931 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, SHLOps), 0);
2932 return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Slwi), 0);
2933 }
2934 case ISD::SETNE: {
2935 // Bitwise xor the operands, count leading zeros, shift right by 5 bits and
2936 // flip the bit, finally take 2's complement.
2937 // (sext (setcc %a, %b, setne)) ->
2938 // (neg (xor (lshr (ctlz (xor %a, %b)), 5), 1))
2939 // Same as above, but the first xor is not needed.
2940 // (sext (setcc %a, 0, setne)) ->
2941 // (neg (xor (lshr (ctlz %a), 5), 1))
2942 SDValue Xor = IsRHSZero ? LHS :
2943 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2944 SDValue Clz =
2945 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2946 SDValue ShiftOps[] =
2947 { Clz, S->getI32Imm(27, dl), S->getI32Imm(5, dl), S->getI32Imm(31, dl) };
2948 SDValue Shift =
2949 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, ShiftOps), 0);
2950 SDValue Xori =
2951 SDValue(CurDAG->getMachineNode(PPC::XORI, dl, MVT::i32, Shift,
2952 S->getI32Imm(1, dl)), 0);
2953 return SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Xori), 0);
2954 }
2955 case ISD::SETGE: {
2956 // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %a, %b), 63), -1)
2957 // (sext (setcc %a, 0, setge)) -> (ashr (~ %a), 31)
2958 if (IsRHSZero)
2959 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt);
2960
2961 // Not a special case (i.e. RHS == 0). Handle (%a >= %b) as (%b <= %a)
2962 // by swapping inputs and falling through.
2963 std::swap(LHS, RHS);
2964 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
2965 IsRHSZero = RHSConst && RHSConst->isNullValue();
2966 LLVM_FALLTHROUGH;
2967 }
2968 case ISD::SETLE: {
2969 if (CmpInGPR == ICGPR_NonExtIn)
2970 return SDValue();
2971 // (sext (setcc %a, %b, setge)) -> (add (lshr (sub %b, %a), 63), -1)
2972 // (sext (setcc %a, 0, setle)) -> (add (lshr (- %a), 63), -1)
2973 if (IsRHSZero)
2974 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt);
2975
2976 // The upper 32-bits of the register can't be undefined for this sequence.
2977 LHS = signExtendInputIfNeeded(LHS);
2978 RHS = signExtendInputIfNeeded(RHS);
2979 SDValue SUBFNode =
2980 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, MVT::Glue,
2981 LHS, RHS), 0);
2982 SDValue Srdi =
2983 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
2984 SUBFNode, S->getI64Imm(1, dl),
2985 S->getI64Imm(63, dl)), 0);
2986 return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Srdi,
2987 S->getI32Imm(-1, dl)), 0);
2988 }
2989 case ISD::SETGT: {
2990 // (sext (setcc %a, %b, setgt)) -> (ashr (sub %b, %a), 63)
2991 // (sext (setcc %a, -1, setgt)) -> (ashr (~ %a), 31)
2992 // (sext (setcc %a, 0, setgt)) -> (ashr (- %a), 63)
2993 if (IsRHSNegOne)
2994 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt);
2995 if (IsRHSZero) {
2996 if (CmpInGPR == ICGPR_NonExtIn)
2997 return SDValue();
2998 // The upper 32-bits of the register can't be undefined for this sequence.
2999 LHS = signExtendInputIfNeeded(LHS);
3000 RHS = signExtendInputIfNeeded(RHS);
3001 SDValue Neg =
3002 SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, LHS), 0);
3003 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Neg,
3004 S->getI64Imm(63, dl)), 0);
3005 }
3006 // Not a special case (i.e. RHS == 0 or RHS == -1). Handle (%a > %b) as
3007 // (%b < %a) by swapping inputs and falling through.
3008 std::swap(LHS, RHS);
3009 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3010 IsRHSZero = RHSConst && RHSConst->isNullValue();
3011 IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1;
3012 LLVM_FALLTHROUGH;
3013 }
3014 case ISD::SETLT: {
3015 // (sext (setcc %a, %b, setgt)) -> (ashr (sub %a, %b), 63)
3016 // (sext (setcc %a, 1, setgt)) -> (add (lshr (- %a), 63), -1)
3017 // (sext (setcc %a, 0, setgt)) -> (ashr %a, 31)
3018 if (IsRHSOne) {
3019 if (CmpInGPR == ICGPR_NonExtIn)
3020 return SDValue();
3021 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt);
3022 }
3023 if (IsRHSZero)
3024 return SDValue(CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, LHS,
3025 S->getI32Imm(31, dl)), 0);
3026
3027 if (CmpInGPR == ICGPR_NonExtIn)
3028 return SDValue();
3029 // The upper 32-bits of the register can't be undefined for this sequence.
3030 LHS = signExtendInputIfNeeded(LHS);
3031 RHS = signExtendInputIfNeeded(RHS);
3032 SDValue SUBFNode =
3033 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0);
3034 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64,
3035 SUBFNode, S->getI64Imm(63, dl)), 0);
3036 }
3037 case ISD::SETUGE:
3038 // (sext (setcc %a, %b, setuge)) -> (add (lshr (sub %a, %b), 63), -1)
3039 // (sext (setcc %a, %b, setule)) -> (add (lshr (sub %b, %a), 63), -1)
3040 std::swap(LHS, RHS);
3041 LLVM_FALLTHROUGH;
3042 case ISD::SETULE: {
3043 if (CmpInGPR == ICGPR_NonExtIn)
3044 return SDValue();
3045 // The upper 32-bits of the register can't be undefined for this sequence.
3046 LHS = zeroExtendInputIfNeeded(LHS);
3047 RHS = zeroExtendInputIfNeeded(RHS);
3048 SDValue Subtract =
3049 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, LHS, RHS), 0);
3050 SDValue Shift =
3051 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Subtract,
3052 S->getI32Imm(1, dl), S->getI32Imm(63,dl)),
3053 0);
3054 return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, Shift,
3055 S->getI32Imm(-1, dl)), 0);
3056 }
3057 case ISD::SETUGT:
3058 // (sext (setcc %a, %b, setugt)) -> (ashr (sub %b, %a), 63)
3059 // (sext (setcc %a, %b, setugt)) -> (ashr (sub %a, %b), 63)
3060 std::swap(LHS, RHS);
3061 LLVM_FALLTHROUGH;
3062 case ISD::SETULT: {
3063 if (CmpInGPR == ICGPR_NonExtIn)
3064 return SDValue();
3065 // The upper 32-bits of the register can't be undefined for this sequence.
3066 LHS = zeroExtendInputIfNeeded(LHS);
3067 RHS = zeroExtendInputIfNeeded(RHS);
3068 SDValue Subtract =
3069 SDValue(CurDAG->getMachineNode(PPC::SUBF8, dl, MVT::i64, RHS, LHS), 0);
3070 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64,
3071 Subtract, S->getI64Imm(63, dl)), 0);
3072 }
3073 }
3074}
3075
3076/// Produces a zero-extended result of comparing two 64-bit values according to
3077/// the passed condition code.
3078SDValue
3079IntegerCompareEliminator::get64BitZExtCompare(SDValue LHS, SDValue RHS,
3080 ISD::CondCode CC,
3081 int64_t RHSValue, SDLoc dl) {
3082 if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 ||
3083 CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Sext)
3084 return SDValue();
3085 bool IsRHSZero = RHSValue == 0;
3086 bool IsRHSOne = RHSValue == 1;
3087 bool IsRHSNegOne = RHSValue == -1LL;
3088 switch (CC) {
3089 default: return SDValue();
3090 case ISD::SETEQ: {
3091 // (zext (setcc %a, %b, seteq)) -> (lshr (ctlz (xor %a, %b)), 6)
3092 // (zext (setcc %a, 0, seteq)) -> (lshr (ctlz %a), 6)
3093 SDValue Xor = IsRHSZero ? LHS :
3094 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
3095 SDValue Clz =
3096 SDValue(CurDAG->getMachineNode(PPC::CNTLZD, dl, MVT::i64, Xor), 0);
3097 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Clz,
3098 S->getI64Imm(58, dl),
3099 S->getI64Imm(63, dl)), 0);
3100 }
3101 case ISD::SETNE: {
3102 // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1)
3103 // (zext (setcc %a, %b, setne)) -> (sube addc.reg, addc.reg, addc.CA)
3104 // {addcz.reg, addcz.CA} = (addcarry %a, -1)
3105 // (zext (setcc %a, 0, setne)) -> (sube addcz.reg, addcz.reg, addcz.CA)
3106 SDValue Xor = IsRHSZero ? LHS :
3107 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
3108 SDValue AC =
3109 SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue,
3110 Xor, S->getI32Imm(~0U, dl)), 0);
3111 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, AC,
3112 Xor, AC.getValue(1)), 0);
3113 }
3114 case ISD::SETGE: {
3115 // {subc.reg, subc.CA} = (subcarry %a, %b)
3116 // (zext (setcc %a, %b, setge)) ->
3117 // (adde (lshr %b, 63), (ashr %a, 63), subc.CA)
3118 // (zext (setcc %a, 0, setge)) -> (lshr (~ %a), 63)
3119 if (IsRHSZero)
3120 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt);
3121 std::swap(LHS, RHS);
3122 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3123 IsRHSZero = RHSConst && RHSConst->isNullValue();
3124 LLVM_FALLTHROUGH;
3125 }
3126 case ISD::SETLE: {
3127 // {subc.reg, subc.CA} = (subcarry %b, %a)
3128 // (zext (setcc %a, %b, setge)) ->
3129 // (adde (lshr %a, 63), (ashr %b, 63), subc.CA)
3130 // (zext (setcc %a, 0, setge)) -> (lshr (or %a, (add %a, -1)), 63)
3131 if (IsRHSZero)
3132 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt);
3133 SDValue ShiftL =
3134 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS,
3135 S->getI64Imm(1, dl),
3136 S->getI64Imm(63, dl)), 0);
3137 SDValue ShiftR =
3138 SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS,
3139 S->getI64Imm(63, dl)), 0);
3140 SDValue SubtractCarry =
3141 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3142 LHS, RHS), 1);
3143 return SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue,
3144 ShiftR, ShiftL, SubtractCarry), 0);
3145 }
3146 case ISD::SETGT: {
3147 // {subc.reg, subc.CA} = (subcarry %b, %a)
3148 // (zext (setcc %a, %b, setgt)) ->
3149 // (xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1)
3150 // (zext (setcc %a, 0, setgt)) -> (lshr (nor (add %a, -1), %a), 63)
3151 if (IsRHSNegOne)
3152 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GEZExt);
3153 if (IsRHSZero) {
3154 SDValue Addi =
3155 SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS,
3156 S->getI64Imm(~0ULL, dl)), 0);
3157 SDValue Nor =
3158 SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Addi, LHS), 0);
3159 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Nor,
3160 S->getI64Imm(1, dl),
3161 S->getI64Imm(63, dl)), 0);
3162 }
3163 std::swap(LHS, RHS);
3164 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3165 IsRHSZero = RHSConst && RHSConst->isNullValue();
3166 IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1;
3167 LLVM_FALLTHROUGH;
3168 }
3169 case ISD::SETLT: {
3170 // {subc.reg, subc.CA} = (subcarry %a, %b)
3171 // (zext (setcc %a, %b, setlt)) ->
3172 // (xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1)
3173 // (zext (setcc %a, 0, setlt)) -> (lshr %a, 63)
3174 if (IsRHSOne)
3175 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LEZExt);
3176 if (IsRHSZero)
3177 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS,
3178 S->getI64Imm(1, dl),
3179 S->getI64Imm(63, dl)), 0);
3180 SDValue SRADINode =
3181 SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64,
3182 LHS, S->getI64Imm(63, dl)), 0);
3183 SDValue SRDINode =
3184 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
3185 RHS, S->getI64Imm(1, dl),
3186 S->getI64Imm(63, dl)), 0);
3187 SDValue SUBFC8Carry =
3188 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3189 RHS, LHS), 1);
3190 SDValue ADDE8Node =
3191 SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue,
3192 SRDINode, SRADINode, SUBFC8Carry), 0);
3193 return SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64,
3194 ADDE8Node, S->getI64Imm(1, dl)), 0);
3195 }
3196 case ISD::SETUGE:
3197 // {subc.reg, subc.CA} = (subcarry %a, %b)
3198 // (zext (setcc %a, %b, setuge)) -> (add (sube %b, %b, subc.CA), 1)
3199 std::swap(LHS, RHS);
3200 LLVM_FALLTHROUGH;
3201 case ISD::SETULE: {
3202 // {subc.reg, subc.CA} = (subcarry %b, %a)
3203 // (zext (setcc %a, %b, setule)) -> (add (sube %a, %a, subc.CA), 1)
3204 SDValue SUBFC8Carry =
3205 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3206 LHS, RHS), 1);
3207 SDValue SUBFE8Node =
3208 SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue,
3209 LHS, LHS, SUBFC8Carry), 0);
3210 return SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64,
3211 SUBFE8Node, S->getI64Imm(1, dl)), 0);
3212 }
3213 case ISD::SETUGT:
3214 // {subc.reg, subc.CA} = (subcarry %b, %a)
3215 // (zext (setcc %a, %b, setugt)) -> -(sube %b, %b, subc.CA)
3216 std::swap(LHS, RHS);
3217 LLVM_FALLTHROUGH;
3218 case ISD::SETULT: {
3219 // {subc.reg, subc.CA} = (subcarry %a, %b)
3220 // (zext (setcc %a, %b, setult)) -> -(sube %a, %a, subc.CA)
3221 SDValue SubtractCarry =
3222 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3223 RHS, LHS), 1);
3224 SDValue ExtSub =
3225 SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64,
3226 LHS, LHS, SubtractCarry), 0);
3227 return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64,
3228 ExtSub), 0);
3229 }
3230 }
3231}
3232
3233/// Produces a sign-extended result of comparing two 64-bit values according to
3234/// the passed condition code.
3235SDValue
3236IntegerCompareEliminator::get64BitSExtCompare(SDValue LHS, SDValue RHS,
3237 ISD::CondCode CC,
3238 int64_t RHSValue, SDLoc dl) {
3239 if (CmpInGPR == ICGPR_I32 || CmpInGPR == ICGPR_SextI32 ||
3240 CmpInGPR == ICGPR_ZextI32 || CmpInGPR == ICGPR_Zext)
3241 return SDValue();
3242 bool IsRHSZero = RHSValue == 0;
3243 bool IsRHSOne = RHSValue == 1;
3244 bool IsRHSNegOne = RHSValue == -1LL;
3245 switch (CC) {
3246 default: return SDValue();
3247 case ISD::SETEQ: {
3248 // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1)
3249 // (sext (setcc %a, %b, seteq)) -> (sube addc.reg, addc.reg, addc.CA)
3250 // {addcz.reg, addcz.CA} = (addcarry %a, -1)
3251 // (sext (setcc %a, 0, seteq)) -> (sube addcz.reg, addcz.reg, addcz.CA)
3252 SDValue AddInput = IsRHSZero ? LHS :
3253 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
3254 SDValue Addic =
3255 SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue,
3256 AddInput, S->getI32Imm(~0U, dl)), 0);
3257 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, Addic,
3258 Addic, Addic.getValue(1)), 0);
3259 }
3260 case ISD::SETNE: {
3261 // {subfc.reg, subfc.CA} = (subcarry 0, (xor %a, %b))
3262 // (sext (setcc %a, %b, setne)) -> (sube subfc.reg, subfc.reg, subfc.CA)
3263 // {subfcz.reg, subfcz.CA} = (subcarry 0, %a)
3264 // (sext (setcc %a, 0, setne)) -> (sube subfcz.reg, subfcz.reg, subfcz.CA)
3265 SDValue Xor = IsRHSZero ? LHS :
3266 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
3267 SDValue SC =
3268 SDValue(CurDAG->getMachineNode(PPC::SUBFIC8, dl, MVT::i64, MVT::Glue,
3269 Xor, S->getI32Imm(0, dl)), 0);
3270 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, SC,
3271 SC, SC.getValue(1)), 0);
3272 }
3273 case ISD::SETGE: {
3274 // {subc.reg, subc.CA} = (subcarry %a, %b)
3275 // (zext (setcc %a, %b, setge)) ->
3276 // (- (adde (lshr %b, 63), (ashr %a, 63), subc.CA))
3277 // (zext (setcc %a, 0, setge)) -> (~ (ashr %a, 63))
3278 if (IsRHSZero)
3279 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt);
3280 std::swap(LHS, RHS);
3281 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3282 IsRHSZero = RHSConst && RHSConst->isNullValue();
3283 LLVM_FALLTHROUGH;
3284 }
3285 case ISD::SETLE: {
3286 // {subc.reg, subc.CA} = (subcarry %b, %a)
3287 // (zext (setcc %a, %b, setge)) ->
3288 // (- (adde (lshr %a, 63), (ashr %b, 63), subc.CA))
3289 // (zext (setcc %a, 0, setge)) -> (ashr (or %a, (add %a, -1)), 63)
3290 if (IsRHSZero)
3291 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt);
3292 SDValue ShiftR =
3293 SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, RHS,
3294 S->getI64Imm(63, dl)), 0);
3295 SDValue ShiftL =
3296 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, LHS,
3297 S->getI64Imm(1, dl),
3298 S->getI64Imm(63, dl)), 0);
3299 SDValue SubtractCarry =
3300 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3301 LHS, RHS), 1);
3302 SDValue Adde =
3303 SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64, MVT::Glue,
3304 ShiftR, ShiftL, SubtractCarry), 0);
3305 return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64, Adde), 0);
3306 }
3307 case ISD::SETGT: {
3308 // {subc.reg, subc.CA} = (subcarry %b, %a)
3309 // (zext (setcc %a, %b, setgt)) ->
3310 // -(xor (adde (lshr %a, 63), (ashr %b, 63), subc.CA), 1)
3311 // (zext (setcc %a, 0, setgt)) -> (ashr (nor (add %a, -1), %a), 63)
3312 if (IsRHSNegOne)
3313 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::GESExt);
3314 if (IsRHSZero) {
3315 SDValue Add =
3316 SDValue(CurDAG->getMachineNode(PPC::ADDI8, dl, MVT::i64, LHS,
3317 S->getI64Imm(-1, dl)), 0);
3318 SDValue Nor =
3319 SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64, Add, LHS), 0);
3320 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, Nor,
3321 S->getI64Imm(63, dl)), 0);
3322 }
3323 std::swap(LHS, RHS);
3324 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3325 IsRHSZero = RHSConst && RHSConst->isNullValue();
3326 IsRHSOne = RHSConst && RHSConst->getSExtValue() == 1;
3327 LLVM_FALLTHROUGH;
3328 }
3329 case ISD::SETLT: {
3330 // {subc.reg, subc.CA} = (subcarry %a, %b)
3331 // (zext (setcc %a, %b, setlt)) ->
3332 // -(xor (adde (lshr %b, 63), (ashr %a, 63), subc.CA), 1)
3333 // (zext (setcc %a, 0, setlt)) -> (ashr %a, 63)
3334 if (IsRHSOne)
3335 return getCompoundZeroComparisonInGPR(LHS, dl, ZeroCompare::LESExt);
3336 if (IsRHSZero) {
3337 return SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, LHS,
3338 S->getI64Imm(63, dl)), 0);
3339 }
3340 SDValue SRADINode =
3341 SDValue(CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64,
3342 LHS, S->getI64Imm(63, dl)), 0);
3343 SDValue SRDINode =
3344 SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64,
3345 RHS, S->getI64Imm(1, dl),
3346 S->getI64Imm(63, dl)), 0);
3347 SDValue SUBFC8Carry =
3348 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3349 RHS, LHS), 1);
3350 SDValue ADDE8Node =
3351 SDValue(CurDAG->getMachineNode(PPC::ADDE8, dl, MVT::i64,
3352 SRDINode, SRADINode, SUBFC8Carry), 0);
3353 SDValue XORI8Node =
3354 SDValue(CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64,
3355 ADDE8Node, S->getI64Imm(1, dl)), 0);
3356 return SDValue(CurDAG->getMachineNode(PPC::NEG8, dl, MVT::i64,
3357 XORI8Node), 0);
3358 }
3359 case ISD::SETUGE:
3360 // {subc.reg, subc.CA} = (subcarry %a, %b)
3361 // (sext (setcc %a, %b, setuge)) -> ~(sube %b, %b, subc.CA)
3362 std::swap(LHS, RHS);
3363 LLVM_FALLTHROUGH;
3364 case ISD::SETULE: {
3365 // {subc.reg, subc.CA} = (subcarry %b, %a)
3366 // (sext (setcc %a, %b, setule)) -> ~(sube %a, %a, subc.CA)
3367 SDValue SubtractCarry =
3368 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3369 LHS, RHS), 1);
3370 SDValue ExtSub =
3371 SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, MVT::Glue, LHS,
3372 LHS, SubtractCarry), 0);
3373 return SDValue(CurDAG->getMachineNode(PPC::NOR8, dl, MVT::i64,
3374 ExtSub, ExtSub), 0);
3375 }
3376 case ISD::SETUGT:
3377 // {subc.reg, subc.CA} = (subcarry %b, %a)
3378 // (sext (setcc %a, %b, setugt)) -> (sube %b, %b, subc.CA)
3379 std::swap(LHS, RHS);
3380 LLVM_FALLTHROUGH;
3381 case ISD::SETULT: {
3382 // {subc.reg, subc.CA} = (subcarry %a, %b)
3383 // (sext (setcc %a, %b, setult)) -> (sube %a, %a, subc.CA)
3384 SDValue SubCarry =
3385 SDValue(CurDAG->getMachineNode(PPC::SUBFC8, dl, MVT::i64, MVT::Glue,
3386 RHS, LHS), 1);
3387 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64,
3388 LHS, LHS, SubCarry), 0);
3389 }
3390 }
3391}
3392
3393/// Do all uses of this SDValue need the result in a GPR?
3394/// This is meant to be used on values that have type i1 since
3395/// it is somewhat meaningless to ask if values of other types
3396/// should be kept in GPR's.
3397static bool allUsesExtend(SDValue Compare, SelectionDAG *CurDAG) {
3398 assert(Compare.getOpcode() == ISD::SETCC &&
3399 "An ISD::SETCC node required here.");
3400
3401 // For values that have a single use, the caller should obviously already have
3402 // checked if that use is an extending use. We check the other uses here.
3403 if (Compare.hasOneUse())
3404 return true;
3405 // We want the value in a GPR if it is being extended, used for a select, or
3406 // used in logical operations.
3407 for (auto CompareUse : Compare.getNode()->uses())
3408 if (CompareUse->getOpcode() != ISD::SIGN_EXTEND &&
3409 CompareUse->getOpcode() != ISD::ZERO_EXTEND &&
3410 CompareUse->getOpcode() != ISD::SELECT &&
3411 !isLogicOp(CompareUse->getOpcode())) {
3412 OmittedForNonExtendUses++;
3413 return false;
3414 }
3415 return true;
3416}
3417
3418/// Returns an equivalent of a SETCC node but with the result the same width as
Hiroshi Inoueb5578462018-06-07 12:49:12 +00003419/// the inputs. This can also be used for SELECT_CC if either the true or false
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +00003420/// values is a power of two while the other is zero.
3421SDValue IntegerCompareEliminator::getSETCCInGPR(SDValue Compare,
3422 SetccInGPROpts ConvOpts) {
3423 assert((Compare.getOpcode() == ISD::SETCC ||
3424 Compare.getOpcode() == ISD::SELECT_CC) &&
3425 "An ISD::SETCC node required here.");
3426
3427 // Don't convert this comparison to a GPR sequence because there are uses
3428 // of the i1 result (i.e. uses that require the result in the CR).
3429 if ((Compare.getOpcode() == ISD::SETCC) && !allUsesExtend(Compare, CurDAG))
3430 return SDValue();
3431
3432 SDValue LHS = Compare.getOperand(0);
3433 SDValue RHS = Compare.getOperand(1);
3434
3435 // The condition code is operand 2 for SETCC and operand 4 for SELECT_CC.
3436 int CCOpNum = Compare.getOpcode() == ISD::SELECT_CC ? 4 : 2;
3437 ISD::CondCode CC =
3438 cast<CondCodeSDNode>(Compare.getOperand(CCOpNum))->get();
3439 EVT InputVT = LHS.getValueType();
3440 if (InputVT != MVT::i32 && InputVT != MVT::i64)
3441 return SDValue();
3442
3443 if (ConvOpts == SetccInGPROpts::ZExtInvert ||
3444 ConvOpts == SetccInGPROpts::SExtInvert)
3445 CC = ISD::getSetCCInverse(CC, true);
3446
3447 bool Inputs32Bit = InputVT == MVT::i32;
3448
3449 SDLoc dl(Compare);
3450 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
3451 int64_t RHSValue = RHSConst ? RHSConst->getSExtValue() : INT64_MAX;
3452 bool IsSext = ConvOpts == SetccInGPROpts::SExtOrig ||
3453 ConvOpts == SetccInGPROpts::SExtInvert;
3454
3455 if (IsSext && Inputs32Bit)
3456 return get32BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
3457 else if (Inputs32Bit)
3458 return get32BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
3459 else if (IsSext)
3460 return get64BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
3461 return get64BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
3462}
3463
Eugene Zelenko8187c192017-01-13 00:58:58 +00003464} // end anonymous namespace
Hal Finkel8adf2252014-12-16 05:51:41 +00003465
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +00003466bool PPCDAGToDAGISel::tryIntCompareInGPR(SDNode *N) {
3467 if (N->getValueType(0) != MVT::i32 &&
3468 N->getValueType(0) != MVT::i64)
3469 return false;
3470
3471 // This optimization will emit code that assumes 64-bit registers
3472 // so we don't want to run it in 32-bit mode. Also don't run it
3473 // on functions that are not to be optimized.
3474 if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64())
3475 return false;
3476
3477 switch (N->getOpcode()) {
3478 default: break;
3479 case ISD::ZERO_EXTEND:
3480 case ISD::SIGN_EXTEND:
3481 case ISD::AND:
3482 case ISD::OR:
3483 case ISD::XOR: {
3484 IntegerCompareEliminator ICmpElim(CurDAG, this);
3485 if (SDNode *New = ICmpElim.Select(N)) {
3486 ReplaceNode(N, New);
3487 return true;
3488 }
3489 }
3490 }
3491 return false;
3492}
3493
Justin Bognerdc8af062016-05-20 21:43:23 +00003494bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) {
Hal Finkel8adf2252014-12-16 05:51:41 +00003495 if (N->getValueType(0) != MVT::i32 &&
3496 N->getValueType(0) != MVT::i64)
Justin Bognerdc8af062016-05-20 21:43:23 +00003497 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00003498
Hal Finkelc58ce412015-01-01 02:53:29 +00003499 if (!UseBitPermRewriter)
Justin Bognerdc8af062016-05-20 21:43:23 +00003500 return false;
Hal Finkelc58ce412015-01-01 02:53:29 +00003501
Hal Finkel8adf2252014-12-16 05:51:41 +00003502 switch (N->getOpcode()) {
3503 default: break;
3504 case ISD::ROTL:
3505 case ISD::SHL:
3506 case ISD::SRL:
3507 case ISD::AND:
3508 case ISD::OR: {
3509 BitPermutationSelector BPS(CurDAG);
Justin Bognerdc8af062016-05-20 21:43:23 +00003510 if (SDNode *New = BPS.Select(N)) {
3511 ReplaceNode(N, New);
3512 return true;
3513 }
3514 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00003515 }
3516 }
3517
Justin Bognerdc8af062016-05-20 21:43:23 +00003518 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00003519}
3520
Chris Lattner2a1823d2005-08-21 18:50:37 +00003521/// SelectCC - Select a comparison of the specified values with the specified
3522/// condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003523SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3524 const SDLoc &dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00003525 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00003526 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00003527
Owen Anderson9f944592009-08-11 20:47:22 +00003528 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00003529 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00003530 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
3531 if (isInt32Immediate(RHS, Imm)) {
3532 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00003533 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003534 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003535 getI32Imm(Imm & 0xFFFF, dl)),
3536 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00003537 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00003538 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003539 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003540 getI32Imm(Imm & 0xFFFF, dl)),
3541 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00003542
Chris Lattneraa3926b2006-09-20 04:25:47 +00003543 // For non-equality comparisons, the default code would materialize the
3544 // constant, then compare against it, like this:
3545 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00003546 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00003547 // cmpw cr0, r3, r2
3548 // Since we are just comparing for equality, we can emit this instead:
3549 // xoris r0,r3,0x1234
3550 // cmplwi cr0,r0,0x5678
3551 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00003552 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003553 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00003554 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003555 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00003556 }
3557 Opc = PPC::CMPLW;
3558 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00003559 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003560 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003561 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00003562 Opc = PPC::CMPLW;
3563 } else {
Lei Huang31710412017-07-07 21:12:35 +00003564 int16_t SImm;
Chris Lattner97b3da12006-06-27 00:04:13 +00003565 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003566 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003567 getI32Imm((int)SImm & 0xFFFF,
3568 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00003569 0);
3570 Opc = PPC::CMPW;
3571 }
Owen Anderson9f944592009-08-11 20:47:22 +00003572 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00003573 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003574 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003575 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003576 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00003577 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003578 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003579 getI32Imm(Imm & 0xFFFF, dl)),
3580 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003581 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00003582 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003583 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003584 getI32Imm(Imm & 0xFFFF, dl)),
3585 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00003586
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003587 // For non-equality comparisons, the default code would materialize the
3588 // constant, then compare against it, like this:
3589 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00003590 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003591 // cmpd cr0, r3, r2
3592 // Since we are just comparing for equality, we can emit this instead:
3593 // xoris r0,r3,0x1234
3594 // cmpldi cr0,r0,0x5678
3595 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00003596 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00003597 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003598 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00003599 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003600 getI64Imm(Imm & 0xFFFF, dl)),
3601 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00003602 }
3603 }
3604 Opc = PPC::CMPLD;
3605 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00003606 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003607 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003608 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00003609 Opc = PPC::CMPLD;
3610 } else {
Lei Huang31710412017-07-07 21:12:35 +00003611 int16_t SImm;
Chris Lattner97b3da12006-06-27 00:04:13 +00003612 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00003613 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003614 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00003615 0);
3616 Opc = PPC::CMPD;
3617 }
Owen Anderson9f944592009-08-11 20:47:22 +00003618 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00003619 Opc = PPC::FCMPUS;
Stefan Pintilie3d763262018-07-09 13:36:14 +00003620 } else if (LHS.getValueType() == MVT::f64) {
Eric Christopher1b8e7632014-05-22 01:07:24 +00003621 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Stefan Pintilie3d763262018-07-09 13:36:14 +00003622 } else {
3623 assert(LHS.getValueType() == MVT::f128 && "Unknown vt!");
3624 assert(PPCSubTarget->hasVSX() && "__float128 requires VSX");
3625 Opc = PPC::XSCMPUQP;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003626 }
Dan Gohman32f71d72009-09-25 18:54:59 +00003627 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00003628}
3629
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003630static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00003631 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00003632 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00003633 case ISD::SETONE:
3634 case ISD::SETOLE:
3635 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003636 llvm_unreachable("Should be lowered by legalize!");
3637 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00003638 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003639 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00003640 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003641 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00003642 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003643 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003644 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003645 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00003646 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003647 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003648 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003649 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003650 case ISD::SETO: return PPC::PRED_NU;
3651 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00003652 // These two are invalid for floating point. Assume we have int.
3653 case ISD::SETULT: return PPC::PRED_LT;
3654 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003655 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00003656}
3657
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003658/// getCRIdxForSetCC - Return the index of the condition register field
3659/// associated with the SetCC condition, and whether or not the field is
3660/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00003661static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00003662 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003663 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003664 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00003665 case ISD::SETOLT:
3666 case ISD::SETLT: return 0; // Bit #0 = SETOLT
3667 case ISD::SETOGT:
3668 case ISD::SETGT: return 1; // Bit #1 = SETOGT
3669 case ISD::SETOEQ:
3670 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
3671 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003672 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00003673 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003674 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00003675 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00003676 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00003677 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
3678 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00003679 case ISD::SETUEQ:
3680 case ISD::SETOGE:
3681 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00003682 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003683 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00003684 // These are invalid for floating point. Assume integer.
3685 case ISD::SETULT: return 0;
3686 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003687 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00003688}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00003689
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003690// getVCmpInst: return the vector compare instruction for the specified
3691// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00003692// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003693static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
3694 bool HasVSX, bool &Swap, bool &Negate) {
3695 Swap = false;
3696 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003697
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003698 if (VecVT.isFloatingPoint()) {
3699 /* Handle some cases by swapping input operands. */
3700 switch (CC) {
3701 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
3702 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
3703 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
3704 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
3705 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
3706 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
3707 default: break;
3708 }
3709 /* Handle some cases by negating the result. */
3710 switch (CC) {
3711 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
3712 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
3713 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
3714 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
3715 default: break;
3716 }
3717 /* We have instructions implementing the remaining cases. */
3718 switch (CC) {
3719 case ISD::SETEQ:
3720 case ISD::SETOEQ:
3721 if (VecVT == MVT::v4f32)
3722 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
3723 else if (VecVT == MVT::v2f64)
3724 return PPC::XVCMPEQDP;
3725 break;
3726 case ISD::SETGT:
3727 case ISD::SETOGT:
3728 if (VecVT == MVT::v4f32)
3729 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
3730 else if (VecVT == MVT::v2f64)
3731 return PPC::XVCMPGTDP;
3732 break;
3733 case ISD::SETGE:
3734 case ISD::SETOGE:
3735 if (VecVT == MVT::v4f32)
3736 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
3737 else if (VecVT == MVT::v2f64)
3738 return PPC::XVCMPGEDP;
3739 break;
3740 default:
3741 break;
3742 }
3743 llvm_unreachable("Invalid floating-point vector compare condition");
3744 } else {
3745 /* Handle some cases by swapping input operands. */
3746 switch (CC) {
3747 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
3748 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
3749 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
3750 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
3751 default: break;
3752 }
3753 /* Handle some cases by negating the result. */
3754 switch (CC) {
3755 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
3756 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
3757 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
3758 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
3759 default: break;
3760 }
3761 /* We have instructions implementing the remaining cases. */
3762 switch (CC) {
3763 case ISD::SETEQ:
3764 case ISD::SETUEQ:
3765 if (VecVT == MVT::v16i8)
3766 return PPC::VCMPEQUB;
3767 else if (VecVT == MVT::v8i16)
3768 return PPC::VCMPEQUH;
3769 else if (VecVT == MVT::v4i32)
3770 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00003771 else if (VecVT == MVT::v2i64)
3772 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003773 break;
3774 case ISD::SETGT:
3775 if (VecVT == MVT::v16i8)
3776 return PPC::VCMPGTSB;
3777 else if (VecVT == MVT::v8i16)
3778 return PPC::VCMPGTSH;
3779 else if (VecVT == MVT::v4i32)
3780 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00003781 else if (VecVT == MVT::v2i64)
3782 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003783 break;
3784 case ISD::SETUGT:
3785 if (VecVT == MVT::v16i8)
3786 return PPC::VCMPGTUB;
3787 else if (VecVT == MVT::v8i16)
3788 return PPC::VCMPGTUH;
3789 else if (VecVT == MVT::v4i32)
3790 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00003791 else if (VecVT == MVT::v2i64)
3792 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003793 break;
3794 default:
3795 break;
3796 }
3797 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003798 }
3799}
3800
Justin Bognerdc8af062016-05-20 21:43:23 +00003801bool PPCDAGToDAGISel::trySETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003802 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00003803 unsigned Imm;
3804 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00003805 EVT PtrVT =
3806 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00003807 bool isPPC64 = (PtrVT == MVT::i64);
3808
Eric Christopher1b8e7632014-05-22 01:07:24 +00003809 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00003810 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00003811 // We can codegen setcc op, imm very efficiently compared to a brcond.
3812 // Check for those cases here.
3813 // setcc op, 0
3814 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003815 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00003816 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00003817 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00003818 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00003819 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003820 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
3821 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003822 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3823 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00003824 }
Chris Lattnere2969492005-10-21 21:17:10 +00003825 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00003826 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003827 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003828 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003829 Op, getI32Imm(~0U, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003830 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
3831 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00003832 }
Evan Chengc3acfc02006-08-27 08:14:06 +00003833 case ISD::SETLT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003834 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
3835 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003836 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3837 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00003838 }
Chris Lattnere2969492005-10-21 21:17:10 +00003839 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003840 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00003841 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
3842 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003843 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
3844 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003845 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3846 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00003847 }
3848 }
Chris Lattner491b8292005-10-06 19:03:35 +00003849 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003850 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00003851 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00003852 default: break;
3853 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00003854 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003855 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003856 Op, getI32Imm(1, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003857 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
3858 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
3859 MVT::i32,
3860 getI32Imm(0, dl)),
3861 0), Op.getValue(1));
3862 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00003863 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00003864 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00003865 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003866 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003867 Op, getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00003868 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op,
3869 SDValue(AD, 1));
3870 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00003871 }
Chris Lattnere2969492005-10-21 21:17:10 +00003872 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00003873 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003874 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00003875 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
3876 Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003877 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
3878 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003879 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3880 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00003881 }
Evan Chengc3acfc02006-08-27 08:14:06 +00003882 case ISD::SETGT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003883 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
3884 getI32Imm(31, dl) };
3885 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003886 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl));
3887 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00003888 }
Evan Chengc3acfc02006-08-27 08:14:06 +00003889 }
Chris Lattner491b8292005-10-06 19:03:35 +00003890 }
3891 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003892
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00003893 SDValue LHS = N->getOperand(0);
3894 SDValue RHS = N->getOperand(1);
3895
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003896 // Altivec Vector compare instructions do not set any CR register by default and
3897 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00003898 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00003899 if (PPCSubTarget->hasQPX())
Justin Bognerdc8af062016-05-20 21:43:23 +00003900 return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003901
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003902 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003903 bool Swap, Negate;
3904 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
3905 PPCSubTarget->hasVSX(), Swap, Negate);
3906 if (Swap)
3907 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003908
Hal Finkel9fdce9a2015-08-20 03:02:02 +00003909 EVT ResVT = VecVT.changeVectorElementTypeToInteger();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003910 if (Negate) {
Hal Finkel9fdce9a2015-08-20 03:02:02 +00003911 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003912 CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR,
3913 ResVT, VCmp, VCmp);
3914 return true;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00003915 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00003916
Justin Bognerdc8af062016-05-20 21:43:23 +00003917 CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS);
3918 return true;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00003919 }
3920
Eric Christopher1b8e7632014-05-22 01:07:24 +00003921 if (PPCSubTarget->useCRBits())
Justin Bognerdc8af062016-05-20 21:43:23 +00003922 return false;
Hal Finkel940ab932014-02-28 00:27:01 +00003923
Chris Lattner491b8292005-10-06 19:03:35 +00003924 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00003925 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00003926 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003927 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00003928
Chris Lattner491b8292005-10-06 19:03:35 +00003929 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00003930 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00003931
Craig Topper062a2ba2014-04-25 05:30:21 +00003932 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00003933 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00003934 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00003935
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00003936 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
3937 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00003938
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003939 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
3940 getI32Imm(31, dl), getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003941 if (!Inv) {
3942 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3943 return true;
3944 }
Chris Lattner89f36e62008-01-08 06:46:30 +00003945
3946 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003947 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00003948 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003949 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
3950 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00003951}
Chris Lattner502a3692005-10-06 18:56:10 +00003952
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +00003953/// Does this node represent a load/store node whose address can be represented
3954/// with a register plus an immediate that's a multiple of \p Val:
3955bool PPCDAGToDAGISel::isOffsetMultipleOf(SDNode *N, unsigned Val) const {
3956 LoadSDNode *LDN = dyn_cast<LoadSDNode>(N);
3957 StoreSDNode *STN = dyn_cast<StoreSDNode>(N);
3958 SDValue AddrOp;
3959 if (LDN)
3960 AddrOp = LDN->getOperand(1);
3961 else if (STN)
3962 AddrOp = STN->getOperand(2);
3963
Hiroshi Inouea2eefb62018-04-06 05:41:16 +00003964 // If the address points a frame object or a frame object with an offset,
3965 // we need to check the object alignment.
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +00003966 short Imm = 0;
Hiroshi Inouea2eefb62018-04-06 05:41:16 +00003967 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(
3968 AddrOp.getOpcode() == ISD::ADD ? AddrOp.getOperand(0) :
3969 AddrOp)) {
Kyle Butt8c0314c2017-09-09 00:37:56 +00003970 // If op0 is a frame index that is under aligned, we can't do it either,
3971 // because it is translated to r31 or r1 + slot + offset. We won't know the
3972 // slot number until the stack frame is finalized.
Hiroshi Inouea2eefb62018-04-06 05:41:16 +00003973 const MachineFrameInfo &MFI = CurDAG->getMachineFunction().getFrameInfo();
3974 unsigned SlotAlign = MFI.getObjectAlignment(FI->getIndex());
3975 if ((SlotAlign % Val) != 0)
3976 return false;
3977
3978 // If we have an offset, we need further check on the offset.
3979 if (AddrOp.getOpcode() != ISD::ADD)
3980 return true;
Kyle Butt8c0314c2017-09-09 00:37:56 +00003981 }
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +00003982
Hiroshi Inouea2eefb62018-04-06 05:41:16 +00003983 if (AddrOp.getOpcode() == ISD::ADD)
3984 return isIntS16Immediate(AddrOp.getOperand(1), Imm) && !(Imm % Val);
3985
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +00003986 // If the address comes from the outside, the offset will be zero.
3987 return AddrOp.getOpcode() == ISD::CopyFromReg;
3988}
3989
Justin Bognerdc8af062016-05-20 21:43:23 +00003990void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
Hal Finkelcf599212015-02-25 21:36:59 +00003991 // Transfer memoperands.
3992 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3993 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
3994 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
Hal Finkelcf599212015-02-25 21:36:59 +00003995}
3996
Hiroshi Inoue33486782018-04-21 09:32:17 +00003997/// This method returns a node after flipping the MSB of each element
3998/// of vector integer type. Additionally, if SignBitVec is non-null,
3999/// this method sets a node with one at MSB of all elements
4000/// and zero at other bits in SignBitVec.
4001MachineSDNode *
4002PPCDAGToDAGISel::flipSignBit(const SDValue &N, SDNode **SignBitVec) {
4003 SDLoc dl(N);
4004 EVT VecVT = N.getValueType();
4005 if (VecVT == MVT::v4i32) {
4006 if (SignBitVec) {
4007 SDNode *ZV = CurDAG->getMachineNode(PPC::V_SET0, dl, MVT::v4i32);
4008 *SignBitVec = CurDAG->getMachineNode(PPC::XVNEGSP, dl, VecVT,
4009 SDValue(ZV, 0));
4010 }
4011 return CurDAG->getMachineNode(PPC::XVNEGSP, dl, VecVT, N);
4012 }
4013 else if (VecVT == MVT::v8i16) {
4014 SDNode *Hi = CurDAG->getMachineNode(PPC::LIS, dl, MVT::i32,
4015 getI32Imm(0x8000, dl));
4016 SDNode *ScaImm = CurDAG->getMachineNode(PPC::ORI, dl, MVT::i32,
4017 SDValue(Hi, 0),
4018 getI32Imm(0x8000, dl));
4019 SDNode *VecImm = CurDAG->getMachineNode(PPC::MTVSRWS, dl, VecVT,
4020 SDValue(ScaImm, 0));
4021 /*
4022 Alternatively, we can do this as follow to use VRF instead of GPR.
4023 vspltish 5, 1
4024 vspltish 6, 15
4025 vslh 5, 6, 5
4026 */
4027 if (SignBitVec) *SignBitVec = VecImm;
4028 return CurDAG->getMachineNode(PPC::VADDUHM, dl, VecVT, N,
4029 SDValue(VecImm, 0));
4030 }
4031 else if (VecVT == MVT::v16i8) {
4032 SDNode *VecImm = CurDAG->getMachineNode(PPC::XXSPLTIB, dl, MVT::i32,
4033 getI32Imm(0x80, dl));
4034 if (SignBitVec) *SignBitVec = VecImm;
4035 return CurDAG->getMachineNode(PPC::VADDUBM, dl, VecVT, N,
4036 SDValue(VecImm, 0));
4037 }
4038 else
4039 llvm_unreachable("Unsupported vector data type for flipSignBit");
4040}
4041
Chris Lattner43ff01e2005-08-17 19:33:03 +00004042// Select - Convert the specified operand from a target-independent to a
4043// target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +00004044void PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004045 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00004046 if (N->isMachineOpcode()) {
4047 N->setNodeId(-1);
Justin Bognerdc8af062016-05-20 21:43:23 +00004048 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00004049 }
Chris Lattner08c319f2005-09-29 00:59:32 +00004050
Hal Finkel51b3fd12014-09-02 06:23:54 +00004051 // In case any misguided DAG-level optimizations form an ADD with a
4052 // TargetConstant operand, crash here instead of miscompiling (by selecting
4053 // an r+r add instead of some kind of r+i add).
4054 if (N->getOpcode() == ISD::ADD &&
4055 N->getOperand(1).getOpcode() == ISD::TargetConstant)
4056 llvm_unreachable("Invalid ADD with TargetConstant operand");
4057
Hal Finkel8adf2252014-12-16 05:51:41 +00004058 // Try matching complex bit permutations before doing anything else.
Justin Bognerdc8af062016-05-20 21:43:23 +00004059 if (tryBitPermutation(N))
4060 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00004061
Nemanja Ivanovicdb7e7702017-11-30 13:39:10 +00004062 // Try to emit integer compares as GPR-only sequences (i.e. no use of CR).
4063 if (tryIntCompareInGPR(N))
4064 return;
4065
Chris Lattner43ff01e2005-08-17 19:33:03 +00004066 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00004067 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00004068
Eugene Zelenko8187c192017-01-13 00:58:58 +00004069 case ISD::Constant:
Justin Bognerdc8af062016-05-20 21:43:23 +00004070 if (N->getValueType(0) == MVT::i64) {
Hiroshi Inoue5703fe32017-07-31 06:27:09 +00004071 ReplaceNode(N, selectI64Imm(CurDAG, N));
Justin Bognerdc8af062016-05-20 21:43:23 +00004072 return;
4073 }
Jim Laskey095e6f32006-12-12 13:23:43 +00004074 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00004075
Eugene Zelenko8187c192017-01-13 00:58:58 +00004076 case ISD::SETCC:
Justin Bognerdc8af062016-05-20 21:43:23 +00004077 if (trySETCC(N))
4078 return;
Hal Finkel940ab932014-02-28 00:27:01 +00004079 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00004080
Strahinja Petrovic06cf6a62018-03-27 11:23:53 +00004081 case PPCISD::CALL: {
4082 const Module *M = MF->getFunction().getParent();
4083
4084 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) != MVT::i32 ||
4085 !PPCSubTarget->isSecurePlt() || !PPCSubTarget->isTargetELF() ||
4086 M->getPICLevel() == PICLevel::SmallPIC)
4087 break;
4088
4089 SDValue Op = N->getOperand(1);
4090
4091 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4092 if (GA->getTargetFlags() == PPCII::MO_PLT)
4093 getGlobalBaseReg();
4094 }
4095 else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
4096 if (ES->getTargetFlags() == PPCII::MO_PLT)
4097 getGlobalBaseReg();
4098 }
4099 }
4100 break;
4101
Evan Cheng6dc90ca2006-02-09 00:37:58 +00004102 case PPCISD::GlobalBaseReg:
Justin Bognerdc8af062016-05-20 21:43:23 +00004103 ReplaceNode(N, getGlobalBaseReg());
4104 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00004105
Hal Finkelb5e9b042014-12-11 22:51:06 +00004106 case ISD::FrameIndex:
Justin Bognerdc8af062016-05-20 21:43:23 +00004107 selectFrameIndex(N, N);
4108 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00004109
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00004110 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004111 SDValue InFlag = N->getOperand(1);
Justin Bognerdc8af062016-05-20 21:43:23 +00004112 ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
4113 N->getOperand(0), InFlag));
4114 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00004115 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004116
Eugene Zelenko8187c192017-01-13 00:58:58 +00004117 case PPCISD::READ_TIME_BASE:
Justin Bognerdc8af062016-05-20 21:43:23 +00004118 ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
4119 MVT::Other, N->getOperand(0)));
4120 return;
Hal Finkelbbdee932014-12-02 22:01:00 +00004121
Hal Finkel13d104b2014-12-11 18:37:52 +00004122 case PPCISD::SRA_ADDZE: {
4123 SDValue N0 = N->getOperand(0);
4124 SDValue ShiftAmt =
4125 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004126 getConstantIntValue(), dl,
4127 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00004128 if (N->getValueType(0) == MVT::i64) {
4129 SDNode *Op =
4130 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
4131 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00004132 CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0),
4133 SDValue(Op, 1));
4134 return;
Hal Finkel13d104b2014-12-11 18:37:52 +00004135 } else {
4136 assert(N->getValueType(0) == MVT::i32 &&
4137 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
4138 SDNode *Op =
4139 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
4140 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00004141 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0),
4142 SDValue(Op, 1));
4143 return;
Chris Lattnerdc664572005-08-25 17:50:06 +00004144 }
Chris Lattner6e184f22005-08-25 22:04:30 +00004145 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004146
Zaara Syeda1110c4d2018-03-15 15:34:41 +00004147 case ISD::STORE: {
4148 // Change TLS initial-exec D-form stores to X-form stores.
4149 StoreSDNode *ST = cast<StoreSDNode>(N);
4150 if (EnableTLSOpt && PPCSubTarget->isELFv2ABI() &&
4151 ST->getAddressingMode() != ISD::PRE_INC)
4152 if (tryTLSXFormStore(ST))
4153 return;
4154 break;
4155 }
Chris Lattnerce645542006-11-10 02:08:47 +00004156 case ISD::LOAD: {
4157 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00004158 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004159 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00004160
Chris Lattnerce645542006-11-10 02:08:47 +00004161 // Normal loads are handled by code generated from the .td file.
Zaara Syeda1110c4d2018-03-15 15:34:41 +00004162 if (LD->getAddressingMode() != ISD::PRE_INC) {
4163 // Change TLS initial-exec D-form loads to X-form loads.
4164 if (EnableTLSOpt && PPCSubTarget->isELFv2ABI())
4165 if (tryTLSXFormLoad(LD))
4166 return;
Chris Lattnerce645542006-11-10 02:08:47 +00004167 break;
Zaara Syeda1110c4d2018-03-15 15:34:41 +00004168 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004169
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004170 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00004171 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00004172 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00004173
Chris Lattner474b5b72006-11-15 19:55:13 +00004174 unsigned Opcode;
4175 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00004176 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00004177 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00004178 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
4179 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004180 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00004181 case MVT::f64: Opcode = PPC::LFDU; break;
4182 case MVT::f32: Opcode = PPC::LFSU; break;
4183 case MVT::i32: Opcode = PPC::LWZU; break;
4184 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
4185 case MVT::i1:
4186 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00004187 }
4188 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00004189 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
4190 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
4191 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004192 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00004193 case MVT::i64: Opcode = PPC::LDU; break;
4194 case MVT::i32: Opcode = PPC::LWZU8; break;
4195 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
4196 case MVT::i1:
4197 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00004198 }
4199 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004200
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004201 SDValue Chain = LD->getChain();
4202 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004203 SDValue Ops[] = { Offset, Base, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00004204 SDNode *MN = CurDAG->getMachineNode(
4205 Opcode, dl, LD->getValueType(0),
4206 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
4207 transferMemOperands(N, MN);
4208 ReplaceNode(N, MN);
4209 return;
Chris Lattnerce645542006-11-10 02:08:47 +00004210 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00004211 unsigned Opcode;
4212 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
4213 if (LD->getValueType(0) != MVT::i64) {
4214 // Handle PPC32 integer and normal FP loads.
4215 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
4216 switch (LoadedVT.getSimpleVT().SimpleTy) {
4217 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00004218 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
4219 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00004220 case MVT::f64: Opcode = PPC::LFDUX; break;
4221 case MVT::f32: Opcode = PPC::LFSUX; break;
4222 case MVT::i32: Opcode = PPC::LWZUX; break;
4223 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
4224 case MVT::i1:
4225 case MVT::i8: Opcode = PPC::LBZUX; break;
4226 }
4227 } else {
4228 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
4229 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
4230 "Invalid sext update load");
4231 switch (LoadedVT.getSimpleVT().SimpleTy) {
4232 default: llvm_unreachable("Invalid PPC load type!");
4233 case MVT::i64: Opcode = PPC::LDUX; break;
4234 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
4235 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
4236 case MVT::i1:
4237 case MVT::i8: Opcode = PPC::LBZUX8; break;
4238 }
4239 }
4240
4241 SDValue Chain = LD->getChain();
4242 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00004243 SDValue Ops[] = { Base, Offset, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00004244 SDNode *MN = CurDAG->getMachineNode(
4245 Opcode, dl, LD->getValueType(0),
4246 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
4247 transferMemOperands(N, MN);
4248 ReplaceNode(N, MN);
4249 return;
Chris Lattnerce645542006-11-10 02:08:47 +00004250 }
4251 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004252
Nate Begemanb3821a32005-08-18 07:30:46 +00004253 case ISD::AND: {
Nate Begemand31efd12006-09-22 05:01:56 +00004254 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00004255 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00004256
Nate Begemanb3821a32005-08-18 07:30:46 +00004257 // If this is an and of a value rotated between 0 and 31 bits and then and'd
4258 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00004259 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00004260 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004261 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004262 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
4263 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004264 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
4265 return;
Nate Begemanb3821a32005-08-18 07:30:46 +00004266 }
Nate Begemand31efd12006-09-22 05:01:56 +00004267 // If this is just a masked value where the input is not handled above, and
4268 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
4269 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00004270 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00004271 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004272 SDValue Val = N->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004273 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
4274 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004275 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
4276 return;
Nate Begemand31efd12006-09-22 05:01:56 +00004277 }
Hal Finkele39526a2012-08-28 02:10:15 +00004278 // If this is a 64-bit zero-extension mask, emit rldicl.
4279 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
4280 isMask_64(Imm64)) {
4281 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00004282 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00004283 SH = 0;
4284
Ehsan Amiri1f31e912016-10-24 15:46:58 +00004285 if (Val.getOpcode() == ISD::ANY_EXTEND) {
4286 auto Op0 = Val.getOperand(0);
4287 if ( Op0.getOpcode() == ISD::SRL &&
4288 isInt32Immediate(Op0.getOperand(1).getNode(), Imm) && Imm <= MB) {
4289
4290 auto ResultType = Val.getNode()->getValueType(0);
4291 auto ImDef = CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl,
4292 ResultType);
4293 SDValue IDVal (ImDef, 0);
4294
4295 Val = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl,
4296 ResultType, IDVal, Op0.getOperand(0),
4297 getI32Imm(1, dl)), 0);
4298 SH = 64 - Imm;
4299 }
4300 }
4301
Hal Finkel22498fa2013-11-20 01:10:15 +00004302 // If the operand is a logical right shift, we can fold it into this
4303 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
4304 // for n <= mb. The right shift is really a left rotate followed by a
4305 // mask, and this mask is a more-restrictive sub-mask of the mask implied
4306 // by the shift.
4307 if (Val.getOpcode() == ISD::SRL &&
4308 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
4309 assert(Imm < 64 && "Illegal shift amount");
4310 Val = Val.getOperand(0);
4311 SH = 64 - Imm;
4312 }
4313
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004314 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004315 CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
4316 return;
Hal Finkele39526a2012-08-28 02:10:15 +00004317 }
Nemanja Ivanovic82d53ed2017-02-24 18:03:16 +00004318 // If this is a negated 64-bit zero-extension mask,
4319 // i.e. the immediate is a sequence of ones from most significant side
4320 // and all zero for reminder, we should use rldicr.
4321 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
4322 isMask_64(~Imm64)) {
4323 SDValue Val = N->getOperand(0);
4324 MB = 63 - countTrailingOnes(~Imm64);
4325 SH = 0;
4326 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
4327 CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops);
4328 return;
4329 }
4330
Nate Begemand31efd12006-09-22 05:01:56 +00004331 // AND X, 0 -> 0, not "rlwinm 32".
4332 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004333 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Justin Bognerdc8af062016-05-20 21:43:23 +00004334 return;
Nate Begemand31efd12006-09-22 05:01:56 +00004335 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00004336 // ISD::OR doesn't get all the bitfield insertion fun.
Hal Finkelb1518d62015-09-05 00:02:59 +00004337 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
4338 // bitfield insert.
Andrew Trickc416ba62010-12-24 04:28:06 +00004339 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00004340 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00004341 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Hal Finkelb1518d62015-09-05 00:02:59 +00004342 // The idea here is to check whether this is equivalent to:
4343 // (c1 & m) | (x & ~m)
4344 // where m is a run-of-ones mask. The logic here is that, for each bit in
4345 // c1 and c2:
4346 // - if both are 1, then the output will be 1.
4347 // - if both are 0, then the output will be 0.
4348 // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
4349 // come from x.
4350 // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
4351 // be 0.
4352 // If that last condition is never the case, then we can form m from the
4353 // bits that are the same between c1 and c2.
Chris Lattner20c88df2006-01-05 18:32:49 +00004354 unsigned MB, ME;
Hal Finkelb1518d62015-09-05 00:02:59 +00004355 if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004356 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00004357 N->getOperand(0).getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004358 getI32Imm(0, dl), getI32Imm(MB, dl),
4359 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004360 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
4361 return;
Nate Begeman9aea6e42005-12-24 01:00:15 +00004362 }
4363 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004364
Chris Lattner1de57062005-09-29 23:33:31 +00004365 // Other cases are autogenerated.
4366 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00004367 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00004368 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00004369 if (N->getValueType(0) == MVT::i32)
Justin Bognerdc8af062016-05-20 21:43:23 +00004370 if (tryBitfieldInsert(N))
4371 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00004372
Lei Huang31710412017-07-07 21:12:35 +00004373 int16_t Imm;
Hal Finkelb5e9b042014-12-11 22:51:06 +00004374 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
4375 isIntS16Immediate(N->getOperand(1), Imm)) {
Craig Topperd0af7e82017-04-28 05:31:46 +00004376 KnownBits LHSKnown;
4377 CurDAG->computeKnownBits(N->getOperand(0), LHSKnown);
Hal Finkelb5e9b042014-12-11 22:51:06 +00004378
4379 // If this is equivalent to an add, then we can fold it with the
4380 // FrameIndex calculation.
Craig Topperd0af7e82017-04-28 05:31:46 +00004381 if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
Justin Bognerdc8af062016-05-20 21:43:23 +00004382 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
4383 return;
4384 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00004385 }
4386
Hiroshi Inouecc555bd2017-08-23 08:55:18 +00004387 // OR with a 32-bit immediate can be handled by ori + oris
4388 // without creating an immediate in a GPR.
4389 uint64_t Imm64 = 0;
4390 bool IsPPC64 = PPCSubTarget->isPPC64();
4391 if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) &&
4392 (Imm64 & ~0xFFFFFFFFuLL) == 0) {
4393 // If ImmHi (ImmHi) is zero, only one ori (oris) is generated later.
4394 uint64_t ImmHi = Imm64 >> 16;
4395 uint64_t ImmLo = Imm64 & 0xFFFF;
4396 if (ImmHi != 0 && ImmLo != 0) {
4397 SDNode *Lo = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
4398 N->getOperand(0),
4399 getI16Imm(ImmLo, dl));
4400 SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)};
4401 CurDAG->SelectNodeTo(N, PPC::ORIS8, MVT::i64, Ops1);
4402 return;
4403 }
4404 }
4405
Chris Lattner1de57062005-09-29 23:33:31 +00004406 // Other cases are autogenerated.
4407 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00004408 }
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00004409 case ISD::XOR: {
Hiroshi Inouecc555bd2017-08-23 08:55:18 +00004410 // XOR with a 32-bit immediate can be handled by xori + xoris
4411 // without creating an immediate in a GPR.
4412 uint64_t Imm64 = 0;
4413 bool IsPPC64 = PPCSubTarget->isPPC64();
4414 if (IsPPC64 && isInt64Immediate(N->getOperand(1), Imm64) &&
4415 (Imm64 & ~0xFFFFFFFFuLL) == 0) {
4416 // If ImmHi (ImmHi) is zero, only one xori (xoris) is generated later.
4417 uint64_t ImmHi = Imm64 >> 16;
4418 uint64_t ImmLo = Imm64 & 0xFFFF;
4419 if (ImmHi != 0 && ImmLo != 0) {
4420 SDNode *Lo = CurDAG->getMachineNode(PPC::XORI8, dl, MVT::i64,
4421 N->getOperand(0),
4422 getI16Imm(ImmLo, dl));
4423 SDValue Ops1[] = { SDValue(Lo, 0), getI16Imm(ImmHi, dl)};
4424 CurDAG->SelectNodeTo(N, PPC::XORIS8, MVT::i64, Ops1);
4425 return;
4426 }
4427 }
4428
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00004429 break;
4430 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00004431 case ISD::ADD: {
Lei Huang31710412017-07-07 21:12:35 +00004432 int16_t Imm;
Hal Finkelb5e9b042014-12-11 22:51:06 +00004433 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
Justin Bognerdc8af062016-05-20 21:43:23 +00004434 isIntS16Immediate(N->getOperand(1), Imm)) {
4435 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
4436 return;
4437 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00004438
4439 break;
4440 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00004441 case ISD::SHL: {
4442 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00004443 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00004444 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004445 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 getI32Imm(SH, dl), getI32Imm(MB, dl),
4447 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004448 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
4449 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00004450 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004451
Nate Begeman9f3c26c2005-10-19 18:42:01 +00004452 // Other cases are autogenerated.
4453 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00004454 }
4455 case ISD::SRL: {
4456 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00004457 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00004458 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004459 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004460 getI32Imm(SH, dl), getI32Imm(MB, dl),
4461 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004462 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
4463 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00004464 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004465
Nate Begeman9f3c26c2005-10-19 18:42:01 +00004466 // Other cases are autogenerated.
4467 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00004468 }
Hal Finkel940ab932014-02-28 00:27:01 +00004469 // FIXME: Remove this once the ANDI glue bug is fixed:
4470 case PPCISD::ANDIo_1_EQ_BIT:
4471 case PPCISD::ANDIo_1_GT_BIT: {
4472 if (!ANDIGlueBug)
4473 break;
4474
4475 EVT InVT = N->getOperand(0).getValueType();
4476 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
4477 "Invalid input type for ANDIo_1_EQ_BIT");
4478
4479 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
4480 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
4481 N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004482 CurDAG->getTargetConstant(1, dl, InVT)),
4483 0);
Hal Finkel940ab932014-02-28 00:27:01 +00004484 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
4485 SDValue SRIdxVal =
4486 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004487 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00004488
Justin Bognerdc8af062016-05-20 21:43:23 +00004489 CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg,
4490 SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */);
4491 return;
Hal Finkel940ab932014-02-28 00:27:01 +00004492 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00004493 case ISD::SELECT_CC: {
4494 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00004495 EVT PtrVT =
4496 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00004497 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00004498
Hal Finkel940ab932014-02-28 00:27:01 +00004499 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004500 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00004501 N->getOperand(0).getValueType() == MVT::i1)
4502 break;
4503
Chris Lattner97b3da12006-06-27 00:04:13 +00004504 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00004505 if (!isPPC64)
4506 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
4507 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
4508 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
4509 if (N1C->isNullValue() && N3C->isNullValue() &&
4510 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
4511 // FIXME: Implement this optzn for PPC64.
4512 N->getValueType(0) == MVT::i32) {
4513 SDNode *Tmp =
4514 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004515 N->getOperand(0), getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00004516 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0),
4517 N->getOperand(0), SDValue(Tmp, 1));
4518 return;
Roman Divacky254f8212011-06-20 15:28:39 +00004519 }
Chris Lattner9b577f12005-08-26 21:23:58 +00004520
Dale Johannesenab8e4422009-02-06 19:16:40 +00004521 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00004522
4523 if (N->getValueType(0) == MVT::i1) {
4524 // An i1 select is: (c & t) | (!c & f).
4525 bool Inv;
4526 unsigned Idx = getCRIdxForSetCC(CC, Inv);
4527
4528 unsigned SRI;
4529 switch (Idx) {
4530 default: llvm_unreachable("Invalid CC index");
4531 case 0: SRI = PPC::sub_lt; break;
4532 case 1: SRI = PPC::sub_gt; break;
4533 case 2: SRI = PPC::sub_eq; break;
4534 case 3: SRI = PPC::sub_un; break;
4535 }
4536
4537 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
4538
4539 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
4540 CCBit, CCBit), 0);
4541 SDValue C = Inv ? NotCCBit : CCBit,
4542 NotC = Inv ? CCBit : NotCCBit;
4543
4544 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
4545 C, N->getOperand(2)), 0);
4546 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
4547 NotC, N->getOperand(3)), 0);
4548
Justin Bognerdc8af062016-05-20 21:43:23 +00004549 CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
4550 return;
Hal Finkel940ab932014-02-28 00:27:01 +00004551 }
4552
Chris Lattner8c6a41e2006-11-17 22:10:59 +00004553 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00004554
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00004555 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00004556 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00004557 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00004558 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00004559 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00004560 else if (N->getValueType(0) == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00004561 if (PPCSubTarget->hasP8Vector())
4562 SelectCCOp = PPC::SELECT_CC_VSSRC;
4563 else
4564 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00004565 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00004566 if (PPCSubTarget->hasVSX())
4567 SelectCCOp = PPC::SELECT_CC_VSFRC;
4568 else
4569 SelectCCOp = PPC::SELECT_CC_F8;
Stefan Pintilie3d763262018-07-09 13:36:14 +00004570 else if (N->getValueType(0) == MVT::f128)
4571 SelectCCOp = PPC::SELECT_CC_F16;
Hal Finkelc93a9a22015-02-25 01:06:45 +00004572 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
4573 SelectCCOp = PPC::SELECT_CC_QFRC;
4574 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
4575 SelectCCOp = PPC::SELECT_CC_QSRC;
4576 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
4577 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00004578 else if (N->getValueType(0) == MVT::v2f64 ||
4579 N->getValueType(0) == MVT::v2i64)
4580 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00004581 else
4582 SelectCCOp = PPC::SELECT_CC_VRRC;
4583
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004584 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004585 getI32Imm(BROpc, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004586 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
4587 return;
Chris Lattnerbec817c2005-08-26 18:46:49 +00004588 }
Hal Finkel732f0f72014-03-26 12:49:28 +00004589 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00004590 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00004591 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004592 CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
4593 return;
Hal Finkel732f0f72014-03-26 12:49:28 +00004594 }
Hal Finkel732f0f72014-03-26 12:49:28 +00004595 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00004596
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004597 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00004598 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004599 N->getValueType(0) == MVT::v2i64)) {
4600 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Kyle Butt015f4fc2015-12-02 18:53:33 +00004601
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004602 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
4603 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
4604 unsigned DM[2];
4605
4606 for (int i = 0; i < 2; ++i)
4607 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
4608 DM[i] = 0;
4609 else
4610 DM[i] = 1;
4611
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004612 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
4613 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4614 isa<LoadSDNode>(Op1.getOperand(0))) {
4615 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
4616 SDValue Base, Offset;
4617
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +00004618 if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() &&
Bill Schmidt048cc972015-10-14 20:45:00 +00004619 (LD->getMemoryVT() == MVT::f64 ||
4620 LD->getMemoryVT() == MVT::i64) &&
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004621 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
4622 SDValue Chain = LD->getChain();
4623 SDValue Ops[] = { Base, Offset, Chain };
Sean Fertile3c8c3852017-01-26 18:59:15 +00004624 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
4625 MemOp[0] = LD->getMemOperand();
Benjamin Kramer58dadd52017-04-20 18:29:14 +00004626 SDNode *NewN = CurDAG->SelectNodeTo(N, PPC::LXVDSX,
4627 N->getValueType(0), Ops);
Sean Fertile3c8c3852017-01-26 18:59:15 +00004628 cast<MachineSDNode>(NewN)->setMemRefs(MemOp, MemOp + 1);
Justin Bognerdc8af062016-05-20 21:43:23 +00004629 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004630 }
4631 }
4632
Bill Schmidtae94f112015-07-01 19:40:07 +00004633 // For little endian, we must swap the input operands and adjust
4634 // the mask elements (reverse and invert them).
4635 if (PPCSubTarget->isLittleEndian()) {
4636 std::swap(Op1, Op2);
4637 unsigned tmp = DM[0];
4638 DM[0] = 1 - DM[1];
4639 DM[1] = 1 - tmp;
4640 }
4641
4642 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
4643 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004644 SDValue Ops[] = { Op1, Op2, DMV };
Justin Bognerdc8af062016-05-20 21:43:23 +00004645 CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
4646 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00004647 }
4648
4649 break;
Hal Finkel25c19922013-05-15 21:37:41 +00004650 case PPCISD::BDNZ:
4651 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00004652 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00004653 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004654 CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ
4655 ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
4656 : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
4657 MVT::Other, Ops);
4658 return;
Hal Finkel25c19922013-05-15 21:37:41 +00004659 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00004660 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00004661 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00004662 // Op #1 is the PPC::PRED_* number.
4663 // Op #2 is the CR#
4664 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00004665 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00004666 // Prevent PPC::PRED_* from being selected into LI.
Hal Finkel65539e32015-12-12 00:32:00 +00004667 unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
4668 if (EnableBranchHint)
4669 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3));
4670
4671 SDValue Pred = getI32Imm(PCC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004672 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00004673 N->getOperand(0), N->getOperand(4) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004674 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
4675 return;
Chris Lattnerbe9377a2006-11-17 22:37:34 +00004676 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00004677 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00004678 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00004679 unsigned PCC = getPredicateForSetCC(CC);
4680
4681 if (N->getOperand(2).getValueType() == MVT::i1) {
4682 unsigned Opc;
4683 bool Swap;
4684 switch (PCC) {
4685 default: llvm_unreachable("Unexpected Boolean-operand predicate");
4686 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
4687 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
4688 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
4689 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
4690 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
4691 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
4692 }
4693
4694 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
4695 N->getOperand(Swap ? 3 : 2),
4696 N->getOperand(Swap ? 2 : 3)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00004697 CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4),
4698 N->getOperand(0));
4699 return;
Hal Finkel940ab932014-02-28 00:27:01 +00004700 }
4701
Hal Finkel65539e32015-12-12 00:32:00 +00004702 if (EnableBranchHint)
4703 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4));
4704
Dale Johannesenab8e4422009-02-06 19:16:40 +00004705 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004706 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00004707 N->getOperand(4), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00004708 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
4709 return;
Chris Lattner2a1823d2005-08-21 18:50:37 +00004710 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00004711 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00004712 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004713 SDValue Chain = N->getOperand(0);
4714 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00004715 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00004716 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00004717 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00004718 Chain), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00004719 CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
4720 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00004721 }
Bill Schmidt34627e32012-11-27 17:35:46 +00004722 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00004723 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
4724 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00004725 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
4726 SDValue GA = N->getOperand(0);
Justin Bognerdc8af062016-05-20 21:43:23 +00004727 SDNode *MN = CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
4728 N->getOperand(1));
4729 transferMemOperands(N, MN);
4730 ReplaceNode(N, MN);
4731 return;
Justin Hibbits3476db42014-08-28 04:40:55 +00004732 }
Bill Schmidt34627e32012-11-27 17:35:46 +00004733
Bill Schmidt27917782013-02-21 17:12:27 +00004734 // For medium and large code model, we generate two instructions as
4735 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00004736 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00004737 CodeModel::Model CModel = TM.getCodeModel();
4738 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00004739 break;
4740
Bill Schmidt5d82f092014-06-16 21:36:02 +00004741 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
Eric Christopherc1808362015-11-20 20:51:31 +00004742 // If it must be toc-referenced according to PPCSubTarget, we generate:
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00004743 // LDtocL(@sym, ADDIStocHA(%x2, @sym))
Bill Schmidt34627e32012-11-27 17:35:46 +00004744 // Otherwise we generate:
Francis Visoiu Mistrih5df3bbf2017-12-14 10:03:09 +00004745 // ADDItocL(ADDIStocHA(%x2, @sym), @sym)
Bill Schmidt34627e32012-11-27 17:35:46 +00004746 SDValue GA = N->getOperand(0);
4747 SDValue TOCbase = N->getOperand(1);
4748 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00004749 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00004750
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00004751 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
Justin Bognerdc8af062016-05-20 21:43:23 +00004752 CModel == CodeModel::Large) {
4753 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
4754 SDValue(Tmp, 0));
4755 transferMemOperands(N, MN);
4756 ReplaceNode(N, MN);
4757 return;
4758 }
Bill Schmidt34627e32012-11-27 17:35:46 +00004759
4760 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
Eric Christopherc1808362015-11-20 20:51:31 +00004761 const GlobalValue *GV = G->getGlobal();
4762 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
4763 if (GVFlags & PPCII::MO_NLP_FLAG) {
Justin Bognerdc8af062016-05-20 21:43:23 +00004764 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
4765 SDValue(Tmp, 0));
4766 transferMemOperands(N, MN);
4767 ReplaceNode(N, MN);
4768 return;
Eric Christopherc1808362015-11-20 20:51:31 +00004769 }
Bill Schmidt34627e32012-11-27 17:35:46 +00004770 }
4771
Justin Bognerdc8af062016-05-20 21:43:23 +00004772 ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
4773 SDValue(Tmp, 0), GA));
4774 return;
Bill Schmidt34627e32012-11-27 17:35:46 +00004775 }
Eugene Zelenko8187c192017-01-13 00:58:58 +00004776 case PPCISD::PPC32_PICGOT:
Hal Finkel7c8ae532014-07-25 17:47:22 +00004777 // Generate a PIC-safe GOT reference.
4778 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
4779 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
Justin Bognerdc8af062016-05-20 21:43:23 +00004780 CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT,
4781 PPCLowering->getPointerTy(CurDAG->getDataLayout()),
4782 MVT::i32);
4783 return;
Eugene Zelenko8187c192017-01-13 00:58:58 +00004784
Bill Schmidt51e79512013-02-20 15:50:31 +00004785 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004786 // This expands into one of three sequences, depending on whether
4787 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00004788 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
4789 isa<ConstantSDNode>(N->getOperand(1)) &&
4790 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004791
4792 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00004793 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004794 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00004795 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004796
Bill Schmidt51e79512013-02-20 15:50:31 +00004797 if (EltSize == 1) {
4798 Opc1 = PPC::VSPLTISB;
4799 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004800 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00004801 VT = MVT::v16i8;
4802 } else if (EltSize == 2) {
4803 Opc1 = PPC::VSPLTISH;
4804 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004805 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00004806 VT = MVT::v8i16;
4807 } else {
4808 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
4809 Opc1 = PPC::VSPLTISW;
4810 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004811 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00004812 VT = MVT::v4i32;
4813 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004814
4815 if ((Elt & 1) == 0) {
4816 // Elt is even, in the range [-32,-18] + [16,30].
4817 //
4818 // Convert: VADD_SPLAT elt, size
4819 // Into: tmp = VSPLTIS[BHW] elt
4820 // VADDU[BHW]M tmp, tmp
4821 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004822 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004823 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
4824 SDValue TmpVal = SDValue(Tmp, 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00004825 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal));
4826 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004827 } else if (Elt > 0) {
4828 // Elt is odd and positive, in the range [17,31].
4829 //
4830 // Convert: VADD_SPLAT elt, size
4831 // Into: tmp1 = VSPLTIS[BHW] elt-16
4832 // tmp2 = VSPLTIS[BHW] -16
4833 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004834 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004835 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004836 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004837 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00004838 ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
4839 SDValue(Tmp2, 0)));
4840 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004841 } else {
4842 // Elt is odd and negative, in the range [-31,-17].
4843 //
4844 // Convert: VADD_SPLAT elt, size
4845 // Into: tmp1 = VSPLTIS[BHW] elt+16
4846 // tmp2 = VSPLTIS[BHW] -16
4847 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004848 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004849 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004850 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004851 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00004852 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
4853 SDValue(Tmp2, 0)));
4854 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00004855 }
Bill Schmidt51e79512013-02-20 15:50:31 +00004856 }
Hiroshi Inoue33486782018-04-21 09:32:17 +00004857 case ISD::ABS: {
4858 assert(PPCSubTarget->hasP9Vector() && "ABS is supported with P9 Vector");
4859
4860 // For vector absolute difference, we use VABSDUW instruction of POWER9.
4861 // Since VABSDU instructions are for unsigned integers, we need adjustment
4862 // for signed integers.
4863 // For abs(sub(a, b)), we generate VABSDUW(a+0x80000000, b+0x80000000).
4864 // Otherwise, abs(sub(-1, 0)) returns 0xFFFFFFFF(=-1) instead of 1.
4865 // For abs(a), we generate VABSDUW(a+0x80000000, 0x80000000).
4866 EVT VecVT = N->getOperand(0).getValueType();
4867 SDNode *AbsOp = nullptr;
4868 unsigned AbsOpcode;
4869
4870 if (VecVT == MVT::v4i32)
4871 AbsOpcode = PPC::VABSDUW;
4872 else if (VecVT == MVT::v8i16)
4873 AbsOpcode = PPC::VABSDUH;
4874 else if (VecVT == MVT::v16i8)
4875 AbsOpcode = PPC::VABSDUB;
4876 else
4877 llvm_unreachable("Unsupported vector data type for ISD::ABS");
4878
4879 // Even for signed integers, we can skip adjustment if all values are
4880 // known to be positive (as signed integer) due to zero-extended inputs.
4881 if (N->getOperand(0).getOpcode() == ISD::SUB &&
4882 N->getOperand(0)->getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4883 N->getOperand(0)->getOperand(1).getOpcode() == ISD::ZERO_EXTEND) {
4884 AbsOp = CurDAG->getMachineNode(AbsOpcode, dl, VecVT,
4885 SDValue(N->getOperand(0)->getOperand(0)),
4886 SDValue(N->getOperand(0)->getOperand(1)));
4887 ReplaceNode(N, AbsOp);
4888 return;
4889 }
4890 if (N->getOperand(0).getOpcode() == ISD::SUB) {
4891 SDValue SubVal = N->getOperand(0);
4892 SDNode *Op0 = flipSignBit(SubVal->getOperand(0));
4893 SDNode *Op1 = flipSignBit(SubVal->getOperand(1));
4894 AbsOp = CurDAG->getMachineNode(AbsOpcode, dl, VecVT,
4895 SDValue(Op0, 0), SDValue(Op1, 0));
4896 }
4897 else {
4898 SDNode *Op1 = nullptr;
4899 SDNode *Op0 = flipSignBit(N->getOperand(0), &Op1);
4900 AbsOp = CurDAG->getMachineNode(AbsOpcode, dl, VecVT, SDValue(Op0, 0),
4901 SDValue(Op1, 0));
4902 }
4903 ReplaceNode(N, AbsOp);
4904 return;
4905 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00004906 }
Andrew Trickc416ba62010-12-24 04:28:06 +00004907
Justin Bognerdc8af062016-05-20 21:43:23 +00004908 SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00004909}
4910
Hal Finkel4edc66b2015-01-03 01:16:37 +00004911// If the target supports the cmpb instruction, do the idiom recognition here.
4912// We don't do this as a DAG combine because we don't want to do it as nodes
4913// are being combined (because we might miss part of the eventual idiom). We
4914// don't want to do it during instruction selection because we want to reuse
4915// the logic for lowering the masking operations already part of the
4916// instruction selector.
4917SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
4918 SDLoc dl(N);
4919
4920 assert(N->getOpcode() == ISD::OR &&
4921 "Only OR nodes are supported for CMPB");
4922
4923 SDValue Res;
4924 if (!PPCSubTarget->hasCMPB())
4925 return Res;
4926
4927 if (N->getValueType(0) != MVT::i32 &&
4928 N->getValueType(0) != MVT::i64)
4929 return Res;
4930
4931 EVT VT = N->getValueType(0);
4932
4933 SDValue RHS, LHS;
Eugene Zelenko8187c192017-01-13 00:58:58 +00004934 bool BytesFound[8] = {false, false, false, false, false, false, false, false};
Hal Finkel4edc66b2015-01-03 01:16:37 +00004935 uint64_t Mask = 0, Alt = 0;
4936
4937 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
4938 uint64_t &Mask, uint64_t &Alt,
4939 SDValue &LHS, SDValue &RHS) {
4940 if (O.getOpcode() != ISD::SELECT_CC)
4941 return false;
4942 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
4943
4944 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
4945 !isa<ConstantSDNode>(O.getOperand(3)))
4946 return false;
4947
4948 uint64_t PM = O.getConstantOperandVal(2);
4949 uint64_t PAlt = O.getConstantOperandVal(3);
4950 for (b = 0; b < 8; ++b) {
4951 uint64_t Mask = UINT64_C(0xFF) << (8*b);
4952 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
4953 break;
4954 }
4955
4956 if (b == 8)
4957 return false;
4958 Mask |= PM;
4959 Alt |= PAlt;
4960
4961 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
4962 O.getConstantOperandVal(1) != 0) {
4963 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
4964 if (Op0.getOpcode() == ISD::TRUNCATE)
4965 Op0 = Op0.getOperand(0);
4966 if (Op1.getOpcode() == ISD::TRUNCATE)
4967 Op1 = Op1.getOperand(0);
4968
4969 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
4970 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
4971 isa<ConstantSDNode>(Op0.getOperand(1))) {
4972
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00004973 unsigned Bits = Op0.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00004974 if (b != Bits/8-1)
4975 return false;
4976 if (Op0.getConstantOperandVal(1) != Bits-8)
4977 return false;
4978
4979 LHS = Op0.getOperand(0);
4980 RHS = Op1.getOperand(0);
4981 return true;
4982 }
4983
4984 // When we have small integers (i16 to be specific), the form present
4985 // post-legalization uses SETULT in the SELECT_CC for the
4986 // higher-order byte, depending on the fact that the
4987 // even-higher-order bytes are known to all be zero, for example:
4988 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
4989 // (so when the second byte is the same, because all higher-order
4990 // bits from bytes 3 and 4 are known to be zero, the result of the
4991 // xor can be at most 255)
4992 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
4993 isa<ConstantSDNode>(O.getOperand(1))) {
4994
4995 uint64_t ULim = O.getConstantOperandVal(1);
4996 if (ULim != (UINT64_C(1) << b*8))
4997 return false;
4998
4999 // Now we need to make sure that the upper bytes are known to be
5000 // zero.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005001 unsigned Bits = Op0.getValueSizeInBits();
5002 if (!CurDAG->MaskedValueIsZero(
5003 Op0, APInt::getHighBitsSet(Bits, Bits - (b + 1) * 8)))
Hal Finkel4edc66b2015-01-03 01:16:37 +00005004 return false;
Kyle Butt015f4fc2015-12-02 18:53:33 +00005005
Hal Finkel4edc66b2015-01-03 01:16:37 +00005006 LHS = Op0.getOperand(0);
5007 RHS = Op0.getOperand(1);
5008 return true;
5009 }
5010
5011 return false;
5012 }
5013
5014 if (CC != ISD::SETEQ)
5015 return false;
5016
5017 SDValue Op = O.getOperand(0);
5018 if (Op.getOpcode() == ISD::AND) {
5019 if (!isa<ConstantSDNode>(Op.getOperand(1)))
5020 return false;
5021 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
5022 return false;
5023
5024 SDValue XOR = Op.getOperand(0);
5025 if (XOR.getOpcode() == ISD::TRUNCATE)
5026 XOR = XOR.getOperand(0);
5027 if (XOR.getOpcode() != ISD::XOR)
5028 return false;
5029
5030 LHS = XOR.getOperand(0);
5031 RHS = XOR.getOperand(1);
5032 return true;
5033 } else if (Op.getOpcode() == ISD::SRL) {
5034 if (!isa<ConstantSDNode>(Op.getOperand(1)))
5035 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005036 unsigned Bits = Op.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00005037 if (b != Bits/8-1)
5038 return false;
5039 if (Op.getConstantOperandVal(1) != Bits-8)
5040 return false;
5041
5042 SDValue XOR = Op.getOperand(0);
5043 if (XOR.getOpcode() == ISD::TRUNCATE)
5044 XOR = XOR.getOperand(0);
5045 if (XOR.getOpcode() != ISD::XOR)
5046 return false;
5047
5048 LHS = XOR.getOperand(0);
5049 RHS = XOR.getOperand(1);
5050 return true;
5051 }
5052
5053 return false;
5054 };
5055
5056 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
5057 while (!Queue.empty()) {
5058 SDValue V = Queue.pop_back_val();
5059
5060 for (const SDValue &O : V.getNode()->ops()) {
5061 unsigned b;
5062 uint64_t M = 0, A = 0;
5063 SDValue OLHS, ORHS;
5064 if (O.getOpcode() == ISD::OR) {
5065 Queue.push_back(O);
5066 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
5067 if (!LHS) {
5068 LHS = OLHS;
5069 RHS = ORHS;
5070 BytesFound[b] = true;
5071 Mask |= M;
5072 Alt |= A;
5073 } else if ((LHS == ORHS && RHS == OLHS) ||
5074 (RHS == ORHS && LHS == OLHS)) {
5075 BytesFound[b] = true;
5076 Mask |= M;
5077 Alt |= A;
5078 } else {
5079 return Res;
5080 }
5081 } else {
5082 return Res;
5083 }
5084 }
5085 }
5086
5087 unsigned LastB = 0, BCnt = 0;
5088 for (unsigned i = 0; i < 8; ++i)
5089 if (BytesFound[LastB]) {
5090 ++BCnt;
5091 LastB = i;
5092 }
5093
5094 if (!LastB || BCnt < 2)
5095 return Res;
5096
5097 // Because we'll be zero-extending the output anyway if don't have a specific
5098 // value for each input byte (via the Mask), we can 'anyext' the inputs.
5099 if (LHS.getValueType() != VT) {
5100 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
5101 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
5102 }
5103
5104 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
5105
5106 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
5107 if (NonTrivialMask && !Alt) {
5108 // Res = Mask & CMPB
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005109 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
5110 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00005111 } else if (Alt) {
5112 // Res = (CMPB & Mask) | (~CMPB & Alt)
5113 // Which, as suggested here:
5114 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
5115 // can be written as:
5116 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
5117 // useful because the (Alt ^ Mask) can be pre-computed.
5118 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005119 CurDAG->getConstant(Mask ^ Alt, dl, VT));
5120 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
5121 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00005122 }
5123
5124 return Res;
5125}
5126
Hal Finkel200d2ad2015-01-05 21:10:24 +00005127// When CR bit registers are enabled, an extension of an i1 variable to a i32
5128// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
5129// involves constant materialization of a 0 or a 1 or both. If the result of
5130// the extension is then operated upon by some operator that can be constant
5131// folded with a constant 0 or 1, and that constant can be materialized using
5132// only one instruction (like a zero or one), then we should fold in those
5133// operations with the select.
5134void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
5135 if (!PPCSubTarget->useCRBits())
5136 return;
5137
5138 if (N->getOpcode() != ISD::ZERO_EXTEND &&
5139 N->getOpcode() != ISD::SIGN_EXTEND &&
5140 N->getOpcode() != ISD::ANY_EXTEND)
5141 return;
5142
5143 if (N->getOperand(0).getValueType() != MVT::i1)
5144 return;
5145
5146 if (!N->hasOneUse())
5147 return;
5148
5149 SDLoc dl(N);
5150 EVT VT = N->getValueType(0);
5151 SDValue Cond = N->getOperand(0);
5152 SDValue ConstTrue =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005153 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
5154 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00005155
5156 do {
5157 SDNode *User = *N->use_begin();
5158 if (User->getNumOperands() != 2)
5159 break;
5160
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005161 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00005162 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
5163 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
5164 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
5165
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005166 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00005167 User->getValueType(0),
5168 O0.getNode(), O1.getNode());
5169 };
5170
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00005171 // FIXME: When the semantics of the interaction between select and undef
5172 // are clearly defined, it may turn out to be unnecessary to break here.
Hal Finkel200d2ad2015-01-05 21:10:24 +00005173 SDValue TrueRes = TryFold(ConstTrue);
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00005174 if (!TrueRes || TrueRes.isUndef())
Hal Finkel200d2ad2015-01-05 21:10:24 +00005175 break;
5176 SDValue FalseRes = TryFold(ConstFalse);
Nemanja Ivanovic845a7962017-07-05 04:51:29 +00005177 if (!FalseRes || FalseRes.isUndef())
Hal Finkel200d2ad2015-01-05 21:10:24 +00005178 break;
5179
5180 // For us to materialize these using one instruction, we must be able to
5181 // represent them as signed 16-bit integers.
5182 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
5183 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
5184 if (!isInt<16>(True) || !isInt<16>(False))
5185 break;
5186
5187 // We can replace User with a new SELECT node, and try again to see if we
5188 // can fold the select with its user.
5189 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
5190 N = User;
5191 ConstTrue = TrueRes;
5192 ConstFalse = FalseRes;
5193 } while (N->hasOneUse());
5194}
5195
Hal Finkel4edc66b2015-01-03 01:16:37 +00005196void PPCDAGToDAGISel::PreprocessISelDAG() {
Lei Huang716103f2018-05-29 13:38:56 +00005197 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
Hal Finkel4edc66b2015-01-03 01:16:37 +00005198
5199 bool MadeChange = false;
5200 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00005201 SDNode *N = &*--Position;
Hal Finkel4edc66b2015-01-03 01:16:37 +00005202 if (N->use_empty())
5203 continue;
5204
5205 SDValue Res;
5206 switch (N->getOpcode()) {
5207 default: break;
5208 case ISD::OR:
5209 Res = combineToCMPB(N);
5210 break;
5211 }
5212
Hal Finkel200d2ad2015-01-05 21:10:24 +00005213 if (!Res)
5214 foldBoolExts(Res, N);
5215
Hal Finkel4edc66b2015-01-03 01:16:37 +00005216 if (Res) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00005217 LLVM_DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
5218 LLVM_DEBUG(N->dump(CurDAG));
5219 LLVM_DEBUG(dbgs() << "\nNew: ");
5220 LLVM_DEBUG(Res.getNode()->dump(CurDAG));
5221 LLVM_DEBUG(dbgs() << "\n");
Hal Finkel4edc66b2015-01-03 01:16:37 +00005222
5223 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
5224 MadeChange = true;
5225 }
5226 }
5227
5228 if (MadeChange)
5229 CurDAG->RemoveDeadNodes();
5230}
5231
Hal Finkel860fa902014-01-02 22:09:39 +00005232/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005233/// on the DAG representation.
5234void PPCDAGToDAGISel::PostprocessISelDAG() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005235 // Skip peepholes at -O0.
5236 if (TM.getOptLevel() == CodeGenOpt::None)
5237 return;
5238
Hal Finkel940ab932014-02-28 00:27:01 +00005239 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00005240 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00005241 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00005242}
5243
Hal Finkelb9989152014-02-28 06:11:16 +00005244// Check if all users of this node will become isel where the second operand
5245// is the constant zero. If this is so, and if we can negate the condition,
5246// then we can flip the true and false operands. This will allow the zero to
5247// be folded with the isel so that we don't need to materialize a register
5248// containing zero.
5249bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
Hal Finkelb9989152014-02-28 06:11:16 +00005250 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5251 UI != UE; ++UI) {
5252 SDNode *User = *UI;
5253 if (!User->isMachineOpcode())
5254 return false;
5255 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
5256 User->getMachineOpcode() != PPC::SELECT_I8)
5257 return false;
5258
5259 SDNode *Op2 = User->getOperand(2).getNode();
5260 if (!Op2->isMachineOpcode())
5261 return false;
5262
5263 if (Op2->getMachineOpcode() != PPC::LI &&
5264 Op2->getMachineOpcode() != PPC::LI8)
5265 return false;
5266
5267 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
5268 if (!C)
5269 return false;
5270
5271 if (!C->isNullValue())
5272 return false;
5273 }
5274
5275 return true;
5276}
5277
5278void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
5279 SmallVector<SDNode *, 4> ToReplace;
5280 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5281 UI != UE; ++UI) {
5282 SDNode *User = *UI;
5283 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
5284 User->getMachineOpcode() == PPC::SELECT_I8) &&
5285 "Must have all select users");
5286 ToReplace.push_back(User);
5287 }
5288
5289 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
5290 UE = ToReplace.end(); UI != UE; ++UI) {
5291 SDNode *User = *UI;
5292 SDNode *ResNode =
5293 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
5294 User->getValueType(0), User->getOperand(0),
5295 User->getOperand(2),
5296 User->getOperand(1));
5297
Nicola Zaghend34e60c2018-05-14 12:53:11 +00005298 LLVM_DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
5299 LLVM_DEBUG(User->dump(CurDAG));
5300 LLVM_DEBUG(dbgs() << "\nNew: ");
5301 LLVM_DEBUG(ResNode->dump(CurDAG));
5302 LLVM_DEBUG(dbgs() << "\n");
Hal Finkelb9989152014-02-28 06:11:16 +00005303
Nicola Zaghend34e60c2018-05-14 12:53:11 +00005304 ReplaceUses(User, ResNode);
Hal Finkelb9989152014-02-28 06:11:16 +00005305 }
5306}
5307
Eric Christopher02e18042014-05-14 00:31:15 +00005308void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00005309 bool IsModified;
5310 do {
5311 IsModified = false;
Pete Cooper65c69402015-07-14 22:10:54 +00005312 for (SDNode &Node : CurDAG->allnodes()) {
5313 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node);
Hal Finkel940ab932014-02-28 00:27:01 +00005314 if (!MachineNode || MachineNode->use_empty())
5315 continue;
5316 SDNode *ResNode = MachineNode;
5317
5318 bool Op1Set = false, Op1Unset = false,
5319 Op1Not = false,
5320 Op2Set = false, Op2Unset = false,
5321 Op2Not = false;
5322
5323 unsigned Opcode = MachineNode->getMachineOpcode();
5324 switch (Opcode) {
5325 default: break;
5326 case PPC::CRAND:
5327 case PPC::CRNAND:
5328 case PPC::CROR:
5329 case PPC::CRXOR:
5330 case PPC::CRNOR:
5331 case PPC::CREQV:
5332 case PPC::CRANDC:
5333 case PPC::CRORC: {
5334 SDValue Op = MachineNode->getOperand(1);
5335 if (Op.isMachineOpcode()) {
5336 if (Op.getMachineOpcode() == PPC::CRSET)
5337 Op2Set = true;
5338 else if (Op.getMachineOpcode() == PPC::CRUNSET)
5339 Op2Unset = true;
5340 else if (Op.getMachineOpcode() == PPC::CRNOR &&
5341 Op.getOperand(0) == Op.getOperand(1))
5342 Op2Not = true;
5343 }
Justin Bognerb03fd122016-08-17 05:10:15 +00005344 LLVM_FALLTHROUGH;
5345 }
Hal Finkel940ab932014-02-28 00:27:01 +00005346 case PPC::BC:
5347 case PPC::BCn:
5348 case PPC::SELECT_I4:
5349 case PPC::SELECT_I8:
5350 case PPC::SELECT_F4:
5351 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005352 case PPC::SELECT_QFRC:
5353 case PPC::SELECT_QSRC:
5354 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00005355 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00005356 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00005357 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00005358 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00005359 SDValue Op = MachineNode->getOperand(0);
5360 if (Op.isMachineOpcode()) {
5361 if (Op.getMachineOpcode() == PPC::CRSET)
5362 Op1Set = true;
5363 else if (Op.getMachineOpcode() == PPC::CRUNSET)
5364 Op1Unset = true;
5365 else if (Op.getMachineOpcode() == PPC::CRNOR &&
5366 Op.getOperand(0) == Op.getOperand(1))
5367 Op1Not = true;
5368 }
5369 }
5370 break;
5371 }
5372
Hal Finkelb9989152014-02-28 06:11:16 +00005373 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00005374 switch (Opcode) {
5375 default: break;
5376 case PPC::CRAND:
5377 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5378 // x & x = x
5379 ResNode = MachineNode->getOperand(0).getNode();
5380 else if (Op1Set)
5381 // 1 & y = y
5382 ResNode = MachineNode->getOperand(1).getNode();
5383 else if (Op2Set)
5384 // x & 1 = x
5385 ResNode = MachineNode->getOperand(0).getNode();
5386 else if (Op1Unset || Op2Unset)
5387 // x & 0 = 0 & y = 0
5388 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
5389 MVT::i1);
5390 else if (Op1Not)
5391 // ~x & y = andc(y, x)
5392 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
5393 MVT::i1, MachineNode->getOperand(1),
5394 MachineNode->getOperand(0).
5395 getOperand(0));
5396 else if (Op2Not)
5397 // x & ~y = andc(x, y)
5398 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
5399 MVT::i1, MachineNode->getOperand(0),
5400 MachineNode->getOperand(1).
5401 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005402 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005403 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
5404 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005405 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005406 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005407 }
Hal Finkel940ab932014-02-28 00:27:01 +00005408 break;
5409 case PPC::CRNAND:
5410 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5411 // nand(x, x) -> nor(x, x)
5412 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5413 MVT::i1, MachineNode->getOperand(0),
5414 MachineNode->getOperand(0));
5415 else if (Op1Set)
5416 // nand(1, y) -> nor(y, y)
5417 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5418 MVT::i1, MachineNode->getOperand(1),
5419 MachineNode->getOperand(1));
5420 else if (Op2Set)
5421 // nand(x, 1) -> nor(x, x)
5422 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5423 MVT::i1, MachineNode->getOperand(0),
5424 MachineNode->getOperand(0));
5425 else if (Op1Unset || Op2Unset)
5426 // nand(x, 0) = nand(0, y) = 1
5427 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
5428 MVT::i1);
5429 else if (Op1Not)
5430 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
5431 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
5432 MVT::i1, MachineNode->getOperand(0).
5433 getOperand(0),
5434 MachineNode->getOperand(1));
5435 else if (Op2Not)
5436 // nand(x, ~y) = ~x | y = orc(y, x)
5437 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
5438 MVT::i1, MachineNode->getOperand(1).
5439 getOperand(0),
5440 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005441 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005442 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
5443 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005444 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005445 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005446 }
Hal Finkel940ab932014-02-28 00:27:01 +00005447 break;
5448 case PPC::CROR:
5449 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5450 // x | x = x
5451 ResNode = MachineNode->getOperand(0).getNode();
5452 else if (Op1Set || Op2Set)
5453 // x | 1 = 1 | y = 1
5454 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
5455 MVT::i1);
5456 else if (Op1Unset)
5457 // 0 | y = y
5458 ResNode = MachineNode->getOperand(1).getNode();
5459 else if (Op2Unset)
5460 // x | 0 = x
5461 ResNode = MachineNode->getOperand(0).getNode();
5462 else if (Op1Not)
5463 // ~x | y = orc(y, x)
5464 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
5465 MVT::i1, MachineNode->getOperand(1),
5466 MachineNode->getOperand(0).
5467 getOperand(0));
5468 else if (Op2Not)
5469 // x | ~y = orc(x, y)
5470 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
5471 MVT::i1, MachineNode->getOperand(0),
5472 MachineNode->getOperand(1).
5473 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005474 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005475 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5476 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005477 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005478 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005479 }
Hal Finkel940ab932014-02-28 00:27:01 +00005480 break;
5481 case PPC::CRXOR:
5482 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5483 // xor(x, x) = 0
5484 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
5485 MVT::i1);
5486 else if (Op1Set)
5487 // xor(1, y) -> nor(y, y)
5488 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5489 MVT::i1, MachineNode->getOperand(1),
5490 MachineNode->getOperand(1));
5491 else if (Op2Set)
5492 // xor(x, 1) -> nor(x, x)
5493 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5494 MVT::i1, MachineNode->getOperand(0),
5495 MachineNode->getOperand(0));
5496 else if (Op1Unset)
5497 // xor(0, y) = y
5498 ResNode = MachineNode->getOperand(1).getNode();
5499 else if (Op2Unset)
5500 // xor(x, 0) = x
5501 ResNode = MachineNode->getOperand(0).getNode();
5502 else if (Op1Not)
5503 // xor(~x, y) = eqv(x, y)
5504 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
5505 MVT::i1, MachineNode->getOperand(0).
5506 getOperand(0),
5507 MachineNode->getOperand(1));
5508 else if (Op2Not)
5509 // xor(x, ~y) = eqv(x, y)
5510 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
5511 MVT::i1, MachineNode->getOperand(0),
5512 MachineNode->getOperand(1).
5513 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005514 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005515 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
5516 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005517 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005518 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005519 }
Hal Finkel940ab932014-02-28 00:27:01 +00005520 break;
5521 case PPC::CRNOR:
5522 if (Op1Set || Op2Set)
5523 // nor(1, y) -> 0
5524 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
5525 MVT::i1);
5526 else if (Op1Unset)
5527 // nor(0, y) = ~y -> nor(y, y)
5528 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5529 MVT::i1, MachineNode->getOperand(1),
5530 MachineNode->getOperand(1));
5531 else if (Op2Unset)
5532 // nor(x, 0) = ~x
5533 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5534 MVT::i1, MachineNode->getOperand(0),
5535 MachineNode->getOperand(0));
5536 else if (Op1Not)
5537 // nor(~x, y) = andc(x, y)
5538 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
5539 MVT::i1, MachineNode->getOperand(0).
5540 getOperand(0),
5541 MachineNode->getOperand(1));
5542 else if (Op2Not)
5543 // nor(x, ~y) = andc(y, x)
5544 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
5545 MVT::i1, MachineNode->getOperand(1).
5546 getOperand(0),
5547 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005548 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005549 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
5550 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005551 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005552 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005553 }
Hal Finkel940ab932014-02-28 00:27:01 +00005554 break;
5555 case PPC::CREQV:
5556 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5557 // eqv(x, x) = 1
5558 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
5559 MVT::i1);
5560 else if (Op1Set)
5561 // eqv(1, y) = y
5562 ResNode = MachineNode->getOperand(1).getNode();
5563 else if (Op2Set)
5564 // eqv(x, 1) = x
5565 ResNode = MachineNode->getOperand(0).getNode();
5566 else if (Op1Unset)
5567 // eqv(0, y) = ~y -> nor(y, y)
5568 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5569 MVT::i1, MachineNode->getOperand(1),
5570 MachineNode->getOperand(1));
5571 else if (Op2Unset)
5572 // eqv(x, 0) = ~x
5573 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5574 MVT::i1, MachineNode->getOperand(0),
5575 MachineNode->getOperand(0));
5576 else if (Op1Not)
5577 // eqv(~x, y) = xor(x, y)
5578 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
5579 MVT::i1, MachineNode->getOperand(0).
5580 getOperand(0),
5581 MachineNode->getOperand(1));
5582 else if (Op2Not)
5583 // eqv(x, ~y) = xor(x, y)
5584 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
5585 MVT::i1, MachineNode->getOperand(0),
5586 MachineNode->getOperand(1).
5587 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005588 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005589 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
5590 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00005591 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00005592 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005593 }
Hal Finkel940ab932014-02-28 00:27:01 +00005594 break;
5595 case PPC::CRANDC:
5596 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5597 // andc(x, x) = 0
5598 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
5599 MVT::i1);
5600 else if (Op1Set)
5601 // andc(1, y) = ~y
5602 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5603 MVT::i1, MachineNode->getOperand(1),
5604 MachineNode->getOperand(1));
5605 else if (Op1Unset || Op2Set)
5606 // andc(0, y) = andc(x, 1) = 0
5607 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
5608 MVT::i1);
5609 else if (Op2Unset)
5610 // andc(x, 0) = x
5611 ResNode = MachineNode->getOperand(0).getNode();
5612 else if (Op1Not)
5613 // andc(~x, y) = ~(x | y) = nor(x, y)
5614 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5615 MVT::i1, MachineNode->getOperand(0).
5616 getOperand(0),
5617 MachineNode->getOperand(1));
5618 else if (Op2Not)
5619 // andc(x, ~y) = x & y
5620 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
5621 MVT::i1, MachineNode->getOperand(0),
5622 MachineNode->getOperand(1).
5623 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005624 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005625 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
5626 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00005627 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00005628 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005629 }
Hal Finkel940ab932014-02-28 00:27:01 +00005630 break;
5631 case PPC::CRORC:
5632 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
5633 // orc(x, x) = 1
5634 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
5635 MVT::i1);
5636 else if (Op1Set || Op2Unset)
5637 // orc(1, y) = orc(x, 0) = 1
5638 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
5639 MVT::i1);
5640 else if (Op2Set)
5641 // orc(x, 1) = x
5642 ResNode = MachineNode->getOperand(0).getNode();
5643 else if (Op1Unset)
5644 // orc(0, y) = ~y
5645 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
5646 MVT::i1, MachineNode->getOperand(1),
5647 MachineNode->getOperand(1));
5648 else if (Op1Not)
5649 // orc(~x, y) = ~(x & y) = nand(x, y)
5650 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
5651 MVT::i1, MachineNode->getOperand(0).
5652 getOperand(0),
5653 MachineNode->getOperand(1));
5654 else if (Op2Not)
5655 // orc(x, ~y) = x | y
5656 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
5657 MVT::i1, MachineNode->getOperand(0),
5658 MachineNode->getOperand(1).
5659 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00005660 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00005661 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
5662 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00005663 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00005664 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00005665 }
Hal Finkel940ab932014-02-28 00:27:01 +00005666 break;
5667 case PPC::SELECT_I4:
5668 case PPC::SELECT_I8:
5669 case PPC::SELECT_F4:
5670 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00005671 case PPC::SELECT_QFRC:
5672 case PPC::SELECT_QSRC:
5673 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00005674 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00005675 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00005676 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00005677 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00005678 if (Op1Set)
5679 ResNode = MachineNode->getOperand(1).getNode();
5680 else if (Op1Unset)
5681 ResNode = MachineNode->getOperand(2).getNode();
5682 else if (Op1Not)
5683 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
5684 SDLoc(MachineNode),
5685 MachineNode->getValueType(0),
5686 MachineNode->getOperand(0).
5687 getOperand(0),
5688 MachineNode->getOperand(2),
5689 MachineNode->getOperand(1));
5690 break;
5691 case PPC::BC:
5692 case PPC::BCn:
5693 if (Op1Not)
5694 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
5695 PPC::BC,
5696 SDLoc(MachineNode),
5697 MVT::Other,
5698 MachineNode->getOperand(0).
5699 getOperand(0),
5700 MachineNode->getOperand(1),
5701 MachineNode->getOperand(2));
5702 // FIXME: Handle Op1Set, Op1Unset here too.
5703 break;
5704 }
5705
Hal Finkelb9989152014-02-28 06:11:16 +00005706 // If we're inverting this node because it is used only by selects that
5707 // we'd like to swap, then swap the selects before the node replacement.
5708 if (SelectSwap)
5709 SwapAllSelectUsers(MachineNode);
5710
Hal Finkel940ab932014-02-28 00:27:01 +00005711 if (ResNode != MachineNode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00005712 LLVM_DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
5713 LLVM_DEBUG(MachineNode->dump(CurDAG));
5714 LLVM_DEBUG(dbgs() << "\nNew: ");
5715 LLVM_DEBUG(ResNode->dump(CurDAG));
5716 LLVM_DEBUG(dbgs() << "\n");
Hal Finkel940ab932014-02-28 00:27:01 +00005717
5718 ReplaceUses(MachineNode, ResNode);
5719 IsModified = true;
5720 }
5721 }
5722 if (IsModified)
5723 CurDAG->RemoveDeadNodes();
5724 } while (IsModified);
5725}
5726
Hal Finkel4c6658f2014-12-12 23:59:36 +00005727// Gather the set of 32-bit operations that are known to have their
5728// higher-order 32 bits zero, where ToPromote contains all such operations.
5729static bool PeepholePPC64ZExtGather(SDValue Op32,
5730 SmallPtrSetImpl<SDNode *> &ToPromote) {
5731 if (!Op32.isMachineOpcode())
5732 return false;
5733
5734 // First, check for the "frontier" instructions (those that will clear the
5735 // higher-order 32 bits.
5736
5737 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
5738 // around. If it does not, then these instructions will clear the
5739 // higher-order bits.
5740 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
5741 Op32.getMachineOpcode() == PPC::RLWNM) &&
5742 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
5743 ToPromote.insert(Op32.getNode());
5744 return true;
5745 }
5746
5747 // SLW and SRW always clear the higher-order bits.
5748 if (Op32.getMachineOpcode() == PPC::SLW ||
5749 Op32.getMachineOpcode() == PPC::SRW) {
5750 ToPromote.insert(Op32.getNode());
5751 return true;
5752 }
5753
5754 // For LI and LIS, we need the immediate to be positive (so that it is not
5755 // sign extended).
5756 if (Op32.getMachineOpcode() == PPC::LI ||
5757 Op32.getMachineOpcode() == PPC::LIS) {
5758 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
5759 return false;
5760
5761 ToPromote.insert(Op32.getNode());
5762 return true;
5763 }
5764
Hal Finkel4e2c7822015-01-05 18:09:06 +00005765 // LHBRX and LWBRX always clear the higher-order bits.
5766 if (Op32.getMachineOpcode() == PPC::LHBRX ||
5767 Op32.getMachineOpcode() == PPC::LWBRX) {
5768 ToPromote.insert(Op32.getNode());
5769 return true;
5770 }
5771
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00005772 // CNT[LT]ZW always produce a 64-bit value in [0,32], and so is zero extended.
5773 if (Op32.getMachineOpcode() == PPC::CNTLZW ||
5774 Op32.getMachineOpcode() == PPC::CNTTZW) {
Hal Finkel49557f12015-01-05 18:52:29 +00005775 ToPromote.insert(Op32.getNode());
5776 return true;
5777 }
5778
Hal Finkel4c6658f2014-12-12 23:59:36 +00005779 // Next, check for those instructions we can look through.
5780
5781 // Assuming the mask does not wrap around, then the higher-order bits are
5782 // taken directly from the first operand.
5783 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
5784 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
5785 SmallPtrSet<SDNode *, 16> ToPromote1;
5786 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
5787 return false;
5788
5789 ToPromote.insert(Op32.getNode());
5790 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
5791 return true;
5792 }
5793
5794 // For OR, the higher-order bits are zero if that is true for both operands.
5795 // For SELECT_I4, the same is true (but the relevant operand numbers are
5796 // shifted by 1).
5797 if (Op32.getMachineOpcode() == PPC::OR ||
5798 Op32.getMachineOpcode() == PPC::SELECT_I4) {
5799 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
5800 SmallPtrSet<SDNode *, 16> ToPromote1;
5801 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
5802 return false;
5803 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
5804 return false;
5805
5806 ToPromote.insert(Op32.getNode());
5807 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
5808 return true;
5809 }
5810
5811 // For ORI and ORIS, we need the higher-order bits of the first operand to be
5812 // zero, and also for the constant to be positive (so that it is not sign
5813 // extended).
5814 if (Op32.getMachineOpcode() == PPC::ORI ||
5815 Op32.getMachineOpcode() == PPC::ORIS) {
5816 SmallPtrSet<SDNode *, 16> ToPromote1;
5817 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
5818 return false;
5819 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
5820 return false;
5821
5822 ToPromote.insert(Op32.getNode());
5823 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
5824 return true;
5825 }
5826
5827 // The higher-order bits of AND are zero if that is true for at least one of
5828 // the operands.
5829 if (Op32.getMachineOpcode() == PPC::AND) {
5830 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
5831 bool Op0OK =
5832 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
5833 bool Op1OK =
5834 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
5835 if (!Op0OK && !Op1OK)
5836 return false;
5837
5838 ToPromote.insert(Op32.getNode());
5839
5840 if (Op0OK)
5841 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
5842
5843 if (Op1OK)
5844 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
5845
5846 return true;
5847 }
5848
5849 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
5850 // of the first operand, or if the second operand is positive (so that it is
5851 // not sign extended).
5852 if (Op32.getMachineOpcode() == PPC::ANDIo ||
5853 Op32.getMachineOpcode() == PPC::ANDISo) {
5854 SmallPtrSet<SDNode *, 16> ToPromote1;
5855 bool Op0OK =
5856 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
5857 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
5858 if (!Op0OK && !Op1OK)
5859 return false;
5860
5861 ToPromote.insert(Op32.getNode());
5862
5863 if (Op0OK)
5864 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
5865
5866 return true;
5867 }
5868
5869 return false;
5870}
5871
5872void PPCDAGToDAGISel::PeepholePPC64ZExt() {
5873 if (!PPCSubTarget->isPPC64())
5874 return;
5875
5876 // When we zero-extend from i32 to i64, we use a pattern like this:
5877 // def : Pat<(i64 (zext i32:$in)),
5878 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
5879 // 0, 32)>;
5880 // There are several 32-bit shift/rotate instructions, however, that will
5881 // clear the higher-order bits of their output, rendering the RLDICL
5882 // unnecessary. When that happens, we remove it here, and redefine the
5883 // relevant 32-bit operation to be a 64-bit operation.
5884
Lei Huang716103f2018-05-29 13:38:56 +00005885 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
Hal Finkel4c6658f2014-12-12 23:59:36 +00005886
5887 bool MadeChange = false;
5888 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00005889 SDNode *N = &*--Position;
Hal Finkel4c6658f2014-12-12 23:59:36 +00005890 // Skip dead nodes and any non-machine opcodes.
5891 if (N->use_empty() || !N->isMachineOpcode())
5892 continue;
5893
5894 if (N->getMachineOpcode() != PPC::RLDICL)
5895 continue;
5896
5897 if (N->getConstantOperandVal(1) != 0 ||
5898 N->getConstantOperandVal(2) != 32)
5899 continue;
5900
5901 SDValue ISR = N->getOperand(0);
5902 if (!ISR.isMachineOpcode() ||
5903 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
5904 continue;
5905
5906 if (!ISR.hasOneUse())
5907 continue;
5908
5909 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
5910 continue;
5911
5912 SDValue IDef = ISR.getOperand(0);
5913 if (!IDef.isMachineOpcode() ||
5914 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
5915 continue;
5916
5917 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
5918 // can get rid of it.
5919
5920 SDValue Op32 = ISR->getOperand(1);
5921 if (!Op32.isMachineOpcode())
5922 continue;
5923
5924 // There are some 32-bit instructions that always clear the high-order 32
5925 // bits, there are also some instructions (like AND) that we can look
5926 // through.
5927 SmallPtrSet<SDNode *, 16> ToPromote;
5928 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
5929 continue;
5930
5931 // If the ToPromote set contains nodes that have uses outside of the set
5932 // (except for the original INSERT_SUBREG), then abort the transformation.
5933 bool OutsideUse = false;
5934 for (SDNode *PN : ToPromote) {
5935 for (SDNode *UN : PN->uses()) {
5936 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
5937 OutsideUse = true;
5938 break;
5939 }
5940 }
5941
5942 if (OutsideUse)
5943 break;
5944 }
5945 if (OutsideUse)
5946 continue;
5947
5948 MadeChange = true;
5949
5950 // We now know that this zero extension can be removed by promoting to
5951 // nodes in ToPromote to 64-bit operations, where for operations in the
5952 // frontier of the set, we need to insert INSERT_SUBREGs for their
5953 // operands.
5954 for (SDNode *PN : ToPromote) {
5955 unsigned NewOpcode;
5956 switch (PN->getMachineOpcode()) {
5957 default:
5958 llvm_unreachable("Don't know the 64-bit variant of this instruction");
5959 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
5960 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
5961 case PPC::SLW: NewOpcode = PPC::SLW8; break;
5962 case PPC::SRW: NewOpcode = PPC::SRW8; break;
5963 case PPC::LI: NewOpcode = PPC::LI8; break;
5964 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00005965 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
5966 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00005967 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00005968 case PPC::CNTTZW: NewOpcode = PPC::CNTTZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00005969 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
5970 case PPC::OR: NewOpcode = PPC::OR8; break;
5971 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
5972 case PPC::ORI: NewOpcode = PPC::ORI8; break;
5973 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
5974 case PPC::AND: NewOpcode = PPC::AND8; break;
5975 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
5976 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
5977 }
5978
5979 // Note: During the replacement process, the nodes will be in an
5980 // inconsistent state (some instructions will have operands with values
5981 // of the wrong type). Once done, however, everything should be right
5982 // again.
5983
5984 SmallVector<SDValue, 4> Ops;
5985 for (const SDValue &V : PN->ops()) {
5986 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
5987 !isa<ConstantSDNode>(V)) {
5988 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
5989 SDNode *ReplOp =
5990 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
5991 ISR.getNode()->getVTList(), ReplOpOps);
5992 Ops.push_back(SDValue(ReplOp, 0));
5993 } else {
5994 Ops.push_back(V);
5995 }
5996 }
5997
5998 // Because all to-be-promoted nodes only have users that are other
5999 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
6000 // the i32 result value type with i64.
6001
6002 SmallVector<EVT, 2> NewVTs;
6003 SDVTList VTs = PN->getVTList();
6004 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
6005 if (VTs.VTs[i] == MVT::i32)
6006 NewVTs.push_back(MVT::i64);
6007 else
6008 NewVTs.push_back(VTs.VTs[i]);
6009
Nicola Zaghend34e60c2018-05-14 12:53:11 +00006010 LLVM_DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
6011 LLVM_DEBUG(PN->dump(CurDAG));
Hal Finkel4c6658f2014-12-12 23:59:36 +00006012
6013 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
6014
Nicola Zaghend34e60c2018-05-14 12:53:11 +00006015 LLVM_DEBUG(dbgs() << "\nNew: ");
6016 LLVM_DEBUG(PN->dump(CurDAG));
6017 LLVM_DEBUG(dbgs() << "\n");
Hal Finkel4c6658f2014-12-12 23:59:36 +00006018 }
6019
6020 // Now we replace the original zero extend and its associated INSERT_SUBREG
6021 // with the value feeding the INSERT_SUBREG (which has now been promoted to
6022 // return an i64).
6023
Nicola Zaghend34e60c2018-05-14 12:53:11 +00006024 LLVM_DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
6025 LLVM_DEBUG(N->dump(CurDAG));
6026 LLVM_DEBUG(dbgs() << "\nNew: ");
6027 LLVM_DEBUG(Op32.getNode()->dump(CurDAG));
6028 LLVM_DEBUG(dbgs() << "\n");
Hal Finkel4c6658f2014-12-12 23:59:36 +00006029
6030 ReplaceUses(N, Op32.getNode());
6031 }
6032
6033 if (MadeChange)
6034 CurDAG->RemoveDeadNodes();
6035}
6036
Hal Finkel940ab932014-02-28 00:27:01 +00006037void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006038 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00006039 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006040 return;
6041
Lei Huang716103f2018-05-29 13:38:56 +00006042 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006043
6044 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00006045 SDNode *N = &*--Position;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006046 // Skip dead nodes and any non-machine opcodes.
6047 if (N->use_empty() || !N->isMachineOpcode())
6048 continue;
6049
6050 unsigned FirstOp;
6051 unsigned StorageOpcode = N->getMachineOpcode();
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006052 bool RequiresMod4Offset = false;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006053
6054 switch (StorageOpcode) {
6055 default: continue;
6056
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006057 case PPC::LWA:
6058 case PPC::LD:
6059 case PPC::DFLOADf64:
6060 case PPC::DFLOADf32:
6061 RequiresMod4Offset = true;
6062 LLVM_FALLTHROUGH;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006063 case PPC::LBZ:
6064 case PPC::LBZ8:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006065 case PPC::LFD:
6066 case PPC::LFS:
6067 case PPC::LHA:
6068 case PPC::LHA8:
6069 case PPC::LHZ:
6070 case PPC::LHZ8:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006071 case PPC::LWZ:
6072 case PPC::LWZ8:
6073 FirstOp = 0;
6074 break;
6075
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006076 case PPC::STD:
6077 case PPC::DFSTOREf64:
6078 case PPC::DFSTOREf32:
6079 RequiresMod4Offset = true;
6080 LLVM_FALLTHROUGH;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006081 case PPC::STB:
6082 case PPC::STB8:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006083 case PPC::STFD:
6084 case PPC::STFS:
6085 case PPC::STH:
6086 case PPC::STH8:
6087 case PPC::STW:
6088 case PPC::STW8:
6089 FirstOp = 1;
6090 break;
6091 }
6092
Kyle Butt1452b762015-12-11 00:47:36 +00006093 // If this is a load or store with a zero offset, or within the alignment,
6094 // we may be able to fold an add-immediate into the memory operation.
6095 // The check against alignment is below, as it can't occur until we check
6096 // the arguments to N
6097 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)))
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006098 continue;
6099
6100 SDValue Base = N->getOperand(FirstOp + 1);
6101 if (!Base.isMachineOpcode())
6102 continue;
6103
6104 unsigned Flags = 0;
6105 bool ReplaceFlags = true;
6106
6107 // When the feeding operation is an add-immediate of some sort,
6108 // determine whether we need to add relocation information to the
6109 // target flags on the immediate operand when we fold it into the
6110 // load instruction.
6111 //
6112 // For something like ADDItocL, the relocation information is
6113 // inferred from the opcode; when we process it in the AsmPrinter,
6114 // we add the necessary relocation there. A load, though, can receive
6115 // relocation from various flavors of ADDIxxx, so we need to carry
6116 // the relocation information in the target flags.
6117 switch (Base.getMachineOpcode()) {
6118 default: continue;
6119
6120 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00006121 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006122 // In some cases (such as TLS) the relocation information
6123 // is already in place on the operand, so copying the operand
6124 // is sufficient.
6125 ReplaceFlags = false;
6126 // For these cases, the immediate may not be divisible by 4, in
6127 // which case the fold is illegal for DS-form instructions. (The
6128 // other cases provide aligned addresses and are always safe.)
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006129 if (RequiresMod4Offset &&
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006130 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
6131 Base.getConstantOperandVal(1) % 4 != 0))
6132 continue;
6133 break;
6134 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00006135 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006136 break;
6137 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00006138 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006139 break;
6140 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00006141 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006142 break;
6143 }
6144
Kyle Butt1452b762015-12-11 00:47:36 +00006145 SDValue ImmOpnd = Base.getOperand(1);
Hal Finkelb54579f2016-09-02 00:28:20 +00006146
6147 // On PPC64, the TOC base pointer is guaranteed by the ABI only to have
6148 // 8-byte alignment, and so we can only use offsets less than 8 (otherwise,
6149 // we might have needed different @ha relocation values for the offset
6150 // pointers).
6151 int MaxDisplacement = 7;
Kyle Butt1452b762015-12-11 00:47:36 +00006152 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
6153 const GlobalValue *GV = GA->getGlobal();
Hal Finkelb54579f2016-09-02 00:28:20 +00006154 MaxDisplacement = std::min((int) GV->getAlignment() - 1, MaxDisplacement);
Kyle Butt1452b762015-12-11 00:47:36 +00006155 }
6156
Hal Finkel7b104d42016-09-02 21:37:07 +00006157 bool UpdateHBase = false;
6158 SDValue HBase = Base.getOperand(0);
6159
Kyle Butt1452b762015-12-11 00:47:36 +00006160 int Offset = N->getConstantOperandVal(FirstOp);
Hal Finkel42c83f12016-09-07 07:36:11 +00006161 if (ReplaceFlags) {
6162 if (Offset < 0 || Offset > MaxDisplacement) {
6163 // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
6164 // one use, then we can do this for any offset, we just need to also
6165 // update the offset (i.e. the symbol addend) on the addis also.
6166 if (Base.getMachineOpcode() != PPC::ADDItocL)
6167 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00006168
Hal Finkel42c83f12016-09-07 07:36:11 +00006169 if (!HBase.isMachineOpcode() ||
6170 HBase.getMachineOpcode() != PPC::ADDIStocHA)
6171 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00006172
Hal Finkel42c83f12016-09-07 07:36:11 +00006173 if (!Base.hasOneUse() || !HBase.hasOneUse())
6174 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00006175
Hal Finkel42c83f12016-09-07 07:36:11 +00006176 SDValue HImmOpnd = HBase.getOperand(1);
6177 if (HImmOpnd != ImmOpnd)
6178 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00006179
Hal Finkel42c83f12016-09-07 07:36:11 +00006180 UpdateHBase = true;
6181 }
6182 } else {
6183 // If we're directly folding the addend from an addi instruction, then:
6184 // 1. In general, the offset on the memory access must be zero.
6185 // 2. If the addend is a constant, then it can be combined with a
6186 // non-zero offset, but only if the result meets the encoding
6187 // requirements.
6188 if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) {
6189 Offset += C->getSExtValue();
6190
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006191 if (RequiresMod4Offset && (Offset % 4) != 0)
Hal Finkel42c83f12016-09-07 07:36:11 +00006192 continue;
6193
6194 if (!isInt<16>(Offset))
6195 continue;
6196
6197 ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd),
6198 ImmOpnd.getValueType());
6199 } else if (Offset != 0) {
6200 continue;
6201 }
Hal Finkel7b104d42016-09-02 21:37:07 +00006202 }
Kyle Butt1452b762015-12-11 00:47:36 +00006203
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006204 // We found an opportunity. Reverse the operands from the add
6205 // immediate and substitute them into the load or store. If
6206 // needed, update the target flags for the immediate operand to
6207 // reflect the necessary relocation information.
Nicola Zaghend34e60c2018-05-14 12:53:11 +00006208 LLVM_DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
6209 LLVM_DEBUG(Base->dump(CurDAG));
6210 LLVM_DEBUG(dbgs() << "\nN: ");
6211 LLVM_DEBUG(N->dump(CurDAG));
6212 LLVM_DEBUG(dbgs() << "\n");
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006213
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006214 // If the relocation information isn't already present on the
6215 // immediate operand, add it now.
6216 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00006217 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006218 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006219 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00006220 // We can't perform this optimization for data whose alignment
6221 // is insufficient for the instruction encoding.
6222 if (GV->getAlignment() < 4 &&
QingShan Zhang9f0fe9a2018-06-19 06:54:51 +00006223 (RequiresMod4Offset || (Offset % 4) != 0)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00006224 LLVM_DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
Bill Schmidt48fc20a2013-07-01 20:52:27 +00006225 continue;
6226 }
Kyle Butt1452b762015-12-11 00:47:36 +00006227 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00006228 } else if (ConstantPoolSDNode *CP =
6229 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00006230 const Constant *C = CP->getConstVal();
6231 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
6232 CP->getAlignment(),
Kyle Butt1452b762015-12-11 00:47:36 +00006233 Offset, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006234 }
6235 }
6236
6237 if (FirstOp == 1) // Store
6238 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
6239 Base.getOperand(0), N->getOperand(3));
6240 else // Load
6241 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
6242 N->getOperand(2));
6243
Hal Finkel7b104d42016-09-02 21:37:07 +00006244 if (UpdateHBase)
6245 (void)CurDAG->UpdateNodeOperands(HBase.getNode(), HBase.getOperand(0),
6246 ImmOpnd);
6247
Bill Schmidtf5b474c2013-02-21 00:38:25 +00006248 // The add-immediate may now be dead, in which case remove it.
6249 if (Base.getNode()->use_empty())
6250 CurDAG->RemoveDeadNode(Base.getNode());
6251 }
6252}
Chris Lattner43ff01e2005-08-17 19:33:03 +00006253
Andrew Trickc416ba62010-12-24 04:28:06 +00006254/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00006255/// PowerPC-specific DAG, ready for instruction scheduling.
6256///
Hiroshi Inoue51020282017-06-27 04:52:17 +00006257FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM,
6258 CodeGenOpt::Level OptLevel) {
6259 return new PPCDAGToDAGISel(TM, OptLevel);
Chris Lattner43ff01e2005-08-17 19:33:03 +00006260}