blob: ab29eb12d23e71aee379a9cf23def38bb6f16638 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an instruction selector for the SystemZ target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SystemZTargetMachine.h"
Richard Sandiford97846492013-07-09 09:46:39 +000014#include "llvm/Analysis/AliasAnalysis.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000015#include "llvm/CodeGen/SelectionDAGISel.h"
16#include "llvm/Support/Debug.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000017#include "llvm/Support/KnownBits.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000018#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
Chandler Carruthe96dd892014-04-21 22:55:11 +000022#define DEBUG_TYPE "systemz-isel"
23
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024namespace {
25// Used to build addressing modes.
26struct SystemZAddressingMode {
27 // The shape of the address.
28 enum AddrForm {
29 // base+displacement
30 FormBD,
31
32 // base+displacement+index for load and store operands
33 FormBDXNormal,
34
35 // base+displacement+index for load address operands
36 FormBDXLA,
37
38 // base+displacement+index+ADJDYNALLOC
39 FormBDXDynAlloc
40 };
41 AddrForm Form;
42
43 // The type of displacement. The enum names here correspond directly
44 // to the definitions in SystemZOperand.td. We could split them into
45 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
46 enum DispRange {
47 Disp12Only,
48 Disp12Pair,
49 Disp20Only,
50 Disp20Only128,
51 Disp20Pair
52 };
53 DispRange DR;
54
55 // The parts of the address. The address is equivalent to:
56 //
57 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
58 SDValue Base;
59 int64_t Disp;
60 SDValue Index;
61 bool IncludesDynAlloc;
62
63 SystemZAddressingMode(AddrForm form, DispRange dr)
64 : Form(form), DR(dr), Base(), Disp(0), Index(),
65 IncludesDynAlloc(false) {}
66
67 // True if the address can have an index register.
68 bool hasIndexField() { return Form != FormBD; }
69
70 // True if the address can (and must) include ADJDYNALLOC.
71 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
72
Jonas Paulssone2c5cbc2018-10-26 00:02:33 +000073 void dump(const llvm::SelectionDAG *DAG) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000074 errs() << "SystemZAddressingMode " << this << '\n';
75
76 errs() << " Base ";
Craig Topper062a2ba2014-04-25 05:30:21 +000077 if (Base.getNode())
Jonas Paulssone2c5cbc2018-10-26 00:02:33 +000078 Base.getNode()->dump(DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000079 else
80 errs() << "null\n";
81
82 if (hasIndexField()) {
83 errs() << " Index ";
Craig Topper062a2ba2014-04-25 05:30:21 +000084 if (Index.getNode())
Jonas Paulssone2c5cbc2018-10-26 00:02:33 +000085 Index.getNode()->dump(DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 else
87 errs() << "null\n";
88 }
89
90 errs() << " Disp " << Disp;
91 if (IncludesDynAlloc)
92 errs() << " + ADJDYNALLOC";
93 errs() << '\n';
94 }
95};
96
Richard Sandiford82ec87d2013-07-16 11:02:24 +000097// Return a mask with Count low bits set.
98static uint64_t allOnes(unsigned int Count) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +000099 assert(Count <= 64);
Justin Bognerc97c48a2015-06-24 05:59:19 +0000100 if (Count > 63)
101 return UINT64_MAX;
102 return (uint64_t(1) << Count) - 1;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000103}
104
Richard Sandiford51093212013-07-18 10:40:35 +0000105// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
106// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
107// Rotate (I5). The combined operand value is effectively:
108//
109// (or (rotl Input, Rotate), ~Mask)
110//
111// for RNSBG and:
112//
113// (and (rotl Input, Rotate), Mask)
114//
Richard Sandiford3e382972013-10-16 13:35:13 +0000115// otherwise. The output value has BitSize bits, although Input may be
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000116// narrower (in which case the upper bits are don't care), or wider (in which
117// case the result will be truncated as part of the operation).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000118struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000119 RxSBGOperands(unsigned Op, SDValue N)
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000120 : Opcode(Op), BitSize(N.getValueSizeInBits()),
Richard Sandiford51093212013-07-18 10:40:35 +0000121 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
122 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000123
Richard Sandiford51093212013-07-18 10:40:35 +0000124 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000125 unsigned BitSize;
126 uint64_t Mask;
127 SDValue Input;
128 unsigned Start;
129 unsigned End;
130 unsigned Rotate;
131};
132
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000134 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000135
136 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000137 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000138 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000139 }
140
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000141 const SystemZTargetMachine &getTargetMachine() const {
142 return static_cast<const SystemZTargetMachine &>(TM);
143 }
144
145 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000146 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000147 }
148
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000149 // Try to fold more of the base or index of AM into AM, where IsBase
150 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000151 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000152
153 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000154 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155
156 // Extract individual target operands from matched address AM.
157 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000158 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000159 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000160 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161
162 // Try to match Addr as a FormBD address with displacement type DR.
163 // Return true on success, storing the base and displacement in
164 // Base and Disp respectively.
165 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000166 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000167
Richard Sandiforda481f582013-08-23 11:18:53 +0000168 // Try to match Addr as a FormBDX address with displacement type DR.
169 // Return true on success and if the result had no index. Store the
170 // base and displacement in Base and Disp respectively.
171 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000172 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000173
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174 // Try to match Addr as a FormBDX* address of form Form with
175 // displacement type DR. Return true on success, storing the base,
176 // displacement and index in Base, Disp and Index respectively.
177 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
178 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000179 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180
181 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000182 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
183 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000184 Target = Addr.getOperand(0);
185 return true;
186 }
187 return false;
188 }
189
190 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000191 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
193 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000194 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
196 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000197 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000198 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
199 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000200 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000201 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
202 }
203
Richard Sandiforda481f582013-08-23 11:18:53 +0000204 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000205 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000206 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
207 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000208 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000209 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
210 }
211
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000212 // BDX matching routines used by SystemZOperands.td.
213 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000214 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
216 SystemZAddressingMode::Disp12Only,
217 Addr, Base, Disp, Index);
218 }
219 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000220 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
222 SystemZAddressingMode::Disp12Pair,
223 Addr, Base, Disp, Index);
224 }
225 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000226 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
228 SystemZAddressingMode::Disp12Only,
229 Addr, Base, Disp, Index);
230 }
231 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000232 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000233 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
234 SystemZAddressingMode::Disp20Only,
235 Addr, Base, Disp, Index);
236 }
237 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000238 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
240 SystemZAddressingMode::Disp20Only128,
241 Addr, Base, Disp, Index);
242 }
243 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000244 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
246 SystemZAddressingMode::Disp20Pair,
247 Addr, Base, Disp, Index);
248 }
249 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000250 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000251 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
252 SystemZAddressingMode::Disp12Pair,
253 Addr, Base, Disp, Index);
254 }
255 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000256 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000257 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
258 SystemZAddressingMode::Disp20Pair,
259 Addr, Base, Disp, Index);
260 }
261
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000262 // Try to match Addr as an address with a base, 12-bit displacement
263 // and index, where the index is element Elem of a vector.
264 // Return true on success, storing the base, displacement and vector
265 // in Base, Disp and Index respectively.
266 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
267 SDValue &Disp, SDValue &Index) const;
268
Richard Sandiford885140c2013-07-16 11:55:57 +0000269 // Check whether (or Op (and X InsertMask)) is effectively an insertion
270 // of X into bits InsertMask of some Y != Op. Return true if so and
271 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000272 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000273
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000274 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
275 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000276 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000277
Richard Sandiford5cbac962013-07-18 09:45:08 +0000278 // Try to fold some of RxSBG.Input into other fields of RxSBG.
279 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000280 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000281
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000282 // Return an undefined value of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000283 SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000284
285 // Convert N to VT, if it isn't already.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000286 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000287
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000288 // Try to implement AND or shift node N using RISBG with the zero flag set.
289 // Return the selected node on success, otherwise return null.
Justin Bognerbbcd2232016-05-10 21:11:26 +0000290 bool tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000291
Richard Sandiford7878b852013-07-18 10:06:15 +0000292 // Try to use RISBG or Opcode to implement OR or XOR node N.
293 // Return the selected node on success, otherwise return null.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000294 bool tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000295
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000296 // If Op0 is null, then Node is a constant that can be loaded using:
297 //
298 // (Opcode UpperVal LowerVal)
299 //
300 // If Op0 is nonnull, then Node can be implemented using:
301 //
302 // (Opcode (Opcode Op0 UpperVal) LowerVal)
Justin Bognerffb273d2016-05-09 23:54:23 +0000303 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
304 uint64_t UpperVal, uint64_t LowerVal);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000305
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000306 // Try to use gather instruction Opcode to implement vector insertion N.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000307 bool tryGather(SDNode *N, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000308
309 // Try to use scatter instruction Opcode to implement store Store.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000310 bool tryScatter(StoreSDNode *Store, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000311
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +0000312 // Change a chain of {load; op; store} of the same value into a simple op
313 // through memory of that value, if the uses of the modified value and its
314 // address are suitable.
315 bool tryFoldLoadStoreIntoMemOperand(SDNode *Node);
316
Richard Sandiford067817e2013-09-27 15:29:20 +0000317 // Return true if Load and Store are loads and stores of the same size
318 // and are guaranteed not to overlap. Such operations can be implemented
319 // using block (SS-format) instructions.
320 //
321 // Partial overlap would lead to incorrect code, since the block operations
322 // are logically bytewise, even though they have a fast path for the
323 // non-overlapping case. We also need to avoid full overlap (i.e. two
324 // addresses that might be equal at run time) because although that case
325 // would be handled correctly, it might be implemented by millicode.
326 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
327
Richard Sandiford178273a2013-09-05 10:36:45 +0000328 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
329 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000330 bool storeLoadCanUseMVC(SDNode *N) const;
331
Richard Sandiford178273a2013-09-05 10:36:45 +0000332 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
333 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
334 // to X.
335 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
336
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000337 // Try to expand a boolean SELECT_CCMASK using an IPM sequence.
338 SDValue expandSelectBoolean(SDNode *Node);
339
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000340public:
341 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000342 : SelectionDAGISel(TM, OptLevel) {}
343
344 bool runOnMachineFunction(MachineFunction &MF) override {
345 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
346 return SelectionDAGISel::runOnMachineFunction(MF);
347 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000348
349 // Override MachineFunctionPass.
Mehdi Amini117296c2016-10-01 02:56:57 +0000350 StringRef getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 return "SystemZ DAG->DAG Pattern Instruction Selection";
352 }
353
354 // Override SelectionDAGISel.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000355 void Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000356 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000357 std::vector<SDValue> &OutOps) override;
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000358 bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000359 void PreprocessISelDAG() override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000360
361 // Include the pieces autogenerated from the target description.
362 #include "SystemZGenDAGISel.inc"
363};
364} // end anonymous namespace
365
366FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
367 CodeGenOpt::Level OptLevel) {
368 return new SystemZDAGToDAGISel(TM, OptLevel);
369}
370
371// Return true if Val should be selected as a displacement for an address
372// with range DR. Here we're interested in the range of both the instruction
373// described by DR and of any pairing instruction.
374static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
375 switch (DR) {
376 case SystemZAddressingMode::Disp12Only:
377 return isUInt<12>(Val);
378
379 case SystemZAddressingMode::Disp12Pair:
380 case SystemZAddressingMode::Disp20Only:
381 case SystemZAddressingMode::Disp20Pair:
382 return isInt<20>(Val);
383
384 case SystemZAddressingMode::Disp20Only128:
385 return isInt<20>(Val) && isInt<20>(Val + 8);
386 }
387 llvm_unreachable("Unhandled displacement range");
388}
389
390// Change the base or index in AM to Value, where IsBase selects
391// between the base and index.
392static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
393 SDValue Value) {
394 if (IsBase)
395 AM.Base = Value;
396 else
397 AM.Index = Value;
398}
399
400// The base or index of AM is equivalent to Value + ADJDYNALLOC,
401// where IsBase selects between the base and index. Try to fold the
402// ADJDYNALLOC into AM.
403static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
404 SDValue Value) {
405 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
406 changeComponent(AM, IsBase, Value);
407 AM.IncludesDynAlloc = true;
408 return true;
409 }
410 return false;
411}
412
413// The base of AM is equivalent to Base + Index. Try to use Index as
414// the index register.
415static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
416 SDValue Index) {
417 if (AM.hasIndexField() && !AM.Index.getNode()) {
418 AM.Base = Base;
419 AM.Index = Index;
420 return true;
421 }
422 return false;
423}
424
425// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
426// between the base and index. Try to fold Op1 into AM's displacement.
427static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000428 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000429 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000430 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431 if (selectDisp(AM.DR, TestDisp)) {
432 changeComponent(AM, IsBase, Op0);
433 AM.Disp = TestDisp;
434 return true;
435 }
436
437 // We could consider forcing the displacement into a register and
438 // using it as an index, but it would need to be carefully tuned.
439 return false;
440}
441
442bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000443 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000444 SDValue N = IsBase ? AM.Base : AM.Index;
445 unsigned Opcode = N.getOpcode();
446 if (Opcode == ISD::TRUNCATE) {
447 N = N.getOperand(0);
448 Opcode = N.getOpcode();
449 }
450 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
451 SDValue Op0 = N.getOperand(0);
452 SDValue Op1 = N.getOperand(1);
453
454 unsigned Op0Code = Op0->getOpcode();
455 unsigned Op1Code = Op1->getOpcode();
456
457 if (Op0Code == SystemZISD::ADJDYNALLOC)
458 return expandAdjDynAlloc(AM, IsBase, Op1);
459 if (Op1Code == SystemZISD::ADJDYNALLOC)
460 return expandAdjDynAlloc(AM, IsBase, Op0);
461
462 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000463 return expandDisp(AM, IsBase, Op1,
464 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000465 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000466 return expandDisp(AM, IsBase, Op0,
467 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000468
469 if (IsBase && expandIndex(AM, Op0, Op1))
470 return true;
471 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000472 if (Opcode == SystemZISD::PCREL_OFFSET) {
473 SDValue Full = N.getOperand(0);
474 SDValue Base = N.getOperand(1);
475 SDValue Anchor = Base.getOperand(0);
476 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
477 cast<GlobalAddressSDNode>(Anchor)->getOffset());
478 return expandDisp(AM, IsBase, Base, Offset);
479 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000480 return false;
481}
482
483// Return true if an instruction with displacement range DR should be
484// used for displacement value Val. selectDisp(DR, Val) must already hold.
485static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
486 assert(selectDisp(DR, Val) && "Invalid displacement");
487 switch (DR) {
488 case SystemZAddressingMode::Disp12Only:
489 case SystemZAddressingMode::Disp20Only:
490 case SystemZAddressingMode::Disp20Only128:
491 return true;
492
493 case SystemZAddressingMode::Disp12Pair:
494 // Use the other instruction if the displacement is too large.
495 return isUInt<12>(Val);
496
497 case SystemZAddressingMode::Disp20Pair:
498 // Use the other instruction if the displacement is small enough.
499 return !isUInt<12>(Val);
500 }
501 llvm_unreachable("Unhandled displacement range");
502}
503
504// Return true if Base + Disp + Index should be performed by LA(Y).
505static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
506 // Don't use LA(Y) for constants.
507 if (!Base)
508 return false;
509
510 // Always use LA(Y) for frame addresses, since we know that the destination
511 // register is almost always (perhaps always) going to be different from
512 // the frame register.
513 if (Base->getOpcode() == ISD::FrameIndex)
514 return true;
515
516 if (Disp) {
517 // Always use LA(Y) if there is a base, displacement and index.
518 if (Index)
519 return true;
520
521 // Always use LA if the displacement is small enough. It should always
522 // be no worse than AGHI (and better if it avoids a move).
523 if (isUInt<12>(Disp))
524 return true;
525
526 // For similar reasons, always use LAY if the constant is too big for AGHI.
527 // LAY should be no worse than AGFI.
528 if (!isInt<16>(Disp))
529 return true;
530 } else {
531 // Don't use LA for plain registers.
532 if (!Index)
533 return false;
534
535 // Don't use LA for plain addition if the index operand is only used
536 // once. It should be a natural two-operand addition in that case.
537 if (Index->hasOneUse())
538 return false;
539
540 // Prefer addition if the second operation is sign-extended, in the
541 // hope of using AGF.
542 unsigned IndexOpcode = Index->getOpcode();
543 if (IndexOpcode == ISD::SIGN_EXTEND ||
544 IndexOpcode == ISD::SIGN_EXTEND_INREG)
545 return false;
546 }
547
548 // Don't use LA for two-operand addition if either operand is only
549 // used once. The addition instructions are better in that case.
550 if (Base->hasOneUse())
551 return false;
552
553 return true;
554}
555
556// Return true if Addr is suitable for AM, updating AM if so.
557bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000558 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000559 // Start out assuming that the address will need to be loaded separately,
560 // then try to extend it as much as we can.
561 AM.Base = Addr;
562
563 // First try treating the address as a constant.
564 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000565 expandDisp(AM, true, SDValue(),
566 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000567 ;
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000568 // Also see if it's a bare ADJDYNALLOC.
569 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
570 expandAdjDynAlloc(AM, true, SDValue()))
571 ;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000572 else
573 // Otherwise try expanding each component.
574 while (expandAddress(AM, true) ||
575 (AM.Index.getNode() && expandAddress(AM, false)))
576 continue;
577
578 // Reject cases where it isn't profitable to use LA(Y).
579 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
580 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
581 return false;
582
583 // Reject cases where the other instruction in a pair should be used.
584 if (!isValidDisp(AM.DR, AM.Disp))
585 return false;
586
587 // Make sure that ADJDYNALLOC is included where necessary.
588 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
589 return false;
590
Jonas Paulssone2c5cbc2018-10-26 00:02:33 +0000591 LLVM_DEBUG(AM.dump(CurDAG));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000592 return true;
593}
594
595// Insert a node into the DAG at least before Pos. This will reposition
596// the node as needed, and will assign it a node ID that is <= Pos's ID.
597// Note that this does *not* preserve the uniqueness of node IDs!
598// The selection DAG must no longer depend on their uniqueness when this
599// function is used.
600static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
Nirav Dave8c5f47a2018-03-22 19:32:07 +0000601 if (N->getNodeId() == -1 ||
602 (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) >
603 SelectionDAGISel::getUninvalidatedNodeId(Pos))) {
Duncan P. N. Exon Smitha2c90e42015-10-20 01:12:46 +0000604 DAG->RepositionNode(Pos->getIterator(), N.getNode());
Nirav Dave3264c1b2018-03-19 20:19:46 +0000605 // Mark Node as invalid for pruning as after this it may be a successor to a
606 // selected node but otherwise be in the same position of Pos.
607 // Conservatively mark it with the same -abs(Id) to assure node id
608 // invariant is preserved.
Nirav Dave8c5f47a2018-03-22 19:32:07 +0000609 N->setNodeId(Pos->getNodeId());
610 SelectionDAGISel::InvalidateNodeId(N.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000611 }
612}
613
614void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
615 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000616 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000617 Base = AM.Base;
618 if (!Base.getNode())
619 // Register 0 means "no base". This is mostly useful for shifts.
620 Base = CurDAG->getRegister(0, VT);
621 else if (Base.getOpcode() == ISD::FrameIndex) {
622 // Lower a FrameIndex to a TargetFrameIndex.
623 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
624 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
625 } else if (Base.getValueType() != VT) {
626 // Truncate values from i64 to i32, for shifts.
627 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
628 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000629 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000630 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
631 insertDAGNode(CurDAG, Base.getNode(), Trunc);
632 Base = Trunc;
633 }
634
635 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000636 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637}
638
639void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
640 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000641 SDValue &Disp,
642 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000643 getAddressOperands(AM, VT, Base, Disp);
644
645 Index = AM.Index;
646 if (!Index.getNode())
647 // Register 0 means "no index".
648 Index = CurDAG->getRegister(0, VT);
649}
650
651bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
652 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000653 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000654 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
655 if (!selectAddress(Addr, AM))
656 return false;
657
658 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
659 return true;
660}
661
Richard Sandiforda481f582013-08-23 11:18:53 +0000662bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
663 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000664 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000665 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
666 if (!selectAddress(Addr, AM) || AM.Index.getNode())
667 return false;
668
669 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
670 return true;
671}
672
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000673bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
674 SystemZAddressingMode::DispRange DR,
675 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000676 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000677 SystemZAddressingMode AM(Form, DR);
678 if (!selectAddress(Addr, AM))
679 return false;
680
681 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
682 return true;
683}
684
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000685bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
686 SDValue &Base,
687 SDValue &Disp,
688 SDValue &Index) const {
689 SDValue Regs[2];
690 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
691 Regs[0].getNode() && Regs[1].getNode()) {
692 for (unsigned int I = 0; I < 2; ++I) {
693 Base = Regs[I];
694 Index = Regs[1 - I];
695 // We can't tell here whether the index vector has the right type
696 // for the access; the caller needs to do that instead.
697 if (Index.getOpcode() == ISD::ZERO_EXTEND)
698 Index = Index.getOperand(0);
699 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
700 Index.getOperand(1) == Elem) {
701 Index = Index.getOperand(0);
702 return true;
703 }
704 }
705 }
706 return false;
707}
708
Richard Sandiford885140c2013-07-16 11:55:57 +0000709bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000710 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000711 // We're only interested in cases where the insertion is into some operand
712 // of Op, rather than into Op itself. The only useful case is an AND.
713 if (Op.getOpcode() != ISD::AND)
714 return false;
715
716 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000717 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000718 if (!MaskNode)
719 return false;
720
721 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
722 uint64_t AndMask = MaskNode->getZExtValue();
723 if (InsertMask & AndMask)
724 return false;
725
726 // It's only an insertion if all bits are covered or are known to be zero.
727 // The inner check covers all cases but is more expensive.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000728 uint64_t Used = allOnes(Op.getValueSizeInBits());
Richard Sandiford885140c2013-07-16 11:55:57 +0000729 if (Used != (AndMask | InsertMask)) {
Simon Pilgrim2482c512018-12-21 14:50:54 +0000730 KnownBits Known = CurDAG->computeKnownBits(Op.getOperand(0));
Craig Topperd0af7e82017-04-28 05:31:46 +0000731 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000732 return false;
733 }
734
735 Op = Op.getOperand(0);
736 return true;
737}
738
Richard Sandiford54b36912013-09-27 15:14:04 +0000739bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
740 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000741 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000742 if (RxSBG.Rotate != 0)
743 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
744 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000745 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000746 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000747 return true;
748 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000749 return false;
750}
751
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000752// Return true if any bits of (RxSBG.Input & Mask) are significant.
753static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
754 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000755 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000756 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
757 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000758}
759
Richard Sandiford54b36912013-09-27 15:14:04 +0000760bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000761 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000762 unsigned Opcode = N.getOpcode();
763 switch (Opcode) {
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000764 case ISD::TRUNCATE: {
765 if (RxSBG.Opcode == SystemZ::RNSBG)
766 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000767 uint64_t BitSize = N.getValueSizeInBits();
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000768 uint64_t Mask = allOnes(BitSize);
769 if (!refineRxSBGMask(RxSBG, Mask))
770 return false;
771 RxSBG.Input = N.getOperand(0);
772 return true;
773 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000774 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000775 if (RxSBG.Opcode == SystemZ::RNSBG)
776 return false;
777
Richard Sandiford21f5d682014-03-06 11:22:58 +0000778 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000779 if (!MaskNode)
780 return false;
781
782 SDValue Input = N.getOperand(0);
783 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000784 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000785 // If some bits of Input are already known zeros, those bits will have
786 // been removed from the mask. See if adding them back in makes the
787 // mask suitable.
Simon Pilgrim2482c512018-12-21 14:50:54 +0000788 KnownBits Known = CurDAG->computeKnownBits(Input);
Craig Topperd0af7e82017-04-28 05:31:46 +0000789 Mask |= Known.Zero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000790 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000791 return false;
792 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000793 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000794 return true;
795 }
796
Richard Sandiford51093212013-07-18 10:40:35 +0000797 case ISD::OR: {
798 if (RxSBG.Opcode != SystemZ::RNSBG)
799 return false;
800
Richard Sandiford21f5d682014-03-06 11:22:58 +0000801 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000802 if (!MaskNode)
803 return false;
804
805 SDValue Input = N.getOperand(0);
806 uint64_t Mask = ~MaskNode->getZExtValue();
807 if (!refineRxSBGMask(RxSBG, Mask)) {
808 // If some bits of Input are already known ones, those bits will have
809 // been removed from the mask. See if adding them back in makes the
810 // mask suitable.
Simon Pilgrim2482c512018-12-21 14:50:54 +0000811 KnownBits Known = CurDAG->computeKnownBits(Input);
Craig Topperd0af7e82017-04-28 05:31:46 +0000812 Mask &= ~Known.One.getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000813 if (!refineRxSBGMask(RxSBG, Mask))
814 return false;
815 }
816 RxSBG.Input = Input;
817 return true;
818 }
819
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000820 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000821 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000822 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000823 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000824 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000825 if (!CountNode)
826 return false;
827
Richard Sandiford5cbac962013-07-18 09:45:08 +0000828 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
829 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000830 return true;
831 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000832
Richard Sandiford220ee492013-12-20 11:49:48 +0000833 case ISD::ANY_EXTEND:
834 // Bits above the extended operand are don't-care.
835 RxSBG.Input = N.getOperand(0);
836 return true;
837
Richard Sandiford3875cb62014-01-09 11:28:53 +0000838 case ISD::ZERO_EXTEND:
839 if (RxSBG.Opcode != SystemZ::RNSBG) {
840 // Restrict the mask to the extended operand.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000841 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiford3875cb62014-01-09 11:28:53 +0000842 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
843 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000844
Richard Sandiford3875cb62014-01-09 11:28:53 +0000845 RxSBG.Input = N.getOperand(0);
846 return true;
847 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000848 LLVM_FALLTHROUGH;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000849
Richard Sandiford220ee492013-12-20 11:49:48 +0000850 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000851 // Check that the extension bits are don't-care (i.e. are masked out
852 // by the final mask).
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000853 unsigned BitSize = N.getValueSizeInBits();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000854 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000855 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
856 // In the case where only the sign bit is active, increase Rotate with
857 // the extension width.
858 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
859 RxSBG.Rotate += (BitSize - InnerBitSize);
860 else
861 return false;
862 }
Richard Sandiford3e382972013-10-16 13:35:13 +0000863
864 RxSBG.Input = N.getOperand(0);
865 return true;
866 }
867
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000868 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000869 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000870 if (!CountNode)
871 return false;
872
873 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000874 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000875 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000876 return false;
877
Richard Sandiford51093212013-07-18 10:40:35 +0000878 if (RxSBG.Opcode == SystemZ::RNSBG) {
879 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
880 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000881 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000882 return false;
883 } else {
884 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000885 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000886 return false;
887 }
888
Richard Sandiford5cbac962013-07-18 09:45:08 +0000889 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
890 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000891 return true;
892 }
893
Richard Sandiford297f7d22013-07-18 10:14:55 +0000894 case ISD::SRL:
895 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000896 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000897 if (!CountNode)
898 return false;
899
900 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000901 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000902 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000903 return false;
904
Richard Sandiford51093212013-07-18 10:40:35 +0000905 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
906 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
907 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000908 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000909 return false;
910 } else {
911 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
912 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000913 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000914 return false;
915 }
916
Richard Sandiford5cbac962013-07-18 09:45:08 +0000917 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
918 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000919 return true;
920 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000921 default:
922 return false;
923 }
924}
925
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000926SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000927 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000928 return SDValue(N, 0);
929}
930
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000931SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
932 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000933 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000934 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000935 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000936 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000937 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000938 assert(N.getValueType() == VT && "Unexpected value types");
939 return N;
940}
941
Justin Bognerbbcd2232016-05-10 21:11:26 +0000942bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000943 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000944 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000945 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000946 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000947 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000948 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000949 while (expandRxSBG(RISBG))
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000950 // The widening or narrowing is expected to be free.
951 // Counting widening or narrowing as a saved operation will result in
952 // preferring an R*SBG over a simple shift/logical instruction.
953 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
954 RISBG.Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +0000955 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000956 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000957 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000958
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000959 // Prefer to use normal shift instructions over RISBG, since they can handle
960 // all cases and are sometimes shorter.
961 if (Count == 1 && N->getOpcode() != ISD::AND)
962 return false;
963
964 // Prefer register extensions like LLC over RISBG. Also prefer to start
965 // out with normal ANDs if one instruction would be enough. We can convert
966 // these ANDs into an RISBG later if a three-address instruction is useful.
967 if (RISBG.Rotate == 0) {
968 bool PreferAnd = false;
969 // Prefer AND for any 32-bit and-immediate operation.
970 if (VT == MVT::i32)
971 PreferAnd = true;
972 // As well as for any 64-bit operation that can be implemented via LLC(R),
973 // LLH(R), LLGT(R), or one of the and-immediate instructions.
974 else if (RISBG.Mask == 0xff ||
975 RISBG.Mask == 0xffff ||
976 RISBG.Mask == 0x7fffffff ||
977 SystemZ::isImmLF(~RISBG.Mask) ||
978 SystemZ::isImmHF(~RISBG.Mask))
979 PreferAnd = true;
Ulrich Weigand92c2c672016-11-11 12:46:28 +0000980 // And likewise for the LLZRGF instruction, which doesn't have a register
981 // to register version.
982 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
983 if (Load->getMemoryVT() == MVT::i32 &&
984 (Load->getExtensionType() == ISD::EXTLOAD ||
985 Load->getExtensionType() == ISD::ZEXTLOAD) &&
986 RISBG.Mask == 0xffffff00 &&
987 Subtarget->hasLoadAndZeroRightmostByte())
988 PreferAnd = true;
989 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000990 if (PreferAnd) {
991 // Replace the current node with an AND. Note that the current node
992 // might already be that same AND, in which case it is already CSE'd
993 // with it, and we must not call ReplaceNode.
994 SDValue In = convertTo(DL, VT, RISBG.Input);
995 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
996 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
997 if (N != New.getNode()) {
998 insertDAGNode(CurDAG, N, Mask);
999 insertDAGNode(CurDAG, N, New);
1000 ReplaceNode(N, New.getNode());
1001 N = New.getNode();
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001002 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001003 // Now, select the machine opcode to implement this operation.
Jonas Paulssonf268cd02018-02-27 07:53:23 +00001004 if (!N->isMachineOpcode())
1005 SelectCode(N);
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001006 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001007 }
Simon Pilgrim0750c842015-08-15 13:27:30 +00001008 }
1009
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001010 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001011 // Prefer RISBGN if available, since it does not clobber CC.
1012 if (Subtarget->hasMiscellaneousExtensions())
1013 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001014 EVT OpcodeVT = MVT::i64;
Ulrich Weigand55b85902017-11-14 19:20:46 +00001015 if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1016 // We can only use the 32-bit instructions if all source bits are
1017 // in the low 32 bits without wrapping, both after rotation (because
1018 // of the smaller range for Start and End) and before rotation
1019 // (because the input value is truncated).
1020 RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1021 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1022 ((RISBG.End + RISBG.Rotate) & 63) >=
1023 ((RISBG.Start + RISBG.Rotate) & 63)) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001024 Opcode = SystemZ::RISBMux;
1025 OpcodeVT = MVT::i32;
1026 RISBG.Start &= 31;
1027 RISBG.End &= 31;
1028 }
Richard Sandiford84f54a32013-07-11 08:59:12 +00001029 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001030 getUNDEF(DL, OpcodeVT),
1031 convertTo(DL, OpcodeVT, RISBG.Input),
1032 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1033 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1034 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +00001035 };
Justin Bognerbbcd2232016-05-10 21:11:26 +00001036 SDValue New = convertTo(
1037 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
Nirav Dave3264c1b2018-03-19 20:19:46 +00001038 ReplaceNode(N, New.getNode());
Justin Bognerbbcd2232016-05-10 21:11:26 +00001039 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001040}
1041
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001042bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +00001043 SDLoc DL(N);
1044 EVT VT = N->getValueType(0);
1045 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001046 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +00001047 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +00001048 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001049 RxSBGOperands RxSBG[] = {
1050 RxSBGOperands(Opcode, N->getOperand(0)),
1051 RxSBGOperands(Opcode, N->getOperand(1))
1052 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001053 unsigned Count[] = { 0, 0 };
1054 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001055 while (expandRxSBG(RxSBG[I]))
Zhan Jun Liau0df35052016-06-22 16:16:27 +00001056 // The widening or narrowing is expected to be free.
1057 // Counting widening or narrowing as a saved operation will result in
1058 // preferring an R*SBG over a simple shift/logical instruction.
1059 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1060 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +00001061 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001062
1063 // Do nothing if neither operand is suitable.
1064 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001065 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001066
1067 // Pick the deepest second operand.
1068 unsigned I = Count[0] > Count[1] ? 0 : 1;
1069 SDValue Op0 = N->getOperand(I ^ 1);
1070
1071 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001072 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001073 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001074 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001075 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001076
1077 // See whether we can avoid an AND in the first operand by converting
1078 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001079 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001080 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001081 // Prefer RISBGN if available, since it does not clobber CC.
1082 if (Subtarget->hasMiscellaneousExtensions())
1083 Opcode = SystemZ::RISBGN;
1084 }
1085
Richard Sandiford885140c2013-07-16 11:55:57 +00001086 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001087 convertTo(DL, MVT::i64, Op0),
1088 convertTo(DL, MVT::i64, RxSBG[I].Input),
1089 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1090 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1091 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001092 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001093 SDValue New = convertTo(
1094 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1095 ReplaceNode(N, New.getNode());
1096 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001097}
1098
Justin Bognerffb273d2016-05-09 23:54:23 +00001099void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1100 SDValue Op0, uint64_t UpperVal,
1101 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001102 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001103 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001104 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001105 if (Op0.getNode())
1106 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001107
1108 {
1109 // When we haven't passed in Op0, Upper will be a constant. In order to
1110 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1111 // SelectCode first and end up with an opaque machine node. This means that
1112 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1113 // SelectCode.
1114 //
1115 // Note that in the case where Op0 is passed in we could just call
1116 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1117 // the handle at all, but it's fine to do it here.
1118 //
1119 // TODO: This is a pretty hacky way to do this. Can we do something that
1120 // doesn't require a two paragraph explanation?
1121 HandleSDNode Handle(Upper);
1122 SelectCode(Upper.getNode());
1123 Upper = Handle.getValue();
1124 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001126 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001127 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001128
Nirav Dave3264c1b2018-03-19 20:19:46 +00001129 ReplaceNode(Node, Or.getNode());
Justin Bognerffb273d2016-05-09 23:54:23 +00001130
1131 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001132}
1133
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001134bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001135 SDValue ElemV = N->getOperand(2);
1136 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1137 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001138 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001139
1140 unsigned Elem = ElemN->getZExtValue();
1141 EVT VT = N->getValueType(0);
1142 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001143 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001144
1145 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
Ulrich Weigand8bb46b02018-12-20 13:01:20 +00001146 if (!Load || !Load->hasNUsesOfValue(1, 0))
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001147 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001148 if (Load->getMemoryVT().getSizeInBits() !=
1149 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001150 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001151
1152 SDValue Base, Disp, Index;
1153 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1154 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001155 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001156
1157 SDLoc DL(Load);
1158 SDValue Ops[] = {
1159 N->getOperand(0), Base, Disp, Index,
1160 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1161 };
1162 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1163 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001164 ReplaceNode(N, Res);
1165 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001166}
1167
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001168bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001169 SDValue Value = Store->getValue();
1170 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001171 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001172 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001173 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001174
1175 SDValue ElemV = Value.getOperand(1);
1176 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1177 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001178 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001179
1180 SDValue Vec = Value.getOperand(0);
1181 EVT VT = Vec.getValueType();
1182 unsigned Elem = ElemN->getZExtValue();
1183 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001184 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001185
1186 SDValue Base, Disp, Index;
1187 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1188 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001189 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001190
1191 SDLoc DL(Store);
1192 SDValue Ops[] = {
1193 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1194 Store->getChain()
1195 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001196 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1197 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001198}
1199
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001200// Check whether or not the chain ending in StoreNode is suitable for doing
1201// the {load; op; store} to modify transformation.
1202static bool isFusableLoadOpStorePattern(StoreSDNode *StoreNode,
1203 SDValue StoredVal, SelectionDAG *CurDAG,
1204 LoadSDNode *&LoadNode,
1205 SDValue &InputChain) {
1206 // Is the stored value result 0 of the operation?
1207 if (StoredVal.getResNo() != 0)
1208 return false;
1209
1210 // Are there other uses of the loaded value than the operation?
1211 if (!StoredVal.getNode()->hasNUsesOfValue(1, 0))
1212 return false;
1213
1214 // Is the store non-extending and non-indexed?
1215 if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
1216 return false;
1217
1218 SDValue Load = StoredVal->getOperand(0);
1219 // Is the stored value a non-extending and non-indexed load?
1220 if (!ISD::isNormalLoad(Load.getNode()))
1221 return false;
1222
1223 // Return LoadNode by reference.
1224 LoadNode = cast<LoadSDNode>(Load);
1225
1226 // Is store the only read of the loaded value?
1227 if (!Load.hasOneUse())
1228 return false;
1229
1230 // Is the address of the store the same as the load?
1231 if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
1232 LoadNode->getOffset() != StoreNode->getOffset())
1233 return false;
1234
1235 // Check if the chain is produced by the load or is a TokenFactor with
1236 // the load output chain as an operand. Return InputChain by reference.
1237 SDValue Chain = StoreNode->getChain();
1238
1239 bool ChainCheck = false;
1240 if (Chain == Load.getValue(1)) {
1241 ChainCheck = true;
1242 InputChain = LoadNode->getChain();
1243 } else if (Chain.getOpcode() == ISD::TokenFactor) {
1244 SmallVector<SDValue, 4> ChainOps;
1245 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
1246 SDValue Op = Chain.getOperand(i);
1247 if (Op == Load.getValue(1)) {
1248 ChainCheck = true;
1249 // Drop Load, but keep its chain. No cycle check necessary.
1250 ChainOps.push_back(Load.getOperand(0));
1251 continue;
1252 }
1253
1254 // Make sure using Op as part of the chain would not cause a cycle here.
1255 // In theory, we could check whether the chain node is a predecessor of
1256 // the load. But that can be very expensive. Instead visit the uses and
1257 // make sure they all have smaller node id than the load.
1258 int LoadId = LoadNode->getNodeId();
1259 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
1260 UE = UI->use_end(); UI != UE; ++UI) {
1261 if (UI.getUse().getResNo() != 0)
1262 continue;
1263 if (UI->getNodeId() > LoadId)
1264 return false;
1265 }
1266
1267 ChainOps.push_back(Op);
1268 }
1269
1270 if (ChainCheck)
1271 // Make a new TokenFactor with all the other input chains except
1272 // for the load.
1273 InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
1274 MVT::Other, ChainOps);
1275 }
1276 if (!ChainCheck)
1277 return false;
1278
1279 return true;
1280}
1281
1282// Change a chain of {load; op; store} of the same value into a simple op
1283// through memory of that value, if the uses of the modified value and its
1284// address are suitable.
1285//
1286// The tablegen pattern memory operand pattern is currently not able to match
1287// the case where the CC on the original operation are used.
1288//
1289// See the equivalent routine in X86ISelDAGToDAG for further comments.
1290bool SystemZDAGToDAGISel::tryFoldLoadStoreIntoMemOperand(SDNode *Node) {
1291 StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
1292 SDValue StoredVal = StoreNode->getOperand(1);
1293 unsigned Opc = StoredVal->getOpcode();
1294 SDLoc DL(StoreNode);
1295
1296 // Before we try to select anything, make sure this is memory operand size
1297 // and opcode we can handle. Note that this must match the code below that
1298 // actually lowers the opcodes.
1299 EVT MemVT = StoreNode->getMemoryVT();
1300 unsigned NewOpc = 0;
1301 bool NegateOperand = false;
1302 switch (Opc) {
1303 default:
1304 return false;
1305 case SystemZISD::SSUBO:
1306 NegateOperand = true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001307 LLVM_FALLTHROUGH;
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001308 case SystemZISD::SADDO:
1309 if (MemVT == MVT::i32)
1310 NewOpc = SystemZ::ASI;
1311 else if (MemVT == MVT::i64)
1312 NewOpc = SystemZ::AGSI;
1313 else
1314 return false;
1315 break;
1316 case SystemZISD::USUBO:
1317 NegateOperand = true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001318 LLVM_FALLTHROUGH;
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001319 case SystemZISD::UADDO:
1320 if (MemVT == MVT::i32)
1321 NewOpc = SystemZ::ALSI;
1322 else if (MemVT == MVT::i64)
1323 NewOpc = SystemZ::ALGSI;
1324 else
1325 return false;
1326 break;
1327 }
1328
1329 LoadSDNode *LoadNode = nullptr;
1330 SDValue InputChain;
1331 if (!isFusableLoadOpStorePattern(StoreNode, StoredVal, CurDAG, LoadNode,
1332 InputChain))
1333 return false;
1334
1335 SDValue Operand = StoredVal.getOperand(1);
1336 auto *OperandC = dyn_cast<ConstantSDNode>(Operand);
1337 if (!OperandC)
1338 return false;
1339 auto OperandV = OperandC->getAPIntValue();
1340 if (NegateOperand)
1341 OperandV = -OperandV;
1342 if (OperandV.getMinSignedBits() > 8)
1343 return false;
1344 Operand = CurDAG->getTargetConstant(OperandV, DL, MemVT);
1345
1346 SDValue Base, Disp;
1347 if (!selectBDAddr20Only(StoreNode->getBasePtr(), Base, Disp))
1348 return false;
1349
1350 SDValue Ops[] = { Base, Disp, Operand, InputChain };
1351 MachineSDNode *Result =
1352 CurDAG->getMachineNode(NewOpc, DL, MVT::i32, MVT::Other, Ops);
Chandler Carruth66654b72018-08-14 23:30:32 +00001353 CurDAG->setNodeMemRefs(
1354 Result, {StoreNode->getMemOperand(), LoadNode->getMemOperand()});
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001355
1356 ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
1357 ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
1358 CurDAG->RemoveDeadNode(Node);
1359 return true;
1360}
1361
Richard Sandiford067817e2013-09-27 15:29:20 +00001362bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1363 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001364 // Check that the two memory operands have the same size.
1365 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001366 return false;
1367
Richard Sandiford178273a2013-09-05 10:36:45 +00001368 // Volatility stops an access from being decomposed.
1369 if (Load->isVolatile() || Store->isVolatile())
1370 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001371
1372 // There's no chance of overlap if the load is invariant.
Justin Lebaradbf09e2016-09-11 01:38:58 +00001373 if (Load->isInvariant() && Load->isDereferenceable())
Richard Sandiford97846492013-07-09 09:46:39 +00001374 return true;
1375
Richard Sandiford97846492013-07-09 09:46:39 +00001376 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001377 const Value *V1 = Load->getMemOperand()->getValue();
1378 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001379 if (!V1 || !V2)
1380 return false;
1381
Richard Sandiford067817e2013-09-27 15:29:20 +00001382 // Reject equality.
1383 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001384 int64_t End1 = Load->getSrcValueOffset() + Size;
1385 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001386 if (V1 == V2 && End1 == End2)
1387 return false;
1388
Chandler Carruthac80dc72015-06-17 07:18:54 +00001389 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1390 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001391}
1392
Richard Sandiford178273a2013-09-05 10:36:45 +00001393bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001394 auto *Store = cast<StoreSDNode>(N);
1395 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001396
1397 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1398 // instructions.
1399 uint64_t Size = Load->getMemoryVT().getStoreSize();
1400 if (Size > 1 && Size <= 8) {
1401 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001402 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001403 return false;
1404 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001405 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001406 return false;
1407 }
1408
Richard Sandiford067817e2013-09-27 15:29:20 +00001409 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001410}
1411
1412bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1413 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001414 auto *StoreA = cast<StoreSDNode>(N);
1415 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1416 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001417 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001418}
1419
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001420void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001421 // If we have a custom node, we already have selected!
1422 if (Node->isMachineOpcode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001423 LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001424 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001425 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001426 }
1427
1428 unsigned Opcode = Node->getOpcode();
1429 switch (Opcode) {
1430 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001431 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001432 if (tryRxSBG(Node, SystemZ::ROSBG))
1433 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001434 goto or_xor;
1435
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001436 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001437 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001438 if (tryRxSBG(Node, SystemZ::RXSBG))
1439 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001440 // Fall through.
1441 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001442 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
Ulrich Weigand5f4373a2017-11-14 20:00:34 +00001443 // split the operation into two. If both operands here happen to be
1444 // constant, leave this to common code to optimize.
1445 if (Node->getValueType(0) == MVT::i64 &&
1446 Node->getOperand(0).getOpcode() != ISD::Constant)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001447 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001448 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001449 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1450 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1451 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001452 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001453 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001454 }
1455 break;
1456
Richard Sandiford84f54a32013-07-11 08:59:12 +00001457 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001458 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001459 if (tryRxSBG(Node, SystemZ::RNSBG))
1460 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001461 LLVM_FALLTHROUGH;
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001462 case ISD::ROTL:
1463 case ISD::SHL:
1464 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001465 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001466 if (tryRISBGZero(Node))
1467 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001468 break;
1469
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001470 case ISD::Constant:
1471 // If this is a 64-bit constant that is out of the range of LLILF,
1472 // LLIHF and LGFI, split it into two 32-bit pieces.
1473 if (Node->getValueType(0) == MVT::i64) {
1474 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001475 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1476 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1477 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001478 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001479 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001480 }
1481 break;
1482
Richard Sandifordee834382013-07-31 12:38:08 +00001483 case SystemZISD::SELECT_CCMASK: {
1484 SDValue Op0 = Node->getOperand(0);
1485 SDValue Op1 = Node->getOperand(1);
1486 // Prefer to put any load first, so that it can be matched as a
Ulrich Weigand524f2762016-11-28 13:34:08 +00001487 // conditional load. Likewise for constants in range for LOCHI.
1488 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1489 (Subtarget->hasLoadStoreOnCond2() &&
1490 Node->getValueType(0).isInteger() &&
1491 Op1.getOpcode() == ISD::Constant &&
1492 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1493 !(Op0.getOpcode() == ISD::Constant &&
1494 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
Richard Sandifordee834382013-07-31 12:38:08 +00001495 SDValue CCValid = Node->getOperand(2);
1496 SDValue CCMask = Node->getOperand(3);
1497 uint64_t ConstCCValid =
1498 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1499 uint64_t ConstCCMask =
1500 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1501 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001502 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001503 CCMask.getValueType());
1504 SDValue Op4 = Node->getOperand(4);
Jonas Paulssonde54c0582018-05-18 11:54:04 +00001505 SDNode *UpdatedNode =
1506 CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1507 if (UpdatedNode != Node) {
Jonas Paulssonb51ccaf2018-05-18 12:07:16 +00001508 // In case this node already exists then replace Node with it.
Jonas Paulssonde54c0582018-05-18 11:54:04 +00001509 ReplaceNode(Node, UpdatedNode);
1510 Node = UpdatedNode;
1511 }
Richard Sandifordee834382013-07-31 12:38:08 +00001512 }
1513 break;
1514 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001515
1516 case ISD::INSERT_VECTOR_ELT: {
1517 EVT VT = Node->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001518 unsigned ElemBitSize = VT.getScalarSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001519 if (ElemBitSize == 32) {
1520 if (tryGather(Node, SystemZ::VGEF))
1521 return;
1522 } else if (ElemBitSize == 64) {
1523 if (tryGather(Node, SystemZ::VGEG))
1524 return;
1525 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001526 break;
1527 }
1528
1529 case ISD::STORE: {
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001530 if (tryFoldLoadStoreIntoMemOperand(Node))
1531 return;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001532 auto *Store = cast<StoreSDNode>(Node);
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001533 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001534 if (ElemBitSize == 32) {
1535 if (tryScatter(Store, SystemZ::VSCEF))
1536 return;
1537 } else if (ElemBitSize == 64) {
1538 if (tryScatter(Store, SystemZ::VSCEG))
1539 return;
1540 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001541 break;
1542 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001543 }
1544
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001545 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001546}
1547
1548bool SystemZDAGToDAGISel::
1549SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001550 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001551 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001552 SystemZAddressingMode::AddrForm Form;
1553 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001554 SDValue Base, Disp, Index;
1555
Daniel Sanders2eeace22015-03-17 16:16:14 +00001556 switch(ConstraintID) {
1557 default:
1558 llvm_unreachable("Unexpected asm memory constraint");
1559 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001560 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001561 // Accept an address with a short displacement, but no index.
1562 Form = SystemZAddressingMode::FormBD;
1563 DispRange = SystemZAddressingMode::Disp12Only;
1564 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001565 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001566 // Accept an address with a short displacement and an index.
1567 Form = SystemZAddressingMode::FormBDXNormal;
1568 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001569 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001570 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001571 // Accept an address with a long displacement, but no index.
1572 Form = SystemZAddressingMode::FormBD;
1573 DispRange = SystemZAddressingMode::Disp20Only;
1574 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001575 case InlineAsm::Constraint_T:
1576 case InlineAsm::Constraint_m:
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001577 case InlineAsm::Constraint_o:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001578 // Accept an address with a long displacement and an index.
1579 // m works the same as T, as this is the most general case.
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001580 // We don't really have any special handling of "offsettable"
1581 // memory addresses, so just treat o the same as m.
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001582 Form = SystemZAddressingMode::FormBDXNormal;
1583 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001584 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001585 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001586
1587 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
Zhan Jun Liaucf2f4b32016-08-18 21:44:15 +00001588 const TargetRegisterClass *TRC =
1589 Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1590 SDLoc DL(Base);
1591 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1592
1593 // Make sure that the base address doesn't go into %r0.
1594 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1595 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1596 Base.getOpcode() != ISD::Register) {
1597 Base =
1598 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1599 DL, Base.getValueType(),
1600 Base, RC), 0);
1601 }
1602
1603 // Make sure that the index register isn't assigned to %r0 either.
1604 if (Index.getOpcode() != ISD::Register) {
1605 Index =
1606 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1607 DL, Index.getValueType(),
1608 Index, RC), 0);
1609 }
1610
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001611 OutOps.push_back(Base);
1612 OutOps.push_back(Disp);
1613 OutOps.push_back(Index);
1614 return false;
1615 }
1616
Daniel Sanders2eeace22015-03-17 16:16:14 +00001617 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001618}
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001619
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001620// IsProfitableToFold - Returns true if is profitable to fold the specific
1621// operand node N of U during instruction selection that starts at Root.
1622bool
1623SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1624 SDNode *Root) const {
1625 // We want to avoid folding a LOAD into an ICMP node if as a result
1626 // we would be forced to spill the condition code into a GPR.
1627 if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) {
1628 if (!N.hasOneUse() || !U->hasOneUse())
1629 return false;
1630
1631 // The user of the CC value will usually be a CopyToReg into the
1632 // physical CC register, which in turn is glued and chained to the
1633 // actual instruction that uses the CC value. Bail out if we have
1634 // anything else than that.
1635 SDNode *CCUser = *U->use_begin();
1636 SDNode *CCRegUser = nullptr;
1637 if (CCUser->getOpcode() == ISD::CopyToReg ||
1638 cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) {
1639 for (auto *U : CCUser->uses()) {
1640 if (CCRegUser == nullptr)
1641 CCRegUser = U;
1642 else if (CCRegUser != U)
1643 return false;
1644 }
1645 }
1646 if (CCRegUser == nullptr)
1647 return false;
1648
1649 // If the actual instruction is a branch, the only thing that remains to be
1650 // checked is whether the CCUser chain is a predecessor of the load.
1651 if (CCRegUser->isMachineOpcode() &&
1652 CCRegUser->getMachineOpcode() == SystemZ::BRC)
1653 return !N->isPredecessorOf(CCUser->getOperand(0).getNode());
1654
1655 // Otherwise, the instruction may have multiple operands, and we need to
1656 // verify that none of them are a predecessor of the load. This is exactly
1657 // the same check that would be done by common code if the CC setter were
1658 // glued to the CC user, so simply invoke that check here.
1659 if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false))
1660 return false;
1661 }
1662
1663 return true;
1664}
1665
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001666namespace {
1667// Represents a sequence for extracting a 0/1 value from an IPM result:
1668// (((X ^ XORValue) + AddValue) >> Bit)
1669struct IPMConversion {
1670 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1671 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1672
1673 int64_t XORValue;
1674 int64_t AddValue;
1675 unsigned Bit;
1676};
1677} // end anonymous namespace
1678
1679// Return a sequence for getting a 1 from an IPM result when CC has a
1680// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1681// The handling of CC values outside CCValid doesn't matter.
1682static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1683 // Deal with cases where the result can be taken directly from a bit
1684 // of the IPM result.
1685 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1686 return IPMConversion(0, 0, SystemZ::IPM_CC);
1687 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1688 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1689
1690 // Deal with cases where we can add a value to force the sign bit
1691 // to contain the right value. Putting the bit in 31 means we can
1692 // use SRL rather than RISBG(L), and also makes it easier to get a
1693 // 0/-1 value, so it has priority over the other tests below.
1694 //
1695 // These sequences rely on the fact that the upper two bits of the
1696 // IPM result are zero.
1697 uint64_t TopBit = uint64_t(1) << 31;
1698 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1699 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1700 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1701 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1702 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1703 | SystemZ::CCMASK_1
1704 | SystemZ::CCMASK_2)))
1705 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1706 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1707 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1708 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1709 | SystemZ::CCMASK_2
1710 | SystemZ::CCMASK_3)))
1711 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1712
1713 // Next try inverting the value and testing a bit. 0/1 could be
1714 // handled this way too, but we dealt with that case above.
1715 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1716 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1717
1718 // Handle cases where adding a value forces a non-sign bit to contain
1719 // the right value.
1720 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1721 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1722 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1723 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1724
1725 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
1726 // can be done by inverting the low CC bit and applying one of the
1727 // sign-based extractions above.
1728 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1729 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1730 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1731 return IPMConversion(1 << SystemZ::IPM_CC,
1732 TopBit - (3 << SystemZ::IPM_CC), 31);
1733 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1734 | SystemZ::CCMASK_1
1735 | SystemZ::CCMASK_3)))
1736 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1737 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1738 | SystemZ::CCMASK_2
1739 | SystemZ::CCMASK_3)))
1740 return IPMConversion(1 << SystemZ::IPM_CC,
1741 TopBit - (1 << SystemZ::IPM_CC), 31);
1742
1743 llvm_unreachable("Unexpected CC combination");
1744}
1745
1746SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
1747 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
1748 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1749 if (!TrueOp || !FalseOp)
1750 return SDValue();
1751 if (FalseOp->getZExtValue() != 0)
1752 return SDValue();
1753 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
1754 return SDValue();
1755
1756 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1757 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
1758 if (!CCValidOp || !CCMaskOp)
1759 return SDValue();
1760 int CCValid = CCValidOp->getZExtValue();
1761 int CCMask = CCMaskOp->getZExtValue();
1762
1763 SDLoc DL(Node);
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001764 SDValue CCReg = Node->getOperand(4);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001765 IPMConversion IPM = getIPMConversion(CCValid, CCMask);
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001766 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001767
1768 if (IPM.XORValue)
1769 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
1770 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
1771
1772 if (IPM.AddValue)
1773 Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
1774 CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
1775
1776 EVT VT = Node->getValueType(0);
1777 if (VT == MVT::i32 && IPM.Bit == 31) {
1778 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
1779 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
1780 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1781 } else {
1782 if (VT != MVT::i32)
1783 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
1784
1785 if (TrueOp->getSExtValue() == 1) {
1786 // The SHR/AND sequence should get optimized to an RISBG.
1787 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
1788 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1789 Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
1790 CurDAG->getConstant(1, DL, VT));
1791 } else {
1792 // Sign-extend from IPM.Bit using a pair of shifts.
1793 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
1794 int SraAmt = VT.getSizeInBits() - 1;
1795 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
1796 CurDAG->getConstant(ShlAmt, DL, MVT::i32));
1797 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
1798 CurDAG->getConstant(SraAmt, DL, MVT::i32));
1799 }
1800 }
1801
1802 return Result;
1803}
1804
1805void SystemZDAGToDAGISel::PreprocessISelDAG() {
Ulrich Weigand426f6be2018-01-19 20:56:04 +00001806 // If we have conditional immediate loads, we always prefer
1807 // using those over an IPM sequence.
1808 if (Subtarget->hasLoadStoreOnCond2())
1809 return;
1810
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001811 bool MadeChange = false;
1812
1813 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1814 E = CurDAG->allnodes_end();
1815 I != E;) {
1816 SDNode *N = &*I++;
1817 if (N->use_empty())
1818 continue;
1819
1820 SDValue Res;
1821 switch (N->getOpcode()) {
1822 default: break;
1823 case SystemZISD::SELECT_CCMASK:
1824 Res = expandSelectBoolean(N);
1825 break;
1826 }
1827
1828 if (Res) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001829 LLVM_DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: ");
1830 LLVM_DEBUG(N->dump(CurDAG));
1831 LLVM_DEBUG(dbgs() << "\nNew: ");
1832 LLVM_DEBUG(Res.getNode()->dump(CurDAG));
1833 LLVM_DEBUG(dbgs() << "\n");
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001834
1835 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
1836 MadeChange = true;
1837 }
1838 }
1839
1840 if (MadeChange)
1841 CurDAG->RemoveDeadNodes();
1842}