blob: d51f2698c7e8b329694166322ac6189d07e025f5 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
Chris Lattner43ff01e2005-08-17 19:33:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner43ff01e2005-08-17 19:33:03 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010// This file defines a pattern matching instruction selector for PowerPC,
Chris Lattner43ff01e2005-08-17 19:33:03 +000011// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
Eugene Zelenko8187c192017-01-13 00:58:58 +000015#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000017#include "PPC.h"
18#include "PPCISelLowering.h"
Hal Finkel3ee2af72014-07-18 23:29:49 +000019#include "PPCMachineFunctionInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000020#include "PPCSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "PPCTargetMachine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/ADT/STLExtras.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/ADT/Statistic.h"
Hal Finkel65539e32015-12-12 00:32:00 +000028#include "llvm/Analysis/BranchProbabilityInfo.h"
29#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000030#include "llvm/CodeGen/ISDOpcodes.h"
31#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner45640392005-08-19 22:38:53 +000032#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000035#include "llvm/CodeGen/MachineValueType.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000036#include "llvm/CodeGen/SelectionDAG.h"
37#include "llvm/CodeGen/SelectionDAGISel.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000038#include "llvm/CodeGen/SelectionDAGNodes.h"
39#include "llvm/CodeGen/ValueTypes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Function.h"
43#include "llvm/IR/GlobalValue.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000044#include "llvm/IR/InlineAsm.h"
45#include "llvm/IR/InstrTypes.h"
Justin Hibbitsa88b6052014-11-12 15:16:30 +000046#include "llvm/IR/Module.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000047#include "llvm/Support/Casting.h"
48#include "llvm/Support/CodeGen.h"
Hal Finkel940ab932014-02-28 00:27:01 +000049#include "llvm/Support/CommandLine.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000050#include "llvm/Support/Compiler.h"
Chris Lattner43ff01e2005-08-17 19:33:03 +000051#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000052#include "llvm/Support/ErrorHandling.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000053#include "llvm/Support/KnownBits.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000055#include "llvm/Support/raw_ostream.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000056#include "llvm/Target/TargetInstrInfo.h"
57#include "llvm/Target/TargetRegisterInfo.h"
58#include <algorithm>
59#include <cassert>
60#include <cstdint>
61#include <iterator>
62#include <limits>
63#include <memory>
64#include <new>
65#include <tuple>
66#include <utility>
67
Chris Lattner43ff01e2005-08-17 19:33:03 +000068using namespace llvm;
69
Chandler Carruth84e68b22014-04-22 02:41:26 +000070#define DEBUG_TYPE "ppc-codegen"
71
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +000072STATISTIC(NumSextSetcc,
73 "Number of (sext(setcc)) nodes expanded into GPR sequence.");
74STATISTIC(NumZextSetcc,
75 "Number of (zext(setcc)) nodes expanded into GPR sequence.");
76STATISTIC(SignExtensionsAdded,
77 "Number of sign extensions for compare inputs added.");
78STATISTIC(ZeroExtensionsAdded,
79 "Number of zero extensions for compare inputs added.");
Nemanja Ivanovice597bd82017-05-31 05:40:25 +000080STATISTIC(NumLogicOpsOnComparison,
81 "Number of logical ops on i1 values calculated in GPR.");
82STATISTIC(OmittedForNonExtendUses,
83 "Number of compares not eliminated as they have non-extending uses.");
84
Hal Finkel940ab932014-02-28 00:27:01 +000085// FIXME: Remove this once the bug has been fixed!
86cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
87cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
88
Benjamin Kramer970eac42015-02-06 17:51:54 +000089static cl::opt<bool>
90 UseBitPermRewriter("ppc-use-bit-perm-rewriter", cl::init(true),
91 cl::desc("use aggressive ppc isel for bit permutations"),
92 cl::Hidden);
93static cl::opt<bool> BPermRewriterNoMasking(
94 "ppc-bit-perm-rewriter-stress-rotates",
95 cl::desc("stress rotate selection in aggressive ppc isel for "
96 "bit permutations"),
97 cl::Hidden);
Hal Finkelc58ce412015-01-01 02:53:29 +000098
Hal Finkel65539e32015-12-12 00:32:00 +000099static cl::opt<bool> EnableBranchHint(
100 "ppc-use-branch-hint", cl::init(true),
101 cl::desc("Enable static hinting of branches on ppc"),
102 cl::Hidden);
103
Chris Lattner43ff01e2005-08-17 19:33:03 +0000104namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000105
Chris Lattner43ff01e2005-08-17 19:33:03 +0000106 //===--------------------------------------------------------------------===//
Nate Begeman0b71e002005-10-18 00:28:58 +0000107 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
Chris Lattner43ff01e2005-08-17 19:33:03 +0000108 /// instructions for SelectionDAG operations.
109 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +0000110 class PPCDAGToDAGISel : public SelectionDAGISel {
Dan Gohman21cea8a2010-04-17 15:26:15 +0000111 const PPCTargetMachine &TM;
Eric Christopher1b8e7632014-05-22 01:07:24 +0000112 const PPCSubtarget *PPCSubTarget;
Eric Christophercccae792015-01-30 22:02:31 +0000113 const PPCTargetLowering *PPCLowering;
Chris Lattner45640392005-08-19 22:38:53 +0000114 unsigned GlobalBaseReg;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000115
Chris Lattner43ff01e2005-08-17 19:33:03 +0000116 public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000117 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
Chandler Carruth9ac86ef2016-06-03 10:13:31 +0000118 : SelectionDAGISel(tm), TM(tm) {}
Andrew Trickc416ba62010-12-24 04:28:06 +0000119
Craig Topper0d3fa922014-04-29 07:57:37 +0000120 bool runOnMachineFunction(MachineFunction &MF) override {
Chris Lattner45640392005-08-19 22:38:53 +0000121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
Eric Christophercccae792015-01-30 22:02:31 +0000123 PPCSubTarget = &MF.getSubtarget<PPCSubtarget>();
124 PPCLowering = PPCSubTarget->getTargetLowering();
Dan Gohman5ea74d52009-07-31 18:16:33 +0000125 SelectionDAGISel::runOnMachineFunction(MF);
Andrew Trickc416ba62010-12-24 04:28:06 +0000126
Eric Christopher1b8e7632014-05-22 01:07:24 +0000127 if (!PPCSubTarget->isSVR4ABI())
Bill Schmidt38d94582012-10-10 20:54:15 +0000128 InsertVRSaveCode(MF);
129
Chris Lattner1678a6c2006-03-16 18:25:23 +0000130 return true;
Chris Lattner45640392005-08-19 22:38:53 +0000131 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000132
Hal Finkel4edc66b2015-01-03 01:16:37 +0000133 void PreprocessISelDAG() override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000134 void PostprocessISelDAG() override;
Bill Schmidtf5b474c2013-02-21 00:38:25 +0000135
Chris Lattner43ff01e2005-08-17 19:33:03 +0000136 /// getI32Imm - Return a target constant with the specified value, of type
137 /// i32.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000138 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000139 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000140 }
Chris Lattner45640392005-08-19 22:38:53 +0000141
Chris Lattner97b3da12006-06-27 00:04:13 +0000142 /// getI64Imm - Return a target constant with the specified value, of type
143 /// i64.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000144 inline SDValue getI64Imm(uint64_t Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000145 return CurDAG->getTargetConstant(Imm, dl, MVT::i64);
Chris Lattner97b3da12006-06-27 00:04:13 +0000146 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000147
Chris Lattner97b3da12006-06-27 00:04:13 +0000148 /// getSmallIPtrImm - Return a target constant of pointer type.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000149 inline SDValue getSmallIPtrImm(unsigned Imm, const SDLoc &dl) {
Mehdi Amini44ede332015-07-09 02:09:04 +0000150 return CurDAG->getTargetConstant(
151 Imm, dl, PPCLowering->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner97b3da12006-06-27 00:04:13 +0000152 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000153
Nate Begemand31efd12006-09-22 05:01:56 +0000154 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
155 /// rotate and mask opcode and mask operation.
Dale Johannesen86dcae12009-11-24 01:09:07 +0000156 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
Nate Begemand31efd12006-09-22 05:01:56 +0000157 unsigned &SH, unsigned &MB, unsigned &ME);
Andrew Trickc416ba62010-12-24 04:28:06 +0000158
Chris Lattner45640392005-08-19 22:38:53 +0000159 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
160 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000161 SDNode *getGlobalBaseReg();
Andrew Trickc416ba62010-12-24 04:28:06 +0000162
Justin Bognerdc8af062016-05-20 21:43:23 +0000163 void selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset = 0);
Hal Finkelb5e9b042014-12-11 22:51:06 +0000164
Chris Lattner43ff01e2005-08-17 19:33:03 +0000165 // Select - Convert the specified operand from a target-independent to a
166 // target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +0000167 void Select(SDNode *N) override;
Andrew Trickc416ba62010-12-24 04:28:06 +0000168
Justin Bognerdc8af062016-05-20 21:43:23 +0000169 bool tryBitfieldInsert(SDNode *N);
170 bool tryBitPermutation(SDNode *N);
Nate Begeman93c4bc62005-08-19 00:38:14 +0000171
Chris Lattner2a1823d2005-08-21 18:50:37 +0000172 /// SelectCC - Select a comparison of the specified values with the
173 /// specified condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000174 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
175 const SDLoc &dl);
Chris Lattner2a1823d2005-08-21 18:50:37 +0000176
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000177 /// SelectAddrImm - Returns true if the address N can be represented by
178 /// a base register plus a signed 16-bit displacement [r+imm].
Chris Lattner0e023ea2010-09-21 20:31:19 +0000179 bool SelectAddrImm(SDValue N, SDValue &Disp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000180 SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000181 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
Chris Lattnera801fced2006-11-08 02:15:41 +0000182 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000183
Chris Lattner6f5840c2006-11-16 00:41:37 +0000184 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000185 /// immediate field. Note that the operand at this point is already the
186 /// result of a prior SelectAddressRegImm call.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000187 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
Ulrich Weigandd1b99d32013-03-22 14:58:17 +0000188 if (N.getOpcode() == ISD::TargetConstant ||
Hal Finkela86b0f22012-06-21 20:10:48 +0000189 N.getOpcode() == ISD::TargetGlobalAddress) {
Hal Finkel1cc27e42012-06-19 02:34:32 +0000190 Out = N;
191 return true;
192 }
193
194 return false;
195 }
196
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000197 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
198 /// represented as an indexed [r+r] operation. Returns false if it can
199 /// be represented by [r+imm], which are preferred.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000200 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000201 return PPCLowering->SelectAddressRegReg(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000202 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000203
Nate Begeman8e6a8af2005-12-19 23:25:09 +0000204 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
205 /// represented as an indexed [r+r] operation.
Chris Lattner0e023ea2010-09-21 20:31:19 +0000206 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000207 return PPCLowering->SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
Chris Lattnera801fced2006-11-08 02:15:41 +0000208 }
Chris Lattnerc5292ec2005-08-21 22:31:09 +0000209
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000210 /// SelectAddrImmX4 - Returns true if the address N can be represented by
211 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
212 /// Suitable for use by STD and friends.
213 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
Eric Christopher1b8e7632014-05-22 01:07:24 +0000214 return PPCLowering->SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
Chris Lattnera801fced2006-11-08 02:15:41 +0000215 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000216
Hal Finkel756810f2013-03-21 21:37:52 +0000217 // Select an address into a single register.
218 bool SelectAddr(SDValue N, SDValue &Base) {
219 Base = N;
220 return true;
221 }
222
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000223 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
Dale Johannesen4a50e682009-08-18 00:18:39 +0000224 /// inline asm expressions. It is always correct to compute the value into
225 /// a register. The case of adding a (possibly relocatable) constant to a
226 /// register can be improved, but it is wrong to substitute Reg+Reg for
227 /// Reg in an asm, because the load or store opcode would have to change.
Hal Finkeld4338382014-12-03 23:40:13 +0000228 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000229 unsigned ConstraintID,
Craig Topper0d3fa922014-04-29 07:57:37 +0000230 std::vector<SDValue> &OutOps) override {
Daniel Sanders08288602015-03-17 11:09:13 +0000231 switch(ConstraintID) {
232 default:
233 errs() << "ConstraintID: " << ConstraintID << "\n";
234 llvm_unreachable("Unexpected asm memory constraint");
235 case InlineAsm::Constraint_es:
Daniel Sanders914b9472015-03-17 12:00:04 +0000236 case InlineAsm::Constraint_i:
Daniel Sanders08288602015-03-17 11:09:13 +0000237 case InlineAsm::Constraint_m:
238 case InlineAsm::Constraint_o:
239 case InlineAsm::Constraint_Q:
240 case InlineAsm::Constraint_Z:
241 case InlineAsm::Constraint_Zy:
242 // We need to make sure that this one operand does not end up in r0
243 // (because we might end up lowering this as 0(%op)).
244 const TargetRegisterInfo *TRI = PPCSubTarget->getRegisterInfo();
245 const TargetRegisterClass *TRC = TRI->getPointerRegClass(*MF, /*Kind=*/1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000246 SDLoc dl(Op);
247 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Daniel Sanders08288602015-03-17 11:09:13 +0000248 SDValue NewOp =
249 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000250 dl, Op.getValueType(),
Daniel Sanders08288602015-03-17 11:09:13 +0000251 Op, RC), 0);
252
253 OutOps.push_back(NewOp);
254 return false;
255 }
256 return true;
Chris Lattnera1ec1dd2006-02-24 02:13:12 +0000257 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000258
Dan Gohman5ea74d52009-07-31 18:16:33 +0000259 void InsertVRSaveCode(MachineFunction &MF);
Chris Lattner1678a6c2006-03-16 18:25:23 +0000260
Mehdi Amini117296c2016-10-01 02:56:57 +0000261 StringRef getPassName() const override {
Chris Lattner43ff01e2005-08-17 19:33:03 +0000262 return "PowerPC DAG->DAG Pattern Instruction Selection";
Andrew Trickc416ba62010-12-24 04:28:06 +0000263 }
264
Chris Lattner03e08ee2005-09-13 22:03:06 +0000265// Include the pieces autogenerated from the target description.
Chris Lattner0921e3b2005-10-14 23:37:35 +0000266#include "PPCGenDAGISel.inc"
Andrew Trickc416ba62010-12-24 04:28:06 +0000267
Chris Lattner259e6c72005-10-06 18:45:51 +0000268private:
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000269 // Conversion type for interpreting results of a 32-bit instruction as
270 // a 64-bit value or vice versa.
271 enum ExtOrTruncConversion { Ext, Trunc };
272
273 // Modifiers to guide how an ISD::SETCC node's result is to be computed
274 // in a GPR.
275 // ZExtOrig - use the original condition code, zero-extend value
276 // ZExtInvert - invert the condition code, zero-extend value
277 // SExtOrig - use the original condition code, sign-extend value
278 // SExtInvert - invert the condition code, sign-extend value
279 enum SetccInGPROpts { ZExtOrig, ZExtInvert, SExtOrig, SExtInvert };
280
Justin Bognerdc8af062016-05-20 21:43:23 +0000281 bool trySETCC(SDNode *N);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000282 bool tryEXTEND(SDNode *N);
Nemanja Ivanovice597bd82017-05-31 05:40:25 +0000283 bool tryLogicOpOfCompares(SDNode *N);
284 SDValue computeLogicOpInGPR(SDValue LogicOp);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000285 SDValue signExtendInputIfNeeded(SDValue Input);
286 SDValue zeroExtendInputIfNeeded(SDValue Input);
287 SDValue addExtOrTrunc(SDValue NatWidthRes, ExtOrTruncConversion Conv);
288 SDValue get32BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
289 int64_t RHSValue, SDLoc dl);
290 SDValue get32BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
291 int64_t RHSValue, SDLoc dl);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +0000292 SDValue get64BitZExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
293 int64_t RHSValue, SDLoc dl);
294 SDValue get64BitSExtCompare(SDValue LHS, SDValue RHS, ISD::CondCode CC,
295 int64_t RHSValue, SDLoc dl);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000296 SDValue getSETCCInGPR(SDValue Compare, SetccInGPROpts ConvOpts);
Hal Finkel940ab932014-02-28 00:27:01 +0000297
298 void PeepholePPC64();
Hal Finkel4c6658f2014-12-12 23:59:36 +0000299 void PeepholePPC64ZExt();
Eric Christopher02e18042014-05-14 00:31:15 +0000300 void PeepholeCROps();
Hal Finkelb9989152014-02-28 06:11:16 +0000301
Hal Finkel4edc66b2015-01-03 01:16:37 +0000302 SDValue combineToCMPB(SDNode *N);
Hal Finkel200d2ad2015-01-05 21:10:24 +0000303 void foldBoolExts(SDValue &Res, SDNode *&N);
Hal Finkel4edc66b2015-01-03 01:16:37 +0000304
Hal Finkelb9989152014-02-28 06:11:16 +0000305 bool AllUsersSelectZero(SDNode *N);
306 void SwapAllSelectUsers(SDNode *N);
Hal Finkelcf599212015-02-25 21:36:59 +0000307
Justin Bognerdc8af062016-05-20 21:43:23 +0000308 void transferMemOperands(SDNode *N, SDNode *Result);
Chris Lattner43ff01e2005-08-17 19:33:03 +0000309 };
Eugene Zelenko8187c192017-01-13 00:58:58 +0000310
311} // end anonymous namespace
Chris Lattner43ff01e2005-08-17 19:33:03 +0000312
Chris Lattner1678a6c2006-03-16 18:25:23 +0000313/// InsertVRSaveCode - Once the entire function has been instruction selected,
314/// all virtual registers are created and all machine instructions are built,
315/// check to see if we need to save/restore VRSAVE. If so, do it.
Dan Gohman5ea74d52009-07-31 18:16:33 +0000316void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000317 // Check to see if this function uses vector registers, which means we have to
Andrew Trickc416ba62010-12-24 04:28:06 +0000318 // save and restore the VRSAVE register and update it with the regs we use.
Chris Lattner02e2c182006-03-13 21:52:10 +0000319 //
Dan Gohman4a618822010-02-10 16:03:48 +0000320 // In this case, there will be virtual registers of vector type created
Chris Lattner02e2c182006-03-13 21:52:10 +0000321 // by the scheduler. Detect them now.
Chris Lattner02e2c182006-03-13 21:52:10 +0000322 bool HasVectorVReg = false;
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000323 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
324 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
325 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
Chris Lattner02e2c182006-03-13 21:52:10 +0000326 HasVectorVReg = true;
327 break;
328 }
Jakob Stoklund Olesen4a7b48d2011-01-08 23:11:11 +0000329 }
Chris Lattner1678a6c2006-03-16 18:25:23 +0000330 if (!HasVectorVReg) return; // nothing to do.
Andrew Trickc416ba62010-12-24 04:28:06 +0000331
Chris Lattner02e2c182006-03-13 21:52:10 +0000332 // If we have a vector register, we want to emit code into the entry and exit
333 // blocks to save and restore the VRSAVE register. We do this here (instead
334 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
335 //
336 // 1. This (trivially) reduces the load on the register allocator, by not
337 // having to represent the live range of the VRSAVE register.
338 // 2. This (more significantly) allows us to create a temporary virtual
339 // register to hold the saved VRSAVE value, allowing this temporary to be
340 // register allocated, instead of forcing it to be spilled to the stack.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000341
342 // Create two vregs - one to hold the VRSAVE register that is live-in to the
343 // function and one for the value after having bits or'd into it.
Chris Lattnera10fff52007-12-31 04:13:23 +0000344 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
345 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
Andrew Trickc416ba62010-12-24 04:28:06 +0000346
Eric Christophercccae792015-01-30 22:02:31 +0000347 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner1678a6c2006-03-16 18:25:23 +0000348 MachineBasicBlock &EntryBB = *Fn.begin();
Chris Lattner6f306d72010-04-02 20:16:16 +0000349 DebugLoc dl;
Chris Lattner1678a6c2006-03-16 18:25:23 +0000350 // Emit the following code into the entry block:
351 // InVRSAVE = MFVRSAVE
352 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
353 // MTVRSAVE UpdatedVRSAVE
354 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
Dale Johannesene9f623e2009-02-13 02:27:39 +0000355 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
356 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
Chris Lattnera98c6792008-01-07 01:56:04 +0000357 UpdatedVRSAVE).addReg(InVRSAVE);
Dale Johannesene9f623e2009-02-13 02:27:39 +0000358 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000359
Chris Lattner1678a6c2006-03-16 18:25:23 +0000360 // Find all return blocks, outputting a restore in each epilog.
Chris Lattner1678a6c2006-03-16 18:25:23 +0000361 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000362 if (BB->isReturnBlock()) {
Chris Lattner1678a6c2006-03-16 18:25:23 +0000363 IP = BB->end(); --IP;
Andrew Trickc416ba62010-12-24 04:28:06 +0000364
Chris Lattner1678a6c2006-03-16 18:25:23 +0000365 // Skip over all terminator instructions, which are part of the return
366 // sequence.
367 MachineBasicBlock::iterator I2 = IP;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000368 while (I2 != BB->begin() && (--I2)->isTerminator())
Chris Lattner1678a6c2006-03-16 18:25:23 +0000369 IP = I2;
Andrew Trickc416ba62010-12-24 04:28:06 +0000370
Chris Lattner1678a6c2006-03-16 18:25:23 +0000371 // Emit: MTVRSAVE InVRSave
Dale Johannesene9f623e2009-02-13 02:27:39 +0000372 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
Andrew Trickc416ba62010-12-24 04:28:06 +0000373 }
Chris Lattner02e2c182006-03-13 21:52:10 +0000374 }
Chris Lattner259e6c72005-10-06 18:45:51 +0000375}
Chris Lattner8ae95252005-09-03 01:17:22 +0000376
Chris Lattner45640392005-08-19 22:38:53 +0000377/// getGlobalBaseReg - Output the instructions required to put the
378/// base address to use for accessing globals into a register.
379///
Evan Cheng61413a32006-08-26 05:34:46 +0000380SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner45640392005-08-19 22:38:53 +0000381 if (!GlobalBaseReg) {
Eric Christophercccae792015-01-30 22:02:31 +0000382 const TargetInstrInfo &TII = *PPCSubTarget->getInstrInfo();
Chris Lattner45640392005-08-19 22:38:53 +0000383 // Insert the set of GlobalBaseReg into the first MBB of the function
Dan Gohmanfca89682009-08-15 02:07:36 +0000384 MachineBasicBlock &FirstMBB = MF->front();
Chris Lattner45640392005-08-19 22:38:53 +0000385 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000386 const Module *M = MF->getFunction()->getParent();
Chris Lattner6f306d72010-04-02 20:16:16 +0000387 DebugLoc dl;
Chris Lattner97b3da12006-06-27 00:04:13 +0000388
Mehdi Amini44ede332015-07-09 02:09:04 +0000389 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) == MVT::i32) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000390 if (PPCSubTarget->isTargetELF()) {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000391 GlobalBaseReg = PPC::R30;
Davide Italiano4cccc482016-06-17 18:07:14 +0000392 if (M->getPICLevel() == PICLevel::SmallPIC) {
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000393 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MoveGOTtoLR));
394 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Justin Hibbits98a532d2015-01-08 15:47:19 +0000395 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000396 } else {
397 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
398 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
399 unsigned TempReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
400 BuildMI(FirstMBB, MBBI, dl,
Hal Finkelcf599212015-02-25 21:36:59 +0000401 TII.get(PPC::UpdateGBR), GlobalBaseReg)
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000402 .addReg(TempReg, RegState::Define).addReg(GlobalBaseReg);
403 MF->getInfo<PPCFunctionInfo>()->setUsesPICBase(true);
404 }
405 } else {
Hal Finkel3ee2af72014-07-18 23:29:49 +0000406 GlobalBaseReg =
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000407 RegInfo->createVirtualRegister(&PPC::GPRC_and_GPRC_NOR0RegClass);
Justin Hibbitsa88b6052014-11-12 15:16:30 +0000408 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
409 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
Hal Finkel3ee2af72014-07-18 23:29:49 +0000410 }
Chris Lattnerb5429252006-11-14 18:43:11 +0000411 } else {
Joerg Sonnenbergerbef36212016-11-02 15:00:31 +0000412 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_and_G8RC_NOX0RegClass);
Cameron Zwarichdadd7332011-05-19 02:56:28 +0000413 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
Dale Johannesene9f623e2009-02-13 02:27:39 +0000414 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
Chris Lattnerb5429252006-11-14 18:43:11 +0000415 }
Chris Lattner45640392005-08-19 22:38:53 +0000416 }
Gabor Greif81d6a382008-08-31 15:37:04 +0000417 return CurDAG->getRegister(GlobalBaseReg,
Mehdi Amini44ede332015-07-09 02:09:04 +0000418 PPCLowering->getPointerTy(CurDAG->getDataLayout()))
419 .getNode();
Chris Lattner97b3da12006-06-27 00:04:13 +0000420}
421
422/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
423/// or 64-bit immediate, and if the value can be accurately represented as a
424/// sign extension from a 16-bit value. If so, this returns true and the
425/// immediate.
426static bool isIntS16Immediate(SDNode *N, short &Imm) {
427 if (N->getOpcode() != ISD::Constant)
428 return false;
429
Dan Gohmaneffb8942008-09-12 16:56:44 +0000430 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +0000431 if (N->getValueType(0) == MVT::i32)
Dan Gohmaneffb8942008-09-12 16:56:44 +0000432 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000433 else
Dan Gohmaneffb8942008-09-12 16:56:44 +0000434 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000435}
436
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000437static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000438 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattner45640392005-08-19 22:38:53 +0000439}
440
Chris Lattner97b3da12006-06-27 00:04:13 +0000441/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
442/// operand. If so Imm will receive the 32-bit value.
443static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000444 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000445 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Nate Begeman72d6f882005-08-18 05:00:13 +0000446 return true;
447 }
448 return false;
449}
450
Chris Lattner97b3da12006-06-27 00:04:13 +0000451/// isInt64Immediate - This method tests to see if the node is a 64-bit constant
452/// operand. If so Imm will receive the 64-bit value.
453static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
Owen Anderson9f944592009-08-11 20:47:22 +0000454 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
Dan Gohmaneffb8942008-09-12 16:56:44 +0000455 Imm = cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattner97b3da12006-06-27 00:04:13 +0000456 return true;
457 }
458 return false;
459}
460
461// isInt32Immediate - This method tests to see if a constant operand.
462// If so Imm will receive the 32 bit value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000463static bool isInt32Immediate(SDValue N, unsigned &Imm) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000464 return isInt32Immediate(N.getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000465}
466
Hal Finkel65539e32015-12-12 00:32:00 +0000467static unsigned getBranchHint(unsigned PCC, FunctionLoweringInfo *FuncInfo,
468 const SDValue &DestMBB) {
469 assert(isa<BasicBlockSDNode>(DestMBB));
470
471 if (!FuncInfo->BPI) return PPC::BR_NO_HINT;
472
473 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock();
474 const TerminatorInst *BBTerm = BB->getTerminator();
475
476 if (BBTerm->getNumSuccessors() != 2) return PPC::BR_NO_HINT;
477
478 const BasicBlock *TBB = BBTerm->getSuccessor(0);
479 const BasicBlock *FBB = BBTerm->getSuccessor(1);
480
Cong Houe93b8e12015-12-22 18:56:14 +0000481 auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);
482 auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);
Hal Finkel65539e32015-12-12 00:32:00 +0000483
484 // We only want to handle cases which are easy to predict at static time, e.g.
485 // C++ throw statement, that is very likely not taken, or calling never
486 // returned function, e.g. stdlib exit(). So we set Threshold to filter
487 // unwanted cases.
488 //
489 // Below is LLVM branch weight table, we only want to handle case 1, 2
490 //
491 // Case Taken:Nontaken Example
492 // 1. Unreachable 1048575:1 C++ throw, stdlib exit(),
493 // 2. Invoke-terminating 1:1048575
494 // 3. Coldblock 4:64 __builtin_expect
495 // 4. Loop Branch 124:4 For loop
496 // 5. PH/ZH/FPH 20:12
497 const uint32_t Threshold = 10000;
498
Cong Houe93b8e12015-12-22 18:56:14 +0000499 if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))
Hal Finkel65539e32015-12-12 00:32:00 +0000500 return PPC::BR_NO_HINT;
501
502 DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName() << "::"
503 << BB->getName() << "'\n"
Cong Houe93b8e12015-12-22 18:56:14 +0000504 << " -> " << TBB->getName() << ": " << TProb << "\n"
505 << " -> " << FBB->getName() << ": " << FProb << "\n");
Hal Finkel65539e32015-12-12 00:32:00 +0000506
507 const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);
508
Cong Houe93b8e12015-12-22 18:56:14 +0000509 // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities,
510 // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock
Hal Finkel65539e32015-12-12 00:32:00 +0000511 if (BBDN->getBasicBlock()->getBasicBlock() != TBB)
Cong Houe93b8e12015-12-22 18:56:14 +0000512 std::swap(TProb, FProb);
Hal Finkel65539e32015-12-12 00:32:00 +0000513
Cong Houe93b8e12015-12-22 18:56:14 +0000514 return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;
Hal Finkel65539e32015-12-12 00:32:00 +0000515}
Chris Lattner97b3da12006-06-27 00:04:13 +0000516
517// isOpcWithIntImmediate - This method tests to see if the node is a specific
518// opcode and that it has a immediate integer right operand.
519// If so Imm will receive the 32 bit value.
520static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
Gabor Greif81d6a382008-08-31 15:37:04 +0000521 return N->getOpcode() == Opc
522 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
Chris Lattner97b3da12006-06-27 00:04:13 +0000523}
524
Justin Bognerdc8af062016-05-20 21:43:23 +0000525void PPCDAGToDAGISel::selectFrameIndex(SDNode *SN, SDNode *N, unsigned Offset) {
Hal Finkelb5e9b042014-12-11 22:51:06 +0000526 SDLoc dl(SN);
527 int FI = cast<FrameIndexSDNode>(N)->getIndex();
528 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
529 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
530 if (SN->hasOneUse())
Justin Bognerdc8af062016-05-20 21:43:23 +0000531 CurDAG->SelectNodeTo(SN, Opc, N->getValueType(0), TFI,
532 getSmallIPtrImm(Offset, dl));
533 else
534 ReplaceNode(SN, CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
535 getSmallIPtrImm(Offset, dl)));
Hal Finkelb5e9b042014-12-11 22:51:06 +0000536}
537
Andrew Trickc416ba62010-12-24 04:28:06 +0000538bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
539 bool isShiftMask, unsigned &SH,
Nate Begemand31efd12006-09-22 05:01:56 +0000540 unsigned &MB, unsigned &ME) {
Nate Begeman92e77502005-10-19 00:05:37 +0000541 // Don't even go down this path for i64, since different logic will be
542 // necessary for rldicl/rldicr/rldimi.
Owen Anderson9f944592009-08-11 20:47:22 +0000543 if (N->getValueType(0) != MVT::i32)
Nate Begeman92e77502005-10-19 00:05:37 +0000544 return false;
545
Nate Begemanb3821a32005-08-18 07:30:46 +0000546 unsigned Shift = 32;
547 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
548 unsigned Opcode = N->getOpcode();
Chris Lattnere413b602005-08-30 00:59:16 +0000549 if (N->getNumOperands() != 2 ||
Gabor Greiff304a7a2008-08-28 21:40:38 +0000550 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
Nate Begemanb3821a32005-08-18 07:30:46 +0000551 return false;
Andrew Trickc416ba62010-12-24 04:28:06 +0000552
Nate Begemanb3821a32005-08-18 07:30:46 +0000553 if (Opcode == ISD::SHL) {
554 // apply shift left to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000555 if (isShiftMask) Mask = Mask << Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000556 // determine which bits are made indeterminant by shift
557 Indeterminant = ~(0xFFFFFFFFu << Shift);
Andrew Trickc416ba62010-12-24 04:28:06 +0000558 } else if (Opcode == ISD::SRL) {
Nate Begemanb3821a32005-08-18 07:30:46 +0000559 // apply shift right to mask if it comes first
Dale Johannesen86dcae12009-11-24 01:09:07 +0000560 if (isShiftMask) Mask = Mask >> Shift;
Nate Begemanb3821a32005-08-18 07:30:46 +0000561 // determine which bits are made indeterminant by shift
562 Indeterminant = ~(0xFFFFFFFFu >> Shift);
563 // adjust for the left rotate
564 Shift = 32 - Shift;
Nate Begemand31efd12006-09-22 05:01:56 +0000565 } else if (Opcode == ISD::ROTL) {
566 Indeterminant = 0;
Nate Begemanb3821a32005-08-18 07:30:46 +0000567 } else {
568 return false;
569 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000570
Nate Begemanb3821a32005-08-18 07:30:46 +0000571 // if the mask doesn't intersect any Indeterminant bits
572 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnera2963392006-05-12 16:29:37 +0000573 SH = Shift & 31;
Nate Begemanb3821a32005-08-18 07:30:46 +0000574 // make sure the mask is still a mask (wrap arounds may not be)
575 return isRunOfOnes(Mask, MB, ME);
576 }
577 return false;
578}
579
Justin Bognerdc8af062016-05-20 21:43:23 +0000580/// Turn an or of two masked values into the rotate left word immediate then
581/// mask insert (rlwimi) instruction.
582bool PPCDAGToDAGISel::tryBitfieldInsert(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000583 SDValue Op0 = N->getOperand(0);
584 SDValue Op1 = N->getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000585 SDLoc dl(N);
Andrew Trickc416ba62010-12-24 04:28:06 +0000586
Craig Topperd0af7e82017-04-28 05:31:46 +0000587 KnownBits LKnown, RKnown;
588 CurDAG->computeKnownBits(Op0, LKnown);
589 CurDAG->computeKnownBits(Op1, RKnown);
Andrew Trickc416ba62010-12-24 04:28:06 +0000590
Craig Topperd0af7e82017-04-28 05:31:46 +0000591 unsigned TargetMask = LKnown.Zero.getZExtValue();
592 unsigned InsertMask = RKnown.Zero.getZExtValue();
Andrew Trickc416ba62010-12-24 04:28:06 +0000593
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000594 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
595 unsigned Op0Opc = Op0.getOpcode();
596 unsigned Op1Opc = Op1.getOpcode();
597 unsigned Value, SH = 0;
598 TargetMask = ~TargetMask;
599 InsertMask = ~InsertMask;
Nate Begeman1333cea2006-05-07 00:23:38 +0000600
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000601 // If the LHS has a foldable shift and the RHS does not, then swap it to the
602 // RHS so that we can fold the shift into the insert.
Nate Begeman1333cea2006-05-07 00:23:38 +0000603 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
604 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
605 Op0.getOperand(0).getOpcode() == ISD::SRL) {
606 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
607 Op1.getOperand(0).getOpcode() != ISD::SRL) {
608 std::swap(Op0, Op1);
609 std::swap(Op0Opc, Op1Opc);
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000610 std::swap(TargetMask, InsertMask);
Nate Begeman1333cea2006-05-07 00:23:38 +0000611 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000612 }
Nate Begeman9b6d4c22006-05-08 17:38:32 +0000613 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
614 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
615 Op1.getOperand(0).getOpcode() != ISD::SRL) {
616 std::swap(Op0, Op1);
617 std::swap(Op0Opc, Op1Opc);
618 std::swap(TargetMask, InsertMask);
619 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000620 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000621
Nate Begeman1333cea2006-05-07 00:23:38 +0000622 unsigned MB, ME;
Hal Finkelff3ea802013-07-11 16:31:51 +0000623 if (isRunOfOnes(InsertMask, MB, ME)) {
Dale Johannesen8495a502009-11-20 22:16:40 +0000624 SDValue Tmp1, Tmp2;
Nate Begeman1333cea2006-05-07 00:23:38 +0000625
626 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000627 isInt32Immediate(Op1.getOperand(1), Value)) {
Nate Begeman1333cea2006-05-07 00:23:38 +0000628 Op1 = Op1.getOperand(0);
629 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
630 }
631 if (Op1Opc == ISD::AND) {
Hal Finkeld9963c72014-04-13 17:10:58 +0000632 // The AND mask might not be a constant, and we need to make sure that
633 // if we're going to fold the masking with the insert, all bits not
634 // know to be zero in the mask are known to be one.
Craig Topperd0af7e82017-04-28 05:31:46 +0000635 KnownBits MKnown;
636 CurDAG->computeKnownBits(Op1.getOperand(1), MKnown);
637 bool CanFoldMask = InsertMask == MKnown.One.getZExtValue();
Hal Finkeld9963c72014-04-13 17:10:58 +0000638
Nate Begeman1333cea2006-05-07 00:23:38 +0000639 unsigned SHOpc = Op1.getOperand(0).getOpcode();
Hal Finkeld9963c72014-04-13 17:10:58 +0000640 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) && CanFoldMask &&
Chris Lattner97b3da12006-06-27 00:04:13 +0000641 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
Eric Christopher02e18042014-05-14 00:31:15 +0000642 // Note that Value must be in range here (less than 32) because
643 // otherwise there would not be any bits set in InsertMask.
Nate Begeman1333cea2006-05-07 00:23:38 +0000644 Op1 = Op1.getOperand(0).getOperand(0);
645 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
Nate Begeman1333cea2006-05-07 00:23:38 +0000646 }
647 }
Dale Johannesen8495a502009-11-20 22:16:40 +0000648
Chris Lattnera2963392006-05-12 16:29:37 +0000649 SH &= 31;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000650 SDValue Ops[] = { Op0, Op1, getI32Imm(SH, dl), getI32Imm(MB, dl),
651 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +0000652 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
653 return true;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000654 }
Nate Begeman93c4bc62005-08-19 00:38:14 +0000655 }
Justin Bognerdc8af062016-05-20 21:43:23 +0000656 return false;
Nate Begeman93c4bc62005-08-19 00:38:14 +0000657}
658
Hal Finkelc58ce412015-01-01 02:53:29 +0000659// Predict the number of instructions that would be generated by calling
Justin Bognerdc8af062016-05-20 21:43:23 +0000660// getInt64(N).
661static unsigned getInt64CountDirect(int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000662 // Assume no remaining bits.
663 unsigned Remainder = 0;
664 // Assume no shift required.
665 unsigned Shift = 0;
666
667 // If it can't be represented as a 32 bit value.
668 if (!isInt<32>(Imm)) {
669 Shift = countTrailingZeros<uint64_t>(Imm);
670 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
671
672 // If the shifted value fits 32 bits.
673 if (isInt<32>(ImmSh)) {
674 // Go with the shifted value.
675 Imm = ImmSh;
676 } else {
677 // Still stuck with a 64 bit value.
678 Remainder = Imm;
679 Shift = 32;
680 Imm >>= 32;
681 }
682 }
683
684 // Intermediate operand.
685 unsigned Result = 0;
686
687 // Handle first 32 bits.
688 unsigned Lo = Imm & 0xFFFF;
Hal Finkelc58ce412015-01-01 02:53:29 +0000689
690 // Simple value.
691 if (isInt<16>(Imm)) {
692 // Just the Lo bits.
693 ++Result;
694 } else if (Lo) {
695 // Handle the Hi bits and Lo bits.
696 Result += 2;
697 } else {
698 // Just the Hi bits.
699 ++Result;
700 }
701
702 // If no shift, we're done.
703 if (!Shift) return Result;
704
Guozhi Wei0cd65422016-10-14 20:41:50 +0000705 // If Hi word == Lo word,
706 // we can use rldimi to insert the Lo word into Hi word.
707 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
708 ++Result;
709 return Result;
710 }
711
Hal Finkelc58ce412015-01-01 02:53:29 +0000712 // Shift for next step if the upper 32-bits were not zero.
713 if (Imm)
714 ++Result;
715
716 // Add in the last bits as required.
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000717 if ((Remainder >> 16) & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000718 ++Result;
Tilmann Scheller990a8d82015-11-10 12:29:37 +0000719 if (Remainder & 0xFFFF)
Hal Finkelc58ce412015-01-01 02:53:29 +0000720 ++Result;
721
722 return Result;
723}
724
Hal Finkel241ba792015-01-04 15:43:55 +0000725static uint64_t Rot64(uint64_t Imm, unsigned R) {
726 return (Imm << R) | (Imm >> (64 - R));
727}
728
Justin Bognerdc8af062016-05-20 21:43:23 +0000729static unsigned getInt64Count(int64_t Imm) {
730 unsigned Count = getInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000731 if (Count == 1)
732 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000733
Hal Finkel241ba792015-01-04 15:43:55 +0000734 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000735 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000736 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000737 Count = std::min(Count, RCount);
738
Justin Bognerdc8af062016-05-20 21:43:23 +0000739 // See comments in getInt64 for an explanation of the logic below.
Hal Finkel2f618792015-01-05 03:41:38 +0000740 unsigned LS = findLastSet(RImm);
741 if (LS != r-1)
742 continue;
743
744 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
745 uint64_t RImmWithOnes = RImm | OnesMask;
746
Justin Bognerdc8af062016-05-20 21:43:23 +0000747 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000748 Count = std::min(Count, RCount);
749 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000750
Hal Finkel241ba792015-01-04 15:43:55 +0000751 return Count;
Hal Finkelca6375f2015-01-04 12:35:03 +0000752}
753
Justin Bognerdc8af062016-05-20 21:43:23 +0000754// Select a 64-bit constant. For cost-modeling purposes, getInt64Count
Hal Finkelc58ce412015-01-01 02:53:29 +0000755// (above) needs to be kept in sync with this function.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000756static SDNode *getInt64Direct(SelectionDAG *CurDAG, const SDLoc &dl,
757 int64_t Imm) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000758 // Assume no remaining bits.
759 unsigned Remainder = 0;
760 // Assume no shift required.
761 unsigned Shift = 0;
762
763 // If it can't be represented as a 32 bit value.
764 if (!isInt<32>(Imm)) {
765 Shift = countTrailingZeros<uint64_t>(Imm);
766 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
767
768 // If the shifted value fits 32 bits.
769 if (isInt<32>(ImmSh)) {
770 // Go with the shifted value.
771 Imm = ImmSh;
772 } else {
773 // Still stuck with a 64 bit value.
774 Remainder = Imm;
775 Shift = 32;
776 Imm >>= 32;
777 }
778 }
779
780 // Intermediate operand.
781 SDNode *Result;
782
783 // Handle first 32 bits.
784 unsigned Lo = Imm & 0xFFFF;
785 unsigned Hi = (Imm >> 16) & 0xFFFF;
786
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000787 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
788 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkelc58ce412015-01-01 02:53:29 +0000789 };
790
791 // Simple value.
792 if (isInt<16>(Imm)) {
793 // Just the Lo bits.
794 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
795 } else if (Lo) {
796 // Handle the Hi bits.
797 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
798 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
799 // And Lo bits.
800 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
801 SDValue(Result, 0), getI32Imm(Lo));
802 } else {
803 // Just the Hi bits.
804 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
805 }
806
807 // If no shift, we're done.
808 if (!Shift) return Result;
809
Guozhi Wei0cd65422016-10-14 20:41:50 +0000810 // If Hi word == Lo word,
811 // we can use rldimi to insert the Lo word into Hi word.
812 if ((unsigned)(Imm & 0xFFFFFFFF) == Remainder) {
813 SDValue Ops[] =
814 { SDValue(Result, 0), SDValue(Result, 0), getI32Imm(Shift), getI32Imm(0)};
815 return CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops);
816 }
817
Hal Finkelc58ce412015-01-01 02:53:29 +0000818 // Shift for next step if the upper 32-bits were not zero.
819 if (Imm) {
820 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
821 SDValue(Result, 0),
822 getI32Imm(Shift),
823 getI32Imm(63 - Shift));
824 }
825
826 // Add in the last bits as required.
827 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
828 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
829 SDValue(Result, 0), getI32Imm(Hi));
830 }
831 if ((Lo = Remainder & 0xFFFF)) {
832 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
833 SDValue(Result, 0), getI32Imm(Lo));
834 }
835
836 return Result;
837}
838
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000839static SDNode *getInt64(SelectionDAG *CurDAG, const SDLoc &dl, int64_t Imm) {
Justin Bognerdc8af062016-05-20 21:43:23 +0000840 unsigned Count = getInt64CountDirect(Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000841 if (Count == 1)
Justin Bognerdc8af062016-05-20 21:43:23 +0000842 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel2f618792015-01-05 03:41:38 +0000843
Hal Finkel241ba792015-01-04 15:43:55 +0000844 unsigned RMin = 0;
Hal Finkelca6375f2015-01-04 12:35:03 +0000845
Hal Finkel2f618792015-01-05 03:41:38 +0000846 int64_t MatImm;
847 unsigned MaskEnd;
848
Hal Finkel241ba792015-01-04 15:43:55 +0000849 for (unsigned r = 1; r < 63; ++r) {
Hal Finkel2f618792015-01-05 03:41:38 +0000850 uint64_t RImm = Rot64(Imm, r);
Justin Bognerdc8af062016-05-20 21:43:23 +0000851 unsigned RCount = getInt64CountDirect(RImm) + 1;
Hal Finkel241ba792015-01-04 15:43:55 +0000852 if (RCount < Count) {
853 Count = RCount;
854 RMin = r;
Hal Finkel2f618792015-01-05 03:41:38 +0000855 MatImm = RImm;
856 MaskEnd = 63;
857 }
858
859 // If the immediate to generate has many trailing zeros, it might be
860 // worthwhile to generate a rotated value with too many leading ones
861 // (because that's free with li/lis's sign-extension semantics), and then
862 // mask them off after rotation.
863
864 unsigned LS = findLastSet(RImm);
865 // We're adding (63-LS) higher-order ones, and we expect to mask them off
866 // after performing the inverse rotation by (64-r). So we need that:
867 // 63-LS == 64-r => LS == r-1
868 if (LS != r-1)
869 continue;
870
871 uint64_t OnesMask = -(int64_t) (UINT64_C(1) << (LS+1));
872 uint64_t RImmWithOnes = RImm | OnesMask;
873
Justin Bognerdc8af062016-05-20 21:43:23 +0000874 RCount = getInt64CountDirect(RImmWithOnes) + 1;
Hal Finkel2f618792015-01-05 03:41:38 +0000875 if (RCount < Count) {
876 Count = RCount;
877 RMin = r;
878 MatImm = RImmWithOnes;
879 MaskEnd = LS;
Hal Finkel241ba792015-01-04 15:43:55 +0000880 }
Hal Finkelca6375f2015-01-04 12:35:03 +0000881 }
882
Hal Finkel241ba792015-01-04 15:43:55 +0000883 if (!RMin)
Justin Bognerdc8af062016-05-20 21:43:23 +0000884 return getInt64Direct(CurDAG, dl, Imm);
Hal Finkel241ba792015-01-04 15:43:55 +0000885
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000886 auto getI32Imm = [CurDAG, dl](unsigned Imm) {
887 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel241ba792015-01-04 15:43:55 +0000888 };
889
Justin Bognerdc8af062016-05-20 21:43:23 +0000890 SDValue Val = SDValue(getInt64Direct(CurDAG, dl, MatImm), 0);
Hal Finkel2f618792015-01-05 03:41:38 +0000891 return CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Val,
892 getI32Imm(64 - RMin), getI32Imm(MaskEnd));
Hal Finkelca6375f2015-01-04 12:35:03 +0000893}
894
Hal Finkelc58ce412015-01-01 02:53:29 +0000895// Select a 64-bit constant.
Justin Bognerdc8af062016-05-20 21:43:23 +0000896static SDNode *getInt64(SelectionDAG *CurDAG, SDNode *N) {
Hal Finkelc58ce412015-01-01 02:53:29 +0000897 SDLoc dl(N);
898
899 // Get 64 bit value.
900 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
Justin Bognerdc8af062016-05-20 21:43:23 +0000901 return getInt64(CurDAG, dl, Imm);
Hal Finkelc58ce412015-01-01 02:53:29 +0000902}
903
Hal Finkel8adf2252014-12-16 05:51:41 +0000904namespace {
Eugene Zelenko8187c192017-01-13 00:58:58 +0000905
Hal Finkel8adf2252014-12-16 05:51:41 +0000906class BitPermutationSelector {
907 struct ValueBit {
908 SDValue V;
909
910 // The bit number in the value, using a convention where bit 0 is the
911 // lowest-order bit.
912 unsigned Idx;
913
914 enum Kind {
915 ConstZero,
916 Variable
917 } K;
918
919 ValueBit(SDValue V, unsigned I, Kind K = Variable)
920 : V(V), Idx(I), K(K) {}
921 ValueBit(Kind K = Variable)
922 : V(SDValue(nullptr, 0)), Idx(UINT32_MAX), K(K) {}
923
924 bool isZero() const {
925 return K == ConstZero;
926 }
927
928 bool hasValue() const {
929 return K == Variable;
930 }
931
932 SDValue getValue() const {
933 assert(hasValue() && "Cannot get the value of a constant bit");
934 return V;
935 }
936
937 unsigned getValueBitIndex() const {
938 assert(hasValue() && "Cannot get the value bit index of a constant bit");
939 return Idx;
940 }
941 };
942
943 // A bit group has the same underlying value and the same rotate factor.
944 struct BitGroup {
945 SDValue V;
946 unsigned RLAmt;
947 unsigned StartIdx, EndIdx;
948
Hal Finkelc58ce412015-01-01 02:53:29 +0000949 // This rotation amount assumes that the lower 32 bits of the quantity are
950 // replicated in the high 32 bits by the rotation operator (which is done
951 // by rlwinm and friends in 64-bit mode).
952 bool Repl32;
953 // Did converting to Repl32 == true change the rotation factor? If it did,
954 // it decreased it by 32.
955 bool Repl32CR;
956 // Was this group coalesced after setting Repl32 to true?
957 bool Repl32Coalesced;
958
Hal Finkel8adf2252014-12-16 05:51:41 +0000959 BitGroup(SDValue V, unsigned R, unsigned S, unsigned E)
Hal Finkelc58ce412015-01-01 02:53:29 +0000960 : V(V), RLAmt(R), StartIdx(S), EndIdx(E), Repl32(false), Repl32CR(false),
961 Repl32Coalesced(false) {
Hal Finkel8adf2252014-12-16 05:51:41 +0000962 DEBUG(dbgs() << "\tbit group for " << V.getNode() << " RLAmt = " << R <<
963 " [" << S << ", " << E << "]\n");
964 }
965 };
966
967 // Information on each (Value, RLAmt) pair (like the number of groups
968 // associated with each) used to choose the lowering method.
969 struct ValueRotInfo {
970 SDValue V;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000971 unsigned RLAmt = std::numeric_limits<unsigned>::max();
972 unsigned NumGroups = 0;
973 unsigned FirstGroupStartIdx = std::numeric_limits<unsigned>::max();
974 bool Repl32 = false;
Hal Finkel8adf2252014-12-16 05:51:41 +0000975
Eugene Zelenko8187c192017-01-13 00:58:58 +0000976 ValueRotInfo() = default;
Hal Finkel8adf2252014-12-16 05:51:41 +0000977
978 // For sorting (in reverse order) by NumGroups, and then by
979 // FirstGroupStartIdx.
980 bool operator < (const ValueRotInfo &Other) const {
Hal Finkelc58ce412015-01-01 02:53:29 +0000981 // We need to sort so that the non-Repl32 come first because, when we're
982 // doing masking, the Repl32 bit groups might be subsumed into the 64-bit
983 // masking operation.
984 if (Repl32 < Other.Repl32)
985 return true;
986 else if (Repl32 > Other.Repl32)
987 return false;
988 else if (NumGroups > Other.NumGroups)
Hal Finkel8adf2252014-12-16 05:51:41 +0000989 return true;
990 else if (NumGroups < Other.NumGroups)
991 return false;
992 else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
993 return true;
994 return false;
995 }
996 };
997
Tim Shendc698c32016-08-12 18:40:04 +0000998 using ValueBitsMemoizedValue = std::pair<bool, SmallVector<ValueBit, 64>>;
999 using ValueBitsMemoizer =
1000 DenseMap<SDValue, std::unique_ptr<ValueBitsMemoizedValue>>;
1001 ValueBitsMemoizer Memoizer;
1002
1003 // Return a pair of bool and a SmallVector pointer to a memoization entry.
1004 // The bool is true if something interesting was deduced, otherwise if we're
Hal Finkel8adf2252014-12-16 05:51:41 +00001005 // providing only a generic representation of V (or something else likewise
Tim Shendc698c32016-08-12 18:40:04 +00001006 // uninteresting for instruction selection) through the SmallVector.
1007 std::pair<bool, SmallVector<ValueBit, 64> *> getValueBits(SDValue V,
1008 unsigned NumBits) {
1009 auto &ValueEntry = Memoizer[V];
1010 if (ValueEntry)
1011 return std::make_pair(ValueEntry->first, &ValueEntry->second);
1012 ValueEntry.reset(new ValueBitsMemoizedValue());
1013 bool &Interesting = ValueEntry->first;
1014 SmallVector<ValueBit, 64> &Bits = ValueEntry->second;
1015 Bits.resize(NumBits);
1016
Hal Finkel8adf2252014-12-16 05:51:41 +00001017 switch (V.getOpcode()) {
1018 default: break;
1019 case ISD::ROTL:
1020 if (isa<ConstantSDNode>(V.getOperand(1))) {
1021 unsigned RotAmt = V.getConstantOperandVal(1);
1022
Tim Shendc698c32016-08-12 18:40:04 +00001023 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001024
Tim Shendc698c32016-08-12 18:40:04 +00001025 for (unsigned i = 0; i < NumBits; ++i)
1026 Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt];
Hal Finkel8adf2252014-12-16 05:51:41 +00001027
Tim Shendc698c32016-08-12 18:40:04 +00001028 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001029 }
1030 break;
1031 case ISD::SHL:
1032 if (isa<ConstantSDNode>(V.getOperand(1))) {
1033 unsigned ShiftAmt = V.getConstantOperandVal(1);
1034
Tim Shendc698c32016-08-12 18:40:04 +00001035 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001036
Tim Shendc698c32016-08-12 18:40:04 +00001037 for (unsigned i = ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001038 Bits[i] = LHSBits[i - ShiftAmt];
1039
1040 for (unsigned i = 0; i < ShiftAmt; ++i)
1041 Bits[i] = ValueBit(ValueBit::ConstZero);
1042
Tim Shendc698c32016-08-12 18:40:04 +00001043 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001044 }
1045 break;
1046 case ISD::SRL:
1047 if (isa<ConstantSDNode>(V.getOperand(1))) {
1048 unsigned ShiftAmt = V.getConstantOperandVal(1);
1049
Tim Shendc698c32016-08-12 18:40:04 +00001050 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001051
Tim Shendc698c32016-08-12 18:40:04 +00001052 for (unsigned i = 0; i < NumBits - ShiftAmt; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001053 Bits[i] = LHSBits[i + ShiftAmt];
1054
Tim Shendc698c32016-08-12 18:40:04 +00001055 for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001056 Bits[i] = ValueBit(ValueBit::ConstZero);
1057
Tim Shendc698c32016-08-12 18:40:04 +00001058 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001059 }
1060 break;
1061 case ISD::AND:
1062 if (isa<ConstantSDNode>(V.getOperand(1))) {
1063 uint64_t Mask = V.getConstantOperandVal(1);
1064
Tim Shendc698c32016-08-12 18:40:04 +00001065 const SmallVector<ValueBit, 64> *LHSBits;
Hal Finkel8adf2252014-12-16 05:51:41 +00001066 // Mark this as interesting, only if the LHS was also interesting. This
1067 // prevents the overall procedure from matching a single immediate 'and'
1068 // (which is non-optimal because such an and might be folded with other
1069 // things if we don't select it here).
Tim Shendc698c32016-08-12 18:40:04 +00001070 std::tie(Interesting, LHSBits) = getValueBits(V.getOperand(0), NumBits);
1071
1072 for (unsigned i = 0; i < NumBits; ++i)
1073 if (((Mask >> i) & 1) == 1)
1074 Bits[i] = (*LHSBits)[i];
1075 else
1076 Bits[i] = ValueBit(ValueBit::ConstZero);
1077
1078 return std::make_pair(Interesting, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001079 }
1080 break;
1081 case ISD::OR: {
Tim Shendc698c32016-08-12 18:40:04 +00001082 const auto &LHSBits = *getValueBits(V.getOperand(0), NumBits).second;
1083 const auto &RHSBits = *getValueBits(V.getOperand(1), NumBits).second;
Hal Finkel8adf2252014-12-16 05:51:41 +00001084
1085 bool AllDisjoint = true;
Tim Shendc698c32016-08-12 18:40:04 +00001086 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001087 if (LHSBits[i].isZero())
1088 Bits[i] = RHSBits[i];
1089 else if (RHSBits[i].isZero())
1090 Bits[i] = LHSBits[i];
1091 else {
1092 AllDisjoint = false;
1093 break;
1094 }
1095
1096 if (!AllDisjoint)
1097 break;
1098
Tim Shendc698c32016-08-12 18:40:04 +00001099 return std::make_pair(Interesting = true, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001100 }
1101 }
1102
Tim Shendc698c32016-08-12 18:40:04 +00001103 for (unsigned i = 0; i < NumBits; ++i)
Hal Finkel8adf2252014-12-16 05:51:41 +00001104 Bits[i] = ValueBit(V, i);
1105
Tim Shendc698c32016-08-12 18:40:04 +00001106 return std::make_pair(Interesting = false, &Bits);
Hal Finkel8adf2252014-12-16 05:51:41 +00001107 }
1108
1109 // For each value (except the constant ones), compute the left-rotate amount
1110 // to get it from its original to final position.
1111 void computeRotationAmounts() {
1112 HasZeros = false;
1113 RLAmt.resize(Bits.size());
1114 for (unsigned i = 0; i < Bits.size(); ++i)
1115 if (Bits[i].hasValue()) {
1116 unsigned VBI = Bits[i].getValueBitIndex();
1117 if (i >= VBI)
1118 RLAmt[i] = i - VBI;
1119 else
1120 RLAmt[i] = Bits.size() - (VBI - i);
1121 } else if (Bits[i].isZero()) {
1122 HasZeros = true;
1123 RLAmt[i] = UINT32_MAX;
1124 } else {
1125 llvm_unreachable("Unknown value bit type");
1126 }
1127 }
1128
1129 // Collect groups of consecutive bits with the same underlying value and
Hal Finkelc58ce412015-01-01 02:53:29 +00001130 // rotation factor. If we're doing late masking, we ignore zeros, otherwise
1131 // they break up groups.
1132 void collectBitGroups(bool LateMask) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001133 BitGroups.clear();
1134
1135 unsigned LastRLAmt = RLAmt[0];
1136 SDValue LastValue = Bits[0].hasValue() ? Bits[0].getValue() : SDValue();
1137 unsigned LastGroupStartIdx = 0;
1138 for (unsigned i = 1; i < Bits.size(); ++i) {
1139 unsigned ThisRLAmt = RLAmt[i];
1140 SDValue ThisValue = Bits[i].hasValue() ? Bits[i].getValue() : SDValue();
Hal Finkelc58ce412015-01-01 02:53:29 +00001141 if (LateMask && !ThisValue) {
1142 ThisValue = LastValue;
1143 ThisRLAmt = LastRLAmt;
1144 // If we're doing late masking, then the first bit group always starts
1145 // at zero (even if the first bits were zero).
1146 if (BitGroups.empty())
1147 LastGroupStartIdx = 0;
1148 }
Hal Finkel8adf2252014-12-16 05:51:41 +00001149
1150 // If this bit has the same underlying value and the same rotate factor as
1151 // the last one, then they're part of the same group.
1152 if (ThisRLAmt == LastRLAmt && ThisValue == LastValue)
1153 continue;
1154
1155 if (LastValue.getNode())
1156 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1157 i-1));
1158 LastRLAmt = ThisRLAmt;
1159 LastValue = ThisValue;
1160 LastGroupStartIdx = i;
1161 }
1162 if (LastValue.getNode())
1163 BitGroups.push_back(BitGroup(LastValue, LastRLAmt, LastGroupStartIdx,
1164 Bits.size()-1));
1165
1166 if (BitGroups.empty())
1167 return;
1168
1169 // We might be able to combine the first and last groups.
1170 if (BitGroups.size() > 1) {
1171 // If the first and last groups are the same, then remove the first group
1172 // in favor of the last group, making the ending index of the last group
1173 // equal to the ending index of the to-be-removed first group.
1174 if (BitGroups[0].StartIdx == 0 &&
1175 BitGroups[BitGroups.size()-1].EndIdx == Bits.size()-1 &&
1176 BitGroups[0].V == BitGroups[BitGroups.size()-1].V &&
1177 BitGroups[0].RLAmt == BitGroups[BitGroups.size()-1].RLAmt) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001178 DEBUG(dbgs() << "\tcombining final bit group with initial one\n");
Hal Finkel8adf2252014-12-16 05:51:41 +00001179 BitGroups[BitGroups.size()-1].EndIdx = BitGroups[0].EndIdx;
1180 BitGroups.erase(BitGroups.begin());
1181 }
1182 }
1183 }
1184
1185 // Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1186 // associated with each. If there is a degeneracy, pick the one that occurs
1187 // first (in the final value).
1188 void collectValueRotInfo() {
1189 ValueRots.clear();
1190
1191 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001192 unsigned RLAmtKey = BG.RLAmt + (BG.Repl32 ? 64 : 0);
1193 ValueRotInfo &VRI = ValueRots[std::make_pair(BG.V, RLAmtKey)];
Hal Finkel8adf2252014-12-16 05:51:41 +00001194 VRI.V = BG.V;
1195 VRI.RLAmt = BG.RLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001196 VRI.Repl32 = BG.Repl32;
Hal Finkel8adf2252014-12-16 05:51:41 +00001197 VRI.NumGroups += 1;
1198 VRI.FirstGroupStartIdx = std::min(VRI.FirstGroupStartIdx, BG.StartIdx);
1199 }
1200
1201 // Now that we've collected the various ValueRotInfo instances, we need to
1202 // sort them.
1203 ValueRotsVec.clear();
1204 for (auto &I : ValueRots) {
1205 ValueRotsVec.push_back(I.second);
1206 }
1207 std::sort(ValueRotsVec.begin(), ValueRotsVec.end());
1208 }
1209
Hal Finkelc58ce412015-01-01 02:53:29 +00001210 // In 64-bit mode, rlwinm and friends have a rotation operator that
1211 // replicates the low-order 32 bits into the high-order 32-bits. The mask
1212 // indices of these instructions can only be in the lower 32 bits, so they
1213 // can only represent some 64-bit bit groups. However, when they can be used,
1214 // the 32-bit replication can be used to represent, as a single bit group,
1215 // otherwise separate bit groups. We'll convert to replicated-32-bit bit
1216 // groups when possible. Returns true if any of the bit groups were
1217 // converted.
1218 void assignRepl32BitGroups() {
1219 // If we have bits like this:
1220 //
1221 // Indices: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1222 // V bits: ... 7 6 5 4 3 2 1 0 31 30 29 28 27 26 25 24
1223 // Groups: | RLAmt = 8 | RLAmt = 40 |
1224 //
1225 // But, making use of a 32-bit operation that replicates the low-order 32
1226 // bits into the high-order 32 bits, this can be one bit group with a RLAmt
1227 // of 8.
1228
1229 auto IsAllLow32 = [this](BitGroup & BG) {
1230 if (BG.StartIdx <= BG.EndIdx) {
1231 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i) {
1232 if (!Bits[i].hasValue())
1233 continue;
1234 if (Bits[i].getValueBitIndex() >= 32)
1235 return false;
1236 }
1237 } else {
1238 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i) {
1239 if (!Bits[i].hasValue())
1240 continue;
1241 if (Bits[i].getValueBitIndex() >= 32)
1242 return false;
1243 }
1244 for (unsigned i = 0; i <= BG.EndIdx; ++i) {
1245 if (!Bits[i].hasValue())
1246 continue;
1247 if (Bits[i].getValueBitIndex() >= 32)
1248 return false;
1249 }
1250 }
1251
1252 return true;
1253 };
1254
1255 for (auto &BG : BitGroups) {
1256 if (BG.StartIdx < 32 && BG.EndIdx < 32) {
1257 if (IsAllLow32(BG)) {
1258 if (BG.RLAmt >= 32) {
1259 BG.RLAmt -= 32;
1260 BG.Repl32CR = true;
1261 }
1262
1263 BG.Repl32 = true;
1264
1265 DEBUG(dbgs() << "\t32-bit replicated bit group for " <<
1266 BG.V.getNode() << " RLAmt = " << BG.RLAmt <<
1267 " [" << BG.StartIdx << ", " << BG.EndIdx << "]\n");
1268 }
1269 }
1270 }
1271
1272 // Now walk through the bit groups, consolidating where possible.
1273 for (auto I = BitGroups.begin(); I != BitGroups.end();) {
1274 // We might want to remove this bit group by merging it with the previous
1275 // group (which might be the ending group).
1276 auto IP = (I == BitGroups.begin()) ?
1277 std::prev(BitGroups.end()) : std::prev(I);
1278 if (I->Repl32 && IP->Repl32 && I->V == IP->V && I->RLAmt == IP->RLAmt &&
1279 I->StartIdx == (IP->EndIdx + 1) % 64 && I != IP) {
1280
1281 DEBUG(dbgs() << "\tcombining 32-bit replicated bit group for " <<
1282 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1283 " [" << I->StartIdx << ", " << I->EndIdx <<
1284 "] with group with range [" <<
1285 IP->StartIdx << ", " << IP->EndIdx << "]\n");
1286
1287 IP->EndIdx = I->EndIdx;
1288 IP->Repl32CR = IP->Repl32CR || I->Repl32CR;
1289 IP->Repl32Coalesced = true;
1290 I = BitGroups.erase(I);
1291 continue;
1292 } else {
1293 // There is a special case worth handling: If there is a single group
1294 // covering the entire upper 32 bits, and it can be merged with both
1295 // the next and previous groups (which might be the same group), then
1296 // do so. If it is the same group (so there will be only one group in
1297 // total), then we need to reverse the order of the range so that it
1298 // covers the entire 64 bits.
1299 if (I->StartIdx == 32 && I->EndIdx == 63) {
1300 assert(std::next(I) == BitGroups.end() &&
1301 "bit group ends at index 63 but there is another?");
1302 auto IN = BitGroups.begin();
1303
Justin Bognerb0126992016-05-05 23:19:08 +00001304 if (IP->Repl32 && IN->Repl32 && I->V == IP->V && I->V == IN->V &&
Hal Finkelc58ce412015-01-01 02:53:29 +00001305 (I->RLAmt % 32) == IP->RLAmt && (I->RLAmt % 32) == IN->RLAmt &&
1306 IP->EndIdx == 31 && IN->StartIdx == 0 && I != IP &&
1307 IsAllLow32(*I)) {
1308
1309 DEBUG(dbgs() << "\tcombining bit group for " <<
1310 I->V.getNode() << " RLAmt = " << I->RLAmt <<
1311 " [" << I->StartIdx << ", " << I->EndIdx <<
1312 "] with 32-bit replicated groups with ranges [" <<
1313 IP->StartIdx << ", " << IP->EndIdx << "] and [" <<
1314 IN->StartIdx << ", " << IN->EndIdx << "]\n");
1315
1316 if (IP == IN) {
1317 // There is only one other group; change it to cover the whole
1318 // range (backward, so that it can still be Repl32 but cover the
1319 // whole 64-bit range).
1320 IP->StartIdx = 31;
1321 IP->EndIdx = 30;
1322 IP->Repl32CR = IP->Repl32CR || I->RLAmt >= 32;
1323 IP->Repl32Coalesced = true;
1324 I = BitGroups.erase(I);
1325 } else {
1326 // There are two separate groups, one before this group and one
1327 // after us (at the beginning). We're going to remove this group,
1328 // but also the group at the very beginning.
1329 IP->EndIdx = IN->EndIdx;
1330 IP->Repl32CR = IP->Repl32CR || IN->Repl32CR || I->RLAmt >= 32;
1331 IP->Repl32Coalesced = true;
1332 I = BitGroups.erase(I);
1333 BitGroups.erase(BitGroups.begin());
1334 }
1335
1336 // This must be the last group in the vector (and we might have
1337 // just invalidated the iterator above), so break here.
1338 break;
1339 }
1340 }
1341 }
1342
1343 ++I;
1344 }
1345 }
1346
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001347 SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001348 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
Hal Finkel8adf2252014-12-16 05:51:41 +00001349 }
1350
Hal Finkelc58ce412015-01-01 02:53:29 +00001351 uint64_t getZerosMask() {
1352 uint64_t Mask = 0;
1353 for (unsigned i = 0; i < Bits.size(); ++i) {
1354 if (Bits[i].hasValue())
1355 continue;
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001356 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001357 }
1358
1359 return ~Mask;
1360 }
1361
Hal Finkel8adf2252014-12-16 05:51:41 +00001362 // Depending on the number of groups for a particular value, it might be
1363 // better to rotate, mask explicitly (using andi/andis), and then or the
1364 // result. Select this part of the result first.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001365 void SelectAndParts32(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001366 if (BPermRewriterNoMasking)
1367 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00001368
1369 for (ValueRotInfo &VRI : ValueRotsVec) {
1370 unsigned Mask = 0;
1371 for (unsigned i = 0; i < Bits.size(); ++i) {
1372 if (!Bits[i].hasValue() || Bits[i].getValue() != VRI.V)
1373 continue;
1374 if (RLAmt[i] != VRI.RLAmt)
1375 continue;
1376 Mask |= (1u << i);
1377 }
1378
1379 // Compute the masks for andi/andis that would be necessary.
1380 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1381 assert((ANDIMask != 0 || ANDISMask != 0) &&
1382 "No set bits in mask for value bit groups");
1383 bool NeedsRotate = VRI.RLAmt != 0;
1384
1385 // We're trying to minimize the number of instructions. If we have one
1386 // group, using one of andi/andis can break even. If we have three
1387 // groups, we can use both andi and andis and break even (to use both
1388 // andi and andis we also need to or the results together). We need four
1389 // groups if we also need to rotate. To use andi/andis we need to do more
1390 // than break even because rotate-and-mask instructions tend to be easier
1391 // to schedule.
1392
1393 // FIXME: We've biased here against using andi/andis, which is right for
1394 // POWER cores, but not optimal everywhere. For example, on the A2,
1395 // andi/andis have single-cycle latency whereas the rotate-and-mask
1396 // instructions take two cycles, and it would be better to bias toward
1397 // andi/andis in break-even cases.
1398
1399 unsigned NumAndInsts = (unsigned) NeedsRotate +
1400 (unsigned) (ANDIMask != 0) +
1401 (unsigned) (ANDISMask != 0) +
1402 (unsigned) (ANDIMask != 0 && ANDISMask != 0) +
1403 (unsigned) (bool) Res;
Hal Finkelc58ce412015-01-01 02:53:29 +00001404
1405 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1406 " RL: " << VRI.RLAmt << ":" <<
1407 "\n\t\t\tisel using masking: " << NumAndInsts <<
1408 " using rotates: " << VRI.NumGroups << "\n");
1409
Hal Finkel8adf2252014-12-16 05:51:41 +00001410 if (NumAndInsts >= VRI.NumGroups)
1411 continue;
1412
Hal Finkelc58ce412015-01-01 02:53:29 +00001413 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1414
1415 if (InstCnt) *InstCnt += NumAndInsts;
1416
Hal Finkel8adf2252014-12-16 05:51:41 +00001417 SDValue VRot;
1418 if (VRI.RLAmt) {
1419 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001420 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1421 getI32Imm(31, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001422 VRot = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
1423 Ops), 0);
1424 } else {
1425 VRot = VRI.V;
1426 }
1427
1428 SDValue ANDIVal, ANDISVal;
1429 if (ANDIMask != 0)
1430 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001431 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001432 if (ANDISMask != 0)
1433 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001434 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001435
1436 SDValue TotalVal;
1437 if (!ANDIVal)
1438 TotalVal = ANDISVal;
1439 else if (!ANDISVal)
1440 TotalVal = ANDIVal;
1441 else
1442 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1443 ANDIVal, ANDISVal), 0);
1444
1445 if (!Res)
1446 Res = TotalVal;
1447 else
1448 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1449 Res, TotalVal), 0);
1450
1451 // Now, remove all groups with this underlying value and rotation
1452 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001453 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1454 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1455 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001456 }
1457 }
1458
1459 // Instruction selection for the 32-bit case.
Hal Finkelc58ce412015-01-01 02:53:29 +00001460 SDNode *Select32(SDNode *N, bool LateMask, unsigned *InstCnt) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001461 SDLoc dl(N);
1462 SDValue Res;
1463
Hal Finkelc58ce412015-01-01 02:53:29 +00001464 if (InstCnt) *InstCnt = 0;
1465
Hal Finkel8adf2252014-12-16 05:51:41 +00001466 // Take care of cases that should use andi/andis first.
Hal Finkelc58ce412015-01-01 02:53:29 +00001467 SelectAndParts32(dl, Res, InstCnt);
Hal Finkel8adf2252014-12-16 05:51:41 +00001468
1469 // If we've not yet selected a 'starting' instruction, and we have no zeros
1470 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1471 // number of groups), and start with this rotated value.
Hal Finkelc58ce412015-01-01 02:53:29 +00001472 if ((!HasZeros || LateMask) && !Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001473 ValueRotInfo &VRI = ValueRotsVec[0];
1474 if (VRI.RLAmt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001475 if (InstCnt) *InstCnt += 1;
Hal Finkel8adf2252014-12-16 05:51:41 +00001476 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001477 { VRI.V, getI32Imm(VRI.RLAmt, dl), getI32Imm(0, dl),
1478 getI32Imm(31, dl) };
1479 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
1480 0);
Hal Finkel8adf2252014-12-16 05:51:41 +00001481 } else {
1482 Res = VRI.V;
1483 }
1484
1485 // Now, remove all groups with this underlying value and rotation factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001486 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1487 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt;
1488 });
Hal Finkel8adf2252014-12-16 05:51:41 +00001489 }
1490
Hal Finkelc58ce412015-01-01 02:53:29 +00001491 if (InstCnt) *InstCnt += BitGroups.size();
1492
Hal Finkel8adf2252014-12-16 05:51:41 +00001493 // Insert the other groups (one at a time).
1494 for (auto &BG : BitGroups) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001495 if (!Res) {
Hal Finkel8adf2252014-12-16 05:51:41 +00001496 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497 { BG.V, getI32Imm(BG.RLAmt, dl),
1498 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1499 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001500 Res = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
1501 } else {
1502 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001503 { Res, BG.V, getI32Imm(BG.RLAmt, dl),
1504 getI32Imm(Bits.size() - BG.EndIdx - 1, dl),
1505 getI32Imm(Bits.size() - BG.StartIdx - 1, dl) };
Hal Finkel8adf2252014-12-16 05:51:41 +00001506 Res = SDValue(CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops), 0);
1507 }
1508 }
1509
Hal Finkelc58ce412015-01-01 02:53:29 +00001510 if (LateMask) {
1511 unsigned Mask = (unsigned) getZerosMask();
1512
1513 unsigned ANDIMask = (Mask & UINT16_MAX), ANDISMask = Mask >> 16;
1514 assert((ANDIMask != 0 || ANDISMask != 0) &&
1515 "No set bits in zeros mask?");
1516
1517 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1518 (unsigned) (ANDISMask != 0) +
1519 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1520
1521 SDValue ANDIVal, ANDISVal;
1522 if (ANDIMask != 0)
1523 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001524 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001525 if (ANDISMask != 0)
1526 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001527 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001528
1529 if (!ANDIVal)
1530 Res = ANDISVal;
1531 else if (!ANDISVal)
1532 Res = ANDIVal;
1533 else
1534 Res = SDValue(CurDAG->getMachineNode(PPC::OR, dl, MVT::i32,
1535 ANDIVal, ANDISVal), 0);
1536 }
1537
Hal Finkel8adf2252014-12-16 05:51:41 +00001538 return Res.getNode();
1539 }
1540
Hal Finkelc58ce412015-01-01 02:53:29 +00001541 unsigned SelectRotMask64Count(unsigned RLAmt, bool Repl32,
1542 unsigned MaskStart, unsigned MaskEnd,
1543 bool IsIns) {
1544 // In the notation used by the instructions, 'start' and 'end' are reversed
1545 // because bits are counted from high to low order.
1546 unsigned InstMaskStart = 64 - MaskEnd - 1,
1547 InstMaskEnd = 64 - MaskStart - 1;
1548
1549 if (Repl32)
1550 return 1;
1551
1552 if ((!IsIns && (InstMaskEnd == 63 || InstMaskStart == 0)) ||
1553 InstMaskEnd == 63 - RLAmt)
1554 return 1;
1555
1556 return 2;
1557 }
1558
1559 // For 64-bit values, not all combinations of rotates and masks are
1560 // available. Produce one if it is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001561 SDValue SelectRotMask64(SDValue V, const SDLoc &dl, unsigned RLAmt,
1562 bool Repl32, unsigned MaskStart, unsigned MaskEnd,
Hal Finkelc58ce412015-01-01 02:53:29 +00001563 unsigned *InstCnt = nullptr) {
1564 // In the notation used by the instructions, 'start' and 'end' are reversed
1565 // because bits are counted from high to low order.
1566 unsigned InstMaskStart = 64 - MaskEnd - 1,
1567 InstMaskEnd = 64 - MaskStart - 1;
1568
1569 if (InstCnt) *InstCnt += 1;
1570
1571 if (Repl32) {
1572 // This rotation amount assumes that the lower 32 bits of the quantity
1573 // are replicated in the high 32 bits by the rotation operator (which is
1574 // done by rlwinm and friends).
1575 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1576 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1577 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001578 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1579 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001580 return SDValue(CurDAG->getMachineNode(PPC::RLWINM8, dl, MVT::i64,
1581 Ops), 0);
1582 }
1583
1584 if (InstMaskEnd == 63) {
1585 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001586 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001587 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Ops), 0);
1588 }
1589
1590 if (InstMaskStart == 0) {
1591 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001592 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskEnd, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001593 return SDValue(CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64, Ops), 0);
1594 }
1595
1596 if (InstMaskEnd == 63 - RLAmt) {
1597 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001598 { V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001599 return SDValue(CurDAG->getMachineNode(PPC::RLDIC, dl, MVT::i64, Ops), 0);
1600 }
1601
1602 // We cannot do this with a single instruction, so we'll use two. The
1603 // problem is that we're not free to choose both a rotation amount and mask
1604 // start and end independently. We can choose an arbitrary mask start and
1605 // end, but then the rotation amount is fixed. Rotation, however, can be
1606 // inverted, and so by applying an "inverse" rotation first, we can get the
1607 // desired result.
1608 if (InstCnt) *InstCnt += 1;
1609
1610 // The rotation mask for the second instruction must be MaskStart.
1611 unsigned RLAmt2 = MaskStart;
1612 // The first instruction must rotate V so that the overall rotation amount
1613 // is RLAmt.
1614 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1615 if (RLAmt1)
1616 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1617 return SelectRotMask64(V, dl, RLAmt2, false, MaskStart, MaskEnd);
1618 }
1619
1620 // For 64-bit values, not all combinations of rotates and masks are
1621 // available. Produce a rotate-mask-and-insert if one is available.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001622 SDValue SelectRotMaskIns64(SDValue Base, SDValue V, const SDLoc &dl,
1623 unsigned RLAmt, bool Repl32, unsigned MaskStart,
Hal Finkelc58ce412015-01-01 02:53:29 +00001624 unsigned MaskEnd, unsigned *InstCnt = nullptr) {
1625 // In the notation used by the instructions, 'start' and 'end' are reversed
1626 // because bits are counted from high to low order.
1627 unsigned InstMaskStart = 64 - MaskEnd - 1,
1628 InstMaskEnd = 64 - MaskStart - 1;
1629
1630 if (InstCnt) *InstCnt += 1;
1631
1632 if (Repl32) {
1633 // This rotation amount assumes that the lower 32 bits of the quantity
1634 // are replicated in the high 32 bits by the rotation operator (which is
1635 // done by rlwinm and friends).
1636 assert(InstMaskStart >= 32 && "Mask cannot start out of range");
1637 assert(InstMaskEnd >= 32 && "Mask cannot end out of range");
1638 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001639 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart - 32, dl),
1640 getI32Imm(InstMaskEnd - 32, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001641 return SDValue(CurDAG->getMachineNode(PPC::RLWIMI8, dl, MVT::i64,
1642 Ops), 0);
1643 }
1644
1645 if (InstMaskEnd == 63 - RLAmt) {
1646 SDValue Ops[] =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001647 { Base, V, getI32Imm(RLAmt, dl), getI32Imm(InstMaskStart, dl) };
Hal Finkelc58ce412015-01-01 02:53:29 +00001648 return SDValue(CurDAG->getMachineNode(PPC::RLDIMI, dl, MVT::i64, Ops), 0);
1649 }
1650
1651 // We cannot do this with a single instruction, so we'll use two. The
1652 // problem is that we're not free to choose both a rotation amount and mask
1653 // start and end independently. We can choose an arbitrary mask start and
1654 // end, but then the rotation amount is fixed. Rotation, however, can be
1655 // inverted, and so by applying an "inverse" rotation first, we can get the
1656 // desired result.
1657 if (InstCnt) *InstCnt += 1;
1658
1659 // The rotation mask for the second instruction must be MaskStart.
1660 unsigned RLAmt2 = MaskStart;
1661 // The first instruction must rotate V so that the overall rotation amount
1662 // is RLAmt.
1663 unsigned RLAmt1 = (64 + RLAmt - RLAmt2) % 64;
1664 if (RLAmt1)
1665 V = SelectRotMask64(V, dl, RLAmt1, false, 0, 63);
1666 return SelectRotMaskIns64(Base, V, dl, RLAmt2, false, MaskStart, MaskEnd);
1667 }
1668
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001669 void SelectAndParts64(const SDLoc &dl, SDValue &Res, unsigned *InstCnt) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001670 if (BPermRewriterNoMasking)
1671 return;
1672
1673 // The idea here is the same as in the 32-bit version, but with additional
1674 // complications from the fact that Repl32 might be true. Because we
1675 // aggressively convert bit groups to Repl32 form (which, for small
1676 // rotation factors, involves no other change), and then coalesce, it might
1677 // be the case that a single 64-bit masking operation could handle both
1678 // some Repl32 groups and some non-Repl32 groups. If converting to Repl32
1679 // form allowed coalescing, then we must use a 32-bit rotaton in order to
1680 // completely capture the new combined bit group.
1681
1682 for (ValueRotInfo &VRI : ValueRotsVec) {
1683 uint64_t Mask = 0;
1684
1685 // We need to add to the mask all bits from the associated bit groups.
1686 // If Repl32 is false, we need to add bits from bit groups that have
1687 // Repl32 true, but are trivially convertable to Repl32 false. Such a
1688 // group is trivially convertable if it overlaps only with the lower 32
1689 // bits, and the group has not been coalesced.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001690 auto MatchingBG = [VRI](const BitGroup &BG) {
Hal Finkelc58ce412015-01-01 02:53:29 +00001691 if (VRI.V != BG.V)
1692 return false;
1693
1694 unsigned EffRLAmt = BG.RLAmt;
1695 if (!VRI.Repl32 && BG.Repl32) {
1696 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx <= BG.EndIdx &&
1697 !BG.Repl32Coalesced) {
1698 if (BG.Repl32CR)
1699 EffRLAmt += 32;
1700 } else {
1701 return false;
1702 }
1703 } else if (VRI.Repl32 != BG.Repl32) {
1704 return false;
1705 }
1706
Alexander Kornienko175a7cb2015-12-28 13:38:42 +00001707 return VRI.RLAmt == EffRLAmt;
Hal Finkelc58ce412015-01-01 02:53:29 +00001708 };
1709
1710 for (auto &BG : BitGroups) {
1711 if (!MatchingBG(BG))
1712 continue;
1713
1714 if (BG.StartIdx <= BG.EndIdx) {
1715 for (unsigned i = BG.StartIdx; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001716 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001717 } else {
1718 for (unsigned i = BG.StartIdx; i < Bits.size(); ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001719 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001720 for (unsigned i = 0; i <= BG.EndIdx; ++i)
Hal Finkelddf8d7d2015-01-01 19:33:59 +00001721 Mask |= (UINT64_C(1) << i);
Hal Finkelc58ce412015-01-01 02:53:29 +00001722 }
1723 }
1724
1725 // We can use the 32-bit andi/andis technique if the mask does not
1726 // require any higher-order bits. This can save an instruction compared
1727 // to always using the general 64-bit technique.
1728 bool Use32BitInsts = isUInt<32>(Mask);
1729 // Compute the masks for andi/andis that would be necessary.
1730 unsigned ANDIMask = (Mask & UINT16_MAX),
1731 ANDISMask = (Mask >> 16) & UINT16_MAX;
1732
1733 bool NeedsRotate = VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask));
1734
1735 unsigned NumAndInsts = (unsigned) NeedsRotate +
1736 (unsigned) (bool) Res;
1737 if (Use32BitInsts)
1738 NumAndInsts += (unsigned) (ANDIMask != 0) + (unsigned) (ANDISMask != 0) +
1739 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1740 else
Justin Bognerdc8af062016-05-20 21:43:23 +00001741 NumAndInsts += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001742
1743 unsigned NumRLInsts = 0;
1744 bool FirstBG = true;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001745 bool MoreBG = false;
Hal Finkelc58ce412015-01-01 02:53:29 +00001746 for (auto &BG : BitGroups) {
Guozhi Wei0cd65422016-10-14 20:41:50 +00001747 if (!MatchingBG(BG)) {
1748 MoreBG = true;
Hal Finkelc58ce412015-01-01 02:53:29 +00001749 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001750 }
Hal Finkelc58ce412015-01-01 02:53:29 +00001751 NumRLInsts +=
1752 SelectRotMask64Count(BG.RLAmt, BG.Repl32, BG.StartIdx, BG.EndIdx,
1753 !FirstBG);
1754 FirstBG = false;
1755 }
1756
1757 DEBUG(dbgs() << "\t\trotation groups for " << VRI.V.getNode() <<
1758 " RL: " << VRI.RLAmt << (VRI.Repl32 ? " (32):" : ":") <<
1759 "\n\t\t\tisel using masking: " << NumAndInsts <<
1760 " using rotates: " << NumRLInsts << "\n");
1761
1762 // When we'd use andi/andis, we bias toward using the rotates (andi only
1763 // has a record form, and is cracked on POWER cores). However, when using
1764 // general 64-bit constant formation, bias toward the constant form,
1765 // because that exposes more opportunities for CSE.
1766 if (NumAndInsts > NumRLInsts)
1767 continue;
Guozhi Wei0cd65422016-10-14 20:41:50 +00001768 // When merging multiple bit groups, instruction or is used.
1769 // But when rotate is used, rldimi can inert the rotated value into any
1770 // register, so instruction or can be avoided.
1771 if ((Use32BitInsts || MoreBG) && NumAndInsts == NumRLInsts)
Hal Finkelc58ce412015-01-01 02:53:29 +00001772 continue;
1773
1774 DEBUG(dbgs() << "\t\t\t\tusing masking\n");
1775
1776 if (InstCnt) *InstCnt += NumAndInsts;
1777
1778 SDValue VRot;
1779 // We actually need to generate a rotation if we have a non-zero rotation
1780 // factor or, in the Repl32 case, if we care about any of the
1781 // higher-order replicated bits. In the latter case, we generate a mask
1782 // backward so that it actually includes the entire 64 bits.
1783 if (VRI.RLAmt || (VRI.Repl32 && !isUInt<32>(Mask)))
1784 VRot = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1785 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63);
1786 else
1787 VRot = VRI.V;
1788
1789 SDValue TotalVal;
1790 if (Use32BitInsts) {
1791 assert((ANDIMask != 0 || ANDISMask != 0) &&
1792 "No set bits in mask when using 32-bit ands for 64-bit value");
1793
1794 SDValue ANDIVal, ANDISVal;
1795 if (ANDIMask != 0)
1796 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001797 VRot, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001798 if (ANDISMask != 0)
1799 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001800 VRot, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001801
1802 if (!ANDIVal)
1803 TotalVal = ANDISVal;
1804 else if (!ANDISVal)
1805 TotalVal = ANDIVal;
1806 else
1807 TotalVal = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1808 ANDIVal, ANDISVal), 0);
1809 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001810 TotalVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001811 TotalVal =
1812 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1813 VRot, TotalVal), 0);
1814 }
1815
1816 if (!Res)
1817 Res = TotalVal;
1818 else
1819 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1820 Res, TotalVal), 0);
1821
1822 // Now, remove all groups with this underlying value and rotation
1823 // factor.
Benjamin Kramere7561b82015-06-20 15:59:41 +00001824 eraseMatchingBitGroups(MatchingBG);
Hal Finkelc58ce412015-01-01 02:53:29 +00001825 }
1826 }
1827
1828 // Instruction selection for the 64-bit case.
1829 SDNode *Select64(SDNode *N, bool LateMask, unsigned *InstCnt) {
1830 SDLoc dl(N);
1831 SDValue Res;
1832
1833 if (InstCnt) *InstCnt = 0;
1834
1835 // Take care of cases that should use andi/andis first.
1836 SelectAndParts64(dl, Res, InstCnt);
1837
1838 // If we've not yet selected a 'starting' instruction, and we have no zeros
1839 // to fill in, select the (Value, RLAmt) with the highest priority (largest
1840 // number of groups), and start with this rotated value.
1841 if ((!HasZeros || LateMask) && !Res) {
1842 // If we have both Repl32 groups and non-Repl32 groups, the non-Repl32
1843 // groups will come first, and so the VRI representing the largest number
1844 // of groups might not be first (it might be the first Repl32 groups).
1845 unsigned MaxGroupsIdx = 0;
1846 if (!ValueRotsVec[0].Repl32) {
1847 for (unsigned i = 0, ie = ValueRotsVec.size(); i < ie; ++i)
1848 if (ValueRotsVec[i].Repl32) {
1849 if (ValueRotsVec[i].NumGroups > ValueRotsVec[0].NumGroups)
1850 MaxGroupsIdx = i;
1851 break;
1852 }
1853 }
1854
1855 ValueRotInfo &VRI = ValueRotsVec[MaxGroupsIdx];
1856 bool NeedsRotate = false;
1857 if (VRI.RLAmt) {
1858 NeedsRotate = true;
1859 } else if (VRI.Repl32) {
1860 for (auto &BG : BitGroups) {
1861 if (BG.V != VRI.V || BG.RLAmt != VRI.RLAmt ||
1862 BG.Repl32 != VRI.Repl32)
1863 continue;
1864
1865 // We don't need a rotate if the bit group is confined to the lower
1866 // 32 bits.
1867 if (BG.StartIdx < 32 && BG.EndIdx < 32 && BG.StartIdx < BG.EndIdx)
1868 continue;
1869
1870 NeedsRotate = true;
1871 break;
1872 }
1873 }
1874
1875 if (NeedsRotate)
1876 Res = SelectRotMask64(VRI.V, dl, VRI.RLAmt, VRI.Repl32,
1877 VRI.Repl32 ? 31 : 0, VRI.Repl32 ? 30 : 63,
1878 InstCnt);
1879 else
1880 Res = VRI.V;
1881
1882 // Now, remove all groups with this underlying value and rotation factor.
1883 if (Res)
Benjamin Kramere7561b82015-06-20 15:59:41 +00001884 eraseMatchingBitGroups([VRI](const BitGroup &BG) {
1885 return BG.V == VRI.V && BG.RLAmt == VRI.RLAmt &&
1886 BG.Repl32 == VRI.Repl32;
1887 });
Hal Finkelc58ce412015-01-01 02:53:29 +00001888 }
1889
1890 // Because 64-bit rotates are more flexible than inserts, we might have a
1891 // preference regarding which one we do first (to save one instruction).
1892 if (!Res)
1893 for (auto I = BitGroups.begin(), IE = BitGroups.end(); I != IE; ++I) {
1894 if (SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1895 false) <
1896 SelectRotMask64Count(I->RLAmt, I->Repl32, I->StartIdx, I->EndIdx,
1897 true)) {
1898 if (I != BitGroups.begin()) {
1899 BitGroup BG = *I;
1900 BitGroups.erase(I);
1901 BitGroups.insert(BitGroups.begin(), BG);
1902 }
1903
1904 break;
1905 }
1906 }
1907
1908 // Insert the other groups (one at a time).
1909 for (auto &BG : BitGroups) {
1910 if (!Res)
1911 Res = SelectRotMask64(BG.V, dl, BG.RLAmt, BG.Repl32, BG.StartIdx,
1912 BG.EndIdx, InstCnt);
1913 else
1914 Res = SelectRotMaskIns64(Res, BG.V, dl, BG.RLAmt, BG.Repl32,
1915 BG.StartIdx, BG.EndIdx, InstCnt);
1916 }
1917
1918 if (LateMask) {
1919 uint64_t Mask = getZerosMask();
1920
1921 // We can use the 32-bit andi/andis technique if the mask does not
1922 // require any higher-order bits. This can save an instruction compared
1923 // to always using the general 64-bit technique.
1924 bool Use32BitInsts = isUInt<32>(Mask);
1925 // Compute the masks for andi/andis that would be necessary.
1926 unsigned ANDIMask = (Mask & UINT16_MAX),
1927 ANDISMask = (Mask >> 16) & UINT16_MAX;
1928
1929 if (Use32BitInsts) {
1930 assert((ANDIMask != 0 || ANDISMask != 0) &&
1931 "No set bits in mask when using 32-bit ands for 64-bit value");
1932
1933 if (InstCnt) *InstCnt += (unsigned) (ANDIMask != 0) +
1934 (unsigned) (ANDISMask != 0) +
1935 (unsigned) (ANDIMask != 0 && ANDISMask != 0);
1936
1937 SDValue ANDIVal, ANDISVal;
1938 if (ANDIMask != 0)
1939 ANDIVal = SDValue(CurDAG->getMachineNode(PPC::ANDIo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 Res, getI32Imm(ANDIMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001941 if (ANDISMask != 0)
1942 ANDISVal = SDValue(CurDAG->getMachineNode(PPC::ANDISo8, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 Res, getI32Imm(ANDISMask, dl)), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001944
1945 if (!ANDIVal)
1946 Res = ANDISVal;
1947 else if (!ANDISVal)
1948 Res = ANDIVal;
1949 else
1950 Res = SDValue(CurDAG->getMachineNode(PPC::OR8, dl, MVT::i64,
1951 ANDIVal, ANDISVal), 0);
1952 } else {
Justin Bognerdc8af062016-05-20 21:43:23 +00001953 if (InstCnt) *InstCnt += getInt64Count(Mask) + /* and */ 1;
Hal Finkelc58ce412015-01-01 02:53:29 +00001954
Justin Bognerdc8af062016-05-20 21:43:23 +00001955 SDValue MaskVal = SDValue(getInt64(CurDAG, dl, Mask), 0);
Hal Finkelc58ce412015-01-01 02:53:29 +00001956 Res =
1957 SDValue(CurDAG->getMachineNode(PPC::AND8, dl, MVT::i64,
1958 Res, MaskVal), 0);
1959 }
1960 }
1961
1962 return Res.getNode();
1963 }
1964
1965 SDNode *Select(SDNode *N, bool LateMask, unsigned *InstCnt = nullptr) {
1966 // Fill in BitGroups.
1967 collectBitGroups(LateMask);
1968 if (BitGroups.empty())
1969 return nullptr;
1970
1971 // For 64-bit values, figure out when we can use 32-bit instructions.
1972 if (Bits.size() == 64)
1973 assignRepl32BitGroups();
1974
1975 // Fill in ValueRotsVec.
1976 collectValueRotInfo();
1977
1978 if (Bits.size() == 32) {
1979 return Select32(N, LateMask, InstCnt);
1980 } else {
1981 assert(Bits.size() == 64 && "Not 64 bits here?");
1982 return Select64(N, LateMask, InstCnt);
1983 }
1984
1985 return nullptr;
1986 }
1987
Benjamin Kramere7561b82015-06-20 15:59:41 +00001988 void eraseMatchingBitGroups(function_ref<bool(const BitGroup &)> F) {
David Majnemerc7004902016-08-12 04:32:37 +00001989 BitGroups.erase(remove_if(BitGroups, F), BitGroups.end());
Benjamin Kramere7561b82015-06-20 15:59:41 +00001990 }
1991
Hal Finkel8adf2252014-12-16 05:51:41 +00001992 SmallVector<ValueBit, 64> Bits;
1993
1994 bool HasZeros;
1995 SmallVector<unsigned, 64> RLAmt;
1996
1997 SmallVector<BitGroup, 16> BitGroups;
1998
1999 DenseMap<std::pair<SDValue, unsigned>, ValueRotInfo> ValueRots;
2000 SmallVector<ValueRotInfo, 16> ValueRotsVec;
2001
2002 SelectionDAG *CurDAG;
2003
2004public:
2005 BitPermutationSelector(SelectionDAG *DAG)
2006 : CurDAG(DAG) {}
2007
2008 // Here we try to match complex bit permutations into a set of
2009 // rotate-and-shift/shift/and/or instructions, using a set of heuristics
2010 // known to produce optimial code for common cases (like i32 byte swapping).
2011 SDNode *Select(SDNode *N) {
Tim Shendc698c32016-08-12 18:40:04 +00002012 Memoizer.clear();
2013 auto Result =
2014 getValueBits(SDValue(N, 0), N->getValueType(0).getSizeInBits());
2015 if (!Result.first)
Hal Finkel8adf2252014-12-16 05:51:41 +00002016 return nullptr;
Tim Shendc698c32016-08-12 18:40:04 +00002017 Bits = std::move(*Result.second);
Hal Finkel8adf2252014-12-16 05:51:41 +00002018
2019 DEBUG(dbgs() << "Considering bit-permutation-based instruction"
2020 " selection for: ");
2021 DEBUG(N->dump(CurDAG));
2022
2023 // Fill it RLAmt and set HasZeros.
2024 computeRotationAmounts();
2025
Hal Finkelc58ce412015-01-01 02:53:29 +00002026 if (!HasZeros)
2027 return Select(N, false);
Hal Finkel8adf2252014-12-16 05:51:41 +00002028
Hal Finkelc58ce412015-01-01 02:53:29 +00002029 // We currently have two techniques for handling results with zeros: early
2030 // masking (the default) and late masking. Late masking is sometimes more
2031 // efficient, but because the structure of the bit groups is different, it
2032 // is hard to tell without generating both and comparing the results. With
2033 // late masking, we ignore zeros in the resulting value when inserting each
2034 // set of bit groups, and then mask in the zeros at the end. With early
2035 // masking, we only insert the non-zero parts of the result at every step.
Hal Finkel8adf2252014-12-16 05:51:41 +00002036
Hal Finkelc58ce412015-01-01 02:53:29 +00002037 unsigned InstCnt, InstCntLateMask;
2038 DEBUG(dbgs() << "\tEarly masking:\n");
2039 SDNode *RN = Select(N, false, &InstCnt);
2040 DEBUG(dbgs() << "\t\tisel would use " << InstCnt << " instructions\n");
2041
2042 DEBUG(dbgs() << "\tLate masking:\n");
2043 SDNode *RNLM = Select(N, true, &InstCntLateMask);
2044 DEBUG(dbgs() << "\t\tisel would use " << InstCntLateMask <<
2045 " instructions\n");
2046
2047 if (InstCnt <= InstCntLateMask) {
2048 DEBUG(dbgs() << "\tUsing early-masking for isel\n");
2049 return RN;
Hal Finkel8adf2252014-12-16 05:51:41 +00002050 }
2051
Hal Finkelc58ce412015-01-01 02:53:29 +00002052 DEBUG(dbgs() << "\tUsing late-masking for isel\n");
2053 return RNLM;
Hal Finkel8adf2252014-12-16 05:51:41 +00002054 }
2055};
Eugene Zelenko8187c192017-01-13 00:58:58 +00002056
2057} // end anonymous namespace
Hal Finkel8adf2252014-12-16 05:51:41 +00002058
Justin Bognerdc8af062016-05-20 21:43:23 +00002059bool PPCDAGToDAGISel::tryBitPermutation(SDNode *N) {
Hal Finkel8adf2252014-12-16 05:51:41 +00002060 if (N->getValueType(0) != MVT::i32 &&
2061 N->getValueType(0) != MVT::i64)
Justin Bognerdc8af062016-05-20 21:43:23 +00002062 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002063
Hal Finkelc58ce412015-01-01 02:53:29 +00002064 if (!UseBitPermRewriter)
Justin Bognerdc8af062016-05-20 21:43:23 +00002065 return false;
Hal Finkelc58ce412015-01-01 02:53:29 +00002066
Hal Finkel8adf2252014-12-16 05:51:41 +00002067 switch (N->getOpcode()) {
2068 default: break;
2069 case ISD::ROTL:
2070 case ISD::SHL:
2071 case ISD::SRL:
2072 case ISD::AND:
2073 case ISD::OR: {
2074 BitPermutationSelector BPS(CurDAG);
Justin Bognerdc8af062016-05-20 21:43:23 +00002075 if (SDNode *New = BPS.Select(N)) {
2076 ReplaceNode(N, New);
2077 return true;
2078 }
2079 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002080 }
2081 }
2082
Justin Bognerdc8af062016-05-20 21:43:23 +00002083 return false;
Hal Finkel8adf2252014-12-16 05:51:41 +00002084}
2085
Chris Lattner2a1823d2005-08-21 18:50:37 +00002086/// SelectCC - Select a comparison of the specified values with the specified
2087/// condition code, returning the CR# of the expression.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002088SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC,
2089 const SDLoc &dl) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002090 // Always select the LHS.
Chris Lattner97b3da12006-06-27 00:04:13 +00002091 unsigned Opc;
Andrew Trickc416ba62010-12-24 04:28:06 +00002092
Owen Anderson9f944592009-08-11 20:47:22 +00002093 if (LHS.getValueType() == MVT::i32) {
Chris Lattner9a40cca2006-06-27 00:10:13 +00002094 unsigned Imm;
Chris Lattneraa3926b2006-09-20 04:25:47 +00002095 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
2096 if (isInt32Immediate(RHS, Imm)) {
2097 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002098 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002099 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002100 getI32Imm(Imm & 0xFFFF, dl)),
2101 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002102 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002103 if (isInt<16>((int)Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002104 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002105 getI32Imm(Imm & 0xFFFF, dl)),
2106 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002107
Chris Lattneraa3926b2006-09-20 04:25:47 +00002108 // For non-equality comparisons, the default code would materialize the
2109 // constant, then compare against it, like this:
2110 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002111 // ori r2, r2, 22136
Chris Lattneraa3926b2006-09-20 04:25:47 +00002112 // cmpw cr0, r3, r2
2113 // Since we are just comparing for equality, we can emit this instead:
2114 // xoris r0,r3,0x1234
2115 // cmplwi cr0,r0,0x5678
2116 // beq cr0,L6
Dan Gohman32f71d72009-09-25 18:54:59 +00002117 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002118 getI32Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002119 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002120 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattneraa3926b2006-09-20 04:25:47 +00002121 }
2122 Opc = PPC::CMPLW;
2123 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002124 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002125 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002126 getI32Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002127 Opc = PPC::CMPLW;
2128 } else {
2129 short SImm;
2130 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002131 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002132 getI32Imm((int)SImm & 0xFFFF,
2133 dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002134 0);
2135 Opc = PPC::CMPW;
2136 }
Owen Anderson9f944592009-08-11 20:47:22 +00002137 } else if (LHS.getValueType() == MVT::i64) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002138 uint64_t Imm;
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002139 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002140 if (isInt64Immediate(RHS.getNode(), Imm)) {
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002141 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002142 if (isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002143 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002144 getI32Imm(Imm & 0xFFFF, dl)),
2145 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002146 // If this is a 16-bit signed immediate, fold it.
Benjamin Kramer2788f792010-03-29 21:13:41 +00002147 if (isInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002148 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002149 getI32Imm(Imm & 0xFFFF, dl)),
2150 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002151
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002152 // For non-equality comparisons, the default code would materialize the
2153 // constant, then compare against it, like this:
2154 // lis r2, 4660
Andrew Trickc416ba62010-12-24 04:28:06 +00002155 // ori r2, r2, 22136
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002156 // cmpd cr0, r3, r2
2157 // Since we are just comparing for equality, we can emit this instead:
2158 // xoris r0,r3,0x1234
2159 // cmpldi cr0,r0,0x5678
2160 // beq cr0,L6
Benjamin Kramer2788f792010-03-29 21:13:41 +00002161 if (isUInt<32>(Imm)) {
Dan Gohman32f71d72009-09-25 18:54:59 +00002162 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002163 getI64Imm(Imm >> 16, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002164 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002165 getI64Imm(Imm & 0xFFFF, dl)),
2166 0);
Chris Lattnerda9b1a92006-09-20 04:33:27 +00002167 }
2168 }
2169 Opc = PPC::CMPLD;
2170 } else if (ISD::isUnsignedIntSetCC(CC)) {
Benjamin Kramer2788f792010-03-29 21:13:41 +00002171 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002172 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002173 getI64Imm(Imm & 0xFFFF, dl)), 0);
Chris Lattner97b3da12006-06-27 00:04:13 +00002174 Opc = PPC::CMPLD;
2175 } else {
2176 short SImm;
2177 if (isIntS16Immediate(RHS, SImm))
Dan Gohman32f71d72009-09-25 18:54:59 +00002178 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002179 getI64Imm(SImm & 0xFFFF, dl)),
Chris Lattner97b3da12006-06-27 00:04:13 +00002180 0);
2181 Opc = PPC::CMPD;
2182 }
Owen Anderson9f944592009-08-11 20:47:22 +00002183 } else if (LHS.getValueType() == MVT::f32) {
Chris Lattner97b3da12006-06-27 00:04:13 +00002184 Opc = PPC::FCMPUS;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002185 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00002186 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
Eric Christopher1b8e7632014-05-22 01:07:24 +00002187 Opc = PPCSubTarget->hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002188 }
Dan Gohman32f71d72009-09-25 18:54:59 +00002189 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
Chris Lattner2a1823d2005-08-21 18:50:37 +00002190}
2191
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002192static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
Chris Lattner2a1823d2005-08-21 18:50:37 +00002193 switch (CC) {
Chris Lattner630bbce2006-05-25 16:54:16 +00002194 case ISD::SETUEQ:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002195 case ISD::SETONE:
2196 case ISD::SETOLE:
2197 case ISD::SETOGE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002198 llvm_unreachable("Should be lowered by legalize!");
2199 default: llvm_unreachable("Unknown condition!");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002200 case ISD::SETOEQ:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002201 case ISD::SETEQ: return PPC::PRED_EQ;
Chris Lattner630bbce2006-05-25 16:54:16 +00002202 case ISD::SETUNE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002203 case ISD::SETNE: return PPC::PRED_NE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002204 case ISD::SETOLT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002205 case ISD::SETLT: return PPC::PRED_LT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002206 case ISD::SETULE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002207 case ISD::SETLE: return PPC::PRED_LE;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002208 case ISD::SETOGT:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002209 case ISD::SETGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002210 case ISD::SETUGE:
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002211 case ISD::SETGE: return PPC::PRED_GE;
Chris Lattner8c6a41e2006-11-17 22:10:59 +00002212 case ISD::SETO: return PPC::PRED_NU;
2213 case ISD::SETUO: return PPC::PRED_UN;
Dale Johannesen160be0f2008-11-07 22:54:33 +00002214 // These two are invalid for floating point. Assume we have int.
2215 case ISD::SETULT: return PPC::PRED_LT;
2216 case ISD::SETUGT: return PPC::PRED_GT;
Chris Lattner2a1823d2005-08-21 18:50:37 +00002217 }
Chris Lattner2a1823d2005-08-21 18:50:37 +00002218}
2219
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002220/// getCRIdxForSetCC - Return the index of the condition register field
2221/// associated with the SetCC condition, and whether or not the field is
2222/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Ulrich Weigand47e93282013-07-03 15:13:30 +00002223static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
Chris Lattner89f36e62008-01-08 06:46:30 +00002224 Invert = false;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002225 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002226 default: llvm_unreachable("Unknown condition!");
Chris Lattner89f36e62008-01-08 06:46:30 +00002227 case ISD::SETOLT:
2228 case ISD::SETLT: return 0; // Bit #0 = SETOLT
2229 case ISD::SETOGT:
2230 case ISD::SETGT: return 1; // Bit #1 = SETOGT
2231 case ISD::SETOEQ:
2232 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
2233 case ISD::SETUO: return 3; // Bit #3 = SETUO
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002234 case ISD::SETUGE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002235 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002236 case ISD::SETULE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002237 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
Chris Lattner1fbb0d32006-05-25 18:06:16 +00002238 case ISD::SETUNE:
Chris Lattner89f36e62008-01-08 06:46:30 +00002239 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
2240 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
Andrew Trickc416ba62010-12-24 04:28:06 +00002241 case ISD::SETUEQ:
2242 case ISD::SETOGE:
2243 case ISD::SETOLE:
Dale Johannesen160be0f2008-11-07 22:54:33 +00002244 case ISD::SETONE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00002245 llvm_unreachable("Invalid branch code: should be expanded by legalize");
Dale Johannesen160be0f2008-11-07 22:54:33 +00002246 // These are invalid for floating point. Assume integer.
2247 case ISD::SETULT: return 0;
2248 case ISD::SETUGT: return 1;
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002249 }
Chris Lattner3dcd75b2005-08-25 20:08:18 +00002250}
Chris Lattnerc5292ec2005-08-21 22:31:09 +00002251
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002252// getVCmpInst: return the vector compare instruction for the specified
2253// vector type and condition code. Since this is for altivec specific code,
Kit Barton0cfa7b72015-03-03 19:55:45 +00002254// only support the altivec types (v16i8, v8i16, v4i32, v2i64, and v4f32).
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002255static unsigned int getVCmpInst(MVT VecVT, ISD::CondCode CC,
2256 bool HasVSX, bool &Swap, bool &Negate) {
2257 Swap = false;
2258 Negate = false;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002259
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002260 if (VecVT.isFloatingPoint()) {
2261 /* Handle some cases by swapping input operands. */
2262 switch (CC) {
2263 case ISD::SETLE: CC = ISD::SETGE; Swap = true; break;
2264 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2265 case ISD::SETOLE: CC = ISD::SETOGE; Swap = true; break;
2266 case ISD::SETOLT: CC = ISD::SETOGT; Swap = true; break;
2267 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2268 case ISD::SETUGT: CC = ISD::SETULT; Swap = true; break;
2269 default: break;
2270 }
2271 /* Handle some cases by negating the result. */
2272 switch (CC) {
2273 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2274 case ISD::SETUNE: CC = ISD::SETOEQ; Negate = true; break;
2275 case ISD::SETULE: CC = ISD::SETOGT; Negate = true; break;
2276 case ISD::SETULT: CC = ISD::SETOGE; Negate = true; break;
2277 default: break;
2278 }
2279 /* We have instructions implementing the remaining cases. */
2280 switch (CC) {
2281 case ISD::SETEQ:
2282 case ISD::SETOEQ:
2283 if (VecVT == MVT::v4f32)
2284 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
2285 else if (VecVT == MVT::v2f64)
2286 return PPC::XVCMPEQDP;
2287 break;
2288 case ISD::SETGT:
2289 case ISD::SETOGT:
2290 if (VecVT == MVT::v4f32)
2291 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
2292 else if (VecVT == MVT::v2f64)
2293 return PPC::XVCMPGTDP;
2294 break;
2295 case ISD::SETGE:
2296 case ISD::SETOGE:
2297 if (VecVT == MVT::v4f32)
2298 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
2299 else if (VecVT == MVT::v2f64)
2300 return PPC::XVCMPGEDP;
2301 break;
2302 default:
2303 break;
2304 }
2305 llvm_unreachable("Invalid floating-point vector compare condition");
2306 } else {
2307 /* Handle some cases by swapping input operands. */
2308 switch (CC) {
2309 case ISD::SETGE: CC = ISD::SETLE; Swap = true; break;
2310 case ISD::SETLT: CC = ISD::SETGT; Swap = true; break;
2311 case ISD::SETUGE: CC = ISD::SETULE; Swap = true; break;
2312 case ISD::SETULT: CC = ISD::SETUGT; Swap = true; break;
2313 default: break;
2314 }
2315 /* Handle some cases by negating the result. */
2316 switch (CC) {
2317 case ISD::SETNE: CC = ISD::SETEQ; Negate = true; break;
2318 case ISD::SETUNE: CC = ISD::SETUEQ; Negate = true; break;
2319 case ISD::SETLE: CC = ISD::SETGT; Negate = true; break;
2320 case ISD::SETULE: CC = ISD::SETUGT; Negate = true; break;
2321 default: break;
2322 }
2323 /* We have instructions implementing the remaining cases. */
2324 switch (CC) {
2325 case ISD::SETEQ:
2326 case ISD::SETUEQ:
2327 if (VecVT == MVT::v16i8)
2328 return PPC::VCMPEQUB;
2329 else if (VecVT == MVT::v8i16)
2330 return PPC::VCMPEQUH;
2331 else if (VecVT == MVT::v4i32)
2332 return PPC::VCMPEQUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002333 else if (VecVT == MVT::v2i64)
2334 return PPC::VCMPEQUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002335 break;
2336 case ISD::SETGT:
2337 if (VecVT == MVT::v16i8)
2338 return PPC::VCMPGTSB;
2339 else if (VecVT == MVT::v8i16)
2340 return PPC::VCMPGTSH;
2341 else if (VecVT == MVT::v4i32)
2342 return PPC::VCMPGTSW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002343 else if (VecVT == MVT::v2i64)
2344 return PPC::VCMPGTSD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002345 break;
2346 case ISD::SETUGT:
2347 if (VecVT == MVT::v16i8)
2348 return PPC::VCMPGTUB;
2349 else if (VecVT == MVT::v8i16)
2350 return PPC::VCMPGTUH;
2351 else if (VecVT == MVT::v4i32)
2352 return PPC::VCMPGTUW;
Kit Barton0cfa7b72015-03-03 19:55:45 +00002353 else if (VecVT == MVT::v2i64)
2354 return PPC::VCMPGTUD;
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002355 break;
2356 default:
2357 break;
2358 }
2359 llvm_unreachable("Invalid integer vector compare condition");
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002360 }
2361}
2362
Justin Bognerdc8af062016-05-20 21:43:23 +00002363bool PPCDAGToDAGISel::trySETCC(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002364 SDLoc dl(N);
Chris Lattner491b8292005-10-06 19:03:35 +00002365 unsigned Imm;
2366 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00002367 EVT PtrVT =
2368 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00002369 bool isPPC64 = (PtrVT == MVT::i64);
2370
Eric Christopher1b8e7632014-05-22 01:07:24 +00002371 if (!PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00002372 isInt32Immediate(N->getOperand(1), Imm)) {
Chris Lattner491b8292005-10-06 19:03:35 +00002373 // We can codegen setcc op, imm very efficiently compared to a brcond.
2374 // Check for those cases here.
2375 // setcc op, 0
2376 if (Imm == 0) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002377 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002378 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002379 default: break;
Evan Chengc3acfc02006-08-27 08:14:06 +00002380 case ISD::SETEQ: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002381 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002382 SDValue Ops[] = { Op, getI32Imm(27, dl), getI32Imm(5, dl),
2383 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002384 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2385 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002386 }
Chris Lattnere2969492005-10-21 21:17:10 +00002387 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002388 if (isPPC64) break;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002389 SDValue AD =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002390 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002391 Op, getI32Imm(~0U, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002392 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1));
2393 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002394 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002395 case ISD::SETLT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002396 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2397 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002398 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2399 return true;
Evan Chengc3acfc02006-08-27 08:14:06 +00002400 }
Chris Lattnere2969492005-10-21 21:17:10 +00002401 case ISD::SETGT: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002402 SDValue T =
Dan Gohman32f71d72009-09-25 18:54:59 +00002403 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
2404 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002405 SDValue Ops[] = { T, getI32Imm(1, dl), getI32Imm(31, dl),
2406 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002407 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2408 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002409 }
2410 }
Chris Lattner491b8292005-10-06 19:03:35 +00002411 } else if (Imm == ~0U) { // setcc op, -1
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002412 SDValue Op = N->getOperand(0);
Chris Lattner491b8292005-10-06 19:03:35 +00002413 switch (CC) {
Chris Lattnere2969492005-10-21 21:17:10 +00002414 default: break;
2415 case ISD::SETEQ:
Roman Divacky254f8212011-06-20 15:28:39 +00002416 if (isPPC64) break;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002417 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002418 Op, getI32Imm(1, dl)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002419 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
2420 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
2421 MVT::i32,
2422 getI32Imm(0, dl)),
2423 0), Op.getValue(1));
2424 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002425 case ISD::SETNE: {
Roman Divacky254f8212011-06-20 15:28:39 +00002426 if (isPPC64) break;
Dan Gohman32f71d72009-09-25 18:54:59 +00002427 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002428 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002429 Op, getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00002430 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0), Op,
2431 SDValue(AD, 1));
2432 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002433 }
Chris Lattnere2969492005-10-21 21:17:10 +00002434 case ISD::SETLT: {
Dan Gohman32f71d72009-09-25 18:54:59 +00002435 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002436 getI32Imm(1, dl)), 0);
Dan Gohman32f71d72009-09-25 18:54:59 +00002437 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
2438 Op), 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002439 SDValue Ops[] = { AN, getI32Imm(1, dl), getI32Imm(31, dl),
2440 getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002441 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2442 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002443 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002444 case ISD::SETGT: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002445 SDValue Ops[] = { Op, getI32Imm(1, dl), getI32Imm(31, dl),
2446 getI32Imm(31, dl) };
2447 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002448 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1, dl));
2449 return true;
Chris Lattnere2969492005-10-21 21:17:10 +00002450 }
Evan Chengc3acfc02006-08-27 08:14:06 +00002451 }
Chris Lattner491b8292005-10-06 19:03:35 +00002452 }
2453 }
Andrew Trickc416ba62010-12-24 04:28:06 +00002454
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002455 SDValue LHS = N->getOperand(0);
2456 SDValue RHS = N->getOperand(1);
2457
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002458 // Altivec Vector compare instructions do not set any CR register by default and
2459 // vector compare operations return the same type as the operands.
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002460 if (LHS.getValueType().isVector()) {
Hal Finkelc93a9a22015-02-25 01:06:45 +00002461 if (PPCSubTarget->hasQPX())
Justin Bognerdc8af062016-05-20 21:43:23 +00002462 return false;
Hal Finkelc93a9a22015-02-25 01:06:45 +00002463
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002464 EVT VecVT = LHS.getValueType();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002465 bool Swap, Negate;
2466 unsigned int VCmpInst = getVCmpInst(VecVT.getSimpleVT(), CC,
2467 PPCSubTarget->hasVSX(), Swap, Negate);
2468 if (Swap)
2469 std::swap(LHS, RHS);
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002470
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002471 EVT ResVT = VecVT.changeVectorElementTypeToInteger();
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002472 if (Negate) {
Hal Finkel9fdce9a2015-08-20 03:02:02 +00002473 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, ResVT, LHS, RHS), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002474 CurDAG->SelectNodeTo(N, PPCSubTarget->hasVSX() ? PPC::XXLNOR : PPC::VNOR,
2475 ResVT, VCmp, VCmp);
2476 return true;
Adhemerval Zanella56775e02012-10-30 13:50:19 +00002477 }
Ulrich Weigandc4cc7fe2014-08-04 13:13:57 +00002478
Justin Bognerdc8af062016-05-20 21:43:23 +00002479 CurDAG->SelectNodeTo(N, VCmpInst, ResVT, LHS, RHS);
2480 return true;
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002481 }
2482
Eric Christopher1b8e7632014-05-22 01:07:24 +00002483 if (PPCSubTarget->useCRBits())
Justin Bognerdc8af062016-05-20 21:43:23 +00002484 return false;
Hal Finkel940ab932014-02-28 00:27:01 +00002485
Chris Lattner491b8292005-10-06 19:03:35 +00002486 bool Inv;
Ulrich Weigand47e93282013-07-03 15:13:30 +00002487 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +00002488 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002489 SDValue IntCR;
Andrew Trickc416ba62010-12-24 04:28:06 +00002490
Chris Lattner491b8292005-10-06 19:03:35 +00002491 // Force the ccreg into CR7.
Owen Anderson9f944592009-08-11 20:47:22 +00002492 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
Andrew Trickc416ba62010-12-24 04:28:06 +00002493
Craig Topper062a2ba2014-04-25 05:30:21 +00002494 SDValue InFlag(nullptr, 0); // Null incoming flag value.
Andrew Trickc416ba62010-12-24 04:28:06 +00002495 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
Chris Lattnerbd099102005-12-01 03:50:19 +00002496 InFlag).getValue(1);
Andrew Trickc416ba62010-12-24 04:28:06 +00002497
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00002498 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
2499 CCReg), 0);
Andrew Trickc416ba62010-12-24 04:28:06 +00002500
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002501 SDValue Ops[] = { IntCR, getI32Imm((32 - (3 - Idx)) & 31, dl),
2502 getI32Imm(31, dl), getI32Imm(31, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00002503 if (!Inv) {
2504 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
2505 return true;
2506 }
Chris Lattner89f36e62008-01-08 06:46:30 +00002507
2508 // Get the specified bit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002509 SDValue Tmp =
Michael Liaob53d8962013-04-19 22:22:57 +00002510 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00002511 CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1, dl));
2512 return true;
Chris Lattner491b8292005-10-06 19:03:35 +00002513}
Chris Lattner502a3692005-10-06 18:56:10 +00002514
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002515// Is this opcode a bitwise logical operation?
2516static bool isLogicOp(unsigned Opc) {
2517 return Opc == ISD::AND || Opc == ISD::OR || Opc == ISD::XOR;
2518}
2519
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002520/// If this node is a sign/zero extension of an integer comparison,
2521/// it can usually be computed in GPR's rather than using comparison
2522/// instructions and ISEL. We only do this on 64-bit targets for now
2523/// as the code is specialized for 64-bit (it uses 64-bit instructions
2524/// and assumes 64-bit registers).
2525bool PPCDAGToDAGISel::tryEXTEND(SDNode *N) {
2526 if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64())
2527 return false;
2528 assert((N->getOpcode() == ISD::ZERO_EXTEND ||
2529 N->getOpcode() == ISD::SIGN_EXTEND) &&
2530 "Expecting a zero/sign extend node!");
2531
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002532 SDValue WideRes;
2533 // If we are zero-extending the result of a logical operation on i1
2534 // values, we can keep the values in GPRs.
2535 if (isLogicOp(N->getOperand(0).getOpcode()) &&
2536 N->getOperand(0).getValueType() == MVT::i1 &&
2537 N->getOpcode() == ISD::ZERO_EXTEND)
2538 WideRes = computeLogicOpInGPR(N->getOperand(0));
2539 else if (N->getOperand(0).getOpcode() != ISD::SETCC)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002540 return false;
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002541 else
2542 WideRes =
2543 getSETCCInGPR(N->getOperand(0),
2544 N->getOpcode() == ISD::SIGN_EXTEND ?
2545 SetccInGPROpts::SExtOrig : SetccInGPROpts::ZExtOrig);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002546
2547 if (!WideRes)
2548 return false;
2549
2550 SDLoc dl(N);
2551 bool Inputs32Bit = N->getOperand(0).getOperand(0).getValueType() == MVT::i32;
2552 bool Output32Bit = N->getValueType(0) == MVT::i32;
2553
2554 NumSextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 1 : 0;
2555 NumZextSetcc += N->getOpcode() == ISD::SIGN_EXTEND ? 0 : 1;
2556
2557 SDValue ConvOp = WideRes;
2558 if (Inputs32Bit != Output32Bit)
2559 ConvOp = addExtOrTrunc(WideRes, Inputs32Bit ? ExtOrTruncConversion::Ext :
2560 ExtOrTruncConversion::Trunc);
2561 ReplaceNode(N, ConvOp.getNode());
2562
2563 return true;
2564}
2565
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002566// Lower a logical operation on i1 values into a GPR sequence if possible.
2567// The result can be kept in a GPR if requested.
2568// Three types of inputs can be handled:
2569// - SETCC
2570// - TRUNCATE
2571// - Logical operation (AND/OR/XOR)
2572// There is also a special case that is handled (namely a complement operation
2573// achieved with xor %a, -1).
2574SDValue PPCDAGToDAGISel::computeLogicOpInGPR(SDValue LogicOp) {
2575 assert(isLogicOp(LogicOp.getOpcode()) &&
2576 "Can only handle logic operations here.");
2577 assert(LogicOp.getValueType() == MVT::i1 &&
2578 "Can only handle logic operations on i1 values here.");
2579 SDLoc dl(LogicOp);
2580 SDValue LHS, RHS;
2581
2582 // Special case: xor %a, -1
2583 bool IsBitwiseNegation = isBitwiseNot(LogicOp);
2584
2585 // Produces a GPR sequence for each operand of the binary logic operation.
2586 // For SETCC, it produces the respective comparison, for TRUNCATE it truncates
2587 // the value in a GPR and for logic operations, it will recursively produce
2588 // a GPR sequence for the operation.
2589 auto getLogicOperand = [&] (SDValue Operand) -> SDValue {
2590 unsigned OperandOpcode = Operand.getOpcode();
2591 if (OperandOpcode == ISD::SETCC)
2592 return getSETCCInGPR(Operand, SetccInGPROpts::ZExtOrig);
2593 else if (OperandOpcode == ISD::TRUNCATE) {
2594 SDValue InputOp = Operand.getOperand(0);
2595 EVT InVT = InputOp.getValueType();
2596 return
2597 SDValue(CurDAG->getMachineNode(InVT == MVT::i32 ? PPC::RLDICL_32 :
2598 PPC::RLDICL, dl, InVT, InputOp,
2599 getI64Imm(0, dl), getI64Imm(63, dl)), 0);
2600 } else if (isLogicOp(OperandOpcode))
2601 return computeLogicOpInGPR(Operand);
2602 return SDValue();
2603 };
2604 LHS = getLogicOperand(LogicOp.getOperand(0));
2605 RHS = getLogicOperand(LogicOp.getOperand(1));
2606
2607 // If a GPR sequence can't be produced for the LHS we can't proceed.
2608 // Not producing a GPR sequence for the RHS is only a problem if this isn't
2609 // a bitwise negation operation.
2610 if (!LHS || (!RHS && !IsBitwiseNegation))
2611 return SDValue();
2612
2613 NumLogicOpsOnComparison++;
2614
2615 // We will use the inputs as 64-bit values.
2616 if (LHS.getValueType() == MVT::i32)
2617 LHS = addExtOrTrunc(LHS, ExtOrTruncConversion::Ext);
2618 if (!IsBitwiseNegation && RHS.getValueType() == MVT::i32)
2619 RHS = addExtOrTrunc(RHS, ExtOrTruncConversion::Ext);
2620
2621 unsigned NewOpc;
2622 switch (LogicOp.getOpcode()) {
2623 default: llvm_unreachable("Unknown logic operation.");
2624 case ISD::AND: NewOpc = PPC::AND8; break;
2625 case ISD::OR: NewOpc = PPC::OR8; break;
2626 case ISD::XOR: NewOpc = PPC::XOR8; break;
2627 }
2628
2629 if (IsBitwiseNegation) {
2630 RHS = getI64Imm(1, dl);
2631 NewOpc = PPC::XORI8;
2632 }
2633
2634 return SDValue(CurDAG->getMachineNode(NewOpc, dl, MVT::i64, LHS, RHS), 0);
2635
2636}
2637
2638/// Try performing logical operations on results of comparisons in GPRs.
2639/// It is typically preferred from a performance perspective over performing
2640/// the operations on individual bits in the CR. We only do this on 64-bit
2641/// targets for now as the code is specialized for 64-bit (it uses 64-bit
2642/// instructions and assumes 64-bit registers).
2643bool PPCDAGToDAGISel::tryLogicOpOfCompares(SDNode *N) {
2644 if (TM.getOptLevel() == CodeGenOpt::None || !TM.isPPC64())
2645 return false;
2646 if (N->getValueType(0) != MVT::i1)
2647 return false;
2648 assert(isLogicOp(N->getOpcode()) &&
2649 "Expected a logic operation on setcc results.");
2650 SDValue LoweredLogical = computeLogicOpInGPR(SDValue(N, 0));
2651 if (!LoweredLogical)
2652 return false;
2653
2654 SDLoc dl(N);
2655 bool IsBitwiseNegate = LoweredLogical.getMachineOpcode() == PPC::XORI8;
2656 unsigned SubRegToExtract = IsBitwiseNegate ? PPC::sub_eq : PPC::sub_gt;
2657 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
2658 SDValue LHS = LoweredLogical.getOperand(0);
2659 SDValue RHS = LoweredLogical.getOperand(1);
2660 SDValue WideOp;
2661 SDValue OpToConvToRecForm;
2662
2663 // Look through any 32-bit to 64-bit implicit extend nodes to find the opcode
2664 // that is input to the XORI.
2665 if (IsBitwiseNegate &&
2666 LoweredLogical.getOperand(0).getMachineOpcode() == PPC::INSERT_SUBREG)
2667 OpToConvToRecForm = LoweredLogical.getOperand(0).getOperand(1);
2668 else if (IsBitwiseNegate)
2669 // If the input to the XORI isn't an extension, that's what we're after.
2670 OpToConvToRecForm = LoweredLogical.getOperand(0);
2671 else
2672 // If this is not an XORI, it is a reg-reg logical op and we can convert it
2673 // to record-form.
2674 OpToConvToRecForm = LoweredLogical;
2675
2676 // Get the record-form version of the node we're looking to use to get the
2677 // CR result from.
2678 uint16_t NonRecOpc = OpToConvToRecForm.getMachineOpcode();
2679 int NewOpc = PPCInstrInfo::getRecordFormOpcode(NonRecOpc);
2680
2681 // Convert the right node to record-form. This is either the logical we're
2682 // looking at or it is the input node to the negation (if we're looking at
2683 // a bitwise negation).
2684 if (NewOpc != -1 && IsBitwiseNegate) {
2685 // The input to the XORI has a record-form. Use it.
2686 assert(LoweredLogical.getConstantOperandVal(1) == 1 &&
2687 "Expected a PPC::XORI8 only for bitwise negation.");
2688 // Emit the record-form instruction.
2689 std::vector<SDValue> Ops;
2690 for (int i = 0, e = OpToConvToRecForm.getNumOperands(); i < e; i++)
2691 Ops.push_back(OpToConvToRecForm.getOperand(i));
2692
2693 WideOp =
2694 SDValue(CurDAG->getMachineNode(NewOpc, dl,
2695 OpToConvToRecForm.getValueType(),
2696 MVT::Glue, Ops), 0);
2697 } else {
2698 assert((NewOpc != -1 || !IsBitwiseNegate) &&
2699 "No record form available for AND8/OR8/XOR8?");
2700 WideOp =
2701 SDValue(CurDAG->getMachineNode(NewOpc == -1 ? PPC::ANDIo8 : NewOpc, dl,
2702 MVT::i64, MVT::Glue, LHS, RHS), 0);
2703 }
2704
2705 // Select this node to a single bit from CR0 set by the record-form node
2706 // just created. For bitwise negation, use the EQ bit which is the equivalent
2707 // of negating the result (i.e. it is a bit set when the result of the
2708 // operation is zero).
2709 SDValue SRIdxVal =
2710 CurDAG->getTargetConstant(SubRegToExtract, dl, MVT::i32);
2711 SDValue CRBit =
2712 SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
2713 MVT::i1, CR0Reg, SRIdxVal,
2714 WideOp.getValue(1)), 0);
2715 ReplaceNode(N, CRBit.getNode());
2716 return true;
2717}
2718
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002719/// If the value isn't guaranteed to be sign-extended to 64-bits, extend it.
2720/// Useful when emitting comparison code for 32-bit values without using
2721/// the compare instruction (which only considers the lower 32-bits).
2722SDValue PPCDAGToDAGISel::signExtendInputIfNeeded(SDValue Input) {
2723 assert(Input.getValueType() == MVT::i32 &&
2724 "Can only sign-extend 32-bit values here.");
2725 unsigned Opc = Input.getOpcode();
2726
2727 // The value was sign extended and then truncated to 32-bits. No need to
2728 // sign extend it again.
2729 if (Opc == ISD::TRUNCATE &&
2730 (Input.getOperand(0).getOpcode() == ISD::AssertSext ||
2731 Input.getOperand(0).getOpcode() == ISD::SIGN_EXTEND))
2732 return Input;
2733
2734 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2735 // The input is a sign-extending load. No reason to sign-extend.
2736 if (InputLoad && InputLoad->getExtensionType() == ISD::SEXTLOAD)
2737 return Input;
2738
2739 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2740 // We don't sign-extend constants and already sign-extended values.
2741 if (InputConst || Opc == ISD::AssertSext || Opc == ISD::SIGN_EXTEND_INREG ||
2742 Opc == ISD::SIGN_EXTEND)
2743 return Input;
2744
2745 SDLoc dl(Input);
2746 SignExtensionsAdded++;
2747 return SDValue(CurDAG->getMachineNode(PPC::EXTSW_32, dl, MVT::i32, Input), 0);
2748}
2749
2750/// If the value isn't guaranteed to be zero-extended to 64-bits, extend it.
2751/// Useful when emitting comparison code for 32-bit values without using
2752/// the compare instruction (which only considers the lower 32-bits).
2753SDValue PPCDAGToDAGISel::zeroExtendInputIfNeeded(SDValue Input) {
2754 assert(Input.getValueType() == MVT::i32 &&
2755 "Can only zero-extend 32-bit values here.");
2756 LoadSDNode *InputLoad = dyn_cast<LoadSDNode>(Input);
2757 unsigned Opc = Input.getOpcode();
2758
2759 // No need to zero-extend loaded values (unless they're loaded with
2760 // a sign-extending load).
2761 if (InputLoad && InputLoad->getExtensionType() != ISD::SEXTLOAD)
2762 return Input;
2763
2764 ConstantSDNode *InputConst = dyn_cast<ConstantSDNode>(Input);
2765 bool InputZExtConst = InputConst && InputConst->getSExtValue() >= 0;
2766 // An ISD::TRUNCATE will be lowered to an EXTRACT_SUBREG so we have
2767 // to conservatively actually clear the high bits. We also don't need to
2768 // zero-extend constants or values that are already zero-extended.
2769 if (InputZExtConst || Opc == ISD::AssertZext || Opc == ISD::ZERO_EXTEND)
2770 return Input;
2771
2772 SDLoc dl(Input);
2773 ZeroExtensionsAdded++;
2774 return SDValue(CurDAG->getMachineNode(PPC::RLDICL_32, dl, MVT::i32, Input,
2775 getI64Imm(0, dl), getI64Imm(32, dl)),
2776 0);
2777}
2778
2779// Handle a 32-bit value in a 64-bit register and vice-versa. These are of
2780// course not actual zero/sign extensions that will generate machine code,
2781// they're just a way to reinterpret a 32 bit value in a register as a
2782// 64 bit value and vice-versa.
2783SDValue PPCDAGToDAGISel::addExtOrTrunc(SDValue NatWidthRes,
2784 ExtOrTruncConversion Conv) {
2785 SDLoc dl(NatWidthRes);
2786
2787 // For reinterpreting 32-bit values as 64 bit values, we generate
2788 // INSERT_SUBREG IMPLICIT_DEF:i64, <input>, TargetConstant:i32<1>
2789 if (Conv == ExtOrTruncConversion::Ext) {
2790 SDValue ImDef(CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl, MVT::i64), 0);
2791 SDValue SubRegIdx =
2792 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2793 return SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl, MVT::i64,
2794 ImDef, NatWidthRes, SubRegIdx), 0);
2795 }
2796
2797 assert(Conv == ExtOrTruncConversion::Trunc &&
2798 "Unknown convertion between 32 and 64 bit values.");
2799 // For reinterpreting 64-bit values as 32-bit values, we just need to
2800 // EXTRACT_SUBREG (i.e. extract the low word).
2801 SDValue SubRegIdx =
2802 CurDAG->getTargetConstant(PPC::sub_32, dl, MVT::i32);
2803 return SDValue(CurDAG->getMachineNode(PPC::EXTRACT_SUBREG, dl, MVT::i32,
2804 NatWidthRes, SubRegIdx), 0);
2805}
2806
2807/// Produces a zero-extended result of comparing two 32-bit values according to
2808/// the passed condition code.
2809SDValue PPCDAGToDAGISel::get32BitZExtCompare(SDValue LHS, SDValue RHS,
2810 ISD::CondCode CC,
2811 int64_t RHSValue, SDLoc dl) {
2812 bool IsRHSZero = RHSValue == 0;
2813 switch (CC) {
2814 default: return SDValue();
2815 case ISD::SETEQ: {
2816 // (zext (setcc %a, %b, seteq)) -> (lshr (cntlzw (xor %a, %b)), 5)
2817 // (zext (setcc %a, 0, seteq)) -> (lshr (cntlzw %a), 5)
2818 SDValue Xor = IsRHSZero ? LHS :
2819 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2820 SDValue Clz =
2821 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Xor), 0);
2822 SDValue ShiftOps[] = { Clz, getI32Imm(27, dl), getI32Imm(5, dl),
2823 getI32Imm(31, dl) };
2824 return SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32,
2825 ShiftOps), 0);
2826 }
2827 }
2828}
2829
2830/// Produces a sign-extended result of comparing two 32-bit values according to
2831/// the passed condition code.
2832SDValue PPCDAGToDAGISel::get32BitSExtCompare(SDValue LHS, SDValue RHS,
2833 ISD::CondCode CC,
2834 int64_t RHSValue, SDLoc dl) {
2835 bool IsRHSZero = RHSValue == 0;
2836 switch (CC) {
2837 default: return SDValue();
2838 case ISD::SETEQ: {
2839 // (sext (setcc %a, %b, seteq)) ->
2840 // (ashr (shl (ctlz (xor %a, %b)), 58), 63)
2841 // (sext (setcc %a, 0, seteq)) ->
2842 // (ashr (shl (ctlz %a), 58), 63)
2843 SDValue CountInput = IsRHSZero ? LHS :
2844 SDValue(CurDAG->getMachineNode(PPC::XOR, dl, MVT::i32, LHS, RHS), 0);
2845 SDValue Cntlzw =
2846 SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, CountInput), 0);
2847 SDValue SHLOps[] = { Cntlzw, getI32Imm(58, dl), getI32Imm(0, dl) };
2848 SDValue Sldi =
2849 SDValue(CurDAG->getMachineNode(PPC::RLDICR_32, dl, MVT::i32, SHLOps), 0);
2850 return SDValue(CurDAG->getMachineNode(PPC::SRADI_32, dl, MVT::i32, Sldi,
2851 getI32Imm(63, dl)), 0);
2852 }
2853 }
2854}
2855
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002856/// Produces a zero-extended result of comparing two 64-bit values according to
2857/// the passed condition code.
2858SDValue PPCDAGToDAGISel::get64BitZExtCompare(SDValue LHS, SDValue RHS,
2859 ISD::CondCode CC,
2860 int64_t RHSValue, SDLoc dl) {
2861 bool IsRHSZero = RHSValue == 0;
2862 switch (CC) {
2863 default: return SDValue();
2864 case ISD::SETEQ: {
2865 // (zext (setcc %a, %b, seteq)) -> (lshr (ctlz (xor %a, %b)), 6)
2866 // (zext (setcc %a, 0, seteq)) -> (lshr (ctlz %a), 6)
2867 SDValue Xor = IsRHSZero ? LHS :
2868 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
2869 SDValue Clz =
2870 SDValue(CurDAG->getMachineNode(PPC::CNTLZD, dl, MVT::i64, Xor), 0);
2871 return SDValue(CurDAG->getMachineNode(PPC::RLDICL, dl, MVT::i64, Clz,
2872 getI64Imm(58, dl), getI64Imm(63, dl)),
2873 0);
2874 }
2875 }
2876}
2877
2878/// Produces a sign-extended result of comparing two 64-bit values according to
2879/// the passed condition code.
2880SDValue PPCDAGToDAGISel::get64BitSExtCompare(SDValue LHS, SDValue RHS,
2881 ISD::CondCode CC,
2882 int64_t RHSValue, SDLoc dl) {
2883 bool IsRHSZero = RHSValue == 0;
2884 switch (CC) {
2885 default: return SDValue();
2886 case ISD::SETEQ: {
2887 // {addc.reg, addc.CA} = (addcarry (xor %a, %b), -1)
2888 // (sext (setcc %a, %b, seteq)) -> (sube addc.reg, addc.reg, addc.CA)
2889 // {addcz.reg, addcz.CA} = (addcarry %a, -1)
2890 // (sext (setcc %a, 0, seteq)) -> (sube addcz.reg, addcz.reg, addcz.CA)
2891 SDValue AddInput = IsRHSZero ? LHS :
2892 SDValue(CurDAG->getMachineNode(PPC::XOR8, dl, MVT::i64, LHS, RHS), 0);
2893 SDValue Addic =
2894 SDValue(CurDAG->getMachineNode(PPC::ADDIC8, dl, MVT::i64, MVT::Glue,
2895 AddInput, getI32Imm(~0U, dl)), 0);
2896 return SDValue(CurDAG->getMachineNode(PPC::SUBFE8, dl, MVT::i64, Addic,
2897 Addic, Addic.getValue(1)), 0);
2898 }
2899 }
2900}
2901
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002902/// Does this SDValue have any uses for which keeping the value in a GPR is
2903/// appropriate. This is meant to be used on values that have type i1 since
2904/// it is somewhat meaningless to ask if values of other types can be kept in
2905/// GPR's.
2906static bool allUsesExtend(SDValue Compare, SelectionDAG *CurDAG) {
2907 assert(Compare.getOpcode() == ISD::SETCC &&
2908 "An ISD::SETCC node required here.");
2909
2910 // For values that have a single use, the caller should obviously already have
2911 // checked if that use is an extending use. We check the other uses here.
2912 if (Compare.hasOneUse())
2913 return true;
2914 // We want the value in a GPR if it is being extended, used for a select, or
2915 // used in logical operations.
2916 for (auto CompareUse : Compare.getNode()->uses())
2917 if (CompareUse->getOpcode() != ISD::SIGN_EXTEND &&
2918 CompareUse->getOpcode() != ISD::ZERO_EXTEND &&
2919 CompareUse->getOpcode() != ISD::SELECT &&
2920 !isLogicOp(CompareUse->getOpcode())) {
2921 OmittedForNonExtendUses++;
2922 return false;
2923 }
2924 return true;
2925}
2926
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002927/// Returns an equivalent of a SETCC node but with the result the same width as
2928/// the inputs. This can nalso be used for SELECT_CC if either the true or false
2929/// values is a power of two while the other is zero.
2930SDValue PPCDAGToDAGISel::getSETCCInGPR(SDValue Compare,
2931 SetccInGPROpts ConvOpts) {
2932 assert((Compare.getOpcode() == ISD::SETCC ||
2933 Compare.getOpcode() == ISD::SELECT_CC) &&
2934 "An ISD::SETCC node required here.");
2935
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00002936 // Don't convert this comparison to a GPR sequence because there are uses
2937 // of the i1 result (i.e. uses that require the result in the CR).
2938 if ((Compare.getOpcode() == ISD::SETCC) && !allUsesExtend(Compare, CurDAG))
2939 return SDValue();
2940
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002941 SDValue LHS = Compare.getOperand(0);
2942 SDValue RHS = Compare.getOperand(1);
2943
2944 // The condition code is operand 2 for SETCC and operand 4 for SELECT_CC.
2945 int CCOpNum = Compare.getOpcode() == ISD::SELECT_CC ? 4 : 2;
2946 ISD::CondCode CC =
2947 cast<CondCodeSDNode>(Compare.getOperand(CCOpNum))->get();
2948 EVT InputVT = LHS.getValueType();
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002949 if (InputVT != MVT::i32 && InputVT != MVT::i64)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002950 return SDValue();
2951
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002952 if (ConvOpts == SetccInGPROpts::ZExtInvert ||
2953 ConvOpts == SetccInGPROpts::SExtInvert)
2954 CC = ISD::getSetCCInverse(CC, true);
2955
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002956 bool Inputs32Bit = InputVT == MVT::i32;
2957 if (ISD::isSignedIntSetCC(CC) && Inputs32Bit) {
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002958 LHS = signExtendInputIfNeeded(LHS);
2959 RHS = signExtendInputIfNeeded(RHS);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002960 } else if (ISD::isUnsignedIntSetCC(CC) && Inputs32Bit) {
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002961 LHS = zeroExtendInputIfNeeded(LHS);
2962 RHS = zeroExtendInputIfNeeded(RHS);
2963 }
2964
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002965 SDLoc dl(Compare);
2966 ConstantSDNode *RHSConst = dyn_cast<ConstantSDNode>(RHS);
2967 int64_t RHSValue = RHSConst ? RHSConst->getSExtValue() : INT64_MAX;
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002968 bool IsSext = ConvOpts == SetccInGPROpts::SExtOrig ||
2969 ConvOpts == SetccInGPROpts::SExtInvert;
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002970
2971 if (IsSext && Inputs32Bit)
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002972 return get32BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
Nemanja Ivanovicaccab032017-05-31 08:04:07 +00002973 else if (Inputs32Bit)
2974 return get32BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
2975 else if (IsSext)
2976 return get64BitSExtCompare(LHS, RHS, CC, RHSValue, dl);
2977 return get64BitZExtCompare(LHS, RHS, CC, RHSValue, dl);
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00002978}
2979
Justin Bognerdc8af062016-05-20 21:43:23 +00002980void PPCDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
Hal Finkelcf599212015-02-25 21:36:59 +00002981 // Transfer memoperands.
2982 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2983 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2984 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
Hal Finkelcf599212015-02-25 21:36:59 +00002985}
2986
Chris Lattner43ff01e2005-08-17 19:33:03 +00002987// Select - Convert the specified operand from a target-independent to a
2988// target-specific node if it hasn't already been changed.
Justin Bognerdc8af062016-05-20 21:43:23 +00002989void PPCDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002990 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +00002991 if (N->isMachineOpcode()) {
2992 N->setNodeId(-1);
Justin Bognerdc8af062016-05-20 21:43:23 +00002993 return; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +00002994 }
Chris Lattner08c319f2005-09-29 00:59:32 +00002995
Hal Finkel51b3fd12014-09-02 06:23:54 +00002996 // In case any misguided DAG-level optimizations form an ADD with a
2997 // TargetConstant operand, crash here instead of miscompiling (by selecting
2998 // an r+r add instead of some kind of r+i add).
2999 if (N->getOpcode() == ISD::ADD &&
3000 N->getOperand(1).getOpcode() == ISD::TargetConstant)
3001 llvm_unreachable("Invalid ADD with TargetConstant operand");
3002
Hal Finkel8adf2252014-12-16 05:51:41 +00003003 // Try matching complex bit permutations before doing anything else.
Justin Bognerdc8af062016-05-20 21:43:23 +00003004 if (tryBitPermutation(N))
3005 return;
Hal Finkel8adf2252014-12-16 05:51:41 +00003006
Chris Lattner43ff01e2005-08-17 19:33:03 +00003007 switch (N->getOpcode()) {
Chris Lattner498915d2005-09-07 23:45:15 +00003008 default: break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003009
Eugene Zelenko8187c192017-01-13 00:58:58 +00003010 case ISD::Constant:
Justin Bognerdc8af062016-05-20 21:43:23 +00003011 if (N->getValueType(0) == MVT::i64) {
3012 ReplaceNode(N, getInt64(CurDAG, N));
3013 return;
3014 }
Jim Laskey095e6f32006-12-12 13:23:43 +00003015 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003016
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +00003017 case ISD::ZERO_EXTEND:
3018 case ISD::SIGN_EXTEND:
3019 if (tryEXTEND(N))
3020 return;
3021 break;
3022
Eugene Zelenko8187c192017-01-13 00:58:58 +00003023 case ISD::SETCC:
Justin Bognerdc8af062016-05-20 21:43:23 +00003024 if (trySETCC(N))
3025 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003026 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003027
Evan Cheng6dc90ca2006-02-09 00:37:58 +00003028 case PPCISD::GlobalBaseReg:
Justin Bognerdc8af062016-05-20 21:43:23 +00003029 ReplaceNode(N, getGlobalBaseReg());
3030 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00003031
Hal Finkelb5e9b042014-12-11 22:51:06 +00003032 case ISD::FrameIndex:
Justin Bognerdc8af062016-05-20 21:43:23 +00003033 selectFrameIndex(N, N);
3034 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00003035
Ulrich Weigandd5ebc622013-07-03 17:05:42 +00003036 case PPCISD::MFOCRF: {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003037 SDValue InFlag = N->getOperand(1);
Justin Bognerdc8af062016-05-20 21:43:23 +00003038 ReplaceNode(N, CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
3039 N->getOperand(0), InFlag));
3040 return;
Chris Lattner6961fc72006-03-26 10:06:40 +00003041 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003042
Eugene Zelenko8187c192017-01-13 00:58:58 +00003043 case PPCISD::READ_TIME_BASE:
Justin Bognerdc8af062016-05-20 21:43:23 +00003044 ReplaceNode(N, CurDAG->getMachineNode(PPC::ReadTB, dl, MVT::i32, MVT::i32,
3045 MVT::Other, N->getOperand(0)));
3046 return;
Hal Finkelbbdee932014-12-02 22:01:00 +00003047
Hal Finkel13d104b2014-12-11 18:37:52 +00003048 case PPCISD::SRA_ADDZE: {
3049 SDValue N0 = N->getOperand(0);
3050 SDValue ShiftAmt =
3051 CurDAG->getTargetConstant(*cast<ConstantSDNode>(N->getOperand(1))->
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003052 getConstantIntValue(), dl,
3053 N->getValueType(0));
Hal Finkel13d104b2014-12-11 18:37:52 +00003054 if (N->getValueType(0) == MVT::i64) {
3055 SDNode *Op =
3056 CurDAG->getMachineNode(PPC::SRADI, dl, MVT::i64, MVT::Glue,
3057 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00003058 CurDAG->SelectNodeTo(N, PPC::ADDZE8, MVT::i64, SDValue(Op, 0),
3059 SDValue(Op, 1));
3060 return;
Hal Finkel13d104b2014-12-11 18:37:52 +00003061 } else {
3062 assert(N->getValueType(0) == MVT::i32 &&
3063 "Expecting i64 or i32 in PPCISD::SRA_ADDZE");
3064 SDNode *Op =
3065 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
3066 N0, ShiftAmt);
Justin Bognerdc8af062016-05-20 21:43:23 +00003067 CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, SDValue(Op, 0),
3068 SDValue(Op, 1));
3069 return;
Chris Lattnerdc664572005-08-25 17:50:06 +00003070 }
Chris Lattner6e184f22005-08-25 22:04:30 +00003071 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003072
Chris Lattnerce645542006-11-10 02:08:47 +00003073 case ISD::LOAD: {
3074 // Handle preincrement loads.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00003075 LoadSDNode *LD = cast<LoadSDNode>(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003076 EVT LoadedVT = LD->getMemoryVT();
Andrew Trickc416ba62010-12-24 04:28:06 +00003077
Chris Lattnerce645542006-11-10 02:08:47 +00003078 // Normal loads are handled by code generated from the .td file.
3079 if (LD->getAddressingMode() != ISD::PRE_INC)
3080 break;
Andrew Trickc416ba62010-12-24 04:28:06 +00003081
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003082 SDValue Offset = LD->getOffset();
Ulrich Weigandd1b99d32013-03-22 14:58:17 +00003083 if (Offset.getOpcode() == ISD::TargetConstant ||
Chris Lattnerc5102bf2006-11-11 04:53:30 +00003084 Offset.getOpcode() == ISD::TargetGlobalAddress) {
Andrew Trickc416ba62010-12-24 04:28:06 +00003085
Chris Lattner474b5b72006-11-15 19:55:13 +00003086 unsigned Opcode;
3087 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
Owen Anderson9f944592009-08-11 20:47:22 +00003088 if (LD->getValueType(0) != MVT::i64) {
Chris Lattner474b5b72006-11-15 19:55:13 +00003089 // Handle PPC32 integer and normal FP loads.
Owen Anderson9f944592009-08-11 20:47:22 +00003090 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3091 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003092 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00003093 case MVT::f64: Opcode = PPC::LFDU; break;
3094 case MVT::f32: Opcode = PPC::LFSU; break;
3095 case MVT::i32: Opcode = PPC::LWZU; break;
3096 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
3097 case MVT::i1:
3098 case MVT::i8: Opcode = PPC::LBZU; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00003099 }
3100 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00003101 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
3102 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3103 switch (LoadedVT.getSimpleVT().SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003104 default: llvm_unreachable("Invalid PPC load type!");
Owen Anderson9f944592009-08-11 20:47:22 +00003105 case MVT::i64: Opcode = PPC::LDU; break;
3106 case MVT::i32: Opcode = PPC::LWZU8; break;
3107 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
3108 case MVT::i1:
3109 case MVT::i8: Opcode = PPC::LBZU8; break;
Chris Lattner474b5b72006-11-15 19:55:13 +00003110 }
3111 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003112
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003113 SDValue Chain = LD->getChain();
3114 SDValue Base = LD->getBasePtr();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003115 SDValue Ops[] = { Offset, Base, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00003116 SDNode *MN = CurDAG->getMachineNode(
3117 Opcode, dl, LD->getValueType(0),
3118 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
3119 transferMemOperands(N, MN);
3120 ReplaceNode(N, MN);
3121 return;
Chris Lattnerce645542006-11-10 02:08:47 +00003122 } else {
Hal Finkelca542be2012-06-20 15:43:03 +00003123 unsigned Opcode;
3124 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
3125 if (LD->getValueType(0) != MVT::i64) {
3126 // Handle PPC32 integer and normal FP loads.
3127 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
3128 switch (LoadedVT.getSimpleVT().SimpleTy) {
3129 default: llvm_unreachable("Invalid PPC load type!");
Hal Finkelc93a9a22015-02-25 01:06:45 +00003130 case MVT::v4f64: Opcode = PPC::QVLFDUX; break; // QPX
3131 case MVT::v4f32: Opcode = PPC::QVLFSUX; break; // QPX
Hal Finkelca542be2012-06-20 15:43:03 +00003132 case MVT::f64: Opcode = PPC::LFDUX; break;
3133 case MVT::f32: Opcode = PPC::LFSUX; break;
3134 case MVT::i32: Opcode = PPC::LWZUX; break;
3135 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
3136 case MVT::i1:
3137 case MVT::i8: Opcode = PPC::LBZUX; break;
3138 }
3139 } else {
3140 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
3141 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
3142 "Invalid sext update load");
3143 switch (LoadedVT.getSimpleVT().SimpleTy) {
3144 default: llvm_unreachable("Invalid PPC load type!");
3145 case MVT::i64: Opcode = PPC::LDUX; break;
3146 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
3147 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
3148 case MVT::i1:
3149 case MVT::i8: Opcode = PPC::LBZUX8; break;
3150 }
3151 }
3152
3153 SDValue Chain = LD->getChain();
3154 SDValue Base = LD->getBasePtr();
Ulrich Weigande90b0222013-03-22 14:58:48 +00003155 SDValue Ops[] = { Base, Offset, Chain };
Justin Bognerdc8af062016-05-20 21:43:23 +00003156 SDNode *MN = CurDAG->getMachineNode(
3157 Opcode, dl, LD->getValueType(0),
3158 PPCLowering->getPointerTy(CurDAG->getDataLayout()), MVT::Other, Ops);
3159 transferMemOperands(N, MN);
3160 ReplaceNode(N, MN);
3161 return;
Chris Lattnerce645542006-11-10 02:08:47 +00003162 }
3163 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003164
Nate Begemanb3821a32005-08-18 07:30:46 +00003165 case ISD::AND: {
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003166 if (tryLogicOpOfCompares(N))
3167 return;
3168
Nate Begemand31efd12006-09-22 05:01:56 +00003169 unsigned Imm, Imm2, SH, MB, ME;
Hal Finkele39526a2012-08-28 02:10:15 +00003170 uint64_t Imm64;
Nate Begemand31efd12006-09-22 05:01:56 +00003171
Nate Begemanb3821a32005-08-18 07:30:46 +00003172 // If this is an and of a value rotated between 0 and 31 bits and then and'd
3173 // with a mask, emit rlwinm
Chris Lattner97b3da12006-06-27 00:04:13 +00003174 if (isInt32Immediate(N->getOperand(1), Imm) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00003175 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003176 SDValue Val = N->getOperand(0).getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003177 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl),
3178 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003179 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3180 return;
Nate Begemanb3821a32005-08-18 07:30:46 +00003181 }
Nate Begemand31efd12006-09-22 05:01:56 +00003182 // If this is just a masked value where the input is not handled above, and
3183 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
3184 if (isInt32Immediate(N->getOperand(1), Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00003185 isRunOfOnes(Imm, MB, ME) &&
Nate Begemand31efd12006-09-22 05:01:56 +00003186 N->getOperand(0).getOpcode() != ISD::ROTL) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003187 SDValue Val = N->getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003188 SDValue Ops[] = { Val, getI32Imm(0, dl), getI32Imm(MB, dl),
3189 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003190 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3191 return;
Nate Begemand31efd12006-09-22 05:01:56 +00003192 }
Hal Finkele39526a2012-08-28 02:10:15 +00003193 // If this is a 64-bit zero-extension mask, emit rldicl.
3194 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
3195 isMask_64(Imm64)) {
3196 SDValue Val = N->getOperand(0);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00003197 MB = 64 - countTrailingOnes(Imm64);
Hal Finkel22498fa2013-11-20 01:10:15 +00003198 SH = 0;
3199
Ehsan Amiri1f31e912016-10-24 15:46:58 +00003200 if (Val.getOpcode() == ISD::ANY_EXTEND) {
3201 auto Op0 = Val.getOperand(0);
3202 if ( Op0.getOpcode() == ISD::SRL &&
3203 isInt32Immediate(Op0.getOperand(1).getNode(), Imm) && Imm <= MB) {
3204
3205 auto ResultType = Val.getNode()->getValueType(0);
3206 auto ImDef = CurDAG->getMachineNode(PPC::IMPLICIT_DEF, dl,
3207 ResultType);
3208 SDValue IDVal (ImDef, 0);
3209
3210 Val = SDValue(CurDAG->getMachineNode(PPC::INSERT_SUBREG, dl,
3211 ResultType, IDVal, Op0.getOperand(0),
3212 getI32Imm(1, dl)), 0);
3213 SH = 64 - Imm;
3214 }
3215 }
3216
Hal Finkel22498fa2013-11-20 01:10:15 +00003217 // If the operand is a logical right shift, we can fold it into this
3218 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
3219 // for n <= mb. The right shift is really a left rotate followed by a
3220 // mask, and this mask is a more-restrictive sub-mask of the mask implied
3221 // by the shift.
3222 if (Val.getOpcode() == ISD::SRL &&
3223 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
3224 assert(Imm < 64 && "Illegal shift amount");
3225 Val = Val.getOperand(0);
3226 SH = 64 - Imm;
3227 }
3228
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003229 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003230 CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops);
3231 return;
Hal Finkele39526a2012-08-28 02:10:15 +00003232 }
Nemanja Ivanovic82d53ed2017-02-24 18:03:16 +00003233 // If this is a negated 64-bit zero-extension mask,
3234 // i.e. the immediate is a sequence of ones from most significant side
3235 // and all zero for reminder, we should use rldicr.
3236 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
3237 isMask_64(~Imm64)) {
3238 SDValue Val = N->getOperand(0);
3239 MB = 63 - countTrailingOnes(~Imm64);
3240 SH = 0;
3241 SDValue Ops[] = { Val, getI32Imm(SH, dl), getI32Imm(MB, dl) };
3242 CurDAG->SelectNodeTo(N, PPC::RLDICR, MVT::i64, Ops);
3243 return;
3244 }
3245
Nate Begemand31efd12006-09-22 05:01:56 +00003246 // AND X, 0 -> 0, not "rlwinm 32".
3247 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003248 ReplaceUses(SDValue(N, 0), N->getOperand(1));
Justin Bognerdc8af062016-05-20 21:43:23 +00003249 return;
Nate Begemand31efd12006-09-22 05:01:56 +00003250 }
Nate Begeman9aea6e42005-12-24 01:00:15 +00003251 // ISD::OR doesn't get all the bitfield insertion fun.
Hal Finkelb1518d62015-09-05 00:02:59 +00003252 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
3253 // bitfield insert.
Andrew Trickc416ba62010-12-24 04:28:06 +00003254 if (isInt32Immediate(N->getOperand(1), Imm) &&
Nate Begeman9aea6e42005-12-24 01:00:15 +00003255 N->getOperand(0).getOpcode() == ISD::OR &&
Chris Lattner97b3da12006-06-27 00:04:13 +00003256 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
Hal Finkelb1518d62015-09-05 00:02:59 +00003257 // The idea here is to check whether this is equivalent to:
3258 // (c1 & m) | (x & ~m)
3259 // where m is a run-of-ones mask. The logic here is that, for each bit in
3260 // c1 and c2:
3261 // - if both are 1, then the output will be 1.
3262 // - if both are 0, then the output will be 0.
3263 // - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
3264 // come from x.
3265 // - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
3266 // be 0.
3267 // If that last condition is never the case, then we can form m from the
3268 // bits that are the same between c1 and c2.
Chris Lattner20c88df2006-01-05 18:32:49 +00003269 unsigned MB, ME;
Hal Finkelb1518d62015-09-05 00:02:59 +00003270 if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003271 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Evan Chengc3acfc02006-08-27 08:14:06 +00003272 N->getOperand(0).getOperand(1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003273 getI32Imm(0, dl), getI32Imm(MB, dl),
3274 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003275 ReplaceNode(N, CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops));
3276 return;
Nate Begeman9aea6e42005-12-24 01:00:15 +00003277 }
3278 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003279
Chris Lattner1de57062005-09-29 23:33:31 +00003280 // Other cases are autogenerated.
3281 break;
Nate Begemanb3821a32005-08-18 07:30:46 +00003282 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003283 case ISD::OR: {
Owen Anderson9f944592009-08-11 20:47:22 +00003284 if (N->getValueType(0) == MVT::i32)
Justin Bognerdc8af062016-05-20 21:43:23 +00003285 if (tryBitfieldInsert(N))
3286 return;
Andrew Trickc416ba62010-12-24 04:28:06 +00003287
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003288 if (tryLogicOpOfCompares(N))
3289 return;
3290
Hal Finkelb5e9b042014-12-11 22:51:06 +00003291 short Imm;
3292 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
3293 isIntS16Immediate(N->getOperand(1), Imm)) {
Craig Topperd0af7e82017-04-28 05:31:46 +00003294 KnownBits LHSKnown;
3295 CurDAG->computeKnownBits(N->getOperand(0), LHSKnown);
Hal Finkelb5e9b042014-12-11 22:51:06 +00003296
3297 // If this is equivalent to an add, then we can fold it with the
3298 // FrameIndex calculation.
Craig Topperd0af7e82017-04-28 05:31:46 +00003299 if ((LHSKnown.Zero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
Justin Bognerdc8af062016-05-20 21:43:23 +00003300 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
3301 return;
3302 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003303 }
3304
Chris Lattner1de57062005-09-29 23:33:31 +00003305 // Other cases are autogenerated.
3306 break;
Hal Finkelb5e9b042014-12-11 22:51:06 +00003307 }
Nemanja Ivanovice597bd82017-05-31 05:40:25 +00003308 case ISD::XOR: {
3309 if (tryLogicOpOfCompares(N))
3310 return;
3311 break;
3312 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003313 case ISD::ADD: {
3314 short Imm;
3315 if (N->getOperand(0)->getOpcode() == ISD::FrameIndex &&
Justin Bognerdc8af062016-05-20 21:43:23 +00003316 isIntS16Immediate(N->getOperand(1), Imm)) {
3317 selectFrameIndex(N, N->getOperand(0).getNode(), (int)Imm);
3318 return;
3319 }
Hal Finkelb5e9b042014-12-11 22:51:06 +00003320
3321 break;
3322 }
Nate Begeman33acb2c2005-08-18 23:38:00 +00003323 case ISD::SHL: {
3324 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00003325 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003326 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003327 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003328 getI32Imm(SH, dl), getI32Imm(MB, dl),
3329 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003330 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3331 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00003332 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003333
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003334 // Other cases are autogenerated.
3335 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00003336 }
3337 case ISD::SRL: {
3338 unsigned Imm, SH, MB, ME;
Gabor Greiff304a7a2008-08-28 21:40:38 +00003339 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
Andrew Trickc416ba62010-12-24 04:28:06 +00003340 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003341 SDValue Ops[] = { N->getOperand(0).getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003342 getI32Imm(SH, dl), getI32Imm(MB, dl),
3343 getI32Imm(ME, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003344 CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops);
3345 return;
Nate Begeman9eaa6ba2005-10-19 01:12:32 +00003346 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003347
Nate Begeman9f3c26c2005-10-19 18:42:01 +00003348 // Other cases are autogenerated.
3349 break;
Nate Begeman33acb2c2005-08-18 23:38:00 +00003350 }
Hal Finkel940ab932014-02-28 00:27:01 +00003351 // FIXME: Remove this once the ANDI glue bug is fixed:
3352 case PPCISD::ANDIo_1_EQ_BIT:
3353 case PPCISD::ANDIo_1_GT_BIT: {
3354 if (!ANDIGlueBug)
3355 break;
3356
3357 EVT InVT = N->getOperand(0).getValueType();
3358 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
3359 "Invalid input type for ANDIo_1_EQ_BIT");
3360
3361 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
3362 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
3363 N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003364 CurDAG->getTargetConstant(1, dl, InVT)),
3365 0);
Hal Finkel940ab932014-02-28 00:27:01 +00003366 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
3367 SDValue SRIdxVal =
3368 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003369 PPC::sub_eq : PPC::sub_gt, dl, MVT::i32);
Hal Finkel940ab932014-02-28 00:27:01 +00003370
Justin Bognerdc8af062016-05-20 21:43:23 +00003371 CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1, CR0Reg,
3372 SRIdxVal, SDValue(AndI.getNode(), 1) /* glue */);
3373 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003374 }
Chris Lattnerbec817c2005-08-26 18:46:49 +00003375 case ISD::SELECT_CC: {
3376 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
Mehdi Amini44ede332015-07-09 02:09:04 +00003377 EVT PtrVT =
3378 CurDAG->getTargetLoweringInfo().getPointerTy(CurDAG->getDataLayout());
Roman Divacky254f8212011-06-20 15:28:39 +00003379 bool isPPC64 = (PtrVT == MVT::i64);
Andrew Trickc416ba62010-12-24 04:28:06 +00003380
Hal Finkel940ab932014-02-28 00:27:01 +00003381 // If this is a select of i1 operands, we'll pattern match it.
Eric Christopher1b8e7632014-05-22 01:07:24 +00003382 if (PPCSubTarget->useCRBits() &&
Hal Finkel940ab932014-02-28 00:27:01 +00003383 N->getOperand(0).getValueType() == MVT::i1)
3384 break;
3385
Chris Lattner97b3da12006-06-27 00:04:13 +00003386 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
Roman Divacky254f8212011-06-20 15:28:39 +00003387 if (!isPPC64)
3388 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
3389 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
3390 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
3391 if (N1C->isNullValue() && N3C->isNullValue() &&
3392 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
3393 // FIXME: Implement this optzn for PPC64.
3394 N->getValueType(0) == MVT::i32) {
3395 SDNode *Tmp =
3396 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003397 N->getOperand(0), getI32Imm(~0U, dl));
Justin Bognerdc8af062016-05-20 21:43:23 +00003398 CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(Tmp, 0),
3399 N->getOperand(0), SDValue(Tmp, 1));
3400 return;
Roman Divacky254f8212011-06-20 15:28:39 +00003401 }
Chris Lattner9b577f12005-08-26 21:23:58 +00003402
Dale Johannesenab8e4422009-02-06 19:16:40 +00003403 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
Hal Finkel940ab932014-02-28 00:27:01 +00003404
3405 if (N->getValueType(0) == MVT::i1) {
3406 // An i1 select is: (c & t) | (!c & f).
3407 bool Inv;
3408 unsigned Idx = getCRIdxForSetCC(CC, Inv);
3409
3410 unsigned SRI;
3411 switch (Idx) {
3412 default: llvm_unreachable("Invalid CC index");
3413 case 0: SRI = PPC::sub_lt; break;
3414 case 1: SRI = PPC::sub_gt; break;
3415 case 2: SRI = PPC::sub_eq; break;
3416 case 3: SRI = PPC::sub_un; break;
3417 }
3418
3419 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
3420
3421 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
3422 CCBit, CCBit), 0);
3423 SDValue C = Inv ? NotCCBit : CCBit,
3424 NotC = Inv ? CCBit : NotCCBit;
3425
3426 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
3427 C, N->getOperand(2)), 0);
3428 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
3429 NotC, N->getOperand(3)), 0);
3430
Justin Bognerdc8af062016-05-20 21:43:23 +00003431 CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
3432 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003433 }
3434
Chris Lattner8c6a41e2006-11-17 22:10:59 +00003435 unsigned BROpc = getPredicateForSetCC(CC);
Chris Lattner9b577f12005-08-26 21:23:58 +00003436
Chris Lattnerd3eee1a2005-10-01 01:35:02 +00003437 unsigned SelectCCOp;
Owen Anderson9f944592009-08-11 20:47:22 +00003438 if (N->getValueType(0) == MVT::i32)
Chris Lattner97b3da12006-06-27 00:04:13 +00003439 SelectCCOp = PPC::SELECT_CC_I4;
Owen Anderson9f944592009-08-11 20:47:22 +00003440 else if (N->getValueType(0) == MVT::i64)
Chris Lattner97b3da12006-06-27 00:04:13 +00003441 SelectCCOp = PPC::SELECT_CC_I8;
Owen Anderson9f944592009-08-11 20:47:22 +00003442 else if (N->getValueType(0) == MVT::f32)
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00003443 if (PPCSubTarget->hasP8Vector())
3444 SelectCCOp = PPC::SELECT_CC_VSSRC;
3445 else
3446 SelectCCOp = PPC::SELECT_CC_F4;
Owen Anderson9f944592009-08-11 20:47:22 +00003447 else if (N->getValueType(0) == MVT::f64)
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00003448 if (PPCSubTarget->hasVSX())
3449 SelectCCOp = PPC::SELECT_CC_VSFRC;
3450 else
3451 SelectCCOp = PPC::SELECT_CC_F8;
Hal Finkelc93a9a22015-02-25 01:06:45 +00003452 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f64)
3453 SelectCCOp = PPC::SELECT_CC_QFRC;
3454 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4f32)
3455 SelectCCOp = PPC::SELECT_CC_QSRC;
3456 else if (PPCSubTarget->hasQPX() && N->getValueType(0) == MVT::v4i1)
3457 SelectCCOp = PPC::SELECT_CC_QBRC;
Bill Schmidt61e65232014-10-22 13:13:40 +00003458 else if (N->getValueType(0) == MVT::v2f64 ||
3459 N->getValueType(0) == MVT::v2i64)
3460 SelectCCOp = PPC::SELECT_CC_VSRC;
Chris Lattner0a3d1bb2006-04-08 22:45:08 +00003461 else
3462 SelectCCOp = PPC::SELECT_CC_VRRC;
3463
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003464 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003465 getI32Imm(BROpc, dl) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003466 CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops);
3467 return;
Chris Lattnerbec817c2005-08-26 18:46:49 +00003468 }
Hal Finkel732f0f72014-03-26 12:49:28 +00003469 case ISD::VSELECT:
Eric Christopher1b8e7632014-05-22 01:07:24 +00003470 if (PPCSubTarget->hasVSX()) {
Hal Finkel732f0f72014-03-26 12:49:28 +00003471 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003472 CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops);
3473 return;
Hal Finkel732f0f72014-03-26 12:49:28 +00003474 }
Hal Finkel732f0f72014-03-26 12:49:28 +00003475 break;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003476
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003477 case ISD::VECTOR_SHUFFLE:
Eric Christopher1b8e7632014-05-22 01:07:24 +00003478 if (PPCSubTarget->hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003479 N->getValueType(0) == MVT::v2i64)) {
3480 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Kyle Butt015f4fc2015-12-02 18:53:33 +00003481
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003482 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
3483 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
3484 unsigned DM[2];
3485
3486 for (int i = 0; i < 2; ++i)
3487 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
3488 DM[i] = 0;
3489 else
3490 DM[i] = 1;
3491
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003492 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
3493 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3494 isa<LoadSDNode>(Op1.getOperand(0))) {
3495 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
3496 SDValue Base, Offset;
3497
Nemanja Ivanovicbe5f0c02015-11-02 14:01:11 +00003498 if (LD->isUnindexed() && LD->hasOneUse() && Op1.hasOneUse() &&
Bill Schmidt048cc972015-10-14 20:45:00 +00003499 (LD->getMemoryVT() == MVT::f64 ||
3500 LD->getMemoryVT() == MVT::i64) &&
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003501 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
3502 SDValue Chain = LD->getChain();
3503 SDValue Ops[] = { Base, Offset, Chain };
Sean Fertile3c8c3852017-01-26 18:59:15 +00003504 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3505 MemOp[0] = LD->getMemOperand();
Benjamin Kramer58dadd52017-04-20 18:29:14 +00003506 SDNode *NewN = CurDAG->SelectNodeTo(N, PPC::LXVDSX,
3507 N->getValueType(0), Ops);
Sean Fertile3c8c3852017-01-26 18:59:15 +00003508 cast<MachineSDNode>(NewN)->setMemRefs(MemOp, MemOp + 1);
Justin Bognerdc8af062016-05-20 21:43:23 +00003509 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003510 }
3511 }
3512
Bill Schmidtae94f112015-07-01 19:40:07 +00003513 // For little endian, we must swap the input operands and adjust
3514 // the mask elements (reverse and invert them).
3515 if (PPCSubTarget->isLittleEndian()) {
3516 std::swap(Op1, Op2);
3517 unsigned tmp = DM[0];
3518 DM[0] = 1 - DM[1];
3519 DM[1] = 1 - tmp;
3520 }
3521
3522 SDValue DMV = CurDAG->getTargetConstant(DM[1] | (DM[0] << 1), dl,
3523 MVT::i32);
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003524 SDValue Ops[] = { Op1, Op2, DMV };
Justin Bognerdc8af062016-05-20 21:43:23 +00003525 CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops);
3526 return;
Hal Finkeldf3e34d2014-03-26 22:58:37 +00003527 }
3528
3529 break;
Hal Finkel25c19922013-05-15 21:37:41 +00003530 case PPCISD::BDNZ:
3531 case PPCISD::BDZ: {
Eric Christopher1b8e7632014-05-22 01:07:24 +00003532 bool IsPPC64 = PPCSubTarget->isPPC64();
Hal Finkel25c19922013-05-15 21:37:41 +00003533 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003534 CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ
3535 ? (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ)
3536 : (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
3537 MVT::Other, Ops);
3538 return;
Hal Finkel25c19922013-05-15 21:37:41 +00003539 }
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003540 case PPCISD::COND_BRANCH: {
Dan Gohman7a638a82008-11-05 17:16:24 +00003541 // Op #0 is the Chain.
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003542 // Op #1 is the PPC::PRED_* number.
3543 // Op #2 is the CR#
3544 // Op #3 is the Dest MBB
Dan Gohmanf14b77e2008-11-05 04:14:16 +00003545 // Op #4 is the Flag.
Evan Cheng58d1eac2007-06-29 01:25:06 +00003546 // Prevent PPC::PRED_* from being selected into LI.
Hal Finkel65539e32015-12-12 00:32:00 +00003547 unsigned PCC = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3548 if (EnableBranchHint)
3549 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(3));
3550
3551 SDValue Pred = getI32Imm(PCC, dl);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003552 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003553 N->getOperand(0), N->getOperand(4) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003554 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
3555 return;
Chris Lattnerbe9377a2006-11-17 22:37:34 +00003556 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +00003557 case ISD::BR_CC: {
Chris Lattner2a1823d2005-08-21 18:50:37 +00003558 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Hal Finkel940ab932014-02-28 00:27:01 +00003559 unsigned PCC = getPredicateForSetCC(CC);
3560
3561 if (N->getOperand(2).getValueType() == MVT::i1) {
3562 unsigned Opc;
3563 bool Swap;
3564 switch (PCC) {
3565 default: llvm_unreachable("Unexpected Boolean-operand predicate");
3566 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
3567 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
3568 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
3569 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
3570 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
3571 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
3572 }
3573
3574 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
3575 N->getOperand(Swap ? 3 : 2),
3576 N->getOperand(Swap ? 2 : 3)), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003577 CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other, BitComp, N->getOperand(4),
3578 N->getOperand(0));
3579 return;
Hal Finkel940ab932014-02-28 00:27:01 +00003580 }
3581
Hal Finkel65539e32015-12-12 00:32:00 +00003582 if (EnableBranchHint)
3583 PCC |= getBranchHint(PCC, FuncInfo, N->getOperand(4));
3584
Dale Johannesenab8e4422009-02-06 19:16:40 +00003585 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 SDValue Ops[] = { getI32Imm(PCC, dl), CondCode,
Evan Chengc3acfc02006-08-27 08:14:06 +00003587 N->getOperand(4), N->getOperand(0) };
Justin Bognerdc8af062016-05-20 21:43:23 +00003588 CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops);
3589 return;
Chris Lattner2a1823d2005-08-21 18:50:37 +00003590 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +00003591 case ISD::BRIND: {
Chris Lattnerb055c872006-06-10 01:15:02 +00003592 // FIXME: Should custom lower this.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003593 SDValue Chain = N->getOperand(0);
3594 SDValue Target = N->getOperand(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003595 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
Roman Divackya4a59ae2011-06-03 15:47:49 +00003596 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
Hal Finkel528ff4b2011-12-08 04:36:44 +00003597 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
Dan Gohman32f71d72009-09-25 18:54:59 +00003598 Chain), 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003599 CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
3600 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +00003601 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003602 case PPCISD::TOC_ENTRY: {
Justin Hibbitsa88b6052014-11-12 15:16:30 +00003603 assert ((PPCSubTarget->isPPC64() || PPCSubTarget->isSVR4ABI()) &&
3604 "Only supported for 64-bit ABI and 32-bit SVR4");
Hal Finkel3ee2af72014-07-18 23:29:49 +00003605 if (PPCSubTarget->isSVR4ABI() && !PPCSubTarget->isPPC64()) {
3606 SDValue GA = N->getOperand(0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003607 SDNode *MN = CurDAG->getMachineNode(PPC::LWZtoc, dl, MVT::i32, GA,
3608 N->getOperand(1));
3609 transferMemOperands(N, MN);
3610 ReplaceNode(N, MN);
3611 return;
Justin Hibbits3476db42014-08-28 04:40:55 +00003612 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003613
Bill Schmidt27917782013-02-21 17:12:27 +00003614 // For medium and large code model, we generate two instructions as
3615 // described below. Otherwise we allow SelectCodeCommon to handle this,
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003616 // selecting one of LDtoc, LDtocJTI, LDtocCPT, and LDtocBA.
Bill Schmidt27917782013-02-21 17:12:27 +00003617 CodeModel::Model CModel = TM.getCodeModel();
3618 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +00003619 break;
3620
Bill Schmidt5d82f092014-06-16 21:36:02 +00003621 // The first source operand is a TargetGlobalAddress or a TargetJumpTable.
Eric Christopherc1808362015-11-20 20:51:31 +00003622 // If it must be toc-referenced according to PPCSubTarget, we generate:
Bill Schmidt34627e32012-11-27 17:35:46 +00003623 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
3624 // Otherwise we generate:
3625 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
3626 SDValue GA = N->getOperand(0);
3627 SDValue TOCbase = N->getOperand(1);
3628 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
Hal Finkelcf599212015-02-25 21:36:59 +00003629 TOCbase, GA);
Bill Schmidt34627e32012-11-27 17:35:46 +00003630
Ulrich Weigandc8c2ea22014-10-31 10:33:14 +00003631 if (isa<JumpTableSDNode>(GA) || isa<BlockAddressSDNode>(GA) ||
Justin Bognerdc8af062016-05-20 21:43:23 +00003632 CModel == CodeModel::Large) {
3633 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3634 SDValue(Tmp, 0));
3635 transferMemOperands(N, MN);
3636 ReplaceNode(N, MN);
3637 return;
3638 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003639
3640 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
Eric Christopherc1808362015-11-20 20:51:31 +00003641 const GlobalValue *GV = G->getGlobal();
3642 unsigned char GVFlags = PPCSubTarget->classifyGlobalReference(GV);
3643 if (GVFlags & PPCII::MO_NLP_FLAG) {
Justin Bognerdc8af062016-05-20 21:43:23 +00003644 SDNode *MN = CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
3645 SDValue(Tmp, 0));
3646 transferMemOperands(N, MN);
3647 ReplaceNode(N, MN);
3648 return;
Eric Christopherc1808362015-11-20 20:51:31 +00003649 }
Bill Schmidt34627e32012-11-27 17:35:46 +00003650 }
3651
Justin Bognerdc8af062016-05-20 21:43:23 +00003652 ReplaceNode(N, CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
3653 SDValue(Tmp, 0), GA));
3654 return;
Bill Schmidt34627e32012-11-27 17:35:46 +00003655 }
Eugene Zelenko8187c192017-01-13 00:58:58 +00003656 case PPCISD::PPC32_PICGOT:
Hal Finkel7c8ae532014-07-25 17:47:22 +00003657 // Generate a PIC-safe GOT reference.
3658 assert(!PPCSubTarget->isPPC64() && PPCSubTarget->isSVR4ABI() &&
3659 "PPCISD::PPC32_PICGOT is only supported for 32-bit SVR4");
Justin Bognerdc8af062016-05-20 21:43:23 +00003660 CurDAG->SelectNodeTo(N, PPC::PPC32PICGOT,
3661 PPCLowering->getPointerTy(CurDAG->getDataLayout()),
3662 MVT::i32);
3663 return;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003664
Bill Schmidt51e79512013-02-20 15:50:31 +00003665 case PPCISD::VADD_SPLAT: {
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003666 // This expands into one of three sequences, depending on whether
3667 // the first operand is odd or even, positive or negative.
Bill Schmidt51e79512013-02-20 15:50:31 +00003668 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
3669 isa<ConstantSDNode>(N->getOperand(1)) &&
3670 "Invalid operand on VADD_SPLAT!");
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003671
3672 int Elt = N->getConstantOperandVal(0);
Bill Schmidt51e79512013-02-20 15:50:31 +00003673 int EltSize = N->getConstantOperandVal(1);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003674 unsigned Opc1, Opc2, Opc3;
Bill Schmidt51e79512013-02-20 15:50:31 +00003675 EVT VT;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003676
Bill Schmidt51e79512013-02-20 15:50:31 +00003677 if (EltSize == 1) {
3678 Opc1 = PPC::VSPLTISB;
3679 Opc2 = PPC::VADDUBM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003680 Opc3 = PPC::VSUBUBM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003681 VT = MVT::v16i8;
3682 } else if (EltSize == 2) {
3683 Opc1 = PPC::VSPLTISH;
3684 Opc2 = PPC::VADDUHM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003685 Opc3 = PPC::VSUBUHM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003686 VT = MVT::v8i16;
3687 } else {
3688 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
3689 Opc1 = PPC::VSPLTISW;
3690 Opc2 = PPC::VADDUWM;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003691 Opc3 = PPC::VSUBUWM;
Bill Schmidt51e79512013-02-20 15:50:31 +00003692 VT = MVT::v4i32;
3693 }
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003694
3695 if ((Elt & 1) == 0) {
3696 // Elt is even, in the range [-32,-18] + [16,30].
3697 //
3698 // Convert: VADD_SPLAT elt, size
3699 // Into: tmp = VSPLTIS[BHW] elt
3700 // VADDU[BHW]M tmp, tmp
3701 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003702 SDValue EltVal = getI32Imm(Elt >> 1, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003703 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
3704 SDValue TmpVal = SDValue(Tmp, 0);
Justin Bognerdc8af062016-05-20 21:43:23 +00003705 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal));
3706 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003707 } else if (Elt > 0) {
3708 // Elt is odd and positive, in the range [17,31].
3709 //
3710 // Convert: VADD_SPLAT elt, size
3711 // Into: tmp1 = VSPLTIS[BHW] elt-16
3712 // tmp2 = VSPLTIS[BHW] -16
3713 // VSUBU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003714 SDValue EltVal = getI32Imm(Elt - 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003715 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003716 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003717 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003718 ReplaceNode(N, CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
3719 SDValue(Tmp2, 0)));
3720 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003721 } else {
3722 // Elt is odd and negative, in the range [-31,-17].
3723 //
3724 // Convert: VADD_SPLAT elt, size
3725 // Into: tmp1 = VSPLTIS[BHW] elt+16
3726 // tmp2 = VSPLTIS[BHW] -16
3727 // VADDU[BHW]M tmp1, tmp2
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003728 SDValue EltVal = getI32Imm(Elt + 16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003729 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003730 EltVal = getI32Imm(-16, dl);
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003731 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
Justin Bognerdc8af062016-05-20 21:43:23 +00003732 ReplaceNode(N, CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
3733 SDValue(Tmp2, 0)));
3734 return;
Bill Schmidtc6cbecc2013-02-20 20:41:42 +00003735 }
Bill Schmidt51e79512013-02-20 15:50:31 +00003736 }
Chris Lattner43ff01e2005-08-17 19:33:03 +00003737 }
Andrew Trickc416ba62010-12-24 04:28:06 +00003738
Justin Bognerdc8af062016-05-20 21:43:23 +00003739 SelectCode(N);
Chris Lattner43ff01e2005-08-17 19:33:03 +00003740}
3741
Hal Finkel4edc66b2015-01-03 01:16:37 +00003742// If the target supports the cmpb instruction, do the idiom recognition here.
3743// We don't do this as a DAG combine because we don't want to do it as nodes
3744// are being combined (because we might miss part of the eventual idiom). We
3745// don't want to do it during instruction selection because we want to reuse
3746// the logic for lowering the masking operations already part of the
3747// instruction selector.
3748SDValue PPCDAGToDAGISel::combineToCMPB(SDNode *N) {
3749 SDLoc dl(N);
3750
3751 assert(N->getOpcode() == ISD::OR &&
3752 "Only OR nodes are supported for CMPB");
3753
3754 SDValue Res;
3755 if (!PPCSubTarget->hasCMPB())
3756 return Res;
3757
3758 if (N->getValueType(0) != MVT::i32 &&
3759 N->getValueType(0) != MVT::i64)
3760 return Res;
3761
3762 EVT VT = N->getValueType(0);
3763
3764 SDValue RHS, LHS;
Eugene Zelenko8187c192017-01-13 00:58:58 +00003765 bool BytesFound[8] = {false, false, false, false, false, false, false, false};
Hal Finkel4edc66b2015-01-03 01:16:37 +00003766 uint64_t Mask = 0, Alt = 0;
3767
3768 auto IsByteSelectCC = [this](SDValue O, unsigned &b,
3769 uint64_t &Mask, uint64_t &Alt,
3770 SDValue &LHS, SDValue &RHS) {
3771 if (O.getOpcode() != ISD::SELECT_CC)
3772 return false;
3773 ISD::CondCode CC = cast<CondCodeSDNode>(O.getOperand(4))->get();
3774
3775 if (!isa<ConstantSDNode>(O.getOperand(2)) ||
3776 !isa<ConstantSDNode>(O.getOperand(3)))
3777 return false;
3778
3779 uint64_t PM = O.getConstantOperandVal(2);
3780 uint64_t PAlt = O.getConstantOperandVal(3);
3781 for (b = 0; b < 8; ++b) {
3782 uint64_t Mask = UINT64_C(0xFF) << (8*b);
3783 if (PM && (PM & Mask) == PM && (PAlt & Mask) == PAlt)
3784 break;
3785 }
3786
3787 if (b == 8)
3788 return false;
3789 Mask |= PM;
3790 Alt |= PAlt;
3791
3792 if (!isa<ConstantSDNode>(O.getOperand(1)) ||
3793 O.getConstantOperandVal(1) != 0) {
3794 SDValue Op0 = O.getOperand(0), Op1 = O.getOperand(1);
3795 if (Op0.getOpcode() == ISD::TRUNCATE)
3796 Op0 = Op0.getOperand(0);
3797 if (Op1.getOpcode() == ISD::TRUNCATE)
3798 Op1 = Op1.getOperand(0);
3799
3800 if (Op0.getOpcode() == ISD::SRL && Op1.getOpcode() == ISD::SRL &&
3801 Op0.getOperand(1) == Op1.getOperand(1) && CC == ISD::SETEQ &&
3802 isa<ConstantSDNode>(Op0.getOperand(1))) {
3803
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003804 unsigned Bits = Op0.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00003805 if (b != Bits/8-1)
3806 return false;
3807 if (Op0.getConstantOperandVal(1) != Bits-8)
3808 return false;
3809
3810 LHS = Op0.getOperand(0);
3811 RHS = Op1.getOperand(0);
3812 return true;
3813 }
3814
3815 // When we have small integers (i16 to be specific), the form present
3816 // post-legalization uses SETULT in the SELECT_CC for the
3817 // higher-order byte, depending on the fact that the
3818 // even-higher-order bytes are known to all be zero, for example:
3819 // select_cc (xor $lhs, $rhs), 256, 65280, 0, setult
3820 // (so when the second byte is the same, because all higher-order
3821 // bits from bytes 3 and 4 are known to be zero, the result of the
3822 // xor can be at most 255)
3823 if (Op0.getOpcode() == ISD::XOR && CC == ISD::SETULT &&
3824 isa<ConstantSDNode>(O.getOperand(1))) {
3825
3826 uint64_t ULim = O.getConstantOperandVal(1);
3827 if (ULim != (UINT64_C(1) << b*8))
3828 return false;
3829
3830 // Now we need to make sure that the upper bytes are known to be
3831 // zero.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003832 unsigned Bits = Op0.getValueSizeInBits();
3833 if (!CurDAG->MaskedValueIsZero(
3834 Op0, APInt::getHighBitsSet(Bits, Bits - (b + 1) * 8)))
Hal Finkel4edc66b2015-01-03 01:16:37 +00003835 return false;
Kyle Butt015f4fc2015-12-02 18:53:33 +00003836
Hal Finkel4edc66b2015-01-03 01:16:37 +00003837 LHS = Op0.getOperand(0);
3838 RHS = Op0.getOperand(1);
3839 return true;
3840 }
3841
3842 return false;
3843 }
3844
3845 if (CC != ISD::SETEQ)
3846 return false;
3847
3848 SDValue Op = O.getOperand(0);
3849 if (Op.getOpcode() == ISD::AND) {
3850 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3851 return false;
3852 if (Op.getConstantOperandVal(1) != (UINT64_C(0xFF) << (8*b)))
3853 return false;
3854
3855 SDValue XOR = Op.getOperand(0);
3856 if (XOR.getOpcode() == ISD::TRUNCATE)
3857 XOR = XOR.getOperand(0);
3858 if (XOR.getOpcode() != ISD::XOR)
3859 return false;
3860
3861 LHS = XOR.getOperand(0);
3862 RHS = XOR.getOperand(1);
3863 return true;
3864 } else if (Op.getOpcode() == ISD::SRL) {
3865 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3866 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00003867 unsigned Bits = Op.getValueSizeInBits();
Hal Finkel4edc66b2015-01-03 01:16:37 +00003868 if (b != Bits/8-1)
3869 return false;
3870 if (Op.getConstantOperandVal(1) != Bits-8)
3871 return false;
3872
3873 SDValue XOR = Op.getOperand(0);
3874 if (XOR.getOpcode() == ISD::TRUNCATE)
3875 XOR = XOR.getOperand(0);
3876 if (XOR.getOpcode() != ISD::XOR)
3877 return false;
3878
3879 LHS = XOR.getOperand(0);
3880 RHS = XOR.getOperand(1);
3881 return true;
3882 }
3883
3884 return false;
3885 };
3886
3887 SmallVector<SDValue, 8> Queue(1, SDValue(N, 0));
3888 while (!Queue.empty()) {
3889 SDValue V = Queue.pop_back_val();
3890
3891 for (const SDValue &O : V.getNode()->ops()) {
3892 unsigned b;
3893 uint64_t M = 0, A = 0;
3894 SDValue OLHS, ORHS;
3895 if (O.getOpcode() == ISD::OR) {
3896 Queue.push_back(O);
3897 } else if (IsByteSelectCC(O, b, M, A, OLHS, ORHS)) {
3898 if (!LHS) {
3899 LHS = OLHS;
3900 RHS = ORHS;
3901 BytesFound[b] = true;
3902 Mask |= M;
3903 Alt |= A;
3904 } else if ((LHS == ORHS && RHS == OLHS) ||
3905 (RHS == ORHS && LHS == OLHS)) {
3906 BytesFound[b] = true;
3907 Mask |= M;
3908 Alt |= A;
3909 } else {
3910 return Res;
3911 }
3912 } else {
3913 return Res;
3914 }
3915 }
3916 }
3917
3918 unsigned LastB = 0, BCnt = 0;
3919 for (unsigned i = 0; i < 8; ++i)
3920 if (BytesFound[LastB]) {
3921 ++BCnt;
3922 LastB = i;
3923 }
3924
3925 if (!LastB || BCnt < 2)
3926 return Res;
3927
3928 // Because we'll be zero-extending the output anyway if don't have a specific
3929 // value for each input byte (via the Mask), we can 'anyext' the inputs.
3930 if (LHS.getValueType() != VT) {
3931 LHS = CurDAG->getAnyExtOrTrunc(LHS, dl, VT);
3932 RHS = CurDAG->getAnyExtOrTrunc(RHS, dl, VT);
3933 }
3934
3935 Res = CurDAG->getNode(PPCISD::CMPB, dl, VT, LHS, RHS);
3936
3937 bool NonTrivialMask = ((int64_t) Mask) != INT64_C(-1);
3938 if (NonTrivialMask && !Alt) {
3939 // Res = Mask & CMPB
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003940 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
3941 CurDAG->getConstant(Mask, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003942 } else if (Alt) {
3943 // Res = (CMPB & Mask) | (~CMPB & Alt)
3944 // Which, as suggested here:
3945 // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge
3946 // can be written as:
3947 // Res = Alt ^ ((Alt ^ Mask) & CMPB)
3948 // useful because the (Alt ^ Mask) can be pre-computed.
3949 Res = CurDAG->getNode(ISD::AND, dl, VT, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003950 CurDAG->getConstant(Mask ^ Alt, dl, VT));
3951 Res = CurDAG->getNode(ISD::XOR, dl, VT, Res,
3952 CurDAG->getConstant(Alt, dl, VT));
Hal Finkel4edc66b2015-01-03 01:16:37 +00003953 }
3954
3955 return Res;
3956}
3957
Hal Finkel200d2ad2015-01-05 21:10:24 +00003958// When CR bit registers are enabled, an extension of an i1 variable to a i32
3959// or i64 value is lowered in terms of a SELECT_I[48] operation, and thus
3960// involves constant materialization of a 0 or a 1 or both. If the result of
3961// the extension is then operated upon by some operator that can be constant
3962// folded with a constant 0 or 1, and that constant can be materialized using
3963// only one instruction (like a zero or one), then we should fold in those
3964// operations with the select.
3965void PPCDAGToDAGISel::foldBoolExts(SDValue &Res, SDNode *&N) {
3966 if (!PPCSubTarget->useCRBits())
3967 return;
3968
3969 if (N->getOpcode() != ISD::ZERO_EXTEND &&
3970 N->getOpcode() != ISD::SIGN_EXTEND &&
3971 N->getOpcode() != ISD::ANY_EXTEND)
3972 return;
3973
3974 if (N->getOperand(0).getValueType() != MVT::i1)
3975 return;
3976
3977 if (!N->hasOneUse())
3978 return;
3979
3980 SDLoc dl(N);
3981 EVT VT = N->getValueType(0);
3982 SDValue Cond = N->getOperand(0);
3983 SDValue ConstTrue =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003984 CurDAG->getConstant(N->getOpcode() == ISD::SIGN_EXTEND ? -1 : 1, dl, VT);
3985 SDValue ConstFalse = CurDAG->getConstant(0, dl, VT);
Hal Finkel200d2ad2015-01-05 21:10:24 +00003986
3987 do {
3988 SDNode *User = *N->use_begin();
3989 if (User->getNumOperands() != 2)
3990 break;
3991
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003992 auto TryFold = [this, N, User, dl](SDValue Val) {
Hal Finkel200d2ad2015-01-05 21:10:24 +00003993 SDValue UserO0 = User->getOperand(0), UserO1 = User->getOperand(1);
3994 SDValue O0 = UserO0.getNode() == N ? Val : UserO0;
3995 SDValue O1 = UserO1.getNode() == N ? Val : UserO1;
3996
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003997 return CurDAG->FoldConstantArithmetic(User->getOpcode(), dl,
Hal Finkel200d2ad2015-01-05 21:10:24 +00003998 User->getValueType(0),
3999 O0.getNode(), O1.getNode());
4000 };
4001
4002 SDValue TrueRes = TryFold(ConstTrue);
4003 if (!TrueRes)
4004 break;
4005 SDValue FalseRes = TryFold(ConstFalse);
4006 if (!FalseRes)
4007 break;
4008
4009 // For us to materialize these using one instruction, we must be able to
4010 // represent them as signed 16-bit integers.
4011 uint64_t True = cast<ConstantSDNode>(TrueRes)->getZExtValue(),
4012 False = cast<ConstantSDNode>(FalseRes)->getZExtValue();
4013 if (!isInt<16>(True) || !isInt<16>(False))
4014 break;
4015
4016 // We can replace User with a new SELECT node, and try again to see if we
4017 // can fold the select with its user.
4018 Res = CurDAG->getSelect(dl, User->getValueType(0), Cond, TrueRes, FalseRes);
4019 N = User;
4020 ConstTrue = TrueRes;
4021 ConstFalse = FalseRes;
4022 } while (N->hasOneUse());
4023}
4024
Hal Finkel4edc66b2015-01-03 01:16:37 +00004025void PPCDAGToDAGISel::PreprocessISelDAG() {
4026 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4027 ++Position;
4028
4029 bool MadeChange = false;
4030 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004031 SDNode *N = &*--Position;
Hal Finkel4edc66b2015-01-03 01:16:37 +00004032 if (N->use_empty())
4033 continue;
4034
4035 SDValue Res;
4036 switch (N->getOpcode()) {
4037 default: break;
4038 case ISD::OR:
4039 Res = combineToCMPB(N);
4040 break;
4041 }
4042
Hal Finkel200d2ad2015-01-05 21:10:24 +00004043 if (!Res)
4044 foldBoolExts(Res, N);
4045
Hal Finkel4edc66b2015-01-03 01:16:37 +00004046 if (Res) {
4047 DEBUG(dbgs() << "PPC DAG preprocessing replacing:\nOld: ");
4048 DEBUG(N->dump(CurDAG));
4049 DEBUG(dbgs() << "\nNew: ");
4050 DEBUG(Res.getNode()->dump(CurDAG));
4051 DEBUG(dbgs() << "\n");
4052
4053 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
4054 MadeChange = true;
4055 }
4056 }
4057
4058 if (MadeChange)
4059 CurDAG->RemoveDeadNodes();
4060}
4061
Hal Finkel860fa902014-01-02 22:09:39 +00004062/// PostprocessISelDAG - Perform some late peephole optimizations
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004063/// on the DAG representation.
4064void PPCDAGToDAGISel::PostprocessISelDAG() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004065 // Skip peepholes at -O0.
4066 if (TM.getOptLevel() == CodeGenOpt::None)
4067 return;
4068
Hal Finkel940ab932014-02-28 00:27:01 +00004069 PeepholePPC64();
Eric Christopher02e18042014-05-14 00:31:15 +00004070 PeepholeCROps();
Hal Finkel4c6658f2014-12-12 23:59:36 +00004071 PeepholePPC64ZExt();
Hal Finkel940ab932014-02-28 00:27:01 +00004072}
4073
Hal Finkelb9989152014-02-28 06:11:16 +00004074// Check if all users of this node will become isel where the second operand
4075// is the constant zero. If this is so, and if we can negate the condition,
4076// then we can flip the true and false operands. This will allow the zero to
4077// be folded with the isel so that we don't need to materialize a register
4078// containing zero.
4079bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
Hal Finkelb9989152014-02-28 06:11:16 +00004080 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4081 UI != UE; ++UI) {
4082 SDNode *User = *UI;
4083 if (!User->isMachineOpcode())
4084 return false;
4085 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
4086 User->getMachineOpcode() != PPC::SELECT_I8)
4087 return false;
4088
4089 SDNode *Op2 = User->getOperand(2).getNode();
4090 if (!Op2->isMachineOpcode())
4091 return false;
4092
4093 if (Op2->getMachineOpcode() != PPC::LI &&
4094 Op2->getMachineOpcode() != PPC::LI8)
4095 return false;
4096
4097 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
4098 if (!C)
4099 return false;
4100
4101 if (!C->isNullValue())
4102 return false;
4103 }
4104
4105 return true;
4106}
4107
4108void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
4109 SmallVector<SDNode *, 4> ToReplace;
4110 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4111 UI != UE; ++UI) {
4112 SDNode *User = *UI;
4113 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
4114 User->getMachineOpcode() == PPC::SELECT_I8) &&
4115 "Must have all select users");
4116 ToReplace.push_back(User);
4117 }
4118
4119 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
4120 UE = ToReplace.end(); UI != UE; ++UI) {
4121 SDNode *User = *UI;
4122 SDNode *ResNode =
4123 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
4124 User->getValueType(0), User->getOperand(0),
4125 User->getOperand(2),
4126 User->getOperand(1));
4127
4128 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
4129 DEBUG(User->dump(CurDAG));
4130 DEBUG(dbgs() << "\nNew: ");
4131 DEBUG(ResNode->dump(CurDAG));
4132 DEBUG(dbgs() << "\n");
4133
4134 ReplaceUses(User, ResNode);
4135 }
4136}
4137
Eric Christopher02e18042014-05-14 00:31:15 +00004138void PPCDAGToDAGISel::PeepholeCROps() {
Hal Finkel940ab932014-02-28 00:27:01 +00004139 bool IsModified;
4140 do {
4141 IsModified = false;
Pete Cooper65c69402015-07-14 22:10:54 +00004142 for (SDNode &Node : CurDAG->allnodes()) {
4143 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node);
Hal Finkel940ab932014-02-28 00:27:01 +00004144 if (!MachineNode || MachineNode->use_empty())
4145 continue;
4146 SDNode *ResNode = MachineNode;
4147
4148 bool Op1Set = false, Op1Unset = false,
4149 Op1Not = false,
4150 Op2Set = false, Op2Unset = false,
4151 Op2Not = false;
4152
4153 unsigned Opcode = MachineNode->getMachineOpcode();
4154 switch (Opcode) {
4155 default: break;
4156 case PPC::CRAND:
4157 case PPC::CRNAND:
4158 case PPC::CROR:
4159 case PPC::CRXOR:
4160 case PPC::CRNOR:
4161 case PPC::CREQV:
4162 case PPC::CRANDC:
4163 case PPC::CRORC: {
4164 SDValue Op = MachineNode->getOperand(1);
4165 if (Op.isMachineOpcode()) {
4166 if (Op.getMachineOpcode() == PPC::CRSET)
4167 Op2Set = true;
4168 else if (Op.getMachineOpcode() == PPC::CRUNSET)
4169 Op2Unset = true;
4170 else if (Op.getMachineOpcode() == PPC::CRNOR &&
4171 Op.getOperand(0) == Op.getOperand(1))
4172 Op2Not = true;
4173 }
Justin Bognerb03fd122016-08-17 05:10:15 +00004174 LLVM_FALLTHROUGH;
4175 }
Hal Finkel940ab932014-02-28 00:27:01 +00004176 case PPC::BC:
4177 case PPC::BCn:
4178 case PPC::SELECT_I4:
4179 case PPC::SELECT_I8:
4180 case PPC::SELECT_F4:
4181 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00004182 case PPC::SELECT_QFRC:
4183 case PPC::SELECT_QSRC:
4184 case PPC::SELECT_QBRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004185 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00004186 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00004187 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004188 case PPC::SELECT_VSRC: {
Hal Finkel940ab932014-02-28 00:27:01 +00004189 SDValue Op = MachineNode->getOperand(0);
4190 if (Op.isMachineOpcode()) {
4191 if (Op.getMachineOpcode() == PPC::CRSET)
4192 Op1Set = true;
4193 else if (Op.getMachineOpcode() == PPC::CRUNSET)
4194 Op1Unset = true;
4195 else if (Op.getMachineOpcode() == PPC::CRNOR &&
4196 Op.getOperand(0) == Op.getOperand(1))
4197 Op1Not = true;
4198 }
4199 }
4200 break;
4201 }
4202
Hal Finkelb9989152014-02-28 06:11:16 +00004203 bool SelectSwap = false;
Hal Finkel940ab932014-02-28 00:27:01 +00004204 switch (Opcode) {
4205 default: break;
4206 case PPC::CRAND:
4207 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4208 // x & x = x
4209 ResNode = MachineNode->getOperand(0).getNode();
4210 else if (Op1Set)
4211 // 1 & y = y
4212 ResNode = MachineNode->getOperand(1).getNode();
4213 else if (Op2Set)
4214 // x & 1 = x
4215 ResNode = MachineNode->getOperand(0).getNode();
4216 else if (Op1Unset || Op2Unset)
4217 // x & 0 = 0 & y = 0
4218 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4219 MVT::i1);
4220 else if (Op1Not)
4221 // ~x & y = andc(y, x)
4222 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4223 MVT::i1, MachineNode->getOperand(1),
4224 MachineNode->getOperand(0).
4225 getOperand(0));
4226 else if (Op2Not)
4227 // x & ~y = andc(x, y)
4228 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4229 MVT::i1, MachineNode->getOperand(0),
4230 MachineNode->getOperand(1).
4231 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004232 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004233 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
4234 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004235 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004236 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004237 }
Hal Finkel940ab932014-02-28 00:27:01 +00004238 break;
4239 case PPC::CRNAND:
4240 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4241 // nand(x, x) -> nor(x, x)
4242 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4243 MVT::i1, MachineNode->getOperand(0),
4244 MachineNode->getOperand(0));
4245 else if (Op1Set)
4246 // nand(1, y) -> nor(y, y)
4247 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4248 MVT::i1, MachineNode->getOperand(1),
4249 MachineNode->getOperand(1));
4250 else if (Op2Set)
4251 // nand(x, 1) -> nor(x, x)
4252 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4253 MVT::i1, MachineNode->getOperand(0),
4254 MachineNode->getOperand(0));
4255 else if (Op1Unset || Op2Unset)
4256 // nand(x, 0) = nand(0, y) = 1
4257 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4258 MVT::i1);
4259 else if (Op1Not)
4260 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
4261 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4262 MVT::i1, MachineNode->getOperand(0).
4263 getOperand(0),
4264 MachineNode->getOperand(1));
4265 else if (Op2Not)
4266 // nand(x, ~y) = ~x | y = orc(y, x)
4267 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4268 MVT::i1, MachineNode->getOperand(1).
4269 getOperand(0),
4270 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004271 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004272 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
4273 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004274 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004275 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004276 }
Hal Finkel940ab932014-02-28 00:27:01 +00004277 break;
4278 case PPC::CROR:
4279 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4280 // x | x = x
4281 ResNode = MachineNode->getOperand(0).getNode();
4282 else if (Op1Set || Op2Set)
4283 // x | 1 = 1 | y = 1
4284 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4285 MVT::i1);
4286 else if (Op1Unset)
4287 // 0 | y = y
4288 ResNode = MachineNode->getOperand(1).getNode();
4289 else if (Op2Unset)
4290 // x | 0 = x
4291 ResNode = MachineNode->getOperand(0).getNode();
4292 else if (Op1Not)
4293 // ~x | y = orc(y, x)
4294 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4295 MVT::i1, MachineNode->getOperand(1),
4296 MachineNode->getOperand(0).
4297 getOperand(0));
4298 else if (Op2Not)
4299 // x | ~y = orc(x, y)
4300 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4301 MVT::i1, MachineNode->getOperand(0),
4302 MachineNode->getOperand(1).
4303 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004304 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004305 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4306 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004307 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004308 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004309 }
Hal Finkel940ab932014-02-28 00:27:01 +00004310 break;
4311 case PPC::CRXOR:
4312 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4313 // xor(x, x) = 0
4314 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4315 MVT::i1);
4316 else if (Op1Set)
4317 // xor(1, y) -> nor(y, y)
4318 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4319 MVT::i1, MachineNode->getOperand(1),
4320 MachineNode->getOperand(1));
4321 else if (Op2Set)
4322 // xor(x, 1) -> nor(x, x)
4323 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4324 MVT::i1, MachineNode->getOperand(0),
4325 MachineNode->getOperand(0));
4326 else if (Op1Unset)
4327 // xor(0, y) = y
4328 ResNode = MachineNode->getOperand(1).getNode();
4329 else if (Op2Unset)
4330 // xor(x, 0) = x
4331 ResNode = MachineNode->getOperand(0).getNode();
4332 else if (Op1Not)
4333 // xor(~x, y) = eqv(x, y)
4334 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4335 MVT::i1, MachineNode->getOperand(0).
4336 getOperand(0),
4337 MachineNode->getOperand(1));
4338 else if (Op2Not)
4339 // xor(x, ~y) = eqv(x, y)
4340 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4341 MVT::i1, MachineNode->getOperand(0),
4342 MachineNode->getOperand(1).
4343 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004344 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004345 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
4346 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004347 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004348 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004349 }
Hal Finkel940ab932014-02-28 00:27:01 +00004350 break;
4351 case PPC::CRNOR:
4352 if (Op1Set || Op2Set)
4353 // nor(1, y) -> 0
4354 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4355 MVT::i1);
4356 else if (Op1Unset)
4357 // nor(0, y) = ~y -> nor(y, y)
4358 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4359 MVT::i1, MachineNode->getOperand(1),
4360 MachineNode->getOperand(1));
4361 else if (Op2Unset)
4362 // nor(x, 0) = ~x
4363 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4364 MVT::i1, MachineNode->getOperand(0),
4365 MachineNode->getOperand(0));
4366 else if (Op1Not)
4367 // nor(~x, y) = andc(x, y)
4368 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4369 MVT::i1, MachineNode->getOperand(0).
4370 getOperand(0),
4371 MachineNode->getOperand(1));
4372 else if (Op2Not)
4373 // nor(x, ~y) = andc(y, x)
4374 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4375 MVT::i1, MachineNode->getOperand(1).
4376 getOperand(0),
4377 MachineNode->getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004378 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004379 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
4380 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004381 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004382 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004383 }
Hal Finkel940ab932014-02-28 00:27:01 +00004384 break;
4385 case PPC::CREQV:
4386 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4387 // eqv(x, x) = 1
4388 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4389 MVT::i1);
4390 else if (Op1Set)
4391 // eqv(1, y) = y
4392 ResNode = MachineNode->getOperand(1).getNode();
4393 else if (Op2Set)
4394 // eqv(x, 1) = x
4395 ResNode = MachineNode->getOperand(0).getNode();
4396 else if (Op1Unset)
4397 // eqv(0, y) = ~y -> nor(y, y)
4398 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4399 MVT::i1, MachineNode->getOperand(1),
4400 MachineNode->getOperand(1));
4401 else if (Op2Unset)
4402 // eqv(x, 0) = ~x
4403 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4404 MVT::i1, MachineNode->getOperand(0),
4405 MachineNode->getOperand(0));
4406 else if (Op1Not)
4407 // eqv(~x, y) = xor(x, y)
4408 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4409 MVT::i1, MachineNode->getOperand(0).
4410 getOperand(0),
4411 MachineNode->getOperand(1));
4412 else if (Op2Not)
4413 // eqv(x, ~y) = xor(x, y)
4414 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4415 MVT::i1, MachineNode->getOperand(0),
4416 MachineNode->getOperand(1).
4417 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004418 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004419 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
4420 MVT::i1, MachineNode->getOperand(0),
Richard Trieu7a083812016-02-18 22:09:30 +00004421 MachineNode->getOperand(1));
Hal Finkelb9989152014-02-28 06:11:16 +00004422 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004423 }
Hal Finkel940ab932014-02-28 00:27:01 +00004424 break;
4425 case PPC::CRANDC:
4426 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4427 // andc(x, x) = 0
4428 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4429 MVT::i1);
4430 else if (Op1Set)
4431 // andc(1, y) = ~y
4432 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4433 MVT::i1, MachineNode->getOperand(1),
4434 MachineNode->getOperand(1));
4435 else if (Op1Unset || Op2Set)
4436 // andc(0, y) = andc(x, 1) = 0
4437 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
4438 MVT::i1);
4439 else if (Op2Unset)
4440 // andc(x, 0) = x
4441 ResNode = MachineNode->getOperand(0).getNode();
4442 else if (Op1Not)
4443 // andc(~x, y) = ~(x | y) = nor(x, y)
4444 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4445 MVT::i1, MachineNode->getOperand(0).
4446 getOperand(0),
4447 MachineNode->getOperand(1));
4448 else if (Op2Not)
4449 // andc(x, ~y) = x & y
4450 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
4451 MVT::i1, MachineNode->getOperand(0),
4452 MachineNode->getOperand(1).
4453 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004454 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004455 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
4456 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00004457 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00004458 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004459 }
Hal Finkel940ab932014-02-28 00:27:01 +00004460 break;
4461 case PPC::CRORC:
4462 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
4463 // orc(x, x) = 1
4464 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4465 MVT::i1);
4466 else if (Op1Set || Op2Unset)
4467 // orc(1, y) = orc(x, 0) = 1
4468 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
4469 MVT::i1);
4470 else if (Op2Set)
4471 // orc(x, 1) = x
4472 ResNode = MachineNode->getOperand(0).getNode();
4473 else if (Op1Unset)
4474 // orc(0, y) = ~y
4475 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
4476 MVT::i1, MachineNode->getOperand(1),
4477 MachineNode->getOperand(1));
4478 else if (Op1Not)
4479 // orc(~x, y) = ~(x & y) = nand(x, y)
4480 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
4481 MVT::i1, MachineNode->getOperand(0).
4482 getOperand(0),
4483 MachineNode->getOperand(1));
4484 else if (Op2Not)
4485 // orc(x, ~y) = x | y
4486 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
4487 MVT::i1, MachineNode->getOperand(0),
4488 MachineNode->getOperand(1).
4489 getOperand(0));
Richard Trieu7a083812016-02-18 22:09:30 +00004490 else if (AllUsersSelectZero(MachineNode)) {
Hal Finkelb9989152014-02-28 06:11:16 +00004491 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
4492 MVT::i1, MachineNode->getOperand(1),
Richard Trieu7a083812016-02-18 22:09:30 +00004493 MachineNode->getOperand(0));
Hal Finkelb9989152014-02-28 06:11:16 +00004494 SelectSwap = true;
Richard Trieu7a083812016-02-18 22:09:30 +00004495 }
Hal Finkel940ab932014-02-28 00:27:01 +00004496 break;
4497 case PPC::SELECT_I4:
4498 case PPC::SELECT_I8:
4499 case PPC::SELECT_F4:
4500 case PPC::SELECT_F8:
Hal Finkelc93a9a22015-02-25 01:06:45 +00004501 case PPC::SELECT_QFRC:
4502 case PPC::SELECT_QSRC:
4503 case PPC::SELECT_QBRC:
Hal Finkel940ab932014-02-28 00:27:01 +00004504 case PPC::SELECT_VRRC:
Bill Schmidt9c54bbd2014-10-22 16:58:20 +00004505 case PPC::SELECT_VSFRC:
Nemanja Ivanovicf3c94b12015-05-07 18:24:05 +00004506 case PPC::SELECT_VSSRC:
Bill Schmidt61e65232014-10-22 13:13:40 +00004507 case PPC::SELECT_VSRC:
Hal Finkel940ab932014-02-28 00:27:01 +00004508 if (Op1Set)
4509 ResNode = MachineNode->getOperand(1).getNode();
4510 else if (Op1Unset)
4511 ResNode = MachineNode->getOperand(2).getNode();
4512 else if (Op1Not)
4513 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
4514 SDLoc(MachineNode),
4515 MachineNode->getValueType(0),
4516 MachineNode->getOperand(0).
4517 getOperand(0),
4518 MachineNode->getOperand(2),
4519 MachineNode->getOperand(1));
4520 break;
4521 case PPC::BC:
4522 case PPC::BCn:
4523 if (Op1Not)
4524 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
4525 PPC::BC,
4526 SDLoc(MachineNode),
4527 MVT::Other,
4528 MachineNode->getOperand(0).
4529 getOperand(0),
4530 MachineNode->getOperand(1),
4531 MachineNode->getOperand(2));
4532 // FIXME: Handle Op1Set, Op1Unset here too.
4533 break;
4534 }
4535
Hal Finkelb9989152014-02-28 06:11:16 +00004536 // If we're inverting this node because it is used only by selects that
4537 // we'd like to swap, then swap the selects before the node replacement.
4538 if (SelectSwap)
4539 SwapAllSelectUsers(MachineNode);
4540
Hal Finkel940ab932014-02-28 00:27:01 +00004541 if (ResNode != MachineNode) {
4542 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
4543 DEBUG(MachineNode->dump(CurDAG));
4544 DEBUG(dbgs() << "\nNew: ");
4545 DEBUG(ResNode->dump(CurDAG));
4546 DEBUG(dbgs() << "\n");
4547
4548 ReplaceUses(MachineNode, ResNode);
4549 IsModified = true;
4550 }
4551 }
4552 if (IsModified)
4553 CurDAG->RemoveDeadNodes();
4554 } while (IsModified);
4555}
4556
Hal Finkel4c6658f2014-12-12 23:59:36 +00004557// Gather the set of 32-bit operations that are known to have their
4558// higher-order 32 bits zero, where ToPromote contains all such operations.
4559static bool PeepholePPC64ZExtGather(SDValue Op32,
4560 SmallPtrSetImpl<SDNode *> &ToPromote) {
4561 if (!Op32.isMachineOpcode())
4562 return false;
4563
4564 // First, check for the "frontier" instructions (those that will clear the
4565 // higher-order 32 bits.
4566
4567 // For RLWINM and RLWNM, we need to make sure that the mask does not wrap
4568 // around. If it does not, then these instructions will clear the
4569 // higher-order bits.
4570 if ((Op32.getMachineOpcode() == PPC::RLWINM ||
4571 Op32.getMachineOpcode() == PPC::RLWNM) &&
4572 Op32.getConstantOperandVal(2) <= Op32.getConstantOperandVal(3)) {
4573 ToPromote.insert(Op32.getNode());
4574 return true;
4575 }
4576
4577 // SLW and SRW always clear the higher-order bits.
4578 if (Op32.getMachineOpcode() == PPC::SLW ||
4579 Op32.getMachineOpcode() == PPC::SRW) {
4580 ToPromote.insert(Op32.getNode());
4581 return true;
4582 }
4583
4584 // For LI and LIS, we need the immediate to be positive (so that it is not
4585 // sign extended).
4586 if (Op32.getMachineOpcode() == PPC::LI ||
4587 Op32.getMachineOpcode() == PPC::LIS) {
4588 if (!isUInt<15>(Op32.getConstantOperandVal(0)))
4589 return false;
4590
4591 ToPromote.insert(Op32.getNode());
4592 return true;
4593 }
4594
Hal Finkel4e2c7822015-01-05 18:09:06 +00004595 // LHBRX and LWBRX always clear the higher-order bits.
4596 if (Op32.getMachineOpcode() == PPC::LHBRX ||
4597 Op32.getMachineOpcode() == PPC::LWBRX) {
4598 ToPromote.insert(Op32.getNode());
4599 return true;
4600 }
4601
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00004602 // CNT[LT]ZW always produce a 64-bit value in [0,32], and so is zero extended.
4603 if (Op32.getMachineOpcode() == PPC::CNTLZW ||
4604 Op32.getMachineOpcode() == PPC::CNTTZW) {
Hal Finkel49557f12015-01-05 18:52:29 +00004605 ToPromote.insert(Op32.getNode());
4606 return true;
4607 }
4608
Hal Finkel4c6658f2014-12-12 23:59:36 +00004609 // Next, check for those instructions we can look through.
4610
4611 // Assuming the mask does not wrap around, then the higher-order bits are
4612 // taken directly from the first operand.
4613 if (Op32.getMachineOpcode() == PPC::RLWIMI &&
4614 Op32.getConstantOperandVal(3) <= Op32.getConstantOperandVal(4)) {
4615 SmallPtrSet<SDNode *, 16> ToPromote1;
4616 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4617 return false;
4618
4619 ToPromote.insert(Op32.getNode());
4620 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4621 return true;
4622 }
4623
4624 // For OR, the higher-order bits are zero if that is true for both operands.
4625 // For SELECT_I4, the same is true (but the relevant operand numbers are
4626 // shifted by 1).
4627 if (Op32.getMachineOpcode() == PPC::OR ||
4628 Op32.getMachineOpcode() == PPC::SELECT_I4) {
4629 unsigned B = Op32.getMachineOpcode() == PPC::SELECT_I4 ? 1 : 0;
4630 SmallPtrSet<SDNode *, 16> ToPromote1;
4631 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+0), ToPromote1))
4632 return false;
4633 if (!PeepholePPC64ZExtGather(Op32.getOperand(B+1), ToPromote1))
4634 return false;
4635
4636 ToPromote.insert(Op32.getNode());
4637 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4638 return true;
4639 }
4640
4641 // For ORI and ORIS, we need the higher-order bits of the first operand to be
4642 // zero, and also for the constant to be positive (so that it is not sign
4643 // extended).
4644 if (Op32.getMachineOpcode() == PPC::ORI ||
4645 Op32.getMachineOpcode() == PPC::ORIS) {
4646 SmallPtrSet<SDNode *, 16> ToPromote1;
4647 if (!PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1))
4648 return false;
4649 if (!isUInt<15>(Op32.getConstantOperandVal(1)))
4650 return false;
4651
4652 ToPromote.insert(Op32.getNode());
4653 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4654 return true;
4655 }
4656
4657 // The higher-order bits of AND are zero if that is true for at least one of
4658 // the operands.
4659 if (Op32.getMachineOpcode() == PPC::AND) {
4660 SmallPtrSet<SDNode *, 16> ToPromote1, ToPromote2;
4661 bool Op0OK =
4662 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4663 bool Op1OK =
4664 PeepholePPC64ZExtGather(Op32.getOperand(1), ToPromote2);
4665 if (!Op0OK && !Op1OK)
4666 return false;
4667
4668 ToPromote.insert(Op32.getNode());
4669
4670 if (Op0OK)
4671 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4672
4673 if (Op1OK)
4674 ToPromote.insert(ToPromote2.begin(), ToPromote2.end());
4675
4676 return true;
4677 }
4678
4679 // For ANDI and ANDIS, the higher-order bits are zero if either that is true
4680 // of the first operand, or if the second operand is positive (so that it is
4681 // not sign extended).
4682 if (Op32.getMachineOpcode() == PPC::ANDIo ||
4683 Op32.getMachineOpcode() == PPC::ANDISo) {
4684 SmallPtrSet<SDNode *, 16> ToPromote1;
4685 bool Op0OK =
4686 PeepholePPC64ZExtGather(Op32.getOperand(0), ToPromote1);
4687 bool Op1OK = isUInt<15>(Op32.getConstantOperandVal(1));
4688 if (!Op0OK && !Op1OK)
4689 return false;
4690
4691 ToPromote.insert(Op32.getNode());
4692
4693 if (Op0OK)
4694 ToPromote.insert(ToPromote1.begin(), ToPromote1.end());
4695
4696 return true;
4697 }
4698
4699 return false;
4700}
4701
4702void PPCDAGToDAGISel::PeepholePPC64ZExt() {
4703 if (!PPCSubTarget->isPPC64())
4704 return;
4705
4706 // When we zero-extend from i32 to i64, we use a pattern like this:
4707 // def : Pat<(i64 (zext i32:$in)),
4708 // (RLDICL (INSERT_SUBREG (i64 (IMPLICIT_DEF)), $in, sub_32),
4709 // 0, 32)>;
4710 // There are several 32-bit shift/rotate instructions, however, that will
4711 // clear the higher-order bits of their output, rendering the RLDICL
4712 // unnecessary. When that happens, we remove it here, and redefine the
4713 // relevant 32-bit operation to be a 64-bit operation.
4714
4715 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4716 ++Position;
4717
4718 bool MadeChange = false;
4719 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004720 SDNode *N = &*--Position;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004721 // Skip dead nodes and any non-machine opcodes.
4722 if (N->use_empty() || !N->isMachineOpcode())
4723 continue;
4724
4725 if (N->getMachineOpcode() != PPC::RLDICL)
4726 continue;
4727
4728 if (N->getConstantOperandVal(1) != 0 ||
4729 N->getConstantOperandVal(2) != 32)
4730 continue;
4731
4732 SDValue ISR = N->getOperand(0);
4733 if (!ISR.isMachineOpcode() ||
4734 ISR.getMachineOpcode() != TargetOpcode::INSERT_SUBREG)
4735 continue;
4736
4737 if (!ISR.hasOneUse())
4738 continue;
4739
4740 if (ISR.getConstantOperandVal(2) != PPC::sub_32)
4741 continue;
4742
4743 SDValue IDef = ISR.getOperand(0);
4744 if (!IDef.isMachineOpcode() ||
4745 IDef.getMachineOpcode() != TargetOpcode::IMPLICIT_DEF)
4746 continue;
4747
4748 // We now know that we're looking at a canonical i32 -> i64 zext. See if we
4749 // can get rid of it.
4750
4751 SDValue Op32 = ISR->getOperand(1);
4752 if (!Op32.isMachineOpcode())
4753 continue;
4754
4755 // There are some 32-bit instructions that always clear the high-order 32
4756 // bits, there are also some instructions (like AND) that we can look
4757 // through.
4758 SmallPtrSet<SDNode *, 16> ToPromote;
4759 if (!PeepholePPC64ZExtGather(Op32, ToPromote))
4760 continue;
4761
4762 // If the ToPromote set contains nodes that have uses outside of the set
4763 // (except for the original INSERT_SUBREG), then abort the transformation.
4764 bool OutsideUse = false;
4765 for (SDNode *PN : ToPromote) {
4766 for (SDNode *UN : PN->uses()) {
4767 if (!ToPromote.count(UN) && UN != ISR.getNode()) {
4768 OutsideUse = true;
4769 break;
4770 }
4771 }
4772
4773 if (OutsideUse)
4774 break;
4775 }
4776 if (OutsideUse)
4777 continue;
4778
4779 MadeChange = true;
4780
4781 // We now know that this zero extension can be removed by promoting to
4782 // nodes in ToPromote to 64-bit operations, where for operations in the
4783 // frontier of the set, we need to insert INSERT_SUBREGs for their
4784 // operands.
4785 for (SDNode *PN : ToPromote) {
4786 unsigned NewOpcode;
4787 switch (PN->getMachineOpcode()) {
4788 default:
4789 llvm_unreachable("Don't know the 64-bit variant of this instruction");
4790 case PPC::RLWINM: NewOpcode = PPC::RLWINM8; break;
4791 case PPC::RLWNM: NewOpcode = PPC::RLWNM8; break;
4792 case PPC::SLW: NewOpcode = PPC::SLW8; break;
4793 case PPC::SRW: NewOpcode = PPC::SRW8; break;
4794 case PPC::LI: NewOpcode = PPC::LI8; break;
4795 case PPC::LIS: NewOpcode = PPC::LIS8; break;
Hal Finkel4e2c7822015-01-05 18:09:06 +00004796 case PPC::LHBRX: NewOpcode = PPC::LHBRX8; break;
4797 case PPC::LWBRX: NewOpcode = PPC::LWBRX8; break;
Hal Finkel49557f12015-01-05 18:52:29 +00004798 case PPC::CNTLZW: NewOpcode = PPC::CNTLZW8; break;
Nemanja Ivanovic32b5fed2016-10-27 05:17:58 +00004799 case PPC::CNTTZW: NewOpcode = PPC::CNTTZW8; break;
Hal Finkel4c6658f2014-12-12 23:59:36 +00004800 case PPC::RLWIMI: NewOpcode = PPC::RLWIMI8; break;
4801 case PPC::OR: NewOpcode = PPC::OR8; break;
4802 case PPC::SELECT_I4: NewOpcode = PPC::SELECT_I8; break;
4803 case PPC::ORI: NewOpcode = PPC::ORI8; break;
4804 case PPC::ORIS: NewOpcode = PPC::ORIS8; break;
4805 case PPC::AND: NewOpcode = PPC::AND8; break;
4806 case PPC::ANDIo: NewOpcode = PPC::ANDIo8; break;
4807 case PPC::ANDISo: NewOpcode = PPC::ANDISo8; break;
4808 }
4809
4810 // Note: During the replacement process, the nodes will be in an
4811 // inconsistent state (some instructions will have operands with values
4812 // of the wrong type). Once done, however, everything should be right
4813 // again.
4814
4815 SmallVector<SDValue, 4> Ops;
4816 for (const SDValue &V : PN->ops()) {
4817 if (!ToPromote.count(V.getNode()) && V.getValueType() == MVT::i32 &&
4818 !isa<ConstantSDNode>(V)) {
4819 SDValue ReplOpOps[] = { ISR.getOperand(0), V, ISR.getOperand(2) };
4820 SDNode *ReplOp =
4821 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, SDLoc(V),
4822 ISR.getNode()->getVTList(), ReplOpOps);
4823 Ops.push_back(SDValue(ReplOp, 0));
4824 } else {
4825 Ops.push_back(V);
4826 }
4827 }
4828
4829 // Because all to-be-promoted nodes only have users that are other
4830 // promoted nodes (or the original INSERT_SUBREG), we can safely replace
4831 // the i32 result value type with i64.
4832
4833 SmallVector<EVT, 2> NewVTs;
4834 SDVTList VTs = PN->getVTList();
4835 for (unsigned i = 0, ie = VTs.NumVTs; i != ie; ++i)
4836 if (VTs.VTs[i] == MVT::i32)
4837 NewVTs.push_back(MVT::i64);
4838 else
4839 NewVTs.push_back(VTs.VTs[i]);
4840
4841 DEBUG(dbgs() << "PPC64 ZExt Peephole morphing:\nOld: ");
4842 DEBUG(PN->dump(CurDAG));
4843
4844 CurDAG->SelectNodeTo(PN, NewOpcode, CurDAG->getVTList(NewVTs), Ops);
4845
4846 DEBUG(dbgs() << "\nNew: ");
4847 DEBUG(PN->dump(CurDAG));
4848 DEBUG(dbgs() << "\n");
4849 }
4850
4851 // Now we replace the original zero extend and its associated INSERT_SUBREG
4852 // with the value feeding the INSERT_SUBREG (which has now been promoted to
4853 // return an i64).
4854
4855 DEBUG(dbgs() << "PPC64 ZExt Peephole replacing:\nOld: ");
4856 DEBUG(N->dump(CurDAG));
4857 DEBUG(dbgs() << "\nNew: ");
4858 DEBUG(Op32.getNode()->dump(CurDAG));
4859 DEBUG(dbgs() << "\n");
4860
4861 ReplaceUses(N, Op32.getNode());
4862 }
4863
4864 if (MadeChange)
4865 CurDAG->RemoveDeadNodes();
4866}
4867
Hal Finkel940ab932014-02-28 00:27:01 +00004868void PPCDAGToDAGISel::PeepholePPC64() {
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004869 // These optimizations are currently supported only for 64-bit SVR4.
Eric Christopher1b8e7632014-05-22 01:07:24 +00004870 if (PPCSubTarget->isDarwin() || !PPCSubTarget->isPPC64())
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004871 return;
4872
4873 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
4874 ++Position;
4875
4876 while (Position != CurDAG->allnodes_begin()) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +00004877 SDNode *N = &*--Position;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004878 // Skip dead nodes and any non-machine opcodes.
4879 if (N->use_empty() || !N->isMachineOpcode())
4880 continue;
4881
4882 unsigned FirstOp;
4883 unsigned StorageOpcode = N->getMachineOpcode();
4884
4885 switch (StorageOpcode) {
4886 default: continue;
4887
4888 case PPC::LBZ:
4889 case PPC::LBZ8:
4890 case PPC::LD:
4891 case PPC::LFD:
4892 case PPC::LFS:
4893 case PPC::LHA:
4894 case PPC::LHA8:
4895 case PPC::LHZ:
4896 case PPC::LHZ8:
4897 case PPC::LWA:
4898 case PPC::LWZ:
4899 case PPC::LWZ8:
4900 FirstOp = 0;
4901 break;
4902
4903 case PPC::STB:
4904 case PPC::STB8:
4905 case PPC::STD:
4906 case PPC::STFD:
4907 case PPC::STFS:
4908 case PPC::STH:
4909 case PPC::STH8:
4910 case PPC::STW:
4911 case PPC::STW8:
4912 FirstOp = 1;
4913 break;
4914 }
4915
Kyle Butt1452b762015-12-11 00:47:36 +00004916 // If this is a load or store with a zero offset, or within the alignment,
4917 // we may be able to fold an add-immediate into the memory operation.
4918 // The check against alignment is below, as it can't occur until we check
4919 // the arguments to N
4920 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)))
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004921 continue;
4922
4923 SDValue Base = N->getOperand(FirstOp + 1);
4924 if (!Base.isMachineOpcode())
4925 continue;
4926
4927 unsigned Flags = 0;
4928 bool ReplaceFlags = true;
4929
4930 // When the feeding operation is an add-immediate of some sort,
4931 // determine whether we need to add relocation information to the
4932 // target flags on the immediate operand when we fold it into the
4933 // load instruction.
4934 //
4935 // For something like ADDItocL, the relocation information is
4936 // inferred from the opcode; when we process it in the AsmPrinter,
4937 // we add the necessary relocation there. A load, though, can receive
4938 // relocation from various flavors of ADDIxxx, so we need to carry
4939 // the relocation information in the target flags.
4940 switch (Base.getMachineOpcode()) {
4941 default: continue;
4942
4943 case PPC::ADDI8:
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +00004944 case PPC::ADDI:
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004945 // In some cases (such as TLS) the relocation information
4946 // is already in place on the operand, so copying the operand
4947 // is sufficient.
4948 ReplaceFlags = false;
4949 // For these cases, the immediate may not be divisible by 4, in
4950 // which case the fold is illegal for DS-form instructions. (The
4951 // other cases provide aligned addresses and are always safe.)
4952 if ((StorageOpcode == PPC::LWA ||
4953 StorageOpcode == PPC::LD ||
4954 StorageOpcode == PPC::STD) &&
4955 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
4956 Base.getConstantOperandVal(1) % 4 != 0))
4957 continue;
4958 break;
4959 case PPC::ADDIdtprelL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004960 Flags = PPCII::MO_DTPREL_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004961 break;
4962 case PPC::ADDItlsldL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004963 Flags = PPCII::MO_TLSLD_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004964 break;
4965 case PPC::ADDItocL:
Ulrich Weigandd51c09f2013-06-21 14:42:20 +00004966 Flags = PPCII::MO_TOC_LO;
Bill Schmidtf5b474c2013-02-21 00:38:25 +00004967 break;
4968 }
4969
Kyle Butt1452b762015-12-11 00:47:36 +00004970 SDValue ImmOpnd = Base.getOperand(1);
Hal Finkelb54579f2016-09-02 00:28:20 +00004971
4972 // On PPC64, the TOC base pointer is guaranteed by the ABI only to have
4973 // 8-byte alignment, and so we can only use offsets less than 8 (otherwise,
4974 // we might have needed different @ha relocation values for the offset
4975 // pointers).
4976 int MaxDisplacement = 7;
Kyle Butt1452b762015-12-11 00:47:36 +00004977 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
4978 const GlobalValue *GV = GA->getGlobal();
Hal Finkelb54579f2016-09-02 00:28:20 +00004979 MaxDisplacement = std::min((int) GV->getAlignment() - 1, MaxDisplacement);
Kyle Butt1452b762015-12-11 00:47:36 +00004980 }
4981
Hal Finkel7b104d42016-09-02 21:37:07 +00004982 bool UpdateHBase = false;
4983 SDValue HBase = Base.getOperand(0);
4984
Kyle Butt1452b762015-12-11 00:47:36 +00004985 int Offset = N->getConstantOperandVal(FirstOp);
Hal Finkel42c83f12016-09-07 07:36:11 +00004986 if (ReplaceFlags) {
4987 if (Offset < 0 || Offset > MaxDisplacement) {
4988 // If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
4989 // one use, then we can do this for any offset, we just need to also
4990 // update the offset (i.e. the symbol addend) on the addis also.
4991 if (Base.getMachineOpcode() != PPC::ADDItocL)
4992 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00004993
Hal Finkel42c83f12016-09-07 07:36:11 +00004994 if (!HBase.isMachineOpcode() ||
4995 HBase.getMachineOpcode() != PPC::ADDIStocHA)
4996 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00004997
Hal Finkel42c83f12016-09-07 07:36:11 +00004998 if (!Base.hasOneUse() || !HBase.hasOneUse())
4999 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005000
Hal Finkel42c83f12016-09-07 07:36:11 +00005001 SDValue HImmOpnd = HBase.getOperand(1);
5002 if (HImmOpnd != ImmOpnd)
5003 continue;
Hal Finkel7b104d42016-09-02 21:37:07 +00005004
Hal Finkel42c83f12016-09-07 07:36:11 +00005005 UpdateHBase = true;
5006 }
5007 } else {
5008 // If we're directly folding the addend from an addi instruction, then:
5009 // 1. In general, the offset on the memory access must be zero.
5010 // 2. If the addend is a constant, then it can be combined with a
5011 // non-zero offset, but only if the result meets the encoding
5012 // requirements.
5013 if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) {
5014 Offset += C->getSExtValue();
5015
5016 if ((StorageOpcode == PPC::LWA || StorageOpcode == PPC::LD ||
5017 StorageOpcode == PPC::STD) && (Offset % 4) != 0)
5018 continue;
5019
5020 if (!isInt<16>(Offset))
5021 continue;
5022
5023 ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd),
5024 ImmOpnd.getValueType());
5025 } else if (Offset != 0) {
5026 continue;
5027 }
Hal Finkel7b104d42016-09-02 21:37:07 +00005028 }
Kyle Butt1452b762015-12-11 00:47:36 +00005029
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005030 // We found an opportunity. Reverse the operands from the add
5031 // immediate and substitute them into the load or store. If
5032 // needed, update the target flags for the immediate operand to
5033 // reflect the necessary relocation information.
5034 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
5035 DEBUG(Base->dump(CurDAG));
5036 DEBUG(dbgs() << "\nN: ");
5037 DEBUG(N->dump(CurDAG));
5038 DEBUG(dbgs() << "\n");
5039
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005040 // If the relocation information isn't already present on the
5041 // immediate operand, add it now.
5042 if (ReplaceFlags) {
Bill Schmidt49498da2013-02-21 14:35:42 +00005043 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005044 SDLoc dl(GA);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005045 const GlobalValue *GV = GA->getGlobal();
Bill Schmidt48fc20a2013-07-01 20:52:27 +00005046 // We can't perform this optimization for data whose alignment
5047 // is insufficient for the instruction encoding.
5048 if (GV->getAlignment() < 4 &&
5049 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
Kyle Butt1452b762015-12-11 00:47:36 +00005050 StorageOpcode == PPC::LWA || (Offset % 4) != 0)) {
Bill Schmidt48fc20a2013-07-01 20:52:27 +00005051 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
5052 continue;
5053 }
Kyle Butt1452b762015-12-11 00:47:36 +00005054 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, Offset, Flags);
Bill Schmidt836c45b2013-02-21 17:26:05 +00005055 } else if (ConstantPoolSDNode *CP =
5056 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
Bill Schmidt49498da2013-02-21 14:35:42 +00005057 const Constant *C = CP->getConstVal();
5058 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
5059 CP->getAlignment(),
Kyle Butt1452b762015-12-11 00:47:36 +00005060 Offset, Flags);
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005061 }
5062 }
5063
5064 if (FirstOp == 1) // Store
5065 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
5066 Base.getOperand(0), N->getOperand(3));
5067 else // Load
5068 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
5069 N->getOperand(2));
5070
Hal Finkel7b104d42016-09-02 21:37:07 +00005071 if (UpdateHBase)
5072 (void)CurDAG->UpdateNodeOperands(HBase.getNode(), HBase.getOperand(0),
5073 ImmOpnd);
5074
Bill Schmidtf5b474c2013-02-21 00:38:25 +00005075 // The add-immediate may now be dead, in which case remove it.
5076 if (Base.getNode()->use_empty())
5077 CurDAG->RemoveDeadNode(Base.getNode());
5078 }
5079}
Chris Lattner43ff01e2005-08-17 19:33:03 +00005080
Andrew Trickc416ba62010-12-24 04:28:06 +00005081/// createPPCISelDag - This pass converts a legalized DAG into a
Chris Lattner43ff01e2005-08-17 19:33:03 +00005082/// PowerPC-specific DAG, ready for instruction scheduling.
5083///
Evan Cheng2dd2c652006-03-13 23:20:37 +00005084FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
Nate Begeman0b71e002005-10-18 00:28:58 +00005085 return new PPCDAGToDAGISel(TM);
Chris Lattner43ff01e2005-08-17 19:33:03 +00005086}